1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // IP sockets stubs for Plan 9
17 // probeIPv6Stack returns two boolean values. If the first boolean value is
18 // true, kernel supports basic IPv6 functionality. If the second
19 // boolean value is true, kernel supports IPv6 IPv4-mapping.
20 func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
24 // parsePlan9Addr parses address of the form [ip!]port (e.g. 127.0.0.1!80).
25 func parsePlan9Addr(s string) (ip IP, iport int, err error) {
26 addr := IPv4zero // address contains port only
27 i := byteIndex(s, '!')
31 return nil, 0, errors.New("net: parsing IP failed")
34 p, _, ok := dtoi(s[i+1:], 0)
36 return nil, 0, errors.New("net: parsing port failed")
38 if p < 0 || p > 0xFFFF {
39 return nil, 0, &AddrError{"invalid port", string(p)}
44 func readPlan9Addr(proto, filename string) (addr Addr, err error) {
47 f, err := os.Open(filename)
51 n, err := f.Read(buf[:])
55 ip, port, err := parsePlan9Addr(string(buf[:n]))
61 addr = &TCPAddr{ip, port}
63 addr = &UDPAddr{ip, port}
65 return nil, errors.New("unknown protocol " + proto)
70 type plan9Conn struct {
71 proto, name, dir string
76 func newPlan9Conn(proto, name string, ctl *os.File, laddr, raddr Addr) *plan9Conn {
77 return &plan9Conn{proto, name, "/net/" + proto + "/" + name, ctl, nil, laddr, raddr}
80 func (c *plan9Conn) ok() bool { return c != nil && c.ctl != nil }
82 // Implementation of the Conn interface - see Conn for documentation.
84 // Read implements the Conn Read method.
85 func (c *plan9Conn) Read(b []byte) (n int, err error) {
87 return 0, syscall.EINVAL
90 c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
95 n, err = c.data.Read(b)
96 if c.proto == "udp" && err == io.EOF {
103 // Write implements the Conn Write method.
104 func (c *plan9Conn) Write(b []byte) (n int, err error) {
106 return 0, syscall.EINVAL
109 c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
114 return c.data.Write(b)
117 // Close closes the connection.
118 func (c *plan9Conn) Close() error {
120 return syscall.EINVAL
134 // LocalAddr returns the local network address.
135 func (c *plan9Conn) LocalAddr() Addr {
142 // RemoteAddr returns the remote network address.
143 func (c *plan9Conn) RemoteAddr() Addr {
150 // SetDeadline implements the Conn SetDeadline method.
151 func (c *plan9Conn) SetDeadline(t time.Time) error {
152 return syscall.EPLAN9
155 // SetReadDeadline implements the Conn SetReadDeadline method.
156 func (c *plan9Conn) SetReadDeadline(t time.Time) error {
157 return syscall.EPLAN9
160 // SetWriteDeadline implements the Conn SetWriteDeadline method.
161 func (c *plan9Conn) SetWriteDeadline(t time.Time) error {
162 return syscall.EPLAN9
165 func startPlan9(net string, addr Addr) (ctl *os.File, dest, proto, name string, err error) {
170 switch a := addr.(type) {
180 err = UnknownNetworkError(net)
184 clone, dest, err := queryCS1(proto, ip, port)
188 f, err := os.OpenFile(clone, os.O_RDWR, 0)
193 n, err := f.Read(buf[:])
197 return f, dest, proto, string(buf[:n]), nil
200 func dialPlan9(net string, laddr, raddr Addr) (c *plan9Conn, err error) {
201 f, dest, proto, name, err := startPlan9(net, raddr)
205 _, err = f.WriteString("connect " + dest)
209 laddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/local")
213 raddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/remote")
217 return newPlan9Conn(proto, name, f, laddr, raddr), nil
220 type plan9Listener struct {
221 proto, name, dir string
226 func listenPlan9(net string, laddr Addr) (l *plan9Listener, err error) {
227 f, dest, proto, name, err := startPlan9(net, laddr)
231 _, err = f.WriteString("announce " + dest)
235 laddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/local")
239 l = new(plan9Listener)
242 l.dir = "/net/" + proto + "/" + name
248 func (l *plan9Listener) plan9Conn() *plan9Conn {
249 return newPlan9Conn(l.proto, l.name, l.ctl, l.laddr, nil)
252 func (l *plan9Listener) acceptPlan9() (c *plan9Conn, err error) {
253 f, err := os.Open(l.dir + "/listen")
258 n, err := f.Read(buf[:])
262 name := string(buf[:n])
263 laddr, err := readPlan9Addr(l.proto, l.dir+"/local")
267 raddr, err := readPlan9Addr(l.proto, l.dir+"/remote")
271 return newPlan9Conn(l.proto, name, f, laddr, raddr), nil
274 func (l *plan9Listener) Accept() (c Conn, err error) {
275 c1, err := l.acceptPlan9()
282 func (l *plan9Listener) Close() error {
283 if l == nil || l.ctl == nil {
284 return syscall.EINVAL
289 func (l *plan9Listener) Addr() Addr { return l.laddr }