Home | History | Annotate | Line # | Download | only in rpcbind
util.c revision 1.14.6.1
      1  1.14.6.1      yamt /*	$NetBSD: util.c,v 1.14.6.1 2008/05/18 12:36:23 yamt Exp $	*/
      2       1.1      fvdl 
      3       1.1      fvdl /*-
      4       1.1      fvdl  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5       1.1      fvdl  * All rights reserved.
      6       1.1      fvdl  *
      7       1.1      fvdl  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      fvdl  * by Frank van der Linden.
      9       1.1      fvdl  *
     10       1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     11       1.1      fvdl  * modification, are permitted provided that the following conditions
     12       1.1      fvdl  * are met:
     13       1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     14       1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     15       1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     18       1.1      fvdl  *
     19       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1      fvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1      fvdl  */
     31       1.1      fvdl 
     32       1.1      fvdl #include <sys/types.h>
     33       1.1      fvdl #include <sys/socket.h>
     34       1.1      fvdl #include <sys/queue.h>
     35       1.1      fvdl #include <net/if.h>
     36       1.1      fvdl #include <netinet/in.h>
     37      1.12  christos #include <assert.h>
     38       1.1      fvdl #include <ifaddrs.h>
     39       1.8    itojun #include <poll.h>
     40       1.1      fvdl #include <rpc/rpc.h>
     41       1.1      fvdl #include <errno.h>
     42       1.1      fvdl #include <stdlib.h>
     43       1.2   thorpej #include <string.h>
     44       1.1      fvdl #include <unistd.h>
     45       1.1      fvdl #include <netdb.h>
     46       1.1      fvdl #include <netconfig.h>
     47       1.1      fvdl #include <stdio.h>
     48       1.1      fvdl #include <arpa/inet.h>
     49       1.1      fvdl 
     50       1.1      fvdl #include "rpcbind.h"
     51       1.1      fvdl 
     52       1.1      fvdl static struct sockaddr_in *local_in4;
     53       1.1      fvdl #ifdef INET6
     54       1.1      fvdl static struct sockaddr_in6 *local_in6;
     55       1.1      fvdl #endif
     56       1.1      fvdl 
     57      1.13  christos static int bitmaskcmp(void *, void *, void *, int);
     58       1.1      fvdl #ifdef INET6
     59      1.13  christos static void in6_fillscopeid(struct sockaddr_in6 *);
     60       1.1      fvdl #endif
     61       1.1      fvdl 
     62       1.1      fvdl /*
     63       1.1      fvdl  * For all bits set in "mask", compare the corresponding bits in
     64       1.1      fvdl  * "dst" and "src", and see if they match.
     65       1.1      fvdl  */
     66       1.1      fvdl static int
     67       1.1      fvdl bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
     68       1.1      fvdl {
     69       1.1      fvdl 	int i, j;
     70       1.1      fvdl 	u_int8_t *p1 = dst, *p2 = src, *netmask = mask;
     71       1.1      fvdl 	u_int8_t bitmask;
     72       1.1      fvdl 
     73       1.1      fvdl 	for (i = 0; i < bytelen; i++) {
     74       1.1      fvdl 		for (j = 0; j < 8; j++) {
     75       1.1      fvdl 			bitmask = 1 << j;
     76       1.1      fvdl 			if (!(netmask[i] & bitmask))
     77       1.1      fvdl 				continue;
     78       1.1      fvdl 			if ((p1[i] & bitmask) != (p2[i] & bitmask))
     79       1.1      fvdl 				return 1;
     80       1.1      fvdl 		}
     81       1.1      fvdl 	}
     82       1.1      fvdl 
     83       1.1      fvdl 	return 0;
     84       1.1      fvdl }
     85       1.1      fvdl 
     86       1.1      fvdl /*
     87       1.1      fvdl  * Taken from ifconfig.c
     88       1.1      fvdl  */
     89       1.1      fvdl #ifdef INET6
     90       1.1      fvdl static void
     91       1.1      fvdl in6_fillscopeid(struct sockaddr_in6 *sin6)
     92       1.1      fvdl {
     93       1.1      fvdl         if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
     94       1.1      fvdl                 sin6->sin6_scope_id =
     95       1.1      fvdl                         ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
     96       1.1      fvdl                 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
     97       1.1      fvdl         }
     98       1.1      fvdl }
     99       1.1      fvdl #endif
    100       1.1      fvdl 
    101       1.1      fvdl char *
    102       1.1      fvdl addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
    103       1.1      fvdl 	  char *netid)
    104       1.1      fvdl {
    105       1.4      fvdl 	struct ifaddrs *ifap, *ifp, *bestif;
    106       1.1      fvdl #ifdef INET6
    107       1.1      fvdl 	struct sockaddr_in6 *servsin6, *sin6mask, *clntsin6, *ifsin6, *realsin6;
    108       1.1      fvdl 	struct sockaddr_in6 *newsin6;
    109       1.1      fvdl #endif
    110       1.1      fvdl 	struct sockaddr_in *servsin, *sinmask, *clntsin, *newsin, *ifsin;
    111       1.1      fvdl 	struct netbuf *serv_nbp, *clnt_nbp = NULL, tbuf;
    112       1.1      fvdl 	struct sockaddr *serv_sa;
    113       1.1      fvdl 	struct sockaddr *clnt_sa;
    114       1.1      fvdl 	struct sockaddr_storage ss;
    115       1.1      fvdl 	struct netconfig *nconf;
    116       1.1      fvdl 	struct sockaddr *clnt = caller->buf;
    117       1.1      fvdl 	char *ret = NULL;
    118       1.1      fvdl 
    119      1.11     lukem #ifdef INET6
    120      1.10     lukem 	servsin6 = ifsin6 = newsin6 = NULL;	/* XXXGCC -Wuninitialized */
    121      1.11     lukem #endif
    122      1.10     lukem 	servsin = newsin = NULL;		/* XXXGCC -Wuninitialized */
    123      1.10     lukem 
    124      1.14       dsl #ifdef RPCBIND_DEBUG
    125       1.1      fvdl 	if (debugging)
    126       1.1      fvdl 		fprintf(stderr, "addrmerge(caller, %s, %s, %s\n", serv_uaddr,
    127       1.1      fvdl 		    clnt_uaddr, netid);
    128       1.1      fvdl #endif
    129       1.1      fvdl 	nconf = getnetconfigent(netid);
    130       1.1      fvdl 	if (nconf == NULL)
    131       1.1      fvdl 		return NULL;
    132       1.1      fvdl 
    133       1.4      fvdl 	/*
    134       1.4      fvdl 	 * Local merge, just return a duplicate.
    135       1.4      fvdl 	 */
    136       1.4      fvdl 	if (clnt_uaddr != NULL && strncmp(clnt_uaddr, "0.0.0.0.", 8) == 0)
    137       1.4      fvdl 		return strdup(clnt_uaddr);
    138       1.4      fvdl 
    139       1.1      fvdl 	serv_nbp = uaddr2taddr(nconf, serv_uaddr);
    140       1.1      fvdl 	if (serv_nbp == NULL)
    141       1.1      fvdl 		return NULL;
    142       1.1      fvdl 
    143       1.1      fvdl 	serv_sa = (struct sockaddr *)serv_nbp->buf;
    144       1.1      fvdl 	if (clnt_uaddr != NULL) {
    145       1.1      fvdl 		clnt_nbp = uaddr2taddr(nconf, clnt_uaddr);
    146       1.6      fvdl 		if (clnt_nbp == NULL) {
    147       1.6      fvdl 			free(serv_nbp);
    148       1.6      fvdl 			return NULL;
    149       1.6      fvdl 		}
    150       1.1      fvdl 		clnt_sa = (struct sockaddr *)clnt_nbp->buf;
    151       1.6      fvdl 		if (clnt_sa->sa_family == AF_LOCAL) {
    152       1.6      fvdl 			free(serv_nbp);
    153       1.6      fvdl 			free(clnt_nbp);
    154       1.6      fvdl 			free(clnt_sa);
    155       1.7      fvdl 			return strdup(serv_uaddr);
    156       1.6      fvdl 		}
    157       1.1      fvdl 	} else {
    158       1.1      fvdl 		clnt_sa = (struct sockaddr *)
    159       1.1      fvdl 		    malloc(sizeof (struct sockaddr_storage));
    160       1.1      fvdl 		memcpy(clnt_sa, clnt, clnt->sa_len);
    161       1.1      fvdl 	}
    162       1.1      fvdl 
    163       1.6      fvdl 	if (getifaddrs(&ifp) < 0) {
    164       1.6      fvdl 		free(serv_nbp);
    165       1.6      fvdl 		free(clnt_sa);
    166       1.6      fvdl 		if (clnt_nbp != NULL)
    167       1.6      fvdl 			free(clnt_nbp);
    168       1.1      fvdl 		return 0;
    169       1.6      fvdl 	}
    170       1.1      fvdl 
    171       1.1      fvdl 	/*
    172       1.1      fvdl 	 * Loop through all interfaces. For each interface, see if the
    173       1.1      fvdl 	 * network portion of its address is equal to that of the client.
    174       1.1      fvdl 	 * If so, we have found the interface that we want to use.
    175       1.1      fvdl 	 */
    176       1.1      fvdl 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
    177       1.1      fvdl 		if (ifap->ifa_addr->sa_family != clnt->sa_family ||
    178       1.1      fvdl 		    !(ifap->ifa_flags & IFF_UP))
    179       1.1      fvdl 			continue;
    180       1.1      fvdl 
    181       1.1      fvdl 		switch (clnt->sa_family) {
    182       1.1      fvdl 		case AF_INET:
    183       1.1      fvdl 			/*
    184       1.1      fvdl 			 * realsin: address that recvfrom gave us.
    185       1.1      fvdl 			 * ifsin: address of interface being examined.
    186       1.1      fvdl 			 * clntsin: address that client want us to contact
    187       1.1      fvdl 			 *           it on
    188       1.1      fvdl 			 * servsin: local address of RPC service.
    189       1.1      fvdl 			 * sinmask: netmask of this interface
    190       1.1      fvdl 			 * newsin: initially a copy of clntsin, eventually
    191       1.1      fvdl 			 *         the merged address
    192       1.1      fvdl 			 */
    193       1.1      fvdl 			servsin = (struct sockaddr_in *)serv_sa;
    194       1.1      fvdl 			clntsin = (struct sockaddr_in *)clnt_sa;
    195       1.1      fvdl 			sinmask = (struct sockaddr_in *)ifap->ifa_netmask;
    196       1.1      fvdl 			newsin = (struct sockaddr_in *)&ss;
    197       1.1      fvdl 			ifsin = (struct sockaddr_in *)ifap->ifa_addr;
    198       1.1      fvdl 			if (!bitmaskcmp(&ifsin->sin_addr, &clntsin->sin_addr,
    199       1.1      fvdl 			    &sinmask->sin_addr, sizeof (struct in_addr))) {
    200       1.1      fvdl 				goto found;
    201       1.1      fvdl 			}
    202       1.1      fvdl 			break;
    203       1.1      fvdl #ifdef INET6
    204       1.1      fvdl 		case AF_INET6:
    205       1.1      fvdl 			/*
    206       1.1      fvdl 			 * realsin6: address that recvfrom gave us.
    207       1.1      fvdl 			 * ifsin6: address of interface being examined.
    208       1.1      fvdl 			 * clntsin6: address that client want us to contact
    209       1.1      fvdl 			 *           it on
    210       1.1      fvdl 			 * servsin6: local address of RPC service.
    211       1.1      fvdl 			 * sin6mask: netmask of this interface
    212       1.1      fvdl 			 * newsin6: initially a copy of clntsin, eventually
    213       1.1      fvdl 			 *          the merged address
    214       1.1      fvdl 			 *
    215       1.1      fvdl 			 * For v6 link local addresses, if the client contacted
    216       1.1      fvdl 			 * us via a link-local address, and wants us to reply
    217       1.1      fvdl 			 * to one, use the scope id to see which one.
    218       1.1      fvdl 			 */
    219       1.1      fvdl 			realsin6 = (struct sockaddr_in6 *)clnt;
    220       1.1      fvdl 			ifsin6 = (struct sockaddr_in6 *)ifap->ifa_addr;
    221       1.1      fvdl 			in6_fillscopeid(ifsin6);
    222       1.1      fvdl 			clntsin6 = (struct sockaddr_in6 *)clnt_sa;
    223       1.1      fvdl 			servsin6 = (struct sockaddr_in6 *)serv_sa;
    224       1.1      fvdl 			sin6mask = (struct sockaddr_in6 *)ifap->ifa_netmask;
    225       1.1      fvdl 			newsin6 = (struct sockaddr_in6 *)&ss;
    226       1.1      fvdl 			if (IN6_IS_ADDR_LINKLOCAL(&ifsin6->sin6_addr) &&
    227       1.1      fvdl 			    IN6_IS_ADDR_LINKLOCAL(&realsin6->sin6_addr) &&
    228       1.1      fvdl 			    IN6_IS_ADDR_LINKLOCAL(&clntsin6->sin6_addr)) {
    229       1.1      fvdl 				if (ifsin6->sin6_scope_id !=
    230       1.1      fvdl 				    realsin6->sin6_scope_id)
    231       1.1      fvdl 					continue;
    232       1.1      fvdl 				goto found;
    233       1.1      fvdl 			}
    234       1.1      fvdl 			if (!bitmaskcmp(&ifsin6->sin6_addr,
    235       1.1      fvdl 			    &clntsin6->sin6_addr, &sin6mask->sin6_addr,
    236       1.1      fvdl 			    sizeof (struct in6_addr)))
    237       1.5       jmc 				goto found;
    238       1.1      fvdl 			break;
    239       1.1      fvdl #endif
    240       1.1      fvdl 		default:
    241       1.1      fvdl 			goto freeit;
    242       1.1      fvdl 		}
    243       1.1      fvdl 	}
    244       1.4      fvdl 	/*
    245       1.4      fvdl 	 * Didn't find anything. Get the first possibly useful interface,
    246       1.4      fvdl 	 * preferring "normal" interfaces to point-to-point and loopback
    247       1.4      fvdl 	 * ones.
    248       1.4      fvdl 	 */
    249       1.4      fvdl 	bestif = NULL;
    250       1.4      fvdl 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
    251       1.4      fvdl 		if (ifap->ifa_addr->sa_family != clnt->sa_family ||
    252       1.4      fvdl 		    !(ifap->ifa_flags & IFF_UP))
    253       1.4      fvdl 			continue;
    254       1.4      fvdl 		if (!(ifap->ifa_flags & IFF_LOOPBACK) &&
    255       1.4      fvdl 		    !(ifap->ifa_flags & IFF_POINTOPOINT)) {
    256       1.4      fvdl 			bestif = ifap;
    257       1.4      fvdl 			break;
    258       1.4      fvdl 		}
    259       1.4      fvdl 		if (bestif == NULL)
    260       1.4      fvdl 			bestif = ifap;
    261       1.4      fvdl 		else if ((bestif->ifa_flags & IFF_LOOPBACK) &&
    262       1.4      fvdl 		    !(ifap->ifa_flags & IFF_LOOPBACK))
    263       1.4      fvdl 			bestif = ifap;
    264       1.4      fvdl 	}
    265       1.4      fvdl 	ifap = bestif;
    266       1.1      fvdl found:
    267       1.5       jmc 	switch (clnt->sa_family) {
    268       1.5       jmc 	case AF_INET:
    269       1.5       jmc 		memcpy(newsin, ifap->ifa_addr, clnt_sa->sa_len);
    270       1.5       jmc 		newsin->sin_port = servsin->sin_port;
    271       1.5       jmc 		tbuf.len = clnt_sa->sa_len;
    272       1.5       jmc 		tbuf.maxlen = sizeof (struct sockaddr_storage);
    273       1.5       jmc 		tbuf.buf = newsin;
    274       1.5       jmc 		break;
    275       1.5       jmc #ifdef INET6
    276       1.5       jmc 	case AF_INET6:
    277      1.12  christos 		assert(newsin6);
    278       1.5       jmc 		memcpy(newsin6, ifsin6, clnt_sa->sa_len);
    279       1.5       jmc 		newsin6->sin6_port = servsin6->sin6_port;
    280       1.5       jmc 		tbuf.maxlen = sizeof (struct sockaddr_storage);
    281       1.5       jmc 		tbuf.len = clnt_sa->sa_len;
    282       1.5       jmc 		tbuf.buf = newsin6;
    283       1.5       jmc 		break;
    284       1.5       jmc #endif
    285       1.5       jmc 	default:
    286       1.5       jmc 		goto freeit;
    287       1.5       jmc 	}
    288       1.1      fvdl 	if (ifap != NULL)
    289       1.1      fvdl 		ret = taddr2uaddr(nconf, &tbuf);
    290       1.1      fvdl freeit:
    291       1.3      fvdl 	freenetconfigent(nconf);
    292       1.1      fvdl 	free(serv_sa);
    293       1.1      fvdl 	free(serv_nbp);
    294       1.1      fvdl 	if (clnt_sa != NULL)
    295       1.1      fvdl 		free(clnt_sa);
    296       1.1      fvdl 	if (clnt_nbp != NULL)
    297       1.1      fvdl 		free(clnt_nbp);
    298       1.1      fvdl 	freeifaddrs(ifp);
    299       1.1      fvdl 
    300      1.14       dsl #ifdef RPCBIND_DEBUG
    301       1.1      fvdl 	if (debugging)
    302       1.1      fvdl 		fprintf(stderr, "addrmerge: returning %s\n", ret);
    303       1.1      fvdl #endif
    304       1.1      fvdl 	return ret;
    305       1.1      fvdl }
    306       1.1      fvdl 
    307       1.1      fvdl void
    308       1.1      fvdl network_init()
    309       1.1      fvdl {
    310       1.1      fvdl #ifdef INET6
    311       1.1      fvdl 	struct ifaddrs *ifap, *ifp;
    312       1.1      fvdl 	struct ipv6_mreq mreq6;
    313       1.1      fvdl 	int ifindex, s;
    314       1.1      fvdl #endif
    315       1.1      fvdl 	int ecode;
    316       1.1      fvdl 	struct addrinfo hints, *res;
    317       1.1      fvdl 
    318       1.1      fvdl 	memset(&hints, 0, sizeof hints);
    319       1.1      fvdl 	hints.ai_family = AF_INET;
    320       1.1      fvdl 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
    321       1.1      fvdl 		if (debugging)
    322       1.1      fvdl 			fprintf(stderr, "can't get local ip4 address: %s\n",
    323       1.1      fvdl 			    gai_strerror(ecode));
    324       1.1      fvdl 	} else {
    325       1.1      fvdl 		local_in4 = (struct sockaddr_in *)malloc(sizeof *local_in4);
    326       1.1      fvdl 		if (local_in4 == NULL) {
    327       1.1      fvdl 			if (debugging)
    328       1.1      fvdl 				fprintf(stderr, "can't alloc local ip4 addr\n");
    329       1.1      fvdl 		}
    330       1.1      fvdl 		memcpy(local_in4, res->ai_addr, sizeof *local_in4);
    331       1.1      fvdl 	}
    332       1.1      fvdl 
    333       1.1      fvdl #ifdef INET6
    334       1.1      fvdl 	hints.ai_family = AF_INET6;
    335       1.1      fvdl 	if ((ecode = getaddrinfo(NULL, "sunrpc", &hints, &res))) {
    336       1.1      fvdl 		if (debugging)
    337       1.1      fvdl 			fprintf(stderr, "can't get local ip6 address: %s\n",
    338       1.1      fvdl 			    gai_strerror(ecode));
    339       1.1      fvdl 	} else {
    340       1.1      fvdl 		local_in6 = (struct sockaddr_in6 *)malloc(sizeof *local_in6);
    341       1.1      fvdl 		if (local_in6 == NULL) {
    342       1.1      fvdl 			if (debugging)
    343       1.1      fvdl 				fprintf(stderr, "can't alloc local ip6 addr\n");
    344       1.1      fvdl 		}
    345       1.1      fvdl 		memcpy(local_in6, res->ai_addr, sizeof *local_in6);
    346       1.1      fvdl 	}
    347       1.1      fvdl 
    348       1.1      fvdl 	/*
    349       1.1      fvdl 	 * Now join the RPC ipv6 multicast group on all interfaces.
    350       1.1      fvdl 	 */
    351       1.1      fvdl 	if (getifaddrs(&ifp) < 0)
    352       1.1      fvdl 		return;
    353       1.1      fvdl 
    354       1.1      fvdl 	mreq6.ipv6mr_interface = 0;
    355       1.1      fvdl 	inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, &mreq6.ipv6mr_multiaddr);
    356       1.1      fvdl 
    357       1.1      fvdl 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
    358       1.1      fvdl 
    359       1.1      fvdl 	/*
    360       1.1      fvdl 	 * Loop through all interfaces. For each interface, see if the
    361       1.1      fvdl 	 * network portion of its address is equal to that of the client.
    362       1.1      fvdl 	 * If so, we have found the interface that we want to use.
    363       1.1      fvdl 	 */
    364       1.1      fvdl 	for (ifap = ifp; ifap != NULL; ifap = ifap->ifa_next) {
    365       1.1      fvdl 		if (ifap->ifa_addr->sa_family != AF_INET6 ||
    366       1.1      fvdl 		    !(ifap->ifa_flags & IFF_MULTICAST))
    367       1.1      fvdl 			continue;
    368       1.1      fvdl 		ifindex = if_nametoindex(ifap->ifa_name);
    369       1.1      fvdl 		if (ifindex == mreq6.ipv6mr_interface)
    370       1.1      fvdl 			/*
    371       1.1      fvdl 			 * Already did this one.
    372       1.1      fvdl 			 */
    373       1.1      fvdl 			continue;
    374       1.1      fvdl 		mreq6.ipv6mr_interface = ifindex;
    375       1.1      fvdl 		if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6,
    376       1.1      fvdl 		    sizeof mreq6) < 0)
    377       1.1      fvdl 			if (debugging)
    378       1.1      fvdl 				perror("setsockopt v6 multicast");
    379       1.1      fvdl 	}
    380       1.1      fvdl #endif
    381       1.1      fvdl 
    382       1.1      fvdl 	/* close(s); */
    383       1.1      fvdl }
    384       1.1      fvdl 
    385       1.1      fvdl struct sockaddr *
    386       1.1      fvdl local_sa(int af)
    387       1.1      fvdl {
    388       1.1      fvdl 	switch (af) {
    389       1.1      fvdl 	case AF_INET:
    390       1.1      fvdl 		return (struct sockaddr *)local_in4;
    391       1.1      fvdl #ifdef INET6
    392       1.1      fvdl 	case AF_INET6:
    393       1.1      fvdl 		return (struct sockaddr *)local_in6;
    394       1.1      fvdl #endif
    395       1.1      fvdl 	default:
    396       1.1      fvdl 		return NULL;
    397       1.1      fvdl 	}
    398       1.1      fvdl }
    399