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.

558 lines
18 KiB

  1. /*++
  2. Copyright (c) 1997-1998 Microsoft Corporation
  3. Module Name:
  4. rtmmain.h
  5. Abstract:
  6. Private defs for Routing Table Manager DLL
  7. Author:
  8. Chaitanya Kodeboyina (chaitk) 17-Aug-1998
  9. Revision History:
  10. --*/
  11. #ifndef __ROUTING_RTMMAIN_H__
  12. #define __ROUTING_RTMMAIN_H__
  13. //
  14. // Common Header for all RTM internal structures
  15. //
  16. // Disable warnings for unnamed structs
  17. #pragma warning(disable : 4201)
  18. typedef struct _OBJECT_HEADER
  19. {
  20. #if DBG_HDL
  21. union
  22. {
  23. DWORD TypeSign; // Type & unique signature for object
  24. struct
  25. {
  26. CHAR Type; // Identifies type of the object
  27. CHAR Signature[2]; // Pattern unique for an object type
  28. CHAR Alloc; // Set + if allocated, - if freed
  29. };
  30. };
  31. #endif
  32. #if DBG_MEM
  33. LIST_ENTRY AllocLE; // On list of all memory allocations
  34. #endif
  35. LONG RefCount; // Reference count for this object
  36. #if DBG_REF
  37. LONG RefTypes[MAX_REFS]; // Nature of references on the object
  38. #endif
  39. }
  40. OBJECT_HEADER, *POBJECT_HEADER;
  41. #pragma warning(default : 4201)
  42. //
  43. // Defns to validate handles & convert them to pointers
  44. //
  45. #define HANDLE_CONV_KEY \
  46. (ULONG_PTR)(('RTM2') | ('RTM2' << (sizeof(PVOID) - 4)))
  47. #define MAKE_HANDLE_FROM_POINTER(ObjectHandle) \
  48. (HANDLE) (((ULONG_PTR) ObjectHandle) ^ HANDLE_CONV_KEY)
  49. #define GET_POINTER_FROM_HANDLE(ObjectHandle) \
  50. (PVOID) (((ULONG_PTR) ObjectHandle) ^ HANDLE_CONV_KEY)
  51. PVOID
  52. __inline
  53. GetObjectFromHandle(HANDLE ObjectHandle, UCHAR ObjectType)
  54. {
  55. POBJECT_HEADER ObjHdr = GET_POINTER_FROM_HANDLE(ObjectHandle);
  56. UNREFERENCED_PARAMETER(ObjectType);
  57. #if DBG_HDL
  58. try
  59. {
  60. if (ObjHdr->TypeSign != OBJECT_SIGNATURE[ObjectType])
  61. {
  62. if (ObjectType != GENERIC_TYPE)
  63. {
  64. ObjHdr = NULL;
  65. }
  66. }
  67. }
  68. except(EXCEPTION_EXECUTE_HANDLER)
  69. {
  70. ObjHdr = NULL;
  71. }
  72. #endif
  73. return ObjHdr;
  74. }
  75. #define OBJECT_FROM_HANDLE(ObjectHandle, ObjectType) \
  76. (POBJECT_HEADER) GetObjectFromHandle(ObjectHandle, ObjectType);
  77. #define VALIDATE_OBJECT_HANDLE(ObjectHandle, ObjectType, pObject) \
  78. *pObject = (PVOID) OBJECT_FROM_HANDLE(ObjectHandle, ObjectType);\
  79. if ((!*pObject)) \
  80. { \
  81. return ERROR_INVALID_HANDLE; \
  82. } \
  83. #define ENTITY_FROM_HANDLE(EntityHandle) \
  84. (PENTITY_INFO) GetObjectFromHandle(EntityHandle, ENTITY_TYPE)
  85. #define VALIDATE_ENTITY_HANDLE(EntityHandle, pEntity) \
  86. *pEntity = ENTITY_FROM_HANDLE(EntityHandle); \
  87. if ((!*pEntity)||((*pEntity)->State==ENTITY_STATE_DEREGISTERED))\
  88. { \
  89. return ERROR_INVALID_HANDLE; \
  90. } \
  91. #define DBG_VALIDATE_ENTITY_HANDLE(EntityHandle, pEntity) \
  92. UNREFERENCED_PARAMETER(EntityHandle); \
  93. DBG_UNREFERENCED_LOCAL_VARIABLE(*pEntity); \
  94. if (DBG_HDL) \
  95. { \
  96. VALIDATE_ENTITY_HANDLE(EntityHandle, pEntity) \
  97. }
  98. #define DEST_FROM_HANDLE(DestHandle) \
  99. (PDEST_INFO) GetObjectFromHandle(DestHandle, DEST_TYPE)
  100. #define VALIDATE_DEST_HANDLE(DestHandle, pDest) \
  101. *pDest = DEST_FROM_HANDLE(DestHandle); \
  102. if ((!(*pDest)) || ((*pDest)->State == DEST_STATE_DELETED)) \
  103. { \
  104. return ERROR_INVALID_HANDLE; \
  105. } \
  106. #define ROUTE_FROM_HANDLE(RouteHandle) \
  107. (PROUTE_INFO) GetObjectFromHandle(RouteHandle, ROUTE_TYPE)
  108. #define VALIDATE_ROUTE_HANDLE(RouteHandle, pRoute) \
  109. *pRoute = ROUTE_FROM_HANDLE(RouteHandle); \
  110. if ( \
  111. (!(*pRoute)) || \
  112. ((*pRoute)->RouteInfo.State == RTM_ROUTE_STATE_DELETED) \
  113. ) \
  114. { \
  115. return ERROR_INVALID_HANDLE; \
  116. } \
  117. #define NEXTHOP_FROM_HANDLE(NextHopHandle) \
  118. (PNEXTHOP_INFO) GetObjectFromHandle(NextHopHandle, NEXTHOP_TYPE)
  119. #define VALIDATE_NEXTHOP_HANDLE(NextHopHandle, pNextHop) \
  120. *pNextHop = NEXTHOP_FROM_HANDLE(NextHopHandle); \
  121. if (!(*pNextHop)) \
  122. { \
  123. return ERROR_INVALID_HANDLE; \
  124. } \
  125. #define DEST_ENUM_FROM_HANDLE(DestEnumHandle) \
  126. (PDEST_ENUM) GetObjectFromHandle(DestEnumHandle, DEST_ENUM_TYPE)
  127. #define VALIDATE_DEST_ENUM_HANDLE(DestEnumHandle, pDestEnum) \
  128. *pDestEnum = DEST_ENUM_FROM_HANDLE(DestEnumHandle); \
  129. if ((!*pDestEnum)) \
  130. { \
  131. return ERROR_INVALID_HANDLE; \
  132. } \
  133. #define ROUTE_ENUM_FROM_HANDLE(RouteEnumHandle) \
  134. (PROUTE_ENUM) GetObjectFromHandle(RouteEnumHandle, ROUTE_ENUM_TYPE)
  135. #define VALIDATE_ROUTE_ENUM_HANDLE(RouteEnumHandle, pRouteEnum) \
  136. *pRouteEnum = ROUTE_ENUM_FROM_HANDLE(RouteEnumHandle); \
  137. if ((!*pRouteEnum)) \
  138. { \
  139. return ERROR_INVALID_HANDLE; \
  140. } \
  141. #define NEXTHOP_ENUM_FROM_HANDLE(EnumHandle) \
  142. (PNEXTHOP_ENUM) GetObjectFromHandle(EnumHandle, NEXTHOP_ENUM_TYPE)
  143. #define VALIDATE_NEXTHOP_ENUM_HANDLE(NextHopEnumHandle, pNextHopEnum) \
  144. *pNextHopEnum = NEXTHOP_ENUM_FROM_HANDLE(NextHopEnumHandle); \
  145. if ((!*pNextHopEnum)) \
  146. { \
  147. return ERROR_INVALID_HANDLE; \
  148. } \
  149. #define NOTIFY_FROM_HANDLE(NotifyHandle) \
  150. (PNOTIFY_INFO) GetObjectFromHandle(NotifyHandle, NOTIFY_TYPE)
  151. #define VALIDATE_NOTIFY_HANDLE(NotifyHandle, pNotify) \
  152. *pNotify = NOTIFY_FROM_HANDLE(NotifyHandle); \
  153. if ((!*pNotify) || ((*pNotify)->CNIndex < 0)) \
  154. { \
  155. return ERROR_INVALID_HANDLE; \
  156. } \
  157. #define ROUTE_LIST_FROM_HANDLE(ListHandle) \
  158. (PROUTE_LIST) GetObjectFromHandle(ListHandle, ROUTE_LIST_TYPE)
  159. #define VALIDATE_ROUTE_LIST_HANDLE(ListHandle, pList) \
  160. *pList = ROUTE_LIST_FROM_HANDLE(ListHandle); \
  161. if ((!*pList)) \
  162. { \
  163. return ERROR_INVALID_HANDLE; \
  164. } \
  165. #define LIST_ENUM_FROM_HANDLE(ListEnumHandle) \
  166. (PLIST_ENUM) GetObjectFromHandle(ListEnumHandle, LIST_ENUM_TYPE)
  167. #define VALIDATE_LIST_ENUM_HANDLE(ListEnumHandle, pListEnum) \
  168. *pListEnum = LIST_ENUM_FROM_HANDLE(ListEnumHandle); \
  169. if ((!*pListEnum)) \
  170. { \
  171. return ERROR_INVALID_HANDLE; \
  172. } \
  173. //
  174. // Defns used to maintain reference count on structures
  175. //
  176. ULONG
  177. __inline
  178. InitializeObjectReference(POBJECT_HEADER Object, UCHAR RefType)
  179. {
  180. UNREFERENCED_PARAMETER(RefType);
  181. #if DBG_REF
  182. InterlockedIncrement(&Object->RefTypes[RefType]);
  183. #endif
  184. return InterlockedIncrement(&Object->RefCount);
  185. }
  186. ULONG
  187. __inline
  188. ReferenceObject(POBJECT_HEADER Object, UCHAR RefType)
  189. {
  190. UNREFERENCED_PARAMETER(RefType);
  191. // Once ref falls to 0, this should never happen
  192. ASSERT(Object->RefCount > 0);
  193. #if DBG_REF
  194. ASSERT(Object->RefTypes[RefType] >= 0);
  195. InterlockedIncrement(&Object->RefTypes[RefType]);
  196. #endif
  197. return InterlockedIncrement(&Object->RefCount);
  198. }
  199. ULONG
  200. __inline
  201. DereferenceObject(POBJECT_HEADER Object, UCHAR RefType)
  202. {
  203. UNREFERENCED_PARAMETER(RefType);
  204. // Ref count should be +ve before we decrement it
  205. ASSERT(Object->RefCount > 0);
  206. #if DBG_REF
  207. ASSERT(Object->RefTypes[RefType] > 0);
  208. InterlockedDecrement(&Object->RefTypes[RefType]);
  209. #endif
  210. return InterlockedDecrement(&Object->RefCount);
  211. }
  212. #define INITIALIZE_INSTANCE_REFERENCE(Instance, RefType) \
  213. InitializeObjectReference(&Instance->ObjectHeader, RefType);
  214. #define REFERENCE_INSTANCE(Instance, RefType) \
  215. ReferenceObject(&Instance->ObjectHeader, RefType);
  216. #define DEREFERENCE_INSTANCE(Instance, RefType) \
  217. if (DereferenceObject(&Instance->ObjectHeader, RefType) == 0) \
  218. DestroyInstance(Instance);
  219. #define INITIALIZE_ADDR_FAMILY_REFERENCE(Instance, RefType) \
  220. InitializeObjectReference(&Instance->ObjectHeader, RefType);
  221. #define REFERENCE_ADDR_FAMILY(AddrFamilyInfo, RefType) \
  222. ReferenceObject(&AddrFamilyInfo->ObjectHeader, RefType);
  223. #define DEREFERENCE_ADDR_FAMILY(AddrFamInfo, RefType) \
  224. if (DereferenceObject(&AddrFamInfo->ObjectHeader,RefType) == 0) \
  225. DestroyAddressFamily(AddrFamInfo);
  226. #define INITIALIZE_ENTITY_REFERENCE(Entity, RefType) \
  227. InitializeObjectReference(&Entity->ObjectHeader, RefType);
  228. #define REFERENCE_ENTITY(Entity, RefType) \
  229. ReferenceObject(&Entity->ObjectHeader, RefType);
  230. #define DEREFERENCE_ENTITY(Entity, RefType) \
  231. if (DereferenceObject(&Entity->ObjectHeader, RefType) == 0) \
  232. DestroyEntity(Entity);
  233. #define INITIALIZE_DEST_REFERENCE(Dest, RefType) \
  234. InitializeObjectReference(&(Dest)->ObjectHeader, RefType);
  235. #define REFERENCE_DEST(Dest, RefType) \
  236. ReferenceObject(&(Dest)->ObjectHeader, RefType);
  237. #define DEREFERENCE_DEST(Dest, RefType) \
  238. if (DereferenceObject(&(Dest)->ObjectHeader, RefType) == 0) \
  239. DestroyDest(Dest);
  240. #define INITIALIZE_ROUTE_REFERENCE(Route, RefType) \
  241. InitializeObjectReference(&(Route)->ObjectHeader, RefType);
  242. #define REFERENCE_ROUTE(Route, RefType) \
  243. ReferenceObject(&(Route)->ObjectHeader, RefType);
  244. #define DEREFERENCE_ROUTE(Route, RefType) \
  245. if (DereferenceObject(&(Route)->ObjectHeader, RefType) == 0) \
  246. DestroyRoute(Route);
  247. #define INITIALIZE_NEXTHOP_REFERENCE(NextHop, RefType) \
  248. InitializeObjectReference(&(NextHop)->ObjectHeader, RefType);
  249. #define REFERENCE_NEXTHOP(NextHop, RefType) \
  250. ReferenceObject(&(NextHop)->ObjectHeader, RefType);
  251. #define DEREFERENCE_NEXTHOP(NextHop, RefType) \
  252. if (DereferenceObject(&(NextHop)->ObjectHeader, RefType) == 0) \
  253. DestroyNextHop(NextHop);
  254. //
  255. // Macros used to lock structures using critical sections
  256. //
  257. #define CREATE_LOCK(Lock) \
  258. InitializeCriticalSection((Lock))
  259. #define DELETE_LOCK(Lock) \
  260. DeleteCriticalSection((Lock))
  261. #define ACQUIRE_LOCK(Lock) \
  262. EnterCriticalSection((Lock))
  263. #define RELEASE_LOCK(Lock) \
  264. LeaveCriticalSection((Lock))
  265. //
  266. // Macros used to lock structures in read or write mode
  267. //
  268. typedef RTL_RESOURCE READ_WRITE_LOCK, *PREAD_WRITE_LOCK;
  269. #define CREATE_READ_WRITE_LOCK(pRWL) \
  270. RtlInitializeResource((pRWL))
  271. #define DELETE_READ_WRITE_LOCK(pRWL) \
  272. RtlDeleteResource((pRWL))
  273. #define READ_WRITE_LOCK_CREATED(pRWL) (TRUE)
  274. #define ACQUIRE_READ_LOCK(pRWL) \
  275. RtlAcquireResourceShared((pRWL),TRUE)
  276. #define RELEASE_READ_LOCK(pRWL) \
  277. RtlReleaseResource((pRWL))
  278. #define ACQUIRE_WRITE_LOCK(pRWL) \
  279. RtlAcquireResourceExclusive((pRWL),TRUE)
  280. #define RELEASE_WRITE_LOCK(pRWL) \
  281. RtlReleaseResource((pRWL))
  282. #define READ_LOCK_TO_WRITE_LOCK(pRWL) \
  283. RtlConvertSharedToExclusive((pRWL))
  284. #define WRITE_LOCK_TO_READ_LOCK(pRWL) \
  285. RtlConvertExclusiveToShared((pRWL))
  286. //
  287. // Macros to acquire and release dynamic R/W locks
  288. // [ This has been borrowed from the MGM libary ]
  289. //
  290. #define ACQUIRE_DYNAMIC_READ_LOCK(ppRWL) \
  291. AcquireReadLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  292. #define RELEASE_DYNAMIC_READ_LOCK(ppRWL) \
  293. ReleaseReadLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  294. #define ACQUIRE_DYNAMIC_WRITE_LOCK(ppRWL) \
  295. AcquireWriteLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  296. #define RELEASE_DYNAMIC_WRITE_LOCK(ppRWL) \
  297. ReleaseWriteLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  298. //
  299. // Macros used in allocating and operating on memory
  300. //
  301. #define ZeroMemory RtlZeroMemory
  302. #define CopyMemory RtlCopyMemory
  303. #define CompareMemory RtlEqualMemory
  304. #define AllocOnStack(nb) _alloca((nb))
  305. #define AllocMemory(nb) HeapAlloc(RtmGlobals.GlobalHeap, \
  306. 0, \
  307. (nb))
  308. #define AllocNZeroMemory(nb) HeapAlloc(RtmGlobals.GlobalHeap, \
  309. HEAP_ZERO_MEMORY, \
  310. (nb))
  311. #define FreeMemory(ptr) HeapFree(RtmGlobals.GlobalHeap, \
  312. 0, \
  313. (ptr))
  314. #if !DBG_MEM
  315. #define AllocNZeroObject(nb) AllocNZeroMemory(nb)
  316. #else
  317. PVOID
  318. __inline
  319. AllocNZeroObject(UINT NumBytes)
  320. {
  321. OBJECT_HEADER *Object;
  322. Object = AllocNZeroMemory(NumBytes);
  323. if (Object)
  324. {
  325. ACQUIRE_ALLOCS_LIST_LOCK();
  326. InsertTailList(&RtmGlobals.AllocsList, &Object->AllocLE);
  327. RELEASE_ALLOCS_LIST_LOCK();
  328. }
  329. return Object;
  330. }
  331. #endif
  332. #if !DBG_MEM
  333. #define FreeObject(ptr) FreeMemory(ptr)
  334. #else
  335. VOID
  336. __inline
  337. FreeObject(PVOID Object)
  338. {
  339. ACQUIRE_ALLOCS_LIST_LOCK();
  340. RemoveEntryList(&((POBJECT_HEADER)Object)->AllocLE);
  341. RELEASE_ALLOCS_LIST_LOCK();
  342. FreeMemory(Object);
  343. }
  344. #endif
  345. //
  346. // Other Misc Macros
  347. //
  348. DWORD
  349. __inline
  350. NumBitsInDword (DWORD Dword)
  351. {
  352. DWORD NumBits = 0;
  353. while (Dword)
  354. {
  355. Dword &= (Dword - 1);
  356. NumBits++;
  357. }
  358. return NumBits;
  359. }
  360. #define NUMBER_OF_BITS NumBitsInDword
  361. //
  362. // Error Handling and other related macros
  363. //
  364. #define SUCCESS(code) (code == NO_ERROR)
  365. //
  366. // DLL Startup, Cleanup Functions and Macros
  367. //
  368. BOOL
  369. RtmDllStartup(
  370. VOID
  371. );
  372. BOOL
  373. RtmDllCleanup(
  374. VOID
  375. );
  376. DWORD
  377. RtmApiStartup(
  378. VOID
  379. );
  380. #define CHECK_FOR_RTM_API_INITIALIZED() \
  381. if (!RtmGlobals.ApiInitialized) \
  382. { \
  383. Status = RtmApiStartup(); \
  384. \
  385. if (Status != NO_ERROR) \
  386. { \
  387. return Status; \
  388. } \
  389. } \
  390. #endif //__ROUTING_RTMMAIN_H__