aboutsummaryrefslogtreecommitdiff
path: root/pkg/routingTable.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/routingTable.go')
-rw-r--r--pkg/routingTable.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/pkg/routingTable.go b/pkg/routingTable.go
new file mode 100644
index 0000000..bda4524
--- /dev/null
+++ b/pkg/routingTable.go
@@ -0,0 +1,71 @@
+package routingTable
+
+import (
+ "fmt"
+ "net"
+ "net/netip"
+ "os"
+ "bufio"
+)
+
+type Address struct {
+ addr netip.Addr;
+ prefix netip.Prefix;
+}
+
+type Routing struct {
+ dest Address;
+ cost uint16_t;
+ mask netip.Prefix;
+}
+
+routingTable := make(map[Address]Routing)
+
+func Initialize(config IpConfig) (error) {
+ if len(os.Args) != 2 {
+ fmt.Printf("Usage: %s <configFile>\n", os.Args[0])
+ os.Exit(1)
+ }
+ fileName := os.Args[1]
+
+ lnxConfig, err := lnxconfig.ParseConfig(fileName)
+ if err != nil {
+ panic(err)
+ }
+
+ // populate routing table
+ for _, iface := range lnxConfig.Interfaces {
+ routingTable[Address{iface.AssignedIP, iface.AssignedPrefix}] = Routing{Address{iface.AssignedIP, iface.AssignedPrefix}, 0, iface.AssignedPrefix}
+ }
+
+}
+
+func AddRoute(dest Address, cost uint16_t, mask netip.Prefix) (error) {
+ if _, ok := routingTable[dest]; ok {
+ return newErrString(ln, "Route already exists")
+ }
+ routingTable[dest] = Routing{dest, cost, mask}
+}
+
+func RemoveRoute(dest Address) (error) {
+ if _, ok := routingTable[dest]; !ok {
+ return newErrString(ln, "Route does not exist")
+ }
+ delete(routingTable, dest)
+}
+
+func GetRoute(dest Address) (Routing, error) {
+ // get the most specific route
+ for key, value := range routingTable {
+ if key.prefix.Contains(dest.addr) {
+ return value, nil
+ }
+ }
+ return nil, newErrString(ln, "Route does not exist")
+}
+
+func PrintRoutingTable() {
+ for key, value := range routingTable {
+ fmt.Printf("%s/%d\t%d\n", key.addr, key.prefix.Bits(), value.cost)
+ }
+} \ No newline at end of file