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.

5544 lines
151 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. WshTcpip.c
  5. Abstract:
  6. This module contains necessary routines for the TCP/IP Windows Sockets
  7. Helper DLL. This DLL provides the transport-specific support necessary
  8. for the Windows Sockets DLL to use TCP/IP as a transport.
  9. Author:
  10. David Treadwell (davidtr) 19-Jul-1992
  11. Revision History:
  12. Keith Moore (keithmo) 02-May-1996
  13. Added WinSock 2 support.
  14. Dave Thaler (dthaler) 17-Jan-2000
  15. Added IGMPv3 support.
  16. --*/
  17. #define UNICODE
  18. #include <nt.h>
  19. #include <ntrtl.h>
  20. #include <nturtl.h>
  21. #include <winsock2.h>
  22. #include <windows.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <wchar.h>
  26. #include <tdi.h>
  27. #include <ws2tcpip.h>
  28. #include <wsahelp.h>
  29. #include <ipexport.h>
  30. #include <tdistat.h>
  31. #include <tdiinfo.h>
  32. #include <llinfo.h>
  33. #include <ipinfo.h>
  34. #include <ntddtcp.h>
  35. typedef unsigned long ulong;
  36. typedef unsigned short ushort;
  37. typedef unsigned int uint;
  38. typedef unsigned char uchar;
  39. #define TL_INSTANCE 0
  40. // private socket options to be accessed via WSAIoctl
  41. #include <mstcpip.h>
  42. #include <ntddtcp.h>
  43. #define NT // temporarily needed by tdiinfo.h...
  44. #include <ipexport.h>
  45. #include <tdiinfo.h>
  46. #include <tcpinfo.h>
  47. #include <ipinfo.h>
  48. #include <basetyps.h>
  49. #include <nspapi.h>
  50. #include <nspapip.h>
  51. #include <afd.h>
  52. #define TCP_NAME L"TCP/IP"
  53. #define UDP_NAME L"UDP/IP"
  54. #define IS_DGRAM_SOCK(type) (((type) == SOCK_DGRAM) || ((type) == SOCK_RAW))
  55. //
  56. // Define valid flags for WSHOpenSocket2().
  57. //
  58. #define VALID_TCP_FLAGS (WSA_FLAG_OVERLAPPED)
  59. #define VALID_UDP_FLAGS (WSA_FLAG_OVERLAPPED | \
  60. WSA_FLAG_MULTIPOINT_C_LEAF | \
  61. WSA_FLAG_MULTIPOINT_D_LEAF)
  62. //
  63. // Buffer management constants for GetTcpipInterfaceList().
  64. //
  65. #define MAX_FAST_ENTITY_BUFFER ( sizeof(TDIEntityID) * 10 )
  66. #define MAX_FAST_ADDRESS_BUFFER ( sizeof(IPAddrEntry) * 4 )
  67. //
  68. // Structure and variables to define the triples supported by TCP/IP. The
  69. // first entry of each array is considered the canonical triple for
  70. // that socket type; the other entries are synonyms for the first.
  71. //
  72. typedef struct _MAPPING_TRIPLE {
  73. INT AddressFamily;
  74. INT SocketType;
  75. INT Protocol;
  76. } MAPPING_TRIPLE, *PMAPPING_TRIPLE;
  77. MAPPING_TRIPLE TcpMappingTriples[] = { AF_INET, SOCK_STREAM, IPPROTO_TCP,
  78. AF_INET, SOCK_STREAM, 0,
  79. AF_INET, 0, IPPROTO_TCP,
  80. AF_UNSPEC, 0, IPPROTO_TCP,
  81. AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP };
  82. MAPPING_TRIPLE UdpMappingTriples[] = { AF_INET, SOCK_DGRAM, IPPROTO_UDP,
  83. AF_INET, SOCK_DGRAM, 0,
  84. AF_INET, 0, IPPROTO_UDP,
  85. AF_UNSPEC, 0, IPPROTO_UDP,
  86. AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP };
  87. MAPPING_TRIPLE RawMappingTriples[] = { AF_INET, SOCK_RAW, 0 };
  88. //
  89. // Winsock 2 WSAPROTOCOL_INFO structures for all supported protocols.
  90. //
  91. #define WINSOCK_SPI_VERSION 2
  92. #define UDP_MESSAGE_SIZE (65535-68)
  93. WSAPROTOCOL_INFOW Winsock2Protocols[] =
  94. {
  95. //
  96. // TCP
  97. //
  98. {
  99. XP1_GUARANTEED_DELIVERY // dwServiceFlags1
  100. | XP1_GUARANTEED_ORDER
  101. | XP1_GRACEFUL_CLOSE
  102. | XP1_EXPEDITED_DATA
  103. | XP1_IFS_HANDLES,
  104. 0, // dwServiceFlags2
  105. 0, // dwServiceFlags3
  106. 0, // dwServiceFlags4
  107. PFL_MATCHES_PROTOCOL_ZERO, // dwProviderFlags
  108. { // gProviderId
  109. 0, 0, 0,
  110. { 0, 0, 0, 0, 0, 0, 0, 0 }
  111. },
  112. 0, // dwCatalogEntryId
  113. { // ProtocolChain
  114. BASE_PROTOCOL, // ChainLen
  115. { 0, 0, 0, 0, 0, 0, 0 } // ChainEntries
  116. },
  117. WINSOCK_SPI_VERSION, // iVersion
  118. AF_INET, // iAddressFamily
  119. sizeof(SOCKADDR_IN), // iMaxSockAddr
  120. sizeof(SOCKADDR_IN), // iMinSockAddr
  121. SOCK_STREAM, // iSocketType
  122. IPPROTO_TCP, // iProtocol
  123. 0, // iProtocolMaxOffset
  124. BIGENDIAN, // iNetworkByteOrder
  125. SECURITY_PROTOCOL_NONE, // iSecurityScheme
  126. 0, // dwMessageSize
  127. 0, // dwProviderReserved
  128. L"MSAFD Tcpip [TCP/IP]" // szProtocol
  129. },
  130. //
  131. // UDP
  132. //
  133. {
  134. XP1_CONNECTIONLESS // dwServiceFlags1
  135. | XP1_MESSAGE_ORIENTED
  136. | XP1_SUPPORT_BROADCAST
  137. | XP1_SUPPORT_MULTIPOINT
  138. | XP1_IFS_HANDLES,
  139. 0, // dwServiceFlags2
  140. 0, // dwServiceFlags3
  141. 0, // dwServiceFlags4
  142. PFL_MATCHES_PROTOCOL_ZERO, // dwProviderFlags
  143. { // gProviderId
  144. 0, 0, 0,
  145. { 0, 0, 0, 0, 0, 0, 0, 0 }
  146. },
  147. 0, // dwCatalogEntryId
  148. { // ProtocolChain
  149. BASE_PROTOCOL, // ChainLen
  150. { 0, 0, 0, 0, 0, 0, 0 } // ChainEntries
  151. },
  152. WINSOCK_SPI_VERSION, // iVersion
  153. AF_INET, // iAddressFamily
  154. sizeof(SOCKADDR_IN), // iMaxSockAddr
  155. sizeof(SOCKADDR_IN), // iMinSockAddr
  156. SOCK_DGRAM, // iSocketType
  157. IPPROTO_UDP, // iProtocol
  158. 0, // iProtocolMaxOffset
  159. BIGENDIAN, // iNetworkByteOrder
  160. SECURITY_PROTOCOL_NONE, // iSecurityScheme
  161. UDP_MESSAGE_SIZE, // dwMessageSize
  162. 0, // dwProviderReserved
  163. L"MSAFD Tcpip [UDP/IP]" // szProtocol
  164. },
  165. //
  166. // RAW
  167. //
  168. {
  169. XP1_CONNECTIONLESS // dwServiceFlags1
  170. | XP1_MESSAGE_ORIENTED
  171. | XP1_SUPPORT_BROADCAST
  172. | XP1_SUPPORT_MULTIPOINT
  173. | XP1_IFS_HANDLES,
  174. 0, // dwServiceFlags2
  175. 0, // dwServiceFlags3
  176. 0, // dwServiceFlags4
  177. PFL_MATCHES_PROTOCOL_ZERO // dwProviderFlags
  178. | PFL_HIDDEN,
  179. { // gProviderId
  180. 0, 0, 0,
  181. { 0, 0, 0, 0, 0, 0, 0, 0 }
  182. },
  183. 0, // dwCatalogEntryId
  184. { // ProtocolChain
  185. BASE_PROTOCOL, // ChainLen
  186. { 0, 0, 0, 0, 0, 0, 0 } // ChainEntries
  187. },
  188. WINSOCK_SPI_VERSION, // iVersion
  189. AF_INET, // iAddressFamily
  190. sizeof(SOCKADDR_IN), // iMaxSockAddr
  191. sizeof(SOCKADDR_IN), // iMinSockAddr
  192. SOCK_RAW, // iSocketType
  193. 0, // iProtocol
  194. 255, // iProtocolMaxOffset
  195. BIGENDIAN, // iNetworkByteOrder
  196. SECURITY_PROTOCOL_NONE, // iSecurityScheme
  197. UDP_MESSAGE_SIZE, // dwMessageSize
  198. 0, // dwProviderReserved
  199. L"MSAFD Tcpip [RAW/IP]" // szProtocol
  200. }
  201. };
  202. #define NUM_WINSOCK2_PROTOCOLS \
  203. ( sizeof(Winsock2Protocols) / sizeof(Winsock2Protocols[0]) )
  204. //
  205. // The GUID identifying this provider.
  206. //
  207. GUID TcpipProviderGuid = { /* e70f1aa0-ab8b-11cf-8ca3-00805f48a192 */
  208. 0xe70f1aa0,
  209. 0xab8b,
  210. 0x11cf,
  211. {0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}
  212. };
  213. //
  214. // Forward declarations of internal routines.
  215. //
  216. VOID
  217. CompleteTdiActionApc (
  218. IN PVOID ApcContext,
  219. IN PIO_STATUS_BLOCK IoStatusBlock
  220. );
  221. INT
  222. GetTdiInformation (
  223. IN HANDLE TdiConnectionObjectHandle,
  224. IN ULONG Entity,
  225. IN ULONG Class,
  226. IN ULONG Type,
  227. IN ULONG Id,
  228. IN PVOID Value,
  229. IN ULONG InValueLength,
  230. IN ULONG OutValueLength
  231. );
  232. INT
  233. SetTdiInformation (
  234. IN HANDLE TdiConnectionObjectHandle,
  235. IN ULONG Entity,
  236. IN ULONG Class,
  237. IN ULONG Type,
  238. IN ULONG Id,
  239. IN PVOID Value,
  240. IN ULONG ValueLength,
  241. IN BOOLEAN WaitForCompletion
  242. );
  243. BOOLEAN
  244. IsTripleInList (
  245. IN PMAPPING_TRIPLE List,
  246. IN ULONG ListLength,
  247. IN INT AddressFamily,
  248. IN INT SocketType,
  249. IN INT Protocol
  250. );
  251. NTSTATUS
  252. GetTcpipInterfaceList(
  253. IN LPVOID OutputBuffer,
  254. IN DWORD OutputBufferLength,
  255. OUT LPDWORD NumberOfBytesReturned
  256. );
  257. INT
  258. NtStatusToSocketError (
  259. IN NTSTATUS Status
  260. );
  261. //
  262. // The socket context structure for this DLL. Each open TCP/IP socket
  263. // will have one of these context structures, which is used to maintain
  264. // information about the socket.
  265. //
  266. typedef struct _WSHTCPIP_SOCKET_CONTEXT {
  267. INT AddressFamily;
  268. INT SocketType;
  269. INT Protocol;
  270. INT ReceiveBufferSize;
  271. DWORD Flags;
  272. INT MulticastTtl;
  273. ULONG MulticastInterface;
  274. struct tcp_keepalive KeepAliveVals;
  275. IN_ADDR MultipointTarget;
  276. INT HdrInclude;
  277. BOOLEAN UcastIf;
  278. BOOLEAN MulticastLoopback;
  279. BOOLEAN KeepAlive;
  280. BOOLEAN DontRoute;
  281. BOOLEAN NoDelay;
  282. BOOLEAN BsdUrgent;
  283. BOOLEAN MultipointLeaf;
  284. BOOLEAN UdpNoChecksum;
  285. BOOLEAN Broadcast;
  286. BOOLEAN HdrIncludeSet;
  287. BOOLEAN LimitBroadcasts;
  288. UCHAR IpPktInfo;
  289. UCHAR IpTtl;
  290. UCHAR IpTos;
  291. UCHAR IpDontFragment;
  292. UCHAR IpOptionsLength;
  293. UCHAR IpOptions[MAX_OPT_SIZE];
  294. } WSHTCPIP_SOCKET_CONTEXT, *PWSHTCPIP_SOCKET_CONTEXT;
  295. #define DEFAULT_RECEIVE_BUFFER_SIZE 8192
  296. #define DEFAULT_MULTICAST_TTL 1
  297. #define DEFAULT_MULTICAST_INTERFACE INADDR_ANY
  298. #define DEFAULT_MULTICAST_LOOPBACK TRUE
  299. #define DEFAULT_BROADCAST FALSE
  300. #define DEFAULT_UCAST_IF FALSE
  301. //
  302. //
  303. #define DEFAULT_IP_TTL 32
  304. #define DEFAULT_IP_TOS 0
  305. BOOLEAN
  306. DllInitialize (
  307. IN PVOID DllHandle,
  308. IN ULONG Reason,
  309. IN PVOID Context OPTIONAL
  310. )
  311. {
  312. switch ( Reason ) {
  313. case DLL_PROCESS_ATTACH:
  314. //
  315. // We don't need to receive thread attach and detach
  316. // notifications, so disable them to help application
  317. // performance.
  318. //
  319. DisableThreadLibraryCalls( DllHandle );
  320. return TRUE;
  321. case DLL_THREAD_ATTACH:
  322. break;
  323. case DLL_PROCESS_DETACH:
  324. break;
  325. case DLL_THREAD_DETACH:
  326. break;
  327. }
  328. return TRUE;
  329. } // SockInitialize
  330. INT
  331. WSHGetSockaddrType (
  332. IN PSOCKADDR Sockaddr,
  333. IN DWORD SockaddrLength,
  334. OUT PSOCKADDR_INFO SockaddrInfo
  335. )
  336. /*++
  337. Routine Description:
  338. This routine parses a sockaddr to determine the type of the
  339. machine address and endpoint address portions of the sockaddr.
  340. This is called by the winsock DLL whenever it needs to interpret
  341. a sockaddr.
  342. Arguments:
  343. Sockaddr - a pointer to the sockaddr structure to evaluate.
  344. SockaddrLength - the number of bytes in the sockaddr structure.
  345. SockaddrInfo - a pointer to a structure that will receive information
  346. about the specified sockaddr.
  347. Return Value:
  348. INT - a winsock error code indicating the status of the operation, or
  349. NO_ERROR if the operation succeeded.
  350. --*/
  351. {
  352. UNALIGNED SOCKADDR_IN *sockaddr = (PSOCKADDR_IN)Sockaddr;
  353. ULONG i;
  354. //
  355. // Make sure that the address family is correct.
  356. //
  357. if ( sockaddr->sin_family != AF_INET ) {
  358. return WSAEAFNOSUPPORT;
  359. }
  360. //
  361. // Make sure that the length is correct.
  362. //
  363. if ( SockaddrLength < sizeof(SOCKADDR_IN) ) {
  364. return WSAEFAULT;
  365. }
  366. //
  367. // The address passed the tests, looks like a good address.
  368. // Determine the type of the address portion of the sockaddr.
  369. //
  370. if ( sockaddr->sin_addr.s_addr == INADDR_ANY ) {
  371. ASSERT (htonl(INADDR_ANY)==INADDR_ANY);
  372. SockaddrInfo->AddressInfo = SockaddrAddressInfoWildcard;
  373. } else if ( sockaddr->sin_addr.s_addr == INADDR_BROADCAST ) {
  374. ASSERT (htonl(INADDR_BROADCAST)==INADDR_BROADCAST);
  375. SockaddrInfo->AddressInfo = SockaddrAddressInfoBroadcast;
  376. } else if ( sockaddr->sin_addr.s_addr == htonl(INADDR_LOOPBACK) ) {
  377. SockaddrInfo->AddressInfo = SockaddrAddressInfoLoopback;
  378. } else {
  379. SockaddrInfo->AddressInfo = SockaddrAddressInfoNormal;
  380. }
  381. //
  382. // Determine the type of the port (endpoint) in the sockaddr.
  383. //
  384. if ( sockaddr->sin_port == 0 ) {
  385. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoWildcard;
  386. } else if ( ntohs( sockaddr->sin_port ) < 2000 ) {
  387. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoReserved;
  388. } else {
  389. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoNormal;
  390. }
  391. //
  392. // Zero out the sin_zero part of the address. We silently allow
  393. // nonzero values in this field.
  394. //
  395. for ( i = 0; i < sizeof(sockaddr->sin_zero); i++ ) {
  396. sockaddr->sin_zero[i] = 0;
  397. }
  398. return NO_ERROR;
  399. } // WSHGetSockaddrType
  400. INT
  401. WSHGetSocketInformation (
  402. IN PVOID HelperDllSocketContext,
  403. IN SOCKET SocketHandle,
  404. IN HANDLE TdiAddressObjectHandle,
  405. IN HANDLE TdiConnectionObjectHandle,
  406. IN INT Level,
  407. IN INT OptionName,
  408. OUT PCHAR OptionValue,
  409. OUT PINT OptionLength
  410. )
  411. /*++
  412. Routine Description:
  413. This routine retrieves information about a socket for those socket
  414. options supported in this helper DLL. The options supported here
  415. are SO_KEEPALIVE, SO_DONTROUTE, and TCP_EXPEDITED_1122. This routine is
  416. called by the winsock DLL when a level/option name combination is
  417. passed to getsockopt() that the winsock DLL does not understand.
  418. Arguments:
  419. HelperDllSocketContext - the context pointer returned from
  420. WSHOpenSocket().
  421. SocketHandle - the handle of the socket for which we're getting
  422. information.
  423. TdiAddressObjectHandle - the TDI address object of the socket, if
  424. any. If the socket is not yet bound to an address, then
  425. it does not have a TDI address object and this parameter
  426. will be NULL.
  427. TdiConnectionObjectHandle - the TDI connection object of the socket,
  428. if any. If the socket is not yet connected, then it does not
  429. have a TDI connection object and this parameter will be NULL.
  430. Level - the level parameter passed to getsockopt().
  431. OptionName - the optname parameter passed to getsockopt().
  432. OptionValue - the optval parameter passed to getsockopt().
  433. OptionLength - the optlen parameter passed to getsockopt().
  434. Return Value:
  435. INT - a winsock error code indicating the status of the operation, or
  436. NO_ERROR if the operation succeeded.
  437. --*/
  438. {
  439. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  440. UNREFERENCED_PARAMETER( SocketHandle );
  441. UNREFERENCED_PARAMETER( TdiAddressObjectHandle );
  442. UNREFERENCED_PARAMETER( TdiConnectionObjectHandle );
  443. //
  444. // Check if this is an internal request for context information.
  445. //
  446. if ( Level == SOL_INTERNAL && OptionName == SO_CONTEXT ) {
  447. //
  448. // The Windows Sockets DLL is requesting context information
  449. // from us. If an output buffer was not supplied, the Windows
  450. // Sockets DLL is just requesting the size of our context
  451. // information.
  452. //
  453. if ( OptionValue != NULL ) {
  454. //
  455. // Make sure that the buffer is sufficient to hold all the
  456. // context information.
  457. //
  458. if ( *OptionLength < sizeof(*context) ) {
  459. return WSAEFAULT;
  460. }
  461. //
  462. // Copy in the context information.
  463. //
  464. RtlCopyMemory( OptionValue, context, sizeof(*context) );
  465. }
  466. *OptionLength = sizeof(*context);
  467. return NO_ERROR;
  468. }
  469. //
  470. // The only other levels we support here are SOL_SOCKET,
  471. // IPPROTO_TCP, IPPROTO_UDP, and IPPROTO_IP.
  472. //
  473. if ( Level != SOL_SOCKET &&
  474. Level != IPPROTO_TCP &&
  475. Level != IPPROTO_UDP &&
  476. Level != IPPROTO_IP ) {
  477. return WSAEINVAL;
  478. }
  479. //
  480. // Make sure that the output buffer is sufficiently large.
  481. //
  482. if ( *OptionLength < sizeof(char)) {
  483. return WSAEFAULT;
  484. }
  485. __try {
  486. RtlZeroMemory( OptionValue, *OptionLength );
  487. }
  488. __except (EXCEPTION_EXECUTE_HANDLER) {
  489. return WSAEFAULT;
  490. }
  491. //
  492. // Handle TCP-level options.
  493. //
  494. if ( Level == IPPROTO_TCP ) {
  495. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  496. return WSAENOPROTOOPT;
  497. }
  498. switch ( OptionName ) {
  499. case TCP_NODELAY:
  500. *OptionValue = context->NoDelay;
  501. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  502. break;
  503. case TCP_EXPEDITED_1122:
  504. *OptionValue = !context->BsdUrgent;
  505. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  506. break;
  507. default:
  508. return WSAEINVAL;
  509. }
  510. return NO_ERROR;
  511. }
  512. //
  513. // Handle UDP-level options.
  514. //
  515. if ( Level == IPPROTO_UDP ) {
  516. switch ( OptionName ) {
  517. case UDP_NOCHECKSUM :
  518. //
  519. // This option is only valid for datagram sockets.
  520. //
  521. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  522. return WSAENOPROTOOPT;
  523. }
  524. *OptionValue = context->UdpNoChecksum;
  525. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  526. break;
  527. break;
  528. default :
  529. return WSAEINVAL;
  530. }
  531. return NO_ERROR;
  532. }
  533. //
  534. // Handle IP-level options.
  535. //
  536. if ( Level == IPPROTO_IP ) {
  537. //
  538. // Act based on the specific option.
  539. //
  540. switch ( OptionName ) {
  541. case IP_TTL:
  542. *OptionValue = (char) context->IpTtl;
  543. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  544. return NO_ERROR;
  545. case IP_TOS:
  546. *OptionValue = (char) context->IpTos;
  547. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  548. return NO_ERROR;
  549. case IP_DONTFRAGMENT:
  550. *OptionValue = (char) context->IpDontFragment;
  551. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  552. return NO_ERROR;
  553. case IP_OPTIONS:
  554. if ( *OptionLength < context->IpOptionsLength ) {
  555. return WSAEFAULT;
  556. }
  557. if (context->IpOptionsLength>0) {
  558. RtlMoveMemory(
  559. OptionValue,
  560. context->IpOptions,
  561. context->IpOptionsLength
  562. );
  563. }
  564. *OptionLength = context->IpOptionsLength;
  565. return NO_ERROR;
  566. default:
  567. //
  568. // No match, fall through.
  569. //
  570. break;
  571. }
  572. //
  573. // The following IP options are only valid on datagram sockets.
  574. //
  575. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  576. return WSAENOPROTOOPT;
  577. }
  578. //
  579. // Act based on the specific option.
  580. //
  581. switch ( OptionName ) {
  582. case IP_MULTICAST_TTL:
  583. *OptionValue = (char)context->MulticastTtl;
  584. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  585. return NO_ERROR;
  586. case IP_MULTICAST_IF:
  587. *(int *)OptionValue = context->MulticastInterface;
  588. *OptionLength = sizeof(int);
  589. return NO_ERROR;
  590. case IP_MULTICAST_LOOP:
  591. *OptionValue = context->MulticastLoopback;
  592. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  593. return NO_ERROR;
  594. case IP_HDRINCL:
  595. // User hdr include option
  596. //
  597. //
  598. if (*OptionLength<sizeof (int))
  599. return WSAEFAULT;
  600. if (context->HdrIncludeSet == TRUE) {
  601. *((PINT)OptionValue) = context->HdrInclude;
  602. }
  603. else {
  604. *((PINT)OptionValue) = 0;
  605. }
  606. *OptionLength = sizeof(int);
  607. return NO_ERROR;
  608. case IP_PKTINFO:
  609. *OptionValue = (char) context->IpPktInfo;
  610. *OptionLength = *OptionLength < sizeof (int) ? sizeof (char) : sizeof(int);
  611. return NO_ERROR;
  612. default:
  613. return WSAENOPROTOOPT;
  614. }
  615. }
  616. //
  617. // Handle socket-level options.
  618. //
  619. switch ( OptionName ) {
  620. case SO_KEEPALIVE:
  621. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  622. return WSAENOPROTOOPT;
  623. }
  624. *OptionValue = context->KeepAlive;
  625. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  626. break;
  627. case SO_DONTROUTE:
  628. *OptionValue = context->DontRoute;
  629. *OptionLength = *OptionLength<sizeof (int) ? sizeof (char) : sizeof(int);
  630. break;
  631. default:
  632. return WSAENOPROTOOPT;
  633. }
  634. return NO_ERROR;
  635. } // WSHGetSocketInformation
  636. INT
  637. WSHGetWildcardSockaddr (
  638. IN PVOID HelperDllSocketContext,
  639. OUT PSOCKADDR Sockaddr,
  640. OUT PINT SockaddrLength
  641. )
  642. /*++
  643. Routine Description:
  644. This routine returns a wildcard socket address. A wildcard address
  645. is one which will bind the socket to an endpoint of the transport's
  646. choosing. For TCP/IP, a wildcard address has IP address ==
  647. 0.0.0.0 and port = 0.
  648. Arguments:
  649. HelperDllSocketContext - the context pointer returned from
  650. WSHOpenSocket() for the socket for which we need a wildcard
  651. address.
  652. Sockaddr - points to a buffer which will receive the wildcard socket
  653. address.
  654. SockaddrLength - receives the length of the wioldcard sockaddr.
  655. Return Value:
  656. INT - a winsock error code indicating the status of the operation, or
  657. NO_ERROR if the operation succeeded.
  658. --*/
  659. {
  660. if ( *SockaddrLength < sizeof(SOCKADDR_IN) ) {
  661. return WSAEFAULT;
  662. }
  663. *SockaddrLength = sizeof(SOCKADDR_IN);
  664. //
  665. // Just zero out the address and set the family to AF_INET--this is
  666. // a wildcard address for TCP/IP.
  667. //
  668. RtlZeroMemory( Sockaddr, sizeof(SOCKADDR_IN) );
  669. Sockaddr->sa_family = AF_INET;
  670. return NO_ERROR;
  671. } // WSAGetWildcardSockaddr
  672. DWORD
  673. WSHGetWinsockMapping (
  674. OUT PWINSOCK_MAPPING Mapping,
  675. IN DWORD MappingLength
  676. )
  677. /*++
  678. Routine Description:
  679. Returns the list of address family/socket type/protocol triples
  680. supported by this helper DLL.
  681. Arguments:
  682. Mapping - receives a pointer to a WINSOCK_MAPPING structure that
  683. describes the triples supported here.
  684. MappingLength - the length, in bytes, of the passed-in Mapping buffer.
  685. Return Value:
  686. DWORD - the length, in bytes, of a WINSOCK_MAPPING structure for this
  687. helper DLL. If the passed-in buffer is too small, the return
  688. value will indicate the size of a buffer needed to contain
  689. the WINSOCK_MAPPING structure.
  690. --*/
  691. {
  692. DWORD mappingLength;
  693. mappingLength = sizeof(WINSOCK_MAPPING) - sizeof(MAPPING_TRIPLE) +
  694. sizeof(TcpMappingTriples) + sizeof(UdpMappingTriples)
  695. + sizeof(RawMappingTriples);
  696. //
  697. // If the passed-in buffer is too small, return the length needed
  698. // now without writing to the buffer. The caller should allocate
  699. // enough memory and call this routine again.
  700. //
  701. if ( mappingLength > MappingLength ) {
  702. return mappingLength;
  703. }
  704. //
  705. // Fill in the output mapping buffer with the list of triples
  706. // supported in this helper DLL.
  707. //
  708. Mapping->Rows = sizeof(TcpMappingTriples) / sizeof(TcpMappingTriples[0])
  709. + sizeof(UdpMappingTriples) / sizeof(UdpMappingTriples[0])
  710. + sizeof(RawMappingTriples) / sizeof(RawMappingTriples[0]);
  711. Mapping->Columns = sizeof(MAPPING_TRIPLE) / sizeof(DWORD);
  712. RtlMoveMemory(
  713. Mapping->Mapping,
  714. TcpMappingTriples,
  715. sizeof(TcpMappingTriples)
  716. );
  717. RtlMoveMemory(
  718. (PCHAR)Mapping->Mapping + sizeof(TcpMappingTriples),
  719. UdpMappingTriples,
  720. sizeof(UdpMappingTriples)
  721. );
  722. RtlMoveMemory(
  723. (PCHAR)Mapping->Mapping + sizeof(TcpMappingTriples)
  724. + sizeof(UdpMappingTriples),
  725. RawMappingTriples,
  726. sizeof(RawMappingTriples)
  727. );
  728. //
  729. // Return the number of bytes we wrote.
  730. //
  731. return mappingLength;
  732. } // WSHGetWinsockMapping
  733. INT
  734. WSHOpenSocket (
  735. IN OUT PINT AddressFamily,
  736. IN OUT PINT SocketType,
  737. IN OUT PINT Protocol,
  738. OUT PUNICODE_STRING TransportDeviceName,
  739. OUT PVOID *HelperDllSocketContext,
  740. OUT PDWORD NotificationEvents
  741. )
  742. {
  743. return WSHOpenSocket2(
  744. AddressFamily,
  745. SocketType,
  746. Protocol,
  747. 0, // Group
  748. 0, // Flags
  749. TransportDeviceName,
  750. HelperDllSocketContext,
  751. NotificationEvents
  752. );
  753. } // WSHOpenSocket
  754. INT
  755. WSHOpenSocket2 (
  756. IN OUT PINT AddressFamily,
  757. IN OUT PINT SocketType,
  758. IN OUT PINT Protocol,
  759. IN GROUP Group,
  760. IN DWORD Flags,
  761. OUT PUNICODE_STRING TransportDeviceName,
  762. OUT PVOID *HelperDllSocketContext,
  763. OUT PDWORD NotificationEvents
  764. )
  765. /*++
  766. Routine Description:
  767. Does the necessary work for this helper DLL to open a socket and is
  768. called by the winsock DLL in the socket() routine. This routine
  769. verifies that the specified triple is valid, determines the NT
  770. device name of the TDI provider that will support that triple,
  771. allocates space to hold the socket's context block, and
  772. canonicalizes the triple.
  773. Arguments:
  774. AddressFamily - on input, the address family specified in the
  775. socket() call. On output, the canonicalized value for the
  776. address family.
  777. SocketType - on input, the socket type specified in the socket()
  778. call. On output, the canonicalized value for the socket type.
  779. Protocol - on input, the protocol specified in the socket() call.
  780. On output, the canonicalized value for the protocol.
  781. Group - Identifies the group for the new socket.
  782. Flags - Zero or more WSA_FLAG_* flags as passed into WSASocket().
  783. TransportDeviceName - receives the name of the TDI provider that
  784. will support the specified triple.
  785. HelperDllSocketContext - receives a context pointer that the winsock
  786. DLL will return to this helper DLL on future calls involving
  787. this socket.
  788. NotificationEvents - receives a bitmask of those state transitions
  789. this helper DLL should be notified on.
  790. Return Value:
  791. INT - a winsock error code indicating the status of the operation, or
  792. NO_ERROR if the operation succeeded.
  793. --*/
  794. {
  795. PWSHTCPIP_SOCKET_CONTEXT context;
  796. //
  797. // Determine whether this is to be a TCP, UDP, or RAW socket.
  798. //
  799. if ( IsTripleInList(
  800. TcpMappingTriples,
  801. sizeof(TcpMappingTriples) / sizeof(TcpMappingTriples[0]),
  802. *AddressFamily,
  803. *SocketType,
  804. *Protocol ) ) {
  805. //
  806. // It's a TCP socket. Check the flags.
  807. //
  808. if( ( Flags & ~VALID_TCP_FLAGS ) != 0 ) {
  809. return WSAEINVAL;
  810. }
  811. //
  812. // Return the canonical form of a TCP socket triple.
  813. //
  814. *AddressFamily = TcpMappingTriples[0].AddressFamily;
  815. *SocketType = TcpMappingTriples[0].SocketType;
  816. *Protocol = TcpMappingTriples[0].Protocol;
  817. //
  818. // Indicate the name of the TDI device that will service
  819. // SOCK_STREAM sockets in the internet address family.
  820. //
  821. RtlInitUnicodeString( TransportDeviceName, DD_TCP_DEVICE_NAME );
  822. } else if ( IsTripleInList(
  823. UdpMappingTriples,
  824. sizeof(UdpMappingTriples) / sizeof(UdpMappingTriples[0]),
  825. *AddressFamily,
  826. *SocketType,
  827. *Protocol ) ) {
  828. //
  829. // It's a UDP socket. Check the flags & group ID.
  830. //
  831. if( ( Flags & ~VALID_UDP_FLAGS ) != 0 ||
  832. Group == SG_CONSTRAINED_GROUP ) {
  833. return WSAEINVAL;
  834. }
  835. //
  836. // Return the canonical form of a UDP socket triple.
  837. //
  838. *AddressFamily = UdpMappingTriples[0].AddressFamily;
  839. *SocketType = UdpMappingTriples[0].SocketType;
  840. *Protocol = UdpMappingTriples[0].Protocol;
  841. //
  842. // Indicate the name of the TDI device that will service
  843. // SOCK_DGRAM sockets in the internet address family.
  844. //
  845. RtlInitUnicodeString( TransportDeviceName, DD_UDP_DEVICE_NAME );
  846. } else if ( IsTripleInList(
  847. RawMappingTriples,
  848. sizeof(RawMappingTriples) / sizeof(RawMappingTriples[0]),
  849. *AddressFamily,
  850. *SocketType,
  851. *Protocol ) )
  852. {
  853. UNICODE_STRING unicodeString;
  854. NTSTATUS status;
  855. //
  856. // There is no canonicalization to be done for SOCK_RAW.
  857. //
  858. if (*Protocol < 0 || *Protocol > 255) {
  859. return(WSAEINVAL);
  860. }
  861. //
  862. // Indicate the name of the TDI device that will service
  863. // SOCK_RAW sockets in the internet address family.
  864. //
  865. RtlInitUnicodeString(&unicodeString, DD_RAW_IP_DEVICE_NAME);
  866. RtlInitUnicodeString(TransportDeviceName, NULL);
  867. TransportDeviceName->MaximumLength = unicodeString.Length +
  868. (4 * sizeof(WCHAR) +
  869. sizeof(UNICODE_NULL));
  870. TransportDeviceName->Buffer = RtlAllocateHeap(
  871. RtlProcessHeap( ),
  872. 0,
  873. TransportDeviceName->MaximumLength
  874. );
  875. if (TransportDeviceName->Buffer == NULL) {
  876. return(WSAENOBUFS);
  877. }
  878. //
  879. // Append the device name.
  880. //
  881. status = RtlAppendUnicodeStringToString(
  882. TransportDeviceName,
  883. &unicodeString
  884. );
  885. ASSERT(NT_SUCCESS(status));
  886. //
  887. // Append a separator.
  888. //
  889. TransportDeviceName->Buffer[TransportDeviceName->Length/sizeof(WCHAR)] =
  890. OBJ_NAME_PATH_SEPARATOR;
  891. TransportDeviceName->Length += sizeof(WCHAR);
  892. TransportDeviceName->Buffer[TransportDeviceName->Length/sizeof(WCHAR)] =
  893. UNICODE_NULL;
  894. //
  895. // Append the protocol number.
  896. //
  897. unicodeString.Buffer = TransportDeviceName->Buffer +
  898. (TransportDeviceName->Length / sizeof(WCHAR));
  899. unicodeString.Length = 0;
  900. unicodeString.MaximumLength = TransportDeviceName->MaximumLength -
  901. TransportDeviceName->Length;
  902. status = RtlIntegerToUnicodeString(
  903. (ULONG) *Protocol,
  904. 10,
  905. &unicodeString
  906. );
  907. TransportDeviceName->Length += unicodeString.Length;
  908. ASSERT(NT_SUCCESS(status));
  909. } else {
  910. //
  911. // This should never happen if the registry information about this
  912. // helper DLL is correct. If somehow this did happen, just return
  913. // an error.
  914. //
  915. return WSAEINVAL;
  916. }
  917. //
  918. // Allocate context for this socket. The Windows Sockets DLL will
  919. // return this value to us when it asks us to get/set socket options.
  920. //
  921. context = RtlAllocateHeap( RtlProcessHeap( ), 0, sizeof(*context) );
  922. if ( context == NULL ) {
  923. return WSAENOBUFS;
  924. }
  925. //
  926. // Initialize the context for the socket.
  927. //
  928. context->AddressFamily = *AddressFamily;
  929. context->SocketType = *SocketType;
  930. context->Protocol = *Protocol;
  931. context->ReceiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
  932. context->Flags = Flags;
  933. context->MulticastTtl = DEFAULT_MULTICAST_TTL;
  934. context->MulticastInterface = DEFAULT_MULTICAST_INTERFACE;
  935. context->MulticastLoopback = DEFAULT_MULTICAST_LOOPBACK;
  936. context->KeepAlive = FALSE;
  937. context->DontRoute = FALSE;
  938. context->NoDelay = FALSE;
  939. context->BsdUrgent = TRUE;
  940. context->IpDontFragment = FALSE;
  941. context->IpTtl = DEFAULT_IP_TTL;
  942. context->IpTos = DEFAULT_IP_TOS;
  943. context->IpOptionsLength = 0;
  944. context->MultipointLeaf = FALSE;
  945. context->UdpNoChecksum = FALSE;
  946. context->Broadcast = DEFAULT_BROADCAST;
  947. context->HdrIncludeSet = FALSE;
  948. context->KeepAliveVals.onoff = FALSE;
  949. context->UcastIf = DEFAULT_UCAST_IF;
  950. context->LimitBroadcasts = FALSE;
  951. context->IpPktInfo = FALSE;
  952. //
  953. // Tell the Windows Sockets DLL which state transitions we're
  954. // interested in being notified of. The only times we need to be
  955. // called is after a connect has completed so that we can turn on
  956. // the sending of keepalives if SO_KEEPALIVE was set before the
  957. // socket was connected, when the socket is closed so that we can
  958. // free context information, and when a connect fails so that we
  959. // can, if appropriate, dial in to the network that will support the
  960. // connect attempt.
  961. //
  962. if (*SocketType == SOCK_STREAM) {
  963. *NotificationEvents =
  964. WSH_NOTIFY_CONNECT | WSH_NOTIFY_CLOSE | WSH_NOTIFY_CONNECT_ERROR;
  965. }
  966. else { // *SocketType == SOCK_DGRAM || *SocketType == SOCK_RAW
  967. *NotificationEvents =
  968. WSH_NOTIFY_CONNECT | WSH_NOTIFY_CLOSE | WSH_NOTIFY_CONNECT_ERROR |
  969. WSH_NOTIFY_BIND;
  970. }
  971. //
  972. // Everything worked, return success.
  973. //
  974. *HelperDllSocketContext = context;
  975. return NO_ERROR;
  976. } // WSHOpenSocket
  977. INT
  978. WSHNotify (
  979. IN PVOID HelperDllSocketContext,
  980. IN SOCKET SocketHandle,
  981. IN HANDLE TdiAddressObjectHandle,
  982. IN HANDLE TdiConnectionObjectHandle,
  983. IN DWORD NotifyEvent
  984. )
  985. /*++
  986. Routine Description:
  987. This routine is called by the winsock DLL after a state transition
  988. of the socket. Only state transitions returned in the
  989. NotificationEvents parameter of WSHOpenSocket() are notified here.
  990. This routine allows a winsock helper DLL to track the state of
  991. socket and perform necessary actions corresponding to state
  992. transitions.
  993. Arguments:
  994. HelperDllSocketContext - the context pointer given to the winsock
  995. DLL by WSHOpenSocket().
  996. SocketHandle - the handle for the socket.
  997. TdiAddressObjectHandle - the TDI address object of the socket, if
  998. any. If the socket is not yet bound to an address, then
  999. it does not have a TDI address object and this parameter
  1000. will be NULL.
  1001. TdiConnectionObjectHandle - the TDI connection object of the socket,
  1002. if any. If the socket is not yet connected, then it does not
  1003. have a TDI connection object and this parameter will be NULL.
  1004. NotifyEvent - indicates the state transition for which we're being
  1005. called.
  1006. Return Value:
  1007. INT - a winsock error code indicating the status of the operation, or
  1008. NO_ERROR if the operation succeeded.
  1009. --*/
  1010. {
  1011. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  1012. INT err;
  1013. //
  1014. // We should only be called after a connect() completes or when the
  1015. // socket is being closed.
  1016. //
  1017. if ( NotifyEvent == WSH_NOTIFY_CONNECT ) {
  1018. ULONG true = TRUE;
  1019. ULONG false = FALSE;
  1020. //
  1021. // If a connection-object option was set on the socket before
  1022. // it was connected, set the option for real now.
  1023. //
  1024. if ( context->KeepAlive ) {
  1025. err = SetTdiInformation(
  1026. TdiConnectionObjectHandle,
  1027. CO_TL_ENTITY,
  1028. INFO_CLASS_PROTOCOL,
  1029. INFO_TYPE_CONNECTION,
  1030. TCP_SOCKET_KEEPALIVE,
  1031. &true,
  1032. sizeof(true),
  1033. TRUE
  1034. );
  1035. if ( err != NO_ERROR ) {
  1036. return err;
  1037. }
  1038. }
  1039. if ( context->KeepAliveVals.onoff ) {
  1040. err = SetTdiInformation(
  1041. TdiConnectionObjectHandle,
  1042. CO_TL_ENTITY,
  1043. INFO_CLASS_PROTOCOL,
  1044. INFO_TYPE_CONNECTION,
  1045. TCP_SOCKET_KEEPALIVE_VALS,
  1046. &context->KeepAliveVals,
  1047. sizeof(struct tcp_keepalive),
  1048. TRUE
  1049. );
  1050. if ( err != NO_ERROR ) {
  1051. return err;
  1052. }
  1053. }
  1054. if ( context->NoDelay ) {
  1055. err = SetTdiInformation(
  1056. TdiConnectionObjectHandle,
  1057. CO_TL_ENTITY,
  1058. INFO_CLASS_PROTOCOL,
  1059. INFO_TYPE_CONNECTION,
  1060. TCP_SOCKET_NODELAY,
  1061. &true,
  1062. sizeof(true),
  1063. TRUE
  1064. );
  1065. if ( err != NO_ERROR ) {
  1066. return err;
  1067. }
  1068. }
  1069. if ( context->ReceiveBufferSize != DEFAULT_RECEIVE_BUFFER_SIZE ) {
  1070. err = SetTdiInformation(
  1071. TdiConnectionObjectHandle,
  1072. CO_TL_ENTITY,
  1073. INFO_CLASS_PROTOCOL,
  1074. INFO_TYPE_CONNECTION,
  1075. TCP_SOCKET_WINDOW,
  1076. &context->ReceiveBufferSize,
  1077. sizeof(context->ReceiveBufferSize),
  1078. TRUE
  1079. );
  1080. if ( err != NO_ERROR ) {
  1081. return err;
  1082. }
  1083. }
  1084. if ( !context->BsdUrgent ) {
  1085. err = SetTdiInformation(
  1086. TdiConnectionObjectHandle,
  1087. CO_TL_ENTITY,
  1088. INFO_CLASS_PROTOCOL,
  1089. INFO_TYPE_CONNECTION,
  1090. TCP_SOCKET_BSDURGENT,
  1091. &false,
  1092. sizeof(false),
  1093. TRUE
  1094. );
  1095. if ( err != NO_ERROR ) {
  1096. return err;
  1097. }
  1098. }
  1099. if ( context->IpTos != DEFAULT_IP_TOS ) {
  1100. int value = (int) context->IpTos;
  1101. err = SetTdiInformation(
  1102. TdiConnectionObjectHandle,
  1103. CO_TL_ENTITY,
  1104. INFO_CLASS_PROTOCOL,
  1105. INFO_TYPE_CONNECTION,
  1106. TCP_SOCKET_TOS,
  1107. &value,
  1108. sizeof(int),
  1109. TRUE
  1110. );
  1111. if ( err != NO_ERROR ) {
  1112. return err;
  1113. }
  1114. }
  1115. } else if ( NotifyEvent == WSH_NOTIFY_CLOSE ) {
  1116. //
  1117. // If this is a multipoint leaf, then remove the multipoint target
  1118. // from the session.
  1119. //
  1120. if( context->MultipointLeaf &&
  1121. TdiAddressObjectHandle != NULL ) {
  1122. struct ip_mreq req;
  1123. req.imr_multiaddr = context->MultipointTarget;
  1124. req.imr_interface.s_addr = 0;
  1125. SetTdiInformation(
  1126. TdiAddressObjectHandle,
  1127. CL_TL_ENTITY,
  1128. INFO_CLASS_PROTOCOL,
  1129. INFO_TYPE_ADDRESS_OBJECT,
  1130. AO_OPTION_DEL_MCAST,
  1131. &req,
  1132. sizeof(req),
  1133. TRUE
  1134. );
  1135. }
  1136. //
  1137. // Free the socket context.
  1138. //
  1139. RtlFreeHeap( RtlProcessHeap( ), 0, context );
  1140. } else if ( NotifyEvent == WSH_NOTIFY_CONNECT_ERROR ) {
  1141. //
  1142. // Return WSATRY_AGAIN to get wsock32 to attempt the connect
  1143. // again. Any other return code is ignored.
  1144. //
  1145. } else if ( NotifyEvent == WSH_NOTIFY_BIND ) {
  1146. ULONG true = TRUE;
  1147. if( context->UdpNoChecksum ) {
  1148. ULONG flag = FALSE;
  1149. err = SetTdiInformation(
  1150. TdiAddressObjectHandle,
  1151. CL_TL_ENTITY,
  1152. INFO_CLASS_PROTOCOL,
  1153. INFO_TYPE_ADDRESS_OBJECT,
  1154. AO_OPTION_XSUM,
  1155. &flag,
  1156. sizeof(flag),
  1157. TRUE
  1158. );
  1159. if( err != NO_ERROR ) {
  1160. return err;
  1161. }
  1162. }
  1163. if ( context->IpTtl != DEFAULT_IP_TTL ) {
  1164. int value = (int) context->IpTtl;
  1165. err = SetTdiInformation(
  1166. TdiAddressObjectHandle,
  1167. CO_TL_ENTITY,
  1168. INFO_CLASS_PROTOCOL,
  1169. INFO_TYPE_ADDRESS_OBJECT,
  1170. AO_OPTION_TTL,
  1171. &value,
  1172. sizeof(int),
  1173. TRUE
  1174. );
  1175. if ( err != NO_ERROR ) {
  1176. return err;
  1177. }
  1178. }
  1179. if ( context->IpTos != DEFAULT_IP_TOS ) {
  1180. int value = (int) context->IpTos;
  1181. err = SetTdiInformation(
  1182. TdiAddressObjectHandle,
  1183. CO_TL_ENTITY,
  1184. INFO_CLASS_PROTOCOL,
  1185. INFO_TYPE_ADDRESS_OBJECT,
  1186. AO_OPTION_TOS,
  1187. &value,
  1188. sizeof(int),
  1189. TRUE
  1190. );
  1191. if ( err != NO_ERROR ) {
  1192. return err;
  1193. }
  1194. }
  1195. if ( context->MulticastTtl != DEFAULT_MULTICAST_TTL ) {
  1196. int value = (int) context->MulticastTtl;
  1197. err = SetTdiInformation(
  1198. TdiAddressObjectHandle,
  1199. CO_TL_ENTITY,
  1200. INFO_CLASS_PROTOCOL,
  1201. INFO_TYPE_ADDRESS_OBJECT,
  1202. AO_OPTION_MCASTTTL,
  1203. &value,
  1204. sizeof(int),
  1205. TRUE
  1206. );
  1207. if ( err != NO_ERROR ) {
  1208. return err;
  1209. }
  1210. }
  1211. if ( context->MulticastInterface != DEFAULT_MULTICAST_INTERFACE ) {
  1212. int value = (int) context->MulticastInterface;
  1213. err = SetTdiInformation(
  1214. TdiAddressObjectHandle,
  1215. CO_TL_ENTITY,
  1216. INFO_CLASS_PROTOCOL,
  1217. INFO_TYPE_ADDRESS_OBJECT,
  1218. AO_OPTION_MCASTIF,
  1219. &value,
  1220. sizeof(int),
  1221. TRUE
  1222. );
  1223. if ( err != NO_ERROR ) {
  1224. return err;
  1225. }
  1226. }
  1227. if ( context->MulticastLoopback != DEFAULT_MULTICAST_LOOPBACK ) {
  1228. int value = (int) context->MulticastLoopback;
  1229. err = SetTdiInformation(
  1230. TdiAddressObjectHandle,
  1231. CO_TL_ENTITY,
  1232. INFO_CLASS_PROTOCOL,
  1233. INFO_TYPE_ADDRESS_OBJECT,
  1234. AO_OPTION_MCASTLOOP,
  1235. &value,
  1236. sizeof(int),
  1237. TRUE
  1238. );
  1239. if ( err != NO_ERROR ) {
  1240. return err;
  1241. }
  1242. }
  1243. if ( context->HdrIncludeSet ) {
  1244. err = SetTdiInformation(
  1245. TdiAddressObjectHandle,
  1246. CO_TL_ENTITY,
  1247. INFO_CLASS_PROTOCOL,
  1248. INFO_TYPE_ADDRESS_OBJECT,
  1249. AO_OPTION_IP_HDRINCL,
  1250. &context->HdrInclude,
  1251. sizeof (context->HdrInclude),
  1252. TRUE
  1253. );
  1254. if ( err != NO_ERROR ) {
  1255. return err;
  1256. }
  1257. }
  1258. if (context->IpOptionsLength > 0 ) {
  1259. err = SetTdiInformation(
  1260. TdiAddressObjectHandle,
  1261. CO_TL_ENTITY,
  1262. INFO_CLASS_PROTOCOL,
  1263. INFO_TYPE_ADDRESS_OBJECT,
  1264. AO_OPTION_IPOPTIONS,
  1265. context->IpOptions,
  1266. context->IpOptionsLength,
  1267. TRUE
  1268. );
  1269. if ( err != NO_ERROR ) {
  1270. return err;
  1271. }
  1272. }
  1273. if ( context->UcastIf != DEFAULT_UCAST_IF ) {
  1274. int value = (int) context->UcastIf;
  1275. err = SetTdiInformation(
  1276. TdiAddressObjectHandle,
  1277. CO_TL_ENTITY,
  1278. INFO_CLASS_PROTOCOL,
  1279. INFO_TYPE_ADDRESS_OBJECT,
  1280. AO_OPTION_IP_UCASTIF,
  1281. &value,
  1282. sizeof(int),
  1283. TRUE
  1284. );
  1285. if ( err != NO_ERROR ) {
  1286. return err;
  1287. }
  1288. }
  1289. if ( context->IpDontFragment ) {
  1290. err = SetTdiInformation(
  1291. TdiAddressObjectHandle,
  1292. CO_TL_ENTITY,
  1293. INFO_CLASS_PROTOCOL,
  1294. INFO_TYPE_ADDRESS_OBJECT,
  1295. AO_OPTION_IP_DONTFRAGMENT,
  1296. &true,
  1297. sizeof(true),
  1298. TRUE
  1299. );
  1300. if ( err != NO_ERROR ) {
  1301. return err;
  1302. }
  1303. }
  1304. if ( context->Broadcast != DEFAULT_BROADCAST ) {
  1305. int value = (int) context->Broadcast;
  1306. err = SetTdiInformation(
  1307. TdiAddressObjectHandle,
  1308. CO_TL_ENTITY,
  1309. INFO_CLASS_PROTOCOL,
  1310. INFO_TYPE_ADDRESS_OBJECT,
  1311. AO_OPTION_BROADCAST,
  1312. &value,
  1313. sizeof(int),
  1314. TRUE
  1315. );
  1316. if ( err != NO_ERROR ) {
  1317. return err;
  1318. }
  1319. }
  1320. if ( context->LimitBroadcasts ) {
  1321. err = SetTdiInformation(
  1322. TdiAddressObjectHandle,
  1323. CO_TL_ENTITY,
  1324. INFO_CLASS_PROTOCOL,
  1325. INFO_TYPE_ADDRESS_OBJECT,
  1326. AO_OPTION_LIMIT_BCASTS,
  1327. &true,
  1328. sizeof(true),
  1329. TRUE
  1330. );
  1331. if ( err != NO_ERROR ) {
  1332. return err;
  1333. }
  1334. }
  1335. if ( context->IpPktInfo ) {
  1336. err = SetTdiInformation(
  1337. TdiAddressObjectHandle,
  1338. CO_TL_ENTITY,
  1339. INFO_CLASS_PROTOCOL,
  1340. INFO_TYPE_ADDRESS_OBJECT,
  1341. AO_OPTION_IP_PKTINFO,
  1342. &true,
  1343. sizeof (TRUE),
  1344. TRUE
  1345. );
  1346. if ( err != NO_ERROR ) {
  1347. return err;
  1348. }
  1349. }
  1350. } else {
  1351. return WSAEINVAL;
  1352. }
  1353. return NO_ERROR;
  1354. } // WSHNotify
  1355. INT
  1356. WSHSetSocketInformation (
  1357. IN PVOID HelperDllSocketContext,
  1358. IN SOCKET SocketHandle,
  1359. IN HANDLE TdiAddressObjectHandle,
  1360. IN HANDLE TdiConnectionObjectHandle,
  1361. IN INT Level,
  1362. IN INT OptionName,
  1363. IN PCHAR OptionValue,
  1364. IN INT OptionLength
  1365. )
  1366. /*++
  1367. Routine Description:
  1368. This routine sets information about a socket for those socket
  1369. options supported in this helper DLL. The options supported here
  1370. are SO_KEEPALIVE, SO_DONTROUTE, and TCP_EXPEDITED_1122. This routine is
  1371. called by the winsock DLL when a level/option name combination is
  1372. passed to setsockopt() that the winsock DLL does not understand.
  1373. Arguments:
  1374. HelperDllSocketContext - the context pointer returned from
  1375. WSHOpenSocket().
  1376. SocketHandle - the handle of the socket for which we're getting
  1377. information.
  1378. TdiAddressObjectHandle - the TDI address object of the socket, if
  1379. any. If the socket is not yet bound to an address, then
  1380. it does not have a TDI address object and this parameter
  1381. will be NULL.
  1382. TdiConnectionObjectHandle - the TDI connection object of the socket,
  1383. if any. If the socket is not yet connected, then it does not
  1384. have a TDI connection object and this parameter will be NULL.
  1385. Level - the level parameter passed to setsockopt().
  1386. OptionName - the optname parameter passed to setsockopt().
  1387. OptionValue - the optval parameter passed to setsockopt().
  1388. OptionLength - the optlen parameter passed to setsockopt().
  1389. Return Value:
  1390. INT - a winsock error code indicating the status of the operation, or
  1391. NO_ERROR if the operation succeeded.
  1392. --*/
  1393. {
  1394. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  1395. INT error;
  1396. INT optionValue;
  1397. //
  1398. // Check if this is an internal request for context information.
  1399. //
  1400. if ( Level == SOL_INTERNAL && OptionName == SO_CONTEXT ) {
  1401. //
  1402. // The Windows Sockets DLL is requesting that we set context
  1403. // information for a new socket. If the new socket was
  1404. // accept()'ed, then we have already been notified of the socket
  1405. // and HelperDllSocketContext will be valid. If the new socket
  1406. // was inherited or duped into this process, then this is our
  1407. // first notification of the socket and HelperDllSocketContext
  1408. // will be equal to NULL.
  1409. //
  1410. // Insure that the context information being passed to us is
  1411. // sufficiently large.
  1412. //
  1413. if ( OptionLength < sizeof(*context) ) {
  1414. return WSAEINVAL;
  1415. }
  1416. if ( HelperDllSocketContext == NULL ) {
  1417. //
  1418. // This is our notification that a socket handle was
  1419. // inherited or duped into this process. Allocate a context
  1420. // structure for the new socket.
  1421. //
  1422. context = RtlAllocateHeap( RtlProcessHeap( ), 0, sizeof(*context) );
  1423. if ( context == NULL ) {
  1424. return WSAENOBUFS;
  1425. }
  1426. //
  1427. // Copy over information into the context block.
  1428. //
  1429. RtlCopyMemory( context, OptionValue, sizeof(*context) );
  1430. //
  1431. // Tell the Windows Sockets DLL where our context information is
  1432. // stored so that it can return the context pointer in future
  1433. // calls.
  1434. //
  1435. *(PWSHTCPIP_SOCKET_CONTEXT *)OptionValue = context;
  1436. return NO_ERROR;
  1437. } else {
  1438. PWSHTCPIP_SOCKET_CONTEXT parentContext;
  1439. INT one = 1;
  1440. INT zero = 0;
  1441. //
  1442. // The socket was accept()'ed and it needs to have the same
  1443. // properties as it's parent. The OptionValue buffer
  1444. // contains the context information of this socket's parent.
  1445. //
  1446. parentContext = (PWSHTCPIP_SOCKET_CONTEXT)OptionValue;
  1447. ASSERT( context->AddressFamily == parentContext->AddressFamily );
  1448. ASSERT( context->SocketType == parentContext->SocketType );
  1449. ASSERT( context->Protocol == parentContext->Protocol );
  1450. //
  1451. // Turn on in the child any options that have been set in
  1452. // the parent.
  1453. //
  1454. if ( parentContext->KeepAlive ) {
  1455. error = WSHSetSocketInformation(
  1456. HelperDllSocketContext,
  1457. SocketHandle,
  1458. TdiAddressObjectHandle,
  1459. TdiConnectionObjectHandle,
  1460. SOL_SOCKET,
  1461. SO_KEEPALIVE,
  1462. (PCHAR)&one,
  1463. sizeof(one)
  1464. );
  1465. if ( error != NO_ERROR ) {
  1466. return error;
  1467. }
  1468. }
  1469. if ( parentContext->KeepAliveVals.onoff ) {
  1470. struct tcp_keepalive *optionval;
  1471. //
  1472. // Atempt to turn on or off keepalive sending, as necessary.
  1473. //
  1474. optionval = (struct tcp_keepalive *)&parentContext->KeepAliveVals;
  1475. if ( TdiConnectionObjectHandle != NULL ) {
  1476. error = SetTdiInformation(
  1477. TdiConnectionObjectHandle,
  1478. CO_TL_ENTITY,
  1479. INFO_CLASS_PROTOCOL,
  1480. INFO_TYPE_CONNECTION,
  1481. TCP_SOCKET_KEEPALIVE_VALS,
  1482. optionval,
  1483. sizeof(struct tcp_keepalive),
  1484. TRUE
  1485. );
  1486. if ( error != NO_ERROR ) {
  1487. return error;
  1488. }
  1489. }
  1490. //
  1491. // Remember that keepalives are enabled for this socket.
  1492. //
  1493. context->KeepAliveVals.onoff = TRUE;
  1494. context->KeepAliveVals.keepalivetime = optionval->keepalivetime;
  1495. context->KeepAliveVals.keepaliveinterval = optionval->keepaliveinterval;
  1496. }
  1497. if ( parentContext->DontRoute ) {
  1498. error = WSHSetSocketInformation(
  1499. HelperDllSocketContext,
  1500. SocketHandle,
  1501. TdiAddressObjectHandle,
  1502. TdiConnectionObjectHandle,
  1503. SOL_SOCKET,
  1504. SO_DONTROUTE,
  1505. (PCHAR)&one,
  1506. sizeof(one)
  1507. );
  1508. if ( error != NO_ERROR ) {
  1509. return error;
  1510. }
  1511. }
  1512. if ( parentContext->NoDelay ) {
  1513. error = WSHSetSocketInformation(
  1514. HelperDllSocketContext,
  1515. SocketHandle,
  1516. TdiAddressObjectHandle,
  1517. TdiConnectionObjectHandle,
  1518. IPPROTO_TCP,
  1519. TCP_NODELAY,
  1520. (PCHAR)&one,
  1521. sizeof(one)
  1522. );
  1523. if ( error != NO_ERROR ) {
  1524. return error;
  1525. }
  1526. }
  1527. if ( parentContext->ReceiveBufferSize != DEFAULT_RECEIVE_BUFFER_SIZE ) {
  1528. error = WSHSetSocketInformation(
  1529. HelperDllSocketContext,
  1530. SocketHandle,
  1531. TdiAddressObjectHandle,
  1532. TdiConnectionObjectHandle,
  1533. SOL_SOCKET,
  1534. SO_RCVBUF,
  1535. (PCHAR)&parentContext->ReceiveBufferSize,
  1536. sizeof(parentContext->ReceiveBufferSize)
  1537. );
  1538. if ( error != NO_ERROR ) {
  1539. return error;
  1540. }
  1541. }
  1542. if ( !parentContext->BsdUrgent ) {
  1543. error = WSHSetSocketInformation(
  1544. HelperDllSocketContext,
  1545. SocketHandle,
  1546. TdiAddressObjectHandle,
  1547. TdiConnectionObjectHandle,
  1548. IPPROTO_TCP,
  1549. TCP_EXPEDITED_1122,
  1550. (PCHAR)&one,
  1551. sizeof(one)
  1552. );
  1553. if ( error != NO_ERROR ) {
  1554. return error;
  1555. }
  1556. }
  1557. if ( parentContext->IpTos != DEFAULT_IP_TOS ) {
  1558. int value = (int) parentContext->IpTos;
  1559. error = WSHSetSocketInformation(
  1560. HelperDllSocketContext,
  1561. SocketHandle,
  1562. TdiAddressObjectHandle,
  1563. TdiConnectionObjectHandle,
  1564. IPPROTO_IP,
  1565. IP_TOS,
  1566. (PCHAR)&value,
  1567. sizeof(value)
  1568. );
  1569. if ( error != NO_ERROR ) {
  1570. return error;
  1571. }
  1572. }
  1573. return NO_ERROR;
  1574. }
  1575. }
  1576. //
  1577. // The only other levels we support here are SOL_SOCKET,
  1578. // IPPROTO_TCP, IPPROTO_UDP, and IPPROTO_IP.
  1579. //
  1580. if ( Level != SOL_SOCKET &&
  1581. Level != IPPROTO_TCP &&
  1582. Level != IPPROTO_UDP &&
  1583. Level != IPPROTO_IP ) {
  1584. return WSAEINVAL;
  1585. }
  1586. //
  1587. // Make sure that the option length is sufficient.
  1588. //
  1589. if ( OptionLength < sizeof(char) ) {
  1590. return WSAEFAULT;
  1591. }
  1592. if ( OptionLength >= sizeof (int)) {
  1593. optionValue = *((INT UNALIGNED *)OptionValue);
  1594. }
  1595. else {
  1596. optionValue = (UCHAR)*OptionValue;
  1597. }
  1598. //
  1599. // Handle TCP-level options.
  1600. //
  1601. if ( Level == IPPROTO_TCP && OptionName == TCP_NODELAY ) {
  1602. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  1603. return WSAENOPROTOOPT;
  1604. }
  1605. //
  1606. // Atempt to turn on or off Nagle's algorithm, as necessary.
  1607. //
  1608. if ( !context->NoDelay && optionValue != 0 ) {
  1609. optionValue = TRUE;
  1610. //
  1611. // NoDelay is currently off and the application wants to
  1612. // turn it on. If the TDI connection object handle is NULL,
  1613. // then the socket is not yet connected. In this case we'll
  1614. // just remember that the no delay option was set and
  1615. // actually turn them on in WSHNotify() after a connect()
  1616. // has completed on the socket.
  1617. //
  1618. if ( TdiConnectionObjectHandle != NULL ) {
  1619. error = SetTdiInformation(
  1620. TdiConnectionObjectHandle,
  1621. CO_TL_ENTITY,
  1622. INFO_CLASS_PROTOCOL,
  1623. INFO_TYPE_CONNECTION,
  1624. TCP_SOCKET_NODELAY,
  1625. &optionValue,
  1626. sizeof(optionValue),
  1627. TRUE
  1628. );
  1629. if ( error != NO_ERROR ) {
  1630. return error;
  1631. }
  1632. }
  1633. //
  1634. // Remember that no delay is enabled for this socket.
  1635. //
  1636. context->NoDelay = TRUE;
  1637. } else if ( context->NoDelay && optionValue == 0 ) {
  1638. //
  1639. // No delay is currently enabled and the application wants
  1640. // to turn it off. If the TDI connection object is NULL,
  1641. // the socket is not yet connected. In this case we'll just
  1642. // remember that nodelay is disabled.
  1643. //
  1644. if ( TdiConnectionObjectHandle != NULL ) {
  1645. error = SetTdiInformation(
  1646. TdiConnectionObjectHandle,
  1647. CO_TL_ENTITY,
  1648. INFO_CLASS_PROTOCOL,
  1649. INFO_TYPE_CONNECTION,
  1650. TCP_SOCKET_NODELAY,
  1651. &optionValue,
  1652. sizeof(optionValue),
  1653. TRUE
  1654. );
  1655. if ( error != NO_ERROR ) {
  1656. return error;
  1657. }
  1658. }
  1659. //
  1660. // Remember that no delay is disabled for this socket.
  1661. //
  1662. context->NoDelay = FALSE;
  1663. }
  1664. return NO_ERROR;
  1665. }
  1666. if ( Level == IPPROTO_TCP && OptionName == TCP_EXPEDITED_1122 ) {
  1667. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  1668. return WSAENOPROTOOPT;
  1669. }
  1670. //
  1671. // Atempt to turn on or off BSD-style urgent data semantics as
  1672. // necessary.
  1673. //
  1674. if ( !context->BsdUrgent && optionValue == 0 ) {
  1675. optionValue = TRUE;
  1676. //
  1677. // BsdUrgent is currently off and the application wants to
  1678. // turn it on. If the TDI connection object handle is NULL,
  1679. // then the socket is not yet connected. In this case we'll
  1680. // just remember that the no delay option was set and
  1681. // actually turn them on in WSHNotify() after a connect()
  1682. // has completed on the socket.
  1683. //
  1684. if ( TdiConnectionObjectHandle != NULL ) {
  1685. error = SetTdiInformation(
  1686. TdiConnectionObjectHandle,
  1687. CO_TL_ENTITY,
  1688. INFO_CLASS_PROTOCOL,
  1689. INFO_TYPE_CONNECTION,
  1690. TCP_SOCKET_BSDURGENT,
  1691. &optionValue,
  1692. sizeof(optionValue),
  1693. TRUE
  1694. );
  1695. if ( error != NO_ERROR ) {
  1696. return error;
  1697. }
  1698. }
  1699. //
  1700. // Remember that BSD urgent is enabled for this socket.
  1701. //
  1702. context->BsdUrgent = TRUE;
  1703. } else if ( context->BsdUrgent && optionValue != 0 ) {
  1704. //
  1705. // No delay is currently enabled and the application wants
  1706. // to turn it off. If the TDI connection object is NULL,
  1707. // the socket is not yet connected. In this case we'll just
  1708. // remember that BsdUrgent is disabled.
  1709. //
  1710. if ( TdiConnectionObjectHandle != NULL ) {
  1711. error = SetTdiInformation(
  1712. TdiConnectionObjectHandle,
  1713. CO_TL_ENTITY,
  1714. INFO_CLASS_PROTOCOL,
  1715. INFO_TYPE_CONNECTION,
  1716. TCP_SOCKET_BSDURGENT,
  1717. &optionValue,
  1718. sizeof(optionValue),
  1719. TRUE
  1720. );
  1721. if ( error != NO_ERROR ) {
  1722. return error;
  1723. }
  1724. }
  1725. //
  1726. // Remember that BSD urgent is disabled for this socket.
  1727. //
  1728. context->BsdUrgent = FALSE;
  1729. }
  1730. return NO_ERROR;
  1731. }
  1732. //
  1733. // Handle UDP-level options.
  1734. //
  1735. if ( Level == IPPROTO_UDP ) {
  1736. switch ( OptionName ) {
  1737. case UDP_NOCHECKSUM :
  1738. //
  1739. // This option is only valid for datagram sockets.
  1740. //
  1741. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1742. return WSAENOPROTOOPT;
  1743. }
  1744. if( TdiAddressObjectHandle != NULL ) {
  1745. ULONG flag;
  1746. //
  1747. // Note that the incoming flag is TRUE if XSUM should
  1748. // be *disabled*, but the flag we pass to TDI is TRUE
  1749. // if it should be *enabled*, so we must negate the flag.
  1750. //
  1751. flag = (ULONG)!optionValue;
  1752. error = SetTdiInformation(
  1753. TdiAddressObjectHandle,
  1754. CL_TL_ENTITY,
  1755. INFO_CLASS_PROTOCOL,
  1756. INFO_TYPE_ADDRESS_OBJECT,
  1757. AO_OPTION_XSUM,
  1758. &flag,
  1759. sizeof(flag),
  1760. TRUE
  1761. );
  1762. if( error != NO_ERROR ) {
  1763. return error;
  1764. }
  1765. }
  1766. context->UdpNoChecksum = !!optionValue;
  1767. break;
  1768. default :
  1769. return WSAEINVAL;
  1770. }
  1771. return NO_ERROR;
  1772. }
  1773. //
  1774. // Handle IP-level options.
  1775. //
  1776. if ( Level == IPPROTO_IP ) {
  1777. //
  1778. // Act based on the specific option.
  1779. //
  1780. switch ( OptionName ) {
  1781. case IP_TTL:
  1782. //
  1783. // An attempt to change the unicast TTL sent on
  1784. // this socket. It is illegal to set this to a value
  1785. // greater than 255.
  1786. //
  1787. if ( optionValue > 255 || optionValue < 0 ) {
  1788. return WSAEINVAL;
  1789. }
  1790. //
  1791. // If we have a TDI address object, set this option to
  1792. // the address object. If we don't have a TDI address
  1793. // object then we'll have to wait until after the socket
  1794. // is bound.
  1795. //
  1796. if ( TdiAddressObjectHandle != NULL ) {
  1797. error = SetTdiInformation(
  1798. TdiAddressObjectHandle,
  1799. CL_TL_ENTITY,
  1800. INFO_CLASS_PROTOCOL,
  1801. INFO_TYPE_ADDRESS_OBJECT,
  1802. AO_OPTION_TTL,
  1803. &optionValue,
  1804. sizeof(optionValue),
  1805. TRUE
  1806. );
  1807. if ( error != NO_ERROR ) {
  1808. return error;
  1809. }
  1810. }
  1811. context->IpTtl = (uchar) optionValue;
  1812. return NO_ERROR;
  1813. case IP_TOS:
  1814. //
  1815. // An attempt to change the Type Of Service of packets sent on
  1816. // this socket. It is illegal to set this to a value
  1817. // greater than 255.
  1818. //
  1819. if ( optionValue > 255 || optionValue < 0 ) {
  1820. return WSAEINVAL;
  1821. }
  1822. //
  1823. // If we have a TDI address or connectionobject,
  1824. // set this option to it. If we don't have a TDI
  1825. // object then we'll have to wait until after the socket
  1826. // is bound or connected.
  1827. //
  1828. if ( TdiConnectionObjectHandle != NULL ) {
  1829. error = SetTdiInformation(
  1830. TdiConnectionObjectHandle,
  1831. CO_TL_ENTITY,
  1832. INFO_CLASS_PROTOCOL,
  1833. INFO_TYPE_CONNECTION,
  1834. TCP_SOCKET_TOS,
  1835. &optionValue,
  1836. sizeof(optionValue),
  1837. TRUE
  1838. );
  1839. if ( error != NO_ERROR ) {
  1840. return error;
  1841. }
  1842. }
  1843. else if ( TdiAddressObjectHandle != NULL ) {
  1844. error = SetTdiInformation(
  1845. TdiAddressObjectHandle,
  1846. CL_TL_ENTITY,
  1847. INFO_CLASS_PROTOCOL,
  1848. INFO_TYPE_ADDRESS_OBJECT,
  1849. AO_OPTION_TOS,
  1850. &optionValue,
  1851. sizeof(optionValue),
  1852. TRUE
  1853. );
  1854. if ( error != NO_ERROR ) {
  1855. return error;
  1856. }
  1857. }
  1858. context->IpTos = (uchar) optionValue;
  1859. return NO_ERROR;
  1860. case IP_MULTICAST_TTL:
  1861. //
  1862. // This option is only valid for datagram sockets.
  1863. //
  1864. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1865. return WSAENOPROTOOPT;
  1866. }
  1867. //
  1868. // An attempt to change the TTL on multicasts sent on
  1869. // this socket. It is illegal to set this to a value
  1870. // greater than 255.
  1871. //
  1872. if ( optionValue > 255 || optionValue < 0 ) {
  1873. return WSAEINVAL;
  1874. }
  1875. //
  1876. // If we have a TDI address object, set this option to
  1877. // the address object. If we don't have a TDI address
  1878. // object then we'll have to wait until after the socket
  1879. // is bound.
  1880. //
  1881. if ( TdiAddressObjectHandle != NULL ) {
  1882. error = SetTdiInformation(
  1883. TdiAddressObjectHandle,
  1884. CL_TL_ENTITY,
  1885. INFO_CLASS_PROTOCOL,
  1886. INFO_TYPE_ADDRESS_OBJECT,
  1887. AO_OPTION_MCASTTTL,
  1888. &optionValue,
  1889. sizeof(optionValue),
  1890. TRUE
  1891. );
  1892. if ( error != NO_ERROR ) {
  1893. return error;
  1894. }
  1895. }
  1896. context->MulticastTtl = optionValue;
  1897. return NO_ERROR;
  1898. case IP_MULTICAST_IF:
  1899. //
  1900. // This option is only valid for datagram sockets.
  1901. //
  1902. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1903. return WSAENOPROTOOPT;
  1904. }
  1905. //
  1906. // If we have a TDI address object, set this option to
  1907. // the address object. If we don't have a TDI address
  1908. // object then we'll have to wait until after the socket
  1909. // is bound.
  1910. //
  1911. if ( TdiAddressObjectHandle != NULL ) {
  1912. error = SetTdiInformation(
  1913. TdiAddressObjectHandle,
  1914. CL_TL_ENTITY,
  1915. INFO_CLASS_PROTOCOL,
  1916. INFO_TYPE_ADDRESS_OBJECT,
  1917. AO_OPTION_MCASTIF,
  1918. &optionValue,
  1919. sizeof(optionValue),
  1920. TRUE
  1921. );
  1922. if ( error != NO_ERROR ) {
  1923. return error;
  1924. }
  1925. }
  1926. context->MulticastInterface = optionValue;
  1927. return NO_ERROR;
  1928. case IP_MULTICAST_LOOP:
  1929. //
  1930. // This option is only valid for datagram sockets.
  1931. //
  1932. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1933. return WSAENOPROTOOPT;
  1934. }
  1935. if ( TdiAddressObjectHandle != NULL ) {
  1936. error = SetTdiInformation(
  1937. TdiAddressObjectHandle,
  1938. CL_TL_ENTITY,
  1939. INFO_CLASS_PROTOCOL,
  1940. INFO_TYPE_ADDRESS_OBJECT,
  1941. AO_OPTION_MCASTLOOP,
  1942. &optionValue,
  1943. sizeof(optionValue),
  1944. TRUE
  1945. );
  1946. if ( error != NO_ERROR ) {
  1947. return error;
  1948. }
  1949. }
  1950. context->MulticastLoopback = optionValue ? TRUE : FALSE;
  1951. return NO_ERROR;
  1952. case IP_ADD_MEMBERSHIP:
  1953. case IP_DROP_MEMBERSHIP:
  1954. //
  1955. // This option is only valid for datagram sockets.
  1956. //
  1957. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1958. return WSAENOPROTOOPT;
  1959. }
  1960. //
  1961. // Make sure that the option buffer is large enough.
  1962. //
  1963. if ( OptionLength < sizeof(struct ip_mreq) ) {
  1964. return WSAEFAULT;
  1965. }
  1966. //
  1967. // If we have a TDI address object, set this option to
  1968. // the address object. If we don't have a TDI address
  1969. // object then we'll have to wait until after the socket
  1970. // is bound.
  1971. //
  1972. if ( TdiAddressObjectHandle != NULL ) {
  1973. error = SetTdiInformation(
  1974. TdiAddressObjectHandle,
  1975. CL_TL_ENTITY,
  1976. INFO_CLASS_PROTOCOL,
  1977. INFO_TYPE_ADDRESS_OBJECT,
  1978. OptionName == IP_ADD_MEMBERSHIP ?
  1979. AO_OPTION_ADD_MCAST : AO_OPTION_DEL_MCAST,
  1980. OptionValue,
  1981. OptionLength,
  1982. TRUE
  1983. );
  1984. if ( error != NO_ERROR ) {
  1985. return error;
  1986. }
  1987. }
  1988. return NO_ERROR;
  1989. case IP_BLOCK_SOURCE:
  1990. case IP_UNBLOCK_SOURCE:
  1991. //
  1992. // This option is only valid for datagram sockets.
  1993. //
  1994. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1995. return WSAENOPROTOOPT;
  1996. }
  1997. //
  1998. // Make sure that the option buffer is large enough.
  1999. //
  2000. if ( OptionLength < sizeof(struct ip_mreq_source) ) {
  2001. return WSAEFAULT;
  2002. }
  2003. //
  2004. // If we have a TDI address object, set this option to
  2005. // the address object. If we don't have a TDI address
  2006. // object then we'll have to wait until after the socket
  2007. // is bound.
  2008. //
  2009. if ( TdiAddressObjectHandle != NULL ) {
  2010. error = SetTdiInformation(
  2011. TdiAddressObjectHandle,
  2012. CL_TL_ENTITY,
  2013. INFO_CLASS_PROTOCOL,
  2014. INFO_TYPE_ADDRESS_OBJECT,
  2015. OptionName == IP_BLOCK_SOURCE ?
  2016. AO_OPTION_BLOCK_MCAST_SRC :
  2017. AO_OPTION_UNBLOCK_MCAST_SRC,
  2018. OptionValue,
  2019. OptionLength,
  2020. TRUE
  2021. );
  2022. if ( error != NO_ERROR ) {
  2023. return error;
  2024. }
  2025. }
  2026. return NO_ERROR;
  2027. case IP_ADD_SOURCE_MEMBERSHIP:
  2028. case IP_DROP_SOURCE_MEMBERSHIP:
  2029. //
  2030. // This option is only valid for datagram sockets.
  2031. //
  2032. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  2033. return WSAENOPROTOOPT;
  2034. }
  2035. //
  2036. // Make sure that the option buffer is large enough.
  2037. //
  2038. if ( OptionLength < sizeof(struct ip_mreq_source) ) {
  2039. return WSAEFAULT;
  2040. }
  2041. //
  2042. // If we have a TDI address object, set this option to
  2043. // the address object. If we don't have a TDI address
  2044. // object then we'll have to wait until after the socket
  2045. // is bound.
  2046. //
  2047. if ( TdiAddressObjectHandle != NULL ) {
  2048. error = SetTdiInformation(
  2049. TdiAddressObjectHandle,
  2050. CL_TL_ENTITY,
  2051. INFO_CLASS_PROTOCOL,
  2052. INFO_TYPE_ADDRESS_OBJECT,
  2053. OptionName == IP_ADD_SOURCE_MEMBERSHIP ?
  2054. AO_OPTION_ADD_MCAST_SRC :
  2055. AO_OPTION_DEL_MCAST_SRC,
  2056. OptionValue,
  2057. OptionLength,
  2058. TRUE
  2059. );
  2060. if ( error != NO_ERROR ) {
  2061. return error;
  2062. }
  2063. }
  2064. return NO_ERROR;
  2065. case IP_HDRINCL:
  2066. // User hdr include option
  2067. //
  2068. //
  2069. if ( OptionLength != 4) {
  2070. return WSAEINVAL;
  2071. }
  2072. if ( TdiAddressObjectHandle != NULL ) {
  2073. error = SetTdiInformation(
  2074. TdiAddressObjectHandle,
  2075. CL_TL_ENTITY,
  2076. INFO_CLASS_PROTOCOL,
  2077. INFO_TYPE_ADDRESS_OBJECT,
  2078. AO_OPTION_IP_HDRINCL,
  2079. &optionValue,
  2080. OptionLength,
  2081. TRUE
  2082. );
  2083. if ( error != NO_ERROR ) {
  2084. return error;
  2085. }
  2086. }
  2087. context->HdrIncludeSet = TRUE;
  2088. context->HdrInclude = optionValue;
  2089. return NO_ERROR;
  2090. case IP_PKTINFO:
  2091. //
  2092. // This option is only valid for datagram sockets.
  2093. //
  2094. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  2095. return WSAENOPROTOOPT;
  2096. }
  2097. if ( TdiAddressObjectHandle != NULL ) {
  2098. error = SetTdiInformation(
  2099. TdiAddressObjectHandle,
  2100. CL_TL_ENTITY,
  2101. INFO_CLASS_PROTOCOL,
  2102. INFO_TYPE_ADDRESS_OBJECT,
  2103. AO_OPTION_IP_PKTINFO,
  2104. &optionValue,
  2105. sizeof(optionValue),
  2106. TRUE
  2107. );
  2108. if ( error != NO_ERROR ) {
  2109. return error;
  2110. }
  2111. }
  2112. context->IpPktInfo = optionValue ? TRUE : FALSE;
  2113. return NO_ERROR;
  2114. default:
  2115. //
  2116. // No match, fall through.
  2117. //
  2118. break;
  2119. }
  2120. if ( OptionName == IP_OPTIONS ) {
  2121. //
  2122. // Setting IP options.
  2123. //
  2124. if (OptionLength < 0 || OptionLength > MAX_OPT_SIZE) {
  2125. return WSAEINVAL;
  2126. }
  2127. //
  2128. // Try to set these options. If the TDI address object handle
  2129. // is NULL, then the socket is not yet bound. In this case we'll
  2130. // just remember options and actually set them in WSHNotify()
  2131. // after a bind has completed on the socket.
  2132. //
  2133. if ( TdiAddressObjectHandle != NULL ) {
  2134. error = SetTdiInformation(
  2135. TdiAddressObjectHandle,
  2136. CO_TL_ENTITY,
  2137. INFO_CLASS_PROTOCOL,
  2138. INFO_TYPE_ADDRESS_OBJECT,
  2139. AO_OPTION_IPOPTIONS,
  2140. OptionValue,
  2141. OptionLength,
  2142. TRUE
  2143. );
  2144. if ( error != NO_ERROR ) {
  2145. return error;
  2146. }
  2147. }
  2148. //
  2149. // They were successfully set. Copy them.
  2150. //
  2151. RtlMoveMemory(context->IpOptions, OptionValue, OptionLength);
  2152. context->IpOptionsLength = (UCHAR)OptionLength;
  2153. return NO_ERROR;
  2154. }
  2155. if ( OptionName == IP_DONTFRAGMENT ) {
  2156. //
  2157. // Attempt to turn on or off the DF bit in the IP header.
  2158. //
  2159. if ( !context->IpDontFragment && optionValue != 0 ) {
  2160. optionValue = TRUE;
  2161. //
  2162. // DF is currently off and the application wants to
  2163. // turn it on. If the TDI address object handle is NULL,
  2164. // then the socket is not yet bound. In this case we'll
  2165. // just remember that the header inclusion option was set and
  2166. // actually turn it on in WSHNotify() after a bind
  2167. // has completed on the socket.
  2168. //
  2169. if ( TdiAddressObjectHandle != NULL ) {
  2170. error = SetTdiInformation(
  2171. TdiAddressObjectHandle,
  2172. CO_TL_ENTITY,
  2173. INFO_CLASS_PROTOCOL,
  2174. INFO_TYPE_ADDRESS_OBJECT,
  2175. AO_OPTION_IP_DONTFRAGMENT,
  2176. &optionValue,
  2177. sizeof(optionValue),
  2178. TRUE
  2179. );
  2180. if ( error != NO_ERROR ) {
  2181. return error;
  2182. }
  2183. }
  2184. //
  2185. // Remember that header inclusion is enabled for this socket.
  2186. //
  2187. context->IpDontFragment = TRUE;
  2188. } else if ( context->IpDontFragment && optionValue == 0 ) {
  2189. //
  2190. // The DF flag is currently set and the application wants
  2191. // to turn it off. If the TDI address object is NULL,
  2192. // the socket is not yet bound. In this case we'll just
  2193. // remember that the flag is turned off.
  2194. //
  2195. if ( TdiAddressObjectHandle != NULL ) {
  2196. error = SetTdiInformation(
  2197. TdiAddressObjectHandle,
  2198. CO_TL_ENTITY,
  2199. INFO_CLASS_PROTOCOL,
  2200. INFO_TYPE_ADDRESS_OBJECT,
  2201. AO_OPTION_IP_DONTFRAGMENT,
  2202. &optionValue,
  2203. sizeof(optionValue),
  2204. TRUE
  2205. );
  2206. if ( error != NO_ERROR ) {
  2207. return error;
  2208. }
  2209. }
  2210. //
  2211. // Remember that DF flag is not set for this socket.
  2212. //
  2213. context->IpDontFragment = FALSE;
  2214. }
  2215. return NO_ERROR;
  2216. }
  2217. //
  2218. // We don't support this option.
  2219. //
  2220. return WSAENOPROTOOPT;
  2221. }
  2222. //
  2223. // Handle socket-level options.
  2224. //
  2225. switch ( OptionName ) {
  2226. case SO_KEEPALIVE:
  2227. //
  2228. // Atempt to turn on or off keepalive sending, as necessary.
  2229. //
  2230. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  2231. return WSAENOPROTOOPT;
  2232. }
  2233. if ( !context->KeepAlive && optionValue != 0 ) {
  2234. optionValue = TRUE;
  2235. //
  2236. // Keepalives are currently off and the application wants to
  2237. // turn them on. If the TDI connection object handle is
  2238. // NULL, then the socket is not yet connected. In this case
  2239. // we'll just remember that the keepalive option was set and
  2240. // actually turn them on in WSHNotify() after a connect()
  2241. // has completed on the socket.
  2242. //
  2243. if ( TdiConnectionObjectHandle != NULL ) {
  2244. error = SetTdiInformation(
  2245. TdiConnectionObjectHandle,
  2246. CO_TL_ENTITY,
  2247. INFO_CLASS_PROTOCOL,
  2248. INFO_TYPE_CONNECTION,
  2249. TCP_SOCKET_KEEPALIVE,
  2250. &optionValue,
  2251. sizeof(optionValue),
  2252. TRUE
  2253. );
  2254. if ( error != NO_ERROR ) {
  2255. return error;
  2256. }
  2257. }
  2258. //
  2259. // Remember that keepalives are enabled for this socket.
  2260. //
  2261. context->KeepAlive = TRUE;
  2262. } else if ( context->KeepAlive && optionValue == 0 ) {
  2263. //
  2264. // Keepalives are currently enabled and the application
  2265. // wants to turn them off. If the TDI connection object is
  2266. // NULL, the socket is not yet connected. In this case
  2267. // we'll just remember that keepalives are disabled.
  2268. //
  2269. if ( TdiConnectionObjectHandle != NULL ) {
  2270. error = SetTdiInformation(
  2271. TdiConnectionObjectHandle,
  2272. CO_TL_ENTITY,
  2273. INFO_CLASS_PROTOCOL,
  2274. INFO_TYPE_CONNECTION,
  2275. TCP_SOCKET_KEEPALIVE,
  2276. &optionValue,
  2277. sizeof(optionValue),
  2278. TRUE
  2279. );
  2280. if ( error != NO_ERROR ) {
  2281. return error;
  2282. }
  2283. }
  2284. //
  2285. // Remember that keepalives are disabled for this socket.
  2286. //
  2287. context->KeepAlive = FALSE;
  2288. }
  2289. break;
  2290. case SO_DONTROUTE:
  2291. //
  2292. // We don't really support SO_DONTROUTE. Just remember that the
  2293. // option was set or unset.
  2294. //
  2295. if ( optionValue != 0 ) {
  2296. context->DontRoute = TRUE;
  2297. } else if ( optionValue == 0 ) {
  2298. context->DontRoute = FALSE;
  2299. }
  2300. break;
  2301. case SO_RCVBUF:
  2302. //
  2303. // If the receive buffer size is being changed, tell TCP about
  2304. // it. Do nothing if this is a datagram.
  2305. //
  2306. if ( context->ReceiveBufferSize == optionValue ||
  2307. IS_DGRAM_SOCK(context->SocketType)
  2308. ) {
  2309. break;
  2310. }
  2311. if ( TdiConnectionObjectHandle != NULL ) {
  2312. error = SetTdiInformation(
  2313. TdiConnectionObjectHandle,
  2314. CO_TL_ENTITY,
  2315. INFO_CLASS_PROTOCOL,
  2316. INFO_TYPE_CONNECTION,
  2317. TCP_SOCKET_WINDOW,
  2318. &optionValue,
  2319. sizeof(optionValue),
  2320. TRUE
  2321. );
  2322. if ( error != NO_ERROR ) {
  2323. return error;
  2324. }
  2325. }
  2326. context->ReceiveBufferSize = optionValue;
  2327. break;
  2328. case SO_BROADCAST:
  2329. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  2330. if ( TdiAddressObjectHandle != NULL ) {
  2331. error = SetTdiInformation(
  2332. TdiAddressObjectHandle,
  2333. CO_TL_ENTITY,
  2334. INFO_CLASS_PROTOCOL,
  2335. INFO_TYPE_ADDRESS_OBJECT,
  2336. AO_OPTION_BROADCAST,
  2337. &optionValue,
  2338. sizeof(optionValue),
  2339. TRUE
  2340. );
  2341. if ( error != NO_ERROR ) {
  2342. return error;
  2343. }
  2344. }
  2345. context->Broadcast = ( optionValue ? TRUE : FALSE );
  2346. break;
  2347. }
  2348. default:
  2349. return WSAENOPROTOOPT;
  2350. }
  2351. return NO_ERROR;
  2352. } // WSHSetSocketInformation
  2353. INT
  2354. WSHEnumProtocols (
  2355. IN LPINT lpiProtocols,
  2356. IN LPWSTR lpTransportKeyName,
  2357. IN OUT LPVOID lpProtocolBuffer,
  2358. IN OUT LPDWORD lpdwBufferLength
  2359. )
  2360. /*++
  2361. Routine Description:
  2362. Enumerates the protocols supported by this helper.
  2363. Arguments:
  2364. lpiProtocols - Pointer to a NULL-terminated array of protocol
  2365. identifiers. Only protocols specified in this array will
  2366. be returned by this function. If this pointer is NULL,
  2367. all protocols are returned.
  2368. lpTransportKeyName -
  2369. lpProtocolBuffer - Pointer to a buffer to fill with PROTOCOL_INFO
  2370. structures.
  2371. lpdwBufferLength - Pointer to a variable that, on input, contains
  2372. the size of lpProtocolBuffer. On output, this value will be
  2373. updated with the size of the data actually written to the buffer.
  2374. Return Value:
  2375. INT - The number of protocols returned if successful, -1 if not.
  2376. --*/
  2377. {
  2378. DWORD bytesRequired;
  2379. PPROTOCOL_INFO tcpProtocolInfo;
  2380. PPROTOCOL_INFO udpProtocolInfo;
  2381. BOOL useTcp = FALSE;
  2382. BOOL useUdp = FALSE;
  2383. DWORD i;
  2384. lpTransportKeyName; // Avoid compiler warnings.
  2385. //
  2386. // Make sure that the caller cares about TCP and/or UDP.
  2387. //
  2388. if ( ARGUMENT_PRESENT( lpiProtocols ) ) {
  2389. for ( i = 0; lpiProtocols[i] != 0; i++ ) {
  2390. if ( lpiProtocols[i] == IPPROTO_TCP ) {
  2391. useTcp = TRUE;
  2392. }
  2393. if ( lpiProtocols[i] == IPPROTO_UDP ) {
  2394. useUdp = TRUE;
  2395. }
  2396. }
  2397. } else {
  2398. useTcp = TRUE;
  2399. useUdp = TRUE;
  2400. }
  2401. if ( !useTcp && !useUdp ) {
  2402. *lpdwBufferLength = 0;
  2403. return 0;
  2404. }
  2405. //
  2406. // Make sure that the caller has specified a sufficiently large
  2407. // buffer.
  2408. //
  2409. bytesRequired = (DWORD)((sizeof(PROTOCOL_INFO) * 2) +
  2410. ( (wcslen( TCP_NAME ) + 1) * sizeof(WCHAR)) +
  2411. ( (wcslen( UDP_NAME ) + 1) * sizeof(WCHAR)));
  2412. if ( bytesRequired > *lpdwBufferLength ) {
  2413. *lpdwBufferLength = bytesRequired;
  2414. return -1;
  2415. }
  2416. //
  2417. // Fill in TCP info, if requested.
  2418. //
  2419. if ( useTcp ) {
  2420. tcpProtocolInfo = lpProtocolBuffer;
  2421. tcpProtocolInfo->dwServiceFlags = XP_GUARANTEED_DELIVERY |
  2422. XP_GUARANTEED_ORDER |
  2423. XP_GRACEFUL_CLOSE |
  2424. XP_EXPEDITED_DATA |
  2425. XP_FRAGMENTATION;
  2426. tcpProtocolInfo->iAddressFamily = AF_INET;
  2427. tcpProtocolInfo->iMaxSockAddr = sizeof(SOCKADDR_IN);
  2428. tcpProtocolInfo->iMinSockAddr = sizeof(SOCKADDR_IN);
  2429. tcpProtocolInfo->iSocketType = SOCK_STREAM;
  2430. tcpProtocolInfo->iProtocol = IPPROTO_TCP;
  2431. tcpProtocolInfo->dwMessageSize = 0;
  2432. tcpProtocolInfo->lpProtocol = (LPWSTR)
  2433. ( (PBYTE)lpProtocolBuffer + *lpdwBufferLength -
  2434. ( (wcslen( TCP_NAME ) + 1) * sizeof(WCHAR) ) );
  2435. wcscpy( tcpProtocolInfo->lpProtocol, TCP_NAME );
  2436. udpProtocolInfo = tcpProtocolInfo + 1;
  2437. udpProtocolInfo->lpProtocol = (LPWSTR)
  2438. ( (PBYTE)tcpProtocolInfo->lpProtocol -
  2439. ( (wcslen( UDP_NAME ) + 1) * sizeof(WCHAR) ) );
  2440. } else {
  2441. udpProtocolInfo = lpProtocolBuffer;
  2442. udpProtocolInfo->lpProtocol = (LPWSTR)
  2443. ( (PBYTE)lpProtocolBuffer + *lpdwBufferLength -
  2444. ( (wcslen( UDP_NAME ) + 1) * sizeof(WCHAR) ) );
  2445. }
  2446. //
  2447. // Fill in UDP info, if requested.
  2448. //
  2449. if ( useUdp ) {
  2450. udpProtocolInfo->dwServiceFlags = XP_CONNECTIONLESS |
  2451. XP_MESSAGE_ORIENTED |
  2452. XP_SUPPORTS_BROADCAST |
  2453. XP_SUPPORTS_MULTICAST |
  2454. XP_FRAGMENTATION;
  2455. udpProtocolInfo->iAddressFamily = AF_INET;
  2456. udpProtocolInfo->iMaxSockAddr = sizeof(SOCKADDR_IN);
  2457. udpProtocolInfo->iMinSockAddr = sizeof(SOCKADDR_IN);
  2458. udpProtocolInfo->iSocketType = SOCK_DGRAM;
  2459. udpProtocolInfo->iProtocol = IPPROTO_UDP;
  2460. udpProtocolInfo->dwMessageSize = UDP_MESSAGE_SIZE;
  2461. wcscpy( udpProtocolInfo->lpProtocol, UDP_NAME );
  2462. }
  2463. *lpdwBufferLength = bytesRequired;
  2464. return (useTcp && useUdp) ? 2 : 1;
  2465. } // WSHEnumProtocols
  2466. BOOLEAN
  2467. IsTripleInList (
  2468. IN PMAPPING_TRIPLE List,
  2469. IN ULONG ListLength,
  2470. IN INT AddressFamily,
  2471. IN INT SocketType,
  2472. IN INT Protocol
  2473. )
  2474. /*++
  2475. Routine Description:
  2476. Determines whether the specified triple has an exact match in the
  2477. list of triples.
  2478. Arguments:
  2479. List - a list of triples (address family/socket type/protocol) to
  2480. search.
  2481. ListLength - the number of triples in the list.
  2482. AddressFamily - the address family to look for in the list.
  2483. SocketType - the socket type to look for in the list.
  2484. Protocol - the protocol to look for in the list.
  2485. Return Value:
  2486. BOOLEAN - TRUE if the triple was found in the list, false if not.
  2487. --*/
  2488. {
  2489. ULONG i;
  2490. //
  2491. // Walk through the list searching for an exact match.
  2492. //
  2493. for ( i = 0; i < ListLength; i++ ) {
  2494. //
  2495. // If all three elements of the triple match, return indicating
  2496. // that the triple did exist in the list.
  2497. //
  2498. if ( AddressFamily == List[i].AddressFamily &&
  2499. SocketType == List[i].SocketType &&
  2500. ( (Protocol == List[i].Protocol) || (SocketType == SOCK_RAW) )
  2501. ) {
  2502. return TRUE;
  2503. }
  2504. }
  2505. //
  2506. // The triple was not found in the list.
  2507. //
  2508. return FALSE;
  2509. } // IsTripleInList
  2510. INT
  2511. GetTdiInformation (
  2512. IN HANDLE TdiConnectionObjectHandle,
  2513. IN ULONG Entity,
  2514. IN ULONG Class,
  2515. IN ULONG Type,
  2516. IN ULONG Id,
  2517. IN PVOID Value,
  2518. IN ULONG InValueLength,
  2519. IN ULONG OutValueLength
  2520. )
  2521. {
  2522. NTSTATUS status;
  2523. TCP_REQUEST_QUERY_INFORMATION_EX tcpRequest;
  2524. IO_STATUS_BLOCK ioStatusBlock;
  2525. ASSERT(InValueLength <= CONTEXT_SIZE);
  2526. //
  2527. // Initialize the TDI information buffers.
  2528. //
  2529. RtlZeroMemory( &tcpRequest, sizeof(tcpRequest) );
  2530. tcpRequest.ID.toi_entity.tei_entity = Entity;
  2531. tcpRequest.ID.toi_entity.tei_instance = TL_INSTANCE;
  2532. tcpRequest.ID.toi_class = Class;
  2533. tcpRequest.ID.toi_type = Type;
  2534. tcpRequest.ID.toi_id = Id;
  2535. RtlCopyMemory(tcpRequest.Context, Value, InValueLength);
  2536. //
  2537. // Make the actual TDI action call. The Streams TDI mapper will
  2538. // translate this into a TPI option management request for us and
  2539. // give it to TCP/IP.
  2540. //
  2541. status = NtDeviceIoControlFile(
  2542. TdiConnectionObjectHandle,
  2543. NULL,
  2544. NULL, // ApcRoutine
  2545. NULL, // ApcContext
  2546. &ioStatusBlock,
  2547. IOCTL_TCP_QUERY_INFORMATION_EX,
  2548. &tcpRequest,
  2549. sizeof(tcpRequest),
  2550. Value,
  2551. OutValueLength
  2552. );
  2553. if (NT_SUCCESS (status)) {
  2554. return NO_ERROR;
  2555. } else {
  2556. return NtStatusToSocketError (status);
  2557. }
  2558. }
  2559. INT
  2560. SetTdiInformation (
  2561. IN HANDLE TdiConnectionObjectHandle,
  2562. IN ULONG Entity,
  2563. IN ULONG Class,
  2564. IN ULONG Type,
  2565. IN ULONG Id,
  2566. IN PVOID Value,
  2567. IN ULONG ValueLength,
  2568. IN BOOLEAN WaitForCompletion
  2569. )
  2570. /*++
  2571. Routine Description:
  2572. Performs a TDI action to the TCP/IP driver. A TDI action translates
  2573. into a streams T_OPTMGMT_REQ.
  2574. Arguments:
  2575. TdiConnectionObjectHandle - a TDI connection object on which to perform
  2576. the TDI action.
  2577. Entity - value to put in the tei_entity field of the TDIObjectID
  2578. structure.
  2579. Class - value to put in the toi_class field of the TDIObjectID
  2580. structure.
  2581. Type - value to put in the toi_type field of the TDIObjectID
  2582. structure.
  2583. Id - value to put in the toi_id field of the TDIObjectID structure.
  2584. Value - a pointer to a buffer to set as the information.
  2585. ValueLength - the length of the buffer.
  2586. WaitForCompletion - TRUE if we should wait for the TDI action to
  2587. complete, FALSE if we're at APC level and cannot do a wait.
  2588. Return Value:
  2589. INT - NO_ERROR, or a Windows Sockets error code.
  2590. --*/
  2591. {
  2592. NTSTATUS status;
  2593. PTCP_REQUEST_SET_INFORMATION_EX setInfoEx;
  2594. PIO_STATUS_BLOCK ioStatusBlock;
  2595. PVOID completionApc;
  2596. PVOID apcContext;
  2597. CHAR localBuffer[sizeof (IO_STATUS_BLOCK) +
  2598. sizeof (TCP_REQUEST_SET_INFORMATION_EX) +
  2599. 32];
  2600. if (WaitForCompletion || ValueLength>32) {
  2601. //
  2602. // Allocate space to hold the TDI set information buffers and the IO
  2603. // status block. These cannot be stack variables in case we must
  2604. // return before the operation is complete.
  2605. //
  2606. ioStatusBlock = RtlAllocateHeap(
  2607. RtlProcessHeap( ),
  2608. 0,
  2609. sizeof(*ioStatusBlock) + sizeof(*setInfoEx) +
  2610. ValueLength
  2611. );
  2612. if ( ioStatusBlock == NULL ) {
  2613. return WSAENOBUFS;
  2614. }
  2615. }
  2616. else {
  2617. ioStatusBlock = (PIO_STATUS_BLOCK)&localBuffer;
  2618. }
  2619. //
  2620. // Initialize the TDI information buffers.
  2621. //
  2622. setInfoEx = (PTCP_REQUEST_SET_INFORMATION_EX)(ioStatusBlock + 1);
  2623. setInfoEx->ID.toi_entity.tei_entity = Entity;
  2624. setInfoEx->ID.toi_entity.tei_instance = TL_INSTANCE;
  2625. setInfoEx->ID.toi_class = Class;
  2626. setInfoEx->ID.toi_type = Type;
  2627. setInfoEx->ID.toi_id = Id;
  2628. RtlCopyMemory( setInfoEx->Buffer, Value, ValueLength );
  2629. setInfoEx->BufferSize = ValueLength;
  2630. //
  2631. // If we need to wait for completion of the operation, create an
  2632. // event to wait on. If we can't wait for completion because we
  2633. // are being called at APC level, we'll use an APC routine to
  2634. // free the heap we allocated above.
  2635. //
  2636. if ( WaitForCompletion ) {
  2637. completionApc = NULL;
  2638. apcContext = NULL;
  2639. } else {
  2640. completionApc = CompleteTdiActionApc;
  2641. apcContext = ioStatusBlock;
  2642. }
  2643. //
  2644. // Make the actual TDI action call. The Streams TDI mapper will
  2645. // translate this into a TPI option management request for us and
  2646. // give it to TCP/IP.
  2647. //
  2648. ioStatusBlock->Status = STATUS_PENDING;
  2649. status = NtDeviceIoControlFile(
  2650. TdiConnectionObjectHandle,
  2651. NULL,
  2652. completionApc,
  2653. apcContext,
  2654. ioStatusBlock,
  2655. IOCTL_TCP_WSH_SET_INFORMATION_EX,
  2656. setInfoEx,
  2657. sizeof(*setInfoEx) + ValueLength,
  2658. NULL,
  2659. 0
  2660. );
  2661. //
  2662. // If the call pended and we were supposed to wait for completion,
  2663. // then wait.
  2664. //
  2665. if ( status == STATUS_PENDING && WaitForCompletion ) {
  2666. #if DBG
  2667. INT count=0;
  2668. #endif
  2669. while (ioStatusBlock->Status==STATUS_PENDING) {
  2670. LARGE_INTEGER timeout;
  2671. //
  2672. // Wait one millisecond
  2673. //
  2674. timeout.QuadPart = -1i64*1000i64*10i64;
  2675. NtDelayExecution (FALSE, &timeout);
  2676. #if DBG
  2677. if (count++>10*1000) {
  2678. DbgPrint ("WSHTCPIP: Waiting for TCP/IP IOCTL completion for more than 10 seconds!!!!\n");
  2679. DbgBreakPoint ();
  2680. }
  2681. #endif
  2682. }
  2683. status = ioStatusBlock->Status;
  2684. }
  2685. if ( (ioStatusBlock != (PIO_STATUS_BLOCK)&localBuffer) &&
  2686. (WaitForCompletion || !NT_SUCCESS(status)) ){
  2687. RtlFreeHeap( RtlProcessHeap( ), 0, ioStatusBlock );
  2688. }
  2689. if (NT_SUCCESS (status)) {
  2690. return NO_ERROR;
  2691. }
  2692. else {
  2693. return NtStatusToSocketError (status);
  2694. }
  2695. } // SetTdiInformation
  2696. VOID
  2697. CompleteTdiActionApc (
  2698. IN PVOID ApcContext,
  2699. IN PIO_STATUS_BLOCK IoStatusBlock
  2700. )
  2701. {
  2702. //
  2703. // Just free the heap we allocated to hold the IO status block and
  2704. // the TDI action buffer. There is nothing we can do if the call
  2705. // failed.
  2706. //
  2707. RtlFreeHeap( RtlProcessHeap( ), 0, ApcContext );
  2708. } // CompleteTdiActionApc
  2709. INT
  2710. WINAPI
  2711. WSHJoinLeaf (
  2712. IN PVOID HelperDllSocketContext,
  2713. IN SOCKET SocketHandle,
  2714. IN HANDLE TdiAddressObjectHandle,
  2715. IN HANDLE TdiConnectionObjectHandle,
  2716. IN PVOID LeafHelperDllSocketContext,
  2717. IN SOCKET LeafSocketHandle,
  2718. IN PSOCKADDR Sockaddr,
  2719. IN DWORD SockaddrLength,
  2720. IN LPWSABUF CallerData,
  2721. IN LPWSABUF CalleeData,
  2722. IN LPQOS SocketQOS,
  2723. IN LPQOS GroupQOS,
  2724. IN DWORD Flags
  2725. )
  2726. /*++
  2727. Routine Description:
  2728. Performs the protocol-dependent portion of creating a multicast
  2729. socket.
  2730. Arguments:
  2731. The following four parameters correspond to the socket passed into
  2732. the WSAJoinLeaf() API:
  2733. HelperDllSocketContext - The context pointer returned from
  2734. WSHOpenSocket().
  2735. SocketHandle - The handle of the socket used to establish the
  2736. multicast "session".
  2737. TdiAddressObjectHandle - The TDI address object of the socket, if
  2738. any. If the socket is not yet bound to an address, then
  2739. it does not have a TDI address object and this parameter
  2740. will be NULL.
  2741. TdiConnectionObjectHandle - The TDI connection object of the socket,
  2742. if any. If the socket is not yet connected, then it does not
  2743. have a TDI connection object and this parameter will be NULL.
  2744. The next two parameters correspond to the newly created socket that
  2745. identifies the multicast "session":
  2746. LeafHelperDllSocketContext - The context pointer returned from
  2747. WSHOpenSocket().
  2748. LeafSocketHandle - The handle of the socket that identifies the
  2749. multicast "session".
  2750. Sockaddr - The name of the peer to which the socket is to be joined.
  2751. SockaddrLength - The length of Sockaddr.
  2752. CallerData - Pointer to user data to be transferred to the peer
  2753. during multipoint session establishment.
  2754. CalleeData - Pointer to user data to be transferred back from
  2755. the peer during multipoint session establishment.
  2756. SocketQOS - Pointer to the flowspecs for SocketHandle, one in each
  2757. direction.
  2758. GroupQOS - Pointer to the flowspecs for the socket group, if any.
  2759. Flags - Flags to indicate if the socket is acting as sender,
  2760. receiver, or both.
  2761. Return Value:
  2762. INT - 0 if successful, a WinSock error code if not.
  2763. --*/
  2764. {
  2765. struct ip_mreq req;
  2766. INT err;
  2767. BOOL bSet_IP_MULTICAST_IF = FALSE;
  2768. PWSHTCPIP_SOCKET_CONTEXT context;
  2769. //
  2770. // Note: at this time we only support non-rooted control schemes,
  2771. // and therefore no leaf socket is created
  2772. //
  2773. //
  2774. // Quick sanity checks.
  2775. //
  2776. if( HelperDllSocketContext == NULL ||
  2777. SocketHandle == INVALID_SOCKET ||
  2778. TdiAddressObjectHandle == NULL ||
  2779. LeafHelperDllSocketContext != NULL ||
  2780. LeafSocketHandle != INVALID_SOCKET ||
  2781. Sockaddr == NULL ||
  2782. Sockaddr->sa_family != AF_INET ||
  2783. SockaddrLength < sizeof(SOCKADDR_IN) ||
  2784. ( CallerData != NULL && CallerData->len > 0 ) ||
  2785. ( CalleeData != NULL && CalleeData->len > 0 ) ||
  2786. SocketQOS != NULL ||
  2787. GroupQOS != NULL ) {
  2788. return WSAEINVAL;
  2789. }
  2790. context = HelperDllSocketContext;
  2791. //
  2792. // The multicast group to join...
  2793. //
  2794. req.imr_multiaddr = ((LPSOCKADDR_IN)Sockaddr)->sin_addr;
  2795. //
  2796. // Now figure out the local interface. Note that the local interface
  2797. // specified in IP_ADD_MEMBERSHIP applies to that on which you wish
  2798. // to receive datagrams, while the local interface specified in
  2799. // IP_MULTICAST_IF applies to that from which to send multicast
  2800. // packets. If there is >1 local interface then we want to be
  2801. // consistent regarding the send/recv interfaces.
  2802. //
  2803. if (context->MulticastInterface == DEFAULT_MULTICAST_INTERFACE) {
  2804. TDI_REQUEST_QUERY_INFORMATION query;
  2805. char tdiAddressInfo[FIELD_OFFSET (TDI_ADDRESS_INFO, Address)
  2806. + sizeof (TA_IP_ADDRESS)];
  2807. NTSTATUS status;
  2808. IO_STATUS_BLOCK ioStatusBlock;
  2809. //
  2810. // App hasn't set IP_MULTICAST_IF, so retrieve the bound
  2811. // address and use that for the send & recv interfaces
  2812. //
  2813. //
  2814. RtlZeroMemory (&query, sizeof (query));
  2815. query.QueryType = TDI_QUERY_ADDRESS_INFO;
  2816. status = NtDeviceIoControlFile(
  2817. TdiAddressObjectHandle,
  2818. NULL,
  2819. NULL,
  2820. NULL,
  2821. &ioStatusBlock,
  2822. IOCTL_TDI_QUERY_INFORMATION,
  2823. &query,
  2824. sizeof (query),
  2825. tdiAddressInfo,
  2826. sizeof(tdiAddressInfo)
  2827. );
  2828. if( NT_SUCCESS(status) ) {
  2829. PTA_IP_ADDRESS pIpAddress =
  2830. (PTA_IP_ADDRESS)(tdiAddressInfo+FIELD_OFFSET (TDI_ADDRESS_INFO, Address));
  2831. req.imr_interface.s_addr = pIpAddress->Address[0].Address[0].in_addr;
  2832. if (req.imr_interface.s_addr != DEFAULT_MULTICAST_INTERFACE) {
  2833. bSet_IP_MULTICAST_IF = TRUE;
  2834. context->MulticastInterface = req.imr_interface.s_addr;
  2835. }
  2836. } else {
  2837. req.imr_interface.s_addr = DEFAULT_MULTICAST_INTERFACE;
  2838. }
  2839. } else {
  2840. req.imr_interface.s_addr = context->MulticastInterface;
  2841. }
  2842. //
  2843. // If the Flags param indicates that caller is a sender only,
  2844. // then there's no point in actually joining the group (anyone
  2845. // can send to a multicast group, but it's only members of the
  2846. // group who recv the packets). So, just check to see if it
  2847. // is necessary to set the IP_MULTICAST_IF to remain consistent
  2848. // ith the bound address.
  2849. //
  2850. // Otherwise, caller is a receiver (possibly a sender too), so
  2851. // we really do want to join the group.
  2852. //
  2853. if (Flags == JL_SENDER_ONLY) {
  2854. if (bSet_IP_MULTICAST_IF) {
  2855. WSHSetSocketInformation (
  2856. HelperDllSocketContext,
  2857. SocketHandle,
  2858. TdiAddressObjectHandle,
  2859. TdiConnectionObjectHandle,
  2860. IPPROTO_IP,
  2861. IP_MULTICAST_IF,
  2862. (char *) &req.imr_interface.s_addr,
  2863. sizeof (req.imr_interface.s_addr)
  2864. );
  2865. }
  2866. err = NO_ERROR;
  2867. } else {
  2868. err = SetTdiInformation(
  2869. TdiAddressObjectHandle,
  2870. CL_TL_ENTITY,
  2871. INFO_CLASS_PROTOCOL,
  2872. INFO_TYPE_ADDRESS_OBJECT,
  2873. AO_OPTION_ADD_MCAST,
  2874. &req,
  2875. sizeof(req),
  2876. TRUE
  2877. );
  2878. if( err == NO_ERROR ) {
  2879. //
  2880. // Record this fact in the leaf socket so we can drop membership
  2881. // when the leaf socket is closed.
  2882. //
  2883. context->MultipointLeaf = TRUE;
  2884. context->MultipointTarget = req.imr_multiaddr;
  2885. //
  2886. // Stay consistent, i.e. send interface should match recv interface
  2887. //
  2888. if (bSet_IP_MULTICAST_IF) {
  2889. WSHSetSocketInformation (
  2890. HelperDllSocketContext,
  2891. SocketHandle,
  2892. TdiAddressObjectHandle,
  2893. TdiConnectionObjectHandle,
  2894. IPPROTO_IP,
  2895. IP_MULTICAST_IF,
  2896. (char *) &req.imr_interface.s_addr,
  2897. sizeof (req.imr_interface.s_addr)
  2898. );
  2899. }
  2900. }
  2901. }
  2902. return err;
  2903. } // WSHJoinLeaf
  2904. INT
  2905. WINAPI
  2906. WSHGetBroadcastSockaddr (
  2907. IN PVOID HelperDllSocketContext,
  2908. OUT PSOCKADDR Sockaddr,
  2909. OUT PINT SockaddrLength
  2910. )
  2911. /*++
  2912. Routine Description:
  2913. This routine returns a broadcast socket address. A broadcast address
  2914. may be used as a destination for the sendto() API to send a datagram
  2915. to all interested clients.
  2916. Arguments:
  2917. HelperDllSocketContext - the context pointer returned from
  2918. WSHOpenSocket() for the socket for which we need a broadcast
  2919. address.
  2920. Sockaddr - points to a buffer which will receive the broadcast socket
  2921. address.
  2922. SockaddrLength - receives the length of the broadcast sockaddr.
  2923. Return Value:
  2924. INT - a winsock error code indicating the status of the operation, or
  2925. NO_ERROR if the operation succeeded.
  2926. --*/
  2927. {
  2928. LPSOCKADDR_IN addr;
  2929. if( *SockaddrLength < sizeof(SOCKADDR_IN) ) {
  2930. return WSAEFAULT;
  2931. }
  2932. *SockaddrLength = sizeof(SOCKADDR_IN);
  2933. //
  2934. // Build the broadcast address.
  2935. //
  2936. addr = (LPSOCKADDR_IN)Sockaddr;
  2937. RtlZeroMemory(
  2938. addr,
  2939. sizeof(*addr)
  2940. );
  2941. addr->sin_family = AF_INET;
  2942. ASSERT (htonl(INADDR_BROADCAST)==INADDR_BROADCAST);
  2943. addr->sin_addr.s_addr = INADDR_BROADCAST;
  2944. return NO_ERROR;
  2945. } // WSAGetBroadcastSockaddr
  2946. INT
  2947. WINAPI
  2948. WSHGetWSAProtocolInfo (
  2949. IN LPWSTR ProviderName,
  2950. OUT LPWSAPROTOCOL_INFOW * ProtocolInfo,
  2951. OUT LPDWORD ProtocolInfoEntries
  2952. )
  2953. /*++
  2954. Routine Description:
  2955. Retrieves a pointer to the WSAPROTOCOL_INFOW structure(s) describing
  2956. the protocol(s) supported by this helper.
  2957. Arguments:
  2958. ProviderName - Contains the name of the provider, such as "TcpIp".
  2959. ProtocolInfo - Receives a pointer to the WSAPROTOCOL_INFOW array.
  2960. ProtocolInfoEntries - Receives the number of entries in the array.
  2961. Return Value:
  2962. INT - 0 if successful, WinSock error code if not.
  2963. --*/
  2964. {
  2965. if( ProviderName == NULL ||
  2966. ProtocolInfo == NULL ||
  2967. ProtocolInfoEntries == NULL ) {
  2968. return WSAEFAULT;
  2969. }
  2970. if( _wcsicmp( ProviderName, L"TcpIp" ) == 0 ) {
  2971. *ProtocolInfo = Winsock2Protocols;
  2972. *ProtocolInfoEntries = NUM_WINSOCK2_PROTOCOLS;
  2973. return NO_ERROR;
  2974. }
  2975. return WSAEINVAL;
  2976. } // WSHGetWSAProtocolInfo
  2977. INT
  2978. WINAPI
  2979. WSHAddressToString (
  2980. IN LPSOCKADDR Address,
  2981. IN INT AddressLength,
  2982. IN LPWSAPROTOCOL_INFOW ProtocolInfo,
  2983. OUT LPWSTR AddressString,
  2984. IN OUT LPDWORD AddressStringLength
  2985. )
  2986. /*++
  2987. Routine Description:
  2988. Converts a SOCKADDR to a human-readable form.
  2989. Arguments:
  2990. Address - The SOCKADDR to convert.
  2991. AddressLength - The length of Address.
  2992. ProtocolInfo - The WSAPROTOCOL_INFOW for a particular provider.
  2993. AddressString - Receives the formatted address string.
  2994. AddressStringLength - On input, contains the length of AddressString.
  2995. On output, contains the number of characters actually written
  2996. to AddressString.
  2997. Return Value:
  2998. INT - 0 if successful, WinSock error code if not.
  2999. --*/
  3000. {
  3001. WCHAR string[32];
  3002. INT length;
  3003. LPSOCKADDR_IN addr;
  3004. INT err = NO_ERROR;
  3005. //
  3006. // Quick sanity checks.
  3007. //
  3008. if( AddressLength < sizeof(SOCKADDR_IN)) {
  3009. return WSAEFAULT;
  3010. }
  3011. __try {
  3012. addr = (LPSOCKADDR_IN)Address;
  3013. if( addr->sin_family != AF_INET ) {
  3014. return WSAEINVAL;
  3015. }
  3016. //
  3017. // Do the converstion.
  3018. //
  3019. //
  3020. length = swprintf(
  3021. string,
  3022. L"%d.%d.%d.%d",
  3023. ( addr->sin_addr.s_addr >> 0 ) & 0xFF,
  3024. ( addr->sin_addr.s_addr >> 8 ) & 0xFF,
  3025. ( addr->sin_addr.s_addr >> 16 ) & 0xFF,
  3026. ( addr->sin_addr.s_addr >> 24 ) & 0xFF
  3027. );
  3028. if( addr->sin_port != 0 ) {
  3029. length += swprintf(
  3030. string + length,
  3031. L":%u",
  3032. ntohs( addr->sin_port )
  3033. );
  3034. }
  3035. length++; // account for terminator
  3036. if( *AddressStringLength >= (DWORD)length ) {
  3037. RtlCopyMemory(
  3038. AddressString,
  3039. string,
  3040. length * sizeof(WCHAR)
  3041. );
  3042. }
  3043. else {
  3044. err = WSAEFAULT;
  3045. }
  3046. *AddressStringLength = length;
  3047. }
  3048. __except (EXCEPTION_EXECUTE_HANDLER) {
  3049. err = WSAEFAULT;
  3050. }
  3051. return err;
  3052. } // WSHAddressToString
  3053. INT
  3054. WINAPI
  3055. WSHStringToAddress (
  3056. IN LPWSTR AddressString,
  3057. IN DWORD AddressFamily,
  3058. IN LPWSAPROTOCOL_INFOW ProtocolInfo,
  3059. OUT LPSOCKADDR Address,
  3060. IN OUT LPINT AddressLength
  3061. )
  3062. /*++
  3063. Routine Description:
  3064. Fills in a SOCKADDR structure by parsing a human-readable string.
  3065. Arguments:
  3066. AddressString - Points to the zero-terminated human-readable string.
  3067. AddressFamily - The address family to which the string belongs.
  3068. ProtocolInfo - The WSAPROTOCOL_INFOW for a particular provider.
  3069. Address - Receives the SOCKADDR structure.
  3070. AddressLength - On input, contains the length of Address. On output,
  3071. contains the number of bytes actually written to Address.
  3072. Return Value:
  3073. INT - 0 if successful, WinSock error code if not.
  3074. --*/
  3075. {
  3076. LPWSTR terminator;
  3077. ULONG ipAddress;
  3078. USHORT port;
  3079. LPSOCKADDR_IN addr;
  3080. __try {
  3081. //
  3082. // Quick sanity checks.
  3083. //
  3084. if (*AddressLength < sizeof(SOCKADDR_IN) ) {
  3085. *AddressLength = sizeof(SOCKADDR_IN);
  3086. return WSAEFAULT;
  3087. }
  3088. if( AddressFamily != AF_INET ) {
  3089. return WSAEINVAL;
  3090. }
  3091. //
  3092. // Convert it.
  3093. //
  3094. if (!NT_SUCCESS(RtlIpv4StringToAddressW(AddressString, FALSE, &terminator,
  3095. (IN_ADDR*)&ipAddress))) {
  3096. return WSAEINVAL;
  3097. }
  3098. if( ipAddress == INADDR_NONE ) {
  3099. return WSAEINVAL;
  3100. }
  3101. if( *terminator == L':' ) {
  3102. WCHAR ch;
  3103. USHORT base;
  3104. terminator++;
  3105. port = 0;
  3106. base = 10;
  3107. if( *terminator == L'0' ) {
  3108. base = 8;
  3109. terminator++;
  3110. if( *terminator == L'x' ) {
  3111. base = 16;
  3112. terminator++;
  3113. }
  3114. }
  3115. while( ch = *terminator++ ) {
  3116. if( iswdigit(ch) && (USHORT)(ch-L'0')<base) {
  3117. port = ( port * base ) + ( ch - L'0' );
  3118. } else if( base == 16 && iswxdigit(ch) ) {
  3119. port = ( port << 4 );
  3120. port += ch + 10 - ( iswlower(ch) ? L'a' : L'A' );
  3121. } else {
  3122. return WSAEINVAL;
  3123. }
  3124. }
  3125. } else if( *terminator == 0 ) {
  3126. port = 0;
  3127. } else {
  3128. return WSAEINVAL;
  3129. }
  3130. //
  3131. // Build the address.
  3132. //
  3133. RtlZeroMemory(
  3134. Address,
  3135. sizeof(SOCKADDR_IN)
  3136. );
  3137. addr = (LPSOCKADDR_IN)Address;
  3138. *AddressLength = sizeof(SOCKADDR_IN);
  3139. addr->sin_family = AF_INET;
  3140. addr->sin_port = htons ( port );
  3141. addr->sin_addr.s_addr = ipAddress;
  3142. }
  3143. __except (EXCEPTION_EXECUTE_HANDLER) {
  3144. return WSAEFAULT;
  3145. }
  3146. return NO_ERROR;
  3147. } // WSHStringToAddress
  3148. INT
  3149. WINAPI
  3150. WSHGetProviderGuid (
  3151. IN LPWSTR ProviderName,
  3152. OUT LPGUID ProviderGuid
  3153. )
  3154. /*++
  3155. Routine Description:
  3156. Returns the GUID identifying the protocols supported by this helper.
  3157. Arguments:
  3158. ProviderName - Contains the name of the provider, such as "TcpIp".
  3159. ProviderGuid - Points to a buffer that receives the provider's GUID.
  3160. Return Value:
  3161. INT - 0 if successful, WinSock error code if not.
  3162. --*/
  3163. {
  3164. if( ProviderName == NULL ||
  3165. ProviderGuid == NULL ) {
  3166. return WSAEFAULT;
  3167. }
  3168. if( _wcsicmp( ProviderName, L"TcpIp" ) == 0 ) {
  3169. RtlCopyMemory(
  3170. ProviderGuid,
  3171. &TcpipProviderGuid,
  3172. sizeof(GUID)
  3173. );
  3174. return NO_ERROR;
  3175. }
  3176. return WSAEINVAL;
  3177. } // WSHGetProviderGuid
  3178. INT
  3179. WINAPI
  3180. WSHIoctl (
  3181. IN PVOID HelperDllSocketContext,
  3182. IN SOCKET SocketHandle,
  3183. IN HANDLE TdiAddressObjectHandle,
  3184. IN HANDLE TdiConnectionObjectHandle,
  3185. IN DWORD IoControlCode,
  3186. IN LPVOID InputBuffer,
  3187. IN DWORD InputBufferLength,
  3188. IN LPVOID OutputBuffer,
  3189. IN DWORD OutputBufferLength,
  3190. OUT LPDWORD NumberOfBytesReturned,
  3191. IN LPWSAOVERLAPPED Overlapped,
  3192. IN LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine,
  3193. OUT LPBOOL NeedsCompletion
  3194. )
  3195. /*++
  3196. Routine Description:
  3197. Performs queries & controls on the socket. This is basically an
  3198. "escape hatch" for IOCTLs not supported by MSAFD.DLL. Any unknown
  3199. IOCTLs are routed to the socket's helper DLL for protocol-specific
  3200. processing.
  3201. Arguments:
  3202. HelperDllSocketContext - the context pointer returned from
  3203. WSHOpenSocket().
  3204. SocketHandle - the handle of the socket for which we're controlling.
  3205. TdiAddressObjectHandle - the TDI address object of the socket, if
  3206. any. If the socket is not yet bound to an address, then
  3207. it does not have a TDI address object and this parameter
  3208. will be NULL.
  3209. TdiConnectionObjectHandle - the TDI connection object of the socket,
  3210. if any. If the socket is not yet connected, then it does not
  3211. have a TDI connection object and this parameter will be NULL.
  3212. IoControlCode - Control code of the operation to perform.
  3213. InputBuffer - Address of the input buffer.
  3214. InputBufferLength - The length of InputBuffer.
  3215. OutputBuffer - Address of the output buffer.
  3216. OutputBufferLength - The length of OutputBuffer.
  3217. NumberOfBytesReturned - Receives the number of bytes actually written
  3218. to the output buffer.
  3219. Overlapped - Pointer to a WSAOVERLAPPED structure for overlapped
  3220. operations.
  3221. CompletionRoutine - Pointer to a completion routine to call when
  3222. the operation is completed.
  3223. NeedsCompletion - WSAIoctl() can be overlapped, with all the gory
  3224. details that involves, such as setting events, queuing completion
  3225. routines, and posting to IO completion ports. Since the majority
  3226. of the IOCTL codes can be completed quickly "in-line", MSAFD.DLL
  3227. can optionally perform the overlapped completion of the operation.
  3228. Setting *NeedsCompletion to TRUE (the default) causes MSAFD.DLL
  3229. to handle all of the IO completion details iff this is an
  3230. overlapped operation on an overlapped socket.
  3231. Setting *NeedsCompletion to FALSE tells MSAFD.DLL to take no
  3232. further action because the helper DLL will perform any necessary
  3233. IO completion.
  3234. Note that if a helper performs its own IO completion, the helper
  3235. is responsible for maintaining the "overlapped" mode of the socket
  3236. at socket creation time and NOT performing overlapped IO completion
  3237. on non-overlapped sockets.
  3238. Return Value:
  3239. INT - 0 if successful, WinSock error code if not.
  3240. --*/
  3241. {
  3242. INT err;
  3243. NTSTATUS status;
  3244. //
  3245. // Quick sanity checks.
  3246. //
  3247. if( HelperDllSocketContext == NULL ||
  3248. SocketHandle == INVALID_SOCKET ||
  3249. NumberOfBytesReturned == NULL ||
  3250. NeedsCompletion == NULL ) {
  3251. return WSAEINVAL;
  3252. }
  3253. *NeedsCompletion = TRUE;
  3254. switch( IoControlCode ) {
  3255. case SIO_MULTIPOINT_LOOPBACK :
  3256. err = WSHSetSocketInformation(
  3257. HelperDllSocketContext,
  3258. SocketHandle,
  3259. TdiAddressObjectHandle,
  3260. TdiConnectionObjectHandle,
  3261. IPPROTO_IP,
  3262. IP_MULTICAST_LOOP,
  3263. (PCHAR)InputBuffer,
  3264. (INT)InputBufferLength
  3265. );
  3266. break;
  3267. case SIO_MULTICAST_SCOPE :
  3268. err = WSHSetSocketInformation(
  3269. HelperDllSocketContext,
  3270. SocketHandle,
  3271. TdiAddressObjectHandle,
  3272. TdiConnectionObjectHandle,
  3273. IPPROTO_IP,
  3274. IP_MULTICAST_TTL,
  3275. (PCHAR)InputBuffer,
  3276. (INT)InputBufferLength
  3277. );
  3278. break;
  3279. case SIO_GET_MULTICAST_FILTER :
  3280. if ( OutputBufferLength < IP_MSFILTER_SIZE(0) ) {
  3281. err = WSAEFAULT;
  3282. break;
  3283. }
  3284. if ( TdiAddressObjectHandle == NULL ) {
  3285. err = WSAEFAULT;
  3286. break;
  3287. }
  3288. err = GetTdiInformation(
  3289. TdiAddressObjectHandle,
  3290. CL_TL_ENTITY,
  3291. INFO_CLASS_PROTOCOL,
  3292. INFO_TYPE_ADDRESS_OBJECT,
  3293. AO_OPTION_MCAST_FILTER,
  3294. OutputBuffer,
  3295. FIELD_OFFSET(UDPMCastFilter, umf_fmode),
  3296. OutputBufferLength
  3297. );
  3298. break;
  3299. case SIO_SET_MULTICAST_FILTER :
  3300. {
  3301. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  3302. //
  3303. // This option is only valid for datagram sockets.
  3304. //
  3305. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  3306. err = WSAENOPROTOOPT;
  3307. break;
  3308. }
  3309. //
  3310. // Make sure that the option buffer is large enough.
  3311. //
  3312. if ( InputBufferLength < IP_MSFILTER_SIZE(0) ) {
  3313. err = WSAEFAULT;
  3314. break;
  3315. }
  3316. //
  3317. // If we have a TDI address object, set this option to
  3318. // the address object. If we don't have a TDI address
  3319. // object then we'll have to wait until after the socket
  3320. // is bound.
  3321. //
  3322. if ( TdiAddressObjectHandle != NULL ) {
  3323. err = SetTdiInformation(
  3324. TdiAddressObjectHandle,
  3325. CL_TL_ENTITY,
  3326. INFO_CLASS_PROTOCOL,
  3327. INFO_TYPE_ADDRESS_OBJECT,
  3328. AO_OPTION_MCAST_FILTER,
  3329. InputBuffer,
  3330. InputBufferLength,
  3331. TRUE
  3332. );
  3333. if ( err != NO_ERROR ) {
  3334. break;
  3335. }
  3336. }
  3337. err = NO_ERROR;
  3338. break;
  3339. }
  3340. case SIO_GET_INTERFACE_LIST :
  3341. status = GetTcpipInterfaceList(
  3342. OutputBuffer,
  3343. OutputBufferLength,
  3344. NumberOfBytesReturned
  3345. );
  3346. if( NT_SUCCESS(status) ) {
  3347. err = NO_ERROR;
  3348. } else {
  3349. err = NtStatusToSocketError (status);
  3350. }
  3351. break;
  3352. case SIO_RCVALL:
  3353. //
  3354. // This option is only valid for raw sockets.
  3355. //
  3356. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3357. return WSAENOPROTOOPT;
  3358. }
  3359. if ( InputBufferLength != sizeof(uint)) {
  3360. return WSAEINVAL;
  3361. }
  3362. if ( TdiAddressObjectHandle != NULL ) {
  3363. err = SetTdiInformation(
  3364. TdiAddressObjectHandle,
  3365. CL_TL_ENTITY,
  3366. INFO_CLASS_PROTOCOL,
  3367. INFO_TYPE_ADDRESS_OBJECT,
  3368. AO_OPTION_RCVALL,
  3369. InputBuffer,
  3370. InputBufferLength,
  3371. TRUE
  3372. );
  3373. if ( err != NO_ERROR ) {
  3374. return err;
  3375. }
  3376. } else {
  3377. return WSAEINVAL;
  3378. }
  3379. return NO_ERROR;
  3380. case SIO_RCVALL_MCAST:
  3381. //
  3382. // This option is only valid for raw sockets.
  3383. //
  3384. if (((PWSHTCPIP_SOCKET_CONTEXT) HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3385. return WSAENOPROTOOPT;
  3386. }
  3387. if ( InputBufferLength != sizeof(uint)) {
  3388. return WSAEINVAL;
  3389. }
  3390. if ( TdiAddressObjectHandle != NULL ) {
  3391. err = SetTdiInformation(
  3392. TdiAddressObjectHandle,
  3393. CL_TL_ENTITY,
  3394. INFO_CLASS_PROTOCOL,
  3395. INFO_TYPE_ADDRESS_OBJECT,
  3396. AO_OPTION_RCVALL_MCAST,
  3397. InputBuffer,
  3398. InputBufferLength,
  3399. TRUE
  3400. );
  3401. if ( err != NO_ERROR ) {
  3402. return err;
  3403. }
  3404. } else {
  3405. return WSAEINVAL;
  3406. }
  3407. return NO_ERROR;
  3408. case SIO_RCVALL_IGMPMCAST:
  3409. //
  3410. // This option is only valid for raw sockets.
  3411. //
  3412. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3413. return WSAENOPROTOOPT;
  3414. }
  3415. if ( InputBufferLength != sizeof(uint)) {
  3416. return WSAEINVAL;
  3417. }
  3418. if ( TdiAddressObjectHandle != NULL ) {
  3419. err = SetTdiInformation(
  3420. TdiAddressObjectHandle,
  3421. CL_TL_ENTITY,
  3422. INFO_CLASS_PROTOCOL,
  3423. INFO_TYPE_ADDRESS_OBJECT,
  3424. AO_OPTION_RCVALL_IGMPMCAST,
  3425. InputBuffer,
  3426. InputBufferLength,
  3427. TRUE
  3428. );
  3429. if ( err != NO_ERROR ) {
  3430. return err;
  3431. }
  3432. } else {
  3433. return WSAEINVAL;
  3434. }
  3435. return NO_ERROR;
  3436. case SIO_ABSORB_RTRALERT:
  3437. //
  3438. // This option is only valid for raw sockets.
  3439. //
  3440. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3441. return WSAENOPROTOOPT;
  3442. }
  3443. if ( InputBufferLength != sizeof(uint)) {
  3444. return WSAEINVAL;
  3445. }
  3446. if ( TdiAddressObjectHandle != NULL ) {
  3447. err = SetTdiInformation(
  3448. TdiAddressObjectHandle,
  3449. CL_TL_ENTITY,
  3450. INFO_CLASS_PROTOCOL,
  3451. INFO_TYPE_ADDRESS_OBJECT,
  3452. AO_OPTION_ABSORB_RTRALERT,
  3453. InputBuffer,
  3454. InputBufferLength,
  3455. TRUE
  3456. );
  3457. if ( err != NO_ERROR ) {
  3458. return err;
  3459. }
  3460. } else {
  3461. return WSAEINVAL;
  3462. }
  3463. return NO_ERROR;
  3464. case SIO_UCAST_IF:
  3465. {
  3466. uint OptionValue;
  3467. //
  3468. // This option is only valid for raw sockets.
  3469. //
  3470. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3471. return WSAENOPROTOOPT;
  3472. }
  3473. // this option is valid only if hdr include option is set
  3474. if ( !(((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->HdrIncludeSet && ((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->HdrInclude) ) {
  3475. return WSAEINVAL;
  3476. }
  3477. if ( InputBufferLength != sizeof(uint)) {
  3478. return WSAEINVAL;
  3479. }
  3480. if ( TdiAddressObjectHandle != NULL ) {
  3481. err = SetTdiInformation(
  3482. TdiAddressObjectHandle,
  3483. CL_TL_ENTITY,
  3484. INFO_CLASS_PROTOCOL,
  3485. INFO_TYPE_ADDRESS_OBJECT,
  3486. AO_OPTION_IP_UCASTIF,
  3487. InputBuffer,
  3488. InputBufferLength,
  3489. TRUE
  3490. );
  3491. if ( err != NO_ERROR ) {
  3492. return err;
  3493. }
  3494. }
  3495. OptionValue = *((uint UNALIGNED *)InputBuffer);
  3496. ((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->UcastIf = OptionValue ? TRUE : FALSE;
  3497. return NO_ERROR;
  3498. }
  3499. case SIO_LIMIT_BROADCASTS:
  3500. {
  3501. uint OptionValue;
  3502. //
  3503. // This option is only valid for UDP sockets.
  3504. //
  3505. if ( !IS_DGRAM_SOCK(
  3506. ((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType
  3507. )) {
  3508. return WSAENOPROTOOPT;
  3509. }
  3510. if ( InputBufferLength != sizeof(uint)) {
  3511. return WSAEINVAL;
  3512. }
  3513. if ( TdiAddressObjectHandle != NULL ) {
  3514. err = SetTdiInformation(
  3515. TdiAddressObjectHandle,
  3516. CL_TL_ENTITY,
  3517. INFO_CLASS_PROTOCOL,
  3518. INFO_TYPE_ADDRESS_OBJECT,
  3519. AO_OPTION_LIMIT_BCASTS,
  3520. InputBuffer,
  3521. InputBufferLength,
  3522. TRUE
  3523. );
  3524. if ( err != NO_ERROR ) {
  3525. return err;
  3526. }
  3527. }
  3528. OptionValue = *((uint UNALIGNED *)InputBuffer);
  3529. ((PWSHTCPIP_SOCKET_CONTEXT)
  3530. HelperDllSocketContext)->LimitBroadcasts =
  3531. OptionValue ? TRUE : FALSE;
  3532. return NO_ERROR;
  3533. }
  3534. case SIO_INDEX_BIND:
  3535. //
  3536. // This option is only valid for raw sockets.
  3537. //
  3538. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3539. return WSAENOPROTOOPT;
  3540. }
  3541. if ( TdiAddressObjectHandle != NULL ) {
  3542. err = SetTdiInformation(
  3543. TdiAddressObjectHandle,
  3544. CL_TL_ENTITY,
  3545. INFO_CLASS_PROTOCOL,
  3546. INFO_TYPE_ADDRESS_OBJECT,
  3547. AO_OPTION_INDEX_BIND,
  3548. InputBuffer,
  3549. InputBufferLength,
  3550. TRUE
  3551. );
  3552. if ( err != NO_ERROR ) {
  3553. return err;
  3554. }
  3555. } else {
  3556. return WSAEINVAL;
  3557. }
  3558. return NO_ERROR;
  3559. case SIO_INDEX_MCASTIF:
  3560. //
  3561. // This option is only valid for raw sockets.
  3562. //
  3563. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3564. return WSAENOPROTOOPT;
  3565. }
  3566. if ( TdiAddressObjectHandle != NULL ) {
  3567. err = SetTdiInformation(
  3568. TdiAddressObjectHandle,
  3569. CL_TL_ENTITY,
  3570. INFO_CLASS_PROTOCOL,
  3571. INFO_TYPE_ADDRESS_OBJECT,
  3572. AO_OPTION_INDEX_MCASTIF,
  3573. InputBuffer,
  3574. InputBufferLength,
  3575. TRUE
  3576. );
  3577. if ( err != NO_ERROR ) {
  3578. return err;
  3579. }
  3580. } else {
  3581. return WSAEINVAL;
  3582. }
  3583. return NO_ERROR;
  3584. case SIO_INDEX_ADD_MCAST:
  3585. //
  3586. // This option is only valid for raw sockets.
  3587. //
  3588. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3589. return WSAENOPROTOOPT;
  3590. }
  3591. if ( TdiAddressObjectHandle != NULL ) {
  3592. err = SetTdiInformation(
  3593. TdiAddressObjectHandle,
  3594. CL_TL_ENTITY,
  3595. INFO_CLASS_PROTOCOL,
  3596. INFO_TYPE_ADDRESS_OBJECT,
  3597. AO_OPTION_INDEX_ADD_MCAST,
  3598. InputBuffer,
  3599. InputBufferLength,
  3600. TRUE
  3601. );
  3602. if ( err != NO_ERROR ) {
  3603. return err;
  3604. }
  3605. } else {
  3606. return WSAEINVAL;
  3607. }
  3608. return NO_ERROR;
  3609. case SIO_INDEX_DEL_MCAST:
  3610. //
  3611. // This option is only valid for raw sockets.
  3612. //
  3613. if (((PWSHTCPIP_SOCKET_CONTEXT)HelperDllSocketContext)->SocketType != SOCK_RAW) {
  3614. return WSAENOPROTOOPT;
  3615. }
  3616. if ( TdiAddressObjectHandle != NULL ) {
  3617. err = SetTdiInformation(
  3618. TdiAddressObjectHandle,
  3619. CL_TL_ENTITY,
  3620. INFO_CLASS_PROTOCOL,
  3621. INFO_TYPE_ADDRESS_OBJECT,
  3622. AO_OPTION_INDEX_DEL_MCAST,
  3623. InputBuffer,
  3624. InputBufferLength,
  3625. TRUE
  3626. );
  3627. if ( err != NO_ERROR ) {
  3628. return err;
  3629. }
  3630. } else {
  3631. return WSAEINVAL;
  3632. }
  3633. return NO_ERROR;
  3634. case SIO_KEEPALIVE_VALS:
  3635. {
  3636. struct tcp_keepalive *optionval;
  3637. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  3638. //
  3639. // Atempt to turn on or off keepalive sending, as necessary.
  3640. //
  3641. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  3642. return WSAENOPROTOOPT;
  3643. }
  3644. if ( InputBufferLength != sizeof(struct tcp_keepalive) ) {
  3645. return WSAEINVAL;
  3646. }
  3647. optionval = (struct tcp_keepalive *)InputBuffer;
  3648. if (optionval->onoff != 0 ) {
  3649. // Application wants to turn the keepalive on and also give the
  3650. // relevant parameters for it. If the TDI connection object handle is
  3651. // NULL, then the socket is not yet connected. In this case
  3652. // we'll just remember that the keepalive option was set and
  3653. // actually turn them on in WSHNotify() after a connect()
  3654. // has completed on the socket.
  3655. //
  3656. if ( TdiConnectionObjectHandle != NULL ) {
  3657. err = SetTdiInformation(
  3658. TdiConnectionObjectHandle,
  3659. CO_TL_ENTITY,
  3660. INFO_CLASS_PROTOCOL,
  3661. INFO_TYPE_CONNECTION,
  3662. TCP_SOCKET_KEEPALIVE_VALS,
  3663. optionval,
  3664. InputBufferLength,
  3665. TRUE
  3666. );
  3667. if ( err != NO_ERROR ) {
  3668. return err;
  3669. }
  3670. }
  3671. //
  3672. // Remember that keepalives are enabled for this socket.
  3673. //
  3674. context->KeepAliveVals.onoff = TRUE;
  3675. context->KeepAliveVals.keepalivetime = optionval->keepalivetime;
  3676. context->KeepAliveVals.keepaliveinterval = optionval->keepaliveinterval;
  3677. } else if ( optionval->onoff == 0 ) {
  3678. //
  3679. // Application wants to turn keepalive off. If the TDI
  3680. // connection object is NULL, the socket is not yet
  3681. // connected. In this case we'll just remember that
  3682. // keepalives are disabled.
  3683. if ( TdiConnectionObjectHandle != NULL ) {
  3684. err = SetTdiInformation(
  3685. TdiConnectionObjectHandle,
  3686. CO_TL_ENTITY,
  3687. INFO_CLASS_PROTOCOL,
  3688. INFO_TYPE_CONNECTION,
  3689. TCP_SOCKET_KEEPALIVE_VALS,
  3690. optionval,
  3691. InputBufferLength,
  3692. TRUE
  3693. );
  3694. if ( err != NO_ERROR ) {
  3695. return err;
  3696. }
  3697. }
  3698. //
  3699. // Remember that keepalives are disabled for this socket.
  3700. //
  3701. context->KeepAliveVals.onoff = FALSE;
  3702. }
  3703. // break;
  3704. return NO_ERROR;
  3705. }
  3706. default :
  3707. err = WSAEINVAL;
  3708. break;
  3709. }
  3710. return err;
  3711. } // WSHIoctl
  3712. NTSTATUS
  3713. GetTcpipInterfaceList(
  3714. IN LPVOID OutputBuffer,
  3715. IN DWORD OutputBufferLength,
  3716. OUT LPDWORD NumberOfBytesReturned
  3717. )
  3718. /*++
  3719. Routine Description:
  3720. This routine queries the INTERFACE_INFO array for all supported
  3721. IP interfaces in the system. This is a helper routine for handling
  3722. the SIO_GET_INTERFACE_LIST IOCTL.
  3723. Arguments:
  3724. OutputBuffer - Points to a buffer that will receive the INTERFACE_INFO
  3725. array.
  3726. OutputBufferLength - The length of OutputBuffer.
  3727. NumberOfBytesReturned - Receives the number of bytes actually written
  3728. to OutputBuffer.
  3729. Return Value:
  3730. NTSTATUS - The completion status.
  3731. --*/
  3732. {
  3733. OBJECT_ATTRIBUTES objectAttributes;
  3734. UNICODE_STRING deviceName;
  3735. NTSTATUS status;
  3736. IO_STATUS_BLOCK ioStatusBlock;
  3737. HANDLE deviceHandle;
  3738. TCP_REQUEST_QUERY_INFORMATION_EX tcpRequest;
  3739. TDIObjectID objectId;
  3740. IPSNMPInfo snmpInfo;
  3741. IPInterfaceInfo * interfaceInfo;
  3742. IFEntry * ifentry;
  3743. IPAddrEntry * addressBuffer;
  3744. IPAddrEntry * addressScan;
  3745. TDIEntityID * entityBuffer;
  3746. TDIEntityID * entityScan;
  3747. ULONG i, j;
  3748. ULONG entityCount;
  3749. ULONG entityBufferLength;
  3750. ULONG entityType;
  3751. ULONG addressBufferLength;
  3752. LPINTERFACE_INFO outputInterfaceInfo;
  3753. DWORD outputBytesRemaining;
  3754. LPSOCKADDR_IN sockaddr;
  3755. CHAR fastAddressBuffer[MAX_FAST_ADDRESS_BUFFER];
  3756. CHAR fastEntityBuffer[MAX_FAST_ENTITY_BUFFER];
  3757. CHAR ifentryBuffer[sizeof(IFEntry) + MAX_IFDESCR_LEN];
  3758. CHAR interfaceInfoBuffer[sizeof(IPInterfaceInfo) + MAX_PHYSADDR_SIZE];
  3759. //
  3760. // Setup locals so we know how to cleanup on exit.
  3761. //
  3762. deviceHandle = NULL;
  3763. addressBuffer = NULL;
  3764. entityBuffer = (PVOID)fastEntityBuffer;
  3765. entityBufferLength = sizeof(fastEntityBuffer);
  3766. interfaceInfo = NULL;
  3767. outputInterfaceInfo = OutputBuffer;
  3768. outputBytesRemaining = OutputBufferLength;
  3769. //
  3770. // Open a handle to the TCP/IP device.
  3771. //
  3772. RtlInitUnicodeString(
  3773. &deviceName,
  3774. DD_TCP_DEVICE_NAME
  3775. );
  3776. InitializeObjectAttributes(
  3777. &objectAttributes,
  3778. &deviceName,
  3779. OBJ_CASE_INSENSITIVE,
  3780. NULL,
  3781. NULL
  3782. );
  3783. status = NtCreateFile(
  3784. &deviceHandle,
  3785. SYNCHRONIZE | GENERIC_EXECUTE,
  3786. &objectAttributes,
  3787. &ioStatusBlock,
  3788. NULL,
  3789. FILE_ATTRIBUTE_NORMAL,
  3790. FILE_SHARE_READ | FILE_SHARE_WRITE,
  3791. FILE_OPEN_IF,
  3792. FILE_SYNCHRONOUS_IO_NONALERT,
  3793. NULL,
  3794. 0
  3795. );
  3796. if( !NT_SUCCESS(status) ) {
  3797. goto exit;
  3798. }
  3799. //
  3800. // Get the entities supported by the TCP device.
  3801. //
  3802. RtlZeroMemory(
  3803. &tcpRequest,
  3804. sizeof(tcpRequest)
  3805. );
  3806. tcpRequest.ID.toi_entity.tei_entity = GENERIC_ENTITY;
  3807. tcpRequest.ID.toi_entity.tei_instance = 0;
  3808. tcpRequest.ID.toi_class = INFO_CLASS_GENERIC;
  3809. tcpRequest.ID.toi_type = INFO_TYPE_PROVIDER;
  3810. tcpRequest.ID.toi_id = ENTITY_LIST_ID;
  3811. for( ; ; ) {
  3812. status = NtDeviceIoControlFile(
  3813. deviceHandle,
  3814. NULL, // Event
  3815. NULL, // ApcRoutine
  3816. NULL, // ApcContext
  3817. &ioStatusBlock,
  3818. IOCTL_TCP_QUERY_INFORMATION_EX,
  3819. &tcpRequest,
  3820. sizeof(tcpRequest),
  3821. entityBuffer,
  3822. entityBufferLength
  3823. );
  3824. if( NT_SUCCESS(status) ) {
  3825. break;
  3826. }
  3827. if( status != STATUS_BUFFER_TOO_SMALL ) {
  3828. goto exit;
  3829. }
  3830. if( entityBuffer != (PVOID)fastEntityBuffer ) {
  3831. RtlFreeHeap(
  3832. RtlProcessHeap(),
  3833. 0,
  3834. entityBuffer
  3835. );
  3836. }
  3837. entityBufferLength += sizeof(TDIEntityID);
  3838. entityBuffer = RtlAllocateHeap(
  3839. RtlProcessHeap(),
  3840. 0,
  3841. entityBufferLength
  3842. );
  3843. if( entityBuffer == NULL ) {
  3844. status = STATUS_INSUFFICIENT_RESOURCES;
  3845. goto exit;
  3846. }
  3847. }
  3848. entityCount = entityBufferLength / sizeof(*entityBuffer);
  3849. //
  3850. // Scan the entities looking for IP.
  3851. //
  3852. for( i = 0, entityScan = entityBuffer ;
  3853. i < entityCount ;
  3854. i++, entityScan++ ) {
  3855. if( entityScan->tei_entity != CL_NL_ENTITY ) {
  3856. continue;
  3857. }
  3858. RtlZeroMemory(
  3859. &tcpRequest,
  3860. sizeof(tcpRequest)
  3861. );
  3862. objectId.toi_entity = *entityScan;
  3863. objectId.toi_class = INFO_CLASS_GENERIC;
  3864. objectId.toi_type = INFO_TYPE_PROVIDER;
  3865. objectId.toi_id = ENTITY_TYPE_ID;
  3866. tcpRequest.ID = objectId;
  3867. status = NtDeviceIoControlFile(
  3868. deviceHandle,
  3869. NULL, // Event
  3870. NULL, // ApcRoutine
  3871. NULL, // ApcContext
  3872. &ioStatusBlock,
  3873. IOCTL_TCP_QUERY_INFORMATION_EX,
  3874. &tcpRequest,
  3875. sizeof(tcpRequest),
  3876. &entityType,
  3877. sizeof(entityType)
  3878. );
  3879. if( !NT_SUCCESS(status) ) {
  3880. goto exit;
  3881. }
  3882. if( entityType != CL_NL_IP ) {
  3883. continue;
  3884. }
  3885. //
  3886. // OK, we found an IP entity. Now lookup its addresses.
  3887. // Start by querying the number of addresses supported by
  3888. // this interface.
  3889. //
  3890. RtlZeroMemory(
  3891. &tcpRequest,
  3892. sizeof(tcpRequest)
  3893. );
  3894. objectId.toi_class = INFO_CLASS_PROTOCOL;
  3895. objectId.toi_id = IP_MIB_STATS_ID;
  3896. tcpRequest.ID = objectId;
  3897. status = NtDeviceIoControlFile(
  3898. deviceHandle,
  3899. NULL, // Event
  3900. NULL, // ApcRoutine
  3901. NULL, // ApcContext
  3902. &ioStatusBlock,
  3903. IOCTL_TCP_QUERY_INFORMATION_EX,
  3904. &tcpRequest,
  3905. sizeof(tcpRequest),
  3906. &snmpInfo,
  3907. sizeof(snmpInfo)
  3908. );
  3909. if( !NT_SUCCESS(status) ) {
  3910. goto exit;
  3911. }
  3912. if( snmpInfo.ipsi_numaddr <= 0 ) {
  3913. continue;
  3914. }
  3915. //
  3916. // This interface has addresses. Cool. Allocate a temporary
  3917. // buffer so we can query them.
  3918. //
  3919. addressBufferLength = snmpInfo.ipsi_numaddr * sizeof(*addressBuffer);
  3920. if( addressBufferLength <= sizeof(fastAddressBuffer) ) {
  3921. addressBuffer = (PVOID)fastAddressBuffer;
  3922. } else {
  3923. addressBuffer = RtlAllocateHeap(
  3924. RtlProcessHeap(),
  3925. 0,
  3926. addressBufferLength
  3927. );
  3928. if( addressBuffer == NULL ) {
  3929. status = STATUS_INSUFFICIENT_RESOURCES;
  3930. goto exit;
  3931. }
  3932. }
  3933. RtlZeroMemory(
  3934. &tcpRequest,
  3935. sizeof(tcpRequest)
  3936. );
  3937. objectId.toi_id = IP_MIB_ADDRTABLE_ENTRY_ID;
  3938. tcpRequest.ID = objectId;
  3939. status = NtDeviceIoControlFile(
  3940. deviceHandle,
  3941. NULL, // Event
  3942. NULL, // ApcRoutine
  3943. NULL, // ApcContext
  3944. &ioStatusBlock,
  3945. IOCTL_TCP_QUERY_INFORMATION_EX,
  3946. &tcpRequest,
  3947. sizeof(tcpRequest),
  3948. addressBuffer,
  3949. addressBufferLength
  3950. );
  3951. if( !NT_SUCCESS(status) ) {
  3952. goto exit;
  3953. }
  3954. addressBufferLength = (ULONG)ioStatusBlock.Information;
  3955. //
  3956. // Try to get the IFEntry info so we can tell if the interface
  3957. // is "up".
  3958. //
  3959. ifentry = (PVOID)ifentryBuffer;
  3960. RtlZeroMemory(
  3961. ifentryBuffer,
  3962. sizeof(ifentryBuffer)
  3963. );
  3964. RtlZeroMemory(
  3965. &tcpRequest,
  3966. sizeof(tcpRequest)
  3967. );
  3968. addressScan = (IPAddrEntry *) addressBuffer;
  3969. RtlCopyMemory(
  3970. &tcpRequest.Context,
  3971. &addressScan->iae_addr,
  3972. sizeof(addressScan->iae_addr)
  3973. );
  3974. objectId.toi_id = IF_MIB_STATS_ID;
  3975. tcpRequest.ID = objectId;
  3976. tcpRequest.ID.toi_entity.tei_entity = IF_ENTITY;
  3977. status = NtDeviceIoControlFile(
  3978. deviceHandle,
  3979. NULL, // Event
  3980. NULL, // ApcRoutine
  3981. NULL, // ApcContext
  3982. &ioStatusBlock,
  3983. IOCTL_TCP_QUERY_INFORMATION_EX,
  3984. &tcpRequest,
  3985. sizeof(tcpRequest),
  3986. ifentry,
  3987. sizeof(ifentryBuffer)
  3988. );
  3989. if( !NT_SUCCESS(status ) ) {
  3990. ifentry->if_adminstatus = 0;
  3991. }
  3992. //
  3993. // Now scan the list
  3994. //
  3995. for( j = 0, addressScan = addressBuffer ;
  3996. j < snmpInfo.ipsi_numaddr ;
  3997. j++, addressScan++ ) {
  3998. //
  3999. // Skip any entries that don't have IP addresses yet.
  4000. //
  4001. if( addressScan->iae_addr == 0 ) {
  4002. continue;
  4003. }
  4004. //
  4005. // If the output buffer is full, fail the request now.
  4006. //
  4007. if( outputBytesRemaining <= sizeof(*outputInterfaceInfo) ) {
  4008. status = STATUS_BUFFER_TOO_SMALL;
  4009. goto exit;
  4010. }
  4011. //
  4012. // Setup the output structure.
  4013. //
  4014. RtlZeroMemory(
  4015. outputInterfaceInfo,
  4016. sizeof(*outputInterfaceInfo)
  4017. );
  4018. outputInterfaceInfo->iiFlags = IFF_MULTICAST;
  4019. sockaddr = &outputInterfaceInfo->iiAddress.AddressIn;
  4020. sockaddr->sin_family = AF_INET;
  4021. sockaddr->sin_addr.s_addr = addressScan->iae_addr;
  4022. if( sockaddr->sin_addr.s_addr == htonl( INADDR_LOOPBACK ) ) {
  4023. outputInterfaceInfo->iiFlags |= IFF_LOOPBACK;
  4024. }
  4025. sockaddr = &outputInterfaceInfo->iiNetmask.AddressIn;
  4026. sockaddr->sin_family = AF_INET;
  4027. sockaddr->sin_addr.s_addr = addressScan->iae_mask;
  4028. if( addressScan->iae_bcastaddr != 0 ) {
  4029. outputInterfaceInfo->iiFlags |= IFF_BROADCAST;
  4030. sockaddr = &outputInterfaceInfo->iiBroadcastAddress.AddressIn;
  4031. sockaddr->sin_family = AF_INET;
  4032. ASSERT (htonl(INADDR_BROADCAST)==INADDR_BROADCAST);
  4033. sockaddr->sin_addr.s_addr = INADDR_BROADCAST;
  4034. }
  4035. //
  4036. // Grrr...
  4037. //
  4038. // I know how to enumerate the entities supported by the
  4039. // driver (those are stored in entityBuffer).
  4040. //
  4041. // I know how to find the IP entities in that buffer.
  4042. //
  4043. // I know how to enumerate the IP addresses supported
  4044. // by a given IP entity (those are stored in addressBuffer).
  4045. //
  4046. // I also know (but haven't done yet) how to find the IF
  4047. // entities (interfaces; basically NICs) and determine if
  4048. // a given IF is "up" or "down".
  4049. //
  4050. // What I don't know how to do is associate the addresses
  4051. // in addressBuffer with specific IF entities. Without
  4052. // this information, I cannot determine if a given *address*
  4053. // is "up" or "down", so for now I'll just assume they're
  4054. // all "up".
  4055. //
  4056. // if( ifentry->if_adminstatus == IF_STATUS_UP )
  4057. {
  4058. outputInterfaceInfo->iiFlags |= IFF_UP;
  4059. }
  4060. //
  4061. // Get the IP interface info for this interface so we can
  4062. // determine if it's "point-to-point".
  4063. //
  4064. interfaceInfo = (PVOID)interfaceInfoBuffer;
  4065. RtlZeroMemory(
  4066. interfaceInfoBuffer,
  4067. sizeof(interfaceInfoBuffer)
  4068. );
  4069. RtlZeroMemory(
  4070. &tcpRequest,
  4071. sizeof(tcpRequest)
  4072. );
  4073. RtlCopyMemory(
  4074. &tcpRequest.Context,
  4075. &addressScan->iae_addr,
  4076. sizeof(addressScan->iae_addr)
  4077. );
  4078. objectId.toi_id = IP_INTFC_INFO_ID;
  4079. tcpRequest.ID = objectId;
  4080. status = NtDeviceIoControlFile(
  4081. deviceHandle,
  4082. NULL, // Event
  4083. NULL, // ApcRoutine
  4084. NULL, // ApcContext
  4085. &ioStatusBlock,
  4086. IOCTL_TCP_QUERY_INFORMATION_EX,
  4087. &tcpRequest,
  4088. sizeof(tcpRequest),
  4089. interfaceInfo,
  4090. sizeof(interfaceInfoBuffer)
  4091. );
  4092. if( NT_SUCCESS(status) ) {
  4093. if( interfaceInfo->iii_flags & IP_INTFC_FLAG_P2P ) {
  4094. outputInterfaceInfo->iiFlags |= IFF_POINTTOPOINT;
  4095. }
  4096. } else {
  4097. //
  4098. // Print something informative here, then press on.
  4099. //
  4100. }
  4101. //
  4102. // Advance to the next output structure.
  4103. //
  4104. outputInterfaceInfo++;
  4105. outputBytesRemaining -= sizeof(*outputInterfaceInfo);
  4106. }
  4107. //
  4108. // Free the temporary buffer.
  4109. //
  4110. if( addressBuffer != (PVOID)fastAddressBuffer ) {
  4111. RtlFreeHeap(
  4112. RtlProcessHeap(),
  4113. 0,
  4114. addressBuffer
  4115. );
  4116. }
  4117. addressBuffer = NULL;
  4118. }
  4119. //
  4120. // Success!
  4121. //
  4122. *NumberOfBytesReturned = OutputBufferLength - outputBytesRemaining;
  4123. status = STATUS_SUCCESS;
  4124. exit:
  4125. if( addressBuffer != (PVOID)fastAddressBuffer &&
  4126. addressBuffer != NULL ) {
  4127. RtlFreeHeap(
  4128. RtlProcessHeap(),
  4129. 0,
  4130. addressBuffer
  4131. );
  4132. }
  4133. if( entityBuffer != (PVOID)fastEntityBuffer &&
  4134. entityBuffer != NULL ) {
  4135. RtlFreeHeap(
  4136. RtlProcessHeap(),
  4137. 0,
  4138. entityBuffer
  4139. );
  4140. }
  4141. if( deviceHandle != NULL ) {
  4142. NtClose( deviceHandle );
  4143. }
  4144. return status;
  4145. } // GetTcpipInterfaceList
  4146. INT
  4147. NtStatusToSocketError (
  4148. IN NTSTATUS Status
  4149. )
  4150. {
  4151. switch ( Status ) {
  4152. case STATUS_PENDING:
  4153. ASSERT (FALSE);
  4154. return WSASYSCALLFAILURE;
  4155. case STATUS_INVALID_HANDLE:
  4156. case STATUS_OBJECT_TYPE_MISMATCH:
  4157. return WSAENOTSOCK;
  4158. case STATUS_INSUFFICIENT_RESOURCES:
  4159. case STATUS_PAGEFILE_QUOTA:
  4160. case STATUS_COMMITMENT_LIMIT:
  4161. case STATUS_WORKING_SET_QUOTA:
  4162. case STATUS_NO_MEMORY:
  4163. case STATUS_CONFLICTING_ADDRESSES:
  4164. case STATUS_QUOTA_EXCEEDED:
  4165. case STATUS_TOO_MANY_PAGING_FILES:
  4166. case STATUS_REMOTE_RESOURCES:
  4167. case STATUS_TOO_MANY_ADDRESSES:
  4168. return WSAENOBUFS;
  4169. case STATUS_SHARING_VIOLATION:
  4170. case STATUS_ADDRESS_ALREADY_EXISTS:
  4171. return WSAEADDRINUSE;
  4172. case STATUS_LINK_TIMEOUT:
  4173. case STATUS_IO_TIMEOUT:
  4174. case STATUS_TIMEOUT:
  4175. return WSAETIMEDOUT;
  4176. case STATUS_GRACEFUL_DISCONNECT:
  4177. return WSAEDISCON;
  4178. case STATUS_REMOTE_DISCONNECT:
  4179. case STATUS_CONNECTION_RESET:
  4180. case STATUS_LINK_FAILED:
  4181. case STATUS_CONNECTION_DISCONNECTED:
  4182. case STATUS_PORT_UNREACHABLE:
  4183. return WSAECONNRESET;
  4184. case STATUS_LOCAL_DISCONNECT:
  4185. case STATUS_TRANSACTION_ABORTED:
  4186. case STATUS_CONNECTION_ABORTED:
  4187. return WSAECONNABORTED;
  4188. case STATUS_BAD_NETWORK_PATH:
  4189. case STATUS_NETWORK_UNREACHABLE:
  4190. case STATUS_PROTOCOL_UNREACHABLE:
  4191. return WSAENETUNREACH;
  4192. case STATUS_HOST_UNREACHABLE:
  4193. return WSAEHOSTUNREACH;
  4194. case STATUS_CANCELLED:
  4195. case STATUS_REQUEST_ABORTED:
  4196. return WSAEINTR;
  4197. case STATUS_BUFFER_OVERFLOW:
  4198. case STATUS_INVALID_BUFFER_SIZE:
  4199. return WSAEMSGSIZE;
  4200. case STATUS_BUFFER_TOO_SMALL:
  4201. case STATUS_ACCESS_VIOLATION:
  4202. return WSAEFAULT;
  4203. case STATUS_DEVICE_NOT_READY:
  4204. case STATUS_REQUEST_NOT_ACCEPTED:
  4205. return WSAEWOULDBLOCK;
  4206. case STATUS_INVALID_NETWORK_RESPONSE:
  4207. case STATUS_NETWORK_BUSY:
  4208. case STATUS_NO_SUCH_DEVICE:
  4209. case STATUS_NO_SUCH_FILE:
  4210. case STATUS_OBJECT_PATH_NOT_FOUND:
  4211. case STATUS_OBJECT_NAME_NOT_FOUND:
  4212. case STATUS_UNEXPECTED_NETWORK_ERROR:
  4213. return WSAENETDOWN;
  4214. case STATUS_INVALID_CONNECTION:
  4215. return WSAENOTCONN;
  4216. case STATUS_REMOTE_NOT_LISTENING:
  4217. case STATUS_CONNECTION_REFUSED:
  4218. return WSAECONNREFUSED;
  4219. case STATUS_PIPE_DISCONNECTED:
  4220. return WSAESHUTDOWN;
  4221. case STATUS_INVALID_ADDRESS:
  4222. case STATUS_INVALID_ADDRESS_COMPONENT:
  4223. return WSAEADDRNOTAVAIL;
  4224. case STATUS_NOT_SUPPORTED:
  4225. case STATUS_NOT_IMPLEMENTED:
  4226. return WSAEOPNOTSUPP;
  4227. case STATUS_ACCESS_DENIED:
  4228. return WSAEACCES;
  4229. default:
  4230. if ( NT_SUCCESS(Status) ) {
  4231. #if DBG
  4232. DbgPrint ("SockNtStatusToSocketError: success status %lx "
  4233. "not mapped\n", Status );
  4234. #endif
  4235. return NO_ERROR;
  4236. }
  4237. #if DBG
  4238. DbgPrint ("SockNtStatusToSocketError: unable to map 0x%lX, returning\n", Status );
  4239. #endif
  4240. return WSAENOBUFS;
  4241. case STATUS_UNSUCCESSFUL:
  4242. case STATUS_INVALID_PARAMETER:
  4243. case STATUS_ADDRESS_CLOSED:
  4244. case STATUS_CONNECTION_INVALID:
  4245. case STATUS_ADDRESS_ALREADY_ASSOCIATED:
  4246. case STATUS_ADDRESS_NOT_ASSOCIATED:
  4247. case STATUS_CONNECTION_ACTIVE:
  4248. case STATUS_INVALID_DEVICE_STATE:
  4249. case STATUS_INVALID_DEVICE_REQUEST:
  4250. return WSAEINVAL;
  4251. }
  4252. } // NtStatusToSocketError