diff options
Diffstat (limited to 'lib/libc/net')
-rw-r--r-- | lib/libc/net/if_indextoname.c | 154 | ||||
-rw-r--r-- | lib/libc/net/if_nameindex.c | 207 | ||||
-rw-r--r-- | lib/libc/net/if_nametoindex.c | 117 |
3 files changed, 220 insertions, 258 deletions
diff --git a/lib/libc/net/if_indextoname.c b/lib/libc/net/if_indextoname.c index b3c89fb9a8b..cca862de617 100644 --- a/lib/libc/net/if_indextoname.c +++ b/lib/libc/net/if_indextoname.c @@ -1,34 +1,6 @@ /* * %%% copyright-cmetz-98-bsd * Copyright (c) 1998-1999, Craig Metz, All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by Craig Metz and - * by other contributors. - * 4. Neither the name of the author nor the names of contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -45,7 +17,7 @@ * 4. Neither the name of the author nor the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -57,7 +29,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * */ #include <sys/types.h> @@ -67,75 +38,82 @@ #include <net/if.h> #include <net/if_dl.h> #include <errno.h> +#include <string.h> -static char __name[IFNAMSIZ + 1]; +static char __name[IFNAMSIZ]; -char *if_indextoname(unsigned int index, char *name) +char * +if_indextoname(unsigned int index, char *name) { - int i, fd; - struct ifconf ifconf; - void *p; - int len; - char lastname[IFNAMSIZ + 1]; - char iname[IFNAMSIZ + 1]; - char *retname = NULL; + int i, fd, len; + struct ifconf ifconf; + char lastname[IFNAMSIZ], iname[IFNAMSIZ], *retname = NULL; + struct sockaddr *sa; + void *p; - if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) - return 0; + if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) + return 0; - if (!name) - name = __name; + if (!name) + name = __name; - ifconf.ifc_len = 0; - ifconf.ifc_buf = 0; - if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) - goto ret; - if (ifconf.ifc_len < IFNAMSIZ) - goto ret; - if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) - goto ret; - if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) - goto ret; + ifconf.ifc_len = 0; + ifconf.ifc_buf = 0; + if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) + goto ret; + if (ifconf.ifc_len < IFNAMSIZ) + goto ret; + if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) + goto ret; + if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) + goto ret; - i = 0; - p = ifconf.ifc_buf; - len = ifconf.ifc_len; - lastname[0] = 0; - lastname[IFNAMSIZ] = 0; - iname[0] = 0; + i = 0; + p = ifconf.ifc_buf; + len = ifconf.ifc_len; + lastname[0] = 0; + lastname[sizeof(lastname)-1] = 0; + iname[0] = 0; - while(len > 0) { - if (len < (IFNAMSIZ + sizeof(struct sockaddr))) - goto ret; - if (strncmp(lastname, p, IFNAMSIZ)) { - if (i == index) - strcpy(iname, lastname); - memcpy(lastname, p, IFNAMSIZ); - i++; - }; - len -= IFNAMSIZ; - p += IFNAMSIZ; + while (len > 0) { + if (len < (IFNAMSIZ + sizeof(struct sockaddr))) + goto ret; + if (strncmp(lastname, p, IFNAMSIZ)) { + if (i == index) + memcpy(iname, lastname, sizeof(iname)); + strlcpy(lastname, p, sizeof(lastname)); + i++; + } + len -= IFNAMSIZ; + p += IFNAMSIZ; + sa = p; - if (((struct sockaddr *)p)->sa_family == AF_LINK) - if (((struct sockaddr_dl *)p)->sdl_index == index) { - strcpy(retname = name, lastname); - goto ret; - }; + if (sa->sa_family == AF_LINK) { + struct sockaddr_dl *sd = p; - if (len < SA_LEN((struct sockaddr *)p)) - goto ret; - len -= SA_LEN((struct sockaddr *)p); - p += SA_LEN((struct sockaddr *)p); - }; + if (sd->sdl_index == index) { + strlcpy(name, lastname, sizeof(name)); + retname = name; + goto ret; + } + } - if (i == index) - strcpy(iname, lastname); + if (len < sa->sa_len) + goto ret; + len -= sa->sa_len; + p += sa->sa_len; + } - if (iname[0]) - strcpy(retname = name, iname); + if (i == index) + strlcpy(iname, lastname, sizeof(iname)); + if (iname[0]) { + strlcpy(name, iname, sizeof(name)); + retname = name; + } ret: - close(fd); - free(ifconf.ifc_buf); - return retname; -}; + close(fd); + if (ifconf.ifc_buf) + free(ifconf.ifc_buf); + return (retname); +} diff --git a/lib/libc/net/if_nameindex.c b/lib/libc/net/if_nameindex.c index 2acbeeb3f87..8714c1f95a1 100644 --- a/lib/libc/net/if_nameindex.c +++ b/lib/libc/net/if_nameindex.c @@ -1,34 +1,6 @@ /* * %%% copyright-cmetz-98-bsd * Copyright (c) 1998-1999, Craig Metz, All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by Craig Metz and - * by other contributors. - * 4. Neither the name of the author nor the names of contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -45,7 +17,7 @@ * 4. Neither the name of the author nor the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -57,7 +29,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * */ #include <stdlib.h> @@ -67,102 +38,108 @@ #include <net/if.h> #include <net/if_dl.h> #include <errno.h> +#include <string.h> -struct if_nameindex *if_nameindex(void) +struct if_nameindex * +if_nameindex(void) { - int i, j, fd; - struct if_nameindex *nameindex = NULL; - struct ifconf ifconf; - void *p; - int len; - char lastname[IFNAMSIZ + 1]; - - if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) - return NULL; + int i, j, fd, len; + struct if_nameindex *nameindex = NULL; + struct ifconf ifconf; + char lastname[IFNAMSIZ], *c; + struct if_nameindex *n; + struct sockaddr_dl *sd; + struct sockaddr *sa; + void *p; - ifconf.ifc_len = 0; - ifconf.ifc_buf = 0; - if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) - goto ret; - if (ifconf.ifc_len < IFNAMSIZ) - goto ret; - if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) - goto ret; - if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) - goto ret; + if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) + return NULL; - i = sizeof(struct if_nameindex); - j = 0; - p = ifconf.ifc_buf; - len = ifconf.ifc_len; - lastname[0] = 0; - lastname[IFNAMSIZ] = 0; + ifconf.ifc_len = 0; + ifconf.ifc_buf = 0; + if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) + goto ret; + if (ifconf.ifc_len < IFNAMSIZ) + goto ret; + if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) + goto ret; + if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) + goto ret; - while(len > 0) { - if (len < (IFNAMSIZ + sizeof(struct sockaddr))) - goto ret; - if (strncmp(lastname, p, IFNAMSIZ)) { - memcpy(lastname, p, IFNAMSIZ); - i += sizeof(struct if_nameindex); - j += strlen(lastname) + 1; - }; - len -= IFNAMSIZ; - p += IFNAMSIZ; + i = sizeof(struct if_nameindex); + j = 0; + p = ifconf.ifc_buf; + len = ifconf.ifc_len; + lastname[0] = 0; + lastname[sizeof(lastname)-1] = 0; - if (len < SA_LEN((struct sockaddr *)p)) - goto ret; - len -= SA_LEN((struct sockaddr *)p); - p += SA_LEN((struct sockaddr *)p); - }; + while (len > 0) { + if (len < (IFNAMSIZ + sizeof(struct sockaddr))) + goto ret; + if (strncmp(lastname, p, IFNAMSIZ)) { + strlcpy(lastname, p, sizeof(lastname)); + i += sizeof(struct if_nameindex); + j += strlen(lastname) + 1; + } + len -= IFNAMSIZ; + p += IFNAMSIZ; + sa = p; - if (!(nameindex = malloc(i + j))) { - errno = ENOMEM; - goto ret; - }; - memset(nameindex, 0, i + j); + if (len < sa->sa_len) + goto ret; + len -= sa->sa_len; + p += sa->sa_len; + } - { - struct if_nameindex *n; - char *c; + nameindex = malloc(i + j); + if (nameindex == NULL) { + errno = ENOMEM; + goto ret; + } + memset(nameindex, 0, i + j); - n = nameindex; - p = ifconf.ifc_buf; - c = (void *)nameindex + i; - i = 0; - len = ifconf.ifc_len; - lastname[0] = 0; + n = nameindex; + p = ifconf.ifc_buf; + c = (void *) nameindex + i; + i = 0; + len = ifconf.ifc_len; + lastname[0] = 0; - while(len > 0) { - if (len < (IFNAMSIZ + sizeof(struct sockaddr))) - goto ret; - if (strncmp(lastname, p, IFNAMSIZ)) { - if (i) { - if (!n->if_index) - n->if_index = i; - n++; - }; - i++; - memcpy(lastname, p, IFNAMSIZ); - strcpy(n->if_name = c, lastname); - c += strlen(c) + 1; - }; - len -= IFNAMSIZ; - p += IFNAMSIZ; + while (len > 0) { + if (len < IFNAMSIZ + sizeof(struct sockaddr)) + goto ret; + if (strncmp(lastname, p, IFNAMSIZ)) { + if (i) { + if (!n->if_index) + n->if_index = i; + n++; + } + i++; + memcpy(lastname, p, sizeof(lastname)); + strlcpy(c, lastname, sizeof(lastname)); + n->if_name = c; + c += strlen(c) + 1; + } + len -= IFNAMSIZ; + p += IFNAMSIZ; + sa = p; - if (len < SA_LEN((struct sockaddr *)p)) - goto ret; - if (((struct sockaddr *)p)->sa_family == AF_LINK) - n->if_index = ((struct sockaddr_dl *)p)->sdl_index; - len -= SA_LEN((struct sockaddr *)p); - p += SA_LEN((struct sockaddr *)p); - }; + if (len < sa->sa_len) + goto ret; + if (sa->sa_family == AF_LINK) { + struct sockaddr_dl *sd = (struct sockaddr_dl *)sa; + n->if_index = sd->sdl_index; + } + len -= sa->sa_len; + p += sa->sa_len; + } - if (!n->if_index) - n->if_index = i; - }; + if (n->if_index == 0) + n->if_index = i; ret: - close(fd); - free(ifconf.ifc_buf); - return nameindex; -}; + close(fd); + if (ifconf.ifc_buf) + free(ifconf.ifc_buf); + return (nameindex); +} diff --git a/lib/libc/net/if_nametoindex.c b/lib/libc/net/if_nametoindex.c index ad05387e34b..0318f606808 100644 --- a/lib/libc/net/if_nametoindex.c +++ b/lib/libc/net/if_nametoindex.c @@ -1,7 +1,7 @@ /* * %%% copyright-cmetz-98-bsd * Copyright (c) 1998-1999, Craig Metz, All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -17,7 +17,7 @@ * 4. Neither the name of the author nor the names of contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -29,7 +29,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * */ #include <sys/types.h> @@ -38,69 +37,77 @@ #include <sys/ioctl.h> #include <net/if.h> #include <net/if_dl.h> +#include <string.h> #include <errno.h> -unsigned int if_nametoindex(const char *name) +unsigned int +if_nametoindex(const char *name) { - int i, fd; - struct ifconf ifconf; - void *p; - int len; - char lastname[IFNAMSIZ + 1], *thisname; - unsigned int index = 0; + int i, fd, len; + struct ifconf ifconf; + char lastname[IFNAMSIZ], *thisname; + unsigned int index = 0; + struct sockaddr *sa; + void *p; + + if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) + return 0; - if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) - return 0; + ifconf.ifc_len = 0; + ifconf.ifc_buf = 0; + if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) + goto ret; + if (ifconf.ifc_len < IFNAMSIZ) + goto ret; + if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) + goto ret; + if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) + goto ret; - ifconf.ifc_len = 0; - ifconf.ifc_buf = 0; - if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) - goto ret; - if (ifconf.ifc_len < IFNAMSIZ) - goto ret; - if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) - goto ret; - if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) - goto ret; + i = 0; + p = ifconf.ifc_buf; + len = ifconf.ifc_len; + lastname[0] = 0; + lastname[sizeof(lastname)-1] = 0; - i = 0; - p = ifconf.ifc_buf; - len = ifconf.ifc_len; - lastname[0] = 0; - lastname[IFNAMSIZ] = 0; + while (len > 0) { + if (len < IFNAMSIZ + sizeof(struct sockaddr)) + goto ret; - while(len > 0) { - if (len < (IFNAMSIZ + sizeof(struct sockaddr))) - goto ret; + thisname = p; + if (strncmp(lastname, p, IFNAMSIZ)) { + if (!strcmp(lastname, name)) { + index = i; + goto ret; + } + memcpy(lastname, thisname, IFNAMSIZ); + i++; + } - if (strncmp(lastname, thisname = p, IFNAMSIZ)) { - if (!strcmp(lastname, name)) { - index = i; - goto ret; - }; - memcpy(lastname, thisname, IFNAMSIZ); - i++; - }; + len -= IFNAMSIZ; + p += IFNAMSIZ; + sa = (struct sockaddr *)p; - len -= IFNAMSIZ; - p += IFNAMSIZ; + if (!strncmp(thisname, name, IFNAMSIZ) && + sa->sa_family == AF_LINK) { + struct sockaddr_dl *sd = p; - if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) { - index = ((struct sockaddr_dl *)p)->sdl_index; - goto ret; - }; + index = sd->sdl_index; + goto ret; + } - if (len < SA_LEN((struct sockaddr *)p)) - goto ret; - len -= SA_LEN((struct sockaddr *)p); - p += SA_LEN((struct sockaddr *)p); - }; + if (len < sa->sa_len) + goto ret; + len -= sa->sa_len; + p += sa->sa_len; + } - if (!strcmp(lastname, name)) - index = i; + if (!strcmp(lastname, name)) + index = i; ret: - close(fd); - free(ifconf.ifc_buf); - return index; -}; + close(fd); + if (ifconf.ifc_buf) + free(ifconf.ifc_buf); + return index; +} |