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.

1716 lines
40 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. wshclus.c
  5. Abstract:
  6. This module contains necessary routines for the Cluster Transport
  7. Windows Sockets Helper DLL. This DLL provides the transport-specific
  8. support necessary for the Windows Sockets DLL to use the Cluster
  9. Transport.
  10. This file is largely a clone of the TCP/IP helper code.
  11. Author:
  12. Mike Massa (mikemas) 21-Feb-1997
  13. Revision History:
  14. --*/
  15. #define UNICODE
  16. #include <nt.h>
  17. #include <ntrtl.h>
  18. #include <nturtl.h>
  19. #include <windows.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <wchar.h>
  23. #include <tdi.h>
  24. #include <winsock2.h>
  25. #include <wsahelp.h>
  26. #include <ws2spi.h>
  27. #include <basetyps.h>
  28. #include <nspapi.h>
  29. #include <nspapip.h>
  30. #include <wsclus.h>
  31. #include <clustdi.h>
  32. #include <clusdef.h>
  33. #include <ntddcnet.h>
  34. #define CDP_NAME L"CDP"
  35. #define IS_DGRAM_SOCK(type) ((type) == SOCK_DGRAM)
  36. //
  37. // Define valid flags for WSHOpenSocket2().
  38. //
  39. #define VALID_CDP_FLAGS (WSA_FLAG_OVERLAPPED)
  40. //
  41. // Structure and variables to define the triples supported by the
  42. // Cluster Transport. The first entry of each array is considered
  43. // the canonical triple for that socket type; the other entries are
  44. // synonyms for the first.
  45. //
  46. typedef struct _MAPPING_TRIPLE {
  47. INT AddressFamily;
  48. INT SocketType;
  49. INT Protocol;
  50. } MAPPING_TRIPLE, *PMAPPING_TRIPLE;
  51. MAPPING_TRIPLE CdpMappingTriples[] =
  52. { AF_CLUSTER, SOCK_DGRAM, CLUSPROTO_CDP,
  53. AF_CLUSTER, SOCK_DGRAM, 0,
  54. AF_CLUSTER, 0, CLUSPROTO_CDP,
  55. AF_UNSPEC, 0, CLUSPROTO_CDP,
  56. AF_UNSPEC, SOCK_DGRAM, CLUSPROTO_CDP
  57. };
  58. //
  59. // Winsock 2 WSAPROTOCOL_INFO structures for all supported protocols.
  60. //
  61. #define WINSOCK_SPI_VERSION 2
  62. #define CDP_MESSAGE_SIZE (65535-20-68)
  63. WSAPROTOCOL_INFOW Winsock2Protocols[] =
  64. {
  65. //
  66. // CDP
  67. //
  68. {
  69. XP1_CONNECTIONLESS // dwServiceFlags1
  70. | XP1_MESSAGE_ORIENTED
  71. | XP1_IFS_HANDLES,
  72. 0, // dwServiceFlags2
  73. 0, // dwServiceFlags3
  74. 0, // dwServiceFlags4
  75. PFL_MATCHES_PROTOCOL_ZERO // dwProviderFlags
  76. | PFL_HIDDEN,
  77. { // gProviderId
  78. 0, 0, 0,
  79. { 0, 0, 0, 0, 0, 0, 0, 0 }
  80. },
  81. 0, // dwCatalogEntryId
  82. { // ProtocolChain
  83. BASE_PROTOCOL, // ChainLen
  84. { 0, 0, 0, 0, 0, 0, 0 } // ChainEntries
  85. },
  86. WINSOCK_SPI_VERSION, // iVersion
  87. AF_CLUSTER, // iAddressFamily
  88. sizeof(SOCKADDR_CLUSTER), // iMaxSockAddr
  89. sizeof(SOCKADDR_CLUSTER), // iMinSockAddr
  90. SOCK_DGRAM, // iSocketType
  91. CLUSPROTO_CDP, // iProtocol
  92. 0, // iProtocolMaxOffset
  93. LITTLEENDIAN, // iNetworkByteOrder
  94. SECURITY_PROTOCOL_NONE, // iSecurityScheme
  95. CDP_MESSAGE_SIZE, // dwMessageSize
  96. 0, // dwProviderReserved
  97. L"MSAFD Cluster Datagram Protocol" // szProtocol
  98. }
  99. };
  100. #define NUM_WINSOCK2_PROTOCOLS \
  101. ( sizeof(Winsock2Protocols) / sizeof(Winsock2Protocols[0]) )
  102. //
  103. // The GUID identifying this provider.
  104. //
  105. GUID ClusnetProviderGuid = { /* 03614682-8c42-11d0-a8fc-00a0c9062993 */
  106. 0x03614682,
  107. 0x8c42,
  108. 0x11d0,
  109. {0x00, 0xa0, 0xc9, 0x06, 0x29, 0x93, 0x8c}
  110. };
  111. LPWSTR ClusnetProviderName = L"ClusNet";
  112. //
  113. // Forward declarations of internal routines.
  114. //
  115. BOOLEAN
  116. IsTripleInList (
  117. IN PMAPPING_TRIPLE List,
  118. IN ULONG ListLength,
  119. IN INT AddressFamily,
  120. IN INT SocketType,
  121. IN INT Protocol
  122. );
  123. NTSTATUS
  124. DoNtIoctl(
  125. HANDLE Handle,
  126. DWORD IoctlCode,
  127. PVOID Request,
  128. DWORD RequestSize,
  129. PVOID Response,
  130. PDWORD ResponseSize
  131. );
  132. //
  133. // The socket context structure for this DLL. Each open ClusNet socket
  134. // will have one of these context structures, which is used to maintain
  135. // information about the socket.
  136. //
  137. typedef struct _WSHSOCKET_CONTEXT {
  138. INT AddressFamily;
  139. INT SocketType;
  140. INT Protocol;
  141. INT ReceiveBufferSize;
  142. DWORD Flags;
  143. BOOLEAN IgnoreNodeState;
  144. } WSHSOCKET_CONTEXT, *PWSHSOCKET_CONTEXT;
  145. #define DEFAULT_RECEIVE_BUFFER_SIZE 8192
  146. BOOLEAN
  147. DllInitialize (
  148. IN PVOID DllHandle,
  149. IN ULONG Reason,
  150. IN PVOID Context OPTIONAL
  151. )
  152. {
  153. switch ( Reason ) {
  154. case DLL_PROCESS_ATTACH:
  155. //
  156. // We don't need to receive thread attach and detach
  157. // notifications, so disable them to help application
  158. // performance.
  159. //
  160. DisableThreadLibraryCalls( DllHandle );
  161. return TRUE;
  162. case DLL_THREAD_ATTACH:
  163. break;
  164. case DLL_PROCESS_DETACH:
  165. break;
  166. case DLL_THREAD_DETACH:
  167. break;
  168. }
  169. return TRUE;
  170. } // DllInitialize
  171. INT
  172. WSHGetSockaddrType (
  173. IN PSOCKADDR Sockaddr,
  174. IN DWORD SockaddrLength,
  175. OUT PSOCKADDR_INFO SockaddrInfo
  176. )
  177. /*++
  178. Routine Description:
  179. This routine parses a sockaddr to determine the type of the
  180. machine address and endpoint address portions of the sockaddr.
  181. This is called by the winsock DLL whenever it needs to interpret
  182. a sockaddr.
  183. Arguments:
  184. Sockaddr - a pointer to the sockaddr structure to evaluate.
  185. SockaddrLength - the number of bytes in the sockaddr structure.
  186. SockaddrInfo - a pointer to a structure that will receive information
  187. about the specified sockaddr.
  188. Return Value:
  189. INT - a winsock error code indicating the status of the operation, or
  190. NO_ERROR if the operation succeeded.
  191. --*/
  192. {
  193. UNALIGNED SOCKADDR_CLUSTER *sockaddr = (PSOCKADDR_CLUSTER)Sockaddr;
  194. ULONG i;
  195. //
  196. // Make sure that the address family is correct.
  197. //
  198. if ( sockaddr->sac_family != AF_CLUSTER ) {
  199. return WSAEAFNOSUPPORT;
  200. }
  201. //
  202. // Make sure that the length is correct.
  203. //
  204. if ( SockaddrLength < sizeof(SOCKADDR_CLUSTER) ) {
  205. return WSAEFAULT;
  206. }
  207. //
  208. // The address passed the tests, looks like a good address.
  209. // Determine the type of the address portion of the sockaddr.
  210. //
  211. if ( sockaddr->sac_node == CLUSADDR_ANY ) {
  212. SockaddrInfo->AddressInfo = SockaddrAddressInfoWildcard;
  213. } else {
  214. SockaddrInfo->AddressInfo = SockaddrAddressInfoNormal;
  215. }
  216. //
  217. // Determine the type of the port (endpoint) in the sockaddr.
  218. //
  219. if ( sockaddr->sac_port == 0 ) {
  220. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoWildcard;
  221. } else {
  222. SockaddrInfo->EndpointInfo = SockaddrEndpointInfoNormal;
  223. }
  224. //
  225. // Zero out the sin_reserved_mbz part of the address. We silently allow
  226. // nonzero values in this field.
  227. //
  228. sockaddr->sac_zero = 0;
  229. return NO_ERROR;
  230. } // WSHGetSockaddrType
  231. INT
  232. WSHGetSocketInformation (
  233. IN PVOID HelperDllSocketContext,
  234. IN SOCKET SocketHandle,
  235. IN HANDLE TdiAddressObjectHandle,
  236. IN HANDLE TdiConnectionObjectHandle,
  237. IN INT Level,
  238. IN INT OptionName,
  239. OUT PCHAR OptionValue,
  240. OUT PINT OptionLength
  241. )
  242. /*++
  243. Routine Description:
  244. This routine retrieves information about a socket for those socket
  245. options supported in this helper DLL. This routine is
  246. called by the winsock DLL when a level/option name combination is
  247. passed to getsockopt() that the winsock DLL does not understand.
  248. Arguments:
  249. HelperDllSocketContext - the context pointer returned from
  250. WSHOpenSocket().
  251. SocketHandle - the handle of the socket for which we're getting
  252. information.
  253. TdiAddressObjectHandle - the TDI address object of the socket, if
  254. any. If the socket is not yet bound to an address, then
  255. it does not have a TDI address object and this parameter
  256. will be NULL.
  257. TdiConnectionObjectHandle - the TDI connection object of the socket,
  258. if any. If the socket is not yet connected, then it does not
  259. have a TDI connection object and this parameter will be NULL.
  260. Level - the level parameter passed to getsockopt().
  261. OptionName - the optname parameter passed to getsockopt().
  262. OptionValue - the optval parameter passed to getsockopt().
  263. OptionLength - the optlen parameter passed to getsockopt().
  264. Return Value:
  265. INT - a winsock error code indicating the status of the operation, or
  266. NO_ERROR if the operation succeeded.
  267. --*/
  268. {
  269. PWSHSOCKET_CONTEXT context = HelperDllSocketContext;
  270. UNREFERENCED_PARAMETER( SocketHandle );
  271. UNREFERENCED_PARAMETER( TdiAddressObjectHandle );
  272. UNREFERENCED_PARAMETER( TdiConnectionObjectHandle );
  273. return WSAENOPROTOOPT;
  274. } // WSHGetSocketInformation
  275. INT
  276. WSHGetWildcardSockaddr (
  277. IN PVOID HelperDllSocketContext,
  278. OUT PSOCKADDR Sockaddr,
  279. OUT PINT SockaddrLength
  280. )
  281. /*++
  282. Routine Description:
  283. This routine returns a wildcard socket address. A wildcard address
  284. is one which will bind the socket to an endpoint of the transport's
  285. choosing. For the Cluster Network, a wildcard address has
  286. node ID == 0 and port = 0.
  287. Arguments:
  288. HelperDllSocketContext - the context pointer returned from
  289. WSHOpenSocket() for the socket for which we need a wildcard
  290. address.
  291. Sockaddr - points to a buffer which will receive the wildcard socket
  292. address.
  293. SockaddrLength - receives the length of the wioldcard sockaddr.
  294. Return Value:
  295. INT - a winsock error code indicating the status of the operation, or
  296. NO_ERROR if the operation succeeded.
  297. --*/
  298. {
  299. PSOCKADDR_CLUSTER ClusAddr = (PSOCKADDR_CLUSTER) Sockaddr;
  300. if ( *SockaddrLength < sizeof(SOCKADDR_CLUSTER) ) {
  301. return WSAEFAULT;
  302. }
  303. *SockaddrLength = sizeof(SOCKADDR_CLUSTER);
  304. ClusAddr->sac_family = AF_CLUSTER;
  305. ClusAddr->sac_port = 0;
  306. ClusAddr->sac_node = CLUSADDR_ANY;
  307. ClusAddr->sac_zero = 0;
  308. return NO_ERROR;
  309. } // WSAGetWildcardSockaddr
  310. DWORD
  311. WSHGetWinsockMapping (
  312. OUT PWINSOCK_MAPPING Mapping,
  313. IN DWORD MappingLength
  314. )
  315. /*++
  316. Routine Description:
  317. Returns the list of address family/socket type/protocol triples
  318. supported by this helper DLL.
  319. Arguments:
  320. Mapping - receives a pointer to a WINSOCK_MAPPING structure that
  321. describes the triples supported here.
  322. MappingLength - the length, in bytes, of the passed-in Mapping buffer.
  323. Return Value:
  324. DWORD - the length, in bytes, of a WINSOCK_MAPPING structure for this
  325. helper DLL. If the passed-in buffer is too small, the return
  326. value will indicate the size of a buffer needed to contain
  327. the WINSOCK_MAPPING structure.
  328. --*/
  329. {
  330. DWORD mappingLength;
  331. mappingLength = FIELD_OFFSET(WINSOCK_MAPPING, Mapping[0])
  332. + sizeof(CdpMappingTriples);
  333. //
  334. // If the passed-in buffer is too small, return the length needed
  335. // now without writing to the buffer. The caller should allocate
  336. // enough memory and call this routine again.
  337. //
  338. if ( mappingLength > MappingLength ) {
  339. return mappingLength;
  340. }
  341. //
  342. // Fill in the output mapping buffer with the list of triples
  343. // supported in this helper DLL.
  344. //
  345. Mapping->Rows = sizeof(CdpMappingTriples) / sizeof(CdpMappingTriples[0]);
  346. Mapping->Columns = sizeof(MAPPING_TRIPLE) / sizeof(DWORD);
  347. RtlMoveMemory(
  348. Mapping->Mapping,
  349. CdpMappingTriples,
  350. sizeof(CdpMappingTriples)
  351. );
  352. //
  353. // Return the number of bytes we wrote.
  354. //
  355. return mappingLength;
  356. } // WSHGetWinsockMapping
  357. INT
  358. WSHOpenSocket (
  359. IN OUT PINT AddressFamily,
  360. IN OUT PINT SocketType,
  361. IN OUT PINT Protocol,
  362. OUT PUNICODE_STRING TransportDeviceName,
  363. OUT PVOID *HelperDllSocketContext,
  364. OUT PDWORD NotificationEvents
  365. )
  366. {
  367. return WSHOpenSocket2(
  368. AddressFamily,
  369. SocketType,
  370. Protocol,
  371. 0, // Group
  372. 0, // Flags
  373. TransportDeviceName,
  374. HelperDllSocketContext,
  375. NotificationEvents
  376. );
  377. } // WSHOpenSocket
  378. INT
  379. WSHOpenSocket2 (
  380. IN OUT PINT AddressFamily,
  381. IN OUT PINT SocketType,
  382. IN OUT PINT Protocol,
  383. IN GROUP Group,
  384. IN DWORD Flags,
  385. OUT PUNICODE_STRING TransportDeviceName,
  386. OUT PVOID *HelperDllSocketContext,
  387. OUT PDWORD NotificationEvents
  388. )
  389. /*++
  390. Routine Description:
  391. Does the necessary work for this helper DLL to open a socket and is
  392. called by the winsock DLL in the socket() routine. This routine
  393. verifies that the specified triple is valid, determines the NT
  394. device name of the TDI provider that will support that triple,
  395. allocates space to hold the socket's context block, and
  396. canonicalizes the triple.
  397. Arguments:
  398. AddressFamily - on input, the address family specified in the
  399. socket() call. On output, the canonicalized value for the
  400. address family.
  401. SocketType - on input, the socket type specified in the socket()
  402. call. On output, the canonicalized value for the socket type.
  403. Protocol - on input, the protocol specified in the socket() call.
  404. On output, the canonicalized value for the protocol.
  405. Group - Identifies the group for the new socket.
  406. Flags - Zero or more WSA_FLAG_* flags as passed into WSASocket().
  407. TransportDeviceName - receives the name of the TDI provider that
  408. will support the specified triple.
  409. HelperDllSocketContext - receives a context pointer that the winsock
  410. DLL will return to this helper DLL on future calls involving
  411. this socket.
  412. NotificationEvents - receives a bitmask of those state transitions
  413. this helper DLL should be notified on.
  414. Return Value:
  415. INT - a winsock error code indicating the status of the operation, or
  416. NO_ERROR if the operation succeeded.
  417. --*/
  418. {
  419. PWSHSOCKET_CONTEXT context;
  420. if ( IsTripleInList(
  421. CdpMappingTriples,
  422. sizeof(CdpMappingTriples) / sizeof(CdpMappingTriples[0]),
  423. *AddressFamily,
  424. *SocketType,
  425. *Protocol ) ) {
  426. //
  427. // It's a CDP socket. Check the flags.
  428. //
  429. if( (Flags & ~VALID_CDP_FLAGS ) != 0) {
  430. return WSAEINVAL;
  431. }
  432. //
  433. // Return the canonical form of a CDP socket triple.
  434. //
  435. *AddressFamily = CdpMappingTriples[0].AddressFamily;
  436. *SocketType = CdpMappingTriples[0].SocketType;
  437. *Protocol = CdpMappingTriples[0].Protocol;
  438. //
  439. // Indicate the name of the TDI device that will service
  440. // SOCK_DGRAM sockets in the cluster address family.
  441. //
  442. RtlInitUnicodeString( TransportDeviceName, DD_CDP_DEVICE_NAME );
  443. } else {
  444. //
  445. // This should never happen if the registry information about this
  446. // helper DLL is correct. If somehow this did happen, just return
  447. // an error.
  448. //
  449. return WSAEINVAL;
  450. }
  451. //
  452. // Allocate context for this socket. The Windows Sockets DLL will
  453. // return this value to us when it asks us to get/set socket options.
  454. //
  455. context = RtlAllocateHeap( RtlProcessHeap( ), 0, sizeof(*context) );
  456. if ( context == NULL ) {
  457. return WSAENOBUFS;
  458. }
  459. //
  460. // Initialize the context for the socket.
  461. //
  462. context->AddressFamily = *AddressFamily;
  463. context->SocketType = *SocketType;
  464. context->Protocol = *Protocol;
  465. context->ReceiveBufferSize = DEFAULT_RECEIVE_BUFFER_SIZE;
  466. context->Flags = Flags;
  467. context->IgnoreNodeState = FALSE;
  468. //
  469. // Tell the Windows Sockets DLL which state transitions we're
  470. // interested in being notified of.
  471. if (*SocketType == SOCK_DGRAM) {
  472. *NotificationEvents = WSH_NOTIFY_CLOSE | WSH_NOTIFY_BIND;
  473. }
  474. //
  475. // Everything worked, return success.
  476. //
  477. *HelperDllSocketContext = context;
  478. return NO_ERROR;
  479. } // WSHOpenSocket2
  480. INT
  481. WSHNotify (
  482. IN PVOID HelperDllSocketContext,
  483. IN SOCKET SocketHandle,
  484. IN HANDLE TdiAddressObjectHandle,
  485. IN HANDLE TdiConnectionObjectHandle,
  486. IN DWORD NotifyEvent
  487. )
  488. /*++
  489. Routine Description:
  490. This routine is called by the winsock DLL after a state transition
  491. of the socket. Only state transitions returned in the
  492. NotificationEvents parameter of WSHOpenSocket() are notified here.
  493. This routine allows a winsock helper DLL to track the state of
  494. socket and perform necessary actions corresponding to state
  495. transitions.
  496. Arguments:
  497. HelperDllSocketContext - the context pointer given to the winsock
  498. DLL by WSHOpenSocket().
  499. SocketHandle - the handle for the socket.
  500. TdiAddressObjectHandle - the TDI address object of the socket, if
  501. any. If the socket is not yet bound to an address, then
  502. it does not have a TDI address object and this parameter
  503. will be NULL.
  504. TdiConnectionObjectHandle - the TDI connection object of the socket,
  505. if any. If the socket is not yet connected, then it does not
  506. have a TDI connection object and this parameter will be NULL.
  507. NotifyEvent - indicates the state transition for which we're being
  508. called.
  509. Return Value:
  510. INT - a winsock error code indicating the status of the operation, or
  511. NO_ERROR if the operation succeeded.
  512. --*/
  513. {
  514. PWSHSOCKET_CONTEXT context = HelperDllSocketContext;
  515. INT err;
  516. if ( NotifyEvent == WSH_NOTIFY_CLOSE ) {
  517. //
  518. // Free the socket context.
  519. //
  520. RtlFreeHeap( RtlProcessHeap( ), 0, context );
  521. } else if ( NotifyEvent == WSH_NOTIFY_BIND ) {
  522. ULONG true = TRUE;
  523. if ( context->IgnoreNodeState ) {
  524. ULONG responseSize = 0;
  525. NTSTATUS status;
  526. status = DoNtIoctl(
  527. TdiAddressObjectHandle,
  528. IOCTL_CX_IGNORE_NODE_STATE,
  529. NULL,
  530. 0,
  531. NULL,
  532. &responseSize
  533. );
  534. if( !NT_SUCCESS(status)) {
  535. return(WSAENOPROTOOPT); // SWAG
  536. }
  537. }
  538. }
  539. else {
  540. return WSAEINVAL;
  541. }
  542. return NO_ERROR;
  543. } // WSHNotify
  544. INT
  545. WSHSetSocketInformation (
  546. IN PVOID HelperDllSocketContext,
  547. IN SOCKET SocketHandle,
  548. IN HANDLE TdiAddressObjectHandle,
  549. IN HANDLE TdiConnectionObjectHandle,
  550. IN INT Level,
  551. IN INT OptionName,
  552. IN PCHAR OptionValue,
  553. IN INT OptionLength
  554. )
  555. /*++
  556. Routine Description:
  557. This routine sets information about a socket for those socket
  558. options supported in this helper DLL. This routine is
  559. called by the winsock DLL when a level/option name combination is
  560. passed to setsockopt() that the winsock DLL does not understand.
  561. Arguments:
  562. HelperDllSocketContext - the context pointer returned from
  563. WSHOpenSocket().
  564. SocketHandle - the handle of the socket for which we're getting
  565. information.
  566. TdiAddressObjectHandle - the TDI address object of the socket, if
  567. any. If the socket is not yet bound to an address, then
  568. it does not have a TDI address object and this parameter
  569. will be NULL.
  570. TdiConnectionObjectHandle - the TDI connection object of the socket,
  571. if any. If the socket is not yet connected, then it does not
  572. have a TDI connection object and this parameter will be NULL.
  573. Level - the level parameter passed to setsockopt().
  574. OptionName - the optname parameter passed to setsockopt().
  575. OptionValue - the optval parameter passed to setsockopt().
  576. OptionLength - the optlen parameter passed to setsockopt().
  577. Return Value:
  578. INT - a winsock error code indicating the status of the operation, or
  579. NO_ERROR if the operation succeeded.
  580. --*/
  581. {
  582. PWSHSOCKET_CONTEXT context = HelperDllSocketContext;
  583. INT error;
  584. INT optionValue;
  585. UNREFERENCED_PARAMETER( SocketHandle );
  586. UNREFERENCED_PARAMETER( TdiAddressObjectHandle );
  587. UNREFERENCED_PARAMETER( TdiConnectionObjectHandle );
  588. //
  589. // Check if this is an internal request for context information.
  590. //
  591. if ( Level == SOL_INTERNAL && OptionName == SO_CONTEXT ) {
  592. //
  593. // The Windows Sockets DLL is requesting that we set context
  594. // information for a new socket. If the new socket was
  595. // accept()'ed, then we have already been notified of the socket
  596. // and HelperDllSocketContext will be valid. If the new socket
  597. // was inherited or duped into this process, then this is our
  598. // first notification of the socket and HelperDllSocketContext
  599. // will be equal to NULL.
  600. //
  601. // Insure that the context information being passed to us is
  602. // sufficiently large.
  603. //
  604. if ( OptionLength < sizeof(*context) ) {
  605. return WSAEINVAL;
  606. }
  607. if ( HelperDllSocketContext == NULL ) {
  608. //
  609. // This is our notification that a socket handle was
  610. // inherited or duped into this process. Allocate a context
  611. // structure for the new socket.
  612. //
  613. context = RtlAllocateHeap(
  614. RtlProcessHeap( ),
  615. 0,
  616. sizeof(*context)
  617. );
  618. if ( context == NULL ) {
  619. return WSAENOBUFS;
  620. }
  621. //
  622. // Copy over information into the context block.
  623. //
  624. RtlCopyMemory( context, OptionValue, sizeof(*context) );
  625. //
  626. // Tell the Windows Sockets DLL where our context information is
  627. // stored so that it can return the context pointer in future
  628. // calls.
  629. //
  630. *(PWSHSOCKET_CONTEXT *)OptionValue = context;
  631. return NO_ERROR;
  632. }
  633. }
  634. return WSAENOPROTOOPT;
  635. } // WSHSetSocketInformation
  636. INT
  637. WSHEnumProtocols (
  638. IN LPINT lpiProtocols,
  639. IN LPWSTR lpTransportKeyName,
  640. IN OUT LPVOID lpProtocolBuffer,
  641. IN OUT LPDWORD lpdwBufferLength
  642. )
  643. /*++
  644. Routine Description:
  645. Enumerates the protocols supported by this helper.
  646. Arguments:
  647. lpiProtocols - Pointer to a NULL-terminated array of protocol
  648. identifiers. Only protocols specified in this array will
  649. be returned by this function. If this pointer is NULL,
  650. all protocols are returned.
  651. lpTransportKeyName -
  652. lpProtocolBuffer - Pointer to a buffer to fill with PROTOCOL_INFO
  653. structures.
  654. lpdwBufferLength - Pointer to a variable that, on input, contains
  655. the size of lpProtocolBuffer. On output, this value will be
  656. updated with the size of the data actually written to the buffer.
  657. Return Value:
  658. INT - The number of protocols returned if successful, -1 if not.
  659. --*/
  660. {
  661. DWORD bytesRequired;
  662. PPROTOCOL_INFO CdpProtocolInfo;
  663. BOOL useCdp = FALSE;
  664. DWORD i;
  665. UNREFERENCED_PARAMETER(lpTransportKeyName);
  666. //
  667. // Make sure that the caller cares about CDP.
  668. //
  669. if ( ARGUMENT_PRESENT( lpiProtocols ) ) {
  670. for ( i = 0; lpiProtocols[i] != 0; i++ ) {
  671. if ( lpiProtocols[i] == CLUSPROTO_CDP ) {
  672. useCdp = TRUE;
  673. }
  674. }
  675. } else {
  676. useCdp = TRUE;
  677. }
  678. if ( !useCdp ) {
  679. *lpdwBufferLength = 0;
  680. return 0;
  681. }
  682. //
  683. // Make sure that the caller has specified a sufficiently large
  684. // buffer.
  685. //
  686. bytesRequired = (DWORD)((sizeof(PROTOCOL_INFO) * 1) +
  687. ( (wcslen( CDP_NAME ) + 1) * sizeof(WCHAR)));
  688. if ( bytesRequired > *lpdwBufferLength ) {
  689. *lpdwBufferLength = bytesRequired;
  690. return -1;
  691. }
  692. //
  693. // Fill in CDP info, if requested.
  694. //
  695. if ( useCdp ) {
  696. CdpProtocolInfo = lpProtocolBuffer;
  697. CdpProtocolInfo->lpProtocol = (LPWSTR)
  698. ( (PBYTE)lpProtocolBuffer + *lpdwBufferLength -
  699. ( (wcslen( CDP_NAME ) + 1) * sizeof(WCHAR) ) );
  700. CdpProtocolInfo->dwServiceFlags = XP_CONNECTIONLESS |
  701. XP_MESSAGE_ORIENTED;
  702. CdpProtocolInfo->iAddressFamily = AF_CLUSTER;
  703. CdpProtocolInfo->iMaxSockAddr = sizeof(SOCKADDR_CLUSTER);
  704. CdpProtocolInfo->iMinSockAddr = sizeof(SOCKADDR_CLUSTER);
  705. CdpProtocolInfo->iSocketType = SOCK_DGRAM;
  706. CdpProtocolInfo->iProtocol = CLUSPROTO_CDP;
  707. CdpProtocolInfo->dwMessageSize = CDP_MESSAGE_SIZE;
  708. wcscpy( CdpProtocolInfo->lpProtocol, CDP_NAME );
  709. }
  710. *lpdwBufferLength = bytesRequired;
  711. return 1;
  712. } // WSHEnumProtocols
  713. BOOLEAN
  714. IsTripleInList (
  715. IN PMAPPING_TRIPLE List,
  716. IN ULONG ListLength,
  717. IN INT AddressFamily,
  718. IN INT SocketType,
  719. IN INT Protocol
  720. )
  721. /*++
  722. Routine Description:
  723. Determines whether the specified triple has an exact match in the
  724. list of triples.
  725. Arguments:
  726. List - a list of triples (address family/socket type/protocol) to
  727. search.
  728. ListLength - the number of triples in the list.
  729. AddressFamily - the address family to look for in the list.
  730. SocketType - the socket type to look for in the list.
  731. Protocol - the protocol to look for in the list.
  732. Return Value:
  733. BOOLEAN - TRUE if the triple was found in the list, false if not.
  734. --*/
  735. {
  736. ULONG i;
  737. //
  738. // Walk through the list searching for an exact match.
  739. //
  740. for ( i = 0; i < ListLength; i++ ) {
  741. //
  742. // If all three elements of the triple match, return indicating
  743. // that the triple did exist in the list.
  744. //
  745. if ( AddressFamily == List[i].AddressFamily &&
  746. SocketType == List[i].SocketType &&
  747. Protocol == List[i].Protocol
  748. ) {
  749. return TRUE;
  750. }
  751. }
  752. //
  753. // The triple was not found in the list.
  754. //
  755. return FALSE;
  756. } // IsTripleInList
  757. #if 0
  758. INT
  759. WINAPI
  760. WSHGetBroadcastSockaddr (
  761. IN PVOID HelperDllSocketContext,
  762. OUT PSOCKADDR Sockaddr,
  763. OUT PINT SockaddrLength
  764. )
  765. /*++
  766. Routine Description:
  767. This routine returns a broadcast socket address. A broadcast address
  768. may be used as a destination for the sendto() API to send a datagram
  769. to all interested clients.
  770. Arguments:
  771. HelperDllSocketContext - the context pointer returned from
  772. WSHOpenSocket() for the socket for which we need a broadcast
  773. address.
  774. Sockaddr - points to a buffer which will receive the broadcast socket
  775. address.
  776. SockaddrLength - receives the length of the broadcast sockaddr.
  777. Return Value:
  778. INT - a winsock error code indicating the status of the operation, or
  779. NO_ERROR if the operation succeeded.
  780. --*/
  781. {
  782. LPSOCKADDR_CLUSTER addr;
  783. if( *SockaddrLength < sizeof(SOCKADDR_CLUSTER) ) {
  784. return WSAEFAULT;
  785. }
  786. *SockaddrLength = sizeof(SOCKADDR_CLUSTER);
  787. //
  788. // Build the broadcast address.
  789. //
  790. addr = (LPSOCKADDR_CLUSTER)Sockaddr;
  791. RtlZeroMemory( addr, sizeof(*addr));
  792. addr->sac_family = AF_CLUSTER;
  793. addr->sac_node = CLUSADDR_BROADCAST;
  794. return NO_ERROR;
  795. } // WSAGetBroadcastSockaddr
  796. #endif // 0
  797. INT
  798. WINAPI
  799. WSHGetWSAProtocolInfo (
  800. IN LPWSTR ProviderName,
  801. OUT LPWSAPROTOCOL_INFOW * ProtocolInfo,
  802. OUT LPDWORD ProtocolInfoEntries
  803. )
  804. /*++
  805. Routine Description:
  806. Retrieves a pointer to the WSAPROTOCOL_INFOW structure(s) describing
  807. the protocol(s) supported by this helper.
  808. Arguments:
  809. ProviderName - Contains the name of the provider, such as "TcpIp".
  810. ProtocolInfo - Receives a pointer to the WSAPROTOCOL_INFOW array.
  811. ProtocolInfoEntries - Receives the number of entries in the array.
  812. Return Value:
  813. INT - 0 if successful, WinSock error code if not.
  814. --*/
  815. {
  816. if( ProviderName == NULL ||
  817. ProtocolInfo == NULL ||
  818. ProtocolInfoEntries == NULL ) {
  819. return WSAEFAULT;
  820. }
  821. if( _wcsicmp( ProviderName, ClusnetProviderName ) == 0 ) {
  822. *ProtocolInfo = Winsock2Protocols;
  823. *ProtocolInfoEntries = NUM_WINSOCK2_PROTOCOLS;
  824. return NO_ERROR;
  825. }
  826. return WSAEINVAL;
  827. } // WSHGetWSAProtocolInfo
  828. INT
  829. WINAPI
  830. WSHAddressToString (
  831. IN LPSOCKADDR Address,
  832. IN INT AddressLength,
  833. IN LPWSAPROTOCOL_INFOW ProtocolInfo,
  834. OUT LPWSTR AddressString,
  835. IN OUT LPDWORD AddressStringLength
  836. )
  837. /*++
  838. Routine Description:
  839. Converts a SOCKADDR to a human-readable form.
  840. Arguments:
  841. Address - The SOCKADDR to convert.
  842. AddressLength - The length of Address.
  843. ProtocolInfo - The WSAPROTOCOL_INFOW for a particular provider.
  844. AddressString - Receives the formatted address string.
  845. AddressStringLength - On input, contains the length of AddressString.
  846. On output, contains the number of characters actually written
  847. to AddressString.
  848. Return Value:
  849. INT - 0 if successful, WinSock error code if not.
  850. --*/
  851. {
  852. WCHAR string[32];
  853. INT length;
  854. LPSOCKADDR_CLUSTER addr;
  855. //
  856. // Quick sanity checks.
  857. //
  858. if( Address == NULL ||
  859. AddressLength < sizeof(SOCKADDR_CLUSTER) ||
  860. AddressString == NULL ||
  861. AddressStringLength == NULL ) {
  862. return WSAEFAULT;
  863. }
  864. addr = (LPSOCKADDR_CLUSTER)Address;
  865. if( addr->sac_family != AF_CLUSTER ) {
  866. return WSAEINVAL;
  867. }
  868. //
  869. // Do the converstion.
  870. //
  871. length = wsprintfW(string, L"%u", addr->sac_node);
  872. length += wsprintfW(string + length, L":%u", addr->sac_port);
  873. length++; // account for terminator
  874. if( *AddressStringLength < (DWORD)length ) {
  875. return WSAEFAULT;
  876. }
  877. *AddressStringLength = (DWORD)length;
  878. RtlCopyMemory(
  879. AddressString,
  880. string,
  881. length * sizeof(WCHAR)
  882. );
  883. return NO_ERROR;
  884. } // WSHAddressToString
  885. INT
  886. WINAPI
  887. WSHStringToAddress (
  888. IN LPWSTR AddressString,
  889. IN DWORD AddressFamily,
  890. IN LPWSAPROTOCOL_INFOW ProtocolInfo,
  891. OUT LPSOCKADDR Address,
  892. IN OUT LPINT AddressLength
  893. )
  894. /*++
  895. Routine Description:
  896. Fills in a SOCKADDR structure by parsing a human-readable string.
  897. Arguments:
  898. AddressString - Points to the zero-terminated human-readable string.
  899. AddressFamily - The address family to which the string belongs.
  900. ProtocolInfo - The WSAPROTOCOL_INFOW for a particular provider.
  901. Address - Receives the SOCKADDR structure.
  902. AddressLength - On input, contains the length of Address. On output,
  903. contains the number of bytes actually written to Address.
  904. Return Value:
  905. INT - 0 if successful, WinSock error code if not.
  906. --*/
  907. {
  908. LPWSTR terminator;
  909. WCHAR ch;
  910. USHORT base;
  911. USHORT port;
  912. ULONG node;
  913. LPSOCKADDR_CLUSTER addr;
  914. //
  915. // Quick sanity checks.
  916. //
  917. if( AddressString == NULL ||
  918. *AddressString == UNICODE_NULL ||
  919. Address == NULL ||
  920. AddressLength == NULL ||
  921. *AddressLength < sizeof(SOCKADDR_CLUSTER) ) {
  922. return WSAEFAULT;
  923. }
  924. if( AddressFamily != AF_CLUSTER ) {
  925. return WSAEINVAL;
  926. }
  927. //
  928. // Convert it. The format is node:port
  929. //
  930. node = 0;
  931. base = 10;
  932. terminator = AddressString;
  933. if( *terminator == L'0' ) {
  934. base = 8;
  935. terminator++;
  936. if( *terminator == UNICODE_NULL ) {
  937. return(WSAEINVAL);
  938. }
  939. if ( *terminator == L'x' ) {
  940. base = 16;
  941. terminator++;
  942. }
  943. }
  944. while( (ch = *terminator++) != L':' ) {
  945. if( iswdigit(ch) ) {
  946. node = ( node * base ) + ( ch - L'0' );
  947. } else if( base == 16 && iswxdigit(ch) ) {
  948. node = ( node << 4 );
  949. node += ch + 10 - ( iswlower(ch) ? L'a' : L'A' );
  950. } else {
  951. return WSAEINVAL;
  952. }
  953. }
  954. port = 0;
  955. base = 10;
  956. if( *terminator == L'0' ) {
  957. base = 8;
  958. terminator++;
  959. if( *terminator == UNICODE_NULL ) {
  960. return(WSAEINVAL);
  961. }
  962. if( *terminator == L'x' ) {
  963. base = 16;
  964. terminator++;
  965. }
  966. }
  967. while( (ch = *terminator++) != UNICODE_NULL ) {
  968. if( iswdigit(ch) ) {
  969. port = ( port * base ) + ( ch - L'0' );
  970. } else if( base == 16 && iswxdigit(ch) ) {
  971. port = ( port << 4 );
  972. port += ch + 10 - ( iswlower(ch) ? L'a' : L'A' );
  973. } else {
  974. return WSAEINVAL;
  975. }
  976. }
  977. //
  978. // Build the address.
  979. //
  980. RtlZeroMemory(
  981. Address,
  982. sizeof(SOCKADDR_CLUSTER)
  983. );
  984. addr = (LPSOCKADDR_CLUSTER)Address;
  985. *AddressLength = sizeof(SOCKADDR_CLUSTER);
  986. addr->sac_family = AF_CLUSTER;
  987. addr->sac_port = port;
  988. addr->sac_node = node;
  989. return NO_ERROR;
  990. } // WSHStringToAddress
  991. INT
  992. WINAPI
  993. WSHGetProviderGuid (
  994. IN LPWSTR ProviderName,
  995. OUT LPGUID ProviderGuid
  996. )
  997. /*++
  998. Routine Description:
  999. Returns the GUID identifying the protocols supported by this helper.
  1000. Arguments:
  1001. ProviderName - Contains the name of the provider, such as "TcpIp".
  1002. ProviderGuid - Points to a buffer that receives the provider's GUID.
  1003. Return Value:
  1004. INT - 0 if successful, WinSock error code if not.
  1005. --*/
  1006. {
  1007. if( ProviderName == NULL ||
  1008. ProviderGuid == NULL ) {
  1009. return WSAEFAULT;
  1010. }
  1011. if( _wcsicmp( ProviderName, ClusnetProviderName ) == 0 ) {
  1012. RtlCopyMemory(
  1013. ProviderGuid,
  1014. &ClusnetProviderGuid,
  1015. sizeof(GUID)
  1016. );
  1017. return NO_ERROR;
  1018. }
  1019. return WSAEINVAL;
  1020. } // WSHGetProviderGuid
  1021. INT
  1022. WINAPI
  1023. WSHIoctl (
  1024. IN PVOID HelperDllSocketContext,
  1025. IN SOCKET SocketHandle,
  1026. IN HANDLE TdiAddressObjectHandle,
  1027. IN HANDLE TdiConnectionObjectHandle,
  1028. IN DWORD IoControlCode,
  1029. IN LPVOID InputBuffer,
  1030. IN DWORD InputBufferLength,
  1031. IN LPVOID OutputBuffer,
  1032. IN DWORD OutputBufferLength,
  1033. OUT LPDWORD NumberOfBytesReturned,
  1034. IN LPWSAOVERLAPPED Overlapped,
  1035. IN LPWSAOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine,
  1036. OUT LPBOOL NeedsCompletion
  1037. )
  1038. /*++
  1039. Routine Description:
  1040. Performs queries & controls on the socket. This is basically an
  1041. "escape hatch" for IOCTLs not supported by MSAFD.DLL. Any unknown
  1042. IOCTLs are routed to the socket's helper DLL for protocol-specific
  1043. processing.
  1044. Arguments:
  1045. HelperDllSocketContext - the context pointer returned from
  1046. WSHOpenSocket().
  1047. SocketHandle - the handle of the socket for which we're controlling.
  1048. TdiAddressObjectHandle - the TDI address object of the socket, if
  1049. any. If the socket is not yet bound to an address, then
  1050. it does not have a TDI address object and this parameter
  1051. will be NULL.
  1052. TdiConnectionObjectHandle - the TDI connection object of the socket,
  1053. if any. If the socket is not yet connected, then it does not
  1054. have a TDI connection object and this parameter will be NULL.
  1055. IoControlCode - Control code of the operation to perform.
  1056. InputBuffer - Address of the input buffer.
  1057. InputBufferLength - The length of InputBuffer.
  1058. OutputBuffer - Address of the output buffer.
  1059. OutputBufferLength - The length of OutputBuffer.
  1060. NumberOfBytesReturned - Receives the number of bytes actually written
  1061. to the output buffer.
  1062. Overlapped - Pointer to a WSAOVERLAPPED structure for overlapped
  1063. operations.
  1064. CompletionRoutine - Pointer to a completion routine to call when
  1065. the operation is completed.
  1066. NeedsCompletion - WSAIoctl() can be overlapped, with all the gory
  1067. details that involves, such as setting events, queuing completion
  1068. routines, and posting to IO completion ports. Since the majority
  1069. of the IOCTL codes can be completed quickly "in-line", MSAFD.DLL
  1070. can optionally perform the overlapped completion of the operation.
  1071. Setting *NeedsCompletion to TRUE (the default) causes MSAFD.DLL
  1072. to handle all of the IO completion details iff this is an
  1073. overlapped operation on an overlapped socket.
  1074. Setting *NeedsCompletion to FALSE tells MSAFD.DLL to take no
  1075. further action because the helper DLL will perform any necessary
  1076. IO completion.
  1077. Note that if a helper performs its own IO completion, the helper
  1078. is responsible for maintaining the "overlapped" mode of the socket
  1079. at socket creation time and NOT performing overlapped IO completion
  1080. on non-overlapped sockets.
  1081. Return Value:
  1082. INT - 0 if successful, WinSock error code if not.
  1083. --*/
  1084. {
  1085. PWSHSOCKET_CONTEXT context = HelperDllSocketContext;
  1086. INT err;
  1087. NTSTATUS status;
  1088. //
  1089. // Quick sanity checks.
  1090. //
  1091. if( HelperDllSocketContext == NULL ||
  1092. SocketHandle == INVALID_SOCKET ||
  1093. NeedsCompletion == NULL ) {
  1094. return WSAEINVAL;
  1095. }
  1096. *NeedsCompletion = TRUE;
  1097. switch( IoControlCode ) {
  1098. case SIO_CLUS_IGNORE_NODE_STATE :
  1099. //
  1100. // This option is only valid for datagram sockets.
  1101. //
  1102. if ( !IS_DGRAM_SOCK(context->SocketType) ) {
  1103. return WSAENOPROTOOPT;
  1104. }
  1105. if( TdiAddressObjectHandle != NULL ) {
  1106. ULONG responseSize = 0;
  1107. NTSTATUS status;
  1108. status = DoNtIoctl(
  1109. TdiAddressObjectHandle,
  1110. IOCTL_CX_IGNORE_NODE_STATE,
  1111. NULL,
  1112. 0,
  1113. NULL,
  1114. &responseSize
  1115. );
  1116. if( NT_SUCCESS(status) ) {
  1117. err = NO_ERROR;
  1118. } else {
  1119. err = WSAENOPROTOOPT; // SWAG
  1120. }
  1121. }
  1122. else {
  1123. err = NO_ERROR;
  1124. }
  1125. context->IgnoreNodeState = TRUE;
  1126. break;
  1127. default :
  1128. err = WSAEINVAL;
  1129. break;
  1130. }
  1131. return err;
  1132. } // WSHIoctl
  1133. NTSTATUS
  1134. DoNtIoctl(
  1135. HANDLE Handle,
  1136. DWORD IoctlCode,
  1137. PVOID Request,
  1138. DWORD RequestSize,
  1139. PVOID Response,
  1140. PDWORD ResponseSize
  1141. )
  1142. /*++
  1143. Routine Description:
  1144. Packages and issues an ioctl.
  1145. Arguments:
  1146. Handle - An open file Handle on which to issue the request.
  1147. IoctlCode - The IOCTL opcode.
  1148. Request - A pointer to the input buffer.
  1149. RequestSize - Size of the input buffer.
  1150. Response - A pointer to the output buffer.
  1151. ResponseSize - On input, the size in bytes of the output buffer.
  1152. On output, the number of bytes returned in the output buffer.
  1153. Return Value:
  1154. NT Status Code.
  1155. --*/
  1156. {
  1157. IO_STATUS_BLOCK ioStatusBlock;
  1158. NTSTATUS status = 0xaabbccdd;
  1159. HANDLE event;
  1160. event = CreateEvent(NULL, FALSE, FALSE, NULL);
  1161. if (event == NULL) {
  1162. return(GetLastError());
  1163. }
  1164. ioStatusBlock.Information = 0;
  1165. status = NtDeviceIoControlFile(
  1166. Handle, // Driver Handle
  1167. event, // Event
  1168. NULL, // APC Routine
  1169. NULL, // APC context
  1170. &ioStatusBlock, // Status block
  1171. IoctlCode, // Control code
  1172. Request, // Input buffer
  1173. RequestSize, // Input buffer size
  1174. Response, // Output buffer
  1175. *ResponseSize // Output buffer size
  1176. );
  1177. if (status == STATUS_PENDING) {
  1178. status = NtWaitForSingleObject(
  1179. event,
  1180. TRUE,
  1181. NULL
  1182. );
  1183. }
  1184. if (status == STATUS_SUCCESS) {
  1185. status = ioStatusBlock.Status;
  1186. // NOTENOTE: on 64 bit this truncates, might want to add > check code
  1187. *ResponseSize = (ULONG)ioStatusBlock.Information;
  1188. }
  1189. else {
  1190. *ResponseSize = 0;
  1191. }
  1192. CloseHandle(event);
  1193. return(status);
  1194. } // DoIoctl