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.

242 lines
5.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: advert.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "Remote.h"
  14. // ===================================================
  15. // CLIENT END OF THE MAILSLOT
  16. // ===================================================
  17. #define INITIAL_AD_RATE 15*1000 //10 Sec. REMOVE
  18. #define MAXIMUM_AD_RATE 45*60*1000 //1 hour
  19. #define INITIAL_SLEEP_PERIOD 2*60*1000 //Initial Sleep Period REMOVE - change to 5 min.
  20. #define KDCONNECTED TEXT("Kernel Debugger connection established.")
  21. extern TCHAR SaveFileName[64];
  22. extern SESSION_TYPE ClientList[10];
  23. extern HANDLE ListenThreadH;
  24. HANDLE hRead;
  25. HANDLE
  26. IsConnectedToClient(
  27. TCHAR *ClientName
  28. );
  29. BOOL
  30. WaitForString(
  31. HANDLE hRead,
  32. TCHAR *str
  33. );
  34. VOID
  35. InitAd(
  36. BOOL IsAdvertise
  37. );
  38. VOID
  39. ShutAd(
  40. BOOL IsAdvertise
  41. );
  42. DWORD
  43. Advertise(
  44. TCHAR *PipeName
  45. );
  46. VOID
  47. InitAd(
  48. BOOL IsAdvertise
  49. )
  50. {
  51. if (IsAdvertise)
  52. {
  53. HANDLE hThread;
  54. DWORD WhoCares;
  55. hThread=CreateThread
  56. (
  57. (LPSECURITY_ATTRIBUTES)NULL,
  58. 0,
  59. (LPTHREAD_START_ROUTINE)Advertise,
  60. (LPVOID)PipeName,
  61. 0,
  62. &WhoCares
  63. );
  64. CloseHandle(hThread);
  65. }
  66. }
  67. VOID
  68. ShutAd(
  69. BOOL IsAdvertise
  70. )
  71. {
  72. if (IsAdvertise)
  73. SAFECLOSEHANDLE(hRead);
  74. }
  75. DWORD
  76. Advertise(
  77. TCHAR *PipeName
  78. )
  79. {
  80. DWORD WaitTime=INITIAL_AD_RATE;
  81. DWORD NameLen=32;
  82. HANDLE hThread;
  83. TCHAR SendBuff[256];
  84. DWORD tmp;
  85. HANDLE hMailSlot;
  86. TCHAR Hostname[32];
  87. Sleep(INITIAL_SLEEP_PERIOD);
  88. hRead=CreateFile(
  89. SaveFileName,
  90. GENERIC_READ|GENERIC_WRITE,
  91. FILE_SHARE_READ|FILE_SHARE_WRITE,
  92. NULL,
  93. OPEN_EXISTING,
  94. FILE_ATTRIBUTE_NORMAL,
  95. NULL);
  96. if (hRead==INVALID_HANDLE_VALUE)
  97. {
  98. return(1);
  99. }
  100. hMailSlot=CreateFile(
  101. TEXT("\\\\NTDEV\\MAILSLOT\\REMOTE\\ADVERTISE"),
  102. GENERIC_WRITE,
  103. FILE_SHARE_WRITE,
  104. (LPSECURITY_ATTRIBUTES)NULL,
  105. OPEN_EXISTING,
  106. FILE_ATTRIBUTE_NORMAL,
  107. (HANDLE)NULL
  108. );
  109. if (hMailSlot==INVALID_HANDLE_VALUE)
  110. {
  111. return(2);
  112. }
  113. GetComputerName(HostName,&NameLen);
  114. _stprintf(SendBuff,TEXT("%.31s %.31s %.63s"),HostName,PipeName,ChildCmd);
  115. if (!WaitForString(hRead,KDCONNECTED))
  116. {
  117. return(1);
  118. }
  119. while(TRUE)
  120. {
  121. if (!(WriteFile(
  122. hMailSlot,
  123. (LPVOID)SendBuff,
  124. (DWORD)_tcslen(SendBuff)+1,
  125. &tmp,
  126. NULL
  127. )
  128. )
  129. )
  130. {
  131. //ERRORMSSG(TEXT("WriteFile Failed on Mailslot"));
  132. Sleep(10*60*1000);
  133. }
  134. else
  135. {
  136. Sleep(15*1000);
  137. while ((hThread=IsConnectedToClient(TEXT("SKYLINK")))!=NULL)
  138. {
  139. WaitForSingleObject(hThread,INFINITE);
  140. WaitTime=INITIAL_AD_RATE;
  141. WaitForString(hRead,KDCONNECTED); //REMOVE COMMENT
  142. }
  143. WaitTime=min(MAXIMUM_AD_RATE,2*WaitTime);
  144. Sleep(WaitTime);
  145. }
  146. }
  147. return(0); //Should never get here
  148. }
  149. BOOL
  150. WaitForString(
  151. HANDLE hRead,
  152. TCHAR *str
  153. )
  154. {
  155. int len=_tcslen(str);
  156. TCHAR Buff[512];
  157. TCHAR *BuffEnd=Buff;
  158. TCHAR *BuffPos=Buff;
  159. TCHAR *strPos=str;
  160. int matchcount=0;
  161. BOOL Found=FALSE;
  162. DWORD bytesread;
  163. while(!Found)
  164. {
  165. while (BuffPos==BuffEnd)
  166. {
  167. BuffPos=Buff;
  168. SetLastError(0);
  169. if ((!ReadFile(hRead,Buff,512,&bytesread,NULL))||(bytesread==0))
  170. {
  171. if ((GetLastError()!=ERROR_HANDLE_EOF)&&(GetLastError()!=0))
  172. {
  173. return(FALSE);
  174. }
  175. Sleep(2*60*1000);
  176. }
  177. BuffEnd=Buff+bytesread;
  178. }
  179. while(!Found && (BuffPos!=BuffEnd))
  180. {
  181. if (*(BuffPos++)==*(strPos++))
  182. {
  183. matchcount++;
  184. if (matchcount==len)
  185. Found=TRUE;
  186. }
  187. else
  188. {
  189. matchcount=0;
  190. strPos=str;
  191. }
  192. }
  193. }
  194. return(Found);
  195. }
  196. HANDLE
  197. IsConnectedToClient(
  198. TCHAR *ClientName
  199. )
  200. {
  201. extern SESSION_TYPE ClientList[MAX_SESSION];
  202. int i;
  203. for (i=0;i<MAX_SESSION;i++)
  204. {
  205. if ((ClientList[i].Active==TRUE) &&
  206. (_tcscmp(ClientName,ClientList[i].Name)==0)
  207. )
  208. {
  209. return(ClientList[i].hThread);
  210. }
  211. }
  212. return(NULL);
  213. }