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.

419 lines
9.7 KiB

  1. /*****************************************\
  2. * Data Logging -- Debug only *
  3. \*****************************************/
  4. //
  5. // Need local header ONLY to allow precompiled header.
  6. // Nothing in this module depends on specific DNS resolver
  7. // definitions.
  8. //
  9. #include "local.h"
  10. //
  11. // NT Headers
  12. //
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. //
  17. // Windows Headers
  18. //
  19. #include <windows.h>
  20. #pragma hdrstop
  21. #include "logit.h"
  22. // #if DBG
  23. int LoggingMode;
  24. time_t long_time; // has to be in DS, assumed by time() funcs
  25. int LineCount;
  26. char * month[] =
  27. {
  28. "January",
  29. "February",
  30. "March",
  31. "April",
  32. "May",
  33. "June",
  34. "July",
  35. "August",
  36. "September",
  37. "October",
  38. "November",
  39. "December"
  40. } ;
  41. /*
  42. - LogInit
  43. -
  44. * Purpose:
  45. * Determines if logging is desired and if so, adds a header to log file.
  46. *
  47. * Parameters:
  48. *
  49. */
  50. void LogInit()
  51. {
  52. FILE *fp;
  53. struct tm *newtime;
  54. char am_pm[] = "a.m.";
  55. LoggingMode = 0;
  56. LineCount = 0;
  57. if ( fp = fopen( "dnsrslvr.log", "r+" ) )
  58. {
  59. LoggingMode = 1;
  60. fclose( fp );
  61. // Get time and date information
  62. long_time = time( NULL); /* Get time as long integer. */
  63. newtime = localtime( &long_time ); /* Convert to local time. */
  64. if( newtime->tm_hour > 12 ) /* Set up extension. */
  65. am_pm[0] = 'p';
  66. if( newtime->tm_hour > 12 ) /* Convert from 24-hour */
  67. newtime->tm_hour -= 12; /* to 12-hour clock. */
  68. if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
  69. newtime->tm_hour = 12;
  70. // Write out a header to file
  71. fp = fopen("dnsrslvr.log", "w" );
  72. if ( !fp )
  73. {
  74. return;
  75. }
  76. fprintf( fp, "Logging information for DNS Caching Resolver service\n" );
  77. fprintf( fp, "****************************************************\n" );
  78. fprintf( fp, "\tTime: %d:%02d %s\n\tDate: %s %d, 19%d\n",
  79. newtime->tm_hour, newtime->tm_min, am_pm,
  80. month[newtime->tm_mon], newtime->tm_mday,
  81. newtime->tm_year );
  82. fprintf( fp, "****************************************************\n\n" );
  83. fclose( fp );
  84. }
  85. }
  86. /*
  87. - LogIt
  88. -
  89. * Purpose:
  90. * Formats a string and prints it to a log file with handle hLog.
  91. *
  92. * Parameters:
  93. * LPSTR - Pointer to string to format
  94. * ... - variable argument list
  95. */
  96. #ifdef WIN32
  97. #define S16
  98. #else
  99. #define S16 static
  100. #endif
  101. void CDECL LogIt( char * lpszFormat, ... )
  102. {
  103. FILE *fp;
  104. #ifndef _ALPHA_
  105. va_list pArgs = NULL; // reference to quiet compiler
  106. #else
  107. va_list pArgs = {NULL,0};
  108. #endif
  109. S16 char szLogStr[1024];
  110. int i;
  111. if ( !LoggingMode )
  112. return;
  113. #ifdef WIN32 // parse parameters and insert in string
  114. va_start( pArgs, lpszFormat);
  115. vsprintf(szLogStr, lpszFormat, pArgs);
  116. va_end(pArgs);
  117. i = lstrlenA( szLogStr);
  118. #else // parsing doesn't work, just give them string.
  119. _fstrcpy( szLogStr, lpszFormat);
  120. i = _fstrlen( szLogStr);
  121. #endif
  122. szLogStr[i] = '\n';
  123. szLogStr[i+1] = '\0';
  124. if ( LineCount > 50000 )
  125. {
  126. fp = fopen( "dnsrslvr.log", "w" );
  127. LineCount = 0;
  128. }
  129. else
  130. {
  131. fp = fopen( "dnsrslvr.log", "a" );
  132. }
  133. if( fp )
  134. {
  135. fprintf( fp, szLogStr );
  136. LineCount++;
  137. fclose( fp );
  138. }
  139. }
  140. void LogTime()
  141. {
  142. struct tm *newtime;
  143. char am_pm[] = "a.m.";
  144. if ( !LoggingMode )
  145. return;
  146. // Get time and date information
  147. long_time = time( NULL); /* Get time as long integer. */
  148. newtime = localtime( &long_time ); /* Convert to local time. */
  149. if ( !newtime )
  150. return;
  151. if( newtime->tm_hour > 12 ) /* Set up extension. */
  152. am_pm[0] = 'p';
  153. if( newtime->tm_hour > 12 ) /* Convert from 24-hour */
  154. newtime->tm_hour -= 12; /* to 12-hour clock. */
  155. if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
  156. newtime->tm_hour = 12;
  157. // Write out a header to file
  158. LogIt( "DNS Caching Resolver service" );
  159. LogIt( "System Time Information" );
  160. LogIt( "****************************************************" );
  161. LogIt( "\tTime: %d:%02d %s\n\tDate: %s %d, 19%d",
  162. newtime->tm_hour, newtime->tm_min, am_pm,
  163. month[newtime->tm_mon], newtime->tm_mday,
  164. newtime->tm_year );
  165. LogIt( "****************************************************" );
  166. LogIt( "" );
  167. }
  168. DWORD LogIn( char * string )
  169. {
  170. LogIt( "%s", string );
  171. return GetTickCount();
  172. }
  173. void LogOut( char * string, DWORD InTime )
  174. {
  175. LogIt( "%s --- Duration: %ld milliseconds", string, GetTickCount() - InTime );
  176. }
  177. // #endif
  178. //
  179. // Special logging routines
  180. //
  181. //
  182. // ENHANCE: print routines here are really log routines
  183. // - they are really log routines
  184. // - should print generically so can be hooked to any print duty
  185. // - and have macros to log desired structure
  186. //
  187. VOID
  188. PrintIpAddress(
  189. IN DWORD dwIpAddress
  190. )
  191. {
  192. DNSLOG_F5(
  193. " %d.%d.%d.%d",
  194. ((BYTE *) &dwIpAddress)[0],
  195. ((BYTE *) &dwIpAddress)[1],
  196. ((BYTE *) &dwIpAddress)[2],
  197. ((BYTE *) &dwIpAddress)[3] );
  198. }
  199. VOID
  200. PrintAddressInfo(
  201. IN DNS_ADDRESS_INFO AddressInfo
  202. )
  203. {
  204. DNSLOG_F1( " ipAddress : " );
  205. PrintIpAddress( AddressInfo.ipAddress );
  206. DNSLOG_F1( " subnetMask : " );
  207. PrintIpAddress( AddressInfo.subnetMask );
  208. DNSLOG_F1( "" );
  209. }
  210. VOID
  211. PrintSearchList(
  212. IN PSEARCH_LIST pSearchList
  213. )
  214. {
  215. DWORD iter;
  216. DNSLOG_F1( "" );
  217. DNSLOG_F1( " DNS Search List :" );
  218. for ( iter = 0; iter < pSearchList->cNameCount; iter++ )
  219. {
  220. DNSLOG_F3( " %s (Flags: 0x%X)",
  221. pSearchList->SearchNameArray[iter].pszName,
  222. pSearchList->SearchNameArray[iter].Flags );
  223. }
  224. DNSLOG_F1( "" );
  225. if ( pSearchList->pszDomainOrZoneName )
  226. DNSLOG_F2( " Primary domain name : %s",
  227. pSearchList->pszDomainOrZoneName );
  228. DNSLOG_F1( "" );
  229. }
  230. VOID
  231. PrintServerInfo(
  232. IN DNS_SERVER_INFO ServerInfo
  233. )
  234. {
  235. DNSLOG_F1( " IpAddress : " );
  236. PrintIpAddress( ServerInfo.IpAddress );
  237. DNSLOG_F2( " Priority : %d", ServerInfo.Priority );
  238. DNSLOG_F2( " status : %d", ServerInfo.Status );
  239. DNSLOG_F1( "" );
  240. }
  241. VOID
  242. PrintAdapterInfo(
  243. IN PDNS_ADAPTER pAdapter
  244. )
  245. {
  246. DWORD iter;
  247. DNSLOG_F2( " %s", pAdapter->pszAdapterGuidName );
  248. DNSLOG_F1( " ----------------------------------------------------" );
  249. DNSLOG_F2( " pszAdapterDomain : %s", pAdapter->pszAdapterDomain );
  250. if ( pAdapter->pAdapterIPAddresses )
  251. {
  252. PIP_ARRAY pIp = pAdapter->pAdapterIPAddresses;
  253. DNSLOG_F1( " Adapter Ip Address(es) :" );
  254. for ( iter = 0; iter < pIp->AddrCount; iter++ )
  255. {
  256. PrintIpAddress( pIp->AddrArray[iter] );
  257. }
  258. }
  259. if ( pAdapter->pAdapterIPSubnetMasks )
  260. {
  261. PIP_ARRAY pMask = pAdapter->pAdapterIPSubnetMasks;
  262. DNSLOG_F1( " Adapter Ip Subnet Mask(s) :" );
  263. for ( iter = 0; iter < pMask->AddrCount; iter++ )
  264. {
  265. PrintIpAddress( pMask->AddrArray[iter] );
  266. }
  267. }
  268. DNSLOG_F2( " Status : 0x%x", pAdapter->Status );
  269. DNSLOG_F2( " InfoFlags : 0x%x", pAdapter->InfoFlags );
  270. DNSLOG_F2( " ReturnFlags : 0x%x", pAdapter->ReturnFlags );
  271. DNSLOG_F1( " IpLastSend : " );
  272. PrintIpAddress( pAdapter->IpLastSend );
  273. DNSLOG_F2( " cServerCount : %d", pAdapter->cServerCount );
  274. DNSLOG_F2( " cTotalListSize : %d", pAdapter->cTotalListSize );
  275. DNSLOG_F1( "" );
  276. for ( iter = 0; iter < pAdapter->cServerCount; iter++ )
  277. {
  278. DNSLOG_F1( " ------------------------" );
  279. DNSLOG_F2( " DNS Server Info (%d)", iter + 1 );
  280. DNSLOG_F1( " ------------------------" );
  281. PrintServerInfo( pAdapter->ServerArray[iter] );
  282. }
  283. DNSLOG_F1( "" );
  284. }
  285. VOID
  286. PrintNetworkInfoToLog(
  287. IN PDNS_NETINFO pNetworkInfo
  288. )
  289. {
  290. DWORD iter;
  291. if ( pNetworkInfo )
  292. {
  293. DNSLOG_F1( "Current network adapter information is :" );
  294. DNSLOG_F1( "" );
  295. DNSLOG_F2( " pNetworkInfo->ReturnFlags : 0x%x", pNetworkInfo->ReturnFlags );
  296. if ( pNetworkInfo->pSearchList )
  297. PrintSearchList( pNetworkInfo->pSearchList );
  298. DNSLOG_F2( " pNetworkInfo->cAdapterCount : %d", pNetworkInfo->cAdapterCount );
  299. DNSLOG_F2( " pNetworkInfo->cTotalListSize : %d", pNetworkInfo->cTotalListSize );
  300. DNSLOG_F1( "" );
  301. for ( iter = 0; iter < pNetworkInfo->cAdapterCount; iter++ )
  302. {
  303. DNSLOG_F1( " ----------------------------------------------------" );
  304. DNSLOG_F2( " Adapter Info (%d)", iter + 1 );
  305. DNSLOG_F1( "" );
  306. PrintAdapterInfo( pNetworkInfo->AdapterArray[iter] );
  307. }
  308. }
  309. else
  310. {
  311. DNSLOG_F1( "Current network adapter information is empty" );
  312. DNSLOG_F1( "" );
  313. }
  314. }
  315. VOID
  316. PrintIpAddressListToLog (
  317. IN PDNS_ADDRESS_INFO AddressInfoList,
  318. IN DWORD AddressInfoListCount
  319. )
  320. {
  321. DWORD iter;
  322. if ( AddressInfoList && AddressInfoListCount )
  323. {
  324. DNSLOG_F1( "Current IP address list is :" );
  325. for ( iter = 0; iter < AddressInfoListCount; iter++ )
  326. {
  327. PrintAddressInfo( AddressInfoList[iter] );
  328. }
  329. DNSLOG_F1( "" );
  330. }
  331. else
  332. {
  333. DNSLOG_F1( "Current IP address list is empty" );
  334. DNSLOG_F1( "" );
  335. }
  336. }
  337. //
  338. // End of logit.c
  339. //