aboutsummaryrefslogtreecommitdiff
path: root/cmd/vhost/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/vhost/main.go')
-rw-r--r--cmd/vhost/main.go71
1 files changed, 70 insertions, 1 deletions
diff --git a/cmd/vhost/main.go b/cmd/vhost/main.go
index 82b8195..27e073f 100644
--- a/cmd/vhost/main.go
+++ b/cmd/vhost/main.go
@@ -4,14 +4,83 @@ import (
"bufio"
"fmt"
"os"
+ "iptcp/pkg/ipstack"
+ "strings"
)
func main() {
+ if len(os.Args) != 2 {
+ fmt.Printf("Usage: %s <configFile>\n", os.Args[0])
+ os.Exit(1)
+ }
+
+ fileName := os.Args[1]
+
+ go ipstack.Initialize(fileName)
+
+
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
- fmt.Println(line)
+
+ if line == "li" {
+ fmt.Println("Name\tAddr/Prefix\tState")
+ ipstack.SprintInterfaces()
+ }
+
+ if line == "ln" {
+ fmt.Println("Iface\tVIP\tUDPAddr")
+ ipstack.SprintNeighbors()
+ }
+
+ if line == "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)
+ }
+
+ 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)
+ }
+ }
+
+ if line == "q" {
+ ipstack.CleanUp()
+ os.Exit(0)
+ }
}
} \ No newline at end of file