log packet information

This commit is contained in:
Johannes Kimmel 2021-12-14 10:28:17 +01:00
parent ce4c21154b
commit 3983700788
2 changed files with 16 additions and 11 deletions

View File

@ -42,13 +42,17 @@ func run(opt options) error {
buf := [4096]byte{}
var s tlv.PacketDecoder
for {
b, src, ifindex, err := conn.ReadFrom(buf[:])
b, src, rcm, err := conn.ReadFrom(buf[:])
if err != nil {
fmt.Println("Skipping packet:", err)
continue
}
s.Reset(b, src, ifindex)
fmt.Print("\x1B[1m")
fmt.Printf("Packet size %4d %s", len(b), rcm)
fmt.Println("\x1B[0m")
s.Reset(b, src, rcm.IfIndex)
for s.Scan() {
switch t := s.TLV().(type) {
case tlv.NextHop:

View File

@ -48,15 +48,16 @@ func (c Conn) JoinGroup(ifname string, addr string) error {
return c.v6pc.JoinGroup(ifi, &net.UDPAddr{IP: ip.IPAddr().IP})
}
func (c Conn) ReadFrom(b []byte) (body []byte, src netaddr.IP, ifindex int, err error) {
n, rcm, _, err := c.v6pc.ReadFrom(b)
func (c Conn) ReadFrom(b []byte) (body []byte, src netaddr.IP, rcm *ipv6.ControlMessage, err error) {
var n int
n, rcm, _, err = c.v6pc.ReadFrom(b)
if err != nil {
return nil, netaddr.IP{}, 0, err
return nil, netaddr.IP{}, nil, err
}
b = b[:n]
if len(b) < BabelPacketHeaderSize {
return nil, netaddr.IP{}, 0, fmt.Errorf("Packet too short: %d", len(b))
return nil, netaddr.IP{}, nil, fmt.Errorf("Packet too short: %d", len(b))
}
magic := b[0]
@ -65,22 +66,22 @@ func (c Conn) ReadFrom(b []byte) (body []byte, src netaddr.IP, ifindex int, err
b = b[4:]
if magic != BabelMagic {
return nil, netaddr.IP{}, 0, fmt.Errorf("Invalid magic number %d", magic)
return nil, netaddr.IP{}, nil, fmt.Errorf("Invalid magic number %d", magic)
}
if version != BabelVersion {
return nil, netaddr.IP{}, 0, fmt.Errorf("Unsupported version number %d", version)
return nil, netaddr.IP{}, nil, fmt.Errorf("Unsupported version number %d", version)
}
if int(length) > len(b) {
return nil, netaddr.IP{}, 0, fmt.Errorf("Invalid length for packet of size %d: %d", n, length)
return nil, netaddr.IP{}, nil, fmt.Errorf("Invalid length for packet of size %d: %d", n, length)
}
var ok bool
src, ok = netaddr.FromStdIPRaw(rcm.Src)
if !ok {
return nil, netaddr.IP{}, 0, fmt.Errorf("Invalid src address %q", rcm.Src)
return nil, netaddr.IP{}, nil, fmt.Errorf("Invalid src address %q", rcm.Src)
}
return b, src, rcm.IfIndex, err
return b, src, rcm, err
}