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 */
54 if ((list[*i] = strdup(name)) == NULL)
58 list[*i] = NULL; /* zero out the next entry */
63 static int getifaces(const int sockfd, char **list, int *length)
65 #ifdef HAVE_IFNAMEINDEX
66 struct if_nameindex *ifstart, *ifs;
69 if (!list || *length < 1)
72 ifs = ifstart = if_nameindex();
73 while (ifs && ifs->if_name) {
74 /* just bail if there's a problem */
75 if (addname(list, &i, length, ifs->if_name) < 0)
80 if_freenameindex(ifstart);
85 struct ifreq ifrs[ 64 ], *ifr, *nextifr;
88 if (!list || *length < 1)
91 memset( &ifc, 0, sizeof( struct ifconf ));
92 ifc.ifc_len = sizeof( ifrs );
93 memset( ifrs, 0, sizeof( ifrs ));
94 ifc.ifc_buf = (caddr_t)ifrs;
95 if ( ioctl( sockfd, SIOCGIFCONF, &ifc ) < 0 ) {
99 for ( ifr = ifc.ifc_req; ifc.ifc_len >= sizeof( struct ifreq );
100 ifc.ifc_len -= ifrsize, ifr = nextifr ) {
102 ifrsize = sizeof(ifr->ifr_name) +
103 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
104 ? ifr->ifr_addr.sa_len : sizeof(struct sockaddr));
106 ifrsize = sizeof( struct ifreq );
108 nextifr = (struct ifreq *)((caddr_t)ifr + ifrsize );
110 /* just bail if there's a problem */
111 if (addname(list, &i, length, ifr->ifr_name) < 0)
120 * Get interfaces from the kernel. we keep an extra null entry to signify
121 * the end of the interface list.
123 char **getifacelist()
125 char **list = (char **) malloc(sizeof(char **)*(IFACE_NUM + 1));
127 int length = IFACE_NUM, i, fd;
132 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
135 if ((i = getifaces(fd, list, &length)) == 0) {
143 (new = (char **) realloc(list, sizeof(char **)*(i + 1))))
150 /* go through and free the interface list */
151 void freeifacelist(char **ifacelist)
153 char *value, **list = ifacelist;
158 while ((value = *list++)) {