From marka@isc.org  Mon May  5 22:26:56 2003
Return-Path: <marka@isc.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id C4B2837B404
	for <FreeBSD-gnats-submit@freebsd.org>; Mon,  5 May 2003 22:26:56 -0700 (PDT)
Received: from bsdi.dv.isc.org (c17249.carlnfd1.nsw.optusnet.com.au [210.49.138.109])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 2F94943FD7
	for <FreeBSD-gnats-submit@freebsd.org>; Mon,  5 May 2003 22:26:55 -0700 (PDT)
	(envelope-from marka@isc.org)
Received: from drugs.dv.isc.org (drugs.dv.isc.org [192.168.191.236])
	by bsdi.dv.isc.org (8.12.9/8.12.9) with ESMTP id h465Qrab068405
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 6 May 2003 15:26:53 +1000 (EST)
	(envelope-from marka@isc.org)
Received: from drugs.dv.isc.org (localhost [127.0.0.1])
	by drugs.dv.isc.org (8.12.9/8.12.9) with ESMTP id h465Qr1G037638
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 6 May 2003 15:26:53 +1000 (EST)
	(envelope-from marka@drugs.dv.isc.org)
Received: (from marka@localhost)
	by drugs.dv.isc.org (8.12.9/8.12.9/Submit) id h465QqT3037637;
	Tue, 6 May 2003 15:26:52 +1000 (EST)
	(envelope-from marka)
Message-Id: <200305060526.h465QqT3037637@drugs.dv.isc.org>
Date: Tue, 6 May 2003 15:26:52 +1000 (EST)
From: Mark Andrews <marka@isc.org>
Reply-To: Mark Andrews <marka@isc.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: getaddrinfo() is broken with numeric service
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         51827
>Category:       bin
>Synopsis:       [libc] [patch] getaddrinfo(3) is broken with numeric service
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    ume
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon May 05 22:30:09 PDT 2003
>Closed-Date:    Sun Apr 12 19:21:23 UTC 2009
>Last-Modified:  Sun Apr 12 19:21:23 UTC 2009
>Originator:     Mark Andrews
>Release:        FreeBSD 4.8-RC i386
>Organization:
ISC
>Environment:
System: FreeBSD drugs.dv.isc.org 4.8-RC FreeBSD 4.8-RC #6: Sun Mar 30 11:45:29 EST 2003 marka@drugs.dv.isc.org:/usr/obj/usr/src/sys/DRUGS i386


>Description:

	getaddrinfo() should accept numeric when ai_socktype is not
	specified in hint or hints is NULL.

	RFC 3493:

   If servname is null, the call shall return network-level addresses
   for the specified nodename.  If servname is not null, it is a null-
   terminated character string identifying the requested service.  This
   can be either a descriptive name or a numeric representation suitable
   for use with the address family or families.  If the specified
   address family is AF_INET, AF_INET6 or AF_UNSPEC, the service can be
   specified as a string specifying a decimal port number.

	While I havn't checked the posix spec nothing in RFC 3493
	indicates that numeric values are only to be accepted when
	ai_socktype is specified.

>How-To-Repeat:

% ./a.out
53/NULL
servname not supported for ai_socktype
53/hint
servname not supported for ai_socktype
domain/hint
28 2 17
2 2 17
28 1 6
2 1 6
domain/hint
28 2 17
2 2 17
28 1 6
2 1 6
% 

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

main() {
	int error;
	struct addrinfo *res, *res0;
	struct addrinfo hint;

	memset(&hint, 0, sizeof(hint));
	hint.ai_family = AF_UNSPEC;

	printf("53/NULL\n");
	error = getaddrinfo("rc.isc.org", "53", NULL,  &res0);

	if (error) {
		printf("%s\n", gai_strerror(error));
	} else {

		for (res = res0; res; res = res->ai_next) {
			printf("%d %u %u\n", res->ai_family, res->ai_socktype,
			       res->ai_protocol);
		}

		freeaddrinfo(res0);
	}

	printf("53/hint\n");
	error = getaddrinfo("rc.isc.org", "53", &hint,  &res0);

	if (error) {
		printf("%s\n", gai_strerror(error));
	} else {

		for (res = res0; res; res = res->ai_next) {
			printf("%d %u %u\n", res->ai_family, res->ai_socktype,
			       res->ai_protocol);
		}

		freeaddrinfo(res0);
	}

	printf("domain/hint\n");
	error = getaddrinfo("rc.isc.org", "domain", NULL,  &res0);

	if (error) {
		printf("%s\n", gai_strerror(error));
	} else {

		for (res = res0; res; res = res->ai_next) {
			printf("%d %u %u\n", res->ai_family, res->ai_socktype,
			       res->ai_protocol);
		}

		freeaddrinfo(res0);
	}

	printf("domain/hint\n");
	error = getaddrinfo("rc.isc.org", "domain", &hint,  &res0);

	if (error) {
		printf("%s\n", gai_strerror(error));
	} else {

		for (res = res0; res; res = res->ai_next) {
			printf("%d %u %u\n", res->ai_family, res->ai_socktype,
			       res->ai_protocol);
		}

		freeaddrinfo(res0);
	}
	exit(0);
}

