2011-08-17 11:28:29 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2011-11-01 20:05:34 -06:00
|
|
|
"errors"
|
2011-08-17 11:28:29 -06:00
|
|
|
"os"
|
2012-02-16 18:59:30 -07:00
|
|
|
"syscall"
|
2012-11-08 09:35:16 -07:00
|
|
|
"time"
|
2011-08-17 11:28:29 -06:00
|
|
|
)
|
|
|
|
|
2013-03-31 01:47:54 -06:00
|
|
|
// UDPConn is the implementation of the Conn and PacketConn interfaces
|
|
|
|
// for UDP network connections.
|
2011-08-17 11:28:29 -06:00
|
|
|
type UDPConn struct {
|
2012-11-13 00:18:37 -07:00
|
|
|
conn
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2013-03-31 01:47:54 -06:00
|
|
|
func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
|
2013-02-19 18:11:17 -07:00
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// ReadFromUDP reads a UDP packet from c, copying the payload into b.
|
|
|
|
// It returns the number of bytes copied into b and the return address
|
|
|
|
// that was on the packet.
|
|
|
|
//
|
2012-09-26 01:11:49 -06:00
|
|
|
// ReadFromUDP can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetReadDeadline.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
|
2013-02-19 18:11:17 -07:00
|
|
|
if !c.ok() || c.fd.data == nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
buf := make([]byte, udpHeaderSize+len(b))
|
2012-11-13 00:18:37 -07:00
|
|
|
m, err := c.fd.data.Read(buf)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2015-04-16 08:10:56 -06:00
|
|
|
return 0, nil, &OpError{Op: "read", Net: c.fd.net, Addr: c.fd.laddr, Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if m < udpHeaderSize {
|
2015-04-16 08:10:56 -06:00
|
|
|
return 0, nil, &OpError{Op: "read", Net: c.fd.net, Addr: c.fd.laddr, Err: errors.New("short read reading UDP header")}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
buf = buf[:m]
|
|
|
|
|
|
|
|
h, buf := unmarshalUDPHeader(buf)
|
|
|
|
n = copy(b, buf)
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return n, &UDPAddr{IP: h.raddr, Port: int(h.rport)}, nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-01-29 03:11:05 -07:00
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
2012-11-30 12:41:50 -07:00
|
|
|
func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return c.ReadFromUDP(b)
|
|
|
|
}
|
|
|
|
|
2012-09-26 01:11:49 -06:00
|
|
|
// ReadMsgUDP reads a packet from c, copying the payload into b and
|
2013-03-20 17:32:37 -06:00
|
|
|
// the associated out-of-band data into oob. It returns the number
|
2012-09-26 01:11:49 -06:00
|
|
|
// of bytes copied into b, the number of bytes copied into oob, the
|
|
|
|
// flags that were set on the packet and the source address of the
|
|
|
|
// packet.
|
|
|
|
func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
|
2015-04-16 08:10:56 -06:00
|
|
|
return 0, 0, 0, nil, &OpError{Op: "read", Net: c.fd.net, Addr: c.fd.laddr, Err: syscall.EPLAN9}
|
2012-09-26 01:11:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToUDP writes a UDP packet to addr via c, copying the payload
|
|
|
|
// from b.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
2012-09-26 01:11:49 -06:00
|
|
|
// WriteToUDP can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetWriteDeadline. On packet-oriented connections, write timeouts
|
|
|
|
// are rare.
|
2012-11-30 12:41:50 -07:00
|
|
|
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
|
2013-02-19 18:11:17 -07:00
|
|
|
if !c.ok() || c.fd.data == nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.dir, Addr: nil, Err: errMissingAddress}
|
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
h := new(udpHeader)
|
|
|
|
h.raddr = addr.IP.To16()
|
2012-11-13 00:18:37 -07:00
|
|
|
h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
|
2011-08-17 11:28:29 -06:00
|
|
|
h.ifcaddr = IPv6zero // ignored (receive only)
|
|
|
|
h.rport = uint16(addr.Port)
|
2012-11-13 00:18:37 -07:00
|
|
|
h.lport = uint16(c.fd.laddr.(*UDPAddr).Port)
|
2011-08-17 11:28:29 -06:00
|
|
|
|
|
|
|
buf := make([]byte, udpHeaderSize+len(b))
|
|
|
|
i := copy(buf, h.Bytes())
|
|
|
|
copy(buf[i:], b)
|
2012-11-13 00:18:37 -07:00
|
|
|
return c.fd.data.Write(buf)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-01-29 03:11:05 -07:00
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
2012-11-30 12:41:50 -07:00
|
|
|
func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
a, ok := addr.(*UDPAddr)
|
|
|
|
if !ok {
|
2012-11-13 00:18:37 -07:00
|
|
|
return 0, &OpError{"write", c.fd.dir, addr, syscall.EINVAL}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return c.WriteToUDP(b, a)
|
|
|
|
}
|
|
|
|
|
2015-02-10 03:27:29 -07:00
|
|
|
// WriteMsgUDP writes a packet to addr via c if c isn't connected, or
|
|
|
|
// to c's remote destination address if c is connected (in which case
|
|
|
|
// addr must be nil). The payload is copied from b and the associated
|
|
|
|
// out-of-band data is copied from oob. It returns the number of
|
|
|
|
// payload and out-of-band bytes written.
|
2012-09-26 01:11:49 -06:00
|
|
|
func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
|
|
|
|
return 0, 0, syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// DialUDP connects to the remote address raddr on the network net,
|
2012-09-26 01:11:49 -06:00
|
|
|
// which must be "udp", "udp4", or "udp6". If laddr is not nil, it is
|
|
|
|
// used as the local address for the connection.
|
2012-11-30 12:41:50 -07:00
|
|
|
func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
|
2012-11-08 09:35:16 -07:00
|
|
|
return dialUDP(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
|
|
|
|
2012-11-13 00:18:37 -07:00
|
|
|
func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) {
|
2012-11-08 09:35:16 -07:00
|
|
|
if !deadline.IsZero() {
|
|
|
|
panic("net.dialUDP: deadline not implemented on Plan 9")
|
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if raddr == nil {
|
2012-01-23 10:59:43 -07:00
|
|
|
return nil, &OpError{"dial", net, nil, errMissingAddress}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
fd, err := dialPlan9(net, laddr, raddr)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2012-11-13 00:18:37 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-02-19 18:11:17 -07:00
|
|
|
return newUDPConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const udpHeaderSize = 16*3 + 2*2
|
|
|
|
|
|
|
|
type udpHeader struct {
|
|
|
|
raddr, laddr, ifcaddr IP
|
|
|
|
rport, lport uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *udpHeader) Bytes() []byte {
|
|
|
|
b := make([]byte, udpHeaderSize)
|
|
|
|
i := 0
|
|
|
|
i += copy(b[i:i+16], h.raddr)
|
|
|
|
i += copy(b[i:i+16], h.laddr)
|
|
|
|
i += copy(b[i:i+16], h.ifcaddr)
|
|
|
|
b[i], b[i+1], i = byte(h.rport>>8), byte(h.rport), i+2
|
|
|
|
b[i], b[i+1], i = byte(h.lport>>8), byte(h.lport), i+2
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalUDPHeader(b []byte) (*udpHeader, []byte) {
|
|
|
|
h := new(udpHeader)
|
|
|
|
h.raddr, b = IP(b[:16]), b[16:]
|
|
|
|
h.laddr, b = IP(b[:16]), b[16:]
|
|
|
|
h.ifcaddr, b = IP(b[:16]), b[16:]
|
|
|
|
h.rport, b = uint16(b[0])<<8|uint16(b[1]), b[2:]
|
|
|
|
h.lport, b = uint16(b[0])<<8|uint16(b[1]), b[2:]
|
|
|
|
return h, b
|
|
|
|
}
|
|
|
|
|
2012-09-26 01:11:49 -06:00
|
|
|
// ListenUDP listens for incoming UDP packets addressed to the local
|
2013-03-29 00:06:43 -06:00
|
|
|
// address laddr. Net must be "udp", "udp4", or "udp6". If laddr has
|
|
|
|
// a port of 0, ListenUDP will choose an available port.
|
|
|
|
// The LocalAddr method of the returned UDPConn can be used to
|
|
|
|
// discover the port. The returned connection's ReadFrom and WriteTo
|
2012-09-26 01:11:49 -06:00
|
|
|
// methods can be used to receive and send UDP packets with per-packet
|
|
|
|
// addressing.
|
2012-11-13 00:18:37 -07:00
|
|
|
func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if laddr == nil {
|
2012-11-12 20:56:28 -07:00
|
|
|
laddr = &UDPAddr{}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
l, err := listenPlan9(net, laddr)
|
|
|
|
if err != nil {
|
2012-11-13 00:18:37 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
_, err = l.ctl.WriteString("headers")
|
|
|
|
if err != nil {
|
2012-11-13 00:18:37 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-02-19 18:11:17 -07:00
|
|
|
l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-01-22 14:21:53 -07:00
|
|
|
fd, err := l.netFD()
|
|
|
|
return newUDPConn(fd), err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-01-31 09:53:26 -07:00
|
|
|
// ListenMulticastUDP listens for incoming multicast UDP packets
|
2012-09-26 01:11:49 -06:00
|
|
|
// addressed to the group address gaddr on ifi, which specifies the
|
|
|
|
// interface to join. ListenMulticastUDP uses default multicast
|
|
|
|
// interface if ifi is nil.
|
2012-01-31 09:53:26 -07:00
|
|
|
func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
|
2012-02-16 18:59:30 -07:00
|
|
|
return nil, syscall.EPLAN9
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|