1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20: #include "ifreq.h"
21:
22:
23: void
24: __ifreq (struct ifreq **ifreqs, int *num_ifs, int sockfd)
25: {
26: int fd = sockfd;
27: struct ifconf ifc;
28: int rq_len;
29: int nifs;
30: # define RQ_IFS 4
31:
32: if (fd < 0)
33: fd = __opensock ();
34: if (fd < 0)
35: {
36: *num_ifs = 0;
37: *ifreqs = NULL;
38: return;
39: }
40:
41: ifc.ifc_buf = NULL;
42: rq_len = RQ_IFS * sizeof (struct ifreq) / 2;
43: do
44: {
45: ifc.ifc_len = rq_len *= 2;
46: void *newp = realloc (ifc.ifc_buf, ifc.ifc_len);
47: if (newp == NULL || __ioctl (fd, SIOCGIFCONF, &ifc) < 0)
48: {
49: free (ifc.ifc_buf);
50:
51: if (fd != sockfd)
52: __close (fd);
53: *num_ifs = 0;
54: *ifreqs = NULL;
55: return;
56: }
57: ifc.ifc_buf = newp;
58: }
59: while (rq_len < sizeof (struct ifreq) + ifc.ifc_len);
60:
61: if (fd != sockfd)
62: __close (fd);
63:
64: #ifdef _HAVE_SA_LEN
65: struct ifreq *ifr = *ifreqs;
66: nifs = 0;
67: while ((char *) ifr < ifc.ifc_buf + ifc.ifc_len)
68: {
69: ++nifs;
70: ifr = __if_nextreq (ifr);
71: if (ifr == NULL)
72: break;
73: }
74: #else
75: nifs = ifc.ifc_len / sizeof (struct ifreq);
76: #endif
77:
78: *num_ifs = nifs;
79: *ifreqs = realloc (ifc.ifc_buf, nifs * sizeof (struct ifreq));
80: }