inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/tcpip/link/sniffer/pcap.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sniffer
    16  
    17  import (
    18  	"encoding"
    19  	"encoding/binary"
    20  	"time"
    21  
    22  	"inet.af/netstack/tcpip"
    23  	"inet.af/netstack/tcpip/stack"
    24  )
    25  
    26  type pcapHeader struct {
    27  	// MagicNumber is the file magic number.
    28  	MagicNumber uint32
    29  
    30  	// VersionMajor is the major version number.
    31  	VersionMajor uint16
    32  
    33  	// VersionMinor is the minor version number.
    34  	VersionMinor uint16
    35  
    36  	// Thiszone is the GMT to local correction.
    37  	Thiszone int32
    38  
    39  	// Sigfigs is the accuracy of timestamps.
    40  	Sigfigs uint32
    41  
    42  	// Snaplen is the max length of captured packets, in octets.
    43  	Snaplen uint32
    44  
    45  	// Network is the data link type.
    46  	Network uint32
    47  }
    48  
    49  var _ encoding.BinaryMarshaler = (*pcapPacket)(nil)
    50  
    51  type pcapPacket struct {
    52  	timestamp     time.Time
    53  	packet        *stack.PacketBuffer
    54  	maxCaptureLen int
    55  }
    56  
    57  func (p *pcapPacket) MarshalBinary() ([]byte, error) {
    58  	packetSize := p.packet.Size()
    59  	captureLen := p.maxCaptureLen
    60  	if packetSize < captureLen {
    61  		captureLen = packetSize
    62  	}
    63  	b := make([]byte, 16+captureLen)
    64  	binary.BigEndian.PutUint32(b[0:4], uint32(p.timestamp.Unix()))
    65  	binary.BigEndian.PutUint32(b[4:8], uint32(p.timestamp.Nanosecond()/1000))
    66  	binary.BigEndian.PutUint32(b[8:12], uint32(captureLen))
    67  	binary.BigEndian.PutUint32(b[12:16], uint32(packetSize))
    68  	w := tcpip.SliceWriter(b[16:])
    69  	for _, v := range p.packet.Views() {
    70  		if captureLen == 0 {
    71  			break
    72  		}
    73  		if len(v) > captureLen {
    74  			v = v[:captureLen]
    75  		}
    76  		n, err := w.Write(v)
    77  		if err != nil {
    78  			panic(err)
    79  		}
    80  		captureLen -= n
    81  	}
    82  	return b, nil
    83  }