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.

367 lines
8.8 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. fputs( szLogStr, fp );
  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. PrintSearchList(
  201. IN PSEARCH_LIST pSearchList
  202. )
  203. {
  204. DWORD iter;
  205. DNSLOG_F1( "" );
  206. DNSLOG_F1( " DNS Search List :" );
  207. for ( iter = 0; iter < pSearchList->NameCount; iter++ )
  208. {
  209. DNSLOG_F3( " %s (Flags: 0x%X)",
  210. pSearchList->SearchNameArray[iter].pszName,
  211. pSearchList->SearchNameArray[iter].Flags );
  212. }
  213. DNSLOG_F1( "" );
  214. }
  215. VOID
  216. PrintServerInfo(
  217. IN PDNS_ADDR pServer
  218. )
  219. {
  220. // FIX6: DCR: PrintServerInfo not IP6 safe
  221. DNSLOG_F1( " IpAddress : " );
  222. PrintIpAddress( *(PIP4_ADDRESS)&pServer->SockaddrIn.sin_addr );
  223. DNSLOG_F2( " Priority : %d", pServer->Priority );
  224. DNSLOG_F2( " status : %d", pServer->Status );
  225. DNSLOG_F1( "" );
  226. }
  227. VOID
  228. PrintAdapterInfo(
  229. IN PDNS_ADAPTER pAdapter
  230. )
  231. {
  232. PDNS_ADDR_ARRAY pserverArray;
  233. DWORD iter;
  234. DNSLOG_F2( " %s", pAdapter->pszAdapterGuidName );
  235. DNSLOG_F1( " ----------------------------------------------------" );
  236. DNSLOG_F2( " pszAdapterDomain : %s", pAdapter->pszAdapterDomain );
  237. if ( pAdapter->pLocalAddrs )
  238. {
  239. PDNS_ADDR_ARRAY pIp = pAdapter->pLocalAddrs;
  240. DNSLOG_F1( " Adapter Ip Address(es) :" );
  241. for ( iter = 0; iter < pIp->AddrCount; iter++ )
  242. {
  243. PrintIpAddress( DnsAddr_GetIp4( &pIp->AddrArray[iter] ) );
  244. }
  245. }
  246. DNSLOG_F2( " Status : 0x%x", pAdapter->Status );
  247. DNSLOG_F2( " InfoFlags : 0x%x", pAdapter->InfoFlags );
  248. DNSLOG_F2( " ReturnFlags : 0x%x", pAdapter->RunFlags );
  249. DNSLOG_F1( "" );
  250. pserverArray = pAdapter->pDnsAddrs;
  251. if ( pserverArray )
  252. {
  253. for ( iter = 0; iter < pserverArray->AddrCount; iter++ )
  254. {
  255. DNSLOG_F1( " ------------------------" );
  256. DNSLOG_F2( " DNS Server Info (%d)", iter + 1 );
  257. DNSLOG_F1( " ------------------------" );
  258. PrintServerInfo( &pserverArray->AddrArray[iter] );
  259. }
  260. }
  261. DNSLOG_F1( "" );
  262. }
  263. VOID
  264. PrintNetworkInfoToLog(
  265. IN PDNS_NETINFO pNetworkInfo
  266. )
  267. {
  268. DWORD iter;
  269. if ( pNetworkInfo )
  270. {
  271. DNSLOG_F1( "Current network adapter information is :" );
  272. DNSLOG_F1( "" );
  273. DNSLOG_F2( " pNetworkInfo->ReturnFlags : 0x%x", pNetworkInfo->ReturnFlags );
  274. if ( pNetworkInfo->pSearchList )
  275. PrintSearchList( pNetworkInfo->pSearchList );
  276. DNSLOG_F2( " pNetworkInfo->AdapterCount : %d", pNetworkInfo->AdapterCount );
  277. DNSLOG_F2( " pNetworkInfo->TotalListSize : %d", pNetworkInfo->MaxAdapterCount );
  278. DNSLOG_F1( "" );
  279. for ( iter = 0; iter < pNetworkInfo->AdapterCount; iter++ )
  280. {
  281. DNSLOG_F1( " ----------------------------------------------------" );
  282. DNSLOG_F2( " Adapter Info (%d)", iter + 1 );
  283. DNSLOG_F1( "" );
  284. PrintAdapterInfo( &pNetworkInfo->AdapterArray[iter] );
  285. }
  286. }
  287. else
  288. {
  289. DNSLOG_F1( "Current network adapter information is empty" );
  290. DNSLOG_F1( "" );
  291. }
  292. }
  293. //
  294. // End of logit.c
  295. //