Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: different lists of addresses are returned depending on order of multiple aliases in /etc/hosts on Mac OS with CGO #24514

Open
murphyke opened this issue Mar 23, 2018 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@murphyke
Copy link

TL;DR - net.LookupIP may return a different list of addresses depending on the order of multiple names in an /etc/hosts mapping (e.g. 127.0.0.1 my.local localhost). I don't know if this should be surprising or not; however, I did have a NATS client as well as an official NATS sample client malfunction on me, and I wondered what the problem might be.

What version of Go are you using (go version)?

go version go1.10 darwin/amd64

Does this issue reproduce with the latest release?

On latest.

What operating system and processor architecture are you using (go env)?

darwin amd64

What did you do?

  • With the following test code:
package main

import (
  "fmt"
  "net"
)

func main() {
  ips, _ := net.LookupIP("localhost")
  fmt.Printf("IPs: %v\n", ips)
}
  • With the following /etc/hosts:
127.0.0.1	localhost my.local       # This line will be varied in the tests below
255.255.255.255	broadcasthost
::1             localhost
  • Run the test program four times while varying 1) the resolver and 2) the order of localhost and my.local in /etc/hosts. (After changing /etc/hosts, run sudo killall -HUP mDNSResponder).
# localhost FIRST (`127.0.0.1 localhost my.local`) and pure GO resolver:
$ GODEBUG=netdns=go+1 go run junk.go
go package net: GODEBUG setting forcing use of Go's resolver
IPs: [::1 127.0.0.1]
# localhost FIRST (`127.0.0.1 localhost my.local`) and CGO resolver:
$ GODEBUG=netdns=1 go run junk.go
go package net: using cgo DNS resolver
IPs: [::1 127.0.0.1]
# localhost SECOND (`127.0.0.1 my.local localhost`) and pure GO resolver:
$ GODEBUG=netdns=go+1 go run junk.go
go package net: GODEBUG setting forcing use of Go's resolver
IPs: [::1 127.0.0.1]
# localhost SECOND (`127.0.0.1 my.local localhost`) and CGO resolver:
$ GODEBUG=netdns=1 go run junk.go
go package net: using cgo DNS resolver
IPs: [127.0.0.1]                # <---- This is the oddball; no IPv6 address

What did you expect to see?

I expected the returned set of IPs to be the same in all four tests.

What did you see instead?

The combination of localhost-last and cgo caused a different set of addresses to be resolved.

@gopherbot gopherbot added this to the Unreleased milestone Mar 23, 2018
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 24, 2018
@andybons
Copy link
Member

@ianlancetaylor

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Mar 24, 2018

RFC 2782 says that we should return the addresses in pseudo-random order (see the discussion of "weight"). I haven't looked closely but I suspect that is what is happening here.

@liuhongyi0101
Copy link

IPs: [::1 127.0.0.1]

@odeke-em odeke-em changed the title x/net: question about cgo with multiple aliases in /etc/hosts on Mac OS net: different lists of addresses are returned depending on order of multiple aliases in /etc/hosts on Mac OS with CGO Mar 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants