0170e7772aff1a200523f1186bf736f401be880f
[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         void * 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 = &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 = &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 = if_nametoindex((char *)networkInterface->name);
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         int ipTtlSetting;
314         unsigned int index;
315
316         union olsr_sockaddr address;
317         void * addr;
318         size_t addrSize;
319
320         int txSocket = -1;
321
322         int mcLoopValue = 0;
323         int txTtl = getTxTtl();
324
325         assert(networkInterface != NULL);
326         assert(strncmp((char *) &networkInterface->name[0], "",
327                                         sizeof(networkInterface->name)) != 0);
328
329         memset(&address, 0, sizeof(address));
330         if (networkInterface->ipAddress.in.sa_family == AF_INET) {
331                 assert(networkInterface->ipAddress.in4.sin_addr.s_addr != INADDR_ANY);
332
333                 ipFamilySetting = AF_INET;
334                 ipProtoSetting = IPPROTO_IP;
335                 ipMcLoopSetting = IP_MULTICAST_LOOP;
336                 ipMcIfSetting = IP_MULTICAST_IF;
337                 ipTtlSetting = IP_MULTICAST_TTL;
338                 index = 0;
339
340                 address.in4.sin_family = ipFamilySetting;
341                 address.in4.sin_addr = networkInterface->ipAddress.in4.sin_addr;
342                 address.in4.sin_port = getTxMcPort();
343                 addr = &address.in4;
344                 addrSize = sizeof(struct sockaddr_in);
345         } else {
346                 assert(networkInterface->ipAddress.in6.sin6_addr.s6_addr != in6addr_any.s6_addr);
347
348                 ipFamilySetting = AF_INET6;
349                 ipProtoSetting = IPPROTO_IPV6;
350                 ipMcLoopSetting = IPV6_MULTICAST_LOOP;
351                 ipMcIfSetting = IPV6_MULTICAST_IF;
352                 ipTtlSetting = IPV6_MULTICAST_HOPS;
353                 index = if_nametoindex((char *)networkInterface->name);
354
355                 addr = &index;
356                 addrSize = sizeof(index);
357         }
358
359         /*  Create a datagram socket on which to transmit */
360         errno = 0;
361         txSocket = socket(ipFamilySetting, SOCK_DGRAM, 0);
362         if (txSocket < 0) {
363                 pudError(true, "Could not create a transmit socket for interface %s",
364                                 networkInterface->name);
365                 goto bail;
366         }
367
368         /* Bind the socket to the desired interface */
369         errno = 0;
370         if (setsockopt(txSocket, ipProtoSetting, ipMcIfSetting, addr, addrSize) < 0) {
371                 pudError(true, "Could not set the multicast interface on the"
372                         " transmit socket to interface %s", networkInterface->name);
373                 goto bail;
374         }
375
376         /* Disable multicast local loopback */
377         errno = 0;
378         if (setsockopt(txSocket, ipProtoSetting, ipMcLoopSetting, &mcLoopValue,
379                         sizeof(mcLoopValue)) < 0) {
380                 pudError(true, "Could not %s multicast loopback on the"
381                         " transmit socket for interface %s", mcLoopValue ? "enable"
382                                 : "disable", networkInterface->name);
383                 goto bail;
384         }
385
386         /* Set the TTL on the socket */
387         errno = 0;
388         if (setsockopt(txSocket, ipProtoSetting, ipTtlSetting, &txTtl,
389                         sizeof(txTtl)) < 0) {
390                 pudError(true, "Could not set TTL on the transmit socket"
391                         " for interface %s", networkInterface->name);
392                 goto bail;
393         }
394
395         /* Set the no delay option on the socket */
396         errno = 0;
397         if (fcntl(txSocket, F_SETFL, O_NDELAY) < 0) {
398                 pudError(true, "Could not set the no delay option on the"
399                         " transmit socket for interface %s", networkInterface->name);
400                 goto bail;
401         }
402
403         return txSocket;
404
405         bail: if (txSocket >= 0) {
406                 close(txSocket);
407         }
408         return -1;
409 }
410
411 /**
412  Create a transmit interface and add it to the list of transmit network
413  interface objects
414
415  @param ifName
416  the network interface name
417  @param ipAddr
418  the IP address of the interface
419
420  @return
421  - true on success
422  - false on failure
423  */
424 static bool createTxInterface(const char * ifName, union olsr_sockaddr * ipAddr) {
425         int socketFd = -1;
426         TRxTxNetworkInterface * networkInterface = NULL;
427
428         if (ifName == NULL) {
429                 goto bail;
430         }
431
432         networkInterface = olsr_malloc(sizeof(TRxTxNetworkInterface),
433                         "TRxTxNetworkInterface (PUD)");
434         if (networkInterface == NULL) {
435                 goto bail;
436         }
437
438         memcpy(networkInterface->name, ifName, sizeof(networkInterface->name));
439         networkInterface->name[IFNAMSIZ] = '\0';
440         networkInterface->ipAddress = *ipAddr;
441         networkInterface->handler = NULL;
442         networkInterface->next = NULL;
443
444         /* networkInterface needs to be filled in when calling createTxSocket */
445         socketFd = createTxSocket(networkInterface);
446         if (socketFd < 0) {
447                 goto bail;
448         }
449         networkInterface->socketFd = socketFd;
450
451         /* Add new object to the end of the global list. */
452         if (txNetworkInterfacesListHead == NULL) {
453                 txNetworkInterfacesListHead = networkInterface;
454                 lastTxNetworkInterface = networkInterface;
455         } else {
456                 lastTxNetworkInterface->next = networkInterface;
457                 lastTxNetworkInterface = networkInterface;
458         }
459
460         return true;
461
462         bail: if (networkInterface != NULL) {
463                 free(networkInterface);
464         }
465         return false;
466 }
467
468 /*
469  * Downlink interface
470  */
471
472 /** The socket fd, receiving downlinked messages */
473 static int downlinkSocketFd = -1;
474
475 /** the downlink handler function */
476 static socket_handler_func downlinkHandler = NULL;
477
478
479 /**
480  @return
481  The downlink socket fd. -1 when not valid.
482  */
483 int getDownlinkSocketFd(void) {
484         return downlinkSocketFd;
485 }
486
487 /**
488  Create an downlink socket
489
490  @param ipVersion
491  The IP version (AF_INET or AF_INET6) for the socket
492  @param rxSocketHandlerFunction
493  The socket handler function
494
495  @return
496  - the socket descriptor (>= 0)
497  - -1 if an error occurred
498  */
499 static int createDownlinkSocket(int ipVersion, socket_handler_func rxSocketHandlerFunction) {
500         union olsr_sockaddr address;
501         void * addr;
502         size_t addrSize;
503
504         int downlinkSocket = -1;
505
506         int socketReuseFlagValue = 1;
507
508         memset(&address, 0, sizeof(address));
509         if (ipVersion == AF_INET) {
510                 address.in4.sin_family = AF_INET;
511                 address.in4.sin_addr.s_addr = INADDR_ANY;
512                 address.in4.sin_port = getDownlinkPort();
513                 addr = &address.in4;
514                 addrSize = sizeof(struct sockaddr_in);
515         } else {
516                 address.in6.sin6_family = AF_INET6;
517                 address.in6.sin6_addr = in6addr_any;
518                 address.in6.sin6_port = getDownlinkPort();
519                 addr = &address.in6;
520                 addrSize = sizeof(struct sockaddr_in6);
521         }
522
523         /*  Create a datagram socket on which to receive */
524         errno = 0;
525         downlinkSocket = socket(ipVersion, SOCK_DGRAM, 0);
526         if (downlinkSocket < 0) {
527                 pudError(true, "Could not create the downlink socket");
528                 goto bail;
529         }
530
531         /* Enable SO_REUSEADDR to allow multiple applications to receive the same
532          * messages */
533         errno = 0;
534         if (setsockopt(downlinkSocket, SOL_SOCKET, SO_REUSEADDR, &socketReuseFlagValue,
535                         sizeof(socketReuseFlagValue)) < 0) {
536                 pudError(true, "Could not set REUSE option on the downlink socket");
537                 goto bail;
538         }
539
540         /* Bind to the proper port number with the IP address INADDR_ANY
541          * (INADDR_ANY is really required here, do not change it) */
542         errno = 0;
543         if (bind(downlinkSocket, addr, addrSize)) {
544                 pudError(true, "Could not bind downlink socket to port %d",
545                                 getDownlinkPort());
546                 goto bail;
547         }
548
549         add_olsr_socket(downlinkSocket, rxSocketHandlerFunction, NULL, NULL,
550                         SP_PR_READ);
551
552         downlinkHandler = rxSocketHandlerFunction;
553
554         return downlinkSocket;
555
556         bail: if (downlinkSocket >= 0) {
557                 close(downlinkSocket);
558         }
559         return -1;
560 }
561
562 /*
563  * Interface Functions
564  */
565
566 /**
567  Creates receive and transmit sockets and register the receive sockets with
568  the OLSR stack
569
570  @param rxSocketHandlerFunction
571  The function to call upon reception of data on a receive socket
572  @param rxSocketHandlerFunctionDownlink
573  The function to call upon reception of data on a downlink receive socket
574
575  @return
576  - true on success
577  - false on failure
578  */
579 bool createNetworkInterfaces(socket_handler_func rxSocketHandlerFunction,
580                 socket_handler_func rxSocketHandlerFunctionDownlink) {
581         int retval = false;
582         struct ifaddrs *ifAddrs = NULL;
583         struct ifaddrs *ifAddr = NULL;
584         union olsr_sockaddr * rxMcAddr = getRxMcAddr();
585         union olsr_sockaddr * txMcAddr = getTxMcAddr();
586
587         errno = 0;
588         if (getifaddrs(&ifAddrs) != 0) {
589                 pudError(true, "Could not get list of interfaces and their addresses");
590                 return retval;
591         }
592
593         /* loop over all interfaces */
594         for (ifAddr = ifAddrs; ifAddr != NULL; ifAddr = ifAddr->ifa_next) {
595                 union olsr_sockaddr * addr = (union olsr_sockaddr *)ifAddr->ifa_addr;
596                 if ((addr != NULL) && ((addr->in.sa_family == AF_INET) || (addr->in.sa_family == AF_INET6))) {
597                         char * ifName = ifAddr->ifa_name;
598
599                         /* determine whether the iterated interface is configured as a
600                          * non-OLSR interface in the plugin parameter list */
601                         bool isRxNonOlsrIf = isRxNonOlsrInterface(ifName) && (addr->in.sa_family == rxMcAddr->in.sa_family);
602                         bool isTxNonOlsrIf = isTxNonOlsrInterface(ifName) && (addr->in.sa_family == txMcAddr->in.sa_family);
603
604                         bool isNonOlsrIf = isRxNonOlsrIf || isTxNonOlsrIf;
605
606                         if (!isNonOlsrIf) {
607                                 /* interface is not configured as non-OLSR interface: skip */
608                                 continue;
609                         }
610
611                         if (isRxNonOlsrIf && !createRxInterface(ifName, addr, rxSocketHandlerFunction, rxMcAddr)) {
612                                 /* creating a receive interface failed */
613                                 goto end;
614                         }
615
616                         if (isTxNonOlsrIf && !createTxInterface(ifName, addr)) {
617                                 /* creating a transmit interface failed */
618                                 goto end;
619                         }
620                 }
621         }
622
623         if (isUplinkAddrSet()) {
624                 downlinkSocketFd = createDownlinkSocket(getUplinkAddr()->in.sa_family, rxSocketHandlerFunctionDownlink);
625                 if (downlinkSocketFd == -1) {
626                         goto end;
627                 }
628         } else {
629                 downlinkSocketFd = -1;
630         }
631
632         retval = true;
633
634         end: freeifaddrs(ifAddrs);
635         return retval;
636 }
637
638 /**
639  Close and cleanup the network interfaces in the given list
640
641  @param networkInterface
642  the list of network interface to close and clean up
643  */
644 static void closeInterfaces(TRxTxNetworkInterface * networkInterface) {
645         TRxTxNetworkInterface * nextNetworkInterface = networkInterface;
646         while (nextNetworkInterface != NULL) {
647                 TRxTxNetworkInterface * iteratedNetworkInterface = nextNetworkInterface;
648                 if (iteratedNetworkInterface->socketFd >= 0) {
649                         if (iteratedNetworkInterface->handler) {
650                                 remove_olsr_socket(iteratedNetworkInterface->socketFd,
651                                                 iteratedNetworkInterface->handler, NULL);
652                         }
653                         close(iteratedNetworkInterface->socketFd);
654                         iteratedNetworkInterface->socketFd = -1;
655                 }
656                 nextNetworkInterface = iteratedNetworkInterface->next;
657                 iteratedNetworkInterface->next = NULL;
658                 free(iteratedNetworkInterface);
659         }
660 }
661
662 /**
663  Close and cleanup all receive and transmit network interfaces
664  */
665 void closeNetworkInterfaces(void) {
666         if (rxNetworkInterfacesListHead != NULL) {
667                 closeInterfaces(rxNetworkInterfacesListHead);
668                 rxNetworkInterfacesListHead = NULL;
669         }
670
671         if (txNetworkInterfacesListHead != NULL) {
672                 closeInterfaces(txNetworkInterfacesListHead);
673                 txNetworkInterfacesListHead = NULL;
674         }
675
676         if (downlinkSocketFd != -1 ) {
677                 if (downlinkHandler) {
678                         remove_olsr_socket (downlinkSocketFd, downlinkHandler, NULL);
679                         downlinkHandler = NULL;
680                 }
681                 close(downlinkSocketFd);
682                 downlinkSocketFd = -1;
683         }
684 }