ReactOS 0.4.15-dev-7918-g2a2556c
dsocket.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dll/win32/ws2_32/src/dsocket.c
5 * PURPOSE: Socket Object
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9/* INCLUDES ******************************************************************/
10
11#include <ws2_32.h>
12
13/* DATA **********************************************************************/
14
16
17/* FUNCTIONS *****************************************************************/
18
19INT
22{
23 /* Check if we have a socket table */
25 {
26 /* Create it */
28 }
29
30 /* Nothing to do */
31 return NO_ERROR;
32}
33
34VOID
37{
38 /* Check if we have a socket table */
40 {
41 /* Destroy it */
43 }
44}
45
49{
50 PWSSOCKET Socket;
51
52 /* Allocate the socket object */
53 Socket = HeapAlloc(WsSockHeap, HEAP_ZERO_MEMORY, sizeof(*Socket));
54 if (Socket)
55 {
56 /* Setup default non-zero values */
57 Socket->RefCount = 2;
58 Socket->Overlapped = TRUE;
59 }
60
61 /* Return it */
62 return Socket;
63}
64
65INT
68 IN PTCATALOG_ENTRY CatalogEntry)
69{
70 PWSTHREAD CurrentThread;
71
72 /* Associate this catalog and reference it */
73 Socket->CatalogEntry = CatalogEntry;
74 InterlockedIncrement(&CatalogEntry->RefCount);
75
76 /* Associate the Provider and Process Objects */
77 Socket->Provider = CatalogEntry->Provider;
78
79 /* Get the current Thread Object */
80 if ((CurrentThread = TlsGetValue(TlsIndex)))
81 {
82 /* Set the overlapped mode */
83 Socket->Overlapped = (CurrentThread->OpenType == 0);
84 }
85
86 /* Return status */
87 return ERROR_SUCCESS;
88}
89
93{
94 /* Let WAH do the translation */
96 (HANDLE)Handle);
97}
98
100WSAAPI
102{
104 DWORD Flags;
105 PWSSOCKET Socket = NULL;
107 PTCATALOG Catalog = NULL;
108
109 /* Validate the socket and get handle info */
110 if ((Handle != INVALID_SOCKET) &&
112 {
113 /* Get the process */
114 if ((Process = WsGetProcess()))
115 {
116 /* Get the catalog */
117 Catalog = WsProcGetTCatalog(Process);
118
119 /* Get the IFS Provider */
121
122 /* Check for success */
124 {
125 /* Get the Socket now */
127
128 /* Mark it as an API Socket */
129 if (Socket) Socket->ApiSocket = TRUE;
130 }
131 }
132 }
133
134 /* Return the socket */
135 return Socket;
136}
137
139WSAAPI
141{
142 PWSSOCKET Socket;
143
144 /* Let WAH do the translation */
145 if ((WsSockHandleTable != NULL) &&
147 {
148 return Socket;
149 }
150 else
151 {
152 /* WAH didn't find it, use IFS */
154 }
155}
156
157INT
158WSAAPI
160{
161 PWSSOCKET Socket;
162
163 /* Get the Socket now */
164 if ((Socket = WsSockGetSocketNoExport(Handle)))
165 {
166 /* Mark it as an API Socket */
167 if (Socket) Socket->ApiSocket = TRUE;
168
169 /* Remove a reference and return */
170 WsSockDereference(Socket);
171 return ERROR_SUCCESS;
172 }
173
174 /* Return error */
175 return WSASYSCALLFAILURE;
176}
177
178BOOL
179WSAAPI
182{
183 /* Call the detach routine */
185}
186
187VOID
188WSAAPI
190{
191 /* Check if we have a catalog entry */
192 if (Socket->CatalogEntry)
193 {
194 /* Dereference it */
195 WsTcEntryDereference(Socket->CatalogEntry);
196 Socket->CatalogEntry = NULL;
197 }
198
199 /* Delete us */
200 HeapFree(WsSockHeap, 0, Socket);
201}
202
203VOID
204WSAAPI
206{
207 /* Dereference and check if it's now 0 */
208 if (!(InterlockedDecrement(&Socket->RefCount)))
209 {
210 /* We can delete the Provider now */
211 WsSockDelete(Socket);
212 }
213}
214
215INT
216WSAAPI
218{
219 /* Remove it from the list */
221}
222
223INT
224WSAAPI
227 IN BOOLEAN IsProvider)
228{
230 PWSSOCKET OldSocket;
231
232 /* Save the socket and provider */
233 Socket->IsProvider = IsProvider;
234 Socket->Handle = (HANDLE)Handle;
235
236 /* Insert it into the handle table */
238 (PWAH_HANDLE)Socket);
239
240 /* Check if a socket already existed */
241 if (OldSocket != Socket)
242 {
243 /* We'll dereference it */
244 WsSockDereference(OldSocket);
245 }
246 else if (!OldSocket)
247 {
248 /* No memory to allocate it */
250 }
251
252 /* Return */
253 return ErrorCode;
254}
unsigned char BOOLEAN
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define NO_ERROR
Definition: dderror.h:5
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
BOOL WINAPI GetHandleInformation(IN HANDLE hObject, OUT LPDWORD lpdwFlags)
Definition: handle.c:40
LPVOID WINAPI TlsGetValue(IN DWORD Index)
Definition: thread.c:1240
DWORD WINAPI WahCreateHandleContextTable(OUT PWAH_HANDLE_TABLE *Table)
Definition: context.c:189
DWORD WINAPI WahRemoveHandleContext(IN PWAH_HANDLE_TABLE Table, IN PWAH_HANDLE Handle)
Definition: context.c:564
PWAH_HANDLE WINAPI WahInsertHandleContext(IN PWAH_HANDLE_TABLE Table, IN PWAH_HANDLE Handle)
Definition: context.c:336
DWORD WINAPI WahDestroyHandleContextTable(IN PWAH_HANDLE_TABLE Table)
Definition: context.c:246
PWAH_HANDLE WINAPI WahReferenceContextByHandle(IN PWAH_HANDLE_TABLE Table, IN HANDLE Handle)
Definition: context.c:524
PWSSOCKET WSAAPI WsSockGetSocketNoExport(IN SOCKET Handle)
Definition: dsocket.c:92
INT WSAAPI WsSockStartup(VOID)
Definition: dsocket.c:21
INT WSAAPI WsSockAssociateHandle(IN PWSSOCKET Socket, IN SOCKET Handle, IN BOOLEAN IsProvider)
Definition: dsocket.c:225
INT WSAAPI WsSockInitialize(IN PWSSOCKET Socket, IN PTCATALOG_ENTRY CatalogEntry)
Definition: dsocket.c:67
PWSSOCKET WSAAPI WsSockFindIfsSocket(IN SOCKET Handle)
Definition: dsocket.c:101
VOID WSAAPI WsSockDereference(IN PWSSOCKET Socket)
Definition: dsocket.c:205
PWSSOCKET WSAAPI WsSockAllocate(VOID)
Definition: dsocket.c:48
VOID WSPAPI WsSockCleanup(VOID)
Definition: dsocket.c:36
PWAH_HANDLE_TABLE WsSockHandleTable
Definition: dsocket.c:15
INT WSAAPI WsSockAddApiReference(IN SOCKET Handle)
Definition: dsocket.c:159
VOID WSAAPI WsSockDelete(IN PWSSOCKET Socket)
Definition: dsocket.c:189
PWSSOCKET WSAAPI WsSockGetSocket(IN SOCKET Handle)
Definition: dsocket.c:140
INT WSAAPI WsSockDisassociateHandle(IN PWSSOCKET Socket)
Definition: dsocket.c:217
BOOL WSAAPI WsSockDeleteSockets(IN LPVOID Context, IN PWAH_HANDLE Handle)
Definition: dsocket.c:180
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
ULONG Handle
Definition: gdb_input.c:15
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
Definition: ws2_32p.h:86
BOOLEAN IsProvider
Definition: ws2_32p.h:203
LONG RefCount
Definition: ws2_32p.h:196
BOOLEAN Overlapped
Definition: ws2_32p.h:201
BOOLEAN ApiSocket
Definition: ws2_32p.h:202
DWORD OpenType
Definition: ws2_32p.h:190
PVOID HANDLE
Definition: typedefs.h:73
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
#define WSASYSCALLFAILURE
Definition: winerror.h:1994
#define WSAENOBUFS
Definition: winerror.h:1968
#define WSAAPI
Definition: winsock2.h:605
#define INVALID_SOCKET
Definition: winsock.h:332
UINT_PTR SOCKET
Definition: winsock.h:47
struct _WSSOCKET * PWSSOCKET
BOOL WSAAPI WsProcDetachSocket(IN PWSPROCESS Process, IN PWAH_HANDLE Handle)
Definition: dprocess.c:182
VOID WSAAPI WsTcEntryDereference(IN PTCATALOG_ENTRY CatalogEntry)
Definition: dcatitem.c:51
FORCEINLINE PWSPROCESS WsGetProcess(VOID)
Definition: ws2_32p.h:885
PTCATALOG WSAAPI WsProcGetTCatalog(IN PWSPROCESS Process)
Definition: dprocess.c:150
HANDLE WsSockHeap
Definition: dllmain.c:21
#define TlsIndex
Definition: ws2_32p.h:277
DWORD WSAAPI WsTcFindIfsProviderForSocket(IN PTCATALOG TCatalog, SOCKET Socket)
#define WSPAPI
Definition: ws2spi.h:39
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170