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 main1112import (13 "bufio"14 "flag"15 "log"16 "net"17)1819var (20 caddr = flag.String("ca", ":2342", "Control server network address")21 paddr = flag.String("pa", ":4223", "9P server network address")22)2324// Handles incoming connections on the control socket.25func handlecc(conn net.Conn) {26 log.Println("New control server connection")2728 scanner := bufio.NewScanner(conn)29 for scanner.Scan() {30 cmd := scanner.Text()3132 log.Printf("Received control command %q\n", cmd)33 if reply, ok := ctlcmds[cmd]; !ok {34 log.Println("Unknown control command")35 replyChan <- failureReply36 } else {37 replyChan <- reply38 }39 }4041 err := scanner.Err()42 if err != nil {43 log.Printf("Scanner error: %_\n", err.Error())44 }45}4647// Handles incoming connection on the protocol socket. Connection are48// 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}5354func main() {55 flag.Parse()5657 cl, err := net.Listen("tcp", *caddr)58 if err != nil {59 panic(err)60 }6162 pl, err := net.Listen("tcp", *paddr)63 if err != nil {64 panic(err)65 }6667 for {68 cc, err := cl.Accept()69 if err != nil {70 log.Printf("Accept: %v\n", err.Error())71 continue72 }73 go handlecc(cc)7475 pc, err := pl.Accept()76 if err != nil {77 log.Printf("Accept: %v\n", err.Error())78 continue79 }80 go handlepc(pc)81 }82}