diff options
-rw-r--r-- | cmd/vhost/main.go | 37 | ||||
-rw-r--r-- | cmd/vrouter/main.go | 38 | ||||
-rw-r--r-- | pkg/ipstack/ipstack.go | 46 | ||||
-rwxr-xr-x | vhost | bin | 3100397 -> 3102340 bytes | |||
-rwxr-xr-x | vrouter | bin | 3100405 -> 3102620 bytes |
5 files changed, 41 insertions, 80 deletions
diff --git a/cmd/vhost/main.go b/cmd/vhost/main.go index 549b1de..17b4a02 100644 --- a/cmd/vhost/main.go +++ b/cmd/vhost/main.go @@ -69,43 +69,6 @@ func main() { } } } - // - // // 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 - // } - // - // 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 - // } - // 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 { diff --git a/cmd/vrouter/main.go b/cmd/vrouter/main.go index ec9a891..f274741 100644 --- a/cmd/vrouter/main.go +++ b/cmd/vrouter/main.go @@ -86,44 +86,6 @@ func main() { } } } - - //// 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 { diff --git a/pkg/ipstack/ipstack.go b/pkg/ipstack/ipstack.go index 4a552cf..5160ab2 100644 --- a/pkg/ipstack/ipstack.go +++ b/pkg/ipstack/ipstack.go @@ -10,6 +10,7 @@ import ( "log" "net" "net/netip" + "sync" "time" // "bytes" // "unicode" @@ -616,16 +617,44 @@ func periodicUpdateRoutine() { } } -//timeoutTable := make(map[netip.Addr]time.Time) -//func manageTimeoutsRoutine() { -// for -//} +var mu sync.Mutex +var timeoutTable = make(map[netip.Prefix]int) +var MAX_TIMEOUT = 12 + +func manageTimeoutsRoutine() { + for { + time.Sleep(time.Second) + + wg := &sync.WaitGroup{} + wg.Add(len(timeoutTable)) + mu.Lock() + for prefix, _ := range timeoutTable { + go func(p netip.Prefix) { + timeoutTable[p]++ + if timeoutTable[p] == MAX_TIMEOUT { + delete(routingTable, p) + delete(timeoutTable, p) + } + + wg.Done() + }(prefix) + } + wg.Wait() + mu.Unlock() + + // fmt.Println("Timeout table: ", timeoutTable) + } +} func startRipRoutines() { // send a request to every neighbor go func() { for _, iface := range myInterfaces { for _, neighbor := range myNeighbors[iface.Name] { + _, in := myRIPNeighbors[neighbor.VipAddr.String()] + if !in { + continue + } // send a request message := NewRIPMessage(1, nil) err := SendRIPMessage(*iface, neighbor, message) @@ -639,7 +668,7 @@ func startRipRoutines() { go periodicUpdateRoutine() // make a "timeout" table, for each response we add to the table via rip - // go manageTimeoutsRoutine() + go manageTimeoutsRoutine() // start a routine that sends updates every 10 seconds } @@ -692,6 +721,8 @@ func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.I continue } + fmt.Println(address) + prefix := netip.PrefixFrom(netip.MustParseAddr(address), int(entry.mask)) // fmt.Println(prefix.String()) @@ -700,6 +731,11 @@ func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.I if entry.cost < node.Cost { routingTable[prefix.Masked()] = Hop{entry.cost + 1, "R", src, hdr.Src} } + if node.Type == "R" { + mu.Lock() + timeoutTable[prefix.Masked()] = 0 + mu.Unlock() + } continue } |