Skip to content

Commit

Permalink
net: use C library resolver on FreeBSD, Linux, OS X / amd64, 386
Browse files Browse the repository at this point in the history
This CL makes it possible to resolve DNS names on OS X
without offending the Application-Level Firewall.

It also means that cross-compiling from one operating
system to another is no longer possible when using
package net, because cgo needs to be able to sniff around
the local C libraries.  We could special-case this one use
and check in generated files, but it seems more trouble
than it's worth.  Cross compiling is dead anyway.

It is still possible to use either GOARCH=amd64 or GOARCH=386
on typical Linux and OS X x86 systems.

It is also still possible to build GOOS=linux GOARCH=arm on
any system, because arm is for now excluded from this change
(there is no cgo for arm yet).

R=iant, r, mikioh
CC=golang-dev
https://golang.org/cl/4437053
  • Loading branch information
rsc committed Apr 20, 2011
1 parent 64787e3 commit c9164a5
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 34 deletions.
19 changes: 18 additions & 1 deletion src/pkg/net/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ include ../../Make.inc

TARG=net
GOFILES=\
cgo_stub.go\
dial.go\
dnsmsg.go\
fd_$(GOOS).go\
Expand All @@ -31,13 +30,19 @@ GOFILES_freebsd=\
dnsclient.go\
port.go\

CGOFILES_freebsd=\
cgo_unix.go\

GOFILES_darwin=\
newpollserver.go\
fd.go\
file.go\
dnsconfig.go\
dnsclient.go\
port.go\

CGOFILES_darwin=\
cgo_unix.go\

GOFILES_linux=\
newpollserver.go\
Expand All @@ -47,10 +52,22 @@ GOFILES_linux=\
dnsclient.go\
port.go\

ifeq ($(GOARCH),arm)
# ARM has no cgo, so use the stubs.
GOFILES_linux+=cgo_stub.go
else
CGOFILES_linux=\
cgo_unix.go
endif

GOFILES_windows=\
cgo_stub.go\
resolv_windows.go\
file_windows.go\

GOFILES+=$(GOFILES_$(GOOS))
ifneq ($(CGOFILES_$(GOOS)),)
CGOFILES+=$(CGOFILES_$(GOOS))
endif

