/* -*- Mode: C; tab-width: 4 -*-
*
* Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mDNSUNP.h"
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
/* Some weird platforms derived from 4.4BSD Lite (e.g. EFI) need the ALIGN(P)
CMSG_NXTHDR macro is well-formed. On such platforms, the symbol NEED_ALIGN_MACRO
should be set to the name of the header to include to get the ALIGN(P) macro.
*/
#ifdef NEED_ALIGN_MACRO
#include NEED_ALIGN_MACRO
#endif
other platforms don't even have that include file. So,
if we haven't yet got a definition, let's try to find
*/
#ifndef SIOCGIFCONF
#endif
/* sockaddr_dl is only referenced if we're using IP_RECVIF,
so only include the header in that case.
*/
#ifdef IP_RECVIF
#endif
#if !HAVE_SOLARIS
#else
#include <alloca.h>
#endif /* !HAVE_SOLARIS */
#endif
#include <netdb.h>
/* Converts a prefix length to IPv6 network mask */
int i;
for(i=0; i<=colons; i++) {
else ones_in_block=plen;
plen -= ones_in_block;
}
}
/* Gets IPv6 interface information from the /proc filesystem in linux*/
{
int err;
lastname[0] = 0;
if (sockfd < 0) {
goto gotError;
}
"%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %8s\n",
myflags = 0;
if (doaliases == 0)
continue; /* already processed this interface */
}
goto gotError;
}
/* Add address of the interface */
if (err) {
goto gotError;
}
goto gotError;
}
/* Add netmask of the interface */
goto gotError;
}
/* Add interface name */
/* Add interface index */
/* Add interface flags*/
if (errno == EADDRNOTAVAIL) {
/*
* If the main interface is configured with no IP address but
* an alias interface exists with an IP address, you get
* EADDRNOTAVAIL for the main interface
*/
continue;
} else {
goto gotError;
}
}
}
}
goto done;
}
}
done:
if (sockfd != -1) {
}
}
return(ifihead); /* pointer to first structure in linked list */
}
#endif // defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
#if HAVE_SOLARIS
/*
* Converts prefix length to network mask. Assumes
* addr points to a zeroed out buffer and prefix <= sizeof(addr)
* Unlike plen_to_mask returns netmask in binary form and not
* in text form.
*/
*addr++ = 0xff;
}
/*
* This function goes through all the IP interfaces associated with a
* physical interface and finds the best matched one for use by mDNS.
* Returns NULL when none of the IP interfaces associated with a physical
* interface are usable. Otherwise returns the best matched interface
* information and a pointer to the best matched lifreq.
*/
struct ifi_info *
{
char *chptr;
int i;
/*
* Check all logical interfaces associated with the physical
* interface and figure out which one works best for us.
*/
continue; /* skip interface */
/* Strip logical interface number before checking ifname */
*chptr = '\0';
/*
* Check ifname to see if the logical interface is associated
* with the physical interface we are interested in.
*/
continue;
/* interface removed */
continue;
return(NULL);
}
/* ignore address if not up */
continue;
/*
* Avoid address if any of the following flags are set:
* IFF_NOXMIT: no packets transmitted over interface
* IFF_NOLOCAL: no address
* IFF_PRIVATE: is not advertised
*/
continue;
/* A DHCP client will have IFF_UP set yet the address is zero. Ignore */
continue;
}
/*
* Check if we found a better interface by checking
* the flags. If flags are identical we prefer
* the new found interface.
*/
/* If interface has a different set of flags */
if (diff_flags != 0) {
/* Check flags in increasing order of ones we prefer */
/* Address temporary? */
if ((diff_flags & IFF_TEMPORARY) &&
(ifflags & IFF_TEMPORARY))
continue;
/* Deprecated address? */
if ((diff_flags & IFF_DEPRECATED) &&
(ifflags & IFF_DEPRECATED))
continue;
/* Last best-matched interface address has preferred? */
if ((diff_flags & IFF_PREFERRED) &&
((ifflags & IFF_PREFERRED) == 0))
continue;
}
}
/* Set best match interface & flags */
}
return(NULL);
/* Found a match: return the interface information */
return(NULL);
if (strlcpy(ifi->ifi_name, (*best_lifr)->lifr_name, sizeof(ifi->ifi_name)) >= sizeof(ifi->ifi_name)) {
return(NULL);
}
return(ifi);
}
/*
* Returns a list of IP interface information on Solaris. The function
* returns all IP interfaces on the system with IPv4 address assigned
* when passed AF_INET and returns IP interfaces with IPv6 address assigned
* when AF_INET6 is passed.
*/
{
int sockfd;
int len;
char *buf;
char *cptr;
#endif
if (sockfd < 0)
goto gotError;
lifn.lifn_flags = 0;
goto gotError;
/*
* Pad interface count to detect & retrieve any
* additional interfaces between IFNUM & IFCONF calls.
*/
lifc.lifc_flags = 0;
goto gotError;
goto again;
continue;
/*
* See if we have already processed the interface
* by checking the interface names.
*/
goto gotError;
*cptr = '\0';
/*
* If any of the interfaces found so far share the physical
* interface name then we have already processed the interface.
*/
/* Retrieve physical interface name */
/* Strip logical interface number before checking ifname */
*cptr = '\0';
break;
}
continue; /* already processed */
/*
* New interface, find the one with the preferred source
* address for our use in Multicast DNS.
*/
continue;
case AF_INET6:
goto gotError;
goto gotError;
break;
#endif
case AF_INET:
goto gotError;
/* interface removed */
continue;
}
goto gotError;
}
goto gotError;
break;
default:
/* never reached */
break;
}
}
return(ifihead); /* pointer to first structure in linked list */
if (sockfd != -1)
return(NULL);
}
#endif /* HAVE_SOLARIS */
{
int junk;
#ifdef NOT_HAVE_IF_NAMETOINDEX
#endif
#endif
return get_ifi_info_solaris(family);
#endif
sockfd = -1;
sockf6 = -1;
if (sockfd < 0) {
goto gotError;
}
lastlen = 0;
for ( ; ; ) {
goto gotError;
}
goto gotError;
}
} else {
break; /* success, len has not changed */
}
}
lastname[0] = 0;
/* end get_ifi_info1 */
/* include get_ifi_info2 */
/* Advance to next one in buffer */
else
// fprintf(stderr, "intf %p name=%s AF=%d\n", index, ifr->ifr_name, ifr->ifr_addr.sa_family);
continue; /* ignore if not desired address family */
myflags = 0;
*cptr = 0; /* replace colon will null */
if (doaliases == 0)
continue; /* already processed this interface */
}
goto gotError;
}
continue; /* ignore if interface not up */
goto gotError;
}
#ifndef NOT_HAVE_IF_NAMETOINDEX
#else
#ifdef SIOCGIFINDEX
else
#endif
#endif
/* end get_ifi_info2 */
/* include get_ifi_info3 */
case AF_INET:
goto gotError;
}
#ifdef SIOCGIFNETMASK
if (errno == EADDRNOTAVAIL) {
/*
* If the main interface is configured with no IP address but
* an alias interface exists with an IP address, you get
* EADDRNOTAVAIL for the main interface
*/
continue;
} else {
goto gotError;
}
}
/* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
#ifndef NOT_HAVE_SA_LEN
#endif
#endif
#ifdef SIOCGIFBRDADDR
if (flags & IFF_BROADCAST) {
goto gotError;
}
/* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
#ifndef NOT_HAVE_SA_LEN
#endif
goto gotError;
}
}
#endif
#ifdef SIOCGIFDSTADDR
if (flags & IFF_POINTOPOINT) {
goto gotError;
}
/* The BSD ioctls (including Mac OS X) stick some weird values in for sin_len and sin_family */
#ifndef NOT_HAVE_SA_LEN
#endif
goto gotError;
}
}
#endif
}
break;
case AF_INET6:
goto gotError;
}
/* Some platforms (*BSD) inject the prefix in IPv6LL addresses */
/* We need to strip that out */
#ifdef SIOCGIFNETMASK_IN6
{
if (sockf6 == -1)
if (errno == EADDRNOTAVAIL) {
/*
* If the main interface is configured with no IP address but
* an alias interface exists with an IP address, you get
* EADDRNOTAVAIL for the main interface
*/
continue;
} else {
goto gotError;
}
}
}
#endif
}
break;
#endif
default:
break;
}
}
goto done;
}
done:
}
if (sockfd != -1) {
}
if (sockf6 != -1) {
}
return(ifihead); /* pointer to first structure in linked list */
}
/* end get_ifi_info3 */
/* include free_ifi_info */
void
{
}
}
/* end free_ifi_info */
{
ssize_t n;
#ifdef CMSG_FIRSTHDR
union {
} control_un;
#else
#endif /* CMSG_FIRSTHDR */
return(n);
if (pktp) {
/* 0.0.0.0, i/f = -1 */
/* We set the interface to -1 so that the caller can
tell whether we returned a meaningful value or
just some default. Previously this code just
set the value to 0, but I'm concerned that 0
might be a valid interface value.
*/
}
/* end recvfrom_flags1 */
/* include recvfrom_flags2 */
#ifndef CMSG_FIRSTHDR
#warning CMSG_FIRSTHDR not defined. Will not be able to determine destination address, received interface, etc.
*flagsp = 0; /* pass back results */
return(n);
#else
return(n);
#ifdef IP_PKTINFO
struct in_pktinfo
{
int ipi_ifindex;
struct in_addr ipi_spec_dst;
};
#endif
struct in_pktinfo *tmp;
continue;
}
#endif
#ifdef IP_RECVDSTADDR
continue;
}
#endif
#ifdef IP_RECVIF
#ifndef HAVE_BROKEN_RECVIF_NAME
#endif
/*
* the is memcpy used for sparc? no idea;)
* pktp->ipi_ifindex = sdl->sdl_index;
*/
#ifdef HAVE_BROKEN_RECVIF_NAME
}
#endif
// null terminated because of memset above
continue;
}
#endif
#ifdef IP_RECVTTL
continue;
}
continue;
}
#endif
#if defined(IPV6_PKTINFO) && HAVE_IPV6
#ifndef NOT_HAVE_SA_LEN
#endif
sin6->sin6_flowinfo = 0;
sin6->sin6_scope_id = 0;
continue;
}
#endif
#if defined(IPV6_HOPLIMIT) && HAVE_IPV6
continue;
}
#endif
assert(0); // unknown ancillary data
}
return(n);
#endif /* CMSG_FIRSTHDR */
}
// **********************************************************************************************
// daemonize the process. Adapted from "Unix Network Programming" vol 1 by Stevens, section 12.4.
// Returns 0 on success, -1 on failure.
#ifdef NOT_HAVE_DAEMON
#include <fcntl.h>
{
switch (fork())
{
case -1: return (-1); // Fork failed
case 0: break; // Child -- continue
default: _exit(0); // Parent -- exit
}
switch (fork()) // Fork again, primarily for reasons of Unix trivia
{
case -1: return (-1); // Fork failed
case 0: break; // Child -- continue
default: _exit(0); // Parent -- exit
}
umask(0);
if (!noclose)
{
if (fd != -1)
{
// Avoid unnecessarily duplicating a file descriptor to itself
}
}
return (0);
}
#endif /* NOT_HAVE_DAEMON */