From 71c249746fdb40623b30f5074c5d4cfcccdf0a3c Mon Sep 17 00:00:00 2001 From: Jim Colderwood Date: Tue, 16 Apr 2024 13:12:48 +0100 Subject: [PATCH] config parsing --- config.json | 5 +++++ config/config.go | 18 ++++++++++++++++++ main.go | 40 ++-------------------------------------- ripv2/ripv2.go | 2 +- 4 files changed, 26 insertions(+), 39 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..ca9ff69 --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "multicastAddr": "224.0.0.9", + "multicastInterface": "enp1s0f1", + "MulticastPort": 520 +} \ No newline at end of file diff --git a/config/config.go b/config/config.go index d912156..8481acf 100644 --- a/config/config.go +++ b/config/config.go @@ -1 +1,19 @@ package config + +import ( + "encoding/json" + "log" + "os" +) + +func Read(fname string) *Config { + var config Config + cb, err := os.ReadFile(fname) + if err != nil { + log.Fatal("config file:", err) + } + if err := json.Unmarshal(cb, &config); err != nil { + log.Fatal("config parse error:", err) + } + return &config +} diff --git a/main.go b/main.go index a2c7ef0..69cc035 100644 --- a/main.go +++ b/main.go @@ -1,55 +1,19 @@ package main import ( - "fmt" "log" - "net" "github.com/crip/config" "github.com/crip/ripv2" ) func main() { + config := config.Read("config.json") rip := ripv2.New() routes := ripv2.Init() - addr := net.UDPAddr{ - Port: 520, - IP: net.ParseIP("224.0.0.9"), - } - iface := net.Interface{ - Name: "enp1s0f1", - Flags: net.FlagMulticast, - } - conn, err := net.ListenMulticastUDP("udp4", &iface, &addr) - if err != nil { - log.Fatalln(err) - } - defer conn.Close() - buff := make([]byte, 1024) log.Println("Running") routes.Timer() - rip.Run(&config.Config{ - IP: string(net.ParseIP("224.0.0.9")), - MulticastInterface: "enp1s0f1", - Port: 520, - }) + rip.Run(config) /* NOT REACHED */ - for { - n, UDPAddr, err := conn.ReadFromUDP(buff) - if err != nil { - log.Fatalln(err) - } - _ = UDPAddr - - if err := rip.MParse(&buff, n); err != nil { - log.Fatalln(err) - } - routes.ParseRoutes(*UDPAddr, &rip.RIP) - for _, v := range routes.Routes { - fmt.Printf("Route:%s\tSubnet:%s\tNexthop:%s\tMetric:%d\tTime: %v\n", v.Addr.String(), v.Subnet.String(), v.Nexthop.String(), v.Metric, v.TTL.Unix()) - } - //rtable.Add(routes) - fmt.Println("") - } } diff --git a/ripv2/ripv2.go b/ripv2/ripv2.go index 42392da..53b8b6d 100644 --- a/ripv2/ripv2.go +++ b/ripv2/ripv2.go @@ -65,7 +65,7 @@ func (m *Message) Run(c *config.Config) { Flags: net.FlagMulticast, } addr := net.UDPAddr{ - IP: net.IP(c.IP), + IP: net.ParseIP(c.IP), Port: c.Port, } conn, err := net.ListenMulticastUDP("udp4", &iface, &addr)