本文整理汇总了Golang中code/google/com/p/gopacket.NewPacket函数的典型用法代码示例。如果您正苦于以下问题:Golang NewPacket函数的具体用法?Golang NewPacket怎么用?Golang NewPacket使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewPacket函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestPacketDot11CtrlAck
func TestPacketDot11CtrlAck(t *testing.T) {
p := gopacket.NewPacket(testPacketDot11CtrlAck, LinkTypeIEEE80211Radio, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11}, t)
if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok {
if !got.ChecksumValid() {
t.Errorf("Dot11 packet processing failed:\nchecksum failed. got :\n%#v\n\n", got)
}
}
if got, ok := p.Layer(LayerTypeDot11).(*Dot11); ok {
want := &Dot11{
BaseLayer: BaseLayer{
Contents: []uint8{0xd4, 0x0, 0x0, 0x0, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52},
Payload: []uint8{},
},
Type: Dot11TypeCtrlAck,
Proto: 0x0,
Flags: 0x0,
DurationID: 0x0,
Address1: net.HardwareAddr{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52},
Address2: net.HardwareAddr(nil),
Address3: net.HardwareAddr(nil),
Address4: net.HardwareAddr(nil),
Checksum: 0x8776e946,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Dot11 packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want)
}
}
}
开发者ID:carriercomm,项目名称:gopacket,代码行数:34,代码来源:dot11_test.go
示例2: TestDecodeIPv6Jumbogram
func TestDecodeIPv6Jumbogram(t *testing.T) {
// Haven't found any of these in the wild or on example pcaps online, so had
// to generate one myself via scapy. Unfortunately, scapy can only
// str(packet) for packets with length < 65536, due to limitations in python's
// struct library, so I generated the header with:
// Ether() / IPv6(src='::1', dst='::2') / IPv6ExtHdrHopByHop(options=[Jumbo(jumboplen=70000)]) / TCP(sport=8888, dport=80)
// then added the payload manually ("payload" * 9996). The checksums here are
// not correct, but we don't check, so who cares ;)
dataStr := "\x00\x1f\xca\[email protected]$\xbe\x05'\x0b\x17\x86\xdd`\x00\x00\x00\x00\x00\[email protected]\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06\x00\xc2\x04\x00\x01\x11p\"\xb8\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00l\xd8\x00\x00"
payload := strings.Repeat("payload", 9996)
data := []byte(dataStr + payload)
p := gopacket.NewPacket(data, LinkTypeEthernet, gopacket.Default)
checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeIPv6HopByHop, LayerTypeTCP, gopacket.LayerTypePayload}, t)
if p.ApplicationLayer() == nil {
t.Error("Packet has no application layer")
} else if string(p.ApplicationLayer().Payload()) != payload {
t.Errorf("Jumbogram payload wrong")
}
// Check truncated for jumbograms
data = data[:len(data)-1]
p = gopacket.NewPacket(data, LinkTypeEthernet, gopacket.Default)
checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeIPv6HopByHop, LayerTypeTCP, gopacket.LayerTypePayload}, t)
if !p.Metadata().Truncated {
t.Error("Jumbogram should be truncated")
}
}
开发者ID:jcantrill,项目名称:geard,代码行数:26,代码来源:decode_test.go
示例3: TestPacketIPSecESP
func TestPacketIPSecESP(t *testing.T) {
p := gopacket.NewPacket(testPacketIPSecESP, LinkTypeEthernet, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeIPSecESP}, t)
}
开发者ID:dazer-chen,项目名称:gopacket,代码行数:7,代码来源:ipsec_test.go
示例4: TestPacketDot11DataARP
func TestPacketDot11DataARP(t *testing.T) {
p := gopacket.NewPacket(testPacketDot11DataARP, LinkTypeIEEE80211Radio, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeARP}, t)
if got, ok := p.Layer(LayerTypeARP).(*ARP); ok {
want := &ARP{
BaseLayer: BaseLayer{
Contents: []uint8{0x0, 0x1, 0x8, 0x0, 0x6, 0x4, 0x0, 0x1, 0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52, 0xa9, 0xfe, 0xf7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x43, 0x8, 0xe, 0x36},
Payload: []uint8{},
},
AddrType: 0x1,
Protocol: 0x800,
HwAddressSize: 0x6,
ProtAddressSize: 0x4,
Operation: 0x1,
SourceHwAddress: []uint8{0x0, 0x19, 0xe3, 0xd3, 0x53, 0x52},
SourceProtAddress: []uint8{0xa9, 0xfe, 0xf7, 0x0},
DstHwAddress: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
DstProtAddress: []uint8{0x43, 0x8, 0xe, 0x36},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("ARP packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want)
}
}
}
开发者ID:carriercomm,项目名称:gopacket,代码行数:29,代码来源:dot11_test.go
示例5: TestPacketDot11DataIP
func TestPacketDot11DataIP(t *testing.T) {
p := gopacket.NewPacket(testPacketDot11DataIP, LinkTypeIEEE80211Radio, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11Data, LayerTypeLLC, LayerTypeSNAP, LayerTypeIPv4, LayerTypeUDP, gopacket.LayerTypePayload}, t)
}
开发者ID:carriercomm,项目名称:gopacket,代码行数:7,代码来源:dot11_test.go
示例6: BenchmarkEndpoints
func BenchmarkEndpoints(b *testing.B) {
p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
flow := p.NetworkLayer().NetworkFlow()
for i := 0; i < b.N; i++ {
flow.Endpoints()
}
}
开发者ID:jcantrill,项目名称:geard,代码行数:7,代码来源:decode_test.go
示例7: getack
func getack(conn net.PacketConn, srcport layers.TCPPort, dstip string) (ack uint32, err error) {
for {
b := make([]byte, 4096)
log.Println("reading from conn")
var n int
var addr net.Addr
n, addr, err = conn.ReadFrom(b)
if err != nil {
log.Println("reading..", err)
return
} else if addr.String() == dstip {
// Decode a packet
packet := gopacket.NewPacket(b[:n], layers.LayerTypeTCP, gopacket.Default)
// Get the TCP layer from this packet
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.TCP)
if tcp.DstPort == srcport {
if tcp.SYN && tcp.ACK {
ack = tcp.Seq
} else {
err = errors.New("Port is CLOSED")
}
return
}
}
} else {
err = errors.New("Got packet not matching addr")
}
}
return
}
开发者ID:MDfog,项目名称:initcwndcheck,代码行数:31,代码来源:initcwndcheck.go
示例8: TestDecodeNortelDiscovery
func TestDecodeNortelDiscovery(t *testing.T) {
// http://www.thetechfirm.com/packets/nortel_btdp/btdp_nai.enc
data := []byte{
0x01, 0x00, 0x81, 0x00, 0x01, 0x00, 0x00, 0x04, 0x38, 0xe0, 0xcc, 0xde,
0x00, 0x13, 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x81, 0x01, 0xa2, 0xac, 0x13,
0x58, 0x03, 0x00, 0x04, 0x15, 0x30, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00,
0x00, 0x04, 0x38, 0xe0, 0xcc, 0xde, 0x80, 0x6a, 0x00, 0x01, 0x14, 0x00,
0x02, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
p := gopacket.NewPacket(data, LinkTypeEthernet, gopacket.Default)
wantLayers := []gopacket.LayerType{LayerTypeEthernet, LayerTypeLLC, LayerTypeSNAP, LayerTypeNortelDiscovery}
checkLayers(p, wantLayers, t)
want := &NortelDiscovery{
IPAddress: []byte{172, 19, 88, 3},
SegmentID: []byte{0x00, 0x04, 0x15},
Chassis: NDPChassisBayStack450101001000Switches,
Backplane: NDPBackplaneEthernetFastEthernetGigabitEthernet,
State: NDPStateHeartbeat,
NumLinks: 0,
}
ndpL := p.Layer(LayerTypeNortelDiscovery)
info, _ := ndpL.(*NortelDiscovery)
if !reflect.DeepEqual(info, want) {
t.Errorf("Values mismatch, \ngot %#v\nwant %#v\n", info, want)
}
}
开发者ID:jcantrill,项目名称:geard,代码行数:27,代码来源:decode_test.go
示例9: go_callback
//export go_callback
func go_callback(queueId C.int, data *C.uchar, length C.int, cb *chan NFPacket) VerdictModified {
xdata := C.GoBytes(unsafe.Pointer(data), length)
packet := gopacket.NewPacket(xdata, layers.LayerTypeIPv4, gopacket.DecodeOptions{
Lazy: true,
NoCopy: true,
SkipDecodeRecovery: false,
})
p := NFPacket{
verdictChannel: make(chan Verdict),
verdictModifiedChannel: make(chan VerdictPacket),
Packet: packet,
}
select {
case (*cb) <- p:
select {
case v := <-p.verdictModifiedChannel:
return VerdictModified{
verdict: C.uint(v.Verdict),
data: (*C.uchar)(unsafe.Pointer(&v.Packet[0])),
length: C.uint(len(v.Packet)),
}
case v := <-p.verdictChannel:
return VerdictModified{
verdict: C.uint(v),
data: nil,
length: 0,
}
}
default:
return VerdictModified{verdict: C.uint(NF_DROP), data: nil, length: 0}
}
}
开发者ID:wheelcomplex,项目名称:go-netfilter-queue,代码行数:33,代码来源:netfilter.go
示例10: TestPacketUSB0
func TestPacketUSB0(t *testing.T) {
p := gopacket.NewPacket(testPacketUSB0, LinkTypeLinuxUSB, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeUSB, LayerTypeUSBInterrupt}, t)
if got, ok := p.Layer(LayerTypeUSB).(*USB); ok {
want := &USB{
BaseLayer: BaseLayer{
Contents: []uint8{0x0, 0x38, 0x4a, 0x3b, 0x0, 0x88, 0xff, 0xff, 0x43, 0x1, 0x81, 0x1, 0x2, 0x0, 0x2d, 0x0, 0xc0, 0xd3, 0x5b, 0x50, 0x0, 0x0, 0x0, 0x0, 0x8a, 0x85, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0},
Payload: []uint8{0x4},
},
ID: 0xffff88003b4a3800,
EventType: USBEventTypeComplete,
TransferType: USBTransportTypeInterrupt,
Direction: 0x1,
EndpointNumber: 0x1,
DeviceAddress: 0x1,
BusID: 0x2,
TimestampSec: 1348195264,
TimestampUsec: 689546,
Setup: false,
Data: true,
Status: 0,
UrbLength: 0x1,
UrbDataLength: 0x1,
}
if !reflect.DeepEqual(got, want) {
t.Errorf("USB packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want)
}
}
}
开发者ID:dazer-chen,项目名称:gopacket,代码行数:35,代码来源:usb_test.go
示例11: TestICMP
func TestICMP(t *testing.T) {
p := gopacket.NewPacket(testICMP, LinkTypeEthernet, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload}, t)
testSerialization(t, p, testICMP)
}
开发者ID:jcantrill,项目名称:geard,代码行数:8,代码来源:decode_test.go
示例12: BenchmarkTCPTransportLayerFromDecodedPacket
func BenchmarkTCPTransportLayerFromDecodedPacket(b *testing.B) {
b.StopTimer()
p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Default)
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = p.TransportLayer()
}
}
开发者ID:jcantrill,项目名称:geard,代码行数:8,代码来源:decode_test.go
示例13: BenchmarkFmtVerboseString
func BenchmarkFmtVerboseString(b *testing.B) {
b.StopTimer()
p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Default)
b.StartTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf("%#v", p)
}
}
开发者ID:jcantrill,项目名称:geard,代码行数:8,代码来源:decode_test.go
示例14: TestPacketP6196
func TestPacketP6196(t *testing.T) {
p := gopacket.NewPacket(testPacketP6196, LinkTypeIEEE80211Radio, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeRadioTap, LayerTypeDot11, LayerTypeDot11WEP}, t)
}
开发者ID:carriercomm,项目名称:gopacket,代码行数:8,代码来源:dot11_test.go
示例15: BenchmarkPacketDumpString
func BenchmarkPacketDumpString(b *testing.B) {
b.StopTimer()
p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Default)
b.StartTimer()
for i := 0; i < b.N; i++ {
p.String()
}
}
开发者ID:jcantrill,项目名称:geard,代码行数:8,代码来源:decode_test.go
示例16: BenchmarkTCPLayerClassFromDecodedPacket
func BenchmarkTCPLayerClassFromDecodedPacket(b *testing.B) {
b.StopTimer()
p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Default)
lc := gopacket.NewLayerClass([]gopacket.LayerType{LayerTypeTCP})
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = p.LayerClass(lc)
}
}
开发者ID:jcantrill,项目名称:geard,代码行数:9,代码来源:decode_test.go
示例17: getSerializeLayers
func getSerializeLayers() []gopacket.SerializableLayer {
p := gopacket.NewPacket(testSimpleTCPPacket, LinkTypeEthernet, gopacket.Default)
slayers := []gopacket.SerializableLayer{}
for _, l := range p.Layers() {
slayers = append(slayers, l.(gopacket.SerializableLayer))
}
p.Layer(LayerTypeTCP).(*TCP).SetNetworkLayerForChecksum(
p.NetworkLayer())
return slayers
}
开发者ID:jcantrill,项目名称:geard,代码行数:10,代码来源:decode_test.go
示例18: TestPacketICMPv6
func TestPacketICMPv6(t *testing.T) {
p := gopacket.NewPacket(testPacketICMPv6, LinkTypeEthernet, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv6, LayerTypeICMPv6, gopacket.LayerTypePayload}, t)
if got, ok := p.Layer(LayerTypeIPv6).(*IPv6); ok {
want := &IPv6{
BaseLayer: BaseLayer{
Contents: []byte{0x60, 0x0, 0x0, 0x0, 0x0, 0x18,
0x3a, 0xff, 0x26, 0x20, 0x0, 0x0, 0x10, 0x5, 0x0, 0x0, 0x26, 0xbe, 0x5,
0xff, 0xfe, 0x27, 0xb, 0x17, 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x2, 0x1f, 0xca, 0xff, 0xfe, 0xb3, 0x76, 0x40},
Payload: []byte{0x88, 0x0, 0x1e, 0xd6, 0x40, 0x0, 0x0, 0x0, 0x26, 0x20,
0x0, 0x0, 0x10, 0x5, 0x0, 0x0, 0x26, 0xbe, 0x5, 0xff, 0xfe, 0x27, 0xb,
0x17},
},
Version: 6,
TrafficClass: 0,
FlowLabel: 0,
Length: 24,
NextHeader: IPProtocolICMPv6,
HopLimit: 255,
SrcIP: net.IP{0x26, 0x20, 0x0, 0x0, 0x10, 0x5, 0x0, 0x0, 0x26, 0xbe, 0x5, 0xff, 0xfe, 0x27, 0xb, 0x17},
DstIP: net.IP{0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x1f, 0xca, 0xff, 0xfe, 0xb3, 0x76, 0x40},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("IPv6 packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want)
}
} else {
t.Error("No IPv6 layer type found in packet")
}
if got, ok := p.Layer(LayerTypeICMPv6).(*ICMPv6); ok {
want := &ICMPv6{
BaseLayer: BaseLayer{
Contents: []byte{0x88, 0x0, 0x1e, 0xd6, 0x40, 0x0, 0x0, 0x0},
Payload: []byte{0x26, 0x20, 0x0, 0x0, 0x10,
0x5, 0x0, 0x0, 0x26, 0xbe, 0x5, 0xff, 0xfe, 0x27, 0xb, 0x17},
},
TypeCode: 0x8800,
Checksum: 0x1ed6,
TypeBytes: []byte{0x40, 0x0, 0x0, 0x0},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("ICMPv6 packet processing failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", got, want)
}
if got.TypeCode.String() != "NeighborAdvertisement(0)" {
t.Errorf("ICMPv6 type code, got %q want 'NeighborAdvertisement(0)'", got.TypeCode.String())
}
} else {
t.Error("No ICMPv6 layer type found in packet")
}
}
开发者ID:dazer-chen,项目名称:gopacket,代码行数:53,代码来源:icmp6_test.go
示例19: go_callback
//export go_callback
func go_callback(queueId C.int, data *C.uchar, len C.int, cb *chan NFPacket) C.struct_NFResult {
xdata := C.GoBytes(unsafe.Pointer(data), len)
packet := gopacket.NewPacket(xdata, layers.LayerTypeIPv4, gopacket.DecodeOptions{Lazy: true, NoCopy: true})
p := NFPacket{resultChannel: make(chan C.struct_NFResult), Packet: packet}
select {
case (*cb) <- p:
r := <-p.resultChannel
return r
default:
return C.struct_NFResult{Verdict: C.uint(NF_DROP), Data: nil, Len: 0}
}
}
开发者ID:ThomasJClark,项目名称:cs4404project,代码行数:13,代码来源:netfilter.go
示例20: go_callback
//export go_callback
func go_callback(queueId C.int, data *C.uchar, len C.int, cb *chan NFPacket) Verdict {
xdata := C.GoBytes(unsafe.Pointer(data), len)
packet := gopacket.NewPacket(xdata, layers.LayerTypeIPv4, gopacket.DecodeOptions{true, true})
p := NFPacket{verdictChannel: make(chan Verdict), Packet: packet}
select {
case (*cb) <- p:
v := <-p.verdictChannel
return v
default:
return NF_DROP
}
}
开发者ID:arunmk,项目名称:go-netfilter-queue,代码行数:13,代码来源:netfilter.go
注:本文中的code/google/com/p/gopacket.NewPacket函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论