17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*b127ac41SPhilip Kirk * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <stdio.h>
2745916cd2Sjpk #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/socket.h>
317c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
327c478bd9Sstevel@tonic-gate #include <net/if.h>
337c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
347c478bd9Sstevel@tonic-gate #include <netinet/in.h>
357c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
367c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
377c478bd9Sstevel@tonic-gate #include <netdb.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <setjmp.h>
4145916cd2Sjpk #include <arpa/inet.h>
4245916cd2Sjpk #include "snoop.h"
437c478bd9Sstevel@tonic-gate
4445916cd2Sjpk static sigjmp_buf nisjmp;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define MAXHASH 1024 /* must be a power of 2 */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define SEPARATORS " \t\n"
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate struct hostdata {
517c478bd9Sstevel@tonic-gate struct hostdata *h_next;
527c478bd9Sstevel@tonic-gate char *h_hostname;
537c478bd9Sstevel@tonic-gate int h_pktsout;
547c478bd9Sstevel@tonic-gate int h_pktsin;
557c478bd9Sstevel@tonic-gate };
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate struct hostdata4 {
587c478bd9Sstevel@tonic-gate struct hostdata4 *h4_next;
597c478bd9Sstevel@tonic-gate char *h4_hostname;
607c478bd9Sstevel@tonic-gate int h4_pktsout;
617c478bd9Sstevel@tonic-gate int h4_pktsin;
627c478bd9Sstevel@tonic-gate struct in_addr h4_addr;
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate struct hostdata6 {
667c478bd9Sstevel@tonic-gate struct hostdata6 *h6_next;
677c478bd9Sstevel@tonic-gate char *h6_hostname;
687c478bd9Sstevel@tonic-gate int h6_pktsout;
697c478bd9Sstevel@tonic-gate int h6_pktsin;
707c478bd9Sstevel@tonic-gate struct in6_addr h6_addr;
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate
7345916cd2Sjpk static struct hostdata *addhost(int, const void *, const char *, char **);
747c478bd9Sstevel@tonic-gate
7545916cd2Sjpk static struct hostdata4 *h_table4[MAXHASH];
7645916cd2Sjpk static struct hostdata6 *h_table6[MAXHASH];
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate #define iphash(e) ((e) & (MAXHASH-1))
797c478bd9Sstevel@tonic-gate
8045916cd2Sjpk /* ARGSUSED */
817c478bd9Sstevel@tonic-gate static void
wakeup(int n)8245916cd2Sjpk wakeup(int n)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate siglongjmp(nisjmp, 1);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate extern char *inet_ntoa();
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate static struct hostdata *
iplookup(struct in_addr ipaddr)907c478bd9Sstevel@tonic-gate iplookup(struct in_addr ipaddr)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate register struct hostdata4 *h;
937c478bd9Sstevel@tonic-gate struct hostent *hp = NULL;
947c478bd9Sstevel@tonic-gate struct netent *np;
957c478bd9Sstevel@tonic-gate int error_num;
967c478bd9Sstevel@tonic-gate struct hostdata *retval;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate for (h = h_table4[iphash(ipaddr.s_addr)]; h; h = h->h4_next) {
997c478bd9Sstevel@tonic-gate if (h->h4_addr.s_addr == ipaddr.s_addr)
1007c478bd9Sstevel@tonic-gate return ((struct hostdata *)h);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate /* not found. Put it in */
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (ipaddr.s_addr == htonl(INADDR_BROADCAST))
1067c478bd9Sstevel@tonic-gate return (addhost(AF_INET, &ipaddr, "BROADCAST", NULL));
1077c478bd9Sstevel@tonic-gate if (ipaddr.s_addr == htonl(INADDR_ANY))
1087c478bd9Sstevel@tonic-gate return (addhost(AF_INET, &ipaddr, "OLD-BROADCAST", NULL));
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * Set an alarm here so we don't get held up by
1127c478bd9Sstevel@tonic-gate * an unresponsive name server.
1137c478bd9Sstevel@tonic-gate * Give it 3 sec to do its work.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate if (! rflg && sigsetjmp(nisjmp, 1) == 0) {
1167c478bd9Sstevel@tonic-gate (void) snoop_alarm(3, wakeup);
1177c478bd9Sstevel@tonic-gate hp = getipnodebyaddr((char *)&ipaddr, sizeof (int),
1187c478bd9Sstevel@tonic-gate AF_INET, &error_num);
1197c478bd9Sstevel@tonic-gate if (hp == NULL && inet_lnaof(ipaddr) == 0) {
1207c478bd9Sstevel@tonic-gate np = getnetbyaddr(inet_netof(ipaddr), AF_INET);
1217c478bd9Sstevel@tonic-gate if (np)
1227c478bd9Sstevel@tonic-gate return (addhost(AF_INET, &ipaddr, np->n_name,
1237c478bd9Sstevel@tonic-gate np->n_aliases));
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate (void) snoop_alarm(0, wakeup);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate retval = addhost(AF_INET, &ipaddr,
1297c478bd9Sstevel@tonic-gate hp ? hp->h_name : inet_ntoa(ipaddr),
1307c478bd9Sstevel@tonic-gate hp ? hp->h_aliases : NULL);
1317c478bd9Sstevel@tonic-gate if (hp != NULL)
1327c478bd9Sstevel@tonic-gate freehostent(hp);
1337c478bd9Sstevel@tonic-gate return (retval);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate static struct hostdata *
ip6lookup(const struct in6_addr * ip6addr)13745916cd2Sjpk ip6lookup(const struct in6_addr *ip6addr)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate struct hostdata6 *h;
1407c478bd9Sstevel@tonic-gate struct hostent *hp = NULL;
1417c478bd9Sstevel@tonic-gate int error_num;
1427c478bd9Sstevel@tonic-gate char addrstr[INET6_ADDRSTRLEN];
1432e3b6467Skcpoon char *addname;
1447c478bd9Sstevel@tonic-gate struct hostdata *retval;
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate for (h = h_table6[iphash(((uint32_t *)ip6addr)[3])]; h;
1477c478bd9Sstevel@tonic-gate h = h->h6_next) {
1487c478bd9Sstevel@tonic-gate if (IN6_ARE_ADDR_EQUAL(&h->h6_addr, ip6addr))
1497c478bd9Sstevel@tonic-gate return ((struct hostdata *)h);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate /* not in the hash table, put it in */
1537c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_UNSPECIFIED(ip6addr))
1547c478bd9Sstevel@tonic-gate return (addhost(AF_INET6, ip6addr, "UNSPECIFIED", NULL));
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * Set an alarm here so we don't get held up by
1587c478bd9Sstevel@tonic-gate * an unresponsive name server.
1597c478bd9Sstevel@tonic-gate * Give it 3 sec to do its work.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate if (! rflg && sigsetjmp(nisjmp, 1) == 0) {
1627c478bd9Sstevel@tonic-gate (void) snoop_alarm(3, wakeup);
1637c478bd9Sstevel@tonic-gate hp = getipnodebyaddr(ip6addr, sizeof (struct in6_addr),
1647c478bd9Sstevel@tonic-gate AF_INET6, &error_num);
1657c478bd9Sstevel@tonic-gate (void) snoop_alarm(0, wakeup);
1667c478bd9Sstevel@tonic-gate } else {
1677c478bd9Sstevel@tonic-gate hp = NULL;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if (hp != NULL)
1717c478bd9Sstevel@tonic-gate addname = hp->h_name;
1727c478bd9Sstevel@tonic-gate else {
1737c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET6, ip6addr, addrstr, INET6_ADDRSTRLEN);
1747c478bd9Sstevel@tonic-gate addname = addrstr;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate retval = addhost(AF_INET6, ip6addr, addname, hp ? hp->h_aliases : NULL);
1787c478bd9Sstevel@tonic-gate if (hp != NULL)
1797c478bd9Sstevel@tonic-gate freehostent(hp);
1807c478bd9Sstevel@tonic-gate return (retval);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate static struct hostdata *
addhost(int family,const void * ipaddr,const char * name,char ** aliases)18445916cd2Sjpk addhost(int family, const void *ipaddr, const char *name, char **aliases)
1857c478bd9Sstevel@tonic-gate {
18645916cd2Sjpk struct hostdata **hp, *n = NULL;
1877c478bd9Sstevel@tonic-gate extern FILE *namefile;
1887c478bd9Sstevel@tonic-gate int hashval;
1897c478bd9Sstevel@tonic-gate static char aname[128];
1907c478bd9Sstevel@tonic-gate char *np;
1917c478bd9Sstevel@tonic-gate static struct hostdata h;
1927c478bd9Sstevel@tonic-gate int ind;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate switch (family) {
1957c478bd9Sstevel@tonic-gate case AF_INET:
1967c478bd9Sstevel@tonic-gate n = (struct hostdata *)malloc(sizeof (struct hostdata4));
1977c478bd9Sstevel@tonic-gate if (n == NULL)
1987c478bd9Sstevel@tonic-gate goto alloc_failed;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate memset(n, 0, sizeof (struct hostdata4));
2017c478bd9Sstevel@tonic-gate n->h_hostname = strdup(name);
2027c478bd9Sstevel@tonic-gate if (n->h_hostname == NULL)
2037c478bd9Sstevel@tonic-gate goto alloc_failed;
2047c478bd9Sstevel@tonic-gate
20545916cd2Sjpk ((struct hostdata4 *)n)->h4_addr =
20645916cd2Sjpk *(const struct in_addr *)ipaddr;
2077c478bd9Sstevel@tonic-gate hashval = ((struct in_addr *)ipaddr)->s_addr;
2087c478bd9Sstevel@tonic-gate hp = (struct hostdata **)&h_table4[iphash(hashval)];
2097c478bd9Sstevel@tonic-gate break;
2107c478bd9Sstevel@tonic-gate case AF_INET6:
2117c478bd9Sstevel@tonic-gate n = (struct hostdata *)malloc(sizeof (struct hostdata6));
2127c478bd9Sstevel@tonic-gate if (n == NULL)
2137c478bd9Sstevel@tonic-gate goto alloc_failed;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate memset(n, 0, sizeof (struct hostdata6));
2167c478bd9Sstevel@tonic-gate n->h_hostname = strdup(name);
2177c478bd9Sstevel@tonic-gate if (n->h_hostname == NULL)
2187c478bd9Sstevel@tonic-gate goto alloc_failed;
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate memcpy(&((struct hostdata6 *)n)->h6_addr, ipaddr,
2217c478bd9Sstevel@tonic-gate sizeof (struct in6_addr));
22245916cd2Sjpk hashval = ((const int *)ipaddr)[3];
2237c478bd9Sstevel@tonic-gate hp = (struct hostdata **)&h_table6[iphash(hashval)];
2247c478bd9Sstevel@tonic-gate break;
2257c478bd9Sstevel@tonic-gate default:
2267c478bd9Sstevel@tonic-gate fprintf(stderr, "snoop: ERROR: Unknown address family: %d",
2277c478bd9Sstevel@tonic-gate family);
2287c478bd9Sstevel@tonic-gate exit(1);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate n->h_next = *hp;
2327c478bd9Sstevel@tonic-gate *hp = n;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate if (namefile != NULL) {
2357c478bd9Sstevel@tonic-gate if (family == AF_INET) {
23645916cd2Sjpk np = inet_ntoa(*(const struct in_addr *)ipaddr);
2377c478bd9Sstevel@tonic-gate if (np) {
2387c478bd9Sstevel@tonic-gate (void) fprintf(namefile, "%s\t%s", np, name);
2397c478bd9Sstevel@tonic-gate if (aliases) {
2407c478bd9Sstevel@tonic-gate for (ind = 0;
2417c478bd9Sstevel@tonic-gate aliases[ind] != NULL;
2427c478bd9Sstevel@tonic-gate ind++) {
2437c478bd9Sstevel@tonic-gate (void) fprintf(namefile, " %s",
2447c478bd9Sstevel@tonic-gate aliases[ind]);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate (void) fprintf(namefile, "\n");
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate } else if (family == AF_INET6) {
2507c478bd9Sstevel@tonic-gate np = (char *)inet_ntop(AF_INET6, (void *)ipaddr, aname,
2517c478bd9Sstevel@tonic-gate sizeof (aname));
2527c478bd9Sstevel@tonic-gate if (np) {
2537c478bd9Sstevel@tonic-gate (void) fprintf(namefile, "%s\t%s", np, name);
2547c478bd9Sstevel@tonic-gate if (aliases) {
2557c478bd9Sstevel@tonic-gate for (ind = 0;
2567c478bd9Sstevel@tonic-gate aliases[ind] != NULL;
2577c478bd9Sstevel@tonic-gate ind++) {
2587c478bd9Sstevel@tonic-gate (void) fprintf(namefile, " %s",
2597c478bd9Sstevel@tonic-gate aliases[ind]);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate (void) fprintf(namefile, "\n");
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate } else {
2657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "addhost: unknown family %d\n",
2667c478bd9Sstevel@tonic-gate family);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate return (n);
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate alloc_failed:
2727c478bd9Sstevel@tonic-gate if (n)
2737c478bd9Sstevel@tonic-gate free(n);
2747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "addhost: no mem\n");
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate aname[0] = '\0';
2777c478bd9Sstevel@tonic-gate memset(&h, 0, sizeof (struct hostdata));
2787c478bd9Sstevel@tonic-gate h.h_hostname = aname;
2797c478bd9Sstevel@tonic-gate return (&h);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate char *
addrtoname(int family,const void * ipaddr)28345916cd2Sjpk addrtoname(int family, const void *ipaddr)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate switch (family) {
2867c478bd9Sstevel@tonic-gate case AF_INET:
28745916cd2Sjpk return (iplookup(*(const struct in_addr *)ipaddr)->h_hostname);
2887c478bd9Sstevel@tonic-gate case AF_INET6:
28945916cd2Sjpk return (ip6lookup((const struct in6_addr *)ipaddr)->h_hostname);
29045916cd2Sjpk }
29145916cd2Sjpk (void) fprintf(stderr, "snoop: ERROR: unknown address family: %d\n",
2927c478bd9Sstevel@tonic-gate family);
2937c478bd9Sstevel@tonic-gate exit(1);
29445916cd2Sjpk /* NOTREACHED */
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate void
load_names(fname)2987c478bd9Sstevel@tonic-gate load_names(fname)
2997c478bd9Sstevel@tonic-gate char *fname;
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate char buf[1024];
3027c478bd9Sstevel@tonic-gate char *addr, *name, *alias;
3037c478bd9Sstevel@tonic-gate FILE *f;
3047c478bd9Sstevel@tonic-gate unsigned int addrv4;
3057c478bd9Sstevel@tonic-gate struct in6_addr addrv6;
3067c478bd9Sstevel@tonic-gate int family;
3077c478bd9Sstevel@tonic-gate void *naddr;
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Loading name file %s\n", fname);
3107c478bd9Sstevel@tonic-gate f = fopen(fname, "r");
3117c478bd9Sstevel@tonic-gate if (f == NULL) {
3127c478bd9Sstevel@tonic-gate perror(fname);
3137c478bd9Sstevel@tonic-gate return;
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate while (fgets(buf, 1024, f) != NULL) {
3177c478bd9Sstevel@tonic-gate addr = strtok(buf, SEPARATORS);
3187c478bd9Sstevel@tonic-gate if (addr == NULL || *addr == '#')
3197c478bd9Sstevel@tonic-gate continue;
3207c478bd9Sstevel@tonic-gate if (inet_pton(AF_INET6, addr, (void *)&addrv6) == 1) {
3217c478bd9Sstevel@tonic-gate family = AF_INET6;
3227c478bd9Sstevel@tonic-gate naddr = (void *)&addrv6;
32345916cd2Sjpk } else if ((addrv4 = inet_addr(addr)) != (ulong_t)-1) {
3247c478bd9Sstevel@tonic-gate family = AF_INET;
3257c478bd9Sstevel@tonic-gate naddr = (void *)&addrv4;
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate name = strtok(NULL, SEPARATORS);
3287c478bd9Sstevel@tonic-gate if (name == NULL)
3297c478bd9Sstevel@tonic-gate continue;
33045916cd2Sjpk while ((alias = strtok(NULL, SEPARATORS)) != NULL &&
33145916cd2Sjpk (*alias != '#')) {
3327c478bd9Sstevel@tonic-gate (void) addhost(family, naddr, alias, NULL);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate (void) addhost(family, naddr, name, NULL);
3357c478bd9Sstevel@tonic-gate /* Note: certain addresses such as broadcast are skipped */
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate (void) fclose(f);
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * lgetipnodebyname: looks up hostname in cached address data. This allows
3437c478bd9Sstevel@tonic-gate * filtering on hostnames from the .names file to work properly, and
3447c478bd9Sstevel@tonic-gate * avoids name clashes between domains. Note that only the first of the
3457c478bd9Sstevel@tonic-gate * ipv4, ipv6, or v4mapped address will be returned, because the
3467c478bd9Sstevel@tonic-gate * cache does not contain information on multi-homed hosts.
3477c478bd9Sstevel@tonic-gate */
3487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3497c478bd9Sstevel@tonic-gate struct hostent *
lgetipnodebyname(const char * name,int af,int flags,int * error_num)3507c478bd9Sstevel@tonic-gate lgetipnodebyname(const char *name, int af, int flags, int *error_num)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate int i;
3537c478bd9Sstevel@tonic-gate struct hostdata4 *h;
3547c478bd9Sstevel@tonic-gate struct hostdata6 *h6;
3557c478bd9Sstevel@tonic-gate static struct hostent he; /* host entry */
3567c478bd9Sstevel@tonic-gate static struct in6_addr h46_addr[MAXADDRS]; /* v4mapped address */
3577c478bd9Sstevel@tonic-gate static char h_name[MAXHOSTNAMELEN]; /* hostname */
3587c478bd9Sstevel@tonic-gate static char *list[MAXADDRS]; /* addr_list array */
3597c478bd9Sstevel@tonic-gate struct hostent *hp = &he;
3607c478bd9Sstevel@tonic-gate int ind;
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate (void) memset((char *)hp, 0, sizeof (struct hostent));
3637c478bd9Sstevel@tonic-gate hp->h_name = h_name;
3647c478bd9Sstevel@tonic-gate h_name[0] = '\0';
3657c478bd9Sstevel@tonic-gate strcpy(h_name, name);
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate hp->h_addrtype = AF_INET6;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate hp->h_addr_list = list;
3707c478bd9Sstevel@tonic-gate for (i = 0; i < MAXADDRS; i++)
3717c478bd9Sstevel@tonic-gate hp->h_addr_list[i] = NULL;
3727c478bd9Sstevel@tonic-gate ind = 0;
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate /* ipv6 lookup */
3757c478bd9Sstevel@tonic-gate if (af == AF_INET6) {
3767c478bd9Sstevel@tonic-gate hp->h_length = sizeof (struct in6_addr);
3777c478bd9Sstevel@tonic-gate for (i = 0; i < MAXHASH; i++) {
3787c478bd9Sstevel@tonic-gate for (h6 = h_table6[i]; h6; h6 = h6->h6_next) {
3797c478bd9Sstevel@tonic-gate if (strcmp(name, h6->h6_hostname) == 0) {
3807c478bd9Sstevel@tonic-gate if (ind >= MAXADDRS - 1) {
3817c478bd9Sstevel@tonic-gate /* too many addresses */
3827c478bd9Sstevel@tonic-gate return (hp);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate /* found ipv6 addr */
3857c478bd9Sstevel@tonic-gate hp->h_addr_list[ind] =
3867c478bd9Sstevel@tonic-gate (char *)&h6->h6_addr;
3877c478bd9Sstevel@tonic-gate ind++;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate /* ipv4 or v4mapped lookup */
3937c478bd9Sstevel@tonic-gate if (af == AF_INET || (flags & AI_ALL)) {
3947c478bd9Sstevel@tonic-gate for (i = 0; i < MAXHASH; i++) {
3957c478bd9Sstevel@tonic-gate for (h = h_table4[i]; h; h = h->h4_next) {
3967c478bd9Sstevel@tonic-gate if (strcmp(name, h->h4_hostname) == 0) {
3977c478bd9Sstevel@tonic-gate if (ind >= MAXADDRS - 1) {
3987c478bd9Sstevel@tonic-gate /* too many addresses */
3997c478bd9Sstevel@tonic-gate return (hp);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate if (af == AF_INET) {
4027c478bd9Sstevel@tonic-gate /* found ipv4 addr */
4037c478bd9Sstevel@tonic-gate hp->h_addrtype = AF_INET;
4047c478bd9Sstevel@tonic-gate hp->h_length =
4057c478bd9Sstevel@tonic-gate sizeof (struct in_addr);
4067c478bd9Sstevel@tonic-gate hp->h_addr_list[ind] =
4077c478bd9Sstevel@tonic-gate (char *)&h->h4_addr;
4087c478bd9Sstevel@tonic-gate ind++;
4097c478bd9Sstevel@tonic-gate } else {
4107c478bd9Sstevel@tonic-gate /* found v4mapped addr */
4117c478bd9Sstevel@tonic-gate hp->h_length =
4127c478bd9Sstevel@tonic-gate sizeof (struct in6_addr);
4137c478bd9Sstevel@tonic-gate hp->h_addr_list[ind] =
4147c478bd9Sstevel@tonic-gate (char *)&h46_addr[ind];
4157c478bd9Sstevel@tonic-gate IN6_INADDR_TO_V4MAPPED(
4167c478bd9Sstevel@tonic-gate &h->h4_addr,
4177c478bd9Sstevel@tonic-gate &h46_addr[ind]);
4187c478bd9Sstevel@tonic-gate ind++;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate return (ind > 0 ? hp : NULL);
4257c478bd9Sstevel@tonic-gate }
426