github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/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  	if a.Zone != "" {
    21  		return a.IP.String() + "%" + a.Zone
    22  	}
    23  	return a.IP.String()
    24  }
    25  
    26  func (a *IPAddr) toAddr() Addr {
    27  	if a == nil {
    28  		return nil
    29  	}
    30  	return a
    31  }
    32  
    33  // ResolveIPAddr parses addr as an IP address of the form "host" or
    34  // "ipv6-host%zone" and resolves the domain name on the network net,
    35  // which must be "ip", "ip4" or "ip6".
    36  func ResolveIPAddr(net, addr string) (*IPAddr, error) {
    37  	if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
    38  		net = "ip"
    39  	}
    40  	afnet, _, err := parseNetwork(net)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	switch afnet {
    45  	case "ip", "ip4", "ip6":
    46  	default:
    47  		return nil, UnknownNetworkError(net)
    48  	}
    49  	a, err := resolveInternetAddr(afnet, addr, noDeadline)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return a.toAddr().(*IPAddr), nil
    54  }