1
1
mirror of https://github.com/wader/fq.git synced 2025-01-07 06:36:26 +03:00
fq/format/inet/ether8023_frame.go
Mattias Wadman 8e0dde03d0 decode: Support multiple format args and some rename and refactor
This will allow passing both cli options and format options to sub decoder.
Ex: pass keylog option to a tls decoder when decoding a pcap.
Ex: pass decode options to a format inside a http body inside a pcap.

Add ArgAs method to lookup argument based on type. This also makes the format
decode function have same signature as sub decoders in the decode API.

This change decode.Format a bit:
DecodeFn is now just func(d *D) any
DecodeInArg renamed to DefaultInArg
2023-02-18 21:38:51 +01:00

58 lines
1.4 KiB
Go

package inet
// TODO: move to own package?
import (
"encoding/binary"
"fmt"
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
)
var ether8023FrameInetPacketGroup decode.Group
func init() {
interp.RegisterFormat(decode.Format{
Name: format.ETHER8023_FRAME,
Description: "Ethernet 802.3 frame",
Groups: []string{format.LINK_FRAME},
Dependencies: []decode.Dependency{
{Names: []string{format.INET_PACKET}, Group: &ether8023FrameInetPacketGroup},
},
DecodeFn: decodeEthernetFrame,
})
}
// TODO: move to shared?
var mapUToEtherSym = scalar.UintFn(func(s scalar.Uint) (scalar.Uint, error) {
var b [8]byte
binary.BigEndian.PutUint64(b[:], s.Actual)
s.Sym = fmt.Sprintf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", b[2], b[3], b[4], b[5], b[6], b[7])
return s, nil
})
func decodeEthernetFrame(d *decode.D) any {
var lfi format.LinkFrameIn
if d.ArgAs(&lfi) {
if lfi.Type != format.LinkTypeETHERNET {
d.Fatalf("wrong link type %d", lfi.Type)
}
}
d.FieldU("destination", 48, mapUToEtherSym, scalar.UintHex)
d.FieldU("source", 48, mapUToEtherSym, scalar.UintHex)
etherType := d.FieldU16("ether_type", format.EtherTypeMap, scalar.UintHex)
d.FieldFormatOrRawLen(
"payload",
d.BitsLeft(),
ether8023FrameInetPacketGroup,
format.InetPacketIn{EtherType: int(etherType)},
)
return nil
}