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.

372 lines
9.4 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. UNREFERENCED_PARAMETER( pReserved );
  61. Trace1(ERR,"IpHlpDllEntry %d", fdwReason );
  62. switch (fdwReason)
  63. {
  64. case DLL_PROCESS_ATTACH:
  65. {
  66. LONG lLockCount = 0;
  67. Trace0(ERR,"IpHlpDllEntry DLL_PROCESS_ATTACH");
  68. g_bProcessAttached = FALSE;
  69. DisableThreadLibraryCalls(hInstDll);
  70. if (!IcmpEntryPoint(hInstDll, fdwReason, pReserved))
  71. {
  72. return FALSE;
  73. }
  74. g_hPrivateHeap = HeapCreate(0, 4*1024, 0);
  75. if (g_hPrivateHeap is NULL)
  76. {
  77. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  78. return FALSE;
  79. }
  80. __try
  81. {
  82. InitializeCriticalSection(&g_ifLock); lLockCount++;
  83. InitializeCriticalSection(&g_ipNetLock); lLockCount++;
  84. InitializeCriticalSection(&g_tcpipLock); lLockCount++;
  85. InitializeCriticalSection(&g_stateLock); lLockCount++;
  86. }
  87. __except((GetExceptionCode() == STATUS_NO_MEMORY)
  88. ? EXCEPTION_EXECUTE_HANDLER
  89. : EXCEPTION_CONTINUE_SEARCH)
  90. {
  91. ;
  92. }
  93. if (lLockCount != 4)
  94. {
  95. if (lLockCount-- > 0) { DeleteCriticalSection(&g_ifLock); }
  96. if (lLockCount-- > 0) { DeleteCriticalSection(&g_ipNetLock); }
  97. if (lLockCount-- > 0) { DeleteCriticalSection(&g_tcpipLock); }
  98. if (lLockCount-- > 0) { DeleteCriticalSection(&g_stateLock); }
  99. HeapDestroy(g_hPrivateHeap);
  100. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  101. return FALSE;
  102. }
  103. g_dwLastIfUpdateTime = 0;
  104. g_dwLastArpUpdateTime = 0;
  105. g_pdwArpEntTable = NULL;
  106. g_dwNumArpEntEntries = 0;
  107. g_dwNumIf = 0;
  108. InitAdapterMappingTable();
  109. g_hMIBServer = NULL;
  110. #if API_TRACE
  111. g_dwTraceHandle = TraceRegister("IPHLPAPI");
  112. if (g_dwTraceHandle is INVALID_TRACEID)
  113. {
  114. UnInitAdapterMappingTable();
  115. DeleteCriticalSection(&g_ifLock);
  116. DeleteCriticalSection(&g_ipNetLock);
  117. DeleteCriticalSection(&g_tcpipLock);
  118. DeleteCriticalSection(&g_stateLock);
  119. HeapDestroy(g_hPrivateHeap);
  120. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  121. return FALSE;
  122. }
  123. #endif // API_TRACE
  124. if (OpenTCPDriver(AF_INET) is NO_ERROR)
  125. {
  126. g_bIpConfigured = TRUE;
  127. if (UpdateAdapterToIFInstanceMapping() isnot NO_ERROR ||
  128. UpdateAdapterToATInstanceMapping() isnot NO_ERROR)
  129. {
  130. g_bIpConfigured = FALSE;
  131. CloseTCPDriver();
  132. #if API_TRACE
  133. TraceDeregister(g_dwTraceHandle);
  134. #endif // API_TRACE
  135. UnInitAdapterMappingTable();
  136. DeleteCriticalSection(&g_ifLock);
  137. DeleteCriticalSection(&g_ipNetLock);
  138. DeleteCriticalSection(&g_tcpipLock);
  139. DeleteCriticalSection(&g_stateLock);
  140. HeapDestroy(g_hPrivateHeap);
  141. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  142. return FALSE;
  143. }
  144. }
  145. else
  146. {
  147. //
  148. // we are not running on an IP machine
  149. //
  150. g_bIpConfigured = FALSE;
  151. }
  152. #ifndef CHICAGO
  153. InitFilterApis();
  154. //
  155. // Attach ipcfgdll library
  156. //
  157. if (g_bIpConfigured)
  158. {
  159. if (IpcfgdllInit(hInstDll, fdwReason, pReserved) == FALSE)
  160. {
  161. UnInitFilterApis();
  162. CloseTCPDriver();
  163. #if API_TRACE
  164. TraceDeregister(g_dwTraceHandle);
  165. #endif // API_TRACE
  166. UnInitAdapterMappingTable();
  167. DeleteCriticalSection(&g_ifLock);
  168. DeleteCriticalSection(&g_ipNetLock);
  169. DeleteCriticalSection(&g_tcpipLock);
  170. DeleteCriticalSection(&g_stateLock);
  171. HeapDestroy(g_hPrivateHeap);
  172. IcmpEntryPoint(hInstDll, DLL_PROCESS_DETACH, pReserved);
  173. return FALSE;
  174. }
  175. }
  176. #endif
  177. g_hModule = hInstDll;
  178. InitNameMappers();
  179. g_bProcessAttached = TRUE;
  180. break;
  181. }
  182. case DLL_PROCESS_DETACH:
  183. {
  184. Trace0(ERR,"IpHlpDllEntry DLL_PROCESS_DETACH");
  185. if (!g_bProcessAttached)
  186. {
  187. break;
  188. }
  189. DeinitNameMappers();
  190. if (g_hPrivateHeap isnot NULL)
  191. {
  192. HeapDestroy(g_hPrivateHeap);
  193. }
  194. DeleteCriticalSection(&g_ifLock);
  195. DeleteCriticalSection(&g_ipNetLock);
  196. DeleteCriticalSection(&g_tcpipLock);
  197. DeleteCriticalSection(&g_stateLock);
  198. #ifndef CHICAGO
  199. if (g_hMIBServer isnot NULL)
  200. {
  201. MprAdminMIBServerDisconnect(g_hMIBServer);
  202. }
  203. #endif
  204. if (g_bIpConfigured)
  205. {
  206. CloseTCPDriver();
  207. }
  208. if (g_bIp6Configured)
  209. {
  210. CloseTCP6Driver();
  211. }
  212. #ifndef CHICAGO
  213. UnInitFilterApis();
  214. //
  215. // Detach ipcfgdll library
  216. //
  217. IpcfgdllInit(hInstDll, fdwReason, pReserved);
  218. #endif
  219. #if API_TRACE
  220. TraceDeregister(g_dwTraceHandle);
  221. #endif // API_TRACE
  222. g_bProcessAttached = FALSE;
  223. if (!IcmpEntryPoint(hInstDll, fdwReason, pReserved))
  224. {
  225. return FALSE;
  226. }
  227. break;
  228. }
  229. default:
  230. {
  231. break;
  232. }
  233. }
  234. return TRUE;
  235. }
  236. #ifndef CHICAGO
  237. BOOL
  238. IsRouterRunning(VOID)
  239. {
  240. DWORD dwResult;
  241. if(!MprAdminIsServiceRunning(NULL))
  242. {
  243. return FALSE;
  244. }
  245. EnterCriticalSection(&g_stateLock);
  246. if(g_hMIBServer is NULL)
  247. {
  248. dwResult = MprAdminMIBServerConnect(NULL,
  249. &g_hMIBServer);
  250. if(dwResult isnot NO_ERROR)
  251. {
  252. Trace1(ERR,
  253. "IsRouterRunning: Error %d connecting to MIB Server\n",
  254. dwResult);
  255. LeaveCriticalSection(&g_stateLock);
  256. return FALSE;
  257. }
  258. }
  259. LeaveCriticalSection(&g_stateLock);
  260. return TRUE;
  261. }
  262. BOOL
  263. IsRouterSettingRoutes(VOID)
  264. {
  265. DWORD dwResult, dwOutEntrySize;
  266. BOOL bRet;
  267. MIB_OPAQUE_QUERY Query;
  268. PMIB_OPAQUE_INFO pInfo;
  269. PMIB_ROUTESTATE pState;
  270. //
  271. // This has to be called after calling IsRouterRunning, so g_hMibServer
  272. // is already set
  273. //
  274. if(g_hMIBServer is NULL)
  275. {
  276. return FALSE;
  277. }
  278. Query.dwVarId = ROUTE_STATE;
  279. dwResult = MprAdminMIBEntryGet(g_hMIBServer,
  280. PID_IP,
  281. IPRTRMGR_PID,
  282. (PVOID)&Query,
  283. sizeof(MIB_OPAQUE_QUERY),
  284. (PVOID)&pInfo,
  285. &dwOutEntrySize);
  286. if(dwResult isnot NO_ERROR)
  287. {
  288. return FALSE;
  289. }
  290. CAST_MIB_INFO(pInfo, PMIB_ROUTESTATE, pState);
  291. bRet = pState->bRoutesSetToStack;
  292. MprAdminMIBBufferFree((PVOID)pInfo);;
  293. return bRet;
  294. }
  295. #endif