[WS2_32]
[reactos.git] / reactos / dll / win32 / ws2_32 / misc / sndrcv.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 DLL
4 * FILE: misc/sndrcv.c
5 * PURPOSE: Send/receive functions
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * REVISIONS:
8 * CSH 01/09-2000 Created
9 */
10
11 #include <w32api.h>
12 #include <ws2_32.h>
13 #include <catalog.h>
14 #include <handle.h>
15
16
17 /*
18 * @implemented
19 */
20 INT
21 EXPORT
22 recv(IN SOCKET s,
23 OUT CHAR FAR* buf,
24 IN INT len,
25 IN INT flags)
26 {
27 DWORD Error;
28 DWORD BytesReceived;
29 WSABUF WSABuf;
30
31 WS_DbgPrint(MAX_TRACE, ("s (0x%X) buf (0x%X) len (0x%X) flags (0x%X).\n",
32 s,
33 buf,
34 len,
35 flags));
36
37 WSABuf.len = len;
38 WSABuf.buf = (CHAR FAR*)buf;
39
40 Error = WSARecv(s,
41 &WSABuf,
42 1,
43 &BytesReceived,
44 (LPDWORD)&flags,
45 NULL,
46 NULL);
47
48 if( Error )
49 return -1;
50 else
51 return BytesReceived;
52 }
53
54
55 /*
56 * @implemented
57 */
58 INT
59 EXPORT
60 recvfrom(IN SOCKET s,
61 OUT CHAR FAR* buf,
62 IN INT len,
63 IN INT flags,
64 OUT LPSOCKADDR from,
65 IN OUT INT FAR* fromlen)
66 {
67 DWORD Error;
68 DWORD BytesReceived;
69 WSABUF WSABuf;
70
71 WS_DbgPrint(MAX_TRACE, ("s (0x%X) buf (0x%X) len (0x%X) flags (0x%X).\n",
72 s,
73 buf,
74 len,
75 flags));
76
77 WSABuf.len = len;
78 WSABuf.buf = (CHAR FAR*)buf;
79
80 Error = WSARecvFrom(s,
81 &WSABuf,
82 1,
83 &BytesReceived,
84 (LPDWORD)&flags,
85 from,
86 fromlen,
87 NULL,
88 NULL);
89
90 if( Error )
91 return -1;
92 else
93 return BytesReceived;
94 }
95
96
97 /*
98 * @implemented
99 */
100 INT
101 EXPORT
102 send(IN SOCKET s,
103 IN CONST CHAR FAR* buf,
104 IN INT len,
105 IN INT flags)
106 {
107 DWORD BytesSent;
108 DWORD Error;
109 WSABUF WSABuf;
110
111 WS_DbgPrint(MAX_TRACE, ("s (0x%X) buf (0x%X) len (0x%X) flags (0x%X).\n",
112 s,
113 buf,
114 len,
115 flags));
116
117 WSABuf.len = len;
118 WSABuf.buf = (CHAR FAR*)buf;
119
120 Error = WSASend(s,
121 &WSABuf,
122 1,
123 &BytesSent,
124 flags,
125 NULL,
126 NULL);
127
128 if( Error )
129 {
130 WS_DbgPrint(MAX_TRACE,("Reporting error %d\n", Error));
131 return -1;
132 }
133 else
134 {
135 WS_DbgPrint(MAX_TRACE,("Read %d bytes\n", BytesSent));
136 return BytesSent;
137 }
138 }
139
140
141 /*
142 * @implemented
143 */
144 INT
145 EXPORT
146 sendto(IN SOCKET s,
147 IN CONST CHAR FAR* buf,
148 IN INT len,
149 IN INT flags,
150 IN CONST struct sockaddr *to,
151 IN INT tolen)
152 {
153 DWORD Error;
154 DWORD BytesSent;
155 WSABUF WSABuf;
156
157 WS_DbgPrint(MAX_TRACE, ("s (0x%X) buf (0x%X) len (0x%X) flags (0x%X).\n",
158 s,
159 buf,
160 len,
161 flags));
162
163 WSABuf.len = len;
164 WSABuf.buf = (CHAR FAR*)buf;
165
166 Error = WSASendTo(s,
167 &WSABuf,
168 1,
169 &BytesSent,
170 flags,
171 to,
172 tolen,
173 NULL,
174 NULL);
175
176 if( Error )
177 return -1;
178 else
179 return BytesSent;
180 }
181
182
183 /*
184 * @implemented
185 */
186 INT
187 EXPORT
188 WSARecv(IN SOCKET s,
189 IN OUT LPWSABUF lpBuffers,
190 IN DWORD dwBufferCount,
191 OUT LPDWORD lpNumberOfBytesRecvd,
192 IN OUT LPDWORD lpFlags,
193 IN LPWSAOVERLAPPED lpOverlapped,
194 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
195 {
196 PCATALOG_ENTRY Provider;
197 INT Errno;
198 INT Code;
199
200 WS_DbgPrint(MAX_TRACE, ("Called.\n"));
201
202 if (!ReferenceProviderByHandle((HANDLE)s,
203 &Provider))
204 {
205 WSASetLastError(WSAENOTSOCK);
206 return SOCKET_ERROR;
207 }
208
209 assert(Provider->ProcTable.lpWSPRecv);
210
211 Code = Provider->ProcTable.lpWSPRecv(s,
212 lpBuffers,
213 dwBufferCount,
214 lpNumberOfBytesRecvd,
215 lpFlags,
216 lpOverlapped,
217 lpCompletionRoutine,
218 NULL /* lpThreadId */,
219 &Errno);
220
221 DereferenceProviderByPointer(Provider);
222
223 if (Code == SOCKET_ERROR)
224 WSASetLastError(Errno);
225
226 return Code;
227 }
228
229
230 /*
231 * @unimplemented
232 */
233 INT
234 EXPORT
235 WSARecvDisconnect(IN SOCKET s,
236 OUT LPWSABUF lpInboundDisconnectData)
237 {
238 UNIMPLEMENTED
239
240 return 0;
241 }
242
243
244 /*
245 * @implemented
246 */
247 INT
248 EXPORT
249 WSARecvFrom(IN SOCKET s,
250 IN OUT LPWSABUF lpBuffers,
251 IN DWORD dwBufferCount,
252 OUT LPDWORD lpNumberOfBytesRecvd,
253 IN OUT LPDWORD lpFlags,
254 OUT LPSOCKADDR lpFrom,
255 IN OUT LPINT lpFromlen,
256 IN LPWSAOVERLAPPED lpOverlapped,
257 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
258 {
259 PCATALOG_ENTRY Provider;
260 INT Errno;
261 INT Code;
262
263 WS_DbgPrint(MAX_TRACE, ("Called.\n"));
264
265 if (!ReferenceProviderByHandle((HANDLE)s, &Provider))
266 {
267 WSASetLastError(WSAENOTSOCK);
268 return SOCKET_ERROR;
269 }
270
271 assert(Provider->ProcTable.lpWSPRecvFrom);
272
273 Code = Provider->ProcTable.lpWSPRecvFrom(s,
274 lpBuffers,
275 dwBufferCount,
276 lpNumberOfBytesRecvd,
277 lpFlags,
278 lpFrom,
279 lpFromlen,
280 lpOverlapped,
281 lpCompletionRoutine,
282 NULL /* lpThreadId */,
283 &Errno);
284
285 DereferenceProviderByPointer(Provider);
286
287 if (Code == SOCKET_ERROR)
288 WSASetLastError(Errno);
289
290 return Code;
291 }
292
293
294 /*
295 * @implemented
296 */
297 INT
298 EXPORT
299 WSASend(IN SOCKET s,
300 IN LPWSABUF lpBuffers,
301 IN DWORD dwBufferCount,
302 OUT LPDWORD lpNumberOfBytesSent,
303 IN DWORD dwFlags,
304 IN LPWSAOVERLAPPED lpOverlapped,
305 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
306 {
307 PCATALOG_ENTRY Provider;
308 INT Errno;
309 INT Code;
310
311 WS_DbgPrint(MAX_TRACE, ("Called.\n"));
312
313 if (!ReferenceProviderByHandle((HANDLE)s, &Provider))
314 {
315 WSASetLastError(WSAENOTSOCK);
316 return SOCKET_ERROR;
317 }
318
319 assert(Provider->ProcTable.lpWSPSend);
320
321 Code = Provider->ProcTable.lpWSPSend(s,
322 lpBuffers,
323 dwBufferCount,
324 lpNumberOfBytesSent,
325 dwFlags,
326 lpOverlapped,
327 lpCompletionRoutine,
328 NULL /* lpThreadId */,
329 &Errno);
330
331 DereferenceProviderByPointer(Provider);
332
333 if (Code == SOCKET_ERROR)
334 WSASetLastError(Errno);
335
336 return Code;
337 }
338
339
340 /*
341 * @unimplemented
342 */
343 INT
344 EXPORT
345 WSASendDisconnect(IN SOCKET s,
346 IN LPWSABUF lpOutboundDisconnectData)
347 {
348 UNIMPLEMENTED
349
350 return 0;
351 }
352
353
354 /*
355 * @implemented
356 */
357 INT
358 EXPORT
359 WSASendTo(IN SOCKET s,
360 IN LPWSABUF lpBuffers,
361 IN DWORD dwBufferCount,
362 OUT LPDWORD lpNumberOfBytesSent,
363 IN DWORD dwFlags,
364 IN CONST struct sockaddr *lpTo,
365 IN INT iToLen,
366 IN LPWSAOVERLAPPED lpOverlapped,
367 IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
368 {
369 PCATALOG_ENTRY Provider;
370 INT Errno;
371 INT Code;
372
373 WS_DbgPrint(MAX_TRACE, ("Called.\n"));
374
375 if (!ReferenceProviderByHandle((HANDLE)s, &Provider))
376 {
377 WSASetLastError(WSAENOTSOCK);
378 return SOCKET_ERROR;
379 }
380
381 assert(Provider->ProcTable.lpWSPSendTo);
382
383 #if (__W32API_MAJOR_VERSION < 2 || __W32API_MINOR_VERSION < 5)
384 Code = Provider->ProcTable.lpWSPSendTo(s,
385 lpBuffers,
386 dwBufferCount,
387 lpNumberOfBytesSent,
388 dwFlags,
389 (CONST LPSOCKADDR)lpTo,
390 iToLen,
391 lpOverlapped,
392 lpCompletionRoutine,
393 NULL /* lpThreadId */,
394 &Errno);
395 #else
396 Code = Provider->ProcTable.lpWSPSendTo(s,
397 lpBuffers,
398 dwBufferCount,
399 lpNumberOfBytesSent,
400 dwFlags,
401 lpTo,
402 iToLen,
403 lpOverlapped,
404 lpCompletionRoutine,
405 NULL /* lpThreadId */,
406 &Errno);
407 #endif /* __W32API_MAJOR_VERSION < 2 || __W32API_MINOR_VERSION < 5 */
408
409 DereferenceProviderByPointer(Provider);
410
411 if (Code == SOCKET_ERROR)
412 WSASetLastError(Errno);
413 else
414 WSASetLastError(0);
415
416 return Code;
417 }
418
419 /* EOF */