diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-10-22 06:18:06 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-10-22 06:18:06 -0400 |
commit | a935739d332ccb82174ddb925dcf5e473dfae41f (patch) | |
tree | d6b1d7cd0853e25eec59467be059b958be2e078b /cmd/vrouter/main.go | |
parent | 6618f59d55ae5bbf99426b06e3cdd891f9a0f0c4 (diff) |
rewrite table to be more robust and efficient for sending.
Diffstat (limited to 'cmd/vrouter/main.go')
-rw-r--r-- | cmd/vrouter/main.go | 113 |
1 files changed, 65 insertions, 48 deletions
diff --git a/cmd/vrouter/main.go b/cmd/vrouter/main.go index eb7a1eb..ba7f285 100644 --- a/cmd/vrouter/main.go +++ b/cmd/vrouter/main.go @@ -3,33 +3,37 @@ package main import ( "bufio" "fmt" - "os" - "time" "iptcp/pkg/ipstack" + "net/netip" + "os" "strings" - ) func main() { - if len(os.Args) != 2 { + if len(os.Args) == 1 { fmt.Printf("Usage: %s <configFile>\n", os.Args[0]) os.Exit(1) } - fileName := os.Args[1] + fileName := os.Args[2] - go ipstack.Initialize(fileName) + 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) - } - }() - + //go func() { + // for { + // ipstack.RequestRip() + // // takes time to compute I think + // // TODO @ MICHAEL + // time.Sleep(2 * time.Second) + // } + //}() + // TODO @ MICHEAL // go ipstack.CheckAndUpdateRoutingTable() @@ -66,42 +70,55 @@ func main() { // 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, nil) - if err != nil { - fmt.Println(err) - } - break + 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) } } - 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, nil) - if err != nil { - fmt.Println(err) - } - break - } - } - continue + + //// 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 { @@ -114,4 +131,4 @@ func main() { continue } } -}
\ No newline at end of file +} |