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.

366 lines
12 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. // {0705ECA3-7AAC-11d2-89DC-006008B0E5B9}
  4. const GUID g_MyGuid =
  5. { 0x705eca3, 0x7aac, 0x11d2, { 0x89, 0xdc, 0x0, 0x60, 0x8, 0xb0, 0xe5, 0xb9 } };
  6. static const GUID g_IpGuid = IPMONTR_GUID;
  7. #define IPPROMON_HELPER_VERSION 1
  8. // shell functions
  9. PNS_REGISTER_HELPER RegisterHelper;
  10. PNS_MATCH_CMD_LINE MatchCmdToken;
  11. PNS_MATCH_TOKEN MatchToken;
  12. PNS_MATCH_ENUM_TAG MatchEnumTag;
  13. PNS_MATCH_TAGS_IN_CMD_LINE MatchTagsInCmdLine;
  14. PNS_MAKE_STRING MakeString;
  15. PNS_FREE_STRING FreeString;
  16. PNS_MAKE_QUOTED_STRING MakeQuotedString;
  17. PNS_FREE_QUOTED_STRING FreeQuotedString;
  18. PNS_DISPLAY_ERR DisplayError;
  19. PNS_DISPLAY_MSG DisplayMessage;
  20. PNS_DISPLAY_MSG_T DisplayMessageT;
  21. PNS_EXECUTE_HANDLER ExecuteHandler;
  22. PNS_INIT_CONSOLE InitializeConsole;
  23. PNS_DISPLAY_MSG_CONSOLE DisplayMessageMib;
  24. PNS_REFRESH_CONSOLE RefreshConsole;
  25. PNS_UPDATE_NEW_CONTEXT UpdateNewContext;
  26. PNS_PREPROCESS_COMMAND PreprocessCommand;
  27. ULONG StartedCommonInitialization, CompletedCommonInitialization ;
  28. HANDLE g_hModule;
  29. MIB_SERVER_HANDLE g_hMibServer;
  30. VOID
  31. CommonNetshInit(
  32. IN PNETSH_ATTRIBUTES pUtilityTable
  33. )
  34. {
  35. //
  36. // common utility functions exported by the shell
  37. //
  38. RegisterHelper = pUtilityTable->pfnRegisterHelper;
  39. MatchCmdToken = pUtilityTable->pfnMatchCmdLine;
  40. MatchToken = pUtilityTable->pfnMatchToken;
  41. MatchEnumTag = pUtilityTable->pfnMatchEnumTag;
  42. MatchTagsInCmdLine = pUtilityTable->pfnMatchTagsInCmdLine;
  43. MakeString = pUtilityTable->pfnMakeString;
  44. FreeString = pUtilityTable->pfnFreeString;
  45. MakeQuotedString = pUtilityTable->pfnMakeQuotedString;
  46. FreeQuotedString = pUtilityTable->pfnFreeQuotedString;
  47. DisplayError = pUtilityTable->pfnDisplayError;
  48. DisplayMessage = pUtilityTable->pfnDisplayMessage;
  49. DisplayMessageT = pUtilityTable->pfnDisplayMessageT;
  50. ExecuteHandler = pUtilityTable->pfnExecuteHandler;
  51. InitializeConsole = pUtilityTable->pfnInitializeConsole;
  52. DisplayMessageMib = pUtilityTable->pfnDisplayMessageToConsole;
  53. RefreshConsole = pUtilityTable->pfnRefreshConsole;
  54. UpdateNewContext = pUtilityTable->pfnUpdateNewContext;
  55. PreprocessCommand = pUtilityTable->pfnPreprocessCommand;
  56. }
  57. BOOL WINAPI
  58. DllMain(
  59. HINSTANCE hInstDll,
  60. DWORD fdwReason,
  61. LPVOID pReserved
  62. )
  63. {
  64. HANDLE hDll;
  65. switch (fdwReason)
  66. {
  67. case DLL_PROCESS_ATTACH:
  68. {
  69. // printf("Trying to attach\n");
  70. g_hModule = hInstDll;
  71. DisableThreadLibraryCalls(hInstDll);
  72. break;
  73. }
  74. case DLL_PROCESS_DETACH:
  75. {
  76. //
  77. // Clean up any structures used for commit
  78. //
  79. break;
  80. }
  81. default:
  82. {
  83. break;
  84. }
  85. }
  86. return TRUE;
  87. }
  88. DWORD WINAPI
  89. IppromonStartHelper(
  90. IN CONST GUID *pguidParent,
  91. IN PVOID pfnRegisterContext,
  92. IN DWORD dwVersion
  93. )
  94. {
  95. DWORD dwErr;
  96. PNS_REGISTER_CONTEXT RegisterContext
  97. = (PNS_REGISTER_CONTEXT) pfnRegisterContext;
  98. NS_CONTEXT_ATTRIBUTES attMyAttributes;
  99. // If you add any more contexts, then this should be converted
  100. // to use an array instead of duplicating code!
  101. // Register the IGMP context
  102. ZeroMemory(&attMyAttributes, sizeof(attMyAttributes));
  103. attMyAttributes.pwszContext = L"igmp";
  104. attMyAttributes.guidHelper = g_MyGuid;
  105. attMyAttributes.dwVersion = 1;
  106. attMyAttributes.dwFlags = 0;
  107. attMyAttributes.pfnDumpFn = IgmpDump;
  108. attMyAttributes.ulNumTopCmds= g_ulNumIgmpTopCmds;
  109. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_IgmpCmds;
  110. attMyAttributes.ulNumGroups = g_ulIgmpNumGroups;
  111. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_IgmpCmdGroups;
  112. dwErr = RegisterContext( &attMyAttributes );
  113. // Register the RIP context
  114. attMyAttributes.pwszContext = L"rip";
  115. attMyAttributes.dwVersion = 1;
  116. attMyAttributes.dwFlags = 0;
  117. attMyAttributes.pfnDumpFn = RipDump;
  118. attMyAttributes.ulNumTopCmds= g_ulRipNumTopCmds;
  119. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_RipCmds;
  120. attMyAttributes.ulNumGroups = g_ulRipNumGroups;
  121. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_RipCmdGroups;
  122. dwErr = RegisterContext( &attMyAttributes );
  123. // Register the OSPF context
  124. attMyAttributes.pwszContext = L"ospf";
  125. attMyAttributes.dwVersion = 1;
  126. attMyAttributes.dwFlags = 0;
  127. attMyAttributes.pfnDumpFn = OspfDump;
  128. attMyAttributes.ulNumTopCmds= g_ulOspfNumTopCmds;
  129. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_OspfCmds;
  130. attMyAttributes.ulNumGroups = g_ulOspfNumGroups;
  131. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_OspfCmdGroups;
  132. dwErr = RegisterContext( &attMyAttributes );
  133. // Register the RouterDiscovery relay context
  134. attMyAttributes.pwszContext = L"routerdiscovery";
  135. attMyAttributes.dwVersion = 1;
  136. attMyAttributes.dwFlags = 0;
  137. attMyAttributes.pfnDumpFn = RdiscDump;
  138. attMyAttributes.ulNumTopCmds= g_RdiscTopCmdCount;
  139. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_RdiscTopCmdTable;
  140. attMyAttributes.ulNumGroups = g_RdiscCmdGroupCount;
  141. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_RdiscCmdGroupTable;
  142. dwErr = RegisterContext( &attMyAttributes );
  143. // Register the DHCP relay context
  144. attMyAttributes.pwszContext = L"relay";
  145. attMyAttributes.dwVersion = 1;
  146. attMyAttributes.dwFlags = 0;
  147. attMyAttributes.pfnDumpFn = BootpDump;
  148. attMyAttributes.ulNumTopCmds= g_ulBootpNumTopCmds;
  149. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_BootpTopCmds;
  150. attMyAttributes.ulNumGroups = g_ulBootpNumGroups;
  151. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_BootpCmdGroups;
  152. dwErr = RegisterContext( &attMyAttributes );
  153. // Register the Connection sharing contexts
  154. attMyAttributes.pwszContext = L"autodhcp";
  155. attMyAttributes.dwVersion = 1;
  156. attMyAttributes.dwFlags = 0;
  157. attMyAttributes.pfnDumpFn = AutoDhcpDump;
  158. attMyAttributes.ulNumTopCmds= g_AutoDhcpTopCmdCount;
  159. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_AutoDhcpTopCmdTable;
  160. attMyAttributes.ulNumGroups = g_AutoDhcpCmdGroupCount;
  161. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_AutoDhcpCmdGroupTable;
  162. dwErr = RegisterContext( &attMyAttributes );
  163. attMyAttributes.pwszContext = L"dnsproxy";
  164. attMyAttributes.dwVersion = 1;
  165. attMyAttributes.dwFlags = 0;
  166. attMyAttributes.pfnDumpFn = DnsProxyDump;
  167. attMyAttributes.ulNumTopCmds= g_DnsProxyTopCmdCount;
  168. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_DnsProxyTopCmdTable;
  169. attMyAttributes.ulNumGroups = g_DnsProxyCmdGroupCount;
  170. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_DnsProxyCmdGroupTable;
  171. dwErr = RegisterContext( &attMyAttributes );
  172. attMyAttributes.pwszContext = L"nat";
  173. attMyAttributes.dwVersion = 1;
  174. attMyAttributes.dwFlags = 0;
  175. attMyAttributes.pfnDumpFn = NatDump;
  176. attMyAttributes.ulNumTopCmds= g_NatTopCmdCount;
  177. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_NatTopCmdTable;
  178. attMyAttributes.ulNumGroups = g_NatCmdGroupCount;
  179. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_NatCmdGroupTable;
  180. dwErr = RegisterContext( &attMyAttributes );
  181. attMyAttributes.pwszContext = L"qos";
  182. attMyAttributes.dwVersion = 1;
  183. attMyAttributes.dwFlags = 0;
  184. attMyAttributes.pfnDumpFn = QosDump;
  185. attMyAttributes.ulNumTopCmds= g_ulQosNumTopCmds;
  186. attMyAttributes.pTopCmds = (CMD_ENTRY (*)[])&g_QosCmds;
  187. attMyAttributes.ulNumGroups = g_ulQosNumGroups;
  188. attMyAttributes.pCmdGroups = (CMD_GROUP_ENTRY (*)[])&g_QosCmdGroups;
  189. dwErr = RegisterContext( &attMyAttributes );
  190. return dwErr;
  191. }
  192. DWORD WINAPI
  193. InitHelperDll(
  194. IN PNETSH_ATTRIBUTES pUtilityTable,
  195. OUT PNS_DLL_ATTRIBUTES pDllTable
  196. )
  197. {
  198. DWORD dwErr;
  199. NS_HELPER_ATTRIBUTES attMyAttributes;
  200. CommonNetshInit( pUtilityTable );
  201. pDllTable->dwVersion = NETSH_VERSION_50;
  202. pDllTable->pfnStopFn = NULL;
  203. // Register helpers. We could either register 1 helper which
  204. // registers three contexts, or we could register 3 helpers
  205. // which each register one context. There's only a difference
  206. // if we support sub-helpers, which this DLL does not.
  207. // If we later support sub-helpers, then it's better to have
  208. // 3 helpers so that sub-helpers can register with 1 of them,
  209. // since it registers with a parent helper, not a parent context.
  210. // For now, we just use a single 3-context helper for efficiency.
  211. ZeroMemory( &attMyAttributes, sizeof(attMyAttributes) );
  212. attMyAttributes.guidHelper = g_MyGuid;
  213. attMyAttributes.dwVersion = IPPROMON_HELPER_VERSION;
  214. attMyAttributes.pfnStart = IppromonStartHelper;
  215. attMyAttributes.pfnStop = NULL;
  216. dwErr = RegisterHelper( &g_IpGuid, &attMyAttributes );
  217. return dwErr;
  218. }
  219. BOOL
  220. IsProtocolInstalled(
  221. DWORD dwProtoId,
  222. DWORD dwNameId,
  223. DWORD dwErrorLog
  224. )
  225. /*++
  226. Routine Description:
  227. Finds if the protocol is already installed
  228. Arguments:
  229. dwProtoId - protocol id
  230. pswzName - protocol name
  231. dwErrorLog - TRUE(if not installed display error)
  232. FALSE(if installed display error)
  233. -1 (do not display error log)
  234. Return Value:
  235. TRUE if protocol already installed, else FALSE
  236. --*/
  237. {
  238. PVOID pvStart;
  239. DWORD dwCount, dwBlkSize, dwRes;
  240. WCHAR *pwszName;
  241. dwRes = IpmontrGetInfoBlockFromGlobalInfo(dwProtoId,
  242. (PBYTE *) NULL,
  243. &dwBlkSize,
  244. &dwCount);
  245. if ((dwRes isnot NO_ERROR) && (dwErrorLog == TRUE))
  246. {
  247. pwszName = MakeString( g_hModule, dwNameId);
  248. DisplayError(g_hModule, EMSG_PROTO_NOT_INSTALLED, pwszName);
  249. FreeString(pwszName);
  250. }
  251. else if ((dwRes == NO_ERROR) && (dwErrorLog == FALSE))
  252. {
  253. pwszName = MakeString( g_hModule, dwNameId);
  254. DisplayError(g_hModule, EMSG_PROTO_INSTALLED, pwszName);
  255. FreeString(pwszName);
  256. }
  257. return (dwRes == NO_ERROR) ? TRUE : FALSE;
  258. }
  259. DWORD
  260. GetMIBIfIndex(
  261. IN PTCHAR *pptcArguments,
  262. IN DWORD dwCurrentIndex,
  263. OUT PDWORD pdwIndices,
  264. OUT PDWORD pdwNumParsed
  265. )
  266. /*++
  267. Routine Description:
  268. Gets the interface index.
  269. Arguments:
  270. pptcArguments - Argument array
  271. dwCurrentIndex - Index of the first argument in array
  272. pdwIndices - Indices specified in command
  273. pdwNumParsed - Number of indices in command
  274. Return Value:
  275. NO_ERROR
  276. --*/
  277. {
  278. DWORD dwErr = NO_ERROR;
  279. *pdwNumParsed = 1;
  280. // If index was specified just use it
  281. if (iswdigit(pptcArguments[dwCurrentIndex][0]))
  282. {
  283. pdwIndices[0] = _tcstoul(pptcArguments[dwCurrentIndex],NULL,10);
  284. return NO_ERROR;
  285. }
  286. // Try converting a friendly name to an ifindex
  287. return IpmontrGetIfIndexFromFriendlyName( g_hMibServer,
  288. pptcArguments[dwCurrentIndex],
  289. &pdwIndices[0] );
  290. }