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 char **new = realloc(list, sizeof(char **)*(*length + IFACE_NUM));
49 if (!new) /* just break if we can't allocate anything */
52 /* copy the old list */
53 memcpy(new, list, *length);
59 if ((list[*i] = strdup(name)) == NULL)
63 list[*i] = NULL; /* zero out the next entry */
68 static int getifaces(const int sockfd, char **list, int *length)
70 #ifdef HAVE_IFNAMEINDEX
71 struct if_nameindex *ifstart, *ifs;
74 if (!list || *length < 1)
77 ifs = ifstart = if_nameindex();
78 while (ifs && ifs->if_name) {
79 /* just bail if there's a problem */
80 if (addname(list, &i, length, ifs->if_name) < 0)
85 if_freenameindex(ifstart);
90 struct ifreq ifrs[ 64 ], *ifr, *nextifr;
93 if (!list || *length < 1)
96 memset( &ifc, 0, sizeof( struct ifconf ));
97 ifc.ifc_len = sizeof( ifrs );
98 memset( ifrs, 0, sizeof( ifrs ));
99 ifc.ifc_buf = (caddr_t)ifrs;
100 if ( ioctl( sockfd, SIOCGIFCONF, &ifc ) < 0 ) {
104 for ( ifr = ifc.ifc_req; ifc.ifc_len >= sizeof( struct ifreq );
105 ifc.ifc_len -= ifrsize, ifr = nextifr ) {
107 ifrsize = sizeof(ifr->ifr_name) +
108 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
109 ? ifr->ifr_addr.sa_len : sizeof(struct sockaddr));
111 ifrsize = sizeof( struct ifreq );
113 nextifr = (struct ifreq *)((caddr_t)ifr + ifrsize );
115 /* just bail if there's a problem */
116 if (addname(list, &i, length, ifr->ifr_name) < 0)
125 * Get interfaces from the kernel. we keep an extra null entry to signify
126 * the end of the interface list.
128 char **getifacelist()
130 char **list = (char **) malloc(sizeof(char **)*(IFACE_NUM + 1));
132 int length = IFACE_NUM, i, fd;
137 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
140 if ((i = getifaces(fd, list, &length)) == 0) {
148 (new = (char **) realloc(list, sizeof(char **)*(i + 1))))
155 /* go through and free the interface list */
156 void freeifacelist(char **ifacelist)
158 char *value, **list = ifacelist;
163 while ((value = *list++)) {