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.

287 lines
6.5 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. handle.cxx
  5. Abstract:
  6. Contains functions to allocate and deallocate handle values
  7. Contents:
  8. HandleInitialize
  9. HandleTerminate
  10. AllocateHandle
  11. FreeHandle
  12. MapHandleToAddress
  13. DereferenceObject
  14. Author:
  15. Richard L Firth (rfirth) 31-Oct-1994
  16. Revision History:
  17. 12-Mar-2001 rajeevd
  18. Gutted
  19. 11-Jan-1996 rfirth
  20. Use fixed memory instead of moveable (Win95 has a bug w/ LocalUnlock)
  21. 31-Oct-1994 rfirth
  22. Created
  23. --*/
  24. #include <wininetp.h>
  25. //
  26. // private prototypes
  27. //
  28. DEBUG_ONLY (PRIVATE void LogHandleClassSizes(); )
  29. //
  30. // functions
  31. //
  32. DWORD HandleInitialize (VOID)
  33. {
  34. DEBUG_ONLY (LogHandleClassSizes(); )
  35. return ERROR_SUCCESS;
  36. }
  37. VOID HandleTerminate(VOID)
  38. {
  39. // nothing to do
  40. }
  41. DWORD AllocateHandle (IN LPVOID Address,OUT LPHINTERNET lpHandle)
  42. {
  43. HINTERNET Handle = (HINTERNET) Address;
  44. *lpHandle = Handle;
  45. return ERROR_SUCCESS;
  46. }
  47. DWORD FreeHandle(IN HINTERNET Handle)
  48. {
  49. return ERROR_SUCCESS;
  50. }
  51. DWORD
  52. MapHandleToAddress(
  53. IN HINTERNET Handle,
  54. OUT LPVOID * lpAddress,
  55. IN BOOL Invalidate
  56. )
  57. /*++
  58. Routine Description:
  59. The handle object represented by Handle is referenced
  60. Assumes: 1. only HINTERNETs visible at the API are presented to this
  61. function.
  62. Arguments:
  63. Handle - handle value generated by AllocateHandle()
  64. lpAddress - place to store mapped address. If the handle has been closed
  65. and unmapped, NULL is returned. If the handle is still
  66. mapped, even though it has been invalidated, its address will
  67. be returned, and its reference count incremented
  68. Invalidate - TRUE if we are invalidating this handle
  69. Return Value:
  70. LPVOID
  71. Success - ERROR_SUCCESS
  72. Failure - ERROR_INVALID_HANDLE
  73. if *lpAddress == NULL then the handle has been closed and
  74. unmapped, else it is still mapped, but invalidated. In
  75. this case, we incremented the reference count
  76. --*/
  77. {
  78. DEBUG_ENTER((DBG_HANDLE,
  79. Dword,
  80. "MapHandleToAddress",
  81. "%#x, %#x, %B",
  82. Handle,
  83. lpAddress,
  84. Invalidate
  85. ));
  86. DWORD error;
  87. // Cast the handle to an address and validate
  88. LPVOID address = (LPVOID) Handle;
  89. if (address)
  90. {
  91. error = ((HANDLE_OBJECT *)address)->IsValid(TypeWildHandle);
  92. }
  93. else
  94. {
  95. error = ERROR_INVALID_HANDLE;
  96. }
  97. if (error != ERROR_SUCCESS)
  98. {
  99. DEBUG_PRINT(HANDLE,
  100. ERROR,
  101. ("invalid handle object: %#x [%#x]\n",
  102. Handle,
  103. address
  104. ));
  105. error = ERROR_INVALID_HANDLE;
  106. address = NULL;
  107. goto quit;
  108. }
  109. // Attempt to increment the reference count on the handle.
  110. error = ((HANDLE_OBJECT *)address)->Reference();
  111. DEBUG_PRINT(HANDLE, ERROR, ("Reference() returns %d\n", error));
  112. switch (error)
  113. {
  114. case ERROR_SUCCESS: // handle was refcounted but is not tombstoned
  115. if (Invalidate)
  116. {
  117. // we were called from a handle close API.
  118. // Subsequent API calls will discover that the
  119. // handle is already invalidated and will quit
  120. ((HANDLE_OBJECT *)address)->Invalidate();
  121. }
  122. break;
  123. case ERROR_INVALID_HANDLE: // handle was refcounted but was tombstoned.
  124. break;
  125. default:
  126. INET_ASSERT (false);
  127. // intentional fall through
  128. case ERROR_ACCESS_DENIED: // handle is being destroyed
  129. DEBUG_PRINT(HANDLE,
  130. ERROR,
  131. ("Reference() failed - handle %#x [%#x] about to be deleted\n",
  132. Handle,
  133. address
  134. ));
  135. error = ERROR_INVALID_HANDLE;
  136. address = NULL;
  137. break;
  138. }
  139. quit:
  140. *lpAddress = address;
  141. DEBUG_LEAVE(error);
  142. return error;
  143. }
  144. DWORD
  145. DereferenceObject(
  146. IN LPVOID lpObject
  147. )
  148. /*++
  149. Routine Description:
  150. Undoes the reference added to the handle object by MapHandleToAddress(). May
  151. result in the handle object being deleted
  152. Arguments:
  153. lpObject - address of object to dereference. This MUST be the mapped
  154. object address as returned by MapHandleToAddress()
  155. Return Value:
  156. DWORD
  157. Success - ERROR_SUCCESS
  158. The handle object was destroyed
  159. Failure - ERROR_INVALID_HANDLE
  160. The object was not a valid handle
  161. --*/
  162. {
  163. DEBUG_ENTER((DBG_HANDLE,
  164. Dword,
  165. "DereferenceObject",
  166. "%#x",
  167. lpObject
  168. ));
  169. INET_ASSERT(lpObject != NULL);
  170. HANDLE_OBJECT * object = (HANDLE_OBJECT *)lpObject;
  171. DWORD error = object->IsValid(TypeWildHandle);
  172. // Serialize with MapHandleToAddress
  173. if (error == ERROR_SUCCESS)
  174. {
  175. object->Dereference();
  176. }
  177. else
  178. {
  179. //
  180. // IsValid() should never return an error if the reference counts
  181. // are correct
  182. //
  183. INET_ASSERT(FALSE);
  184. }
  185. DEBUG_LEAVE(error);
  186. return error;
  187. }
  188. #if INET_DEBUG
  189. PRIVATE
  190. void
  191. LogHandleClassSizes()
  192. {
  193. DEBUG_PRINT(HANDLE,
  194. INFO,
  195. ("sizeof(HANDLE_OBJECT) = %d bytes\n",
  196. sizeof(HANDLE_OBJECT)
  197. ));
  198. DEBUG_PRINT(HANDLE,
  199. INFO,
  200. ("sizeof(INTERNET_HANDLE_OBJECT) = %d bytes\n",
  201. sizeof(INTERNET_HANDLE_OBJECT)
  202. ));
  203. DEBUG_PRINT(HANDLE,
  204. INFO,
  205. ("sizeof(INTERNET_CONNECT_HANDLE_OBJECT) = %d bytes\n",
  206. sizeof(INTERNET_CONNECT_HANDLE_OBJECT)
  207. ));
  208. DEBUG_PRINT(HANDLE,
  209. INFO,
  210. ("sizeof(HTTP_REQUEST_HANDLE_OBJECT) = %d bytes\n",
  211. sizeof(HTTP_REQUEST_HANDLE_OBJECT)
  212. ));
  213. }
  214. #endif