207671535c6eba93c1d03cd5207c07bdf3637bba
[olsrd.git] / lib / pud / src / networkInterfaces.c
1 #include "networkInterfaces.h"
2
3 /* Plugin includes */
4 #include "pud.h"
5 #include "configuration.h"
6 #include "netTools.h"
7
8 /* OLSRD includes */
9 #include "olsr.h"
10
11 /* System includes */
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <ifaddrs.h>
15
16 /*
17  * Main IP MAC address
18  */
19
20 /** the MAC address of the main IP address */
21 static unsigned char mac[PUD_NODEIDTYPE_MAC_BYTES] = { 0 };
22
23 /** true when the MAC address of the main IP address has been retrieved */
24 static bool macSet = false;
25
26 /**
27  * @return
28  * the MAC address of the main IP address
29  */
30 unsigned char * getMainIpMacAddress(void) {
31         if (!macSet) {
32                 struct ifreq ifr;
33                 unsigned char * macInIfr;
34
35                 struct interface *mainInterface = if_ifwithaddr(&olsr_cnf->main_addr);
36                 if (!mainInterface) {
37                         pudError(true, "Could not get the main interface");
38                         return NULL;
39                 }
40                 macInIfr = getHardwareAddress(mainInterface->int_name, olsr_cnf->ip_version, &ifr);
41                 if (!macInIfr) {
42                         pudError(true, "Could not get the MAC address of the main interface");
43                         return NULL;
44                 }
45                 memcpy(&mac[0], &macInIfr[0], PUD_NODEIDTYPE_MAC_BYTES);
46                 macSet = true;
47         }
48
49         return &mac[0];
50 }
51
52 /*
53  * RX interfaces
54  */
55
56 /** The list of network interface objects, receiving GPS NMEA sentences */
57 static TRxTxNetworkInterface *rxNetworkInterfacesListHead = NULL;
58
59 /** Pointer to the last network interface object, receiving GPS NMEA sentences */
60 static TRxTxNetworkInterface *lastRxNetworkInterface = NULL;
61
62 /**
63  @return
64  The list of network interface objects, receiving GPS NMEA sentences
65  */
66 TRxTxNetworkInterface *getRxNetworkInterfaces(void) {
67         return rxNetworkInterfacesListHead;
68 }
69
70 /**
71  Create a receive socket for a network interface
72
73  @param networkInterface
74  The network interface object. This function expects it to be filled with all
75  information, except for the socket descriptor.
76  @param rxSocketHandlerFunction
77  The function that handles reception of data on the network interface
78  @param rxMcAddr
79  The receive multicast address (must have same IP version as networkInterface address)
80
81  @return
82  - the socket descriptor (>= 0)
83  - -1 if an error occurred
84  */
85 static int createRxSocket(TRxTxNetworkInterface * networkInterface,
86                 socket_handler_func rxSocketHandlerFunction, union olsr_sockaddr * rxMcAddr) {
87         int ipFamilySetting;
88         int ipProtoSetting;
89         int ipMcLoopSetting;
90         int ipAddMembershipSetting;
91
92         union olsr_sockaddr address;
93         struct sockaddr * addr;
94         size_t addrSize;
95
96         int rxSocket = -1;
97
98         int socketReuseFlagValue = 1;
99         int mcLoopValue = 1;
100
101         assert(networkInterface != NULL);
102         assert(rxSocketHandlerFunction != NULL);
103         assert(strncmp((char *) &networkInterface->name[0], "",
104                                         sizeof(networkInterface->name)) != 0);
105         assert(networkInterface->ipAddress.in.sa_family == rxMcAddr->in.sa_family);
106
107         memset(&address, 0, sizeof(address));
108         if (networkInterface->ipAddress.in.sa_family == AF_INET) {
109                 assert(networkInterface->ipAddress.in4.sin_addr.s_addr != INADDR_ANY);
110
111                 ipFamilySetting = AF_INET;
112                 ipProtoSetting = IPPROTO_IP;
113                 ipMcLoopSetting = IP_MULTICAST_LOOP;
114                 ipAddMembershipSetting = IP_ADD_MEMBERSHIP;
115
116                 address.in4.sin_family = ipFamilySetting;
117                 address.in4.sin_addr.s_addr = INADDR_ANY;
118                 address.in4.sin_port = getRxMcPort();
119                 addr = (struct sockaddr *)&address.in4;
120                 addrSize = sizeof(struct sockaddr_in);
121         } else {
122                 assert(networkInterface->ipAddress.in6.sin6_addr.s6_addr != in6addr_any.s6_addr);
123
124                 ipFamilySetting = AF_INET6;
125                 ipProtoSetting = IPPROTO_IPV6;
126                 ipMcLoopSetting = IPV6_MULTICAST_LOOP;
127                 ipAddMembershipSetting = IPV6_ADD_MEMBERSHIP;
128
129                 address.in6.sin6_family = ipFamilySetting;
130                 address.in6.sin6_addr = in6addr_any;
131                 address.in6.sin6_port = getRxMcPort();
132                 addr = (struct sockaddr *)&address.in6;
133                 addrSize = sizeof(struct sockaddr_in6);
134         }
135
136         /* Create a datagram socket on which to receive. */
137         errno = 0;
138         rxSocket = socket(ipFamilySetting, SOCK_DGRAM, 0);
139         if (rxSocket < 0) {
140                 pudError(true, "Could not create a receive socket for interface %s",
141                                 networkInterface->name);
142                 goto bail;
143         }
144
145         /* Enable SO_REUSEADDR to allow multiple applications to receive the same
146          * multicast messages */
147         errno = 0;
148         if (setsockopt(rxSocket, SOL_SOCKET, SO_REUSEADDR, &socketReuseFlagValue,
149                         sizeof(socketReuseFlagValue)) < 0) {
150                 pudError(true, "Could not set the reuse flag on the receive socket for"
151                         " interface %s", networkInterface->name);
152                 goto bail;
153         }
154
155         /* Bind to the proper port number with the IP address INADDR_ANY
156          * (INADDR_ANY is really required here, do not change it) */
157         errno = 0;
158         if (bind(rxSocket, addr, addrSize) < 0) {
159                 pudError(true, "Could not bind the receive socket for interface"
160                         " %s to port %u", networkInterface->name, ntohs(getRxMcPort()));
161                 goto bail;
162         }
163
164         /* Enable multicast local loopback */
165         errno = 0;
166         if (setsockopt(rxSocket, ipProtoSetting, ipMcLoopSetting, &mcLoopValue,
167                         sizeof(mcLoopValue)) < 0) {
168                 pudError(true, "Could not %s multicast loopback on the"
169                         " receive socket for interface %s", mcLoopValue ? "enable"
170                                 : "disable", networkInterface->name);
171                 goto bail;
172         }
173
174         /* Join the multicast group on the local interface. Note that this
175          * ADD_MEMBERSHIP option must be called for each local interface over
176          * which the multicast datagrams are to be received. */
177         if (ipFamilySetting == AF_INET) {
178                 struct ip_mreq mc_settings;
179                 (void) memset(&mc_settings, 0, sizeof(mc_settings));
180                 mc_settings.imr_multiaddr = rxMcAddr->in4.sin_addr;
181                 mc_settings.imr_interface = networkInterface->ipAddress.in4.sin_addr;
182                 errno = 0;
183                 if (setsockopt(rxSocket, ipProtoSetting, ipAddMembershipSetting,
184                                 &mc_settings, sizeof(mc_settings)) < 0) {
185                         pudError(true, "Could not subscribe interface %s to the configured"
186                                 " multicast group", networkInterface->name);
187                         goto bail;
188                 }
189         } else {
190                 struct ipv6_mreq mc6_settings;
191                 (void) memset(&mc6_settings, 0, sizeof(mc6_settings));
192                 mc6_settings.ipv6mr_multiaddr = rxMcAddr->in6.sin6_addr;
193                 mc6_settings.ipv6mr_interface = 0;
194                 errno = 0;
195                 if (setsockopt(rxSocket, ipProtoSetting, ipAddMembershipSetting,
196                                 &mc6_settings, sizeof(mc6_settings)) < 0) {
197                         pudError(true, "Could not subscribe interface %s to the configured"
198                                 " multicast group", networkInterface->name);
199                         goto bail;
200                 }
201         }
202
203         add_olsr_socket(rxSocket, rxSocketHandlerFunction, NULL, networkInterface,
204                         SP_PR_READ);
205
206         return rxSocket;
207
208         bail: if (rxSocket >= 0) {
209                 close(rxSocket);
210         }
211         return -1;
212
213 }
214
215 /**
216  Create a receive interface and add it to the list of receive network interface
217  objects
218
219  @param ifName
220  the network interface name
221  @param ipAddr
222  the IP address of the interface
223  @param rxSocketHandlerFunction
224  the function that handles reception of data on the network interface
225  @param rxMcAddr
226  The receive multicast address (must have same IP version as ipAddr)
227
228  @return
229  - true on success
230  - false on failure
231  */
232 static bool createRxInterface(const char * ifName, union olsr_sockaddr * ipAddr,
233                 socket_handler_func rxSocketHandlerFunction, union olsr_sockaddr * rxMcAddr) {
234         int socketFd = -1;
235         TRxTxNetworkInterface * networkInterface = NULL;
236
237         if (ifName == NULL) {
238                 goto bail;
239         }
240
241         networkInterface = olsr_malloc(sizeof(TRxTxNetworkInterface),
242                         "TRxTxNetworkInterface (PUD)");
243         if (networkInterface == NULL) {
244                 goto bail;
245         }
246
247         memcpy(networkInterface->name, ifName, sizeof(networkInterface->name));
248         networkInterface->name[IFNAMSIZ] = '\0';
249         networkInterface->ipAddress = *ipAddr;
250         networkInterface->handler = NULL;
251         networkInterface->next = NULL;
252
253         /* networkInterface needs to be filled in when calling createRxSocket */
254         socketFd = createRxSocket(networkInterface, rxSocketHandlerFunction, rxMcAddr);
255         if (socketFd < 0) {
256                 goto bail;
257         }
258         networkInterface->socketFd = socketFd;
259         networkInterface->handler = rxSocketHandlerFunction;
260
261         /* Add new object to the end of the global list. */
262         if (rxNetworkInterfacesListHead == NULL) {
263                 rxNetworkInterfacesListHead = networkInterface;
264                 lastRxNetworkInterface = networkInterface;
265         } else {
266                 lastRxNetworkInterface->next = networkInterface;
267                 lastRxNetworkInterface = networkInterface;
268         }
269
270         return true;
271
272         bail: if (networkInterface != NULL) {
273                 free(networkInterface);
274         }
275         return false;
276
277 }
278
279 /*
280  * TX interfaces
281  */
282
283 /** The list of network interface objects, sending our NMEA sentences */
284 static TRxTxNetworkInterface *txNetworkInterfacesListHead = NULL;
285
286 /** Pointer to the last network interface object, sending our NMEA sentences */
287 static TRxTxNetworkInterface *lastTxNetworkInterface = NULL;
288
289 /**
290  @return
291  The list of network interface objects, sending our NMEA sentences
292  */
293 TRxTxNetworkInterface *getTxNetworkInterfaces(void) {
294         return txNetworkInterfacesListHead;
295 }
296
297 /**
298  Create a transmit socket for a network interface
299
300  @param networkInterface
301  The network interface object. This function expects it to be filled with all
302  information, except for the socket descriptor.
303
304  @return
305  - the socket descriptor (>= 0)
306  - -1 if an error occurred
307  */
308 static int createTxSocket(TRxTxNetworkInterface * networkInterface) {
309         int ipFamilySetting;
310         int ipProtoSetting;
311         int ipMcLoopSetting;
312         int ipMcIfSetting;
313
314         union olsr_sockaddr address;
315
316         int txSocket = -1;
317
318         int mcLoopValue = 0;
319         int txTtl = getTxTtl();
320
321         assert(networkInterface != NULL);
322         assert(strncmp((char *) &networkInterface->name[0], "",
323                                         sizeof(networkInterface->name)) != 0);
324
325         memset(&address, 0, sizeof(address));
326         if (networkInterface->ipAddress.in.sa_family == AF_INET) {
327                 assert(networkInterface->ipAddress.in4.sin_addr.s_addr != INADDR_ANY);
328
329                 ipFamilySetting = AF_INET;
330                 ipProtoSetting = IPPROTO_IP;
331                 ipMcLoopSetting = IP_MULTICAST_LOOP;
332                 ipMcIfSetting = IP_MULTICAST_IF;
333
334                 address.in4.sin_family = ipFamilySetting;
335                 address.in4.sin_addr = networkInterface->ipAddress.in4.sin_addr;
336                 address.in4.sin_port = getTxMcPort();
337         } else {
338                 assert(networkInterface->ipAddress.in6.sin6_addr.s6_addr != in6addr_any.s6_addr);
339
340                 ipFamilySetting = AF_INET6;
341                 ipProtoSetting = IPPROTO_IPV6;
342                 ipMcLoopSetting = IPV6_MULTICAST_LOOP;
343                 ipMcIfSetting = IPV6_MULTICAST_IF;
344
345                 address.in6.sin6_family = ipFamilySetting;
346                 address.in6.sin6_addr = networkInterface->ipAddress.in6.sin6_addr;
347                 address.in6.sin6_port = getTxMcPort();
348         }
349
350         /*  Create a datagram socket on which to transmit */
351         errno = 0;
352         txSocket = socket(ipFamilySetting, SOCK_DGRAM, 0);
353         if (txSocket < 0) {
354                 pudError(true, "Could not create a transmit socket for interface %s",
355                                 networkInterface->name);
356                 goto bail;
357         }
358
359         /* Bind the socket to the desired interface and port */
360         errno = 0;
361         if (setsockopt(txSocket, ipProtoSetting, ipMcIfSetting, &address,
362                         sizeof(address)) < 0) {
363                 pudError(true, "Could not set the multicast interface on the"
364                         " transmit socket to interface %s", networkInterface->name);
365                 goto bail;
366         }
367
368         /* Disable multicast local loopback */
369         errno = 0;
370         if (setsockopt(txSocket, ipProtoSetting, ipMcLoopSetting, &mcLoopValue,
371                         sizeof(mcLoopValue)) < 0) {
372                 pudError(true, "Could not %s multicast loopback on the"
373                         " transmit socket for interface %s", mcLoopValue ? "enable"
374                                 : "disable", networkInterface->name);
375                 goto bail;
376         }
377
378         /* Set the TTL on the socket */
379         errno = 0;
380         if (setsockopt(txSocket, ipProtoSetting, IP_MULTICAST_TTL, &txTtl,
381                         sizeof(txTtl)) < 0) {
382                 pudError(true, "Could not set TTL on the transmit socket"
383                         " for interface %s", networkInterface->name);
384                 goto bail;
385         }
386
387         /* Set the no delay option on the socket */
388         errno = 0;
389         if (fcntl(txSocket, F_SETFL, O_NDELAY) < 0) {
390                 pudError(true, "Could not set the no delay option on the"
391                         " transmit socket for interface %s", networkInterface->name);
392                 goto bail;
393         }
394
395         return txSocket;
396
397         bail: if (txSocket >= 0) {
398                 close(txSocket);
399         }
400         return -1;
401 }
402
403 /**
404  Create a transmit interface and add it to the list of transmit network
405  interface objects
406
407  @param ifName
408  the network interface name
409  @param ipAddr
410  the IP address of the interface
411
412  @return
413  - true on success
414  - false on failure
415  */
416 static bool createTxInterface(const char * ifName, union olsr_sockaddr * ipAddr) {
417         int socketFd = -1;
418         TRxTxNetworkInterface * networkInterface = NULL;
419
420         if (ifName == NULL) {
421                 goto bail;
422         }
423
424         networkInterface = olsr_malloc(sizeof(TRxTxNetworkInterface),
425                         "TRxTxNetworkInterface (PUD)");
426         if (networkInterface == NULL) {
427                 goto bail;
428         }
429
430         memcpy(networkInterface->name, ifName, sizeof(networkInterface->name));
431         networkInterface->name[IFNAMSIZ] = '\0';
432         networkInterface->ipAddress = *ipAddr;
433         networkInterface->handler = NULL;
434         networkInterface->next = NULL;
435
436         /* networkInterface needs to be filled in when calling createTxSocket */
437         socketFd = createTxSocket(networkInterface);
438         if (socketFd < 0) {
439                 goto bail;
440         }
441         networkInterface->socketFd = socketFd;
442
443         /* Add new object to the end of the global list. */
444         if (txNetworkInterfacesListHead == NULL) {
445                 txNetworkInterfacesListHead = networkInterface;
446                 lastTxNetworkInterface = networkInterface;
447         } else {
448                 lastTxNetworkInterface->next = networkInterface;
449                 lastTxNetworkInterface = networkInterface;
450         }
451
452         return true;
453
454         bail: if (networkInterface != NULL) {
455                 free(networkInterface);
456         }
457         return false;
458 }
459
460 /*
461  * Downlink interface
462  */
463
464 /** The socket fd, receiving downlinked messages */
465 static int downlinkSocketFd = -1;
466
467 /** the downlink handler function */
468 static socket_handler_func downlinkHandler = NULL;
469
470
471 /**
472  @return
473  The downlink socket fd. -1 when not valid.
474  */
475 int getDownlinkSocketFd(void) {
476         return downlinkSocketFd;
477 }
478
479 /**
480  Create an downlink socket
481
482  @param ipVersion
483  The IP version (AF_INET or AF_INET6) for the socket
484  @param rxSocketHandlerFunction
485  The socket handler function
486
487  @return
488  - the socket descriptor (>= 0)
489  - -1 if an error occurred
490  */
491 static int createDownlinkSocket(int ipVersion, socket_handler_func rxSocketHandlerFunction) {
492         union olsr_sockaddr address;
493         struct sockaddr * addr;
494         size_t addrSize;
495
496         int downlinkSocket = -1;
497
498         int socketReuseFlagValue = 1;
499
500         memset(&address, 0, sizeof(address));
501         if (ipVersion == AF_INET) {
502                 address.in4.sin_family = AF_INET;
503                 address.in4.sin_addr.s_addr = INADDR_ANY;
504                 address.in4.sin_port = getDownlinkPort();
505                 addr = (struct sockaddr *)&address.in4;
506                 addrSize = sizeof(struct sockaddr_in);
507         } else {
508                 address.in6.sin6_family = AF_INET6;
509                 address.in6.sin6_addr = in6addr_any;
510                 address.in6.sin6_port = getDownlinkPort();
511                 addr = (struct sockaddr *)&address.in6;
512                 addrSize = sizeof(struct sockaddr_in6);
513         }
514
515         /*  Create a datagram socket on which to receive */
516         errno = 0;
517         downlinkSocket = socket(ipVersion, SOCK_DGRAM, 0);
518         if (downlinkSocket < 0) {
519                 pudError(true, "Could not create the downlink socket");
520                 goto bail;
521         }
522
523         /* Enable SO_REUSEADDR to allow multiple applications to receive the same
524          * messages */
525         errno = 0;
526         if (setsockopt(downlinkSocket, SOL_SOCKET, SO_REUSEADDR, &socketReuseFlagValue,
527                         sizeof(socketReuseFlagValue)) < 0) {
528                 pudError(true, "Could not set REUSE option on the downlink socket");
529                 goto bail;
530         }
531
532         /* Bind to the proper port number with the IP address INADDR_ANY
533          * (INADDR_ANY is really required here, do not change it) */
534         errno = 0;
535         if (bind(downlinkSocket, addr, addrSize)) {
536                 pudError(true, "Could not bind downlink socket to port %d",
537                                 getDownlinkPort());
538                 goto bail;
539         }
540
541         add_olsr_socket(downlinkSocket, rxSocketHandlerFunction, NULL, NULL,
542                         SP_PR_READ);
543
544         downlinkHandler = rxSocketHandlerFunction;
545
546         return downlinkSocket;
547
548         bail: if (downlinkSocket >= 0) {
549                 close(downlinkSocket);
550         }
551         return -1;
552 }
553
554 /*
555  * OLSR interfaces
556  */
557
558 /** The list of OLSR network interface objects */
559 static TOLSRNetworkInterface *olsrNetworkInterfacesListHead = NULL;
560
561 /** Pointer to the last OLSR network interface object */
562 static TOLSRNetworkInterface *lastOlsrNetworkInterface = NULL;
563
564 /**
565  Get the OLSR interface structure for a certain OLSR interface. Note that
566  pointer comparison is performed to compare the OLSR interfaces.
567
568  @param olsrIntf
569  a pointer to an OLSR interface
570
571  @return
572  - a pointer to the OLSR interface structure
573  - NULL when not found
574  */
575 TOLSRNetworkInterface * getOlsrNetworkInterface(struct interface *olsrIntf) {
576         TOLSRNetworkInterface * retval = olsrNetworkInterfacesListHead;
577
578         while ((retval->olsrIntf != olsrIntf) && (retval != NULL)) {
579                 retval = retval->next;
580         }
581
582         return retval;
583 }
584
585 /**
586  Create an OLSR interface and add it to the list of OLSR network interface
587  objects
588
589  @param olsrIntf
590  a pointer to the OLSR interface
591
592  @return
593  - true on success
594  - false on failure
595  */
596 static int createOlsrInterface(struct interface *olsrIntf) {
597         TOLSRNetworkInterface * networkInterface = NULL;
598
599         networkInterface = olsr_malloc(sizeof(TOLSRNetworkInterface),
600                         "TOLSRNetworkInterface (PUD)");
601         if (networkInterface == NULL) {
602                 goto bail;
603         }
604
605         networkInterface->olsrIntf = olsrIntf;
606         networkInterface->next = NULL;
607
608         /* Add new object to the end of the global list. */
609         if (olsrNetworkInterfacesListHead == NULL) {
610                 olsrNetworkInterfacesListHead = networkInterface;
611                 lastOlsrNetworkInterface = networkInterface;
612         } else {
613                 lastOlsrNetworkInterface->next = networkInterface;
614                 lastOlsrNetworkInterface = networkInterface;
615         }
616
617         return true;
618
619         bail: if (networkInterface != NULL) {
620                 free(networkInterface);
621         }
622         return false;
623 }
624
625 /*
626  * Interface Functions
627  */
628
629 /**
630  Creates receive and transmit sockets and register the receive sockets with
631  the OLSR stack
632
633  @param rxSocketHandlerFunction
634  The function to call upon reception of data on a receive socket
635  @param rxSocketHandlerFunctionDownlink
636  The function to call upon reception of data on a downlink receive socket
637
638  @return
639  - true on success
640  - false on failure
641  */
642 bool createNetworkInterfaces(socket_handler_func rxSocketHandlerFunction,
643                 socket_handler_func rxSocketHandlerFunctionDownlink) {
644         int retval = false;
645         struct ifaddrs *ifAddrs = NULL;
646         struct ifaddrs *ifAddr = NULL;
647         union olsr_sockaddr * rxMcAddr = getRxMcAddr();
648         union olsr_sockaddr * txMcAddr = getTxMcAddr();
649
650         errno = 0;
651         if (getifaddrs(&ifAddrs) != 0) {
652                 pudError(true, "Could not get list of interfaces and their addresses");
653                 return retval;
654         }
655
656         /* loop over all interfaces */
657         for (ifAddr = ifAddrs; ifAddr != NULL; ifAddr = ifAddr->ifa_next) {
658                 union olsr_sockaddr * addr = (union olsr_sockaddr *)ifAddr->ifa_addr;
659                 if ((addr != NULL) && ((addr->in.sa_family == AF_INET) || (addr->in.sa_family == AF_INET6))) {
660                         char * ifName = ifAddr->ifa_name;
661
662                         struct interface * olsrIntf = ((addr->in.sa_family != olsr_cnf->ip_version) ?
663                                         NULL :
664                                         if_ifwithaddr((addr->in.sa_family == AF_INET) ?
665                                                         (union olsr_ip_addr *)&addr->in4.sin_addr :
666                                                         (union olsr_ip_addr *)&addr->in6.sin6_addr));
667                         bool isOlsrIf = (olsrIntf != NULL);
668
669                         /* determine whether the iterated interface is configured as a
670                          * non-OLSR interface in the plugin parameter list */
671                         bool isRxNonOlsrIf = isRxNonOlsrInterface(ifName) && (addr->in.sa_family == rxMcAddr->in.sa_family);
672                         bool isTxNonOlsrIf = isTxNonOlsrInterface(ifName) && (addr->in.sa_family == txMcAddr->in.sa_family);
673
674                         bool isNonOlsrIf = isRxNonOlsrIf || isTxNonOlsrIf;
675
676                         if (!isOlsrIf && !isNonOlsrIf) {
677                                 /* Interface is not an OLSR interface AND interface is not
678                                  * configured as non-OLSR interface: skip */
679                                 continue;
680                         }
681
682                         if (isOlsrIf && !createOlsrInterface(olsrIntf)) {
683                                 /* creating an OLSR interface failed */
684                                 goto end;
685                         }
686
687                         if (!isNonOlsrIf) {
688                                 /* interface is not configured as non-OLSR interface: skip */
689                                 continue;
690                         }
691
692                         if (isRxNonOlsrIf && !createRxInterface(ifName, addr, rxSocketHandlerFunction, rxMcAddr)) {
693                                 /* creating a receive interface failed */
694                                 goto end;
695                         }
696
697                         if (isTxNonOlsrIf && !createTxInterface(ifName, addr)) {
698                                 /* creating a transmit interface failed */
699                                 goto end;
700                         }
701                 }
702         }
703
704         if (isUplinkAddrSet()) {
705                 downlinkSocketFd = createDownlinkSocket(getUplinkAddr()->in.sa_family, rxSocketHandlerFunctionDownlink);
706                 if (downlinkSocketFd == -1) {
707                         goto end;
708                 }
709         } else {
710                 downlinkSocketFd = -1;
711         }
712
713         retval = true;
714
715         end: freeifaddrs(ifAddrs);
716         return retval;
717 }
718
719 /**
720  Cleanup the OLSR network interfaces in the given list
721
722  @param networkInterface
723  the list of network interface to close and clean up
724  */
725 static void cleanupOlsrInterfaces(TOLSRNetworkInterface * networkInterface) {
726         TOLSRNetworkInterface * nextNetworkInterface = networkInterface;
727         while (nextNetworkInterface != NULL) {
728                 TOLSRNetworkInterface * iteratedNetworkInterface = nextNetworkInterface;
729                 nextNetworkInterface = iteratedNetworkInterface->next;
730                 iteratedNetworkInterface->next = NULL;
731                 free(iteratedNetworkInterface);
732         }
733 }
734
735 /**
736  Close and cleanup the network interfaces in the given list
737
738  @param networkInterface
739  the list of network interface to close and clean up
740  */
741 static void closeInterfaces(TRxTxNetworkInterface * networkInterface) {
742         TRxTxNetworkInterface * nextNetworkInterface = networkInterface;
743         while (nextNetworkInterface != NULL) {
744                 TRxTxNetworkInterface * iteratedNetworkInterface = nextNetworkInterface;
745                 if (iteratedNetworkInterface->socketFd >= 0) {
746                         if (iteratedNetworkInterface->handler) {
747                                 remove_olsr_socket(iteratedNetworkInterface->socketFd,
748                                                 iteratedNetworkInterface->handler, NULL);
749                         }
750                         close(iteratedNetworkInterface->socketFd);
751                         iteratedNetworkInterface->socketFd = -1;
752                 }
753                 nextNetworkInterface = iteratedNetworkInterface->next;
754                 iteratedNetworkInterface->next = NULL;
755                 free(iteratedNetworkInterface);
756         }
757 }
758
759 /**
760  Close and cleanup all receive and transmit network interfaces
761  */
762 void closeNetworkInterfaces(void) {
763         if (rxNetworkInterfacesListHead != NULL) {
764                 closeInterfaces(rxNetworkInterfacesListHead);
765                 rxNetworkInterfacesListHead = NULL;
766         }
767
768         if (txNetworkInterfacesListHead != NULL) {
769                 closeInterfaces(txNetworkInterfacesListHead);
770                 txNetworkInterfacesListHead = NULL;
771         }
772
773         if (olsrNetworkInterfacesListHead != NULL) {
774                 cleanupOlsrInterfaces(olsrNetworkInterfacesListHead);
775                 olsrNetworkInterfacesListHead = NULL;
776         }
777
778         if (downlinkSocketFd != -1 ) {
779                 if (downlinkHandler) {
780                         remove_olsr_socket (downlinkSocketFd, downlinkHandler, NULL);
781                         downlinkHandler = NULL;
782                 }
783                 close(downlinkSocketFd);
784                 downlinkSocketFd = -1;
785         }
786 }