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.

556 lines
19 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. if (DBG_HDL) \
  93. { \
  94. VALIDATE_ENTITY_HANDLE(EntityHandle, pEntity) \
  95. }
  96. #define DEST_FROM_HANDLE(DestHandle) \
  97. (PDEST_INFO) GetObjectFromHandle(DestHandle, DEST_TYPE)
  98. #define VALIDATE_DEST_HANDLE(DestHandle, pDest) \
  99. *pDest = DEST_FROM_HANDLE(DestHandle); \
  100. if ((!(*pDest)) || ((*pDest)->State == DEST_STATE_DELETED)) \
  101. { \
  102. return ERROR_INVALID_HANDLE; \
  103. } \
  104. #define ROUTE_FROM_HANDLE(RouteHandle) \
  105. (PROUTE_INFO) GetObjectFromHandle(RouteHandle, ROUTE_TYPE)
  106. #define VALIDATE_ROUTE_HANDLE(RouteHandle, pRoute) \
  107. *pRoute = ROUTE_FROM_HANDLE(RouteHandle); \
  108. if ( \
  109. (!(*pRoute)) || \
  110. ((*pRoute)->RouteInfo.State == RTM_ROUTE_STATE_DELETED) \
  111. ) \
  112. { \
  113. return ERROR_INVALID_HANDLE; \
  114. } \
  115. #define NEXTHOP_FROM_HANDLE(NextHopHandle) \
  116. (PNEXTHOP_INFO) GetObjectFromHandle(NextHopHandle, NEXTHOP_TYPE)
  117. #define VALIDATE_NEXTHOP_HANDLE(NextHopHandle, pNextHop) \
  118. *pNextHop = NEXTHOP_FROM_HANDLE(NextHopHandle); \
  119. if (!(*pNextHop)) \
  120. { \
  121. return ERROR_INVALID_HANDLE; \
  122. } \
  123. #define DEST_ENUM_FROM_HANDLE(DestEnumHandle) \
  124. (PDEST_ENUM) GetObjectFromHandle(DestEnumHandle, DEST_ENUM_TYPE)
  125. #define VALIDATE_DEST_ENUM_HANDLE(DestEnumHandle, pDestEnum) \
  126. *pDestEnum = DEST_ENUM_FROM_HANDLE(DestEnumHandle); \
  127. if ((!*pDestEnum)) \
  128. { \
  129. return ERROR_INVALID_HANDLE; \
  130. } \
  131. #define ROUTE_ENUM_FROM_HANDLE(RouteEnumHandle) \
  132. (PROUTE_ENUM) GetObjectFromHandle(RouteEnumHandle, ROUTE_ENUM_TYPE)
  133. #define VALIDATE_ROUTE_ENUM_HANDLE(RouteEnumHandle, pRouteEnum) \
  134. *pRouteEnum = ROUTE_ENUM_FROM_HANDLE(RouteEnumHandle); \
  135. if ((!*pRouteEnum)) \
  136. { \
  137. return ERROR_INVALID_HANDLE; \
  138. } \
  139. #define NEXTHOP_ENUM_FROM_HANDLE(EnumHandle) \
  140. (PNEXTHOP_ENUM) GetObjectFromHandle(EnumHandle, NEXTHOP_ENUM_TYPE)
  141. #define VALIDATE_NEXTHOP_ENUM_HANDLE(NextHopEnumHandle, pNextHopEnum) \
  142. *pNextHopEnum = NEXTHOP_ENUM_FROM_HANDLE(NextHopEnumHandle); \
  143. if ((!*pNextHopEnum)) \
  144. { \
  145. return ERROR_INVALID_HANDLE; \
  146. } \
  147. #define NOTIFY_FROM_HANDLE(NotifyHandle) \
  148. (PNOTIFY_INFO) GetObjectFromHandle(NotifyHandle, NOTIFY_TYPE)
  149. #define VALIDATE_NOTIFY_HANDLE(NotifyHandle, pNotify) \
  150. *pNotify = NOTIFY_FROM_HANDLE(NotifyHandle); \
  151. if ((!*pNotify) || ((*pNotify)->CNIndex < 0)) \
  152. { \
  153. return ERROR_INVALID_HANDLE; \
  154. } \
  155. #define ROUTE_LIST_FROM_HANDLE(ListHandle) \
  156. (PROUTE_LIST) GetObjectFromHandle(ListHandle, ROUTE_LIST_TYPE)
  157. #define VALIDATE_ROUTE_LIST_HANDLE(ListHandle, pList) \
  158. *pList = ROUTE_LIST_FROM_HANDLE(ListHandle); \
  159. if ((!*pList)) \
  160. { \
  161. return ERROR_INVALID_HANDLE; \
  162. } \
  163. #define LIST_ENUM_FROM_HANDLE(ListEnumHandle) \
  164. (PLIST_ENUM) GetObjectFromHandle(ListEnumHandle, LIST_ENUM_TYPE)
  165. #define VALIDATE_LIST_ENUM_HANDLE(ListEnumHandle, pListEnum) \
  166. *pListEnum = LIST_ENUM_FROM_HANDLE(ListEnumHandle); \
  167. if ((!*pListEnum)) \
  168. { \
  169. return ERROR_INVALID_HANDLE; \
  170. } \
  171. //
  172. // Defns used to maintain reference count on structures
  173. //
  174. ULONG
  175. __inline
  176. InitializeObjectReference(POBJECT_HEADER Object, UCHAR RefType)
  177. {
  178. UNREFERENCED_PARAMETER(RefType);
  179. #if DBG_REF
  180. InterlockedIncrement(&Object->RefTypes[RefType]);
  181. #endif
  182. return InterlockedIncrement(&Object->RefCount);
  183. }
  184. ULONG
  185. __inline
  186. ReferenceObject(POBJECT_HEADER Object, UCHAR RefType)
  187. {
  188. UNREFERENCED_PARAMETER(RefType);
  189. // Once ref falls to 0, this should never happen
  190. ASSERT(Object->RefCount > 0);
  191. #if DBG_REF
  192. ASSERT(Object->RefTypes[RefType] >= 0);
  193. InterlockedIncrement(&Object->RefTypes[RefType]);
  194. #endif
  195. return InterlockedIncrement(&Object->RefCount);
  196. }
  197. ULONG
  198. __inline
  199. DereferenceObject(POBJECT_HEADER Object, UCHAR RefType)
  200. {
  201. UNREFERENCED_PARAMETER(RefType);
  202. // Ref count should be +ve before we decrement it
  203. ASSERT(Object->RefCount > 0);
  204. #if DBG_REF
  205. ASSERT(Object->RefTypes[RefType] > 0);
  206. InterlockedDecrement(&Object->RefTypes[RefType]);
  207. #endif
  208. return InterlockedDecrement(&Object->RefCount);
  209. }
  210. #define INITIALIZE_INSTANCE_REFERENCE(Instance, RefType) \
  211. InitializeObjectReference(&Instance->ObjectHeader, RefType);
  212. #define REFERENCE_INSTANCE(Instance, RefType) \
  213. ReferenceObject(&Instance->ObjectHeader, RefType);
  214. #define DEREFERENCE_INSTANCE(Instance, RefType) \
  215. if (DereferenceObject(&Instance->ObjectHeader, RefType) == 0) \
  216. DestroyInstance(Instance);
  217. #define INITIALIZE_ADDR_FAMILY_REFERENCE(Instance, RefType) \
  218. InitializeObjectReference(&Instance->ObjectHeader, RefType);
  219. #define REFERENCE_ADDR_FAMILY(AddrFamilyInfo, RefType) \
  220. ReferenceObject(&AddrFamilyInfo->ObjectHeader, RefType);
  221. #define DEREFERENCE_ADDR_FAMILY(AddrFamInfo, RefType) \
  222. if (DereferenceObject(&AddrFamInfo->ObjectHeader,RefType) == 0) \
  223. DestroyAddressFamily(AddrFamInfo);
  224. #define INITIALIZE_ENTITY_REFERENCE(Entity, RefType) \
  225. InitializeObjectReference(&Entity->ObjectHeader, RefType);
  226. #define REFERENCE_ENTITY(Entity, RefType) \
  227. ReferenceObject(&Entity->ObjectHeader, RefType);
  228. #define DEREFERENCE_ENTITY(Entity, RefType) \
  229. if (DereferenceObject(&Entity->ObjectHeader, RefType) == 0) \
  230. DestroyEntity(Entity);
  231. #define INITIALIZE_DEST_REFERENCE(Dest, RefType) \
  232. InitializeObjectReference(&(Dest)->ObjectHeader, RefType);
  233. #define REFERENCE_DEST(Dest, RefType) \
  234. ReferenceObject(&(Dest)->ObjectHeader, RefType);
  235. #define DEREFERENCE_DEST(Dest, RefType) \
  236. if (DereferenceObject(&(Dest)->ObjectHeader, RefType) == 0) \
  237. DestroyDest(Dest);
  238. #define INITIALIZE_ROUTE_REFERENCE(Route, RefType) \
  239. InitializeObjectReference(&(Route)->ObjectHeader, RefType);
  240. #define REFERENCE_ROUTE(Route, RefType) \
  241. ReferenceObject(&(Route)->ObjectHeader, RefType);
  242. #define DEREFERENCE_ROUTE(Route, RefType) \
  243. if (DereferenceObject(&(Route)->ObjectHeader, RefType) == 0) \
  244. DestroyRoute(Route);
  245. #define INITIALIZE_NEXTHOP_REFERENCE(NextHop, RefType) \
  246. InitializeObjectReference(&(NextHop)->ObjectHeader, RefType);
  247. #define REFERENCE_NEXTHOP(NextHop, RefType) \
  248. ReferenceObject(&(NextHop)->ObjectHeader, RefType);
  249. #define DEREFERENCE_NEXTHOP(NextHop, RefType) \
  250. if (DereferenceObject(&(NextHop)->ObjectHeader, RefType) == 0) \
  251. DestroyNextHop(NextHop);
  252. //
  253. // Macros used to lock structures using critical sections
  254. //
  255. #define CREATE_LOCK(Lock) \
  256. InitializeCriticalSection((Lock))
  257. #define DELETE_LOCK(Lock) \
  258. DeleteCriticalSection((Lock))
  259. #define ACQUIRE_LOCK(Lock) \
  260. EnterCriticalSection((Lock))
  261. #define RELEASE_LOCK(Lock) \
  262. LeaveCriticalSection((Lock))
  263. //
  264. // Macros used to lock structures in read or write mode
  265. //
  266. typedef RTL_RESOURCE READ_WRITE_LOCK, *PREAD_WRITE_LOCK;
  267. #define CREATE_READ_WRITE_LOCK(pRWL) \
  268. RtlInitializeResource((pRWL))
  269. #define DELETE_READ_WRITE_LOCK(pRWL) \
  270. RtlDeleteResource((pRWL))
  271. #define READ_WRITE_LOCK_CREATED(pRWL) (TRUE)
  272. #define ACQUIRE_READ_LOCK(pRWL) \
  273. RtlAcquireResourceShared((pRWL),TRUE)
  274. #define RELEASE_READ_LOCK(pRWL) \
  275. RtlReleaseResource((pRWL))
  276. #define ACQUIRE_WRITE_LOCK(pRWL) \
  277. RtlAcquireResourceExclusive((pRWL),TRUE)
  278. #define RELEASE_WRITE_LOCK(pRWL) \
  279. RtlReleaseResource((pRWL))
  280. #define READ_LOCK_TO_WRITE_LOCK(pRWL) \
  281. RtlConvertSharedToExclusive((pRWL))
  282. #define WRITE_LOCK_TO_READ_LOCK(pRWL) \
  283. RtlConvertExclusiveToShared((pRWL))
  284. //
  285. // Macros to acquire and release dynamic R/W locks
  286. // [ This has been borrowed from the MGM libary ]
  287. //
  288. #define ACQUIRE_DYNAMIC_READ_LOCK(ppRWL) \
  289. AcquireReadLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  290. #define RELEASE_DYNAMIC_READ_LOCK(ppRWL) \
  291. ReleaseReadLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  292. #define ACQUIRE_DYNAMIC_WRITE_LOCK(ppRWL) \
  293. AcquireWriteLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  294. #define RELEASE_DYNAMIC_WRITE_LOCK(ppRWL) \
  295. ReleaseWriteLock((PMGM_READ_WRITE_LOCK *)ppRWL)
  296. //
  297. // Macros used in allocating and operating on memory
  298. //
  299. #define ZeroMemory RtlZeroMemory
  300. #define CopyMemory RtlCopyMemory
  301. #define CompareMemory RtlEqualMemory
  302. #define AllocOnStack(nb) _alloca((nb))
  303. #define AllocMemory(nb) HeapAlloc(RtmGlobals.GlobalHeap, \
  304. 0, \
  305. (nb))
  306. #define AllocNZeroMemory(nb) HeapAlloc(RtmGlobals.GlobalHeap, \
  307. HEAP_ZERO_MEMORY, \
  308. (nb))
  309. #define FreeMemory(ptr) HeapFree(RtmGlobals.GlobalHeap, \
  310. 0, \
  311. (ptr))
  312. #if !DBG_MEM
  313. #define AllocNZeroObject(nb) AllocNZeroMemory(nb)
  314. #else
  315. PVOID
  316. __inline
  317. AllocNZeroObject(UINT NumBytes)
  318. {
  319. OBJECT_HEADER *Object;
  320. Object = AllocNZeroMemory(NumBytes);
  321. if (Object)
  322. {
  323. ACQUIRE_ALLOCS_LIST_LOCK();
  324. InsertTailList(&RtmGlobals.AllocsList, &Object->AllocLE);
  325. RELEASE_ALLOCS_LIST_LOCK();
  326. }
  327. return Object;
  328. }
  329. #endif
  330. #if !DBG_MEM
  331. #define FreeObject(ptr) FreeMemory(ptr)
  332. #else
  333. VOID
  334. __inline
  335. FreeObject(PVOID Object)
  336. {
  337. ACQUIRE_ALLOCS_LIST_LOCK();
  338. RemoveEntryList(&((POBJECT_HEADER)Object)->AllocLE);
  339. RELEASE_ALLOCS_LIST_LOCK();
  340. FreeMemory(Object);
  341. }
  342. #endif
  343. //
  344. // Other Misc Macros
  345. //
  346. DWORD
  347. __inline
  348. NumBitsInDword (DWORD Dword)
  349. {
  350. DWORD NumBits = 0;
  351. while (Dword)
  352. {
  353. Dword &= (Dword - 1);
  354. NumBits++;
  355. }
  356. return NumBits;
  357. }
  358. #define NUMBER_OF_BITS NumBitsInDword
  359. //
  360. // Error Handling and other related macros
  361. //
  362. #define SUCCESS(code) (code == NO_ERROR)
  363. //
  364. // DLL Startup, Cleanup Functions and Macros
  365. //
  366. BOOL
  367. RtmDllStartup(
  368. VOID
  369. );
  370. BOOL
  371. RtmDllCleanup(
  372. VOID
  373. );
  374. DWORD
  375. RtmApiStartup(
  376. VOID
  377. );
  378. #define CHECK_FOR_RTM_API_INITIALIZED() \
  379. if (!RtmGlobals.ApiInitialized) \
  380. { \
  381. Status = RtmApiStartup(); \
  382. \
  383. if (Status != NO_ERROR) \
  384. { \
  385. return Status; \
  386. } \
  387. } \
  388. #endif //__ROUTING_RTMMAIN_H__