getaddrinfo performance problems

Ed W lists at wildgooses.com
Mon Apr 16 22:27:55 UTC 2012


On 16/04/2012 23:02, Ed W wrote:
> On 16/04/2012 18:32, Rich Felker wrote:
>> On Mon, Apr 16, 2012 at 05:57:47PM +0100, Ed W wrote:
>>> Rich, disregarding posix for a moment, what does glibc do?  Options
>>> are: canonname=NULL  or IP as string?
>> If the AI_CANONNAME flag is not specified, the value of the
>> ai_canonname field in the returned structure is unspecified; in this
>> case it should not be filled in at all unless there's no cost to
>> filling it (IIRC musl always fills it because filling it is free if
>> done right).
>>
>>> At present uclibc is returning the reverse dns lookup or textual IP
>>> format.  It should be trivial for me to supply a patch to make this
>>> whatever we want - can someone define what we want? (Posix or
>>> emulate some other wierdness?)
>> Per POSIX:
>>
>>      If nodename is not null, and if requested by the AI_CANONNAME
>>      flag, the ai_canonname field of the first returned addrinfo
>>      structure shall point to a null-terminated string containing the
>>      canonical name corresponding to the input nodename; if the
>>      canonical name is not available, then ai_canonname shall refer to
>>      the nodename argument or a string with the same contents. The
>>      contents of the ai_flags field of the returned structures are
>>      undefined.
>>
>> My reading is that in the numeric case, since there is no canonical
>> name, the text about storing a pointer to the input nodename, or a
>> duplicate of the same, applies.
>>
>
> OK, please see attached version 2 of my patch which I believe 
> implements this satisfactorarily
>
> As before the motivation is to avoid making DNS queries when calling 
> getaddrinfo with a numerical IP address.  Performance improves 
> dramatically in such situations
>
> Grateful if others with more experience will verify and commit

Sorry, typo - s/NUMERICSERV/NUMERICHOST/

Patch re-attached v3

Ed W
-------------- next part --------------
Posix says that canonname should use the text representation of an IP address
when a numerical nodename given
See: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html

Signed-off-by: Ed Wildgoose <lists at wildgooses.com>
---

--- libc/inet/getaddrinfo.c.orig	2012-04-16 18:28:05.000000000 +0100
+++ libc/inet/getaddrinfo.c	2012-04-16 22:51:58.000000000 +0100
@@ -628,13 +628,20 @@
 		char buffer[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
 
 		while (at2 != NULL) {
-			if (req->ai_flags & AI_CANONNAME) {
+			c = inet_ntop(at2->family, at2->addr, buffer, sizeof(buffer));
+			if (c) {
+				namelen = strlen(c) + 1;
+			} else if (req->ai_flags & AI_CANONNAME) {
 				struct hostent *h = NULL;
 				int herrno;
 				struct hostent th;
 				size_t tmpbuflen = 512;
 				char *tmpbuf;
 
+                /* Hint says numeric, but address is not */
+                if (req->ai_flags & AI_NUMERICHOST)
+                    return -EAI_NONAME;
+
 				do {
 					tmpbuflen *= 2;
 					tmpbuf = alloca(tmpbuflen);
@@ -656,9 +663,7 @@
 					return -EAI_SYSTEM;
 				}
 
-				if (h == NULL)
-					c = inet_ntop(at2->family, at2->addr, buffer, sizeof(buffer));
-				else
+				if (h != NULL)
 					c = h->h_name;
 
 				if (c == NULL)


More information about the uClibc mailing list