Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(619)

Side by Side Diff: src/pkg/net/iprawsock.go

Issue 6842053: code review 6842053: net: consoldate literal target address into IP address ... (Closed)
Patch Set: diff -r 61dc42e35948 https://code.google.com/p/go Created 11 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2010 The Go Authors. All rights reserved. 1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Raw IP sockets 5 // Raw IP sockets
6 6
7 package net 7 package net
8 8
9 import (
10 "time"
11 )
12
13 // IPAddr represents the address of an IP end point. 9 // IPAddr represents the address of an IP end point.
14 type IPAddr struct { 10 type IPAddr struct {
15 IP IP 11 IP IP
16 Zone string // IPv6 scoped addressing zone 12 Zone string // IPv6 scoped addressing zone
17 } 13 }
18 14
19 // Network returns the address's network name, "ip". 15 // Network returns the address's network name, "ip".
20 func (a *IPAddr) Network() string { return "ip" } 16 func (a *IPAddr) Network() string { return "ip" }
21 17
22 func (a *IPAddr) String() string { 18 func (a *IPAddr) String() string {
23 if a == nil { 19 if a == nil {
24 return "<nil>" 20 return "<nil>"
25 } 21 }
26 return a.IP.String() 22 return a.IP.String()
27 } 23 }
28 24
29 // ResolveIPAddr parses addr as an IP address and resolves domain 25 // ResolveIPAddr parses addr as an IP address and resolves domain
30 // names to numeric addresses on the network net, which must be 26 // names to numeric addresses on the network net, which must be
31 // "ip", "ip4" or "ip6". A literal IPv6 host address must be 27 // "ip", "ip4" or "ip6". A literal IPv6 host address must be
32 // enclosed in square brackets, as in "[::]". 28 // enclosed in square brackets, as in "[::]".
33 func ResolveIPAddr(net, addr string) (*IPAddr, error) { 29 func ResolveIPAddr(net, addr string) (*IPAddr, error) {
34 » return resolveIPAddr(net, addr, noDeadline) 30 » afnet, _, err := parseDialNetwork(net)
35 }
36
37 func resolveIPAddr(net, addr string, deadline time.Time) (*IPAddr, error) {
38 » ip, err := hostToIP(net, addr, deadline)
39 if err != nil { 31 if err != nil {
40 return nil, err 32 return nil, err
41 } 33 }
42 » return &IPAddr{IP: ip}, nil 34 » ia, err := resolveInternetAddr(afnet, addr, noDeadline)
35 » if err != nil {
36 » » return nil, err
37 » }
38 » ipa, ok := ia.(*IPAddr)
39 » if !ok {
40 » » return nil, &ParseError{"IP address", net + " " + addr}
rsc 2012/11/26 16:07:26 Why does this need a special error? What is wrong
mikio 2012/11/26 16:27:51 yup, that makes unnecessary confusion. will drop.
41 » }
42 » return ipa, nil
43 } 43 }
44
45 // Convert "host" into IP address.
46 func hostToIP(net, host string, deadline time.Time) (ip IP, err error) {
47 var addr IP
48 // Try as an IP address.
49 addr = ParseIP(host)
50 if addr == nil {
51 filter := anyaddr
52 if net != "" && net[len(net)-1] == '4' {
53 filter = ipv4only
54 }
55 if net != "" && net[len(net)-1] == '6' {
56 filter = ipv6only
57 }
58 // Not an IP address. Try as a DNS name.
59 addrs, err1 := lookupHostDeadline(host, deadline)
60 if err1 != nil {
61 err = err1
62 goto Error
63 }
64 addr = firstFavoriteAddr(filter, addrs)
65 if addr == nil {
66 // should not happen
67 err = &AddrError{"LookupHost returned no suitable addres s", addrs[0]}
68 goto Error
69 }
70 }
71 return addr, nil
72 Error:
73 return nil, err
74 }
OLDNEW
« no previous file with comments | « src/pkg/net/dial.go ('k') | src/pkg/net/ipsock.go » ('j') | src/pkg/net/tcpsock.go » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b