aboutsummaryrefslogtreecommitdiff
path: root/cmd/vhost/main.go
blob: 2ecbfa7f9d2865632d486f1ba616450be5f231c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main

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()
		
		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
						}
						err = ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr)
						if err != nil {
							fmt.Println(err)
							continue
						}
						continue
					}
					err = ipstack.SendIP(ipstack.GetMyVIP(), iface, 0, messageToSendBytes, ipAddr)
					if err != nil {
						fmt.Println(err)
						continue
					}
				}
			}
		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)
		}
	}
}