Retro68/gcc/libgo/go/net/ipsock_plan9.go

246 lines
5.8 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00: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.
2014-09-21 17:33:12 +00:00
// Internet protocol family sockets for Plan 9
2012-03-27 23:13:14 +00:00
package net
import (
"os"
"syscall"
)
2015-08-28 15:33:40 +00:00
func probe(filename, query string) bool {
var file *file
var err error
if file, err = open(filename); err != nil {
return false
}
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
}
}
}
file.close()
return r
}
2014-09-21 17:33:12 +00:00
func probeIPv4Stack() bool {
2015-08-28 15:33:40 +00:00
return probe(netdir+"/iproute", "4i")
2014-09-21 17:33:12 +00:00
}
// probeIPv6Stack returns two boolean values. If the first boolean
// value is true, kernel supports basic IPv6 functionality. If the
// second boolean value is true, kernel supports IPv6 IPv4-mapping.
2012-03-27 23:13:14 +00:00
func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
2015-08-28 15:33:40 +00:00
// Plan 9 uses IPv6 natively, see ip(3).
r := probe(netdir+"/iproute", "6i")
v := false
if r {
v = probe(netdir+"/iproute", "4i")
}
return r, v
2012-03-27 23:13:14 +00:00
}
// parsePlan9Addr parses address of the form [ip!]port (e.g. 127.0.0.1!80).
func parsePlan9Addr(s string) (ip IP, iport int, err error) {
addr := IPv4zero // address contains port only
i := byteIndex(s, '!')
if i >= 0 {
addr = ParseIP(s[:i])
if addr == nil {
2017-04-10 11:32:00 +00:00
return nil, 0, &ParseError{Type: "IP address", Text: s}
2012-03-27 23:13:14 +00:00
}
}
p, _, ok := dtoi(s[i+1:], 0)
if !ok {
2017-04-10 11:32:00 +00:00
return nil, 0, &ParseError{Type: "port", Text: s}
2012-03-27 23:13:14 +00:00
}
if p < 0 || p > 0xFFFF {
2017-04-10 11:32:00 +00:00
return nil, 0, &AddrError{Err: "invalid port", Addr: string(p)}
2012-03-27 23:13:14 +00:00
}
return addr, p, nil
}
func readPlan9Addr(proto, filename string) (addr Addr, err error) {
var buf [128]byte
f, err := os.Open(filename)
if err != nil {
return
}
2014-09-21 17:33:12 +00:00
defer f.Close()
2012-03-27 23:13:14 +00: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":
2014-09-21 17:33:12 +00:00
addr = &TCPAddr{IP: ip, Port: port}
2012-03-27 23:13:14 +00:00
case "udp":
2014-09-21 17:33:12 +00:00
addr = &UDPAddr{IP: ip, Port: port}
2012-03-27 23:13:14 +00:00
default:
2017-04-10 11:32:00 +00:00
return nil, UnknownNetworkError(proto)
2012-03-27 23:13:14 +00:00
}
return addr, nil
}
func startPlan9(net string, addr Addr) (ctl *os.File, dest, proto, name string, err error) {
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
}
clone, dest, err := queryCS1(proto, ip, port)
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 {
2014-09-21 17:33:12 +00:00
f.Close()
2012-03-27 23:13:14 +00:00
return
}
return f, dest, proto, string(buf[:n]), nil
}
2014-09-21 17:33:12 +00:00
func netErr(e error) {
oe, ok := e.(*OpError)
if !ok {
return
}
2017-04-10 11:32:00 +00: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
}
2014-09-21 17:33:12 +00:00
if pe, ok := oe.Err.(*os.PathError); ok {
if _, ok = pe.Err.(syscall.ErrorString); ok {
oe.Err = pe.Err
}
}
}
func dialPlan9(net string, laddr, raddr Addr) (fd *netFD, err error) {
defer func() { netErr(err) }()
2012-03-27 23:13:14 +00:00
f, dest, proto, name, err := startPlan9(net, raddr)
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "dial", Net: net, Source: laddr, Addr: raddr, Err: err}
2012-03-27 23:13:14 +00:00
}
_, err = f.WriteString("connect " + dest)
if err != nil {
2014-09-21 17:33:12 +00:00
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "dial", Net: f.Name(), Source: laddr, Addr: raddr, Err: err}
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
data, err := os.OpenFile(netdir+"/"+proto+"/"+name+"/data", os.O_RDWR, 0)
2012-03-27 23:13:14 +00:00
if err != nil {
2014-09-21 17:33:12 +00:00
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "dial", Net: net, Source: laddr, Addr: raddr, Err: err}
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
laddr, err = readPlan9Addr(proto, netdir+"/"+proto+"/"+name+"/local")
2012-03-27 23:13:14 +00:00
if err != nil {
2014-09-21 17:33:12 +00:00
data.Close()
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "dial", Net: proto, Source: laddr, Addr: raddr, Err: err}
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
return newFD(proto, name, f, data, laddr, raddr)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
func listenPlan9(net string, laddr Addr) (fd *netFD, err error) {
defer func() { netErr(err) }()
2012-03-27 23:13:14 +00:00
f, dest, proto, name, err := startPlan9(net, laddr)
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr, Err: err}
2012-03-27 23:13:14 +00:00
}
_, err = f.WriteString("announce " + dest)
if err != nil {
2014-09-21 17:33:12 +00:00
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "announce", Net: proto, Source: nil, Addr: laddr, Err: err}
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
laddr, err = readPlan9Addr(proto, netdir+"/"+proto+"/"+name+"/local")
2012-03-27 23:13:14 +00:00
if err != nil {
2014-09-21 17:33:12 +00:00
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr, Err: err}
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
return newFD(proto, name, f, nil, laddr, nil)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
func (fd *netFD) netFD() (*netFD, error) {
return newFD(fd.net, fd.n, fd.ctl, fd.data, fd.laddr, fd.raddr)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
func (fd *netFD) acceptPlan9() (nfd *netFD, err error) {
2014-09-21 17:33:12 +00:00
defer func() { netErr(err) }()
2017-04-10 11:32:00 +00:00
if err := fd.readLock(); err != nil {
2015-08-28 15:33:40 +00:00
return nil, err
}
2017-04-10 11:32:00 +00:00
defer fd.readUnlock()
f, err := os.Open(fd.dir + "/listen")
2012-03-27 23:13:14 +00:00
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "accept", Net: fd.dir + "/listen", Source: nil, Addr: fd.laddr, Err: err}
2012-03-27 23:13:14 +00:00
}
var buf [16]byte
n, err := f.Read(buf[:])
if err != nil {
2014-09-21 17:33:12 +00:00
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "accept", Net: fd.dir + "/listen", Source: nil, Addr: fd.laddr, Err: err}
2012-03-27 23:13:14 +00:00
}
name := string(buf[:n])
2017-04-10 11:32:00 +00:00
data, err := os.OpenFile(netdir+"/"+fd.net+"/"+name+"/data", os.O_RDWR, 0)
2012-03-27 23:13:14 +00:00
if err != nil {
2014-09-21 17:33:12 +00:00
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "accept", Net: fd.net, Source: nil, Addr: fd.laddr, Err: err}
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
raddr, err := readPlan9Addr(fd.net, netdir+"/"+fd.net+"/"+name+"/remote")
2012-03-27 23:13:14 +00:00
if err != nil {
2014-09-21 17:33:12 +00:00
data.Close()
f.Close()
2017-04-10 11:32:00 +00:00
return nil, &OpError{Op: "accept", Net: fd.net, Source: nil, Addr: fd.laddr, Err: err}
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return newFD(fd.net, name, f, data, fd.laddr, raddr)
2012-03-27 23:13:14 +00:00
}