]> Cypherpunks repositories - gostls13.git/commitdiff
net: make LookupIP("1.2.3.4") behavior consistent
authorRuss Cox <rsc@golang.org>
Wed, 24 Jun 2015 12:39:44 +0000 (08:39 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 24 Jun 2015 15:16:36 +0000 (15:16 +0000)
To date, the behavior has depended on whether we're using cgo and
in turn what the host resolver does. Most host resolvers will "resolve"
IP addresses, but the non-cgo pure Go path has not.
This CL makes resolution of IP addresses always work, even if we're not using cgo
and even if the host resolver does not "resolve" IP addresses.

Fixes #11335.

Change-Id: I19e82be968154d94904bb2f72e9c17893019a909
Reviewed-on: https://go-review.googlesource.com/11420
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/ip_test.go
src/net/lookup.go

index b1939cd08f00b45da7a62345e5ceee9fecbb61dd..9c831d74b32aff716c70d22dae8f52c23d223701 100644 (file)
@@ -52,6 +52,47 @@ func TestParseIP(t *testing.T) {
        }
 }
 
+func TestLookupWithIP(t *testing.T) {
+       _, err := LookupIP("")
+       if err == nil {
+               t.Errorf(`LookupIP("") succeeded, should fail`)
+       }
+       _, err = LookupHost("")
+       if err == nil {
+               t.Errorf(`LookupIP("") succeeded, should fail`)
+       }
+
+       // Test that LookupHost and LookupIP, which normally
+       // expect host names, work with IP addresses.
+       for _, tt := range parseIPTests {
+               addrs, err := LookupHost(tt.in)
+               if tt.out != nil {
+                       if len(addrs) != 1 || addrs[0] != tt.in || err != nil {
+                               t.Errorf("LookupHost(%q) = %v, %v, want %v, nil", tt.in, addrs, err, []string{tt.in})
+                       }
+               } else {
+                       // We can't control what the host resolver does; if it can resolve, say,
+                       // 127.0.0.256 or fe80::1%911 or a host named 'abc', who are we to judge?
+                       // Warn about these discrepancies but don't fail the test.
+                       if err == nil {
+                               t.Logf("warning: LookupHost(%q) = %v, want error", tt.in, addrs)
+                       }
+               }
+
+               ips, err := LookupIP(tt.in)
+               if tt.out != nil {
+                       if len(ips) != 1 || !reflect.DeepEqual(ips[0], tt.out) || err != nil {
+                               t.Errorf("LookupIP(%q) = %v, %v, want %v, nil", tt.in, ips, err, []IP{tt.out})
+                       }
+               } else {
+                       // We can't control what the host resolver does. See above.
+                       if err == nil {
+                               t.Logf("warning: LookupIP(%q) = %v, want error", tt.in, ips)
+                       }
+               }
+       }
+}
+
 func BenchmarkParseIP(b *testing.B) {
        testHookUninstaller.Do(uninstallTestHooks)
 
index e2becc5a90cfaefcd468234900f3dd0924e7bc72..a7ceee823f17592e23a0cbeac406f92e2e24fb7b 100644 (file)
@@ -25,12 +25,28 @@ var protocols = map[string]int{
 // LookupHost looks up the given host using the local resolver.
 // It returns an array of that host's addresses.
 func LookupHost(host string) (addrs []string, err error) {
+       // Make sure that no matter what we do later, host=="" is rejected.
+       // ParseIP, for example, does accept empty strings.
+       if host == "" {
+               return nil, &DNSError{Err: errNoSuchHost.Error(), Name: host}
+       }
+       if ip := ParseIP(host); ip != nil {
+               return []string{host}, nil
+       }
        return lookupHost(host)
 }
 
 // LookupIP looks up host using the local resolver.
 // It returns an array of that host's IPv4 and IPv6 addresses.
 func LookupIP(host string) (ips []IP, err error) {
+       // Make sure that no matter what we do later, host=="" is rejected.
+       // ParseIP, for example, does accept empty strings.
+       if host == "" {
+               return nil, &DNSError{Err: errNoSuchHost.Error(), Name: host}
+       }
+       if ip := ParseIP(host); ip != nil {
+               return []IP{ip}, nil
+       }
        addrs, err := lookupIPMerge(host)
        if err != nil {
                return