github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/net/iprawsock.go (about)

     1  // Copyright 2010 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  // IPAddr represents the address of an IP end point.
     8  type IPAddr struct {
     9  	IP   IP
    10  	Zone string // IPv6 scoped addressing zone
    11  }
    12  
    13  // Network returns the address's network name, "ip".
    14  func (a *IPAddr) Network() string { return "ip" }
    15  
    16  func (a *IPAddr) String() string {
    17  	if a == nil {
    18  		return "<nil>"
    19  	}
    20  	ip := ipEmptyString(a.IP)
    21  	if a.Zone != "" {
    22  		return ip + "%" + a.Zone
    23  	}
    24  	return ip
    25  }
    26  
    27  func (a *IPAddr) isWildcard() bool {
    28  	if a == nil || a.IP == nil {
    29  		return true
    30  	}
    31  	return a.IP.IsUnspecified()
    32  }
    33  
    34  func (a *IPAddr) opAddr() Addr {
    35  	if a == nil {
    36  		return nil
    37  	}
    38  	return a
    39  }
    40  
    41  // ResolveIPAddr parses addr as an IP address of the form "host" or
    42  // "ipv6-host%zone" and resolves the domain name on the network net,
    43  // which must be "ip", "ip4" or "ip6".
    44  func ResolveIPAddr(net, addr string) (*IPAddr, error) {
    45  	if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
    46  		net = "ip"
    47  	}
    48  	afnet, _, err := parseNetwork(net)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	switch afnet {
    53  	case "ip", "ip4", "ip6":
    54  	default:
    55  		return nil, UnknownNetworkError(net)
    56  	}
    57  	addrs, err := internetAddrList(afnet, addr, noDeadline)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return addrs.first(isIPv4).(*IPAddr), nil
    62  }