Home | History | Annotate | Line # | Download | only in rpc.bootparamd
bootparamd.c revision 1.23.2.1
      1  1.23.2.1   minoura /*	$NetBSD: bootparamd.c,v 1.23.2.1 2000/06/22 18:01:09 minoura Exp $	*/
      2       1.7   thorpej 
      3       1.1   deraadt /*
      4       1.1   deraadt  * This code is not copyright, and is placed in the public domain.
      5       1.1   deraadt  * Feel free to use and modify. Please send modifications and/or
      6       1.1   deraadt  * suggestions + bug fixes to Klas Heggemann <klas (at) nada.kth.se>
      7       1.2   deraadt  *
      8       1.1   deraadt  * Various small changes by Theo de Raadt <deraadt (at) fsa.ca>
      9       1.2   deraadt  * Parser rewritten (adding YP support) by Roland McGrath <roland (at) frob.com>
     10       1.1   deraadt  */
     11       1.1   deraadt 
     12      1.12   thorpej #include <sys/cdefs.h>
     13      1.12   thorpej #ifndef lint
     14  1.23.2.1   minoura __RCSID("$NetBSD: bootparamd.c,v 1.23.2.1 2000/06/22 18:01:09 minoura Exp $");
     15      1.12   thorpej #endif
     16      1.12   thorpej 
     17       1.1   deraadt #include <sys/types.h>
     18       1.1   deraadt #include <sys/ioctl.h>
     19       1.1   deraadt #include <sys/stat.h>
     20       1.1   deraadt #include <sys/socket.h>
     21      1.10   mycroft 
     22      1.10   mycroft #include <ctype.h>
     23      1.21  christos #include <errno.h>
     24      1.10   mycroft #include <err.h>
     25      1.10   mycroft #include <netdb.h>
     26      1.12   thorpej #include <stdlib.h>
     27       1.1   deraadt #include <stdio.h>
     28      1.10   mycroft #include <string.h>
     29       1.1   deraadt #include <syslog.h>
     30      1.12   thorpej #include <unistd.h>
     31      1.20   thorpej #include <util.h>
     32      1.22    itojun #include <ifaddrs.h>
     33      1.10   mycroft 
     34      1.23    itojun #include <net/if.h>
     35      1.23    itojun 
     36      1.10   mycroft #include <netinet/in.h>
     37       1.9       cgd #include <arpa/inet.h>
     38      1.10   mycroft 
     39      1.10   mycroft #include <rpc/rpc.h>
     40      1.12   thorpej #include <rpc/pmap_clnt.h>
     41      1.10   mycroft #include <rpcsvc/bootparam_prot.h>
     42      1.12   thorpej #include <rpcsvc/ypclnt.h>
     43      1.10   mycroft 
     44       1.2   deraadt #include "pathnames.h"
     45       1.1   deraadt 
     46       1.1   deraadt #define MAXLEN 800
     47       1.1   deraadt 
     48       1.1   deraadt static char hostname[MAX_MACHINE_NAME];
     49       1.1   deraadt static char askname[MAX_MACHINE_NAME];
     50       1.1   deraadt static char domain_name[MAX_MACHINE_NAME];
     51       1.1   deraadt 
     52       1.5        pk extern void bootparamprog_1 __P((struct svc_req *, SVCXPRT *));
     53       1.1   deraadt 
     54       1.5        pk int	_rpcsvcdirty = 0;
     55       1.5        pk int	_rpcpmstart = 0;
     56       1.1   deraadt int     debug = 0;
     57       1.1   deraadt int     dolog = 0;
     58      1.12   thorpej struct in_addr route_addr;
     59       1.1   deraadt struct sockaddr_in my_addr;
     60      1.11   mycroft extern char *__progname;
     61       1.2   deraadt char   *bootpfile = _PATH_BOOTPARAMS;
     62       1.1   deraadt 
     63      1.12   thorpej int	main __P((int, char *[]));
     64      1.12   thorpej int	lookup_bootparam __P((char *, char *, char *, char **, char **));
     65      1.12   thorpej void	usage __P((void));
     66      1.22    itojun static int get_localaddr __P((const char *, struct sockaddr_in *));
     67       1.1   deraadt 
     68       1.1   deraadt 
     69       1.1   deraadt /*
     70       1.1   deraadt  * ever familiar
     71       1.1   deraadt  */
     72       1.1   deraadt int
     73       1.1   deraadt main(argc, argv)
     74       1.1   deraadt 	int     argc;
     75      1.12   thorpej 	char  *argv[];
     76       1.1   deraadt {
     77       1.1   deraadt 	SVCXPRT *transp;
     78       1.1   deraadt 	struct hostent *he;
     79       1.1   deraadt 	struct stat buf;
     80       1.6      mark 	int    c;
     81       1.1   deraadt 
     82       1.6      mark 	while ((c = getopt(argc, argv, "dsr:f:")) != -1)
     83       1.1   deraadt 		switch (c) {
     84       1.1   deraadt 		case 'd':
     85       1.1   deraadt 			debug = 1;
     86       1.1   deraadt 			break;
     87       1.1   deraadt 		case 'r':
     88       1.1   deraadt 			if (isdigit(*optarg)) {
     89      1.12   thorpej 				if (inet_aton(optarg, &route_addr) != 0)
     90      1.12   thorpej 					break;
     91       1.1   deraadt 			}
     92       1.1   deraadt 			he = gethostbyname(optarg);
     93      1.12   thorpej 			if (he == 0) {
     94      1.13     mikel 				warnx("no such host: %s", optarg);
     95       1.1   deraadt 				usage();
     96       1.1   deraadt 			}
     97      1.15     lukem 			memmove(&route_addr.s_addr, he->h_addr, he->h_length);
     98       1.1   deraadt 			break;
     99       1.1   deraadt 		case 'f':
    100       1.1   deraadt 			bootpfile = optarg;
    101       1.1   deraadt 			break;
    102       1.1   deraadt 		case 's':
    103       1.1   deraadt 			dolog = 1;
    104       1.1   deraadt #ifndef LOG_DAEMON
    105      1.11   mycroft 			openlog(__progname, 0, 0);
    106       1.1   deraadt #else
    107      1.11   mycroft 			openlog(__progname, 0, LOG_DAEMON);
    108       1.1   deraadt 			setlogmask(LOG_UPTO(LOG_NOTICE));
    109       1.1   deraadt #endif
    110       1.1   deraadt 			break;
    111       1.1   deraadt 		default:
    112       1.1   deraadt 			usage();
    113       1.1   deraadt 		}
    114       1.1   deraadt 
    115      1.10   mycroft 	if (stat(bootpfile, &buf))
    116      1.10   mycroft 		err(1, "%s", bootpfile);
    117      1.10   mycroft 
    118      1.12   thorpej 	if (route_addr.s_addr == 0) {
    119      1.22    itojun 		if (get_localaddr(NULL, &my_addr) != 0)
    120      1.22    itojun 			errx(1, "router address not found");
    121      1.12   thorpej 		route_addr.s_addr = my_addr.sin_addr.s_addr;
    122       1.1   deraadt 	}
    123      1.10   mycroft 	if (!debug) {
    124      1.10   mycroft 		if (daemon(0, 0))
    125      1.10   mycroft 			err(1, "can't detach from terminal");
    126      1.10   mycroft 	}
    127      1.20   thorpej 	pidfile(NULL);
    128       1.1   deraadt 
    129       1.1   deraadt 	(void) pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS);
    130       1.1   deraadt 
    131       1.1   deraadt 	transp = svcudp_create(RPC_ANYSOCK);
    132      1.10   mycroft 	if (transp == NULL)
    133      1.10   mycroft 		errx(1, "can't create udp service");
    134      1.10   mycroft 
    135      1.10   mycroft 	if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1,
    136      1.10   mycroft 	    IPPROTO_UDP))
    137  1.23.2.1   minoura /*
    138  1.23.2.1   minoura  * Do NOT change the "%u" in the format string below to "%lu". If your
    139  1.23.2.1   minoura  * build fails update the "rpcgen" program and use "make cleandir" and
    140  1.23.2.1   minoura  * "make includes" in "src/lib/librpcsvc" afterwards.
    141  1.23.2.1   minoura  */
    142  1.23.2.1   minoura 		errx(1, "unable to register BOOTPARAMPROG version %u, udp",
    143       1.1   deraadt 		    BOOTPARAMVERS);
    144      1.10   mycroft 
    145       1.1   deraadt 	svc_run();
    146      1.10   mycroft 	errx(1, "svc_run returned");
    147       1.1   deraadt }
    148       1.1   deraadt 
    149       1.1   deraadt bp_whoami_res *
    150       1.5        pk bootparamproc_whoami_1_svc(whoami, rqstp)
    151       1.1   deraadt 	bp_whoami_arg *whoami;
    152       1.5        pk 	struct svc_req *rqstp;
    153       1.1   deraadt {
    154       1.9       cgd 	static bp_whoami_res res;
    155       1.9       cgd 	struct hostent *he;
    156      1.18   nathanw 	struct in_addr haddr;
    157      1.21  christos 	int e;
    158       1.2   deraadt 
    159       1.1   deraadt 	if (debug)
    160      1.13     mikel 		warnx("whoami got question for %d.%d.%d.%d",
    161       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
    162       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
    163       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
    164       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
    165       1.1   deraadt 	if (dolog)
    166      1.13     mikel 		syslog(LOG_NOTICE, "whoami got question for %d.%d.%d.%d",
    167       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.net,
    168       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.host,
    169       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.lh,
    170       1.1   deraadt 		    255 & whoami->client_address.bp_address_u.ip_addr.impno);
    171       1.1   deraadt 
    172      1.15     lukem 	memmove((char *) &haddr,
    173      1.15     lukem 	    (char *) &whoami->client_address.bp_address_u.ip_addr,
    174      1.15     lukem 	    sizeof(haddr));
    175       1.1   deraadt 	he = gethostbyaddr((char *) &haddr, sizeof(haddr), AF_INET);
    176      1.14       mrg 	if (he) {
    177      1.14       mrg 		strncpy(askname, he->h_name, sizeof(askname));
    178      1.14       mrg 		askname[sizeof(askname)-1] = 0;
    179      1.14       mrg 	} else {
    180      1.18   nathanw 		strncpy(askname, inet_ntoa(haddr), sizeof(askname));
    181      1.14       mrg 		askname[sizeof(askname)-1] = 0;
    182       1.9       cgd 	}
    183       1.1   deraadt 
    184       1.1   deraadt 	if (debug)
    185      1.13     mikel 		warnx("This is host %s", askname);
    186       1.1   deraadt 	if (dolog)
    187      1.13     mikel 		syslog(LOG_NOTICE, "This is host %s", askname);
    188       1.1   deraadt 
    189      1.21  christos 	if ((e = lookup_bootparam(askname, hostname, NULL, NULL, NULL)) == 0) {
    190       1.1   deraadt 		res.client_name = hostname;
    191       1.1   deraadt 		getdomainname(domain_name, MAX_MACHINE_NAME);
    192       1.1   deraadt 		res.domain_name = domain_name;
    193       1.1   deraadt 
    194       1.1   deraadt 		if (res.router_address.address_type != IP_ADDR_TYPE) {
    195       1.1   deraadt 			res.router_address.address_type = IP_ADDR_TYPE;
    196      1.15     lukem 			memmove(&res.router_address.bp_address_u.ip_addr,
    197      1.15     lukem 			    &route_addr.s_addr,4);
    198       1.1   deraadt 		}
    199       1.1   deraadt 		if (debug)
    200      1.13     mikel 			warnx("Returning %s   %s    %d.%d.%d.%d",
    201       1.1   deraadt 			    res.client_name, res.domain_name,
    202       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.net,
    203       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.host,
    204       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.lh,
    205      1.14       mrg 			    255 &res.router_address.bp_address_u.ip_addr.impno);
    206       1.1   deraadt 		if (dolog)
    207      1.13     mikel 			syslog(LOG_NOTICE, "Returning %s   %s    %d.%d.%d.%d",
    208       1.1   deraadt 			    res.client_name, res.domain_name,
    209       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.net,
    210       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.host,
    211       1.1   deraadt 			    255 & res.router_address.bp_address_u.ip_addr.lh,
    212      1.14       mrg 			    255 &res.router_address.bp_address_u.ip_addr.impno);
    213       1.1   deraadt 
    214       1.1   deraadt 		return (&res);
    215       1.1   deraadt 	}
    216      1.21  christos 	errno = e;
    217       1.1   deraadt 	if (debug)
    218      1.21  christos 		warn("whoami failed");
    219       1.1   deraadt 	if (dolog)
    220      1.21  christos 		syslog(LOG_NOTICE, "whoami failed %m");
    221       1.1   deraadt 	return (NULL);
    222       1.1   deraadt }
    223       1.1   deraadt 
    224       1.1   deraadt 
    225       1.1   deraadt bp_getfile_res *
    226       1.5        pk bootparamproc_getfile_1_svc(getfile, rqstp)
    227       1.1   deraadt 	bp_getfile_arg *getfile;
    228       1.5        pk 	struct svc_req *rqstp;
    229       1.1   deraadt {
    230       1.1   deraadt 	static bp_getfile_res res;
    231       1.9       cgd 	struct hostent *he;
    232       1.2   deraadt 	int     err;
    233       1.1   deraadt 
    234       1.1   deraadt 	if (debug)
    235      1.13     mikel 		warnx("getfile got question for \"%s\" and file \"%s\"",
    236       1.1   deraadt 		    getfile->client_name, getfile->file_id);
    237       1.1   deraadt 
    238       1.1   deraadt 	if (dolog)
    239      1.13     mikel 		syslog(LOG_NOTICE,
    240      1.13     mikel 		    "getfile got question for \"%s\" and file \"%s\"",
    241       1.1   deraadt 		    getfile->client_name, getfile->file_id);
    242       1.1   deraadt 
    243       1.1   deraadt 	he = NULL;
    244       1.1   deraadt 	he = gethostbyname(getfile->client_name);
    245       1.1   deraadt 	if (!he)
    246       1.1   deraadt 		goto failed;
    247       1.1   deraadt 
    248      1.14       mrg 	strncpy(askname, he->h_name, sizeof(askname));
    249      1.14       mrg 	askname[sizeof(askname)-1] = 0;
    250       1.2   deraadt 	err = lookup_bootparam(askname, NULL, getfile->file_id,
    251       1.2   deraadt 	    &res.server_name, &res.server_path);
    252       1.2   deraadt 	if (err == 0) {
    253       1.2   deraadt 		he = gethostbyname(res.server_name);
    254       1.2   deraadt 		if (!he)
    255       1.2   deraadt 			goto failed;
    256      1.15     lukem 		memmove(&res.server_address.bp_address_u.ip_addr,
    257      1.15     lukem 		    he->h_addr, 4);
    258       1.2   deraadt 		res.server_address.address_type = IP_ADDR_TYPE;
    259       1.2   deraadt 	} else if (err == ENOENT && !strcmp(getfile->file_id, "dump")) {
    260       1.2   deraadt 		/* Special for dump, answer with null strings. */
    261       1.2   deraadt 		res.server_name[0] = '\0';
    262       1.2   deraadt 		res.server_path[0] = '\0';
    263      1.15     lukem 		memset(&res.server_address.bp_address_u.ip_addr, 0, 4);
    264       1.2   deraadt 	} else {
    265       1.2   deraadt failed:
    266       1.1   deraadt 		if (debug)
    267      1.13     mikel 			warnx("getfile failed for %s",
    268       1.2   deraadt 			    getfile->client_name);
    269       1.1   deraadt 		if (dolog)
    270       1.1   deraadt 			syslog(LOG_NOTICE,
    271      1.13     mikel 			    "getfile failed for %s", getfile->client_name);
    272       1.2   deraadt 		return (NULL);
    273       1.1   deraadt 	}
    274       1.2   deraadt 
    275       1.1   deraadt 	if (debug)
    276      1.10   mycroft 		warnx(
    277      1.13     mikel 		    "returning server:%s path:%s address: %d.%d.%d.%d",
    278       1.2   deraadt 		    res.server_name, res.server_path,
    279       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.net,
    280       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.host,
    281       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.lh,
    282       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.impno);
    283       1.1   deraadt 	if (dolog)
    284       1.1   deraadt 		syslog(LOG_NOTICE,
    285      1.13     mikel 		    "returning server:%s path:%s address: %d.%d.%d.%d",
    286       1.2   deraadt 		    res.server_name, res.server_path,
    287       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.net,
    288       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.host,
    289       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.lh,
    290       1.2   deraadt 		    255 & res.server_address.bp_address_u.ip_addr.impno);
    291       1.2   deraadt 	return (&res);
    292       1.1   deraadt }
    293       1.1   deraadt 
    294       1.1   deraadt 
    295       1.2   deraadt int
    296       1.2   deraadt lookup_bootparam(client, client_canonical, id, server, path)
    297       1.2   deraadt 	char	*client;
    298       1.2   deraadt 	char	*client_canonical;
    299       1.2   deraadt 	char	*id;
    300       1.2   deraadt 	char	**server;
    301       1.2   deraadt 	char	**path;
    302       1.1   deraadt {
    303       1.2   deraadt 	FILE   *f = fopen(bootpfile, "r");
    304       1.2   deraadt #ifdef YP
    305       1.2   deraadt 	static char *ypbuf = NULL;
    306       1.2   deraadt 	static int ypbuflen = 0;
    307       1.2   deraadt #endif
    308       1.2   deraadt 	static char buf[BUFSIZ];
    309      1.12   thorpej 	char   *bp, *word = NULL;
    310       1.2   deraadt 	size_t  idlen = id == NULL ? 0 : strlen(id);
    311       1.2   deraadt 	int     contin = 0;
    312       1.2   deraadt 	int     found = 0;
    313       1.2   deraadt 
    314       1.2   deraadt 	if (f == NULL)
    315       1.2   deraadt 		return EINVAL;	/* ? */
    316       1.2   deraadt 
    317       1.2   deraadt 	while (fgets(buf, sizeof buf, f)) {
    318       1.2   deraadt 		int     wascontin = contin;
    319       1.2   deraadt 		contin = buf[strlen(buf) - 2] == '\\';
    320       1.2   deraadt 		bp = buf + strspn(buf, " \t\n");
    321       1.2   deraadt 
    322       1.2   deraadt 		switch (wascontin) {
    323       1.2   deraadt 		case -1:
    324       1.2   deraadt 			/* Continuation of uninteresting line */
    325       1.2   deraadt 			contin *= -1;
    326       1.2   deraadt 			continue;
    327       1.2   deraadt 		case 0:
    328       1.2   deraadt 			/* New line */
    329       1.2   deraadt 			contin *= -1;
    330       1.2   deraadt 			if (*bp == '#')
    331       1.2   deraadt 				continue;
    332       1.2   deraadt 			if ((word = strsep(&bp, " \t\n")) == NULL)
    333       1.2   deraadt 				continue;
    334       1.2   deraadt #ifdef YP
    335       1.2   deraadt 			/* A + in the file means try YP now */
    336       1.2   deraadt 			if (!strcmp(word, "+")) {
    337       1.2   deraadt 				char   *ypdom;
    338       1.2   deraadt 
    339       1.2   deraadt 				if (yp_get_default_domain(&ypdom) ||
    340       1.2   deraadt 				    yp_match(ypdom, "bootparams", client,
    341       1.2   deraadt 					strlen(client), &ypbuf, &ypbuflen))
    342       1.2   deraadt 					continue;
    343       1.2   deraadt 				bp = ypbuf;
    344       1.2   deraadt 				word = client;
    345       1.2   deraadt 				contin *= -1;
    346       1.2   deraadt 				break;
    347       1.2   deraadt 			}
    348       1.2   deraadt #endif
    349      1.21  christos 			if (debug)
    350      1.21  christos 				warnx("match %s with %s", word, client);
    351       1.2   deraadt 			/* See if this line's client is the one we are
    352       1.2   deraadt 			 * looking for */
    353       1.8       cgd 			if (strcasecmp(word, client) != 0) {
    354       1.2   deraadt 				/*
    355       1.2   deraadt 				 * If it didn't match, try getting the
    356       1.2   deraadt 				 * canonical host name of the client
    357       1.2   deraadt 				 * on this line and comparing that to
    358       1.2   deraadt 				 * the client we are looking for
    359       1.2   deraadt 				 */
    360       1.2   deraadt 				struct hostent *hp = gethostbyname(word);
    361      1.19       abs 				if (hp == NULL ) {
    362      1.19       abs 					if (debug)
    363      1.19       abs 						warnx(
    364      1.19       abs 					    "Unknown bootparams host %s", word);
    365      1.19       abs 					if (dolog)
    366      1.19       abs 						syslog(LOG_NOTICE,
    367      1.19       abs 					    "Unknown bootparams host %s", word);
    368      1.19       abs 					continue;
    369      1.19       abs 				}
    370      1.19       abs 				if (strcasecmp(hp->h_name, client))
    371       1.2   deraadt 					continue;
    372       1.1   deraadt 			}
    373       1.2   deraadt 			contin *= -1;
    374       1.2   deraadt 			break;
    375       1.2   deraadt 		case 1:
    376       1.2   deraadt 			/* Continued line we want to parse below */
    377       1.1   deraadt 			break;
    378       1.1   deraadt 		}
    379       1.1   deraadt 
    380       1.2   deraadt 		if (client_canonical)
    381       1.2   deraadt 			strncpy(client_canonical, word, MAX_MACHINE_NAME);
    382       1.1   deraadt 
    383       1.2   deraadt 		/* We have found a line for CLIENT */
    384       1.4     glass 		if (id == NULL) {
    385       1.4     glass 			(void) fclose(f);
    386       1.2   deraadt 			return 0;
    387       1.4     glass 		}
    388       1.2   deraadt 
    389       1.2   deraadt 		/* Look for a value for the parameter named by ID */
    390       1.2   deraadt 		while ((word = strsep(&bp, " \t\n")) != NULL) {
    391       1.2   deraadt 			if (!strncmp(word, id, idlen) && word[idlen] == '=') {
    392       1.2   deraadt 				/* We have found the entry we want */
    393       1.2   deraadt 				*server = &word[idlen + 1];
    394       1.2   deraadt 				*path = strchr(*server, ':');
    395       1.2   deraadt 				if (*path == NULL)
    396       1.2   deraadt 					/* Malformed entry */
    397       1.2   deraadt 					continue;
    398       1.2   deraadt 				*(*path)++ = '\0';
    399       1.2   deraadt 				(void) fclose(f);
    400       1.2   deraadt 				return 0;
    401       1.2   deraadt 			}
    402       1.1   deraadt 		}
    403       1.2   deraadt 
    404       1.2   deraadt 		found = 1;
    405       1.1   deraadt 	}
    406       1.1   deraadt 
    407       1.2   deraadt 	(void) fclose(f);
    408       1.2   deraadt 	return found ? ENOENT : EPERM;
    409      1.12   thorpej }
    410      1.12   thorpej 
    411      1.12   thorpej void
    412      1.12   thorpej usage()
    413      1.12   thorpej {
    414      1.12   thorpej 	fprintf(stderr,
    415      1.12   thorpej 	    "usage: %s [-d] [-s] [-r router] [-f bootparmsfile]\n", __progname);
    416      1.12   thorpej 	exit(1);
    417      1.22    itojun }
    418      1.22    itojun 
    419      1.22    itojun static int
    420      1.22    itojun get_localaddr(ifname, sin)
    421      1.22    itojun 	const char *ifname;
    422      1.22    itojun 	struct sockaddr_in *sin;
    423      1.22    itojun {
    424      1.22    itojun 	struct ifaddrs *ifap, *ifa;
    425      1.22    itojun 
    426      1.22    itojun 	if (getifaddrs(&ifap) != 0)
    427      1.22    itojun 		return -1;
    428      1.22    itojun 
    429      1.22    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    430      1.22    itojun 		if (ifname && strcmp(ifname, ifa->ifa_name) != 0)
    431      1.22    itojun 			continue;
    432      1.22    itojun 		if (ifa->ifa_addr->sa_family != AF_INET)
    433      1.22    itojun 			continue;
    434      1.22    itojun 		if (ifa->ifa_addr->sa_len != sizeof(*sin))
    435      1.22    itojun 			continue;
    436      1.22    itojun 
    437      1.22    itojun 		/* no loopback please */
    438      1.22    itojun #ifdef IFF_LOOPBACK
    439      1.22    itojun 		if (ifa->ifa_flags & IFF_LOOPBACK)
    440      1.22    itojun 			continue;
    441      1.22    itojun #else
    442      1.23    itojun 		if (strncmp(ifa->ifa_name, "lo", 2) == 0 &&
    443      1.23    itojun 		    (isdigit(ifa->ifa_name[2]) || ifa->ifa_name[2] == '\0'))
    444      1.22    itojun 			continue;
    445      1.22    itojun #endif
    446      1.22    itojun 
    447      1.22    itojun 		/* XXX more sanity checks? */
    448      1.22    itojun 
    449      1.22    itojun 		/* candidate found */
    450      1.22    itojun 		memcpy(sin, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    451      1.22    itojun 		freeifaddrs(ifap);
    452      1.22    itojun 		return 0;
    453      1.22    itojun 	}
    454      1.22    itojun 
    455      1.22    itojun 	/* no candidate */
    456      1.22    itojun 	freeifaddrs(ifap);
    457      1.22    itojun 	return -1;
    458       1.1   deraadt }
    459