rough Parsing

This commit is contained in:
Jim Colderwood 2024-04-10 09:50:56 +01:00
parent d14091306e
commit 269b3f2f3b
2 changed files with 38 additions and 24 deletions

50
main.go
View File

@ -11,40 +11,47 @@ import (
func main() {
rip := ripv2.New()
addr := net.UDPAddr{
Port: 5111,
IP: net.ParseIP("0.0.0.0"),
Port: 520,
IP: net.ParseIP("224.0.0.9"),
}
conn, err := net.ListenUDP("udp", &addr)
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)
bytes, err := rip.Request(conn)
if err != nil {
log.Fatalln(err)
}
/*
bytes, err := rip.Request(conn)
if err != nil {
log.Fatalln(err)
}
fmt.Println(bytes)
fmt.Println(bytes)
RipAddr := net.UDPAddr{
Port: 520,
IP: net.ParseIP("224.0.0.9"),
}
bw, err := conn.WriteToUDP(bytes, &RipAddr)
if err != nil {
log.Fatalln(err)
}
log.Printf("wrote %d bytes\n", bw)
RipAddr := net.UDPAddr{
Port: 520,
IP: net.ParseIP("224.0.0.9"),
}
bw, err := conn.WriteToUDP(bytes, &RipAddr)
if err != nil {
log.Fatalln(err)
}
log.Printf("wrote %d bytes\n", bw)
*/
log.Println("waiting")
n, err := conn.Read(buff)
n, UDPAddr, err := conn.ReadFromUDP(buff)
if err != nil {
log.Fatalln(err)
}
fmt.Println("here")
_ = UDPAddr
if n == 0 {
log.Fatalln("empty")
}
@ -52,5 +59,8 @@ func main() {
if err := rip.Parse(&buff); err != nil {
log.Fatalln(err)
}
fmt.Println(rip)
for i := 0; i < 24; i++ {
ipa := net.IPv4(byte(rip.RIP[i].Addr), byte(rip.RIP[i].Addr>>8), byte(rip.RIP[i].Addr>>16), byte(rip.RIP[i].Addr>>24))
fmt.Printf("Command %d\n%s\t%d\n", rip.Command, ipa.String(), rip.RIP[i].Metric)
}
}

View File

@ -11,11 +11,11 @@ type Message struct {
Command uint8
Version uint8
Zero uint16
RIP RIP
RIP [25]RIP
}
type RIP struct {
AddrF uint16
AFI uint16
Zero uint16
Addr uint32
Packing uint64
@ -33,10 +33,14 @@ func (m *Message) Parse(b *[]byte) error {
bytes := bytes.NewBuffer(*b)
if err := binary.Read(bytes, binary.BigEndian, &m); err != nil {
if err := binary.Read(bytes, binary.LittleEndian, m); err != nil {
return err
}
if m.Version != 2 {
return fmt.Errorf("version: %d", m.Version)
}
return nil
}
@ -44,7 +48,7 @@ func (m *Message) Request(conn *net.UDPConn) ([]byte, error) {
var buffer = make([]byte, 24)
m.Command = REQUEST
m.Version = 2
m.RIP.Metric = 16
m.RIP[0].Metric = 16
buff := bytes.NewBuffer(buffer)
buff.Reset()
if err := binary.Write(buff, binary.BigEndian, m); err != nil {