1
0
mirror of https://github.com/golang/go synced 2024-10-05 18:31:28 -06:00
go/src/pkg/net/iprawsock.go

70 lines
1.5 KiB
Go
Raw Normal View History

// Copyright 2010 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.
// (Raw) IP sockets
package net
import (
"os"
)
// IPAddr represents the address of a IP end point.
type IPAddr struct {
IP IP
}
// Network returns the address's network name, "ip".
func (a *IPAddr) Network() string { return "ip" }
func (a *IPAddr) String() string {
if a == nil {
return "<nil>"
}
return a.IP.String()
}
// ResolveIPAddr parses addr as a IP address and resolves domain
// names to numeric addresses on the network net, which must be
// "ip", "ip4" or "ip6". A literal IPv6 host address must be
// enclosed in square brackets, as in "[::]".
func ResolveIPAddr(net, addr string) (*IPAddr, os.Error) {
ip, err := hostToIP(net, addr)
if err != nil {
return nil, err
}
return &IPAddr{ip}, nil
}
// Convert "host" into IP address.
func hostToIP(net, host string) (ip IP, err os.Error) {
var addr IP
// Try as an IP address.
addr = ParseIP(host)
if addr == nil {
filter := anyaddr
if net != "" && net[len(net)-1] == '4' {
filter = ipv4only
}
if net != "" && net[len(net)-1] == '6' {
filter = ipv6only
}
// Not an IP address. Try as a DNS name.
addrs, err1 := LookupHost(host)
if err1 != nil {
err = err1
goto Error
}
undo CL 4557058 / b4c2ffae7034 Using the getaddrinfo order is only okay if we are smart enough to try multiple addresses in Dial. Since the code does not do that, we must make the right first choice, regardless of what getaddrinfo does, and more often that not that means using the IPv4 address, even on IPv6 systems. With the CL applied, gotest fails in package net on OS X. helix.cam=; gotest ... --- FAIL: net.TestDialGoogleIPv4 (1.05 seconds) -- 74.125.226.179:80 -- -- www.google.com:80 -- Dial("tcp", "", "www.google.com:80") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family -- 74.125.226.179:http -- -- www.google.com:http -- Dial("tcp", "", "www.google.com:http") = _, dial tcp [2001:4860:800f::69]:80: address family not supported by protocol family -- 074.125.226.179:0080 -- -- [::ffff:74.125.226.179]:80 -- -- [::ffff:4a7d:e2b3]:80 -- -- [0:0:0:0:0000:ffff:74.125.226.179]:80 -- -- [0:0:0:0:000000:ffff:74.125.226.179]:80 -- -- [0:0:0:0:0:ffff::74.125.226.179]:80 -- FAIL gotest: "./6.out" failed: exit status 1 ««« original CL description net: name-based destination address selection getaddrinfo() orders the addresses according to RFC 3484. This means when IPv6 is working on a host we get results like: []string = {"2001:4810::110", "66.117.47.214"} and when it's not working we get: []string = {"66.117.47.214", "2001:4810::110"} thus can drop firstFavoriteAddr. This also means /etc/gai.conf works on relevant systems. R=rsc, mikioh.mikioh CC=golang-dev https://golang.org/cl/4557058 »»» R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/4532101
2011-06-01 13:49:57 -06:00
addr = firstFavoriteAddr(filter, addrs)
if addr == nil {
// should not happen
err = &AddrError{"LookupHost returned no suitable address", addrs[0]}
goto Error
}
}
return addr, nil
Error:
return nil, err
}