diff options
Diffstat (limited to 'src/lib/libc/net/__siocgifconf.c')
-rw-r--r-- | src/lib/libc/net/__siocgifconf.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/lib/libc/net/__siocgifconf.c b/src/lib/libc/net/__siocgifconf.c new file mode 100644 index 0000000000..4dac955f56 --- /dev/null +++ b/src/lib/libc/net/__siocgifconf.c | |||
@@ -0,0 +1,131 @@ | |||
1 | /* | ||
2 | * %%% copyright-cmetz-98-bsd | ||
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 | * | ||
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 | */ | ||
89 | |||
90 | #include <sys/types.h> | ||
91 | #include <stdlib.h> | ||
92 | #include <sys/socket.h> | ||
93 | #include <net/if.h> | ||
94 | #include <sys/ioctl.h> | ||
95 | #include <errno.h> | ||
96 | |||
97 | int __siocgifconf(int fd, struct ifconf *ifconf) | ||
98 | { | ||
99 | int i, j; | ||
100 | |||
101 | ifconf->ifc_len = 0; | ||
102 | ifconf->ifc_buf = 0; | ||
103 | if (!ioctl(fd, SIOCGIFCONF, (void *)ifconf) && ifconf->ifc_len) { | ||
104 | if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len))) { | ||
105 | errno = ENOMEM; | ||
106 | return -1; | ||
107 | }; | ||
108 | if (ioctl(fd, SIOCGIFCONF, (void *)ifconf)) { | ||
109 | free(ifconf->ifc_buf); | ||
110 | return -1; | ||
111 | }; | ||
112 | return 0; | ||
113 | }; | ||
114 | |||
115 | for (i = 5; i < 12; i++) { | ||
116 | if (!(ifconf->ifc_buf = malloc(ifconf->ifc_len = j = sizeof(struct ifconf) * (1 << i)))) { | ||
117 | errno = ENOMEM; | ||
118 | return -1; | ||
119 | }; | ||
120 | if (ioctl(fd, SIOCGIFCONF, (void *)ifconf)) { | ||
121 | free(ifconf->ifc_buf); | ||
122 | return -1; | ||
123 | }; | ||
124 | if ((ifconf->ifc_len > IFNAMSIZ + sizeof(struct sockaddr)) | ||
125 | && (ifconf->ifc_len < j)) | ||
126 | return 0; | ||
127 | free(ifconf->ifc_buf); | ||
128 | }; | ||
129 | return -1; | ||
130 | }; | ||
131 | |||