Debian Codegraph @

protolib/src/bsd/bsdCap.cpp

raw
1 /**
2 * @file bsdCap.cpp
3 *
4 * @brief This is an implementation of the Protolib class ProtoCap
5 * using the BSD PF_NDRV socket for packet capture
6 *
7 */
8 /**
9 * @class BsdCap
10 *
11 * @brief This is an implementation of the Protolib class ProtoCap
12 * using the BSD PF_NDRV socket for packet capture
13 *
14 * (THIS WILL _NOT_ WORK FOR AN INTERFACE THAT HAS IP ENABLED ALREADY !!!)
15 *
16 * SO, ONE SHOULD PROBABLY USE "bpfCap.cpp" on BSD for most purposes
17 */
18
19 #include "protoCap.h" // for ProtoCap definition
20 #include "protoSocket.h"
21 #include "protoDebug.h"
22 #include "protoPktETH.h" // for Ether Type enumeration
23
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <sys/ioctl.h>
27 #include <net/bpf.h>
28 #include <sys/socket.h>
29 #include <net/if.h>
30 #include <net/dlil.h>
31 #include <net/ndrv.h>
32 #include <stdio.h> // for snprintf()
33 #include <fcntl.h> // for open()
34 #include <unistd.h> // for close()
35
36 class BsdCap : public ProtoCap
37 {
38 public:
39 BsdCap();
40 ~BsdCap();
41
42 bool Open(const char* interfaceName = NULL);
43 void Close();
44 bool Send(const char* buffer, unsigned int& numBytes);
45 bool Recv(char* buffer, unsigned int& numBytes, Direction* direction = NULL);
46 }; // end class BsdCap
47
48 /**
49 * @brief Returns a new instance of BsdCap cast to a pointer to a ProtoCap.
50 *
51 */
52
53 ProtoCap* ProtoCap::Create()
54 {
55 return (static_cast<ProtoCap*>(new BsdCap));
56 }
57
58 /**
59 * @brief Enables input notification by default.
60 *
61 */
62
63 BsdCap::BsdCap()
64 {
65 StartInputNotification(); // enable input notification by default
66 }
67
68 BsdCap::~BsdCap()
69 {
70 Close();
71 }
72 /**
73 * @brief Associates a BSD PF-NDRV socket with an interface.
74 *
75 * Associates a BSD PF_NDRV socket with the specified interface or it not provded, the
76 * default local interface will be used.
77 *
78 * @param interfaceName
79 * @return success or failure indicator
80 */
81
82
83 bool BsdCap::Open(const char* interfaceName)
84 {
85 Close(); // just in case
86 char buffer[256];
87 if (NULL == interfaceName)
88 {
89 // Try to determine a "default" interface
90 ProtoAddress localAddress;
91 if (!localAddress.ResolveLocalAddress())
92 {
93 PLOG(PL_ERROR, "BsdCap::Open() error: couldn't auto determine local interface\n");
94 return false;
95 }
96 if (!ProtoSocket::GetInterfaceName(localAddress, buffer, 256))
97 {
98 PLOG(PL_ERROR, "BsdCap::Open() error: couldn't determine local interface name\n");
99 return false;
100 }
101 interfaceName = buffer;
102 }
103
104 ProtoAddress macAddr;
105 if (!ProtoSocket::GetInterfaceAddress(interfaceName, ProtoAddress::ETH, if_addr))
106 PLOG(PL_ERROR, "BsdCap::Open() warning: unable to get MAC address for interface \"%s\"\n", interfaceName);
107
108 int ifIndex = ProtoSocket::GetInterfaceIndex(interfaceName);
109
110 // 1) Open a PF_NDRV socket
111 descriptor = socket(PF_NDRV, SOCK_RAW, 0);
112 if (descriptor < 0)
113 {
114 PLOG(PL_ERROR, "BsdCap::Open() error: socket(PF_NDRV) failure: %s\n", GetErrorString());
115 return false;
116 }
117
118 // 2) Bind to our interface name
119 struct sockaddr_ndrv sa;
120 sa.snd_len = sizeof(sa);
121 sa.snd_family = PF_NDRV;
122 strncpy((char*)sa.snd_name, interfaceName, IFNAMSIZ);
123 if (bind(descriptor, (struct sockaddr*)&sa, sa.snd_len) < 0)
124 {
125 PLOG(PL_ERROR, "BsdCap::Open() error: bind(%s) failure: %s\n", interfaceName, GetErrorString());
126 Close();
127 return false;
128 }
129
130 // 3) Register for IP, IPv6, and ARP packets
131
132 struct ndrv_demux_desc desc[3];
133 memset(desc, 0, 3*sizeof(desc));
134
135 desc[0].type = NDRV_DEMUXTYPE_ETHERTYPE;
136 desc[0].length = sizeof(unsigned short);
137 desc[0].data.ether_type = htons(ProtoPktETH::IP);
138 desc[1].type = NDRV_DEMUXTYPE_ETHERTYPE;
139 desc[1].length = sizeof(unsigned short);
140 desc[1].data.ether_type = htons(ProtoPktETH::IPv6);
141 desc[2].type = NDRV_DEMUXTYPE_ETHERTYPE;
142 desc[2].length = sizeof(unsigned short);
143 desc[2].data.ether_type = htons(ProtoPktETH::ARP);
144
145
146 struct ndrv_protocol_desc ndrvDesc;
147 ndrvDesc.version = NDRV_PROTOCOL_DESC_VERS;
148 ndrvDesc.protocol_family = PF_INET;
149 ndrvDesc.demux_count = 3;
150 ndrvDesc.demux_list = desc;
151
152 if (setsockopt(descriptor, SOL_NDRVPROTO, NDRV_SETDMXSPEC,
153 &ndrvDesc, sizeof(ndrvDesc)) < 0)
154 {
155 PLOG(PL_ERROR, "BsdCap::Open() error: setsockopt(NDRV_SETDMXSPEC) failure: %s\n",
156 GetErrorString());
157 Close();
158 return false;
159 }
160
161 if (!ProtoCap::Open(interfaceName))
162 {
163 PLOG(PL_ERROR, "BsdCap::Open() ProtoCap::Open() error\n");
164 Close();
165 return false;
166 }
167 if_index = ifIndex;
168 return true;
169 } // end BsdCap::Open()
170
171 void BsdCap::Close()
172 {
173 if (descriptor >= 0)
174 {
175 ProtoCap::Close();
176 close(descriptor);
177 descriptor = -1;
178 }
179 } // end BsdCap::Close()
180
181 /**
182 * @brief Recv's the packet into buffer.
183 *
184 * @param buffer
185 * @param numBytes
186 * @param direction
187 *
188 * @return success or failure indicator
189 */
190
191 bool BsdCap::Recv(char* buffer, unsigned int& numBytes, Direction* direction)
192 {
193 if (direction) *direction = UNSPECIFIED;
194 for (;;)
195 {
196 ssize_t result = read(descriptor, buffer, numBytes);
197 if (result < 0)
198 {
199 switch (errno)
200 {
201 case EINTR:
202 continue; // try again
203 case EWOULDBLOCK:
204 numBytes = 0;
205 return true;
206 default:
207 PLOG(PL_ERROR, "BsdCap::Recv() read() error: %s\n",
208 GetErrorString());
209 numBytes = 0;
210 return false;
211 }
212 }
213 else
214 {
215 numBytes = result;
216 break;
217 }
218 }
219 return true;
220 } // end BsdCap::Recv()
221 /**
222 * @brief Write packet to the bsd socket descriptor.
223 *
224 * 802.3 frames are not supported
225 *
226 * @param buffer
227 * @param buflen
228 *
229 * @return success or failure indicator
230 */
231
232
233 bool BsdCap::Send(const char* buffer, unsigned int& numBytes)
234 {
235 // Make sure packet is a type that is OK for us to send
236 // (Some packets seem to cause a PF_PACKET socket trouble)
237 UINT16 type;
238 memcpy(&type, buffer+12, 2);
239 type = ntohs(type);
240 if (type <= 0x05dc) // assume it's 802.3 Length and ignore
241 {
242 PLOG(PL_DEBUG, "BsdCap::Send() unsupported 802.3 frame (len = %04x)\n", type);
243 return false;
244 }
245 for (;;)
246 {
247 struct sockaddr sockAddr; // note the raw frame device here does not use the sockAddr
248 ssize_t result = sendto(descriptor, buffer+put, buflen-put, 0, &sockAddr, sizeof(sockAddr));
249 if (result < 0)
250 {
251 switch (errno)
252 {
253 case EINTR:
254 continue; // try again
255 case EWOULDBLOCK:
256 numBytes = 0;
257 case ENOBUFS:
258 // because this doesn't block write()
259 default:
260 PLOG(PL_ERROR, "BsdCap::Send() error: %s", GetErrorString());
261 break;
262 }
263 return false;
264 }
265 else
266 {
267 ASSERT(result == numBytes);
268 break;
269 }
270 }
271 return true;
272 } // end BsdCap::Send()