]> git.neil.brown.name Git - portmap.git/blob - from_local.c
Only fail an 'unregister' attempt if nothing can be unregistered.
[portmap.git] / from_local.c
1  /*
2   * Check if an address belongs to the local system. Adapted from:
3   * 
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.
6   */
7
8 /*
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.
16  *
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.
20  *
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.
24  *
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.
28  *
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.
32  *
33  * Sun Microsystems, Inc.
34  * 2550 Garcia Avenue
35  * Mountain View, California  94043
36  */
37
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <netdb.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #include <sys/ioctl.h>
46 #include <syslog.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #ifndef TRUE
51 #define TRUE    1
52 #define FALSE   0
53 #endif
54
55  /*
56   * With virtual hosting, each hardware network interface can have multiple
57   * network addresses. On such machines the number of machine addresses can
58   * be surprisingly large.
59   */
60 static int num_local;
61 static int num_addrs;
62 static struct in_addr *addrs;
63
64 /* grow_addrs - extend list of local interface addresses */
65
66 static int grow_addrs(void)
67 {
68     struct in_addr *new_addrs;
69     int     new_num;
70
71     /*
72      * Keep the previous result if we run out of memory. The system would
73      * really get hosed if we simply give up.
74      */
75     new_num = (addrs == 0) ? 1 : num_addrs + num_addrs;
76     new_addrs = (struct in_addr *) malloc(sizeof(*addrs) * new_num);
77     if (new_addrs == 0) {
78         perror("portmap: out of memory");
79         return (0);
80     } else {
81         if (addrs != 0) {
82             memcpy((char *) new_addrs, (char *) addrs,
83                    sizeof(*addrs) * num_addrs);
84             free((char *) addrs);
85         }
86         num_addrs = new_num;
87         addrs = new_addrs;
88         return (1);
89     }
90 }
91
92 /* find_local - find all IP addresses for this host */
93
94 static int
95 find_local(void)
96 {
97     struct ifconf ifc;
98     struct ifreq ifreq;
99     struct ifreq *ifr;
100     struct ifreq *the_end;
101     int     sock;
102     char    buf[BUFSIZ];
103
104     /*
105      * Get list of network interfaces. We use a huge buffer to allow for the
106      * presence of non-IP interfaces.
107      */
108
109     if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
110         perror("socket");
111         return (0);
112     }
113     ifc.ifc_len = sizeof(buf);
114     ifc.ifc_buf = buf;
115     if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
116         perror("SIOCGIFCONF");
117         (void) close(sock);
118         return (0);
119     }
120     /* Get IP address of each active IP network interface. */
121
122     the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
123     num_local = 0;
124     for (ifr = ifc.ifc_req; ifr < the_end; ifr++) {
125         if (ifr->ifr_addr.sa_family == AF_INET) {       /* IP net interface */
126             ifreq = *ifr;
127             if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
128                 perror("SIOCGIFFLAGS");
129             } else if (ifreq.ifr_flags & IFF_UP) {      /* active interface */
130                 if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
131                     perror("SIOCGIFADDR");
132                 } else {
133                     if (num_local >= num_addrs)
134                         if (grow_addrs() == 0)
135                             break;
136                     addrs[num_local++] = ((struct sockaddr_in *)
137                                           & ifreq.ifr_addr)->sin_addr;
138                 }
139             }
140         }
141         /* Support for variable-length addresses. */
142 #ifdef HAS_SA_LEN
143         ifr = (struct ifreq *) ((caddr_t) ifr
144                       + ifr->ifr_addr.sa_len - sizeof(struct sockaddr));
145 #endif
146     }
147     (void) close(sock);
148     return (num_local);
149 }
150
151 /* from_local - determine whether request comes from the local system */
152
153 int from_local(struct sockaddr_in *addr)
154 {
155     int     i;
156
157     if (addrs == 0 && find_local() == 0)
158         syslog(LOG_ERR, "cannot find any active local network interfaces");
159
160     for (i = 0; i < num_local; i++) {
161         if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
162                    sizeof(struct in_addr)) == 0)
163             return (TRUE);
164     }
165     /* maybe a new interface was added */
166     free(addrs);
167     addrs = NULL;
168     find_local();
169     for (i = 0; i < num_local; i++) {
170         if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
171                    sizeof(struct in_addr)) == 0)
172             return (TRUE);
173     }
174
175     return (FALSE);
176 }
177
178 #ifdef TEST
179
180 main()
181 {
182     char   *inet_ntoa();
183     int     i;
184
185     find_local();
186     for (i = 0; i < num_local; i++)
187         printf("%s\n", inet_ntoa(addrs[i]));
188 }
189
190 #endif