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.

459 lines
9.8 KiB

  1. /*******************************************************************/
  2. /* Copyright(c) 1992 Microsoft Corporation */
  3. /*******************************************************************/
  4. //***
  5. //
  6. // Filename: ddmif.c
  7. //
  8. // Description: message based communication code
  9. //
  10. // Author: Stefan Solomon (stefans) June 24, 1992.
  11. //
  12. // Revision History:
  13. //
  14. //***
  15. #include "ddm.h"
  16. #include <ddmif.h>
  17. #include <string.h>
  18. #include <raserror.h>
  19. #include <rasppp.h>
  20. //
  21. // Message element definition
  22. //
  23. typedef struct _MESSAGE_Q_OBJECT
  24. {
  25. struct _MESSAGE_Q_OBJECT * pNext;
  26. MESSAGE MsgBuffer;
  27. } MESSAGE_Q_OBJECT, *PMESSAGE_Q_OBJECT;
  28. //
  29. // Message queue header definition
  30. //
  31. typedef struct _MESSAGE_Q
  32. {
  33. MESSAGE_Q_OBJECT * pQHead;
  34. MESSAGE_Q_OBJECT * pQTail;
  35. HANDLE hEvent; // Signaled when enqueueing a new message
  36. DWORD dwLength; // size of message data for each node in Q
  37. CRITICAL_SECTION CriticalSection; // Mutex around this Q
  38. } MESSAGE_Q, *PMESSAGE_Q;
  39. BOOL gfMessageQueueInitialized = FALSE;
  40. //
  41. // Message queue table
  42. //
  43. static MESSAGE_Q MessageQ[MAX_MSG_QUEUES];
  44. VOID
  45. SendPppMessageToDDM(
  46. IN PPP_MESSAGE * pPppMsg
  47. )
  48. {
  49. ServerSendMessage( MESSAGEQ_ID_PPP, (PBYTE)pPppMsg );
  50. }
  51. VOID
  52. RasSecurityDialogComplete(
  53. IN SECURITY_MESSAGE *pSecurityMessage
  54. )
  55. {
  56. ServerSendMessage( MESSAGEQ_ID_SECURITY, (PBYTE)pSecurityMessage );
  57. }
  58. //*** Message Debug Printing Tables ***
  59. typedef struct _MSGDBG
  60. {
  61. WORD id;
  62. LPSTR txtp;
  63. } MSGDBG, *PMSGDBG;
  64. enum
  65. {
  66. MSG_SEND,
  67. MSG_RECEIVE
  68. };
  69. MSGDBG dstsrc[] =
  70. {
  71. { MESSAGEQ_ID_SECURITY, "Security" },
  72. { MESSAGEQ_ID_PPP, "PPP" },
  73. { 0xffff, NULL }
  74. };
  75. MSGDBG authmsgid[] =
  76. {
  77. { AUTH_DONE, "AUTH_DONE" },
  78. { AUTH_FAILURE, "AUTH_FAILURE" },
  79. { AUTH_STOP_COMPLETED, "AUTH_STOP_COMPLETED" },
  80. { AUTH_PROJECTION_REQUEST, "AUTH_PROJECTION_REQUEST" },
  81. { AUTH_CALLBACK_REQUEST, "AUTH_CALLBACK_REQUEST" },
  82. { AUTH_ACCT_OK, "AUTH_ACCT_OK" },
  83. { 0xffff, NULL }
  84. };
  85. MSGDBG pppmsgid[] =
  86. {
  87. { PPPMSG_Stopped, "PPPMSG_Stopped" },
  88. { PPPDDMMSG_PppDone, "PPPDDMMSG_PppDone" },
  89. { PPPDDMMSG_PppFailure, "PPPDDMMSG_PppFailure" },
  90. { PPPDDMMSG_CallbackRequest, "PPPDDMMSG_CallbackRequest" },
  91. { PPPDDMMSG_Authenticated, "PPPDDMMSG_Authenticated" },
  92. { PPPDDMMSG_Stopped, "PPPDDMMSG_Stopped" },
  93. { PPPDDMMSG_NewLink, "PPPDDMMSG_NewLink" },
  94. { PPPDDMMSG_NewBundle, "PPPDDMMSG_NewBundle" },
  95. { PPPDDMMSG_NewBapLinkUp, "PPPDDMMSG_NewBapLinkUp" },
  96. { PPPDDMMSG_BapCallbackRequest, "PPPDDMMSG_BapCallbackRequest" },
  97. { PPPDDMMSG_PnPNotification, "PPPDDMMSG_PnPNotification" },
  98. { PPPDDMMSG_PortCleanedUp, "PPPDDMMSG_PortCleanedUp" },
  99. { 0xffff, NULL }
  100. };
  101. MSGDBG opcodestr[] =
  102. {
  103. { MSG_SEND, "ServerSendMessage" },
  104. { MSG_RECEIVE, "ServerReceiveMessage" },
  105. { 0xffff, NULL }
  106. };
  107. MSGDBG securitymsgid[] =
  108. {
  109. { SECURITYMSG_SUCCESS, "SECURITYMSG_SUCCESS" },
  110. { SECURITYMSG_FAILURE, "SECURITYMSG_FAILURE" },
  111. { SECURITYMSG_ERROR, "SECURITYMSG_ERROR" },
  112. { 0xffff, NULL }
  113. };
  114. char *
  115. getstring(
  116. IN WORD id,
  117. IN PMSGDBG msgdbgp
  118. )
  119. {
  120. char *strp;
  121. PMSGDBG mdp;
  122. for (mdp = msgdbgp; mdp->id != 0xffff; mdp++)
  123. {
  124. if (mdp->id == id)
  125. {
  126. strp = mdp->txtp;
  127. return(strp);
  128. }
  129. }
  130. RTASSERT(FALSE);
  131. return(NULL);
  132. }
  133. //***
  134. //
  135. // Function: msgdbgprint
  136. //
  137. // Descr: prints each message passing through the message module
  138. //
  139. //***
  140. VOID
  141. msgdbgprint(
  142. IN WORD opcode,
  143. IN WORD src,
  144. IN BYTE *buffp
  145. )
  146. {
  147. char *srcsp, *msgidsp, *operation;
  148. HPORT hport = 0;
  149. //
  150. // identify message source. This gives us the clue on the message
  151. // structure.
  152. //
  153. switch (src)
  154. {
  155. case MESSAGEQ_ID_SECURITY:
  156. msgidsp = getstring((WORD)((SECURITY_MESSAGE *) buffp)->dwMsgId,
  157. securitymsgid);
  158. hport = ((SECURITY_MESSAGE *) buffp)->hPort;
  159. break;
  160. case MESSAGEQ_ID_PPP:
  161. msgidsp = getstring((WORD)((PPP_MESSAGE *) buffp)->dwMsgId, pppmsgid );
  162. hport = ((PPP_MESSAGE *) buffp)->hPort;
  163. break;
  164. default:
  165. RTASSERT(FALSE);
  166. }
  167. srcsp = getstring(src, dstsrc);
  168. operation = getstring(opcode, opcodestr);
  169. DDM_PRINT( gblDDMConfigInfo.dwTraceId, TRACE_MESSAGES,
  170. "%s on port/connection: %x from: %s Message: %s",
  171. operation, hport, srcsp, msgidsp);
  172. }
  173. //***
  174. //
  175. // Function: InitializeMessageQs
  176. //
  177. // Returns: None
  178. //
  179. // Description:Initializes the message queue headers
  180. //
  181. //***
  182. VOID
  183. InitializeMessageQs(
  184. IN HANDLE hEventSecurity,
  185. IN HANDLE hEventPPP
  186. )
  187. {
  188. DWORD dwIndex;
  189. MessageQ[MESSAGEQ_ID_SECURITY].hEvent = hEventSecurity;
  190. MessageQ[MESSAGEQ_ID_PPP].hEvent = hEventPPP;
  191. MessageQ[MESSAGEQ_ID_SECURITY].dwLength = sizeof(SECURITY_MESSAGE);
  192. MessageQ[MESSAGEQ_ID_PPP].dwLength = sizeof(PPP_MESSAGE);
  193. for ( dwIndex = 0; dwIndex < MAX_MSG_QUEUES; dwIndex++ )
  194. {
  195. MessageQ[dwIndex].pQHead = NULL;
  196. MessageQ[dwIndex].pQTail = NULL;
  197. InitializeCriticalSection( &(MessageQ[dwIndex].CriticalSection) );
  198. }
  199. gfMessageQueueInitialized = TRUE;
  200. }
  201. //***
  202. //
  203. // Function: DeleteMessageQs
  204. //
  205. // Returns: None
  206. //
  207. // Description:DeInitializes the message queue headers
  208. //
  209. //***
  210. VOID
  211. DeleteMessageQs(
  212. VOID
  213. )
  214. {
  215. DWORD dwIndex;
  216. IN BYTE * pMessage;
  217. if(!gfMessageQueueInitialized)
  218. {
  219. return;
  220. }
  221. //
  222. // Flush the queues
  223. //
  224. for ( dwIndex = 0; dwIndex < MAX_MSG_QUEUES; dwIndex++ )
  225. {
  226. DeleteCriticalSection( &(MessageQ[dwIndex].CriticalSection) );
  227. }
  228. gfMessageQueueInitialized = FALSE;
  229. }
  230. //***
  231. //
  232. // Function: ServerSendMessage
  233. //
  234. // Descr: Sends message from specified server component
  235. // source to server component dst.
  236. //
  237. // Returns: NO_ERROR - success
  238. // else - failure
  239. //
  240. //***
  241. DWORD
  242. ServerSendMessage(
  243. IN MESSAGEQ_ID MsgQId,
  244. IN BYTE * pMessage
  245. )
  246. {
  247. MESSAGE_Q_OBJECT * pMsgQObj;
  248. //
  249. // Make sure DDM is running before accessing any data structure
  250. //
  251. if ( gblDDMConfigInfo.pServiceStatus == NULL )
  252. {
  253. return( ERROR_DDM_NOT_RUNNING );
  254. }
  255. switch( gblDDMConfigInfo.pServiceStatus->dwCurrentState )
  256. {
  257. case SERVICE_STOP_PENDING:
  258. //
  259. // Allow only PPP stopping messages
  260. //
  261. if ( MsgQId == MESSAGEQ_ID_PPP )
  262. {
  263. if ((((PPP_MESSAGE *)pMessage)->dwMsgId == PPPDDMMSG_Stopped ) ||
  264. (((PPP_MESSAGE *)pMessage)->dwMsgId == PPPDDMMSG_PppFailure)||
  265. (((PPP_MESSAGE *)pMessage)->dwMsgId == PPPDDMMSG_PortCleanedUp))
  266. {
  267. break;
  268. }
  269. }
  270. //
  271. // Otherwise fall thru
  272. //
  273. case SERVICE_START_PENDING:
  274. case SERVICE_STOPPED:
  275. return( ERROR_DDM_NOT_RUNNING );
  276. default:
  277. break;
  278. }
  279. EnterCriticalSection( &(MessageQ[MsgQId].CriticalSection) );
  280. //
  281. // allocate a message structure
  282. //
  283. pMsgQObj = (MESSAGE_Q_OBJECT *)LOCAL_ALLOC( LPTR, sizeof(MESSAGE_Q_OBJECT));
  284. if ( pMsgQObj == (MESSAGE_Q_OBJECT *)NULL )
  285. {
  286. //
  287. // can't allocate message buffer
  288. //
  289. RTASSERT(FALSE);
  290. LeaveCriticalSection( &(MessageQ[MsgQId].CriticalSection) );
  291. return( GetLastError() );
  292. }
  293. //
  294. // copy the message
  295. //
  296. CopyMemory( &(pMsgQObj->MsgBuffer), pMessage, MessageQ[MsgQId].dwLength );
  297. //
  298. // Insert it in the Q
  299. //
  300. if ( MessageQ[MsgQId].pQHead == (MESSAGE_Q_OBJECT *)NULL )
  301. {
  302. MessageQ[MsgQId].pQHead = pMsgQObj;
  303. }
  304. else
  305. {
  306. MessageQ[MsgQId].pQTail->pNext = pMsgQObj;
  307. }
  308. MessageQ[MsgQId].pQTail = pMsgQObj;
  309. pMsgQObj->pNext = NULL;
  310. //
  311. // and set appropriate event
  312. //
  313. SetEvent( MessageQ[MsgQId].hEvent );
  314. msgdbgprint( MSG_SEND, (WORD)MsgQId, pMessage );
  315. LeaveCriticalSection( &(MessageQ[MsgQId].CriticalSection) );
  316. return( NO_ERROR );
  317. }
  318. //***
  319. //
  320. // Function: ServerReceiveMessage
  321. //
  322. // Descr: Gets one message from the specified message queue
  323. //
  324. // Returns: TRUE - message fetched
  325. // FALSE - queue empty
  326. //
  327. //***
  328. BOOL
  329. ServerReceiveMessage(
  330. IN MESSAGEQ_ID MsgQId,
  331. IN BYTE * pMessage
  332. )
  333. {
  334. MESSAGE_Q_OBJECT * pMsgQObj;
  335. HLOCAL err;
  336. EnterCriticalSection( &(MessageQ[MsgQId].CriticalSection) );
  337. if ( MessageQ[MsgQId].pQHead == (MESSAGE_Q_OBJECT *)NULL )
  338. {
  339. //
  340. // queue is empty
  341. //
  342. LeaveCriticalSection( &(MessageQ[MsgQId].CriticalSection) );
  343. return( FALSE );
  344. }
  345. pMsgQObj = MessageQ[MsgQId].pQHead;
  346. MessageQ[MsgQId].pQHead = pMsgQObj->pNext;
  347. if ( MessageQ[MsgQId].pQHead == (MESSAGE_Q_OBJECT *)NULL )
  348. {
  349. MessageQ[MsgQId].pQTail = (MESSAGE_Q_OBJECT *)NULL;
  350. }
  351. //
  352. // copy the message in the caller's buffer
  353. //
  354. CopyMemory( pMessage, &(pMsgQObj->MsgBuffer), MessageQ[MsgQId].dwLength );
  355. //
  356. // free the message buffer
  357. //
  358. LOCAL_FREE( pMsgQObj );
  359. msgdbgprint( MSG_RECEIVE, (WORD)MsgQId, pMessage );
  360. LeaveCriticalSection( &(MessageQ[MsgQId].CriticalSection) );
  361. return( TRUE );
  362. }
  363.