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.
5 // +build darwin freebsd linux netbsd openbsd windows
18 var ErrWriteToConnected = errors.New("use of WriteTo with pre-connected UDP")
20 func sockaddrToUDP(sa syscall.Sockaddr) Addr {
21 switch sa := sa.(type) {
22 case *syscall.SockaddrInet4:
23 return &UDPAddr{sa.Addr[0:], sa.Port}
24 case *syscall.SockaddrInet6:
25 return &UDPAddr{sa.Addr[0:], sa.Port}
30 func (a *UDPAddr) family() int {
31 if a == nil || len(a.IP) <= IPv4len {
32 return syscall.AF_INET
34 if a.IP.To4() != nil {
35 return syscall.AF_INET
37 return syscall.AF_INET6
40 func (a *UDPAddr) isWildcard() bool {
41 if a == nil || a.IP == nil {
44 return a.IP.IsUnspecified()
47 func (a *UDPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
48 return ipToSockaddr(family, a.IP, a.Port)
51 func (a *UDPAddr) toAddr() sockaddr {
52 if a == nil { // nil *UDPAddr
53 return nil // nil interface
58 // UDPConn is the implementation of the Conn and PacketConn
59 // interfaces for UDP network connections.
64 func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{fd} }
66 func (c *UDPConn) ok() bool { return c != nil && c.fd != nil }
68 // Implementation of the Conn interface - see Conn for documentation.
70 // Read implements the Conn Read method.
71 func (c *UDPConn) Read(b []byte) (int, error) {
73 return 0, syscall.EINVAL
78 // Write implements the Conn Write method.
79 func (c *UDPConn) Write(b []byte) (int, error) {
81 return 0, syscall.EINVAL
86 // Close closes the UDP connection.
87 func (c *UDPConn) Close() error {
96 // LocalAddr returns the local network address.
97 func (c *UDPConn) LocalAddr() Addr {
104 // RemoteAddr returns the remote network address, a *UDPAddr.
105 func (c *UDPConn) RemoteAddr() Addr {
112 // SetDeadline implements the Conn SetDeadline method.
113 func (c *UDPConn) SetDeadline(t time.Time) error {
115 return syscall.EINVAL
117 return setDeadline(c.fd, t)
120 // SetReadDeadline implements the Conn SetReadDeadline method.
121 func (c *UDPConn) SetReadDeadline(t time.Time) error {
123 return syscall.EINVAL
125 return setReadDeadline(c.fd, t)
128 // SetWriteDeadline implements the Conn SetWriteDeadline method.
129 func (c *UDPConn) SetWriteDeadline(t time.Time) error {
131 return syscall.EINVAL
133 return setWriteDeadline(c.fd, t)
136 // SetReadBuffer sets the size of the operating system's
137 // receive buffer associated with the connection.
138 func (c *UDPConn) SetReadBuffer(bytes int) error {
140 return syscall.EINVAL
142 return setReadBuffer(c.fd, bytes)
145 // SetWriteBuffer sets the size of the operating system's
146 // transmit buffer associated with the connection.
147 func (c *UDPConn) SetWriteBuffer(bytes int) error {
149 return syscall.EINVAL
151 return setWriteBuffer(c.fd, bytes)
154 // UDP-specific methods.
156 // ReadFromUDP reads a UDP packet from c, copying the payload into b.
157 // It returns the number of bytes copied into b and the return address
158 // that was on the packet.
160 // ReadFromUDP can be made to time out and return an error with Timeout() == true
161 // after a fixed time limit; see SetDeadline and SetReadDeadline.
162 func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
164 return 0, nil, syscall.EINVAL
166 n, sa, err := c.fd.ReadFrom(b)
167 switch sa := sa.(type) {
168 case *syscall.SockaddrInet4:
169 addr = &UDPAddr{sa.Addr[0:], sa.Port}
170 case *syscall.SockaddrInet6:
171 addr = &UDPAddr{sa.Addr[0:], sa.Port}
176 // ReadFrom implements the PacketConn ReadFrom method.
177 func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
179 return 0, nil, syscall.EINVAL
181 n, uaddr, err := c.ReadFromUDP(b)
182 return n, uaddr.toAddr(), err
185 // WriteToUDP writes a UDP packet to addr via c, copying the payload from b.
187 // WriteToUDP can be made to time out and return
188 // an error with Timeout() == true after a fixed time limit;
189 // see SetDeadline and SetWriteDeadline.
190 // On packet-oriented connections, write timeouts are rare.
191 func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
193 return 0, syscall.EINVAL
195 if c.fd.isConnected {
196 return 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
198 sa, err := addr.sockaddr(c.fd.family)
200 return 0, &OpError{"write", c.fd.net, addr, err}
202 return c.fd.WriteTo(b, sa)
205 // WriteTo implements the PacketConn WriteTo method.
206 func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
208 return 0, syscall.EINVAL
210 a, ok := addr.(*UDPAddr)
212 return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
214 return c.WriteToUDP(b, a)
217 // File returns a copy of the underlying os.File, set to blocking mode.
218 // It is the caller's responsibility to close f when finished.
219 // Closing c does not affect f, and closing f does not affect c.
220 func (c *UDPConn) File() (f *os.File, err error) { return c.fd.dup() }
222 // DialUDP connects to the remote address raddr on the network net,
223 // which must be "udp", "udp4", or "udp6". If laddr is not nil, it is used
224 // as the local address for the connection.
225 func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
227 case "udp", "udp4", "udp6":
229 return nil, UnknownNetworkError(net)
232 return nil, &OpError{"dial", net, nil, errMissingAddress}
234 fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP)
238 return newUDPConn(fd), nil
241 // ListenUDP listens for incoming UDP packets addressed to the
242 // local address laddr. The returned connection c's ReadFrom
243 // and WriteTo methods can be used to receive and send UDP
244 // packets with per-packet addressing.
245 func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
247 case "udp", "udp4", "udp6":
249 return nil, UnknownNetworkError(net)
252 return nil, &OpError{"listen", net, nil, errMissingAddress}
254 fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
258 return newUDPConn(fd), nil
261 // ListenMulticastUDP listens for incoming multicast UDP packets
262 // addressed to the group address gaddr on ifi, which specifies
263 // the interface to join. ListenMulticastUDP uses default
264 // multicast interface if ifi is nil.
265 func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
267 case "udp", "udp4", "udp6":
269 return nil, UnknownNetworkError(net)
271 if gaddr == nil || gaddr.IP == nil {
272 return nil, &OpError{"listenmulticast", net, nil, errMissingAddress}
274 fd, err := internetSocket(net, gaddr.toAddr(), nil, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
279 ip4 := gaddr.IP.To4()
281 err := listenIPv4MulticastUDP(c, ifi, ip4)
287 err := listenIPv6MulticastUDP(c, ifi, gaddr.IP)
296 func listenIPv4MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
298 err := setIPv4MulticastInterface(c.fd, ifi)
303 err := setIPv4MulticastLoopback(c.fd, false)
307 err = joinIPv4GroupUDP(c, ifi, ip)
314 func listenIPv6MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
316 err := setIPv6MulticastInterface(c.fd, ifi)
321 err := setIPv6MulticastLoopback(c.fd, false)
325 err = joinIPv6GroupUDP(c, ifi, ip)
332 func joinIPv4GroupUDP(c *UDPConn, ifi *Interface, ip IP) error {
333 err := joinIPv4Group(c.fd, ifi, ip)
335 return &OpError{"joinipv4group", c.fd.net, &IPAddr{ip}, err}
340 func leaveIPv4GroupUDP(c *UDPConn, ifi *Interface, ip IP) error {
341 err := leaveIPv4Group(c.fd, ifi, ip)
343 return &OpError{"leaveipv4group", c.fd.net, &IPAddr{ip}, err}
348 func joinIPv6GroupUDP(c *UDPConn, ifi *Interface, ip IP) error {
349 err := joinIPv6Group(c.fd, ifi, ip)
351 return &OpError{"joinipv6group", c.fd.net, &IPAddr{ip}, err}
356 func leaveIPv6GroupUDP(c *UDPConn, ifi *Interface, ip IP) error {
357 err := leaveIPv6Group(c.fd, ifi, ip)
359 return &OpError{"leaveipv6group", c.fd.net, &IPAddr{ip}, err}