add windows support

This commit is contained in:
xiaobiao
2022-04-18 22:28:49 +08:00
parent 87a877750f
commit 43c68ecf2f
33 changed files with 2697 additions and 346 deletions

View File

@@ -1,38 +0,0 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
#*
*~
# examples binaries
examples/synscan/synscan
examples/pfdump/pfdump
examples/pcapdump/pcapdump
examples/httpassembly/httpassembly
examples/statsassembly/statsassembly
examples/arpscan/arpscan
examples/bidirectional/bidirectional
examples/bytediff/bytediff
examples/reassemblydump/reassemblydump
layers/gen
macs/gen
pcap/pcap_tester

View File

@@ -1,7 +0,0 @@
#!/bin/bash
cd "$(dirname $0)"
if [ -n "$(go fmt ./...)" ]; then
echo "Go code is not formatted, run 'go fmt github.com/google/stenographer/...'" >&2
exit 1
fi

View File

@@ -1,28 +0,0 @@
#!/bin/bash
cd "$(dirname $0)"
go get golang.org/x/lint/golint
DIRS=". tcpassembly tcpassembly/tcpreader ip4defrag reassembly macs pcapgo pcap afpacket pfring routing defrag/lcmdefrag"
# Add subdirectories here as we clean up golint on each.
for subdir in $DIRS; do
pushd $subdir
if golint |
grep -v CannotSetRFMon | # pcap exported error name
grep -v DataLost | # tcpassembly/tcpreader exported error name
grep .; then
exit 1
fi
popd
done
pushd layers
for file in *.go; do
if cat .lint_blacklist | grep -q $file; then
echo "Skipping lint of $file due to .lint_blacklist"
elif golint $file | grep .; then
echo "Lint error in file $file"
exit 1
fi
done
popd

View File

@@ -1,10 +0,0 @@
#!/bin/bash
cd "$(dirname $0)"
DIRS=". layers pcap pcapgo tcpassembly tcpassembly/tcpreader routing ip4defrag bytediff macs defrag/lcmdefrag"
set -e
for subdir in $DIRS; do
pushd $subdir
go vet
popd
done

View File

@@ -1,9 +0,0 @@
#!/bin/bash
set -ev
go get github.com/google/gopacket
go get github.com/google/gopacket/layers
go get github.com/google/gopacket/tcpassembly
go get github.com/google/gopacket/reassembly
go get github.com/google/gopacket/pcapgo

View File

@@ -1,10 +0,0 @@
#!/bin/bash
set -ev
go test github.com/google/gopacket
go test github.com/google/gopacket/layers
go test github.com/google/gopacket/tcpassembly
go test github.com/google/gopacket/reassembly
go test github.com/google/gopacket/pcapgo
go test github.com/google/gopacket/pcap

View File

@@ -1,55 +0,0 @@
language: go
go:
- 1.11.x
- 1.12.x
- master
addons:
apt:
packages:
libpcap-dev
# use modules except for older versions (see below)
install: true
env:
- GO111MODULE=on
script: ./.travis.script.sh
matrix:
fast_finish: true
allow_failures:
- go: master
jobs:
include:
- go: 1.5.x
install: ./.travis.install.sh
- go: 1.6.x
install: ./.travis.install.sh
- go: 1.7.x
install: ./.travis.install.sh
- go: 1.8.x
install: ./.travis.install.sh
- go: 1.9.x
install: ./.travis.install.sh
- go: 1.10.x
install: ./.travis.install.sh
- os: osx
go: 1.x
- os: windows
go: 1.x
# winpcap does not work on travis ci - so install nmap to get libpcap
before_install: choco install nmap
- stage: style
name: "fmt/vet/lint"
go: 1.x
script:
- ./.travis.gofmt.sh
- ./.travis.govet.sh
- ./.travis.golint.sh
stages:
- style
- test

View File

@@ -33,6 +33,7 @@ Jesse Ward <jesse@jesseward.com>
Kane Mathers <kane@kanemathers.name>
Jose Selvi <jselvi@pentester.es>
Yerden Zhumabekov <yerden.zhumabekov@gmail.com>
Jensen Hwa <jensenhwa@gmail.com>
-----------------------------------------------
FORKED FROM github.com/akrennmair/gopcap

View File

