diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/ipstack/ipstack.go | 46 |
1 files changed, 41 insertions, 5 deletions
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 } |