diff options
author | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-10-18 00:14:46 -0400 |
---|---|---|
committer | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-10-18 00:14:46 -0400 |
commit | 1e1f56b0dfe1b95d181ff40eeac0cc4260c937fb (patch) | |
tree | 64033021fe0943f4862a4ff751c92fcc721d3907 /cmd/vrouter/main.go | |
parent | db0a9f0a4605d85ba4e535ba0ab590776cc4ba0a (diff) |
add REPL functionality
Diffstat (limited to 'cmd/vrouter/main.go')
-rw-r--r-- | cmd/vrouter/main.go | 75 |
1 files changed, 69 insertions, 6 deletions
diff --git a/cmd/vrouter/main.go b/cmd/vrouter/main.go index 158016a..abd0fc0 100644 --- a/cmd/vrouter/main.go +++ b/cmd/vrouter/main.go @@ -5,19 +5,25 @@ import ( "fmt" "os" "time" + "iptcp/pkg/ipstack" + "strings" + ) func SendUpdates() { + myInterfaces := ipstack.GetInterfaces() + myNeighbors := ipstack.GetNeighbors() for _, iface := range myInterfaces { // send RIP updates to all neighbors - for _, neighbor := range myNeighbors { - iface.udp.Write(neighbor, data) + for _, _ = range myNeighbors { + // iface.udp.Write(neighbor, data) + // iface.RecvSocket.Write(neighbor, data) // wait for response for 12 seconds response := make([]byte, 512) - iface.udp.Read(response) + iface.RecvSocket.Read(response) time.Sleep(12 * time.Second) if len(response) == 0 { - RemoveNeighbor(neighbor) + // ipstack.RemoveNeighbor(neighbor) } } } @@ -32,7 +38,12 @@ func main() { fileName := os.Args[1] - initialize(fileName) + // initialize(fileName) + go ipstack.Initialize(fileName) + // myInterfaces := ipstack.GetInterfaces() + // myNeighbors := ipstack.GetNeighbors() + // myRoutes := ipstack.GetRoutes() + go SendUpdates() @@ -40,6 +51,58 @@ func main() { for scanner.Scan() { line := scanner.Text() - fmt.Println(line) + switch line { + case "li": + fmt.Println("Name\tAddr/Prefix\tState") + ipstack.SprintInterfaces() + case "ln": + fmt.Println("Iface\tVIP\tUDPAddr") + ipstack.SprintNeighbors() + case "lr": + fmt.Println("T\tPrefix\tNext Hop\tCost") + ipstack.SprintRoutingTable() + case "q": + ipstack.CleanUp() + os.Exit(0) + default: + if len(line) > 4 { + if line[:4] == "down" { + // get interface name + ifaceName := line[5:] + ipstack.InterfaceDownREPL(ifaceName) + } + + if line[:4] == "send" { + // get IP address and message that follows it + listOfWords := strings.Split(line, " ") + ipAddr := listOfWords[1] + message := listOfWords[2:] + // combine message into one string + messageToSend := strings.Join(message, " ") + // convert message to byte array + messageToSendBytes := []byte(messageToSend) + // get interface by ipAddr + iface, err := ipstack.GetNeighborByIP(ipAddr) + if err != nil { + fmt.Println(err) + iface, err = ipstack.GetRouteByIP(ipAddr) + if err != nil { + fmt.Println(err) + continue + } + ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr) + } + ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr) + } + } + if len(line) > 2 { + if line[:2] == "up" { + // get interface name + ifaceName := line[3:] + ipstack.InterfaceUpREPL(ifaceName) + } + } + continue + } } }
\ No newline at end of file |