@@ -6,7 +6,7 @@ See [godoc](https://godoc.org/github.com/google/gopacket) for more details.
[![Build Status](https://travis-ci.org/google/gopacket.svg?branch=master)](https://travis-ci.org/google/gopacket)
[![GoDoc](https://godoc.org/github.com/google/gopacket?status.svg)](https://godoc.org/github.com/google/gopacket)
Minimum Go version required is 1.5 except for pcapgo/EthernetHandle, afpacket, and bsdbpf which need at least 1.7 due to x/sys/unix dependencies.
Minimum Go version required is 1.5 except for pcapgo/EthernetHandle, afpacket, and bsdbpf which need at least 1.9 due to x/sys/unix dependencies.
Originally forked from the gopcap project written by Andreas
Krennmair <ak@synflood.at> (http://github.com/akrennmair/gopcap).

194
vendor/github.com/google/gopacket/benchmark_test.go generated vendored Normal file
View File

@@ -0,0 +1,194 @@
// Copyright 2012, Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package gopacket
import (
"runtime"
"testing"
)
// A few benchmarks for figuring out exactly how fast some underlying Go
// things are.
type testError struct{}
func (t *testError) Error() string { return "abc" }
func BenchmarkTypeAssertion(b *testing.B) {
var e error = &testError{}
for i := 0; i < b.N; i++ {
_, _ = e.(*testError)
}
}
func BenchmarkMapLookup(b *testing.B) {
m := map[LayerType]bool{
LayerTypePayload: true,
}
for i := 0; i < b.N; i++ {
_ = m[LayerTypePayload]
}
}
func BenchmarkNilMapLookup(b *testing.B) {
var m map[LayerType]bool
for i := 0; i < b.N; i++ {
_ = m[LayerTypePayload]
}
}
func BenchmarkNilMapLookupWithNilCheck(b *testing.B) {
var m map[LayerType]bool
for i := 0; i < b.N; i++ {
if m != nil {
_ = m[LayerTypePayload]
}
}
}
func BenchmarkArrayLookup(b *testing.B) {
m := make([]bool, 100)
for i := 0; i < b.N; i++ {
_ = m[LayerTypePayload]
}
}
var testError1 = &testError{}
var testError2 error = testError1
func BenchmarkTypeToInterface1(b *testing.B) {
var e error
for i := 0; i < b.N; i++ {
e = testError1
}
// Have to do someting with 'e' or the compiler complains about an unused
// variable.
testError2 = e
}
func BenchmarkTypeToInterface2(b *testing.B) {
var e error
for i := 0; i < b.N; i++ {
e = testError2
}
// Have to do someting with 'e' or the compiler complains about an unused
// variable.
testError2 = e
}
var decodeOpts DecodeOptions
func decodeOptsByValue(_ DecodeOptions) {}
func decodeOptsByPointer(_ *DecodeOptions) {}
func BenchmarkPassDecodeOptionsByValue(b *testing.B) {
for i := 0; i < b.N; i++ {
decodeOptsByValue(decodeOpts)
}
}
func BenchmarkPassDecodeOptionsByPointer(b *testing.B) {
for i := 0; i < b.N; i++ {
decodeOptsByPointer(&decodeOpts)
}
}
func BenchmarkLockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
runtime.LockOSThread()
}
}
func BenchmarkUnlockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
runtime.UnlockOSThread()
}
}
func lockUnlock() {
runtime.LockOSThread()
runtime.UnlockOSThread()
}
func lockDeferUnlock() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
func BenchmarkLockUnlockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
lockUnlock()
}
}
func BenchmarkLockDeferUnlockOSThread(b *testing.B) {
for i := 0; i < b.N; i++ {
lockDeferUnlock()
}
}
func BenchmarkUnbufferedChannel(b *testing.B) {
ca := make(chan bool)
cb := make(chan bool)
defer close(ca)
go func() {
defer close(cb)
for range ca {
cb <- true
}
}()
for i := 0; i < b.N; i++ {
ca <- true
<-cb
}
}
func BenchmarkSmallBufferedChannel(b *testing.B) {
ca := make(chan bool, 1)
cb := make(chan bool, 1)
defer close(ca)
go func() {
defer close(cb)
for range ca {
cb <- true
}
}()
for i := 0; i < b.N; i++ {
ca <- true
<-cb
}
}
func BenchmarkLargeBufferedChannel(b *testing.B) {
ca := make(chan bool, 1000)
cb := make(chan bool, 1000)
defer close(ca)
go func() {
defer close(cb)
for range ca {
cb <- true
}
}()
for i := 0; i < b.N; i++ {
ca <- true
<-cb
}
}
func BenchmarkEndpointFastHashShort(b *testing.B) {
e := Endpoint{typ: 1, len: 2}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}
func BenchmarkEndpointFastHashLong(b *testing.B) {
e := Endpoint{typ: 1, len: 16}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}
func BenchmarkFlowFastHashShort(b *testing.B) {
e := Flow{typ: 1, slen: 2, dlen: 2}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}
func BenchmarkFlowFastHashLong(b *testing.B) {
e := Flow{typ: 1, slen: 16, dlen: 16}
for i := 0; i < b.N; i++ {
e.FastHash()
}
}

