aboutsummaryrefslogtreecommitdiff
path: root/pkg/routingtable/routingtable.go
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-10-09 06:00:08 +0000
committersotech117 <michael_foiani@brown.edu>2023-10-09 06:00:08 +0000
commit077d576becae10b35b84782d4070fbf2f5c0b7c8 (patch)
tree26a7f4c4f0219cf87ea70a004550bf2736a29321 /pkg/routingtable/routingtable.go
parent4966c054b3f462f6eaf24591c4ab1a945e72bb6f (diff)
good progress, fixing almost all of the red. in the process of ensuring the data structures are initialized correctly.
Diffstat (limited to 'pkg/routingtable/routingtable.go')
-rw-r--r--pkg/routingtable/routingtable.go51
1 files changed, 30 insertions, 21 deletions
diff --git a/pkg/routingtable/routingtable.go b/pkg/routingtable/routingtable.go
index 7f7e2b2..90b64ae 100644
--- a/pkg/routingtable/routingtable.go
+++ b/pkg/routingtable/routingtable.go
@@ -7,17 +7,26 @@ import (
)
type Address struct {
- addr netip.Addr
- prefix netip.Prefix
+ Addr netip.Addr
+ Prefix netip.Prefix
}
-type Route struct {
- dest Address
- cost uint32
- mask netip.Prefix
+type Hop struct {
+ Cost uint32
+ VipAsStr string
}
-var table map[Address]Route
+type RoutingTable map[Address]Hop
+
+const (
+ STATIC_COST uint32 = 4294967295 // 2^32 - 1
+)
+
+// TODO: consider making this take in arguments, such as a config file
+func New() *RoutingTable {
+ var table = make(RoutingTable)
+ return &table
+}
//func Initialize(config lnxconfig.IPConfig) error {
// if len(os.Args) != 2 {
@@ -42,39 +51,39 @@ var table map[Address]Route
//
//}
-func AddRoute(dest Address, cost uint32, mask netip.Prefix) error {
- if _, ok := table[dest]; ok {
+func AddRoute(srcAddr Address, cost uint32, addrAsStr string, tableReference *RoutingTable) error {
+ if _, ok := (*tableReference)[srcAddr]; ok {
return errors.New("Route already exists")
}
- table[dest] = Route{dest, cost, mask}
+ (*tableReference)[srcAddr] = Hop{cost, addrAsStr}
return nil
}
-func RemoveRoute(dest Address) error {
- if _, ok := table[dest]; !ok {
+func RemoveRoute(dest Address, table *RoutingTable) error {
+ if _, ok := (*table)[dest]; !ok {
return errors.New("Route doesn't exist")
}
- delete(table, dest)
+ delete(*table, dest)
return nil
}
// TODO: implement this with most specific prefix matching
-func GetRoute(dest Address) (Route, error) {
+func Route(dest Address, table *RoutingTable) (Hop, error) {
// get the most specific route
- for key, value := range table {
- if key.prefix.Contains(dest.addr) {
- return value, nil
+ for address, route := range *table {
+ if address.Prefix.Contains(dest.Addr) {
+ return route, nil
}
}
- return Route{}, errors.New("Route doesn't exist")
+ return Hop{}, errors.New("Route doesn't exist")
}
-func SprintRoutingTable() string {
+func SprintRoutingTable(table *RoutingTable) string {
message := ""
- for address, route := range table {
- message += fmt.Sprintf("%s/%d\t%d\n", address.addr, address.prefix, route.cost)
+ for address, route := range *table {
+ message += fmt.Sprintf("%s/%d\t%d\n", address.Addr, address.Prefix, route.Cost)
}
return message