diff options
author | Ulrich Drepper <drepper@redhat.com> | 1999-08-25 17:19:50 +0000 |
---|---|---|
committer | Ulrich Drepper <drepper@redhat.com> | 1999-08-25 17:19:50 +0000 |
commit | 71d3bda9a7a15bf3f935ffcde8aeceefef8074f7 (patch) | |
tree | 3188ef1b1ad5a60d512813dadfc756225dda0ff4 | |
parent | ad65970214c9dfaa6b61b9519cb0667b376e14d9 (diff) | |
download | glibc-71d3bda9a7a15bf3f935ffcde8aeceefef8074f7.tar.gz glibc-71d3bda9a7a15bf3f935ffcde8aeceefef8074f7.tar.xz glibc-71d3bda9a7a15bf3f935ffcde8aeceefef8074f7.zip |
Update.
1999-08-25 Ulrich Drepper <drepper@cygnus.com> * inet/Makefile (routines): Add getipnodebynm. * inet/getipnodebynm.c: New file. * nss/digits_dots.c: If HAVE_TYPE is define type contains the interface type. * nss/nss_files/files-XXX.c: Define EXTRA_ARGS, EXTRA_ARGS_DECL, and EXTRA_ARGS_VALUE is not already done. (internal_getent): Allow extra parameters and pass them to parse_line. (_nss_files_get,ENTNAME_r): Pass extra parameters to internal_getent. (_nss_files_get##name##_r): Likewise. * nss/nss_files/files-hosts.c: Define EXTRA_ARGS, EXTRA_ARGS_DECL, and EXTRA_ARGS_VALUE to pass flags and type to parser. Add getipnodebyname function. * nss/nss_files/files-parse.c: Define EXTRA_ARGS, EXTRA_ARGS_DECL, and EXTRA_ARGS_VALUE is not already done. (parse_line): Add EXTRA_ARGS_DECL to parameter list. * nss/nss_db/db-XXX.c (lookup): Allow extra parameters and pass them to parse_line. (_nss_db_get##name##_r): Pass extra parameters to lookup. (_nss_db_get,ENTNAME_r): Likewise. * sysdeps/generic/if_index.c (__protocol_available): New function. * sysdeps/unix/sysv/linux/if_index.c: Likewise.
-rw-r--r-- | ChangeLog | 24 | ||||
-rw-r--r-- | inet/Makefile | 3 | ||||
-rw-r--r-- | inet/getipnodebynm.c | 194 | ||||
-rw-r--r-- | login/programs/pt_chown.c | 2 | ||||
-rw-r--r-- | nss/digits_dots.c | 16 | ||||
-rw-r--r-- | nss/nss_db/db-XXX.c | 12 | ||||
-rw-r--r-- | nss/nss_files/files-XXX.c | 16 | ||||
-rw-r--r-- | nss/nss_files/files-hosts.c | 37 | ||||
-rw-r--r-- | nss/nss_files/files-parse.c | 18 | ||||
-rw-r--r-- | sysdeps/generic/if_index.c | 12 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/if_index.c | 83 |
11 files changed, 381 insertions, 36 deletions
diff --git a/ChangeLog b/ChangeLog index 46d724daaa..8e2848d044 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +1999-08-25 Ulrich Drepper <drepper@cygnus.com> + + * inet/Makefile (routines): Add getipnodebynm. + * inet/getipnodebynm.c: New file. + * nss/digits_dots.c: If HAVE_TYPE is define type contains the + interface type. + * nss/nss_files/files-XXX.c: Define EXTRA_ARGS, EXTRA_ARGS_DECL, and + EXTRA_ARGS_VALUE is not already done. + (internal_getent): Allow extra parameters and pass them to parse_line. + (_nss_files_get,ENTNAME_r): Pass extra parameters to internal_getent. + (_nss_files_get##name##_r): Likewise. + * nss/nss_files/files-hosts.c: Define EXTRA_ARGS, EXTRA_ARGS_DECL, + and EXTRA_ARGS_VALUE to pass flags and type to parser. + Add getipnodebyname function. + * nss/nss_files/files-parse.c: Define EXTRA_ARGS, EXTRA_ARGS_DECL, and + EXTRA_ARGS_VALUE is not already done. + (parse_line): Add EXTRA_ARGS_DECL to parameter list. + * nss/nss_db/db-XXX.c (lookup): Allow extra parameters and pass them + to parse_line. + (_nss_db_get##name##_r): Pass extra parameters to lookup. + (_nss_db_get,ENTNAME_r): Likewise. + * sysdeps/generic/if_index.c (__protocol_available): New function. + * sysdeps/unix/sysv/linux/if_index.c: Likewise. + 1999-08-24 Ulrich Drepper <drepper@cygnus.com> * login/pty-private.h: Move to... diff --git a/inet/Makefile b/inet/Makefile index aabe332793..52757ce87d 100644 --- a/inet/Makefile +++ b/inet/Makefile @@ -45,7 +45,8 @@ routines := htonl htons \ rcmd rexec ruserpass \ getnetgrent_r getnetgrent \ getaliasent_r getaliasent getaliasname getaliasname_r \ - in6_addr getnameinfo if_index getipnodebyad freehostent + in6_addr getnameinfo if_index getipnodebyad freehostent \ + getipnodebynm tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-ipnode diff --git a/inet/getipnodebynm.c b/inet/getipnodebynm.c new file mode 100644 index 0000000000..b0be882afb --- /dev/null +++ b/inet/getipnodebynm.c @@ -0,0 +1,194 @@ +/* Copyright (C) 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <netdb.h> +#include <stdlib.h> +#include <string.h> +#include <arpa/inet.h> + +#include "nsswitch.h" + + +/* We avoid using a too large buffer in case the user is accumulating the + results and there is lots of unused space in the allocated buffer. */ +#define BUFLEN 512 + + +/* Internal function to test whether IPv4 and/or IPv6 are available. */ +extern void __protocol_available (int *have_inet, int *have_inet6) + internal_function; + + +/* Type of the lookup function we need here. */ +typedef enum nss_status (*lookup_function) (const char *, int, int, + struct hostent *, char *, + size_t, int *, int *); + +/* The lookup function for the first entry of this service. */ +extern int __nss_hosts_lookup (service_user **nip, const char *name, + void **fctp); + + +struct hostent * +getipnodebyname (const char *name, int type, int flags, int *h_errnop) +{ + static service_user *startp; + static lookup_function start_fct; + service_user *nip; + lookup_function fct; + int no_more; + size_t buffer_size; + char *buffer; + struct hostent *result; + int save; + int have_inet = 1; + int have_inet6 = 1; + + /* First determine whether we have the appropriate interfaces. + XXX I don't think we can cache the result since the system + can be reconfigured. */ + if ((flags & AI_ADDRCONFIG) != 0) + __protocol_available (&have_inet, &have_inet6); + + /* Handle impossible requests first. */ + if ((type == AF_INET && have_inet == 0) + || (type == AF_INET6 && have_inet6 == 0 && ((flags & AI_V4MAPPED) == 0 + || have_inet == 0)) + || (type != AF_INET && type != AF_INET6)) + { + *h_errnop = NO_DATA; + return NULL; + } + + buffer_size = BUFLEN; + buffer = (char *) malloc (buffer_size); + + result = NULL; + if (buffer != NULL) + { +#define HAVE_TYPE +#define resbuf (*((struct hostent *) buffer)) +#include "../nss/digits_dots.c" + } + + if (startp == NULL) + { + no_more = __nss_hosts_lookup (&nip, "getipnodebyname_r", (void **) &fct); + if (no_more) + startp = (service_user *) -1l; + else + { + startp = nip; + start_fct = fct; + } + } + else + { + fct = start_fct; + no_more = (nip = startp) == (service_user *) -1l; + } + + /* First do a lookup with the original protocol type. */ + while (no_more == 0 && buffer != NULL) + { + enum nss_status status; + + status = DL_CALL_FCT (fct, (name, type, + ((flags & AI_ALL) + ? flags : (flags & ~AI_V4MAPPED)), + (struct hostent *) buffer, + (char *) ((struct hostent *) buffer + 1), + buffer_size - sizeof (struct hostent), + &errno, h_errnop)); + + if (status == NSS_STATUS_SUCCESS) + { + result = (struct hostent *) buffer; + break; + } + + if (status == NSS_STATUS_TRYAGAIN && *h_errnop == NETDB_INTERNAL + && errno == ERANGE) + { + /* Resize the buffer, it's too small. */ + buffer_size += BUFLEN; + buffer = (char *) realloc (buffer, buffer_size); + continue; + } + + no_more = __nss_next (&nip, "getipnodebyname_r", + (void **) &fct, status, 0); + } + + /* If we are looking for an IPv6 address but haven't found any and + do not have the AI_ALL but the AI_V4MAPPED flag set, now try + looking up an IPv4 address and map it. */ + if (buffer != NULL && result == NULL + && type == AF_INET6 && (flags & AI_V4MAPPED) != 0 + && (no_more = (nip = startp) == (service_user *) -1l) == 0) + { + /* We have to use a new buffer if there is already a result. */ + fct = start_fct; + + do + { + enum nss_status status; + + status = DL_CALL_FCT (fct, (name, type, flags, + (struct hostent *) buffer, + (char *) ((struct hostent *) buffer + 1), + buffer_size - sizeof (struct hostent), + &errno, h_errnop)); + + if (status == NSS_STATUS_SUCCESS) + { + result = (struct hostent *) buffer; + break; + } + + if (status == NSS_STATUS_TRYAGAIN && *h_errnop == NETDB_INTERNAL + && errno == ERANGE) + { + /* Resize the buffer, it's too small. */ + buffer_size += BUFLEN; + buffer = (char *) realloc (buffer, buffer_size); + continue; + } + + no_more = __nss_next (&nip, "getipnodebyname_r", + (void **) &fct, status, 0); + } + while (no_more == 0 && buffer != NULL); + } + + done: + if (buffer == NULL) + { + /* We are out of memory. */ + *h_errnop = TRY_AGAIN; + assert (result == NULL); + } + else if (result == NULL && buffer != NULL) + free (buffer); + + return result; +} diff --git a/login/programs/pt_chown.c b/login/programs/pt_chown.c index 0205364748..4b0768c04c 100644 --- a/login/programs/pt_chown.c +++ b/login/programs/pt_chown.c @@ -111,7 +111,7 @@ do_pt_chown (void) character device. */ if (stat (pty, &st) < 0 #ifdef unix98_pseudo_p - || ! unix98_pseudo_p (st.st_rdev) + || ! unix98_pseudo_p (major (st.st_rdev)) #else || !S_ISCHR(st.st_mode) #endif diff --git a/nss/digits_dots.c b/nss/digits_dots.c index 03ae2ec7a5..c3c6de7ce1 100644 --- a/nss/digits_dots.c +++ b/nss/digits_dots.c @@ -31,11 +31,12 @@ host_addr_list_t *h_addr_ptrs; size_t size_needed; int addr_size; -#ifndef HAVE_AF - int af = -1; -#endif #ifdef HAVE_TYPE int af = type; +#else +# ifndef HAVE_AF + int af = -1; +# endif #endif switch (af) @@ -153,7 +154,7 @@ /* That's bad. The user hasn't specified that she allows IPv4 numeric addresses. */ result = NULL; - *herrno_p = HOST_NOT_FOUND; + *h_errnop = HOST_NOT_FOUND; goto done; } else @@ -206,11 +207,12 @@ host_addr_list_t *h_addr_ptrs; size_t size_needed; int addr_size; -#ifndef HAVE_AF - int af = -1; -#endif #ifdef HAVE_TYPE int af = type; +#else +# ifndef HAVE_AF + int af = -1; +# endif #endif switch (af) diff --git a/nss/nss_db/db-XXX.c b/nss/nss_db/db-XXX.c index 7356b34462..1cbe8c291e 100644 --- a/nss/nss_db/db-XXX.c +++ b/nss/nss_db/db-XXX.c @@ -1,5 +1,5 @@ /* Common code for DB-based databases in nss_db module. - Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. + Copyright (C) 1996, 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 @@ -162,7 +162,7 @@ CONCAT(_nss_db_end,ENTNAME) (void) /* Do a database lookup for KEY. */ static enum nss_status lookup (DBT *key, struct STRUCTURE *result, - void *buffer, size_t buflen, int *errnop H_ERRNO_PROTO) + void *buffer, size_t buflen, int *errnop H_ERRNO_PROTO EXTRA_ARGS_DECL) { char *p; enum nss_status status; @@ -211,7 +211,7 @@ lookup (DBT *key, struct STRUCTURE *result, while (isspace (*p)) ++p; - err = parse_line (p, result, buffer, buflen, errnop); + err = parse_line (p, result, buffer, buflen, errnop EXTRA_ARGS); if (err == 0) { @@ -274,7 +274,8 @@ _nss_db_get##name##_r (proto, \ key.size = KEYPRINTF keypattern; \ key.flags = 0; \ __libc_lock_lock (lock); \ - status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG); \ + status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG \ + EXTRA_ARGS_VALUE); \ __libc_lock_unlock (lock); \ return status; \ } @@ -302,7 +303,8 @@ CONCAT(_nss_db_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer, { key.size = snprintf (key.data = buf, sizeof buf, "0%u", entidx++); key.flags = 0; - status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG); + status = lookup (&key, result, buffer, buflen, errnop H_ERRNO_ARG + EXTRA_ARGS_VALUE); if (status == NSS_STATUS_TRYAGAIN #ifdef NEED_H_ERRNO && *herrnop == NETDB_INTERNAL diff --git a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c index d14500197b..6dd591987b 100644 --- a/nss/nss_files/files-XXX.c +++ b/nss/nss_files/files-XXX.c @@ -50,6 +50,12 @@ # define H_ERRNO_SET(val) ((void) 0) #endif +#ifndef EXTRA_ARGS +# define EXTRA_ARGS +# define EXTRA_ARGS_DECL +# define EXTRA_ARGS_VALUE +#endif + /* Locks the static variables in this file. */ __libc_lock_define_initialized (static, lock) @@ -161,7 +167,8 @@ CONCAT(_nss_files_end,ENTNAME) (void) static enum nss_status internal_getent (struct STRUCTURE *result, - char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO) + char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO + EXTRA_ARGS_DECL) { char *p; struct parser_data *data = (void *) buffer; @@ -204,7 +211,8 @@ internal_getent (struct STRUCTURE *result, while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */ /* Parse the line. If it is invalid, loop to get the next line of the file to parse. */ - || ! (parse_result = parse_line (p, result, data, buflen, errnop))); + || ! (parse_result = parse_line (p, result, data, buflen, errnop + EXTRA_ARGS))); /* Filled in RESULT with the next entry from the database file. */ return parse_result == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_SUCCESS; @@ -249,7 +257,7 @@ CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer, if (status == NSS_STATUS_SUCCESS) { status = internal_getent (result, buffer, buflen, errnop - H_ERRNO_ARG); + H_ERRNO_ARG EXTRA_ARGS_VALUE); /* Remember this position if we were successful. If the operation failed we give the user a chance to repeat the @@ -298,7 +306,7 @@ _nss_files_get##name##_r (proto, \ last_use = getby; \ \ while ((status = internal_getent (result, buffer, buflen, errnop \ - H_ERRNO_ARG)) \ + H_ERRNO_ARG EXTRA_ARGS_VALUE)) \ == NSS_STATUS_SUCCESS) \ { break_if_match } \ \ diff --git a/nss/nss_files/files-hosts.c b/nss/nss_files/files-hosts.c index 2379919f49..a5f4fcc2a3 100644 --- a/nss/nss_files/files-hosts.c +++ b/nss/nss_files/files-hosts.c @@ -1,5 +1,5 @@ /* Hosts file parser in nss_files module. - Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. + Copyright (C) 1996, 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 @@ -32,6 +32,9 @@ #define DATABASE "hosts" #define NEED_H_ERRNO +#define EXTRA_ARGS , af, flags +#define EXTRA_ARGS_DECL , int af, int flags + #define ENTDATA hostent_data struct hostent_data { @@ -50,9 +53,9 @@ LINE_PARSER STRING_FIELD (addr, isspace, 1); /* Parse address. */ - if (inet_pton (AF_INET, addr, entdata->host_addr) > 0) + if (af == AF_INET && inet_pton (AF_INET, addr, entdata->host_addr) > 0) { - if (_res.options & RES_USE_INET6) + if (flags & AI_V4MAPPED) { map_v4v6_address ((char *) entdata->host_addr, (char *) entdata->host_addr); @@ -65,7 +68,8 @@ LINE_PARSER result->h_length = INADDRSZ; } } - else if (inet_pton (AF_INET6, addr, entdata->host_addr) > 0) + else if (af == AF_INET6 + && inet_pton (AF_INET6, addr, entdata->host_addr) > 0) { result->h_addrtype = AF_INET6; result->h_length = IN6ADDRSZ; @@ -82,26 +86,35 @@ LINE_PARSER STRING_FIELD (result->h_name, isspace, 1); }) +#define EXTRA_ARGS_VALUE \ + , ((_res.options & RES_USE_INET6) ? AF_INET6 : AF_INET), \ + ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0) #include "files-XXX.c" DB_LOOKUP (hostbyname, ,, { - if (result->h_addrtype != ((_res.options & RES_USE_INET6) - ? AF_INET6 : AF_INET)) - continue; LOOKUP_NAME_CASE (h_name, h_aliases) }, const char *name) +#undef EXTRA_ARGS_VALUE +#define EXTRA_ARGS_VALUE \ + , af, ((_res.options & RES_USE_INET6) ? AI_V4MAPPED : 0) DB_LOOKUP (hostbyname2, ,, { - if (result->h_addrtype != af) - continue; LOOKUP_NAME_CASE (h_name, h_aliases) }, const char *name, int af) DB_LOOKUP (hostbyaddr, ,, { - if (result->h_addrtype == type && result->h_length == len && - ! memcmp (addr, result->h_addr_list[0], len)) + if (result->h_length == len + && ! memcmp (addr, result->h_addr_list[0], len)) break; - }, const char *addr, int len, int type) + }, const char *addr, int len, int af) + +#undef EXTRA_ARGS_VALUE +#define EXTRA_ARGS_VALUE \ + , af, flags +DB_LOOKUP (ipnodebyname, ,, + { + LOOKUP_NAME_CASE (h_name, h_aliases) + }, const char *name, int af, int flags) diff --git a/nss/nss_files/files-parse.c b/nss/nss_files/files-parse.c index d688fdc803..41f0ef6a05 100644 --- a/nss/nss_files/files-parse.c +++ b/nss/nss_files/files-parse.c @@ -1,5 +1,5 @@ /* Common code for file-based database parsers in nss_files module. - Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. + Copyright (C) 1996, 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 @@ -33,8 +33,18 @@ NEED_H_ERRNO - defined iff an arg `int *herrnop' is used. + EXTRA_ARGS -- defined iff extra parameters must be passed to the parser + EXTRA_ARGS_DECL -- declaration for these extra parameters + EXTRA_ARGS_VALUE -- values to be passed for these parameters + Also see files-XXX.c. */ +#ifndef EXTRA_ARGS +# define EXTRA_ARGS +# define EXTRA_ARGS_DECL +# define EXTRA_ARGS_VALUE +#endif + #define CONCAT(a,b) CONCAT1(a,b) #define CONCAT1(a,b) a##b @@ -69,7 +79,8 @@ struct parser_data /* The parser is defined in a different module. */ extern int parse_line (char *line, struct STRUCTURE *result, - struct parser_data *data, size_t datalen, int *errnop); + struct parser_data *data, size_t datalen, int *errnop + EXTRA_ARGS_DECL); # define LINE_PARSER(EOLSET, BODY) /* Do nothing */ @@ -80,7 +91,8 @@ extern int parse_line (char *line, struct STRUCTURE *result, # define LINE_PARSER(EOLSET, BODY) \ parser_stclass int \ parse_line (char *line, struct STRUCTURE *result, \ - struct parser_data *data, size_t datalen, int *errnop) \ + struct parser_data *data, size_t datalen, int *errnop \ + EXTRA_ARGS_DECL) \ { \ ENTDATA_DECL (data) \ char *p = strpbrk (line, EOLSET "\n"); \ 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); +} |