diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-10-23 01:48:23 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-10-23 01:48:23 -0400 |
commit | 8f5877a04b3c82f5c298b87f6a0f6356d2acffcd (patch) | |
tree | 3e1a8b3e8d505ea917a3b0c2dc9f571ef617726b /pkg/ipstack | |
parent | dfb6e62b2484660730e271f5568d1366bd439864 (diff) |
potentially implement reverse poision. fix port sending numbers. fix bug secing bug from router static routes.
Diffstat (limited to 'pkg/ipstack')
-rw-r--r-- | pkg/ipstack/ipstack.go | 92 |
1 files changed, 54 insertions, 38 deletions
diff --git a/pkg/ipstack/ipstack.go b/pkg/ipstack/ipstack.go index 7a5c235..9aaee28 100644 --- a/pkg/ipstack/ipstack.go +++ b/pkg/ipstack/ipstack.go @@ -337,26 +337,26 @@ func DebugNeighbors() { } } -// TODO @ MICHAEL: -// func RemoveNeighbor(neighbor Neighbor) { -// // TODO: remove from routing table -// myRoutes := GetRoutes() -// for prefix, hop := range myRoutes { -// if hop.VipAsStr == neighbor.VipAddr.String() { -// delete(myRoutes, prefix) -// } -// } - -// // TODO: remove from myNeighbors -// myNeighbors[neighbor.VipAddr.String()] = nil - -// // TODO: close the UDP socket -// err := neighbor.SendSocket.Close() -// if err != nil { -// fmt.Println("Error closing UDP socket", err) -// } - -// } +//// TODO @ MICHAEL: +//func RemoveNeighbor(neighbor Neighbor) { +// // TODO: remove from routing table +// myRoutes := GetRoutes() +// for prefix, hop := range myRoutes { +// if hop.VipAsStr == neighbor.VipAddr.String() { +// delete(myRoutes, prefix) +// } +// } +// +// // TODO: remove from myNeighbors +// myNeighbors[neighbor.VipAddr.String()] = nil +// +// // TODO: close the UDP socket +// err := neighbor.SendSocket.Close() +// if err != nil { +// fmt.Println("Error closing UDP socket", err) +// } +// +//} // untested function above // ************************************** BASIC FUNCTIONS ********************************************************** @@ -441,14 +441,19 @@ func SendIP(src *netip.Addr, dest *Neighbor, protocolNum int, message []byte, de bytesToSend = append(bytesToSend, []byte(message)...) sendAddr, err := net.ResolveUDPAddr("udp4", dest.UdpAddr.String()) - tmpConn, err := net.DialUDP("udp4", nil, sendAddr) + // tmpConn, err := net.DialUDP("udp4", nil, sendAddr) + // get the interface of this neighbor if err != nil { return errors.WithMessage(err, "Could not bind to UDP port->\t"+dest.UdpAddr.String()) } - bytesWritten, err := tmpConn.Write(bytesToSend) + // bytesWritten, err := tmpConn.Write(bytesToSend) + // TODO: make this faster by removing call + iface, err := GetInterfaceByName(dest.Name) + bytesWritten, err := iface.RecvSocket.WriteToUDP(bytesToSend, sendAddr) if err != nil { - return err + fmt.Println("Error writing to UDP socket") + return errors.WithMessage(err, "Error writing to UDP socket") } fmt.Printf("Sent %d bytes to %s\n", bytesWritten, dest.VipAddr.String()) @@ -585,28 +590,36 @@ func RecvIP(iface *Interface, isOpen *bool) error { func periodicUpdateRoutine() { for { // for each periodic update, we want to send our nodes in the table - entries := make([]RIPEntry, len(routingTable)) - for prefix, hop := range routingTable { - entries = append(entries, - RIPEntry{ - address: ConvertIPToUint32(prefix.Addr().String()), - mask: uint32(prefix.Bits()), - cost: hop.Cost, - }) - // fmt.Printf("Sending RIP update: %s\t%d\t%d\n", prefix.Addr().String(), uint32(prefix.Bits()), hop.Cost) - } - - // send to each neighbor for _, iface := range myInterfaces { for _, n := range myNeighbors[iface.Name] { _, in := myRIPNeighbors[n.VipAddr.String()] if !in { continue } + // TODO: consider making this multithreaded and loops above more efficient + + // if we're here, we are sending this to a rip neighbor + entries := make([]RIPEntry, len(routingTable)) + for prefix, hop := range routingTable { + // implement split horizon + poison reverse at entry level + var cost uint32 + if hop.VIP == n.VipAddr { + cost = INFINITY + } else { + cost = hop.Cost + } + entries = append(entries, + RIPEntry{ + address: ConvertIPToUint32(prefix.Addr().String()), + mask: uint32(prefix.Bits()), + cost: cost, + }) + } + message := NewRIPMessage(2, entries) err := SendRIPMessage(*iface, n, message) if err != nil { - // fmt.Printf("Error sending RIP message to %s\n", n.VipAddr.String()) + fmt.Printf("Error sending RIP message to %s\n", n.VipAddr.String()) continue } } @@ -846,11 +859,14 @@ func SendRIPMessage(src Interface, dest *Neighbor, message *RIPMessage) error { // send RIP message sendAddr, err := net.ResolveUDPAddr("udp4", dest.UdpAddr.String()) - tmpConn, err := net.DialUDP("udp4", nil, sendAddr) + // tmpConn, err := net.DialUDP("udp4", nil, sendAddr) if err != nil { return errors.WithMessage(err, "Could not bind to UDP port->\t"+dest.UdpAddr.String()) } - _, err = tmpConn.Write(bytesToSend) + + iface, err := GetInterfaceByName(dest.Name) + //_, err = tmpConn.Write(bytesToSend) + _, err = iface.RecvSocket.WriteToUDP(bytesToSend, sendAddr) if err != nil { return err } |