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.

232 lines
5.9 KiB

  1. #include "precomp.h"
  2. DEBUG_FILEZONE(ZONE_T120_MCSNC | ZONE_T120_GCCNC);
  3. /*
  4. * packet.cpp
  5. *
  6. * Copyright (c) 1993 - 1995 by DataBeam Corporation, Lexington, KY
  7. *
  8. * Abstract:
  9. * This is the implementation file for the MCS packet class. The packet
  10. * class is responsible for encoding and decoding the PDUs, as well as
  11. * maintaining the necessary pointers to the encoded and decoded data.
  12. * Instances of this class will be created both by User and Connection
  13. * objects as PDUs flow through MCS.
  14. *
  15. * Private Instance Variables:
  16. * Packet_Coder
  17. * A pointer to the packet coder object.
  18. * Encoded_Lock_Count
  19. * A counter indicating the number of locks currently existing on the
  20. * encoded data.
  21. * Decoded_Lock_Count
  22. * A counter indicating the number of locks currently existing on the
  23. * decoded data.
  24. * Free_State
  25. * A boolean value indicating whether the object can be freed when all
  26. * lock counts fall to zero.
  27. * m_EncodedPDU
  28. * This is a pointer to the encoded PDU contained in the internal
  29. * buffer. Note that the reason for keeping this separate is that
  30. * the encoded PDU may not start at the beginning of the encoded data
  31. * memory block identified above. Some encoders actually encode the
  32. * PDUs backward, or back justified.
  33. * Encoded_Data_Length
  34. * Indicates the length of the encoded data. If zero, the packet coder
  35. * must be consulted to obtain the length which is subsequently saved.
  36. * Decoded_Data_Length
  37. * Indicates the length of the decoded data. If zero, the packet coder
  38. * must be consulted to obtain the length which is subsequently saved.
  39. * PDU_Type
  40. * Indicates the type of PDU contained in the packet. Valid values
  41. * are DOMAIN_MCS_PDU or CONNECT_MCS_PDU.
  42. * Packet_Direction_Up
  43. * A boolean indicating whether the direction of travel for the PDU
  44. * is upward.
  45. *
  46. * Private Member Functions:
  47. * PacketSuicideCheck
  48. * This function is called by Unlock() as well as any Unlock call
  49. * (ie. UnlockUncoded()) when it's associated lock count falls to
  50. * zero.
  51. *
  52. * Caveats:
  53. * None.
  54. *
  55. * Author:
  56. * James J. Johnstone IV
  57. */
  58. /*
  59. * Packet ()
  60. *
  61. * Public
  62. *
  63. * Functional Description:
  64. * This version of the constructor is used to create a Packet object
  65. * for outgoing PDUs when the packet is to be created from a structure
  66. * containing the PDU data to be encoded.
  67. */
  68. // outgoing packets
  69. Packet::Packet(PPacketCoder pPacketCoder,
  70. UINT nEncodingRules,
  71. LPVOID pInputPduStructure,
  72. int nPduType,
  73. BOOL fPacketDirectionUp,
  74. PPacketError pePktErr,
  75. BOOL fLockEncodedData)
  76. :
  77. SimplePacket(fPacketDirectionUp),
  78. Packet_Coder(pPacketCoder),
  79. PDU_Type(nPduType),
  80. m_Decoded_Data (NULL),
  81. Decoded_Data_Length (0)
  82. {
  83. /*
  84. * Encode the PDU using the externally provided decoded data. The encoded
  85. * buffer will be allocated by the encoder. The buffer needs to be freed later.
  86. */
  87. if (Packet_Coder->Encode (pInputPduStructure, PDU_Type, nEncodingRules,
  88. &m_EncodedPDU, &Encoded_Data_Length))
  89. {
  90. ASSERT (m_EncodedPDU);
  91. /*
  92. * Encoding was successful.
  93. */
  94. *pePktErr = PACKET_NO_ERROR;
  95. // should we lock encoded data?
  96. if (fLockEncodedData)
  97. lLock = 2;
  98. }
  99. else
  100. {
  101. /*
  102. * Encoding failed.
  103. */
  104. m_EncodedPDU = NULL;
  105. ERROR_OUT(("Packet::Packet: encoding failed"));
  106. *pePktErr = PACKET_MALLOC_FAILURE;
  107. }
  108. }
  109. /*
  110. * Packet ()
  111. *
  112. * Public
  113. *
  114. * Functional Description:
  115. * This version of the constructor is used to create a Packet object
  116. * for incomming PDUs when the packet is to be created from an encoded
  117. * data stream containing the PDU data to be decoded.
  118. */
  119. // incoming packets
  120. Packet::Packet(PPacketCoder pPacketCoder,
  121. UINT nEncodingRules,
  122. LPBYTE pEncodedData,
  123. UINT cbEncodedDataSize,
  124. int nPduType,
  125. BOOL fPacketDirectionUp,
  126. PPacketError pePktErr)
  127. :
  128. SimplePacket(fPacketDirectionUp),
  129. Packet_Coder(pPacketCoder),
  130. PDU_Type(nPduType)
  131. {
  132. //PacketCoderError coder_error;
  133. m_EncodedPDU = NULL;
  134. /*
  135. * Decode the provided encoded buffer. Note that the decoder will
  136. * allocate the space needed. The buffer needs to be freed later.
  137. */
  138. if (Packet_Coder->Decode (pEncodedData, cbEncodedDataSize, PDU_Type,
  139. nEncodingRules, &m_Decoded_Data,
  140. &Decoded_Data_Length) == FALSE)
  141. {
  142. ERROR_OUT(("Packet::Packet: Decode call failed."));
  143. m_Decoded_Data = NULL;
  144. *pePktErr = PACKET_INCOMPATIBLE_PROTOCOL;
  145. }
  146. else
  147. {
  148. ASSERT (m_Decoded_Data != NULL);
  149. /*
  150. * The decode was successful.
  151. */
  152. *pePktErr = PACKET_NO_ERROR;
  153. }
  154. }
  155. /*
  156. * ~Packet ()
  157. *
  158. * Public
  159. *
  160. * Functional Description:
  161. * Destructor for the Packet class. The destructor ensures that all
  162. * resources that have been allocated are freed.
  163. */
  164. Packet::~Packet(void)
  165. {
  166. /*
  167. * If there is memory allocated for encoded data, then free it.
  168. */
  169. if (m_EncodedPDU != NULL) {
  170. // the encoded memory was allocated by the ASN.1 coder.
  171. Packet_Coder->FreeEncoded (m_EncodedPDU);
  172. }
  173. /*
  174. * If there is memory allocated for decoded data, then free it.
  175. */
  176. if (m_Decoded_Data != NULL) {
  177. // the decoded memory was allocated by the ASN.1 decoder
  178. Packet_Coder->FreeDecoded (PDU_Type, m_Decoded_Data);
  179. }
  180. }
  181. /*
  182. * IsDataPacket ()
  183. *
  184. * Public
  185. *
  186. * Functional Description:
  187. * This function returns whether this is a data packet (it's not).
  188. */
  189. BOOL Packet::IsDataPacket(void)
  190. {
  191. return (FALSE);
  192. }
  193. /*
  194. * GetDecodedData ()
  195. *
  196. * Public
  197. *
  198. * Functional Description:
  199. * The GetDecodedData method returns a pointer to the decoded data
  200. * buffer. If the packet does not have decoded data the Decode method is
  201. * called. If decode is unable to provide decoded data then NULL is
  202. * returned.
  203. */
  204. PVoid Packet::GetDecodedData ()
  205. {
  206. ASSERT (m_Decoded_Data != NULL);
  207. return (m_Decoded_Data);
  208. }
  209. /*
  210. * GetPDUType ()
  211. *
  212. * Public
  213. *
  214. * Functional Description:
  215. * The GetPDUType method returns the PDU type for the packet.
  216. */
  217. int Packet::GetPDUType ()
  218. {
  219. return (PDU_Type);
  220. }