14b22b933Srs /* -*- Mode: C; tab-width: 4 -*-
24b22b933Srs  *
33b436d06SToomas Soome  * Copyright (c) 2002-2018 Apple Inc. All rights reserved.
44b22b933Srs  *
54b22b933Srs  * Licensed under the Apache License, Version 2.0 (the "License");
64b22b933Srs  * you may not use this file except in compliance with the License.
74b22b933Srs  * You may obtain a copy of the License at
85ffb0c9bSToomas Soome  *
94b22b933Srs  *     http://www.apache.org/licenses/LICENSE-2.0
105ffb0c9bSToomas Soome  *
114b22b933Srs  * Unless required by applicable law or agreed to in writing, software
124b22b933Srs  * distributed under the License is distributed on an "AS IS" BASIS,
134b22b933Srs  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144b22b933Srs  * See the License for the specific language governing permissions and
154b22b933Srs  * limitations under the License.
165ffb0c9bSToomas Soome  */
184b22b933Srs #ifndef __mDNSUNP_h
194b22b933Srs #define __mDNSUNP_h
214b22b933Srs #include <sys/types.h>
224b22b933Srs #include <sys/socket.h>
234b22b933Srs #include <net/if.h>
244b22b933Srs #include <netinet/in.h>
264b22b933Srs #ifdef HAVE_LINUX
274b22b933Srs #include <linux/socket.h>
285ffb0c9bSToomas Soome #define IPV6_2292_PKTINFO  IPV6_2292PKTINFO
295ffb0c9bSToomas Soome #define IPV6_2292_HOPLIMIT IPV6_2292HOPLIMIT
305ffb0c9bSToomas Soome #else
315ffb0c9bSToomas Soome // The following are the supported non-linux posix OSes -
325ffb0c9bSToomas Soome // netbsd, freebsd and openbsd.
335ffb0c9bSToomas Soome #if HAVE_IPV6
345ffb0c9bSToomas Soome #define IPV6_2292_PKTINFO  19
355ffb0c9bSToomas Soome #define IPV6_2292_HOPLIMIT 20
365ffb0c9bSToomas Soome #endif
374b22b933Srs #endif
394b22b933Srs #ifdef  __cplusplus
405ffb0c9bSToomas Soome extern "C" {
414b22b933Srs #endif
434b22b933Srs #ifdef NOT_HAVE_SOCKLEN_T
445ffb0c9bSToomas Soome typedef unsigned int socklen_t;
454b22b933Srs #endif
474b22b933Srs #ifndef NOT_HAVE_SA_LEN
484b22b933Srs #define GET_SA_LEN(X) (sizeof(struct sockaddr) > ((struct sockaddr*)&(X))->sa_len ? \
494b22b933Srs                        sizeof(struct sockaddr) : ((struct sockaddr*)&(X))->sa_len   )
504b22b933Srs #elif HAVE_IPV6
514b22b933Srs #define GET_SA_LEN(X) (((struct sockaddr*)&(X))->sa_family == AF_INET  ? sizeof(struct sockaddr_in) : \
524b22b933Srs                        ((struct sockaddr*)&(X))->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr))
534b22b933Srs #else
544b22b933Srs #define GET_SA_LEN(X) (((struct sockaddr*)&(X))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr))
554b22b933Srs #endif
573b436d06SToomas Soome #define IFI_NAME    IFNAMSIZ    /* same as IFNAMSIZ in <net/if.h> */
583b436d06SToomas Soome #define IFI_HADDR   8           /* allow for 64-bit EUI-64 in future */
604b22b933Srs // Renamed from my_in_pktinfo because in_pktinfo is used by Linux.
624b22b933Srs struct my_in_pktinfo {
634b22b933Srs     struct sockaddr_storage ipi_addr;
645ffb0c9bSToomas Soome     int ipi_ifindex;                                /* received interface index */
655ffb0c9bSToomas Soome     char ipi_ifname[IFI_NAME];                      /* received interface name  */
664b22b933Srs };
684b22b933Srs /* From the text (Stevens, section 20.2): */
694b22b933Srs /* 'As an example of recvmsg we will write a function named recvfrom_flags that */
704b22b933Srs /* is similar to recvfrom but also returns: */
714b22b933Srs /*	1. the returned msg_flags value, */
724b22b933Srs /*	2. the destination addres of the received datagram (from the IP_RECVDSTADDR socket option, and */
734b22b933Srs /*	3. the index of the interface on which the datagram was received (the IP_RECVIF socket option).' */
744b22b933Srs extern ssize_t recvfrom_flags(int fd, void *ptr, size_t nbytes, int *flagsp,
755ffb0c9bSToomas Soome                               struct sockaddr *sa, socklen_t *salenptr, struct my_in_pktinfo *pktp, u_char *ttl);
774b22b933Srs struct ifi_info {
785ffb0c9bSToomas Soome     char ifi_name[IFI_NAME];    /* interface name, null terminated */
795ffb0c9bSToomas Soome     u_char ifi_haddr[IFI_HADDR]; /* hardware address */
805ffb0c9bSToomas Soome     u_short ifi_hlen;           /* #bytes in hardware address: 0, 6, 8 */
815ffb0c9bSToomas Soome     short ifi_flags;            /* IFF_xxx constants from <net/if.h> */
825ffb0c9bSToomas Soome     short ifi_myflags;          /* our own IFI_xxx flags */
835ffb0c9bSToomas Soome     int ifi_index;              /* interface index */
845ffb0c9bSToomas Soome     struct sockaddr  *ifi_addr; /* primary address */
855ffb0c9bSToomas Soome     struct sockaddr  *ifi_netmask;
865ffb0c9bSToomas Soome     struct sockaddr  *ifi_brdaddr; /* broadcast address */
875ffb0c9bSToomas Soome     struct sockaddr  *ifi_dstaddr; /* destination address */
885ffb0c9bSToomas Soome     struct ifi_info  *ifi_next; /* next of these structures */
894b22b933Srs };
914b22b933Srs #define IFI_ALIAS   1           /* ifi_addr is an alias */
934b22b933Srs /* From the text (Stevens, section 16.6): */
944b22b933Srs /* 'Since many programs need to know all the interfaces on a system, we will develop a */
954b22b933Srs /* function of our own named get_ifi_info that returns a linked list of structures, one */
964b22b933Srs /* for each interface that is currently "up."' */
974b22b933Srs extern struct ifi_info  *get_ifi_info(int family, int doaliases);
994b22b933Srs /* 'The free_ifi_info function, which takes a pointer that was */
1004b22b933Srs /* returned by get_ifi_info and frees all the dynamic memory.' */
101*472cd20dSToomas Soome extern void free_ifi_info(struct ifi_info *);
102*472cd20dSToomas Soome 
103*472cd20dSToomas Soome #if defined(AF_INET6) && HAVE_IPV6
104*472cd20dSToomas Soome #define INET6_ADDRSTRLEN 46 /*Maximum length of IPv6 address */
105*472cd20dSToomas Soome #endif
1074b22b933Srs #ifdef NOT_HAVE_DAEMON
1084b22b933Srs extern int daemon(int nochdir, int noclose);
1094b22b933Srs #endif
1114b22b933Srs #ifdef  __cplusplus
1125ffb0c9bSToomas Soome }
1134b22b933Srs #endif
1154b22b933Srs #endif