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.

3833 lines
106 KiB

  1. // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil -*- (for GNU Emacs)
  2. //
  3. // Copyright (c) 1985-2000 Microsoft Corporation
  4. //
  5. // This file is part of the Microsoft Research IPv6 Network Protocol Stack.
  6. // You should have received a copy of the Microsoft End-User License Agreement
  7. // for this software along with this release; see the file "license.txt".
  8. // If not, please see http://www.research.microsoft.com/msripv6/license.htm,
  9. // or write to Microsoft Research, One Microsoft Way, Redmond, WA 98052-6399.
  10. //
  11. // Abstract:
  12. //
  13. // This module contains necessary routines for the Windows Sockets
  14. // Helper DLL. This DLL provides the transport-specific support necessary
  15. // for the Windows Sockets DLL to use IPv6 as a transport.
  16. //
  17. // Revision History:
  18. //
  19. // Ported from wshsmple.c in the DDK.
  20. //
  21. #define UNICODE
  22. #include <nt.h>
  23. #include <ntrtl.h>
  24. #include <nturtl.h>
  25. #include <windows.h>
  26. #define UDP_HEADER_SIZE 8
  27. #include <wchar.h>
  28. #include <winsock2.h>
  29. #include <ws2tcpip.h>
  30. #include <ws2ip6.h>
  31. #include <ip6.h> // IPv6 protocol definitions.
  32. #include <wsahelp.h>
  33. // private socket options to be accessed via WSAIoctl
  34. #include <mstcpip.h>
  35. #include <ntddip6.h>
  36. #include <ntddtcp.h>
  37. #include <tdiinfo.h>
  38. #include <smpletcp.h>
  39. #include <nspapi.h>
  40. //
  41. // Define alignment macros to align structure sizes and pointers up and down.
  42. //
  43. #define ALIGN_DOWN(length, type) \
  44. ((ULONG)(length) & ~(sizeof(type) - 1))
  45. #define ALIGN_UP(length, type) \
  46. (ALIGN_DOWN(((ULONG)(length) + sizeof(type) - 1), type))
  47. #define ALIGN_DOWN_POINTER(address, type) \
  48. ((PVOID)((ULONG_PTR)(address) & ~((ULONG_PTR)sizeof(type) - 1)))
  49. #define ALIGN_UP_POINTER(address, type) \
  50. (ALIGN_DOWN_POINTER(((ULONG_PTR)(address) + sizeof(type) - 1), type))
  51. ///////////////////////////////////////////////////
  52. #define TCP_NAME L"TCP/IPv6"
  53. #define UDP_NAME L"UDP/IPv6"
  54. #define RAW_NAME L"RAW/IPv6"
  55. #define IS_DGRAM_SOCK(type) (((type) == SOCK_DGRAM) || ((type) == SOCK_RAW))
  56. //
  57. // Define valid flags for WSHOpenSocket2().
  58. //
  59. #define VALID_TCP_FLAGS (WSA_FLAG_OVERLAPPED)
  60. #define VALID_UDP_FLAGS (WSA_FLAG_OVERLAPPED | \
  61. WSA_FLAG_MULTIPOINT_C_LEAF | \
  62. WSA_FLAG_MULTIPOINT_D_LEAF)
  63. //
  64. // Structure and variables to define the triples supported by TCP/IP. The
  65. // first entry of each array is considered the canonical triple for
  66. // that socket type; the other entries are synonyms for the first.
  67. //
  68. typedef struct _MAPPING_TRIPLE {
  69. INT AddressFamily;
  70. INT SocketType;
  71. INT Protocol;
  72. } MAPPING_TRIPLE, *PMAPPING_TRIPLE;
  73. MAPPING_TRIPLE TcpMappingTriples[] = { AF_INET6, SOCK_STREAM, IPPROTO_TCP,
  74. AF_INET6, SOCK_STREAM, 0,
  75. AF_INET6, 0, IPPROTO_TCP };
  76. MAPPING_TRIPLE UdpMappingTriples[] = { AF_INET6, SOCK_DGRAM, IPPROTO_UDP,
  77. AF_INET6, SOCK_DGRAM, 0,
  78. AF_INET6, 0, IPPROTO_UDP };
  79. MAPPING_TRIPLE RawMappingTriples[] = { AF_INET6, SOCK_RAW, 0 };
  80. //
  81. // Winsock 2 WSAPROTOCOL_INFO structures for all supported protocols.
  82. //
  83. #define WINSOCK_SPI_VERSION 2
  84. // sizeof(UDPHeader) == 8
  85. #define UDP_MESSAGE_SIZE (MAX_IPv6_PAYLOAD - 8)
  86. WSAPROTOCOL_INFOW Winsock2Protocols[] =
  87. {
  88. //
  89. // TCP
  90. //
  91. {
  92. XP1_GUARANTEED_DELIVERY // dwServiceFlags1
  93. | XP1_GUARANTEED_ORDER
  94. | XP1_GRACEFUL_CLOSE
  95. | XP1_EXPEDITED_DATA
  96. | XP1_IFS_HANDLES,
  97. 0, // dwServiceFlags2
  98. 0, // dwServiceFlags3
  99. 0, // dwServiceFlags4
  100. PFL_MATCHES_PROTOCOL_ZERO, // dwProviderFlags
  101. { // gProviderId
  102. 0, 0, 0,
  103. { 0, 0, 0, 0, 0, 0, 0, 0 }
  104. },
  105. 0, // dwCatalogEntryId
  106. { // ProtocolChain
  107. BASE_PROTOCOL, // ChainLen
  108. { 0, 0, 0, 0, 0, 0, 0 } // ChainEntries
  109. },
  110. WINSOCK_SPI_VERSION, // iVersion
  111. AF_INET6, // iAddressFamily
  112. sizeof(SOCKADDR_IN6), // iMaxSockAddr
  113. sizeof(SOCKADDR_IN6), // iMinSockAddr
  114. SOCK_STREAM, // iSocketType
  115. IPPROTO_TCP, // iProtocol
  116. 0, // iProtocolMaxOffset
  117. BIGENDIAN, // iNetworkByteOrder
  118. SECURITY_PROTOCOL_NONE, // iSecurityScheme
  119. 0, // dwMessageSize
  120. 0, // dwProviderReserved
  121. L"MSAFD Tcpip [" TCP_NAME L"]" // szProtocol
  122. },
  123. //
  124. // UDP
  125. //
  126. {
  127. XP1_CONNECTIONLESS // dwServiceFlags1
  128. | XP1_MESSAGE_ORIENTED
  129. | XP1_SUPPORT_BROADCAST
  130. | XP1_SUPPORT_MULTIPOINT
  131. | XP1_IFS_HANDLES,
  132. 0, // dwServiceFlags2
  133. 0, // dwServiceFlags3
  134. 0, // dwServiceFlags4
  135. PFL_MATCHES_PROTOCOL_ZERO, // dwProviderFlags
  136. { // gProviderId
  137. 0, 0, 0,
  138. { 0, 0, 0, 0, 0, 0, 0, 0 }
  139. },
  140. 0, // dwCatalogEntryId
  141. { // ProtocolChain
  142. BASE_PROTOCOL, // ChainLen
  143. { 0, 0, 0, 0, 0, 0, 0 } // ChainEntries
  144. },
  145. WINSOCK_SPI_VERSION, // iVersion
  146. AF_INET6, // iAddressFamily
  147. sizeof(SOCKADDR_IN6), // iMaxSockAddr
  148. sizeof(SOCKADDR_IN6), // iMinSockAddr
  149. SOCK_DGRAM, // iSocketType
  150. IPPROTO_UDP, // iProtocol
  151. 0, // iProtocolMaxOffset
  152. BIGENDIAN, // iNetworkByteOrder
  153. SECURITY_PROTOCOL_NONE, // iSecurityScheme
  154. UDP_MESSAGE_SIZE, // dwMessageSize
  155. 0, // dwProviderReserved
  156. L"MSAFD Tcpip [" UDP_NAME L"]" // szProtocol
  157. },
  158. //
  159. // RAW
  160. //
  161. {
  162. XP1_CONNECTIONLESS // dwServiceFlags1
  163. | XP1_MESSAGE_ORIENTED
  164. | XP1_SUPPORT_BROADCAST
  165. | XP1_SUPPORT_MULTIPOINT
  166. | XP1_IFS_HANDLES,
  167. 0, // dwServiceFlags2
  168. 0, // dwServiceFlags3
  169. 0, // dwServiceFlags4
  170. PFL_MATCHES_PROTOCOL_ZERO // dwProviderFlags
  171. | PFL_HIDDEN,
  172. { // gProviderId
  173. 0, 0, 0,
  174. { 0, 0, 0, 0, 0, 0, 0, 0 }
  175. },
  176. 0, // dwCatalogEntryId
  177. { // ProtocolChain
  178. BASE_PROTOCOL, // ChainLen
  179. { 0, 0, 0, 0, 0, 0, 0 } // ChainEntries
  180. },
  181. WINSOCK_SPI_VERSION, // iVersion
  182. AF_INET6, // iAddressFamily
  183. sizeof(SOCKADDR_IN6), // iMaxSockAddr
  184. sizeof(SOCKADDR_IN6), // iMinSockAddr
  185. SOCK_RAW, // iSocketType
  186. 0, // iProtocol
  187. 255, // iProtocolMaxOffset
  188. BIGENDIAN, // iNetworkByteOrder
  189. SECURITY_PROTOCOL_NONE, // iSecurityScheme
  190. UDP_MESSAGE_SIZE, // dwMessageSize
  191. 0, // dwProviderReserved
  192. L"MSAFD Tcpip [" RAW_NAME L"]" // szProtocol
  193. }
  194. };
  195. #define NUM_WINSOCK2_PROTOCOLS \
  196. ( sizeof(Winsock2Protocols) / sizeof(Winsock2Protocols[0]) )
  197. //
  198. // The GUID identifying this provider.
  199. //
  200. GUID IPv6ProviderGuid = { /* f9eab0c0-26d4-11d0-bbbf-00aa006c34e4 */
  201. 0xf9eab0c0,
  202. 0x26d4,
  203. 0x11d0,
  204. {0xbb, 0xbf, 0x00, 0xaa, 0x00, 0x6c, 0x34, 0xe4}
  205. };
  206. #define TL_INSTANCE 0
  207. //
  208. // Declarations of external routines for XP/SP1
  209. //
  210. NTSTATUS
  211. NTAPI
  212. RtlIpv6AddressToStringExW(
  213. IN const struct in6_addr *Address,
  214. IN ULONG ScopeId,
  215. IN USHORT Port,
  216. OUT LPTSTR AddressString,
  217. IN OUT PULONG AddressStringLength
  218. );
  219. NTSTATUS
  220. NTAPI
  221. RtlIpv6StringToAddressExW (
  222. IN LPCTSTR AddressString,
  223. OUT struct in6_addr *Address,
  224. OUT PULONG ScopeId,
  225. OUT PUSHORT Port
  226. );
  227. //
  228. // Forward declarations of internal routines.
  229. //
  230. VOID
  231. CompleteTdiActionApc (
  232. IN PVOID ApcContext,
  233. IN PIO_STATUS_BLOCK IoStatusBlock
  234. );
  235. INT
  236. SetTdiInformation (
  237. IN HANDLE TdiConnectionObjectHandle,
  238. IN ULONG Entity,
  239. IN ULONG Class,
  240. IN ULONG Type,
  241. IN ULONG Id,
  242. IN PVOID Value,
  243. IN ULONG ValueLength,
  244. IN BOOLEAN WaitForCompletion
  245. );
  246. BOOLEAN
  247. IsTripleInList (
  248. IN PMAPPING_TRIPLE List,
  249. IN ULONG ListLength,
  250. IN INT AddressFamily,
  251. IN INT SocketType,
  252. IN INT Protocol
  253. );
  254. //
  255. // The socket context structure for this DLL. Each open TCP/IP socket
  256. // will have one of these context structures, which is used to maintain
  257. // information about the socket.
  258. //
  259. typedef struct _WSHTCPIP_SOCKET_CONTEXT {
  260. INT AddressFamily;
  261. INT SocketType;
  262. INT Protocol;
  263. INT ReceiveBufferSize;
  264. DWORD Flags;
  265. INT MulticastHops;
  266. INT UnicastHops;
  267. ULONG MulticastInterface;
  268. INT MulticastLoopback;
  269. BOOLEAN KeepAlive;
  270. BOOLEAN NoDelay;
  271. BOOLEAN BsdUrgent;
  272. BOOLEAN MultipointLeaf;
  273. BOOLEAN Reserved3;
  274. IN6_ADDR MultipointTarget;
  275. HANDLE MultipointRootTdiAddressHandle;
  276. USHORT UdpChecksumCoverage;
  277. struct tcp_keepalive KeepAliveVals;
  278. INT Ipv6PktInfo;
  279. INT Ipv6HopLimit;
  280. INT HeaderInclude;
  281. } WSHTCPIP_SOCKET_CONTEXT, *PWSHTCPIP_SOCKET_CONTEXT;
  282. #define DEFAULT_RECEIVE_BUFFER_SIZE 8192
  283. #define DEFAULT_MULTICAST_HOPS -1
  284. #define DEFAULT_MULTICAST_INTERFACE 0
  285. #define DEFAULT_MULTICAST_LOOPBACK TRUE
  286. #define DEFAULT_UDP_CHECKSUM_COVERAGE 0
  287. #define DEFAULT_UNICAST_HOPS -1
  288. #define DEFAULT_HEADER_INCLUDE FALSE
  289. BOOLEAN
  290. DllInitialize (
  291. IN PVOID DllHandle,
  292. IN ULONG Reason,
  293. IN PVOID Context OPTIONAL
  294. )
  295. {
  296. switch ( Reason ) {
  297. case DLL_PROCESS_ATTACH:
  298. //
  299. // We don't need to receive thread attach and detach
  300. // notifications, so disable them to help application
  301. // performance.
  302. //
  303. DisableThreadLibraryCalls( DllHandle );
  304. return TRUE;
  305. case DLL_THREAD_ATTACH:
  306. break;
  307. case DLL_PROCESS_DETACH:
  308. break;
  309. case DLL_THREAD_DETACH:
  310. break;
  311. }
  312. return TRUE;
  313. } // SockInitialize
  314. INT
  315. WSHGetSockaddrType (
  316. IN PSOCKADDR Sockaddr,
  317. IN DWORD SockaddrLength,
  318. OUT PSOCKADDR_INFO SockaddrInfo
  319. )
  320. /*++
  321. Routine Description:
  322. This routine parses a sockaddr to determine the type of the
  323. machine address and endpoint address portions of the sockaddr.
  324. This is called by the winsock DLL whenever it needs to interpret
  325. a sockaddr.
  326. Arguments:
  327. Sockaddr - a pointer to the sockaddr structure to evaluate.
  328. SockaddrLength - the number of bytes in the sockaddr structure.
  329. SockaddrInfo - a pointer to a structure that will receive information
  330. about the specified sockaddr.
  331. Return Value:
  332. INT - a winsock error code indicating the status of the operation, or
  333. NO_ERROR if the operation succeeded.
  334. --*/
  335. {
  336. SOCKADDR_IN6 *sockaddr = (PSOCKADDR_IN6)Sockaddr;
  337. //
  338. // Make sure that the length is correct.
  339. //
  340. if ( SockaddrLength < sizeof(SOCKADDR_IN6) ) {
  341. return WSAEFAULT;
  342. }
  343. //
  344. // Make sure that the address family is correct.
  345. //
  346. if ( sockaddr->sin6_family != AF_INET6 ) {
  347. return WSAEAFNOSUPPORT;
  348. }
  349. //
  350. // The address passed the tests, looks like a good address.
  351. // Determine the type of the address portion of the sockaddr.
  352. // Note that IPv6 does not have a broadcast address.
  353. //
  354. if (IN6_IS_ADDR_UNSPECIFIED(&sockaddr->sin6_addr))
  355. SockaddrInfo->AddressInfo = SockaddrAddressInfoWildcard;
  356. else if (IN6_IS_ADDR_LOOPBACK(&sockaddr->sin6_addr))
  357. SockaddrInfo->AddressInfo = SockaddrAddressInfoLoopback;
  358. else
  359. SockaddrInfo->AddressInfo = SockaddrAddressInfoNormal;
  360. //
  361. // Determine the type of the port (endpoint) in the sockaddr.
  362. //
  363. if ( sockaddr->sin6_port == 0 ) {
  364. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoWildcard;
  365. } else if ( ntohs( sockaddr->sin6_port ) < 2000 ) {
  366. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoReserved;
  367. } else {
  368. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoNormal;
  369. }
  370. return NO_ERROR;
  371. } // WSHGetSockaddrType
  372. INT
  373. WSHGetSocketInformation (
  374. IN PVOID HelperDllSocketContext,
  375. IN SOCKET SocketHandle,
  376. IN HANDLE TdiAddressObjectHandle,
  377. IN HANDLE TdiConnectionObjectHandle,
  378. IN INT Level,
  379. IN INT OptionName,
  380. OUT PCHAR OptionValue,
  381. OUT PINT OptionLength
  382. )
  383. /*++
  384. Routine Description:
  385. This routine retrieves information about a socket for those socket
  386. options supported in this helper DLL. The options supported here
  387. are SO_KEEPALIVE, SO_DONTROUTE, and TCP_EXPEDITED_1122. This routine is
  388. called by the winsock DLL when a level/option name combination is
  389. passed to getsockopt() that the winsock DLL does not understand.
  390. Arguments:
  391. HelperDllSocketContext - the context pointer returned from
  392. WSHOpenSocket().
  393. SocketHandle - the handle of the socket for which we're getting
  394. information.
  395. TdiAddressObjectHandle - the TDI address object of the socket, if
  396. any. If the socket is not yet bound to an address, then
  397. it does not have a TDI address object and this parameter
  398. will be NULL.
  399. TdiConnectionObjectHandle - the TDI connection object of the socket,
  400. if any. If the socket is not yet connected, then it does not
  401. have a TDI connection object and this parameter will be NULL.
  402. Level - the level parameter passed to getsockopt().
  403. OptionName - the optname parameter passed to getsockopt().
  404. OptionValue - the optval parameter passed to getsockopt().
  405. OptionLength - the optlen parameter passed to getsockopt().
  406. Return Value:
  407. INT - a winsock error code indicating the status of the operation, or
  408. NO_ERROR if the operation succeeded.
  409. --*/
  410. {
  411. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  412. UNREFERENCED_PARAMETER( SocketHandle );
  413. UNREFERENCED_PARAMETER( TdiAddressObjectHandle );
  414. UNREFERENCED_PARAMETER( TdiConnectionObjectHandle );
  415. //
  416. // Check if this is an internal request for context information.
  417. //
  418. if ( Level == SOL_INTERNAL && OptionName == SO_CONTEXT ) {
  419. //
  420. // The Windows Sockets DLL is requesting context information
  421. // from us. If an output buffer was not supplied, the Windows
  422. // Sockets DLL is just requesting the size of our context
  423. // information.
  424. //
  425. if ( OptionValue != NULL ) {
  426. //
  427. // Make sure that the buffer is sufficient to hold all the
  428. // context information.
  429. //
  430. if ( *OptionLength < sizeof(*context) ) {
  431. return WSAEFAULT;
  432. }
  433. //
  434. // Copy in the context information.
  435. //
  436. CopyMemory( OptionValue, context, sizeof(*context) );
  437. }
  438. *OptionLength = sizeof(*context);
  439. return NO_ERROR;
  440. }
  441. //
  442. // The only other levels we support here are SOL_SOCKET,
  443. // IPPROTO_TCP, IPPROTO_UDP, and IPPROTO_IPV6.
  444. //
  445. if ( Level != SOL_SOCKET &&
  446. Level != IPPROTO_TCP &&
  447. Level != IPPROTO_UDP &&
  448. Level != IPPROTO_IPV6 ) {
  449. return WSAEINVAL;
  450. }
  451. //
  452. // Make sure that the output buffer is sufficiently large.
  453. //
  454. if ( *OptionLength < sizeof(int) ) {
  455. return WSAEFAULT;
  456. }
  457. //
  458. // Handle TCP-level options.
  459. //
  460. if ( Level == IPPROTO_TCP ) {
  461. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  462. return WSAENOPROTOOPT;
  463. }
  464. switch ( OptionName ) {
  465. case TCP_NODELAY:
  466. ZeroMemory( OptionValue, *OptionLength );
  467. *OptionValue = context->NoDelay;
  468. *OptionLength = sizeof(int);
  469. break;
  470. case TCP_EXPEDITED_1122:
  471. ZeroMemory( OptionValue, *OptionLength );
  472. *OptionValue = !context->BsdUrgent;
  473. *OptionLength = sizeof(int);
  474. break;
  475. default:
  476. return WSAEINVAL;
  477. }
  478. return NO_ERROR;
  479. }
  480. //
  481. // Handle UDP-level options.
  482. //
  483. if ( Level == IPPROTO_UDP ) {
  484. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  485. return WSAENOPROTOOPT;
  486. }
  487. //
  488. // Note that UDP_NOCHECKSUM is not supported for IPv6.
  489. //
  490. switch ( OptionName ) {
  491. case UDP_CHECKSUM_COVERAGE:
  492. *(PULONG)OptionValue = context->UdpChecksumCoverage;
  493. *OptionLength = sizeof(int);
  494. break;
  495. default:
  496. return WSAEINVAL;
  497. }
  498. return NO_ERROR;
  499. }
  500. //
  501. // Handle IP-level options.
  502. //
  503. if ( Level == IPPROTO_IPV6 ) {
  504. //
  505. // Act based on the specific option.
  506. //
  507. switch ( OptionName ) {
  508. case IPV6_UNICAST_HOPS:
  509. *(PINT)OptionValue = context->UnicastHops;
  510. *OptionLength = sizeof(int);
  511. return NO_ERROR;
  512. default:
  513. //
  514. // No match, fall through.
  515. //
  516. break;
  517. }
  518. //
  519. // The following IP options are only valid on datagram sockets.
  520. //
  521. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  522. return WSAENOPROTOOPT;
  523. }
  524. //
  525. // Act based on the specific option.
  526. //
  527. switch ( OptionName ) {
  528. case IPV6_MULTICAST_HOPS:
  529. *(PINT)OptionValue = context->MulticastHops;
  530. *OptionLength = sizeof(int);
  531. return NO_ERROR;
  532. case IPV6_MULTICAST_IF:
  533. *(PULONG)OptionValue = context->MulticastInterface;
  534. *OptionLength = sizeof(int);
  535. return NO_ERROR;
  536. case IPV6_MULTICAST_LOOP:
  537. *(PINT)OptionValue = context->MulticastLoopback;
  538. *OptionLength = sizeof(int);
  539. return NO_ERROR;
  540. case IPV6_HDRINCL:
  541. *(PINT)OptionValue = context->HeaderInclude;
  542. *OptionLength = sizeof(int);
  543. return NO_ERROR;
  544. case IPV6_PKTINFO:
  545. *OptionValue = (char)context->Ipv6PktInfo;
  546. *OptionLength = sizeof(int);
  547. return NO_ERROR;
  548. case IPV6_HOPLIMIT:
  549. *OptionValue = (char)context->Ipv6HopLimit;
  550. *OptionLength = sizeof(int);
  551. return NO_ERROR;
  552. default:
  553. return WSAENOPROTOOPT;
  554. }
  555. }
  556. //
  557. // Handle socket-level options.
  558. //
  559. switch ( OptionName ) {
  560. case SO_KEEPALIVE:
  561. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  562. return WSAENOPROTOOPT;
  563. }
  564. ZeroMemory( OptionValue, *OptionLength );
  565. *OptionValue = context->KeepAlive;
  566. *OptionLength = sizeof(int);
  567. break;
  568. default:
  569. return WSAENOPROTOOPT;
  570. }
  571. return NO_ERROR;
  572. } // WSHGetSocketInformation
  573. INT
  574. WSHGetWildcardSockaddr (
  575. IN PVOID HelperDllSocketContext,
  576. OUT PSOCKADDR Sockaddr,
  577. OUT PINT SockaddrLength
  578. )
  579. /*++
  580. Routine Description:
  581. This routine returns a wildcard socket address. A wildcard address
  582. is one which will bind the socket to an endpoint of the transport's
  583. choosing. For IPv6, a wildcard address has address ::0 and port 0.
  584. Arguments:
  585. HelperDllSocketContext - the context pointer returned from
  586. WSHOpenSocket() for the socket for which we need a wildcard
  587. address.
  588. Sockaddr - points to a buffer which will receive the wildcard socket
  589. address.
  590. SockaddrLength - receives the length of the wioldcard sockaddr.
  591. Return Value:
  592. INT - a winsock error code indicating the status of the operation, or
  593. NO_ERROR if the operation succeeded.
  594. --*/
  595. {
  596. if ( *SockaddrLength < sizeof(SOCKADDR_IN6) ) {
  597. return WSAEFAULT;
  598. }
  599. *SockaddrLength = sizeof(SOCKADDR_IN6);
  600. //
  601. // Just zero out the address and set the family to AF_INET6--this is
  602. // a wildcard address for IPv6.
  603. //
  604. ZeroMemory( Sockaddr, sizeof(SOCKADDR_IN6) );
  605. Sockaddr->sa_family = AF_INET6;
  606. return NO_ERROR;
  607. } // WSAGetWildcardSockaddr
  608. DWORD
  609. WSHGetWinsockMapping (
  610. OUT PWINSOCK_MAPPING Mapping,
  611. IN DWORD MappingLength
  612. )
  613. /*++
  614. Routine Description:
  615. Returns the list of address family/socket type/protocol triples
  616. supported by this helper DLL.
  617. Arguments:
  618. Mapping - receives a pointer to a WINSOCK_MAPPING structure that
  619. describes the triples supported here.
  620. MappingLength - the length, in bytes, of the passed-in Mapping buffer.
  621. Return Value:
  622. DWORD - the length, in bytes, of a WINSOCK_MAPPING structure for this
  623. helper DLL. If the passed-in buffer is too small, the return
  624. value will indicate the size of a buffer needed to contain
  625. the WINSOCK_MAPPING structure.
  626. --*/
  627. {
  628. DWORD mappingLength;
  629. mappingLength = sizeof(WINSOCK_MAPPING) - sizeof(MAPPING_TRIPLE) +
  630. sizeof(TcpMappingTriples) + sizeof(UdpMappingTriples)
  631. + sizeof(RawMappingTriples);
  632. //
  633. // If the passed-in buffer is too small, return the length needed
  634. // now without writing to the buffer. The caller should allocate
  635. // enough memory and call this routine again.
  636. //
  637. if ( mappingLength > MappingLength ) {
  638. return mappingLength;
  639. }
  640. //
  641. // Fill in the output mapping buffer with the list of triples
  642. // supported in this helper DLL.
  643. //
  644. Mapping->Rows = sizeof(TcpMappingTriples) / sizeof(TcpMappingTriples[0])
  645. + sizeof(UdpMappingTriples) / sizeof(UdpMappingTriples[0])
  646. + sizeof(RawMappingTriples) / sizeof(RawMappingTriples[0]);
  647. Mapping->Columns = sizeof(MAPPING_TRIPLE) / sizeof(DWORD);
  648. MoveMemory(
  649. Mapping->Mapping,
  650. TcpMappingTriples,
  651. sizeof(TcpMappingTriples)
  652. );
  653. MoveMemory(
  654. (PCHAR)Mapping->Mapping + sizeof(TcpMappingTriples),
  655. UdpMappingTriples,
  656. sizeof(UdpMappingTriples)
  657. );
  658. MoveMemory(
  659. (PCHAR)Mapping->Mapping + sizeof(TcpMappingTriples)
  660. + sizeof(UdpMappingTriples),
  661. RawMappingTriples,
  662. sizeof(RawMappingTriples)
  663. );
  664. //
  665. // Return the number of bytes we wrote.
  666. //
  667. return mappingLength;
  668. } // WSHGetWinsockMapping
  669. INT
  670. WSHOpenSocket (
  671. IN OUT PINT AddressFamily,
  672. IN OUT PINT SocketType,
  673. IN OUT PINT Protocol,
  674. OUT PUNICODE_STRING TransportDeviceName,
  675. OUT PVOID *HelperDllSocketContext,
  676. OUT PDWORD NotificationEvents
  677. )
  678. {
  679. return WSHOpenSocket2(
  680. AddressFamily,
  681. SocketType,
  682. Protocol,
  683. 0, // Group
  684. 0, // Flags
  685. TransportDeviceName,
  686. HelperDllSocketContext,
  687. NotificationEvents
  688. );
  689. } // WSHOpenSocket
  690. INT
  691. WSHOpenSocket2 (
  692. IN OUT PINT AddressFamily,
  693. IN OUT PINT SocketType,
  694. IN OUT PINT Protocol,
  695. IN GROUP Group,
  696. IN DWORD Flags,
  697. OUT PUNICODE_STRING TransportDeviceName,
  698. OUT PVOID *HelperDllSocketContext,
  699. OUT PDWORD NotificationEvents
  700. )
  701. /*++
  702. Routine Description:
  703. Does the necessary work for this helper DLL to open a socket and is
  704. called by the winsock DLL in the socket() routine. This routine
  705. verifies that the specified triple is valid, determines the NT
  706. device name of the TDI provider that will support that triple,
  707. allocates space to hold the socket's context block, and
  708. canonicalizes the triple.
  709. Arguments:
  710. AddressFamily - on input, the address family specified in the
  711. socket() call. On output, the canonicalized value for the
  712. address family.
  713. SocketType - on input, the socket type specified in the socket()
  714. call. On output, the canonicalized value for the socket type.
  715. Protocol - on input, the protocol specified in the socket() call.
  716. On output, the canonicalized value for the protocol.
  717. Group - Identifies the group for the new socket.
  718. Flags - Zero or more WSA_FLAG_* flags as passed into WSASocket().
  719. TransportDeviceName - receives the name of the TDI provider that
  720. will support the specified triple.
  721. HelperDllSocketContext - receives a context pointer that the winsock
  722. DLL will return to this helper DLL on future calls involving
  723. this socket.
  724. NotificationEvents - receives a bitmask of those state transitions
  725. this helper DLL should be notified on.
  726. Return Value:
  727. INT - a winsock error code indicating the status of the operation, or
  728. NO_ERROR if the operation succeeded.
  729. --*/
  730. {
  731. PWSHTCPIP_SOCKET_CONTEXT context;
  732. //
  733. // Determine whether this is to be a TCP, UDP, or RAW socket.
  734. //
  735. if ( IsTripleInList(
  736. TcpMappingTriples,
  737. sizeof(TcpMappingTriples) / sizeof(TcpMappingTriples[0]),
  738. *AddressFamily,
  739. *SocketType,
  740. *Protocol ) ) {
  741. //
  742. // It's a TCP socket. Check the flags.
  743. //
  744. if( ( Flags & ~VALID_TCP_FLAGS ) != 0 ) {
  745. return WSAEINVAL;
  746. }
  747. //
  748. // Return the canonical form of a TCP socket triple.
  749. //
  750. *AddressFamily = TcpMappingTriples[0].AddressFamily;
  751. *SocketType = TcpMappingTriples[0].SocketType;
  752. *Protocol = TcpMappingTriples[0].Protocol;
  753. //
  754. // Indicate the name of the TDI device that will service
  755. // SOCK_STREAM sockets in the internet address family.
  756. //
  757. RtlInitUnicodeString( TransportDeviceName, DD_TCPV6_DEVICE_NAME );
  758. } else if ( IsTripleInList(
  759. UdpMappingTriples,
  760. sizeof(UdpMappingTriples) / sizeof(UdpMappingTriples[0]),
  761. *AddressFamily,
  762. *SocketType,
  763. *Protocol ) ) {
  764. //
  765. // It's a UDP socket. Check the flags & group ID.
  766. //
  767. if( ( Flags & ~VALID_UDP_FLAGS ) != 0 ||
  768. Group == SG_CONSTRAINED_GROUP ) {
  769. return WSAEINVAL;
  770. }
  771. //
  772. // Return the canonical form of a UDP socket triple.
  773. //
  774. *AddressFamily = UdpMappingTriples[0].AddressFamily;
  775. *SocketType = UdpMappingTriples[0].SocketType;
  776. *Protocol = UdpMappingTriples[0].Protocol;
  777. //
  778. // Indicate the name of the TDI device that will service
  779. // SOCK_DGRAM sockets in the internet address family.
  780. //
  781. RtlInitUnicodeString( TransportDeviceName, DD_UDPV6_DEVICE_NAME );
  782. } else if ( IsTripleInList(
  783. RawMappingTriples,
  784. sizeof(RawMappingTriples) / sizeof(RawMappingTriples[0]),
  785. *AddressFamily,
  786. *SocketType,
  787. *Protocol ) )
  788. {
  789. UNICODE_STRING unicodeString;
  790. NTSTATUS status;
  791. //
  792. // There is no canonicalization to be done for SOCK_RAW.
  793. //
  794. if (*Protocol < 0 || *Protocol > 255) {
  795. return(WSAEINVAL);
  796. }
  797. //
  798. // Indicate the name of the TDI device that will service
  799. // SOCK_RAW sockets in the internet address family.
  800. //
  801. RtlInitUnicodeString(&unicodeString, DD_RAW_IPV6_DEVICE_NAME);
  802. RtlInitUnicodeString(TransportDeviceName, NULL);
  803. TransportDeviceName->MaximumLength = unicodeString.Length +
  804. (4 * sizeof(WCHAR) +
  805. sizeof(UNICODE_NULL));
  806. TransportDeviceName->Buffer = HeapAlloc(GetProcessHeap(), 0,
  807. TransportDeviceName->MaximumLength
  808. );
  809. if (TransportDeviceName->Buffer == NULL) {
  810. return(WSAENOBUFS);
  811. }
  812. //
  813. // Append the device name.
  814. //
  815. status = RtlAppendUnicodeStringToString(
  816. TransportDeviceName,
  817. &unicodeString
  818. );
  819. ASSERT(NT_SUCCESS(status));
  820. //
  821. // Append a separator.
  822. //
  823. TransportDeviceName->Buffer[TransportDeviceName->Length/sizeof(WCHAR)] =
  824. OBJ_NAME_PATH_SEPARATOR;
  825. TransportDeviceName->Length += sizeof(WCHAR);
  826. TransportDeviceName->Buffer[TransportDeviceName->Length/sizeof(WCHAR)] =
  827. UNICODE_NULL;
  828. //
  829. // Append the protocol number.
  830. //
  831. unicodeString.Buffer = TransportDeviceName->Buffer +
  832. (TransportDeviceName->Length / sizeof(WCHAR));
  833. unicodeString.Length = 0;
  834. unicodeString.MaximumLength = TransportDeviceName->MaximumLength -
  835. TransportDeviceName->Length;
  836. status = RtlIntegerToUnicodeString(
  837. (ULONG) *Protocol,
  838. 10,
  839. &unicodeString
  840. );
  841. TransportDeviceName->Length += unicodeString.Length;
  842. ASSERT(NT_SUCCESS(status));
  843. } else {
  844. //
  845. // This should never happen if the registry information about this
  846. // helper DLL is correct. If somehow this did happen, just return
  847. // an error.
  848. //
  849. return WSAEINVAL;
  850. }
  851. //
  852. // Allocate context for this socket. The Windows Sockets DLL will
  853. // return this value to us when it asks us to get/set socket options.
  854. //
  855. context = HeapAlloc(GetProcessHeap(), 0, sizeof(*context) );
  856. if ( context == NULL ) {
  857. return WSAENOBUFS;
  858. }
  859. //
  860. // Initialize the context for the socket.
  861. //
  862. context->AddressFamily = *AddressFamily;
  863. context->SocketType = *SocketType;
  864. context->Protocol = *Protocol;
  865. context->ReceiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
  866. context->Flags = Flags;
  867. context->MulticastHops = DEFAULT_MULTICAST_HOPS;
  868. context->MulticastInterface = DEFAULT_MULTICAST_INTERFACE;
  869. context->MulticastLoopback = DEFAULT_MULTICAST_LOOPBACK;
  870. context->KeepAlive = FALSE;
  871. context->NoDelay = FALSE;
  872. context->BsdUrgent = TRUE;
  873. context->UnicastHops = DEFAULT_UNICAST_HOPS;
  874. context->MultipointLeaf = FALSE;
  875. context->Reserved3 = FALSE;
  876. context->MultipointRootTdiAddressHandle = NULL;
  877. context->UdpChecksumCoverage = DEFAULT_UDP_CHECKSUM_COVERAGE;
  878. context->KeepAliveVals.onoff = FALSE;
  879. context->Ipv6PktInfo = FALSE;
  880. context->Ipv6HopLimit = FALSE;
  881. context->HeaderInclude = DEFAULT_HEADER_INCLUDE;
  882. //
  883. // Tell the Windows Sockets DLL which state transitions we're
  884. // interested in being notified of. The only times we need to be
  885. // called is after a connect has completed so that we can turn on
  886. // the sending of keepalives if SO_KEEPALIVE was set before the
  887. // socket was connected, when the socket is closed so that we can
  888. // free context information, and when a connect fails so that we
  889. // can, if appropriate, dial in to the network that will support the
  890. // connect attempt.
  891. //
  892. *NotificationEvents =
  893. WSH_NOTIFY_CONNECT | WSH_NOTIFY_CLOSE | WSH_NOTIFY_CONNECT_ERROR;
  894. if (IS_DGRAM_SOCK(*SocketType)) {
  895. *NotificationEvents |= WSH_NOTIFY_BIND;
  896. }
  897. //
  898. // Everything worked, return success.
  899. //
  900. *HelperDllSocketContext = context;
  901. return NO_ERROR;
  902. } // WSHOpenSocket
  903. INT
  904. WSHNotify (
  905. IN PVOID HelperDllSocketContext,
  906. IN SOCKET SocketHandle,
  907. IN HANDLE TdiAddressObjectHandle,
  908. IN HANDLE TdiConnectionObjectHandle,
  909. IN DWORD NotifyEvent
  910. )
  911. /*++
  912. Routine Description:
  913. This routine is called by the winsock DLL after a state transition
  914. of the socket. Only state transitions returned in the
  915. NotificationEvents parameter of WSHOpenSocket() are notified here.
  916. This routine allows a winsock helper DLL to track the state of
  917. socket and perform necessary actions corresponding to state
  918. transitions.
  919. Arguments:
  920. HelperDllSocketContext - the context pointer given to the winsock
  921. DLL by WSHOpenSocket().
  922. SocketHandle - the handle for the socket.
  923. TdiAddressObjectHandle - the TDI address object of the socket, if
  924. any. If the socket is not yet bound to an address, then
  925. it does not have a TDI address object and this parameter
  926. will be NULL.
  927. TdiConnectionObjectHandle - the TDI connection object of the socket,
  928. if any. If the socket is not yet connected, then it does not
  929. have a TDI connection object and this parameter will be NULL.
  930. NotifyEvent - indicates the state transition for which we're being
  931. called.
  932. Return Value:
  933. INT - a winsock error code indicating the status of the operation, or
  934. NO_ERROR if the operation succeeded.
  935. --*/
  936. {
  937. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  938. INT err;
  939. //
  940. // We should only be called after a connect() completes or when the
  941. // socket is being closed.
  942. //
  943. if ( NotifyEvent == WSH_NOTIFY_CONNECT ) {
  944. ULONG true = TRUE;
  945. ULONG false = FALSE;
  946. //
  947. // If a connection-object option was set on the socket before
  948. // it was connected, set the option for real now.
  949. //
  950. if ( context->KeepAlive ) {
  951. err = SetTdiInformation(
  952. TdiConnectionObjectHandle,
  953. CO_TL_ENTITY,
  954. INFO_CLASS_PROTOCOL,
  955. INFO_TYPE_CONNECTION,
  956. TCP_SOCKET_KEEPALIVE,
  957. &true,
  958. sizeof(true),
  959. TRUE
  960. );
  961. if ( err != NO_ERROR ) {
  962. return err;
  963. }
  964. }
  965. if ( context->KeepAliveVals.onoff ) {
  966. err = SetTdiInformation(
  967. TdiConnectionObjectHandle,
  968. CO_TL_ENTITY,
  969. INFO_CLASS_PROTOCOL,
  970. INFO_TYPE_CONNECTION,
  971. TCP_SOCKET_KEEPALIVE_VALS,
  972. &context->KeepAliveVals,
  973. sizeof(struct tcp_keepalive),
  974. TRUE
  975. );
  976. if ( err != NO_ERROR ) {
  977. return err;
  978. }
  979. }
  980. if ( context->NoDelay ) {
  981. err = SetTdiInformation(
  982. TdiConnectionObjectHandle,
  983. CO_TL_ENTITY,
  984. INFO_CLASS_PROTOCOL,
  985. INFO_TYPE_CONNECTION,
  986. TCP_SOCKET_NODELAY,
  987. &true,
  988. sizeof(true),
  989. TRUE
  990. );
  991. if ( err != NO_ERROR ) {
  992. return err;
  993. }
  994. }
  995. if ( context->ReceiveBufferSize != DEFAULT_RECEIVE_BUFFER_SIZE ) {
  996. err = SetTdiInformation(
  997. TdiConnectionObjectHandle,
  998. CO_TL_ENTITY,
  999. INFO_CLASS_PROTOCOL,
  1000. INFO_TYPE_CONNECTION,
  1001. TCP_SOCKET_WINDOW,
  1002. &context->ReceiveBufferSize,
  1003. sizeof(context->ReceiveBufferSize),
  1004. TRUE
  1005. );
  1006. if ( err != NO_ERROR ) {
  1007. return err;
  1008. }
  1009. }
  1010. if ( !context->BsdUrgent ) {
  1011. err = SetTdiInformation(
  1012. TdiConnectionObjectHandle,
  1013. CO_TL_ENTITY,
  1014. INFO_CLASS_PROTOCOL,
  1015. INFO_TYPE_CONNECTION,
  1016. TCP_SOCKET_BSDURGENT,
  1017. &false,
  1018. sizeof(false),
  1019. TRUE
  1020. );
  1021. if ( err != NO_ERROR ) {
  1022. return err;
  1023. }
  1024. }
  1025. } else if ( NotifyEvent == WSH_NOTIFY_CLOSE ) {
  1026. //
  1027. // If this is a multipoint leaf, then remove the multipoint target
  1028. // from the session.
  1029. //
  1030. if( context->MultipointLeaf &&
  1031. context->MultipointRootTdiAddressHandle != NULL ) {
  1032. struct ipv6_mreq req;
  1033. req.ipv6mr_multiaddr = context->MultipointTarget;
  1034. req.ipv6mr_interface = 0;
  1035. SetTdiInformation(
  1036. context->MultipointRootTdiAddressHandle,
  1037. CL_TL_ENTITY,
  1038. INFO_CLASS_PROTOCOL,
  1039. INFO_TYPE_ADDRESS_OBJECT,
  1040. AO_OPTION_DEL_MCAST,
  1041. &req,
  1042. sizeof(req),
  1043. TRUE
  1044. );
  1045. }
  1046. //
  1047. // Free the socket context.
  1048. //
  1049. HeapFree(GetProcessHeap(), 0, context );
  1050. } else if ( NotifyEvent == WSH_NOTIFY_CONNECT_ERROR ) {
  1051. //
  1052. // Return WSATRY_AGAIN to get wsock32 to attempt the connect
  1053. // again. Any other return code is ignored.
  1054. //
  1055. } else if ( NotifyEvent == WSH_NOTIFY_BIND ) {
  1056. ULONG true = TRUE;
  1057. if ( context->UnicastHops != DEFAULT_UNICAST_HOPS ) {
  1058. int value = context->UnicastHops;
  1059. err = SetTdiInformation(
  1060. TdiAddressObjectHandle,
  1061. CO_TL_ENTITY,
  1062. INFO_CLASS_PROTOCOL,
  1063. INFO_TYPE_ADDRESS_OBJECT,
  1064. AO_OPTION_TTL,
  1065. &value,
  1066. sizeof(int),
  1067. TRUE
  1068. );
  1069. if ( err != NO_ERROR ) {
  1070. return err;
  1071. }
  1072. }
  1073. if ( context->MulticastHops != DEFAULT_MULTICAST_HOPS ) {
  1074. int value = (int) context->MulticastHops;
  1075. err = SetTdiInformation(
  1076. TdiAddressObjectHandle,
  1077. CO_TL_ENTITY,
  1078. INFO_CLASS_PROTOCOL,
  1079. INFO_TYPE_ADDRESS_OBJECT,
  1080. AO_OPTION_MCASTTTL,
  1081. &value,
  1082. sizeof(int),
  1083. TRUE
  1084. );
  1085. if ( err != NO_ERROR ) {
  1086. return err;
  1087. }
  1088. }
  1089. if ( context->MulticastInterface != DEFAULT_MULTICAST_INTERFACE ) {
  1090. int value = (int) context->MulticastInterface;
  1091. err = SetTdiInformation(
  1092. TdiAddressObjectHandle,
  1093. CO_TL_ENTITY,
  1094. INFO_CLASS_PROTOCOL,
  1095. INFO_TYPE_ADDRESS_OBJECT,
  1096. AO_OPTION_MCASTIF,
  1097. &value,
  1098. sizeof(int),
  1099. TRUE
  1100. );
  1101. if ( err != NO_ERROR ) {
  1102. return err;
  1103. }
  1104. }
  1105. if ( context->MulticastLoopback != DEFAULT_MULTICAST_LOOPBACK ) {
  1106. int value = (int) context->MulticastLoopback;
  1107. err = SetTdiInformation(
  1108. TdiAddressObjectHandle,
  1109. CO_TL_ENTITY,
  1110. INFO_CLASS_PROTOCOL,
  1111. INFO_TYPE_ADDRESS_OBJECT,
  1112. AO_OPTION_MCASTLOOP,
  1113. &value,
  1114. sizeof(int),
  1115. TRUE
  1116. );
  1117. if ( err != NO_ERROR ) {
  1118. return err;
  1119. }
  1120. }
  1121. if ( context->UdpChecksumCoverage != DEFAULT_UDP_CHECKSUM_COVERAGE ) {
  1122. err = SetTdiInformation(
  1123. TdiAddressObjectHandle,
  1124. CL_TL_ENTITY,
  1125. INFO_CLASS_PROTOCOL,
  1126. INFO_TYPE_ADDRESS_OBJECT,
  1127. AO_OPTION_UDP_CHKSUM_COVER,
  1128. &context->UdpChecksumCoverage,
  1129. sizeof context->UdpChecksumCoverage,
  1130. TRUE
  1131. );
  1132. if ( err != NO_ERROR ) {
  1133. return err;
  1134. }
  1135. }
  1136. if ( context->HeaderInclude != DEFAULT_HEADER_INCLUDE ) {
  1137. int value = (int) context->HeaderInclude;
  1138. err = SetTdiInformation(
  1139. TdiAddressObjectHandle,
  1140. CO_TL_ENTITY,
  1141. INFO_CLASS_PROTOCOL,
  1142. INFO_TYPE_ADDRESS_OBJECT,
  1143. AO_OPTION_IP_HDRINCL,
  1144. &value,
  1145. sizeof(int),
  1146. TRUE
  1147. );
  1148. if ( err != NO_ERROR ) {
  1149. return err;
  1150. }
  1151. }
  1152. if ( context->Ipv6PktInfo ) {
  1153. err = SetTdiInformation(
  1154. TdiAddressObjectHandle,
  1155. CO_TL_ENTITY,
  1156. INFO_CLASS_PROTOCOL,
  1157. INFO_TYPE_ADDRESS_OBJECT,
  1158. AO_OPTION_IP_PKTINFO,
  1159. &true,
  1160. sizeof (TRUE),
  1161. TRUE
  1162. );
  1163. if ( err != NO_ERROR ) {
  1164. return err;
  1165. }
  1166. }
  1167. if ( context->Ipv6HopLimit ) {
  1168. err = SetTdiInformation(
  1169. TdiAddressObjectHandle,
  1170. CO_TL_ENTITY,
  1171. INFO_CLASS_PROTOCOL,
  1172. INFO_TYPE_ADDRESS_OBJECT,
  1173. AO_OPTION_RCV_HOPLIMIT,
  1174. &true,
  1175. sizeof (TRUE),
  1176. TRUE
  1177. );
  1178. if ( err != NO_ERROR ) {
  1179. return err;
  1180. }
  1181. }
  1182. } else {
  1183. return WSAEINVAL;
  1184. }
  1185. return NO_ERROR;
  1186. } // WSHNotify
  1187. INT
  1188. WSHSetSocketInformation (
  1189. IN PVOID HelperDllSocketContext,
  1190. IN SOCKET SocketHandle,
  1191. IN HANDLE TdiAddressObjectHandle,
  1192. IN HANDLE TdiConnectionObjectHandle,
  1193. IN INT Level,
  1194. IN INT OptionName,
  1195. IN PCHAR OptionValue,
  1196. IN INT OptionLength
  1197. )
  1198. /*++
  1199. Routine Description:
  1200. This routine sets information about a socket for those socket
  1201. options supported in this helper DLL. The options supported here
  1202. are SO_KEEPALIVE, SO_DONTROUTE, and TCP_EXPEDITED_1122. This routine is
  1203. called by the winsock DLL when a level/option name combination is
  1204. passed to setsockopt() that the winsock DLL does not understand.
  1205. Arguments:
  1206. HelperDllSocketContext - the context pointer returned from
  1207. WSHOpenSocket().
  1208. SocketHandle - the handle of the socket for which we're getting
  1209. information.
  1210. TdiAddressObjectHandle - the TDI address object of the socket, if
  1211. any. If the socket is not yet bound to an address, then
  1212. it does not have a TDI address object and this parameter
  1213. will be NULL.
  1214. TdiConnectionObjectHandle - the TDI connection object of the socket,
  1215. if any. If the socket is not yet connected, then it does not
  1216. have a TDI connection object and this parameter will be NULL.
  1217. Level - the level parameter passed to setsockopt().
  1218. OptionName - the optname parameter passed to setsockopt().
  1219. OptionValue - the optval parameter passed to setsockopt().
  1220. OptionLength - the optlen parameter passed to setsockopt().
  1221. Return Value:
  1222. INT - a winsock error code indicating the status of the operation, or
  1223. NO_ERROR if the operation succeeded.
  1224. --*/
  1225. {
  1226. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  1227. INT error;
  1228. INT optionValue;
  1229. UNREFERENCED_PARAMETER( SocketHandle );
  1230. UNREFERENCED_PARAMETER( TdiAddressObjectHandle );
  1231. UNREFERENCED_PARAMETER( TdiConnectionObjectHandle );
  1232. //
  1233. // Check if this is an internal request for context information.
  1234. //
  1235. if ( Level == SOL_INTERNAL && OptionName == SO_CONTEXT ) {
  1236. //
  1237. // The Windows Sockets DLL is requesting that we set context
  1238. // information for a new socket. If the new socket was
  1239. // accept()'ed, then we have already been notified of the socket
  1240. // and HelperDllSocketContext will be valid. If the new socket
  1241. // was inherited or duped into this process, then this is our
  1242. // first notification of the socket and HelperDllSocketContext
  1243. // will be equal to NULL.
  1244. //
  1245. // Insure that the context information being passed to us is
  1246. // sufficiently large.
  1247. //
  1248. if ( OptionLength < sizeof(*context) ) {
  1249. return WSAEINVAL;
  1250. }
  1251. if ( HelperDllSocketContext == NULL ) {
  1252. //
  1253. // This is our notification that a socket handle was
  1254. // inherited or duped into this process. Allocate a context
  1255. // structure for the new socket.
  1256. //
  1257. context = HeapAlloc(GetProcessHeap(), 0, sizeof(*context) );
  1258. if ( context == NULL ) {
  1259. return WSAENOBUFS;
  1260. }
  1261. //
  1262. // Copy over information into the context block.
  1263. //
  1264. CopyMemory( context, OptionValue, sizeof(*context) );
  1265. //
  1266. // Tell the Windows Sockets DLL where our context information is
  1267. // stored so that it can return the context pointer in future
  1268. // calls.
  1269. //
  1270. *(PWSHTCPIP_SOCKET_CONTEXT *)OptionValue = context;
  1271. return NO_ERROR;
  1272. } else {
  1273. PWSHTCPIP_SOCKET_CONTEXT parentContext;
  1274. INT one = 1;
  1275. INT zero = 0;
  1276. //
  1277. // The socket was accept()'ed and it needs to have the same
  1278. // properties as it's parent. The OptionValue buffer
  1279. // contains the context information of this socket's parent.
  1280. //
  1281. parentContext = (PWSHTCPIP_SOCKET_CONTEXT)OptionValue;
  1282. ASSERT( context->AddressFamily == parentContext->AddressFamily );
  1283. ASSERT( context->SocketType == parentContext->SocketType );
  1284. ASSERT( context->Protocol == parentContext->Protocol );
  1285. //
  1286. // Turn on in the child any options that have been set in
  1287. // the parent.
  1288. //
  1289. if ( parentContext->KeepAlive ) {
  1290. error = WSHSetSocketInformation(
  1291. HelperDllSocketContext,
  1292. SocketHandle,
  1293. TdiAddressObjectHandle,
  1294. TdiConnectionObjectHandle,
  1295. SOL_SOCKET,
  1296. SO_KEEPALIVE,
  1297. (PCHAR)&one,
  1298. sizeof(one)
  1299. );
  1300. if ( error != NO_ERROR ) {
  1301. return error;
  1302. }
  1303. }
  1304. if ( parentContext->KeepAliveVals.onoff ) {
  1305. struct tcp_keepalive *optionval;
  1306. //
  1307. // Atempt to turn on or off keepalive sending, as necessary.
  1308. //
  1309. optionval = &parentContext->KeepAliveVals;
  1310. if ( TdiConnectionObjectHandle != NULL ) {
  1311. error = SetTdiInformation(
  1312. TdiConnectionObjectHandle,
  1313. CO_TL_ENTITY,
  1314. INFO_CLASS_PROTOCOL,
  1315. INFO_TYPE_CONNECTION,
  1316. TCP_SOCKET_KEEPALIVE_VALS,
  1317. optionval,
  1318. sizeof(struct tcp_keepalive),
  1319. TRUE
  1320. );
  1321. if ( error != NO_ERROR ) {
  1322. return error;
  1323. }
  1324. }
  1325. //
  1326. // Remember that keepalives are enabled for this socket.
  1327. //
  1328. context->KeepAliveVals = *optionval;
  1329. }
  1330. if ( parentContext->NoDelay ) {
  1331. error = WSHSetSocketInformation(
  1332. HelperDllSocketContext,
  1333. SocketHandle,
  1334. TdiAddressObjectHandle,
  1335. TdiConnectionObjectHandle,
  1336. IPPROTO_TCP,
  1337. TCP_NODELAY,
  1338. (PCHAR)&one,
  1339. sizeof(one)
  1340. );
  1341. if ( error != NO_ERROR ) {
  1342. return error;
  1343. }
  1344. }
  1345. if ( parentContext->ReceiveBufferSize != DEFAULT_RECEIVE_BUFFER_SIZE ) {
  1346. error = WSHSetSocketInformation(
  1347. HelperDllSocketContext,
  1348. SocketHandle,
  1349. TdiAddressObjectHandle,
  1350. TdiConnectionObjectHandle,
  1351. SOL_SOCKET,
  1352. SO_RCVBUF,
  1353. (PCHAR)&parentContext->ReceiveBufferSize,
  1354. sizeof(parentContext->ReceiveBufferSize)
  1355. );
  1356. if ( error != NO_ERROR ) {
  1357. return error;
  1358. }
  1359. }
  1360. if ( !parentContext->BsdUrgent ) {
  1361. error = WSHSetSocketInformation(
  1362. HelperDllSocketContext,
  1363. SocketHandle,
  1364. TdiAddressObjectHandle,
  1365. TdiConnectionObjectHandle,
  1366. IPPROTO_TCP,
  1367. TCP_EXPEDITED_1122,
  1368. (PCHAR)&one,
  1369. sizeof(one)
  1370. );
  1371. if ( error != NO_ERROR ) {
  1372. return error;
  1373. }
  1374. }
  1375. return NO_ERROR;
  1376. }
  1377. }
  1378. //
  1379. // The only other levels we support here are SOL_SOCKET,
  1380. // IPPROTO_TCP, IPPROTO_UDP, and IPPROTO_IPV6.
  1381. //
  1382. if ( Level != SOL_SOCKET &&
  1383. Level != IPPROTO_TCP &&
  1384. Level != IPPROTO_UDP &&
  1385. Level != IPPROTO_IPV6 ) {
  1386. return WSAEINVAL;
  1387. }
  1388. //
  1389. // Make sure that the option length is sufficient.
  1390. //
  1391. if ( OptionLength < sizeof(int) ) {
  1392. return WSAEFAULT;
  1393. }
  1394. optionValue = *(INT UNALIGNED *)OptionValue;
  1395. //
  1396. // Handle TCP-level options.
  1397. //
  1398. if ( Level == IPPROTO_TCP && OptionName == TCP_NODELAY ) {
  1399. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  1400. return WSAENOPROTOOPT;
  1401. }
  1402. //
  1403. // Atempt to turn on or off Nagle's algorithm, as necessary.
  1404. //
  1405. if ( !context->NoDelay && optionValue != 0 ) {
  1406. optionValue = TRUE;
  1407. //
  1408. // NoDelay is currently off and the application wants to
  1409. // turn it on. If the TDI connection object handle is NULL,
  1410. // then the socket is not yet connected. In this case we'll
  1411. // just remember that the no delay option was set and
  1412. // actually turn them on in WSHNotify() after a connect()
  1413. // has completed on the socket.
  1414. //
  1415. if ( TdiConnectionObjectHandle != NULL ) {
  1416. error = SetTdiInformation(
  1417. TdiConnectionObjectHandle,
  1418. CO_TL_ENTITY,
  1419. INFO_CLASS_PROTOCOL,
  1420. INFO_TYPE_CONNECTION,
  1421. TCP_SOCKET_NODELAY,
  1422. &optionValue,
  1423. sizeof(optionValue),
  1424. TRUE
  1425. );
  1426. if ( error != NO_ERROR ) {
  1427. return error;
  1428. }
  1429. }
  1430. //
  1431. // Remember that no delay is enabled for this socket.
  1432. //
  1433. context->NoDelay = TRUE;
  1434. } else if ( context->NoDelay && optionValue == 0 ) {
  1435. //
  1436. // No delay is currently enabled and the application wants
  1437. // to turn it off. If the TDI connection object is NULL,
  1438. // the socket is not yet connected. In this case we'll just
  1439. // remember that nodelay is disabled.
  1440. //
  1441. if ( TdiConnectionObjectHandle != NULL ) {
  1442. error = SetTdiInformation(
  1443. TdiConnectionObjectHandle,
  1444. CO_TL_ENTITY,
  1445. INFO_CLASS_PROTOCOL,
  1446. INFO_TYPE_CONNECTION,
  1447. TCP_SOCKET_NODELAY,
  1448. &optionValue,
  1449. sizeof(optionValue),
  1450. TRUE
  1451. );
  1452. if ( error != NO_ERROR ) {
  1453. return error;
  1454. }
  1455. }
  1456. //
  1457. // Remember that no delay is disabled for this socket.
  1458. //
  1459. context->NoDelay = FALSE;
  1460. }
  1461. return NO_ERROR;
  1462. }
  1463. if ( Level == IPPROTO_TCP && OptionName == TCP_EXPEDITED_1122 ) {
  1464. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  1465. return WSAENOPROTOOPT;
  1466. }
  1467. //
  1468. // Atempt to turn on or off BSD-style urgent data semantics as
  1469. // necessary.
  1470. //
  1471. if ( !context->BsdUrgent && optionValue == 0 ) {
  1472. optionValue = TRUE;
  1473. //
  1474. // BsdUrgent is currently off and the application wants to
  1475. // turn it on. If the TDI connection object handle is NULL,
  1476. // then the socket is not yet connected. In this case we'll
  1477. // just remember that the no delay option was set and
  1478. // actually turn them on in WSHNotify() after a connect()
  1479. // has completed on the socket.
  1480. //
  1481. if ( TdiConnectionObjectHandle != NULL ) {
  1482. error = SetTdiInformation(
  1483. TdiConnectionObjectHandle,
  1484. CO_TL_ENTITY,
  1485. INFO_CLASS_PROTOCOL,
  1486. INFO_TYPE_CONNECTION,
  1487. TCP_SOCKET_BSDURGENT,
  1488. &optionValue,
  1489. sizeof(optionValue),
  1490. TRUE
  1491. );
  1492. if ( error != NO_ERROR ) {
  1493. return error;
  1494. }
  1495. }
  1496. //
  1497. // Remember that no delay is enabled for this socket.
  1498. //
  1499. context->BsdUrgent = TRUE;
  1500. } else if ( context->BsdUrgent && optionValue != 0 ) {
  1501. //
  1502. // No delay is currently enabled and the application wants
  1503. // to turn it off. If the TDI connection object is NULL,
  1504. // the socket is not yet connected. In this case we'll just
  1505. // remember that BsdUrgent is disabled.
  1506. //
  1507. if ( TdiConnectionObjectHandle != NULL ) {
  1508. error = SetTdiInformation(
  1509. TdiConnectionObjectHandle,
  1510. CO_TL_ENTITY,
  1511. INFO_CLASS_PROTOCOL,
  1512. INFO_TYPE_CONNECTION,
  1513. TCP_SOCKET_BSDURGENT,
  1514. &optionValue,
  1515. sizeof(optionValue),
  1516. TRUE
  1517. );
  1518. if ( error != NO_ERROR ) {
  1519. return error;
  1520. }
  1521. }
  1522. //
  1523. // Remember that BSD urgent is disabled for this socket.
  1524. //
  1525. context->BsdUrgent = FALSE;
  1526. }
  1527. return NO_ERROR;
  1528. }
  1529. //
  1530. // Handle UDP-level options.
  1531. //
  1532. if ( Level == IPPROTO_UDP ) {
  1533. //
  1534. // These options are only valid for datagram sockets.
  1535. //
  1536. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1537. return WSAENOPROTOOPT;
  1538. }
  1539. //
  1540. // Note that UDP_NOCHECKSUM is not supported for IPv6.
  1541. //
  1542. switch ( OptionName ) {
  1543. case UDP_CHECKSUM_COVERAGE:
  1544. //
  1545. // The default is 0 which covers the entire datagram.
  1546. // The minimum is the UDP header.
  1547. //
  1548. if ((optionValue != DEFAULT_UDP_CHECKSUM_COVERAGE) &&
  1549. (optionValue < UDP_HEADER_SIZE)) {
  1550. return WSAEINVAL;
  1551. }
  1552. //
  1553. // If we have a TDI address object, set this option to
  1554. // the address object. If we don't have a TDI address
  1555. // object then we'll have to wait until after the socket
  1556. // is bound.
  1557. //
  1558. if ( TdiAddressObjectHandle != NULL ) {
  1559. error = SetTdiInformation(
  1560. TdiAddressObjectHandle,
  1561. CL_TL_ENTITY,
  1562. INFO_CLASS_PROTOCOL,
  1563. INFO_TYPE_ADDRESS_OBJECT,
  1564. AO_OPTION_UDP_CHKSUM_COVER,
  1565. &optionValue,
  1566. sizeof(optionValue),
  1567. TRUE
  1568. );
  1569. if ( error != NO_ERROR ) {
  1570. return error;
  1571. }
  1572. } else {
  1573. return WSAEINVAL;
  1574. }
  1575. context->UdpChecksumCoverage = (USHORT)optionValue;
  1576. break;
  1577. default :
  1578. return WSAEINVAL;
  1579. }
  1580. return NO_ERROR;
  1581. }
  1582. //
  1583. // Handle IP-level options.
  1584. //
  1585. if ( Level == IPPROTO_IPV6 ) {
  1586. //
  1587. // Act based on the specific option.
  1588. //
  1589. switch ( OptionName ) {
  1590. case IPV6_UNICAST_HOPS:
  1591. //
  1592. // An attempt to change the unicast TTL sent on
  1593. // this socket. It is illegal to set this to a value
  1594. // greater than 255.
  1595. //
  1596. if ( optionValue > 255 || optionValue < -1 ) {
  1597. return WSAEINVAL;
  1598. }
  1599. //
  1600. // If we have a TDI address object, set this option to
  1601. // the address object. If we don't have a TDI address
  1602. // object then we'll have to wait until after the socket
  1603. // is bound.
  1604. //
  1605. if ( TdiAddressObjectHandle != NULL ) {
  1606. error = SetTdiInformation(
  1607. TdiAddressObjectHandle,
  1608. CL_TL_ENTITY,
  1609. INFO_CLASS_PROTOCOL,
  1610. INFO_TYPE_ADDRESS_OBJECT,
  1611. AO_OPTION_TTL,
  1612. &optionValue,
  1613. sizeof(optionValue),
  1614. TRUE
  1615. );
  1616. if ( error != NO_ERROR ) {
  1617. return error;
  1618. }
  1619. }
  1620. context->UnicastHops = optionValue;
  1621. return NO_ERROR;
  1622. case IPV6_MULTICAST_HOPS:
  1623. //
  1624. // This option is only valid for datagram sockets.
  1625. //
  1626. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1627. return WSAENOPROTOOPT;
  1628. }
  1629. //
  1630. // An attempt to change the TTL on multicasts sent on
  1631. // this socket. It is illegal to set this to a value
  1632. // greater than 255.
  1633. //
  1634. if ( optionValue > 255 || optionValue < -1 ) {
  1635. return WSAEINVAL;
  1636. }
  1637. //
  1638. // If we have a TDI address object, set this option to
  1639. // the address object. If we don't have a TDI address
  1640. // object then we'll have to wait until after the socket
  1641. // is bound.
  1642. //
  1643. if ( TdiAddressObjectHandle != NULL ) {
  1644. error = SetTdiInformation(
  1645. TdiAddressObjectHandle,
  1646. CL_TL_ENTITY,
  1647. INFO_CLASS_PROTOCOL,
  1648. INFO_TYPE_ADDRESS_OBJECT,
  1649. AO_OPTION_MCASTTTL,
  1650. &optionValue,
  1651. sizeof(optionValue),
  1652. TRUE
  1653. );
  1654. if ( error != NO_ERROR ) {
  1655. return error;
  1656. }
  1657. }
  1658. context->MulticastHops = optionValue;
  1659. return NO_ERROR;
  1660. case IPV6_MULTICAST_IF:
  1661. //
  1662. // This option is only valid for datagram sockets.
  1663. //
  1664. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1665. return WSAENOPROTOOPT;
  1666. }
  1667. //
  1668. // If we have a TDI address object, set this option to
  1669. // the address object. If we don't have a TDI address
  1670. // object then we'll have to wait until after the socket
  1671. // is bound.
  1672. //
  1673. if ( TdiAddressObjectHandle != NULL ) {
  1674. error = SetTdiInformation(
  1675. TdiAddressObjectHandle,
  1676. CL_TL_ENTITY,
  1677. INFO_CLASS_PROTOCOL,
  1678. INFO_TYPE_ADDRESS_OBJECT,
  1679. AO_OPTION_MCASTIF,
  1680. &optionValue,
  1681. sizeof(optionValue),
  1682. TRUE
  1683. );
  1684. if ( error != NO_ERROR ) {
  1685. return error;
  1686. }
  1687. }
  1688. context->MulticastInterface = optionValue;
  1689. return NO_ERROR;
  1690. case IPV6_MULTICAST_LOOP:
  1691. //
  1692. // This option is only valid for datagram sockets.
  1693. //
  1694. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1695. return WSAENOPROTOOPT;
  1696. }
  1697. //
  1698. // This is a boolean option. 0 = false, 1 = true.
  1699. // All other values are illegal.
  1700. //
  1701. if ( optionValue > 1) {
  1702. return WSAEINVAL;
  1703. }
  1704. //
  1705. // If we have a TDI address object, set this option to
  1706. // the address object. If we don't have a TDI address
  1707. // object then we'll have to wait until after the socket
  1708. // is bound.
  1709. //
  1710. if ( TdiAddressObjectHandle != NULL ) {
  1711. error = SetTdiInformation(
  1712. TdiAddressObjectHandle,
  1713. CL_TL_ENTITY,
  1714. INFO_CLASS_PROTOCOL,
  1715. INFO_TYPE_ADDRESS_OBJECT,
  1716. AO_OPTION_MCASTLOOP,
  1717. &optionValue,
  1718. sizeof(optionValue),
  1719. TRUE
  1720. );
  1721. if ( error != NO_ERROR ) {
  1722. return error;
  1723. }
  1724. }
  1725. context->MulticastLoopback = optionValue;
  1726. return NO_ERROR;
  1727. case IPV6_ADD_MEMBERSHIP:
  1728. case IPV6_DROP_MEMBERSHIP:
  1729. //
  1730. // This option is only valid for datagram sockets.
  1731. //
  1732. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1733. return WSAENOPROTOOPT;
  1734. }
  1735. //
  1736. // Make sure that the option buffer is large enough.
  1737. //
  1738. if ( OptionLength < sizeof(struct ipv6_mreq) ) {
  1739. return WSAEINVAL;
  1740. }
  1741. //
  1742. // If we have a TDI address object, set this option to
  1743. // the address object. If we don't have a TDI address
  1744. // object then we'll have to wait until after the socket
  1745. // is bound.
  1746. //
  1747. if ( TdiAddressObjectHandle != NULL ) {
  1748. error = SetTdiInformation(
  1749. TdiAddressObjectHandle,
  1750. CL_TL_ENTITY,
  1751. INFO_CLASS_PROTOCOL,
  1752. INFO_TYPE_ADDRESS_OBJECT,
  1753. OptionName == IPV6_ADD_MEMBERSHIP ?
  1754. AO_OPTION_ADD_MCAST : AO_OPTION_DEL_MCAST,
  1755. OptionValue,
  1756. OptionLength,
  1757. TRUE
  1758. );
  1759. if ( error != NO_ERROR ) {
  1760. return error;
  1761. }
  1762. } else {
  1763. return WSAEINVAL;
  1764. }
  1765. return NO_ERROR;
  1766. case IPV6_HDRINCL:
  1767. //
  1768. // This option is only valid for datagram sockets.
  1769. //
  1770. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1771. return WSAENOPROTOOPT;
  1772. }
  1773. //
  1774. // This is a boolean option. 0 = false, 1 = true.
  1775. // All other values are illegal.
  1776. //
  1777. if ( optionValue > 1) {
  1778. return WSAEINVAL;
  1779. }
  1780. //
  1781. // If we have a TDI address object, set this option to
  1782. // the address object. If we don't have a TDI address
  1783. // object then we'll have to wait until after the socket
  1784. // is bound.
  1785. //
  1786. if ( TdiAddressObjectHandle != NULL ) {
  1787. error = SetTdiInformation(
  1788. TdiAddressObjectHandle,
  1789. CL_TL_ENTITY,
  1790. INFO_CLASS_PROTOCOL,
  1791. INFO_TYPE_ADDRESS_OBJECT,
  1792. AO_OPTION_IP_HDRINCL,
  1793. &optionValue,
  1794. sizeof(optionValue),
  1795. TRUE
  1796. );
  1797. if ( error != NO_ERROR ) {
  1798. return error;
  1799. }
  1800. }
  1801. context->HeaderInclude = optionValue;
  1802. return NO_ERROR;
  1803. case IPV6_PKTINFO:
  1804. //
  1805. // This option is only valid for datagram sockets.
  1806. //
  1807. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1808. return WSAENOPROTOOPT;
  1809. }
  1810. //
  1811. // This is a boolean option. 0 = false, 1 = true.
  1812. // All other values are illegal.
  1813. //
  1814. if ( optionValue > 1) {
  1815. return WSAEINVAL;
  1816. }
  1817. if ( TdiAddressObjectHandle != NULL ) {
  1818. error = SetTdiInformation(
  1819. TdiAddressObjectHandle,
  1820. CL_TL_ENTITY,
  1821. INFO_CLASS_PROTOCOL,
  1822. INFO_TYPE_ADDRESS_OBJECT,
  1823. AO_OPTION_IP_PKTINFO,
  1824. &optionValue,
  1825. sizeof(optionValue),
  1826. TRUE
  1827. );
  1828. if ( error != NO_ERROR ) {
  1829. return error;
  1830. }
  1831. }
  1832. context->Ipv6PktInfo = optionValue;
  1833. return NO_ERROR;
  1834. case IPV6_HOPLIMIT:
  1835. //
  1836. // This option is only valid for datagram sockets.
  1837. //
  1838. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1839. return WSAENOPROTOOPT;
  1840. }
  1841. //
  1842. // This is a boolean option. 0 = false, 1 = true.
  1843. // All other values are illegal.
  1844. //
  1845. if ( optionValue > 1) {
  1846. return WSAEINVAL;
  1847. }
  1848. if ( TdiAddressObjectHandle != NULL ) {
  1849. error = SetTdiInformation(
  1850. TdiAddressObjectHandle,
  1851. CL_TL_ENTITY,
  1852. INFO_CLASS_PROTOCOL,
  1853. INFO_TYPE_ADDRESS_OBJECT,
  1854. AO_OPTION_RCV_HOPLIMIT,
  1855. &optionValue,
  1856. sizeof(optionValue),
  1857. TRUE
  1858. );
  1859. if ( error != NO_ERROR ) {
  1860. return error;
  1861. }
  1862. }
  1863. context->Ipv6HopLimit = optionValue;
  1864. return NO_ERROR;
  1865. default:
  1866. //
  1867. // No match, fall through.
  1868. //
  1869. break;
  1870. }
  1871. //
  1872. // We don't support this option.
  1873. //
  1874. return WSAENOPROTOOPT;
  1875. }
  1876. //
  1877. // Handle socket-level options.
  1878. //
  1879. switch ( OptionName ) {
  1880. case SO_KEEPALIVE:
  1881. //
  1882. // Atempt to turn on or off keepalive sending, as necessary.
  1883. //
  1884. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  1885. return WSAENOPROTOOPT;
  1886. }
  1887. if ( !context->KeepAlive && optionValue != 0 ) {
  1888. optionValue = TRUE;
  1889. //
  1890. // Keepalives are currently off and the application wants to
  1891. // turn them on. If the TDI connection object handle is
  1892. // NULL, then the socket is not yet connected. In this case
  1893. // we'll just remember that the keepalive option was set and
  1894. // actually turn them on in WSHNotify() after a connect()
  1895. // has completed on the socket.
  1896. //
  1897. if ( TdiConnectionObjectHandle != NULL ) {
  1898. error = SetTdiInformation(
  1899. TdiConnectionObjectHandle,
  1900. CO_TL_ENTITY,
  1901. INFO_CLASS_PROTOCOL,
  1902. INFO_TYPE_CONNECTION,
  1903. TCP_SOCKET_KEEPALIVE,
  1904. &optionValue,
  1905. sizeof(optionValue),
  1906. TRUE
  1907. );
  1908. if ( error != NO_ERROR ) {
  1909. return error;
  1910. }
  1911. }
  1912. //
  1913. // Remember that keepalives are enabled for this socket.
  1914. //
  1915. context->KeepAlive = TRUE;
  1916. } else if ( context->KeepAlive && optionValue == 0 ) {
  1917. //
  1918. // Keepalives are currently enabled and the application
  1919. // wants to turn them off. If the TDI connection object is
  1920. // NULL, the socket is not yet connected. In this case
  1921. // we'll just remember that keepalives are disabled.
  1922. //
  1923. if ( TdiConnectionObjectHandle != NULL ) {
  1924. error = SetTdiInformation(
  1925. TdiConnectionObjectHandle,
  1926. CO_TL_ENTITY,
  1927. INFO_CLASS_PROTOCOL,
  1928. INFO_TYPE_CONNECTION,
  1929. TCP_SOCKET_KEEPALIVE,
  1930. &optionValue,
  1931. sizeof(optionValue),
  1932. TRUE
  1933. );
  1934. if ( error != NO_ERROR ) {
  1935. return error;
  1936. }
  1937. }
  1938. //
  1939. // Remember that keepalives are disabled for this socket.
  1940. //
  1941. context->KeepAlive = FALSE;
  1942. }
  1943. break;
  1944. case SO_RCVBUF:
  1945. //
  1946. // If the receive buffer size is being changed, tell TCP about
  1947. // it. Do nothing if this is a datagram.
  1948. //
  1949. if ( context->ReceiveBufferSize == optionValue ||
  1950. IS_DGRAM_SOCK(context->SocketType)
  1951. ) {
  1952. break;
  1953. }
  1954. if ( TdiConnectionObjectHandle != NULL ) {
  1955. error = SetTdiInformation(
  1956. TdiConnectionObjectHandle,
  1957. CO_TL_ENTITY,
  1958. INFO_CLASS_PROTOCOL,
  1959. INFO_TYPE_CONNECTION,
  1960. TCP_SOCKET_WINDOW,
  1961. &optionValue,
  1962. sizeof(optionValue),
  1963. TRUE
  1964. );
  1965. if ( error != NO_ERROR ) {
  1966. return error;
  1967. }
  1968. }
  1969. context->ReceiveBufferSize = optionValue;
  1970. break;
  1971. default:
  1972. return WSAENOPROTOOPT;
  1973. }
  1974. return NO_ERROR;
  1975. } // WSHSetSocketInformation
  1976. INT
  1977. WSHEnumProtocols (
  1978. IN LPINT lpiProtocols,
  1979. IN LPWSTR lpTransportKeyName,
  1980. IN OUT LPVOID lpProtocolBuffer,
  1981. IN OUT LPDWORD lpdwBufferLength
  1982. )
  1983. /*++
  1984. Routine Description:
  1985. Enumerates the protocols supported by this helper.
  1986. Arguments:
  1987. lpiProtocols - Pointer to a NULL-terminated array of protocol
  1988. identifiers. Only protocols specified in this array will
  1989. be returned by this function. If this pointer is NULL,
  1990. all protocols are returned.
  1991. lpTransportKeyName -
  1992. lpProtocolBuffer - Pointer to a buffer to fill with PROTOCOL_INFO
  1993. structures.
  1994. lpdwBufferLength - Pointer to a variable that, on input, contains
  1995. the size of lpProtocolBuffer. On output, this value will be
  1996. updated with the size of the data actually written to the buffer.
  1997. Return Value:
  1998. INT - The number of protocols returned if successful, -1 if not.
  1999. --*/
  2000. {
  2001. DWORD bytesRequired;
  2002. PPROTOCOL_INFO tcpProtocolInfo;
  2003. PPROTOCOL_INFO udpProtocolInfo;
  2004. BOOL useTcp = FALSE;
  2005. BOOL useUdp = FALSE;
  2006. DWORD i;
  2007. lpTransportKeyName; // Avoid compiler warnings.
  2008. //
  2009. // Make sure that the caller cares about TCP and/or UDP.
  2010. //
  2011. if ( ARGUMENT_PRESENT( lpiProtocols ) ) {
  2012. for ( i = 0; lpiProtocols[i] != 0; i++ ) {
  2013. if ( lpiProtocols[i] == IPPROTO_TCP ) {
  2014. useTcp = TRUE;
  2015. }
  2016. if ( lpiProtocols[i] == IPPROTO_UDP ) {
  2017. useUdp = TRUE;
  2018. }
  2019. }
  2020. } else {
  2021. useTcp = TRUE;
  2022. useUdp = TRUE;
  2023. }
  2024. if ( !useTcp && !useUdp ) {
  2025. *lpdwBufferLength = 0;
  2026. return 0;
  2027. }
  2028. //
  2029. // Make sure that the caller has specified a sufficiently large
  2030. // buffer.
  2031. //
  2032. bytesRequired = (DWORD)((sizeof(PROTOCOL_INFO) * 2) +
  2033. ( (wcslen( TCP_NAME ) + 1) * sizeof(WCHAR)) +
  2034. ( (wcslen( UDP_NAME ) + 1) * sizeof(WCHAR)));
  2035. if ( bytesRequired > *lpdwBufferLength ) {
  2036. *lpdwBufferLength = bytesRequired;
  2037. return -1;
  2038. }
  2039. //
  2040. // Fill in TCP info, if requested.
  2041. //
  2042. if ( useTcp ) {
  2043. tcpProtocolInfo = lpProtocolBuffer;
  2044. tcpProtocolInfo->dwServiceFlags = XP_GUARANTEED_DELIVERY |
  2045. XP_GUARANTEED_ORDER |
  2046. XP_GRACEFUL_CLOSE |
  2047. XP_EXPEDITED_DATA |
  2048. XP_FRAGMENTATION;
  2049. tcpProtocolInfo->iAddressFamily = AF_INET6;
  2050. tcpProtocolInfo->iMaxSockAddr = sizeof(SOCKADDR_IN6);
  2051. tcpProtocolInfo->iMinSockAddr = sizeof(SOCKADDR_IN6);
  2052. tcpProtocolInfo->iSocketType = SOCK_STREAM;
  2053. tcpProtocolInfo->iProtocol = IPPROTO_TCP;
  2054. tcpProtocolInfo->dwMessageSize = 0;
  2055. tcpProtocolInfo->lpProtocol = (LPWSTR)
  2056. ( (PBYTE)lpProtocolBuffer + *lpdwBufferLength -
  2057. ( (wcslen( TCP_NAME ) + 1) * sizeof(WCHAR) ) );
  2058. wcscpy( tcpProtocolInfo->lpProtocol, TCP_NAME );
  2059. udpProtocolInfo = tcpProtocolInfo + 1;
  2060. udpProtocolInfo->lpProtocol = (LPWSTR)
  2061. ( (PBYTE)tcpProtocolInfo->lpProtocol -
  2062. ( (wcslen( UDP_NAME ) + 1) * sizeof(WCHAR) ) );
  2063. } else {
  2064. udpProtocolInfo = lpProtocolBuffer;
  2065. udpProtocolInfo->lpProtocol = (LPWSTR)
  2066. ( (PBYTE)lpProtocolBuffer + *lpdwBufferLength -
  2067. ( (wcslen( UDP_NAME ) + 1) * sizeof(WCHAR) ) );
  2068. }
  2069. //
  2070. // Fill in UDP info, if requested.
  2071. //
  2072. if ( useUdp ) {
  2073. udpProtocolInfo->dwServiceFlags = XP_CONNECTIONLESS |
  2074. XP_MESSAGE_ORIENTED |
  2075. XP_SUPPORTS_BROADCAST |
  2076. XP_SUPPORTS_MULTICAST |
  2077. XP_FRAGMENTATION;
  2078. udpProtocolInfo->iAddressFamily = AF_INET6;
  2079. udpProtocolInfo->iMaxSockAddr = sizeof(SOCKADDR_IN6);
  2080. udpProtocolInfo->iMinSockAddr = sizeof(SOCKADDR_IN6);
  2081. udpProtocolInfo->iSocketType = SOCK_DGRAM;
  2082. udpProtocolInfo->iProtocol = IPPROTO_UDP;
  2083. udpProtocolInfo->dwMessageSize = UDP_MESSAGE_SIZE;
  2084. wcscpy( udpProtocolInfo->lpProtocol, UDP_NAME );
  2085. }
  2086. *lpdwBufferLength = bytesRequired;
  2087. return (useTcp && useUdp) ? 2 : 1;
  2088. } // WSHEnumProtocols
  2089. BOOLEAN
  2090. IsTripleInList (
  2091. IN PMAPPING_TRIPLE List,
  2092. IN ULONG ListLength,
  2093. IN INT AddressFamily,
  2094. IN INT SocketType,
  2095. IN INT Protocol
  2096. )
  2097. /*++
  2098. Routine Description:
  2099. Determines whether the specified triple has an exact match in the
  2100. list of triples.
  2101. Arguments:
  2102. List - a list of triples (address family/socket type/protocol) to
  2103. search.
  2104. ListLength - the number of triples in the list.
  2105. AddressFamily - the address family to look for in the list.
  2106. SocketType - the socket type to look for in the list.
  2107. Protocol - the protocol to look for in the list.
  2108. Return Value:
  2109. BOOLEAN - TRUE if the triple was found in the list, false if not.
  2110. --*/
  2111. {
  2112. ULONG i;
  2113. //
  2114. // Walk through the list searching for an exact match.
  2115. //
  2116. for ( i = 0; i < ListLength; i++ ) {
  2117. //
  2118. // If all three elements of the triple match, return indicating
  2119. // that the triple did exist in the list.
  2120. //
  2121. if ( AddressFamily == List[i].AddressFamily &&
  2122. SocketType == List[i].SocketType &&
  2123. ( (Protocol == List[i].Protocol) || (SocketType == SOCK_RAW) )
  2124. ) {
  2125. return TRUE;
  2126. }
  2127. }
  2128. //
  2129. // The triple was not found in the list.
  2130. //
  2131. return FALSE;
  2132. } // IsTripleInList
  2133. INT
  2134. NtStatusToSocketError (
  2135. IN NTSTATUS Status
  2136. )
  2137. {
  2138. switch ( Status ) {
  2139. case STATUS_PENDING:
  2140. ASSERT (FALSE);
  2141. return WSASYSCALLFAILURE;
  2142. case STATUS_INVALID_HANDLE:
  2143. case STATUS_OBJECT_TYPE_MISMATCH:
  2144. return WSAENOTSOCK;
  2145. case STATUS_INSUFFICIENT_RESOURCES:
  2146. case STATUS_PAGEFILE_QUOTA:
  2147. case STATUS_COMMITMENT_LIMIT:
  2148. case STATUS_WORKING_SET_QUOTA:
  2149. case STATUS_NO_MEMORY:
  2150. case STATUS_CONFLICTING_ADDRESSES:
  2151. case STATUS_QUOTA_EXCEEDED:
  2152. case STATUS_TOO_MANY_PAGING_FILES:
  2153. case STATUS_REMOTE_RESOURCES:
  2154. case STATUS_TOO_MANY_ADDRESSES:
  2155. return WSAENOBUFS;
  2156. case STATUS_SHARING_VIOLATION:
  2157. case STATUS_ADDRESS_ALREADY_EXISTS:
  2158. return WSAEADDRINUSE;
  2159. case STATUS_LINK_TIMEOUT:
  2160. case STATUS_IO_TIMEOUT:
  2161. case STATUS_TIMEOUT:
  2162. return WSAETIMEDOUT;
  2163. case STATUS_GRACEFUL_DISCONNECT:
  2164. return WSAEDISCON;
  2165. case STATUS_REMOTE_DISCONNECT:
  2166. case STATUS_CONNECTION_RESET:
  2167. case STATUS_LINK_FAILED:
  2168. case STATUS_CONNECTION_DISCONNECTED:
  2169. case STATUS_PORT_UNREACHABLE:
  2170. return WSAECONNRESET;
  2171. case STATUS_LOCAL_DISCONNECT:
  2172. case STATUS_TRANSACTION_ABORTED:
  2173. case STATUS_CONNECTION_ABORTED:
  2174. return WSAECONNABORTED;
  2175. case STATUS_BAD_NETWORK_PATH:
  2176. case STATUS_NETWORK_UNREACHABLE:
  2177. case STATUS_PROTOCOL_UNREACHABLE:
  2178. return WSAENETUNREACH;
  2179. case STATUS_HOST_UNREACHABLE:
  2180. return WSAEHOSTUNREACH;
  2181. case STATUS_CANCELLED:
  2182. case STATUS_REQUEST_ABORTED:
  2183. return WSAEINTR;
  2184. case STATUS_BUFFER_OVERFLOW:
  2185. case STATUS_INVALID_BUFFER_SIZE:
  2186. return WSAEMSGSIZE;
  2187. case STATUS_BUFFER_TOO_SMALL:
  2188. case STATUS_ACCESS_VIOLATION:
  2189. return WSAEFAULT;
  2190. case STATUS_DEVICE_NOT_READY:
  2191. case STATUS_REQUEST_NOT_ACCEPTED:
  2192. return WSAEWOULDBLOCK;
  2193. case STATUS_INVALID_NETWORK_RESPONSE:
  2194. case STATUS_NETWORK_BUSY:
  2195. case STATUS_NO_SUCH_DEVICE:
  2196. case STATUS_NO_SUCH_FILE:
  2197. case STATUS_OBJECT_PATH_NOT_FOUND:
  2198. case STATUS_OBJECT_NAME_NOT_FOUND:
  2199. case STATUS_UNEXPECTED_NETWORK_ERROR:
  2200. return WSAENETDOWN;
  2201. case STATUS_INVALID_CONNECTION:
  2202. return WSAENOTCONN;
  2203. case STATUS_REMOTE_NOT_LISTENING:
  2204. case STATUS_CONNECTION_REFUSED:
  2205. return WSAECONNREFUSED;
  2206. case STATUS_PIPE_DISCONNECTED:
  2207. return WSAESHUTDOWN;
  2208. case STATUS_INVALID_ADDRESS:
  2209. case STATUS_INVALID_ADDRESS_COMPONENT:
  2210. return WSAEADDRNOTAVAIL;
  2211. case STATUS_NOT_SUPPORTED:
  2212. case STATUS_NOT_IMPLEMENTED:
  2213. return WSAEOPNOTSUPP;
  2214. case STATUS_ACCESS_DENIED:
  2215. return WSAEACCES;
  2216. default:
  2217. if ( NT_SUCCESS(Status) ) {
  2218. #if DBG
  2219. DbgPrint ("SockNtStatusToSocketError: success status %lx "
  2220. "not mapped\n", Status );
  2221. #endif
  2222. return NO_ERROR;
  2223. }
  2224. #if DBG
  2225. DbgPrint ("SockNtStatusToSocketError: unable to map 0x%lX, returning\n",
  2226. Status );
  2227. #endif
  2228. return WSAENOBUFS;
  2229. case STATUS_UNSUCCESSFUL:
  2230. case STATUS_INVALID_PARAMETER:
  2231. case STATUS_ADDRESS_CLOSED:
  2232. case STATUS_CONNECTION_INVALID:
  2233. case STATUS_ADDRESS_ALREADY_ASSOCIATED:
  2234. case STATUS_ADDRESS_NOT_ASSOCIATED:
  2235. case STATUS_CONNECTION_ACTIVE:
  2236. case STATUS_INVALID_DEVICE_STATE:
  2237. case STATUS_INVALID_DEVICE_REQUEST:
  2238. return WSAEINVAL;
  2239. }
  2240. } // NtStatusToSocketError
  2241. INT
  2242. SetTdiInformation (
  2243. IN HANDLE TdiConnectionObjectHandle,
  2244. IN ULONG Entity,
  2245. IN ULONG Class,
  2246. IN ULONG Type,
  2247. IN ULONG Id,
  2248. IN PVOID Value,
  2249. IN ULONG ValueLength,
  2250. IN BOOLEAN WaitForCompletion
  2251. )
  2252. /*++
  2253. Routine Description:
  2254. Performs a TDI action to the TCP/IP driver. A TDI action translates
  2255. into a streams T_OPTMGMT_REQ.
  2256. Arguments:
  2257. TdiConnectionObjectHandle - a TDI connection object on which to perform
  2258. the TDI action.
  2259. Entity - value to put in the tei_entity field of the TDIObjectID
  2260. structure.
  2261. Class - value to put in the toi_class field of the TDIObjectID
  2262. structure.
  2263. Type - value to put in the toi_type field of the TDIObjectID
  2264. structure.
  2265. Id - value to put in the toi_id field of the TDIObjectID structure.
  2266. Value - a pointer to a buffer to set as the information.
  2267. ValueLength - the length of the buffer.
  2268. WaitForCompletion - TRUE if we should wait for the TDI action to
  2269. complete, FALSE if we're at APC level and cannot do a wait.
  2270. Return Value:
  2271. INT - NO_ERROR, or a Windows Sockets error code.
  2272. --*/
  2273. {
  2274. NTSTATUS status;
  2275. PTCP_REQUEST_SET_INFORMATION_EX setInfoEx;
  2276. PIO_STATUS_BLOCK ioStatusBlock;
  2277. PVOID completionApc;
  2278. PVOID apcContext;
  2279. //
  2280. // Allocate space to hold the TDI set information buffers and the IO
  2281. // status block. These cannot be stack variables in case we must
  2282. // return before the operation is complete.
  2283. //
  2284. ioStatusBlock = HeapAlloc(GetProcessHeap(), 0,
  2285. sizeof(*ioStatusBlock) + sizeof(*setInfoEx) +
  2286. ValueLength
  2287. );
  2288. if ( ioStatusBlock == NULL ) {
  2289. return WSAENOBUFS;
  2290. }
  2291. //
  2292. // Initialize the TDI information buffers.
  2293. //
  2294. setInfoEx = (PTCP_REQUEST_SET_INFORMATION_EX)(ioStatusBlock + 1);
  2295. setInfoEx->ID.toi_entity.tei_entity = Entity;
  2296. setInfoEx->ID.toi_entity.tei_instance = TL_INSTANCE;
  2297. setInfoEx->ID.toi_class = Class;
  2298. setInfoEx->ID.toi_type = Type;
  2299. setInfoEx->ID.toi_id = Id;
  2300. CopyMemory( setInfoEx->Buffer, Value, ValueLength );
  2301. setInfoEx->BufferSize = ValueLength;
  2302. //
  2303. // If we need to wait for completion of the operation, create an
  2304. // event to wait on. If we can't wait for completion because we
  2305. // are being called at APC level, we'll use an APC routine to
  2306. // free the heap we allocated above.
  2307. //
  2308. if ( WaitForCompletion ) {
  2309. completionApc = NULL;
  2310. apcContext = NULL;
  2311. } else {
  2312. completionApc = CompleteTdiActionApc;
  2313. apcContext = ioStatusBlock;
  2314. }
  2315. //
  2316. // Make the actual TDI action call. The Streams TDI mapper will
  2317. // translate this into a TPI option management request for us and
  2318. // give it to TCP/IP.
  2319. //
  2320. status = NtDeviceIoControlFile(
  2321. TdiConnectionObjectHandle,
  2322. NULL,
  2323. completionApc,
  2324. apcContext,
  2325. ioStatusBlock,
  2326. IOCTL_TCP_WSH_SET_INFORMATION_EX,
  2327. setInfoEx,
  2328. sizeof(*setInfoEx) + ValueLength,
  2329. NULL,
  2330. 0
  2331. );
  2332. //
  2333. // If the call pended and we were supposed to wait for completion,
  2334. // then wait.
  2335. //
  2336. if ( status == STATUS_PENDING && WaitForCompletion ) {
  2337. while (ioStatusBlock->Status==STATUS_PENDING) {
  2338. LARGE_INTEGER timeout;
  2339. //
  2340. // Wait one millisecond
  2341. //
  2342. timeout.QuadPart = -1i64*1000i64*10i64;
  2343. NtDelayExecution (FALSE, &timeout);
  2344. }
  2345. status = ioStatusBlock->Status;
  2346. }
  2347. if ( WaitForCompletion || !NT_SUCCESS(status) ) {
  2348. RtlFreeHeap( RtlProcessHeap( ), 0, ioStatusBlock );
  2349. }
  2350. if (NT_SUCCESS (status)) {
  2351. return NO_ERROR;
  2352. }
  2353. else {
  2354. return NtStatusToSocketError (status);
  2355. }
  2356. return NO_ERROR;
  2357. } // SetTdiInformation
  2358. VOID
  2359. CompleteTdiActionApc (
  2360. IN PVOID ApcContext,
  2361. IN PIO_STATUS_BLOCK IoStatusBlock
  2362. )
  2363. {
  2364. //
  2365. // Just free the heap we allovcated to hold the IO status block and
  2366. // the TDI action buffer. There is nothing we can do if the call
  2367. // failed.
  2368. //
  2369. HeapFree(GetProcessHeap(), 0, ApcContext );
  2370. } // CompleteTdiActionApc
  2371. INT
  2372. WINAPI
  2373. WSHJoinLeaf (
  2374. IN PVOID HelperDllSocketContext,
  2375. IN SOCKET SocketHandle,
  2376. IN HANDLE TdiAddressObjectHandle,
  2377. IN HANDLE TdiConnectionObjectHandle,
  2378. IN PVOID LeafHelperDllSocketContext,
  2379. IN SOCKET LeafSocketHandle,
  2380. IN PSOCKADDR Sockaddr,
  2381. IN DWORD SockaddrLength,
  2382. IN LPWSABUF CallerData,
  2383. IN LPWSABUF CalleeData,
  2384. IN LPQOS SocketQOS,
  2385. IN LPQOS GroupQOS,
  2386. IN DWORD Flags
  2387. )
  2388. /*++
  2389. Routine Description:
  2390. Performs the protocol-dependent portion of creating a multicast
  2391. socket.
  2392. Arguments:
  2393. The following four parameters correspond to the socket passed into
  2394. the WSAJoinLeaf() API:
  2395. HelperDllSocketContext - The context pointer returned from
  2396. WSHOpenSocket().
  2397. SocketHandle - The handle of the socket used to establish the
  2398. multicast "session".
  2399. TdiAddressObjectHandle - The TDI address object of the socket, if
  2400. any. If the socket is not yet bound to an address, then
  2401. it does not have a TDI address object and this parameter
  2402. will be NULL.
  2403. TdiConnectionObjectHandle - The TDI connection object of the socket,
  2404. if any. If the socket is not yet connected, then it does not
  2405. have a TDI connection object and this parameter will be NULL.
  2406. The next two parameters correspond to the newly created socket that
  2407. identifies the multicast "session":
  2408. LeafHelperDllSocketContext - The context pointer returned from
  2409. WSHOpenSocket().
  2410. LeafSocketHandle - The handle of the socket that identifies the
  2411. multicast "session".
  2412. Sockaddr - The name of the peer to which the socket is to be joined.
  2413. SockaddrLength - The length of Sockaddr.
  2414. CallerData - Pointer to user data to be transferred to the peer
  2415. during multipoint session establishment.
  2416. CalleeData - Pointer to user data to be transferred back from
  2417. the peer during multipoint session establishment.
  2418. SocketQOS - Pointer to the flowspecs for SocketHandle, one in each
  2419. direction.
  2420. GroupQOS - Pointer to the flowspecs for the socket group, if any.
  2421. Flags - Flags to indicate if the socket is acting as sender,
  2422. receiver, or both.
  2423. Return Value:
  2424. INT - 0 if successful, a WinSock error code if not.
  2425. --*/
  2426. {
  2427. struct ipv6_mreq req;
  2428. INT err;
  2429. PWSHTCPIP_SOCKET_CONTEXT context;
  2430. //
  2431. // Quick sanity checks.
  2432. //
  2433. if( HelperDllSocketContext == NULL ||
  2434. SocketHandle == INVALID_SOCKET ||
  2435. TdiAddressObjectHandle == NULL ||
  2436. Sockaddr == NULL ||
  2437. Sockaddr->sa_family != AF_INET6 ||
  2438. SockaddrLength < sizeof(SOCKADDR_IN6) ||
  2439. ( CallerData != NULL && CallerData->len > 0 ) ||
  2440. ( CalleeData != NULL && CalleeData->len > 0 ) ||
  2441. SocketQOS != NULL ||
  2442. GroupQOS != NULL ) {
  2443. return WSAEINVAL;
  2444. }
  2445. //
  2446. // Add membership.
  2447. //
  2448. req.ipv6mr_multiaddr = ((LPSOCKADDR_IN6)Sockaddr)->sin6_addr;
  2449. req.ipv6mr_interface = 0;
  2450. err = SetTdiInformation(
  2451. TdiAddressObjectHandle,
  2452. CL_TL_ENTITY,
  2453. INFO_CLASS_PROTOCOL,
  2454. INFO_TYPE_ADDRESS_OBJECT,
  2455. AO_OPTION_ADD_MCAST,
  2456. &req,
  2457. sizeof(req),
  2458. TRUE
  2459. );
  2460. if( err == NO_ERROR ) {
  2461. //
  2462. // On NT4, we are called with a leaf socket.
  2463. // On NT5, the leaf socket is null.
  2464. //
  2465. if ((LeafHelperDllSocketContext != NULL) &&
  2466. (LeafSocketHandle != INVALID_SOCKET)) {
  2467. //
  2468. // Record this fact in the leaf socket so we can drop membership
  2469. // when the leaf socket is closed.
  2470. //
  2471. context = LeafHelperDllSocketContext;
  2472. context->MultipointLeaf = TRUE;
  2473. context->MultipointTarget = req.ipv6mr_multiaddr;
  2474. context->MultipointRootTdiAddressHandle = TdiAddressObjectHandle;
  2475. }
  2476. }
  2477. return err;
  2478. } // WSHJoinLeaf
  2479. INT
  2480. WINAPI
  2481. WSHGetWSAProtocolInfo (
  2482. IN LPWSTR ProviderName,
  2483. OUT LPWSAPROTOCOL_INFOW * ProtocolInfo,
  2484. OUT LPDWORD ProtocolInfoEntries
  2485. )
  2486. /*++
  2487. Routine Description:
  2488. Retrieves a pointer to the WSAPROTOCOL_INFOW structure(s) describing
  2489. the protocol(s) supported by this helper.
  2490. Arguments:
  2491. ProviderName - Contains the name of the provider, such as "TcpIp".
  2492. ProtocolInfo - Receives a pointer to the WSAPROTOCOL_INFOW array.
  2493. ProtocolInfoEntries - Receives the number of entries in the array.
  2494. Return Value:
  2495. INT - 0 if successful, WinSock error code if not.
  2496. --*/
  2497. {
  2498. if( ProviderName == NULL ||
  2499. ProtocolInfo == NULL ||
  2500. ProtocolInfoEntries == NULL ) {
  2501. return WSAEFAULT;
  2502. }
  2503. if( _wcsicmp( ProviderName, TCPIPV6_NAME ) == 0 ) {
  2504. *ProtocolInfo = Winsock2Protocols;
  2505. *ProtocolInfoEntries = NUM_WINSOCK2_PROTOCOLS;
  2506. return NO_ERROR;
  2507. }
  2508. return WSAEINVAL;
  2509. } // WSHGetWSAProtocolInfo
  2510. INT
  2511. WINAPI
  2512. WSHAddressToString (
  2513. IN LPSOCKADDR Address,
  2514. IN INT AddressLength,
  2515. IN LPWSAPROTOCOL_INFOW ProtocolInfo,
  2516. OUT LPWSTR AddressString,
  2517. IN OUT LPDWORD AddressStringLength
  2518. )
  2519. /*++
  2520. Routine Description:
  2521. Converts a SOCKADDR to a human-readable form.
  2522. Arguments:
  2523. Address - The SOCKADDR to convert.
  2524. AddressLength - The length of Address.
  2525. ProtocolInfo - The WSAPROTOCOL_INFOW for a particular provider.
  2526. AddressString - Receives the formatted address string.
  2527. AddressStringLength - On input, contains the length of AddressString.
  2528. On output, contains the number of characters actually written
  2529. to AddressString.
  2530. Return Value:
  2531. INT - 0 if successful, WinSock error code if not.
  2532. --*/
  2533. {
  2534. PSOCKADDR_IN6 addr;
  2535. //
  2536. // Quick sanity checks.
  2537. //
  2538. if ((Address == NULL) ||
  2539. (AddressLength < sizeof(SOCKADDR_IN6)) ||
  2540. (AddressString == NULL) ||
  2541. (AddressStringLength == NULL)) {
  2542. return WSAEFAULT;
  2543. }
  2544. addr = (PSOCKADDR_IN6)Address;
  2545. if (addr->sin6_family != AF_INET6) {
  2546. return WSAEINVAL;
  2547. }
  2548. if (!NT_SUCCESS(RtlIpv6AddressToStringExW(&addr->sin6_addr,
  2549. addr->sin6_scope_id,
  2550. addr->sin6_port,
  2551. AddressString,
  2552. AddressStringLength))) {
  2553. return WSAEINVAL;
  2554. }
  2555. return NO_ERROR;
  2556. } // WSHAddressToString
  2557. INT
  2558. WINAPI
  2559. WSHStringToAddress (
  2560. IN LPWSTR AddressString,
  2561. IN DWORD AddressFamily,
  2562. IN LPWSAPROTOCOL_INFOW ProtocolInfo,
  2563. OUT LPSOCKADDR Address,
  2564. IN OUT LPINT AddressLength
  2565. )
  2566. /*++
  2567. Routine Description:
  2568. Fills in a SOCKADDR structure by parsing a human-readable string.
  2569. The syntax is address%scope-id or [address%scope-id]:port, where
  2570. the scope-id and port are optional.
  2571. Note that since the IPv6 address format uses a varying number
  2572. of ':' characters, the IPv4 convention of address:port cannot
  2573. be supported without the braces.
  2574. Arguments:
  2575. AddressString - Points to the zero-terminated human-readable string.
  2576. AddressFamily - The address family to which the string belongs.
  2577. ProtocolInfo - The WSAPROTOCOL_INFOW for a particular provider.
  2578. Address - Receives the SOCKADDR structure.
  2579. AddressLength - On input, contains the length of Address. On output,
  2580. contains the number of bytes actually written to Address.
  2581. Return Value:
  2582. INT - 0 if successful, WinSock error code if not.
  2583. --*/
  2584. {
  2585. PSOCKADDR_IN6 addr;
  2586. //
  2587. // Quick sanity checks.
  2588. //
  2589. if ((AddressString == NULL) ||
  2590. (Address == NULL) ||
  2591. (AddressLength == NULL) ||
  2592. (*AddressLength < sizeof(SOCKADDR_IN6))) {
  2593. return WSAEFAULT;
  2594. }
  2595. if (AddressFamily != AF_INET6) {
  2596. return WSAEINVAL;
  2597. }
  2598. addr = (PSOCKADDR_IN6)Address;
  2599. ZeroMemory(Address, sizeof(SOCKADDR_IN6));
  2600. if (!NT_SUCCESS(RtlIpv6StringToAddressExW(AddressString,
  2601. &addr->sin6_addr,
  2602. &addr->sin6_scope_id,
  2603. &addr->sin6_port))) {
  2604. return WSAEINVAL;
  2605. }
  2606. addr->sin6_family = AF_INET6;
  2607. *AddressLength = sizeof(SOCKADDR_IN6);
  2608. return NO_ERROR;
  2609. } // WSHStringToAddress
  2610. INT
  2611. WINAPI
  2612. WSHGetProviderGuid (
  2613. IN LPWSTR ProviderName,
  2614. OUT LPGUID ProviderGuid
  2615. )
  2616. /*++
  2617. Routine Description:
  2618. Returns the GUID identifying the protocols supported by this helper.
  2619. Arguments:
  2620. ProviderName - Contains the name of the provider, such as "TcpIp".
  2621. ProviderGuid - Points to a buffer that receives the provider's GUID.
  2622. Return Value:
  2623. INT - 0 if successful, WinSock error code if not.
  2624. --*/
  2625. {
  2626. if( ProviderName == NULL ||
  2627. ProviderGuid == NULL ) {
  2628. return WSAEFAULT;
  2629. }
  2630. if( _wcsicmp( ProviderName, TCPIPV6_NAME ) == 0 ) {
  2631. CopyMemory(
  2632. ProviderGuid,
  2633. &IPv6ProviderGuid,
  2634. sizeof(GUID)
  2635. );
  2636. return NO_ERROR;
  2637. }
  2638. return WSAEINVAL;
  2639. } // WSHGetProviderGuid
  2640. INT
  2641. SortIPv6Addrs(
  2642. IN LPVOID InputBuffer,
  2643. IN DWORD InputBufferLength,
  2644. IN LPVOID OutputBuffer,
  2645. IN DWORD OutputBufferLength,
  2646. OUT LPDWORD NumberOfBytesReturned)
  2647. {
  2648. PBYTE pBuff = NULL;
  2649. PBYTE pDupIn = NULL;
  2650. TDI_ADDRESS_IP6 *pTDI;
  2651. SOCKET_ADDRESS_LIST *pIn = (SOCKET_ADDRESS_LIST *)InputBuffer;
  2652. SOCKET_ADDRESS_LIST *pOut = (SOCKET_ADDRESS_LIST *)OutputBuffer;
  2653. SOCKADDR_IN6 *pAddr6;
  2654. SOCKET_ADDRESS Addr;
  2655. HANDLE Handle = INVALID_HANDLE_VALUE;
  2656. DWORD *pKey, i, j, NumAddrsIn;
  2657. DWORD NumAddrsOut, InListLength;
  2658. u_long AddrListBytes;
  2659. int rc;
  2660. INT err = 0;
  2661. *NumberOfBytesReturned = 0;
  2662. // Make sure input buffer is big enough to contain a list
  2663. if (InputBufferLength < sizeof(SOCKET_ADDRESS_LIST)) {
  2664. return WSAEINVAL;
  2665. }
  2666. NumAddrsIn = pIn->iAddressCount;
  2667. // Make sure input buffer is actually big enough to hold the whole list
  2668. InListLength = (DWORD)FIELD_OFFSET(SOCKET_ADDRESS_LIST,Address[NumAddrsIn]);
  2669. if (InputBufferLength < InListLength) {
  2670. return WSAEINVAL;
  2671. }
  2672. do {
  2673. //
  2674. // Open a handle to the IPv6 device.
  2675. //
  2676. Handle = CreateFileW(WIN_IPV6_DEVICE_NAME,
  2677. 0, // access mode
  2678. FILE_SHARE_READ | FILE_SHARE_WRITE,
  2679. NULL, // security attributes
  2680. OPEN_EXISTING,
  2681. 0, // flags & attributes
  2682. NULL); // template file
  2683. if (Handle == INVALID_HANDLE_VALUE) {
  2684. //
  2685. // We can not sort the list.
  2686. //
  2687. err = WSASERVICE_NOT_FOUND;
  2688. break;
  2689. }
  2690. //
  2691. // Convert input to TDI list,
  2692. // with extra space for an array of indices
  2693. // following the array of addresses.
  2694. //
  2695. AddrListBytes = ALIGN_UP(NumAddrsIn * sizeof(TDI_ADDRESS_IP6), DWORD);
  2696. AddrListBytes += NumAddrsIn * sizeof(DWORD);
  2697. pBuff = HeapAlloc(GetProcessHeap(), 0, AddrListBytes);
  2698. if (!pBuff) {
  2699. err = WSAENOBUFS;
  2700. break;
  2701. }
  2702. pTDI = (TDI_ADDRESS_IP6 *)pBuff;
  2703. for (i=0; i<NumAddrsIn; i++) {
  2704. pAddr6 = (LPSOCKADDR_IN6)(pIn->Address[i].lpSockaddr);
  2705. // Make sure it's an IPv6 sockaddr
  2706. if (pAddr6->sin6_family != AF_INET6) {
  2707. err = WSAEINVAL;
  2708. break;
  2709. }
  2710. memcpy(&pTDI[i], &pAddr6->sin6_port, sizeof(TDI_ADDRESS_IP6));
  2711. }
  2712. if (err)
  2713. break;
  2714. rc = DeviceIoControl(Handle, IOCTL_IPV6_SORT_DEST_ADDRS,
  2715. pBuff, NumAddrsIn * sizeof(TDI_ADDRESS_IP6),
  2716. pBuff, AddrListBytes,
  2717. &AddrListBytes, NULL);
  2718. if (! rc) {
  2719. //
  2720. // We can not sort the list.
  2721. //
  2722. err = GetLastError();
  2723. break;
  2724. }
  2725. //
  2726. // There might be fewer addresses now.
  2727. //
  2728. NumAddrsOut = (AddrListBytes - NumAddrsIn * sizeof(TDI_ADDRESS_IP6))
  2729. / sizeof(DWORD);
  2730. //
  2731. // The key array starts after the address array.
  2732. //
  2733. pKey = (PDWORD)ALIGN_UP_POINTER(pBuff +
  2734. NumAddrsIn * sizeof(TDI_ADDRESS_IP6), DWORD);
  2735. *NumberOfBytesReturned = FIELD_OFFSET(SOCKET_ADDRESS_LIST,
  2736. Address[NumAddrsOut]);
  2737. if (OutputBufferLength < *NumberOfBytesReturned) {
  2738. err = WSAEFAULT;
  2739. break;
  2740. }
  2741. // First go and update all the scope ids
  2742. for (i=0; i<NumAddrsIn; i++) {
  2743. ((LPSOCKADDR_IN6)pIn->Address[i].lpSockaddr)->sin6_scope_id =
  2744. pTDI[i].sin6_scope_id;
  2745. }
  2746. // Make a copy of the input buffer in case we will overwrite it
  2747. if (pIn == pOut) {
  2748. pDupIn = HeapAlloc(GetProcessHeap(), 0, InListLength);
  2749. if (!pDupIn) {
  2750. err = WSAENOBUFS;
  2751. break;
  2752. }
  2753. CopyMemory(pDupIn, InputBuffer, InListLength );
  2754. pIn = (SOCKET_ADDRESS_LIST *)pDupIn;
  2755. }
  2756. // Now fill in the output sockaddr list
  2757. pOut->iAddressCount = NumAddrsOut;
  2758. for (i=0; i<NumAddrsOut; i++) {
  2759. pOut->Address[i] = pIn->Address[pKey[i]];
  2760. }
  2761. } while (FALSE);
  2762. if (pDupIn)
  2763. HeapFree(GetProcessHeap(), 0, pDupIn);
  2764. if (pBuff)
  2765. HeapFree(GetProcessHeap(), 0, pBuff);
  2766. if (Handle != INVALID_HANDLE_VALUE)
  2767. CloseHandle(Handle);
  2768. return err;
  2769. }
  2770. INT
  2771. WINAPI
  2772. WSHIoctl (
  2773. IN PVOID HelperDllSocketContext,
  2774. IN SOCKET SocketHandle,
  2775. IN HANDLE TdiAddressObjectHandle,
  2776. IN HANDLE TdiConnectionObjectHandle,
  2777. IN DWORD IoControlCode,
  2778. IN LPVOID InputBuffer,
  2779. IN DWORD InputBufferLength,
  2780. IN LPVOID OutputBuffer,
  2781. IN DWORD OutputBufferLength,
  2782. OUT LPDWORD NumberOfBytesReturned,
  2783. IN LPWSAOVERLAPPED Overlapped,
  2784. IN LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine,
  2785. OUT LPBOOL NeedsCompletion
  2786. )
  2787. /*++
  2788. Routine Description:
  2789. Performs queries & controls on the socket. This is basically an
  2790. "escape hatch" for IOCTLs not supported by MSAFD.DLL. Any unknown
  2791. IOCTLs are routed to the socket's helper DLL for protocol-specific
  2792. processing.
  2793. Arguments:
  2794. HelperDllSocketContext - the context pointer returned from
  2795. WSHOpenSocket().
  2796. SocketHandle - the handle of the socket for which we're controlling.
  2797. TdiAddressObjectHandle - the TDI address object of the socket, if
  2798. any. If the socket is not yet bound to an address, then
  2799. it does not have a TDI address object and this parameter
  2800. will be NULL.
  2801. TdiConnectionObjectHandle - the TDI connection object of the socket,
  2802. if any. If the socket is not yet connected, then it does not
  2803. have a TDI connection object and this parameter will be NULL.
  2804. IoControlCode - Control code of the operation to perform.
  2805. InputBuffer - Address of the input buffer.
  2806. InputBufferLength - The length of InputBuffer.
  2807. OutputBuffer - Address of the output buffer.
  2808. OutputBufferLength - The length of OutputBuffer.
  2809. NumberOfBytesReturned - Receives the number of bytes actually written
  2810. to the output buffer.
  2811. Overlapped - Pointer to a WSAOVERLAPPED structure for overlapped
  2812. operations.
  2813. CompletionRoutine - Pointer to a completion routine to call when
  2814. the operation is completed.
  2815. NeedsCompletion - WSAIoctl() can be overlapped, with all the gory
  2816. details that involves, such as setting events, queuing completion
  2817. routines, and posting to IO completion ports. Since the majority
  2818. of the IOCTL codes can be completed quickly "in-line", MSAFD.DLL
  2819. can optionally perform the overlapped completion of the operation.
  2820. Setting *NeedsCompletion to TRUE (the default) causes MSAFD.DLL
  2821. to handle all of the IO completion details iff this is an
  2822. overlapped operation on an overlapped socket.
  2823. Setting *NeedsCompletion to FALSE tells MSAFD.DLL to take no
  2824. further action because the helper DLL will perform any necessary
  2825. IO completion.
  2826. Note that if a helper performs its own IO completion, the helper
  2827. is responsible for maintaining the "overlapped" mode of the socket
  2828. at socket creation time and NOT performing overlapped IO completion
  2829. on non-overlapped sockets.
  2830. Return Value:
  2831. INT - 0 if successful, WinSock error code if not.
  2832. --*/
  2833. {
  2834. INT err;
  2835. NTSTATUS status;
  2836. //
  2837. // Quick sanity checks.
  2838. //
  2839. if( HelperDllSocketContext == NULL ||
  2840. SocketHandle == INVALID_SOCKET ||
  2841. NumberOfBytesReturned == NULL ||
  2842. NeedsCompletion == NULL ) {
  2843. return WSAEINVAL;
  2844. }
  2845. *NeedsCompletion = TRUE;
  2846. switch( IoControlCode ) {
  2847. case SIO_MULTIPOINT_LOOPBACK :
  2848. err = WSHSetSocketInformation(
  2849. HelperDllSocketContext,
  2850. SocketHandle,
  2851. TdiAddressObjectHandle,
  2852. TdiConnectionObjectHandle,
  2853. IPPROTO_IPV6,
  2854. IPV6_MULTICAST_LOOP,
  2855. (PCHAR)InputBuffer,
  2856. (INT)InputBufferLength
  2857. );
  2858. break;
  2859. case SIO_MULTICAST_SCOPE :
  2860. err = WSHSetSocketInformation(
  2861. HelperDllSocketContext,
  2862. SocketHandle,
  2863. TdiAddressObjectHandle,
  2864. TdiConnectionObjectHandle,
  2865. IPPROTO_IPV6,
  2866. IPV6_MULTICAST_HOPS,
  2867. (PCHAR)InputBuffer,
  2868. (INT)InputBufferLength
  2869. );
  2870. break;
  2871. case SIO_ADDRESS_LIST_SORT:
  2872. err = SortIPv6Addrs(InputBuffer, InputBufferLength,
  2873. OutputBuffer, OutputBufferLength,
  2874. NumberOfBytesReturned);
  2875. break;
  2876. case SIO_KEEPALIVE_VALS: {
  2877. struct tcp_keepalive *optionval;
  2878. PWSHTCPIP_SOCKET_CONTEXT context = HelperDllSocketContext;
  2879. //
  2880. // Atempt to turn on or off keepalive sending, as necessary.
  2881. //
  2882. if ( IS_DGRAM_SOCK(context->SocketType) ) {
  2883. return WSAENOPROTOOPT;
  2884. }
  2885. if ( InputBufferLength != sizeof(struct tcp_keepalive) ) {
  2886. return WSAEINVAL;
  2887. }
  2888. optionval = (struct tcp_keepalive *)InputBuffer;
  2889. if (optionval->onoff != 0 ) {
  2890. //
  2891. // Application wants to turn the keepalive on and also give the
  2892. // relevant parameters for it. If the TDI connection object handle
  2893. // is NULL, then the socket is not yet connected. In this case
  2894. // we'll just remember that the keepalive option was set and
  2895. // actually turn them on in WSHNotify() after a connect()
  2896. // has completed on the socket.
  2897. //
  2898. if ( TdiConnectionObjectHandle != NULL ) {
  2899. err = SetTdiInformation(
  2900. TdiConnectionObjectHandle,
  2901. CO_TL_ENTITY,
  2902. INFO_CLASS_PROTOCOL,
  2903. INFO_TYPE_CONNECTION,
  2904. TCP_SOCKET_KEEPALIVE_VALS,
  2905. optionval,
  2906. InputBufferLength,
  2907. TRUE
  2908. );
  2909. if ( err != NO_ERROR ) {
  2910. return err;
  2911. }
  2912. }
  2913. //
  2914. // Remember that keepalives are enabled for this socket.
  2915. //
  2916. context->KeepAliveVals.onoff = TRUE;
  2917. context->KeepAliveVals.keepalivetime = optionval->keepalivetime;
  2918. context->KeepAliveVals.keepaliveinterval = optionval->keepaliveinterval;
  2919. } else if ( optionval->onoff == 0 ) {
  2920. //
  2921. // Application wants to turn keepalive off. If the TDI
  2922. // connection object is NULL, the socket is not yet
  2923. // connected. In this case we'll just remember that
  2924. // keepalives are disabled.
  2925. //
  2926. if ( TdiConnectionObjectHandle != NULL ) {
  2927. err = SetTdiInformation(
  2928. TdiConnectionObjectHandle,
  2929. CO_TL_ENTITY,
  2930. INFO_CLASS_PROTOCOL,
  2931. INFO_TYPE_CONNECTION,
  2932. TCP_SOCKET_KEEPALIVE_VALS,
  2933. optionval,
  2934. InputBufferLength,
  2935. TRUE
  2936. );
  2937. if ( err != NO_ERROR ) {
  2938. return err;
  2939. }
  2940. }
  2941. //
  2942. // Remember that keepalives are disabled for this socket.
  2943. //
  2944. context->KeepAliveVals.onoff = FALSE;
  2945. }
  2946. err = NO_ERROR;
  2947. break;
  2948. }
  2949. default :
  2950. err = WSAEINVAL;
  2951. break;
  2952. }
  2953. return err;
  2954. } // WSHIoctl