aboutsummaryrefslogtreecommitdiff
path: root/cmd/vrouter/main.go
diff options
context:
space:
mode:
authorDavid Doan <daviddoan@Davids-MacBook-Pro-70.local>2023-10-19 22:52:11 -0400
committerDavid Doan <daviddoan@Davids-MacBook-Pro-70.local>2023-10-19 22:52:11 -0400
commita7f9da2bb4ce6649ca38ab808a7b263aff9afbd7 (patch)
treea17b32157c7cedde789ea9eecb2d59584b5c8ac8 /cmd/vrouter/main.go
parent1e66c42543ddbd610968a73b30f8646f289da07a (diff)
refactored and commented
Diffstat (limited to 'cmd/vrouter/main.go')
-rw-r--r--cmd/vrouter/main.go72
1 files changed, 32 insertions, 40 deletions
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 {