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.

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