ReactOS  0.4.13-dev-99-g7e18b6d
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_new/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 
19 INT
20 WSAAPI
22 {
23  /* Check if we have a socket table */
24  if (!WsSockHandleTable)
25  {
26  /* Create it */
28  }
29 
30  /* Nothing to do */
31  return NO_ERROR;
32 }
33 
34 VOID
35 WSPAPI
37 {
38  /* Check if we have a socket table */
40  {
41  /* Destroy it */
43  }
44 }
45 
47 WSAAPI
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 
65 INT
66 WSAAPI
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 
91 WSAAPI
93 {
94  /* Let WAH do the translation */
96  (HANDLE)Handle);
97 }
98 
100 WSAAPI
102 {
103  INT ErrorCode;
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 */
123  if (ErrorCode == ERROR_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 
138 PWSSOCKET
139 WSAAPI
141 {
142  PWSSOCKET Socket;
143 
144  /* Let WAH do the translation */
146  (HANDLE)Handle)))
147  {
148  return Socket;
149  }
150  else
151  {
152  /* WAH didn't find it, use IFS */
153  return WsSockFindIfsSocket(Handle);
154  }
155 }
156 
157 INT
158 WSAAPI
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 
178 BOOL
179 WSAAPI
182 {
183  /* Call the detach routine */
185 }
186 
187 VOID
188 WSAAPI
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 
203 VOID
204 WSAAPI
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 
215 INT
216 WSAAPI
218 {
219  /* Remove it from the list */
221 }
222 
223 INT
224 WSAAPI
226  IN SOCKET Handle,
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 }
BOOLEAN IsProvider
Definition: ws2_32p.h:203
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
BOOLEAN Overlapped
Definition: ws2_32p.h:201
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4418
Definition: ws2_32p.h:85
PWSSOCKET WSAAPI WsSockFindIfsSocket(IN SOCKET Handle)
Definition: dsocket.c:101
#define ERROR_SUCCESS
Definition: deptool.c:10
VOID WSPAPI WsSockCleanup(VOID)
Definition: dsocket.c:36
BOOL WSAAPI WsProcDetachSocket(IN PWSPROCESS Process, IN PWAH_HANDLE Handle)
Definition: dprocess.c:182
BOOL WSAAPI WsSockDeleteSockets(IN LPVOID Context, IN PWAH_HANDLE Handle)
Definition: dsocket.c:180
BOOLEAN ApiSocket
Definition: ws2_32p.h:202
DWORD OpenType
Definition: ws2_32p.h:190
PWAH_HANDLE_TABLE WsSockHandleTable
Definition: dsocket.c:15
DWORD WSAAPI WsTcFindIfsProviderForSocket(IN PTCATALOG TCatalog, SOCKET Socket)
#define NO_ERROR
Definition: dderror.h:5
int32_t INT
Definition: typedefs.h:56
DWORD WINAPI WahCreateHandleContextTable(OUT PWAH_HANDLE_TABLE *Table)
Definition: context.c:189
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
DWORD WINAPI WahRemoveHandleContext(IN PWAH_HANDLE_TABLE Table, IN PWAH_HANDLE Handle)
Definition: context.c:564
INT WSAAPI WsSockAddApiReference(IN SOCKET Handle)
Definition: dsocket.c:159
unsigned int BOOL
Definition: ntddk_ex.h:94
DWORD WINAPI WahDestroyHandleContextTable(IN PWAH_HANDLE_TABLE Table)
Definition: context.c:246
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
FORCEINLINE PWSPROCESS WsGetProcess(VOID)
Definition: ws2_32p.h:885
PWAH_HANDLE WINAPI WahInsertHandleContext(IN PWAH_HANDLE_TABLE Table, IN PWAH_HANDLE Handle)
Definition: context.c:336
INT WSAAPI WsSockInitialize(IN PWSSOCKET Socket, IN PTCATALOG_ENTRY CatalogEntry)
Definition: dsocket.c:67
_In_ HANDLE Handle
Definition: extypes.h:390
VOID WSAAPI WsSockDelete(IN PWSSOCKET Socket)
Definition: dsocket.c:189
VOID WSAAPI WsTcEntryDereference(IN PTCATALOG_ENTRY CatalogEntry)
Definition: dcatitem.c:51
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define INVALID_SOCKET
Definition: winsock.h:332
INT WSAAPI WsSockAssociateHandle(IN PWSSOCKET Socket, IN SOCKET Handle, IN BOOLEAN IsProvider)
Definition: dsocket.c:225
PVOID HANDLE
Definition: typedefs.h:71
unsigned long DWORD
Definition: ntddk_ex.h:95
#define InterlockedDecrement
Definition: armddk.h:52
#define TlsIndex
Definition: ws2_32p.h:277
PWSSOCKET WSAAPI WsSockAllocate(VOID)
Definition: dsocket.c:48
LONG RefCount
Definition: ws2_32p.h:196
INT WSAAPI WsSockDisassociateHandle(IN PWSSOCKET Socket)
Definition: dsocket.c:217
VOID WSAAPI WsSockDereference(IN PWSSOCKET Socket)
Definition: dsocket.c:205
#define WSASYSCALLFAILURE
Definition: winerror.h:1994
#define InterlockedIncrement
Definition: armddk.h:53
#define WSAAPI
Definition: winsock2.h:584
PWSSOCKET WSAAPI WsSockGetSocketNoExport(IN SOCKET Handle)
Definition: dsocket.c:92
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
LPVOID WINAPI TlsGetValue(IN DWORD Index)
Definition: thread.c:1163
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
BOOL WINAPI GetHandleInformation(IN HANDLE hObject, OUT LPDWORD lpdwFlags)
Definition: handle.c:40
PWSSOCKET WSAAPI WsSockGetSocket(IN SOCKET Handle)
Definition: dsocket.c:140
UINT_PTR SOCKET
Definition: winsock.h:47
INT WSAAPI WsSockStartup(VOID)
Definition: dsocket.c:21
struct _WSSOCKET * PWSSOCKET
HANDLE WsSockHeap
Definition: dllmain.c:21
#define WSAENOBUFS
Definition: winerror.h:1968
#define HeapFree(x, y, z)
Definition: compat.h:394
PTCATALOG WSAAPI WsProcGetTCatalog(IN PWSPROCESS Process)
Definition: dprocess.c:150
PWAH_HANDLE WINAPI WahReferenceContextByHandle(IN PWAH_HANDLE_TABLE Table, IN HANDLE Handle)
Definition: context.c:524
#define WSPAPI
Definition: ws2spi.h:39