github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/pkg/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 "github.com/MerlinKodo/gvisor/pkg/tcpip" 23 "github.com/MerlinKodo/gvisor/pkg/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.PacketBufferPtr 54 maxCaptureLen int 55 } 56 57 func (p *pcapPacket) MarshalBinary() ([]byte, error) { 58 pkt := trimmedClone(p.packet) 59 defer pkt.DecRef() 60 packetSize := pkt.Size() 61 captureLen := p.maxCaptureLen 62 if packetSize < captureLen { 63 captureLen = packetSize 64 } 65 b := make([]byte, 16+captureLen) 66 binary.LittleEndian.PutUint32(b[0:4], uint32(p.timestamp.Unix())) 67 binary.LittleEndian.PutUint32(b[4:8], uint32(p.timestamp.Nanosecond()/1000)) 68 binary.LittleEndian.PutUint32(b[8:12], uint32(captureLen)) 69 binary.LittleEndian.PutUint32(b[12:16], uint32(packetSize)) 70 w := tcpip.SliceWriter(b[16:]) 71 for _, v := range pkt.AsSlices() { 72 if captureLen == 0 { 73 break 74 } 75 if len(v) > captureLen { 76 v = v[:captureLen] 77 } 78 n, err := w.Write(v) 79 if err != nil { 80 panic(err) 81 } 82 captureLen -= n 83 } 84 return b, nil 85 }