Ejemplo n.º 1
0
// Generate and return the Filter associated with the Builder.
func (b *Builder) Build() *Filter {
	prog := (*C.struct_bpf_program)(b.filter.Program())
	flen := int(C.bpf_get_len(prog))

	for i := 0; i < flen; i++ {
		insn := C.bpf_get_insn(prog, C.int(i))

		if lbl, ok := b.jumps_k[i]; ok {
			addr := b.labels[lbl]
			if addr != 0 {
				insn.k = C.bpf_u_int32(addr - i - 1)
			}
		}

		if lbl, ok := b.jumps_jt[i]; ok {
			addr := b.labels[lbl]
			if addr != 0 {
				insn.jt = C.u_char(addr - i - 1)
			}
		}

		if lbl, ok := b.jumps_jf[i]; ok {
			addr := b.labels[lbl]
			if addr != 0 {
				insn.jf = C.u_char(addr - i - 1)
			}
		}
	}

	return b.filter
}
Ejemplo n.º 2
0
func (f *Filter) String() string {
	var insns []string

	prog := (*C.struct_bpf_program)(f.Program())
	flen := C.bpf_get_len(prog)

	for i := C.int(0); i < flen; i++ {
		insn := C.bpf_get_insn(prog, i)

		str := fmt.Sprintf(
			"{ 0x%.2x, %3d, %3d, 0x%.8x },",
			insn.code, insn.jt, insn.jf, insn.k,
		)

		insns = append(insns, str)
	}

	return strings.Join(insns, "\n")
}