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 // UDPAddr represents the address of a UDP end point. 8 type UDPAddr struct { 9 IP IP 10 Port int 11 Zone string // IPv6 scoped addressing zone 12 } 13 14 // Network returns the address's network name, "udp". 15 func (a *UDPAddr) Network() string { return "udp" } 16 17 func (a *UDPAddr) 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 *UDPAddr) isWildcard() bool { 29 if a == nil || a.IP == nil { 30 return true 31 } 32 return a.IP.IsUnspecified() 33 } 34 35 func (a *UDPAddr) opAddr() Addr { 36 if a == nil { 37 return nil 38 } 39 return a 40 } 41 42 // ResolveUDPAddr parses addr as a UDP address of the form "host:port" 43 // or "[ipv6-host%zone]:port" and resolves a pair of domain name and 44 // port name on the network net, which must be "udp", "udp4" or 45 // "udp6". A literal address or host name for IPv6 must be enclosed 46 // in square brackets, as in "[::1]:80", "[ipv6-host]:http" or 47 // "[ipv6-host%zone]:80". 48 func ResolveUDPAddr(net, addr string) (*UDPAddr, error) { 49 switch net { 50 case "udp", "udp4", "udp6": 51 case "": // a hint wildcard for Go 1.0 undocumented behavior 52 net = "udp" 53 default: 54 return nil, UnknownNetworkError(net) 55 } 56 addrs, err := internetAddrList(net, addr, noDeadline) 57 if err != nil { 58 return nil, err 59 } 60 return addrs.first(isIPv4).(*UDPAddr), nil 61 } 62