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.

446 lines
15 KiB

  1. /*++
  2. Copyright (c) 1997 - 98, Microsoft Corporation
  3. Module Name:
  4. rtm1to2.h
  5. Abstract:
  6. Contains definitions/macros that wrap RTMv2
  7. in the RTMv1 API.
  8. Author:
  9. Chaitanya Kodeboyina (chaitk) 13-Oct-1998
  10. Revision History:
  11. --*/
  12. #ifndef __ROUTING_RTM1TO2_H__
  13. #define __ROUTING_RTM1TO2_H__
  14. #include <winsock2.h>
  15. #include <rtm.h>
  16. #include <rmrtm.h>
  17. // Protocol Id for the default wrapper registration
  18. #define V1_WRAPPER_REGN_ID 0xA1B2C3D4
  19. // Protocol instance for a v1 entity registration
  20. #define V1_PROTOCOL_INSTANCE 0xABCD1234
  21. //
  22. // Misc defines for wrapper picked from rtmv2p.h
  23. //
  24. #define MAXTICKS MAXULONG
  25. // Basic route info, present in routes of all types
  26. // Disable warnings for unnamed structs
  27. #pragma warning(disable : 4201)
  28. typedef struct
  29. {
  30. ROUTE_HEADER;
  31. }
  32. RTM_XX_ROUTE, *PRTM_XX_ROUTE;
  33. #pragma warning(default : 4201)
  34. //
  35. // Extending certain V1 flags in the wrapper
  36. //
  37. #define RTM_ONLY_OWND_ROUTES 0x00000010
  38. //
  39. // Mapping of v1 address families to standard ids
  40. //
  41. const USHORT ADDRESS_FAMILY[2] =
  42. {
  43. AF_IPX, // RTM_PROTOCOL_FAMILY_IPX = 0
  44. AF_INET // RTM_PROTOCOL_FAMILY_IP = 1
  45. };
  46. //
  47. // Address sizes of supported address families
  48. //
  49. #define IPX_ADDR_SIZE 6
  50. #define IP_ADDR_SIZE 4
  51. const USHORT ADDRESS_SIZE[2] =
  52. {
  53. IPX_ADDR_SIZE, // RTM_PROTOCOL_FAMILY_IPX = 0
  54. IP_ADDR_SIZE // RTM_PROTOCOL_FAMILY_IP = 1
  55. };
  56. //
  57. // Forward declarations for structs
  58. //
  59. typedef struct _V1_REGN_INFO *PV1_REGN_INFO;
  60. //
  61. // Global info for RTMv1 - v2 wrapper
  62. //
  63. typedef struct _V1_GLOBAL_INFO
  64. {
  65. CRITICAL_SECTION PfRegnsLock[RTM_NUM_OF_PROTOCOL_FAMILIES];
  66. // Lock guards the registrations list
  67. LIST_ENTRY PfRegistrations[RTM_NUM_OF_PROTOCOL_FAMILIES];
  68. // List of regns on Protocol family
  69. PV1_REGN_INFO PfRegInfo[RTM_NUM_OF_PROTOCOL_FAMILIES];
  70. // Default regn for this Protocol family
  71. PROUTE_VALIDATE_FUNC
  72. PfValidateRouteFunc[RTM_NUM_OF_PROTOCOL_FAMILIES];
  73. // Func to validate route, fill priority
  74. }
  75. V1_GLOBAL_INFO, *PV1_GLOBAL_INFO;
  76. //
  77. // RTMv2 to v1 Registration Wrapper
  78. //
  79. typedef struct _V1_REGN_INFO
  80. {
  81. OBJECT_HEADER ObjectHeader; // Signature, Type and Reference Count
  82. LIST_ENTRY RegistrationsLE; // Linkage on list of registrations
  83. DWORD ProtocolFamily; // This maps to RTMv2's address family
  84. DWORD RoutingProtocol; // Routing protocol (RIP, OSPF...)
  85. DWORD Flags; // RTMv1 Registration flags
  86. RTM_ENTITY_HANDLE Rtmv2RegHandle; // Handle to actual RTMv2 registration
  87. RTM_REGN_PROFILE Rtmv2Profile; // RTMv2 registration profile
  88. UINT Rtmv2NumViews; // Number of views in the V2 instance
  89. CRITICAL_SECTION NotificationLock; // RTMv1 Notification Lock
  90. PROUTE_CHANGE_CALLBACK
  91. NotificationFunc; // RTMv1 Notification Callback
  92. HANDLE NotificationEvent;// RTMv1 Notification Event
  93. RTM_NOTIFY_HANDLE Rtmv2NotifyHandle;// RTMv2 Notification Handle
  94. }
  95. V1_REGN_INFO, *PV1_REGN_INFO;
  96. //
  97. // RTMv1 Route Info structure
  98. //
  99. typedef union {
  100. RTM_IPX_ROUTE IpxRoute; // IPX route info structure
  101. RTM_IP_ROUTE IpRoute; // IP route info structure
  102. RTM_XX_ROUTE XxRoute; // The Common route header
  103. UCHAR Route[1]; // Generic route info structure
  104. }
  105. V1_ROUTE_INFO, *PV1_ROUTE_INFO;
  106. //
  107. // RTMv2 to v1 Enumeration Wrapper
  108. //
  109. typedef struct _V1_ENUM_INFO
  110. {
  111. OBJECT_HEADER ObjectHeader; // Signature, Type and Reference Count
  112. DWORD ProtocolFamily; // This maps to RTMv2's address family
  113. DWORD EnumFlags; // RTMv1 Enumeration flags
  114. V1_ROUTE_INFO CriteriaRoute; // V1 Criteria route for this enum
  115. CRITICAL_SECTION EnumLock; // To serialize enumeration calls
  116. RTM_ENUM_HANDLE Rtmv2RouteEnum; // Handle to the RTMv2 route enum
  117. }
  118. V1_ENUM_INFO, *PV1_ENUM_INFO;
  119. //
  120. // Miscellaneos Func Pointer Defs
  121. //
  122. typedef BOOL (*PFUNC) (PVOID p, PVOID q, PVOID r);
  123. //
  124. // Macros to validate RTMv1 to v2 wrapper handles
  125. //
  126. #define V1_REGN_FROM_HANDLE(V1RegnHandle) \
  127. (PV1_REGN_INFO) GetObjectFromHandle(V1RegnHandle, V1_REGN_TYPE)
  128. #define VALIDATE_V1_REGN_HANDLE(V1RegnHandle, pV1Regn) \
  129. *pV1Regn = V1_REGN_FROM_HANDLE(V1RegnHandle); \
  130. if ((!*pV1Regn)) \
  131. { \
  132. return ERROR_INVALID_HANDLE; \
  133. } \
  134. #define V1_ENUM_FROM_HANDLE(V1EnumHandle) \
  135. (PV1_ENUM_INFO) GetObjectFromHandle(V1EnumHandle, V1_ENUM_TYPE)
  136. #define VALIDATE_V1_ENUM_HANDLE(V1EnumHandle, pV1Enum) \
  137. *pV1Enum = V1_ENUM_FROM_HANDLE(V1EnumHandle); \
  138. if ((!*pV1Enum)) \
  139. { \
  140. return ERROR_INVALID_HANDLE; \
  141. } \
  142. //
  143. // Macros to acquire locks in the structures above
  144. //
  145. #define ACQUIRE_V1_REGNS_LOCK(ProtocolFamily) \
  146. ACQUIRE_LOCK(&V1Globals.PfRegnsLock[ProtocolFamily])
  147. #define RELEASE_V1_REGNS_LOCK(ProtocolFamily) \
  148. RELEASE_LOCK(&V1Globals.PfRegnsLock[ProtocolFamily])
  149. #define ACQUIRE_V1_ENUM_LOCK(V1Enum) \
  150. ACQUIRE_LOCK(&V1Enum->EnumLock)
  151. #define RELEASE_V1_ENUM_LOCK(V1Enum) \
  152. RELEASE_LOCK(&V1Enum->EnumLock)
  153. #define ACQUIRE_V1_NOTIFY_LOCK(V1Regn) \
  154. ACQUIRE_LOCK(&V1Regn->NotificationLock)
  155. #define RELEASE_V1_NOTIFY_LOCK(V1Regn) \
  156. RELEASE_LOCK(&V1Regn->NotificationLock)
  157. //
  158. // Macros to convert RTMv1 to RTMv2 structs and vice versa
  159. //
  160. #define MakeNetAddress(Network, ProtocolFamily, TempUlong, NetAddr) \
  161. MakeNetAddressForIP(Network, TempUlong, NetAddr)
  162. #define MakeNetAddressForIP(Network, TempUlong, NetAddr) \
  163. (NetAddr)->AddressFamily = AF_INET; \
  164. (NetAddr)->NumBits = 0; \
  165. \
  166. TempUlong = \
  167. RtlUlongByteSwap(((PIP_NETWORK)(Network))->N_NetMask); \
  168. \
  169. while (TempUlong) \
  170. { \
  171. ASSERT(TempUlong & 0x80000000); \
  172. TempUlong <<= 1; \
  173. (NetAddr)->NumBits++; \
  174. } \
  175. \
  176. (* (ULONG *) ((NetAddr)->AddrBits)) = \
  177. ((PIP_NETWORK) (Network))->N_NetNumber; \
  178. #define MakeHostAddress(HostAddr, ProtocolFamily, NetAddr) \
  179. MakeHostAddressForIP(HostAddr, NetAddr)
  180. #define MakeHostAddressForIP(HostAddr, NetAddr) \
  181. (NetAddr)->AddressFamily = AF_INET; \
  182. (NetAddr)->NumBits = IP_ADDR_SIZE * BITS_IN_BYTE; \
  183. (* (ULONG *) ((NetAddr)->AddrBits)) = (* (ULONG *) HostAddr); \
  184. //
  185. // Misc V1 Macros
  186. //
  187. // Macro that gets the network address in the route
  188. #define V1GetRouteNetwork(Route, ProtocolFamily, Network) \
  189. if (ProtocolFamily == RTM_PROTOCOL_FAMILY_IP) \
  190. { \
  191. (*Network) = (PVOID) &((PRTM_IP_ROUTE) Route)->RR_Network; \
  192. } \
  193. else \
  194. { \
  195. (*Network) = (PVOID) &((PRTM_IPX_ROUTE) Route)->RR_Network; \
  196. } \
  197. // Macro that gets the addr of flags in the route
  198. #define V1GetRouteFlags(Route, ProtocolFamily, Flags) \
  199. if (ProtocolFamily == RTM_PROTOCOL_FAMILY_IP) \
  200. { \
  201. Flags = \
  202. &((PRTM_IP_ROUTE)Route)->RR_FamilySpecificData.FSD_Flags; \
  203. } \
  204. else \
  205. { \
  206. Flags = \
  207. &((PRTM_IPX_ROUTE)Route)->RR_FamilySpecificData.FSD_Flags; \
  208. } \
  209. // Macro that copies one v1 route to another
  210. #define V1CopyRoute(RouteDst, RouteSrc, ProtocolFamily) \
  211. if (ProtocolFamily == RTM_PROTOCOL_FAMILY_IP) \
  212. { \
  213. CopyMemory(RouteDst, RouteSrc, sizeof(RTM_IP_ROUTE)); \
  214. } \
  215. else \
  216. { \
  217. CopyMemory(RouteDst, RouteSrc, sizeof(RTM_IPX_ROUTE)); \
  218. } \
  219. //
  220. // Misc V2 Macros
  221. //
  222. // Macro to allocate a set of handles on the stack
  223. #define ALLOC_HANDLES(NumHandles) \
  224. (HANDLE *) _alloca(sizeof(HANDLE) * NumHandles) \
  225. // Macro to allocate a RTM_DEST_INFO on the stack
  226. #define ALLOC_DEST_INFO(NumViews, NumInfos) \
  227. (PRTM_DEST_INFO) _alloca(RTM_SIZE_OF_DEST_INFO(NumViews) * NumInfos)
  228. // Macro to allocate a RTM_ROUTE_INFO on the stack
  229. #define ALLOC_ROUTE_INFO(NumNextHops, NumInfos) \
  230. (PRTM_ROUTE_INFO) _alloca((sizeof(RTM_ROUTE_INFO) + \
  231. (NumNextHops - 1) * \
  232. sizeof(RTM_NEXTHOP_HANDLE)) * NumInfos) \
  233. // Misc Macros
  234. #define SWAP_POINTERS(p1, p2) { PVOID p = p1; p1 = p2; p2 = p; }
  235. //
  236. // Callback that converts RTMv2 events to RTMv1
  237. //
  238. DWORD
  239. WINAPI
  240. V2EventCallback (
  241. IN RTM_ENTITY_HANDLE Rtmv2RegHandle,
  242. IN RTM_EVENT_TYPE EventType,
  243. IN PVOID Context1,
  244. IN PVOID Context2
  245. );
  246. //
  247. // Other helper functions
  248. //
  249. HANDLE
  250. RtmpRegisterClient (
  251. IN DWORD ProtocolFamily,
  252. IN DWORD RoutingProtocol,
  253. IN PROUTE_CHANGE_CALLBACK ChangeFunc OPTIONAL,
  254. IN HANDLE ChangeEvent OPTIONAL,
  255. IN DWORD Flags
  256. );
  257. DWORD
  258. BlockOperationOnRoutes (
  259. IN PV1_REGN_INFO V1Regn,
  260. IN DWORD EnumerationFlags,
  261. IN PVOID CriteriaRoute,
  262. IN PFUNC RouteOperation
  263. );
  264. BOOL
  265. MatchCriteriaAndCopyRoute (
  266. IN PV1_REGN_INFO V1Regn,
  267. IN PRTM_ROUTE_HANDLE V2RouteHandle,
  268. IN PV1_ENUM_INFO V1Enum OPTIONAL,
  269. OUT PVOID V1Route OPTIONAL
  270. );
  271. #define MatchCriteria(R, H, E) MatchCriteriaAndCopyRoute(R, H, E, NULL)
  272. BOOL
  273. MatchCriteriaAndDeleteRoute (
  274. IN PV1_REGN_INFO V1Regn,
  275. IN PRTM_ROUTE_HANDLE V2RouteHandle,
  276. IN PV1_ENUM_INFO V1Enum
  277. );
  278. BOOL
  279. MatchCriteriaAndChangeOwner (
  280. IN PV1_REGN_INFO V1Regn,
  281. IN PRTM_ROUTE_HANDLE V2RouteHandle,
  282. IN PV1_ENUM_INFO V1Enum
  283. );
  284. BOOL
  285. MatchCriteriaAndEnableRoute (
  286. IN PV1_REGN_INFO V1Regn,
  287. IN PRTM_ROUTE_HANDLE V2RouteHandle,
  288. IN PV1_ENUM_INFO V1Enum
  289. );
  290. BOOL
  291. CopyNonLoopbackIPRoute (
  292. IN PV1_REGN_INFO V1Regn,
  293. IN PRTM_DEST_INFO V2DestInfo,
  294. OUT PVOID V1Route
  295. );
  296. VOID
  297. MakeV2RouteFromV1Route (
  298. IN PV1_REGN_INFO V1Regn,
  299. IN PVOID V1Route,
  300. IN PRTM_NEXTHOP_HANDLE V2NextHop,
  301. OUT PRTM_NET_ADDRESS V2DestAddr OPTIONAL,
  302. OUT PRTM_ROUTE_INFO V2RouteInfo OPTIONAL
  303. );
  304. VOID
  305. MakeV2NextHopFromV1Route (
  306. IN PV1_REGN_INFO V1Regn,
  307. IN PVOID V1Route,
  308. OUT PRTM_NEXTHOP_INFO V2NextHop
  309. );
  310. VOID
  311. MakeV1RouteFromV2Dest (
  312. IN PV1_REGN_INFO V1Regn,
  313. IN PRTM_DEST_INFO DestInfo,
  314. OUT PVOID V1Route
  315. );
  316. DWORD
  317. MakeV1RouteFromV2Route (
  318. IN PV1_REGN_INFO V1Regn,
  319. IN PRTM_ROUTE_INFO V2Route,
  320. OUT PVOID V1Route
  321. );
  322. #endif // __ROUTING_RTM1TO2_H__