xref: /csrg-svn/lib/librpc/rpc/get_myaddress.c (revision 46638)
145080Smckusick /* @(#)get_myaddress.c	2.1 88/07/29 4.0 RPCSRC */
245080Smckusick /*
345080Smckusick  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
445080Smckusick  * unrestricted use provided that this legend is included on all tape
545080Smckusick  * media and as a part of the software program in whole or part.  Users
645080Smckusick  * may copy or modify Sun RPC without charge, but are not authorized
745080Smckusick  * to license or distribute it to anyone else except as part of a product or
845080Smckusick  * program developed by the user.
945080Smckusick  *
1045080Smckusick  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1145080Smckusick  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1245080Smckusick  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1345080Smckusick  *
1445080Smckusick  * Sun RPC is provided with no support and without any obligation on the
1545080Smckusick  * part of Sun Microsystems, Inc. to assist in its use, correction,
1645080Smckusick  * modification or enhancement.
1745080Smckusick  *
1845080Smckusick  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1945080Smckusick  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2045080Smckusick  * OR ANY PART THEREOF.
2145080Smckusick  *
2245080Smckusick  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2345080Smckusick  * or profits or other special, indirect and consequential damages, even if
2445080Smckusick  * Sun has been advised of the possibility of such damages.
2545080Smckusick  *
2645080Smckusick  * Sun Microsystems, Inc.
2745080Smckusick  * 2550 Garcia Avenue
2845080Smckusick  * Mountain View, California  94043
2945080Smckusick  */
3045080Smckusick #if !defined(lint) && defined(SCCSIDS)
3145080Smckusick static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";
3245080Smckusick #endif
3345080Smckusick 
3445080Smckusick /*
3545080Smckusick  * get_myaddress.c
3645080Smckusick  *
3745080Smckusick  * Get client's IP address via ioctl.  This avoids using the yellowpages.
3845080Smckusick  * Copyright (C) 1984, Sun Microsystems, Inc.
3945080Smckusick  */
4045080Smckusick 
4145080Smckusick #include <rpc/types.h>
4245080Smckusick #include <rpc/pmap_prot.h>
4345080Smckusick #include <sys/socket.h>
4445080Smckusick #include <stdio.h>
4545080Smckusick #include <net/if.h>
4645080Smckusick #include <sys/ioctl.h>
47*46638Sbostic #include <netinet/in.h>
4845080Smckusick #include <arpa/inet.h>
4945080Smckusick 
5045080Smckusick /*
5145080Smckusick  * don't use gethostbyname, which would invoke yellow pages
5245080Smckusick  */
5345080Smckusick get_myaddress(addr)
5445080Smckusick 	struct sockaddr_in *addr;
5545080Smckusick {
5645080Smckusick 	int s;
5745080Smckusick 	char buf[BUFSIZ];
5845080Smckusick 	struct ifconf ifc;
5945080Smckusick 	struct ifreq ifreq, *ifr;
6045128Smckusick 	int len, slop;
6145080Smckusick 
6245080Smckusick 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
6345080Smckusick 	    perror("get_myaddress: socket");
6445080Smckusick 	    exit(1);
6545080Smckusick 	}
6645080Smckusick 	ifc.ifc_len = sizeof (buf);
6745080Smckusick 	ifc.ifc_buf = buf;
6845080Smckusick 	if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
6945080Smckusick 		perror("get_myaddress: ioctl (get interface configuration)");
7045080Smckusick 		exit(1);
7145080Smckusick 	}
7245080Smckusick 	ifr = ifc.ifc_req;
7345080Smckusick 	for (len = ifc.ifc_len; len; len -= sizeof ifreq) {
7445080Smckusick 		ifreq = *ifr;
7545080Smckusick 		if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
7645080Smckusick 			perror("get_myaddress: ioctl");
7745080Smckusick 			exit(1);
7845080Smckusick 		}
7945080Smckusick 		if ((ifreq.ifr_flags & IFF_UP) &&
8045080Smckusick 		    ifr->ifr_addr.sa_family == AF_INET) {
8145080Smckusick 			*addr = *((struct sockaddr_in *)&ifr->ifr_addr);
8245080Smckusick 			addr->sin_port = htons(PMAPPORT);
8345080Smckusick 			break;
8445080Smckusick 		}
8545128Smckusick 		/*
8645128Smckusick 		 * Deal with variable length addresses
8745128Smckusick 		 */
8845128Smckusick 		slop = ifr->ifr_addr.sa_len - sizeof (struct sockaddr);
8945128Smckusick 		if (slop) {
9045128Smckusick 			ifr = (struct ifreq *) ((caddr_t)ifr + slop);
9145128Smckusick 			len -= slop;
9245128Smckusick 		}
9345080Smckusick 		ifr++;
9445080Smckusick 	}
9545080Smckusick 	(void) close(s);
9645080Smckusick }
97