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 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 JS, methods and functions related to UDPConn are not
21 // UDPAddr represents the address of a UDP end point.
25 Zone string // IPv6 scoped addressing zone
28 // Network returns the address's network name, "udp".
29 func (a *UDPAddr) Network() string { return "udp" }
31 func (a *UDPAddr) String() string {
35 ip := ipEmptyString(a.IP)
37 return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
39 return JoinHostPort(ip, itoa(a.Port))
42 func (a *UDPAddr) isWildcard() bool {
43 if a == nil || a.IP == nil {
46 return a.IP.IsUnspecified()
49 func (a *UDPAddr) opAddr() Addr {
56 // ResolveUDPAddr returns an address of UDP end point.
58 // The network must be a UDP network name.
60 // If the host in the address parameter is not a literal IP address or
61 // the port is not a literal port number, ResolveUDPAddr resolves the
62 // address to an address of UDP end point.
63 // Otherwise, it parses the address as a pair of literal IP address
65 // The address parameter can use a host name, but this is not
66 // recommended, because it will return at most one of the host name's
69 // See func Dial for a description of the network and address
71 func ResolveUDPAddr(network, address string) (*UDPAddr, error) {
73 case "udp", "udp4", "udp6":
74 case "": // a hint wildcard for Go 1.0 undocumented behavior
77 return nil, UnknownNetworkError(network)
79 addrs, err := DefaultResolver.internetAddrList(context.Background(), network, address)
83 return addrs.forResolve(network, address).(*UDPAddr), nil
86 // UDPConn is the implementation of the Conn and PacketConn interfaces
87 // for UDP network connections.
92 // SyscallConn returns a raw network connection.
93 // This implements the syscall.Conn interface.
94 func (c *UDPConn) SyscallConn() (syscall.RawConn, error) {
96 return nil, syscall.EINVAL
98 return newRawConn(c.fd)
101 // ReadFromUDP acts like ReadFrom but returns a UDPAddr.
102 func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error) {
104 return 0, nil, syscall.EINVAL
106 n, addr, err := c.readFrom(b)
108 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
113 // ReadFrom implements the PacketConn ReadFrom method.
114 func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
116 return 0, nil, syscall.EINVAL
118 n, addr, err := c.readFrom(b)
120 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
128 // ReadMsgUDP reads a message from c, copying the payload into b and
129 // the associated out-of-band data into oob. It returns the number of
130 // bytes copied into b, the number of bytes copied into oob, the flags
131 // that were set on the message and the source address of the message.
133 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
134 // used to manipulate IP-level socket options in oob.
135 func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
137 return 0, 0, 0, nil, syscall.EINVAL
139 n, oobn, flags, addr, err = c.readMsg(b, oob)
141 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
146 // WriteToUDP acts like WriteTo but takes a UDPAddr.
147 func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
149 return 0, syscall.EINVAL
151 n, err := c.writeTo(b, addr)
153 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
158 // WriteTo implements the PacketConn WriteTo method.
159 func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
161 return 0, syscall.EINVAL
163 a, ok := addr.(*UDPAddr)
165 return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
167 n, err := c.writeTo(b, a)
169 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
174 // WriteMsgUDP writes a message to addr via c if c isn't connected, or
175 // to c's remote address if c is connected (in which case addr must be
176 // nil). The payload is copied from b and the associated out-of-band
177 // data is copied from oob. It returns the number of payload and
178 // out-of-band bytes written.
180 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
181 // used to manipulate IP-level socket options in oob.
182 func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
184 return 0, 0, syscall.EINVAL
186 n, oobn, err = c.writeMsg(b, oob, addr)
188 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
193 func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
195 // DialUDP acts like Dial for UDP networks.
197 // The network must be a UDP network name; see func Dial for details.
199 // If laddr is nil, a local address is automatically chosen.
200 // If the IP field of raddr is nil or an unspecified IP address, the
201 // local system is assumed.
202 func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
204 case "udp", "udp4", "udp6":
206 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
209 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
211 sd := &sysDialer{network: network, address: raddr.String()}
212 c, err := sd.dialUDP(context.Background(), laddr, raddr)
214 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
219 // ListenUDP acts like ListenPacket for UDP networks.
221 // The network must be a UDP network name; see func Dial for details.
223 // If the IP field of laddr is nil or an unspecified IP address,
224 // ListenUDP listens on all available IP addresses of the local system
225 // except multicast IP addresses.
226 // If the Port field of laddr is 0, a port number is automatically
228 func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error) {
230 case "udp", "udp4", "udp6":
232 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
237 sl := &sysListener{network: network, address: laddr.String()}
238 c, err := sl.listenUDP(context.Background(), laddr)
240 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
245 // ListenMulticastUDP acts like ListenPacket for UDP networks but
246 // takes a group address on a specific network interface.
248 // The network must be a UDP network name; see func Dial for details.
250 // ListenMulticastUDP listens on all available IP addresses of the
251 // local system including the group, multicast IP address.
252 // If ifi is nil, ListenMulticastUDP uses the system-assigned
253 // multicast interface, although this is not recommended because the
254 // assignment depends on platforms and sometimes it might require
255 // routing configuration.
256 // If the Port field of gaddr is 0, a port number is automatically
259 // ListenMulticastUDP is just for convenience of simple, small
260 // applications. There are golang.org/x/net/ipv4 and
261 // golang.org/x/net/ipv6 packages for general purpose uses.
262 func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
264 case "udp", "udp4", "udp6":
266 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: UnknownNetworkError(network)}
268 if gaddr == nil || gaddr.IP == nil {
269 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: errMissingAddress}
271 sl := &sysListener{network: network, address: gaddr.String()}
272 c, err := sl.listenMulticastUDP(context.Background(), ifi, gaddr)
274 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: err}