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.

267 lines
8.5 KiB

  1. /************************************************************************
  2. * *
  3. * INTEL CORPORATION PROPRIETARY INFORMATION *
  4. * *
  5. * This software is supplied under the terms of a license *
  6. * agreement or non-disclosure agreement with Intel Corporation *
  7. * and may not be copied or disclosed except in accordance *
  8. * with the terms of that agreement. *
  9. * *
  10. * Copyright (C) 1997 Intel Corp. All Rights Reserved *
  11. * *
  12. * $Archive: S:\sturgeon\src\gki\vcs\discover.cpv $
  13. * *
  14. * $Revision: 1.10 $
  15. * $Date: 13 Feb 1997 16:20:44 $
  16. * *
  17. * $Author: CHULME $
  18. * *
  19. * $Log: S:\sturgeon\src\gki\vcs\discover.cpv $
  20. //
  21. // Rev 1.10 13 Feb 1997 16:20:44 CHULME
  22. // Moved CGatekeeper::Unlock to end of Discover thread for synchronization
  23. //
  24. // Rev 1.9 12 Feb 1997 01:11:00 CHULME
  25. // Redid thread synchronization to use Gatekeeper.Lock
  26. //
  27. // Rev 1.8 08 Feb 1997 12:12:06 CHULME
  28. // Changed from using unsigned long to HANDLE for thread handles
  29. //
  30. // Rev 1.7 24 Jan 1997 18:36:06 CHULME
  31. // Reverted to rev 1.5
  32. //
  33. // Rev 1.5 22 Jan 1997 16:53:06 CHULME
  34. // Reset the gatekeeper reject flag before issuing discovery request
  35. //
  36. // Rev 1.4 17 Jan 1997 09:01:54 CHULME
  37. // Changed reg.h to gkreg.h to avoid name conflict with inc directory
  38. //
  39. // Rev 1.3 10 Jan 1997 16:14:14 CHULME
  40. // Removed MFC dependency
  41. //
  42. // Rev 1.2 22 Nov 1996 15:20:46 CHULME
  43. // Added VCS log to the header
  44. *************************************************************************/
  45. // discovery.cpp : Provides the discovery thread implementation
  46. //
  47. #include "precomp.h"
  48. #include <process.h>
  49. #include "GKICOM.H"
  50. #include "dspider.h"
  51. #include "dgkilit.h"
  52. #include "DGKIPROT.H"
  53. #include "gksocket.h"
  54. #include "GKREG.H"
  55. #include "GATEKPR.H"
  56. #include "h225asn.h"
  57. #include "coder.hpp"
  58. #include "dgkiext.h"
  59. #ifdef _DEBUG
  60. #undef THIS_FILE
  61. static char THIS_FILE[] = __FILE__;
  62. #endif
  63. #ifdef BROADCAST_DISCOVERY
  64. void
  65. GKDiscovery(void *pv)
  66. {
  67. // ABSTRACT: This function is invoked in a separate thread to
  68. // issue a gatekeeper discovery PDU (GRQ) and listen for a
  69. // responding GCF and/or GRJ. If successful, it will then
  70. // issue a registration request (RRQ).
  71. // AUTHOR: Colin Hulme
  72. char szBuffer[512];
  73. int nRet;
  74. ASN1_BUF Asn1Buf;
  75. DWORD dwErrorCode;
  76. RasMessage *pRasMessage;
  77. char *pDestAddr;
  78. HANDLE hThread;
  79. #ifdef _DEBUG
  80. char szGKDebug[80];
  81. #endif
  82. HRESULT hResult = GKI_OK;
  83. SPIDER_TRACE(SP_FUNC, "GKDiscovery()\n", 0);
  84. ASSERT(g_pCoder && g_pGatekeeper);
  85. if ((g_pCoder == NULL) && (g_pGatekeeper == NULL))
  86. return;
  87. g_pGatekeeper->SetRejectFlag(FALSE); // Reset the reject flag
  88. // Send Async informational notification to client that we are doing a discovery
  89. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_REG_DISCOVERY, 0, 0)\n", 0);
  90. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_REG_DISCOVERY, 0, 0);
  91. // Send a broadcast on the gatekeeper discovery port
  92. if ((hResult = g_pReg->GatekeeperRequest()) != GKI_OK)
  93. {
  94. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, %X)\n", hResult);
  95. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0, hResult);
  96. }
  97. while (hResult == GKI_OK)
  98. {
  99. nRet = g_pReg->m_pSocket->ReceiveFrom(szBuffer, 512);
  100. g_pGatekeeper->Lock();
  101. if (g_pReg == 0)
  102. {
  103. SPIDER_TRACE(SP_THREAD, "Discovery thread exiting\n", 0);
  104. g_pGatekeeper->Unlock();
  105. return;
  106. }
  107. if (nRet != SOCKET_ERROR)
  108. {
  109. if (fGKIEcho && (pEchoBuff != 0))
  110. {
  111. if (nEchoLen != nRet)
  112. {
  113. SPIDER_TRACE(SP_DEBUG, "*** Received buffer len != Sent buffer len ***\n", 0);
  114. }
  115. if (memcmp(szBuffer, pEchoBuff, nEchoLen) == 0)
  116. {
  117. SPIDER_TRACE(SP_DEBUG, "Received buffer = Sent buffer\n", 0);
  118. }
  119. else
  120. {
  121. SPIDER_TRACE(SP_DEBUG, "*** Received buffer != Sent buffer ***\n", 0);
  122. }
  123. SPIDER_TRACE(SP_NEWDEL, "del pEchoBuff = %X\n", pEchoBuff);
  124. delete pEchoBuff;
  125. pEchoBuff = 0;
  126. hResult = GKI_EXIT_THREAD;
  127. }
  128. else // Check incoming PDU for GCF or GRJ
  129. {
  130. // Setup Asn1Buf for decoder and decode PDU
  131. Asn1Buf.length = nRet; // number of bytes received
  132. Asn1Buf.value = (unsigned char *)szBuffer;
  133. dwErrorCode = g_pCoder->Decode(&Asn1Buf, &pRasMessage);
  134. if (dwErrorCode)
  135. {
  136. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, GKI_DECODER_ERROR)\n", 0);
  137. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0, GKI_DECODER_ERROR);
  138. }
  139. else
  140. {
  141. #ifdef _DEBUG
  142. if (dwGKIDLLFlags & SP_DUMPMEM)
  143. DumpMem(pRasMessage, sizeof(RasMessage));
  144. #endif
  145. switch (pRasMessage->choice)
  146. {
  147. case gatekeeperConfirm_chosen:
  148. SPIDER_TRACE(SP_PDU, "Rcv GCF; g_pReg = %X\n", g_pReg);
  149. hResult = g_pReg->GatekeeperConfirm(pRasMessage);
  150. if (hResult != GKI_OK)
  151. {
  152. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, %X)\n", hResult);
  153. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0,
  154. hResult);
  155. }
  156. //////////////////////////////////////////////////////////////////////////////////////////
  157. else
  158. {
  159. pDestAddr = (g_pReg->GetRegistrationTransport() == ipAddress_chosen) ?
  160. g_pGatekeeper->GetIPAddress() : g_pGatekeeper->GetIPXAddress();
  161. // Connect to destination gatekeeper and retrieve RAS port
  162. if (g_pReg->m_pSocket->Connect(pDestAddr))
  163. {
  164. hResult = GKI_WINSOCK2_ERROR(g_pReg->m_pSocket->GetLastError());
  165. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, %X)\n", hResult);
  166. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0, hResult);
  167. }
  168. // Create RegistrationRequest structure - Encode and send PDU
  169. if ((hResult = g_pReg->RegistrationRequest(TRUE)) != GKI_OK)
  170. {
  171. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, %X)\n", hResult);
  172. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0, hResult);
  173. }
  174. // Post a receive on this socket
  175. hThread = (HANDLE)_beginthread(PostReceive, 0, 0);
  176. SPIDER_TRACE(SP_THREAD, "_beginthread(PostReceive, 0, 0); <%X>\n", hThread);
  177. if (hThread == (HANDLE)-1)
  178. {
  179. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, GKI_NO_THREAD)\n", 0);
  180. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0, GKI_NO_THREAD);
  181. }
  182. g_pReg->SetRcvThread(hThread);
  183. if (hResult == GKI_OK)
  184. hResult = GKI_GCF_RCV;
  185. }
  186. break;
  187. case gatekeeperReject_chosen:
  188. SPIDER_TRACE(SP_PDU, "Rcv GRJ; g_pReg = %X\n", g_pReg);
  189. hResult = g_pReg->GatekeeperReject(pRasMessage);
  190. if (hResult != GKI_OK)
  191. {
  192. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, %X)\n", hResult);
  193. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0,
  194. hResult);
  195. }
  196. break;
  197. default:
  198. SPIDER_TRACE(SP_PDU, "Rcv %X\n", pRasMessage->choice);
  199. hResult = g_pReg->UnknownMessage(pRasMessage);
  200. break;
  201. }
  202. }
  203. // Free the encoder memory
  204. g_pCoder->Free(pRasMessage);
  205. }
  206. }
  207. else
  208. {
  209. // WSAEINTR - returned when socket closed
  210. // get out cleanly
  211. if ((nRet = g_pReg->m_pSocket->GetLastError()) == WSAEINTR)
  212. hResult = GKI_GCF_RCV;
  213. else
  214. {
  215. hResult = GKI_WINSOCK2_ERROR(nRet);
  216. SPIDER_TRACE(SP_GKI, "PostMessage(m_hWnd, m_wBaseMessage + GKI_ERROR, 0, %X)\n", hResult);
  217. PostMessage(g_pReg->GetHWnd(), g_pReg->GetBaseMessage() + GKI_ERROR, 0,
  218. hResult);
  219. hResult = GKI_EXIT_THREAD;
  220. }
  221. }
  222. g_pGatekeeper->Unlock();
  223. }
  224. // If not successful - need to remove retry thread and registration object
  225. g_pGatekeeper->Lock();
  226. if (g_pReg == 0)
  227. {
  228. SPIDER_TRACE(SP_THREAD, "Discovery thread exiting\n", 0);
  229. g_pGatekeeper->Unlock();
  230. return;
  231. }
  232. if (hResult != GKI_GCF_RCV)
  233. {
  234. SPIDER_TRACE(SP_NEWDEL, "del g_pReg = %X\n", g_pReg);
  235. delete g_pReg;
  236. g_pReg = 0;
  237. }
  238. else
  239. g_pReg->SetDiscThread(0);
  240. SPIDER_TRACE(SP_THREAD, "GKDiscovery thread exiting\n", 0);
  241. g_pGatekeeper->Unlock();
  242. }
  243. #endif // BROADCAST_DISCOVERY