Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 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 | |
Russ Cox | 2715956 | 2011-09-15 16:48:57 -0400 | [diff] [blame] | 5 | // +build darwin freebsd linux openbsd windows |
| 6 | |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 7 | // UDP sockets |
| 8 | |
| 9 | package net |
| 10 | |
| 11 | import ( |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 12 | "bytes" |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 13 | "os" |
| 14 | "syscall" |
| 15 | ) |
| 16 | |
| 17 | func sockaddrToUDP(sa syscall.Sockaddr) Addr { |
| 18 | switch sa := sa.(type) { |
| 19 | case *syscall.SockaddrInet4: |
| 20 | return &UDPAddr{sa.Addr[0:], sa.Port} |
| 21 | case *syscall.SockaddrInet6: |
| 22 | return &UDPAddr{sa.Addr[0:], sa.Port} |
| 23 | } |
| 24 | return nil |
| 25 | } |
| 26 | |
| 27 | func (a *UDPAddr) family() int { |
Mikio Hara | 80f79ad | 2011-08-24 13:59:33 -0400 | [diff] [blame] | 28 | if a == nil || len(a.IP) <= IPv4len { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 29 | return syscall.AF_INET |
| 30 | } |
| 31 | if a.IP.To4() != nil { |
| 32 | return syscall.AF_INET |
| 33 | } |
| 34 | return syscall.AF_INET6 |
| 35 | } |
| 36 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 37 | func (a *UDPAddr) sockaddr(family int) (syscall.Sockaddr, error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 38 | return ipToSockaddr(family, a.IP, a.Port) |
| 39 | } |
| 40 | |
| 41 | func (a *UDPAddr) toAddr() sockaddr { |
| 42 | if a == nil { // nil *UDPAddr |
| 43 | return nil // nil interface |
| 44 | } |
| 45 | return a |
| 46 | } |
| 47 | |
| 48 | // UDPConn is the implementation of the Conn and PacketConn |
| 49 | // interfaces for UDP network connections. |
| 50 | type UDPConn struct { |
| 51 | fd *netFD |
| 52 | } |
| 53 | |
| 54 | func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{fd} } |
| 55 | |
| 56 | func (c *UDPConn) ok() bool { return c != nil && c.fd != nil } |
| 57 | |
| 58 | // Implementation of the Conn interface - see Conn for documentation. |
| 59 | |
| 60 | // Read implements the net.Conn Read method. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 61 | func (c *UDPConn) Read(b []byte) (n int, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 62 | if !c.ok() { |
| 63 | return 0, os.EINVAL |
| 64 | } |
| 65 | return c.fd.Read(b) |
| 66 | } |
| 67 | |
| 68 | // Write implements the net.Conn Write method. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 69 | func (c *UDPConn) Write(b []byte) (n int, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 70 | if !c.ok() { |
| 71 | return 0, os.EINVAL |
| 72 | } |
| 73 | return c.fd.Write(b) |
| 74 | } |
| 75 | |
| 76 | // Close closes the UDP connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 77 | func (c *UDPConn) Close() error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 78 | if !c.ok() { |
| 79 | return os.EINVAL |
| 80 | } |
| 81 | err := c.fd.Close() |
| 82 | c.fd = nil |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | // LocalAddr returns the local network address. |
| 87 | func (c *UDPConn) LocalAddr() Addr { |
| 88 | if !c.ok() { |
| 89 | return nil |
| 90 | } |
| 91 | return c.fd.laddr |
| 92 | } |
| 93 | |
| 94 | // RemoteAddr returns the remote network address, a *UDPAddr. |
| 95 | func (c *UDPConn) RemoteAddr() Addr { |
| 96 | if !c.ok() { |
| 97 | return nil |
| 98 | } |
| 99 | return c.fd.raddr |
| 100 | } |
| 101 | |
| 102 | // SetTimeout implements the net.Conn SetTimeout method. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 103 | func (c *UDPConn) SetTimeout(nsec int64) error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 104 | if !c.ok() { |
| 105 | return os.EINVAL |
| 106 | } |
| 107 | return setTimeout(c.fd, nsec) |
| 108 | } |
| 109 | |
| 110 | // SetReadTimeout implements the net.Conn SetReadTimeout method. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 111 | func (c *UDPConn) SetReadTimeout(nsec int64) error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 112 | if !c.ok() { |
| 113 | return os.EINVAL |
| 114 | } |
| 115 | return setReadTimeout(c.fd, nsec) |
| 116 | } |
| 117 | |
| 118 | // SetWriteTimeout implements the net.Conn SetWriteTimeout method. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 119 | func (c *UDPConn) SetWriteTimeout(nsec int64) error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 120 | if !c.ok() { |
| 121 | return os.EINVAL |
| 122 | } |
| 123 | return setWriteTimeout(c.fd, nsec) |
| 124 | } |
| 125 | |
| 126 | // SetReadBuffer sets the size of the operating system's |
| 127 | // receive buffer associated with the connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 128 | func (c *UDPConn) SetReadBuffer(bytes int) error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 129 | if !c.ok() { |
| 130 | return os.EINVAL |
| 131 | } |
| 132 | return setReadBuffer(c.fd, bytes) |
| 133 | } |
| 134 | |
| 135 | // SetWriteBuffer sets the size of the operating system's |
| 136 | // transmit buffer associated with the connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 137 | func (c *UDPConn) SetWriteBuffer(bytes int) error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 138 | if !c.ok() { |
| 139 | return os.EINVAL |
| 140 | } |
| 141 | return setWriteBuffer(c.fd, bytes) |
| 142 | } |
| 143 | |
| 144 | // UDP-specific methods. |
| 145 | |
| 146 | // ReadFromUDP reads a UDP packet from c, copying the payload into b. |
| 147 | // It returns the number of bytes copied into b and the return address |
| 148 | // that was on the packet. |
| 149 | // |
| 150 | // ReadFromUDP can be made to time out and return an error with Timeout() == true |
| 151 | // after a fixed time limit; see SetTimeout and SetReadTimeout. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 152 | func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 153 | if !c.ok() { |
| 154 | return 0, nil, os.EINVAL |
| 155 | } |
| 156 | n, sa, err := c.fd.ReadFrom(b) |
| 157 | switch sa := sa.(type) { |
| 158 | case *syscall.SockaddrInet4: |
| 159 | addr = &UDPAddr{sa.Addr[0:], sa.Port} |
| 160 | case *syscall.SockaddrInet6: |
| 161 | addr = &UDPAddr{sa.Addr[0:], sa.Port} |
| 162 | } |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | // ReadFrom implements the net.PacketConn ReadFrom method. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 167 | func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 168 | if !c.ok() { |
| 169 | return 0, nil, os.EINVAL |
| 170 | } |
| 171 | n, uaddr, err := c.ReadFromUDP(b) |
| 172 | return n, uaddr.toAddr(), err |
| 173 | } |
| 174 | |
| 175 | // WriteToUDP writes a UDP packet to addr via c, copying the payload from b. |
| 176 | // |
| 177 | // WriteToUDP can be made to time out and return |
| 178 | // an error with Timeout() == true after a fixed time limit; |
| 179 | // see SetTimeout and SetWriteTimeout. |
| 180 | // On packet-oriented connections, write timeouts are rare. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 181 | func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 182 | if !c.ok() { |
| 183 | return 0, os.EINVAL |
| 184 | } |
| 185 | sa, err1 := addr.sockaddr(c.fd.family) |
| 186 | if err1 != nil { |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 187 | return 0, &OpError{Op: "write", Net: "udp", Addr: addr, Err: err1} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 188 | } |
| 189 | return c.fd.WriteTo(b, sa) |
| 190 | } |
| 191 | |
| 192 | // WriteTo implements the net.PacketConn WriteTo method. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 193 | func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 194 | if !c.ok() { |
| 195 | return 0, os.EINVAL |
| 196 | } |
| 197 | a, ok := addr.(*UDPAddr) |
| 198 | if !ok { |
| 199 | return 0, &OpError{"writeto", "udp", addr, os.EINVAL} |
| 200 | } |
| 201 | return c.WriteToUDP(b, a) |
| 202 | } |
| 203 | |
| 204 | // DialUDP connects to the remote address raddr on the network net, |
| 205 | // which must be "udp", "udp4", or "udp6". If laddr is not nil, it is used |
| 206 | // as the local address for the connection. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 207 | func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 208 | switch net { |
| 209 | case "udp", "udp4", "udp6": |
| 210 | default: |
| 211 | return nil, UnknownNetworkError(net) |
| 212 | } |
| 213 | if raddr == nil { |
| 214 | return nil, &OpError{"dial", "udp", nil, errMissingAddress} |
| 215 | } |
| 216 | fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP) |
| 217 | if e != nil { |
| 218 | return nil, e |
| 219 | } |
| 220 | return newUDPConn(fd), nil |
| 221 | } |
| 222 | |
| 223 | // ListenUDP listens for incoming UDP packets addressed to the |
| 224 | // local address laddr. The returned connection c's ReadFrom |
| 225 | // and WriteTo methods can be used to receive and send UDP |
| 226 | // packets with per-packet addressing. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 227 | func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err error) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 228 | switch net { |
| 229 | case "udp", "udp4", "udp6": |
| 230 | default: |
| 231 | return nil, UnknownNetworkError(net) |
| 232 | } |
| 233 | if laddr == nil { |
| 234 | return nil, &OpError{"listen", "udp", nil, errMissingAddress} |
| 235 | } |
| 236 | fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP) |
| 237 | if e != nil { |
| 238 | return nil, e |
| 239 | } |
| 240 | return newUDPConn(fd), nil |
| 241 | } |
| 242 | |
| 243 | // BindToDevice binds a UDPConn to a network interface. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 244 | func (c *UDPConn) BindToDevice(device string) error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 245 | if !c.ok() { |
| 246 | return os.EINVAL |
| 247 | } |
| 248 | c.fd.incref() |
| 249 | defer c.fd.decref() |
| 250 | return os.NewSyscallError("setsockopt", syscall.BindToDevice(c.fd.sysfd, device)) |
| 251 | } |
| 252 | |
| 253 | // File returns a copy of the underlying os.File, set to blocking mode. |
| 254 | // It is the caller's responsibility to close f when finished. |
| 255 | // Closing c does not affect f, and closing f does not affect c. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 256 | func (c *UDPConn) File() (f *os.File, err error) { return c.fd.dup() } |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 257 | |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 258 | // JoinGroup joins the IP multicast group named by addr on ifi, |
| 259 | // which specifies the interface to join. JoinGroup uses the |
| 260 | // default multicast interface if ifi is nil. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 261 | func (c *UDPConn) JoinGroup(ifi *Interface, addr IP) error { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 262 | if !c.ok() { |
| 263 | return os.EINVAL |
| 264 | } |
| 265 | ip := addr.To4() |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 266 | if ip != nil { |
| 267 | return joinIPv4GroupUDP(c, ifi, ip) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 268 | } |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 269 | return joinIPv6GroupUDP(c, ifi, addr) |
| 270 | } |
| 271 | |
| 272 | // LeaveGroup exits the IP multicast group named by addr on ifi. |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 273 | func (c *UDPConn) LeaveGroup(ifi *Interface, addr IP) error { |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 274 | if !c.ok() { |
| 275 | return os.EINVAL |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 276 | } |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 277 | ip := addr.To4() |
| 278 | if ip != nil { |
| 279 | return leaveIPv4GroupUDP(c, ifi, ip) |
| 280 | } |
| 281 | return leaveIPv6GroupUDP(c, ifi, addr) |
| 282 | } |
| 283 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 284 | func joinIPv4GroupUDP(c *UDPConn, ifi *Interface, ip IP) error { |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 285 | mreq := &syscall.IPMreq{Multiaddr: [4]byte{ip[0], ip[1], ip[2], ip[3]}} |
| 286 | if err := setIPv4InterfaceToJoin(mreq, ifi); err != nil { |
| 287 | return &OpError{"joinipv4group", "udp", &IPAddr{ip}, err} |
| 288 | } |
| 289 | if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPMreq(c.fd.sysfd, syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq)); err != nil { |
| 290 | return &OpError{"joinipv4group", "udp", &IPAddr{ip}, err} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 291 | } |
| 292 | return nil |
| 293 | } |
| 294 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 295 | func leaveIPv4GroupUDP(c *UDPConn, ifi *Interface, ip IP) error { |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 296 | mreq := &syscall.IPMreq{Multiaddr: [4]byte{ip[0], ip[1], ip[2], ip[3]}} |
| 297 | if err := setIPv4InterfaceToJoin(mreq, ifi); err != nil { |
| 298 | return &OpError{"leaveipv4group", "udp", &IPAddr{ip}, err} |
| 299 | } |
| 300 | if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPMreq(c.fd.sysfd, syscall.IPPROTO_IP, syscall.IP_DROP_MEMBERSHIP, mreq)); err != nil { |
| 301 | return &OpError{"leaveipv4group", "udp", &IPAddr{ip}, err} |
| 302 | } |
| 303 | return nil |
| 304 | } |
| 305 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 306 | func setIPv4InterfaceToJoin(mreq *syscall.IPMreq, ifi *Interface) error { |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 307 | if ifi == nil { |
| 308 | return nil |
| 309 | } |
| 310 | ifat, err := ifi.Addrs() |
| 311 | if err != nil { |
| 312 | return err |
| 313 | } |
| 314 | for _, ifa := range ifat { |
| 315 | if x := ifa.(*IPAddr).IP.To4(); x != nil { |
| 316 | copy(mreq.Interface[:], x) |
| 317 | break |
| 318 | } |
| 319 | } |
| 320 | if bytes.Equal(mreq.Multiaddr[:], IPv4zero) { |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 321 | return os.EINVAL |
| 322 | } |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 323 | return nil |
| 324 | } |
| 325 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 326 | func joinIPv6GroupUDP(c *UDPConn, ifi *Interface, ip IP) error { |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 327 | mreq := &syscall.IPv6Mreq{} |
| 328 | copy(mreq.Multiaddr[:], ip) |
| 329 | if ifi != nil { |
| 330 | mreq.Interface = uint32(ifi.Index) |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 331 | } |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 332 | if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPv6Mreq(c.fd.sysfd, syscall.IPPROTO_IPV6, syscall.IPV6_JOIN_GROUP, mreq)); err != nil { |
| 333 | return &OpError{"joinipv6group", "udp", &IPAddr{ip}, err} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 334 | } |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 335 | return nil |
| 336 | } |
| 337 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame^] | 338 | func leaveIPv6GroupUDP(c *UDPConn, ifi *Interface, ip IP) error { |
Mikio Hara | fca5082 | 2011-08-18 12:22:02 -0400 | [diff] [blame] | 339 | mreq := &syscall.IPv6Mreq{} |
| 340 | copy(mreq.Multiaddr[:], ip) |
| 341 | if ifi != nil { |
| 342 | mreq.Interface = uint32(ifi.Index) |
| 343 | } |
| 344 | if err := os.NewSyscallError("setsockopt", syscall.SetsockoptIPv6Mreq(c.fd.sysfd, syscall.IPPROTO_IPV6, syscall.IPV6_LEAVE_GROUP, mreq)); err != nil { |
| 345 | return &OpError{"leaveipv6group", "udp", &IPAddr{ip}, err} |
Fazlul Shahriar | 0f7bc92 | 2011-08-17 13:28:29 -0400 | [diff] [blame] | 346 | } |
| 347 | return nil |
| 348 | } |