diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/vhost/main.go | 97 | ||||
-rw-r--r-- | cmd/vrouter/main.go | 72 |
2 files changed, 84 insertions, 85 deletions
diff --git a/cmd/vhost/main.go b/cmd/vhost/main.go index 2ecbfa7..f5016a1 100644 --- a/cmd/vhost/main.go +++ b/cmd/vhost/main.go @@ -24,72 +24,79 @@ func main() { for scanner.Scan() { line := scanner.Text() - - if line == "li" { + switch line { + case "li": fmt.Println("Name\tAddr/Prefix\tState") ipstack.SprintInterfaces() - } - - if line == "ln" { + case "ln": fmt.Println("Iface\tVIP\tUDPAddr") ipstack.SprintNeighbors() - } - - if line == "lr" { + case "lr": fmt.Println("T\tPrefix\tNext Hop\tCost") ipstack.SprintRoutingTable() - } - - if len(line) > 4 { - if line[:4] == "down" { - // get interface name - ifaceName := line[5:] - ipstack.InterfaceDownREPL(ifaceName) - } + case "q": + ipstack.CleanUp() + os.Exit(0) + default: + if len(line) > 4 { + if line[:4] == "down" { + 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 line[:4] == "send" { + // get IP address and message that follows it + IPAndMessage := strings.Split(line, " ") + ipAddr := IPAndMessage[1] + message := IPAndMessage[2:] + + // combine message into one string + messageToSend := strings.Join(message, " ") + messageToSendBytes := []byte(messageToSend) + + // check if ipAddr is in neighbor table + iface, err := ipstack.GetNeighborByIP(ipAddr) if err != nil { fmt.Println(err) + + // check if ipAddr is in routing table iface, err = ipstack.GetRouteByIP(ipAddr) if err != nil { fmt.Println(err) continue } - err = ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr) - if err != nil { - fmt.Println(err) - continue + + for _, interfaces := range ipstack.GetInterfaces() { + if interfaces.Name == iface.Name { + err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr) + if err != nil { + fmt.Println(err) + } + break + } } continue } - err = ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr) - if err != nil { - fmt.Println(err) - continue + for _, interfaces := range ipstack.GetInterfaces() { + if interfaces.Name == iface.Name { + err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr) + if err != nil { + fmt.Println(err) + } + break + } } + continue } } - if len(line) > 2 { - if line[:2] == "up" { - // get interface name - ifaceName := line[3:] - ipstack.InterfaceUpREPL(ifaceName) + if len(line) > 2 { + if line[:2] == "up" { + // get interface name + ifaceName := line[3:] + ipstack.InterfaceUpREPL(ifaceName) + } } - } - - if line == "q" { - ipstack.CleanUp() - os.Exit(0) + continue } } }
\ No newline at end of file diff --git a/cmd/vrouter/main.go b/cmd/vrouter/main.go index 89f6e5d..1239ca1 100644 --- a/cmd/vrouter/main.go +++ b/cmd/vrouter/main.go @@ -10,26 +10,6 @@ import ( ) -// func SendUpdates() { -// myInterfaces := ipstack.GetInterfaces() -// myNeighbors := ipstack.GetNeighbors() -// for _, iface := range myInterfaces { -// // send RIP updates to all neighbors -// for _, _ = range myNeighbors { -// // iface.udp.Write(neighbor, data) -// // iface.RecvSocket.Write(neighbor, data) -// // wait for response for 12 seconds -// response := make([]byte, 512) -// iface.RecvSocket.Read(response) -// time.Sleep(12 * time.Second) -// if len(response) == 0 { -// // ipstack.RemoveNeighbor(neighbor) -// } -// } -// } -// time.Sleep(5 * time.Second) -// } - func main() { if len(os.Args) != 2 { fmt.Printf("Usage: %s <configFile>\n", os.Args[0]) @@ -38,20 +18,18 @@ func main() { fileName := os.Args[1] - // initialize(fileName) go ipstack.Initialize(fileName) - // myInterfaces := ipstack.GetInterfaces() - // myNeighbors := ipstack.GetNeighbors() - // myRoutes := ipstack.GetRoutes() + // TODO @ MICHAEL: Dont know why its not running instantly go func() { for { - ipstack.SendUpdates() + ipstack.RequestRip() time.Sleep(5 * time.Second) } }() - go ipstack.CheckAndUpdateRoutingTable() + // TODO @ MICHEAL + // go ipstack.CheckAndUpdateRoutingTable() scanner := bufio.NewScanner(os.Stdin) @@ -73,41 +51,55 @@ func main() { 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:] + IPAndMessage := strings.Split(line, " ") + ipAddr := IPAndMessage[1] + message := IPAndMessage[2:] + // combine message into one string messageToSend := strings.Join(message, " ") - // convert message to byte array messageToSendBytes := []byte(messageToSend) - // get interface by ipAddr + + // check if ipAddr is in neighbor table iface, err := ipstack.GetNeighborByIP(ipAddr) if err != nil { fmt.Println(err) + + // check if ipAddr is in routing table iface, err = ipstack.GetRouteByIP(ipAddr) if err != nil { fmt.Println(err) continue } - err = ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr) - if err != nil { - fmt.Println(err) - continue + + // get the interface to send from + for _, interfaces := range ipstack.GetInterfaces() { + if interfaces.Name == iface.Name { + err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr) + if err != nil { + fmt.Println(err) + } + break + } } continue } - err = ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr) - if err != nil { - fmt.Println(err) - continue + // neighbor was found, send to neighbor + for _, interfaces := range ipstack.GetInterfaces() { + if interfaces.Name == iface.Name { + err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr) + if err != nil { + fmt.Println(err) + } + break + } } + continue } } if len(line) > 2 { |