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
7 // UDP sockets for POSIX
16 func sockaddrToUDP(sa syscall.Sockaddr) Addr {
17 switch sa := sa.(type) {
18 case *syscall.SockaddrInet4:
19 return &UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
20 case *syscall.SockaddrInet6:
21 return &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
26 func (a *UDPAddr) family() int {
27 if a == nil || len(a.IP) <= IPv4len {
28 return syscall.AF_INET
30 if a.IP.To4() != nil {
31 return syscall.AF_INET
33 return syscall.AF_INET6
36 func (a *UDPAddr) isWildcard() bool {
37 if a == nil || a.IP == nil {
40 return a.IP.IsUnspecified()
43 func (a *UDPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
44 return ipToSockaddr(family, a.IP, a.Port, a.Zone)
47 func (a *UDPAddr) toAddr() sockaddr {
48 if a == nil { // nil *UDPAddr
49 return nil // nil interface
54 // UDPConn is the implementation of the Conn and PacketConn
55 // interfaces for UDP network connections.
60 func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
62 // ReadFromUDP reads a UDP packet from c, copying the payload into b.
63 // It returns the number of bytes copied into b and the return address
64 // that was on the packet.
66 // ReadFromUDP can be made to time out and return an error with Timeout() == true
67 // after a fixed time limit; see SetDeadline and SetReadDeadline.
68 func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
70 return 0, nil, syscall.EINVAL
72 n, sa, err := c.fd.ReadFrom(b)
73 switch sa := sa.(type) {
74 case *syscall.SockaddrInet4:
75 addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
76 case *syscall.SockaddrInet6:
77 addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
82 // ReadFrom implements the PacketConn ReadFrom method.
83 func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
85 return 0, nil, syscall.EINVAL
87 n, addr, err := c.ReadFromUDP(b)
88 return n, addr.toAddr(), err
91 // ReadMsgUDP reads a packet from c, copying the payload into b and
92 // the associdated out-of-band data into oob. It returns the number
93 // of bytes copied into b, the number of bytes copied into oob, the
94 // flags that were set on the packet and the source address of the
96 func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
98 return 0, 0, 0, nil, syscall.EINVAL
100 var sa syscall.Sockaddr
101 n, oobn, flags, sa, err = c.fd.ReadMsg(b, oob)
102 switch sa := sa.(type) {
103 case *syscall.SockaddrInet4:
104 addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
105 case *syscall.SockaddrInet6:
106 addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
111 // WriteToUDP writes a UDP packet to addr via c, copying the payload from b.
113 // WriteToUDP can be made to time out and return
114 // an error with Timeout() == true after a fixed time limit;
115 // see SetDeadline and SetWriteDeadline.
116 // On packet-oriented connections, write timeouts are rare.
117 func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
119 return 0, syscall.EINVAL
121 if c.fd.isConnected {
122 return 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
124 sa, err := addr.sockaddr(c.fd.family)
126 return 0, &OpError{"write", c.fd.net, addr, err}
128 return c.fd.WriteTo(b, sa)
131 // WriteTo implements the PacketConn WriteTo method.
132 func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
134 return 0, syscall.EINVAL
136 a, ok := addr.(*UDPAddr)
138 return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
140 return c.WriteToUDP(b, a)
143 // WriteMsgUDP writes a packet to addr via c, copying the payload from
144 // b and the associated out-of-band data from oob. It returns the
145 // number of payload and out-of-band bytes written.
146 func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
148 return 0, 0, syscall.EINVAL
150 if c.fd.isConnected {
151 return 0, 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
153 sa, err := addr.sockaddr(c.fd.family)
155 return 0, 0, &OpError{"write", c.fd.net, addr, err}
157 return c.fd.WriteMsg(b, oob, sa)
160 // DialUDP connects to the remote address raddr on the network net,
161 // which must be "udp", "udp4", or "udp6". If laddr is not nil, it is used
162 // as the local address for the connection.
163 func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
164 return dialUDP(net, laddr, raddr, noDeadline)
167 func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) {
169 case "udp", "udp4", "udp6":
171 return nil, UnknownNetworkError(net)
174 return nil, &OpError{"dial", net, nil, errMissingAddress}
176 fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), deadline, syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP)
180 return newUDPConn(fd), nil
183 // ListenUDP listens for incoming UDP packets addressed to the
184 // local address laddr. The returned connection c's ReadFrom
185 // and WriteTo methods can be used to receive and send UDP
186 // packets with per-packet addressing.
187 func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
189 case "udp", "udp4", "udp6":
191 return nil, UnknownNetworkError(net)
196 fd, err := internetSocket(net, laddr.toAddr(), nil, noDeadline, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
200 return newUDPConn(fd), nil
203 // ListenMulticastUDP listens for incoming multicast UDP packets
204 // addressed to the group address gaddr on ifi, which specifies
205 // the interface to join. ListenMulticastUDP uses default
206 // multicast interface if ifi is nil.
207 func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
209 case "udp", "udp4", "udp6":
211 return nil, UnknownNetworkError(net)
213 if gaddr == nil || gaddr.IP == nil {
214 return nil, &OpError{"listen", net, nil, errMissingAddress}
216 fd, err := internetSocket(net, gaddr.toAddr(), nil, noDeadline, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
221 if ip4 := gaddr.IP.To4(); ip4 != nil {
222 if err := listenIPv4MulticastUDP(c, ifi, ip4); err != nil {
224 return nil, &OpError{"listen", net, &IPAddr{IP: ip4}, err}
227 if err := listenIPv6MulticastUDP(c, ifi, gaddr.IP); err != nil {
229 return nil, &OpError{"listen", net, &IPAddr{IP: gaddr.IP}, err}
235 func listenIPv4MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
237 if err := setIPv4MulticastInterface(c.fd, ifi); err != nil {
241 if err := setIPv4MulticastLoopback(c.fd, false); err != nil {
244 if err := joinIPv4Group(c.fd, ifi, ip); err != nil {
250 func listenIPv6MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
252 if err := setIPv6MulticastInterface(c.fd, ifi); err != nil {
256 if err := setIPv6MulticastLoopback(c.fd, false); err != nil {
259 if err := joinIPv6Group(c.fd, ifi, ip); err != nil {