|
| 1 | +// This is a very simple dut program. It builds into one binary to implement |
| 2 | +// both client and server. It's just easier to see both sides of the code and test |
| 3 | +// that way. |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "flag" |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "log" |
| 11 | + "net" |
| 12 | + "net/rpc" |
| 13 | + "os" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/u-root/u-root/pkg/ulog" |
| 17 | + "golang.org/x/sys/unix" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + debug = flag.Bool("d", false, "Enable debug prints") |
| 22 | + host = flag.String("host", "192.168.0.1", "hostname") |
| 23 | + klog = flag.Bool("klog", false, "Direct all logging to klog -- depends on debug") |
| 24 | + port = flag.String("port", "8080", "port number") |
| 25 | + dir = flag.String("dir", ".", "directory to serve") |
| 26 | + |
| 27 | + // for debug |
| 28 | + v = func(string, ...interface{}) {} |
| 29 | +) |
| 30 | + |
| 31 | +func dutStart(t, host, port string) (net.Listener, error) { |
| 32 | + ln, err := net.Listen(t, host+":"+port) |
| 33 | + if err != nil { |
| 34 | + log.Print(err) |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + log.Printf("Listening on %v at %v", ln.Addr(), time.Now()) |
| 38 | + return ln, nil |
| 39 | +} |
| 40 | + |
| 41 | +func dutAccept(l net.Listener) (net.Conn, error) { |
| 42 | + if err := l.(*net.TCPListener).SetDeadline(time.Now().Add(3 * time.Minute)); err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + c, err := l.Accept() |
| 46 | + if err != nil { |
| 47 | + log.Printf("Listen failed: %v at %v", err, time.Now()) |
| 48 | + log.Print(err) |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + log.Printf("Accepted %v", c) |
| 52 | + return c, nil |
| 53 | +} |
| 54 | + |
| 55 | +func dutRPC(host, port string) error { |
| 56 | + l, err := dutStart("tcp", host, port) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + c, err := dutAccept(l) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + cl := rpc.NewClient(c) |
| 65 | + for _, cmd := range []struct { |
| 66 | + call string |
| 67 | + args interface{} |
| 68 | + }{ |
| 69 | + {"Command.Welcome", &RPCWelcome{}}, |
| 70 | + {"Command.Reboot", &RPCReboot{}}, |
| 71 | + } { |
| 72 | + var r RPCRes |
| 73 | + if err := cl.Call(cmd.call, cmd.args, &r); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + fmt.Printf("%v(%v): %v\n", cmd.call, cmd.args, string(r.C)) |
| 77 | + } |
| 78 | + |
| 79 | + if c, err = dutAccept(l); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + cl = rpc.NewClient(c) |
| 83 | + var r RPCRes |
| 84 | + if err := cl.Call("Command.Welcome", &RPCWelcome{}, &r); err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + fmt.Printf("%v(%v): %v\n", "Command.Welcome", nil, string(r.C)) |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func dutcpu(host, port, pubkey, hostkey, cpuport string) error { |
| 93 | + var req = &RPCCPU{Port: cpuport} |
| 94 | + var err error |
| 95 | + |
| 96 | + // we send the pubkey and hostkey as the value of the key, not the |
| 97 | + // name of the file. |
| 98 | + // TODO: maybe use ssh_config to find keys? the cpu client can do that. |
| 99 | + // Note: the public key is not optional. That said, we do not test |
| 100 | + // for len(*pubKey) > 0; if it is set to ""< ReadFile will return |
| 101 | + // an error. |
| 102 | + if req.PubKey, err = ioutil.ReadFile(pubkey); err != nil { |
| 103 | + return fmt.Errorf("Reading pubKey:%w", err) |
| 104 | + } |
| 105 | + if len(hostkey) > 0 { |
| 106 | + if req.HostKey, err = ioutil.ReadFile(hostkey); err != nil { |
| 107 | + return fmt.Errorf("Reading hostKey:%w", err) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + l, err := dutStart("tcp", host, port) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + c, err := dutAccept(l) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + cl := rpc.NewClient(c) |
| 122 | + |
| 123 | + for _, cmd := range []struct { |
| 124 | + call string |
| 125 | + args interface{} |
| 126 | + }{ |
| 127 | + {"Command.Welcome", &RPCWelcome{}}, |
| 128 | + {"Command.Welcome", &RPCWelcome{}}, |
| 129 | + {"Command.CPU", req}, |
| 130 | + } { |
| 131 | + var r RPCRes |
| 132 | + if err := cl.Call(cmd.call, cmd.args, &r); err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + fmt.Printf("%v(%v): %v\n", cmd.call, cmd.args, string(r.C)) |
| 136 | + } |
| 137 | + return err |
| 138 | +} |
| 139 | + |
| 140 | +func main() { |
| 141 | + // for CPU |
| 142 | + flag.Parse() |
| 143 | + |
| 144 | + if *debug { |
| 145 | + v = log.Printf |
| 146 | + if *klog { |
| 147 | + ulog.KernelLog.Reinit() |
| 148 | + v = ulog.KernelLog.Printf |
| 149 | + } |
| 150 | + } |
| 151 | + a := flag.Args() |
| 152 | + if len(a) == 0 { |
| 153 | + a = []string{"device"} |
| 154 | + } |
| 155 | + |
| 156 | + os.Args = a |
| 157 | + var err error |
| 158 | + v("Mode is %v", a[0]) |
| 159 | + switch a[0] { |
| 160 | + case "tester": |
| 161 | + err = dutRPC(*host, *port) |
| 162 | + case "cpu": |
| 163 | + var ( |
| 164 | + pubKey = flag.String("pubkey", "key.pub", "public key file") |
| 165 | + hostKey = flag.String("hostkey", "", "host key file -- usually empty") |
| 166 | + cpuPort = flag.String("cpuport", "17010", "cpu port -- IANA value is ncpu tcp/17010") |
| 167 | + ) |
| 168 | + v("Parse %v", os.Args) |
| 169 | + flag.Parse() |
| 170 | + v("pubkey %v", *pubKey) |
| 171 | + if err := dutcpu(*host, *port, *pubKey, *hostKey, *cpuPort); err != nil { |
| 172 | + log.Printf("cpu service: %v", err) |
| 173 | + } |
| 174 | + case "device": |
| 175 | + err = uinit(*host, *port) |
| 176 | + // What to do after a return? Reboot I suppose. |
| 177 | + log.Printf("Device returns with error %v", err) |
| 178 | + if err := unix.Reboot(int(unix.LINUX_REBOOT_CMD_RESTART)); err != nil { |
| 179 | + log.Printf("Reboot failed, not sure what to do now.") |
| 180 | + } |
| 181 | + default: |
| 182 | + log.Printf("Unknown mode %v", a[0]) |
| 183 | + } |
| 184 | + log.Printf("We are now done ......................") |
| 185 | + if err != nil { |
| 186 | + log.Printf("%v", err) |
| 187 | + os.Exit(2) |
| 188 | + } |
| 189 | +} |
0 commit comments