summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/if_indextoname.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/if_indextoname.c')
-rw-r--r--src/lib/libc/net/if_indextoname.c60
1 files changed, 10 insertions, 50 deletions
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c
index ff7fcada91..1301224444 100644
--- a/src/lib/libc/net/if_indextoname.c
+++ b/src/lib/libc/net/if_indextoname.c
@@ -58,33 +58,6 @@
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE. 59 * SUCH DAMAGE.
60 * 60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * 2. Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * 3. All advertising materials mentioning features or use of this software
70 * must display the following acknowledgement:
71 * This product includes software developed by Craig Metz and
72 * by other contributors.
73 * 4. Neither the name of the author nor the names of contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 */ 61 */
89 62
90#include <sys/types.h> 63#include <sys/types.h>
@@ -92,9 +65,7 @@
92#include <sys/socket.h> 65#include <sys/socket.h>
93#include <sys/ioctl.h> 66#include <sys/ioctl.h>
94#include <net/if.h> 67#include <net/if.h>
95#ifdef AF_LINK
96#include <net/if_dl.h> 68#include <net/if_dl.h>
97#endif /* AF_LINK */
98#include <errno.h> 69#include <errno.h>
99 70
100static char __name[IFNAMSIZ + 1]; 71static char __name[IFNAMSIZ + 1];
@@ -102,16 +73,12 @@ static char __name[IFNAMSIZ + 1];
102char *if_indextoname(unsigned int index, char *name) 73char *if_indextoname(unsigned int index, char *name)
103{ 74{
104 int i, fd; 75 int i, fd;
105#ifdef SIOCGIFNAME
106 struct ifreq ifreq;
107#else /* SIOCGIFNAME */
108 struct ifconf ifconf; 76 struct ifconf ifconf;
109 void *p; 77 void *p;
110 int len; 78 int len;
111 char lastname[IFNAMSIZ + 1]; 79 char lastname[IFNAMSIZ + 1];
112 char iname[IFNAMSIZ + 1]; 80 char iname[IFNAMSIZ + 1];
113 char *retname = NULL; 81 char *retname = NULL;
114#endif /* SIOCGIFNAME */
115 82
116 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) 83 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
117 return 0; 84 return 0;
@@ -119,20 +86,16 @@ char *if_indextoname(unsigned int index, char *name)
119 if (!name) 86 if (!name)
120 name = __name; 87 name = __name;
121 88
122#ifdef SIOCGIFNAME 89 ifconf.ifc_len = 0;
123 ifreq.ifr_ifindex = index; 90 ifconf.ifc_buf = 0;
124 i = ioctl(fd, SIOCGIFNAME, &ifreq); 91 if (ioctl(fd, SIOCGIFCONF, (void *)ifconf))
125 close(fd); 92 goto ret;
126 if (i) 93 if (ifconf->ifc_len < IFNAMSIZ)
127 return NULL; 94 goto ret;
128 95 if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len)))
129 strcpy(name, ifreq.ifr_name); 96 goto ret;
130 return name; 97 if (ioctl(fd, SIOCGIFCONF, (void *)ifconf))
131#else /* SIOCGIFNAME */ 98 goto ret;
132 if (__siocgifconf(fd, &ifconf)) {
133 close(fd);
134 return NULL;
135 };
136 99
137 i = 0; 100 i = 0;
138 p = ifconf.ifc_buf; 101 p = ifconf.ifc_buf;
@@ -153,13 +116,11 @@ char *if_indextoname(unsigned int index, char *name)
153 len -= IFNAMSIZ; 116 len -= IFNAMSIZ;
154 p += IFNAMSIZ; 117 p += IFNAMSIZ;
155 118
156#ifdef AF_LINK
157 if (((struct sockaddr *)p)->sa_family == AF_LINK) 119 if (((struct sockaddr *)p)->sa_family == AF_LINK)
158 if (((struct sockaddr_dl *)p)->sdl_index == index) { 120 if (((struct sockaddr_dl *)p)->sdl_index == index) {
159 strcpy(retname = name, lastname); 121 strcpy(retname = name, lastname);
160 goto ret; 122 goto ret;
161 }; 123 };
162#endif /* AF_LINK */
163 124
164 if (len < SA_LEN((struct sockaddr *)p)) 125 if (len < SA_LEN((struct sockaddr *)p))
165 goto ret; 126 goto ret;
@@ -177,5 +138,4 @@ ret:
177 close(fd); 138 close(fd);
178 free(ifconf.ifc_buf); 139 free(ifconf.ifc_buf);
179 return retname; 140 return retname;
180#endif /* SIOCGIFNAME */
181}; 141};