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.

640 lines
15 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. t30util.c
  5. Abstract:
  6. Utilities for t30
  7. Author:
  8. Rafael Lisitsa (RafaelL) 2-Feb-1996
  9. Revision History:
  10. --*/
  11. #define USE_DEBUG_CONTEXT DEBUG_CONTEXT_T30_MAIN
  12. #include "prep.h"
  13. #include "glbproto.h"
  14. #include "t30gl.h"
  15. #define IDVARSTRINGSIZE (sizeof(VARSTRING)+128)
  16. ///////////////////////////////////////////////////////////////////////////////////
  17. PVOID
  18. T30AllocThreadGlobalData(VOID)
  19. {
  20. PVOID pTG;
  21. pTG = MemAlloc (sizeof(ThrdGlbl) );
  22. if(NULL != pTG)
  23. {
  24. FillMemory (pTG, sizeof(ThrdGlbl), 0);
  25. }
  26. return (pTG);
  27. }
  28. /////////////////////////////////////////////////////////////
  29. BOOL itapi_async_setup(PThrdGlbl pTG)
  30. {
  31. BOOL bRetVal = TRUE;
  32. DEBUG_FUNCTION_NAME(_T("itapi_async_setup"));
  33. EnterCriticalSection(&T30CritSection);
  34. if (pTG->fWaitingForEvent)
  35. {
  36. DebugPrintEx(DEBUG_ERR,"Already waiting for event!");
  37. }
  38. if (pTG->dwSignalledRID)
  39. {
  40. DebugPrintEx( DEBUG_ERR,
  41. "Nonzero old NONZERO ID 0x%lx",
  42. (unsigned long) pTG->dwSignalledRID);
  43. }
  44. pTG->fWaitingForEvent=TRUE;
  45. if (!ResetEvent(pTG->hevAsync))
  46. {
  47. DebugPrintEx( DEBUG_ERR,
  48. "ResetEvent(0x%lx) returns failure code: %ld",
  49. (ULONG_PTR)pTG->hevAsync,
  50. (long) GetLastError());
  51. bRetVal = FALSE;
  52. }
  53. LeaveCriticalSection(&T30CritSection);
  54. return bRetVal;
  55. }
  56. // This function wait till tapi will send message LINE_REPLY.
  57. // dwRequestID - The request we want to get reply for
  58. BOOL itapi_async_wait
  59. (
  60. PThrdGlbl pTG,
  61. DWORD dwRequestID,
  62. PDWORD lpdwParam2,
  63. PDWORD_PTR lpdwParam3,
  64. DWORD dwTimeout
  65. )
  66. {
  67. DWORD NumHandles=2;
  68. HANDLE HandlesArray[2];
  69. DWORD WaitResult;
  70. DEBUG_FUNCTION_NAME(("itapi_async_wait"));
  71. if (!pTG->fWaitingForEvent)
  72. {
  73. DebugPrintEx(DEBUG_ERR,"Not waiting for event!");
  74. // ASSERT(FALSE);
  75. // There are always time-outs that will take us out of here
  76. }
  77. HandlesArray[1] = pTG->AbortReqEvent;
  78. //
  79. // We want to be able to un-block ONCE only from waiting on I/O when the AbortReqEvent is signaled.
  80. //
  81. if (pTG->fAbortRequested)
  82. {
  83. if (pTG->fOkToResetAbortReqEvent && (!pTG->fAbortReqEventWasReset))
  84. {
  85. DebugPrintEx(DEBUG_MSG,"RESETTING AbortReqEvent");
  86. pTG->fAbortReqEventWasReset = TRUE;
  87. if (!ResetEvent(pTG->AbortReqEvent))
  88. {
  89. DebugPrintEx( DEBUG_ERR,
  90. "ResetEvent(0x%lx) returns failure code: %ld",
  91. (ULONG_PTR)pTG->AbortReqEvent,
  92. (long) GetLastError());
  93. }
  94. }
  95. pTG->fUnblockIO = TRUE;
  96. }
  97. HandlesArray[0] = pTG->hevAsync;
  98. NumHandles = 1;
  99. WaitResult = WaitForMultipleObjects(NumHandles, HandlesArray, FALSE, dwTimeout);
  100. if (WaitResult == WAIT_FAILED)
  101. {
  102. DebugPrintEx( DEBUG_ERR,
  103. "WaitForMultipleObjects FAILED le=%lx",
  104. GetLastError());
  105. }
  106. switch( WaitResult )
  107. {
  108. case (WAIT_OBJECT_0):
  109. {
  110. // This happen when TAPI send LINE_REPLY. (The working thread call itapi_async_signal)
  111. BOOL fRet=TRUE;
  112. EnterCriticalSection(&T30CritSection);
  113. // Check that the Request ID we got is the ID we are waiting for
  114. if(pTG->dwSignalledRID == dwRequestID)
  115. {
  116. pTG->dwSignalledRID = 0;
  117. pTG->fWaitingForEvent = FALSE;
  118. }
  119. else
  120. {
  121. DebugPrintEx( DEBUG_ERR,
  122. "Request ID mismatch. Input:0x%p; Curent:0x%p",
  123. dwRequestID,
  124. pTG->dwSignalledRID);
  125. fRet=FALSE;
  126. }
  127. LeaveCriticalSection(&T30CritSection);
  128. if (!fRet)
  129. goto failure;
  130. }
  131. break;
  132. case WAIT_TIMEOUT:
  133. DebugPrintEx( DEBUG_ERR,
  134. "Wait timed out. RequestID=0x%p",
  135. dwRequestID);
  136. goto failure;
  137. default:
  138. DebugPrintEx( DEBUG_ERR,
  139. "Wait returns error. GetLastError=%ld",
  140. (long) GetLastError());
  141. goto failure;
  142. }
  143. if (lpdwParam2)
  144. *lpdwParam2 = pTG->dwSignalledParam2;
  145. if (lpdwParam3)
  146. *lpdwParam3 = pTG->dwSignalledParam3;
  147. return TRUE;
  148. failure:
  149. EnterCriticalSection(&T30CritSection);
  150. if(pTG->dwSignalledRID==dwRequestID)
  151. {
  152. pTG->dwSignalledRID=0;
  153. pTG->fWaitingForEvent=FALSE;
  154. }
  155. LeaveCriticalSection(&T30CritSection);
  156. return FALSE;
  157. }
  158. BOOL itapi_async_signal
  159. (
  160. PThrdGlbl pTG,
  161. DWORD dwRequestID,
  162. DWORD dwParam2,
  163. DWORD_PTR dwParam3
  164. )
  165. {
  166. BOOL fRet=FALSE;
  167. DEBUG_FUNCTION_NAME(_T("itapi_async_signal"));
  168. EnterCriticalSection(&T30CritSection);
  169. if (!pTG->fWaitingForEvent)
  170. {
  171. DebugPrintEx( DEBUG_ERR,
  172. "Not waiting for event, ignoring ID=0x%lx",
  173. (unsigned long) dwRequestID);
  174. goto end;
  175. }
  176. if (pTG->dwSignalledRID)
  177. {
  178. DebugPrintEx( DEBUG_ERR,
  179. "Nonzero old NONZERO ID 0x%lx. New ID=0x%lx",
  180. (unsigned long) pTG->dwSignalledRID,
  181. (unsigned long) dwRequestID);
  182. }
  183. pTG->dwSignalledRID=dwRequestID;
  184. pTG->dwSignalledParam2= 0;
  185. pTG->dwSignalledParam3= 0;
  186. if (!SetEvent(pTG->hevAsync))
  187. {
  188. DebugPrintEx( DEBUG_ERR,
  189. "SetEvent(0x%lx) returns failure code: %ld",
  190. (ULONG_PTR) pTG->hevAsync,
  191. (long) GetLastError());
  192. fRet=FALSE;
  193. goto end;
  194. }
  195. pTG->dwSignalledParam2= dwParam2;
  196. pTG->dwSignalledParam3= dwParam3;
  197. fRet=TRUE;
  198. end:
  199. LeaveCriticalSection(&T30CritSection);
  200. return fRet;
  201. }
  202. #define CALLPARAMS_SIZE (sizeof(LINECALLPARAMS)+512)
  203. LPLINECALLPARAMS itapi_create_linecallparams(void)
  204. {
  205. UINT cb = CALLPARAMS_SIZE;
  206. LPLINECALLPARAMS lpParams = MemAlloc(cb);
  207. if (!lpParams)
  208. goto end;
  209. _fmemset(lpParams,0, cb);
  210. lpParams->dwTotalSize= cb;
  211. lpParams->dwBearerMode = LINEBEARERMODE_PASSTHROUGH;
  212. lpParams->dwMediaMode = LINEMEDIAMODE_DATAMODEM; // Unimodem only accepts
  213. lpParams->dwCallParamFlags = 0;
  214. lpParams->dwAddressMode = LINEADDRESSMODE_ADDRESSID;
  215. lpParams->dwAddressID = 0; // +++ assumes addreddid 0.
  216. lpParams->dwCalledPartySize = 0; // +++
  217. lpParams->dwCalledPartyOffset = 0; // +++
  218. end:
  219. return lpParams;
  220. }
  221. BOOL
  222. SignalStatusChange
  223. (
  224. PThrdGlbl pTG,
  225. DWORD StatusId
  226. )
  227. {
  228. FAX_DEV_STATUS *pFaxStatus = NULL;
  229. LPWSTR lpwCSI; // inside the FaxStatus struct.
  230. LPBYTE lpTemp;
  231. LPWSTR lpwCallerId = NULL;
  232. DEBUG_FUNCTION_NAME(_T("SignalStatusChange"));
  233. //
  234. // If Aborting OR completed then do NOT override the statusId
  235. //
  236. if ( (pTG->StatusId == FS_USER_ABORT) ||
  237. (pTG->StatusId == FS_COMPLETED))
  238. {
  239. // allow changing the status from FS_****_ABORT to FS_COMPLETED only
  240. if (StatusId!=FS_COMPLETED)
  241. {
  242. return (TRUE);
  243. }
  244. }
  245. pTG->StatusId = StatusId;
  246. // should use HeapAlloc because FaxSvc frees it.
  247. pFaxStatus = HeapAlloc(gT30.HeapHandle , HEAP_ZERO_MEMORY, sizeof(FAX_DEV_STATUS) + 4096 );
  248. if (!pFaxStatus)
  249. {
  250. DebugPrintEx(DEBUG_ERR, "SignalStatusChange HeapAlloc failed");
  251. goto failure;
  252. }
  253. pFaxStatus->SizeOfStruct = sizeof (FAX_DEV_STATUS);
  254. pFaxStatus->StatusId = pTG->StatusId;
  255. pFaxStatus->StringId = pTG->StringId;
  256. if (pTG->StatusId == FS_RECEIVING)
  257. {
  258. pFaxStatus->PageCount = pTG->PageCount + 1;
  259. }
  260. else
  261. {
  262. pFaxStatus->PageCount = pTG->PageCount;
  263. }
  264. lpTemp = (LPBYTE) pFaxStatus;
  265. lpTemp += sizeof(FAX_DEV_STATUS);
  266. if (pTG->fRemoteIdAvail)
  267. {
  268. lpwCSI = (LPWSTR) lpTemp;
  269. wcscpy(lpwCSI, pTG->RemoteID);
  270. pFaxStatus->CSI = (LPWSTR) lpwCSI;
  271. lpTemp += ((wcslen(pFaxStatus->CSI)+1)*sizeof(WCHAR));
  272. }
  273. else
  274. {
  275. pFaxStatus->CSI = NULL;
  276. }
  277. pFaxStatus->CallerId = (LPWSTR) lpTemp;
  278. lpwCallerId = (LPWSTR) AnsiStringToUnicodeString(pTG->CallerId);
  279. if (lpwCallerId)
  280. {
  281. wcscpy(pFaxStatus->CallerId, lpwCallerId);
  282. MemFree(lpwCallerId);
  283. }
  284. else
  285. {
  286. pFaxStatus->CallerId = NULL;
  287. }
  288. // do we want to put something here? currently there's no support for Routing Info.
  289. pFaxStatus->RoutingInfo = NULL;
  290. if (! PostQueuedCompletionStatus(pTG->CompletionPortHandle,
  291. sizeof (FAX_DEV_STATUS),
  292. pTG->CompletionKey,
  293. (LPOVERLAPPED) pFaxStatus) )
  294. {
  295. DebugPrintEx( DEBUG_ERR,
  296. "PostQueuedCompletionStatus failed LE=%x",
  297. GetLastError());
  298. goto failure;
  299. }
  300. DebugPrintEx( DEBUG_MSG,
  301. "ID=%x Page=%d",
  302. pTG->StatusId,
  303. pTG->PageCount);
  304. return (TRUE);
  305. failure:
  306. if (pFaxStatus)
  307. {
  308. HeapFree(gT30.HeapHandle,0,pFaxStatus);
  309. }
  310. return (FALSE);
  311. }
  312. BOOL
  313. SignalStatusChangeWithStringId
  314. (
  315. PThrdGlbl pTG,
  316. DWORD StatusId,
  317. DWORD StringId
  318. )
  319. {
  320. pTG->StringId = StringId;
  321. return SignalStatusChange(pTG, StatusId);
  322. }
  323. BOOL
  324. SignalRecoveryStatusChange
  325. (
  326. T30_RECOVERY_GLOB *Recovery
  327. )
  328. {
  329. FAX_DEV_STATUS *pFaxStatus = NULL;
  330. // should use HeapAlloc because FaxSvc frees it.
  331. pFaxStatus = HeapAlloc(gT30.HeapHandle , HEAP_ZERO_MEMORY, sizeof(FAX_DEV_STATUS) + 4096 );
  332. if (!pFaxStatus)
  333. {
  334. goto failure;
  335. }
  336. pFaxStatus->SizeOfStruct = sizeof (FAX_DEV_STATUS) + 4096;
  337. pFaxStatus->StatusId = FS_FATAL_ERROR; // RSL better: FS_FSP_EXCEPTION_HANDLED;
  338. pFaxStatus->StringId = 0;
  339. pFaxStatus->PageCount = 0;
  340. pFaxStatus->CSI = NULL;
  341. pFaxStatus->CallerId = NULL;
  342. pFaxStatus->RoutingInfo = NULL;
  343. if (! PostQueuedCompletionStatus(Recovery->CompletionPortHandle,
  344. sizeof (FAX_DEV_STATUS),
  345. Recovery->CompletionKey,
  346. (LPOVERLAPPED) pFaxStatus) )
  347. {
  348. goto failure;
  349. }
  350. return (TRUE);
  351. failure:
  352. if (pFaxStatus)
  353. {
  354. HeapFree(gT30.HeapHandle,0,pFaxStatus);
  355. }
  356. return (FALSE);
  357. }
  358. DWORD
  359. ComputeCheckSum
  360. (
  361. LPDWORD BaseAddr,
  362. DWORD NumDwords
  363. )
  364. {
  365. DWORD RetValue = 0;
  366. DWORD i;
  367. for (i=0; i<NumDwords; i++)
  368. {
  369. RetValue += *(BaseAddr+i);
  370. }
  371. return (RetValue);
  372. }
  373. static LPCTSTR sgActionDescriptionTable[actionNUM_ACTIONS] =
  374. {
  375. "actionNULL",
  376. "actionFALSE",
  377. "actionTRUE",
  378. "actionERROR",
  379. "actionHANGUP",
  380. "actionDCN",
  381. "actionGONODE_T",
  382. "actionGONODE_R1",
  383. "actionGONODE_R2",
  384. "actionGONODE_A",
  385. "actionGONODE_D",
  386. "actionGONODE_E",
  387. "actionGONODE_F",
  388. "actionGONODE_I",
  389. "actionGONODE_II",
  390. "actionGONODE_III",
  391. "actionGONODE_IV",
  392. "actionGONODE_V",
  393. "actionGONODE_VII",
  394. "actionGONODE_RECVCMD",
  395. "actionGONODE_ECMRETRANSMIT",
  396. "actionGONODE_RECVPHASEC",
  397. "actionGONODE_RECVECMRETRANSMIT",
  398. "actionSEND_DIS",
  399. "actionSEND_DTC",
  400. "actionSEND_DCS",
  401. "actionSENDMPS",
  402. "actionSENDEOM",
  403. "actionSENDEOP",
  404. "actionSENDMCF",
  405. "actionSENDRTP",
  406. "actionSENDRTN",
  407. "actionSENDFTT",
  408. "actionSENDCFR",
  409. "actionSENDEOR_EOP",
  410. "actionGETTCF",
  411. "actionSKIPTCF",
  412. "actionSENDDCSTCF",
  413. "actionDCN_SUCCESS",
  414. "actionNODEF_SUCCESS",
  415. "actionHANGUP_SUCCESS",
  416. };
  417. LPCTSTR action_GetActionDescription(ET30ACTION action)
  418. {
  419. Assert(action<actionNUM_ACTIONS);
  420. if (action>=actionNUM_ACTIONS)
  421. return NULL;
  422. return sgActionDescriptionTable[action];
  423. }
  424. static LPCTSTR sgEventDescriptionTable[eventNUM_EVENTS] =
  425. {
  426. "eventNULL",
  427. "eventGOTFRAMES",
  428. "eventNODE_A",
  429. "eventSENDDCS",
  430. "eventGOTFTT",
  431. "eventGOTCFR",
  432. "eventSTARTSEND",
  433. "eventPOSTPAGE",
  434. "eventGOTPOSTPAGERESP",
  435. "eventGOT_ECM_PPS_RESP",
  436. "eventSENDDIS",
  437. "eventSENDDTC",
  438. "eventRECVCMD",
  439. "eventGOTTCF",
  440. "eventSTARTRECV",
  441. "eventRECVPOSTPAGECMD",
  442. "eventECM_POSTPAGE",
  443. "event4THPPR",
  444. "eventNODE_T",
  445. "eventNODE_R",
  446. };
  447. LPCTSTR event_GetEventDescription(ET30EVENT event)
  448. {
  449. Assert(event<eventNUM_EVENTS);
  450. if (event>=eventNUM_EVENTS)
  451. return NULL;
  452. return sgEventDescriptionTable[event];
  453. }
  454. static LPCTSTR sgIfrDescriptionTable[] =
  455. {
  456. "ifrNULL",
  457. "ifrDIS",
  458. "ifrCSI",
  459. "ifrNSF",
  460. "ifrDTC",
  461. "ifrCIG",
  462. "ifrNSC",
  463. "ifrDCS",
  464. "ifrTSI",
  465. "ifrNSS",
  466. "ifrCFR",
  467. "ifrFTT",
  468. "ifrMPS",
  469. "ifrEOM",
  470. "ifrEOP",
  471. "ifrPWD",
  472. "ifrSEP",
  473. "ifrSUB",
  474. "ifrMCF",
  475. "ifrRTP",
  476. "ifrRTN",
  477. "ifrPIP",
  478. "ifrPIN",
  479. "ifrDCN",
  480. "ifrCRP",
  481. "ifrPRI_MPS",
  482. "ifrPRI_EOM",
  483. "ifrPRI_EOP",
  484. "ifrCTC",
  485. "ifrCTR",
  486. "ifrRR",
  487. "ifrPPR",
  488. "ifrRNR",
  489. "ifrERR",
  490. "ifrPPS_NULL",
  491. "ifrPPS_MPS",
  492. "ifrPPS_EOM",
  493. "ifrPPS_EOP",
  494. "ifrPPS_PRI_MPS",
  495. "ifrPPS_PRI_EOM",
  496. "ifrPPS_PRI_EOP",
  497. "ifrEOR_NULL",
  498. "ifrEOR_MPS",
  499. "ifrEOR_EOM",
  500. "ifrEOR_EOP",
  501. "ifrEOR_PRI_MPS",
  502. "ifrEOR_PRI_EOM",
  503. "ifrEOR_PRI_EOP",
  504. "ifrMAX",
  505. "ifrBAD",
  506. "ifrTIMEOUT",
  507. };
  508. static const int ifrNUM_IFR = sizeof(sgIfrDescriptionTable)/sizeof(sgIfrDescriptionTable[0]);
  509. LPCTSTR ifr_GetIfrDescription(BYTE ifr)
  510. {
  511. Assert(ifr<=ifrNUM_IFR);
  512. if (ifr>ifrNUM_IFR)
  513. return NULL;
  514. return sgIfrDescriptionTable[ifr];
  515. }