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.

222 lines
5.2 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. /*---------------------------------------------------------
  13. Filename: pdu.cpp
  14. Written By: B.Rajeev
  15. ----------------------------------------------------------*/
  16. #include "precomp.h"
  17. #include "common.h"
  18. #include "encdec.h"
  19. #include "vblist.h"
  20. #include "sec.h"
  21. #include "address.h"
  22. #include "pdu.h"
  23. SnmpPdu::SnmpPdu():m_SourceAddress (NULL),m_DestinationAddress(NULL),m_SnmpCommunityName (NULL),length(0),ptr(NULL),m_SnmpVarBindList(NULL)
  24. {
  25. is_valid = FALSE;
  26. }
  27. SnmpPdu::SnmpPdu(IN SnmpPdu &snmpPdu):m_SourceAddress (NULL),m_DestinationAddress(NULL),m_SnmpCommunityName (NULL),m_SnmpVarBindList(NULL)
  28. {
  29. is_valid = FALSE;
  30. Initialize(snmpPdu.GetFrame(), snmpPdu.GetFrameLength());
  31. m_RequestId = snmpPdu.m_RequestId ;
  32. m_PduType = snmpPdu.m_PduType ;
  33. m_ErrorReport = snmpPdu.m_ErrorReport ;
  34. m_SnmpVarBindList = new SnmpVarBindList ( *snmpPdu.m_SnmpVarBindList ) ;
  35. m_SourceAddress = snmpPdu.m_SourceAddress ? snmpPdu.m_SourceAddress->Copy () : NULL ;
  36. m_DestinationAddress = snmpPdu.m_DestinationAddress ? snmpPdu.m_DestinationAddress->Copy () : NULL ;
  37. m_SnmpCommunityName = snmpPdu.m_SnmpCommunityName ? ( SnmpCommunityBasedSecurity * ) snmpPdu.m_SnmpCommunityName->Copy () : NULL ;
  38. is_valid = TRUE ;
  39. }
  40. SnmpPdu::SnmpPdu(IN const UCHAR *frame, IN const ULONG &frameLength) : m_SourceAddress (NULL),m_DestinationAddress(NULL),m_SnmpCommunityName (NULL),m_SnmpVarBindList(NULL)
  41. {
  42. Initialize(frame, frameLength);
  43. }
  44. SnmpPdu::~SnmpPdu(void)
  45. {
  46. FreeFrame();
  47. FreePdu () ;
  48. }
  49. void SnmpPdu::FreeFrame(void)
  50. {
  51. if ( is_valid )
  52. {
  53. delete[] ptr;
  54. }
  55. }
  56. void SnmpPdu::FreePdu ()
  57. {
  58. delete m_SnmpCommunityName ;
  59. delete m_DestinationAddress ;
  60. delete m_SourceAddress ;
  61. }
  62. void SnmpPdu::Initialize(IN const UCHAR *frame, IN const ULONG &frameLength)
  63. {
  64. if ( frame )
  65. {
  66. length = frameLength;
  67. ptr = new UCHAR[frameLength];
  68. memcpy(ptr, frame, length);
  69. }
  70. else
  71. {
  72. length = 0 ;
  73. ptr = NULL ;
  74. }
  75. is_valid = TRUE;
  76. }
  77. ULONG SnmpPdu::GetFrameLength() const
  78. {
  79. return ( (is_valid)?length:0 );
  80. }
  81. UCHAR *SnmpPdu::GetFrame() const
  82. {
  83. return ( (is_valid)?ptr:NULL );
  84. }
  85. void SnmpPdu::SetPdu ( IN SnmpPdu &a_SnmpPdu )
  86. {
  87. FreeFrame();
  88. FreePdu () ;
  89. Initialize(a_SnmpPdu.GetFrame(), a_SnmpPdu.GetFrameLength());
  90. m_RequestId = a_SnmpPdu.m_RequestId ;
  91. m_PduType = a_SnmpPdu.m_PduType ;
  92. m_ErrorReport = a_SnmpPdu.m_ErrorReport ;
  93. m_SnmpVarBindList = new SnmpVarBindList ( *a_SnmpPdu.m_SnmpVarBindList ) ;
  94. m_SourceAddress = a_SnmpPdu.m_SourceAddress ? a_SnmpPdu.m_SourceAddress->Copy () : NULL ;
  95. m_DestinationAddress = a_SnmpPdu.m_DestinationAddress ? a_SnmpPdu.m_DestinationAddress->Copy () : NULL ;
  96. m_SnmpCommunityName = a_SnmpPdu.m_SnmpCommunityName ? ( SnmpCommunityBasedSecurity * ) a_SnmpPdu.m_SnmpCommunityName->Copy () : NULL ;
  97. }
  98. void SnmpPdu::SetPdu(IN const UCHAR *frame, IN const ULONG frameLength)
  99. {
  100. FreeFrame();
  101. Initialize(frame, frameLength);
  102. }
  103. BOOL SnmpPdu :: SetRequestId ( IN RequestId a_RequestId )
  104. {
  105. m_RequestId = a_RequestId ;
  106. return TRUE ;
  107. }
  108. BOOL SnmpPdu :: SetVarBindList ( OUT SnmpVarBindList &a_SnmpVarBindList )
  109. {
  110. if ( m_SnmpVarBindList )
  111. delete m_SnmpVarBindList ;
  112. m_SnmpVarBindList = &a_SnmpVarBindList ;
  113. return TRUE ;
  114. }
  115. BOOL SnmpPdu :: SetCommunityName ( IN SnmpCommunityBasedSecurity &a_SnmpCommunityBasedSecurity )
  116. {
  117. delete m_SnmpCommunityName ;
  118. m_SnmpCommunityName = &a_SnmpCommunityBasedSecurity ;
  119. return TRUE ;
  120. }
  121. BOOL SnmpPdu :: SetErrorReport ( OUT SnmpErrorReport &a_SnmpErrorReport )
  122. {
  123. m_ErrorReport = a_SnmpErrorReport ;
  124. return TRUE ;
  125. }
  126. BOOL SnmpPdu :: SetPduType ( OUT SnmpEncodeDecode :: PduType a_PduType )
  127. {
  128. m_PduType = a_PduType ;
  129. return TRUE ;
  130. }
  131. BOOL SnmpPdu :: SetSourceAddress ( IN SnmpTransportAddress &a_TransportAddress )
  132. {
  133. delete m_SourceAddress ;
  134. m_SourceAddress = &a_TransportAddress ;
  135. return TRUE ;
  136. }
  137. BOOL SnmpPdu :: SetDestinationAddress ( IN SnmpTransportAddress &a_TransportAddress )
  138. {
  139. delete m_DestinationAddress ;
  140. m_DestinationAddress = &a_TransportAddress ;
  141. return TRUE ;
  142. }
  143. SnmpTransportAddress &SnmpPdu :: GetSourceAddress ()
  144. {
  145. return *m_SourceAddress ;
  146. }
  147. SnmpTransportAddress &SnmpPdu :: GetDestinationAddress ()
  148. {
  149. return *m_DestinationAddress ;
  150. }
  151. SnmpEncodeDecode :: PduType &SnmpPdu :: GetPduType ()
  152. {
  153. return m_PduType ;
  154. }
  155. RequestId &SnmpPdu :: GetRequestId ()
  156. {
  157. return m_RequestId ;
  158. }
  159. SnmpErrorReport &SnmpPdu :: GetErrorReport ()
  160. {
  161. return m_ErrorReport ;
  162. }
  163. SnmpVarBindList &SnmpPdu :: GetVarbindList ()
  164. {
  165. return *m_SnmpVarBindList ;
  166. }
  167. SnmpCommunityBasedSecurity &SnmpPdu :: GetCommunityName ()
  168. {
  169. return *m_SnmpCommunityName ;
  170. }