Ejemplo n.º 1
0
func decodeBindings(internal *C.snmp_pdu_t, vbs *VariableBindings) {
	for i := 0; i < int(internal.nbindings); i++ {
		oid := *oidRead(&internal.bindings[i].oid)

		switch SnmpSyntax(internal.bindings[i].syntax) {
		case SNMP_SYNTAX_NULL:
			vbs.AppendWith(oid, NewSnmpNil())
		case SNMP_SYNTAX_INTEGER:
			vbs.AppendWith(oid, NewSnmpInt32(int32(C.snmp_value_get_int32(&internal.bindings[i].v))))
		case SNMP_SYNTAX_OCTETSTRING:
			l := int(C.snmp_value_get_octets_len(&internal.bindings[i].v))
			send_bytes := make([]byte, l, l+10)
			if 0 != l {
				C.snmp_value_get_octets(&internal.bindings[i].v, unsafe.Pointer(&send_bytes[0]))
			}
			vbs.AppendWith(oid, NewSnmpOctetString(send_bytes))
		case SNMP_SYNTAX_OID:
			v := oidRead(C.snmp_value_get_oid(&internal.bindings[i].v))
			vbs.AppendWith(oid, v)
		case SNMP_SYNTAX_IPADDRESS:
			send_bytes := make([]byte, 4)
			tmp := C.snmp_value_get_ipaddress(&internal.bindings[i].v)
			C.memcpy(unsafe.Pointer(&send_bytes[0]), unsafe.Pointer(tmp), 4)
			vbs.AppendWith(oid, NewSnmpAddress(send_bytes))
		case SNMP_SYNTAX_COUNTER:
			vbs.AppendWith(oid, NewSnmpCounter32(uint32(C.snmp_value_get_uint32(&internal.bindings[i].v))))
		case SNMP_SYNTAX_GAUGE:
			vbs.AppendWith(oid, NewSnmpUint32(uint32(C.snmp_value_get_uint32(&internal.bindings[i].v))))
		case SNMP_SYNTAX_TIMETICKS:
			vbs.AppendWith(oid, NewSnmpTimeticks(uint32(C.snmp_value_get_uint32(&internal.bindings[i].v))))
		case SNMP_SYNTAX_COUNTER64:
			u64, err := counter64_to_uint64(&internal.bindings[i].v)
			if nil != err {
				panic("read uint64 failed, " + err.Error())
			}
			vbs.AppendWith(oid, NewSnmpCounter64(u64))
			//vbs.AppendWith(oid, NewSnmpCounter64(uint64(C.snmp_value_get_uint64(&internal.bindings[i].v))))
		default:
			vbs.AppendWith(oid, NewSnmpValueError(uint(internal.bindings[i].syntax)))
		}
	}
}
Ejemplo n.º 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
}