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
|
|
|
|
|
2012-12-13 00:13:29 +01:00
|
|
|
import "errors"
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
var ErrWriteToConnected = errors.New("use of WriteTo with pre-connected UDP")
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// 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-07-16 08:54:42 +02:00
|
|
|
if a.Zone != "" {
|
|
|
|
return JoinHostPort(a.IP.String()+"%"+a.Zone, itoa(a.Port))
|
|
|
|
}
|
2011-03-30 17:33:16 +02:00
|
|
|
return JoinHostPort(a.IP.String(), itoa(a.Port))
|
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)
|
|
|
|
}
|
|
|
|
a, err := resolveInternetAddr(net, addr, noDeadline)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-12-13 00:13:29 +01:00
|
|
|
return a.(*UDPAddr), nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|