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 }
func debug_init_secparams(pdu *C.snmp_pdu_t) { if pdu.user.auth_proto != C.SNMP_AUTH_NOAUTH { pdu.flags |= C.SNMP_MSG_AUTH_FLAG } switch pdu.user.priv_proto { case C.SNMP_PRIV_DES: memcpy(&pdu.msg_salt[0], 8, debug_salt) pdu.flags |= C.SNMP_MSG_PRIV_FLAG case C.SNMP_PRIV_AES: memcpy(&pdu.msg_salt[0], 8, debug_salt) pdu.flags |= C.SNMP_MSG_PRIV_FLAG } }
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 }