X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Futil%2Fgetiface.c;h=bf79fff7f49e62f63983583aeb0b2b7cb4934d0c;hb=5eb3b5ac51c8221009041928a5a08c101d2be743;hp=af175d99ace99292d7e5bd862662552e05dc984d;hpb=cb22616b6b035325d3f25eb910787da358e06b29;p=netatalk.git diff --git a/libatalk/util/getiface.c b/libatalk/util/getiface.c index af175d99..bf79fff7 100644 --- a/libatalk/util/getiface.c +++ b/libatalk/util/getiface.c @@ -11,16 +11,16 @@ #include #include #include - -#ifdef HAVE_STDINT_H #include -#endif - #include #include #include #include #include +#ifdef TRU64 +#include +#include +#endif /* TRU64 */ #include #include @@ -34,18 +34,11 @@ #define IFACE_NUM 5 /* we leave all of the ioctl's to the application */ -static int addname(char **list, int *i, int *length, const char *name) +static int addname(char **list, int *i, const char *name) { /* if we've run out of room, allocate some more. just return * the present list if we can't. */ - if (*i >= *length) { - char **new = realloc(list, sizeof(char **)*(*length + IFACE_NUM)); - - if (!new) /* just break if we can't allocate anything */ - return -1; - *length += IFACE_NUM; - } if ((list[*i] = strdup(name)) == NULL) return -1; @@ -56,32 +49,34 @@ static int addname(char **list, int *i, int *length, const char *name) } -static int getifaces(const int sockfd, char **list, int *length) +static int getifaces(const int sockfd, char ***list) { #ifdef HAVE_IFNAMEINDEX struct if_nameindex *ifstart, *ifs; int i = 0; + char **new; - if (!list || *length < 1) - return 0; - ifs = ifstart = if_nameindex(); + + new = (char **) malloc((sizeof(ifs)/sizeof(struct if_nameindex) + 1) * sizeof(char *)); while (ifs && ifs->if_name) { /* just bail if there's a problem */ - if (addname(list, &i, length, ifs->if_name) < 0) + if (addname(new, &i, ifs->if_name) < 0) break; ifs++; } if_freenameindex(ifstart); + *list = new; return i; #else struct ifconf ifc; struct ifreq ifrs[ 64 ], *ifr, *nextifr; int ifrsize, i = 0; + char **new; - if (!list || *length < 1) + if (!list) return 0; memset( &ifc, 0, sizeof( struct ifconf )); @@ -92,21 +87,23 @@ static int getifaces(const int sockfd, char **list, int *length) return 0; } - for ( ifr = ifc.ifc_req; ifc.ifc_len >= sizeof( struct ifreq ); + new = (char **) malloc((ifc.ifc_len/sizeof(struct ifreq) + 1) * sizeof(char *)); + for ( ifr = ifc.ifc_req; ifc.ifc_len >= (int) sizeof( struct ifreq ); ifc.ifc_len -= ifrsize, ifr = nextifr ) { #ifdef BSD4_4 ifrsize = sizeof(ifr->ifr_name) + (ifr->ifr_addr.sa_len > sizeof(struct sockaddr) ? ifr->ifr_addr.sa_len : sizeof(struct sockaddr)); -#else BSD4_4 +#else /* !BSD4_4 */ ifrsize = sizeof( struct ifreq ); -#endif BSD4_4 +#endif /* BSD4_4 */ nextifr = (struct ifreq *)((caddr_t)ifr + ifrsize ); /* just bail if there's a problem */ - if (addname(list, &i, length, ifr->ifr_name) < 0) + if (addname(new, &i, ifr->ifr_name) < 0) break; } + *list = new; return i; #endif } @@ -116,29 +113,21 @@ static int getifaces(const int sockfd, char **list, int *length) * Get interfaces from the kernel. we keep an extra null entry to signify * the end of the interface list. */ -char **getifacelist() +char **getifacelist(void) { - char **list = (char **) malloc(sizeof(char **)*(IFACE_NUM + 1)); - char **new; - int length = IFACE_NUM, i, fd; + char **list = NULL; /* FIXME */ + int i, fd; - if (!list) - return NULL; - if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) return NULL; - if ((i = getifaces(fd, list, &length)) == 0) { + if ((i = getifaces(fd, &list)) == 0) { free(list); close(fd); return NULL; } close(fd); - if ((i < length) && - (new = (char **) realloc(list, sizeof(char **)*(i + 1)))) - return new; - return list; }