diff options
author | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-10-23 18:32:05 -0400 |
---|---|---|
committer | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-10-23 18:32:05 -0400 |
commit | c4953632a5ce653301def74b19932c6d9703b174 (patch) | |
tree | a3999e2e5a568e27d3721432eeaa47e0a699a345 /pkg | |
parent | b44583954cb762755e85d58da0a9527fa369c01e (diff) | |
parent | 1f926271bc80bcd6ff5ed52bb76a9ade347367b8 (diff) |
sectioning comments
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/ipstack/ipstack.go | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/pkg/ipstack/ipstack.go b/pkg/ipstack/ipstack.go index 475e569..0cea26f 100644 --- a/pkg/ipstack/ipstack.go +++ b/pkg/ipstack/ipstack.go @@ -80,7 +80,7 @@ var myNeighbors = make(map[string][]*Neighbor) var myRIPNeighbors = make(map[string]*Neighbor) -type HandlerFunc func(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.IPv4Header) error +type HandlerFunc func(src *Interface, message []byte, hdr *ipv4header.IPv4Header) error var protocolHandlers = make(map[int]HandlerFunc) @@ -247,11 +247,11 @@ func InterfaceUp(iface *Interface) { if _, ok := protocolHandlers[RIP_PROTOCOL]; ok { ripEntries := make([]RIPEntry, 0) ripEntries = append(ripEntries, RIPEntry{iface.IpPrefix.Masked(), LOCAL_COST}) - sendTriggeredUpdates(ripEntries) + SendTriggeredUpdates(ripEntries) // send a request to all neighbors of this iface to get info ASAP for _, neighbor := range myNeighbors[iface.Name] { - message := makeRipMessage(1, nil) + message := MakeRipMessage(1, nil) addr := iface.IpPrefix.Addr() _, err := SendIP(&addr, neighbor, RIP_PROTOCOL, message, neighbor.VipAddr.String(), nil) if err != nil { @@ -282,7 +282,7 @@ func InterfaceDown(iface *Interface) { if _, ok := protocolHandlers[RIP_PROTOCOL]; ok { ripEntries := make([]RIPEntry, 0) ripEntries = append(ripEntries, RIPEntry{iface.IpPrefix.Masked(), INFINITY}) - sendTriggeredUpdates(ripEntries) + SendTriggeredUpdates(ripEntries) } } @@ -369,7 +369,7 @@ func SprintRoutingTable() string { } // prints the test packet as per the spec -func handleTestPackets(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.IPv4Header) error { +func HandleTestPackets(src *Interface, message []byte, hdr *ipv4header.IPv4Header) error { fmt.Printf("Received test packet: Src: %s, Dst: %s, TTL: %d, Data: %s\n", hdr.Src.String(), hdr.Dst.String(), hdr.TTL, string(message)) return nil @@ -551,7 +551,7 @@ func RecvIP(iface *Interface, isOpen *bool) error { if hdr.Protocol != RIP_PROTOCOL { // fmt.Println("this test packet is exactly for me") } - err := handler(myIface, nil, message, hdr) + err := handler(myIface, message, hdr) if err != nil { fmt.Println(err) } @@ -564,7 +564,7 @@ func RecvIP(iface *Interface, isOpen *bool) error { // if it's a local hop, send to that iface // if it's a RIP hop, send to the neighbor with that VIP // fmt.Println("checking routing table") - hop, err := LongestPrefix(hdr.Dst) + hop, err := Route(hdr.Dst) if err == nil { // on no err, found a match // fmt.Println("found route", hop.VIP) if hop.Type == "S" { @@ -607,7 +607,7 @@ func RecvIP(iface *Interface, isOpen *bool) error { // ************************************** RIP Routines ******************************************************* // creates a byte array that represents a RIP message -func makeRipMessage(command uint16, entries []RIPEntry) []byte { +func MakeRipMessage(command uint16, entries []RIPEntry) []byte { if command == 1 { // request message buf := make([]byte, SIZE_OF_RIP_HEADER) binary.BigEndian.PutUint16(buf[0:2], command) @@ -642,8 +642,8 @@ func makeRipMessage(command uint16, entries []RIPEntry) []byte { return buf } -// sends updates to neighbors every 5 seconds -func periodicUpdateRoutine() { +// PeriodicUpdateRoutine sends updates to neighbors every 5 seconds +func PeriodicUpdateRoutine() { for { // for each periodic update, we want to send our nodes in the table for _, iface := range myInterfaces { @@ -674,7 +674,7 @@ func periodicUpdateRoutine() { } // make the message and send it - message := makeRipMessage(2, entries) + message := MakeRipMessage(2, entries) addr := iface.IpPrefix.Addr() _, err := SendIP(&addr, n, RIP_PROTOCOL, message, n.VipAddr.String(), nil) if err != nil { @@ -689,8 +689,8 @@ func periodicUpdateRoutine() { } } -// when triggered, sends updates to neighbors -func sendTriggeredUpdates(newEntries []RIPEntry) { +// SendTriggeredUpdates sends updates to ALL neighbors +func SendTriggeredUpdates(newEntries []RIPEntry) { for _, iface := range myInterfaces { for _, n := range myNeighbors[iface.Name] { // only send to RIP neighbors, else skip @@ -700,7 +700,7 @@ func sendTriggeredUpdates(newEntries []RIPEntry) { } // send the made entries to the neighbor - message := makeRipMessage(2, newEntries) + message := MakeRipMessage(2, newEntries) addr := iface.IpPrefix.Addr() _, err := SendIP(&addr, n, RIP_PROTOCOL, message, n.VipAddr.String(), nil) if err != nil { @@ -712,7 +712,7 @@ func sendTriggeredUpdates(newEntries []RIPEntry) { } // manages the timeout table go routine -func manageTimeoutsRoutine() { +func ManageTimeoutsRoutine() { for { time.Sleep(time.Second) @@ -730,7 +730,7 @@ func manageTimeoutsRoutine() { // send triggered update on timeout if len(newEntries) > 0 { - sendTriggeredUpdates(newEntries) + SendTriggeredUpdates(newEntries) } } } @@ -739,8 +739,8 @@ func manageTimeoutsRoutine() { } } -// go routine to send rip requests to neighbors -func startRipRoutines() { +// StartRipRoutines routine to send rip requests to neighbors +func StartRipRoutines() { // send a request to every neighbor go func() { for _, iface := range myInterfaces { @@ -751,7 +751,7 @@ func startRipRoutines() { continue } // send a request - message := makeRipMessage(1, nil) + message := MakeRipMessage(1, nil) addr := iface.IpPrefix.Addr() _, err := SendIP(&addr, neighbor, RIP_PROTOCOL, message, neighbor.VipAddr.String(), nil) if err != nil { @@ -762,10 +762,10 @@ func startRipRoutines() { }() // start a routine that sends updates every 5 seconds - go periodicUpdateRoutine() + go PeriodicUpdateRoutine() // make a "timeout" table, for each response we add to the table via rip - go manageTimeoutsRoutine() + go ManageTimeoutsRoutine() } // ************************************** Protocol Handlers ******************************************************* @@ -773,18 +773,18 @@ func startRipRoutines() { // registers a protocol handler func RegisterProtocolHandler(protocolNum int) bool { if protocolNum == RIP_PROTOCOL { - protocolHandlers[protocolNum] = handleRIP - go startRipRoutines() + protocolHandlers[protocolNum] = HandleRIP + go StartRipRoutines() return true } if protocolNum == TEST_PROTOCOL { - protocolHandlers[protocolNum] = handleTestPackets + protocolHandlers[protocolNum] = HandleTestPackets return true } return false } -func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.IPv4Header) error { +func HandleRIP(src *Interface, message []byte, hdr *ipv4header.IPv4Header) error { // parse the RIP message command := int(binary.BigEndian.Uint16(message[0:2])) switch command { @@ -815,7 +815,7 @@ func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.I }) } // send the entries - res := makeRipMessage(2, entries) + res := MakeRipMessage(2, entries) _, err := SendIP(&hdr.Dst, neighbor, RIP_PROTOCOL, res, hdr.Src.String(), nil) if err != nil { return err @@ -925,7 +925,7 @@ func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.I // send out triggered updates if len(triggeredEntries) > 0 { - sendTriggeredUpdates(triggeredEntries) + SendTriggeredUpdates(triggeredEntries) } } @@ -949,8 +949,8 @@ func ValidateChecksum(b []byte, fromHeader uint16) uint16 { // *********************************************** HELPERS ********************************************************** -// returns the longest prefix match for a given ip -func LongestPrefix(src netip.Addr) (Hop, error) { +// Route returns the next HOP, based on longest prefix match for a given ip +func Route(src netip.Addr) (Hop, error) { possibleBits := [2]int{32, 24} for _, bits := range possibleBits { cmpPrefix := netip.PrefixFrom(src, bits) @@ -960,5 +960,5 @@ func LongestPrefix(src netip.Addr) (Hop, error) { } } } - return Hop{}, errors.Errorf("No route to ip %s on table.", src) + return Hop{}, errors.Errorf("error ROUTE: destination %s does not exist on routing table.", src) } |