]> git.ipfire.org Git - thirdparty/glibc.git/blob - inet/ifreq.c
2005-12-13 Ulrich Drepper <drepper@redhat.com>
[thirdparty/glibc.git] / inet / ifreq.c
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
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; /* Doubled in the loop. */
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 }