in pcap-cli/internal/transformer/json_translator.go [269:324]
func (t *JSONPcapTranslator) translateIPv4Layer(
ctx context.Context,
ip4 *layers.IPv4,
) fmt.Stringer {
json := gabs.New()
// https://github.com/google/gopacket/blob/master/layers/ip4.go#L43
L3, _ := json.Object("L3")
L3.Set(ip4.Version, "v")
L3.Set(ip4.SrcIP, "src")
L3.Set(ip4.DstIP, "dst")
L3.Set(ip4.Id, "id")
L3.Set(ip4.IHL, "ihl")
L3.Set(ip4.TTL, "ttl")
L3.Set(ip4.TOS, "tos")
L3.Set(ip4.Length, "len")
L3.Set(ip4.FragOffset, "foff")
L3.Set(ip4.Checksum, "xsum")
proto, _ := L3.Object("proto")
proto.Set(ip4.Protocol, "num")
if sizeOfOptions := len(ip4.Options); sizeOfOptions > 0 {
opts, _ := L3.ArrayOfSize(sizeOfOptions, "opts")
for i, opt := range ip4.Options {
o, _ := opts.ObjectI(i)
o.Set(string(opt.OptionData), "data")
o.Set(opt.OptionType, "type")
}
}
// hashing bytes yields `uint64`, and addition is commutative:
// - so hashing the IP byte array representations and then adding then resulting `uint64`s is a commutative operation as well.
flowID := fnv1a.HashUint64(uint64(4) + fnv1a.HashBytes64(ip4.SrcIP.To4()) + fnv1a.HashBytes64(ip4.DstIP.To4()))
flowIDstr := strconv.FormatUint(flowID, 10)
L3.Set(flowIDstr, "flow") // IPv4(4) (0x04)
atDebugVerbosity(ctx,
t.pcapTranslator,
func(
ctx context.Context,
translator *pcapTranslator,
) {
proto.Set(ip4.Protocol.String(), "name")
// https://github.com/google/gopacket/blob/master/layers/ip4.go#L28-L40
L3.Set(strings.Split(ip4.Flags.String(), "|"), "flags")
networkFlow := ip4.NetworkFlow()
t.addEndpoints(L3, &networkFlow)
})
return json
}