play around

This commit is contained in:
Jim Colderwood 2024-04-09 22:20:07 +01:00
commit 1a9f5fc4da
4 changed files with 119 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/crip
go 1.22.2

54
main.go Normal file
View File

@ -0,0 +1,54 @@
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"),
}
conn, err := net.ListenMulticastUDP("udp", nil, &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)
}
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)
*/
n, err := conn.Read(buff)
if err != nil {
log.Fatalln(err)
}
if n == 0 {
log.Fatalln("empty")
}
if err := rip.Parse(&buff); err != nil {
log.Fatalln(err)
}
fmt.Println(rip)
}

7
ripv2/constants.go Normal file
View File

@ -0,0 +1,7 @@
package ripv2
const (
ZERO = iota
REQUEST
RESPONSE
)

55
ripv2/ripv2.go Normal file
View File

@ -0,0 +1,55 @@
package ripv2
import (
"bytes"
"encoding/binary"
"fmt"
"net"
)
type Message struct {
Command uint8
Version uint8
Zero uint16
RIP [25]RIP
}
type RIP struct {
AddrF uint16
Zero uint16
Addr uint32
Packing uint64
Metric uint32
}
func New() Message {
return Message{}
}
func (m *Message) Parse(b *[]byte) error {
if len(*b) == 0 {
return fmt.Errorf("can't parse empty data")
}
bytes := bytes.NewBuffer(*b)
if err := binary.Read(bytes, binary.LittleEndian, &m); err != nil {
return err
}
return nil
}
func (m *Message) Request(conn *net.UDPConn) ([]byte, error) {
var buffer = make([]byte, 1024)
m.Command = REQUEST
m.Version = 2
buff := bytes.NewBuffer(buffer)
buff.Reset()
if err := binary.Write(buff, binary.LittleEndian, m); err != nil {
return []byte{}, err
}
fmt.Println(buff)
return buffer, nil
}