github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/net/udpsock.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 import "errors" 8 9 var ErrWriteToConnected = errors.New("use of WriteTo with pre-connected UDP") 10 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 16 } 17 18 // Network returns the address's network name, "udp". 19 func (a *UDPAddr) Network() string { return "udp" } 20 21 func (a *UDPAddr) String() string { 22 if a == nil { 23 return "<nil>" 24 } 25 ip := ipEmptyString(a.IP) 26 if a.Zone != "" { 27 return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port)) 28 } 29 return JoinHostPort(ip, itoa(a.Port)) 30 } 31 32 func (a *UDPAddr) toAddr() Addr { 33 if a == nil { 34 return nil 35 } 36 return a 37 } 38 39 // ResolveUDPAddr parses addr as a UDP address of the form "host:port" 40 // or "[ipv6-host%zone]:port" and resolves a pair of domain name and 41 // port name on the network net, which must be "udp", "udp4" or 42 // "udp6". A literal address or host name for IPv6 must be enclosed 43 // in square brackets, as in "[::1]:80", "[ipv6-host]:http" or 44 // "[ipv6-host%zone]:80". 45 func ResolveUDPAddr(net, addr string) (*UDPAddr, error) { 46 switch net { 47 case "udp", "udp4", "udp6": 48 case "": // a hint wildcard for Go 1.0 undocumented behavior 49 net = "udp" 50 default: 51 return nil, UnknownNetworkError(net) 52 } 53 a, err := resolveInternetAddr(net, addr, noDeadline) 54 if err != nil { 55 return nil, err 56 } 57 return a.toAddr().(*UDPAddr), nil 58 }