1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate /*
24*7c478bd9Sstevel@tonic-gate  * get_myaddress.c
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * Get client's IP address via ioctl.  This avoids using the NIS.
27*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1990, Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <rpc/types.h>
31*7c478bd9Sstevel@tonic-gate #include <rpc/pmap_prot.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <net/if.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
37*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
38*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
39*7c478bd9Sstevel@tonic-gate #include <syslog.h>
40*7c478bd9Sstevel@tonic-gate #include <malloc.h>
41*7c478bd9Sstevel@tonic-gate #include <unistd.h>
42*7c478bd9Sstevel@tonic-gate #include <stropts.h>
43*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
44*7c478bd9Sstevel@tonic-gate #include <errno.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * don't use gethostbyname, which would invoke NIS
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate void
get_myaddress(struct sockaddr_in * addr)50*7c478bd9Sstevel@tonic-gate get_myaddress(struct sockaddr_in *addr)
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	int s;
53*7c478bd9Sstevel@tonic-gate 	struct ifconf ifc;
54*7c478bd9Sstevel@tonic-gate 	struct ifreq ifreq, *ifr;
55*7c478bd9Sstevel@tonic-gate 	int len, numifs;
56*7c478bd9Sstevel@tonic-gate 	int ret;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
59*7c478bd9Sstevel@tonic-gate 	    syslog(LOG_ERR, "get_myaddress: socket: %m");
60*7c478bd9Sstevel@tonic-gate 	    exit(1);
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	do {
64*7c478bd9Sstevel@tonic-gate 		ret = ioctl(s, SIOCGIFNUM, (char *)&numifs);
65*7c478bd9Sstevel@tonic-gate 	} while (ret < 0 && (errno == EINTR || errno == EAGAIN));
66*7c478bd9Sstevel@tonic-gate 	if (ret < 0) {
67*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "get_myaddress: ioctl: %m");
68*7c478bd9Sstevel@tonic-gate 		exit(1);
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	ifc.ifc_len = numifs * sizeof (struct ifreq);
72*7c478bd9Sstevel@tonic-gate 	if ((ifc.ifc_buf = (caddr_t)malloc(ifc.ifc_len)) == NULL) {
73*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "get_myaddress: malloc: %m");
74*7c478bd9Sstevel@tonic-gate 		exit(1);
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	do {
78*7c478bd9Sstevel@tonic-gate 		ret = ioctl(s, SIOCGIFCONF, (char *)&ifc);
79*7c478bd9Sstevel@tonic-gate 	} while (ret < 0 && (errno == EINTR || errno == EAGAIN));
80*7c478bd9Sstevel@tonic-gate 	if (ret < 0) {
81*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
82*7c478bd9Sstevel@tonic-gate 		    "get_myaddress: ioctl (get interface configuration): %m");
83*7c478bd9Sstevel@tonic-gate 		exit(1);
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/*
87*7c478bd9Sstevel@tonic-gate 	 * set default to loopback in case nothing is found.
88*7c478bd9Sstevel@tonic-gate 	 */
89*7c478bd9Sstevel@tonic-gate 	addr->sin_family = AF_INET;
90*7c478bd9Sstevel@tonic-gate 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
91*7c478bd9Sstevel@tonic-gate 	addr->sin_port = htons(PMAPPORT);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	ifr = ifc.ifc_req;
94*7c478bd9Sstevel@tonic-gate 	for (len = ifc.ifc_len; len > 0; len -= sizeof (ifreq), ifr++) {
95*7c478bd9Sstevel@tonic-gate 		ifreq = *ifr;
96*7c478bd9Sstevel@tonic-gate 		do {
97*7c478bd9Sstevel@tonic-gate 			ret = ioctl(s, SIOCGIFFLAGS, (char *)&ifreq);
98*7c478bd9Sstevel@tonic-gate 		} while (ret < 0 && (errno == EINTR || errno == EAGAIN));
99*7c478bd9Sstevel@tonic-gate 		if (ret < 0) {
100*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "get_myaddress: ioctl: %m");
101*7c478bd9Sstevel@tonic-gate 			exit(1);
102*7c478bd9Sstevel@tonic-gate 		}
103*7c478bd9Sstevel@tonic-gate 		if (ifr->ifr_addr.sa_family != AF_INET)
104*7c478bd9Sstevel@tonic-gate 			continue;
105*7c478bd9Sstevel@tonic-gate 		if ((ifreq.ifr_flags & IFF_UP) == 0)
106*7c478bd9Sstevel@tonic-gate 			continue;
107*7c478bd9Sstevel@tonic-gate 		if (ifreq.ifr_flags & IFF_LOOPBACK)
108*7c478bd9Sstevel@tonic-gate 			continue;
109*7c478bd9Sstevel@tonic-gate 		if ((ifreq.ifr_flags & (IFF_MULTICAST | IFF_BROADCAST)) == 0)
110*7c478bd9Sstevel@tonic-gate 			continue;
111*7c478bd9Sstevel@tonic-gate 		*addr = *((struct sockaddr_in *)&ifr->ifr_addr);
112*7c478bd9Sstevel@tonic-gate 		addr->sin_port = htons(PMAPPORT);
113*7c478bd9Sstevel@tonic-gate 		break;
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 	free(ifc.ifc_buf);
116*7c478bd9Sstevel@tonic-gate 	(void) close(s);
117*7c478bd9Sstevel@tonic-gate }
118