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

Delta Between Two Patch Sets: src/pkg/net/iprawsock.go

Issue 6849045: code review 6849045: net, cmd/fix: add IPv6 scoped addressing zone to INET, ... (Closed)
Left Patch Set: diff -r fc4e67d82024 https://code.google.com/p/go Created 11 years, 4 months ago
Right Patch Set: diff -r c200281fac50 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/net/ip_test.go ('k') | src/pkg/net/iprawsock_plan9.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 1 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 2 // license that can be found in the LICENSE file.
4 3
5 // Raw IP sockets 4 // (Raw) IP sockets
6 5
7 package net 6 package net
8 7
9 import (
10 "time"
11 )
12
13 // IPAddr represents the address of an IP end point.
14 type IPAddr struct {
15 IP IP
16 ZoneId string // IPv6 scoped addressing zone identifier
17 }
18
19 // Network returns the address's network name, "ip".
20 func (a *IPAddr) Network() string { return "ip" }
21
22 func (a *IPAddr) String() string {
23 if a == nil {
24 return "<nil>"
25 }
26 return a.IP.String()
27 }
28
29 // ResolveIPAddr parses addr as an IP address and resolves domain
30 // names to numeric addresses on the network net, which must be
31 // "ip", "ip4" or "ip6". A literal IPv6 host address must be
32 // enclosed in square brackets, as in "[::]".
33 func ResolveIPAddr(net, addr string) (*IPAddr, error) {
34 return resolveIPAddr(net, addr, noDeadline)
35 }
36
37 func resolveIPAddr(net, addr string, deadline time.Time) (*IPAddr, error) {
38 ip, err := hostToIP(net, addr, deadline)
39 if err != nil {
40 return nil, err
41 }
42 return &IPAddr{IP: ip}, nil
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 }
LEFTRIGHT

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