2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2011-08-17 11:28:29 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-04-14 18:47:25 -06:00
|
|
|
"context"
|
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"
|
2011-08-17 11:28:29 -06:00
|
|
|
)
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UDPConn) readFrom(b []byte) (n int, addr *UDPAddr, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
buf := make([]byte, udpHeaderSize+len(b))
|
2016-03-02 14:22:11 -07:00
|
|
|
m, err := c.fd.Read(buf)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if m < udpHeaderSize {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, nil, 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
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UDPConn) readMsg(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
|
|
|
|
return 0, 0, 0, nil, syscall.EPLAN9
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UDPConn) writeTo(b []byte, addr *UDPAddr) (int, error) {
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, errMissingAddress
|
2013-08-18 04:19:36 -06:00
|
|
|
}
|
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)
|
2016-03-02 14:22:11 -07:00
|
|
|
if _, err := c.fd.Write(buf); err != nil {
|
|
|
|
return 0, err
|
2015-04-15 20:26:44 -06:00
|
|
|
}
|
2015-04-21 08:15:12 -06:00
|
|
|
return len(b), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UDPConn) writeMsg(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
|
|
|
|
return 0, 0, syscall.EPLAN9
|
2012-11-08 09:35:16 -07:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func dialUDP(ctx context.Context, net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
|
net: fix plan9 after context change, propagate contexts more
My previous https://golang.org/cl/22101 to add context throughout the
net package broke Plan 9, which isn't currently tested (#15251).
It also broke some old unsupported version of Windows (Windows 2000?)
which doesn't have the ConnectEx function, but that was only found
visually, since our minimum supported Windows version has ConnectEx.
This change simplifies the Windows and deletes the non-ConnectEx code
path. Windows 2000 will work even less now, if it even worked
before. Windows XP remains our minimum supported version.
Specifically, the previous CL stopped using the "dial" function, which
0intro noted:
https://github.com/golang/go/issues/15333#issuecomment-210842761
This CL removes the dial function instead and makes plan9's net
implementation respect contexts, which likely fixes a number of
t.Skipped tests. I'm leaving that to 0intro to investigate.
In the process of propagating and respecting contexts for plan9, I had
to change some signatures to add contexts to more places and ended up
pushing contexts down into the Go-based DNS resolution as well,
replacing the pure-Go DNS implementation's use of "timeout
time.Duration" with a context instead.
Updates #11932
Updates #15328
Fixes #15333
Change-Id: I6ad1e62f38271cdd86b3f40921f2d0f23374936a
Reviewed-on: https://go-review.googlesource.com/22144
Reviewed-by: David du Colombier <0intro@gmail.com>
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-16 15:17:40 -06:00
|
|
|
fd, err := dialPlan9(ctx, 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
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenUDP(ctx context.Context, network string, laddr *UDPAddr) (*UDPConn, error) {
|
net: fix plan9 after context change, propagate contexts more
My previous https://golang.org/cl/22101 to add context throughout the
net package broke Plan 9, which isn't currently tested (#15251).
It also broke some old unsupported version of Windows (Windows 2000?)
which doesn't have the ConnectEx function, but that was only found
visually, since our minimum supported Windows version has ConnectEx.
This change simplifies the Windows and deletes the non-ConnectEx code
path. Windows 2000 will work even less now, if it even worked
before. Windows XP remains our minimum supported version.
Specifically, the previous CL stopped using the "dial" function, which
0intro noted:
https://github.com/golang/go/issues/15333#issuecomment-210842761
This CL removes the dial function instead and makes plan9's net
implementation respect contexts, which likely fixes a number of
t.Skipped tests. I'm leaving that to 0intro to investigate.
In the process of propagating and respecting contexts for plan9, I had
to change some signatures to add contexts to more places and ended up
pushing contexts down into the Go-based DNS resolution as well,
replacing the pure-Go DNS implementation's use of "timeout
time.Duration" with a context instead.
Updates #11932
Updates #15328
Fixes #15333
Change-Id: I6ad1e62f38271cdd86b3f40921f2d0f23374936a
Reviewed-on: https://go-review.googlesource.com/22144
Reviewed-by: David du Colombier <0intro@gmail.com>
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-16 15:17:40 -06:00
|
|
|
l, err := listenPlan9(ctx, network, laddr)
|
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
|
|
|
}
|
|
|
|
_, err = l.ctl.WriteString("headers")
|
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -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 {
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2013-02-19 18:11:17 -07:00
|
|
|
}
|
2014-01-22 14:21:53 -07:00
|
|
|
fd, err := l.netFD()
|
|
|
|
return newUDPConn(fd), err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenMulticastUDP(ctx context.Context, network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, syscall.EPLAN9
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|