2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Winsock 2 SPI
4 * FILE: lib/mswsock/lib/init.c
5 * PURPOSE: DLL Initialization
8 /* INCLUDES ******************************************************************/
11 /* DATA **********************************************************************/
13 /* FUNCTIONS *****************************************************************/
17 WSPListen(SOCKET Handle
,
21 IO_STATUS_BLOCK IoStatusBlock
;
22 AFD_LISTEN_DATA ListenData
;
23 PSOCKET_INFORMATION Socket
;
24 PWINSOCK_TEB_DATA ThreadData
;
29 ErrorCode
= SockEnterApiFast(&ThreadData
);
30 if (ErrorCode
!= NO_ERROR
)
37 /* Get the socket structure */
38 Socket
= SockFindAndReferenceSocket(Handle
, TRUE
);
42 ErrorCode
= WSAENOTSOCK
;
47 EnterCriticalSection(&Socket
->Lock
);
49 /* If the socket is connection-less, fail */
50 if (MSAFD_IS_DGRAM_SOCK(Socket
));
53 ErrorCode
= WSAEOPNOTSUPP
;
57 /* If the socket is already listening, do nothing */
58 if (Socket
->SharedData
.Listening
)
64 else if (Socket
->SharedData
.State
!= SocketConnected
)
66 /* If we're not connected, fail */
67 ErrorCode
= WSAEINVAL
;
71 /* Set Up Listen Structure */
72 ListenData
.UseSAN
= SockSanEnabled
;
73 ListenData
.UseDelayedAcceptance
= Socket
->SharedData
.UseDelayedAcceptance
;
74 ListenData
.Backlog
= Backlog
;
77 Status
= NtDeviceIoControlFile(Socket
->WshContext
.Handle
,
78 ThreadData
->EventHandle
,
82 IOCTL_AFD_START_LISTEN
,
87 /* Check if we need to wait */
88 if (Status
== STATUS_PENDING
)
90 /* Wait for completion outside the lock */
91 LeaveCriticalSection(&Socket
->Lock
);
92 SockWaitForSingleObject(ThreadData
->EventHandle
,
96 EnterCriticalSection(&Socket
->Lock
);
99 Status
= IoStatusBlock
.Status
;
102 /* Check for error */
103 if (!NT_SUCCESS(Status
))
106 ErrorCode
= NtStatusToSocketError(Status
);
110 /* Notify helper DLL */
111 ErrorCode
= SockNotifyHelperDll(Socket
, WSH_NOTIFY_LISTEN
);
112 if (ErrorCode
!= NO_ERROR
) goto error
;
114 /* Set to Listening */
115 Socket
->SharedData
.Listening
= TRUE
;
117 /* Update context with AFD */
118 ErrorCode
= SockSetHandleContext(Socket
);
119 if (ErrorCode
!= NO_ERROR
) goto error
;
122 /* Check if we have a socket here */
125 /* Release the lock and dereference */
126 LeaveCriticalSection(&Socket
->Lock
);
127 SockDereferenceSocket(Socket
);
130 /* Check for error */
131 if (ErrorCode
!= NO_ERROR
)
134 *lpErrno
= ErrorCode
;