1*5c51f124SMoriah Waterland /*
2*5c51f124SMoriah Waterland * CDDL HEADER START
3*5c51f124SMoriah Waterland *
4*5c51f124SMoriah Waterland * The contents of this file are subject to the terms of the
5*5c51f124SMoriah Waterland * Common Development and Distribution License (the "License").
6*5c51f124SMoriah Waterland * You may not use this file except in compliance with the License.
7*5c51f124SMoriah Waterland *
8*5c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing.
10*5c51f124SMoriah Waterland * See the License for the specific language governing permissions
11*5c51f124SMoriah Waterland * and limitations under the License.
12*5c51f124SMoriah Waterland *
13*5c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
14*5c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the
16*5c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
17*5c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
18*5c51f124SMoriah Waterland *
19*5c51f124SMoriah Waterland * CDDL HEADER END
20*5c51f124SMoriah Waterland */
21*5c51f124SMoriah Waterland
22*5c51f124SMoriah Waterland /*
23*5c51f124SMoriah Waterland * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*5c51f124SMoriah Waterland * Use is subject to license terms.
25*5c51f124SMoriah Waterland */
26*5c51f124SMoriah Waterland
27*5c51f124SMoriah Waterland
28*5c51f124SMoriah Waterland
29*5c51f124SMoriah Waterland #include <stdio.h>
30*5c51f124SMoriah Waterland #include <sys/types.h>
31*5c51f124SMoriah Waterland #include <sys/socket.h>
32*5c51f124SMoriah Waterland #include <netdb.h>
33*5c51f124SMoriah Waterland #include <unistd.h>
34*5c51f124SMoriah Waterland #include <sys/types.h>
35*5c51f124SMoriah Waterland #include <sys/socket.h>
36*5c51f124SMoriah Waterland #include <sys/sockio.h>
37*5c51f124SMoriah Waterland #include <net/if.h>
38*5c51f124SMoriah Waterland #include <netinet/in.h>
39*5c51f124SMoriah Waterland #include <arpa/inet.h>
40*5c51f124SMoriah Waterland #include <errno.h>
41*5c51f124SMoriah Waterland #include <stdlib.h>
42*5c51f124SMoriah Waterland #include <string.h>
43*5c51f124SMoriah Waterland #include <libintl.h>
44*5c51f124SMoriah Waterland
45*5c51f124SMoriah Waterland static int is_local_if(struct hostent *hp);
46*5c51f124SMoriah Waterland
47*5c51f124SMoriah Waterland /*
48*5c51f124SMoriah Waterland * Given a host name, check to see if it points to the local host.
49*5c51f124SMoriah Waterland * If it does, return 1, else return 0.
50*5c51f124SMoriah Waterland *
51*5c51f124SMoriah Waterland * The strategy is this: translate the host name argument to a list of
52*5c51f124SMoriah Waterland * addresses. Then compare each of those addresses to the addresses of
53*5c51f124SMoriah Waterland * network interfaces on this host.
54*5c51f124SMoriah Waterland */
55*5c51f124SMoriah Waterland int
is_local_host(char * host)56*5c51f124SMoriah Waterland is_local_host(char *host)
57*5c51f124SMoriah Waterland {
58*5c51f124SMoriah Waterland struct hostent *hp;
59*5c51f124SMoriah Waterland int err;
60*5c51f124SMoriah Waterland int flags = AI_DEFAULT;
61*5c51f124SMoriah Waterland
62*5c51f124SMoriah Waterland if (hp = getipnodebyname((const char *) host, AF_INET, flags, &err))
63*5c51f124SMoriah Waterland if (is_local_if(hp))
64*5c51f124SMoriah Waterland return (1);
65*5c51f124SMoriah Waterland if (hp = getipnodebyname((const char *) host, AF_INET6, flags, &err))
66*5c51f124SMoriah Waterland if (is_local_if(hp))
67*5c51f124SMoriah Waterland return (1);
68*5c51f124SMoriah Waterland
69*5c51f124SMoriah Waterland return (0);
70*5c51f124SMoriah Waterland }
71*5c51f124SMoriah Waterland
72*5c51f124SMoriah Waterland static int
is_local_if(struct hostent * hp)73*5c51f124SMoriah Waterland is_local_if(struct hostent *hp)
74*5c51f124SMoriah Waterland {
75*5c51f124SMoriah Waterland char *buf;
76*5c51f124SMoriah Waterland struct lifconf lifc;
77*5c51f124SMoriah Waterland struct lifnum lifn;
78*5c51f124SMoriah Waterland struct lifreq lifr;
79*5c51f124SMoriah Waterland struct lifreq *lifrp;
80*5c51f124SMoriah Waterland int bufsiz;
81*5c51f124SMoriah Waterland int nha;
82*5c51f124SMoriah Waterland int nif;
83*5c51f124SMoriah Waterland int s;
84*5c51f124SMoriah Waterland
85*5c51f124SMoriah Waterland if ((s = socket(hp->h_addrtype, SOCK_DGRAM, 0)) == -1) {
86*5c51f124SMoriah Waterland perror("socket");
87*5c51f124SMoriah Waterland return (0);
88*5c51f124SMoriah Waterland }
89*5c51f124SMoriah Waterland
90*5c51f124SMoriah Waterland lifn.lifn_family = hp->h_addrtype;
91*5c51f124SMoriah Waterland lifn.lifn_flags = LIFC_EXTERNAL_SOURCE;
92*5c51f124SMoriah Waterland if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) == -1) {
93*5c51f124SMoriah Waterland perror("SIOCGLIFNUM");
94*5c51f124SMoriah Waterland (void) close(s);
95*5c51f124SMoriah Waterland return (0);
96*5c51f124SMoriah Waterland }
97*5c51f124SMoriah Waterland bufsiz = lifn.lifn_count * sizeof (struct lifreq);
98*5c51f124SMoriah Waterland
99*5c51f124SMoriah Waterland if ((buf = malloc(bufsiz)) == NULL) {
100*5c51f124SMoriah Waterland perror("malloc");
101*5c51f124SMoriah Waterland (void) close(s);
102*5c51f124SMoriah Waterland return (0);
103*5c51f124SMoriah Waterland }
104*5c51f124SMoriah Waterland
105*5c51f124SMoriah Waterland lifc.lifc_family = hp->h_addrtype;
106*5c51f124SMoriah Waterland lifc.lifc_flags = LIFC_EXTERNAL_SOURCE;
107*5c51f124SMoriah Waterland lifc.lifc_len = bufsiz;
108*5c51f124SMoriah Waterland lifc.lifc_buf = buf;
109*5c51f124SMoriah Waterland if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) == -1) {
110*5c51f124SMoriah Waterland perror("SIOCGLIFCONF");
111*5c51f124SMoriah Waterland (void) close(s);
112*5c51f124SMoriah Waterland free(buf);
113*5c51f124SMoriah Waterland return (0);
114*5c51f124SMoriah Waterland }
115*5c51f124SMoriah Waterland
116*5c51f124SMoriah Waterland #define lifraddrp(lifrp) ((lifrp->lifr_addr.ss_family == AF_INET6) ? \
117*5c51f124SMoriah Waterland (void *) &((struct sockaddr_in6 *)&lifrp->lifr_addr)->sin6_addr : \
118*5c51f124SMoriah Waterland (void *) &((struct sockaddr_in *)&lifrp->lifr_addr)->sin_addr)
119*5c51f124SMoriah Waterland
120*5c51f124SMoriah Waterland for (lifrp = lifc.lifc_req,
121*5c51f124SMoriah Waterland nif = lifc.lifc_len / sizeof (struct lifreq);
122*5c51f124SMoriah Waterland nif > 0; nif--, lifrp++) {
123*5c51f124SMoriah Waterland if (lifrp->lifr_addr.ss_family != hp->h_addrtype) {
124*5c51f124SMoriah Waterland continue;
125*5c51f124SMoriah Waterland }
126*5c51f124SMoriah Waterland (void) memset(&lifr, 0, sizeof (lifr));
127*5c51f124SMoriah Waterland (void) strncpy(lifr.lifr_name, lifrp->lifr_name,
128*5c51f124SMoriah Waterland sizeof (lifr.lifr_name));
129*5c51f124SMoriah Waterland if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) == -1) {
130*5c51f124SMoriah Waterland perror("SIOCGLIFFLAGS");
131*5c51f124SMoriah Waterland (void) close(s);
132*5c51f124SMoriah Waterland free(buf);
133*5c51f124SMoriah Waterland return (0);
134*5c51f124SMoriah Waterland }
135*5c51f124SMoriah Waterland
136*5c51f124SMoriah Waterland for (nha = 0; hp->h_addr_list[nha]; nha++) {
137*5c51f124SMoriah Waterland if (memcmp(hp->h_addr_list[nha], lifraddrp(lifrp),
138*5c51f124SMoriah Waterland hp->h_length) == 0) {
139*5c51f124SMoriah Waterland (void) close(s);
140*5c51f124SMoriah Waterland free(buf);
141*5c51f124SMoriah Waterland return (1);
142*5c51f124SMoriah Waterland }
143*5c51f124SMoriah Waterland }
144*5c51f124SMoriah Waterland }
145*5c51f124SMoriah Waterland
146*5c51f124SMoriah Waterland #undef lifraddrp
147*5c51f124SMoriah Waterland
148*5c51f124SMoriah Waterland (void) close(s);
149*5c51f124SMoriah Waterland free(buf);
150*5c51f124SMoriah Waterland return (0);
151*5c51f124SMoriah Waterland }
152