aboutsummaryrefslogtreecommitdiff
path: root/sysdeps
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps')
-rw-r--r--sysdeps/generic/if_index.c12
-rw-r--r--sysdeps/unix/sysv/linux/if_index.c83
2 files changed, 92 insertions, 3 deletions
diff --git a/sysdeps/generic/if_index.c b/sysdeps/generic/if_index.c
index 9ef4e1e810..7aedf270eb 100644
--- a/sysdeps/generic/if_index.c
+++ b/sysdeps/generic/if_index.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
+/* Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -51,3 +51,13 @@ if_nameindex (void)
}
stub_warning (if_nameindex)
#include <stub-tag.h>
+
+
+void
+internal_function
+__protocol_avaliable (int *have_inet, have_inet6)
+{
+ /* By default we assume that IPv4 is avaialble, IPv6 not. */
+ *have_inet = 1;
+ *have_inet6 = 0;
+}
diff --git a/sysdeps/unix/sysv/linux/if_index.c b/sysdeps/unix/sysv/linux/if_index.c
index 9634aa794f..d2951c6fe1 100644
--- a/sysdeps/unix/sysv/linux/if_index.c
+++ b/sysdeps/unix/sysv/linux/if_index.c
@@ -54,9 +54,9 @@ opensock (void)
if (fd == -1)
{
- fd = __socket (sock_af = AF_INET6, SOCK_DGRAM, 0);
+ fd = __socket (sock_af = AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
- fd = __socket (sock_af = AF_INET, SOCK_DGRAM, 0);
+ fd = __socket (sock_af = AF_INET6, SOCK_DGRAM, 0);
if (fd < 0)
fd = __socket (sock_af = AF_IPX, SOCK_DGRAM, 0);
if (fd < 0)
@@ -277,3 +277,82 @@ if_indextoname (unsigned int ifindex, char *ifname)
# endif
#endif
}
+
+
+void
+internal_function
+__protocol_avaliable (int *have_inet, int *have_inet6)
+{
+ int fd = opensock ();
+ unsigned int nifs;
+ int rq_len;
+ struct ifconf ifc;
+# if __ASSUME_SIOCGIFNAME == 0
+ static int old_siocgifconf;
+# else
+# define old_siocgifconf 0
+# endif
+# define RQ_IFS 4
+
+ /* Wirst case assumption. */
+ *have_inet = 0;
+ *have_inet6 = 0;
+
+ if (fd == NULL)
+ /* We cannot open the socket. No networking at all? */
+ return;
+
+ /* We may be able to get the needed buffer size directly, rather than
+ guessing. */
+ if (! old_siocgifconf)
+ {
+ ifc.ifc_buf = NULL;
+ ifc.ifc_len = 0;
+ if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
+ {
+# if __ASSUME_SIOCGIFNAME == 0
+ old_siocgifconf = 1;
+# endif
+ rq_len = RQ_IFS * sizeof (struct ifreq);
+ }
+ else
+ rq_len = ifc.ifc_len;
+ }
+ else
+ rq_len = RQ_IFS * sizeof (struct ifreq);
+
+ /* Read all the interfaces out of the kernel. */
+ do
+ {
+ ifc.ifc_buf = alloca (ifc.ifc_len = rq_len);
+ if (ifc.ifc_buf == NULL || __ioctl (fd, SIOCGIFCONF, &ifc) < 0)
+ {
+ __close (fd);
+ return;
+ }
+ rq_len *= 2;
+ }
+ while (ifc.ifc_len == rq_len && old_siocgifconf);
+
+ nifs = ifc.ifc_len / sizeof (struct ifreq);
+
+ /* Go through all the interfaces and get the address. */
+ while (nifs-- > 0)
+ if (__ioctl (fd, SIOCGIFADDR, &ifc.ifc_req[nifs]) >= 0)
+ {
+ /* We successfully got information about this interface. Now
+ test whether it is an IPv4 or IPv6 address. */
+ if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET)
+ *have_inet = 1;
+ else if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET6)
+ *have_inet6 = 1;
+
+ /* Note, this is & not &&. It works since the values are always
+ 0 or 1. */
+ if (*have_inet & *have_inet6)
+ /* We can stop early. */
+ break;
+ }
+
+ __close (fd);
+}