2 * Check if an address belongs to the local system. Adapted from:
4 * @(#)pmap_svc.c 1.32 91/03/11 Copyright 1984,1990 Sun Microsystems, Inc.
5 * @(#)get_myaddress.c 2.1 88/07/29 4.0 RPCSRC.
9 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
10 * unrestricted use provided that this legend is included on all tape
11 * media and as a part of the software program in whole or part. Users
12 * may copy or modify Sun RPC without charge, but are not authorized
13 * to license or distribute it to anyone else except as part of a product or
14 * program developed by the user or with the express written consent of
15 * Sun Microsystems, Inc.
17 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
18 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
21 * Sun RPC is provided with no support and without any obligation on the
22 * part of Sun Microsystems, Inc. to assist in its use, correction,
23 * modification or enhancement.
25 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
26 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
27 * OR ANY PART THEREOF.
29 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
30 * or profits or other special, indirect and consequential damages, even if
31 * Sun has been advised of the possibility of such damages.
33 * Sun Microsystems, Inc.
35 * Mountain View, California 94043
39 static char sccsid[] = "@(#) from_local.c 1.3 96/05/31 15:52:57";
46 #include <sys/types.h>
47 #include <sys/socket.h>
51 #include <netinet/in.h>
53 #include <sys/ioctl.h>
64 * With virtual hosting, each hardware network interface can have multiple
65 * network addresses. On such machines the number of machine addresses can
66 * be surprisingly large.
70 static struct in_addr *addrs;
72 /* grow_addrs - extend list of local interface addresses */
74 static int grow_addrs(void)
76 struct in_addr *new_addrs;
80 * Keep the previous result if we run out of memory. The system would
81 * really get hosed if we simply give up.
83 new_num = (addrs == 0) ? 1 : num_addrs + num_addrs;
84 new_addrs = (struct in_addr *) malloc(sizeof(*addrs) * new_num);
86 perror("portmap: out of memory");
90 memcpy((char *) new_addrs, (char *) addrs,
91 sizeof(*addrs) * num_addrs);
100 /* find_local - find all IP addresses for this host */
107 struct ifreq *the_end;
112 * Get list of network interfaces. We use a huge buffer to allow for the
113 * presence of non-IP interfaces.
116 if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
120 ifc.ifc_len = sizeof(buf);
122 if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
123 perror("SIOCGIFCONF");
127 /* Get IP address of each active IP network interface. */
129 the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
131 for (ifr = ifc.ifc_req; ifr < the_end; ifr++) {
132 if (ifr->ifr_addr.sa_family == AF_INET) { /* IP net interface */
134 if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
135 perror("SIOCGIFFLAGS");
136 } else if (ifreq.ifr_flags & IFF_UP) { /* active interface */
137 if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
138 perror("SIOCGIFADDR");
140 if (num_local >= num_addrs)
141 if (grow_addrs() == 0)
143 addrs[num_local++] = ((struct sockaddr_in *)
144 & ifreq.ifr_addr)->sin_addr;
148 /* Support for variable-length addresses. */
150 ifr = (struct ifreq *) ((caddr_t) ifr
151 + ifr->ifr_addr.sa_len - sizeof(struct sockaddr));
158 /* from_local - determine whether request comes from the local system */
160 from_local(struct sockaddr_in *addr)
164 if (addrs == 0 && find_local() == 0)
165 syslog(LOG_ERR, "cannot find any active local network interfaces");
167 for (i = 0; i < num_local; i++) {
168 if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
169 sizeof(struct in_addr)) == 0)
183 for (i = 0; i < num_local; i++)
184 printf("%s\n", inet_ntoa(addrs[i]));