>Fix:

	The following if from the BIND 8 source but should apply.

Index: lib/irs/getaddrinfo.c
===================================================================
RCS file: /proj/cvs/isc/bind8/src/lib/irs/getaddrinfo.c,v
retrieving revision 8.12
diff -u -r8.12 getaddrinfo.c
--- lib/irs/getaddrinfo.c	3 Dec 2002 05:26:49 -0000	8.12
+++ lib/irs/getaddrinfo.c	6 May 2003 05:10:26 -0000
@@ -987,7 +987,17 @@
 		allownumeric = 1;
 		break;
 	case ANY:
-		allownumeric = 0;
+		switch (ai->ai_family) {
+		case AF_INET:
+#ifdef AF_INET6
+		case AF_INET6:
+#endif
+			allownumeric = 1;
+			break;
+		default:
+			allownumeric = 0;
+			break;
+		}
 		break;
 	default:
 		return EAI_SOCKTYPE;
>Release-Note:
>Audit-Trail:

From: Bruce Cran <bruce@cran.org.uk>
To: bug-followup@FreeBSD.org, marka@isc.org
Cc:  
Subject: Re: bin/51827: getaddrinfo() is broken with numeric service
Date: Mon, 05 May 2008 01:12:56 +0100

 This is still a problem on 7.0-RELEASE.  In the test code the host 
 rc.isc.org no longer exists and needs to be replaced by one which does. 
   However, once a valid host is specified the same output from the 
 program is seen.
 
 -- 
 Bruce
Responsible-Changed-From-To: freebsd-bugs->freebsd-net 
Responsible-Changed-By: brucec 
Responsible-Changed-When: Mon Mar 23 21:34:21 UTC 2009 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=51827 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/51827: commit references a PR
Date: Tue, 24 Mar 2009 17:47:37 +0000 (UTC)

 Author: ume
 Date: Tue Mar 24 17:47:24 2009
 New Revision: 190382
 URL: http://svn.freebsd.org/changeset/base/190382
 
 Log:
   getaddrinfo(3) should accept numeric when ai_socktype is not
   specified in hint or hints is NULL.
   
   PR:		bin/51827
   Submitted by:	Mark Andrews <marka__at__isc.org>
   MFC after:	1 week
 
 Modified:
   head/lib/libc/net/getaddrinfo.c
 
 Modified: head/lib/libc/net/getaddrinfo.c
 ==============================================================================
 --- head/lib/libc/net/getaddrinfo.c	Tue Mar 24 17:22:10 2009	(r190381)
 +++ head/lib/libc/net/getaddrinfo.c	Tue Mar 24 17:47:24 2009	(r190382)
 @@ -1347,7 +1347,17 @@ get_port(struct addrinfo *ai, const char
  		allownumeric = 1;
  		break;
  	case ANY:
 -		allownumeric = 0;
 +		switch (ai->ai_family) {
 +		case AF_INET:
 +#ifdef AF_INET6
 +		case AF_INET6:
 +#endif
 +			allownumeric = 1;
 +			break;
 +		default:
 +			allownumeric = 0;
 +			break;
 +		}
  		break;
  	default:
  		return EAI_SOCKTYPE;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: ume 
State-Changed-When: Tue Mar 24 17:51:47 UTC 2009 
State-Changed-Why:  
Thank you!  I've just committed it into HEAD. 
I'll MFC it after 1 week. 


Responsible-Changed-From-To: freebsd-net->ume 
Responsible-Changed-By: ume 
Responsible-Changed-When: Tue Mar 24 17:51:47 UTC 2009 
Responsible-Changed-Why:  
Thank you!  I've just committed it into HEAD. 
I'll MFC it after 1 week. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=51827 

From: Lucius Windschuh <lwindschuh@googlemail.com>
To: bug-followup@freebsd.org, Mark Andrews <marka@isc.org>
Cc:  
Subject: Re: bin/51827: [libc] [patch] getaddrinfo(3) is broken with numeric 
	service
Date: Fri, 27 Mar 2009 21:50:20 +0100

 I ran into the described problem with utorrent under wine (no tracker
 connections possible). With the current FreeBSD HEAD, utorrent works
 like a charme and connects to trackers.
 
 The output of Mark's test C file is now:
 $ ./test.c
 53/NULL
 28 2 17
 28 1 6
 2 2 17
 2 1 6
 53/hint
 28 2 17
 28 1 6
 2 2 17
 2 1 6
 domain/hint
 28 2 17
 28 1 6
 2 2 17
 2 1 6
 domain/hint
 28 2 17
 28 1 6
 2 2 17
 2 1 6
 
 
 for a name that exists (www.kame.net instead of rc.isc.org).
 This should be the expected behaviour.
 
 Is this enough evidence to close this PR? :-)
State-Changed-From-To: patched->closed 
State-Changed-By: ume 
State-Changed-When: Sun Apr 12 19:19:49 UTC 2009 
State-Changed-Why:  
I've MFC'ed it into RELENG_7.  Thanks again. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=51827 
>Unformatted:
