本文整理汇总了Golang中code/google/com/p/gopacket.Packet.Layers方法的典型用法代码示例。如果您正苦于以下问题:Golang Packet.Layers方法的具体用法?Golang Packet.Layers怎么用?Golang Packet.Layers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类code/google/com/p/gopacket.Packet
的用法示例。
在下文中一共展示了Packet.Layers方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testSerialization
func testSerialization(t *testing.T, p gopacket.Packet, data []byte) {
// Test re-serialization.
slayers := []gopacket.SerializableLayer{}
for _, l := range p.Layers() {
slayers = append(slayers, l.(gopacket.SerializableLayer))
if h, ok := l.(canSetNetLayer); ok {
if err := h.SetNetworkLayerForChecksum(p.NetworkLayer()); err != nil {
t.Fatal("can't set network layer:", err)
}
}
}
for _, opts := range []gopacket.SerializeOptions{
gopacket.SerializeOptions{},
gopacket.SerializeOptions{FixLengths: true},
gopacket.SerializeOptions{ComputeChecksums: true},
gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true},
} {
buf := gopacket.NewSerializeBuffer()
err := gopacket.SerializeLayers(buf, opts, slayers...)
if err != nil {
t.Errorf("unable to reserialize layers with opts %#v: %v", opts, err)
} else if !bytes.Equal(buf.Bytes(), data) {
t.Errorf("serialization failure with opts %#v:\n---want---\n%v\n---got---\n%v\nBASH-colorized diff, want->got:\n%v", opts, hex.Dump(data), hex.Dump(buf.Bytes()), diffString(data, buf.Bytes()))
}
}
}
示例2: handlePacket
func handlePacket(p gopacket.Packet) {
ap := AP{}
// extract beacon
// extract Ssid
for _, l := range p.Layers() {
switch l.LayerType() {
case layers.LayerTypeDot11MgmtBeacon:
beacon, ok := p.Layer(layers.LayerTypeDot11MgmtBeacon).(*layers.Dot11MgmtBeacon)
if !ok {
log.Println("Could not marshal layer thing")
continue
}
pack := gopacket.NewPacket(beacon.LayerContents(), layers.LayerTypeDot11MgmtBeacon, gopacket.Default)
for _, subpack := range pack.Layers() {
info, ok := subpack.(*layers.Dot11InformationElement)
if !ok {
continue
}
if info.ID == layers.Dot11InformationElementIDSSID {
ap.Ssid = fmt.Sprintf("%s", info.Info)
break
}
}
case layers.LayerTypeDot11:
base, ok := p.Layer(layers.LayerTypeDot11).(*layers.Dot11)
if !ok {
continue
}
ap.Bssid = base.Address2
continue
case layers.LayerTypeRadioTap:
radio, ok := p.Layer(layers.LayerTypeRadioTap).(*layers.RadioTap)
if !ok {
continue
}
ap.Channel = radio.ChannelFrequency
continue
}
}
APList[ap.Ssid] = ap
}
示例3: checkLayers
func checkLayers(p gopacket.Packet, want []gopacket.LayerType, t *testing.T) {
layers := p.Layers()
t.Log("Checking packet layers, want", want)
for _, l := range layers {
t.Logf(" Got layer %v, %d bytes, payload of %d bytes", l.LayerType(),
len(l.LayerContents()), len(l.LayerPayload()))
}
t.Log(p)
if len(layers) != len(want) {
t.Errorf(" Number of layers mismatch: got %d want %d", len(layers),
len(want))
return
}
for i, l := range layers {
if l.LayerType() != want[i] {
t.Errorf(" Layer %d mismatch: got %v want %v", i, l.LayerType(),
want[i])
}
}
}