2012-03-27 23:13:14 +00: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
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// IPAddr represents the address of an IP end point.
|
2012-03-27 23:13:14 +00:00
|
|
|
type IPAddr struct {
|
2014-09-21 17:33:12 +00:00
|
|
|
IP IP
|
|
|
|
Zone string // IPv6 scoped addressing zone
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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>"
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
if a.Zone != "" {
|
|
|
|
return a.IP.String() + "%" + a.Zone
|
|
|
|
}
|
2012-03-27 23:13:14 +00:00
|
|
|
return a.IP.String()
|
|
|
|
}
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
func (a *IPAddr) toAddr() Addr {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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".
|
2012-03-27 23:13:14 +00:00
|
|
|
func ResolveIPAddr(net, addr string) (*IPAddr, error) {
|
2014-09-21 17:33:12 +00:00
|
|
|
if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
|
|
|
|
net = "ip"
|
|
|
|
}
|
|
|
|
afnet, _, err := parseNetwork(net)
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
switch afnet {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
a, err := resolveInternetAddr(afnet, addr, noDeadline)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
return a.toAddr().(*IPAddr), nil
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|