2010-12-03 05:34:57 +01: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-10-03 07:27:36 +02:00
|
|
|
// IPAddr represents the address of an IP end point.
|
2010-12-03 05:34:57 +01:00
|
|
|
type IPAddr struct {
|
2012-12-13 00:13:29 +01:00
|
|
|
IP IP
|
|
|
|
Zone string // IPv6 scoped addressing zone
|
2010-12-03 05:34:57 +01: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>"
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if a.Zone != "" {
|
|
|
|
return a.IP.String() + "%" + a.Zone
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
return a.IP.String()
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func (a *IPAddr) toAddr() Addr {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02: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-12-03 03:17:34 +01:00
|
|
|
func ResolveIPAddr(net, addr string) (*IPAddr, error) {
|
2012-12-13 00:13:29 +01:00
|
|
|
if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
|
|
|
|
net = "ip"
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
afnet, _, err := parseNetwork(net)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-12-13 00:13:29 +01:00
|
|
|
switch afnet {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
a, err := resolveInternetAddr(afnet, addr, noDeadline)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return a.toAddr().(*IPAddr), nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|