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.

241 lines
8.7 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. Protocol.hxx
  5. Abstract:
  6. The Protocol object definitions
  7. Author:
  8. Kamen Moutafov [KamenM]
  9. Revision History:
  10. KamenM 12/22/1998 Creation
  11. --*/
  12. #if _MSC_VER >= 1200
  13. #pragma once
  14. #endif
  15. #ifndef __PROTOCOL_HXX_
  16. #define __PROTOCOL_HXX_
  17. // uncomment this to turn on PNP debug spew
  18. //#define MAJOR_PNP_DEBUG
  19. enum TransportProtocolStates
  20. {
  21. ProtocolNotLoaded,
  22. ProtocolLoadedWithoutAddress,
  23. ProtocolWasLoadedOrNeedsActivation,
  24. ProtocolLoaded,
  25. ProtocolWasLoadedOrNeedsActivationWithoutAddress,
  26. ProtocolLoadedAndMonitored
  27. };
  28. struct BASE_ASYNC_OBJECT; // forwards
  29. extern RPC_ADDRESS_CHANGE_FN * AddressChangeFn;
  30. void RPC_ENTRY NullAddressChangeFn( PVOID arg );
  31. class TransportProtocol
  32. /*
  33. This object encapsulates a protocol on the transport level. Used primarily for PnP support.
  34. */
  35. {
  36. public:
  37. TransportProtocol(void)
  38. {
  39. State = ProtocolNotLoaded;
  40. RpcpInitializeListHead(&ObjectList);
  41. addressChangeSocket = 0;
  42. // provide arbitrary non-zero code, to indicate that this query has not succeeded
  43. addressChangeOverlapped.Internal = (ULONG_PTR)-1;
  44. }
  45. // If a piece of code detects that a protocol is functional through means other
  46. // than PnP, it may call this function to notify the PnP code that the protocol is functional
  47. // The only effect will be speedier processing of a potential PnP notification
  48. void DetectedAsFunctional(PROTOCOL_ID ProtocolId);
  49. // Whenever a PnP notification arrives, this method needs to be called for each protocol.
  50. // The method is idempotent - it saves the previous state and will no-op redundant calls
  51. // However, this method should not be called before the runtime is ready to accept
  52. // calls on transport addresses
  53. void HandleProtocolChange(IN WSAPROTOCOL_INFO *lpProtocolBuffer, IN int ProtocolCount,
  54. IN PROTOCOL_ID thisProtocolId);
  55. // Adds an object to the list of objects for this protocol. The list is used to track down
  56. // all instances of objects belonging to this protocol when a PnP notification arrives.
  57. void AddObjectToList(IN OUT BASE_ASYNC_OBJECT *pObj);
  58. // Removes an object from the list of objects on this protocol
  59. void RemoveObjectFromList(IN OUT BASE_ASYNC_OBJECT *pObj);
  60. // If a NewAddress query fails asynchronously, this function needs to be called which will
  61. // retry submitting all failed query for this protocol. For non-failed, or non-submitted queries,
  62. // this is a no-op
  63. BOOL ResubmitQueriesIfNecessary(PROTOCOL_ID ProtocolId);
  64. // Handle a PnP notification. Does all necessary processing
  65. static BOOL HandlePnPStateChange(void);
  66. // Calls ResubmitQueriesIfNecessary(PROTOCOL_ID ProtocolId) on all protocols.
  67. static BOOL ResubmitQueriesIfNecessary(void);
  68. // Same as the non-static FunctionalProtocolDetected, except that this one operates
  69. // without this pointer - it will retrieve the appropriate object and will call
  70. // FunctionalProtocolDetected on it.
  71. static void FunctionalProtocolDetected(PROTOCOL_ID ProtocolId);
  72. // Same as AddObjectToList except that it will call AddObjectToList on the appropriate
  73. // protocol
  74. static void AddObjectToProtocolList(IN OUT BASE_ASYNC_OBJECT *pObj);
  75. // Same as RemoveObjectFromList except that it will call RemoveObjectFromList on the appropriate
  76. // protocol
  77. static void RemoveObjectFromProtocolList(IN OUT BASE_ASYNC_OBJECT *pObj);
  78. #if defined(DBG) || defined(_DEBUG)
  79. // will ASSERT that all protocols are in a consistent state
  80. static void AssertTransportProtocolState(void);
  81. // will ASSSERT that this protocol is in a consistent state
  82. void AssertState(PROTOCOL_ID ProtocolId);
  83. #endif
  84. private:
  85. // attempts to verify that a protocol is functional, and if found so, advances the state of
  86. // the protocol
  87. BOOL VerifyProtocolIsFunctional(PROTOCOL_ID ProtocolId);
  88. // checks whether the given protocol has an address
  89. BOOL DoesAddressSocketHaveAddress(void);
  90. // If there is a pending address change request (WSAIoctl), it cancels it
  91. // if fForceCancel is FALSE, monitored protocols don't have their
  92. // address change requests canceled. If fForceCancel is TRUE,
  93. // all protocols get their address change requests cancelled
  94. void CancelAddressChangeRequestIfNecessary(IN BOOL fForceCancel, IN PROTOCOL_ID ProtocolId);
  95. // Restarts a protocol that was unloaded. Currently not used.
  96. void RestartProtocol(PROTOCOL_ID ProtocolId);
  97. // Unloads a protocol that was loaded.
  98. void UnloadProtocol(PROTOCOL_ID ProtocolId);
  99. // Submits an address change query (WSAIoctl)
  100. BOOL SubmitAddressChangeQuery(void);
  101. // Sets the state of the given protocol. Other code should never directly set the State - it should
  102. // always go through this function. This allows for derivative protocols (like HTTP) to mirror
  103. // the state of their base protocols - TCP
  104. void SetState(TransportProtocolStates newState, PROTOCOL_ID ProtocolId);
  105. // will return whether address change monitoring is required for this protocol. Currently this
  106. // is used only by RPCSS for SPX (in order to start sending updated SAP broadcasts)
  107. BOOL IsAddressChangeMonitoringOn(PROTOCOL_ID ProtocolId)
  108. {
  109. // if this is TCP & SPX, and somebody actually cares about this (i.e. somebody has
  110. // registered a notification callback), then turn on address monitoring
  111. return (
  112. (
  113. #ifdef SPX_ON
  114. (ProtocolId == SPX)
  115. ||
  116. #endif
  117. (ProtocolId == TCP)
  118. )
  119. &&
  120. (AddressChangeFn != NullAddressChangeFn)
  121. &&
  122. (AddressChangeFn != NULL)
  123. );
  124. }
  125. // Trailing protocols are protocols that don't have their own independent transport provider
  126. // They use the transport provider of another protocol, but for some reason we present it
  127. // to the upper layers as different protocols. We call them trailing, because their
  128. // state trails the state of the protocol whose transport they use.
  129. // Currently we have only one of those - HTTP which trails TCP
  130. BOOL IsTrailingProtocol(PROTOCOL_ID ProtocolId)
  131. {
  132. return (ProtocolId == HTTP);
  133. }
  134. void SetStateToLoadedAndMonitorProtocolIfNecessary(PROTOCOL_ID ProtocolId);
  135. // checks if there is an address change request pending, and if there isn't
  136. // submits one. Will also open an address change socket if necessary
  137. BOOL MonitorFunctionalProtocolForAddressChange(PROTOCOL_ID ProtocolId);
  138. // checks if these's an address change request socket, and if there
  139. // isn't it opens one
  140. BOOL OpenAddressChangeRequestSocket(PROTOCOL_ID ProtocolId);
  141. // if ThisProtocolsId is in ProtocolLoadedAndMonitored or ProtocolLoaded state
  142. // move the Mirrored protocol to the corresponding state
  143. void
  144. MirrorProtocolState (
  145. IN PROTOCOL_ID MirrorProtocolId
  146. );
  147. #ifdef MAJOR_PNP_DEBUG
  148. // Dumps the protocol state on the debugger for the current protocol
  149. void DumpProtocolState(PROTOCOL_ID ProtocolId);
  150. // Dumps the protocol state on the debugger for all protocols
  151. static void DumpProtocolState(void);
  152. #endif
  153. // the current state of this protocol
  154. TransportProtocolStates State;
  155. // the list of opened objects for this protocol
  156. LIST_ENTRY ObjectList;
  157. // iff there's address change request submitted on this socket, it will be non-null
  158. SOCKET addressChangeSocket;
  159. // the OVERLAPPED for the address change request (WSAIoctl). It's Internal member will be -1
  160. // if no request has been submitted. Other data members are as normal.
  161. OVERLAPPED addressChangeOverlapped;
  162. // NOTE: A TransportProtocol object is not aware of its PROTOCOL_ID - it must be
  163. // passed externally to it for all methods that require it. This is done to save memory.
  164. };
  165. extern TransportProtocol *TransportProtocolArray;
  166. // returns the TransportProtocol object for the ProtocolId. Ownership of the object does not pass
  167. // to the caller
  168. inline TransportProtocol *GetTransportProtocol(IN PROTOCOL_ID ProtocolId)
  169. {
  170. ASSERT(ProtocolId >= 1);
  171. ASSERT(ProtocolId < MAX_PROTOCOLS);
  172. return &TransportProtocolArray[ProtocolId];
  173. }
  174. inline void TransportProtocol::FunctionalProtocolDetected(PROTOCOL_ID ProtocolId)
  175. {
  176. GetTransportProtocol(ProtocolId)->DetectedAsFunctional(ProtocolId);
  177. }
  178. RPC_STATUS InitTransportProtocols(void);
  179. #if defined(DBG) || defined(_DEBUG)
  180. #define ASSERT_ALL_TRANSPORT_PROTOCOL_STATE() TransportProtocol::AssertTransportProtocolState()
  181. #define ASSERT_TRANSPORT_PROTOCOL_STATE(p) AssertState(p)
  182. #else
  183. #define ASSERT_ALL_TRANSPORT_PROTOCOL_STATE()
  184. #define ASSERT_TRANSPORT_PROTOCOL_STATE(p)
  185. #endif
  186. #endif