github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/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 "syscall" 8 9 // UDPAddr represents the address of a UDP end point. 10 type UDPAddr struct { 11 IP IP 12 Port int 13 Zone string // IPv6 scoped addressing zone 14 } 15 16 // Network returns the address's network name, "udp". 17 func (a *UDPAddr) Network() string { return "udp" } 18 19 func (a *UDPAddr) String() string { 20 if a == nil { 21 return "<nil>" 22 } 23 ip := ipEmptyString(a.IP) 24 if a.Zone != "" { 25 return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port)) 26 } 27 return JoinHostPort(ip, itoa(a.Port)) 28 } 29 30 func (a *UDPAddr) isWildcard() bool { 31 if a == nil || a.IP == nil { 32 return true 33 } 34 return a.IP.IsUnspecified() 35 } 36 37 func (a *UDPAddr) opAddr() Addr { 38 if a == nil { 39 return nil 40 } 41 return a 42 } 43 44 // ResolveUDPAddr parses addr as a UDP address of the form "host:port" 45 // or "[ipv6-host%zone]:port" and resolves a pair of domain name and 46 // port name on the network net, which must be "udp", "udp4" or 47 // "udp6". A literal address or host name for IPv6 must be enclosed 48 // in square brackets, as in "[::1]:80", "[ipv6-host]:http" or 49 // "[ipv6-host%zone]:80". 50 func ResolveUDPAddr(net, addr string) (*UDPAddr, error) { 51 switch net { 52 case "udp", "udp4", "udp6": 53 case "": // a hint wildcard for Go 1.0 undocumented behavior 54 net = "udp" 55 default: 56 return nil, UnknownNetworkError(net) 57 } 58 addrs, err := internetAddrList(net, addr, noDeadline) 59 if err != nil { 60 return nil, err 61 } 62 return addrs.first(isIPv4).(*UDPAddr), nil 63 } 64 65 // UDPConn is the implementation of the Conn and PacketConn interfaces 66 // for UDP network connections. 67 type UDPConn struct { 68 conn 69 } 70 71 // ReadFromUDP reads a UDP packet from c, copying the payload into b. 72 // It returns the number of bytes copied into b and the return address 73 // that was on the packet. 74 // 75 // ReadFromUDP can be made to time out and return an error with 76 // Timeout() == true after a fixed time limit; see SetDeadline and 77 // SetReadDeadline. 78 func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error) { 79 if !c.ok() { 80 return 0, nil, syscall.EINVAL 81 } 82 n, addr, err := c.readFrom(b) 83 if err != nil { 84 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 85 } 86 return n, addr, err 87 } 88 89 // ReadFrom implements the PacketConn ReadFrom method. 90 func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) { 91 if !c.ok() { 92 return 0, nil, syscall.EINVAL 93 } 94 n, addr, err := c.readFrom(b) 95 if err != nil { 96 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 97 } 98 if addr == nil { 99 return n, nil, err 100 } 101 return n, addr, err 102 } 103 104 // ReadMsgUDP reads a packet from c, copying the payload into b and 105 // the associated out-of-band data into oob. It returns the number 106 // of bytes copied into b, the number of bytes copied into oob, the 107 // flags that were set on the packet and the source address of the 108 // packet. 109 func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) { 110 if !c.ok() { 111 return 0, 0, 0, nil, syscall.EINVAL 112 } 113 n, oobn, flags, addr, err = c.readMsg(b, oob) 114 if err != nil { 115 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} 116 } 117 return 118 } 119 120 // WriteToUDP writes a UDP packet to addr via c, copying the payload 121 // from b. 122 // 123 // WriteToUDP can be made to time out and return an error with 124 // Timeout() == true after a fixed time limit; see SetDeadline and 125 // SetWriteDeadline. On packet-oriented connections, write timeouts 126 // are rare. 127 func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) { 128 if !c.ok() { 129 return 0, syscall.EINVAL 130 } 131 n, err := c.writeTo(b, addr) 132 if err != nil { 133 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err} 134 } 135 return n, err 136 } 137 138 // WriteTo implements the PacketConn WriteTo method. 139 func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) { 140 if !c.ok() { 141 return 0, syscall.EINVAL 142 } 143 a, ok := addr.(*UDPAddr) 144 if !ok { 145 return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL} 146 } 147 n, err := c.writeTo(b, a) 148 if err != nil { 149 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err} 150 } 151 return n, err 152 } 153 154 // WriteMsgUDP writes a packet to addr via c if c isn't connected, or 155 // to c's remote destination address if c is connected (in which case 156 // addr must be nil). The payload is copied from b and the associated 157 // out-of-band data is copied from oob. It returns the number of 158 // payload and out-of-band bytes written. 159 func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) { 160 if !c.ok() { 161 return 0, 0, syscall.EINVAL 162 } 163 n, oobn, err = c.writeMsg(b, oob, addr) 164 if err != nil { 165 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err} 166 } 167 return 168 } 169 170 func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} } 171 172 // DialUDP connects to the remote address raddr on the network net, 173 // which must be "udp", "udp4", or "udp6". If laddr is not nil, it is 174 // used as the local address for the connection. 175 func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) { 176 switch net { 177 case "udp", "udp4", "udp6": 178 default: 179 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(net)} 180 } 181 if raddr == nil { 182 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress} 183 } 184 c, err := dialUDP(net, laddr, raddr, noDeadline) 185 if err != nil { 186 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err} 187 } 188 return c, nil 189 } 190 191 // ListenUDP listens for incoming UDP packets addressed to the local 192 // address laddr. Net must be "udp", "udp4", or "udp6". If laddr has 193 // a port of 0, ListenUDP will choose an available port. 194 // The LocalAddr method of the returned UDPConn can be used to 195 // discover the port. The returned connection's ReadFrom and WriteTo 196 // methods can be used to receive and send UDP packets with per-packet 197 // addressing. 198 func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) { 199 switch net { 200 case "udp", "udp4", "udp6": 201 default: 202 return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)} 203 } 204 if laddr == nil { 205 laddr = &UDPAddr{} 206 } 207 c, err := listenUDP(net, laddr) 208 if err != nil { 209 return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err} 210 } 211 return c, nil 212 } 213 214 // ListenMulticastUDP listens for incoming multicast UDP packets 215 // addressed to the group address gaddr on the interface ifi. 216 // Network must be "udp", "udp4" or "udp6". 217 // ListenMulticastUDP uses the system-assigned multicast interface 218 // when ifi is nil, although this is not recommended because the 219 // assignment depends on platforms and sometimes it might require 220 // routing configuration. 221 // 222 // ListenMulticastUDP is just for convenience of simple, small 223 // applications. There are golang.org/x/net/ipv4 and 224 // golang.org/x/net/ipv6 packages for general purpose uses. 225 func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) { 226 switch network { 227 case "udp", "udp4", "udp6": 228 default: 229 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: UnknownNetworkError(network)} 230 } 231 if gaddr == nil || gaddr.IP == nil { 232 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: errMissingAddress} 233 } 234 c, err := listenMulticastUDP(network, ifi, gaddr) 235 if err != nil { 236 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: err} 237 } 238 return c, nil 239 }