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.

247 lines
5.3 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // EAPDnary.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file declares the class EAPTranslator.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 01/15/1998 Original version.
  16. // 05/08/1998 Do not restrict to attributes defined in raseapif.h.
  17. // Allow filtering of translated attributes.
  18. // 08/26/1998 Converted to a namespace.
  19. // 04/09/1999 Fix leak when converting outgoing attributes.
  20. // 04/17/2000 Port to new dictionary API.
  21. //
  22. ///////////////////////////////////////////////////////////////////////////////
  23. #include <ias.h>
  24. #include <iastlb.h>
  25. #include <iasutil.h>
  26. #include <hashmap.h>
  27. #include <eapdnary.h>
  28. namespace EAPTranslator
  29. {
  30. typedef hash_map < DWORD, IASTYPE, identity<DWORD> > TypeMap;
  31. long theRefCount; // Initialization refCount.
  32. TypeMap theTypeMap(0x200); // Maps attribute ID's to IASTYPE's.
  33. }
  34. HRESULT EAPTranslator::initialize() throw ()
  35. {
  36. std::_Lockit _Lk;
  37. if (theRefCount > 0)
  38. {
  39. // We're already initialized, so just bump the ref count.
  40. ++theRefCount;
  41. return S_OK;
  42. }
  43. try
  44. {
  45. // Names of various columns in the dictionary.
  46. const PCWSTR COLUMNS[] =
  47. {
  48. L"ID",
  49. L"Syntax",
  50. NULL
  51. };
  52. // Open the attributes table.
  53. IASTL::IASDictionary dnary(COLUMNS);
  54. // Iterate through the attributes and populate our dictionary.
  55. while (dnary.next())
  56. {
  57. DWORD id = (DWORD)dnary.getLong(0);
  58. IASTYPE type = (IASTYPE)dnary.getLong(1);
  59. theTypeMap[id] = type;
  60. }
  61. }
  62. catch (std::bad_alloc)
  63. {
  64. theTypeMap.clear();
  65. return E_OUTOFMEMORY;
  66. }
  67. catch (const _com_error& ce)
  68. {
  69. theTypeMap.clear();
  70. return ce.Error();
  71. }
  72. // We made it so increase the refCount.
  73. ++theRefCount;
  74. return S_OK;
  75. }
  76. void EAPTranslator::finalize() throw ()
  77. {
  78. std::_Lockit _Lk;
  79. if (--theRefCount == 0)
  80. {
  81. theTypeMap.clear();
  82. }
  83. }
  84. BOOL EAPTranslator::translate(
  85. IASAttribute& dst,
  86. const RAS_AUTH_ATTRIBUTE& src
  87. )
  88. {
  89. dst->dwId = (DWORD)src.raaType;
  90. const TypeMap::value_type* val = theTypeMap.find(dst->dwId);
  91. IASTYPE itType = val ? val->second : IASTYPE_INVALID;
  92. switch (itType)
  93. {
  94. case IASTYPE_BOOLEAN:
  95. case IASTYPE_INTEGER:
  96. case IASTYPE_ENUM:
  97. case IASTYPE_INET_ADDR:
  98. {
  99. switch (src.dwLength)
  100. {
  101. case 4:
  102. dst->Value.Integer = *(PDWORD)src.Value;
  103. case 2:
  104. dst->Value.Integer = *(PWORD)src.Value;
  105. case 1:
  106. dst->Value.Integer = *(PBYTE)src.Value;
  107. default:
  108. _com_issue_error(E_INVALIDARG);
  109. }
  110. break;
  111. }
  112. case IASTYPE_STRING:
  113. {
  114. dst.setString(src.dwLength, (const BYTE*)src.Value);
  115. break;
  116. }
  117. case IASTYPE_OCTET_STRING:
  118. case IASTYPE_PROV_SPECIFIC:
  119. {
  120. dst.setOctetString(src.dwLength, (const BYTE*)src.Value);
  121. break;
  122. }
  123. default:
  124. return FALSE;
  125. }
  126. dst->Value.itType = itType;
  127. return TRUE;
  128. }
  129. BOOL EAPTranslator::translate(
  130. RAS_AUTH_ATTRIBUTE& dst,
  131. const IASATTRIBUTE& src
  132. )
  133. {
  134. dst.raaType = (RAS_AUTH_ATTRIBUTE_TYPE)src.dwId;
  135. switch (src.Value.itType)
  136. {
  137. case IASTYPE_BOOLEAN:
  138. case IASTYPE_INTEGER:
  139. case IASTYPE_ENUM:
  140. case IASTYPE_INET_ADDR:
  141. {
  142. dst.dwLength = sizeof(DWORD);
  143. dst.Value = (PVOID)&(src.Value.Integer);
  144. break;
  145. }
  146. case IASTYPE_STRING:
  147. {
  148. IASAttributeAnsiAlloc(const_cast<PIASATTRIBUTE>(&src));
  149. if (src.Value.String.pszAnsi)
  150. {
  151. dst.dwLength = strlen(src.Value.String.pszAnsi) + 1;
  152. dst.Value = (PVOID)src.Value.String.pszAnsi;
  153. }
  154. else
  155. {
  156. dst.dwLength = 0;
  157. dst.Value = NULL;
  158. }
  159. break;
  160. }
  161. case IASTYPE_OCTET_STRING:
  162. case IASTYPE_PROV_SPECIFIC:
  163. {
  164. dst.dwLength = src.Value.OctetString.dwLength;
  165. dst.Value = (PVOID)src.Value.OctetString.lpValue;
  166. break;
  167. }
  168. default:
  169. return FALSE;
  170. }
  171. return TRUE;
  172. }
  173. void EAPTranslator::translate(
  174. IASAttributeVector& dst,
  175. const RAS_AUTH_ATTRIBUTE* src
  176. )
  177. {
  178. IASAttribute attr;
  179. const RAS_AUTH_ATTRIBUTE* i;
  180. for (i = src; i->raaType != raatMinimum; ++i)
  181. {
  182. attr.alloc();
  183. if (translate(attr, *i))
  184. {
  185. dst.push_back(attr, false);
  186. attr.detach();
  187. }
  188. }
  189. }
  190. void EAPTranslator::translate(
  191. PRAS_AUTH_ATTRIBUTE dst,
  192. const IASAttributeVector& src,
  193. DWORD filter
  194. )
  195. {
  196. PRAS_AUTH_ATTRIBUTE next = dst;
  197. IASAttributeVector::const_iterator i;
  198. for (i = src.begin(); i != src.end(); ++i)
  199. {
  200. if ((i->pAttribute->dwFlags & filter) != 0 &&
  201. translate(*next, *(i->pAttribute)))
  202. {
  203. ++next;
  204. }
  205. }
  206. next->raaType = raatMinimum;
  207. }