예제 #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
}
예제 #2
0
func encodeBindings(internal *C.snmp_pdu_t, vbs *VariableBindings) SnmpError {
	if SNMP_MAX_BINDINGS < vbs.Len() {
		return Errorf(SNMP_CODE_FAILED, "bindings too long, SNMP_MAX_BINDINGS is %d, variableBindings is %d",
			SNMP_MAX_BINDINGS, vbs.Len())
	}

	for i, vb := range vbs.All() {
		err := oidWrite(&internal.bindings[i].oid, &vb.Oid)
		if nil != err {
			internal.nbindings = C.u_int(i) + 1 // free
			return err
		}

		if nil == vb.Value {
			internal.bindings[i].syntax = uint32(SNMP_SYNTAX_NULL)
			continue
		}

		internal.bindings[i].syntax = uint32(vb.Value.GetSyntax())
		switch vb.Value.GetSyntax() {
		case SNMP_SYNTAX_NULL:
		case SNMP_SYNTAX_INTEGER:
			C.snmp_value_put_int32(&internal.bindings[i].v, C.int32_t(vb.Value.GetInt32()))
		case SNMP_SYNTAX_OCTETSTRING:
			send_bytes := vb.Value.GetBytes()
			if nil != send_bytes && 0 != len(send_bytes) {
				C.snmp_value_put_octets(&internal.bindings[i].v, unsafe.Pointer(&send_bytes[0]), C.u_int(len(send_bytes)))
			} else {
				C.snmp_value_put_octets(&internal.bindings[i].v, nil, C.u_int(0))
			}
		case SNMP_SYNTAX_OID:
			err = oidWrite(C.snmp_value_get_oid(&internal.bindings[i].v), vb.Value)
			if nil != err {
				internal.nbindings = C.u_int(i) + 1 // free
				return err
			}
		case SNMP_SYNTAX_IPADDRESS:
			send_bytes := vb.Value.GetBytes()
			if 4 != len(send_bytes) {
				internal.nbindings = C.u_int(i) + 1 // free
				return Errorf(SNMP_CODE_FAILED, "ip address is error, it's length is %d, excepted length is 4, value is %s",
					len(send_bytes), vb.Value.String())
			}
			C.snmp_value_put_ipaddress(&internal.bindings[i].v, C.u_char(send_bytes[0]),
				C.u_char(send_bytes[1]), C.u_char(send_bytes[2]), C.u_char(send_bytes[3]))
		case SNMP_SYNTAX_COUNTER:
			C.snmp_value_put_uint32(&internal.bindings[i].v, C.uint32_t(vb.Value.GetUint32()))
		case SNMP_SYNTAX_GAUGE:
			C.snmp_value_put_uint32(&internal.bindings[i].v, C.uint32_t(vb.Value.GetUint32()))
		case SNMP_SYNTAX_TIMETICKS:
			C.snmp_value_put_uint32(&internal.bindings[i].v, C.uint32_t(vb.Value.GetUint32()))
		case SNMP_SYNTAX_COUNTER64:
			uint64_to_counter64(vb.Value.GetUint64(), &internal.bindings[i].v)
			//cs := C.CString(s)
			//defer C.free(unsafe.Pointer(cs))
			//C.snmp_value_put_uint64_str(&internal.bindings[i].v, cs)
			//C.snmp_value_put_uint64(&internal.bindings[i].v, C.uint64_t(vb.Value.GetUint64()))
		default:
			internal.nbindings = C.u_int(i) + 1 // free
			return Errorf(SNMP_CODE_FAILED, "unsupported type - %v", vb.Value)
		}
	}
	internal.nbindings = C.u_int(vbs.Len())
	return nil
}