2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * Copyright (c) 1999-2000 Adrian Sun.
4 * All Rights Reserved. See COPYRIGHT.
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
18 #include <netinet/in.h>
19 #include <netinet/tcp.h>
24 #include <sys/sockio.h>
27 #include <atalk/util.h>
29 /* allocation size for interface list. */
32 /* we leave all of the ioctl's to the application */
33 static int addname(char **list, int *i, int *length, const char *name)
36 /* if we've run out of room, allocate some more. just return
37 * the present list if we can't. */
39 char **new = realloc(list, sizeof(char **)*(*length + IFACE_NUM));
41 if (!new) /* just break if we can't allocate anything */
46 if ((list[*i] = strdup(name)) == NULL)
50 list[*i] = NULL; /* zero out the next entry */
55 static int getifaces(const int sockfd, char **list, int *length)
57 #ifdef HAVE_IFNAMEINDEX
58 struct if_nameindex *ifstart, *ifs;
61 if (!list || *length < 1)
64 ifs = ifstart = if_nameindex();
65 while (ifs && ifs->if_name) {
66 /* just bail if there's a problem */
67 if (addname(list, &i, length, ifs->if_name) < 0)
72 if_freenameindex(ifstart);
77 struct ifreq ifrs[ 64 ], *ifr, *nextifr;
80 if (!list || *length < 1)
83 memset( &ifc, 0, sizeof( struct ifconf ));
84 ifc.ifc_len = sizeof( ifrs );
85 memset( ifrs, 0, sizeof( ifrs ));
86 ifc.ifc_buf = (caddr_t)ifrs;
87 if ( ioctl( sockfd, SIOCGIFCONF, &ifc ) < 0 ) {
91 for ( ifr = ifc.ifc_req; ifc.ifc_len >= sizeof( struct ifreq );
92 ifc.ifc_len -= ifrsize, ifr = nextifr ) {
94 ifrsize = sizeof(ifr->ifr_name) +
95 (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)
96 ? ifr->ifr_addr.sa_len : sizeof(struct sockaddr));
98 ifrsize = sizeof( struct ifreq );
100 nextifr = (struct ifreq *)((caddr_t)ifr + ifrsize );
102 /* just bail if there's a problem */
103 if (addname(list, &i, length, ifr->ifr_name) < 0)
112 * Get interfaces from the kernel. we keep an extra null entry to signify
113 * the end of the interface list.
115 char **getifacelist()
117 char **list = (char **) malloc(sizeof(char **)*(IFACE_NUM + 1));
119 int length = IFACE_NUM, i, fd;
124 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
127 if ((i = getifaces(fd, list, &length)) == 0) {
135 (new = (char **) realloc(list, sizeof(char **)*(i + 1))))
142 /* go through and free the interface list */
143 void freeifacelist(char **ifacelist)
145 char *value, **list = ifacelist;
150 while (value = *list++) {