This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

problems with getaddrinfo() and _res.options



Hi,

	few months ago I had problems with getaddrinfo()
returning 0.0.0.0 _sometimes_. Now I know where problem is ...

Problem is when program is doing for example:

	_res_init();
	_res.options |= RES_USE_INET6;
	...
	getaddrinfo();
	...

IMHO getaddrinfo() should set it own resolver options before
calling __gethostbyname2_r() to avoid problems with resolver options.

Testing program:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <resolv.h>

int main(int argc,char *argv[])
{

  struct addrinfo hints, *ai, *aitop;
  int gaierr;
  char addr[NI_MAXHOST], addrip[NI_MAXHOST];
 
  if (argc <= 1 || argc > 2) {
	  fprintf(stderr, "Usage:\t%s\t<host or ip addr>\n", argv[0]);
	  exit(1);
  }

  res_init();
  _res.options |= RES_USE_INET6;
  printf("%ul\n", _res.options);

  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  fprintf(stdout, "Resolving ... %s\n", argv[1]);
  if (!(gaierr = getaddrinfo(argv[1], NULL, &hints, &aitop)))
  {
	  for (ai = aitop; ai; ai = ai->ai_next) {
		  if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
			  continue;
		  if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
				  sizeof(addr), NULL, 0, NI_NAMEREQD))
			  strcpy(addr, "unknown");
		  if (getnameinfo(ai->ai_addr, ai->ai_addrlen, addrip,
				  sizeof(addrip), NULL, 0, NI_NUMERICHOST))
			  strcpy(addrip, "unknown");
		  fprintf(stdout, "fqdn: %s\tip addr: %s\n", addr, addrip);
	  }
	  freeaddrinfo(aitop);
  } else {
	  fprintf(stdout, "%s failed: %s\n", argv[1],
			  gai_strerror(gaierr));
	  exit(1);
  }
  exit(0);
}


simpy (but I'm not sure if proper) workaround:

diff -urN glibc-2.1.3.org/sysdeps/posix/getaddrinfo.c glibc-2.1.3/sysdeps/posix/getaddrinfo.c
--- glibc-2.1.3.org/sysdeps/posix/getaddrinfo.c	Sun May 21 20:12:39 2000
+++ glibc-2.1.3/sysdeps/posix/getaddrinfo.c	Sun May 21 20:30:13 2000
@@ -56,6 +56,7 @@
 #include <netdb.h>
 #include <errno.h>
 #include <arpa/inet.h>
+#include <resolv.h>
 
 #define GAIH_OKIFUNSPEC 0x0100
 #define GAIH_EAI        ~(GAIH_OKIFUNSPEC)
@@ -254,16 +255,23 @@
 #define gethosts(_family, _type)				\
  {								\
   int i, herrno;						\
+  u_long resopt;						\
   size_t tmpbuflen;						\
   struct hostent th;						\
   char *tmpbuf;							\
   tmpbuflen = 512;						\
+  if (_res.options) {						\
+    resopt = _res.options;					\
+    _res.options = RES_DEFAULT;					\
+  }								\
   do {								\
     tmpbuflen *= 2;						\
     tmpbuf = __alloca (tmpbuflen);				\
     rc = __gethostbyname2_r (name, _family, &th, tmpbuf,	\
          tmpbuflen, &h, &herrno);				\
   } while (rc == ERANGE && herrno == NETDB_INTERNAL);		\
+  if (_res.options)						\
+    _res.options = resopt;					\
   if (rc != 0 && herrno == NETDB_INTERNAL)			\
     {								\
       __set_h_errno (herrno);					\


-- 
Arkadiusz Miśkiewicz         http://www.misiek.eu.org/
PLD GNU/Linux [IPv6 enabled]  http://www.pld.org.pl/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]