]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/mDNSUNP.c
64fc06addb18fdac0992fceb25ae9e7b76d7ec2c
[apple/mdnsresponder.git] / mDNSPosix / mDNSUNP.c
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include "mDNSUNP.h"
19
20 #include <errno.h>
21 #include <assert.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <sys/uio.h>
25 #include <sys/ioctl.h>
26 #include <signal.h>
27 #include <unistd.h>
28 #include <stdio.h>
29
30 /* Some weird platforms derived from 4.4BSD Lite (e.g. EFI) need the ALIGN(P)
31 macro, usually defined in <sys/param.h> or someplace like that, to make sure the
32 CMSG_NXTHDR macro is well-formed. On such platforms, the symbol NEED_ALIGN_MACRO
33 should be set to the name of the header to include to get the ALIGN(P) macro.
34 */
35 #ifdef NEED_ALIGN_MACRO
36 #include NEED_ALIGN_MACRO
37 #endif
38
39 /* Solaris defined SIOCGIFCONF etc in <sys/sockio.h> but
40 other platforms don't even have that include file. So,
41 if we haven't yet got a definition, let's try to find
42 <sys/sockio.h>.
43 */
44
45 #ifndef SIOCGIFCONF
46 #include <sys/sockio.h>
47 #endif
48
49 /* sockaddr_dl is only referenced if we're using IP_RECVIF,
50 so only include the header in that case.
51 */
52
53 #ifdef IP_RECVIF
54 #include <net/if_dl.h>
55 #endif
56
57 #if defined(AF_INET6) && HAVE_IPV6 && !HAVE_LINUX
58 #include <net/if_var.h>
59 #include <netinet/in_var.h>
60 // Note: netinet/in_var.h implicitly includes netinet6/in6_var.h for us
61 #endif
62
63 #if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
64 #include <netdb.h>
65 #include <arpa/inet.h>
66
67 /* Converts a prefix length to IPv6 network mask */
68 void plen_to_mask(int plen, char *addr) {
69 int i;
70 int colons=7; /* Number of colons in IPv6 address */
71 int bits_in_block=16; /* Bits per IPv6 block */
72 for(i=0; i<=colons; i++) {
73 int block, ones=0xffff, ones_in_block;
74 if (plen>bits_in_block) ones_in_block=bits_in_block;
75 else ones_in_block=plen;
76 block = ones & (ones << (bits_in_block-ones_in_block));
77 i==0 ? sprintf(addr, "%x", block) : sprintf(addr, "%s:%x", addr, block);
78 plen -= ones_in_block;
79 }
80 }
81
82 /* Gets IPv6 interface information from the /proc filesystem in linux*/
83 struct ifi_info *get_ifi_info_linuxv6(int family, int doaliases)
84 {
85 struct ifi_info *ifi, *ifihead, **ifipnext, *ifipold, **ifiptr;
86 FILE *fp = NULL;
87 char addr[8][5];
88 int flags, myflags, index, plen, scope;
89 char ifname[9], lastname[IFNAMSIZ];
90 char addr6[32+7+1]; /* don't forget the seven ':' */
91 struct addrinfo hints, *res0;
92 int err;
93 int sockfd = -1;
94 struct ifreq ifr;
95
96 res0=NULL;
97 ifihead = NULL;
98 ifipnext = &ifihead;
99 lastname[0] = 0;
100
101 if ((fp = fopen(PROC_IFINET6_PATH, "r")) != NULL) {
102 sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
103 if (sockfd < 0) {
104 goto gotError;
105 }
106 while (fscanf(fp,
107 "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %8s\n",
108 addr[0],addr[1],addr[2],addr[3],
109 addr[4],addr[5],addr[6],addr[7],
110 &index, &plen, &scope, &flags, ifname) != EOF) {
111
112 myflags = 0;
113 if (strncmp(lastname, ifname, IFNAMSIZ) == 0) {
114 if (doaliases == 0)
115 continue; /* already processed this interface */
116 myflags = IFI_ALIAS;
117 }
118 memcpy(lastname, ifname, IFNAMSIZ);
119 ifi = (struct ifi_info*)calloc(1, sizeof(struct ifi_info));
120 if (ifi == NULL) {
121 goto gotError;
122 }
123
124 ifipold = *ifipnext; /* need this later */
125 ifiptr = ifipnext;
126 *ifipnext = ifi; /* prev points to this new one */
127 ifipnext = &ifi->ifi_next; /* pointer to next one goes here */
128
129 sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
130 addr[0],addr[1],addr[2],addr[3],
131 addr[4],addr[5],addr[6],addr[7]);
132
133 /* Add address of the interface */
134 memset(&hints, 0, sizeof(hints));
135 hints.ai_family = AF_INET6;
136 hints.ai_flags = AI_NUMERICHOST;
137 err = getaddrinfo(addr6, NULL, &hints, &res0);
138 if (err) {
139 goto gotError;
140 }
141 ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in6));
142 if (ifi->ifi_addr == NULL) {
143 goto gotError;
144 }
145 memcpy(ifi->ifi_addr, res0->ai_addr, sizeof(struct sockaddr_in6));
146
147 /* Add netmask of the interface */
148 char ipv6addr[INET6_ADDRSTRLEN];
149 plen_to_mask(plen, ipv6addr);
150 ifi->ifi_netmask = calloc(1, sizeof(struct sockaddr_in6));
151 if (ifi->ifi_netmask == NULL) {
152 goto gotError;
153 }
154
155 ((struct sockaddr_in6 *)ifi->ifi_netmask)->sin6_family=family;
156 ((struct sockaddr_in6 *)ifi->ifi_netmask)->sin6_scope_id=scope;
157 inet_pton(family, ipv6addr, &((struct sockaddr_in6 *)ifi->ifi_netmask)->sin6_addr);
158
159 /* Add interface name */
160 memcpy(ifi->ifi_name, ifname, IFI_NAME);
161
162 /* Add interface index */
163 ifi->ifi_index = index;
164
165 /* Add interface flags*/
166 memcpy(ifr.ifr_name, ifname, IFNAMSIZ);
167 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
168 if (errno == EADDRNOTAVAIL) {
169 /*
170 * If the main interface is configured with no IP address but
171 * an alias interface exists with an IP address, you get
172 * EADDRNOTAVAIL for the main interface
173 */
174 free(ifi->ifi_addr);
175 free(ifi->ifi_netmask);
176 free(ifi);
177 ifipnext = ifiptr;
178 *ifipnext = ifipold;
179 continue;
180 } else {
181 goto gotError;
182 }
183 }
184 ifi->ifi_flags = ifr.ifr_flags;
185 freeaddrinfo(res0);
186 res0=NULL;
187 }
188 }
189 goto done;
190
191 gotError:
192 if (ifihead != NULL) {
193 free_ifi_info(ifihead);
194 ifihead = NULL;
195 }
196 if (res0 != NULL) {
197 freeaddrinfo(res0);
198 res0=NULL;
199 }
200 done:
201 if (sockfd != -1) {
202 assert(close(sockfd) == 0);
203 }
204 if (fp != NULL) {
205 fclose(fp);
206 }
207 return(ifihead); /* pointer to first structure in linked list */
208 }
209 #endif // defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
210
211 struct ifi_info *get_ifi_info(int family, int doaliases)
212 {
213 int junk;
214 struct ifi_info *ifi, *ifihead, **ifipnext, *ifipold, **ifiptr;
215 int sockfd, sockf6, len, lastlen, flags, myflags;
216 #ifdef NOT_HAVE_IF_NAMETOINDEX
217 int index = 200;
218 #endif
219 char *ptr, *buf, lastname[IFNAMSIZ], *cptr;
220 struct ifconf ifc;
221 struct ifreq *ifr, ifrcopy;
222 struct sockaddr_in *sinptr;
223
224 #if defined(AF_INET6) && HAVE_IPV6
225 struct sockaddr_in6 *sinptr6;
226 #endif
227
228 #if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
229 if (family == AF_INET6) return get_ifi_info_linuxv6(family, doaliases);
230 #endif
231
232 sockfd = -1;
233 sockf6 = -1;
234 buf = NULL;
235 ifihead = NULL;
236
237 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
238 if (sockfd < 0) {
239 goto gotError;
240 }
241
242 lastlen = 0;
243 len = 100 * sizeof(struct ifreq); /* initial buffer size guess */
244 for ( ; ; ) {
245 buf = (char*)malloc(len);
246 if (buf == NULL) {
247 goto gotError;
248 }
249 ifc.ifc_len = len;
250 ifc.ifc_buf = buf;
251 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
252 if (errno != EINVAL || lastlen != 0) {
253 goto gotError;
254 }
255 } else {
256 if (ifc.ifc_len == lastlen)
257 break; /* success, len has not changed */
258 lastlen = ifc.ifc_len;
259 }
260 len += 10 * sizeof(struct ifreq); /* increment */
261 free(buf);
262 }
263 ifihead = NULL;
264 ifipnext = &ifihead;
265 lastname[0] = 0;
266 /* end get_ifi_info1 */
267
268 /* include get_ifi_info2 */
269 for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
270 ifr = (struct ifreq *) ptr;
271
272 /* Advance to next one in buffer */
273 if (sizeof(struct ifreq) > sizeof(ifr->ifr_name) + GET_SA_LEN(ifr->ifr_addr))
274 ptr += sizeof(struct ifreq);
275 else
276 ptr += sizeof(ifr->ifr_name) + GET_SA_LEN(ifr->ifr_addr);
277
278 // fprintf(stderr, "intf %p name=%s AF=%d\n", index, ifr->ifr_name, ifr->ifr_addr.sa_family);
279
280 if (ifr->ifr_addr.sa_family != family)
281 continue; /* ignore if not desired address family */
282
283 myflags = 0;
284 if ( (cptr = strchr(ifr->ifr_name, ':')) != NULL)
285 *cptr = 0; /* replace colon will null */
286 if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
287 if (doaliases == 0)
288 continue; /* already processed this interface */
289 myflags = IFI_ALIAS;
290 }
291 memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
292
293 ifrcopy = *ifr;
294 if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0) {
295 goto gotError;
296 }
297
298 flags = ifrcopy.ifr_flags;
299 if ((flags & IFF_UP) == 0)
300 continue; /* ignore if interface not up */
301
302 ifi = (struct ifi_info*)calloc(1, sizeof(struct ifi_info));
303 if (ifi == NULL) {
304 goto gotError;
305 }
306 ifipold = *ifipnext; /* need this later */
307 ifiptr = ifipnext;
308 *ifipnext = ifi; /* prev points to this new one */
309 ifipnext = &ifi->ifi_next; /* pointer to next one goes here */
310
311 ifi->ifi_flags = flags; /* IFF_xxx values */
312 ifi->ifi_myflags = myflags; /* IFI_xxx values */
313 #ifndef NOT_HAVE_IF_NAMETOINDEX
314 ifi->ifi_index = if_nametoindex(ifr->ifr_name);
315 #else
316 ifrcopy = *ifr;
317 #ifdef SIOCGIFINDEX
318 if ( 0 >= ioctl(sockfd, SIOCGIFINDEX, &ifrcopy))
319 ifi->ifi_index = ifrcopy.ifr_index;
320 else
321 #endif
322 ifi->ifi_index = index++; /* SIOCGIFINDEX is broken on Solaris 2.5ish, so fake it */
323 #endif
324 memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME);
325 ifi->ifi_name[IFI_NAME-1] = '\0';
326 /* end get_ifi_info2 */
327 /* include get_ifi_info3 */
328 switch (ifr->ifr_addr.sa_family) {
329 case AF_INET:
330 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
331 if (ifi->ifi_addr == NULL) {
332 ifi->ifi_addr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
333 if (ifi->ifi_addr == NULL) {
334 goto gotError;
335 }
336 memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
337
338 #ifdef SIOCGIFNETMASK
339 if (ioctl(sockfd, SIOCGIFNETMASK, &ifrcopy) < 0) {
340 if (errno == EADDRNOTAVAIL) {
341 /*
342 * If the main interface is configured with no IP address but
343 * an alias interface exists with an IP address, you get
344 * EADDRNOTAVAIL for the main interface
345 */
346 free(ifi->ifi_addr);
347 free(ifi);
348 ifipnext = ifiptr;
349 *ifipnext = ifipold;
350 continue;
351 } else {
352 goto gotError;
353 }
354 }
355
356 ifi->ifi_netmask = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
357 if (ifi->ifi_netmask == NULL) goto gotError;
358 sinptr = (struct sockaddr_in *) &ifrcopy.ifr_addr;
359 /* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
360 #ifndef NOT_HAVE_SA_LEN
361 sinptr->sin_len = sizeof(struct sockaddr_in);
362 #endif
363 sinptr->sin_family = AF_INET;
364 memcpy(ifi->ifi_netmask, sinptr, sizeof(struct sockaddr_in));
365 #endif
366
367 #ifdef SIOCGIFBRDADDR
368 if (flags & IFF_BROADCAST) {
369 if (ioctl(sockfd, SIOCGIFBRDADDR, &ifrcopy) < 0) {
370 goto gotError;
371 }
372 sinptr = (struct sockaddr_in *) &ifrcopy.ifr_broadaddr;
373 /* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
374 #ifndef NOT_HAVE_SA_LEN
375 sinptr->sin_len = sizeof( struct sockaddr_in );
376 #endif
377 sinptr->sin_family = AF_INET;
378 ifi->ifi_brdaddr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
379 if (ifi->ifi_brdaddr == NULL) {
380 goto gotError;
381 }
382 memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in));
383 }
384 #endif
385
386 #ifdef SIOCGIFDSTADDR
387 if (flags & IFF_POINTOPOINT) {
388 if (ioctl(sockfd, SIOCGIFDSTADDR, &ifrcopy) < 0) {
389 goto gotError;
390 }
391 sinptr = (struct sockaddr_in *) &ifrcopy.ifr_dstaddr;
392 /* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
393 #ifndef NOT_HAVE_SA_LEN
394 sinptr->sin_len = sizeof( struct sockaddr_in );
395 #endif
396 sinptr->sin_family = AF_INET;
397 ifi->ifi_dstaddr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
398 if (ifi->ifi_dstaddr == NULL) {
399 goto gotError;
400 }
401 memcpy(ifi->ifi_dstaddr, sinptr, sizeof(struct sockaddr_in));
402 }
403 #endif
404 }
405 break;
406
407 #if defined(AF_INET6) && HAVE_IPV6
408 case AF_INET6:
409 sinptr6 = (struct sockaddr_in6 *) &ifr->ifr_addr;
410 if (ifi->ifi_addr == NULL) {
411 ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in6));
412 if (ifi->ifi_addr == NULL) {
413 goto gotError;
414 }
415
416 /* Some platforms (*BSD) inject the prefix in IPv6LL addresses */
417 /* We need to strip that out */
418 if (IN6_IS_ADDR_LINKLOCAL(&sinptr6->sin6_addr))
419 sinptr6->sin6_addr.s6_addr[2] = sinptr6->sin6_addr.s6_addr[3] = 0;
420 memcpy(ifi->ifi_addr, sinptr6, sizeof(struct sockaddr_in6));
421
422 #ifdef SIOCGIFNETMASK_IN6
423 {
424 struct in6_ifreq ifr6;
425 if (sockf6 == -1)
426 sockf6 = socket(AF_INET6, SOCK_DGRAM, 0);
427 memset(&ifr6, 0, sizeof(ifr6));
428 memcpy(&ifr6.ifr_name, &ifr->ifr_name, sizeof(ifr6.ifr_name ));
429 memcpy(&ifr6.ifr_ifru.ifru_addr, &ifr->ifr_addr, sizeof(ifr6.ifr_ifru.ifru_addr));
430 if (ioctl(sockf6, SIOCGIFNETMASK_IN6, &ifr6) < 0) {
431 if (errno == EADDRNOTAVAIL) {
432 /*
433 * If the main interface is configured with no IP address but
434 * an alias interface exists with an IP address, you get
435 * EADDRNOTAVAIL for the main interface
436 */
437 free(ifi->ifi_addr);
438 free(ifi);
439 ifipnext = ifiptr;
440 *ifipnext = ifipold;
441 continue;
442 } else {
443 goto gotError;
444 }
445 }
446 ifi->ifi_netmask = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in6));
447 if (ifi->ifi_netmask == NULL) goto gotError;
448 sinptr6 = (struct sockaddr_in6 *) &ifr6.ifr_ifru.ifru_addr;
449 memcpy(ifi->ifi_netmask, sinptr6, sizeof(struct sockaddr_in6));
450 }
451 #endif
452 }
453 break;
454 #endif
455
456 default:
457 break;
458 }
459 }
460 goto done;
461
462 gotError:
463 if (ifihead != NULL) {
464 free_ifi_info(ifihead);
465 ifihead = NULL;
466 }
467
468 done:
469 if (buf != NULL) {
470 free(buf);
471 }
472 if (sockfd != -1) {
473 junk = close(sockfd);
474 assert(junk == 0);
475 }
476 if (sockf6 != -1) {
477 junk = close(sockf6);
478 assert(junk == 0);
479 }
480 return(ifihead); /* pointer to first structure in linked list */
481 }
482 /* end get_ifi_info3 */
483
484 /* include free_ifi_info */
485 void
486 free_ifi_info(struct ifi_info *ifihead)
487 {
488 struct ifi_info *ifi, *ifinext;
489
490 for (ifi = ifihead; ifi != NULL; ifi = ifinext) {
491 if (ifi->ifi_addr != NULL)
492 free(ifi->ifi_addr);
493 if (ifi->ifi_netmask != NULL)
494 free(ifi->ifi_netmask);
495 if (ifi->ifi_brdaddr != NULL)
496 free(ifi->ifi_brdaddr);
497 if (ifi->ifi_dstaddr != NULL)
498 free(ifi->ifi_dstaddr);
499 ifinext = ifi->ifi_next; /* can't fetch ifi_next after free() */
500 free(ifi); /* the ifi_info{} itself */
501 }
502 }
503 /* end free_ifi_info */
504
505 ssize_t
506 recvfrom_flags(int fd, void *ptr, size_t nbytes, int *flagsp,
507 struct sockaddr *sa, socklen_t *salenptr, struct my_in_pktinfo *pktp, u_char *ttl)
508 {
509 struct msghdr msg;
510 struct iovec iov[1];
511 ssize_t n;
512
513 #ifdef CMSG_FIRSTHDR
514 struct cmsghdr *cmptr;
515 union {
516 struct cmsghdr cm;
517 char control[1024];
518 } control_un;
519
520 *ttl = 255; // If kernel fails to provide TTL data then assume the TTL was 255 as it should be
521
522 msg.msg_control = control_un.control;
523 msg.msg_controllen = sizeof(control_un.control);
524 msg.msg_flags = 0;
525 #else
526 memset(&msg, 0, sizeof(msg)); /* make certain msg_accrightslen = 0 */
527 #endif /* CMSG_FIRSTHDR */
528
529 msg.msg_name = (char *) sa;
530 msg.msg_namelen = *salenptr;
531 iov[0].iov_base = (char *)ptr;
532 iov[0].iov_len = nbytes;
533 msg.msg_iov = iov;
534 msg.msg_iovlen = 1;
535
536 if ( (n = recvmsg(fd, &msg, *flagsp)) < 0)
537 return(n);
538
539 *salenptr = msg.msg_namelen; /* pass back results */
540 if (pktp) {
541 /* 0.0.0.0, i/f = -1 */
542 /* We set the interface to -1 so that the caller can
543 tell whether we returned a meaningful value or
544 just some default. Previously this code just
545 set the value to 0, but I'm concerned that 0
546 might be a valid interface value.
547 */
548 memset(pktp, 0, sizeof(struct my_in_pktinfo));
549 pktp->ipi_ifindex = -1;
550 }
551 /* end recvfrom_flags1 */
552
553 /* include recvfrom_flags2 */
554 #ifndef CMSG_FIRSTHDR
555 #warning CMSG_FIRSTHDR not defined. Will not be able to determine destination address, received interface, etc.
556 *flagsp = 0; /* pass back results */
557 return(n);
558 #else
559
560 *flagsp = msg.msg_flags; /* pass back results */
561 if (msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr) ||
562 (msg.msg_flags & MSG_CTRUNC) || pktp == NULL)
563 return(n);
564
565 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL;
566 cmptr = CMSG_NXTHDR(&msg, cmptr)) {
567
568 #ifdef IP_PKTINFO
569 #if in_pktinfo_definition_is_missing
570 struct in_pktinfo
571 {
572 int ipi_ifindex;
573 struct in_addr ipi_spec_dst;
574 struct in_addr ipi_addr;
575 };
576 #endif
577 if (cmptr->cmsg_level == IPPROTO_IP &&
578 cmptr->cmsg_type == IP_PKTINFO) {
579 struct in_pktinfo *tmp;
580 struct sockaddr_in *sin = (struct sockaddr_in*)&pktp->ipi_addr;
581
582 tmp = (struct in_pktinfo *) CMSG_DATA(cmptr);
583 sin->sin_family = AF_INET;
584 sin->sin_addr = tmp->ipi_addr;
585 sin->sin_port = 0;
586 pktp->ipi_ifindex = tmp->ipi_ifindex;
587 continue;
588 }
589 #endif
590
591 #ifdef IP_RECVDSTADDR
592 if (cmptr->cmsg_level == IPPROTO_IP &&
593 cmptr->cmsg_type == IP_RECVDSTADDR) {
594 struct sockaddr_in *sin = (struct sockaddr_in*)&pktp->ipi_addr;
595
596 sin->sin_family = AF_INET;
597 sin->sin_addr = *(struct in_addr*)CMSG_DATA(cmptr);
598 sin->sin_port = 0;
599 continue;
600 }
601 #endif
602
603 #ifdef IP_RECVIF
604 if (cmptr->cmsg_level == IPPROTO_IP &&
605 cmptr->cmsg_type == IP_RECVIF) {
606 struct sockaddr_dl *sdl = (struct sockaddr_dl *) CMSG_DATA(cmptr);
607 #ifndef HAVE_BROKEN_RECVIF_NAME
608 int nameLen = (sdl->sdl_nlen < IFI_NAME - 1) ? sdl->sdl_nlen : (IFI_NAME - 1);
609 strncpy(pktp->ipi_ifname, sdl->sdl_data, nameLen);
610 #endif
611 pktp->ipi_ifindex = sdl->sdl_index;
612 #ifdef HAVE_BROKEN_RECVIF_NAME
613 if (sdl->sdl_index == 0) {
614 pktp->ipi_ifindex = *(uint_t*)sdl;
615 }
616 #endif
617 assert(pktp->ipi_ifname[IFI_NAME - 1] == 0);
618 // null terminated because of memset above
619 continue;
620 }
621 #endif
622
623 #ifdef IP_RECVTTL
624 if (cmptr->cmsg_level == IPPROTO_IP &&
625 cmptr->cmsg_type == IP_RECVTTL) {
626 *ttl = *(u_char*)CMSG_DATA(cmptr);
627 continue;
628 }
629 else if (cmptr->cmsg_level == IPPROTO_IP &&
630 cmptr->cmsg_type == IP_TTL) { // some implementations seem to send IP_TTL instead of IP_RECVTTL
631 *ttl = *(int*)CMSG_DATA(cmptr);
632 continue;
633 }
634 #endif
635
636 #if defined(IPV6_PKTINFO) && HAVE_IPV6
637 if (cmptr->cmsg_level == IPPROTO_IPV6 &&
638 cmptr->cmsg_type == IPV6_2292_PKTINFO) {
639 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&pktp->ipi_addr;
640 struct in6_pktinfo *ip6_info = (struct in6_pktinfo*)CMSG_DATA(cmptr);
641
642 sin6->sin6_family = AF_INET6;
643 #ifndef NOT_HAVE_SA_LEN
644 sin6->sin6_len = sizeof(*sin6);
645 #endif
646 sin6->sin6_addr = ip6_info->ipi6_addr;
647 sin6->sin6_flowinfo = 0;
648 sin6->sin6_scope_id = 0;
649 sin6->sin6_port = 0;
650 pktp->ipi_ifindex = ip6_info->ipi6_ifindex;
651 continue;
652 }
653 #endif
654
655 #if defined(IPV6_HOPLIMIT) && HAVE_IPV6
656 if (cmptr->cmsg_level == IPPROTO_IPV6 &&
657 cmptr->cmsg_type == IPV6_2292_HOPLIMIT) {
658 *ttl = *(int*)CMSG_DATA(cmptr);
659 continue;
660 }
661 #endif
662 assert(0); // unknown ancillary data
663 }
664 return(n);
665 #endif /* CMSG_FIRSTHDR */
666 }
667
668 // **********************************************************************************************
669
670 // daemonize the process. Adapted from "Unix Network Programming" vol 1 by Stevens, section 12.4.
671 // Returns 0 on success, -1 on failure.
672
673 #ifdef NOT_HAVE_DAEMON
674 #include <fcntl.h>
675 #include <sys/stat.h>
676 #include <sys/signal.h>
677
678 int daemon(int nochdir, int noclose)
679 {
680 switch (fork())
681 {
682 case -1: return (-1); // Fork failed
683 case 0: break; // Child -- continue
684 default: _exit(0); // Parent -- exit
685 }
686
687 if (setsid() == -1) return(-1);
688
689 signal(SIGHUP, SIG_IGN);
690
691 switch (fork()) // Fork again, primarily for reasons of Unix trivia
692 {
693 case -1: return (-1); // Fork failed
694 case 0: break; // Child -- continue
695 default: _exit(0); // Parent -- exit
696 }
697
698 if (!nochdir) (void)chdir("/");
699 umask(0);
700
701 if (!noclose)
702 {
703 int fd = open("/dev/null", O_RDWR, 0);
704 if (fd != -1)
705 {
706 // Avoid unnecessarily duplicating a file descriptor to itself
707 if (fd != STDIN_FILENO) (void)dup2(fd, STDIN_FILENO);
708 if (fd != STDOUT_FILENO) (void)dup2(fd, STDOUT_FILENO);
709 if (fd != STDERR_FILENO) (void)dup2(fd, STDERR_FILENO);
710 if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO)
711 (void)close (fd);
712 }
713 }
714 return (0);
715 }
716 #endif /* NOT_HAVE_DAEMON */