Beispiel #1
0
func (pd *PcapDumper) Dump(pkt *Packet) {
	var pkthdr C.struct_pcap_pkthdr
	pkthdr.ts.tv_sec = (C.__time_t)(pkt.Time.Sec)
	pkthdr.ts.tv_usec = (C.__suseconds_t)(pkt.Time.Usec)
	pkthdr.caplen = (C.bpf_u_int32)(pkt.Caplen)
	pkthdr.len = (C.bpf_u_int32)(pkt.Len)

	buf := (*C.char)(C.malloc((C.size_t)(len(pkt.Data))))

	for i := 0; i < len(pkt.Data); i++ {
		*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(buf)) + uintptr(i))) = pkt.Data[i]
	}

	C.pcap_dump((*C.u_char)(unsafe.Pointer(pd.cptr)), &pkthdr, (*C.u_char)(unsafe.Pointer(buf)))
}
Beispiel #2
0
// Writes a packet to the file. The return values of ReadPacketData
// can be passed to this function as arguments.
func (d *Dumper) WritePacketData(data []byte, ci gopacket.CaptureInfo) (err error) {
	var pkthdr _Ctype_struct_pcap_pkthdr
	pkthdr.caplen = C.bpf_u_int32(ci.CaptureLength)
	pkthdr.len = C.bpf_u_int32(ci.Length)

	pkthdr.ts.tv_sec = C.gopacket_time_secs_t(ci.Timestamp.Unix())
	pkthdr.ts.tv_usec = C.gopacket_time_usecs_t(ci.Timestamp.Nanosecond() / 1000)

	// pcap_dump takes a u_char pointer to the dumper as first argument
	dumper_ptr := (*C.u_char)(unsafe.Pointer(d.cptr))

	// trick to get a pointer to the underling slice
	ptr := (*C.u_char)(unsafe.Pointer(&data[0]))

	_, err = C.pcap_dump(dumper_ptr, &pkthdr, ptr)
	return
}
Beispiel #3
0
func (d *Dumper) Dump(packet *Packet) {
	C.pcap_dump((*C.u_char)(unsafe.Pointer(d.dumper)), packet.header,
		packet.data)
}