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.

518 lines
12 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. debug.cpp
  5. Abstract:
  6. Routines for displaying debug messages.
  7. Author:
  8. Nikhil Bobde (NikhilB)
  9. Revision History:
  10. --*/
  11. //
  12. // Include files
  13. //
  14. #include "globals.h"
  15. #define DEBUG_FORMAT_HEADER "H323 "
  16. #define DEBUG_FORMAT_TIMESTAMP "[%02u:%02u:%02u.%03u"
  17. #define DEBUG_FORMAT_THREADID ",tid=%x] "
  18. #define DEBUG_FORMAT_END "\r\n"
  19. HANDLE
  20. H323CreateEvent(
  21. LPSECURITY_ATTRIBUTES lpEventAttributes, // SD
  22. BOOL bManualReset, // reset type
  23. BOOL bInitialState, // initial state
  24. LPCTSTR lpName // object name
  25. )
  26. {
  27. #if DBG
  28. return CreateEvent( lpEventAttributes, bManualReset, bInitialState, lpName );
  29. #else
  30. return CreateEvent( lpEventAttributes, bManualReset, bInitialState, NULL );
  31. #endif
  32. }
  33. #if DBG
  34. DWORD g_dwTraceID = INVALID_TRACEID;
  35. static TCHAR g_szTraceName[100]; // saves name of dll
  36. //
  37. // Private definitions
  38. //
  39. #define MAX_DEBUG_STRLEN 800
  40. #define STATUS_MASK_ERROR 0x0000FFFF
  41. #define STATUS_MASK_FACILITY 0x0FFF0000
  42. void H323DUMPBUFFER( IN BYTE * pEncoded, IN DWORD cbEncodedSize )
  43. {
  44. DWORD indexI, indexJ=0;
  45. char ch;
  46. char szDebugMessage[MAX_DEBUG_STRLEN];
  47. szDebugMessage[0] = '\0';
  48. H323DBG(( DEBUG_LEVEL_ERROR, "ASN buffer start." ));
  49. for( indexI=0; indexI<cbEncodedSize; indexI++ )
  50. {
  51. ch = (pEncoded[indexI]>>4);
  52. if( (ch>=0)&&(ch<=9) )
  53. {
  54. ch+='0';
  55. }
  56. else
  57. {
  58. ch=ch+'A'-10;
  59. }
  60. szDebugMessage[indexJ++]=ch;
  61. ch = (pEncoded[indexI]& 0x0F);
  62. if( (ch>=0)&&(ch<=9) )
  63. {
  64. ch+='0';
  65. }
  66. else
  67. {
  68. ch= ch+'A'-10;
  69. }
  70. szDebugMessage[indexJ++]=ch;
  71. if( indexJ >= 700 )
  72. {
  73. szDebugMessage[indexJ]= '\0';
  74. H323DBG((DEBUG_LEVEL_ERROR, szDebugMessage ));
  75. indexJ = 0;
  76. }
  77. }
  78. szDebugMessage[indexJ]= '\0';
  79. H323DBG((DEBUG_LEVEL_ERROR, szDebugMessage ));
  80. H323DBG(( DEBUG_LEVEL_ERROR, "ASN buffer end." ));
  81. return;
  82. }
  83. //
  84. // Public procedures
  85. //
  86. /*++
  87. Routine Description:
  88. Debug output routine for service provider.
  89. Arguments:
  90. Same as printf.
  91. Return Values:
  92. None.
  93. --*/
  94. VOID
  95. H323DbgPrint(
  96. DWORD dwLevel,
  97. LPSTR szFormat,
  98. ...
  99. )
  100. {
  101. va_list Args;
  102. SYSTEMTIME SystemTime;
  103. char szDebugMessage[MAX_DEBUG_STRLEN+1];
  104. int nLengthRemaining;
  105. int nLength = 0;
  106. // point at first argument
  107. va_start(Args, szFormat);
  108. // see if level enabled
  109. if( dwLevel <= g_RegistrySettings.dwLogLevel )
  110. {
  111. // always emit messages at DEBUG_LEVEL_FORCE
  112. // retrieve local time
  113. GetLocalTime(&SystemTime);
  114. // add component header to the debug message
  115. nLength += sprintf(&szDebugMessage[nLength],
  116. DEBUG_FORMAT_HEADER
  117. );
  118. // add timestamp to the debug message
  119. nLength += sprintf(&szDebugMessage[nLength],
  120. DEBUG_FORMAT_TIMESTAMP,
  121. SystemTime.wHour,
  122. SystemTime.wMinute,
  123. SystemTime.wSecond,
  124. SystemTime.wMilliseconds
  125. );
  126. // add thread id to the debug message
  127. nLength += sprintf(&szDebugMessage[nLength],
  128. DEBUG_FORMAT_THREADID,
  129. GetCurrentThreadId()
  130. );
  131. // determine number of bytes left in buffer
  132. nLengthRemaining = sizeof(szDebugMessage) - nLength -
  133. strlen(DEBUG_FORMAT_END);
  134. // add user specified debug message
  135. nLength += _vsnprintf(&szDebugMessage[nLength],
  136. nLengthRemaining,
  137. szFormat,
  138. Args
  139. );
  140. nLength += sprintf(&szDebugMessage[nLength], DEBUG_FORMAT_END );
  141. // output message to specified sink
  142. OutputDebugStringA(szDebugMessage);
  143. }
  144. TraceVprintfExA( g_dwTraceID, (dwLevel | TRACE_USE_MASK), szFormat, Args );
  145. // release pointer
  146. va_end(Args);
  147. }
  148. void DumpError(
  149. IN DWORD ErrorCode )
  150. {
  151. CHAR ErrorText [0x100];
  152. DWORD Length;
  153. Length = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL,
  154. ErrorCode, LANG_NEUTRAL, ErrorText, 0x100, NULL );
  155. if( Length == 0 )
  156. {
  157. _snprintf( ErrorText, 0x100, "<unknown error %08XH %d>",
  158. ErrorCode, ErrorCode);
  159. }
  160. H323DBG ((DEBUG_LEVEL_ERROR, "\t%s", ErrorText));
  161. }
  162. BOOL TRACELogRegister(LPCTSTR szName)
  163. {
  164. #ifdef UNICODE
  165. wsprintf(g_szTraceName, _T("%ls"), szName);
  166. #else
  167. wsprintfA(g_szTraceName, "%s", szName);
  168. #endif
  169. g_dwTraceID = TraceRegister(g_szTraceName);
  170. return( g_dwTraceID != INVALID_TRACEID );
  171. }
  172. void TRACELogDeRegister()
  173. {
  174. if( g_dwTraceID != INVALID_TRACEID )
  175. {
  176. TraceDeregister(g_dwTraceID);
  177. g_dwTraceID = INVALID_TRACEID;
  178. }
  179. }
  180. #else
  181. HANDLE g_hLogFile;
  182. VOID
  183. OpenLogFile()
  184. {
  185. g_hLogFile = INVALID_HANDLE_VALUE;
  186. g_hLogFile = CreateFile( _T("h323log.txt"),
  187. GENERIC_READ|GENERIC_WRITE,
  188. FILE_SHARE_READ,
  189. NULL,
  190. OPEN_ALWAYS,
  191. FILE_ATTRIBUTE_NORMAL,
  192. NULL );
  193. }
  194. VOID
  195. CloseLogFile()
  196. {
  197. if( g_hLogFile != INVALID_HANDLE_VALUE )
  198. {
  199. CloseHandle(g_hLogFile);
  200. }
  201. }
  202. VOID
  203. H323DbgPrintFre(
  204. DWORD dwLevel,
  205. LPSTR szFormat,
  206. ...
  207. )
  208. {
  209. va_list Args;
  210. SYSTEMTIME SystemTime;
  211. char szDebugMessage[800];
  212. DWORD NumberOfBytesWritten=0;
  213. int nLengthRemaining;
  214. int nLength = 0;
  215. // see if level enabled
  216. if( dwLevel <= g_RegistrySettings.dwLogLevel )
  217. {
  218. // point at first argument
  219. va_start(Args, szFormat);
  220. // always emit messages at DEBUG_LEVEL_FORCE
  221. // retrieve local time
  222. GetLocalTime(&SystemTime);
  223. // add component header to the debug message
  224. nLength += sprintf(&szDebugMessage[nLength],
  225. DEBUG_FORMAT_HEADER
  226. );
  227. // add timestamp to the debug message
  228. nLength += sprintf(&szDebugMessage[nLength],
  229. DEBUG_FORMAT_TIMESTAMP,
  230. SystemTime.wHour,
  231. SystemTime.wMinute,
  232. SystemTime.wSecond,
  233. SystemTime.wMilliseconds
  234. );
  235. // add thread id to the debug message
  236. nLength += sprintf(&szDebugMessage[nLength],
  237. DEBUG_FORMAT_THREADID,
  238. GetCurrentThreadId()
  239. );
  240. // determine number of bytes left in buffer
  241. nLengthRemaining = sizeof(szDebugMessage) - nLength -
  242. strlen(DEBUG_FORMAT_END);
  243. // add user specified debug message
  244. nLength += _vsnprintf(&szDebugMessage[nLength],
  245. nLengthRemaining,
  246. szFormat,
  247. Args
  248. );
  249. nLength += sprintf(&szDebugMessage[nLength], DEBUG_FORMAT_END );
  250. // output message to specified sink
  251. //OutputDebugStringA(szDebugMessage);
  252. if( g_hLogFile != INVALID_HANDLE_VALUE )
  253. {
  254. WriteFile( g_hLogFile,
  255. szDebugMessage,
  256. nLength,
  257. &NumberOfBytesWritten,
  258. NULL );
  259. }
  260. // release pointer
  261. va_end(Args);
  262. }
  263. }
  264. #endif // DBG
  265. PSTR
  266. EventIDToString(
  267. DWORD eventID
  268. )
  269. {
  270. static PSTR apszEventNameStrings[] = {
  271. "TSPI_NO_EVENT",
  272. "TSPI_MAKE_CALL",
  273. "TSPI_ANSWER_CALL",
  274. "TSPI_DROP_CALL",
  275. "TSPI_CLOSE_CALL",
  276. "TSPI_RELEASE_U2U",
  277. "TSPI_SEND_U2U",
  278. "TSPI_COMPLETE_TRANSFER",
  279. "TSPI_LINEFORWARD_SPECIFIC",
  280. "TSPI_LINEFORWARD_NOSPECIFIC",
  281. "TSPI_DIAL_TRNASFEREDCALL",
  282. "TSPI_CALL_UNHOLD",
  283. "TSPI_CALL_HOLD",
  284. "TSPI_DELETE_CALL",
  285. "TSPI_CALL_DIVERT",
  286. "H450_PLACE_DIVERTEDCALL",
  287. "SWAP_REPLACEMENT_CALL",
  288. "DELETE_PRIMARY_CALL",
  289. "STOP_CTIDENTIFYRR_TIMER",
  290. "SEND_CTINITIATE_MESSAGE"
  291. };
  292. // return corresponding string
  293. return apszEventNameStrings[eventID];
  294. }
  295. PSTR
  296. H323TSPMessageToString(
  297. DWORD dwMessageType
  298. )
  299. {
  300. static PSTR msgstrings[] =
  301. {
  302. "SP_MSG_InitiateCall",
  303. "SP_MSG_AnswerCall",
  304. "SP_MSG_PrepareToAnswer",
  305. "SP_MSG_ProceedWithAnswer",
  306. "SP_MSG_ReadyToInitiate",
  307. "SP_MSG_ReadyToAnswer",
  308. "SP_MSG_FastConnectResponse",
  309. "SP_MSG_StartH245",
  310. "SP_MSG_ConnectComplete",
  311. "SP_MSG_H245PDU",
  312. "SP_MSG_MCLocationIdentify",
  313. "SP_MSG_Hold",
  314. "SP_MSG_H245Hold",
  315. "SP_MSG_ConferenceList",
  316. "SP_MSG_SendDTMFDigits",
  317. "SP_MSG_ReleaseCall",
  318. "SP_MSG_CallShutdown",
  319. "SP_MSG_H245Terminated",
  320. "SP_MSG_RASRegistration",
  321. "SP_MSG_RASRegistrationEvent",
  322. "SP_MSG_RASLocationRequest",
  323. "SP_MSG_RASLocationConfirm",
  324. "SP_MSG_RASBandwidthRequest",
  325. "SP_MSG_RASBandwidthConfirm"
  326. };
  327. return msgstrings[dwMessageType];
  328. }
  329. /*++
  330. Routine Description:
  331. Converts tapi call state to string.
  332. Arguments:
  333. dwCallState - Specifies value to convert.
  334. Return Values:
  335. Returns string describing value.
  336. --*/
  337. PSTR
  338. H323CallStateToString(
  339. DWORD dwCallState
  340. )
  341. {
  342. DWORD i;
  343. DWORD dwBitMask;
  344. static PSTR apszCallStateStrings[] = {
  345. "IDLE",
  346. "OFFERING",
  347. "ACCEPTED",
  348. "DIALTONE",
  349. "DIALING",
  350. "RINGBACK",
  351. "BUSY",
  352. "SPECIALINFO",
  353. "CONNECTED",
  354. "PROCEEDING",
  355. "ONHOLD",
  356. "CONFERENCED",
  357. "ONHOLDPENDCONF",
  358. "ONHOLDPENDTRANSFER",
  359. "DISCONNECTED",
  360. "UNKNOWN"
  361. };
  362. // keep shifting bit until the call state matchs the one specified
  363. for(i = 0, dwBitMask = 1; dwCallState != dwBitMask; i++, dwBitMask <<= 1)
  364. ;
  365. // return corresponding string
  366. return apszCallStateStrings[i];
  367. }
  368. PSTR
  369. H323AddressTypeToString(
  370. DWORD dwAddressType
  371. )
  372. /*++
  373. Routine Description:
  374. Converts TAPI address type to string.
  375. Arguments:
  376. dwAddressType - TAPI address type.
  377. Return Values:
  378. Returns string describing value.
  379. --*/
  380. {
  381. switch (dwAddressType) {
  382. case LINEADDRESSTYPE_PHONENUMBER:
  383. return "PHONENUMBER";
  384. case LINEADDRESSTYPE_SDP:
  385. return "SDP";
  386. case LINEADDRESSTYPE_EMAILNAME:
  387. return "EMAILNAME";
  388. case LINEADDRESSTYPE_DOMAINNAME:
  389. return "DOMAINNAME";
  390. case LINEADDRESSTYPE_IPADDRESS:
  391. return "IPADDRESS";
  392. default:
  393. return "unknown";
  394. }
  395. }