github.com/FlowerWrong/netstack@v0.0.0-20191009141956-e5848263af28/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 "time"
    18  
    19  type pcapHeader struct {
    20  	// MagicNumber is the file magic number.
    21  	MagicNumber uint32
    22  
    23  	// VersionMajor is the major version number.
    24  	VersionMajor uint16
    25  
    26  	// VersionMinor is the minor version number.
    27  	VersionMinor uint16
    28  
    29  	// Thiszone is the GMT to local correction.
    30  	Thiszone int32
    31  
    32  	// Sigfigs is the accuracy of timestamps.
    33  	Sigfigs uint32
    34  
    35  	// Snaplen is the max length of captured packets, in octets.
    36  	Snaplen uint32
    37  
    38  	// Network is the data link type.
    39  	Network uint32
    40  }
    41  
    42  const pcapPacketHeaderLen = 16
    43  
    44  type pcapPacketHeader struct {
    45  	// Seconds is the timestamp seconds.
    46  	Seconds uint32
    47  
    48  	// Microseconds is the timestamp microseconds.
    49  	Microseconds uint32
    50  
    51  	// IncludedLength is the number of octets of packet saved in file.
    52  	IncludedLength uint32
    53  
    54  	// OriginalLength is the actual length of packet.
    55  	OriginalLength uint32
    56  }
    57  
    58  func newPCAPPacketHeader(incLen, orgLen uint32) pcapPacketHeader {
    59  	now := time.Now()
    60  	return pcapPacketHeader{
    61  		Seconds:        uint32(now.Unix()),
    62  		Microseconds:   uint32(now.Nanosecond() / 1000),
    63  		IncludedLength: incLen,
    64  		OriginalLength: orgLen,
    65  	}
    66  }