1 | /* Copyright (C) 1999, 2002, 2003, 2004 Free Software Foundation, Inc. |
---|
2 | This file is part of the GNU C Library. |
---|
3 | Contributed by Andreas Jaeger <aj@suse.de>. |
---|
4 | |
---|
5 | The GNU C Library is free software; you can redistribute it and/or |
---|
6 | modify it under the terms of the GNU Lesser General Public |
---|
7 | License as published by the Free Software Foundation; either |
---|
8 | version 2.1 of the License, or (at your option) any later version. |
---|
9 | |
---|
10 | The GNU C Library is distributed in the hope that it will be useful, |
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
13 | Lesser General Public License for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU Lesser General Public |
---|
16 | License along with the GNU C Library; if not, write to the Free |
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
18 | 02111-1307 USA. */ |
---|
19 | |
---|
20 | #include "ifreq.h" |
---|
21 | |
---|
22 | /* Variable to signal whether SIOCGIFCONF is not available. */ |
---|
23 | #if __ASSUME_SIOCGIFNAME == 0 || 1 |
---|
24 | static int old_siocgifconf; |
---|
25 | #else |
---|
26 | # define old_siocgifconf 0 |
---|
27 | #endif |
---|
28 | |
---|
29 | |
---|
30 | void |
---|
31 | __ifreq (struct ifreq **ifreqs, int *num_ifs, int sockfd) |
---|
32 | { |
---|
33 | int fd = sockfd; |
---|
34 | struct ifconf ifc; |
---|
35 | int rq_len; |
---|
36 | int nifs; |
---|
37 | # define RQ_IFS 4 |
---|
38 | |
---|
39 | if (fd < 0) |
---|
40 | fd = __opensock (); |
---|
41 | if (fd < 0) |
---|
42 | { |
---|
43 | *num_ifs = 0; |
---|
44 | *ifreqs = NULL; |
---|
45 | return; |
---|
46 | } |
---|
47 | |
---|
48 | ifc.ifc_buf = NULL; |
---|
49 | |
---|
50 | /* We may be able to get the needed buffer size directly, rather than |
---|
51 | guessing. */ |
---|
52 | if (! old_siocgifconf) |
---|
53 | { |
---|
54 | ifc.ifc_buf = NULL; |
---|
55 | ifc.ifc_len = 0; |
---|
56 | if (ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0) |
---|
57 | { |
---|
58 | # if __ASSUME_SIOCGIFNAME == 0 |
---|
59 | old_siocgifconf = 1; |
---|
60 | # endif |
---|
61 | rq_len = RQ_IFS * sizeof (struct ifreq); |
---|
62 | } |
---|
63 | else |
---|
64 | rq_len = ifc.ifc_len; |
---|
65 | } |
---|
66 | else |
---|
67 | rq_len = RQ_IFS * sizeof (struct ifreq); |
---|
68 | |
---|
69 | /* Read all the interfaces out of the kernel. */ |
---|
70 | while (1) |
---|
71 | { |
---|
72 | ifc.ifc_len = rq_len; |
---|
73 | void *newp = realloc (ifc.ifc_buf, ifc.ifc_len); |
---|
74 | if (newp == NULL |
---|
75 | || (ifc.ifc_buf = newp, ioctl (fd, SIOCGIFCONF, &ifc)) < 0) |
---|
76 | { |
---|
77 | free (ifc.ifc_buf); |
---|
78 | |
---|
79 | if (fd != sockfd) |
---|
80 | close (fd); |
---|
81 | |
---|
82 | *num_ifs = 0; |
---|
83 | *ifreqs = NULL; |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | if (!old_siocgifconf || ifc.ifc_len < rq_len) |
---|
88 | break; |
---|
89 | |
---|
90 | rq_len *= 2; |
---|
91 | } |
---|
92 | |
---|
93 | nifs = ifc.ifc_len / sizeof (struct ifreq); |
---|
94 | |
---|
95 | if (fd != sockfd) |
---|
96 | close (fd); |
---|
97 | |
---|
98 | *num_ifs = nifs; |
---|
99 | *ifreqs = realloc (ifc.ifc_buf, nifs * sizeof (struct ifreq)); |
---|
100 | } |
---|