]> git.decadent.org.uk Git - nfs-utils.git/blob - support/misc/from_local.c
Replace the Sun RPC license with the BSD license,
[nfs-utils.git] / support / misc / 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  * Copyright (c) 2009, Sun Microsystems, Inc.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * - Redistributions of source code must retain the above copyright notice,
15  *   this list of conditions and the following disclaimer.
16  * - Redistributions in binary form must reproduce the above copyright notice,
17  *   this list of conditions and the following disclaimer in the documentation
18  *   and/or other materials provided with the distribution.
19  * - Neither the name of Sun Microsystems, Inc. nor the names of its
20  *   contributors may be used to endorse or promote products derived
21  *   from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #if 0
37 static char sccsid[] = "@(#) from_local.c 1.3 96/05/31 15:52:57";
38 #endif
39
40 #ifdef TEST
41 #undef perror
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <netdb.h>
49 #include <netinet/in.h>
50 #include <net/if.h>
51 #include <sys/ioctl.h>
52 #include <syslog.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #ifndef TRUE
57 #define TRUE    1
58 #define FALSE   0
59 #endif
60
61  /*
62   * With virtual hosting, each hardware network interface can have multiple
63   * network addresses. On such machines the number of machine addresses can
64   * be surprisingly large.
65   */
66 static int num_local;
67 static int num_addrs;
68 static struct in_addr *addrs;
69
70 /* grow_addrs - extend list of local interface addresses */
71
72 static int grow_addrs(void)
73 {
74     struct in_addr *new_addrs;
75     int     new_num;
76
77     /*
78      * Keep the previous result if we run out of memory. The system would
79      * really get hosed if we simply give up.
80      */
81     new_num = (addrs == 0) ? 1 : num_addrs + num_addrs;
82     new_addrs = (struct in_addr *) malloc(sizeof(*addrs) * new_num);
83     if (new_addrs == 0) {
84         perror("portmap: out of memory");
85         return (0);
86     } else {
87         if (addrs != 0) {
88             memcpy((char *) new_addrs, (char *) addrs,
89                    sizeof(*addrs) * num_addrs);
90             free((char *) addrs);
91         }
92         num_addrs = new_num;
93         addrs = new_addrs;
94         return (1);
95     }
96 }
97
98 /* find_local - find all IP addresses for this host */
99 static int
100 find_local(void)
101 {
102     struct ifconf ifc;
103     struct ifreq ifreq;
104     struct ifreq *ifr;
105     struct ifreq *the_end;
106     int     sock;
107     char    buf[BUFSIZ];
108
109     /*
110      * Get list of network interfaces. We use a huge buffer to allow for the
111      * presence of non-IP interfaces.
112      */
113
114     if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
115         perror("socket");
116         return (0);
117     }
118     ifc.ifc_len = sizeof(buf);
119     ifc.ifc_buf = buf;
120     if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
121         perror("SIOCGIFCONF");
122         (void) close(sock);
123         return (0);
124     }
125     /* Get IP address of each active IP network interface. */
126
127     the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
128     num_local = 0;
129     for (ifr = ifc.ifc_req; ifr < the_end; ifr++) {
130         if (ifr->ifr_addr.sa_family == AF_INET) {       /* IP net interface */
131             ifreq = *ifr;
132             if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) {
133                 perror("SIOCGIFFLAGS");
134             } else if (ifreq.ifr_flags & IFF_UP) {      /* active interface */
135                 if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) {
136                     perror("SIOCGIFADDR");
137                 } else {
138                     if (num_local >= num_addrs)
139                         if (grow_addrs() == 0)
140                             break;
141                     addrs[num_local++] = ((struct sockaddr_in *)
142                                           & ifreq.ifr_addr)->sin_addr;
143                 }
144             }
145         }
146         /* Support for variable-length addresses. */
147 #ifdef HAS_SA_LEN
148         ifr = (struct ifreq *) ((caddr_t) ifr
149                       + ifr->ifr_addr.sa_len - sizeof(struct sockaddr));
150 #endif
151     }
152     (void) close(sock);
153     return (num_local);
154 }
155
156 /* from_local - determine whether request comes from the local system */
157 int
158 from_local(struct sockaddr_in *addr)
159 {
160     int     i;
161
162     if (addrs == 0 && find_local() == 0)
163         syslog(LOG_ERR, "cannot find any active local network interfaces");
164
165     for (i = 0; i < num_local; i++) {
166         if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
167                    sizeof(struct in_addr)) == 0)
168             return (TRUE);
169     }
170     return (FALSE);
171 }
172
173 #ifdef TEST
174
175 main()
176 {
177     char   *inet_ntoa();
178     int     i;
179
180     find_local();
181     for (i = 0; i < num_local; i++)
182         printf("%s\n", inet_ntoa(addrs[i]));
183 }
184
185 #endif