diff options
Diffstat (limited to 'src/lib/libc/net/if_nametoindex.c')
| -rw-r--r-- | src/lib/libc/net/if_nametoindex.c | 87 |
1 files changed, 10 insertions, 77 deletions
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c index 8ec2b233d1..2b9810d314 100644 --- a/src/lib/libc/net/if_nametoindex.c +++ b/src/lib/libc/net/if_nametoindex.c | |||
| @@ -30,61 +30,6 @@ | |||
| 30 | * 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 |
| 31 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
| 32 | * | 32 | * |
| 33 | * Redistribution and use in source and binary forms, with or without | ||
| 34 | * modification, are permitted provided that the following conditions | ||
| 35 | * are met: | ||
| 36 | * 1. Redistributions of source code must retain the above copyright | ||
| 37 | * notice, this list of conditions and the following disclaimer. | ||
| 38 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 39 | * notice, this list of conditions and the following disclaimer in the | ||
| 40 | * documentation and/or other materials provided with the distribution. | ||
| 41 | * 3. All advertising materials mentioning features or use of this software | ||
| 42 | * must display the following acknowledgement: | ||
| 43 | * This product includes software developed by Craig Metz and | ||
| 44 | * by other contributors. | ||
| 45 | * 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 | ||
| 47 | * without specific prior written permission. | ||
| 48 | * | ||
| 49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 57 | * 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 | ||
| 59 | * SUCH DAMAGE. | ||
| 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 | */ | 33 | */ |
| 89 | 34 | ||
| 90 | #include <sys/types.h> | 35 | #include <sys/types.h> |
| @@ -92,40 +37,31 @@ | |||
| 92 | #include <sys/socket.h> | 37 | #include <sys/socket.h> |
| 93 | #include <sys/ioctl.h> | 38 | #include <sys/ioctl.h> |
| 94 | #include <net/if.h> | 39 | #include <net/if.h> |
| 95 | #ifdef AF_LINK | ||
| 96 | #include <net/if_dl.h> | 40 | #include <net/if_dl.h> |
| 97 | #endif /* AF_LINK */ | ||
| 98 | #include <errno.h> | 41 | #include <errno.h> |
| 99 | 42 | ||
| 100 | unsigned int if_nametoindex(const char *name) | 43 | unsigned int if_nametoindex(const char *name) |
| 101 | { | 44 | { |
| 102 | int i, fd; | 45 | int i, fd; |
| 103 | #ifdef SIOCGIFINDEX | ||
| 104 | struct ifreq ifreq; | ||
| 105 | #else /* SIOCGIFINDEX */ | ||
| 106 | struct ifconf ifconf; | 46 | struct ifconf ifconf; |
| 107 | void *p; | 47 | void *p; |
| 108 | int len; | 48 | int len; |
| 109 | char lastname[IFNAMSIZ + 1], *thisname; | 49 | char lastname[IFNAMSIZ + 1], *thisname; |
| 110 | unsigned int index = 0; | 50 | unsigned int index = 0; |
| 111 | #endif /* SIOCGIFINDEX */ | ||
| 112 | 51 | ||
| 113 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | 52 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
| 114 | return 0; | 53 | return 0; |
| 115 | 54 | ||
| 116 | #ifdef SIOCGIFINDEX | 55 | ifconf.ifc_len = 0; |
| 117 | strcpy(ifreq.ifr_name, name); | 56 | ifconf.ifc_buf = 0; |
| 118 | i = ioctl(fd, SIOCGIFINDEX, &ifreq); | 57 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) |
| 119 | close(fd); | 58 | goto ret; |
| 120 | if (i) | 59 | if (ifconf->ifc_len < IFNAMSIZ) |
| 121 | return 0; | 60 | goto ret; |
| 122 | 61 | if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len))) | |
| 123 | return ifreq.ifr_ifindex; | 62 | goto ret; |
| 124 | #else /* SIOCGIFINDEX */ | 63 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) |
| 125 | if (__siocgifconf(fd, &ifconf)) { | 64 | goto ret; |
| 126 | close(fd); | ||
| 127 | return 0; | ||
| 128 | }; | ||
| 129 | 65 | ||
| 130 | i = 0; | 66 | i = 0; |
| 131 | p = ifconf.ifc_buf; | 67 | p = ifconf.ifc_buf; |
| @@ -149,12 +85,10 @@ unsigned int if_nametoindex(const char *name) | |||
| 149 | len -= IFNAMSIZ; | 85 | len -= IFNAMSIZ; |
| 150 | p += IFNAMSIZ; | 86 | p += IFNAMSIZ; |
| 151 | 87 | ||
| 152 | #ifdef AF_LINK | ||
| 153 | if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) { | 88 | if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) { |
| 154 | index = ((struct sockaddr_dl *)p)->sdl_index; | 89 | index = ((struct sockaddr_dl *)p)->sdl_index; |
| 155 | goto ret; | 90 | goto ret; |
| 156 | }; | 91 | }; |
| 157 | #endif /* AF_LINK */ | ||
| 158 | 92 | ||
| 159 | if (len < SA_LEN((struct sockaddr *)p)) | 93 | if (len < SA_LEN((struct sockaddr *)p)) |
| 160 | goto ret; | 94 | goto ret; |
| @@ -169,5 +103,4 @@ ret: | |||
| 169 | close(fd); | 103 | close(fd); |
| 170 | free(ifconf.ifc_buf); | 104 | free(ifconf.ifc_buf); |
| 171 | return index; | 105 | return index; |
| 172 | #endif /* SIOCGIFINDEX */ | ||
| 173 | }; | 106 | }; |
