Retro68/gcc/libgo/go/net/iprawsock.go

63 lines
1.4 KiB
Go
Raw Normal View History

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>"
}
2017-04-10 11:32:00 +00:00
ip := ipEmptyString(a.IP)
2014-09-21 17:33:12 +00:00
if a.Zone != "" {
2017-04-10 11:32:00 +00:00
return ip + "%" + a.Zone
2014-09-21 17:33:12 +00:00
}
2017-04-10 11:32:00 +00:00
return ip
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
func (a *IPAddr) isWildcard() bool {
if a == nil || a.IP == nil {
return true
}
return a.IP.IsUnspecified()
}
func (a *IPAddr) opAddr() Addr {
2014-09-21 17:33:12 +00:00
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)
}
2017-04-10 11:32:00 +00:00
addrs, err := internetAddrList(afnet, addr, noDeadline)
2014-09-21 17:33:12 +00:00
if err != nil {
return nil, err
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return addrs.first(isIPv4).(*IPAddr), nil
2012-03-27 23:13:14 +00:00
}