diff options
author | deraadt <> | 1999-07-08 22:29:53 +0000 |
---|---|---|
committer | deraadt <> | 1999-07-08 22:29:53 +0000 |
commit | 25db34ffae569e9ff78fd3f5f6f621ee73da9a2f (patch) | |
tree | 6737e541d0f898211da97411c27a0e4a0a0648ce /src/lib | |
parent | caef165b37f567732591ba7a68dad20f274cd92c (diff) | |
download | openbsd-25db34ffae569e9ff78fd3f5f6f621ee73da9a2f.tar.gz openbsd-25db34ffae569e9ff78fd3f5f6f621ee73da9a2f.tar.bz2 openbsd-25db34ffae569e9ff78fd3f5f6f621ee73da9a2f.zip |
use SIOCGIFCONF much more carefully
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libc/net/if_indextoname.c | 48 | ||||
-rw-r--r-- | src/lib/libc/net/if_nameindex.c | 48 | ||||
-rw-r--r-- | src/lib/libc/net/if_nametoindex.c | 48 |
3 files changed, 102 insertions, 42 deletions
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c index cca862de61..b05e82e9ff 100644 --- a/src/lib/libc/net/if_indextoname.c +++ b/src/lib/libc/net/if_indextoname.c | |||
@@ -45,29 +45,48 @@ static char __name[IFNAMSIZ]; | |||
45 | char * | 45 | char * |
46 | if_indextoname(unsigned int index, char *name) | 46 | if_indextoname(unsigned int index, char *name) |
47 | { | 47 | { |
48 | int i, fd, len; | 48 | int i, fd = -1, extra, len = 0; |
49 | struct ifconf ifconf; | 49 | struct ifconf ifconf; |
50 | char lastname[IFNAMSIZ], iname[IFNAMSIZ], *retname = NULL; | 50 | char lastname[IFNAMSIZ], iname[IFNAMSIZ], *retname = NULL, *inbuf; |
51 | struct sockaddr *sa; | 51 | struct sockaddr *sa; |
52 | void *p; | 52 | void *p; |
53 | 53 | ||
54 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | 54 | ifconf.ifc_buf = 0; |
55 | return 0; | ||
56 | 55 | ||
57 | if (!name) | 56 | if (!name) |
58 | name = __name; | 57 | name = __name; |
59 | 58 | ||
60 | ifconf.ifc_len = 0; | 59 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
61 | ifconf.ifc_buf = 0; | ||
62 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | ||
63 | goto ret; | ||
64 | if (ifconf.ifc_len < IFNAMSIZ) | ||
65 | goto ret; | ||
66 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) | ||
67 | goto ret; | ||
68 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | ||
69 | goto ret; | 60 | goto ret; |
70 | 61 | ||
62 | /* | ||
63 | * Try ifc_len == 0 hack first, to get the actual length. | ||
64 | * If that fails, revert to a loop which grows the ifc_buf | ||
65 | * until it is sufficiently large. | ||
66 | */ | ||
67 | extra = sizeof(struct ifreq); | ||
68 | while (1) { | ||
69 | ifconf.ifc_len = len; | ||
70 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf) == -1 && | ||
71 | ifconf.ifc_buf) | ||
72 | goto ret; | ||
73 | if (ifconf.ifc_buf && | ||
74 | ifconf.ifc_len + extra < len) | ||
75 | break; | ||
76 | if (ifconf.ifc_buf) { | ||
77 | if (len == 0) | ||
78 | len = 4096; | ||
79 | ifconf.ifc_len = len *= 2; | ||
80 | } else { | ||
81 | len = ifconf.ifc_len; | ||
82 | extra = 0; | ||
83 | } | ||
84 | inbuf = realloc(ifconf.ifc_buf, ifconf.ifc_len); | ||
85 | if (inbuf == NULL) | ||
86 | goto ret; | ||
87 | ifconf.ifc_buf = inbuf; | ||
88 | } | ||
89 | |||
71 | i = 0; | 90 | i = 0; |
72 | p = ifconf.ifc_buf; | 91 | p = ifconf.ifc_buf; |
73 | len = ifconf.ifc_len; | 92 | len = ifconf.ifc_len; |
@@ -112,7 +131,8 @@ if_indextoname(unsigned int index, char *name) | |||
112 | retname = name; | 131 | retname = name; |
113 | } | 132 | } |
114 | ret: | 133 | ret: |
115 | close(fd); | 134 | if (fd != -1) |
135 | close(fd); | ||
116 | if (ifconf.ifc_buf) | 136 | if (ifconf.ifc_buf) |
117 | free(ifconf.ifc_buf); | 137 | free(ifconf.ifc_buf); |
118 | return (retname); | 138 | return (retname); |
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c index 8714c1f95a..8643397db2 100644 --- a/src/lib/libc/net/if_nameindex.c +++ b/src/lib/libc/net/if_nameindex.c | |||
@@ -43,29 +43,48 @@ | |||
43 | struct if_nameindex * | 43 | struct if_nameindex * |
44 | if_nameindex(void) | 44 | if_nameindex(void) |
45 | { | 45 | { |
46 | int i, j, fd, len; | 46 | int i, j, fd = -1, extra, len = 0; |
47 | struct if_nameindex *nameindex = NULL; | 47 | struct if_nameindex *nameindex = NULL; |
48 | struct ifconf ifconf; | 48 | struct ifconf ifconf; |
49 | char lastname[IFNAMSIZ], *c; | 49 | char lastname[IFNAMSIZ], *c, *inbuf; |
50 | struct if_nameindex *n; | 50 | struct if_nameindex *n; |
51 | struct sockaddr_dl *sd; | 51 | struct sockaddr_dl *sd; |
52 | struct sockaddr *sa; | 52 | struct sockaddr *sa; |
53 | void *p; | 53 | void *p; |
54 | 54 | ||
55 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | 55 | ifconf.ifc_buf = NULL; |
56 | return NULL; | ||
57 | 56 | ||
58 | ifconf.ifc_len = 0; | 57 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
59 | ifconf.ifc_buf = 0; | ||
60 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | ||
61 | goto ret; | ||
62 | if (ifconf.ifc_len < IFNAMSIZ) | ||
63 | goto ret; | ||
64 | if (!(ifconf.ifc_buf = malloc(ifconf.ifc_len))) | ||
65 | goto ret; | ||
66 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | ||
67 | goto ret; | 58 | goto ret; |
68 | 59 | ||
60 | /* | ||
61 | * Try ifc_len == 0 hack first, to get the actual length. | ||
62 | * If that fails, revert to a loop which grows the ifc_buf | ||
63 | * until it is sufficiently large. | ||
64 | */ | ||
65 | extra = sizeof(struct ifreq); | ||
66 | while (1) { | ||
67 | ifconf.ifc_len = len; | ||
68 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf) == -1 && | ||
69 | ifconf.ifc_buf) | ||
70 | goto ret; | ||
71 | if (ifconf.ifc_buf && | ||
72 | ifconf.ifc_len + extra < len) | ||
73 | break; | ||
74 | if (ifconf.ifc_buf) { | ||
75 | if (len == 0) | ||
76 | len = 4096; | ||
77 | ifconf.ifc_len = len *= 2; | ||
78 | } else { | ||
79 | len = ifconf.ifc_len; | ||
80 | extra = 0; | ||
81 | } | ||
82 | inbuf = realloc(ifconf.ifc_buf, ifconf.ifc_len); | ||
83 | if (inbuf == NULL) | ||
84 | goto ret; | ||
85 | ifconf.ifc_buf = inbuf; | ||
86 | } | ||
87 | |||
69 | i = sizeof(struct if_nameindex); | 88 | i = sizeof(struct if_nameindex); |
70 | j = 0; | 89 | j = 0; |
71 | p = ifconf.ifc_buf; | 90 | p = ifconf.ifc_buf; |
@@ -138,7 +157,8 @@ if_nameindex(void) | |||
138 | n->if_index = i; | 157 | n->if_index = i; |
139 | 158 | ||
140 | ret: | 159 | ret: |
141 | close(fd); | 160 | if (fd != -1) |
161 | close(fd); | ||
142 | if (ifconf.ifc_buf) | 162 | if (ifconf.ifc_buf) |
143 | free(ifconf.ifc_buf); | 163 | free(ifconf.ifc_buf); |
144 | return (nameindex); | 164 | return (nameindex); |
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c index 0318f60680..d41f8ecad1 100644 --- a/src/lib/libc/net/if_nametoindex.c +++ b/src/lib/libc/net/if_nametoindex.c | |||
@@ -43,27 +43,46 @@ | |||
43 | unsigned int | 43 | unsigned int |
44 | if_nametoindex(const char *name) | 44 | if_nametoindex(const char *name) |
45 | { | 45 | { |
46 | int i, fd, len; | 46 | int i, fd = -1, extra, len = 0; |
47 | struct ifconf ifconf; | 47 | struct ifconf ifconf; |
48 | char lastname[IFNAMSIZ], *thisname; | 48 | char lastname[IFNAMSIZ], *thisname, *inbuf; |
49 | unsigned int index = 0; | 49 | unsigned int index = 0; |
50 | struct sockaddr *sa; | 50 | struct sockaddr *sa; |
51 | void *p; | 51 | void *p; |
52 | 52 | ||
53 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) | ||
54 | return 0; | ||
55 | |||
56 | ifconf.ifc_len = 0; | ||
57 | ifconf.ifc_buf = 0; | 53 | ifconf.ifc_buf = 0; |
58 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf)) | 54 | |
59 | goto ret; | 55 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
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; | 56 | goto ret; |
66 | 57 | ||
58 | /* | ||
59 | * Try ifc_len == 0 hack first, to get the actual length. | ||
60 | * If that fails, revert to a loop which grows the ifc_buf | ||
61 | * until it is sufficiently large. | ||
62 | */ | ||
63 | extra = sizeof(struct ifreq); | ||
64 | while (1) { | ||
65 | ifconf.ifc_len = len; | ||
66 | if (ioctl(fd, SIOCGIFCONF, (void *) &ifconf) == -1 && | ||
67 | ifconf.ifc_buf) | ||
68 | goto ret; | ||
69 | if (ifconf.ifc_buf && | ||
70 | ifconf.ifc_len + extra < len) | ||
71 | break; | ||
72 | if (ifconf.ifc_buf) { | ||
73 | if (len == 0) | ||
74 | len = 4096; | ||
75 | ifconf.ifc_len = len *= 2; | ||
76 | } else { | ||
77 | len = ifconf.ifc_len; | ||
78 | extra = 0; | ||
79 | } | ||
80 | inbuf = realloc(ifconf.ifc_buf, ifconf.ifc_len); | ||
81 | if (inbuf == NULL) | ||
82 | goto ret; | ||
83 | ifconf.ifc_buf = inbuf; | ||
84 | } | ||
85 | |||
67 | i = 0; | 86 | i = 0; |
68 | p = ifconf.ifc_buf; | 87 | p = ifconf.ifc_buf; |
69 | len = ifconf.ifc_len; | 88 | len = ifconf.ifc_len; |
@@ -106,7 +125,8 @@ if_nametoindex(const char *name) | |||
106 | index = i; | 125 | index = i; |
107 | 126 | ||
108 | ret: | 127 | ret: |
109 | close(fd); | 128 | if (fd != -1) |
129 | close(fd); | ||
110 | if (ifconf.ifc_buf) | 130 | if (ifconf.ifc_buf) |
111 | free(ifconf.ifc_buf); | 131 | free(ifconf.ifc_buf); |
112 | return index; | 132 | return index; |