crip/main.go
2024-04-10 13:59:01 +01:00

50 lines
905 B
Go

package main
import (
"fmt"
"log"
"net"
"github.com/crip/ripv2"
)
func main() {
rip := ripv2.New()
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("waiting")
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)
}
/* Just testing the parsing output */
for i := 0; i < 24; i++ {
if rip.RIP[i].Metric == 0 {
break
}
fmt.Printf("%d\t%s\t%s\t%s\t%d\n", rip.RIP[i].AFI, rip.RIP[i].Addr.String(), rip.RIP[i].Subnet.String(), rip.RIP[i].Nexthop.String(), rip.RIP[i].Metric)
}
fmt.Println("")
}
}