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.
12 // BUG(mikio): On NaCl and Plan 9, the ReadMsgUDP and
13 // WriteMsgUDP methods of UDPConn are not implemented.
15 // BUG(mikio): On Windows, the File method of UDPConn is not
18 // BUG(mikio): On NaCl, the ListenMulticastUDP function is not
21 // BUG(mikio): On JS, methods and functions related to UDPConn are not
24 // UDPAddr represents the address of a UDP end point.
28 Zone string // IPv6 scoped addressing zone
31 // Network returns the address's network name, "udp".
32 func (a *UDPAddr) Network() string { return "udp" }
34 func (a *UDPAddr) String() string {
38 ip := ipEmptyString(a.IP)
40 return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
42 return JoinHostPort(ip, itoa(a.Port))
45 func (a *UDPAddr) isWildcard() bool {
46 if a == nil || a.IP == nil {
49 return a.IP.IsUnspecified()
52 func (a *UDPAddr) opAddr() Addr {
59 // ResolveUDPAddr returns an address of UDP end point.
61 // The network must be a UDP network name.
63 // If the host in the address parameter is not a literal IP address or
64 // the port is not a literal port number, ResolveUDPAddr resolves the
65 // address to an address of UDP end point.
66 // Otherwise, it parses the address as a pair of literal IP address
68 // The address parameter can use a host name, but this is not
69 // recommended, because it will return at most one of the host name's
72 // See func Dial for a description of the network and address
74 func ResolveUDPAddr(network, address string) (*UDPAddr, error) {
76 case "udp", "udp4", "udp6":
77 case "": // a hint wildcard for Go 1.0 undocumented behavior
80 return nil, UnknownNetworkError(network)
82 addrs, err := DefaultResolver.internetAddrList(context.Background(), network, address)
86 return addrs.forResolve(network, address).(*UDPAddr), nil
89 // UDPConn is the implementation of the Conn and PacketConn interfaces
90 // for UDP network connections.
95 // SyscallConn returns a raw network connection.
96 // This implements the syscall.Conn interface.
97 func (c *UDPConn) SyscallConn() (syscall.RawConn, error) {
99 return nil, syscall.EINVAL
101 return newRawConn(c.fd)
104 // ReadFromUDP acts like ReadFrom but returns a UDPAddr.
105 func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error) {
107 return 0, nil, syscall.EINVAL
109 n, addr, err := c.readFrom(b)
111 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
116 // ReadFrom implements the PacketConn ReadFrom method.
117 func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
119 return 0, nil, syscall.EINVAL
121 n, addr, err := c.readFrom(b)
123 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
131 // ReadMsgUDP reads a message from c, copying the payload into b and
132 // the associated out-of-band data into oob. It returns the number of
133 // bytes copied into b, the number of bytes copied into oob, the flags
134 // that were set on the message and the source address of the message.
136 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
137 // used to manipulate IP-level socket options in oob.
138 func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
140 return 0, 0, 0, nil, syscall.EINVAL
142 n, oobn, flags, addr, err = c.readMsg(b, oob)
144 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
149 // WriteToUDP acts like WriteTo but takes a UDPAddr.
150 func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
152 return 0, syscall.EINVAL
154 n, err := c.writeTo(b, addr)
156 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
161 // WriteTo implements the PacketConn WriteTo method.
162 func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
164 return 0, syscall.EINVAL
166 a, ok := addr.(*UDPAddr)
168 return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
170 n, err := c.writeTo(b, a)
172 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
177 // WriteMsgUDP writes a message to addr via c if c isn't connected, or
178 // to c's remote address if c is connected (in which case addr must be
179 // nil). The payload is copied from b and the associated out-of-band
180 // data is copied from oob. It returns the number of payload and
181 // out-of-band bytes written.
183 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
184 // used to manipulate IP-level socket options in oob.
185 func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
187 return 0, 0, syscall.EINVAL
189 n, oobn, err = c.writeMsg(b, oob, addr)
191 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
196 func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
198 // DialUDP acts like Dial for UDP networks.
200 // The network must be a UDP network name; see func Dial for details.
202 // If laddr is nil, a local address is automatically chosen.
203 // If the IP field of raddr is nil or an unspecified IP address, the
204 // local system is assumed.
205 func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
207 case "udp", "udp4", "udp6":
209 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
212 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
214 sd := &sysDialer{network: network, address: raddr.String()}
215 c, err := sd.dialUDP(context.Background(), laddr, raddr)
217 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
222 // ListenUDP acts like ListenPacket for UDP networks.
224 // The network must be a UDP network name; see func Dial for details.
226 // If the IP field of laddr is nil or an unspecified IP address,
227 // ListenUDP listens on all available IP addresses of the local system
228 // except multicast IP addresses.
229 // If the Port field of laddr is 0, a port number is automatically
231 func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error) {
233 case "udp", "udp4", "udp6":
235 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
240 sl := &sysListener{network: network, address: laddr.String()}
241 c, err := sl.listenUDP(context.Background(), laddr)
243 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
248 // ListenMulticastUDP acts like ListenPacket for UDP networks but
249 // takes a group address on a specific network interface.
251 // The network must be a UDP network name; see func Dial for details.
253 // ListenMulticastUDP listens on all available IP addresses of the
254 // local system including the group, multicast IP address.
255 // If ifi is nil, ListenMulticastUDP uses the system-assigned
256 // multicast interface, although this is not recommended because the
257 // assignment depends on platforms and sometimes it might require
258 // routing configuration.
259 // If the Port field of gaddr is 0, a port number is automatically
262 // ListenMulticastUDP is just for convenience of simple, small
263 // applications. There are golang.org/x/net/ipv4 and
264 // golang.org/x/net/ipv6 packages for general purpose uses.
265 func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
267 case "udp", "udp4", "udp6":
269 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: UnknownNetworkError(network)}
271 if gaddr == nil || gaddr.IP == nil {
272 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: errMissingAddress}
274 sl := &sysListener{network: network, address: gaddr.String()}
275 c, err := sl.listenMulticastUDP(context.Background(), ifi, gaddr)
277 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: err}