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.

386 lines
8.9 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. net\routing\iphlpapi.c
  5. Abstract:
  6. This files contains the DLL entry point and some miscellanous functions for
  7. IPHLPAPI.DLL
  8. Revision History:
  9. Amritansh Raghav
  10. --*/
  11. #include "inc.h"
  12. #pragma hdrstop
  13. #include <initguid.h>
  14. #include <ifguid.h>
  15. HANDLE g_hPrivateHeap;
  16. HANDLE g_hTCPDriverGetHandle;
  17. HANDLE g_hTCP6DriverGetHandle;
  18. HANDLE g_hTCPDriverSetHandle;
  19. HANDLE g_hTCP6DriverSetHandle;
  20. HANDLE g_hIPDriverHandle;
  21. HANDLE g_hIP6DriverHandle;
  22. HANDLE g_hIPGetDriverHandle;
  23. HANDLE g_hIP6GetDriverHandle;
  24. DWORD g_dwTraceHandle;
  25. LIST_ENTRY g_pAdapterMappingTable[MAP_HASH_SIZE];
  26. DWORD g_dwLastIfUpdateTime;
  27. PDWORD g_pdwArpEntTable;
  28. DWORD g_dwNumArpEntEntries;
  29. DWORD g_dwLastArpUpdateTime;
  30. DWORD g_dwNumIf;
  31. BOOL g_bIpConfigured;
  32. BOOL g_bIp6Configured;
  33. BOOL g_bProcessAttached;
  34. HANDLE g_hModule;
  35. MIB_SERVER_HANDLE g_hMIBServer;
  36. CRITICAL_SECTION g_ifLock;
  37. CRITICAL_SECTION g_ipNetLock;
  38. CRITICAL_SECTION g_tcpipLock;
  39. CRITICAL_SECTION g_stateLock;
  40. WSADATA WsaData;
  41. #ifndef CHICAGO
  42. VOID
  43. InitFilterApis();
  44. VOID
  45. UnInitFilterApis();
  46. #endif
  47. BOOL WINAPI
  48. IcmpEntryPoint(
  49. HANDLE hDll,
  50. DWORD dwReason,
  51. LPVOID lpReserved
  52. );
  53. BOOL WINAPI
  54. DllMain(
  55. HINSTANCE hInstDll,
  56. DWORD fdwReason,
  57. LPVOID pReserved
  58. )
  59. {
  60. DWORD dwResult;
  61. int retcode;
  62. UNREFERENCED_PARAMETER( pReserved );
  63. Trace1(ERR,"IpHlpDllEntry %d", fdwReason );
  64. switch (fdwReason)
  65. {
  66. case DLL_PROCESS_ATTACH:
  67. {
  68. Trace0(ERR,"IpHlpDllEntry DLL_PROCESS_ATTACH");
  69. g_bProcessAttached = FALSE;
  70. // Check platform.
  71. if (GetVersion() & 0x80000000)
  72. {
  73. // CHICAGO
  74. #ifndef CHICAGO
  75. DEBUG_PRINT(("IpHlpDllEntry: This an NT product.\n"));
  76. return FALSE;
  77. #endif
  78. }
  79. else
  80. {
  81. // NT
  82. #ifdef CHICAGO
  83. DEBUG_PRINT(("IpHlpDllEntry: This is an CHICAGO product.\n"));
  84. return FALSE;
  85. #endif
  86. }
  87. //
  88. // we use WsControl amongst other things from wsock32
  89. //
  90. #ifdef CHICAGO
  91. retcode = WSAStartup(0x0101, &WsaData);
  92. if (retcode != 0)
  93. {
  94. DEBUG_PRINT(("WSAStartup failed %d\n", GetLastError() ));
  95. return FALSE;
  96. }
  97. #endif
  98. DisableThreadLibraryCalls(hInstDll);
  99. if (!IcmpEntryPoint(hInstDll, fdwReason, pReserved))
  100. {
  101. return FALSE;
  102. }
  103. g_hPrivateHeap = HeapCreate(0, 4*1024, 0);
  104. if (g_hPrivateHeap is NULL)
  105. {
  106. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  107. return FALSE;
  108. }
  109. InitializeCriticalSection(&g_ifLock);
  110. InitializeCriticalSection(&g_ipNetLock);
  111. InitializeCriticalSection(&g_tcpipLock);
  112. InitializeCriticalSection(&g_stateLock);
  113. g_dwLastIfUpdateTime = 0;
  114. g_dwLastArpUpdateTime = 0;
  115. g_pdwArpEntTable = NULL;
  116. g_dwNumArpEntEntries = 0;
  117. g_dwNumIf = 0;
  118. InitAdapterMappingTable();
  119. g_hMIBServer = NULL;
  120. #if API_TRACE
  121. g_dwTraceHandle = TraceRegister("IPHLPAPI");
  122. if (g_dwTraceHandle is INVALID_TRACEID)
  123. {
  124. UnInitAdapterMappingTable();
  125. DeleteCriticalSection(&g_ifLock);
  126. DeleteCriticalSection(&g_ipNetLock);
  127. DeleteCriticalSection(&g_tcpipLock);
  128. DeleteCriticalSection(&g_stateLock);
  129. HeapDestroy(g_hPrivateHeap);
  130. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  131. return FALSE;
  132. }
  133. #endif // API_TRACE
  134. if (OpenTCPDriver(AF_INET) is NO_ERROR)
  135. {
  136. g_bIpConfigured = TRUE;
  137. if (UpdateAdapterToIFInstanceMapping() isnot NO_ERROR ||
  138. UpdateAdapterToATInstanceMapping() isnot NO_ERROR)
  139. {
  140. g_bIpConfigured = FALSE;
  141. CloseTCPDriver();
  142. #if API_TRACE
  143. TraceDeregister(g_dwTraceHandle);
  144. #endif // API_TRACE
  145. UnInitAdapterMappingTable();
  146. DeleteCriticalSection(&g_ifLock);
  147. DeleteCriticalSection(&g_ipNetLock);
  148. DeleteCriticalSection(&g_tcpipLock);
  149. DeleteCriticalSection(&g_stateLock);
  150. HeapDestroy(g_hPrivateHeap);
  151. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  152. return FALSE;
  153. }
  154. }
  155. else
  156. {
  157. //
  158. // we are not running on an IP machine
  159. //
  160. g_bIpConfigured = FALSE;
  161. }
  162. #ifndef CHICAGO
  163. InitFilterApis();
  164. //
  165. // Attach ipcfgdll library
  166. //
  167. if (g_bIpConfigured)
  168. {
  169. if (IpcfgdllInit(hInstDll, fdwReason, pReserved) == FALSE)
  170. {
  171. UnInitFilterApis();
  172. CloseTCPDriver();
  173. #if API_TRACE
  174. TraceDeregister(g_dwTraceHandle);
  175. #endif // API_TRACE
  176. UnInitAdapterMappingTable();
  177. DeleteCriticalSection(&g_ifLock);
  178. DeleteCriticalSection(&g_ipNetLock);
  179. DeleteCriticalSection(&g_tcpipLock);
  180. DeleteCriticalSection(&g_stateLock);
  181. HeapDestroy(g_hPrivateHeap);
  182. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  183. return FALSE;
  184. }
  185. }
  186. #endif
  187. g_hModule = hInstDll;
  188. InitNameMappers();
  189. g_bProcessAttached = TRUE;
  190. break;
  191. }
  192. case DLL_PROCESS_DETACH:
  193. {
  194. Trace0(ERR,"IpHlpDllEntry DLL_PROCESS_DETACH");
  195. if (!g_bProcessAttached)
  196. {
  197. break;
  198. }
  199. DeinitNameMappers();
  200. if (g_hPrivateHeap isnot NULL)
  201. {
  202. HeapDestroy(g_hPrivateHeap);
  203. }
  204. DeleteCriticalSection(&g_ifLock);
  205. DeleteCriticalSection(&g_ipNetLock);
  206. DeleteCriticalSection(&g_tcpipLock);
  207. DeleteCriticalSection(&g_stateLock);
  208. #ifndef CHICAGO
  209. if (g_hMIBServer isnot NULL)
  210. {
  211. MprAdminMIBServerDisconnect(g_hMIBServer);
  212. }
  213. #endif
  214. if (g_bIpConfigured)
  215. {
  216. CloseTCPDriver();
  217. }
  218. if (g_bIp6Configured)
  219. {
  220. CloseTCP6Driver();
  221. }
  222. #ifndef CHICAGO
  223. UnInitFilterApis();
  224. //
  225. // Detach ipcfgdll library
  226. //
  227. IpcfgdllInit(hInstDll, fdwReason, pReserved);
  228. #endif
  229. #if API_TRACE
  230. TraceDeregister(g_dwTraceHandle);
  231. #endif // API_TRACE
  232. g_bProcessAttached = FALSE;
  233. if (!IcmpEntryPoint(hInstDll, fdwReason, pReserved))
  234. {
  235. return FALSE;
  236. }
  237. break;
  238. }
  239. default:
  240. {
  241. break;
  242. }
  243. }
  244. return TRUE;
  245. }
  246. #ifndef CHICAGO
  247. BOOL
  248. IsRouterRunning(VOID)
  249. {
  250. DWORD dwResult;
  251. if(!MprAdminIsServiceRunning(NULL))
  252. {
  253. g_hMIBServer = NULL;
  254. return FALSE;
  255. }
  256. if(g_hMIBServer is NULL)
  257. {
  258. dwResult = MprAdminMIBServerConnect(NULL,
  259. &g_hMIBServer);
  260. if(dwResult isnot NO_ERROR)
  261. {
  262. Trace1(ERR,
  263. "IsRouterRunning: Error %d connecting to MIB Server\n",
  264. dwResult);
  265. g_hMIBServer = NULL;
  266. return FALSE;
  267. }
  268. }
  269. return TRUE;
  270. }
  271. BOOL
  272. IsRouterSettingRoutes(VOID)
  273. {
  274. DWORD dwResult, dwOutEntrySize;
  275. BOOL bRet;
  276. MIB_OPAQUE_QUERY Query;
  277. PMIB_OPAQUE_INFO pInfo;
  278. PMIB_ROUTESTATE pState;
  279. //
  280. // This has to be called after calling IsRouterRunning, so g_hMibServer
  281. // is already set
  282. //
  283. if(g_hMIBServer is NULL)
  284. {
  285. return FALSE;
  286. }
  287. Query.dwVarId = ROUTE_STATE;
  288. dwResult = MprAdminMIBEntryGet(g_hMIBServer,
  289. PID_IP,
  290. IPRTRMGR_PID,
  291. (PVOID)&Query,
  292. sizeof(MIB_OPAQUE_QUERY),
  293. (PVOID)&pInfo,
  294. &dwOutEntrySize);
  295. if(dwResult isnot NO_ERROR)
  296. {
  297. return FALSE;
  298. }
  299. CAST_MIB_INFO(pInfo, PMIB_ROUTESTATE, pState);
  300. bRet = pState->bRoutesSetToStack;
  301. MprAdminMIBBufferFree((PVOID)pInfo);;
  302. return bRet;
  303. }
  304. #endif