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.

273 lines
8.9 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. //
  4. // File: hshroute.h
  5. //
  6. // Description: Hash Routing functions. In many cases, domains are
  7. // identified by domain *and* routing informtion. This additional
  8. // information is identified by router GUID and a DWORD idenitfier.
  9. // This header provides macros to create unique domain-name-like strings
  10. // from the domain name and additional routing information.
  11. //
  12. // Author: Mike Swafford (MikeSwa)
  13. //
  14. // History:
  15. // 9/24/98 - MikeSwa Created
  16. //
  17. // Copyright (C) 1998 Microsoft Corporation
  18. //
  19. //-----------------------------------------------------------------------------
  20. #ifndef __HSHROUTE_H__
  21. #define __HSHROUTE_H__
  22. #include <dbgtrace.h>
  23. //The following is a list of 1 character "types" that can be used to identify
  24. //the type of routing information hashed. It is unlikely that this list will
  25. //grow significantly (or even at all).
  26. typedef enum
  27. {
  28. //Be sure to update FIRST_ROUTE_HASH_TYPE if you change the first type
  29. ROUTE_HASH_MESSAGE_TYPE = 0,
  30. ROUTE_HASH_SCHEDULE_ID,
  31. ROUTE_HASH_NUM_TYPES,
  32. } eRouteHashType;
  33. #define FIRST_ROUTE_HASH_TYPE ROUTE_HASH_MESSAGE_TYPE
  34. _declspec (selectany) CHAR g_rgHashTypeChars[ROUTE_HASH_NUM_TYPES+1] =
  35. {
  36. 'M', //ROUTE_HASH_MESSAGE_TYPE
  37. 'S', //ROUTE_HASH_SCHEDULE_ID
  38. '\0' //End of list
  39. };
  40. //---[ dwConvertHexChar ]------------------------------------------------------
  41. //
  42. //
  43. // Description:
  44. // Converts Hex character into an int from 0 to 15.
  45. // Parameters:
  46. // chHex Hex character to convert
  47. // Returns:
  48. // DWORD value between 0 and 15
  49. // History:
  50. // 9/24/98 - MikeSwa Created
  51. //
  52. //-----------------------------------------------------------------------------
  53. inline DWORD dwConvertHexChar(CHAR chHex)
  54. {
  55. DWORD dwValue = 0;
  56. if (('0' <= chHex) && ('9' >= chHex))
  57. dwValue = chHex-'0';
  58. else if (('a' <= chHex) && ('f' >= chHex))
  59. dwValue = 10 + chHex-'a';
  60. else if (('A' <= chHex) && ('F' >= chHex))
  61. dwValue = 10 + chHex-'A';
  62. else
  63. _ASSERT(0 && "Invalid hex character");
  64. _ASSERT(15 >= dwValue);
  65. return dwValue;
  66. }
  67. //Format is <type>.GUID.DWORD... each byte takes 2 chars to encode in HEX
  68. // eg - M.00112233445566778899AABBCCDDEEFF.00112233.foo.com
  69. //Offsets are defined to be used in ptr arith or array offsets
  70. #define ROUTE_HASH_CHARS_IN_DWORD (2*sizeof(DWORD))
  71. #define ROUTE_HASH_CHARS_IN_GUID (2*sizeof(GUID))
  72. #define ROUTE_HASH_TYPE_OFFSET 0
  73. #define ROUTE_HASH_GUID_OFFSET 2
  74. #define ROUTE_HASH_DWORD_OFFSET (3 + ROUTE_HASH_CHARS_IN_GUID)
  75. #define ROUTE_HASH_DOMAIN_OFFSET (4 + ROUTE_HASH_CHARS_IN_GUID + ROUTE_HASH_CHARS_IN_DWORD)
  76. #define ROUTE_HASH_PREFIX_SIZE sizeof(CHAR)*ROUTE_HASH_DOMAIN_OFFSET
  77. //---[ erhtGetRouteHashType ]---------------------------------------------------
  78. //
  79. //
  80. // Description:
  81. // For a given hashed domain, will return the assocaiated eRouteHashType
  82. // Parameters:
  83. // IN szHashedDomain Hashed domain
  84. // Returns:
  85. // eRouteHashType of hashed domain
  86. // ROUTE_HASH_NUM_TYPES (and asserts) if domain is not a valid route hash
  87. // History:
  88. // 9/24/98 - MikeSwa Created
  89. //
  90. //-----------------------------------------------------------------------------
  91. inline eRouteHashType erhtGetRouteHashType(LPSTR szHashedDomain)
  92. {
  93. eRouteHashType erthRet = FIRST_ROUTE_HASH_TYPE;
  94. PCHAR pchRouteHashType = g_rgHashTypeChars;
  95. CHAR chRouteHashType = szHashedDomain[ROUTE_HASH_TYPE_OFFSET];
  96. while (('\0' != *pchRouteHashType) &&
  97. (*pchRouteHashType != chRouteHashType))
  98. {
  99. pchRouteHashType++;
  100. erthRet = (eRouteHashType) (erthRet + 1);
  101. //we should not get to end of list
  102. _ASSERT(ROUTE_HASH_NUM_TYPES > erthRet);
  103. }
  104. return erthRet;
  105. }
  106. //---[ szGetDomainFromRouteHash ]----------------------------------------------
  107. //
  108. //
  109. // Description:
  110. // Returns the string of the unhashed domain from the hashed domain
  111. // string. Do *NOT* attempt to free this string.
  112. // Parameters:
  113. // IN szHashedDomain Hashed domain
  114. // Returns:
  115. // Original... unhashed domain
  116. // History:
  117. // 9/24/98 - MikeSwa Created
  118. //
  119. //-----------------------------------------------------------------------------
  120. inline LPSTR szGetDomainFromRouteHash(LPSTR szHashedDomain)
  121. {
  122. _ASSERT('.' == szHashedDomain[ROUTE_HASH_DOMAIN_OFFSET-1]);
  123. return (szHashedDomain + ROUTE_HASH_DOMAIN_OFFSET);
  124. }
  125. //---[ guidGetGUIDFromRouteHash ]----------------------------------------------
  126. //
  127. //
  128. // Description:
  129. // Extracts the guid from the route hash
  130. // Parameters:
  131. // IN szHashedDomain Hashed domain
  132. // IN OUT pguid Extracted GUID
  133. // Returns:
  134. // -
  135. // History:
  136. // 9/24/98 - MikeSwa Created
  137. //
  138. //-----------------------------------------------------------------------------
  139. inline void GetGUIDFromRouteHash(LPSTR szHashedDomain, IN OUT GUID *pguid)
  140. {
  141. DWORD *pdwGuid = (DWORD *) pguid;
  142. LPSTR szGUIDString = szHashedDomain+ROUTE_HASH_GUID_OFFSET;
  143. _ASSERT(sizeof(GUID)/sizeof(DWORD) == 4); //should be 4 DWORDs in GUID
  144. //Reverse operation of hash... convert to array of 4 DWORDs
  145. for (int i = 0; i < 4; i++)
  146. {
  147. pdwGuid[i] = 0;
  148. for (int j = 0;j < ROUTE_HASH_CHARS_IN_DWORD; j++)
  149. {
  150. pdwGuid[i] *= 16;
  151. pdwGuid[i] += dwConvertHexChar(*szGUIDString);
  152. szGUIDString++;
  153. }
  154. }
  155. _ASSERT('.' == *szGUIDString);
  156. }
  157. //---[ dwGetIDFromRouteHash ]--------------------------------------------------
  158. //
  159. //
  160. // Description:
  161. // Extracts DWORD ID from route hash
  162. // Parameters:
  163. // IN szHashedDomain Hashed domain
  164. // Returns:
  165. // DWORD ID encoded in route hash
  166. // History:
  167. // 9/24/98 - MikeSwa Created
  168. //
  169. //-----------------------------------------------------------------------------
  170. inline DWORD dwGetIDFromRouteHash(LPSTR szHashedDomain)
  171. {
  172. _ASSERT(szHashedDomain);
  173. LPSTR szDWORDString = szHashedDomain+ROUTE_HASH_DWORD_OFFSET;
  174. DWORD dwID = 0;
  175. for (int i = 0; i < ROUTE_HASH_CHARS_IN_DWORD; i++)
  176. {
  177. dwID *= 16;
  178. dwID += dwConvertHexChar(*szDWORDString);
  179. szDWORDString++;
  180. }
  181. _ASSERT('.' == *szDWORDString);
  182. return dwID;
  183. }
  184. //---[ dwGetSizeForRouteHash ]--------------------------------------------------
  185. //
  186. //
  187. // Description:
  188. // Returns the required sizeof the hash buffer from the string
  189. // length of the domain
  190. // Parameters:
  191. // IN cbDomainName string length of domain name (in bytes)
  192. // Returns:
  193. // size (in bytes) of buffer required to create route hash
  194. // History:
  195. // 9/24/98 - MikeSwa Created
  196. //
  197. //-----------------------------------------------------------------------------
  198. inline DWORD dwGetSizeForRouteHash(DWORD cbDomainName)
  199. {
  200. //include prefix size and NULL char
  201. return (cbDomainName + sizeof(CHAR) + ROUTE_HASH_PREFIX_SIZE);
  202. }
  203. //---[ CreateRouteHash ]-------------------------------------------------------
  204. //
  205. //
  206. // Description:
  207. // Creates a route-hashed domain name from a given domain name and
  208. // routing information.
  209. // Parameters:
  210. // IN cbDomainName string length of domain name (in bytes)
  211. // IN szDomainName Name of domain to hash
  212. // IN pguid Ptr to GUID of router
  213. // IN dwRouterID ID Provided by router
  214. // IN OUT szHashedDomain Buffer that is filled with route-hashed domain
  215. // name
  216. // IN cbHashedDomain Size of output buffer
  217. // Returns:
  218. // -
  219. // History:
  220. // 9/24/98 - MikeSwa Created
  221. //
  222. //-----------------------------------------------------------------------------
  223. inline void CreateRouteHash(IN DWORD cbDomainName,
  224. IN const LPSTR szDomainName,
  225. IN eRouteHashType erhtType,
  226. IN GUID *pguidRouter,
  227. IN DWORD dwRouterID,
  228. IN OUT LPSTR szHashedDomain,
  229. IN OUT DWORD cbHashedDomain)
  230. {
  231. _ASSERT(ROUTE_HASH_NUM_TYPES > erhtType);
  232. _ASSERT(pguidRouter);
  233. _ASSERT(sizeof(GUID) == 16);
  234. DWORD *pdwGuids = (DWORD *) pguidRouter;
  235. _ASSERT(cbHashedDomain >= dwGetSizeForRouteHash(cbDomainName));
  236. _snprintf(szHashedDomain, cbHashedDomain, "%c.%08X%08X%08X%08X.%08X.%s",
  237. g_rgHashTypeChars[erhtType], pdwGuids[0], pdwGuids[1], pdwGuids[2],
  238. pdwGuids[3], dwRouterID, szDomainName);
  239. // ensure that the string is terminated
  240. szHashedDomain[cbHashedDomain-1] = 0;
  241. _ASSERT('.' == szHashedDomain[ROUTE_HASH_GUID_OFFSET-1]);
  242. _ASSERT('.' == szHashedDomain[ROUTE_HASH_DWORD_OFFSET-1]);
  243. _ASSERT('.' == szHashedDomain[ROUTE_HASH_DOMAIN_OFFSET-1]);
  244. }
  245. #endif //__HSHROUTE_H__