@@ -57,6 +57,13 @@ type AncillaryVLAN struct {
5757 VLAN int
5858}
5959
60+ // AncillaryPktType structures are used to pass the packet type
61+ // as ancillary data via CaptureInfo.
62+ type AncillaryPktType struct {
63+ // The packet type provided by the kernel.
64+ Type uint8
65+ }
66+
6067// Stats is a set of counters detailing the work TPacket has done so far.
6168type Stats struct {
6269 // Packets is the total number of packets returned to the caller.
@@ -293,10 +300,11 @@ func (h *TPacket) releaseCurrentPacket() error {
293300// to old bytes when using ZeroCopyReadPacketData... if you need to keep data past
294301// the next time you call ZeroCopyReadPacketData, use ReadPacketData, which copies
295302// the bytes into a new buffer for you.
296- // tp, _ := NewTPacket(...)
297- // data1, _, _ := tp.ZeroCopyReadPacketData()
298- // // do everything you want with data1 here, copying bytes out of it if you'd like to keep them around.
299- // data2, _, _ := tp.ZeroCopyReadPacketData() // invalidates bytes in data1
303+ //
304+ // tp, _ := NewTPacket(...)
305+ // data1, _, _ := tp.ZeroCopyReadPacketData()
306+ // // do everything you want with data1 here, copying bytes out of it if you'd like to keep them around.
307+ // data2, _, _ := tp.ZeroCopyReadPacketData() // invalidates bytes in data1
300308func (h * TPacket ) ZeroCopyReadPacketData () (data []byte , ci gopacket.CaptureInfo , err error ) {
301309 h .mu .Lock ()
302310retry:
@@ -324,6 +332,9 @@ retry:
324332 if vlan >= 0 {
325333 ci .AncillaryData = append (ci .AncillaryData , AncillaryVLAN {vlan })
326334 }
335+ if h .opts .addPktType {
336+ ci .AncillaryData = append (ci .AncillaryData , AncillaryPktType {h .current .getPktType ()})
337+ }
327338 atomic .AddInt64 (& h .stats .Packets , 1 )
328339 h .headerNextNeeded = true
329340 h .mu .Unlock ()
0 commit comments