bf67a62a9e36a689a673bdaa09884013f174e281
[openafs.git] / src / rx / rx_getaddr.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #ifndef AFS_DJGPP_ENV
17 #ifndef KERNEL
18 #ifndef AFS_NT40_ENV
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/time.h>
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <sys/ioctl.h>
25 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
26 #include <sys/sysctl.h>
27 #include <net/route.h>
28 #include <net/if_dl.h>
29 #endif
30
31 /*
32  * By including this, we get any system dependencies. In particular,
33  * the pthreads for solaris requires the socket call to be mapped.
34  */
35 #include "rx.h"
36 #endif
37 #else /* KERNEL */
38 #ifdef UKERNEL
39 #include "rx/rx_kcommon.h"
40 #else /* UKERNEL */
41 #include "rx/rx.h"
42 #endif /* UKERNEL */
43 #endif /* KERNEL */
44
45 #define NIFS            512
46
47 #ifdef KERNEL
48 /* only used for generating random noise */
49
50 afs_int32 rxi_tempAddr = 0;     /* default attempt */
51
52 /* set the advisory noise */
53 void
54 rxi_setaddr(afs_int32 x)
55 {
56     rxi_tempAddr = x;
57 }
58
59 /* get approx to net addr */
60 afs_int32
61 rxi_getaddr(void)
62 {
63     return rxi_tempAddr;
64 }
65
66 #endif /* KERNEL */
67
68 #ifndef KERNEL
69
70 /* to satisfy those who call setaddr */
71 void
72 rxi_setaddr(afs_int32 x)
73 {
74 }
75
76 #endif /* !KERNEL */
77
78
79 #if !defined(AFS_NT40_ENV)
80 /* For NT, rxi_getaddr has moved to rx_user.c. rxi_GetIfInfo is called by
81  * rx_Init which sets up the list of addresses for us.
82  */
83
84 #ifndef KERNEL
85
86 /* Return our internet address as a long in network byte order.  Returns zero
87  * if it can't find one.
88  */
89 afs_int32
90 rxi_getaddr(void)
91 {
92     afs_int32 buffer[1024];
93     int count;
94
95     count = rx_getAllAddr(buffer, 1024);
96     if (count > 0)
97         return buffer[0];       /* returns the first address */
98     else
99         return count;
100 }
101
102 #endif /* !KERNEL */
103
104 #if !defined(KERNEL) || defined(UKERNEL)
105
106 #ifndef MAX
107 #define MAX(A,B) (((A)<(B)) ? (B) : (A))
108 #endif
109
110 #ifdef UKERNEL
111 #undef ioctl
112 #undef socket
113 #endif /* UKERNEL */
114
115 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
116 #define ROUNDUP(a) \
117         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
118 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
119
120 static void
121 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
122 {
123     struct sockaddr *sa;
124     int i;
125
126     memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
127     for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
128         if ((rtinfo->rti_addrs & (1 << i)) == 0)
129             continue;
130         rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
131         ADVANCE(cp, sa);
132     }
133 }
134 #endif
135
136
137 /* this function returns the total number of interface addresses 
138 ** the buffer has to be passed in by the caller
139 */
140 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
141 int
142 rx_getAllAddr(afs_int32 buffer[], int maxSize)
143 {
144     size_t needed;
145     int mib[6];
146     struct if_msghdr *ifm, *nextifm;
147     struct ifa_msghdr *ifam;
148     struct sockaddr_dl *sdl;
149     struct rt_addrinfo info;
150     char *buf, *lim, *next;
151     int count = 0, addrcount = 0;
152
153     mib[0] = CTL_NET;
154     mib[1] = PF_ROUTE;
155     mib[2] = 0;
156     mib[3] = AF_INET;           /* address family */
157     mib[4] = NET_RT_IFLIST;
158     mib[5] = 0;
159     if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
160         return 0;
161     if ((buf = malloc(needed)) == NULL)
162         return 0;
163     if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
164         free(buf);
165         return 0;
166     }
167     lim = buf + needed;
168     next = buf;
169     while (next < lim) {
170         ifm = (struct if_msghdr *)next;
171         if (ifm->ifm_type != RTM_IFINFO) {
172             dpf(("out of sync parsing NET_RT_IFLIST\n"));
173             free(buf);
174             return 0;
175         }
176         sdl = (struct sockaddr_dl *)(ifm + 1);
177         next += ifm->ifm_msglen;
178         ifam = NULL;
179         addrcount = 0;
180         while (next < lim) {
181             nextifm = (struct if_msghdr *)next;
182             if (nextifm->ifm_type != RTM_NEWADDR)
183                 break;
184             if (ifam == NULL)
185                 ifam = (struct ifa_msghdr *)nextifm;
186             addrcount++;
187             next += nextifm->ifm_msglen;
188         }
189         if ((ifm->ifm_flags & IFF_UP) == 0)
190             continue;           /* not up */
191         if (ifm->ifm_flags & IFF_LOOPBACK) {
192             continue;           /* skip aliased loopbacks as well. */
193         }
194         while (addrcount > 0) {
195             struct sockaddr_in *a;
196
197             info.rti_addrs = ifam->ifam_addrs;
198
199             /* Expand the compacted addresses */
200             rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
201                       &info);
202             if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
203                 continue;
204             a = info.rti_info[RTAX_IFA];
205
206             if (count >= maxSize)       /* no more space */
207                 dpf(("Too many interfaces..ignoring 0x%x\n",
208                        a->sin_addr.s_addr));
209             else
210                 buffer[count++] = a->sin_addr.s_addr;
211             addrcount--;
212             ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
213         }
214     }
215     free(buf);
216     return count;
217 }
218
219 int
220 rxi_getAllAddrMaskMtu(afs_int32 addrBuffer[], afs_int32 maskBuffer[],
221                       afs_int32 mtuBuffer[], int maxSize)
222 {
223     int s;
224
225     size_t needed;
226     int mib[6];
227     struct if_msghdr *ifm, *nextifm;
228     struct ifa_msghdr *ifam;
229     struct sockaddr_dl *sdl;
230     struct rt_addrinfo info;
231     char *buf, *lim, *next;
232     int count = 0, addrcount = 0;
233
234     mib[0] = CTL_NET;
235     mib[1] = PF_ROUTE;
236     mib[2] = 0;
237     mib[3] = AF_INET;           /* address family */
238     mib[4] = NET_RT_IFLIST;
239     mib[5] = 0;
240     if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
241         return 0;
242     if ((buf = malloc(needed)) == NULL)
243         return 0;
244     if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
245         free(buf);
246         return 0;
247     }
248     s = socket(PF_INET, SOCK_DGRAM, 0);
249     if (s < 0)
250         return 0;
251     lim = buf + needed;
252     next = buf;
253     while (next < lim) {
254         ifm = (struct if_msghdr *)next;
255         if (ifm->ifm_type != RTM_IFINFO) {
256             dpf(("out of sync parsing NET_RT_IFLIST\n"));
257             free(buf);
258             return 0;
259         }
260         sdl = (struct sockaddr_dl *)(ifm + 1);
261         next += ifm->ifm_msglen;
262         ifam = NULL;
263         addrcount = 0;
264         while (next < lim) {
265             nextifm = (struct if_msghdr *)next;
266             if (nextifm->ifm_type != RTM_NEWADDR)
267                 break;
268             if (ifam == NULL)
269                 ifam = (struct ifa_msghdr *)nextifm;
270             addrcount++;
271             next += nextifm->ifm_msglen;
272         }
273         if ((ifm->ifm_flags & IFF_UP) == 0)
274             continue;           /* not up */
275         while (addrcount > 0) {
276             struct sockaddr_in *a;
277
278             info.rti_addrs = ifam->ifam_addrs;
279
280             /* Expand the compacted addresses */
281             rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
282                       &info);
283             if (info.rti_info[RTAX_IFA]->sa_family != AF_INET)
284                 continue;
285             a = info.rti_info[RTAX_IFA];
286
287             if (a->sin_addr.s_addr != htonl(0x7f000001) ) {
288                 if (count >= maxSize) { /* no more space */
289                     dpf(("Too many interfaces..ignoring 0x%x\n",
290                            a->sin_addr.s_addr));
291                 } else {
292                     struct ifreq ifr;
293                     
294                     addrBuffer[count] = a->sin_addr.s_addr;
295                     a = info.rti_info[RTAX_NETMASK];
296                     if (a)
297                         maskBuffer[count] = a->sin_addr.s_addr;
298                     else
299                         maskBuffer[count] = htonl(0xffffffff);
300                     memset(&ifr, sizeof(ifr), 0);
301                     ifr.ifr_addr.sa_family = AF_INET;
302                     strncpy(ifr.ifr_name, sdl->sdl_data, sdl->sdl_nlen);
303                     if (ioctl(s, SIOCGIFMTU, (caddr_t) & ifr) < 0)
304                         mtuBuffer[count] = htonl(1500);
305                     else
306                         mtuBuffer[count] = htonl(ifr.ifr_mtu);
307                     count++;
308                 }
309             }
310             addrcount--;
311             ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
312         }
313     }
314     free(buf);
315     return count;
316 }
317 #else
318 static int
319 rx_getAllAddr_internal(afs_int32 buffer[], int maxSize, int loopbacks)
320 {
321     int s;
322     int i, len, count = 0;
323     struct ifconf ifc;
324     struct ifreq ifs[NIFS], *ifr;
325     struct sockaddr_in *a;
326 #if    defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
327     char *cp, *cplim, *cpnext;  /* used only for AIX 41 */
328 #endif
329
330     s = socket(AF_INET, SOCK_DGRAM, 0);
331     if (s < 0)
332         return 0;
333     ifc.ifc_len = sizeof(ifs);
334     ifc.ifc_buf = (caddr_t) ifs;
335     i = ioctl(s, SIOCGIFCONF, &ifc);
336     if (i < 0)
337         return 0;
338     len = ifc.ifc_len / sizeof(struct ifreq);
339     if (len > NIFS)
340         len = NIFS;
341 #if    defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
342     if (ifc.ifc_len > sizeof(ifs))      /* safety check */
343         ifc.ifc_len = sizeof(ifs);
344     for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
345          cp < cplim;
346 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
347          cp += _SIZEOF_ADDR_IFREQ(*ifr)
348 #else
349 #ifdef AFS_AIX51_ENV
350          cp = cpnext
351 #else
352          cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))
353 #endif
354 #endif
355         ) 
356 #else
357     for (i = 0; i < len; ++i) 
358 #endif
359     {
360 #if    defined(AFS_AIX41_ENV) || defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
361         ifr = (struct ifreq *)cp;
362 #else
363         ifr = &ifs[i];
364 #endif
365         a = (struct sockaddr_in *)&ifr->ifr_addr;
366 #ifdef AFS_AIX51_ENV
367         cpnext = cp + sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a));
368 #endif
369         if (a->sin_family != AF_INET)
370             continue;
371         if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
372             perror("SIOCGIFFLAGS");
373             continue;           /* ignore this address */
374         }
375         if (a->sin_addr.s_addr != 0) {
376             if (!loopbacks) {
377                 if (a->sin_addr.s_addr == htonl(0x7f000001)) 
378                     continue;   /* skip loopback address as well. */
379             } else {
380                 if (ifr->ifr_flags & IFF_LOOPBACK) 
381                     continue;   /* skip aliased loopbacks as well. */
382             }
383             if (count >= maxSize)       /* no more space */
384                 dpf(("Too many interfaces..ignoring 0x%x\n",
385                        a->sin_addr.s_addr));
386             else
387                 buffer[count++] = a->sin_addr.s_addr;
388         }
389     }
390     close(s);
391     return count;
392 }
393
394 int
395 rx_getAllAddr(afs_int32 buffer[], int maxSize)
396 {
397     return rx_getAllAddr_internal(buffer, maxSize, 0);
398 }
399
400 /* this function returns the total number of interface addresses
401  * the buffer has to be passed in by the caller. It also returns
402  * the interface mask. If AFS_USERSPACE_IP_ADDR is defined, it
403  * gets the mask which is then passed into the kernel and is used
404  * by afsi_SetServerIPRank().
405  */
406 int
407 rxi_getAllAddrMaskMtu(afs_int32 addrBuffer[], afs_int32 maskBuffer[],
408                       afs_int32 mtuBuffer[], int maxSize)
409 {
410     int s;
411     int i, len, count = 0;
412     struct ifconf ifc;
413     struct ifreq ifs[NIFS], *ifr;
414     struct sockaddr_in *a;
415 #if     defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
416     char *cp, *cplim;           /* used only for AIX 41 */
417 #endif
418
419 #if !defined(AFS_USERSPACE_IP_ADDR)
420     count = rx_getAllAddr_internal(addrBuffer, 1024, 1);
421     for (i = 0; i < count; i++) {
422         maskBuffer[i] = htonl(0xffffffff);
423         mtuBuffer[i] = htonl(1500);
424     }
425     return count;
426 #else /* AFS_USERSPACE_IP_ADDR */
427     s = socket(AF_INET, SOCK_DGRAM, 0);
428     if (s < 0)
429         return 0;
430
431     ifc.ifc_len = sizeof(ifs);
432     ifc.ifc_buf = (caddr_t) ifs;
433     i = ioctl(s, SIOCGIFCONF, &ifc);
434     if (i < 0) {
435         close(s);
436         return 0;
437     }
438     len = ifc.ifc_len / sizeof(struct ifreq);
439     if (len > NIFS)
440         len = NIFS;
441
442 #if     defined(AFS_AIX41_ENV) || defined(AFS_USR_AIX_ENV)
443     if (ifc.ifc_len > sizeof(ifs))      /* safety check */
444         ifc.ifc_len = sizeof(ifs);
445     for (cp = (char *)ifc.ifc_buf, cplim = ifc.ifc_buf + ifc.ifc_len;
446          cp < cplim;
447          cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
448         ifr = (struct ifreq *)cp;
449 #else
450     for (i = 0; i < len; ++i) {
451         ifr = &ifs[i];
452 #endif
453         a = (struct sockaddr_in *)&ifr->ifr_addr;
454         if (a->sin_addr.s_addr != 0 && a->sin_family == AF_INET) {
455
456             if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
457                 perror("SIOCGIFFLAGS");
458                 continue;       /* ignore this address */
459             }
460
461             if (a->sin_addr.s_addr == htonl(0x7f000001) )
462                 continue;   /* skip loopback address as well. */
463
464             if (count >= maxSize) {     /* no more space */
465                 dpf("Too many interfaces..ignoring 0x%x\n",
466                        a->sin_addr.s_addr));
467                 continue;
468             }
469
470             addrBuffer[count] = a->sin_addr.s_addr;
471
472             if (ioctl(s, SIOCGIFNETMASK, (caddr_t) ifr) < 0) {
473                 perror("SIOCGIFNETMASK");
474                 maskBuffer[count] = htonl(0xffffffff);
475             } else {
476                 maskBuffer[count] = (((struct sockaddr_in *)
477                                       (&ifr->ifr_addr))->sin_addr).s_addr;
478             }
479
480             mtuBuffer[count] = htonl(1500);
481 #ifdef SIOCGIFMTU
482             if (ioctl(s, SIOCGIFMTU, (caddr_t) ifr) < 0) {
483                 perror("SIOCGIFMTU");
484             } else {
485                 mtuBuffer[count] = htonl(ifr->ifr_metric);
486             }
487 #endif /* SIOCGIFMTU */
488 #ifdef SIOCRIPMTU
489             if (ioctl(s, SIOCRIPMTU, (caddr_t) ifr) < 0) {
490                 perror("SIOCRIPMTU");
491             } else {
492                 mtuBuffer[count] = htonl(ifr->ifr_metric);
493             }
494 #endif /* SIOCRIPMTU */
495
496             count++;
497         }
498     }
499     close(s);
500     return count;
501 #endif /* AFS_USERSPACE_IP_ADDR */
502 }
503 #endif
504
505 #endif /* ! AFS_NT40_ENV */
506 #endif /* !KERNEL || UKERNEL */
507
508 #endif /* !AFS_DJGPP_ENV */