libgo: Update to Go 1.1.1.
[official-gcc.git] / libgo / go / net / udpsock.go
blob5ce7d6bea0f7935826249a8f22426e42dd15e2ad
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.
5 package net
7 import "errors"
9 var ErrWriteToConnected = errors.New("use of WriteTo with pre-connected UDP")
11 // UDPAddr represents the address of a UDP end point.
12 type UDPAddr struct {
13 IP IP
14 Port int
15 Zone string // IPv6 scoped addressing zone
18 // Network returns the address's network name, "udp".
19 func (a *UDPAddr) Network() string { return "udp" }
21 func (a *UDPAddr) String() string {
22 if a == nil {
23 return "<nil>"
25 if a.Zone != "" {
26 return JoinHostPort(a.IP.String()+"%"+a.Zone, itoa(a.Port))
28 return JoinHostPort(a.IP.String(), itoa(a.Port))
31 // ResolveUDPAddr parses addr as a UDP address of the form "host:port"
32 // or "[ipv6-host%zone]:port" and resolves a pair of domain name and
33 // port name on the network net, which must be "udp", "udp4" or
34 // "udp6". A literal address or host name for IPv6 must be enclosed
35 // in square brackets, as in "[::1]:80", "[ipv6-host]:http" or
36 // "[ipv6-host%zone]:80".
37 func ResolveUDPAddr(net, addr string) (*UDPAddr, error) {
38 switch net {
39 case "udp", "udp4", "udp6":
40 case "": // a hint wildcard for Go 1.0 undocumented behavior
41 net = "udp"
42 default:
43 return nil, UnknownNetworkError(net)
45 a, err := resolveInternetAddr(net, addr, noDeadline)
46 if err != nil {
47 return nil, err
49 return a.(*UDPAddr), nil