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.

388 lines
9.1 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. spdutil.cpp
  7. FILE HISTORY:
  8. */
  9. #include "stdafx.h"
  10. #include "spdutil.h"
  11. #include "objplus.h"
  12. #include "ipaddres.h"
  13. #include "spddb.h"
  14. #include "server.h"
  15. extern CHashTable g_HashTable;
  16. const DWORD IPSM_PROTOCOL_TCP = 6;
  17. const DWORD IPSM_PROTOCOL_UDP = 17;
  18. const TCHAR c_szSingleAddressMask[] = _T("255.255.255.255");
  19. const CHAR c_EqualMatchType[] = "=";
  20. const CHAR c_RangeMatchType[] = "IN";
  21. const ProtocolStringMap c_ProtocolStringMap[] =
  22. {
  23. {0, IDS_PROTOCOL_ANY},
  24. {1, IDS_PROTOCOL_ICMP},
  25. {2, IDS_PROTOCOL_IGMP},
  26. {3, IDS_PROTOCOL_GGP},
  27. {6, IDS_PROTOCOL_TCP},
  28. {8, IDS_PROTOCOL_EGP},
  29. {12, IDS_PROTOCOL_PUP},
  30. {17, IDS_PROTOCOL_UDP},
  31. {20, IDS_PROTOCOL_HMP},
  32. {22, IDS_PROTOCOL_XNS_IDP},
  33. {27, IDS_PROTOCOL_RDP},
  34. {66, IDS_PROTOCOL_RVD}
  35. };
  36. const int c_nProtocols = DimensionOf(c_ProtocolStringMap);
  37. // The frequency table into which the channel is the index
  38. const ULONG g_ulFreqTable[] = {2412000, 2417000, 2422000, 2427000, 2432000,
  39. 2437000, 2442000, 2447000, 2452000, 2457000,
  40. 2462000, 2467000, 2472000, 2484000};
  41. ULONG RevertDwordBytes(DWORD dw)
  42. {
  43. ULONG ulRet;
  44. ulRet = dw >> 24;
  45. ulRet += (dw & 0xFF0000) >> 8;
  46. ulRet += (dw & 0x00FF00) << 8;
  47. ulRet += (dw & 0x0000FF) << 24;
  48. return ulRet;
  49. }
  50. void ProtocolToString
  51. (
  52. BYTE protocol,
  53. CString * pst
  54. )
  55. {
  56. BOOL fFound = FALSE;
  57. for (int i = 0; i < DimensionOf(c_ProtocolStringMap); i++)
  58. {
  59. if (c_ProtocolStringMap[i].dwProtocol == protocol)
  60. {
  61. pst->LoadString(c_ProtocolStringMap[i].nStringID);
  62. fFound = TRUE;
  63. }
  64. }
  65. if (!fFound)
  66. {
  67. pst->Format(IDS_OTHER_PROTO, protocol);
  68. }
  69. }
  70. void BoolToString
  71. (
  72. BOOL bl,
  73. CString * pst
  74. )
  75. {
  76. if (bl)
  77. pst->LoadString (IDS_YES);
  78. else
  79. pst->LoadString (IDS_NO);
  80. }
  81. void IpToString(ULONG ulIp, CString *pst)
  82. {
  83. ULONG ul;
  84. CIpAddress ipAddr;
  85. ul = RevertDwordBytes(ulIp);
  86. ipAddr = ul;
  87. *pst = (CString) ipAddr;
  88. }
  89. void NumToString(DWORD number, CString * pst)
  90. {
  91. pst->Format(_T("%d"), number);
  92. return;
  93. }
  94. void FileTimeToString(FILETIME logDataTime, CString *pst)
  95. {
  96. SYSTEMTIME timeFields;
  97. FileTimeToSystemTime ( (PFILETIME)&logDataTime, &timeFields);
  98. pst->Format (_T("%2d/%2d/%4d %02d:%02d:%02d:%03d"),
  99. timeFields.wMonth,
  100. timeFields.wDay,
  101. timeFields.wYear,
  102. timeFields.wHour,
  103. timeFields.wMinute,
  104. timeFields.wSecond,
  105. timeFields.wMilliseconds
  106. );
  107. return;
  108. }
  109. VOID SSIDToString(NDIS_802_11_SSID Ssid, CString &SsidStr)
  110. {
  111. LPTSTR pStr = SsidStr.GetBuffer(Ssid.SsidLength + 1);
  112. MultiByteToWideChar( CP_ACP,
  113. MB_PRECOMPOSED,
  114. (LPCSTR)Ssid.Ssid,
  115. (int)Ssid.SsidLength,
  116. pStr,
  117. (int)Ssid.SsidLength);
  118. pStr[Ssid.SsidLength] = _T('\0');
  119. SsidStr.ReleaseBuffer();
  120. }
  121. VOID GuidToString (LPWSTR guid, CString &guidStr)
  122. {
  123. guidStr = guid;
  124. }
  125. VOID MacToString (NDIS_802_11_MAC_ADDRESS macAddress, CString &macAddrStr)
  126. {
  127. LPTSTR pStr = macAddrStr.GetBuffer(18);
  128. UINT i;
  129. for (i = 0; i < 6; i++)
  130. {
  131. wsprintf(pStr, _T("%02X-"), macAddress[i]);
  132. pStr += 3;
  133. }
  134. *(pStr-1) = _T('\0');
  135. }
  136. DWORD CategoryToString(DWORD dwCategory, CString &csCategory)
  137. {
  138. BOOL bLoaded = FALSE;
  139. DWORD dwErr = ERROR_SUCCESS;
  140. switch (dwCategory)
  141. {
  142. case DBLOG_CATEG_INFO:
  143. bLoaded = csCategory.LoadString(IDS_LOGDATA_TYPE_INFORMATION);
  144. Assert(TRUE == bLoaded);
  145. break;
  146. case DBLOG_CATEG_WARN:
  147. bLoaded = csCategory.LoadString(IDS_LOGDATA_TYPE_WARNING);
  148. Assert(TRUE == bLoaded);
  149. break;
  150. case DBLOG_CATEG_ERR:
  151. bLoaded = csCategory.LoadString(IDS_LOGDATA_TYPE_ERROR);
  152. Assert(TRUE == bLoaded);
  153. break;
  154. case DBLOG_CATEG_PACKET:
  155. bLoaded = csCategory.LoadString(IDS_LOGDATA_TYPE_PACKET);
  156. Assert(TRUE == bLoaded);
  157. break;
  158. default:
  159. bLoaded = csCategory.LoadString(IDS_LOGDATA_TYPE_UNKNOWN);
  160. Assert(TRUE == bLoaded);
  161. dwErr = ERROR_BAD_FORMAT;
  162. break;
  163. }
  164. return dwErr;
  165. }
  166. DWORD PrivacyToString(ULONG ulPrivacy, CString *pcs)
  167. {
  168. DWORD dwErr = ERROR_SUCCESS;
  169. BOOL bLoaded = FALSE;
  170. switch(ulPrivacy)
  171. {
  172. case 0:
  173. bLoaded = pcs->LoadString(IDS_APDATA_PRIVACY_DISABLED);
  174. Assert(TRUE == bLoaded);
  175. break;
  176. case 1:
  177. bLoaded = pcs->LoadString(IDS_APDATA_PRIVACY_ENABLED);
  178. Assert(TRUE == bLoaded);
  179. break;
  180. default:
  181. bLoaded = pcs->LoadString(IDS_APDATA_PRIVACY_UNKNOWN);
  182. Assert(TRUE == bLoaded);
  183. dwErr = ERROR_BAD_FORMAT;
  184. break;
  185. }
  186. return dwErr;
  187. }
  188. DWORD InfraToString(NDIS_802_11_NETWORK_INFRASTRUCTURE InfraMode, CString *pcs)
  189. {
  190. DWORD dwErr = ERROR_SUCCESS;
  191. BOOL bLoaded = FALSE;
  192. switch(InfraMode)
  193. {
  194. case Ndis802_11IBSS:
  195. bLoaded = pcs->LoadString(IDS_APDATA_INFRA_PEER);
  196. Assert(TRUE == bLoaded);
  197. break;
  198. case Ndis802_11Infrastructure:
  199. bLoaded = pcs->LoadString(IDS_APDATA_INFRA_INFRA);
  200. Assert(TRUE == bLoaded);
  201. break;
  202. default:
  203. bLoaded = pcs->LoadString(IDS_APDATA_INFRA_UNKNOWN);
  204. Assert(TRUE == bLoaded);
  205. dwErr = ERROR_BAD_FORMAT;
  206. break;
  207. }
  208. return dwErr;
  209. }
  210. DWORD RateToString(NDIS_802_11_RATES Rates, CString *pcs)
  211. {
  212. int i = 0;
  213. UCHAR ucMask = 0x7F;
  214. float fMul = 0.5;
  215. float fVal = 0.0;
  216. DWORD dwErr = ERROR_SUCCESS;
  217. CString csTemp;
  218. if (0 == Rates[i])
  219. goto done;
  220. fVal = fMul * (float)(Rates[i] & ucMask);
  221. csTemp.Format(_T("%.2f"), fVal);
  222. (*pcs) += csTemp;
  223. i++;
  224. while (Rates[i] != 0)
  225. {
  226. (*pcs) += _T(", ");
  227. fVal = fMul * (float)(Rates[i] & ucMask);
  228. csTemp.Format(_T("%.2f"), fVal);
  229. (*pcs) += csTemp;
  230. i++;
  231. }
  232. done:
  233. return dwErr;
  234. }
  235. DWORD ChannelToString(NDIS_802_11_CONFIGURATION *pConfig, CString *pcs)
  236. {
  237. DWORD dwErr = ERROR_SUCCESS;
  238. int nChannel = 0;
  239. while ( (nChannel < ARRAYLEN(g_ulFreqTable)) &&
  240. (g_ulFreqTable[nChannel] != pConfig->DSConfig) )
  241. nChannel++;
  242. //channels number from 1 instead of 0
  243. nChannel++;
  244. if (nChannel <= ARRAYLEN(g_ulFreqTable))
  245. pcs->Format(_T("%lu [%d]"), pConfig->DSConfig, nChannel);
  246. else
  247. pcs->Format(_T("%ls [?]"), pConfig->DSConfig);
  248. return dwErr;
  249. }
  250. DWORD ComponentIDToString(DWORD dwCompID, CString &csComponent)
  251. {
  252. BOOL bLoaded = FALSE;
  253. DWORD dwErr = ERROR_SUCCESS;
  254. switch (dwCompID)
  255. {
  256. case DBLOG_COMPID_WZCSVC:
  257. bLoaded = csComponent.LoadString(IDS_LOGDATA_SOURCE_WZCSVC);
  258. Assert(TRUE == bLoaded);
  259. break;
  260. case DBLOG_COMPID_EAPOL:
  261. bLoaded = csComponent.LoadString(IDS_LOGDATA_SOURCE_EAPOL);
  262. Assert(TRUE == bLoaded);
  263. break;
  264. default:
  265. bLoaded = csComponent.LoadString(IDS_LOGDATA_SOURCE_UNKNOWN);
  266. Assert(TRUE == bLoaded);
  267. dwErr = ERROR_BAD_FORMAT;
  268. break;
  269. }
  270. return dwErr;
  271. }
  272. DWORD CopyAndStripNULL(LPTSTR lptstrDest, LPTSTR lptstrSrc, DWORD dwLen)
  273. {
  274. DWORD dwErr = ERROR_SUCCESS;
  275. if (0 == dwLen)
  276. {
  277. lptstrDest[dwLen]=_T('\0');
  278. goto exit;
  279. }
  280. //Length sent is length of string in bytes + two for NULL terminator
  281. dwLen /= sizeof(TCHAR);
  282. dwLen--;
  283. lptstrDest[dwLen]=_T('\0');
  284. while (0 != dwLen--)
  285. {
  286. if (_T('\0') != lptstrSrc[dwLen])
  287. lptstrDest[dwLen] = lptstrSrc[dwLen];
  288. else
  289. lptstrDest[dwLen] = _T(' ');
  290. }
  291. exit:
  292. return dwErr;
  293. }
  294. BOOL operator==(const FILETIME& ftLHS, const FILETIME& ftRHS)
  295. {
  296. BOOL bEqual = FALSE;
  297. const ULONGLONG ullLHS = *(const UNALIGNED ULONGLONG * UNALIGNED) &ftLHS;
  298. const ULONGLONG ullRHS = *(const UNALIGNED ULONGLONG * UNALIGNED) &ftRHS;
  299. LONGLONG llDiff = 0;
  300. llDiff = ullRHS - ullLHS;
  301. if (0 == llDiff)
  302. bEqual = TRUE;
  303. return bEqual;
  304. }
  305. BOOL operator!=(const FILETIME& ftLHS, const FILETIME& ftRHS)
  306. {
  307. BOOL bNotEqual = FALSE;
  308. const ULONGLONG ullLHS = *(const UNALIGNED ULONGLONG * UNALIGNED) &ftLHS;
  309. const ULONGLONG ullRHS = *(const UNALIGNED ULONGLONG * UNALIGNED) &ftRHS;
  310. LONGLONG llDiff = 0;
  311. llDiff = ullRHS - ullLHS;
  312. if (0 != llDiff)
  313. bNotEqual = TRUE;
  314. return bNotEqual;
  315. }