aboutsummaryrefslogtreecommitdiff
path: root/pkg/ipstack
diff options
context:
space:
mode:
authorDavid Doan <daviddoan@Davids-MacBook-Pro-70.local>2023-10-23 14:53:30 -0400
committerDavid Doan <daviddoan@Davids-MacBook-Pro-70.local>2023-10-23 14:53:30 -0400
commit972cc0287e2e1231b7b67177e5bc89af5dead23c (patch)
treefe1012417c06775c98e81c4d88372c691f0ba1bd /pkg/ipstack
parent0d68d5aa82a980b10b8afd00e054f6c126e50d93 (diff)
comments and refactoring
Diffstat (limited to 'pkg/ipstack')
-rw-r--r--pkg/ipstack/ipstack.go67
1 files changed, 30 insertions, 37 deletions
diff --git a/pkg/ipstack/ipstack.go b/pkg/ipstack/ipstack.go
index 959e5ff..c2d83ba 100644
--- a/pkg/ipstack/ipstack.go
+++ b/pkg/ipstack/ipstack.go
@@ -12,8 +12,6 @@ import (
"net/netip"
"sync"
"time"
- // "bytes"
- // "unicode"
)
const (
@@ -64,8 +62,8 @@ type Hop struct {
}
// GLOBAL VARIABLES (data structures) ------------------------------------------
-var myVIP Interface
var myInterfaces []*Interface
+
var myNeighbors = make(map[string][]*Neighbor)
var myRIPNeighbors = make(map[string]*Neighbor)
@@ -78,6 +76,8 @@ var routingTable = make(map[netip.Prefix]Hop)
// ************************************** INIT FUNCTIONS **********************************************************
// reference: https://github.com/brown-csci1680/lecture-examples/blob/main/ip-demo/cmd/udp-ip-recv/main.go
+
+// createUDPListener creates a UDP listener.
func createUDPListener(UdpAddr netip.AddrPort, conn *net.UDPConn) error {
listenString := UdpAddr.String()
listenAddr, err := net.ResolveUDPAddr("udp4", listenString)
@@ -93,6 +93,7 @@ func createUDPListener(UdpAddr netip.AddrPort, conn *net.UDPConn) error {
return nil
}
+// initialize parse the lnxfile and initializes the data structures and listener routines
func Initialize(lnxFilePath string) error {
// Parse the file
lnxConfig, err := lnxconfig.ParseConfig(lnxFilePath)
@@ -100,7 +101,7 @@ func Initialize(lnxFilePath string) error {
return errors.WithMessage(err, "Error parsing config file->\t"+lnxFilePath)
}
- // 1) add each local if to the routing table, as dictated by its subnet
+ // 1) add each local "if" to the routing table, as dictated by its subnet
for _, iface := range lnxConfig.Interfaces {
prefix := netip.PrefixFrom(iface.AssignedIP, iface.AssignedPrefix.Bits())
i := &Interface{
@@ -111,16 +112,20 @@ func Initialize(lnxFilePath string) error {
SocketChannel: make(chan bool),
State: true,
}
- // Added this for printing purposes for REPL FYI, if you have a better way lmk
+ // create the UDP listener
err := createUDPListener(iface.UDPAddr, &i.RecvSocket)
if err != nil {
return errors.WithMessage(err, "Error creating UDP socket for interface->\t"+iface.Name)
}
+ // start the listener routine
go InterfaceListenerRoutine(i)
+
+ // add to the list of interfaces
myInterfaces = append(myInterfaces, i)
+ // add to the routing table
routingTable[prefix.Masked()] = Hop{LOCAL_COST, "L", i, prefix.Addr()}
}
@@ -162,10 +167,10 @@ func Initialize(lnxFilePath string) error {
}
}
- // add protocol handlers
return nil
}
+// defines the go routine that listens on the UDP socket
func InterfaceListenerRoutine(i *Interface) {
// decompose the interface
socket := i.RecvSocket
@@ -175,8 +180,9 @@ func InterfaceListenerRoutine(i *Interface) {
isUp := true
closed := false
- // go routine that hangs on the recv
// fmt.Println("MAKING GO ROUTINE TO LISTEN:\t", socket.LocalAddr().String())
+
+ // go routine that hangs on the recv
go func() {
defer func() {
fmt.Println("exiting go routine that listens on ", socket.LocalAddr().String())
@@ -186,14 +192,15 @@ func InterfaceListenerRoutine(i *Interface) {
if closed { // stop this go routine if channel is closed
return
}
+
//if !isUp { // don't call the listeners if interface is down
// continue
//}
+
// TODO: remove these "training wheels"
time.Sleep(1 * time.Millisecond)
err := RecvIP(i, &isUp)
if err != nil {
- // fmt.Println("Error receiving IP packet", err)
continue
}
}
@@ -201,6 +208,7 @@ func InterfaceListenerRoutine(i *Interface) {
for {
select {
+ // if the channel is closed, exit
case sig, ok := <-signal:
if !ok {
fmt.Println("channel closed, exiting")
@@ -209,6 +217,7 @@ func InterfaceListenerRoutine(i *Interface) {
}
// fmt.Println("received isUP SIGNAL with value", sig)
isUp = sig
+ // if the channel is not closed, continue
default:
continue
}
@@ -217,7 +226,9 @@ func InterfaceListenerRoutine(i *Interface) {
// ************************************** DOWN/UP FUNCTIONS ******************************************************
+// sets the interface to be up and sends a triggered update
func InterfaceUp(iface *Interface) {
+ // set the state to up and send the signal
iface.State = true
iface.SocketChannel <- true
@@ -235,12 +246,13 @@ func InterfaceUpREPL(ifaceName string) {
fmt.Println("Error getting interface by name", err)
return
}
+ // set the state to up and send the signal
InterfaceUp(iface)
}
-// we could do either of these but the REPL results in less work done in router and host
-
+// sets the interface to be down and sends a triggered update
func InterfaceDown(iface *Interface) {
+ // set the state to down and send the signal
iface.SocketChannel <- false
iface.State = false
@@ -258,6 +270,7 @@ func InterfaceDownREPL(ifaceName string) {
fmt.Println("Error getting interface by name", err)
return
}
+ // set the state to down and send the signal
InterfaceDown(iface)
}
@@ -272,31 +285,6 @@ func GetInterfaceByName(ifaceName string) (*Interface, error) {
return nil, errors.Errorf("No interface with name %s", ifaceName)
}
-func GetNeighborByIP(ipAddr string) (*Neighbor, error) {
- // iterate through the neighbors and return the one with the same ipAddr
- for _, neighbors := range myNeighbors {
- for _, neighbor := range neighbors {
- if neighbor.VipAddr.String() == ipAddr {
- return neighbor, nil
- }
- }
- }
-
- return nil, errors.Errorf("No interface with ip %s", ipAddr)
-}
-
-func GetNeighborsToInterface(ifaceName string) ([]*Neighbor, error) {
- if neighbors, ok := myNeighbors[ifaceName]; ok {
- return neighbors, nil
- }
-
- return nil, errors.Errorf("No interface with name %s", ifaceName)
-}
-
-func GetMyVIP() Interface {
- return myVIP
-}
-
func GetInterfaces() []*Interface {
return myInterfaces
}
@@ -311,22 +299,27 @@ func GetRoutes() map[netip.Prefix]Hop {
// ************************************** PRINT FUNCTIONS **********************************************************
+// Sprint functions return a string representation of the myInterfaces data structure
func SprintInterfaces() string {
tmp := ""
for _, iface := range myInterfaces {
if iface.State {
+ // if the state is up, print UP
tmp += fmt.Sprintf("%s\t%s\t%s\n", iface.Name, iface.IpPrefix.String(), "UP")
} else {
+ // if the state is down, print DOWN
tmp += fmt.Sprintf("%s\t%s\t%s\n", iface.Name, iface.IpPrefix.String(), "DOWN")
}
}
return tmp
}
+// Sprint functions return a string representation of the myNeighbors data structure
func SprintNeighbors() string {
tmp := ""
for _, iface := range myInterfaces {
if !iface.State {
+ // if the interface is down, skip it
continue
}
for _, n := range myNeighbors[iface.Name] {
@@ -1017,7 +1010,7 @@ func LongestPrefix(src netip.Addr) (Hop, error) {
// for _, ripNeighbor := range myRIPNeighbors {
// if neighbor.VipAddr.String() == ripNeighbor.String() {
// // send RIP message
-// err := SendRIPMessage(myVIP, neighbor, message)
+ // err := SendRIPMessage(myVIP, neighbor, message)
// if err != nil {
// continue
// }
@@ -1107,7 +1100,7 @@ func LongestPrefix(src netip.Addr) (Hop, error) {
// // send RIP message
// for _, Interfaces := range myInterfaces {
// if Interfaces.Name == neighbor.Name {
-// err := SendRIPMessage(myVIP, neighbor, message)
+ // err := SendRIPMessage(myVIP, neighbor, message)
// if err != nil {
// continue
// }