2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 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
|
|
|
|
|
|
|
|
// UDPAddr represents the address of a UDP end point.
|
|
|
|
type UDPAddr struct {
|
|
|
|
IP IP
|
|
|
|
Port int
|
2012-12-13 00:13:29 +01:00
|
|
|
Zone string // IPv6 scoped addressing zone
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the address's network name, "udp".
|
|
|
|
func (a *UDPAddr) Network() string { return "udp" }
|
|
|
|
|
|
|
|
func (a *UDPAddr) String() string {
|
|
|
|
if a == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
ip := ipEmptyString(a.IP)
|
2013-07-16 08:54:42 +02:00
|
|
|
if a.Zone != "" {
|
2013-11-06 20:49:01 +01:00
|
|
|
return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return JoinHostPort(ip, itoa(a.Port))
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (a *UDPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UDPAddr) opAddr() Addr {
|
2013-11-06 20:49:01 +01:00
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// ResolveUDPAddr parses addr as a UDP address of the form "host:port"
|
|
|
|
// or "[ipv6-host%zone]:port" and resolves a pair of domain name and
|
|
|
|
// port name on the network net, which must be "udp", "udp4" or
|
|
|
|
// "udp6". A literal address or host name for IPv6 must be enclosed
|
|
|
|
// in square brackets, as in "[::1]:80", "[ipv6-host]:http" or
|
|
|
|
// "[ipv6-host%zone]:80".
|
2011-12-03 03:17:34 +01:00
|
|
|
func ResolveUDPAddr(net, addr string) (*UDPAddr, error) {
|
2012-12-13 00:13:29 +01:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
case "": // a hint wildcard for Go 1.0 undocumented behavior
|
|
|
|
net = "udp"
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
addrs, err := internetAddrList(net, addr, noDeadline)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return addrs.first(isIPv4).(*UDPAddr), nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|