diff options
Diffstat (limited to 'src/lib/libc/net/if_indextoname.c')
-rw-r--r-- | src/lib/libc/net/if_indextoname.c | 154 |
1 files changed, 66 insertions, 88 deletions
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c index b3c89fb9a8..cca862de61 100644 --- a/src/lib/libc/net/if_indextoname.c +++ b/src/lib/libc/net/if_indextoname.c | |||
@@ -1,34 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * %%% copyright-cmetz-98-bsd | 2 | * %%% copyright-cmetz-98-bsd |
3 | * Copyright (c) 1998-1999, Craig Metz, All rights reserved. | 3 | * Copyright (c) 1998-1999, Craig Metz, All rights reserved. |
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. All advertising materials mentioning features or use of this software | ||
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by Craig Metz and | ||
16 | * by other contributors. | ||
17 | * 4. Neither the name of the author nor the names of contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * | 4 | * |
33 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
34 | * modification, are permitted provided that the following conditions | 6 | * modification, are permitted provided that the following conditions |
@@ -45,7 +17,7 @@ | |||
45 | * 4. Neither the name of the author nor the names of contributors | 17 | * 4. Neither the name of the author nor the names of contributors |
46 | * may be used to endorse or promote products derived from this software | 18 | * may be used to endorse or promote products derived from this software |
47 | * without specific prior written permission. | 19 | * without specific prior written permission. |
48 | * | 20 | * |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -57,7 +29,6 @@ | |||
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
59 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
60 | * | ||
61 | */ | 32 | */ |
62 | 33 | ||
63 | #include <sys/types.h> | 34 | #include <sys/types.h> |
@@ -67,75 +38,82 @@ | |||
67 | #include <net/if.h> | 38 | #include <net/if.h> |
68 | #include <net/if_dl.h> | 39 | #include <net/if_dl.h> |
69 | #include <errno.h> | 40 | #include <errno.h> |
41 | #include <string.h> | ||
70 | 42 | ||
71 | static char __name[IFNAMSIZ + 1]; | 43 | static char __name[IFNAMSIZ]; |
72 | 44 | ||
73 | char *if_indextoname(unsigned int index, char *name) | 45 | char * |
46 | if_indextoname(unsigned int index, char *name) | ||
74 | { | 47 | { |
75 | int i, fd; | 48 | int i, fd, len; |
76 | struct ifconf ifconf; | 49 | struct ifconf ifconf; |
77 | void *p; | 50 | char lastname[IFNAMSIZ], iname[IFNAMSIZ], *retname = NULL; |
78 | int len; | 51 | struct sockaddr *sa; |
79 | char lastname[IFNAMSIZ + 1]; | 52 | void *p; |
80 | char iname[IFNAMSIZ + 1]; | ||
81 | char *retname = NULL; | ||
82 | 53 | ||
83 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | 54 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
84 | return 0; | 55 | return 0; |
85 | 56 | ||
86 | if (!name) | 57 | if (!name) |
87 | name = __name; | 58 | name = __name; |
88 | 59 | ||
89 | ifconf.ifc_len = 0; | 60 | ifconf.ifc_len = 0; |
90 | ifconf.ifc_buf = 0; | 61 | ifconf.ifc_buf = 0; |
91 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) | 62 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) |
92 | goto ret; | 63 | goto ret; |
93 | if (ifconf.ifc_len < IFNAMSIZ) | 64 | if (ifconf.ifc_len < IFNAMSIZ) |
94 | goto ret; | 65 | goto ret; |
95 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) | 66 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) |
96 | goto ret; | 67 | goto ret; |
97 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) | 68 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) |
98 | goto ret; | 69 | goto ret; |
99 | 70 | ||
100 | i = 0; | 71 | i = 0; |
101 | p = ifconf.ifc_buf; | 72 | p = ifconf.ifc_buf; |
102 | len = ifconf.ifc_len; | 73 | len = ifconf.ifc_len; |
103 | lastname[0] = 0; | 74 | lastname[0] = 0; |
104 | lastname[IFNAMSIZ] = 0; | 75 | lastname[sizeof(lastname)-1] = 0; |
105 | iname[0] = 0; | 76 | iname[0] = 0; |
106 | 77 | ||
107 | while(len > 0) { | 78 | while (len > 0) { |
108 | if (len < (IFNAMSIZ + sizeof(struct sockaddr))) | 79 | if (len < (IFNAMSIZ + sizeof(struct sockaddr))) |
109 | goto ret; | 80 | goto ret; |
110 | if (strncmp(lastname, p, IFNAMSIZ)) { | 81 | if (strncmp(lastname, p, IFNAMSIZ)) { |
111 | if (i == index) | 82 | if (i == index) |
112 | strcpy(iname, lastname); | 83 | memcpy(iname, lastname, sizeof(iname)); |
113 | memcpy(lastname, p, IFNAMSIZ); | 84 | strlcpy(lastname, p, sizeof(lastname)); |
114 | i++; | 85 | i++; |
115 | }; | 86 | } |
116 | len -= IFNAMSIZ; | 87 | len -= IFNAMSIZ; |
117 | p += IFNAMSIZ; | 88 | p += IFNAMSIZ; |
89 | sa = p; | ||
118 | 90 | ||
119 | if (((struct sockaddr *)p)->sa_family == AF_LINK) | 91 | if (sa->sa_family == AF_LINK) { |
120 | if (((struct sockaddr_dl *)p)->sdl_index == index) { | 92 | struct sockaddr_dl *sd = p; |
121 | strcpy(retname = name, lastname); | ||
122 | goto ret; | ||
123 | }; | ||
124 | 93 | ||
125 | if (len < SA_LEN((struct sockaddr *)p)) | 94 | if (sd->sdl_index == index) { |
126 | goto ret; | 95 | strlcpy(name, lastname, sizeof(name)); |
127 | len -= SA_LEN((struct sockaddr *)p); | 96 | retname = name; |
128 | p += SA_LEN((struct sockaddr *)p); | 97 | goto ret; |
129 | }; | 98 | } |
99 | } | ||
130 | 100 | ||
131 | if (i == index) | 101 | if (len < sa->sa_len) |
132 | strcpy(iname, lastname); | 102 | goto ret; |
103 | len -= sa->sa_len; | ||
104 | p += sa->sa_len; | ||
105 | } | ||
133 | 106 | ||
134 | if (iname[0]) | 107 | if (i == index) |
135 | strcpy(retname = name, iname); | 108 | strlcpy(iname, lastname, sizeof(iname)); |
136 | 109 | ||
110 | if (iname[0]) { | ||
111 | strlcpy(name, iname, sizeof(name)); | ||
112 | retname = name; | ||
113 | } | ||
137 | ret: | 114 | ret: |
138 | close(fd); | 115 | close(fd); |
139 | free(ifconf.ifc_buf); | 116 | if (ifconf.ifc_buf) |
140 | return retname; | 117 | free(ifconf.ifc_buf); |
141 | }; | 118 | return (retname); |
119 | } | ||