本文整理汇总了Golang中code/google/com/p/gopacket.NewPacket函数的典型用法代码示例。如果您正苦于以下问题:Golang NewPacket函数的具体用法?Golang NewPacket怎么用?Golang NewPacket使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewPacket函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
}
}
}
示例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")
}
}
示例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)
}
示例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)
}
}
}
示例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)
}
示例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()
}
}
示例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
}
示例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)
}
}
示例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}
}
}
示例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)
}
}
}
示例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)
}
示例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()
}
}
示例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)
}
}
示例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)
}
示例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()
}
}