Debian Codegraph @

protolib/src/bsd/bsdNet.cpp

raw
1
2 // This file contains BSD (and MacOS) specific implementations of
3 // ProtoNet features.
4
5 // Note that the remainder of the ProtoNet stuff is
6 // implemented in the "src/unix/unixNet.cpp" file
7 // in the Protolib source tree and the common stuff is
8 // in "src/common/protoNet.cpp"
9
10 #include "protoNet.h"
11 #include "protoDebug.h"
12 #include "protoTree.h"
13
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <ifaddrs.h>
17 #include <string.h>
18 #include <net/if_dl.h>
19
20 #include <unistd.h> // for close()
21 #include <stdio.h> // for sprintf()
22 #ifdef MACOSX
23 #include <sys/kern_event.h> // // for kernel event stuff
24 #endif
25 #include <sys/ioctl.h> // for ioctl()
26 #include <net/if.h> // for KEV_DL_SUBCLASS, etc
27 #include <net/if_var.h> // needed for freebsd
28 #include <netinet/in.h> // for KEV_INET_SUBCLASS, etc
29 #include <netinet/in_var.h> // for KEV_INET_SUBCLASS, etc
30 #include <netinet6/in6_var.h> // for KEV_INET6_SUBCLASS, etc
31
32 bool ProtoNet::GetGroupMemberships(const char* ifaceName, ProtoAddress::Type addrType, ProtoAddressList& addrList)
33 {
34 int family;
35 switch (addrType)
36 {
37 case ProtoAddress::IPv4:
38 family = AF_INET;
39 break;
40 #ifdef HAVE_IPV6
41 case ProtoAddress::IPv6:
42 family = AF_INET6;
43 break;
44 #endif // HAVE_IPV6
45 default:
46 PLOG(PL_ERROR, "UnixNet::GetInterfaceName() error: invalid address type\n");
47 return 0;
48 }
49 // use getifmaddrs() to fetch memberships for given interface
50 struct ifmaddrs* ifmap;
51 if (0 == getifmaddrs(&ifmap))
52 {
53 // Look for addrType address for given "interfaceName"
54 struct ifmaddrs* ptr = ifmap;
55 while (NULL != ptr)
56 {
57 if ((NULL != ptr->ifma_name) &&
58 (NULL != ptr->ifma_addr) &&
59 (family == ptr->ifma_addr->sa_family))
60 {
61 if (ptr->ifma_name->sa_family != AF_LINK)
62 {
63 PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: invalid interface data from kernel!\n");
64 freeifmaddrs(ifmap);
65 return false;
66 }
67 // This union hack avoids cast-align warning
68 typedef struct SockAddrPtr
69 {
70 union
71 {
72 struct sockaddr* sa;
73 struct sockaddr_dl* sd;
74 };
75 } SockAddrPtr;
76 SockAddrPtr sap;
77 sap.sa = ptr->ifma_name;
78 char ifName[64];
79 unsigned int nameLen = sap.sd->sdl_nlen;
80 if (nameLen > 63) nameLen = 63;
81 strncpy(ifName, sap.sd->sdl_data, nameLen);
82 ifName[nameLen] = '\0';
83 if (0 == strcmp(ifaceName, ifName))
84 {
85 ProtoAddress groupAddr;
86 if (!groupAddr.SetSockAddr(*(ptr->ifma_addr)))
87 {
88 PLOG(PL_WARN, "ProtoNet::GetGroupMemberships() error: invalid address family\n");
89 ptr = ptr->ifma_next;
90 continue;
91 }
92 if (!addrList.Insert(groupAddr))
93 {
94 PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() error: unable to add address to list!\n");
95 freeifmaddrs(ifmap);
96 return false;
97 }
98 }
99 }
100 ptr = ptr->ifma_next;
101 }
102 if (NULL != ifmap)
103 freeifmaddrs(ifmap);
104 return true;
105 }
106 else
107 {
108 PLOG(PL_ERROR, "ProtoNet::GetGroupMemberships() getifmaddrs() error: %s\n", GetErrorString());
109 return false;
110 }
111 } // end ProtoNet::GetGroupMemberships()
112
113 // IMPORTANT - The BsdNetMonitor implementation here is currently
114 // specific to Mac OSX. TBD - Provide implementation that
115 // works on other BSD platforms, too.
116 #ifdef MACOSX
117
118 class BsdNetMonitor : public ProtoNet::Monitor
119 {
120 public:
121 BsdNetMonitor();
122 ~BsdNetMonitor();
123
124 bool Open();
125 void Close();
126 bool GetNextEvent(Event& theEvent);
127
128 private:
129 // We keep a list of "up" interfaces that is populated upon "Open()"
130 // and modified as interfaces go up and down
131
132
133 class Interface : public ProtoTree::Item
134 {
135 public:
136 Interface(const char* name, unsigned int index);
137 ~Interface();
138
139 const char* GetName() const
140 {return iface_name;}
141 unsigned int GetIndex() const
142 {return iface_index;}
143 void SetIndex(unsigned int index)
144 {iface_index = index;}
145
146 private:
147 const char* GetKey() const
148 {return iface_name;}
149 unsigned int GetKeysize() const
150 {return iface_name_bits;}
151
152 char iface_name[IFNAMSIZ+1];
153 unsigned int iface_name_bits;
154 unsigned int iface_index;
155 }; // end class BsdNetMonitor::Interface
156 class InterfaceList : public ProtoTreeTemplate<Interface> {};
157
158 InterfaceList iface_list;
159 u_int32_t* msg_buffer;
160 u_int32_t msg_buffer_size;
161
162 }; // end class BsdNetMonitor
163
164 BsdNetMonitor::Interface::Interface(const char* name, unsigned int index)
165 : iface_index(index)
166 {
167 iface_name[IFNAMSIZ] = '\0';
168 strncpy(iface_name, name, IFNAMSIZ);
169 iface_name_bits = (unsigned int)strlen(iface_name) << 3;
170 }
171
172 BsdNetMonitor::Interface::~Interface()
173 {
174 }
175
176
177 // This is the implementation of the ProtoNet::Monitor::Create()
178 // static method (our BSD-specific factory)
179 ProtoNet::Monitor* ProtoNet::Monitor::Create()
180 {
181 return static_cast<ProtoNet::Monitor*>(new BsdNetMonitor);
182 } // end ProtoNet::Monitor::Create()
183
184 BsdNetMonitor::BsdNetMonitor()
185 : msg_buffer(NULL), msg_buffer_size(0)
186 {
187 }
188
189 BsdNetMonitor::~BsdNetMonitor()
190 {
191 Close();
192 }
193
194 bool BsdNetMonitor::Open()
195 {
196 if (IsOpen()) Close();
197 if (0 > (descriptor = socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT)))
198 {
199 PLOG(PL_ERROR, "BsdNetMonitor::Open() socket() error: %s\n", GetErrorString());
200 return false;
201 }
202 // NOTE: This stuff here is currently MACOS only!!!
203 // (TBD - implement BSD equivalent)
204 // Call ioctl(SIOCSKEVFILT) to monitor for network events
205 struct kev_request req;
206 req.vendor_code = KEV_VENDOR_APPLE;
207 req.kev_class = KEV_NETWORK_CLASS;
208 req.kev_subclass = KEV_ANY_SUBCLASS; //KEV_DL_SUBCLASS;
209 int result = ioctl(descriptor, SIOCSKEVFILT, (void*)&req);
210 if (0 != result)
211 {
212 PLOG(PL_ERROR, "BsdNetMonitor::Open() ioctl(SIOSKEVFILT) error");
213 Close();
214 return false;
215 }
216 if (!ProtoNet::Monitor::Open())
217 {
218 Close();
219 return false;
220 }
221
222 // Populate our iface_list with "up" interfaces
223 unsigned int ifaceCount = ProtoNet::GetInterfaceCount();
224 if (ifaceCount > 0)
225 {
226 unsigned int* indexArray = new unsigned int[ifaceCount];
227 if (NULL == indexArray)
228 {
229 PLOG(PL_ERROR, "BsdNetMonitor::Open() new indexArray[%u] error: %s\n", ifaceCount, GetErrorString());
230 Close();
231 return false;
232 }
233 if (ProtoNet::GetInterfaceIndices(indexArray, ifaceCount) != ifaceCount)
234 {
235 PLOG(PL_ERROR, "BsdNetMonitor::Open() GetInterfaceIndices() error?!\n");
236 delete[] indexArray;
237 Close();
238 return false;
239 }
240 for (unsigned int i = 0; i < ifaceCount; i++)
241 {
242 char ifaceName[IFNAMSIZ+1];
243 ifaceName[IFNAMSIZ] = '\0';
244 if (!ProtoNet::GetInterfaceName(indexArray[i], ifaceName, IFNAMSIZ))
245 {
246 PLOG(PL_ERROR, "BsdNetMonitor::Open() error: unable to get interface name for index: %u\n", indexArray[i]);
247 delete[] indexArray;
248 Close();
249 return false;
250 }
251 Interface* iface = iface_list.FindString(ifaceName);
252 if (NULL != iface)
253 {
254 PLOG(PL_ERROR, "BsdNetMonitor::Open() warning: interface \"%s\" index:%u already listed as index:%u\n",
255 ifaceName, indexArray[i], iface->GetIndex());
256 }
257 else if (NULL == (iface = new Interface(ifaceName, indexArray[i])))
258 {
259 PLOG(PL_ERROR, "BsdNetMonitor::Open() new Interface[ error: %s\n", GetErrorString());
260 Close();
261 return false;
262 }
263 iface_list.Insert(*iface);
264 }
265 }
266 return true;
267 } // end BsdNetMonitor::Open()
268
269 void BsdNetMonitor::Close()
270 {
271 iface_list.Destroy();
272 if (IsOpen())
273 {
274 ProtoNet::Monitor::Close();
275 close(descriptor);
276 descriptor = INVALID_HANDLE;
277 }
278 if (NULL != msg_buffer)
279 {
280 delete[] msg_buffer;
281 msg_buffer_size = 0;
282 msg_buffer = NULL;
283 }
284 } // end BsdNetMonitor::Open()
285
286 bool BsdNetMonitor::GetNextEvent(Event& theEvent)
287 {
288 // 0) Initialize event instance
289 theEvent.SetType(Event::UNKNOWN_EVENT);
290 theEvent.SetInterfaceIndex(0);
291 theEvent.AccessAddress().Invalidate();
292
293 // 1) "peek" at the kern_event msg header to see how
294 // big the message data buffer needs to be.
295 struct kern_event_msg tmpMsg;
296 ssize_t result = recv(descriptor, (void*)&tmpMsg, sizeof(tmpMsg), MSG_PEEK);
297 if (result < 0)
298 {
299 switch(errno)
300 {
301 case EINTR:
302 case EAGAIN:
303 theEvent.SetType(Event::NULL_EVENT);
304 return true;
305 default:
306 PLOG(PL_ERROR, "BsdNetMonitor::GetNextEvent() recv(PEEK) error: %s", GetErrorString());
307 return false;
308 }
309 }
310 // 2) alloc/realloc "msgBuffer" as needed and "recv()" message
311 if (tmpMsg.total_size > msg_buffer_size)
312 {
313 if (NULL != msg_buffer) delete[] msg_buffer;
314 if (NULL == (msg_buffer = new u_int32_t[tmpMsg.total_size / sizeof(u_int32_t)]))
315 {
316 PLOG(PL_ERROR, "BsdNetMonitor::GetNextEvent() new msg_buffer[tmpMsg.total_size / sizeof(u_int32_t)] error: %s\n",
317 tmpMsg.total_size / sizeof(u_int32_t), GetErrorString());
318 msg_buffer_size = 0;
319 return false;
320 }
321 msg_buffer_size = tmpMsg.total_size;
322 }
323 result = recv(descriptor, (void*)msg_buffer, msg_buffer_size, 0);
324 if (result < 0)
325 {
326 switch(errno)
327 {
328 case EINTR:
329 case EAGAIN:
330 theEvent.SetType(Event::NULL_EVENT);
331 return true;
332 default:
333 PLOG(PL_ERROR, "BsdNetMonitor::GetNextEvent() recv() error: %s", GetErrorString());
334 return false;
335 }
336 }
337 // 3) Parse the received kernel event message to see what happened
338 struct kern_event_msg* kmsg = (struct kern_event_msg*)msg_buffer;
339 switch (kmsg->kev_subclass)
340 {
341 case KEV_DL_SUBCLASS:
342 {
343 // The events we handle use "struct net_event_data" for the "event_data" portion
344 struct net_event_data* dat = (struct net_event_data*)kmsg->event_data;
345 switch (kmsg->event_code)
346 {
347 case KEV_DL_LINK_ON:
348 theEvent.SetType(Event::IFACE_UP);
349 break;
350 case KEV_DL_LINK_OFF:
351 theEvent.SetType(Event::IFACE_DOWN);
352 break;
353 case KEV_DL_SIFFLAGS: // iface flags have changed (addr assigned, etc?)
354 theEvent.SetType(Event::IFACE_STATE);
355 break;
356 default:
357 PLOG(PL_INFO, "BsdNetMonitor::GetNextEvent() warning: unhandled iface network event:%d\n", kmsg->event_code);
358 break;
359 }
360 // If it was a supported event, fill in ifaceIndex and ifaceName
361 //if (Event::UNKNOWN_EVENT != theEvent.GetType())
362 {
363 // Do we already know this interface?
364 char ifName[IFNAMSIZ+1];
365 ifName[IFNAMSIZ] = '\0';
366 sprintf(ifName, "%s%d", dat->if_name, dat->if_unit);
367 theEvent.SetInterfaceName(ifName);
368 Interface* iface =iface_list.FindString(ifName);
369 // Fetch the system index in case it's not consistent
370 unsigned int ifIndex = ProtoNet::GetInterfaceIndex(ifName);
371 if (0 == ifIndex)
372 {
373 if (NULL != iface)
374 {
375 // If a known iface has lost its index, assume it's gone DOWN?
376 if (Event::IFACE_DOWN != theEvent.GetType())
377 {
378 PLOG(PL_INFO, "BsdNetMonitor::GetNextEvent() known interface has lost it's index (assuming IFACE_DOWN)\n");
379 theEvent.SetType(Event::IFACE_DOWN);
380 }
381 ifIndex = iface->GetIndex();
382 }
383 else
384 {
385 // Unknown interface with no index, so ignore
386 PLOG(PL_INFO, "BsdNetMonitor::GetNextEvent() unable to get index for iface \"%s\"\n", ifName);
387 return GetNextEvent(theEvent);
388 }
389 }
390 else
391 {
392 if (NULL != iface)
393 {
394 if (ifIndex != iface->GetIndex())
395 {
396 PLOG(PL_ERROR, "BsdNetMonitor::GetNextEvent() warning: index changed for known interface \"%s\"\n",ifName);
397 iface->SetIndex(ifIndex);
398 }
399 }
400 else if (Event::IFACE_DOWN != theEvent.GetType() && (ProtoNet::IFACE_UP == ProtoNet::GetInterfaceStatus(ifName)))
401 {
402 if (NULL == (iface = new Interface(ifName, ifIndex)))
403 {
404 PLOG(PL_ERROR, "BsdNetMonitor::Open() new Interface[ error: %s\n", GetErrorString());
405 }
406 else
407 {
408 if (Event::IFACE_UP != theEvent.GetType())
409 {
410 PLOG(PL_INFO, "BsdNetMonitor::GetNextEvent() event for unknown interface (assuming IFACE_UP)\n");
411 theEvent.SetType(Event::IFACE_UP);
412 }
413 TRACE("Inserting iface \"%s\"\n", ifName);
414 iface_list.Insert(*iface);
415 }
416 }
417 }
418 if (Event::IFACE_STATE == theEvent.GetType())
419 {
420 // See if state changed from UP to DOWN or vice versa
421 if (ProtoNet::IFACE_UP != ProtoNet::GetInterfaceStatus(ifName) && (NULL != iface))
422 {
423 PLOG(PL_INFO, "BsdNetMonitor::GetNextEvent() status event for known, but DOWN, interface (assuming IFACE_DOWN)\n");
424 theEvent.SetType(Event::IFACE_DOWN);
425 }
426 }
427
428 if ((Event::IFACE_DOWN == theEvent.GetType()) && (NULL != iface))
429 {
430 // Remove "DOWN"" iface from iface_list
431 iface_list.Remove(*iface);
432 delete iface;
433 }
434 theEvent.SetInterfaceIndex(ifIndex);
435 }
436 break;
437 } // end case KEV_DL_SUBCLASS
438 case KEV_INET_SUBCLASS:
439 {
440 // The events we handle use "struct kev_in_data" for the "event_data" portion
441 struct kev_in_data* dat = (struct kev_in_data*)kmsg->event_data;
442 switch (kmsg->event_code)
443 {
444 case KEV_INET_NEW_ADDR:
445 //case KEV_INET_CHANGED_ADDR:
446 theEvent.SetType(Event::IFACE_ADDR_NEW);
447 break;
448 case KEV_INET_ADDR_DELETED:
449 theEvent.SetType(Event::IFACE_ADDR_DELETE);
450 break;
451 default:
452 PLOG(PL_INFO, "BsdNetMonitor::GetNextEvent() warning: unhandled ipv4 network event:%d\n", kmsg->event_code);
453 break;
454 }
455 // If it was a supported event, fill in ifaceIndex and ifaceAddress
456 //if (Event::UNKNOWN_EVENT != theEvent.GetType())
457 {
458
459 char ifName[IFNAMSIZ+1];
460 ifName[IFNAMSIZ] = '\0';
461 sprintf(ifName, "%s%d", dat->link_data.if_name, dat->link_data.if_unit);
462 theEvent.SetInterfaceName(ifName);
463 unsigned int ifIndex = ProtoNet::GetInterfaceIndex(ifName);
464 if (0 == ifIndex)
465 {
466 PLOG(PL_ERROR, "BsdNetMonitor::GetNextEvent() unable to get index for ip4 iface \"%s\"\n", ifName);
467 return GetNextEvent(theEvent);
468 }
469 theEvent.SetInterfaceIndex(ifIndex);
470 theEvent.AccessAddress().SetRawHostAddress(ProtoAddress::IPv4, (char*)&(dat->ia_addr), 4);
471 // TBD - for consistency with Linux, etc we may want to ignore Link Local addr new/delete events???
472 }
473 break;
474 } // end KEV_INET_SUBCLASS
475 case KEV_INET6_SUBCLASS:
476 {
477 // The events we handle use "struct kev_in6_data" for the "event_data" portion
478 struct kev_in6_data* dat = (struct kev_in6_data*)kmsg->event_data;
479 switch (kmsg->event_code)
480 {
481 case KEV_INET6_NEW_USER_ADDR:
482 case KEV_INET6_NEW_LL_ADDR:
483 case KEV_INET6_NEW_RTADV_ADDR:
484 case KEV_INET6_CHANGED_ADDR:
485 theEvent.SetType(Event::IFACE_ADDR_NEW);
486 break;
487 case KEV_INET6_ADDR_DELETED:
488 theEvent.SetType(Event::IFACE_ADDR_DELETE);
489 break;
490 default:
491 PLOG(PL_INFO, "BsdNetMonitor::GetNextEvent() warning: unhandled ipv6 network event:%d\n", kmsg->event_code);
492 break;
493 }
494 // If it was a supported event, fill in ifaceIndex and ifaceAddress
495 //if (Event::UNKNOWN_EVENT != theEvent.GetType())
496 {
497
498 char ifName[IFNAMSIZ+1];
499 ifName[IFNAMSIZ] = '\0';
500 sprintf(ifName, "%s%d", dat->link_data.if_name, dat->link_data.if_unit);
501 theEvent.SetInterfaceName(ifName);
502 unsigned int ifIndex = ProtoNet::GetInterfaceIndex(ifName);
503 if (0 == ifIndex)
504 {
505 PLOG(PL_ERROR, "BsdNetMonitor::GetNextEvent() unable to get index for ip6 iface \"%s\"\n", ifName);
506 return GetNextEvent(theEvent);
507 }
508 theEvent.SetInterfaceIndex(ifIndex);
509 theEvent.AccessAddress().SetRawHostAddress(ProtoAddress::IPv6, (char*)&(dat->ia_addr), 16);
510 // TBD - for consistency with Linux, etc we may want to ignore Link Local addr new/delete events???
511 }
512 break;
513 }
514 default:
515 printf("unhandled network event subclass:%d code:%d\n", kmsg->kev_subclass, kmsg->event_code);
516 break;
517 }
518 return true;
519 } // end BsdNetMonitor::GetNextEvent()
520
521 #endif // MACOSX (TBD - make this implementation work on other BSD systems, too)