config parsing

This commit is contained in:
Jim Colderwood 2024-04-16 13:12:48 +01:00
parent 46f0c8aedf
commit 71c249746f
4 changed files with 26 additions and 39 deletions

5
config.json Normal file
View File

@ -0,0 +1,5 @@
{
"multicastAddr": "224.0.0.9",
"multicastInterface": "enp1s0f1",
"MulticastPort": 520
}

View File

@ -1 +1,19 @@
package config 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
}

40
main.go
View File

@ -1,55 +1,19 @@
package main package main
import ( import (
"fmt"
"log" "log"
"net"
"github.com/crip/config" "github.com/crip/config"
"github.com/crip/ripv2" "github.com/crip/ripv2"
) )
func main() { func main() {
config := config.Read("config.json")
rip := ripv2.New() rip := ripv2.New()
routes := ripv2.Init() 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") log.Println("Running")
routes.Timer() routes.Timer()
rip.Run(&config.Config{ rip.Run(config)
IP: string(net.ParseIP("224.0.0.9")),
MulticastInterface: "enp1s0f1",
Port: 520,
})
/* NOT REACHED */ /* 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("")
}
} }

View File

@ -65,7 +65,7 @@ func (m *Message) Run(c *config.Config) {
Flags: net.FlagMulticast, Flags: net.FlagMulticast,
} }
addr := net.UDPAddr{ addr := net.UDPAddr{
IP: net.IP(c.IP), IP: net.ParseIP(c.IP),
Port: c.Port, Port: c.Port,
} }
conn, err := net.ListenMulticastUDP("udp4", &iface, &addr) conn, err := net.ListenMulticastUDP("udp4", &iface, &addr)