Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1992-1997 Microsoft Corporation
  3. Module Name:
  4. vbl.c
  5. Abstract:
  6. Contains routines to manipulate variable binding lists.
  7. SnmpUtilVarBindListCpy
  8. SnmpUtilVarBindListFree
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // //
  15. // Include files //
  16. // //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include <snmp.h>
  19. #include <snmputil.h>
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // //
  22. // Public Procedures //
  23. // //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. SNMPAPI
  26. SNMP_FUNC_TYPE
  27. SnmpUtilVarBindListCpy(
  28. SnmpVarBindList * pVblDst,
  29. SnmpVarBindList * pVblSrc
  30. )
  31. /*++
  32. Routine Description:
  33. Copies a variable binding list.
  34. Arguments:
  35. pVblDst - pointer to structure to receive VarBindList.
  36. pVblSrc - pointer to VarBindList to copy.
  37. Return Values:
  38. Returns SNMPAPI_NOERROR if successful.
  39. --*/
  40. {
  41. UINT i;
  42. SNMPAPI nResult = SNMPAPI_ERROR;
  43. // validate pointer
  44. if (pVblDst != NULL) {
  45. // initialize
  46. pVblDst->list = NULL;
  47. pVblDst->len = 0;
  48. // check for varbinds
  49. if ((pVblSrc != NULL) &&
  50. (pVblSrc->list != NULL) &&
  51. (pVblSrc->len != 0)) {
  52. // attempt to allocate varbinds
  53. pVblDst->list = SnmpUtilMemAlloc(
  54. pVblSrc->len * sizeof(SnmpVarBind)
  55. );
  56. // validate pointer
  57. if (pVblDst->list != NULL) {
  58. // loop through the varbinds
  59. for (i = 0; i < pVblSrc->len; i++) {
  60. // copy individual varbind
  61. nResult = SnmpUtilVarBindCpy(
  62. &pVblDst->list[i],
  63. &pVblSrc->list[i]
  64. );
  65. // validate return code
  66. if (nResult == SNMPAPI_NOERROR) {
  67. // increment
  68. pVblDst->len++; // success...
  69. } else {
  70. break; // failure...
  71. }
  72. }
  73. } else {
  74. SNMPDBG((
  75. SNMP_LOG_ERROR,
  76. "SNMP: API: could not allocate varbinds.\n"
  77. ));
  78. SetLastError(SNMP_MEM_ALLOC_ERROR);
  79. }
  80. } else {
  81. SNMPDBG((
  82. SNMP_LOG_WARNING,
  83. "SNMP: API: copying null varbindlist.\n"
  84. ));
  85. nResult = SNMPAPI_NOERROR; // success..,
  86. }
  87. } else {
  88. SNMPDBG((
  89. SNMP_LOG_ERROR,
  90. "SNMP: API: null varbindlist pointer.\n"
  91. ));
  92. SetLastError(ERROR_INVALID_PARAMETER);
  93. }
  94. // make sure we cleanup
  95. if (nResult == SNMPAPI_ERROR) {
  96. // release new varbinds
  97. SnmpUtilVarBindListFree(pVblDst);
  98. }
  99. return nResult;
  100. }
  101. VOID
  102. SNMP_FUNC_TYPE
  103. SnmpUtilVarBindListFree(
  104. SnmpVarBindList * pVbl
  105. )
  106. /*++
  107. Routine Description:
  108. Frees memory associated with variable binding list.
  109. Arguments:
  110. pVbl - pointer to VarBindList to free.
  111. Return Values:
  112. None.
  113. --*/
  114. {
  115. UINT i;
  116. // loop throught varbinds
  117. for (i = 0; i < pVbl->len; i++) {
  118. // release individual varbind
  119. SnmpUtilVarBindFree(&pVbl->list[i]);
  120. }
  121. // release actual list
  122. SnmpUtilMemFree(pVbl->list);
  123. // re-initialize
  124. pVbl->list = NULL;
  125. pVbl->len = 0;
  126. }