package main import ( "bufio" "fmt" "os" "time" "iptcp/pkg/ipstack" "strings" ) func main() { if len(os.Args) != 2 { fmt.Printf("Usage: %s \n", os.Args[0]) os.Exit(1) } fileName := os.Args[1] go ipstack.Initialize(fileName) // TODO @ MICHAEL: Dont know why its not running instantly go func() { for { ipstack.RequestRip() time.Sleep(5 * time.Second) } }() // TODO @ MICHEAL // go ipstack.CheckAndUpdateRoutingTable() scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { line := scanner.Text() 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" { ifaceName := line[5:] ipstack.InterfaceDownREPL(ifaceName) } 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 } // 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 } // 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 { if line[:2] == "up" { // get interface name ifaceName := line[3:] ipstack.InterfaceUpREPL(ifaceName) } } continue } } }