2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * Copyright (c) 1999-2000 Adrian Sun.
4 * All Rights Reserved. See COPYRIGHT.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/ioctl.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
26 #include <net/route.h>
32 #include <sys/sockio.h>
35 #include <atalk/util.h>
37 /* allocation size for interface list. */
40 /* we leave all of the ioctl's to the application */
41 static int addname(char **list, int *i, int *length, const char *name)
44 /* if we've run out of room, allocate some more. just return
45 * the present list if we can't. */
47 if ((list[*i] = strdup(name)) == NULL)
51 list[*i] = NULL; /* zero out the next entry */
56 static int getifaces(const int sockfd, char ***list, int *length)
58 #ifdef HAVE_IFNAMEINDEX
59 struct if_nameindex *ifstart, *ifs;
63 ifs = ifstart = if_nameindex();
65 new = (char **) malloc((sizeof(ifs)/sizeof(struct if_nameindex) + 1) * sizeof(char *));
66 while (ifs && ifs->if_name) {
67 /* just bail if there's a problem */
68 if (addname(new, &i, length, ifs->if_name) < 0)
73 if_freenameindex(ifstart);
79 struct ifreq ifrs[ 64 ], *ifr, *nextifr;
86 memset( &ifc, 0, sizeof( struct ifconf ));
87 ifc.ifc_len = sizeof( ifrs );
88 memset( ifrs, 0, sizeof( ifrs ));
89 ifc.ifc_buf = (caddr_t)ifrs;
90 if ( ioctl( sockfd, SIOCGIFCONF, &ifc ) < 0 ) {
94 new = (char **) malloc((ifc.ifc_len/sizeof(struct ifreq) + 1) * sizeof(char *));
95 for ( ifr = ifc.ifc_req; ifc.ifc_len >= sizeof( struct ifreq );
96 ifc.ifc_len -= ifrsize, ifr = nextifr ) {
98 ifrsize = sizeof(ifr->ifr_name) +
99 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
100 ? ifr->ifr_addr.sa_len : sizeof(struct sockaddr));
102 ifrsize = sizeof( struct ifreq );
104 nextifr = (struct ifreq *)((caddr_t)ifr + ifrsize );
106 /* just bail if there's a problem */
107 if (addname(new, &i, length, ifr->ifr_name) < 0)
117 * Get interfaces from the kernel. we keep an extra null entry to signify
118 * the end of the interface list.
120 char **getifacelist()
125 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
128 if ((i = getifaces(fd, &list, &length)) == 0) {
139 /* go through and free the interface list */
140 void freeifacelist(char **ifacelist)
142 char *value, **list = ifacelist;
147 while ((value = *list++)) {