config parsing
This commit is contained in:
parent
46f0c8aedf
commit
71c249746f
5
config.json
Normal file
5
config.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"multicastAddr": "224.0.0.9",
|
||||
"multicastInterface": "enp1s0f1",
|
||||
"MulticastPort": 520
|
||||
}
|
@ -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
|
||||
}
|
||||
|
40
main.go
40
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("")
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user