2010-05-21 18:30:40 -06:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-07-19 17:32:25 -06:00
|
|
|
// IPAddr represents the address of an IP end point.
|
2010-05-21 18:30:40 -06:00
|
|
|
type IPAddr struct {
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
IP IP
|
|
|
|
Zone string // IPv6 scoped addressing zone
|
2010-05-21 18:30:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the address's network name, "ip".
|
|
|
|
func (a *IPAddr) Network() string { return "ip" }
|
|
|
|
|
2010-07-27 01:22:22 -06:00
|
|
|
func (a *IPAddr) String() string {
|
|
|
|
if a == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
if a.Zone != "" {
|
|
|
|
return a.IP.String() + "%" + a.Zone
|
|
|
|
}
|
2010-07-27 01:22:22 -06:00
|
|
|
return a.IP.String()
|
|
|
|
}
|
2010-05-21 18:30:40 -06:00
|
|
|
|
2015-03-26 08:26:45 -06:00
|
|
|
func (a *IPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
2013-08-29 18:09:45 -06:00
|
|
|
func (a *IPAddr) toAddr() Addr {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
// ResolveIPAddr parses addr as an IP address of the form "host" or
|
|
|
|
// "ipv6-host%zone" and resolves the domain name on the network net,
|
|
|
|
// which must be "ip", "ip4" or "ip6".
|
2011-11-01 20:05:34 -06:00
|
|
|
func ResolveIPAddr(net, addr string) (*IPAddr, error) {
|
2012-11-30 22:49:54 -07:00
|
|
|
if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
|
|
|
|
net = "ip"
|
|
|
|
}
|
2013-02-08 05:53:10 -07:00
|
|
|
afnet, _, err := parseNetwork(net)
|
2010-05-21 18:30:40 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
switch afnet {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
2010-05-21 18:30:40 -06:00
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
a, err := resolveInternetAddr(afnet, addr, noDeadline)
|
2012-11-27 16:45:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-29 18:09:45 -06:00
|
|
|
return a.toAddr().(*IPAddr), nil
|
2010-05-21 18:30:40 -06:00
|
|
|
}
|