2017-10-07 00:16:47 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2012-03-27 23:13:14 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2017-10-07 00:16:47 +00:00
|
|
|
"context"
|
2012-03-27 23:13:14 +00:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func (c *UDPConn) readFrom(b []byte) (n int, addr *UDPAddr, err error) {
|
2012-03-27 23:13:14 +00:00
|
|
|
buf := make([]byte, udpHeaderSize+len(b))
|
2017-10-07 00:16:47 +00:00
|
|
|
m, err := c.fd.Read(buf)
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
2017-10-07 00:16:47 +00:00
|
|
|
return 0, nil, err
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
if m < udpHeaderSize {
|
2017-10-07 00:16:47 +00:00
|
|
|
return 0, nil, errors.New("short read reading UDP header")
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
buf = buf[:m]
|
|
|
|
|
|
|
|
h, buf := unmarshalUDPHeader(buf)
|
|
|
|
n = copy(b, buf)
|
2014-09-21 17:33:12 +00:00
|
|
|
return n, &UDPAddr{IP: h.raddr, Port: int(h.rport)}, nil
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func (c *UDPConn) readMsg(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
|
|
|
|
return 0, 0, 0, nil, syscall.EPLAN9
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func (c *UDPConn) writeTo(b []byte, addr *UDPAddr) (int, error) {
|
2014-09-21 17:33:12 +00:00
|
|
|
if addr == nil {
|
2017-10-07 00:16:47 +00:00
|
|
|
return 0, errMissingAddress
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
h := new(udpHeader)
|
|
|
|
h.raddr = addr.IP.To16()
|
2014-09-21 17:33:12 +00:00
|
|
|
h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
|
2012-03-27 23:13:14 +00:00
|
|
|
h.ifcaddr = IPv6zero // ignored (receive only)
|
|
|
|
h.rport = uint16(addr.Port)
|
2014-09-21 17:33:12 +00:00
|
|
|
h.lport = uint16(c.fd.laddr.(*UDPAddr).Port)
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
buf := make([]byte, udpHeaderSize+len(b))
|
|
|
|
i := copy(buf, h.Bytes())
|
|
|
|
copy(buf[i:], b)
|
2017-10-07 00:16:47 +00:00
|
|
|
if _, err := c.fd.Write(buf); err != nil {
|
|
|
|
return 0, err
|
2017-04-10 11:32:00 +00:00
|
|
|
}
|
|
|
|
return len(b), nil
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 00:16:47 +00:00
|
|
|
func (c *UDPConn) writeMsg(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
|
|
|
|
return 0, 0, syscall.EPLAN9
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func (sd *sysDialer) dialUDP(ctx context.Context, laddr, raddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
fd, err := dialPlan9(ctx, sd.network, laddr, raddr)
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
2014-09-21 17:33:12 +00:00
|
|
|
return nil, err
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2014-09-21 17:33:12 +00:00
|
|
|
return newUDPConn(fd), nil
|
2012-03-27 23:13:14 +00: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
|
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func (sl *sysListener) listenUDP(ctx context.Context, laddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
l, err := listenPlan9(ctx, sl.network, laddr)
|
2012-03-27 23:13:14 +00:00
|
|
|
if err != nil {
|
2014-09-21 17:33:12 +00:00
|
|
|
return nil, err
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
_, err = l.ctl.WriteString("headers")
|
|
|
|
if err != nil {
|
2017-10-07 00:16:47 +00:00
|
|
|
return nil, err
|
2014-09-21 17:33:12 +00:00
|
|
|
}
|
|
|
|
l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
2017-10-07 00:16:47 +00:00
|
|
|
return nil, err
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
2015-08-28 15:33:40 +00:00
|
|
|
fd, err := l.netFD()
|
|
|
|
return newUDPConn(fd), err
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 15:48:37 +00:00
|
|
|
func (sl *sysListener) listenMulticastUDP(ctx context.Context, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
l, err := listenPlan9(ctx, sl.network, gaddr)
|
2017-10-07 00:16:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = l.ctl.WriteString("headers")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var addrs []Addr
|
|
|
|
if ifi != nil {
|
|
|
|
addrs, err = ifi.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addrs, err = InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if ipnet, ok := addr.(*IPNet); ok {
|
|
|
|
_, err = l.ctl.WriteString("addmulti " + ipnet.IP.String() + " " + gaddr.IP.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fd, err := l.netFD()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newUDPConn(fd), nil
|
2012-03-27 23:13:14 +00:00
|
|
|
}
|