147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* -*- Mode: C; tab-width: 4 -*-
247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * you may not use this file except in compliance with the License.
747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * You may obtain a copy of the License at
847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     http://www.apache.org/licenses/LICENSE-2.0
1047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
1147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
1247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
1347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * See the License for the specific language governing permissions and
1547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * limitations under the License.
1647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt */
1747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
1847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef __mDNSUNP_h
1947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define __mDNSUNP_h
2047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
2147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <sys/types.h>
2247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <sys/socket.h>
2347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <net/if.h>
2447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <netinet/in.h>
2547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
2647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef HAVE_LINUX
2747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <linux/socket.h>
2847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define IPV6_2292_PKTINFO  IPV6_2292PKTINFO
2947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define IPV6_2292_HOPLIMIT IPV6_2292HOPLIMIT
3047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
3147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The following are the supported non-linux posix OSes -
3247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// netbsd, freebsd and openbsd.
3347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if HAVE_IPV6
3447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define IPV6_2292_PKTINFO  19
3547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define IPV6_2292_HOPLIMIT 20
3647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
3747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
3847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef  __cplusplus
4047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    extern "C" {
4147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
4247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
4347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef NOT_HAVE_SOCKLEN_T
4447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    typedef unsigned int socklen_t;
4547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
4647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
47dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt#if !defined(_SS_MAXSIZE) && !defined(__ANDROID__)
4847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if HAVE_IPV6
4947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define sockaddr_storage sockaddr_in6
5047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
5147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define sockaddr_storage sockaddr
5247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif // HAVE_IPV6
53dd52342e088715929196886494c5055f9186d5c3Robert Greenwalt#endif // !defined(_SS_MAXSIZE) && !defined(__ANDROID__)
5447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef NOT_HAVE_SA_LEN
5647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define GET_SA_LEN(X) (sizeof(struct sockaddr) > ((struct sockaddr*)&(X))->sa_len ? \
5747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                       sizeof(struct sockaddr) : ((struct sockaddr*)&(X))->sa_len   )
5847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#elif HAVE_IPV6
5947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define GET_SA_LEN(X) (((struct sockaddr*)&(X))->sa_family == AF_INET  ? sizeof(struct sockaddr_in) : \
6047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                       ((struct sockaddr*)&(X))->sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr))
6147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
6247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define GET_SA_LEN(X) (((struct sockaddr*)&(X))->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr))
6347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
6447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
6547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define IFI_NAME    16          /* same as IFNAMSIZ in <net/if.h> */
6647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define IFI_HADDR    8          /* allow for 64-bit EUI-64 in future */
6747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
6847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Renamed from my_in_pktinfo because in_pktinfo is used by Linux.
6947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
7047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct my_in_pktinfo {
7147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    struct sockaddr_storage ipi_addr;
7247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    int                     ipi_ifindex;            /* received interface index */
7347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    char                    ipi_ifname[IFI_NAME];   /* received interface name  */
7447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt};
7547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
7647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* From the text (Stevens, section 20.2): */
7747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* 'As an example of recvmsg we will write a function named recvfrom_flags that */
7847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* is similar to recvfrom but also returns: */
7947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/*	1. the returned msg_flags value, */
8047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/*	2. the destination addres of the received datagram (from the IP_RECVDSTADDR socket option, and */
8147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/*	3. the index of the interface on which the datagram was received (the IP_RECVIF socket option).' */
8247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern ssize_t recvfrom_flags(int fd, void *ptr, size_t nbytes, int *flagsp,
8347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               struct sockaddr *sa, socklen_t *salenptr, struct my_in_pktinfo *pktp, u_char *ttl);
8447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
8547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct ifi_info {
8647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  char    ifi_name[IFI_NAME];   /* interface name, null terminated */
8747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  u_char  ifi_haddr[IFI_HADDR]; /* hardware address */
8847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  u_short ifi_hlen;             /* #bytes in hardware address: 0, 6, 8 */
8947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  short   ifi_flags;            /* IFF_xxx constants from <net/if.h> */
9047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  short   ifi_myflags;          /* our own IFI_xxx flags */
9147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  int     ifi_index;            /* interface index */
9247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  struct sockaddr  *ifi_addr;   /* primary address */
9347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  struct sockaddr  *ifi_netmask;
9447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  struct sockaddr  *ifi_brdaddr;/* broadcast address */
9547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  struct sockaddr  *ifi_dstaddr;/* destination address */
9647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  struct ifi_info  *ifi_next;   /* next of these structures */
9747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt};
9847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
9947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
10047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define PROC_IFINET6_PATH "/proc/net/if_inet6"
10147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern struct ifi_info  *get_ifi_info_linuxv6(int family, int doaliases);
10247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
10347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if defined(AF_INET6) && HAVE_IPV6
10547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define INET6_ADDRSTRLEN 46 /*Maximum length of IPv6 address */
10647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
10747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define IFI_ALIAS   1           /* ifi_addr is an alias */
11147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* From the text (Stevens, section 16.6): */
11347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* 'Since many programs need to know all the interfaces on a system, we will develop a */
11447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* function of our own named get_ifi_info that returns a linked list of structures, one */
11547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* for each interface that is currently "up."' */
11647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern struct ifi_info  *get_ifi_info(int family, int doaliases);
11747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* 'The free_ifi_info function, which takes a pointer that was */
11947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* returned by get_ifi_info and frees all the dynamic memory.' */
12047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void             free_ifi_info(struct ifi_info *);
12147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
12247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef NOT_HAVE_DAEMON
12347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern int daemon(int nochdir, int noclose);
12447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
12547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
12647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef  __cplusplus
12747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    }
12847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
12947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
13047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
131