github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/net/tcpsock.go (about)

     1  // Copyright 2009 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  // TCPAddr represents the address of a TCP end point.
     8  type TCPAddr struct {
     9  	IP   IP
    10  	Port int
    11  	Zone string // IPv6 scoped addressing zone
    12  }
    13  
    14  // Network returns the address's network name, "tcp".
    15  func (a *TCPAddr) Network() string { return "tcp" }
    16  
    17  func (a *TCPAddr) String() string {
    18  	if a == nil {
    19  		return "<nil>"
    20  	}
    21  	ip := ipEmptyString(a.IP)
    22  	if a.Zone != "" {
    23  		return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
    24  	}
    25  	return JoinHostPort(ip, itoa(a.Port))
    26  }
    27  
    28  func (a *TCPAddr) toAddr() Addr {
    29  	if a == nil {
    30  		return nil
    31  	}
    32  	return a
    33  }
    34  
    35  // ResolveTCPAddr parses addr as a TCP address of the form "host:port"
    36  // or "[ipv6-host%zone]:port" and resolves a pair of domain name and
    37  // port name on the network net, which must be "tcp", "tcp4" or
    38  // "tcp6".  A literal address or host name for IPv6 must be enclosed
    39  // in square brackets, as in "[::1]:80", "[ipv6-host]:http" or
    40  // "[ipv6-host%zone]:80".
    41  func ResolveTCPAddr(net, addr string) (*TCPAddr, error) {
    42  	switch net {
    43  	case "tcp", "tcp4", "tcp6":
    44  	case "": // a hint wildcard for Go 1.0 undocumented behavior
    45  		net = "tcp"
    46  	default:
    47  		return nil, UnknownNetworkError(net)
    48  	}
    49  	a, err := resolveInternetAddr(net, addr, noDeadline)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return a.toAddr().(*TCPAddr), nil
    54  }