diff options
author | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-10-20 12:24:59 -0400 |
---|---|---|
committer | David Doan <daviddoan@Davids-MacBook-Pro-70.local> | 2023-10-20 12:24:59 -0400 |
commit | 6618f59d55ae5bbf99426b06e3cdd891f9a0f0c4 (patch) | |
tree | 413ebd5ea736631ab0abc9c78fe9004de5348154 /pkg/ipstack | |
parent | a7f9da2bb4ce6649ca38ab808a7b263aff9afbd7 (diff) |
bug fixing
Diffstat (limited to 'pkg/ipstack')
-rw-r--r-- | pkg/ipstack/ipstack.go | 143 |
1 files changed, 82 insertions, 61 deletions
diff --git a/pkg/ipstack/ipstack.go b/pkg/ipstack/ipstack.go index 51ea07c..0cc1fff 100644 --- a/pkg/ipstack/ipstack.go +++ b/pkg/ipstack/ipstack.go @@ -11,8 +11,8 @@ import ( "net/netip" "time" "encoding/binary" - "bytes" - "unicode" + // "bytes" + // "unicode" ) const ( @@ -111,7 +111,7 @@ func Initialize(lnxFilePath string) error { } // 1) initialize the interfaces on this node here and into the routing table - // static := false + static := false interfaceToReturn := Interface{} for _, iface := range lnxConfig.Interfaces { prefix := netip.PrefixFrom(iface.AssignedIP, iface.AssignedPrefix.Bits()) @@ -135,6 +135,13 @@ func Initialize(lnxFilePath string) error { } go InterfaceListenerRoutine(i.RecvSocket, i.SocketChannel) myInterfaces = append(myInterfaces, i) + + // TODO @ MICHAEL: The hop shouldn't be the interface name + if !static { + ifacePrefix := netip.MustParsePrefix("0.0.0.0/0") + routingTable[ifacePrefix] = Hop{STATIC_COST, iface.Name, "S"} + static = true + } } // 2) initialize the neighbors connected to the node and into the routing table @@ -171,7 +178,7 @@ func Initialize(lnxFilePath string) error { routingTable[prefix] = Hop{STATIC_COST, addr.String(), "S"} } - SendUpdates() + go SendUpdates() // add protocol handlers protocolHandlers[RIP_PROTOCOL] = handleRIP protocolHandlers[TEST_PROTOCOL] = handleTestPackets @@ -296,12 +303,20 @@ func GetRouteByIP(ipAddr string) (*Neighbor, error) { neighbor, err := GetNeighborByIP(hop.VipAsStr) if err != nil { fmt.Println("Error getting neighbors to interface", err) - continue // fix with longest prefix matching? + neighbor, err = GetRouteByIP(hop.VipAsStr) + if err != nil { + fmt.Println("Error getting route by IP", err) + return nil, err + } + // continue // fix with longest prefix matching? } return neighbor, nil } } + if defaultRoute == nil { + return nil, errors.Errorf("No route to ip %s", ipAddr) + } fmt.Println("returning default route", defaultRoute.VipAddr.String()) return defaultRoute, nil } @@ -431,23 +446,41 @@ func CleanUp() { } // TODO: have it take TTL so we can decrement it when forwarding -func SendIP(src *Interface, dest *Neighbor, protocolNum int, message []byte, destIP string) error { - hdr := ipv4header.IPv4Header{ - Version: 4, - Len: 20, // Header length is always 20 when no IP options - TOS: 0, - TotalLen: ipv4header.HeaderLen + len(message), - ID: 0, - Flags: 0, - FragOff: 0, - TTL: 32, - Protocol: protocolNum, - Checksum: 0, // Should be 0 until checksum is computed - Src: src.IpPrefix.Addr(), - Dst: netip.MustParseAddr(destIP), - Options: []byte{}, +func SendIP(src *Interface, dest *Neighbor, protocolNum int, message []byte, destIP string, hdr *ipv4header.IPv4Header) error { + if hdr == nil { + hdr = &ipv4header.IPv4Header{ + Version: 4, + Len: 20, // Header length is always 20 when no IP options + TOS: 0, + TotalLen: ipv4header.HeaderLen + len(message), + ID: 0, + Flags: 0, + FragOff: 0, + TTL: 32, + Protocol: protocolNum, + Checksum: 0, // Should be 0 until checksum is computed + Src: src.IpPrefix.Addr(), + Dst: netip.MustParseAddr(destIP), + Options: []byte{}, + } + } else { + hdr = &ipv4header.IPv4Header{ + Version: 4, + Len: 20, // Header length is always 20 when no IP options + TOS: 0, + TotalLen: ipv4header.HeaderLen + len(message), + ID: 0, + Flags: 0, + FragOff: 0, + TTL: hdr.TTL - 1, + Protocol: protocolNum, + Checksum: 0, // Should be 0 until checksum is computed + Src: src.IpPrefix.Addr(), + Dst: netip.MustParseAddr(destIP), + Options: []byte{}, + } } - + // Assemble the header into a byte array headerBytes, err := hdr.Marshal() if err != nil { @@ -473,8 +506,7 @@ func SendIP(src *Interface, dest *Neighbor, protocolNum int, message []byte, des return err } - // TODO @ MICHAEL UPDATE with appropriate src - fmt.Printf("Sent %d bytes to %s\n", bytesWritten, dest.UdpAddr.String()) + fmt.Printf("Sent %d bytes to %s\n", bytesWritten, dest.VipAddr.String()) return nil } @@ -521,7 +553,6 @@ func RecvIP(conn net.UDPConn, isOpen *bool) error { // Next, get the message, which starts after the header message := buffer[headerSize:] - // TODO: handle the message // 1) check if the TTL & checksum is valid TTL := hdr.TTL if TTL == 0 { @@ -565,6 +596,7 @@ func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.I case 1: // request SendUpdates() + break case 2: numEntries := message[1] @@ -595,8 +627,11 @@ func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.I if _, ok := routingTable[netip.MustParsePrefix(address+"/24")]; ok { continue } - - routingTable[netip.MustParsePrefix(address+"/24")] = Hop{entry.cost + 1, mask, "R"} + if entry.cost == 17 { + routingTable[netip.MustParsePrefix(address+"/24")] = Hop{0, mask, "R"} + } else { + routingTable[netip.MustParsePrefix(address+"/24")] = Hop{entry.cost + 1, mask, "R"} + } } } @@ -605,15 +640,16 @@ func handleRIP(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.I func handleTestPackets(src *Interface, dest *Neighbor, message []byte, hdr *ipv4header.IPv4Header) error { // 2) check if the message is for me, if so, sendUP (aka call the correct handler) - // filter the message, still not the same - // TODO @ MICHAEL - message = bytes.Map(func(r rune) rune { - if unicode.IsPrint(r) { - return r - } - return -1 - }, message) - + + // potentially unneeded + // message = bytes.Map(func(r rune) rune { + // if unicode.IsPrint(r) { + // return r + // } + // return -1 + // }, message) + + fmt.Println("checking my interfaces") for _, interfaces := range myInterfaces { if hdr.Dst.String() == interfaces.IpPrefix.Addr().String() { fmt.Printf("Received test packet: Src: %s, Dst: %s, TTL: %d, Data: %s\n", @@ -622,28 +658,12 @@ func handleTestPackets(src *Interface, dest *Neighbor, message []byte, hdr *ipv4 } } - // TODO @ MICHAEL: check if this is correct... // if not, need to forward the packer to a neighbor or check the table // after decrementing TTL and updating checksum - hdr.TTL-- - // update checksum - headerBytes, err := hdr.Marshal() - if err != nil { - log.Fatalln("Error marshalling header: ", err) - } - - hdr.Checksum = int(ComputeChecksum(headerBytes)) - headerBytes, err = hdr.Marshal() - if err != nil { - log.Fatalln("Error marshalling header: ", err) - } - bytesToSend := make([]byte, 0, len(headerBytes)+len(message)) - bytesToSend = append(bytesToSend, headerBytes...) - bytesToSend = append(bytesToSend, message...) - + // DONE IN SENDIP FUNCTION // 3) check if message is for a neighbor, if so, sendIP there - // fmt.Println("checking neighbors") + fmt.Println("checking neighbors") for _, neighbors := range myNeighbors { for _, neighbor := range neighbors { // check for matching neighbor @@ -651,7 +671,7 @@ func handleTestPackets(src *Interface, dest *Neighbor, message []byte, hdr *ipv4 // get the source interface and set dst to it for _, interfaces := range myInterfaces { if interfaces.Name == neighbor.Name { - err = SendIP(interfaces, neighbor, hdr.Protocol, bytesToSend, hdr.Dst.String()) + err := SendIP(interfaces, neighbor, hdr.Protocol, message, hdr.Dst.String(), hdr) if err != nil { fmt.Println(err) } @@ -663,7 +683,7 @@ func handleTestPackets(src *Interface, dest *Neighbor, message []byte, hdr *ipv4 } } // 4) check forwarding table. if so, forward to the neighbor with that VIP - // fmt.Println("checking routing table") + fmt.Println("checking routing table") for prefix, hop := range routingTable { netIP := net.ParseIP(prefix.Addr().String()) // check if the destination is in the routing table @@ -682,7 +702,7 @@ func handleTestPackets(src *Interface, dest *Neighbor, message []byte, hdr *ipv4 } for _, interfaces := range myInterfaces { if interfaces.Name == neighbor.Name { - err = SendIP(interfaces, neighbor, hdr.Protocol, bytesToSend, hdr.Dst.String()) + err = SendIP(interfaces, neighbor, hdr.Protocol, message, hdr.Dst.String(), hdr) if err != nil { fmt.Println(err) } @@ -694,7 +714,7 @@ func handleTestPackets(src *Interface, dest *Neighbor, message []byte, hdr *ipv4 // get the source interface and set dst to it for _, interfaces := range myInterfaces { if interfaces.Name == neighbor.Name { - err = SendIP(interfaces, neighbor, hdr.Protocol, bytesToSend, hdr.Dst.String()) + err = SendIP(interfaces, neighbor, hdr.Protocol, message, hdr.Dst.String(), hdr) if err != nil { fmt.Println(err) } @@ -895,17 +915,18 @@ func SendUpdates() { if iface.Name == iface2.Name { continue } - // cost should be 0 but it is one + // TODO @ MICHAEL: fix this + // hardcoded way to get cost to 0, fix if you want a better way entry := &RIPEntry{ address: ConvertIPToUint32(iface2.IpPrefix.Addr().String()), - cost: LOCAL_COST, + cost: 17, mask: ConvertIPToUint32(iface.IpPrefix.Addr().String()), } entries = append(entries, *entry) entry = &RIPEntry{ address: ConvertIPToUint32(iface.IpPrefix.Addr().String()), - cost: LOCAL_COST, + cost: 17, mask: ConvertIPToUint32(iface2.IpPrefix.Addr().String()), } entries = append(entries, *entry) @@ -979,4 +1000,4 @@ func ConvertIPToUint32(ip string) uint32 { // } // } -// TODO @ MICHAEL: Triggereed Updates
\ No newline at end of file +// TODO @ MICHAEL: Triggered Updates and Split Horizon with Poisoned Reverse
\ No newline at end of file |