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.

316 lines
7.3 KiB

  1. //++
  2. //
  3. // Copyright (C) Microsoft Corporation, 1987 - 1999
  4. //
  5. // Module Name:
  6. //
  7. // xportst.c
  8. //
  9. // Abstract:
  10. //
  11. // Tests for the transports on the local workstation
  12. //
  13. // Author:
  14. //
  15. // 1-Feb-1998 (karolys)
  16. //
  17. // Environment:
  18. //
  19. // User mode only.
  20. // Contains NT-specific code.
  21. //
  22. // Revision History:
  23. //
  24. //--
  25. /*==========================< Include files >==============================*/
  26. #include "precomp.h"
  27. #define BUFF_SIZE 650
  28. /*===========================< NetBT vars >===============================*/
  29. #define NETBIOS_NAME_SIZE 16
  30. /*==========================< DHCP Include>==============================*/
  31. #include "dhcptest.h"
  32. /*=======================< Function prototypes >=================================*/
  33. DWORD
  34. OpenDriver(
  35. OUT HANDLE *Handle,
  36. IN LPWSTR DriverName
  37. )
  38. //++
  39. //
  40. // Routine Description:
  41. //
  42. // This function opens a specified IO drivers.
  43. //
  44. // Arguments:
  45. //
  46. // Handle - pointer to location where the opened drivers handle is
  47. // returned.
  48. //
  49. // DriverName - name of the driver to be opened.
  50. //
  51. // Return Value:
  52. //
  53. // Windows Error Code.
  54. //--
  55. {
  56. OBJECT_ATTRIBUTES objectAttributes;
  57. IO_STATUS_BLOCK ioStatusBlock;
  58. UNICODE_STRING nameString;
  59. NTSTATUS status;
  60. *Handle = NULL;
  61. //
  62. // Open a Handle to the IP driver.
  63. //
  64. RtlInitUnicodeString(&nameString, DriverName);
  65. InitializeObjectAttributes(
  66. &objectAttributes,
  67. &nameString,
  68. OBJ_CASE_INSENSITIVE,
  69. (HANDLE) NULL,
  70. (PSECURITY_DESCRIPTOR) NULL
  71. );
  72. status = NtCreateFile(
  73. Handle,
  74. SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA,
  75. &objectAttributes,
  76. &ioStatusBlock,
  77. NULL,
  78. FILE_ATTRIBUTE_NORMAL,
  79. FILE_SHARE_READ | FILE_SHARE_WRITE,
  80. FILE_OPEN_IF,
  81. 0,
  82. NULL,
  83. 0
  84. );
  85. return( RtlNtStatusToDosError( status ) );
  86. }
  87. //-------------------------------------------------------------------------//
  88. //###### I s I c m p R e s p o n s e () #################################//
  89. //-------------------------------------------------------------------------//
  90. BOOL
  91. IsIcmpResponseA(
  92. LPCSTR pszIpAddrStr
  93. )
  94. //++
  95. //
  96. // Routine Description:
  97. //
  98. // Sends ICMP echo request frames to the IP address specified.
  99. //
  100. // Arguments:
  101. //
  102. // pszIAddrStr - address to ping
  103. //
  104. // Return Value:
  105. //
  106. // TRUE: Test suceeded.
  107. // FALSE: Test failed
  108. //
  109. //--
  110. {
  111. char *SendBuffer, *RcvBuffer;
  112. int i, nReplyCnt;
  113. int nReplySum = 0;
  114. HANDLE hIcmp;
  115. PICMP_ECHO_REPLY reply;
  116. //
  117. // contact ICMP driver
  118. //
  119. hIcmp = IcmpCreateFile();
  120. if ( hIcmp == INVALID_HANDLE_VALUE )
  121. {
  122. DebugMessage( " [FATAL] Cannot get ICMP handle." );
  123. return FALSE;
  124. }
  125. //
  126. // prepare buffers
  127. //
  128. SendBuffer = Malloc( DEFAULT_SEND_SIZE );
  129. if ( SendBuffer == NULL )
  130. {
  131. DebugMessage(" [FATAL] Cannot allocate buffer for the ICMP echo frame." );
  132. return FALSE;
  133. }
  134. ZeroMemory( SendBuffer, DEFAULT_SEND_SIZE );
  135. RcvBuffer = Malloc( MAX_ICMP_BUF_SIZE );
  136. if ( RcvBuffer == NULL )
  137. {
  138. Free( SendBuffer );
  139. DebugMessage(" [FATAL] Cannot allocate buffer for the ICMP echo frame." );
  140. return FALSE;
  141. }
  142. ZeroMemory( RcvBuffer, MAX_ICMP_BUF_SIZE );
  143. //
  144. // send ICMP echo request
  145. //
  146. for ( i = 0; i < PING_RETRY_CNT; i++ )
  147. {
  148. nReplyCnt = IcmpSendEcho( hIcmp,
  149. inet_addr(pszIpAddrStr),
  150. SendBuffer,
  151. (unsigned short )DEFAULT_SEND_SIZE,
  152. NULL,
  153. RcvBuffer,
  154. MAX_ICMP_BUF_SIZE,
  155. DEFAULT_TIMEOUT
  156. );
  157. //
  158. // test for destination unreachables
  159. //
  160. if ( nReplyCnt != 0 )
  161. {
  162. reply = (PICMP_ECHO_REPLY )RcvBuffer;
  163. if ( reply->Status == IP_SUCCESS )
  164. {
  165. nReplySum += nReplyCnt;
  166. }
  167. }
  168. } /* for loop */
  169. //
  170. // cleanup
  171. //
  172. Free( SendBuffer );
  173. Free( RcvBuffer );
  174. IcmpCloseHandle( hIcmp );
  175. if ( nReplySum == 0 )
  176. {
  177. return FALSE;
  178. }
  179. else
  180. {
  181. return TRUE;
  182. }
  183. } /* END OF IsIcmpResponse() */
  184. BOOL
  185. IsIcmpResponseW(
  186. LPCWSTR pszIpAddrStr
  187. )
  188. {
  189. LPSTR pszAddr = NULL;
  190. BOOL fRetval;
  191. pszAddr = StrDupAFromW(pszIpAddrStr);
  192. if (pszAddr == NULL)
  193. return FALSE;
  194. fRetval = IsIcmpResponseA(pszAddr);
  195. Free(pszAddr);
  196. return fRetval;
  197. }
  198. //-------------------------------------------------------------------------//
  199. //###### W S L o o p B k T e s t () #####################################//
  200. //-------------------------------------------------------------------------//
  201. // Abstract: //
  202. // Opens a datagram socket and sends a UDP frame through the loopback.//
  203. // If the frame comes back then Winsock and AFD are most probably OK. //
  204. // Arguments: //
  205. // none //
  206. // Return value: //
  207. // TRUE - test passed //
  208. // FALSE - test failed //
  209. // Global variables used: //
  210. // none //
  211. //-------------------------------------------------------------------------//
  212. BOOL WSLoopBkTest( PVOID Context ) {
  213. BOOL RetVal = TRUE;
  214. SOCKET tstSock;
  215. DWORD optionValue; // helper var for setsockopt()
  216. SOCKADDR_IN sockAddr; // struct holding source socket info
  217. //
  218. // create a socket
  219. //
  220. /*
  221. tstSock = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP );
  222. if ( tstSock == INVALID_SOCKET ) {
  223. printf( " [FATAL] socket creation failed!\n" );
  224. printf( " You have a potential Winsock problem!\n" );
  225. return FALSE;
  226. }
  227. tstSock = WSASocket( PF_INET,
  228. SOCK_DGRAM,
  229. IPPROTO_UDP,
  230. NULL,
  231. 0,
  232. WSA_FLAG_OVERLAPPED
  233. );
  234. if ( tstSock == INVALID_SOCKET ) {
  235. printf( " [FATAL] socket creation failed!\n" );
  236. printf( " You have a potential Winsock problem!\n" );
  237. return FALSE;
  238. }
  239. sockAddr.sin_family = PF_INET;
  240. sockAddr.sin_addr.s_addr = 0; // use any local address
  241. sockAddr.sin_port = htons( PORT_4_LOOPBK_TST );
  242. // RtlZeroMemory( sockAddr.sin_zero, 8 );
  243. if ( bind(tstSock, (LPSOCKADDR )&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR ) {
  244. printf( " [FATAL] bind() failed with error %d!\n", WSAGetLastError() );
  245. printf( " You have a potential Winsock problem!\n" );
  246. return FALSE;
  247. }
  248. */
  249. return RetVal;
  250. UNREFERENCED_PARAMETER( Context );
  251. } /* END OF WSLoopBkTest() */
  252. //###################### END OF FILE xportst.c ##########################//
  253. //-------------------------------------------------------------------------//