Example #1
0
func Dissect(dissect bool, data []byte) {
	lock.Lock()
	if dissect {
		parser.DecodeLayers(data, &decoded)

		for _, typ := range decoded {
			switch typ {
			case layers.LayerTypeEthernet:
				fmt.Println(gopacket.LayerString(&eth))
			case layers.LayerTypeIPv4:
				fmt.Println(gopacket.LayerString(&ip4))
			case layers.LayerTypeIPv6:
				fmt.Println(gopacket.LayerString(&ip6))
			case layers.LayerTypeTCP:
				fmt.Println(gopacket.LayerString(&tcp))
			case layers.LayerTypeUDP:
				fmt.Println(gopacket.LayerString(&udp))
			case layers.LayerTypeICMPv4:
				fmt.Println(gopacket.LayerString(&icmp4))
			case layers.LayerTypeICMPv6:
				fmt.Println(gopacket.LayerString(&icmp6))
			default:
				fmt.Println("Unknown layer")
			}
		}
		if parser.Truncated {
			fmt.Println("  Packet has been truncated")
		}

	} else {
		fmt.Println(hex.Dump(data))
	}
	lock.Unlock()
}
Example #2
0
func (rp *reusingPacket) String() string {
	var b bytes.Buffer
	var err error
	_, err = fmt.Fprintf(&b, "PACKET: %d bytes", len(rp.Data()))
	errs.CheckE(err)
	if rp.ci.Length > 0 {
		_, err = fmt.Fprintf(&b, ", wire length %d cap length %d", rp.ci.Length, rp.ci.CaptureLength)
		errs.CheckE(err)
	}
	if !rp.ci.Timestamp.IsZero() {
		_, err = fmt.Fprintf(&b, " @ %v", rp.ci.Timestamp)
		errs.CheckE(err)
	}
	b.WriteByte('\n')
	for i, l := range rp.layers {
		if gl, ok := l.(gopacket.Layer); ok {
			_, err = fmt.Fprintf(&b, "- Layer %d (%02d bytes) = %s\n", i+1, len(gl.LayerContents()), gopacket.LayerString(gl))
		} else {
			_, err = fmt.Fprintf(&b, "- Layer %d <cannot print>\n", i+1)
		}
		errs.CheckE(err)
	}
	return b.String()
}