Leaked source code of windows server 2003
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.

204 lines
4.7 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. #include <limits.h>
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // //
  23. // Public Procedures //
  24. // //
  25. ///////////////////////////////////////////////////////////////////////////////
  26. SNMPAPI
  27. SNMP_FUNC_TYPE
  28. SnmpUtilVarBindListCpy(
  29. SnmpVarBindList * pVblDst,
  30. SnmpVarBindList * pVblSrc
  31. )
  32. /*++
  33. Routine Description:
  34. Copies a variable binding list.
  35. Arguments:
  36. pVblDst - pointer to structure to receive VarBindList.
  37. pVblSrc - pointer to VarBindList to copy.
  38. Return Values:
  39. Returns SNMPAPI_NOERROR if successful.
  40. --*/
  41. {
  42. UINT i;
  43. SNMPAPI nResult = SNMPAPI_ERROR;
  44. // validate pointer
  45. if (pVblDst != NULL) {
  46. // initialize
  47. pVblDst->list = NULL;
  48. pVblDst->len = 0;
  49. // check for varbinds
  50. if ((pVblSrc != NULL) &&
  51. (pVblSrc->list != NULL) &&
  52. (pVblSrc->len != 0)) {
  53. // check for arithmetic overflow
  54. if (pVblSrc->len > UINT_MAX/sizeof(SnmpVarBind)) {
  55. SNMPDBG((
  56. SNMP_LOG_ERROR,
  57. "SNMP: API: pVblSrc->len 0x%x will overflow\n",
  58. pVblSrc->len
  59. ));
  60. SetLastError(SNMP_MEM_ALLOC_ERROR);
  61. return nResult;
  62. }
  63. // attempt to allocate varbinds
  64. pVblDst->list = SnmpUtilMemAlloc(
  65. pVblSrc->len * sizeof(SnmpVarBind)
  66. );
  67. // validate pointer
  68. if (pVblDst->list != NULL) {
  69. // loop through the varbinds
  70. for (i = 0; i < pVblSrc->len; i++) {
  71. // copy individual varbind
  72. nResult = SnmpUtilVarBindCpy(
  73. &pVblDst->list[i],
  74. &pVblSrc->list[i]
  75. );
  76. // validate return code
  77. if (nResult == SNMPAPI_NOERROR) {
  78. // increment
  79. pVblDst->len++; // success...
  80. } else {
  81. break; // failure...
  82. }
  83. }
  84. } else {
  85. SNMPDBG((
  86. SNMP_LOG_ERROR,
  87. "SNMP: API: could not allocate varbinds.\n"
  88. ));
  89. SetLastError(SNMP_MEM_ALLOC_ERROR);
  90. }
  91. } else {
  92. SNMPDBG((
  93. SNMP_LOG_WARNING,
  94. "SNMP: API: copying null varbindlist.\n"
  95. ));
  96. nResult = SNMPAPI_NOERROR; // success..,
  97. }
  98. } else {
  99. SNMPDBG((
  100. SNMP_LOG_ERROR,
  101. "SNMP: API: null varbindlist pointer.\n"
  102. ));
  103. SetLastError(ERROR_INVALID_PARAMETER);
  104. }
  105. // make sure we cleanup
  106. if (nResult == SNMPAPI_ERROR) {
  107. // release new varbinds
  108. SnmpUtilVarBindListFree(pVblDst);
  109. }
  110. return nResult;
  111. }
  112. VOID
  113. SNMP_FUNC_TYPE
  114. SnmpUtilVarBindListFree(
  115. SnmpVarBindList * pVbl
  116. )
  117. /*++
  118. Routine Description:
  119. Frees memory associated with variable binding list.
  120. Arguments:
  121. pVbl - pointer to VarBindList to free.
  122. Return Values:
  123. None.
  124. --*/
  125. {
  126. UINT i;
  127. // loop throught varbinds
  128. for (i = 0; i < pVbl->len; i++) {
  129. // release individual varbind
  130. SnmpUtilVarBindFree(&pVbl->list[i]);
  131. }
  132. // release actual list
  133. SnmpUtilMemFree(pVbl->list);
  134. // re-initialize
  135. pVbl->list = NULL;
  136. pVbl->len = 0;
  137. }