ninenano

Client implementation of the 9P protocol for constrained devices

git clone https://git.8pit.net/ninenano.git

 1// Package main implements a simple 9P server which can be used to test
 2// 9P client implementations. It opens two TCP sockets on given ports.
 3// The first socket is the control socket, the client must send a
 4// test name delimited by a newline character to this socket before
 5// connecting to the second socket (the 9P protocol socket).
 6//
 7// After sending the test name to the server the client can connect to
 8// the 9P protocol socket. The server accepts T-messages on this port
 9// and (depending on the test name) responses with R-messages.
10package main
11
12import (
13	"bufio"
14	"flag"
15	"log"
16	"net"
17)
18
19var (
20	caddr = flag.String("ca", ":2342", "Control server network address")
21	paddr = flag.String("pa", ":4223", "9P server network address")
22)
23
24// Handles incoming connections on the control socket.
25func handlecc(conn net.Conn) {
26	log.Println("New control server connection")
27
28	scanner := bufio.NewScanner(conn)
29	for scanner.Scan() {
30		cmd := scanner.Text()
31
32		log.Printf("Received control command %q\n", cmd)
33		if reply, ok := ctlcmds[cmd]; !ok {
34			log.Println("Unknown control command")
35			replyChan <- failureReply
36		} else {
37			replyChan <- reply
38		}
39	}
40
41	err := scanner.Err()
42	if err != nil {
43		log.Printf("Scanner error: %_\n", err.Error())
44	}
45}
46
47// Handles incoming connection on the protocol socket. Connection are
48// basically just passed to the ninep server.
49func handlepc(conn net.Conn) {
50	log.Println("New protocol server connection")
51	NewServer(log.Printf, conn, conn).Start()
52}
53
54func main() {
55	flag.Parse()
56
57	cl, err := net.Listen("tcp", *caddr)
58	if err != nil {
59		panic(err)
60	}
61
62	pl, err := net.Listen("tcp", *paddr)
63	if err != nil {
64		panic(err)
65	}
66
67	for {
68		cc, err := cl.Accept()
69		if err != nil {
70			log.Printf("Accept: %v\n", err.Error())
71			continue
72		}
73		go handlecc(cc)
74
75		pc, err := pl.Accept()
76		if err != nil {
77			log.Printf("Accept: %v\n", err.Error())
78			continue
79		}
80		go handlepc(pc)
81	}
82}