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.

198 lines
5.6 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. wsctrl.c
  5. Abstract:
  6. Functions to talk to TCP/IP device driver
  7. Contents:
  8. WsControl
  9. Author:
  10. Richard L Firth (rfirth) 6-Aug-1994
  11. Revision History:
  12. rfirth 6-Aug-1994
  13. Created
  14. --*/
  15. #include "precomp.h"
  16. #include "ntddip6.h"
  17. #pragma hdrstop
  18. extern CRITICAL_SECTION g_stateLock;
  19. HANDLE TcpipDriverHandle = INVALID_HANDLE_VALUE;
  20. HANDLE Ip6DriverHandle = INVALID_HANDLE_VALUE;
  21. DWORD
  22. Ip6Control(
  23. DWORD Request,
  24. LPVOID InputBuffer,
  25. LPDWORD InputBufferLength,
  26. LPVOID OutputBuffer,
  27. LPDWORD OutputBufferLength
  28. )
  29. {
  30. BOOL ok;
  31. DWORD bytesReturned;
  32. HANDLE Handle;
  33. if (Ip6DriverHandle == INVALID_HANDLE_VALUE) {
  34. Handle = CreateFileW(WIN_IPV6_DEVICE_NAME,
  35. 0, // access mode
  36. FILE_SHARE_READ | FILE_SHARE_WRITE,
  37. NULL, // security attributes
  38. OPEN_EXISTING,
  39. 0, // flags & attributes
  40. NULL); // template file
  41. EnterCriticalSection(&g_stateLock);
  42. if (Ip6DriverHandle == INVALID_HANDLE_VALUE) {
  43. if (Handle == INVALID_HANDLE_VALUE) {
  44. LeaveCriticalSection(&g_stateLock);
  45. return GetLastError();
  46. } else {
  47. Ip6DriverHandle = Handle;
  48. }
  49. } else {
  50. CloseHandle(Handle);
  51. }
  52. LeaveCriticalSection(&g_stateLock);
  53. }
  54. ok = DeviceIoControl(Ip6DriverHandle,
  55. Request,
  56. InputBuffer,
  57. *InputBufferLength,
  58. OutputBuffer,
  59. *OutputBufferLength,
  60. &bytesReturned,
  61. NULL
  62. );
  63. if (!ok) {
  64. *OutputBufferLength = bytesReturned;
  65. return GetLastError();
  66. }
  67. *OutputBufferLength = bytesReturned;
  68. return NO_ERROR;
  69. }
  70. /*******************************************************************************
  71. *
  72. * WsControl
  73. *
  74. * ENTRY Protocol - ignored
  75. * Request - ignored
  76. * InputBuffer - pointer to request buffer
  77. * InputBufferLength - pointer to DWORD: IN = request buffer length
  78. * OutputBuffer - pointer to output buffer
  79. * OutputBufferLength - pointer to DWORD: IN = length of output buffer;
  80. * OUT = length of returned data
  81. *
  82. * EXIT OutputBuffer - contains queried info if successful
  83. * OutputBufferLength - contains number of bytes in OutputBuffer if
  84. * successful
  85. *
  86. * RETURNS Success = STATUS_SUCCESS/NO_ERROR
  87. * Failure = Win32 error code
  88. *
  89. * ASSUMES
  90. *
  91. ******************************************************************************/
  92. DWORD
  93. WsControl(
  94. DWORD Protocol,
  95. DWORD Request,
  96. LPVOID InputBuffer,
  97. LPDWORD InputBufferLength,
  98. LPVOID OutputBuffer,
  99. LPDWORD OutputBufferLength
  100. )
  101. {
  102. BOOL ok;
  103. DWORD bytesReturned;
  104. HANDLE Handle;
  105. UNREFERENCED_PARAMETER(Request);
  106. if (Protocol == IPPROTO_IPV6) {
  107. return Ip6Control(Request,
  108. InputBuffer,
  109. InputBufferLength,
  110. OutputBuffer,
  111. OutputBufferLength);
  112. }
  113. if (TcpipDriverHandle == INVALID_HANDLE_VALUE) {
  114. OBJECT_ATTRIBUTES objectAttributes;
  115. IO_STATUS_BLOCK iosb;
  116. UNICODE_STRING string;
  117. NTSTATUS status;
  118. RtlInitUnicodeString(&string, DD_TCP_DEVICE_NAME);
  119. InitializeObjectAttributes(&objectAttributes,
  120. &string,
  121. OBJ_CASE_INSENSITIVE,
  122. NULL,
  123. NULL
  124. );
  125. status = NtCreateFile(&Handle,
  126. SYNCHRONIZE | GENERIC_EXECUTE,
  127. &objectAttributes,
  128. &iosb,
  129. NULL,
  130. FILE_ATTRIBUTE_NORMAL,
  131. FILE_SHARE_READ | FILE_SHARE_WRITE,
  132. FILE_OPEN_IF,
  133. FILE_SYNCHRONOUS_IO_NONALERT,
  134. NULL,
  135. 0
  136. );
  137. EnterCriticalSection(&g_stateLock);
  138. if (TcpipDriverHandle == INVALID_HANDLE_VALUE) {
  139. if (!NT_SUCCESS(status)) {
  140. LeaveCriticalSection(&g_stateLock);
  141. return RtlNtStatusToDosError(status);
  142. } else {
  143. TcpipDriverHandle = Handle;
  144. }
  145. } else {
  146. NtClose(Handle);
  147. }
  148. LeaveCriticalSection(&g_stateLock);
  149. }
  150. ok = DeviceIoControl(TcpipDriverHandle,
  151. IOCTL_TCP_QUERY_INFORMATION_EX,
  152. InputBuffer,
  153. *InputBufferLength,
  154. OutputBuffer,
  155. *OutputBufferLength,
  156. &bytesReturned,
  157. NULL
  158. );
  159. if (!ok) {
  160. *OutputBufferLength = bytesReturned;
  161. return GetLastError();
  162. }
  163. *OutputBufferLength = bytesReturned;
  164. return NO_ERROR;
  165. }