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)
9 /* INCLUDES ******************************************************************/
13 /* DATA **********************************************************************/
15 PWAH_HANDLE_TABLE WsSockHandleTable
;
17 /* FUNCTIONS *****************************************************************/
23 /* Check if we have a socket table */
24 if (!WsSockHandleTable
)
27 return WahCreateHandleContextTable(&WsSockHandleTable
);
38 /* Check if we have a socket table */
39 if (WsSockHandleTable
)
42 WahDestroyHandleContextTable(WsSockHandleTable
);
52 /* Allocate the socket object */
53 Socket
= HeapAlloc(WsSockHeap
, HEAP_ZERO_MEMORY
, sizeof(WSSOCKET
));
55 /* Setup default non-zero values */
57 Socket
->Overlapped
= TRUE
;
65 WsSockInitialize(IN PWSSOCKET Socket
,
66 IN PTCATALOG_ENTRY CatalogEntry
)
68 PWSTHREAD CurrentThread
;
70 /* Associate this catalog and reference it */
71 Socket
->CatalogEntry
= CatalogEntry
;
72 InterlockedIncrement(&CatalogEntry
->RefCount
);
74 /* Associate the Provider and Process Objects */
75 Socket
->Provider
= CatalogEntry
->Provider
;
77 /* Get the current Thread Object */
78 if ((CurrentThread
= TlsGetValue(TlsIndex
)))
80 /* Set the overlapped mode */
81 Socket
->Overlapped
= (CurrentThread
->OpenType
== 0);
90 WsSockGetSocketNoExport(IN SOCKET Handle
)
92 /* Let WAH do the translation */
93 return (PWSSOCKET
)WahReferenceContextByHandle(WsSockHandleTable
,
99 WsSockFindIfsSocket(IN SOCKET Handle
)
103 PWSSOCKET Socket
= NULL
;
104 PWSPROCESS Process
= NULL
;
105 PTCATALOG Catalog
= NULL
;
107 /* Validate the socket and get handle info */
108 if ((Handle
!= INVALID_SOCKET
) &&
109 (GetHandleInformation((HANDLE
)Handle
, &Flags
)))
111 /* Get the process */
112 if ((Process
= WsGetProcess()))
114 /* Get the catalog */
115 Catalog
= WsProcGetTCatalog(Process
);
117 /* Get the IFS Provider */
118 ErrorCode
= WsTcFindIfsProviderForSocket(Catalog
, Handle
);
120 /* Check for success */
121 if (ErrorCode
== ERROR_SUCCESS
)
123 /* Get the Socket now */
124 Socket
= WsSockGetSocketNoExport(Handle
);
126 /* Mark it as an API Socket */
127 if (Socket
) Socket
->ApiSocket
= TRUE
;
132 /* Return the socket */
138 WsSockGetSocket(IN SOCKET Handle
)
142 /* Let WAH do the translation */
143 if ((Socket
= (PWSSOCKET
)WahReferenceContextByHandle(WsSockHandleTable
,
150 /* WAH didn't find it, use IFS */
151 return WsSockFindIfsSocket(Handle
);
157 WsSockAddApiReference(IN SOCKET Handle
)
161 /* Get the Socket now */
162 if ((Socket
= WsSockGetSocketNoExport(Handle
)))
164 /* Mark it as an API Socket */
165 if (Socket
) Socket
->ApiSocket
= TRUE
;
167 /* Remove a reference and return */
168 WsSockDereference(Socket
);
169 return ERROR_SUCCESS
;
173 return WSASYSCALLFAILURE
;
178 WsSockDeleteSockets(IN LPVOID Context
,
179 IN PWAH_HANDLE Handle
)
181 /* Call the detach routine */
182 return WsProcDetachSocket((PWSPROCESS
)Context
, Handle
);
187 WsSockDelete(IN PWSSOCKET Socket
)
189 /* Check if we have a catalog entry */
190 if (Socket
->CatalogEntry
)
193 WsTcEntryDereference(Socket
->CatalogEntry
);
194 Socket
->CatalogEntry
= NULL
;
200 WsSockDereference(IN PWSSOCKET Socket
)
202 /* Dereference and check if it's now 0 */
203 if (!(InterlockedDecrement(&Socket
->RefCount
)))
205 /* We can delete the Provider now */
206 WsSockDelete(Socket
);
212 WsSockDisassociateHandle(IN PWSSOCKET Socket
)
214 /* Remove it from the list */
215 return WahRemoveHandleContext(WsSockHandleTable
, (PWAH_HANDLE
)Socket
);
220 WsSockAssociateHandle(IN PWSSOCKET Socket
,
222 IN BOOLEAN IsProvider
)
224 INT ErrorCode
= ERROR_SUCCESS
;
227 /* Save the socket and provider */
228 Socket
->IsProvider
= IsProvider
;
229 Socket
->Handle
= (HANDLE
)Handle
;
231 /* Insert it into the handle table */
232 OldSocket
= (PWSSOCKET
)WahInsertHandleContext(WsSockHandleTable
,
233 (PWAH_HANDLE
)Socket
);
235 /* Check if a socket already existed */
236 if (OldSocket
!= Socket
)
238 /* We'll dereference it */
239 WsSockDereference(OldSocket
);
243 /* No memory to allocate it */
244 ErrorCode
= WSAENOBUFS
;