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
13 // probeIPv6Stack returns two boolean values. If the first boolean value is
14 // true, kernel supports basic IPv6 functionality. If the second
15 // boolean value is true, kernel supports IPv6 IPv4-mapping.
16 func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
20 // parsePlan9Addr parses address of the form [ip!]port (e.g. 127.0.0.1!80).
21 func parsePlan9Addr(s string) (ip IP, iport int, err os.Error) {
27 addr = IPv4zero // address contains port only
32 err = os.NewError("net: parsing IP failed")
36 p, _, ok = dtoi(s[i+1:], 0)
38 err = os.NewError("net: parsing port failed")
41 if p < 0 || p > 0xFFFF {
42 err = &AddrError{"invalid port", string(p)}
51 func readPlan9Addr(proto, filename string) (addr Addr, err os.Error) {
54 f, err := os.Open(filename)
58 n, err := f.Read(buf[:])
62 ip, port, err := parsePlan9Addr(string(buf[:n]))
68 addr = &TCPAddr{ip, port}
70 addr = &UDPAddr{ip, port}
72 return nil, os.NewError("unknown protocol " + proto)
77 type plan9Conn struct {
78 proto, name, dir string
83 func newPlan9Conn(proto, name string, ctl *os.File, laddr, raddr Addr) *plan9Conn {
84 return &plan9Conn{proto, name, "/net/" + proto + "/" + name, ctl, nil, laddr, raddr}
87 func (c *plan9Conn) ok() bool { return c != nil && c.ctl != nil }
89 // Implementation of the Conn interface - see Conn for documentation.
91 // Read implements the net.Conn Read method.
92 func (c *plan9Conn) Read(b []byte) (n int, err os.Error) {
97 c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
102 n, err = c.data.Read(b)
103 if c.proto == "udp" && err == os.EOF {
110 // Write implements the net.Conn Write method.
111 func (c *plan9Conn) Write(b []byte) (n int, err os.Error) {
116 c.data, err = os.OpenFile(c.dir+"/data", os.O_RDWR, 0)
121 return c.data.Write(b)
124 // Close closes the connection.
125 func (c *plan9Conn) Close() os.Error {
141 // LocalAddr returns the local network address.
142 func (c *plan9Conn) LocalAddr() Addr {
149 // RemoteAddr returns the remote network address.
150 func (c *plan9Conn) RemoteAddr() Addr {
157 // SetTimeout implements the net.Conn SetTimeout method.
158 func (c *plan9Conn) SetTimeout(nsec int64) os.Error {
165 // SetReadTimeout implements the net.Conn SetReadTimeout method.
166 func (c *plan9Conn) SetReadTimeout(nsec int64) os.Error {
173 // SetWriteTimeout implements the net.Conn SetWriteTimeout method.
174 func (c *plan9Conn) SetWriteTimeout(nsec int64) os.Error {
181 func startPlan9(net string, addr Addr) (ctl *os.File, dest, proto, name string, err os.Error) {
186 switch a := addr.(type) {
196 err = UnknownNetworkError(net)
200 clone, dest, err := queryCS1(proto, ip, port)
204 f, err := os.OpenFile(clone, os.O_RDWR, 0)
209 n, err := f.Read(buf[:])
213 return f, dest, proto, string(buf[:n]), nil
216 func dialPlan9(net string, laddr, raddr Addr) (c *plan9Conn, err os.Error) {
217 f, dest, proto, name, err := startPlan9(net, raddr)
221 _, err = f.WriteString("connect " + dest)
225 laddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/local")
229 raddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/remote")
233 return newPlan9Conn(proto, name, f, laddr, raddr), nil
236 type plan9Listener struct {
237 proto, name, dir string
242 func listenPlan9(net string, laddr Addr) (l *plan9Listener, err os.Error) {
243 f, dest, proto, name, err := startPlan9(net, laddr)
247 _, err = f.WriteString("announce " + dest)
251 laddr, err = readPlan9Addr(proto, "/net/"+proto+"/"+name+"/local")
255 l = new(plan9Listener)
258 l.dir = "/net/" + proto + "/" + name
264 func (l *plan9Listener) plan9Conn() *plan9Conn {
265 return newPlan9Conn(l.proto, l.name, l.ctl, l.laddr, nil)
268 func (l *plan9Listener) acceptPlan9() (c *plan9Conn, err os.Error) {
269 f, err := os.Open(l.dir + "/listen")
274 n, err := f.Read(buf[:])
278 name := string(buf[:n])
279 laddr, err := readPlan9Addr(l.proto, l.dir+"/local")
283 raddr, err := readPlan9Addr(l.proto, l.dir+"/remote")
287 return newPlan9Conn(l.proto, name, f, laddr, raddr), nil
290 func (l *plan9Listener) Accept() (c Conn, err os.Error) {
291 c1, err := l.acceptPlan9()
298 func (l *plan9Listener) Close() os.Error {
299 if l == nil || l.ctl == nil {
305 func (l *plan9Listener) Addr() Addr { return l.laddr }