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
40 static char sccsid[] = "@(#) from_local.c 1.2 93/11/16 21:50:02";
42 static const char rcsid[] =
43 "$FreeBSD: src/usr.sbin/portmap/from_local.c,v 1.10.2.1 2000/08/16 14:04:37 brian Exp $";
50 #include <sys/types.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
53 #include <sys/sysctl.h>
65 #include <net/if_dl.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
69 #include "pmap_check.h"
76 #define ROUNDUP(x) ((x) ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long))
78 /* How many interfaces could there be on a computer? */
80 #define ESTIMATED_LOCAL 20
81 static int num_local = -1;
82 static struct in_addr *addrs;
85 rtiparse(struct ifa_msghdr *ifam, struct rt_addrinfo *ai)
90 wp = (char *)(ifam + 1);
92 ai->rti_addrs = ifam->ifam_addrs;
93 for (rtax = 0; rtax < sizeof ai->rti_info / sizeof *ai->rti_info; rtax++)
94 if (ifam->ifam_addrs & (1 << rtax)) {
95 ai->rti_info[rtax] = (struct sockaddr *)wp;
96 wp += ROUNDUP(ai->rti_info[rtax]->sa_len);
98 ai->rti_info[rtax] = NULL;
101 /* find_local - find all IP addresses for this host */
106 int mib[6], n, s, alloced;
108 char *buf, *end, *ptr;
109 struct if_msghdr *ifm;
110 struct ifa_msghdr *ifam;
111 struct rt_addrinfo ai;
113 struct sockaddr_dl *dl;
117 mib[4] = NET_RT_IFLIST;
118 mib[2] = mib[3] = mib[5] = 0;
120 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
124 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
126 perror("sysctl(NET_RT_IFLIST)");
129 if ((buf = (char *)malloc(needed)) == NULL) {
134 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
137 perror("sysctl(NET_RT_IFLIST)(after malloc)");
149 for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
150 ifm = (struct if_msghdr *)ptr;
151 dl = (struct sockaddr_dl *)(ifm + 1);
153 if (ifm->ifm_index != dl->sdl_index || dl->sdl_nlen == 0)
154 /* Skip over remaining ifa_msghdrs */
157 n = dl->sdl_nlen > sizeof ifr.ifr_name ?
158 sizeof ifr.ifr_name : dl->sdl_nlen;
159 strncpy(ifr.ifr_name, dl->sdl_data, n);
160 if (n < sizeof ifr.ifr_name)
161 ifr.ifr_name[n] = '\0';
163 /* we only want the first address from each interface */
164 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
165 fprintf(stderr, "%.*s: SIOCGIFFLAGS: %s\n", n, ifr.ifr_name,
167 else if (ifr.ifr_flags & IFF_UP) { /* active interface */
168 ifam = (struct ifa_msghdr *)(ptr + ifm->ifm_msglen);
169 while ((char *)ifam < end && ifam->ifam_type == RTM_NEWADDR) {
172 if (ai.rti_info[RTAX_IFA] != NULL &&
173 ai.rti_info[RTAX_IFA]->sa_family == AF_INET) {
174 if (alloced < num_local + 1) {
175 alloced += ESTIMATED_LOCAL;
176 addrs = (struct in_addr *)realloc(addrs, alloced * sizeof addrs[0]);
178 perror("malloc/realloc");
183 addrs[num_local++] = ((struct sockaddr_in *)
184 ai.rti_info[RTAX_IFA])->sin_addr;
187 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
197 /* from_local - determine whether request comes from the local system */
201 struct sockaddr_in *addr;
205 if (num_local == -1 && find_local() == 0)
206 syslog(LOG_ERR, "cannot find any active local network interfaces");
208 for (i = 0; i < num_local; i++) {
209 if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
210 sizeof(struct in_addr)) == 0)
227 for (i = 0; i < num_local; i++)
228 printf("%s\n", inet_ntoa(addrs[i]));