[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-gnulib] Re: getaddrinfo
From: |
Simon Josefsson |
Subject: |
[Bug-gnulib] Re: getaddrinfo |
Date: |
Wed, 10 Nov 2004 15:50:59 +0100 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux) |
Paul Eggert <address@hidden> writes:
>> I have a vague recollection that using CPP #if's inside a C statement
>> in this way was not valid.
>
> It's not valid to use #if's like this inside a macro call.
Ah, that was it. Thanks.
Bruno Haible <address@hidden> writes:
>> #ifndef PF_INET
>> # define PF_INET 0.5
>> #endif
>> #ifndef PF_INET6
>> # define PF_INET6 0.5
>> #endif
>>
>> if (hints
>> && hints->ai_family != PF_INET
>> && hints->ai_family != PF_INET6
>> && hints->ai_family != PF_UNSPEC)
>
> not only obfuscates the intent of code (it looks aesthetically better
> but is more confusing), but also leaves a useless floating-point
> computation in the code path. (Yes, gcc does not eliminate the
> useless comparisons.)
I agree with Bruno here, while the 0.5 approach might be a neat trick,
it is (to me) far from a idiomatic construct that simplify reading,
rather I think it is confusing for a casual reader.
> If you want to avoid #if conditionals inside expressions, I suggest to
> change the expression into multiple statements:
>
> static inline bool
> validate_family (int family)
> {
> #if HAVE_IPV4
> if (family == PF_INET)
> return true;
> #endif
> #if HAVE_IPV6
> if (family == PF_INET6)
> return true;
> #endif
> if (family == PF_UNSPEC)
> return true;
> return false;
> }
>
> if (hints && !validate_family (hints->ai_family))
> return EAI_FAMILY;
I'm using this now.
I have installed the patch below.
Thanks.
Index: ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/ChangeLog,v
retrieving revision 1.215
diff -u -p -r1.215 ChangeLog
--- ChangeLog 10 Nov 2004 08:50:25 -0000 1.215
+++ ChangeLog 10 Nov 2004 14:52:40 -0000
@@ -1,3 +1,10 @@
+2004-11-08 Simon Josefsson <address@hidden>
+
+ * MODULES.html.sh (Support for systems lacking POSIX:2001): Add
+ getaddrinfo.
+
+ * modules/getaddrinfo: New file.
+
2004-11-10 Jim Meyering <address@hidden>
* modules/closeout (Depends-on): Remove fpending.
Index: MODULES.html.sh
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.66
diff -u -p -r1.66 MODULES.html.sh
--- MODULES.html.sh 11 Oct 2004 13:08:55 -0000 1.66
+++ MODULES.html.sh 10 Nov 2004 14:52:40 -0000
@@ -1730,6 +1730,7 @@ func_all_modules ()
func_module chown
func_module dup2
func_module ftruncate
+ func_module getaddrinfo
func_module getcwd
func_module getgroups
func_module gethostname
Index: lib/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/ChangeLog,v
retrieving revision 1.741
diff -u -p -r1.741 ChangeLog
--- lib/ChangeLog 10 Nov 2004 08:48:50 -0000 1.741
+++ lib/ChangeLog 10 Nov 2004 14:52:41 -0000
@@ -1,3 +1,7 @@
+2004-11-08 Simon Josefsson <address@hidden>
+
+ * getaddrinfo.h, getaddrinfo.c: New file.
+
2004-11-10 Jim Meyering <address@hidden>
Ensure that no close failure goes unreported.
Index: lib/gai_strerror.c
===================================================================
RCS file: lib/gai_strerror.c
diff -N lib/gai_strerror.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/gai_strerror.c 10 Nov 2004 14:52:41 -0000
@@ -0,0 +1,72 @@
+/* Copyright (C) 1997, 2001, 2002, 2004 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Philip Blundell <address@hidden>, 1997.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <stdio.h>
+#include <netdb.h>
+
+#ifdef _LIBC
+# include <libintl.h>
+#else
+# include "getaddrinfo.h"
+# include "gettext.h"
+# define _(String) gettext (String)
+# define N_(String) String
+#endif
+
+static struct
+ {
+ int code;
+ const char *msg;
+ }
+values[] =
+ {
+ { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
+ { EAI_AGAIN, N_("Temporary failure in name resolution") },
+ { EAI_BADFLAGS, N_("Bad value for ai_flags") },
+ { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
+ { EAI_FAMILY, N_("ai_family not supported") },
+ { EAI_MEMORY, N_("Memory allocation failure") },
+ { EAI_NODATA, N_("No address associated with hostname") },
+ { EAI_NONAME, N_("Name or service not known") },
+ { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
+ { EAI_SOCKTYPE, N_("ai_socktype not supported") },
+ { EAI_SYSTEM, N_("System error") },
+#ifdef __USE_GNU
+ { EAI_INPROGRESS, N_("Processing request in progress") },
+ { EAI_CANCELED, N_("Request canceled") },
+ { EAI_NOTCANCELED, N_("Request not canceled") },
+ { EAI_ALLDONE, N_("All requests done") },
+ { EAI_INTR, N_("Interrupted by a signal") },
+ { EAI_IDN_ENCODE, N_("Parameter string not correctly encoded") }
+#endif
+ };
+
+const char *
+gai_strerror (int code)
+{
+ size_t i;
+ for (i = 0; i < sizeof (values) / sizeof (values[0]); ++i)
+ if (values[i].code == code)
+ return _(values[i].msg);
+
+ return _("Unknown error");
+}
+#ifdef _LIBC
+libc_hidden_def (gai_strerror)
+#endif
Index: lib/getaddrinfo.h
===================================================================
RCS file: lib/getaddrinfo.h
diff -N lib/getaddrinfo.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/getaddrinfo.h 10 Nov 2004 14:52:41 -0000
@@ -0,0 +1,90 @@
+/* Get address information.
+ Copyright (C) 1996-2002, 2003, 2004 Free Software Foundation, Inc.
+ Contributed by Simon Josefsson <address@hidden>.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#ifndef GETADDRINFO_H
+# define GETADDRINFO_H
+
+/* Get getaddrinfo declarations, if available. */
+# include <sys/socket.h>
+# include <netdb.h>
+
+# if defined HAVE_GETADDRINFO && !HAVE_GETADDRINFO
+
+/* Get socklen_t, struct sockaddr. */
+# include <sys/types.h>
+
+/* Structure to contain information about address of a service provider. */
+struct addrinfo
+{
+ int ai_flags; /* Input flags. */
+ int ai_family; /* Protocol family for socket. */
+ int ai_socktype; /* Socket type. */
+ int ai_protocol; /* Protocol for socket. */
+ socklen_t ai_addrlen; /* Length of socket address. */
+ struct sockaddr *ai_addr; /* Socket address for socket. */
+ char *ai_canonname; /* Canonical name for service location. */
+ struct addrinfo *ai_next; /* Pointer to next in list. */
+};
+
+/* Possible values for `ai_flags' field in `addrinfo' structure. */
+# define AI_PASSIVE 0x0001 /* Socket address is intended for `bind'. */
+# define AI_CANONNAME 0x0002 /* Request for canonical name. */
+# define AI_NUMERICHOST 0x0004 /* Don't use name resolution. */
+# define AI_V4MAPPED 0x0008 /* IPv4 mapped addresses are acceptable. */
+# define AI_ALL 0x0010 /* Return IPv4 mapped and IPv6
addresses. */
+# define AI_ADDRCONFIG 0x0020 /* Use configuration of this host to choose
+ returned address type.. */
+
+/* Error values for `getaddrinfo' function. */
+# define EAI_BADFLAGS -1 /* Invalid value for `ai_flags' field. */
+# define EAI_NONAME -2 /* NAME or SERVICE is unknown. */
+# define EAI_AGAIN -3 /* Temporary failure in name resolution. */
+# define EAI_FAIL -4 /* Non-recoverable failure in name res. */
+# define EAI_NODATA -5 /* No address associated with NAME. */
+# define EAI_FAMILY -6 /* `ai_family' not supported. */
+# define EAI_SOCKTYPE -7 /* `ai_socktype' not supported. */
+# define EAI_SERVICE -8 /* SERVICE not supported for `ai_socktype'. */
+# define EAI_ADDRFAMILY -9 /* Address family for NAME not
supported. */
+# define EAI_MEMORY -10 /* Memory allocation failure. */
+# define EAI_SYSTEM -11 /* System error returned in `errno'. */
+# define EAI_OVERFLOW -12 /* Argument buffer overflow. */
+# ifdef __USE_GNU
+# define EAI_INPROGRESS -100 /* Processing request in progress. */
+# define EAI_CANCELED -101 /* Request canceled. */
+# define EAI_NOTCANCELED -102 /* Request not canceled. */
+# define EAI_ALLDONE -103 /* All requests done. */
+# define EAI_INTR -104 /* Interrupted by a signal. */
+# define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
+# endif
+
+/* Translate name of a service location and/or a service name to set of
+ socket addresses. */
+extern int getaddrinfo (const char *restrict nodename,
+ const char *restrict servname,
+ const struct addrinfo *restrict hints,
+ struct addrinfo **restrict res);
+
+/* Free `addrinfo' structure AI including associated storage. */
+extern void freeaddrinfo (struct addrinfo *ai);
+
+/* Convert error return from getaddrinfo() to a string. */
+extern const char *gai_strerror (int ecode);
+
+# endif
+
+#endif /* GETADDRINFO_H */
Index: lib/getaddrinfo.c
===================================================================
RCS file: lib/getaddrinfo.c
diff -N lib/getaddrinfo.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/getaddrinfo.c 10 Nov 2004 14:52:41 -0000
@@ -0,0 +1,195 @@
+/* Get address information (partial implementation).
+ Copyright (C) 1997, 2001, 2002, 2004 Free Software Foundation, Inc.
+ Contributed by Simon Josefsson <address@hidden>.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Get calloc. */
+#include <stdlib.h>
+
+/* Get memcpy. */
+#include <string.h>
+
+/* Get struct hostent. */
+#include <netdb.h>
+
+#include <stdbool.h>
+
+#include "gettext.h"
+#define _(String) gettext (String)
+#define N_(String) String
+
+#include "getaddrinfo.h"
+
+static inline bool
+validate_family (int family)
+{
+ /* FIXME: Support more families. */
+#if HAVE_IPV4
+ if (family == PF_INET)
+ return true;
+#endif
+#if HAVE_IPV6
+ if (family == PF_INET6)
+ return true;
+#endif
+ if (family == PF_UNSPEC)
+ return true;
+ return false;
+}
+
+/* Translate name of a service location and/or a service name to set of
+ socket addresses. */
+int
+getaddrinfo (const char *restrict nodename,
+ const char *restrict servname,
+ const struct addrinfo *restrict hints,
+ struct addrinfo **restrict res)
+{
+ struct addrinfo *tmp;
+ struct servent *se;
+ struct hostent *he;
+ size_t sinlen;
+
+ if (hints && hints->ai_flags)
+ /* FIXME: Support more flags. */
+ return EAI_BADFLAGS;
+
+ if (hints && !validate_family (hints->ai_family))
+ return EAI_FAMILY;
+
+ if (hints && hints->ai_socktype)
+ /* FIXME: Support more socket types. */
+ return EAI_SOCKTYPE;
+
+ if (hints &&
+ hints->ai_protocol != SOCK_STREAM && hints->ai_protocol != SOCK_DGRAM)
+ /* FIXME: Support other protocols. */
+ return EAI_SERVICE; /* FIXME: Better return code? */
+
+ if (!nodename)
+ /* FIXME: Support server bind mode. */
+ return EAI_NONAME;
+
+ if (servname)
+ {
+ const char *proto =
+ (hints && hints->ai_protocol == SOCK_DGRAM) ? "udp" : "tcp";
+
+ /* FIXME: Use getservbyname_r if available. */
+ se = getservbyname (servname, proto);
+
+ if (!se)
+ return EAI_SERVICE;
+ }
+
+ /* FIXME: Use gethostbyname_r if available. */
+ he = gethostbyname (nodename);
+ if (!he || he->h_addr_list[0] == NULL)
+ return EAI_NONAME;
+
+ switch (he->h_addrtype)
+ {
+#if HAVE_IPV6
+ case PF_INET6:
+ sinlen = sizeof (struct sockaddr_in6);
+ break;
+#endif
+
+#if HAVE_IPV4
+ case PF_INET:
+ sinlen = sizeof (struct sockaddr_in);
+ break;
+#endif
+
+ default:
+ return EAI_NODATA;
+ }
+
+ tmp = calloc (1, sizeof (*tmp) + sinlen);
+ if (!tmp)
+ return EAI_MEMORY;
+
+ switch (he->h_addrtype)
+ {
+#if HAVE_IPV6
+ case PF_INET6:
+ {
+ struct sockaddr_in6 *sinp = (void *) tmp + sizeof (*tmp);
+
+ if (se)
+ sinp->sin6_port = se->s_port;
+
+ if (he->h_length != sizeof (sinp->sin6_addr))
+ return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
+
+ memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
+
+ tmp->ai_addr = (struct sockaddr *) sinp;
+ tmp->ai_addrlen = sinlen;
+ }
+ break;
+#endif
+
+#if HAVE_IPV4
+ case PF_INET:
+ {
+ struct sockaddr_in *sinp = (void *) tmp + sizeof (*tmp);
+
+ if (se)
+ sinp->sin_port = se->s_port;
+
+ if (he->h_length != sizeof (sinp->sin_addr))
+ return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
+
+ memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
+
+ tmp->ai_addr = (struct sockaddr *) sinp;
+ tmp->ai_addrlen = sinlen;
+ }
+ break;
+#endif
+
+ default:
+ free (tmp);
+ return EAI_NODATA;
+ }
+
+ tmp->ai_addr->sa_family = he->h_addrtype;
+
+ /* FIXME: If more than one address, create linked list of addrinfo's. */
+
+ *res = tmp;
+
+ return 0;
+}
+
+/* Free `addrinfo' structure AI including associated storage. */
+void
+freeaddrinfo (struct addrinfo *ai)
+{
+ while (ai)
+ {
+ struct addrinfo *cur;
+
+ cur = ai;
+ ai = ai->ai_next;
+ free (cur);
+ }
+}
Index: m4/ChangeLog
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/ChangeLog,v
retrieving revision 1.591
diff -u -p -r1.591 ChangeLog
--- m4/ChangeLog 9 Nov 2004 20:54:49 -0000 1.591
+++ m4/ChangeLog 10 Nov 2004 14:52:41 -0000
@@ -1,3 +1,7 @@
+2004-11-08 Simon Josefsson <address@hidden>
+
+ * getaddrinfo.m4: New file.
+
2004-11-09 Paul Eggert <address@hidden>
* strftime.m4 (_gl_STRFTIME_PREREQS): Remove. Move its body to
Index: m4/getaddrinfo.m4
===================================================================
RCS file: m4/getaddrinfo.m4
diff -N m4/getaddrinfo.m4
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ m4/getaddrinfo.m4 10 Nov 2004 14:52:41 -0000
@@ -0,0 +1,20 @@
+# getaddrinfo.m4 serial 1
+dnl Copyright (C) 2004 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License. As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_GETADDRINFO],
+[
+ AC_REPLACE_FUNCS(getaddrinfo)
+ gl_PREREQ_GETADDRINFO
+])
+
+# Prerequisites of lib/getaddrinfo.h and lib/getaddrinfo.c.
+AC_DEFUN([gl_PREREQ_GETADDRINFO], [
+ AC_REQUIRE([gl_C_RESTRICT])
+ AC_REQUIRE([gl_SOCKET_FAMILIES])
+ AC_REQUIRE([AC_C_INLINE])
+])
Index: modules/getaddrinfo
===================================================================
RCS file: modules/getaddrinfo
diff -N modules/getaddrinfo
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ modules/getaddrinfo 10 Nov 2004 14:52:41 -0000
@@ -0,0 +1,26 @@
+Description:
+Get address information.
+
+Files:
+lib/getaddrinfo.h
+lib/getaddrinfo.c
+lib/gai_strerror.c
+m4/getaddrinfo.m4
+m4/sockpfaf.m4
+
+Depends-on:
+restrict
+gettext
+stdbool
+
+configure.ac:
+gl_GETADDRINFO
+
+Makefile.am:
+lib_SOURCES += getaddrinfo.h
+
+Include:
+"getaddrinfo.h"
+
+Maintainer:
+Simon Josefsson
- [Bug-gnulib] Re: getaddrinfo, (continued)
- Re: [Bug-gnulib] Re: getaddrinfo, Bruno Haible, 2004/11/09
- [Bug-gnulib] Re: getaddrinfo, Simon Josefsson, 2004/11/09
- [Bug-gnulib] Re: getaddrinfo, Bruno Haible, 2004/11/09
- [Bug-gnulib] Re: getaddrinfo, Simon Josefsson, 2004/11/09
- Re: [Bug-gnulib] Re: getaddrinfo, Bruno Haible, 2004/11/09
- [Bug-gnulib] Re: getaddrinfo, Simon Josefsson, 2004/11/09
- Re: [Bug-gnulib] Re: getaddrinfo, Paul Eggert, 2004/11/09
- Re: [Bug-gnulib] Re: getaddrinfo, Paul Eggert, 2004/11/09
- Re: [Bug-gnulib] Re: getaddrinfo, Bruno Haible, 2004/11/10
- [Bug-gnulib] Re: getaddrinfo,
Simon Josefsson <=
- Re: [Bug-gnulib] Re: getaddrinfo, Bob Proulx, 2004/11/09
- [Bug-gnulib] Re: getaddrinfo, Simon Josefsson, 2004/11/09