blob: 1ba57a227e952dcb5ed5aecce3c97d19d082d955 [file] [log] [blame]
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -04001// 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
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -04005package net
6
7import (
Russ Coxeb692922011-11-01 22:05:34 -04008 "errors"
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -04009 "os"
Mikio Hara03d4c7c2012-02-17 10:59:30 +090010 "syscall"
Brad Fitzpatrickef6806f2012-11-08 10:35:16 -060011 "time"
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040012)
13
Mikio Harae2c453e2013-03-31 16:47:54 +090014// UDPConn is the implementation of the Conn and PacketConn interfaces
15// for UDP network connections.
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040016type UDPConn struct {
Mikio Hara306afc72012-11-13 16:18:37 +090017 conn
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040018}
19
Mikio Harae2c453e2013-03-31 16:47:54 +090020func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
Akshat Kumar66b69a12013-02-19 17:11:17 -080021
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040022// ReadFromUDP reads a UDP packet from c, copying the payload into b.
23// It returns the number of bytes copied into b and the return address
24// that was on the packet.
25//
Mikio Harad6665bc2012-09-26 16:11:49 +090026// ReadFromUDP can be made to time out and return an error with
27// Timeout() == true after a fixed time limit; see SetDeadline and
28// SetReadDeadline.
Russ Coxeb692922011-11-01 22:05:34 -040029func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
Akshat Kumar66b69a12013-02-19 17:11:17 -080030 if !c.ok() || c.fd.data == nil {
Rob Pike56069f02012-02-17 10:04:29 +110031 return 0, nil, syscall.EINVAL
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040032 }
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040033 buf := make([]byte, udpHeaderSize+len(b))
Mikio Hara306afc72012-11-13 16:18:37 +090034 m, err := c.fd.data.Read(buf)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040035 if err != nil {
Mikio Haraafd2d2b2015-04-21 22:53:47 +090036 return 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040037 }
38 if m < udpHeaderSize {
Mikio Haraafd2d2b2015-04-21 22:53:47 +090039 return 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: errors.New("short read reading UDP header")}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040040 }
41 buf = buf[:m]
42
43 h, buf := unmarshalUDPHeader(buf)
44 n = copy(b, buf)
Mikio Harae8cf49f2012-11-27 00:45:42 +090045 return n, &UDPAddr{IP: h.raddr, Port: int(h.rport)}, nil
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040046}
47
Mikio Hara3d400db2012-01-29 19:11:05 +090048// ReadFrom implements the PacketConn ReadFrom method.
Anthony Martin253ed022012-11-30 11:41:50 -080049func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040050 if !c.ok() {
Rob Pike56069f02012-02-17 10:04:29 +110051 return 0, nil, syscall.EINVAL
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040052 }
53 return c.ReadFromUDP(b)
54}
55
Mikio Harad6665bc2012-09-26 16:11:49 +090056// ReadMsgUDP reads a packet from c, copying the payload into b and
Robert Griesemer0ad265d2013-03-20 16:32:37 -070057// the associated out-of-band data into oob. It returns the number
Mikio Harad6665bc2012-09-26 16:11:49 +090058// of bytes copied into b, the number of bytes copied into oob, the
59// flags that were set on the packet and the source address of the
60// packet.
61func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
Mikio Haraafd2d2b2015-04-21 22:53:47 +090062 return 0, 0, 0, nil, &OpError{Op: "read", Net: c.fd.dir, Source: c.fd.laddr, Addr: c.fd.raddr, Err: syscall.EPLAN9}
Mikio Harad6665bc2012-09-26 16:11:49 +090063}
64
65// WriteToUDP writes a UDP packet to addr via c, copying the payload
66// from b.
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040067//
Mikio Harad6665bc2012-09-26 16:11:49 +090068// WriteToUDP can be made to time out and return an error with
69// Timeout() == true after a fixed time limit; see SetDeadline and
70// SetWriteDeadline. On packet-oriented connections, write timeouts
71// are rare.
Anthony Martin253ed022012-11-30 11:41:50 -080072func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
Akshat Kumar66b69a12013-02-19 17:11:17 -080073 if !c.ok() || c.fd.data == nil {
Rob Pike56069f02012-02-17 10:04:29 +110074 return 0, syscall.EINVAL
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040075 }
Mikio Hara7917b882013-08-18 19:19:36 +090076 if addr == nil {
Mikio Hara22829bd2015-05-30 07:33:16 +090077 return 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: nil, Err: errMissingAddress}
Mikio Hara7917b882013-08-18 19:19:36 +090078 }
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040079 h := new(udpHeader)
80 h.raddr = addr.IP.To16()
Mikio Hara306afc72012-11-13 16:18:37 +090081 h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040082 h.ifcaddr = IPv6zero // ignored (receive only)
83 h.rport = uint16(addr.Port)
Mikio Hara306afc72012-11-13 16:18:37 +090084 h.lport = uint16(c.fd.laddr.(*UDPAddr).Port)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040085
86 buf := make([]byte, udpHeaderSize+len(b))
87 i := copy(buf, h.Bytes())
88 copy(buf[i:], b)
Mikio Hara27575542015-04-21 23:15:12 +090089 if _, err := c.fd.data.Write(buf); err != nil {
Mikio Hara22829bd2015-05-30 07:33:16 +090090 return 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
Mikio Hara11b5f982015-04-16 11:26:44 +090091 }
Mikio Hara27575542015-04-21 23:15:12 +090092 return len(b), nil
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040093}
94
Mikio Hara3d400db2012-01-29 19:11:05 +090095// WriteTo implements the PacketConn WriteTo method.
Anthony Martin253ed022012-11-30 11:41:50 -080096func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040097 if !c.ok() {
Rob Pike56069f02012-02-17 10:04:29 +110098 return 0, syscall.EINVAL
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -040099 }
100 a, ok := addr.(*UDPAddr)
101 if !ok {
Mikio Haraafd2d2b2015-04-21 22:53:47 +0900102 return 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400103 }
104 return c.WriteToUDP(b, a)
105}
106
Mikio Haraa9a67572015-02-10 19:27:29 +0900107// WriteMsgUDP writes a packet to addr via c if c isn't connected, or
108// to c's remote destination address if c is connected (in which case
109// addr must be nil). The payload is copied from b and the associated
110// out-of-band data is copied from oob. It returns the number of
111// payload and out-of-band bytes written.
Mikio Harad6665bc2012-09-26 16:11:49 +0900112func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
Mikio Hara22829bd2015-05-30 07:33:16 +0900113 return 0, 0, &OpError{Op: "write", Net: c.fd.dir, Source: c.fd.laddr, Addr: addr.opAddr(), Err: syscall.EPLAN9}
Mikio Harad6665bc2012-09-26 16:11:49 +0900114}
115
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400116// DialUDP connects to the remote address raddr on the network net,
Mikio Harad6665bc2012-09-26 16:11:49 +0900117// which must be "udp", "udp4", or "udp6". If laddr is not nil, it is
118// used as the local address for the connection.
Anthony Martin253ed022012-11-30 11:41:50 -0800119func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
Brad Fitzpatrickef6806f2012-11-08 10:35:16 -0600120 return dialUDP(net, laddr, raddr, noDeadline)
121}
122
Mikio Hara306afc72012-11-13 16:18:37 +0900123func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) {
Brad Fitzpatrickef6806f2012-11-08 10:35:16 -0600124 if !deadline.IsZero() {
125 panic("net.dialUDP: deadline not implemented on Plan 9")
126 }
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400127 switch net {
128 case "udp", "udp4", "udp6":
129 default:
Mikio Hara22829bd2015-05-30 07:33:16 +0900130 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(net)}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400131 }
132 if raddr == nil {
Mikio Hara22829bd2015-05-30 07:33:16 +0900133 return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400134 }
Mikio Hara306afc72012-11-13 16:18:37 +0900135 fd, err := dialPlan9(net, laddr, raddr)
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400136 if err != nil {
Mikio Hara306afc72012-11-13 16:18:37 +0900137 return nil, err
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400138 }
Akshat Kumar66b69a12013-02-19 17:11:17 -0800139 return newUDPConn(fd), nil
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400140}
141
142const udpHeaderSize = 16*3 + 2*2
143
144type udpHeader struct {
145 raddr, laddr, ifcaddr IP
146 rport, lport uint16
147}
148
149func (h *udpHeader) Bytes() []byte {
150 b := make([]byte, udpHeaderSize)
151 i := 0
152 i += copy(b[i:i+16], h.raddr)
153 i += copy(b[i:i+16], h.laddr)
154 i += copy(b[i:i+16], h.ifcaddr)
155 b[i], b[i+1], i = byte(h.rport>>8), byte(h.rport), i+2
156 b[i], b[i+1], i = byte(h.lport>>8), byte(h.lport), i+2
157 return b
158}
159
160func unmarshalUDPHeader(b []byte) (*udpHeader, []byte) {
161 h := new(udpHeader)
162 h.raddr, b = IP(b[:16]), b[16:]
163 h.laddr, b = IP(b[:16]), b[16:]
164 h.ifcaddr, b = IP(b[:16]), b[16:]
165 h.rport, b = uint16(b[0])<<8|uint16(b[1]), b[2:]
166 h.lport, b = uint16(b[0])<<8|uint16(b[1]), b[2:]
167 return h, b
168}
169
Mikio Harad6665bc2012-09-26 16:11:49 +0900170// ListenUDP listens for incoming UDP packets addressed to the local
Mikio Hara245dc622013-03-29 15:06:43 +0900171// address laddr. Net must be "udp", "udp4", or "udp6". If laddr has
172// a port of 0, ListenUDP will choose an available port.
173// The LocalAddr method of the returned UDPConn can be used to
174// discover the port. The returned connection's ReadFrom and WriteTo
Mikio Harad6665bc2012-09-26 16:11:49 +0900175// methods can be used to receive and send UDP packets with per-packet
176// addressing.
Mikio Hara306afc72012-11-13 16:18:37 +0900177func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400178 switch net {
179 case "udp", "udp4", "udp6":
180 default:
Mikio Hara22829bd2015-05-30 07:33:16 +0900181 return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400182 }
183 if laddr == nil {
Mikio Hara677c6e62012-11-13 12:56:28 +0900184 laddr = &UDPAddr{}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400185 }
186 l, err := listenPlan9(net, laddr)
187 if err != nil {
Mikio Hara306afc72012-11-13 16:18:37 +0900188 return nil, err
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400189 }
190 _, err = l.ctl.WriteString("headers")
191 if err != nil {
Mikio Haraafd2d2b2015-04-21 22:53:47 +0900192 return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr, Err: err}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400193 }
Akshat Kumar66b69a12013-02-19 17:11:17 -0800194 l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0)
195 if err != nil {
Mikio Haraafd2d2b2015-04-21 22:53:47 +0900196 return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr, Err: err}
Akshat Kumar66b69a12013-02-19 17:11:17 -0800197 }
Jeff Sickel52125732014-01-22 22:21:53 +0100198 fd, err := l.netFD()
199 return newUDPConn(fd), err
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400200}
201
Mikio Hara2f63afd2012-02-01 01:53:26 +0900202// ListenMulticastUDP listens for incoming multicast UDP packets
Mikio Hara49894be2015-05-07 19:10:29 +0900203// addressed to the group address gaddr on the interface ifi.
204// Network must be "udp", "udp4" or "udp6".
205// ListenMulticastUDP uses the system-assigned multicast interface
206// when ifi is nil, although this is not recommended because the
207// assignment depends on platforms and sometimes it might require
208// routing configuration.
209//
210// ListenMulticastUDP is just for convenience of simple, small
211// applications. There are golang.org/x/net/ipv4 and
212// golang.org/x/net/ipv6 packages for general purpose uses.
213func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
Mikio Hara22829bd2015-05-30 07:33:16 +0900214 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: syscall.EPLAN9}
Fazlul Shahriar0f7bc922011-08-17 13:28:29 -0400215}