1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2002-2018 Apple 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 #ifndef __mDNSUNP_h
19 #define __mDNSUNP_h
20 
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 
26 #ifdef HAVE_LINUX
27 #include <linux/socket.h>
28 #define IPV6_2292_PKTINFO  IPV6_2292PKTINFO
29 #define IPV6_2292_HOPLIMIT IPV6_2292HOPLIMIT
30 #else
31 // The following are the supported non-linux posix OSes -
32 // netbsd, freebsd and openbsd.
33 #if HAVE_IPV6
34 #define IPV6_2292_PKTINFO  19
35 #define IPV6_2292_HOPLIMIT 20
36 #endif
37 #endif
38 
39 #ifdef  __cplusplus
40 extern "C" {
41 #endif
42 
43 #ifdef NOT_HAVE_SOCKLEN_T
44 typedef unsigned int socklen_t;
45 #endif
46 
47 #ifndef NOT_HAVE_SA_LEN
48 #define GET_SA_LEN(X) (sizeof(struct sockaddr) > ((struct sockaddr*)&(X))->sa_len ? \
49                        sizeof(struct sockaddr) : ((struct sockaddr*)&(X))->sa_len   )
50 #elif HAVE_IPV6
51 #define GET_SA_LEN(X) (((struct sockaddr*)&(X))->sa_family == AF_INET  ? sizeof(struct sockaddr_in) : \
52                        ((struct sockaddr*)&(X))->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr))
53 #else
54 #define GET_SA_LEN(X) (((struct sockaddr*)&(X))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr))
55 #endif
56 
57 #define IFI_NAME    IFNAMSIZ    /* same as IFNAMSIZ in <net/if.h> */
58 #define IFI_HADDR   8           /* allow for 64-bit EUI-64 in future */
59 
60 // Renamed from my_in_pktinfo because in_pktinfo is used by Linux.
61 
62 struct my_in_pktinfo {
63     struct sockaddr_storage ipi_addr;
64     int ipi_ifindex;                                /* received interface index */
65     char ipi_ifname[IFI_NAME];                      /* received interface name  */
66 };
67 
68 /* From the text (Stevens, section 20.2): */
69 /* 'As an example of recvmsg we will write a function named recvfrom_flags that */
70 /* is similar to recvfrom but also returns: */
71 /*	1. the returned msg_flags value, */
72 /*	2. the destination addres of the received datagram (from the IP_RECVDSTADDR socket option, and */
73 /*	3. the index of the interface on which the datagram was received (the IP_RECVIF socket option).' */
74 extern ssize_t recvfrom_flags(int fd, void *ptr, size_t nbytes, int *flagsp,
75                               struct sockaddr *sa, socklen_t *salenptr, struct my_in_pktinfo *pktp, u_char *ttl);
76 
77 struct ifi_info {
78     char ifi_name[IFI_NAME];    /* interface name, null terminated */
79     u_char ifi_haddr[IFI_HADDR]; /* hardware address */
80     u_short ifi_hlen;           /* #bytes in hardware address: 0, 6, 8 */
81     short ifi_flags;            /* IFF_xxx constants from <net/if.h> */
82     short ifi_myflags;          /* our own IFI_xxx flags */
83     int ifi_index;              /* interface index */
84     struct sockaddr  *ifi_addr; /* primary address */
85     struct sockaddr  *ifi_netmask;
86     struct sockaddr  *ifi_brdaddr; /* broadcast address */
87     struct sockaddr  *ifi_dstaddr; /* destination address */
88     struct ifi_info  *ifi_next; /* next of these structures */
89 };
90 
91 #define IFI_ALIAS   1           /* ifi_addr is an alias */
92 
93 /* From the text (Stevens, section 16.6): */
94 /* 'Since many programs need to know all the interfaces on a system, we will develop a */
95 /* function of our own named get_ifi_info that returns a linked list of structures, one */
96 /* for each interface that is currently "up."' */
97 extern struct ifi_info  *get_ifi_info(int family, int doaliases);
98 
99 /* 'The free_ifi_info function, which takes a pointer that was */
100 /* returned by get_ifi_info and frees all the dynamic memory.' */
101 extern void free_ifi_info(struct ifi_info *);
102 
103 #if defined(AF_INET6) && HAVE_IPV6
104 #define INET6_ADDRSTRLEN 46 /*Maximum length of IPv6 address */
105 #endif
106 
107 #ifdef NOT_HAVE_DAEMON
108 extern int daemon(int nochdir, int noclose);
109 #endif
110 
111 #ifdef  __cplusplus
112 }
113 #endif
114 
115 #endif
116