View File

@@ -208,7 +208,7 @@ based on endpoint criteria:
}
}
// Find all packets coming from UDP port 1000 to UDP port 500
interestingFlow := gopacket.NewFlow(layers.NewUDPPortEndpoint(1000), layers.NewUDPPortEndpoint(500))
interestingFlow := gopacket.FlowFromEndpoints(layers.NewUDPPortEndpoint(1000), layers.NewUDPPortEndpoint(500))
if t := packet.NetworkLayer(); t != nil && t.TransportFlow() == interestingFlow {
fmt.Println("Found that UDP flow I was looking for!")
}

View File

@@ -1,75 +0,0 @@
// Copyright 2019 The GoPacket Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
// +build ignore
// This file generates LayersDecoder function for DecodingLayerContainer
// go run gen.go | gofmt > layers_decoder.go
package main
import (
"fmt"
"os"
"time"
)
const headerFmt = `// Copyright 2019 The GoPacket Authors. All rights reserved.
package gopacket
// Created by gen.go, don't edit manually
// Generated at %s
// LayersDecoder returns DecodingLayerFunc for specified
// DecodingLayerContainer, LayerType value to start decoding with and
// some DecodeFeedback.
func LayersDecoder(dl DecodingLayerContainer, first LayerType, df DecodeFeedback) DecodingLayerFunc {
firstDec, ok := dl.Decoder(first)
if !ok {
return func([]byte, *[]LayerType) (LayerType, error) {
return first, nil
}
}
`
var funcBody = `return func(data []byte, decoded *[]LayerType) (LayerType, error) {
*decoded = (*decoded)[:0] // Truncated decoded layers.
typ := first
decoder := firstDec
for {
if err := decoder.DecodeFromBytes(data, df); err != nil {
return LayerTypeZero, err
}
*decoded = append(*decoded, typ)
typ = decoder.NextLayerType()
if data = decoder.LayerPayload(); len(data) == 0 {
break
}
if decoder, ok = dlc.Decoder(typ); !ok {
return typ, nil
}
}
return LayerTypeZero, nil
}`
func main() {
fmt.Fprintf(os.Stderr, "Writing results to stdout\n")
types := []string{
"DecodingLayerSparse",
"DecodingLayerArray",
"DecodingLayerMap",
}
fmt.Printf(headerFmt, time.Now())
for _, t := range types {
fmt.Printf("if dlc, ok := dl.(%s); ok {", t)
fmt.Println(funcBody)
fmt.Println("}")
}
fmt.Println("dlc := dl")
fmt.Println(funcBody)
fmt.Println("}")
}

View File

@@ -3,6 +3,9 @@ module github.com/google/gopacket
go 1.12
require (
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67
github.com/vishvananda/netlink v1.1.0
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
golang.org/x/sys v0.0.0-20200217220822-9197077df867
)

View File

@@ -1,7 +1,27 @@
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f h1:p4VB7kIXpOQvVn1ZaTIVp+3vuYAXFe3OJEvjbUYJLaA=
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67 h1:1Fzlr8kkDLQwqMP8GxrhptBLqZG/EDpiATneiZHY998=
golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200217220822-9197077df867 h1:JoRuNIf+rpHl+VhScRQQvzbHed86tKkqwPMV34T8myw=
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

62
vendor/github.com/google/gopacket/packet_test.go generated vendored Normal file
View File

@@ -0,0 +1,62 @@
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package gopacket
import (
"io"
"reflect"
"testing"
)
type embedded struct {
A, B int
}
type embedding struct {
embedded
C, D int
}
func TestDumpEmbedded(t *testing.T) {
e := embedding{embedded: embedded{A: 1, B: 2}, C: 3, D: 4}
if got, want := layerString(reflect.ValueOf(e), false, false), "{A=1 B=2 C=3 D=4}"; got != want {
t.Errorf("embedded dump mismatch:\n got: %v\n want: %v", got, want)
}
}
type singlePacketSource [1][]byte
func (s *singlePacketSource) ReadPacketData() ([]byte, CaptureInfo, error) {
if (*s)[0] == nil {
return nil, CaptureInfo{}, io.EOF
}
out := (*s)[0]
(*s)[0] = nil
return out, CaptureInfo{}, nil
}
func TestConcatPacketSources(t *testing.T) {
sourceA := &singlePacketSource{[]byte{1}}
sourceB := &singlePacketSource{[]byte{2}}
sourceC := &singlePacketSource{[]byte{3}}
concat := ConcatFinitePacketDataSources(sourceA, sourceB, sourceC)
a, _, err := concat.ReadPacketData()
if err != nil || len(a) != 1 || a[0] != 1 {
t.Errorf("expected [1], got %v/%v", a, err)
}
b, _, err := concat.ReadPacketData()
if err != nil || len(b) != 1 || b[0] != 2 {
t.Errorf("expected [2], got %v/%v", b, err)
}
c, _, err := concat.ReadPacketData()
if err != nil || len(c) != 1 || c[0] != 3 {
t.Errorf("expected [3], got %v/%v", c, err)
}
if _, _, err := concat.ReadPacketData(); err != io.EOF {
t.Errorf("expected io.EOF, got %v", err)
}
}

