Ejemplo n.º 1
0
// Compile the given tcpdump-like expression to a BPF filter.
func Compile(filter string, link_type packet.Type, optimize bool) (*Filter, error) {
	var do_optimize int

	if optimize {
		do_optimize = 1
	} else {
		do_optimize = 0
	}

	f := &Filter{}

	filter_str := C.CString(filter)
	defer C.free(unsafe.Pointer(filter_str))

	pcap_type := link_type.ToLinkType()

	err := C.pcap_compile_nopcap(
		C.int(0x7fff), C.int(pcap_type),
		(*C.struct_bpf_program)(f.Program()),
		filter_str, C.int(do_optimize), 0xffffffff,
	)
	if err < 0 {
		return nil, fmt.Errorf("Could not compile filter")
	}

	return f, nil
}
Ejemplo n.º 2
0
// Compile the given tcpdump-like expression to a BPF filter.
func Compile(filter string, link_type packet.Type) (*Filter, error) {
	f := &Filter{}

	fil_str := C.CString(filter)
	defer C.free(unsafe.Pointer(fil_str))

	err_str := (*C.char)(C.calloc(256, 1))
	defer C.free(unsafe.Pointer(err_str))

	pcap_type := link_type.ToLinkType()

	err := C.pcap_compile_nopcap(
		C.int(0x7fff), C.int(pcap_type),
		(*C.struct_bpf_program)(f.Program()),
		fil_str, 0, 0xffffffff,
	)
	if err < 0 {
		return nil, fmt.Errorf("Could not compile filter")
	}

	return f, nil
}