~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/net/iprawsock.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
 
5
 
// (Raw) IP sockets
6
 
 
7
5
package net
8
6
 
9
 
// IPAddr represents the address of a IP end point.
 
7
// IPAddr represents the address of an IP end point.
10
8
type IPAddr struct {
11
 
        IP IP
 
9
        IP   IP
 
10
        Zone string // IPv6 scoped addressing zone
12
11
}
13
12
 
14
13
// Network returns the address's network name, "ip".
18
17
        if a == nil {
19
18
                return "<nil>"
20
19
        }
 
20
        if a.Zone != "" {
 
21
                return a.IP.String() + "%" + a.Zone
 
22
        }
21
23
        return a.IP.String()
22
24
}
23
25
 
24
 
// ResolveIPAddr parses addr as a IP address and resolves domain
25
 
// names to numeric addresses on the network net, which must be
26
 
// "ip", "ip4" or "ip6".  A literal IPv6 host address must be
27
 
// enclosed in square brackets, as in "[::]".
 
26
// ResolveIPAddr parses addr as an IP address of the form "host" or
 
27
// "ipv6-host%zone" and resolves the domain name on the network net,
 
28
// which must be "ip", "ip4" or "ip6".
28
29
func ResolveIPAddr(net, addr string) (*IPAddr, error) {
29
 
        ip, err := hostToIP(net, addr)
30
 
        if err != nil {
31
 
                return nil, err
32
 
        }
33
 
        return &IPAddr{ip}, nil
34
 
}
35
 
 
36
 
// Convert "host" into IP address.
37
 
func hostToIP(net, host string) (ip IP, err error) {
38
 
        var addr IP
39
 
        // Try as an IP address.
40
 
        addr = ParseIP(host)
41
 
        if addr == nil {
42
 
                filter := anyaddr
43
 
                if net != "" && net[len(net)-1] == '4' {
44
 
                        filter = ipv4only
45
 
                }
46
 
                if net != "" && net[len(net)-1] == '6' {
47
 
                        filter = ipv6only
48
 
                }
49
 
                // Not an IP address.  Try as a DNS name.
50
 
                addrs, err1 := LookupHost(host)
51
 
                if err1 != nil {
52
 
                        err = err1
53
 
                        goto Error
54
 
                }
55
 
                addr = firstFavoriteAddr(filter, addrs)
56
 
                if addr == nil {
57
 
                        // should not happen
58
 
                        err = &AddrError{"LookupHost returned no suitable address", addrs[0]}
59
 
                        goto Error
60
 
                }
61
 
        }
62
 
        return addr, nil
63
 
Error:
64
 
        return nil, err
 
30
        if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
 
31
                net = "ip"
 
32
        }
 
33
        afnet, _, err := parseNetwork(net)
 
34
        if err != nil {
 
35
                return nil, err
 
36
        }
 
37
        switch afnet {
 
38
        case "ip", "ip4", "ip6":
 
39
        default:
 
40
                return nil, UnknownNetworkError(net)
 
41
        }
 
42
        a, err := resolveInternetAddr(afnet, addr, noDeadline)
 
43
        if err != nil {
 
44
                return nil, err
 
45
        }
 
46
        return a.(*IPAddr), nil
65
47
}