diff options
author | sotech117 <michael_foiani@brown.edu> | 2023-10-08 23:05:53 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2023-10-08 23:05:53 -0400 |
commit | 4966c054b3f462f6eaf24591c4ab1a945e72bb6f (patch) | |
tree | b33ce376ceeadaecb926f0daa841f6d11f56442f | |
parent | 5a5dec572c01a7d306c3bdd3fd0ad8cec63c6049 (diff) |
fix heiarchy. rewrite routing table code
-rw-r--r-- | .idea/.gitignore | 8 | ||||
-rw-r--r-- | .idea/iptcp-jailpt2.iml | 9 | ||||
-rw-r--r-- | .idea/modules.xml | 8 | ||||
-rw-r--r-- | .idea/vcs.xml | 6 | ||||
-rw-r--r-- | cmd/example/main.go | 28 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | pkg/ipstack/ipstack.go (renamed from pkg/protocol.go) | 17 | ||||
-rw-r--r-- | pkg/lnxconfig/lnxconfig.go (renamed from pkg/lnxconfig.go) | 3 | ||||
-rw-r--r-- | pkg/routingTable.go | 71 | ||||
-rw-r--r-- | pkg/routingtable/routingtable.go | 81 |
10 files changed, 151 insertions, 82 deletions
diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/iptcp-jailpt2.iml b/.idea/iptcp-jailpt2.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/iptcp-jailpt2.iml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="WEB_MODULE" version="4"> + <component name="Go" enabled="true" /> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$" /> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module>
\ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6ae4065 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/iptcp-jailpt2.iml" filepath="$PROJECT_DIR$/.idea/iptcp-jailpt2.iml" /> + </modules> + </component> +</project>
\ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project>
\ No newline at end of file diff --git a/cmd/example/main.go b/cmd/example/main.go new file mode 100644 index 0000000..5b6ae61 --- /dev/null +++ b/cmd/example/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "iptcp-jailpt2/pkg/lnxconfig" + "net/netip" + "os" +) + +func main() { + if len(os.Args) != 2 { + fmt.Printf("Usage: %s <configFile>\n", os.Args[0]) + os.Exit(1) + } + fileName := os.Args[1] + + // Parse the file + lnxConfig, err := lnxconfig.ParseConfig(fileName) + if err != nil { + panic(err) + } + + // Demo: print out the IP for each interface in this config + for _, iface := range lnxConfig.Interfaces { + prefixForm := netip.PrefixFrom(iface.AssignedIP, iface.AssignedPrefix.Bits()) + fmt.Printf("%s has IP %s\n", iface.Name, prefixForm.String()) + } +} @@ -1,4 +1,4 @@ -module golang-sockets +module iptcp go 1.20 diff --git a/pkg/protocol.go b/pkg/ipstack/ipstack.go index 9358099..770fd17 100644 --- a/pkg/protocol.go +++ b/pkg/ipstack/ipstack.go @@ -1,16 +1,15 @@ -package protocol +package ipstack import ( - "net" - "net/netip" "fmt" - "os" - "bufio" - "time" - "github.com/pkg/errors" ipv4header "github.com/brown-csci1680/iptcp-headers" "github.com/google/netstack/tcpip/header" - "github.com/brown-csci1680/ipstack-utils" + "github.com/pkg/errors" + "../../pkg/lnxconfig" + "log" + "net" + "net/netip" + "os" ) const ( @@ -23,7 +22,7 @@ type Interface struct { AssignedPrefix netip.Prefix UDPAddr netip.AddrPort - State uint8_t + State bool neighbors map[netip.AddrPort]netip.AddrPort } diff --git a/pkg/lnxconfig.go b/pkg/lnxconfig/lnxconfig.go index 36b1b56..d0699f9 100644 --- a/pkg/lnxconfig.go +++ b/pkg/lnxconfig/lnxconfig.go @@ -3,6 +3,7 @@ package lnxconfig import ( "bufio" "fmt" + "github.com/pkg/errors" "net/netip" "os" "strings" @@ -362,4 +363,4 @@ func ParseConfig(configFile string) (*IPConfig, error) { } return config, nil -}
\ No newline at end of file +} diff --git a/pkg/routingTable.go b/pkg/routingTable.go deleted file mode 100644 index bda4524..0000000 --- a/pkg/routingTable.go +++ /dev/null @@ -1,71 +0,0 @@ -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 diff --git a/pkg/routingtable/routingtable.go b/pkg/routingtable/routingtable.go new file mode 100644 index 0000000..7f7e2b2 --- /dev/null +++ b/pkg/routingtable/routingtable.go @@ -0,0 +1,81 @@ +package routingtable + +import ( + "fmt" + "github.com/pkg/errors" + "net/netip" +) + +type Address struct { + addr netip.Addr + prefix netip.Prefix +} + +type Route struct { + dest Address + cost uint32 + mask netip.Prefix +} + +var table map[Address]Route + +//func Initialize(config lnxconfig.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) +// } +// +// // make and populate routing table +// table = make(map[Address]Route) +// for _, iface := range lnxConfig.Interfaces { +// var address = Address{iface.AssignedIP, iface.AssignedPrefix} +// var route = Route{Address{iface.AssignedIP, iface.AssignedPrefix}, 0, iface.AssignedPrefix} +// table[address] = route +// } +// +// +//} + +func AddRoute(dest Address, cost uint32, mask netip.Prefix) error { + if _, ok := table[dest]; ok { + return errors.New("Route already exists") + } + + table[dest] = Route{dest, cost, mask} + return nil +} + +func RemoveRoute(dest Address) error { + if _, ok := table[dest]; !ok { + return errors.New("Route doesn't exist") + } + + delete(table, dest) + return nil +} + +// TODO: implement this with most specific prefix matching +func GetRoute(dest Address) (Route, error) { + // get the most specific route + for key, value := range table { + if key.prefix.Contains(dest.addr) { + return value, nil + } + } + return Route{}, errors.New("Route doesn't exist") +} + +func SprintRoutingTable() string { + message := "" + for address, route := range table { + message += fmt.Sprintf("%s/%d\t%d\n", address.addr, address.prefix, route.cost) + } + + return message +} |