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.

404 lines
13 KiB

  1. //============================================================================
  2. // Copyright (C) Microsoft Corporation, 1996 - 1999
  3. //
  4. // File: mibutil.cpp
  5. //
  6. // History:
  7. // 7/22/97 Kenn M. Takara Created.
  8. //
  9. // Implements IPX-related utility functions.
  10. //
  11. //============================================================================
  12. #include "stdafx.h"
  13. #include "ipxutil.h"
  14. #include "tregkey.h" // RegKey class
  15. #include "reg.h" // Router registry utility functions
  16. #include "format.h" // FormatXXX functions
  17. #include "strmap.h" // MapDWORDToCString
  18. #include "ipxconst.h" // IPX constants
  19. #include "routprot.h"
  20. #include "ipxrtdef.h"
  21. #include "ctype.h" // for _totlower
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. static CString s_stIfTypeClient;
  28. static CString s_stIfTypeHomeRouter;
  29. static CString s_stIfTypeFullRouter;
  30. static CString s_stIfTypeDedicated;
  31. static CString s_stIfTypeInternal;
  32. static CString s_stIfTypeLoopback;
  33. static CString s_stIfTypeNotAvailable;
  34. const CStringMapEntry s_IpxTypeMap[] =
  35. {
  36. { ROUTER_IF_TYPE_CLIENT, &s_stIfTypeClient, IDS_IPXIFTYPE_CLIENT },
  37. { ROUTER_IF_TYPE_HOME_ROUTER, &s_stIfTypeHomeRouter, IDS_IPXIFTYPE_HOME_ROUTER },
  38. { ROUTER_IF_TYPE_FULL_ROUTER, &s_stIfTypeFullRouter, IDS_IPXIFTYPE_FULL_ROUTER },
  39. { ROUTER_IF_TYPE_DEDICATED, &s_stIfTypeDedicated, IDS_IPXIFTYPE_DEDICATED },
  40. { ROUTER_IF_TYPE_INTERNAL, &s_stIfTypeInternal, IDS_IPXIFTYPE_INTERNAL },
  41. { ROUTER_IF_TYPE_LOOPBACK, &s_stIfTypeLoopback, IDS_IPXIFTYPE_LOOPBACK },
  42. { -1, &s_stIfTypeNotAvailable, IDS_IPXIFTYPE_NOTAVAILABLE },
  43. };
  44. static CString s_stAdminStateDisabled;
  45. static CString s_stAdminStateEnabled;
  46. static CString s_stAdminStateUnknown;
  47. const CStringMapEntry s_IpxAdminStateMap[] =
  48. {
  49. { ADMIN_STATE_DISABLED, &s_stAdminStateDisabled, IDS_IPXADMINSTATE_DISABLED },
  50. { ADMIN_STATE_ENABLED, &s_stAdminStateEnabled, IDS_IPXADMINSTATE_ENABLED },
  51. { -1, &s_stAdminStateUnknown, IDS_IPXADMINSTATE_UNKNOWN },
  52. };
  53. static CString s_stOperStateDown;
  54. static CString s_stOperStateUp;
  55. static CString s_stOperStateSleeping;
  56. static CString s_stOperStateUnknown;
  57. const CStringMapEntry s_IpxOperStateMap[] =
  58. {
  59. { OPER_STATE_DOWN, &s_stOperStateDown, IDS_IPXOPERSTATE_DOWN },
  60. { OPER_STATE_UP, &s_stOperStateUp, IDS_IPXOPERSTATE_UP },
  61. { OPER_STATE_SLEEPING, &s_stOperStateSleeping, IDS_IPXOPERSTATE_SLEEPING },
  62. { -1, &s_stOperStateUnknown, IDS_IPXOPERSTATE_UNKNOWN },
  63. };
  64. static CString s_stProtocolLocal;
  65. static CString s_stProtocolStatic;
  66. static CString s_stProtocolRip;
  67. static CString s_stProtocolSap;
  68. static CString s_stProtocolUnknown;
  69. const CStringMapEntry s_IpxProtocolMap[] =
  70. {
  71. { IPX_PROTOCOL_LOCAL, &s_stProtocolLocal, IDS_IPX_PROTOCOL_LOCAL },
  72. { IPX_PROTOCOL_STATIC, &s_stProtocolStatic,IDS_IPX_PROTOCOL_STATIC },
  73. { IPX_PROTOCOL_RIP, &s_stProtocolRip, IDS_IPX_PROTOCOL_RIP },
  74. { IPX_PROTOCOL_SAP, &s_stProtocolSap, IDS_IPX_PROTOCOL_SAP },
  75. { -1, &s_stProtocolUnknown, IDS_IPX_PROTOCOL_UNKNOWN },
  76. };
  77. static CString s_stRouteNotesNone;
  78. static CString s_stRouteNotesWanRoute;
  79. static CString s_stRouteNotesAdvertise;
  80. const CStringMapEntry s_IpxRouteNotesMap[] =
  81. {
  82. { 0, &s_stRouteNotesNone, IDS_IPX_ROUTE_NOTES_NONE },
  83. { GLOBAL_WAN_ROUTE, &s_stRouteNotesWanRoute, IDS_IPX_ROUTE_NOTES_WAN },
  84. { DO_NOT_ADVERTISE_ROUTE, &s_stRouteNotesAdvertise, IDS_IPX_DONT_ADVERTISE_ROUTE },
  85. { -1, &s_stRouteNotesNone, IDS_IPX_ROUTE_NOTES_NONE },
  86. };
  87. static CString s_stDeliveredEnabled;
  88. static CString s_stDeliveredDisabled;
  89. static CString s_stDeliveredNetBIOSOnly;
  90. static CString s_stDeliveredOperStateUp;
  91. static CString s_stDeliveredUnknown;
  92. const CStringMapEntry s_IpxDeliveredBroadcastsMap[] =
  93. {
  94. { ADMIN_STATE_ENABLED, &s_stDeliveredEnabled, IDS_DELIVERED_BROADCASTS_ENABLED },
  95. { ADMIN_STATE_DISABLED, &s_stDeliveredDisabled, IDS_DELIVERED_BROADCASTS_DISABLED },
  96. { ADMIN_STATE_ENABLED_ONLY_FOR_NETBIOS_STATIC_ROUTING, &s_stDeliveredNetBIOSOnly, IDS_DELIVERED_BROADCASTS_NETBIOS_ONLY },
  97. { ADMIN_STATE_ENABLED_ONLY_FOR_OPER_STATE_UP, &s_stDeliveredOperStateUp, IDS_DELIVERED_BROADCASTS_OPER_STATE_UP },
  98. { -1, &s_stDeliveredUnknown, IDS_DELIVERED_BROADCASTS_UNKNOWN },
  99. };
  100. static CString s_stStandardUpdate;
  101. static CString s_stNoUpdate;
  102. static CString s_stAutoStaticUpdate;
  103. static CString s_stUnknownUpdate;
  104. const CStringMapEntry s_IpxRipSapUpdateModeMap[] =
  105. {
  106. { IPX_STANDARD_UPDATE, &s_stStandardUpdate, IDS_UPDATEMODE_STANDARD },
  107. { IPX_NO_UPDATE, &s_stNoUpdate, IDS_UPDATEMODE_NONE },
  108. { IPX_AUTO_STATIC_UPDATE, &s_stAutoStaticUpdate,IDS_UPDATEMODE_AUTOSTATIC },
  109. { -1, &s_stUnknownUpdate, IDS_UPDATEMODE_UNKNOWN },
  110. };
  111. static CString s_stRouteFilterDeny;
  112. static CString s_stRouteFilterPermit;
  113. static CString s_stRouteFilterUnknown;
  114. const CStringMapEntry s_IpxRouteFilterMap[] =
  115. {
  116. { IPX_ROUTE_FILTER_DENY, &s_stRouteFilterDeny, IDS_ROUTEFILTER_DENY },
  117. { IPX_ROUTE_FILTER_PERMIT, &s_stRouteFilterPermit, IDS_ROUTEFILTER_PERMIT },
  118. { -1, &s_stRouteFilterUnknown, IDS_ROUTEFILTER_UNKNOWN },
  119. };
  120. static CString s_stServiceFilterDeny;
  121. static CString s_stServiceFilterPermit;
  122. static CString s_stServiceFilterUnknown;
  123. const CStringMapEntry s_IpxServiceFilterMap[] =
  124. {
  125. { IPX_SERVICE_FILTER_DENY, &s_stServiceFilterDeny, IDS_SERVICEFILTER_DENY },
  126. { IPX_SERVICE_FILTER_PERMIT, &s_stServiceFilterPermit, IDS_SERVICEFILTER_PERMIT },
  127. { -1, &s_stServiceFilterUnknown, IDS_SERVICEFILTER_UNKNOWN },
  128. };
  129. /*!--------------------------------------------------------------------------
  130. IpxDeliveredBroadcastsToCString
  131. -
  132. Author: KennT
  133. ---------------------------------------------------------------------------*/
  134. CString& IpxDeliveredBroadcastsToCString(DWORD dwDelivered)
  135. {
  136. return MapDWORDToCString(dwDelivered, s_IpxDeliveredBroadcastsMap);
  137. }
  138. /*!--------------------------------------------------------------------------
  139. IpxAcceptBroadcastsToCString
  140. -
  141. Author: KennT
  142. ---------------------------------------------------------------------------*/
  143. CString& IpxAcceptBroadcastsToCString(DWORD dwAccept)
  144. {
  145. // Uses the same map as the admin state
  146. return MapDWORDToCString(dwAccept, s_IpxAdminStateMap);
  147. }
  148. /*!--------------------------------------------------------------------------
  149. IpxTypeToCString
  150. -
  151. Author: KennT
  152. ---------------------------------------------------------------------------*/
  153. CString& IpxTypeToCString(DWORD dwType)
  154. {
  155. return MapDWORDToCString(dwType, s_IpxTypeMap);
  156. }
  157. /*!--------------------------------------------------------------------------
  158. IpxAdminStateToCString
  159. -
  160. Author: KennT
  161. ---------------------------------------------------------------------------*/
  162. CString& IpxAdminStateToCString(DWORD dwAdminState)
  163. {
  164. return MapDWORDToCString(dwAdminState, s_IpxAdminStateMap);
  165. }
  166. /*!--------------------------------------------------------------------------
  167. IpxOperStateToCString
  168. -
  169. Author: KennT
  170. ---------------------------------------------------------------------------*/
  171. CString& IpxOperStateToCString(DWORD dwOperState)
  172. {
  173. return MapDWORDToCString(dwOperState, s_IpxOperStateMap);
  174. }
  175. /*!--------------------------------------------------------------------------
  176. IpxProtocolToCString
  177. -
  178. Author: KennT
  179. ---------------------------------------------------------------------------*/
  180. CString& IpxProtocolToCString(DWORD dwProtocol)
  181. {
  182. return MapDWORDToCString(dwProtocol, s_IpxProtocolMap);
  183. }
  184. /*!--------------------------------------------------------------------------
  185. IpxRouteNotesToCString
  186. -
  187. Author: KennT
  188. ---------------------------------------------------------------------------*/
  189. CString IpxRouteNotesToCString(DWORD dwFlags)
  190. {
  191. return MapDWORDToCString(dwFlags, s_IpxRouteNotesMap);
  192. }
  193. /*!--------------------------------------------------------------------------
  194. RipSapUpdateModeToCString
  195. -
  196. Author: KennT
  197. ---------------------------------------------------------------------------*/
  198. CString& RipSapUpdateModeToCString(DWORD dwUpdateMode)
  199. {
  200. return MapDWORDToCString(dwUpdateMode, s_IpxRipSapUpdateModeMap);
  201. }
  202. CString& RouteFilterActionToCString(DWORD dwFilterAction)
  203. {
  204. return MapDWORDToCString(dwFilterAction, s_IpxRouteFilterMap);
  205. }
  206. CString& ServiceFilterActionToCString(DWORD dwFilterAction)
  207. {
  208. return MapDWORDToCString(dwFilterAction, s_IpxServiceFilterMap);
  209. }
  210. /*!--------------------------------------------------------------------------
  211. FormatBytes
  212. -
  213. Author: KennT
  214. ---------------------------------------------------------------------------*/
  215. void FormatBytes(LPTSTR pszDestBuffer, ULONG cchBuffer,
  216. UCHAR *pchBytes, ULONG cchBytes)
  217. {
  218. TCHAR * psz = pszDestBuffer;
  219. int i = 0;
  220. TCHAR szTemp[4];
  221. while ((psz < (pszDestBuffer + cchBuffer - 1)) && cchBytes)
  222. {
  223. wsprintf(szTemp, _T("%.2x"), pchBytes[i]);
  224. StrCpy(psz, szTemp);
  225. i++;
  226. psz += 2;
  227. cchBytes--;
  228. }
  229. pszDestBuffer[cchBuffer-1] = 0;
  230. }
  231. /*!--------------------------------------------------------------------------
  232. FormatIpxNetworkNumber
  233. -
  234. Author: KennT
  235. ---------------------------------------------------------------------------*/
  236. void FormatIpxNetworkNumber(LPTSTR pszNetwork, ULONG cchMax,
  237. UCHAR *pchNetwork, ULONG cchNetwork)
  238. {
  239. Assert(cchNetwork == 4);
  240. FormatBytes(pszNetwork, cchMax, pchNetwork, cchNetwork);
  241. }
  242. /*!--------------------------------------------------------------------------
  243. FormatMACAddress
  244. -
  245. Author: KennT
  246. ---------------------------------------------------------------------------*/
  247. void FormatMACAddress(LPTSTR pszMacAddress, ULONG cchMax,
  248. UCHAR *pchMac, ULONG cchMac)
  249. {
  250. Assert(cchMac == 6);
  251. FormatBytes(pszMacAddress, cchMax, pchMac, cchMac);
  252. }
  253. /*!--------------------------------------------------------------------------
  254. ConvertCharacter
  255. -
  256. Author: KennT
  257. ---------------------------------------------------------------------------*/
  258. BYTE ConvertCharacter(TCHAR ch)
  259. {
  260. BYTE value = 0;
  261. switch (_totlower(ch))
  262. {
  263. case _T('0'): value = 0; break;
  264. case _T('1'): value = 1; break;
  265. case _T('2'): value = 2; break;
  266. case _T('3'): value = 3; break;
  267. case _T('4'): value = 4; break;
  268. case _T('5'): value = 5; break;
  269. case _T('6'): value = 6; break;
  270. case _T('7'): value = 7; break;
  271. case _T('8'): value = 8; break;
  272. case _T('9'): value = 9; break;
  273. case _T('a'): value = 10; break;
  274. case _T('b'): value = 11; break;
  275. case _T('c'): value = 12; break;
  276. case _T('d'): value = 13; break;
  277. case _T('e'): value = 14; break;
  278. case _T('f'): value = 15; break;
  279. }
  280. return value;
  281. }
  282. /*!--------------------------------------------------------------------------
  283. ConvertToBytes
  284. -
  285. Author: KennT
  286. ---------------------------------------------------------------------------*/
  287. void ConvertToBytes(LPCTSTR pszBytes, BYTE *pchDest, UINT cchDest)
  288. {
  289. const TCHAR * psz;
  290. BYTE * pDest;
  291. BYTE chTemp;
  292. ::ZeroMemory(pchDest, cchDest);
  293. for (psz=pszBytes,pDest=pchDest; *psz && cchDest; psz++, pDest++, cchDest--)
  294. {
  295. // Look at the first character
  296. *pDest = ConvertCharacter(*psz) << 4;
  297. if (*(psz+1))
  298. {
  299. *pDest |= ConvertCharacter(*(psz+1));
  300. *psz++;
  301. }
  302. }
  303. }
  304. /*!--------------------------------------------------------------------------
  305. ConvertMACAddressToBytes
  306. -
  307. Author: KennT
  308. ---------------------------------------------------------------------------*/
  309. void ConvertMACAddressToBytes(LPCTSTR pszMacAddress,
  310. BYTE * pchDest,
  311. UINT cchDest)
  312. {
  313. Assert(cchDest == 6);
  314. ConvertToBytes(pszMacAddress, pchDest, cchDest);
  315. }
  316. /*!--------------------------------------------------------------------------
  317. ConvertNetworkNumberToBytes
  318. -
  319. Author: KennT
  320. ---------------------------------------------------------------------------*/
  321. void ConvertNetworkNumberToBytes(LPCTSTR pszNetwork,
  322. BYTE * pchDest,
  323. UINT cchDest)
  324. {
  325. Assert(cchDest == 4);
  326. ConvertToBytes(pszNetwork, pchDest, cchDest);
  327. }
  328. /*!--------------------------------------------------------------------------
  329. ConvertToNetBIOSName
  330. -
  331. Author: KennT
  332. ---------------------------------------------------------------------------*/
  333. void ConvertToNetBIOSName(LPSTR szNetBIOSName,
  334. LPCTSTR pszName,
  335. USHORT type)
  336. {
  337. USES_CONVERSION;
  338. Assert(!IsBadWritePtr(szNetBIOSName, 16*sizeof(BYTE)));
  339. wsprintfA(szNetBIOSName, "%-15s", T2CA(pszName));
  340. szNetBIOSName[15] = (BYTE) type;
  341. }
  342. /*!--------------------------------------------------------------------------
  343. FormatNetBIOSName
  344. -
  345. Author: KennT
  346. ---------------------------------------------------------------------------*/
  347. void FormatNetBIOSName(LPTSTR pszName,
  348. USHORT *puType,
  349. LPCSTR szNetBIOSName)
  350. {
  351. Assert(!IsBadWritePtr(pszName, 16*sizeof(TCHAR)));
  352. StrnCpyTFromA(pszName, szNetBIOSName, 16);
  353. pszName[15] = 0;
  354. *puType = (USHORT) (UCHAR) szNetBIOSName[15];
  355. }