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.

312 lines
9.7 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2001-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: socketdata.cpp
  6. * Content: Socket list that can be shared between DPNWSOCK service provider interfaces.
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 10/25/2001 vanceo Extracted from spdata.cpp
  13. ***************************************************************************/
  14. #include "dnwsocki.h"
  15. //**********************************************************************
  16. // Constant definitions
  17. //**********************************************************************
  18. //**********************************************************************
  19. // Macro definitions
  20. //**********************************************************************
  21. //**********************************************************************
  22. // Structure definitions
  23. //**********************************************************************
  24. //**********************************************************************
  25. // Variable definitions
  26. //**********************************************************************
  27. //**********************************************************************
  28. // Function prototypes
  29. //**********************************************************************
  30. //**********************************************************************
  31. // Function definitions
  32. //**********************************************************************
  33. //**********************************************************************
  34. // ------------------------------
  35. // CSocketData::PoolAllocFunction - function called when item is created in pool
  36. //
  37. // Entry: Pointer to item
  38. // Pointer to context
  39. //
  40. // Exit: Boolean indicating success
  41. // TRUE = success
  42. // FALSE = failure
  43. // ------------------------------
  44. #undef DPF_MODNAME
  45. #define DPF_MODNAME "CSocketData::PoolAllocFunction"
  46. BOOL CSocketData::PoolAllocFunction( void* pvItem, void* pvContext )
  47. {
  48. BOOL fCritSecInitted = FALSE;
  49. CSocketData* pSocketData = (CSocketData*)pvItem;
  50. pSocketData->m_Sig[0] = 'S';
  51. pSocketData->m_Sig[1] = 'O';
  52. pSocketData->m_Sig[2] = 'D';
  53. pSocketData->m_Sig[3] = 'T';
  54. pSocketData->m_lRefCount = 0;
  55. #ifdef DPNBUILD_ONLYONEADAPTER
  56. pSocketData->m_blSocketPorts.Initialize();
  57. #else // ! DPNBUILD_ONLYONEADAPTER
  58. pSocketData->m_blAdapters.Initialize();
  59. #endif // ! DPNBUILD_ONLYONEADAPTER
  60. //
  61. // No socket ports yet.
  62. //
  63. pSocketData->m_lSocketPortRefCount = 0;
  64. pSocketData->m_pThreadPool = NULL;
  65. //
  66. // attempt to initialize the internal critical section
  67. //
  68. if (! DNInitializeCriticalSection(&pSocketData->m_csLock))
  69. {
  70. DPFX(DPFPREP, 0, "Problem initializing critical section for this endpoint!");
  71. goto Failure;
  72. }
  73. DebugSetCriticalSectionRecursionCount(&pSocketData->m_csLock, 0);
  74. DebugSetCriticalSectionGroup( &pSocketData->m_csLock, &g_blDPNWSockCritSecsHeld ); // separate dpnwsock CSes from the rest of DPlay's CSes
  75. fCritSecInitted = TRUE;
  76. //
  77. // Create a manual reset event that is initially set.
  78. //
  79. pSocketData->m_hSocketPortShutdownEvent = DNCreateEvent(NULL, TRUE, TRUE, NULL);
  80. if (pSocketData->m_hSocketPortShutdownEvent == NULL)
  81. {
  82. #ifdef DBG
  83. DWORD dwError;
  84. dwError = GetLastError();
  85. DPFX(DPFPREP, 0, "Couldn't create socket port shutdown event (err = %u)!", dwError);
  86. #endif // DBG
  87. goto Failure;
  88. }
  89. return TRUE;
  90. Failure:
  91. if (pSocketData->m_hSocketPortShutdownEvent != NULL)
  92. {
  93. DNCloseHandle(pSocketData->m_hSocketPortShutdownEvent);
  94. pSocketData->m_hSocketPortShutdownEvent = NULL;
  95. }
  96. if (fCritSecInitted)
  97. {
  98. DNDeleteCriticalSection(&pSocketData->m_csLock);
  99. fCritSecInitted = FALSE;
  100. }
  101. return FALSE;
  102. }
  103. //**********************************************************************
  104. //**********************************************************************
  105. // ------------------------------
  106. // CSocketData::PoolInitFunction - function called when item is removed from pool
  107. //
  108. // Entry: Pointer to item
  109. // Pointer to context
  110. //
  111. // Exit: Nothing
  112. // ------------------------------
  113. #undef DPF_MODNAME
  114. #define DPF_MODNAME "CSocketData::PoolInitFunction"
  115. void CSocketData::PoolInitFunction( void* pvItem, void* pvContext )
  116. {
  117. CSocketData * pSocketData = (CSocketData*) pvItem;
  118. DPFX(DPFPREP, 8, "This = 0x%p, context = 0x%p", pvItem, pvContext);
  119. DNASSERT(pSocketData->m_lRefCount == 0);
  120. #ifdef DPNBUILD_ONLYONEADAPTER
  121. DNASSERT(pSocketData->m_blSocketPorts.IsEmpty());
  122. #else // ! DPNBUILD_ONLYONEADAPTER
  123. DNASSERT(pSocketData->m_blAdapters.IsEmpty());
  124. #endif // ! DPNBUILD_ONLYONEADAPTER
  125. pSocketData->m_lRefCount = 1; // the person retrieving from the pool will have a reference
  126. pSocketData->m_pThreadPool = (CThreadPool*) pvContext;
  127. }
  128. //**********************************************************************
  129. //**********************************************************************
  130. // ------------------------------
  131. // CSocketData::PoolReleaseFunction - function called when item is returning
  132. // to the pool
  133. //
  134. // Entry: Pointer to item
  135. //
  136. // Exit: Nothing
  137. // ------------------------------
  138. #undef DPF_MODNAME
  139. #define DPF_MODNAME "CSocketData::PoolReleaseFunction"
  140. void CSocketData::PoolReleaseFunction( void* pvItem )
  141. {
  142. CSocketData * pSocketData = (CSocketData*) pvItem;
  143. DPFX(DPFPREP, 8, "This = 0x%p", pvItem);
  144. DNASSERT(pSocketData->m_lRefCount == 0);
  145. #ifdef DPNBUILD_ONLYONEADAPTER
  146. DNASSERT(pSocketData->m_blSocketPorts.IsEmpty());
  147. #else // ! DPNBUILD_ONLYONEADAPTER
  148. DNASSERT(pSocketData->m_blAdapters.IsEmpty());
  149. #endif // ! DPNBUILD_ONLYONEADAPTER
  150. DNASSERT(pSocketData->m_lSocketPortRefCount == 0);
  151. pSocketData->m_pThreadPool = NULL;
  152. }
  153. //**********************************************************************
  154. //**********************************************************************
  155. // ------------------------------
  156. // CSocketData::PoolDeallocFunction - function called when item is deallocated
  157. // from the pool
  158. //
  159. // Entry: Pointer to item
  160. //
  161. // Exit: Nothing
  162. // ------------------------------
  163. #undef DPF_MODNAME
  164. #define DPF_MODNAME "CSocketData::PoolDeallocFunction"
  165. void CSocketData::PoolDeallocFunction( void* pvItem )
  166. {
  167. CSocketData * pSocketData = (CSocketData*) pvItem;
  168. DPFX(DPFPREP, 8, "This = 0x%p", pvItem);
  169. DNASSERT(pSocketData->m_lRefCount == 0);
  170. #ifdef DPNBUILD_ONLYONEADAPTER
  171. DNASSERT(pSocketData->m_blSocketPorts.IsEmpty());
  172. #else // ! DPNBUILD_ONLYONEADAPTER
  173. DNASSERT(pSocketData->m_blAdapters.IsEmpty());
  174. #endif // ! DPNBUILD_ONLYONEADAPTER
  175. DNCloseHandle(pSocketData->m_hSocketPortShutdownEvent);
  176. pSocketData->m_hSocketPortShutdownEvent = NULL;
  177. DNDeleteCriticalSection(&pSocketData->m_csLock);
  178. DNASSERT(pSocketData->m_pThreadPool == NULL);
  179. }
  180. //**********************************************************************
  181. //**********************************************************************
  182. // ------------------------------
  183. // CSocketData::FindSocketPort - looks up the socket port with the given address.
  184. // The socketdata lock must be held.
  185. //
  186. // Entry: Pointer to socketport address, place to store socketport pointer
  187. //
  188. // Exit: TRUE if socketport found, FALSE if not
  189. // ------------------------------
  190. #undef DPF_MODNAME
  191. #define DPF_MODNAME "CSocketData::FindSocketPort"
  192. BOOL CSocketData::FindSocketPort(const CSocketAddress * const pSocketAddress, CSocketPort ** const ppSocketPort )
  193. {
  194. CBilink * pBilinkSocketPorts;
  195. CSocketPort * pTempSocketPort;
  196. #ifndef DPNBUILD_ONLYONEADAPTER
  197. CBilink * pBilinkAdapters;
  198. CAdapterEntry* pTempAdapterEntry;
  199. #endif // ! DPNBUILD_ONLYONEADAPTER
  200. AssertCriticalSectionIsTakenByThisThread(&m_csLock, TRUE);
  201. #ifdef DPNBUILD_ONLYONEADAPTER
  202. //
  203. // Loop through all socket ports.
  204. //
  205. pBilinkSocketPorts = m_blSocketPorts.GetNext();
  206. while ( pBilinkSocketPorts != &m_blSocketPorts )
  207. {
  208. pTempSocketPort = CSocketPort::SocketPortFromBilink( pBilinkSocketPorts );
  209. if ( CSocketAddress::CompareFunction( (PVOID) pSocketAddress, (PVOID) pTempSocketPort->GetNetworkAddress() ) )
  210. {
  211. DPFX(DPFPREP, 3, "Socket port 0x%p matches address", pTempSocketPort );
  212. DumpSocketAddress( 3, pSocketAddress->GetAddress(), pSocketAddress->GetFamily() );
  213. (*ppSocketPort) = pTempSocketPort;
  214. return TRUE;
  215. }
  216. pBilinkSocketPorts = pBilinkSocketPorts->GetNext();
  217. }
  218. #else // ! DPNBUILD_ONLYONEADAPTER
  219. //
  220. // Loop through all adapters.
  221. //
  222. pBilinkAdapters = m_blAdapters.GetNext();
  223. while ( pBilinkAdapters != &m_blAdapters )
  224. {
  225. pTempAdapterEntry = CAdapterEntry::AdapterEntryFromAdapterLinkage( pBilinkAdapters );
  226. if ( pSocketAddress->CompareToBaseAddress( pTempAdapterEntry->BaseAddress() ) == 0 )
  227. {
  228. //
  229. // Loop through all socket ports for this adapter.
  230. //
  231. pBilinkSocketPorts = pTempAdapterEntry->SocketPortList()->GetNext();
  232. while ( pBilinkSocketPorts != pTempAdapterEntry->SocketPortList() )
  233. {
  234. pTempSocketPort = CSocketPort::SocketPortFromBilink( pBilinkSocketPorts );
  235. if ( CSocketAddress::CompareFunction( (PVOID) pSocketAddress, (PVOID) pTempSocketPort->GetNetworkAddress() ) )
  236. {
  237. DPFX(DPFPREP, 3, "Socket port 0x%p matches address", pTempSocketPort );
  238. DumpSocketAddress( 3, pSocketAddress->GetAddress(), pSocketAddress->GetFamily() );
  239. (*ppSocketPort) = pTempSocketPort;
  240. return TRUE;
  241. }
  242. pBilinkSocketPorts = pBilinkSocketPorts->GetNext();
  243. }
  244. }
  245. pBilinkAdapters = pBilinkAdapters->GetNext();
  246. }
  247. #endif // ! DPNBUILD_ONLYONEADAPTER
  248. DPFX(DPFPREP, 3, "Couldn't find socket port matching address.");
  249. DumpSocketAddress( 3, pSocketAddress->GetAddress(), pSocketAddress->GetFamily() );
  250. return FALSE;
  251. }
  252. //**********************************************************************