aboutsummaryrefslogtreecommitdiff
path: root/cmd/vrouter/main.go
blob: ba7f285ae39e0fed3af1393c6cbcefcacf171818 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main

import (
	"bufio"
	"fmt"
	"iptcp/pkg/ipstack"
	"net/netip"
	"os"
	"strings"
)

func main() {
	if len(os.Args) == 1 {
		fmt.Printf("Usage:  %s <configFile>\n", os.Args[0])
		os.Exit(1)
	}

	fileName := os.Args[2]

	err := ipstack.Initialize(fileName)
	if err != nil {
		return
	}
	ipstack.RegisterProtocolHandler(ipstack.TEST_PROTOCOL)
	ipstack.RegisterProtocolHandler(ipstack.RIP_PROTOCOL)

	// TODO @ MICHAEL: Dont know why its not running instantly
	//go func() {
	//	for {
	//		ipstack.RequestRip()
	//		// takes time to compute I think
	//		// TODO @ MICHAEL
	//		time.Sleep(2 * 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)

					hop, err := ipstack.LongestPrefix(netip.MustParseAddr(ipAddr))
					myAddr := hop.Interface.IpPrefix.Addr()
					for _, neighbor := range ipstack.GetNeighbors()[hop.Interface.Name] {
						if neighbor.VipAddr == netip.MustParseAddr(ipAddr) {
							err = ipstack.SendIP(&myAddr, neighbor, ipstack.TEST_PROTOCOL, messageToSendBytes, ipAddr, nil)
							if err != nil {
								fmt.Println(err)
							}
						}
					}

					//// 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 {
					//			src := interfaces.IpPrefix.Addr()
					//			err = ipstack.SendIP(&src, iface, 0, messageToSendBytes, ipAddr, nil)
					//			if err != nil {
					//				fmt.Println(err)
					//			}
					//			break
					//		}
					//	}
					//	continue
					//}
					//// neighbor was found, send to neighbor
					//for _, interfaces := range ipstack.GetInterfaces() {
					//	if interfaces.Name == iface.Name {
					//		src := interfaces.IpPrefix.Addr()
					//		err = ipstack.SendIP(&src, iface, 0, messageToSendBytes, ipAddr, nil)
					//		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
		}
	}
}