aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd/vhost/main.go4
-rw-r--r--cmd/vrouter/main.go8
-rw-r--r--pkg/ipstack/ipstack.go143
-rwxr-xr-xvhostbin3107581 -> 3093677 bytes
-rwxr-xr-xvrouterbin3112082 -> 3094162 bytes
5 files changed, 89 insertions, 66 deletions
diff --git a/cmd/vhost/main.go b/cmd/vhost/main.go
index f5016a1..02158c4 100644
--- a/cmd/vhost/main.go
+++ b/cmd/vhost/main.go
@@ -68,7 +68,7 @@ func main() {
for _, interfaces := range ipstack.GetInterfaces() {
if interfaces.Name == iface.Name {
- err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr)
+ err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr, nil)
if err != nil {
fmt.Println(err)
}
@@ -79,7 +79,7 @@ func main() {
}
for _, interfaces := range ipstack.GetInterfaces() {
if interfaces.Name == iface.Name {
- err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr)
+ err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr, nil)
if err != nil {
fmt.Println(err)
}
diff --git a/cmd/vrouter/main.go b/cmd/vrouter/main.go
index 1239ca1..eb7a1eb 100644
--- a/cmd/vrouter/main.go
+++ b/cmd/vrouter/main.go
@@ -24,7 +24,9 @@ func main() {
go func() {
for {
ipstack.RequestRip()
- time.Sleep(5 * time.Second)
+ // takes time to compute I think
+ // TODO @ MICHAEL
+ time.Sleep(2 * time.Second)
}
}()
@@ -80,7 +82,7 @@ func main() {
// get the interface to send from
for _, interfaces := range ipstack.GetInterfaces() {
if interfaces.Name == iface.Name {
- err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr)
+ err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr, nil)
if err != nil {
fmt.Println(err)
}
@@ -92,7 +94,7 @@ func main() {
// neighbor was found, send to neighbor
for _, interfaces := range ipstack.GetInterfaces() {
if interfaces.Name == iface.Name {
- err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr)
+ err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr, nil)
if err != nil {
fmt.Println(err)
}
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
diff --git a/vhost b/vhost
index 7524060..4664392 100755
--- a/vhost
+++ b/vhost
Binary files differ
diff --git a/vrouter b/vrouter
index 61ab7e6..d7d51b4 100755
--- a/vrouter
+++ b/vrouter
Binary files differ