Пример #1
0
func DecodePDUBody2(buffer *C.asn_buf_t, pdu *C.snmp_pdu_t) (SnmpError, bool) {
	var recv_len C.int32_t
	var ignored C.int32_t = 0
	var ret_code uint32

	if C.SNMP_V3 == pdu.version {
		if C.SNMP_SECMODEL_USM != pdu.security_model {
			return Errorf(SNMP_CODE_FAILED, "unsupport security model - %d", int(pdu.security_model)), false
		}

		if ret_code = C.snmp_pdu_decode_secmode(buffer, pdu); C.SNMP_CODE_OK != ret_code {
			return Error(SnmpResult(ret_code), C.GoString(C.snmp_pdu_get_error(pdu, ret_code))), false
		}
	}

	if ret_code = C.snmp_pdu_decode_scoped2(buffer, pdu, &recv_len, &ignored); C.SNMP_CODE_OK != ret_code {
		switch ret_code {
		case C.SNMP_CODE_BADENC:
			if C.SNMP_Verr == pdu.version {
				return Errorf(SNMP_CODE_FAILED, "unsupport security model - %d", int(pdu.security_model)), false
			}
		}

		return Error(SnmpResult(ret_code), C.GoString(C.snmp_pdu_get_error(pdu, ret_code))), false
	}
	if 1 == ignored {
		return nil, true
	}
	return nil, false
}
Пример #2
0
func fill_pdu_for_test_crypt(is_encrypt bool, pt C.enum_snmp_privacy, salt, key, data []byte) ([]byte, error) {
	var digest [100000]byte
	var pdu C.snmp_pdu_t

	C.snmp_pdu_init(&pdu)

	pdu.user.priv_proto = pt

	C.memcpy(unsafe.Pointer(&pdu.user.priv_key[0]), unsafe.Pointer(&key[0]), C.size_t(C.SNMP_PRIV_KEY_SIZ))
	pdu.user.priv_len = C.size_t(16) //C.size_t(C.SNMP_PRIV_KEY_SIZ) //(au == SNMP_PRIV_DES ) ? SNMP_AUTH_HMACMD5_KEY_SIZ : SNMP_AUTH_HMACSHA_KEY_SIZ;

	pdu.engine.engine_boots = 3
	pdu.engine.engine_time = 3
	C.memcpy(unsafe.Pointer(&pdu.msg_salt[0]), unsafe.Pointer(&salt[0]), 8)

	copy(digest[:], data)
	pdu.scoped_ptr = (*C.u_char)(unsafe.Pointer(&digest[0]))
	pdu.scoped_len = C.size_t((len(data) / 8) * 8)
	var ret_code C.enum_snmp_code
	if is_encrypt {
		ret_code = C.snmp_pdu_encrypt(&pdu)
	} else {
		ret_code = C.snmp_pdu_decrypt(&pdu)
	}
	if 0 != ret_code {
		err := errors.New(C.GoString(C.snmp_pdu_get_error(&pdu, ret_code)))
		return nil, err
	}

	return readGoBytes((*C.uint8_t)(pdu.scoped_ptr), C.uint32_t(pdu.scoped_len)), nil
}
Пример #3
0
func DecodePDUHeader(buffer *C.asn_buf_t, pdu *C.snmp_pdu_t) SnmpError {
	C.snmp_pdu_init(pdu)

	ret_code := C.snmp_pdu_decode_header(buffer, pdu)
	if 0 != ret_code {
		return Error(SNMP_CODE_FAILED, "decode pdu header failed -"+C.GoString(C.snmp_pdu_get_error(pdu, ret_code)))
	}
	return nil
}
Пример #4
0
func encodeNativePdu(bs []byte, pdu *C.snmp_pdu_t) ([]byte, SnmpError) {
	if pdu.engine.max_msg_size == 0 {
		pdu.engine.max_msg_size = C.int32_t(*maxPDUSize)
	}

	var buffer C.asn_buf_t
	C.set_asn_u_ptr(&buffer.asn_u, (*C.char)(unsafe.Pointer(&bs[0])))
	buffer.asn_len = C.size_t(len(bs))

	ret_code := C.snmp_pdu_encode(pdu, &buffer)
	if 0 != ret_code {
		err := Error(SnmpResult(ret_code), C.GoString(C.snmp_pdu_get_error(pdu, ret_code)))
		return nil, err
	}
	length := C.get_buffer_length(&buffer, (*C.u_char)(unsafe.Pointer(&bs[0])))
	return bs[0:length], nil
}