73
vendor/github.com/google/gopacket/time_test.go generated vendored Normal file
View File

@@ -0,0 +1,73 @@
// Copyright 2019 The GoPacket Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package gopacket
import (
"testing"
"time"
)
func TestToDuration(t *testing.T) {
for i, test := range []struct {
r TimestampResolution
d time.Duration
}{
{
TimestampResolutionMillisecond,
time.Millisecond,
},
{
TimestampResolutionMicrosecond,
time.Microsecond,
},
{
TimestampResolutionNanosecond,
time.Nanosecond,
},
{
TimestampResolutionNTP,
0, // this is not representable since it's ~0.233 nanoseconds
},
{
TimestampResolution{2, -16},
15258,
},
{
TimestampResolution{2, 1},
2 * time.Second,
},
{
TimestampResolution{10, 1},
10 * time.Second,
},
{
TimestampResolution{10, 0},
time.Second,
},
{
TimestampResolution{2, 0},
time.Second,
},
{
TimestampResolution{0, 0},
0,
},
{
TimestampResolution{3, 2},
9 * time.Second,
},
{
TimestampResolution{3, -2},
111111111,
},
} {
d := test.r.ToDuration()
if d != test.d {
t.Errorf("%d: resolution: %s want: %d got: %d", i, test.r, test.d, d)
}
}
}

94
vendor/github.com/google/gopacket/writer_test.go generated vendored Normal file
View File

@@ -0,0 +1,94 @@
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package gopacket
import (
"fmt"
"testing"
)
func TestExponentialSizeIncreasePrepend(t *testing.T) {
var b serializeBuffer
for i, test := range []struct {
prepend, size int
}{
{2, 2},
{2, 4},
{2, 8},
{2, 8},
{2, 16},
{2, 16},
{2, 16},
{2, 16},
{2, 32},
} {
b.PrependBytes(test.prepend)
if test.size != cap(b.data) {
t.Error(i, "size want", test.size, "got", cap(b.data))
}
}
b.Clear()
if b.start != 32 {
t.Error(b.start)
}
}
func TestExponentialSizeIncreaseAppend(t *testing.T) {
var b serializeBuffer
for i, test := range []struct {
appnd, size int
}{
{2, 2},
{2, 4},
{2, 8},
{2, 8},
{2, 16},
{2, 16},
{2, 16},
{2, 16},
{2, 32},
} {
b.AppendBytes(test.appnd)
if test.size != cap(b.data) {
t.Error(i, "size want", test.size, "got", cap(b.data))
}
}
b.Clear()
if b.start != 0 {
t.Error(b.start)
}
}
func ExampleSerializeBuffer() {
b := NewSerializeBuffer()
fmt.Println("1:", b.Bytes())
bytes, _ := b.PrependBytes(3)
copy(bytes, []byte{1, 2, 3})
fmt.Println("2:", b.Bytes())
bytes, _ = b.AppendBytes(2)
copy(bytes, []byte{4, 5})
fmt.Println("3:", b.Bytes())
bytes, _ = b.PrependBytes(1)
copy(bytes, []byte{0})
fmt.Println("4:", b.Bytes())
bytes, _ = b.AppendBytes(3)
copy(bytes, []byte{6, 7, 8})
fmt.Println("5:", b.Bytes())
b.Clear()
fmt.Println("6:", b.Bytes())
bytes, _ = b.PrependBytes(2)
copy(bytes, []byte{9, 9})
fmt.Println("7:", b.Bytes())
// Output:
// 1: []
// 2: [1 2 3]
// 3: [1 2 3 4 5]
// 4: [0 1 2 3 4 5]
// 5: [0 1 2 3 4 5 6 7 8]
// 6: []
// 7: [9 9]
}