include ../../Make.pkg
4 changes: 4 additions & 0 deletions src/pkg/net/cgo_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ func cgoLookupPort(network, service string) (port int, err os.Error, completed b
func cgoLookupIP(name string) (addrs []IP, err os.Error, completed bool) {
return nil, nil, false
}

func cgoLookupCNAME(name string) (cname string, err os.Error, completed bool) {
return "", nil, false
}
148 changes: 148 additions & 0 deletions src/pkg/net/cgo_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

/*
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
*/
import "C"

import (
"os"
"syscall"
"unsafe"
)

func cgoLookupHost(name string) (addrs []string, err os.Error, completed bool) {
ip, err, completed := cgoLookupIP(name)
for _, p := range ip {
addrs = append(addrs, p.String())
}
return
}

func cgoLookupPort(net, service string) (port int, err os.Error, completed bool) {
var res *C.struct_addrinfo
var hints C.struct_addrinfo

switch net {
case "":
// no hints
case "tcp", "tcp4", "tcp6":
hints.ai_socktype = C.SOCK_STREAM
hints.ai_protocol = C.IPPROTO_TCP
case "udp", "udp4", "udp6":
hints.ai_socktype = C.SOCK_DGRAM
hints.ai_protocol = C.IPPROTO_UDP
default:
return 0, UnknownNetworkError(net), true
}
if len(net) >= 4 {
switch net[3] {
case '4':
hints.ai_family = C.AF_INET
case '6':
hints.ai_family = C.AF_INET6
}
}

s := C.CString(service)
defer C.free(unsafe.Pointer(s))
if C.getaddrinfo(nil, s, &hints, &res) == 0 {
defer C.freeaddrinfo(res)
for r := res; r != nil; r = r.ai_next {
switch r.ai_family {
default:
continue
case C.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
case C.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
p := (*[2]byte)(unsafe.Pointer(&sa.Port))
return int(p[0])<<8 | int(p[1]), nil, true
}
}
}
return 0, &AddrError{"unknown port", net + "/" + service}, true
}

func cgoLookupIPCNAME(name string) (addrs []IP, cname string, err os.Error, completed bool) {
var res *C.struct_addrinfo
var hints C.struct_addrinfo

// NOTE(rsc): In theory there are approximately balanced
// arguments for and against including AI_ADDRCONFIG
// in the flags (it includes IPv4 results only on IPv4 systems,
// and similarly for IPv6), but in practice setting it causes
// getaddrinfo to return the wrong canonical name on Linux.
// So definitely leave it out.
hints.ai_flags = C.AI_ALL | C.AI_V4MAPPED | C.AI_CANONNAME

h := C.CString(name)
defer C.free(unsafe.Pointer(h))
gerrno, err := C.getaddrinfo(h, nil, &hints, &res)
if gerrno != 0 {
var str string
if gerrno == C.EAI_NONAME {
str = noSuchHost
} else if gerrno == C.EAI_SYSTEM {
str = err.String()
} else {
str = C.GoString(C.gai_strerror(gerrno))
}
return nil, "", &DNSError{Error: str, Name: name}, true
}
defer C.freeaddrinfo(res)
if res != nil {
cname = C.GoString(res.ai_canonname)
if cname == "" {
cname = name
}
if len(cname) > 0 && cname[len(cname)-1] != '.' {
cname += "."
}
}
for r := res; r != nil; r = r.ai_next {
// Everything comes back twice, once for UDP and once for TCP.
if r.ai_socktype != C.SOCK_STREAM {
continue
}
switch r.ai_family {
default:
continue
case C.AF_INET:
sa := (*syscall.RawSockaddrInet4)(unsafe.Pointer(r.ai_addr))
addrs = append(addrs, copyIP(sa.Addr[:]))
case C.AF_INET6:
sa := (*syscall.RawSockaddrInet6)(unsafe.Pointer(r.ai_addr))
addrs = append(addrs, copyIP(sa.Addr[:]))
}
}
return addrs, cname, nil, true
}

func cgoLookupIP(name string) (addrs []IP, err os.Error, completed bool) {
addrs, _, err, completed = cgoLookupIPCNAME(name)
return
}

func cgoLookupCNAME(name string) (cname string, err os.Error, completed bool) {
_, cname, err, completed = cgoLookupIPCNAME(name)
return
}

func copyIP(x IP) IP {
y := make(IP, len(x))
copy(y, x)
return y
}
8 changes: 4 additions & 4 deletions src/pkg/net/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Dial(net, addr string) (c Conn, err os.Error) {
switch net {
case "tcp", "tcp4", "tcp6":
var ra *TCPAddr
if ra, err = ResolveTCPAddr(raddr); err != nil {
if ra, err = ResolveTCPAddr(net, raddr); err != nil {
goto Error
}
c, err := DialTCP(net, nil, ra)
Expand All @@ -40,7 +40,7 @@ func Dial(net, addr string) (c Conn, err os.Error) {
return c, nil
case "udp", "udp4", "udp6":
var ra *UDPAddr
if ra, err = ResolveUDPAddr(raddr); err != nil {
if ra, err = ResolveUDPAddr(net, raddr); err != nil {
goto Error
}
c, err := DialUDP(net, nil, ra)
Expand Down Expand Up @@ -83,7 +83,7 @@ func Listen(net, laddr string) (l Listener, err os.Error) {
case "tcp", "tcp4", "tcp6":
var la *TCPAddr
if laddr != "" {
if la, err = ResolveTCPAddr(laddr); err != nil {
if la, err = ResolveTCPAddr(net, laddr); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func ListenPacket(net, laddr string) (c PacketConn, err os.Error) {
case "udp", "udp4", "udp6":
var la *UDPAddr
if laddr != "" {
if la, err = ResolveUDPAddr(laddr); err != nil {
if la, err = ResolveUDPAddr(net, laddr); err != nil {
return nil, err
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/pkg/net/dialgoogle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,22 @@ func TestDialGoogle(t *testing.T) {
googleaddrs[len(googleaddrs)-1] = ""
}

// Insert an actual IP address for google.com
// Insert an actual IPv4 address for google.com
// into the table.

addrs, err := LookupIP("www.google.com")
if err != nil {
t.Fatalf("lookup www.google.com: %v", err)
}
if len(addrs) == 0 {
t.Fatalf("no addresses for www.google.com")
var ip IP
for _, addr := range addrs {
if x := addr.To4(); x != nil {
ip = x
break
}
}
if ip == nil {
t.Fatalf("no IPv4 addresses for www.google.com")
}
ip := addrs[0].To4()

for i, s := range googleaddrs {
if strings.Contains(s, "%") {
Expand Down
22 changes: 17 additions & 5 deletions src/pkg/net/dnsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro
}

// goLookupHost is the native Go implementation of LookupHost.
// Used only if cgoLookupHost refuses to handle the request
// (that is, only if cgoLookupHost is the stub in cgo_stub.go).
// Normally we let cgo use the C library resolver instead of
// depending on our lookup code, so that Go and C get the same
// answers.
func goLookupHost(name string) (addrs []string, err os.Error) {
onceLoadConfig.Do(loadConfig)
if dnserr != nil || cfg == nil {
Expand All @@ -330,6 +335,11 @@ func goLookupHost(name string) (addrs []string, err os.Error) {
}

// goLookupIP is the native Go implementation of LookupIP.
// Used only if cgoLookupIP refuses to handle the request
// (that is, only if cgoLookupIP is the stub in cgo_stub.go).
// Normally we let cgo use the C library resolver instead of
// depending on our lookup code, so that Go and C get the same
// answers.
func goLookupIP(name string) (addrs []IP, err os.Error) {
onceLoadConfig.Do(loadConfig)
if dnserr != nil || cfg == nil {
Expand Down Expand Up @@ -358,11 +368,13 @@ func goLookupIP(name string) (addrs []IP, err os.Error) {
return
}

// LookupCNAME returns the canonical DNS host for the given name.
// Callers that do not care about the canonical name can call
// LookupHost or LookupIP directly; both take care of resolving
// the canonical name as part of the lookup.
func LookupCNAME(name string) (cname string, err os.Error) {
// goLookupCNAME is the native Go implementation of LookupCNAME.
// Used only if cgoLookupCNAME refuses to handle the request
// (that is, only if cgoLookupCNAME is the stub in cgo_stub.go).
// Normally we let cgo use the C library resolver instead of
// depending on our lookup code, so that Go and C get the same
// answers.
func goLookupCNAME(name string) (cname string, err os.Error) {
onceLoadConfig.Do(loadConfig)
if dnserr != nil || cfg == nil {
err = dnserr
Expand Down
15 changes: 15 additions & 0 deletions src/pkg/net/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package net

import (
"sort"
"testing"
)

Expand Down Expand Up @@ -51,3 +52,17 @@ func TestLookupStaticHost(t *testing.T) {
}
hostsPath = p
}

func TestLookupHost(t *testing.T) {
// Can't depend on this to return anything in particular,
// but if it does return something, make sure it doesn't
// duplicate addresses (a common bug due to the way
// getaddrinfo works).
addrs, _ := LookupHost("localhost")
sort.SortStrings(addrs)
for i := 0; i+1 < len(addrs); i++ {
if addrs[i] == addrs[i+1] {
t.Fatalf("LookupHost(\"localhost\") = %v, has duplicate addresses", addrs)
}
}
}
2 changes: 1 addition & 1 deletion src/pkg/net/iprawsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func hostToIP(host string) (ip IP, err os.Error) {
err = err1
goto Error
}
addr = firstSupportedAddr(addrs)
addr = firstSupportedAddr(anyaddr, addrs)
if addr == nil {
// should not happen
err = &AddrError{"LookupHost returned invalid address", addrs[0]}
Expand Down
Loading

0 comments on commit c9164a5

Please sign in to comment.