summaryrefslogtreecommitdiff
path: root/lib/libc/net/if_indextoname.c
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>1999-07-01 15:36:00 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>1999-07-01 15:36:00 +0000
commitfe702e2a44ff3c117488259c3dc6a73adc327b34 (patch)
tree2daea701fb84332037f695ece2b38817d6f7aa0b /lib/libc/net/if_indextoname.c
parent0116d8322de2d4583a64e0d6bdce88ad7d567074 (diff)
indent and audit
Diffstat (limited to 'lib/libc/net/if_indextoname.c')
-rw-r--r--lib/libc/net/if_indextoname.c154
1 files changed, 66 insertions, 88 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);
+}