diff options
Diffstat (limited to 'src/lib/libc/net/if_nametoindex.c')
| -rw-r--r-- | src/lib/libc/net/if_nametoindex.c | 117 |
1 files changed, 62 insertions, 55 deletions
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c index ad05387e34..0318f60680 100644 --- a/src/lib/libc/net/if_nametoindex.c +++ b/src/lib/libc/net/if_nametoindex.c | |||
| @@ -1,7 +1,7 @@ | |||
| 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 | * | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions | 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: | 7 | * are met: |
| @@ -17,7 +17,7 @@ | |||
| 17 | * 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 |
| 18 | * 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 |
| 19 | * without specific prior written permission. | 19 | * without specific prior written permission. |
| 20 | * | 20 | * |
| 21 | * 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 |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| @@ -29,7 +29,6 @@ | |||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 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 | 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
| 32 | * | ||
| 33 | */ | 32 | */ |
| 34 | 33 | ||
| 35 | #include <sys/types.h> | 34 | #include <sys/types.h> |
| @@ -38,69 +37,77 @@ | |||
| 38 | #include <sys/ioctl.h> | 37 | #include <sys/ioctl.h> |
| 39 | #include <net/if.h> | 38 | #include <net/if.h> |
| 40 | #include <net/if_dl.h> | 39 | #include <net/if_dl.h> |
| 40 | #include <string.h> | ||
| 41 | #include <errno.h> | 41 | #include <errno.h> |
| 42 | 42 | ||
| 43 | unsigned int if_nametoindex(const char *name) | 43 | unsigned int |
| 44 | if_nametoindex(const char *name) | ||
| 44 | { | 45 | { |
| 45 | int i, fd; | 46 | int i, fd, len; |
| 46 | struct ifconf ifconf; | 47 | struct ifconf ifconf; |
| 47 | void *p; | 48 | char lastname[IFNAMSIZ], *thisname; |
| 48 | int len; | 49 | unsigned int index = 0; |
| 49 | char lastname[IFNAMSIZ + 1], *thisname; | 50 | struct sockaddr *sa; |
| 50 | unsigned int index = 0; | 51 | void *p; |
| 52 | |||
| 53 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | ||
| 54 | return 0; | ||
| 51 | 55 | ||
| 52 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | 56 | ifconf.ifc_len = 0; |
| 53 | return 0; | 57 | ifconf.ifc_buf = 0; |
| 58 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | ||
| 59 | goto ret; | ||
| 60 | if (ifconf.ifc_len < IFNAMSIZ) | ||
| 61 | goto ret; | ||
| 62 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) | ||
| 63 | goto ret; | ||
| 64 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | ||
| 65 | goto ret; | ||
| 54 | 66 | ||
| 55 | ifconf.ifc_len = 0; | 67 | i = 0; |
| 56 | ifconf.ifc_buf = 0; | 68 | p = ifconf.ifc_buf; |
| 57 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) | 69 | len = ifconf.ifc_len; |
| 58 | goto ret; | 70 | lastname[0] = 0; |
| 59 | if (ifconf.ifc_len < IFNAMSIZ) | 71 | lastname[sizeof(lastname)-1] = 0; |
| 60 | goto ret; | ||
| 61 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) | ||
| 62 | goto ret; | ||
| 63 | if (ioctl(fd, SIOCGIFCONF, (void *)&ifconf)) | ||
| 64 | goto ret; | ||
| 65 | 72 | ||
| 66 | i = 0; | 73 | while (len > 0) { |
| 67 | p = ifconf.ifc_buf; | 74 | if (len < IFNAMSIZ + sizeof(struct sockaddr)) |
| 68 | len = ifconf.ifc_len; | 75 | goto ret; |
| 69 | lastname[0] = 0; | ||
| 70 | lastname[IFNAMSIZ] = 0; | ||
| 71 | 76 | ||
| 72 | while(len > 0) { | 77 | thisname = p; |
| 73 | if (len < (IFNAMSIZ + sizeof(struct sockaddr))) | 78 | if (strncmp(lastname, p, IFNAMSIZ)) { |
| 74 | goto ret; | 79 | if (!strcmp(lastname, name)) { |
| 80 | index = i; | ||
| 81 | goto ret; | ||
| 82 | } | ||
| 83 | memcpy(lastname, thisname, IFNAMSIZ); | ||
| 84 | i++; | ||
| 85 | } | ||
| 75 | 86 | ||
| 76 | if (strncmp(lastname, thisname = p, IFNAMSIZ)) { | 87 | len -= IFNAMSIZ; |
| 77 | if (!strcmp(lastname, name)) { | 88 | p += IFNAMSIZ; |
| 78 | index = i; | 89 | sa = (struct sockaddr *)p; |
| 79 | goto ret; | ||
| 80 | }; | ||
| 81 | memcpy(lastname, thisname, IFNAMSIZ); | ||
| 82 | i++; | ||
| 83 | }; | ||
| 84 | 90 | ||
| 85 | len -= IFNAMSIZ; | 91 | if (!strncmp(thisname, name, IFNAMSIZ) && |
| 86 | p += IFNAMSIZ; | 92 | sa->sa_family == AF_LINK) { |
| 93 | struct sockaddr_dl *sd = p; | ||
| 87 | 94 | ||
| 88 | if (!strncmp(thisname, name, IFNAMSIZ) && (((struct sockaddr *)p)->sa_family == AF_LINK)) { | 95 | index = sd->sdl_index; |
| 89 | index = ((struct sockaddr_dl *)p)->sdl_index; | 96 | goto ret; |
| 90 | goto ret; | 97 | } |
| 91 | }; | ||
| 92 | 98 | ||
| 93 | if (len < SA_LEN((struct sockaddr *)p)) | 99 | if (len < sa->sa_len) |
| 94 | goto ret; | 100 | goto ret; |
| 95 | len -= SA_LEN((struct sockaddr *)p); | 101 | len -= sa->sa_len; |
| 96 | p += SA_LEN((struct sockaddr *)p); | 102 | p += sa->sa_len; |
| 97 | }; | 103 | } |
| 98 | 104 | ||
| 99 | if (!strcmp(lastname, name)) | 105 | if (!strcmp(lastname, name)) |
| 100 | index = i; | 106 | index = i; |
| 101 | 107 | ||
| 102 | ret: | 108 | ret: |
| 103 | close(fd); | 109 | close(fd); |
| 104 | free(ifconf.ifc_buf); | 110 | if (ifconf.ifc_buf) |
| 105 | return index; | 111 | free(ifconf.ifc_buf); |
| 106 | }; | 112 | return index; |
| 113 | } | ||
