2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2011-09-16 17:47:21 +02: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-07-22 20:15:38 +02:00
|
|
|
"context"
|
2019-01-18 20:04:36 +01:00
|
|
|
"internal/bytealg"
|
2011-09-16 17:47:21 +02:00
|
|
|
"os"
|
2013-07-16 08:54:42 +02:00
|
|
|
"syscall"
|
2011-09-16 17:47:21 +02:00
|
|
|
)
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// Probe probes IPv4, IPv6 and IPv4-mapped IPv6 communication
|
|
|
|
// capabilities.
|
|
|
|
//
|
|
|
|
// Plan 9 uses IPv6 natively, see ip(3).
|
|
|
|
func (p *ipStackCapabilities) probe() {
|
|
|
|
p.ipv4Enabled = probe(netdir+"/iproute", "4i")
|
|
|
|
p.ipv6Enabled = probe(netdir+"/iproute", "6i")
|
|
|
|
if p.ipv4Enabled && p.ipv6Enabled {
|
|
|
|
p.ipv4MappedIPv6Enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
func probe(filename, query string) bool {
|
|
|
|
var file *file
|
|
|
|
var err error
|
|
|
|
if file, err = open(filename); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
defer file.close()
|
2014-06-07 00:37:27 +02:00
|
|
|
|
|
|
|
r := false
|
|
|
|
for line, ok := file.readLine(); ok && !r; line, ok = file.readLine() {
|
|
|
|
f := getFields(line)
|
|
|
|
if len(f) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := 0; i < len(f); i++ {
|
|
|
|
if query == f[i] {
|
|
|
|
r = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// parsePlan9Addr parses address of the form [ip!]port (e.g. 127.0.0.1!80).
|
2011-12-03 03:17:34 +01:00
|
|
|
func parsePlan9Addr(s string) (ip IP, iport int, err error) {
|
2012-02-09 09:19:58 +01:00
|
|
|
addr := IPv4zero // address contains port only
|
2019-01-18 20:04:36 +01:00
|
|
|
i := bytealg.IndexByteString(s, '!')
|
2011-09-16 17:47:21 +02:00
|
|
|
if i >= 0 {
|
|
|
|
addr = ParseIP(s[:i])
|
|
|
|
if addr == nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, 0, &ParseError{Type: "IP address", Text: s}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
p, _, ok := dtoi(s[i+1:])
|
2011-09-16 17:47:21 +02:00
|
|
|
if !ok {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, 0, &ParseError{Type: "port", Text: s}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
if p < 0 || p > 0xFFFF {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, 0, &AddrError{Err: "invalid port", Addr: string(p)}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return addr, p, nil
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func readPlan9Addr(proto, filename string) (addr Addr, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
var buf [128]byte
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
defer f.Close()
|
2011-09-16 17:47:21 +02:00
|
|
|
n, err := f.Read(buf[:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ip, port, err := parsePlan9Addr(string(buf[:n]))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch proto {
|
|
|
|
case "tcp":
|
2012-12-13 00:13:29 +01:00
|
|
|
addr = &TCPAddr{IP: ip, Port: port}
|
2011-09-16 17:47:21 +02:00
|
|
|
case "udp":
|
2012-12-13 00:13:29 +01:00
|
|
|
addr = &UDPAddr{IP: ip, Port: port}
|
2011-09-16 17:47:21 +02:00
|
|
|
default:
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, UnknownNetworkError(proto)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func startPlan9(ctx context.Context, net string, addr Addr) (ctl *os.File, dest, proto, name string, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
var (
|
|
|
|
ip IP
|
|
|
|
port int
|
|
|
|
)
|
|
|
|
switch a := addr.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
proto = "tcp"
|
|
|
|
ip = a.IP
|
|
|
|
port = a.Port
|
|
|
|
case *UDPAddr:
|
|
|
|
proto = "udp"
|
|
|
|
ip = a.IP
|
|
|
|
port = a.Port
|
|
|
|
default:
|
|
|
|
err = UnknownNetworkError(net)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
if port > 65535 {
|
|
|
|
err = InvalidAddrError("port should be < 65536")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
clone, dest, err := queryCS1(ctx, proto, ip, port)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(clone, os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf [16]byte
|
|
|
|
n, err := f.Read(buf[:])
|
|
|
|
if err != nil {
|
2012-10-23 06:31:11 +02:00
|
|
|
f.Close()
|
2011-09-16 17:47:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return f, dest, proto, string(buf[:n]), nil
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func fixErr(err error) {
|
|
|
|
oe, ok := err.(*OpError)
|
2013-07-16 08:54:42 +02:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
nonNilInterface := func(a Addr) bool {
|
|
|
|
switch a := a.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
return a == nil
|
|
|
|
case *UDPAddr:
|
|
|
|
return a == nil
|
|
|
|
case *IPAddr:
|
|
|
|
return a == nil
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nonNilInterface(oe.Source) {
|
|
|
|
oe.Source = nil
|
|
|
|
}
|
|
|
|
if nonNilInterface(oe.Addr) {
|
|
|
|
oe.Addr = nil
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
if pe, ok := oe.Err.(*os.PathError); ok {
|
|
|
|
if _, ok = pe.Err.(syscall.ErrorString); ok {
|
|
|
|
oe.Err = pe.Err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func dialPlan9(ctx context.Context, net string, laddr, raddr Addr) (fd *netFD, err error) {
|
|
|
|
defer func() { fixErr(err) }()
|
|
|
|
type res struct {
|
|
|
|
fd *netFD
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
resc := make(chan res)
|
|
|
|
go func() {
|
|
|
|
testHookDialChannel()
|
|
|
|
fd, err := dialPlan9Blocking(ctx, net, laddr, raddr)
|
|
|
|
select {
|
|
|
|
case resc <- res{fd, err}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
if fd != nil {
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case res := <-resc:
|
|
|
|
return res.fd, res.err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, mapErr(ctx.Err())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialPlan9Blocking(ctx context.Context, net string, laddr, raddr Addr) (fd *netFD, err error) {
|
2017-01-14 01:05:42 +01:00
|
|
|
if isWildcard(raddr) {
|
|
|
|
raddr = toLocal(raddr, net)
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
f, dest, proto, name, err := startPlan9(ctx, net, raddr)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
_, err = f.WriteString("connect " + dest)
|
|
|
|
if err != nil {
|
2012-10-23 06:31:11 +02:00
|
|
|
f.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
data, err := os.OpenFile(netdir+"/"+proto+"/"+name+"/data", os.O_RDWR, 0)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2012-10-23 06:31:11 +02:00
|
|
|
f.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
laddr, err = readPlan9Addr(proto, netdir+"/"+proto+"/"+name+"/local")
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2013-07-16 08:54:42 +02:00
|
|
|
data.Close()
|
2012-10-23 06:31:11 +02:00
|
|
|
f.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
return newFD(proto, name, nil, f, data, laddr, raddr)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func listenPlan9(ctx context.Context, net string, laddr Addr) (fd *netFD, err error) {
|
|
|
|
defer func() { fixErr(err) }()
|
|
|
|
f, dest, proto, name, err := startPlan9(ctx, net, laddr)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
_, err = f.WriteString("announce " + dest)
|
|
|
|
if err != nil {
|
2012-10-23 06:31:11 +02:00
|
|
|
f.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2014-06-07 00:37:27 +02:00
|
|
|
laddr, err = readPlan9Addr(proto, netdir+"/"+proto+"/"+name+"/local")
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2012-10-23 06:31:11 +02:00
|
|
|
f.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
return newFD(proto, name, nil, f, nil, laddr, nil)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (fd *netFD) netFD() (*netFD, error) {
|
2017-01-14 01:05:42 +01:00
|
|
|
return newFD(fd.net, fd.n, fd.listen, fd.ctl, fd.data, fd.laddr, fd.raddr)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (fd *netFD) acceptPlan9() (nfd *netFD, err error) {
|
2016-07-22 20:15:38 +02:00
|
|
|
defer func() { fixErr(err) }()
|
2017-09-14 19:11:35 +02:00
|
|
|
if err := fd.pfd.ReadLock(); err != nil {
|
2014-06-07 00:37:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
defer fd.pfd.ReadUnlock()
|
2017-01-14 01:05:42 +01:00
|
|
|
listen, err := os.Open(fd.dir + "/listen")
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
var buf [16]byte
|
2017-01-14 01:05:42 +01:00
|
|
|
n, err := listen.Read(buf[:])
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2017-01-14 01:05:42 +01:00
|
|
|
listen.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
name := string(buf[:n])
|
2017-01-14 01:05:42 +01:00
|
|
|
ctl, err := os.OpenFile(netdir+"/"+fd.net+"/"+name+"/ctl", os.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
listen.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
data, err := os.OpenFile(netdir+"/"+fd.net+"/"+name+"/data", os.O_RDWR, 0)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2017-01-14 01:05:42 +01:00
|
|
|
listen.Close()
|
|
|
|
ctl.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
raddr, err := readPlan9Addr(fd.net, netdir+"/"+fd.net+"/"+name+"/remote")
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2017-01-14 01:05:42 +01:00
|
|
|
listen.Close()
|
|
|
|
ctl.Close()
|
2013-07-16 08:54:42 +02:00
|
|
|
data.Close()
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2017-01-14 01:05:42 +01:00
|
|
|
return newFD(fd.net, name, listen, ctl, data, fd.laddr, raddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWildcard(a Addr) bool {
|
|
|
|
var wildcard bool
|
|
|
|
switch a := a.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
wildcard = a.isWildcard()
|
|
|
|
case *UDPAddr:
|
|
|
|
wildcard = a.isWildcard()
|
|
|
|
case *IPAddr:
|
|
|
|
wildcard = a.isWildcard()
|
|
|
|
}
|
|
|
|
return wildcard
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLocal(a Addr, net string) Addr {
|
|
|
|
switch a := a.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
a.IP = loopbackIP(net)
|
|
|
|
case *UDPAddr:
|
|
|
|
a.IP = loopbackIP(net)
|
|
|
|
case *IPAddr:
|
|
|
|
a.IP = loopbackIP(net)
|
|
|
|
}
|
|
|
|
return a
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|