- New winsock (part 3 of x)
[reactos.git] / dll / win32 / ws2_32_new / src / dupsock.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dupsock.c
5 * PURPOSE: Socket Duplication
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /* INCLUDES ******************************************************************/
10 #include "ws2_32.h"
11
12 //#define NDEBUG
13 #include <debug.h>
14
15 /* DATA **********************************************************************/
16
17 /* FUNCTIONS *****************************************************************/
18
19 /*
20 * @implemented
21 */
22 INT
23 WSAAPI
24 WSADuplicateSocketA(IN SOCKET s,
25 IN DWORD dwProcessId,
26 OUT LPWSAPROTOCOL_INFOA lpProtocolInfo)
27 {
28 WSAPROTOCOL_INFOW ProtocolInfoW;
29 INT ErrorCode;
30 DPRINT("WSADuplicateSocketA: %lx, %lx, %p\n", s, dwProcessId, lpProtocolInfo);
31
32 /* Call the Unicode Function */
33 ErrorCode = WSADuplicateSocketW(s, dwProcessId, &ProtocolInfoW);
34
35 /* Check for success */
36 if (ErrorCode == ERROR_SUCCESS)
37 {
38 /* Convert Protocol Info to Ansi */
39 if (lpProtocolInfo)
40 {
41 /* Convert the information to ANSI */
42 ErrorCode = MapUnicodeProtocolInfoToAnsi(&ProtocolInfoW,
43 lpProtocolInfo);
44 }
45 else
46 {
47 /* Fail */
48 ErrorCode = WSAEFAULT;
49 }
50
51 /* Check if the conversion failed */
52 if (ErrorCode != ERROR_SUCCESS)
53 {
54 /* Set the last error and normalize the error */
55 SetLastError(ErrorCode);
56 ErrorCode = SOCKET_ERROR;
57 }
58 }
59
60 /* Return */
61 return ErrorCode;
62 }
63
64 /*
65 * @implemented
66 */
67 INT
68 WSAAPI
69 WSADuplicateSocketW(IN SOCKET s,
70 IN DWORD dwProcessId,
71 OUT LPWSAPROTOCOL_INFOW lpProtocolInfo)
72 {
73 PWSSOCKET Socket;
74 INT Status;
75 INT ErrorCode;
76 DPRINT("WSADuplicateSocketW: %lx, %lx, %p\n", s, dwProcessId, lpProtocolInfo);
77
78 /* Check for WSAStartup */
79 if ((ErrorCode = WsQuickProlog()) == ERROR_SUCCESS)
80 {
81 /* Get the Socket Context */
82 if ((Socket = WsSockGetSocket(s)))
83 {
84 /* Make the call */
85 Status = Socket->Provider->Service.lpWSPDuplicateSocket(s,
86 dwProcessId,
87 lpProtocolInfo,
88 &ErrorCode);
89 /* Deference the Socket Context */
90 WsSockDereference(Socket);
91
92 /* Return Provider Value */
93 if (Status == ERROR_SUCCESS) return Status;
94
95 /* If everything seemed fine, then the WSP call failed itself */
96 if (ErrorCode == NO_ERROR) ErrorCode = WSASYSCALLFAILURE;
97 }
98 else
99 {
100 /* No Socket Context Found */
101 ErrorCode = WSAENOTSOCK;
102 }
103 }
104
105 /* Return with an Error */
106 SetLastError(ErrorCode);
107 return SOCKET_ERROR;
108 }