xref: /openbsd-src/lib/libc/rpc/get_myaddress.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: get_myaddress.c,v 1.13 2010/09/01 14:43:34 millert Exp $ */
2 
3 /*
4  * Copyright (c) 2010, Oracle America, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials
15  *       provided with the distribution.
16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * get_myaddress.c
36  *
37  * Get client's IP address via ioctl.  This avoids using the yellowpages.
38  */
39 
40 #include <rpc/types.h>
41 #include <rpc/xdr.h>
42 #include <rpc/pmap_prot.h>
43 #include <sys/socket.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <net/if.h>
48 #include <netinet/in.h>
49 #include <ifaddrs.h>
50 
51 /*
52  * don't use gethostbyname, which would invoke yellow pages
53  *
54  * Avoid loopback interfaces.  We return information from a loopback
55  * interface only if there are no other possible interfaces.
56  */
57 int
58 get_myaddress(struct sockaddr_in *addr)
59 {
60 	struct ifaddrs *ifap, *ifa;
61 	int loopback = 0, gotit = 0;
62 
63 	if (getifaddrs(&ifap) != 0)
64 		return (-1);
65 
66   again:
67 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
68 		if ((ifa->ifa_flags & IFF_UP) &&
69 		    ifa->ifa_addr->sa_family == AF_INET &&
70 		    (loopback == 1 && (ifa->ifa_flags & IFF_LOOPBACK))) {
71 			*addr = *((struct sockaddr_in *)ifa->ifa_addr);
72 			addr->sin_port = htons(PMAPPORT);
73 			gotit = 1;
74 			break;
75 		}
76 	}
77 	if (gotit == 0 && loopback == 0) {
78 		loopback = 1;
79 		goto again;
80 	}
81 	freeifaddrs(ifap);
82 	return (0);
83 }
84