]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/mDNSUNP.c
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
24 Change History (most recent first):
27 Revision 1.30 2005/11/29 20:03:02 mkrochma
28 Wrapped sin_len with #ifndef NOT_HAVE_SA_LEN
30 Revision 1.29 2005/11/12 02:23:10 cheshire
31 <rdar://problem/4317680> mDNSUNP.c needs to deal with lame results from SIOCGIFNETMASK, SIOCGIFBRDADDR and SIOCGIFDSTADDR
33 Revision 1.28 2005/10/31 22:09:45 cheshire
34 Buffer "char addr6[33]" was seven bytes too small
36 Revision 1.27 2005/06/29 15:54:21 cheshire
37 <rdar://problem/4113742> mDNSResponder-107.1 does not work on FreeBSD
38 Refine last checkin so that it (hopefully) doesn't break get_ifi_info() for every other OS
40 Revision 1.26 2005/04/08 21:43:59 ksekar
41 <rdar://problem/4083426> mDNSPosix (v98) retrieve interface list bug on AMD64 architecture
42 Submitted by Andrew de Quincey
44 Revision 1.25 2005/04/08 21:37:57 ksekar
45 <rdar://problem/3792767> get_ifi_info doesn't return IPv6 interfaces on Linux
47 Revision 1.24 2005/04/08 21:30:16 ksekar
48 <rdar://problem/4007457> Compiling problems with mDNSResponder-98 on Solaris/Sparc v9
49 Patch submitted by Bernd Kuhls
51 Revision 1.23 2004/12/01 04:25:05 cheshire
52 <rdar://problem/3872803> Darwin patches for Solaris and Suse
53 Provide daemon() for platforms that don't have it
55 Revision 1.22 2004/11/30 22:37:01 cheshire
56 Update copyright dates and add "Mode: C; tab-width: 4" headers
58 Revision 1.21 2004/11/08 22:13:59 rpantos
59 Create sockf6 lazily when v6 interface found.
61 Revision 1.20 2004/10/16 00:17:01 cheshire
62 <rdar://problem/3770558> Replace IP TTL 255 check with local subnet source address check
64 Revision 1.19 2004/07/20 01:47:36 rpantos
65 NOT_HAVE_SA_LEN applies to v6, too. And use more-portable s6_addr.
67 Revision 1.18 2004/07/08 21:30:21 rpantos
69 Revision 1.17 2004/06/25 00:26:27 rpantos
70 Changes to fix the Posix build on Solaris.
72 Revision 1.16 2004/03/20 05:37:09 cheshire
73 Fix contributed by Terry Lambert & Alfred Perlstein:
74 Don't use uint8_t -- it requires stdint.h, which doesn't exist on FreeBSD 4.x
76 Revision 1.15 2004/02/14 01:09:45 rpantos
77 Just use HAVE_IPV6 rather than defined(HAVE_IPV6).
79 Revision 1.14 2003/12/11 18:53:40 cheshire
80 Fix compiler warning reported by Paul Guyot
82 Revision 1.13 2003/12/08 20:47:02 rpantos
83 Add support for mDNSResponder on Linux.
85 Revision 1.12 2003/09/02 20:47:13 cheshire
86 Fix signed/unsigned warning
88 Revision 1.11 2003/08/12 19:56:26 cheshire
91 Revision 1.10 2003/08/06 18:20:51 cheshire
94 Revision 1.9 2003/07/14 18:11:54 cheshire
95 Fix stricter compiler warnings
97 Revision 1.8 2003/07/02 21:19:59 cheshire
98 <rdar://problem/3313413> Update copyright notices, etc., in source code comments
100 Revision 1.7 2003/03/20 21:10:31 cheshire
101 Fixes done at IETF 56 to make mDNSProxyResponderPosix run on Solaris
103 Revision 1.6 2003/03/13 03:46:21 cheshire
104 Fixes to make the code build on Linux
106 Revision 1.5 2003/02/07 03:02:02 cheshire
107 Submitted by: Mitsutaka Watanabe
108 The code saying "index += 1;" was effectively making up random interface index values.
109 The right way to find the correct interface index is if_nametoindex();
111 Revision 1.4 2002/12/23 22:13:31 jgraessl
113 Reviewed by: Stuart Cheshire
114 Initial IPv6 support for mDNSResponder.
116 Revision 1.3 2002/09/21 20:44:53 zarzycki
119 Revision 1.2 2002/09/19 04:20:44 cheshire
120 Remove high-ascii characters that confuse some systems
122 Revision 1.1 2002/09/17 06:24:34 cheshire
134 #include <sys/ioctl.h>
138 /* Solaris defined SIOCGIFCONF etc in <sys/sockio.h> but
139 other platforms don't even have that include file. So,
140 if we haven't yet got a definition, let's try to find
145 #include <sys/sockio.h>
148 /* sockaddr_dl is only referenced if we're using IP_RECVIF,
149 so only include the header in that case.
153 #include <net/if_dl.h>
156 #if defined(AF_INET6) && HAVE_IPV6 && !HAVE_LINUX
157 #include <netinet6/in6_var.h>
160 #if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
162 #include <arpa/inet.h>
164 /* Converts a prefix length to IPv6 network mask */
165 void plen_to_mask(int plen
, char *addr
) {
167 int colons
=7; /* Number of colons in IPv6 address */
168 int bits_in_block
=16; /* Bits per IPv6 block */
169 for(i
=0;i
<=colons
;i
++) {
170 int block
, ones
=0xffff, ones_in_block
;
171 if(plen
>bits_in_block
) ones_in_block
=bits_in_block
;
172 else ones_in_block
=plen
;
173 block
= ones
& (ones
<< (bits_in_block
-ones_in_block
));
174 i
==0 ? sprintf(addr
, "%x", block
) :
175 sprintf(addr
, "%s:%x", addr
, block
);
176 plen
-= ones_in_block
;
180 /* Gets IPv6 interface information from the /proc filesystem in linux*/
181 struct ifi_info
*get_ifi_info_linuxv6(int family
, int doaliases
)
183 struct ifi_info
*ifi
, *ifihead
, **ifipnext
;
186 int flags
, myflags
, index
, plen
, scope
;
187 char ifname
[8], lastname
[IFNAMSIZ
];
188 char addr6
[32+7+1]; /* don't forget the seven ':' */
189 struct addrinfo hints
, *res0
;
190 struct sockaddr_in6
*sin6
;
191 struct in6_addr
*addrptr
;
199 if ((fp
= fopen(PROC_IFINET6_PATH
, "r")) != NULL
) {
201 "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %8s\n",
202 addr
[0],addr
[1],addr
[2],addr
[3],
203 addr
[4],addr
[5],addr
[6],addr
[7],
204 &index
, &plen
, &scope
, &flags
, ifname
) != EOF
) {
207 if (strncmp(lastname
, ifname
, IFNAMSIZ
) == 0) {
209 continue; /* already processed this interface */
212 memcpy(lastname
, ifname
, IFNAMSIZ
);
213 ifi
= (struct ifi_info
*)calloc(1, sizeof(struct ifi_info
));
218 *ifipnext
= ifi
; /* prev points to this new one */
219 ifipnext
= &ifi
->ifi_next
; /* pointer to next one goes here */
221 sprintf(addr6
, "%s:%s:%s:%s:%s:%s:%s:%s",
222 addr
[0],addr
[1],addr
[2],addr
[3],
223 addr
[4],addr
[5],addr
[6],addr
[7]);
225 /* Add address of the interface */
226 memset(&hints
, 0, sizeof(hints
));
227 hints
.ai_family
= AF_INET6
;
228 hints
.ai_flags
= AI_NUMERICHOST
;
229 err
= getaddrinfo(addr6
, NULL
, &hints
, &res0
);
233 ifi
->ifi_addr
= calloc(1, sizeof(struct sockaddr_in6
));
234 if (ifi
->ifi_addr
== NULL
) {
237 memcpy(ifi
->ifi_addr
, res0
->ai_addr
, sizeof(struct sockaddr_in6
));
239 /* Add netmask of the interface */
240 char ipv6addr
[INET6_ADDRSTRLEN
];
241 plen_to_mask(plen
, ipv6addr
);
242 ifi
->ifi_netmask
= calloc(1, sizeof(struct sockaddr_in6
));
243 if (ifi
->ifi_addr
== NULL
) {
246 sin6
=calloc(1, sizeof(struct sockaddr_in6
));
247 addrptr
=calloc(1, sizeof(struct in6_addr
));
248 inet_pton(family
, ipv6addr
, addrptr
);
249 sin6
->sin6_family
=family
;
250 sin6
->sin6_addr
=*addrptr
;
251 sin6
->sin6_scope_id
=scope
;
252 memcpy(ifi
->ifi_netmask
, sin6
, sizeof(struct sockaddr_in6
));
256 /* Add interface name */
257 memcpy(ifi
->ifi_name
, ifname
, IFI_NAME
);
259 /* Add interface index */
260 ifi
->ifi_index
= index
;
262 /* If interface is in /proc then it is up*/
263 ifi
->ifi_flags
= IFF_UP
;
272 if (ifihead
!= NULL
) {
273 free_ifi_info(ifihead
);
281 return(ifihead
); /* pointer to first structure in linked list */
283 #endif // defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
285 struct ifi_info
*get_ifi_info(int family
, int doaliases
)
288 struct ifi_info
*ifi
, *ifihead
, **ifipnext
;
289 int sockfd
, sockf6
, len
, lastlen
, flags
, myflags
;
290 #ifdef NOT_HAVE_IF_NAMETOINDEX
293 char *ptr
, *buf
, lastname
[IFNAMSIZ
], *cptr
;
295 struct ifreq
*ifr
, ifrcopy
;
296 struct sockaddr_in
*sinptr
;
298 #if defined(AF_INET6) && HAVE_IPV6
299 struct sockaddr_in6
*sinptr6
;
302 #if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
303 if(family
== AF_INET6
) return get_ifi_info_linuxv6(family
, doaliases
);
311 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
317 len
= 100 * sizeof(struct ifreq
); /* initial buffer size guess */
319 buf
= (char*)malloc(len
);
325 if (ioctl(sockfd
, SIOCGIFCONF
, &ifc
) < 0) {
326 if (errno
!= EINVAL
|| lastlen
!= 0) {
330 if (ifc
.ifc_len
== lastlen
)
331 break; /* success, len has not changed */
332 lastlen
= ifc
.ifc_len
;
334 len
+= 10 * sizeof(struct ifreq
); /* increment */
340 /* end get_ifi_info1 */
342 /* include get_ifi_info2 */
343 for (ptr
= buf
; ptr
< buf
+ ifc
.ifc_len
; ) {
344 ifr
= (struct ifreq
*) ptr
;
346 /* Advance to next one in buffer */
347 if (sizeof(struct ifreq
) > sizeof(ifr
->ifr_name
) + GET_SA_LEN(ifr
->ifr_addr
))
348 ptr
+= sizeof(struct ifreq
);
350 ptr
+= sizeof(ifr
->ifr_name
) + GET_SA_LEN(ifr
->ifr_addr
);
352 // fprintf(stderr, "intf %p name=%s AF=%d\n", index, ifr->ifr_name, ifr->ifr_addr.sa_family);
354 if (ifr
->ifr_addr
.sa_family
!= family
)
355 continue; /* ignore if not desired address family */
358 if ( (cptr
= strchr(ifr
->ifr_name
, ':')) != NULL
)
359 *cptr
= 0; /* replace colon will null */
360 if (strncmp(lastname
, ifr
->ifr_name
, IFNAMSIZ
) == 0) {
362 continue; /* already processed this interface */
365 memcpy(lastname
, ifr
->ifr_name
, IFNAMSIZ
);
368 if (ioctl(sockfd
, SIOCGIFFLAGS
, &ifrcopy
) < 0) {
372 flags
= ifrcopy
.ifr_flags
;
373 if ((flags
& IFF_UP
) == 0)
374 continue; /* ignore if interface not up */
376 ifi
= (struct ifi_info
*)calloc(1, sizeof(struct ifi_info
));
380 *ifipnext
= ifi
; /* prev points to this new one */
381 ifipnext
= &ifi
->ifi_next
; /* pointer to next one goes here */
383 ifi
->ifi_flags
= flags
; /* IFF_xxx values */
384 ifi
->ifi_myflags
= myflags
; /* IFI_xxx values */
385 #ifndef NOT_HAVE_IF_NAMETOINDEX
386 ifi
->ifi_index
= if_nametoindex(ifr
->ifr_name
);
389 if ( 0 >= ioctl(sockfd
, SIOCGIFINDEX
, &ifrcopy
))
390 ifi
->ifi_index
= ifrcopy
.ifr_index
;
392 ifi
->ifi_index
= index
++; /* SIOCGIFINDEX is broken on Solaris 2.5ish, so fake it */
394 memcpy(ifi
->ifi_name
, ifr
->ifr_name
, IFI_NAME
);
395 ifi
->ifi_name
[IFI_NAME
-1] = '\0';
396 /* end get_ifi_info2 */
397 /* include get_ifi_info3 */
398 switch (ifr
->ifr_addr
.sa_family
) {
400 sinptr
= (struct sockaddr_in
*) &ifr
->ifr_addr
;
401 if (ifi
->ifi_addr
== NULL
) {
402 ifi
->ifi_addr
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
403 if (ifi
->ifi_addr
== NULL
) {
406 memcpy(ifi
->ifi_addr
, sinptr
, sizeof(struct sockaddr_in
));
408 #ifdef SIOCGIFNETMASK
409 if (ioctl(sockfd
, SIOCGIFNETMASK
, &ifrcopy
) < 0) goto gotError
;
410 ifi
->ifi_netmask
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
411 if (ifi
->ifi_netmask
== NULL
) goto gotError
;
412 sinptr
= (struct sockaddr_in
*) &ifrcopy
.ifr_addr
;
413 /* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
414 #ifndef NOT_HAVE_SA_LEN
415 sinptr
->sin_len
= sizeof(struct sockaddr_in
);
417 sinptr
->sin_family
= AF_INET
;
418 memcpy(ifi
->ifi_netmask
, sinptr
, sizeof(struct sockaddr_in
));
421 #ifdef SIOCGIFBRDADDR
422 if (flags
& IFF_BROADCAST
) {
423 if (ioctl(sockfd
, SIOCGIFBRDADDR
, &ifrcopy
) < 0) {
426 sinptr
= (struct sockaddr_in
*) &ifrcopy
.ifr_broadaddr
;
427 /* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
428 #ifndef NOT_HAVE_SA_LEN
429 sinptr
->sin_len
= sizeof( struct sockaddr_in
);
431 sinptr
->sin_family
= AF_INET
;
432 ifi
->ifi_brdaddr
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
433 if (ifi
->ifi_brdaddr
== NULL
) {
436 memcpy(ifi
->ifi_brdaddr
, sinptr
, sizeof(struct sockaddr_in
));
440 #ifdef SIOCGIFDSTADDR
441 if (flags
& IFF_POINTOPOINT
) {
442 if (ioctl(sockfd
, SIOCGIFDSTADDR
, &ifrcopy
) < 0) {
445 sinptr
= (struct sockaddr_in
*) &ifrcopy
.ifr_dstaddr
;
446 /* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
447 #ifndef NOT_HAVE_SA_LEN
448 sinptr
->sin_len
= sizeof( struct sockaddr_in
);
450 sinptr
->sin_family
= AF_INET
;
451 ifi
->ifi_dstaddr
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in
));
452 if (ifi
->ifi_dstaddr
== NULL
) {
455 memcpy(ifi
->ifi_dstaddr
, sinptr
, sizeof(struct sockaddr_in
));
461 #if defined(AF_INET6) && HAVE_IPV6
463 sinptr6
= (struct sockaddr_in6
*) &ifr
->ifr_addr
;
464 if (ifi
->ifi_addr
== NULL
) {
465 ifi
->ifi_addr
= calloc(1, sizeof(struct sockaddr_in6
));
466 if (ifi
->ifi_addr
== NULL
) {
470 /* Some platforms (*BSD) inject the prefix in IPv6LL addresses */
471 /* We need to strip that out */
472 if (IN6_IS_ADDR_LINKLOCAL(&sinptr6
->sin6_addr
))
473 sinptr6
->sin6_addr
.s6_addr
[2] = sinptr6
->sin6_addr
.s6_addr
[3] = 0;
474 memcpy(ifi
->ifi_addr
, sinptr6
, sizeof(struct sockaddr_in6
));
476 #ifdef SIOCGIFNETMASK_IN6
478 struct in6_ifreq ifr6
;
480 sockf6
= socket(AF_INET6
, SOCK_DGRAM
, 0);
481 bzero(&ifr6
, sizeof(ifr6
));
482 memcpy(&ifr6
.ifr_name
, &ifr
->ifr_name
, sizeof(ifr6
.ifr_name
));
483 memcpy(&ifr6
.ifr_ifru
.ifru_addr
, &ifr
->ifr_addr
, sizeof(ifr6
.ifr_ifru
.ifru_addr
));
484 if (ioctl(sockf6
, SIOCGIFNETMASK_IN6
, &ifr6
) < 0) goto gotError
;
485 ifi
->ifi_netmask
= (struct sockaddr
*)calloc(1, sizeof(struct sockaddr_in6
));
486 if (ifi
->ifi_netmask
== NULL
) goto gotError
;
487 sinptr6
= (struct sockaddr_in6
*) &ifr6
.ifr_ifru
.ifru_addr
;
488 memcpy(ifi
->ifi_netmask
, sinptr6
, sizeof(struct sockaddr_in6
));
502 if (ifihead
!= NULL
) {
503 free_ifi_info(ifihead
);
512 junk
= close(sockfd
);
516 junk
= close(sockf6
);
519 return(ifihead
); /* pointer to first structure in linked list */
521 /* end get_ifi_info3 */
523 /* include free_ifi_info */
525 free_ifi_info(struct ifi_info
*ifihead
)
527 struct ifi_info
*ifi
, *ifinext
;
529 for (ifi
= ifihead
; ifi
!= NULL
; ifi
= ifinext
) {
530 if (ifi
->ifi_addr
!= NULL
)
532 if (ifi
->ifi_brdaddr
!= NULL
)
533 free(ifi
->ifi_brdaddr
);
534 if (ifi
->ifi_dstaddr
!= NULL
)
535 free(ifi
->ifi_dstaddr
);
536 ifinext
= ifi
->ifi_next
; /* can't fetch ifi_next after free() */
537 free(ifi
); /* the ifi_info{} itself */
540 /* end free_ifi_info */
543 recvfrom_flags(int fd
, void *ptr
, size_t nbytes
, int *flagsp
,
544 struct sockaddr
*sa
, socklen_t
*salenptr
, struct my_in_pktinfo
*pktp
, u_char
*ttl
)
551 struct cmsghdr
*cmptr
;
557 *ttl
= 255; // If kernel fails to provide TTL data then assume the TTL was 255 as it should be
559 msg
.msg_control
= control_un
.control
;
560 msg
.msg_controllen
= sizeof(control_un
.control
);
563 memset(&msg
, 0, sizeof(msg
)); /* make certain msg_accrightslen = 0 */
564 #endif /* CMSG_FIRSTHDR */
566 msg
.msg_name
= (char *) sa
;
567 msg
.msg_namelen
= *salenptr
;
568 iov
[0].iov_base
= (char *)ptr
;
569 iov
[0].iov_len
= nbytes
;
573 if ( (n
= recvmsg(fd
, &msg
, *flagsp
)) < 0)
576 *salenptr
= msg
.msg_namelen
; /* pass back results */
578 /* 0.0.0.0, i/f = -1 */
579 /* We set the interface to -1 so that the caller can
580 tell whether we returned a meaningful value or
581 just some default. Previously this code just
582 set the value to 0, but I'm concerned that 0
583 might be a valid interface value.
585 memset(pktp
, 0, sizeof(struct my_in_pktinfo
));
586 pktp
->ipi_ifindex
= -1;
588 /* end recvfrom_flags1 */
590 /* include recvfrom_flags2 */
591 #ifndef CMSG_FIRSTHDR
592 #warning CMSG_FIRSTHDR not defined. Will not be able to determine destination address, received interface, etc.
593 *flagsp
= 0; /* pass back results */
597 *flagsp
= msg
.msg_flags
; /* pass back results */
598 if (msg
.msg_controllen
< (socklen_t
)sizeof(struct cmsghdr
) ||
599 (msg
.msg_flags
& MSG_CTRUNC
) || pktp
== NULL
)
602 for (cmptr
= CMSG_FIRSTHDR(&msg
); cmptr
!= NULL
;
603 cmptr
= CMSG_NXTHDR(&msg
, cmptr
)) {
606 #if in_pktinfo_definition_is_missing
610 struct in_addr ipi_spec_dst
;
611 struct in_addr ipi_addr
;
614 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
615 cmptr
->cmsg_type
== IP_PKTINFO
) {
616 struct in_pktinfo
*tmp
;
617 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&pktp
->ipi_addr
;
619 tmp
= (struct in_pktinfo
*) CMSG_DATA(cmptr
);
620 sin
->sin_family
= AF_INET
;
621 sin
->sin_addr
= tmp
->ipi_addr
;
623 pktp
->ipi_ifindex
= tmp
->ipi_ifindex
;
628 #ifdef IP_RECVDSTADDR
629 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
630 cmptr
->cmsg_type
== IP_RECVDSTADDR
) {
631 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&pktp
->ipi_addr
;
633 sin
->sin_family
= AF_INET
;
634 sin
->sin_addr
= *(struct in_addr
*)CMSG_DATA(cmptr
);
641 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
642 cmptr
->cmsg_type
== IP_RECVIF
) {
643 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*) CMSG_DATA(cmptr
);
644 #ifndef HAVE_BROKEN_RECVIF_NAME
645 int nameLen
= (sdl
->sdl_nlen
< IFI_NAME
- 1) ? sdl
->sdl_nlen
: (IFI_NAME
- 1);
646 strncpy(pktp
->ipi_ifname
, sdl
->sdl_data
, nameLen
);
648 pktp
->ipi_ifindex
= sdl
->sdl_index
;
649 assert(pktp
->ipi_ifname
[IFI_NAME
- 1] == 0);
650 // null terminated because of memset above
656 if (cmptr
->cmsg_level
== IPPROTO_IP
&&
657 cmptr
->cmsg_type
== IP_RECVTTL
) {
658 *ttl
= *(u_char
*)CMSG_DATA(cmptr
);
661 else if (cmptr
->cmsg_level
== IPPROTO_IP
&&
662 cmptr
->cmsg_type
== IP_TTL
) { // some implementations seem to send IP_TTL instead of IP_RECVTTL
663 *ttl
= *(int*)CMSG_DATA(cmptr
);
668 #if defined(IPV6_PKTINFO) && HAVE_IPV6
669 if (cmptr
->cmsg_level
== IPPROTO_IPV6
&&
670 cmptr
->cmsg_type
== IPV6_PKTINFO
) {
671 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&pktp
->ipi_addr
;
672 struct in6_pktinfo
*ip6_info
= (struct in6_pktinfo
*)CMSG_DATA(cmptr
);
674 sin6
->sin6_family
= AF_INET6
;
675 #ifndef NOT_HAVE_SA_LEN
676 sin6
->sin6_len
= sizeof(*sin6
);
678 sin6
->sin6_addr
= ip6_info
->ipi6_addr
;
679 sin6
->sin6_flowinfo
= 0;
680 sin6
->sin6_scope_id
= 0;
682 pktp
->ipi_ifindex
= ip6_info
->ipi6_ifindex
;
687 #if defined(IPV6_HOPLIMIT) && HAVE_IPV6
688 if (cmptr
->cmsg_level
== IPPROTO_IPV6
&&
689 cmptr
->cmsg_type
== IPV6_HOPLIMIT
) {
690 *ttl
= *(int*)CMSG_DATA(cmptr
);
694 assert(0); // unknown ancillary data
697 #endif /* CMSG_FIRSTHDR */
700 // **********************************************************************************************
702 // daemonize the process. Adapted from "Unix Network Programming" vol 1 by Stevens, section 12.4.
703 // Returns 0 on success, -1 on failure.
705 #ifdef NOT_HAVE_DAEMON
707 #include <sys/stat.h>
710 int daemon(int nochdir
, int noclose
)
714 case -1: return (-1); // Fork failed
715 case 0: break; // Child -- continue
716 default: _exit(0); // Parent -- exit
719 if (setsid() == -1) return(-1);
721 signal(SIGHUP
, SIG_IGN
);
723 switch (fork()) // Fork again, primarily for reasons of Unix trivia
725 case -1: return (-1); // Fork failed
726 case 0: break; // Child -- continue
727 default: _exit(0); // Parent -- exit
730 if (!nochdir
) (void)chdir("/");
735 int fd
= open("/dev/null", O_RDWR
, 0);
738 // Avoid unnecessarily duplicating a file descriptor to itself
739 if (fd
!= STDIN_FILENO
) (void)dup2(fd
, STDIN_FILENO
);
740 if (fd
!= STDOUT_FILENO
) (void)dup2(fd
, STDOUT_FILENO
);
741 if (fd
!= STDERR_FILENO
) (void)dup2(fd
, STDERR_FILENO
);
742 if (fd
!= STDIN_FILENO
&& fd
!= STDOUT_FILENO
&& fd
!= STDERR_FILENO
)
748 #endif /* NOT_HAVE_DAEMON */