2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
6 * Copyright (C) 2005 Marcus Meissner
7 * Copyright (C) 2006 Kai Blin
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTE: If you make any changes to fix a particular app, make sure
24 * they don't break something else like Netscape or telnet and ftp
25 * clients and servers (www.winsite.com got a lot of those).
29 #include "wine/port.h"
34 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
44 #ifdef HAVE_SYS_SOCKIO_H
45 # include <sys/sockio.h>
49 # include <sys/so_ioctl.h>
52 #ifdef HAVE_SYS_PARAM_H
53 # include <sys/param.h>
59 #ifdef HAVE_SYS_WAIT_H
60 # include <sys/wait.h>
65 #ifdef HAVE_SYS_SOCKET_H
66 #include <sys/socket.h>
68 #ifdef HAVE_NETINET_IN_H
69 # include <netinet/in.h>
71 #ifdef HAVE_NETINET_TCP_H
72 # include <netinet/tcp.h>
74 #ifdef HAVE_ARPA_INET_H
75 # include <arpa/inet.h>
80 #ifdef HAVE_SYS_ERRNO_H
81 #include <sys/errno.h>
90 #ifdef HAVE_ARPA_NAMESER_H
91 # include <arpa/nameser.h>
100 #ifdef HAVE_NETIPX_IPX_H
101 # include <netipx/ipx.h>
103 #elif defined(HAVE_LINUX_IPX_H)
104 # ifdef HAVE_ASM_TYPES_H
105 # include <asm/types.h>
107 # include <linux/ipx.h>
114 #ifdef HAVE_SYS_POLL_H
115 # include <sys/poll.h>
117 #ifdef HAVE_SYS_TIME_H
118 # include <sys/time.h>
121 #define NONAMELESSUNION
122 #define NONAMELESSSTRUCT
123 #include "ntstatus.h"
124 #define WIN32_NO_STATUS
129 #include "winerror.h"
131 #include "winsock2.h"
133 #include "ws2tcpip.h"
137 #include "iphlpapi.h"
139 #include "wine/server.h"
140 #include "wine/debug.h"
141 #include "wine/unicode.h"
144 # include "wsnwlink.h"
148 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
149 # define sipx_network sipx_addr.x_net
150 # define sipx_node sipx_addr.x_host.c_host
151 #endif /* __FreeBSD__ */
154 #define INADDR_NONE ~0UL
157 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
159 /* critical section to protect some non-rentrant net function */
160 extern CRITICAL_SECTION csWSgetXXXbyYYY
;
162 inline static const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
164 if (!a
) return "(nil)";
165 return wine_dbg_sprintf("{ family %d, address %s, port %d }",
166 ((const struct sockaddr_in
*)a
)->sin_family
,
167 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
168 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
171 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
172 #define SOCKET2HANDLE(s) ((HANDLE)(s))
173 #define HANDLE2SOCKET(h) ((SOCKET)(h))
175 /****************************************************************
176 * Async IO declarations
177 ****************************************************************/
179 typedef struct ws2_async
182 enum ws2_mode
{ws2m_read
, ws2m_write
, ws2m_sd_read
, ws2m_sd_write
} mode
;
183 LPWSAOVERLAPPED user_overlapped
;
184 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
187 struct WS_sockaddr
*addr
;
190 int val
; /* for send operations */
191 int *ptr
; /* for recv operations */
197 /****************************************************************/
199 /* ----------------------------------- internal data */
201 /* ws_... struct conversion flags */
203 typedef struct /* WSAAsyncSelect() control struct */
205 HANDLE service
, event
, sock
;
211 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
212 #define WS_MAX_UDP_DATAGRAM 1024
213 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
215 /* hostent's, servent's and protent's are stored in one buffer per thread,
216 * as documented on MSDN for the functions that return any of the buffers */
217 struct per_thread_data
220 struct WS_hostent
*he_buffer
;
221 struct WS_servent
*se_buffer
;
222 struct WS_protoent
*pe_buffer
;
228 static INT num_startup
; /* reference counter */
229 static FARPROC blocking_hook
= WSA_DefaultBlockingHook
;
231 /* function prototypes */
232 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
233 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
234 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
236 int WSAIOCTL_GetInterfaceCount(void);
237 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
240 UINT
wsaHerrno(int errnr
);
242 #define MAP_OPTION(opt) { WS_##opt, opt }
244 static const int ws_sock_map
[][2] =
246 MAP_OPTION( SO_DEBUG
),
247 MAP_OPTION( SO_ACCEPTCONN
),
248 MAP_OPTION( SO_REUSEADDR
),
249 MAP_OPTION( SO_KEEPALIVE
),
250 MAP_OPTION( SO_DONTROUTE
),
251 MAP_OPTION( SO_BROADCAST
),
252 MAP_OPTION( SO_LINGER
),
253 MAP_OPTION( SO_OOBINLINE
),
254 MAP_OPTION( SO_SNDBUF
),
255 MAP_OPTION( SO_RCVBUF
),
256 MAP_OPTION( SO_ERROR
),
257 MAP_OPTION( SO_TYPE
),
259 MAP_OPTION( SO_RCVTIMEO
),
262 MAP_OPTION( SO_SNDTIMEO
),
266 static const int ws_tcp_map
[][2] =
269 MAP_OPTION( TCP_NODELAY
),
273 static const int ws_ip_map
[][2] =
275 MAP_OPTION( IP_MULTICAST_IF
),
276 MAP_OPTION( IP_MULTICAST_TTL
),
277 MAP_OPTION( IP_MULTICAST_LOOP
),
278 MAP_OPTION( IP_ADD_MEMBERSHIP
),
279 MAP_OPTION( IP_DROP_MEMBERSHIP
),
280 MAP_OPTION( IP_OPTIONS
),
282 MAP_OPTION( IP_HDRINCL
),
284 MAP_OPTION( IP_TOS
),
285 MAP_OPTION( IP_TTL
),
288 static const int ws_af_map
[][2] =
290 MAP_OPTION( AF_UNSPEC
),
291 MAP_OPTION( AF_INET
),
292 MAP_OPTION( AF_INET6
),
294 MAP_OPTION( AF_IPX
),
298 static const int ws_socktype_map
[][2] =
300 MAP_OPTION( SOCK_DGRAM
),
301 MAP_OPTION( SOCK_STREAM
),
302 MAP_OPTION( SOCK_RAW
),
305 static const int ws_proto_map
[][2] =
307 MAP_OPTION( IPPROTO_IP
),
308 MAP_OPTION( IPPROTO_TCP
),
309 MAP_OPTION( IPPROTO_UDP
),
310 MAP_OPTION( IPPROTO_ICMP
),
311 MAP_OPTION( IPPROTO_IGMP
),
312 MAP_OPTION( IPPROTO_RAW
),
315 static const int ws_aiflag_map
[][2] =
317 MAP_OPTION( AI_PASSIVE
),
318 MAP_OPTION( AI_CANONNAME
),
319 MAP_OPTION( AI_NUMERICHOST
),
320 /* Linux/UNIX knows a lot more. But Windows only
321 * has 3 as far as I could see. -Marcus
325 static const int ws_eai_map
[][2] =
327 MAP_OPTION( EAI_AGAIN
),
328 MAP_OPTION( EAI_BADFLAGS
),
329 MAP_OPTION( EAI_FAIL
),
330 MAP_OPTION( EAI_FAMILY
),
331 MAP_OPTION( EAI_MEMORY
),
332 /* Note: EAI_NODATA is deprecated, but still
333 * used by Windows and Linux... We map the newer
334 * EAI_NONAME to EAI_NODATA for now until Windows
338 MAP_OPTION( EAI_NODATA
),
341 { WS_EAI_NODATA
, EAI_NONAME
},
344 MAP_OPTION( EAI_SERVICE
),
345 MAP_OPTION( EAI_SOCKTYPE
),
349 inline static DWORD
NtStatusToWSAError( const DWORD status
)
351 /* We only need to cover the status codes set by server async request handling */
355 case STATUS_SUCCESS
: wserr
= 0; break;
356 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
357 case STATUS_INVALID_HANDLE
: wserr
= WSAENOTSOCK
; break; /* WSAEBADF ? */
358 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
359 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
360 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
361 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
362 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
364 if ( status
>= WSABASEERR
&& status
<= WSABASEERR
+1004 )
365 /* It is not an NT status code but a winsock error */
369 wserr
= RtlNtStatusToDosError( status
);
370 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
376 /* set last error code from NT status without mapping WSA errors */
377 inline static unsigned int set_error( unsigned int err
)
381 err
= NtStatusToWSAError( err
);
387 inline static int get_sock_fd( SOCKET s
, DWORD access
, int *flags
)
390 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, flags
) ))
395 inline static void release_sock_fd( SOCKET s
, int fd
)
397 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
400 static void _enable_event( HANDLE s
, unsigned int event
,
401 unsigned int sstate
, unsigned int cstate
)
403 SERVER_START_REQ( enable_socket_event
)
407 req
->sstate
= sstate
;
408 req
->cstate
= cstate
;
409 wine_server_call( req
);
414 static int _is_blocking(SOCKET s
)
417 SERVER_START_REQ( get_socket_event
)
419 req
->handle
= SOCKET2HANDLE(s
);
420 req
->service
= FALSE
;
422 wine_server_call( req
);
423 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
429 static unsigned int _get_sock_mask(SOCKET s
)
432 SERVER_START_REQ( get_socket_event
)
434 req
->handle
= SOCKET2HANDLE(s
);
435 req
->service
= FALSE
;
437 wine_server_call( req
);
444 static void _sync_sock_state(SOCKET s
)
446 /* do a dummy wineserver request in order to let
447 the wineserver run through its select loop once */
448 (void)_is_blocking(s
);
451 static int _get_sock_error(SOCKET s
, unsigned int bit
)
453 int events
[FD_MAX_EVENTS
];
455 SERVER_START_REQ( get_socket_event
)
457 req
->handle
= SOCKET2HANDLE(s
);
458 req
->service
= FALSE
;
460 wine_server_set_reply( req
, events
, sizeof(events
) );
461 wine_server_call( req
);
467 static struct per_thread_data
*get_per_thread_data(void)
469 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
470 /* lazy initialization */
473 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
474 NtCurrentTeb()->WinSockData
= ptb
;
479 static void free_per_thread_data(void)
481 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
485 /* delete scratch buffers */
486 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
487 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
488 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
489 ptb
->he_buffer
= NULL
;
490 ptb
->se_buffer
= NULL
;
491 ptb
->pe_buffer
= NULL
;
493 HeapFree( GetProcessHeap(), 0, ptb
);
494 NtCurrentTeb()->WinSockData
= NULL
;
497 /***********************************************************************
498 * DllMain (WS2_32.init)
500 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
502 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
504 case DLL_PROCESS_ATTACH
:
506 case DLL_PROCESS_DETACH
:
507 free_per_thread_data();
510 case DLL_THREAD_DETACH
:
511 free_per_thread_data();
517 /***********************************************************************
520 * Converts socket flags from Windows format.
521 * Return 1 if converted, 0 if not (error).
523 static int convert_sockopt(INT
*level
, INT
*optname
)
530 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
531 if( ws_sock_map
[i
][0] == *optname
)
533 *optname
= ws_sock_map
[i
][1];
537 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
540 *level
= IPPROTO_TCP
;
541 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
542 if ( ws_tcp_map
[i
][0] == *optname
)
544 *optname
= ws_tcp_map
[i
][1];
548 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
552 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
553 if (ws_ip_map
[i
][0] == *optname
)
555 *optname
= ws_ip_map
[i
][1];
559 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
561 default: FIXME("Unimplemented or unknown socket level\n");
566 static inline BOOL
is_timeout_option( int optname
)
569 if (optname
== SO_RCVTIMEO
) return TRUE
;
572 if (optname
== SO_SNDTIMEO
) return TRUE
;
577 /* ----------------------------------- Per-thread info (or per-process?) */
579 static char *strdup_lower(const char *str
)
582 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
586 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
589 else SetLastError(WSAENOBUFS
);
593 inline static int sock_error_p(int s
)
595 unsigned int optval
, optlen
;
597 optlen
= sizeof(optval
);
598 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
599 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
603 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
604 * from an fd and return the value converted to milli seconds
605 * or -1 if there is an infinite time out */
606 static inline int get_rcvsnd_timeo( int fd
, int optname
)
609 unsigned int len
= sizeof(tv
);
610 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
612 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
613 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
618 /* macro wrappers for portability */
620 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
622 #define GET_RCVTIMEO(fd) (-1)
626 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
628 #define GET_SNDTIMEO(fd) (-1)
631 /* utility: given an fd, will block until one of the events occurs */
632 static inline int do_block( int fd
, int events
, int timeout
)
640 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
651 convert_af_w2u(int windowsaf
) {
654 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
655 if (ws_af_map
[i
][0] == windowsaf
)
656 return ws_af_map
[i
][1];
657 FIXME("unhandled Windows address family %d\n", windowsaf
);
662 convert_af_u2w(int unixaf
) {
665 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
666 if (ws_af_map
[i
][1] == unixaf
)
667 return ws_af_map
[i
][0];
668 FIXME("unhandled UNIX address family %d\n", unixaf
);
673 convert_proto_w2u(int windowsproto
) {
676 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
677 if (ws_proto_map
[i
][0] == windowsproto
)
678 return ws_proto_map
[i
][1];
679 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
684 convert_proto_u2w(int unixproto
) {
687 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
688 if (ws_proto_map
[i
][1] == unixproto
)
689 return ws_proto_map
[i
][0];
690 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
695 convert_socktype_w2u(int windowssocktype
) {
698 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
699 if (ws_socktype_map
[i
][0] == windowssocktype
)
700 return ws_socktype_map
[i
][1];
701 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
706 convert_socktype_u2w(int unixsocktype
) {
709 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
710 if (ws_socktype_map
[i
][1] == unixsocktype
)
711 return ws_socktype_map
[i
][0];
712 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
716 /* ----------------------------------- API -----
718 * Init / cleanup / error checking.
721 /***********************************************************************
722 * WSAStartup (WS2_32.115)
724 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
726 TRACE("verReq=%x\n", wVersionRequested
);
728 if (LOBYTE(wVersionRequested
) < 1)
729 return WSAVERNOTSUPPORTED
;
731 if (!lpWSAData
) return WSAEINVAL
;
735 /* that's the whole of the negotiation for now */
736 lpWSAData
->wVersion
= wVersionRequested
;
737 /* return winsock information */
738 lpWSAData
->wHighVersion
= 0x0202;
739 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
740 strcpy(lpWSAData
->szSystemStatus
, "Running" );
741 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
742 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
743 /* don't do anything with lpWSAData->lpVendorInfo */
744 /* (some apps don't allocate the space for this field) */
746 TRACE("succeeded\n");
751 /***********************************************************************
752 * WSACleanup (WS2_32.116)
754 INT WINAPI
WSACleanup(void)
760 SetLastError(WSANOTINITIALISED
);
765 /***********************************************************************
766 * WSAGetLastError (WINSOCK.111)
767 * WSAGetLastError (WS2_32.111)
769 INT WINAPI
WSAGetLastError(void)
771 return GetLastError();
774 /***********************************************************************
775 * WSASetLastError (WS2_32.112)
777 void WINAPI
WSASetLastError(INT iError
) {
778 SetLastError(iError
);
781 static struct WS_hostent
*check_buffer_he(int size
)
783 struct per_thread_data
* ptb
= get_per_thread_data();
786 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
787 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
789 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
790 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
791 return ptb
->he_buffer
;
794 static struct WS_servent
*check_buffer_se(int size
)
796 struct per_thread_data
* ptb
= get_per_thread_data();
799 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
800 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
802 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
803 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
804 return ptb
->se_buffer
;
807 static struct WS_protoent
*check_buffer_pe(int size
)
809 struct per_thread_data
* ptb
= get_per_thread_data();
812 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
813 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
815 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
816 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
817 return ptb
->pe_buffer
;
820 /* ----------------------------------- i/o APIs */
823 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf)== WS_AF_IPX || (pf) == WS_AF_INET6)
825 #define SUPPORTED_PF(pf) ((pf)==WS_AF_INET || (pf) == WS_AF_INET6)
829 /**********************************************************************/
831 /* Returns the converted address if successful, NULL if it was too small to
832 * start with. Note that the returned pointer may be the original pointer
833 * if no conversion is necessary.
835 static struct sockaddr
* ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
, unsigned int *uaddrlen
)
837 switch (wsaddr
->sa_family
)
842 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
843 struct sockaddr_ipx
* uipx
;
845 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
848 *uaddrlen
=sizeof(struct sockaddr_ipx
);
849 uipx
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *uaddrlen
);
850 uipx
->sipx_family
=AF_IPX
;
851 uipx
->sipx_port
=wsipx
->sa_socket
;
852 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
855 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
856 #ifdef IPX_FRAME_NONE
857 uipx
->sipx_type
=IPX_FRAME_NONE
;
859 return (struct sockaddr
*)uipx
;
863 struct sockaddr_in6
* uin6
;
864 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
866 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
867 * scope_id, one without. Check:
868 * http://msdn.microsoft.com/library/en-us/winsock/winsock/sockaddr_2.asp
870 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
871 *uaddrlen
=sizeof(struct sockaddr_in6
);
872 uin6
=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *uaddrlen
);
873 uin6
->sin6_family
= AF_INET6
;
874 uin6
->sin6_port
= win6
->sin6_port
;
875 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
876 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
877 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
879 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
880 return (struct sockaddr
*)uin6
;
882 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
886 struct sockaddr_in
* uin
;
887 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
889 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
891 *uaddrlen
=sizeof(struct sockaddr_in
);
892 uin
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *uaddrlen
);
893 uin
->sin_family
= AF_INET
;
894 uin
->sin_port
= win
->sin_port
;
895 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
896 return (struct sockaddr
*)uin
;
899 /* Try to determine the needed space by the passed windows sockaddr space */
901 default: /* likely a ipv4 address */
902 case sizeof(struct WS_sockaddr_in
):
903 *uaddrlen
= sizeof(struct sockaddr_in
);
906 case sizeof(struct WS_sockaddr_ipx
):
907 *uaddrlen
= sizeof(struct sockaddr_ipx
);
910 case sizeof(struct WS_sockaddr_in6
):
911 case sizeof(struct WS_sockaddr_in6_old
):
912 *uaddrlen
= sizeof(struct sockaddr_in6
);
915 return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,*uaddrlen
);
918 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
923 /* Allocates a Unix sockaddr structure to receive the data */
924 static inline struct sockaddr
* ws_sockaddr_alloc(const struct WS_sockaddr
* wsaddr
, int* wsaddrlen
, unsigned int* uaddrlen
)
928 ERR( "WINE shouldn't pass a NULL wsaddr! Attempting to continue\n" );
930 /* This is not strictly the right thing to do. Hope it works however */
939 *uaddrlen
=max(sizeof(struct sockaddr
),*wsaddrlen
);
941 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, *uaddrlen
);
944 /* Returns 0 if successful, -1 if the buffer is too small */
945 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, int uaddrlen
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
949 switch(uaddr
->sa_family
)
954 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
955 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
958 switch (*wsaddrlen
) /* how much can we copy? */
963 wsipx
->sa_socket
=uipx
->sipx_port
;
967 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
975 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
981 wsipx
->sa_family
=WS_AF_IPX
;
992 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
993 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
995 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
997 win6old
->sin6_family
= WS_AF_INET6
;
998 win6old
->sin6_port
= uin6
->sin6_port
;
999 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1000 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1001 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1002 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1003 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1004 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1005 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1006 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1012 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1013 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1015 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1017 win
->sin_family
= WS_AF_INET
;
1018 win
->sin_port
= uin
->sin_port
;
1019 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1020 memset(&win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1021 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1025 memset(wsaddr
,0,*wsaddrlen
);
1029 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1035 /* to be called to free the memory allocated by ws_sockaddr_ws2u or
1038 static inline void ws_sockaddr_free(const struct sockaddr
* uaddr
, const struct WS_sockaddr
* wsaddr
)
1040 if (uaddr
!=(const struct sockaddr
*)wsaddr
)
1041 HeapFree(GetProcessHeap(), 0, (void *)uaddr
);
1044 /**************************************************************************
1045 * Functions for handling overlapped I/O
1046 **************************************************************************/
1048 static void CALLBACK
ws2_async_terminate(ws2_async
* as
, IO_STATUS_BLOCK
* iosb
)
1050 TRACE( "as: %p uovl %p ovl %p\n", as
, as
->user_overlapped
, iosb
);
1052 if (as
->event
) NtSetEvent( as
->event
, NULL
);
1054 if (as
->completion_func
)
1055 as
->completion_func( NtStatusToWSAError (iosb
->u
.Status
),
1056 iosb
->Information
, as
->user_overlapped
, as
->flags
);
1057 if ( !as
->user_overlapped
)
1060 /* FIXME: I don't think this is really used */
1061 if ( as
->overlapped
->hEvent
!= INVALID_HANDLE_VALUE
)
1062 WSACloseEvent( as
->overlapped
->hEvent
);
1064 HeapFree( GetProcessHeap(), 0, iosb
);
1067 HeapFree( GetProcessHeap(), 0, as
->iovec
);
1068 HeapFree( GetProcessHeap(), 0, as
);
1071 /***********************************************************************
1072 * WS2_make_async (INTERNAL)
1075 static void WINAPI
WS2_async_recv(void*, IO_STATUS_BLOCK
*, ULONG
);
1076 static void WINAPI
WS2_async_send(void*, IO_STATUS_BLOCK
*, ULONG
);
1077 static void WINAPI
WS2_async_shutdown( void*, IO_STATUS_BLOCK
*, ULONG
);
1079 inline static struct ws2_async
*
1080 WS2_make_async(SOCKET s
, enum ws2_mode mode
, struct iovec
*iovec
, DWORD dwBufferCount
,
1081 LPDWORD lpFlags
, struct WS_sockaddr
*addr
,
1082 LPINT addrlen
, LPWSAOVERLAPPED lpOverlapped
,
1083 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
,
1084 IO_STATUS_BLOCK
**piosb
)
1086 struct ws2_async
*wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof( ws2_async
) );
1088 TRACE( "wsa %p\n", wsa
);
1093 wsa
->hSocket
= (HANDLE
) s
;
1099 wsa
->flags
= *lpFlags
;
1100 wsa
->addrlen
.ptr
= addrlen
;
1105 wsa
->addrlen
.val
= *addrlen
;
1108 ERR("Invalid async mode: %d\n", mode
);
1110 wsa
->user_overlapped
= lpOverlapped
;
1111 wsa
->completion_func
= lpCompletionRoutine
;
1113 wsa
->n_iovecs
= dwBufferCount
;
1119 *piosb
= (IO_STATUS_BLOCK
*)lpOverlapped
;
1120 if (!lpCompletionRoutine
)
1122 wsa
->event
= lpOverlapped
->hEvent
;
1123 NtResetEvent(wsa
->event
, NULL
);
1126 else if (!(*piosb
= HeapAlloc( GetProcessHeap(), 0, sizeof(IO_STATUS_BLOCK
))))
1129 (*piosb
)->Information
= 0;
1130 (*piosb
)->u
.Status
= STATUS_PENDING
;
1131 TRACE( "wsa %p, h %p, ev %p, iosb %p, uov %p, cfunc %p\n",
1132 wsa
, wsa
->hSocket
, wsa
->event
,
1133 *piosb
, wsa
->user_overlapped
, wsa
->completion_func
);
1139 HeapFree( GetProcessHeap(), 0, wsa
);
1143 static ULONG
ws2_queue_async(struct ws2_async
* wsa
, IO_STATUS_BLOCK
* iosb
)
1145 PIO_APC_ROUTINE apc
;
1151 case ws2m_read
: apc
= WS2_async_recv
; type
= ASYNC_TYPE_READ
; break;
1152 case ws2m_write
: apc
= WS2_async_send
; type
= ASYNC_TYPE_WRITE
; break;
1153 case ws2m_sd_read
: apc
= WS2_async_shutdown
; type
= ASYNC_TYPE_READ
; break;
1154 case ws2m_sd_write
: apc
= WS2_async_shutdown
; type
= ASYNC_TYPE_WRITE
; break;
1155 default: FIXME("Unknown internal mode (%d)\n", wsa
->mode
); return STATUS_INVALID_PARAMETER
;
1158 SERVER_START_REQ( register_async
)
1160 req
->handle
= wsa
->hSocket
;
1165 req
->count
= iosb
->Information
;
1166 status
= wine_server_call( req
);
1170 if ( status
) iosb
->u
.Status
= status
;
1171 if ( iosb
->u
.Status
!= STATUS_PENDING
)
1173 /* Note: we get here a non zero status when we couldn't queue the async
1174 * in the server. Therefore, we simply terminate the async.
1176 status
= iosb
->u
.Status
;
1177 ws2_async_terminate(wsa
, iosb
);
1180 NtCurrentTeb()->num_async_io
++;
1181 return STATUS_SUCCESS
;
1184 /***********************************************************************
1185 * WS2_recv (INTERNAL)
1187 * Workhorse for both synchronous and asynchronous recv() operations.
1189 static int WS2_recv( int fd
, struct iovec
* iov
, int count
,
1190 struct WS_sockaddr
*lpFrom
, LPINT lpFromlen
,
1195 TRACE( "fd %d, iovec %p, count %d addr %s, len %p, flags %x\n",
1196 fd
, iov
, count
, debugstr_sockaddr(lpFrom
), lpFromlen
, *lpFlags
);
1198 hdr
.msg_name
= NULL
;
1202 hdr
.msg_namelen
= *lpFromlen
;
1203 hdr
.msg_name
= ws_sockaddr_alloc( lpFrom
, lpFromlen
, &hdr
.msg_namelen
);
1204 if ( !hdr
.msg_name
)
1206 WSASetLastError( WSAEFAULT
);
1212 hdr
.msg_namelen
= 0;
1215 hdr
.msg_iovlen
= count
;
1216 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1217 hdr
.msg_accrights
= NULL
;
1218 hdr
.msg_accrightslen
= 0;
1220 hdr
.msg_control
= NULL
;
1221 hdr
.msg_controllen
= 0;
1225 if ( (n
= recvmsg(fd
, &hdr
, *lpFlags
)) == -1 )
1227 TRACE( "recvmsg error %d\n", errno
);
1232 ws_sockaddr_u2ws( hdr
.msg_name
, hdr
.msg_namelen
,
1233 lpFrom
, lpFromlen
) != 0 )
1235 /* The from buffer was too small, but we read the data
1236 * anyway. Is that really bad?
1238 WSASetLastError( WSAEFAULT
);
1239 WARN( "Address buffer too small\n" );
1244 ws_sockaddr_free( hdr
.msg_name
, lpFrom
);
1245 TRACE("-> %d\n", n
);
1249 /***********************************************************************
1250 * WS2_async_recv (INTERNAL)
1252 * Handler for overlapped recv() operations.
1254 static void WINAPI
WS2_async_recv( void* ovp
, IO_STATUS_BLOCK
* iosb
, ULONG status
)
1256 ws2_async
* wsa
= (ws2_async
*) ovp
;
1257 int result
, fd
, err
;
1259 TRACE( "(%p %p %x)\n", wsa
, iosb
, status
);
1263 case STATUS_ALERTED
:
1264 if ((iosb
->u
.Status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1266 ws2_async_terminate(wsa
, iosb
);
1269 result
= WS2_recv( fd
, wsa
->iovec
, wsa
->n_iovecs
,
1270 wsa
->addr
, wsa
->addrlen
.ptr
, &wsa
->flags
);
1271 wine_server_release_fd( wsa
->hSocket
, fd
);
1274 iosb
->u
.Status
= STATUS_SUCCESS
;
1275 iosb
->Information
= result
;
1276 TRACE( "received %d bytes\n", result
);
1277 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1282 if ( err
== WSAEINTR
|| err
== WSAEWOULDBLOCK
) /* errno: EINTR / EAGAIN */
1284 iosb
->u
.Status
= STATUS_PENDING
;
1285 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1286 TRACE( "still pending\n" );
1290 iosb
->u
.Status
= err
; /* FIXME: is this correct ???? */
1291 TRACE( "Error: %x\n", err
);
1294 if (iosb
->u
.Status
== STATUS_PENDING
)
1295 ws2_queue_async(wsa
, iosb
);
1297 ws2_async_terminate(wsa
, iosb
);
1300 FIXME( "status: %d\n", status
);
1301 iosb
->u
.Status
= status
;
1302 ws2_async_terminate(wsa
, iosb
);
1307 /***********************************************************************
1308 * WS2_send (INTERNAL)
1310 * Workhorse for both synchronous and asynchronous send() operations.
1312 static int WS2_send( int fd
, struct iovec
* iov
, int count
,
1313 const struct WS_sockaddr
*to
, INT tolen
, DWORD dwFlags
)
1317 TRACE( "fd %d, iovec %p, count %d addr %s, len %d, flags %x\n",
1318 fd
, iov
, count
, debugstr_sockaddr(to
), tolen
, dwFlags
);
1320 hdr
.msg_name
= NULL
;
1324 hdr
.msg_name
= (struct sockaddr
*) ws_sockaddr_ws2u( to
, tolen
, &hdr
.msg_namelen
);
1325 if ( !hdr
.msg_name
)
1327 WSASetLastError( WSAEFAULT
);
1333 if(to
->sa_family
== WS_AF_IPX
)
1336 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1338 unsigned int len
=sizeof(int);
1340 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1341 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1342 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1343 * ipx type in the sockaddr_opx structure with the stored value.
1345 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1347 TRACE("ptype: %d (fd:%d)\n", val
, fd
);
1348 uipx
->sipx_type
= val
;
1356 hdr
.msg_namelen
= 0;
1359 hdr
.msg_iovlen
= count
;
1360 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1361 hdr
.msg_accrights
= NULL
;
1362 hdr
.msg_accrightslen
= 0;
1364 hdr
.msg_control
= NULL
;
1365 hdr
.msg_controllen
= 0;
1369 n
= sendmsg(fd
, &hdr
, dwFlags
);
1372 ws_sockaddr_free( hdr
.msg_name
, to
);
1376 /***********************************************************************
1377 * WS2_async_send (INTERNAL)
1379 * Handler for overlapped send() operations.
1381 static void WINAPI
WS2_async_send(void* as
, IO_STATUS_BLOCK
* iosb
, ULONG status
)
1383 ws2_async
* wsa
= (ws2_async
*) as
;
1386 TRACE( "(%p %p %x)\n", wsa
, iosb
, status
);
1390 case STATUS_ALERTED
:
1391 if (iosb
->u
.Status
!= STATUS_PENDING
) FIXME("wrong %08x\n", iosb
->u
.Status
);
1392 if ((iosb
->u
.Status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1394 ws2_async_terminate(wsa
, iosb
);
1397 /* check to see if the data is ready (non-blocking) */
1398 result
= WS2_send( fd
, wsa
->iovec
, wsa
->n_iovecs
, wsa
->addr
, wsa
->addrlen
.val
, wsa
->flags
);
1399 wine_server_release_fd( wsa
->hSocket
, fd
);
1403 iosb
->u
.Status
= STATUS_SUCCESS
;
1404 iosb
->Information
= result
;
1405 TRACE( "sent %d bytes\n", result
);
1406 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1410 int err
= wsaErrno();
1411 if ( err
== WSAEINTR
)
1413 iosb
->u
.Status
= STATUS_PENDING
;
1414 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1415 TRACE( "still pending\n" );
1419 /* We set the status to a winsock error code and check for that
1420 later in NtStatusToWSAError () */
1421 iosb
->u
.Status
= err
;
1422 TRACE( "Error: %x\n", err
);
1425 if (iosb
->u
.Status
== STATUS_PENDING
)
1426 ws2_queue_async(wsa
, iosb
);
1428 ws2_async_terminate(wsa
, iosb
);
1431 FIXME( "status: %d\n", status
);
1432 iosb
->u
.Status
= status
;
1433 ws2_async_terminate(wsa
, iosb
);
1439 /***********************************************************************
1440 * WS2_async_shutdown (INTERNAL)
1442 * Handler for shutdown() operations on overlapped sockets.
1444 static void WINAPI
WS2_async_shutdown( void* as
, PIO_STATUS_BLOCK iosb
, ULONG status
)
1446 ws2_async
* wsa
= (ws2_async
*) as
;
1449 TRACE( "async %p %d\n", wsa
, wsa
->mode
);
1452 case STATUS_ALERTED
:
1453 if ((iosb
->u
.Status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1455 ws2_async_terminate(wsa
, iosb
);
1458 switch ( wsa
->mode
)
1460 case ws2m_sd_read
: err
= shutdown( fd
, 0 ); break;
1461 case ws2m_sd_write
: err
= shutdown( fd
, 1 ); break;
1462 default: ERR("invalid mode: %d\n", wsa
->mode
);
1464 wine_server_release_fd( wsa
->hSocket
, fd
);
1465 iosb
->u
.Status
= err
? wsaErrno() : STATUS_SUCCESS
;
1466 if (iosb
->u
.Status
== STATUS_PENDING
)
1467 ws2_queue_async(wsa
, iosb
);
1469 ws2_async_terminate(wsa
, iosb
);
1472 iosb
->u
.Status
= status
;
1473 ws2_async_terminate(wsa
, iosb
);
1479 /***********************************************************************
1480 * WS2_register_async_shutdown (INTERNAL)
1482 * Helper function for WS_shutdown() on overlapped sockets.
1484 static int WS2_register_async_shutdown( SOCKET s
, enum ws2_mode mode
)
1486 struct ws2_async
*wsa
;
1487 int ret
, err
= WSAEFAULT
;
1490 LPWSAOVERLAPPED ovl
= HeapAlloc(GetProcessHeap(), 0, sizeof( WSAOVERLAPPED
));
1491 IO_STATUS_BLOCK
*iosb
= NULL
;
1493 TRACE("s %d mode %d\n", s
, mode
);
1497 ovl
->hEvent
= WSACreateEvent();
1498 if ( ovl
->hEvent
== WSA_INVALID_EVENT
)
1501 wsa
= WS2_make_async( s
, mode
, NULL
, 0, &dwflags
, NULL
, &len
, ovl
, NULL
, &iosb
);
1505 /* Hack: this will cause ws2_async_terminate() to free the overlapped structure */
1506 wsa
->user_overlapped
= NULL
;
1507 if ( (ret
= ws2_queue_async( wsa
, iosb
)) )
1509 err
= NtStatusToWSAError( ret
);
1512 /* Try immediate completion */
1513 while ( WaitForSingleObjectEx( ovl
->hEvent
, 0, TRUE
) == STATUS_USER_APC
);
1517 WSACloseEvent( ovl
->hEvent
);
1519 HeapFree( GetProcessHeap(), 0, ovl
);
1524 /***********************************************************************
1527 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1533 TRACE("socket %04x\n", s
);
1534 is_blocking
= _is_blocking(s
);
1539 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1540 if (fd
== -1) return INVALID_SOCKET
;
1542 do_block(fd
, POLLIN
, -1);
1543 _sync_sock_state(s
); /* let wineserver notice connection */
1544 release_sock_fd( s
, fd
);
1545 /* retrieve any error codes from it */
1546 SetLastError(_get_sock_error(s
, FD_ACCEPT_BIT
));
1547 /* FIXME: care about the error? */
1549 SERVER_START_REQ( accept_socket
)
1551 req
->lhandle
= SOCKET2HANDLE(s
);
1552 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1553 req
->attributes
= OBJ_INHERIT
;
1554 set_error( wine_server_call( req
) );
1555 as
= HANDLE2SOCKET( reply
->handle
);
1560 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1563 } while (is_blocking
);
1564 return INVALID_SOCKET
;
1567 /***********************************************************************
1570 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1572 int fd
= get_sock_fd( s
, 0, NULL
);
1573 int res
= SOCKET_ERROR
;
1575 TRACE("socket %04x, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1579 if (!name
|| (name
->sa_family
&& !SUPPORTED_PF(name
->sa_family
)))
1581 SetLastError(WSAEAFNOSUPPORT
);
1585 const struct sockaddr
* uaddr
;
1586 unsigned int uaddrlen
;
1588 uaddr
=ws_sockaddr_ws2u(name
,namelen
,&uaddrlen
);
1591 SetLastError(WSAEFAULT
);
1595 if (bind(fd
, uaddr
, uaddrlen
) < 0)
1597 int loc_errno
= errno
;
1598 WARN("\tfailure - errno = %i\n", errno
);
1603 SetLastError(WSAENOTSOCK
);
1606 SetLastError(WSAEINVAL
);
1609 SetLastError(wsaErrno());
1615 res
=0; /* success */
1617 ws_sockaddr_free(uaddr
,name
);
1620 release_sock_fd( s
, fd
);
1625 /***********************************************************************
1626 * closesocket (WS2_32.3)
1628 int WINAPI
WS_closesocket(SOCKET s
)
1630 TRACE("socket %04x\n", s
);
1631 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
1632 return SOCKET_ERROR
;
1635 /***********************************************************************
1636 * connect (WS2_32.4)
1638 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1640 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1642 TRACE("socket %04x, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1646 const struct sockaddr
* uaddr
;
1647 unsigned int uaddrlen
;
1649 uaddr
=ws_sockaddr_ws2u(name
,namelen
,&uaddrlen
);
1652 SetLastError(WSAEFAULT
);
1658 rc
=connect(fd
, uaddr
, uaddrlen
);
1659 ws_sockaddr_free(uaddr
,name
);
1661 goto connect_success
;
1664 if (errno
== EINPROGRESS
)
1666 /* tell wineserver that a connection is in progress */
1667 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1668 FD_CONNECT
|FD_READ
|FD_WRITE
,
1669 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
1670 if (_is_blocking(s
))
1674 do_block(fd
, POLLIN
| POLLOUT
, -1);
1675 _sync_sock_state(s
); /* let wineserver notice connection */
1676 /* retrieve any error codes from it */
1677 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
1679 SetLastError(result
);
1682 goto connect_success
;
1687 SetLastError(WSAEWOULDBLOCK
);
1692 SetLastError(wsaErrno());
1694 release_sock_fd( s
, fd
);
1696 return SOCKET_ERROR
;
1699 release_sock_fd( s
, fd
);
1700 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1701 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
1702 FD_CONNECT
|FD_WINE_LISTENING
);
1706 /***********************************************************************
1707 * WSAConnect (WS2_32.30)
1709 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
1710 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
1711 LPQOS lpSQOS
, LPQOS lpGQOS
)
1713 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
1714 FIXME("unsupported parameters!\n");
1715 return WS_connect( s
, name
, namelen
);
1719 /***********************************************************************
1720 * getpeername (WS2_32.5)
1722 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1727 TRACE("socket: %04x, ptr %p, len %08x\n", s
, name
, *namelen
);
1729 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1730 if( (name
== NULL
) || (namelen
== NULL
) )
1732 SetLastError( WSAEFAULT
);
1733 return SOCKET_ERROR
;
1736 fd
= get_sock_fd( s
, 0, NULL
);
1741 struct sockaddr
* uaddr
;
1742 unsigned int uaddrlen
;
1744 uaddr
=ws_sockaddr_alloc(name
,namelen
,&uaddrlen
);
1745 if (getpeername(fd
, uaddr
, &uaddrlen
) != 0)
1747 SetLastError(wsaErrno());
1749 else if (ws_sockaddr_u2ws(uaddr
,uaddrlen
,name
,namelen
) != 0)
1751 /* The buffer was too small */
1752 SetLastError(WSAEFAULT
);
1758 ws_sockaddr_free(uaddr
,name
);
1759 release_sock_fd( s
, fd
);
1764 /***********************************************************************
1765 * getsockname (WS2_32.6)
1767 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1772 TRACE("socket: %04x, ptr %p, len %8x\n", s
, name
, *namelen
);
1774 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1775 if( (name
== NULL
) || (namelen
== NULL
) )
1777 SetLastError( WSAEFAULT
);
1778 return SOCKET_ERROR
;
1781 fd
= get_sock_fd( s
, 0, NULL
);
1786 struct sockaddr
* uaddr
;
1787 unsigned int uaddrlen
;
1789 uaddr
=ws_sockaddr_alloc(name
,namelen
,&uaddrlen
);
1790 if (getsockname(fd
, uaddr
, &uaddrlen
) != 0)
1792 SetLastError(wsaErrno());
1794 else if (ws_sockaddr_u2ws(uaddr
,uaddrlen
,name
,namelen
) != 0)
1796 /* The buffer was too small */
1797 SetLastError(WSAEFAULT
);
1803 ws_sockaddr_free(uaddr
,name
);
1804 release_sock_fd( s
, fd
);
1809 /***********************************************************************
1810 * getsockopt (WS2_32.7)
1812 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
1813 INT optname
, char *optval
, INT
*optlen
)
1818 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n",
1819 s
, level
, optname
, optval
, *optlen
);
1827 /* Handle common cases. The special cases are below, sorted
1829 case WS_SO_ACCEPTCONN
:
1830 case WS_SO_BROADCAST
:
1833 case WS_SO_KEEPALIVE
:
1834 case WS_SO_OOBINLINE
:
1838 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1839 return SOCKET_ERROR
;
1840 convert_sockopt(&level
, &optname
);
1841 if (getsockopt(fd
,(int) level
, optname
, optval
,
1842 (unsigned int *)optlen
) != 0 )
1844 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1847 release_sock_fd( s
, fd
);
1850 case WS_SO_DONTLINGER
:
1852 struct linger lingval
;
1853 unsigned int len
= sizeof(struct linger
);
1855 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
1857 SetLastError(WSAEFAULT
);
1858 return SOCKET_ERROR
;
1860 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1861 return SOCKET_ERROR
;
1863 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1865 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1870 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
1871 *optlen
= sizeof(BOOL
);
1874 release_sock_fd( s
, fd
);
1878 /* As mentioned in setsockopt, Windows ignores this, so we
1879 * always return true here */
1880 case WS_SO_DONTROUTE
:
1881 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
1883 SetLastError(WSAEFAULT
);
1884 return SOCKET_ERROR
;
1886 *(BOOL
*)optval
= TRUE
;
1887 *optlen
= sizeof(BOOL
);
1892 struct linger lingval
;
1893 unsigned int len
= sizeof(struct linger
);
1895 /* struct linger and LINGER have different sizes */
1896 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
1898 SetLastError(WSAEFAULT
);
1899 return SOCKET_ERROR
;
1901 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1902 return SOCKET_ERROR
;
1904 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1906 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1911 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
1912 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
1913 *optlen
= sizeof(struct linger
);
1916 release_sock_fd( s
, fd
);
1920 case WS_SO_MAX_MSG_SIZE
:
1921 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1923 SetLastError(WSAEFAULT
);
1924 return SOCKET_ERROR
;
1926 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1927 *(int *)optval
= 65507;
1928 *optlen
= sizeof(int);
1931 /* SO_OPENTYPE does not require a valid socket handle. */
1932 case WS_SO_OPENTYPE
:
1933 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1935 SetLastError(WSAEFAULT
);
1936 return SOCKET_ERROR
;
1938 *(int *)optval
= get_per_thread_data()->opentype
;
1939 *optlen
= sizeof(int);
1940 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
1944 case WS_SO_RCVTIMEO
:
1947 case WS_SO_SNDTIMEO
:
1949 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
1952 unsigned int len
= sizeof(struct timeval
);
1954 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
1956 SetLastError(WSAEFAULT
);
1957 return SOCKET_ERROR
;
1959 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1960 return SOCKET_ERROR
;
1962 convert_sockopt(&level
, &optname
);
1963 if (getsockopt(fd
,(int) level
, optname
, &tv
, &len
) != 0 )
1965 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1970 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1971 *optlen
= sizeof(int);
1974 release_sock_fd( s
, fd
);
1978 /* As mentioned in setsockopt, the windows style SO_REUSEADDR is
1979 * not possible in Unix, so always return false here. */
1980 case WS_SO_REUSEADDR
:
1981 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1983 SetLastError(WSAEFAULT
);
1984 return SOCKET_ERROR
;
1987 *optlen
= sizeof(int);
1991 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
1992 SetLastError(WSAENOPROTOOPT
);
1993 return SOCKET_ERROR
;
1994 } /* end switch(optname) */
1995 }/* end case WS_SOL_SOCKET */
1999 struct WS_sockaddr_ipx addr
;
2000 IPX_ADDRESS_DATA
*data
;
2005 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2007 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
2014 socklen_t len
=sizeof(struct ipx
);
2015 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2018 *optval
= (int)val
.ipx_pt
;
2021 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2022 release_sock_fd( s
, fd
);
2027 * On a Win2000 system with one network card there are usually
2028 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2029 * Using this call you can then retrieve info about this all.
2030 * In case of Linux it is a bit different. Usually you have
2031 * only "one" device active and further it is not possible to
2032 * query things like the linkspeed.
2034 FIXME("IPX_ADDRESS\n");
2035 namelen
= sizeof(struct WS_sockaddr_ipx
);
2036 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2037 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2039 data
= (IPX_ADDRESS_DATA
*)optval
;
2040 memcpy(data
->nodenum
,&addr
.sa_nodenum
,sizeof(data
->nodenum
));
2041 memcpy(data
->netnum
,&addr
.sa_netnum
,sizeof(data
->netnum
));
2042 data
->adapternum
= 0;
2043 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2044 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2045 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2046 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2047 * note 1MB = 1000kB in this case */
2050 case IPX_MAX_ADAPTER_NUM
:
2051 FIXME("IPX_MAX_ADAPTER_NUM\n");
2052 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2056 FIXME("IPX optname:%x\n", optname
);
2057 return SOCKET_ERROR
;
2058 }/* end switch(optname) */
2059 } /* end case NSPROTO_IPX */
2061 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2062 case WS_IPPROTO_TCP
:
2065 case WS_TCP_NODELAY
:
2066 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2067 return SOCKET_ERROR
;
2068 convert_sockopt(&level
, &optname
);
2069 if (getsockopt(fd
,(int) level
, optname
, optval
,
2070 (unsigned int *)optlen
) != 0 )
2072 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2075 release_sock_fd( s
, fd
);
2078 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2079 return SOCKET_ERROR
;
2084 case WS_IP_ADD_MEMBERSHIP
:
2085 case WS_IP_DROP_MEMBERSHIP
:
2089 case WS_IP_MULTICAST_IF
:
2090 case WS_IP_MULTICAST_LOOP
:
2091 case WS_IP_MULTICAST_TTL
:
2095 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2096 return SOCKET_ERROR
;
2097 convert_sockopt(&level
, &optname
);
2098 if (getsockopt(fd
,(int) level
, optname
, optval
,
2099 (unsigned int *)optlen
) != 0 )
2101 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2104 release_sock_fd( s
, fd
);
2107 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2108 return SOCKET_ERROR
;
2111 FIXME("Unknown level: 0x%08x\n", level
);
2112 return SOCKET_ERROR
;
2113 } /* end switch(level) */
2116 /***********************************************************************
2120 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2122 return htonl(hostlong
);
2126 /***********************************************************************
2130 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2132 return htons(hostshort
);
2135 /***********************************************************************
2136 * WSAHtonl (WS2_32.46)
2137 * From MSDN decription of error codes, this function should also
2138 * check if WinSock has been initialized and the socket is a valid
2139 * socket. But why? This function only translates a host byte order
2140 * u_long into a network byte order u_long...
2142 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2146 *lpnetlong
= htonl(hostlong
);
2149 WSASetLastError(WSAEFAULT
);
2150 return SOCKET_ERROR
;
2153 /***********************************************************************
2154 * WSAHtons (WS2_32.47)
2155 * From MSDN decription of error codes, this function should also
2156 * check if WinSock has been initialized and the socket is a valid
2157 * socket. But why? This function only translates a host byte order
2158 * u_short into a network byte order u_short...
2160 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2165 *lpnetshort
= htons(hostshort
);
2168 WSASetLastError(WSAEFAULT
);
2169 return SOCKET_ERROR
;
2173 /***********************************************************************
2174 * inet_addr (WINSOCK.10)
2175 * inet_addr (WS2_32.11)
2177 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2179 if (!cp
) return INADDR_NONE
;
2180 return inet_addr(cp
);
2184 /***********************************************************************
2185 * ntohl (WINSOCK.14)
2188 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2190 return ntohl(netlong
);
2194 /***********************************************************************
2195 * ntohs (WINSOCK.15)
2198 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2200 return ntohs(netshort
);
2204 /***********************************************************************
2205 * inet_ntoa (WS2_32.12)
2207 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2209 /* use "buffer for dummies" here because some applications have a
2210 * propensity to decode addresses in ws_hostent structure without
2211 * saving them first...
2213 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2215 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2221 SetLastError(wsaErrno());
2225 /**********************************************************************
2226 * WSAIoctl (WS2_32.50)
2229 INT WINAPI
WSAIoctl(SOCKET s
,
2230 DWORD dwIoControlCode
,
2233 LPVOID lpbOutBuffer
,
2235 LPDWORD lpcbBytesReturned
,
2236 LPWSAOVERLAPPED lpOverlapped
,
2237 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2239 TRACE("%d, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2240 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
2241 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
2243 switch( dwIoControlCode
)
2246 if (cbInBuffer
!= sizeof(WS_u_long
)) {
2247 WSASetLastError(WSAEFAULT
);
2248 return SOCKET_ERROR
;
2250 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
2253 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
2254 WSASetLastError(WSAEFAULT
);
2255 return SOCKET_ERROR
;
2257 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
2259 case WS_SIO_GET_INTERFACE_LIST
:
2261 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
2262 DWORD size
, numInt
, apiReturn
;
2265 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2269 WSASetLastError(WSAEFAULT
);
2270 return SOCKET_ERROR
;
2272 if (!lpcbBytesReturned
)
2274 WSASetLastError(WSAEFAULT
);
2275 return SOCKET_ERROR
;
2278 fd
= get_sock_fd( s
, 0, NULL
);
2279 if (fd
== -1) return SOCKET_ERROR
;
2281 apiReturn
= GetAdaptersInfo(NULL
, &size
);
2282 if (apiReturn
== ERROR_NO_DATA
)
2286 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
2288 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
2292 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
2294 PIP_ADAPTER_INFO ptr
;
2296 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
2298 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
2299 HeapFree(GetProcessHeap(),0,table
);
2300 release_sock_fd( s
, fd
);
2301 WSASetLastError(WSAEFAULT
);
2302 return SOCKET_ERROR
;
2304 for (ptr
= table
, numInt
= 0; ptr
;
2305 ptr
= ptr
->Next
, intArray
++, numInt
++)
2307 unsigned int addr
, mask
, bcast
;
2308 struct ifreq ifInfo
;
2310 /* Socket Status Flags */
2311 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
2312 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
2314 ERR("Error obtaining status flags for socket!\n");
2315 HeapFree(GetProcessHeap(),0,table
);
2316 release_sock_fd( s
, fd
);
2317 WSASetLastError(WSAEINVAL
);
2318 return SOCKET_ERROR
;
2322 /* set flags; the values of IFF_* are not the same
2323 under Linux and Windows, therefore must generate
2325 intArray
->iiFlags
= 0;
2326 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
2327 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
2328 #ifdef IFF_POINTOPOINT
2329 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
2330 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
2332 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
2333 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
2334 if (ifInfo
.ifr_flags
& IFF_UP
)
2335 intArray
->iiFlags
|= WS_IFF_UP
;
2336 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
2337 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
2340 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
2341 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
2342 bcast
= addr
| (addr
& !mask
);
2343 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
2344 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
2345 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
2347 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
2348 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
2349 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
2351 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
2353 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
2354 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
2360 ERR("Unable to get interface table!\n");
2361 release_sock_fd( s
, fd
);
2362 HeapFree(GetProcessHeap(),0,table
);
2363 WSASetLastError(WSAEINVAL
);
2364 return SOCKET_ERROR
;
2366 HeapFree(GetProcessHeap(),0,table
);
2370 release_sock_fd( s
, fd
);
2371 WSASetLastError(WSAEINVAL
);
2372 return SOCKET_ERROR
;
2377 ERR("Unable to get interface table!\n");
2378 release_sock_fd( s
, fd
);
2379 WSASetLastError(WSAEINVAL
);
2380 return SOCKET_ERROR
;
2382 /* Calculate the size of the array being returned */
2383 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
2384 release_sock_fd( s
, fd
);
2388 case WS_SIO_ADDRESS_LIST_CHANGE
:
2389 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2390 /* FIXME: error and return code depend on whether socket was created
2391 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2394 case WS_SIO_ADDRESS_LIST_QUERY
:
2398 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2400 if (!lpcbBytesReturned
)
2402 WSASetLastError(WSAEFAULT
);
2403 return SOCKET_ERROR
;
2406 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
2408 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
2411 if (!table
|| GetAdaptersInfo(table
, &size
))
2413 HeapFree(GetProcessHeap(), 0, table
);
2414 WSASetLastError(WSAEINVAL
);
2415 return SOCKET_ERROR
;
2418 for (p
= table
, num
= 0; p
; p
= p
->Next
)
2419 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
2421 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
2422 need
+= sizeof(SOCKADDR
) * num
;
2423 *lpcbBytesReturned
= need
;
2425 if (need
> cbOutBuffer
)
2427 HeapFree(GetProcessHeap(), 0, table
);
2428 WSASetLastError(WSAEFAULT
);
2429 return SOCKET_ERROR
;
2436 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
2437 SOCKADDR_IN
*sockaddr
;
2439 sa
= sa_list
->Address
;
2440 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
2441 sa_list
->iAddressCount
= num
;
2443 for (p
= table
, i
= 0; p
; p
= p
->Next
)
2445 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
2447 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
2448 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
2450 sockaddr
[i
].sin_family
= AF_INET
;
2451 sockaddr
[i
].sin_port
= 0;
2452 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
2457 HeapFree(GetProcessHeap(), 0, table
);
2462 WARN("unable to get IP address list\n");
2463 WSASetLastError(WSAEINVAL
);
2464 return SOCKET_ERROR
;
2468 FIXME("SIO_FLUSH: stub.\n");
2472 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode
);
2473 WSASetLastError(WSAEOPNOTSUPP
);
2474 return SOCKET_ERROR
;
2481 /***********************************************************************
2482 * ioctlsocket (WS2_32.10)
2484 int WINAPI
WS_ioctlsocket(SOCKET s
, long cmd
, WS_u_long
*argp
)
2489 TRACE("socket %04x, cmd %08lx, ptr %p\n", s
, cmd
, argp
);
2498 if( _get_sock_mask(s
) )
2500 /* AsyncSelect()'ed sockets are always nonblocking */
2501 if (*argp
) return 0;
2502 SetLastError(WSAEINVAL
);
2503 return SOCKET_ERROR
;
2505 fd
= get_sock_fd( s
, 0, NULL
);
2511 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
2512 ret
= fcntl( fd
, F_SETFL
, O_NONBLOCK
);
2516 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
2517 ret
= fcntl( fd
, F_SETFL
, 0 );
2519 release_sock_fd( s
, fd
);
2521 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2523 return SOCKET_ERROR
;
2530 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2531 SetLastError(WSAEINVAL
);
2532 return SOCKET_ERROR
;
2534 case SIOCGIFBRDADDR
:
2535 case SIOCGIFNETMASK
:
2537 /* These don't need any special handling. They are used by
2538 WsControl, and are here to suppress an unnecessary warning. */
2542 /* Netscape tries hard to use bogus ioctl 0x667e */
2543 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2544 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2546 WARN("\tunknown WS_IOCTL cmd (%08lx)\n", cmd
);
2550 fd
= get_sock_fd( s
, 0, NULL
);
2553 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
2555 release_sock_fd( s
, fd
);
2558 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2559 release_sock_fd( s
, fd
);
2561 return SOCKET_ERROR
;
2564 /***********************************************************************
2565 * listen (WS2_32.13)
2567 int WINAPI
WS_listen(SOCKET s
, int backlog
)
2569 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2571 TRACE("socket %04x, backlog %d\n", s
, backlog
);
2574 if (listen(fd
, backlog
) == 0)
2576 release_sock_fd( s
, fd
);
2577 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
2579 FD_CONNECT
|FD_WINE_CONNECTED
);
2582 SetLastError(wsaErrno());
2583 release_sock_fd( s
, fd
);
2585 return SOCKET_ERROR
;
2588 /***********************************************************************
2591 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
2593 DWORD n
, dwFlags
= flags
;
2599 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
2600 return SOCKET_ERROR
;
2605 /***********************************************************************
2606 * recvfrom (WS2_32.17)
2608 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
2609 struct WS_sockaddr
*from
, int *fromlen
)
2611 DWORD n
, dwFlags
= flags
;
2617 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
) == SOCKET_ERROR
)
2618 return SOCKET_ERROR
;
2623 /* allocate a poll array for the corresponding fd sets */
2624 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2625 const WS_fd_set
*exceptfds
, int *count_ptr
)
2627 int i
, j
= 0, count
= 0;
2630 if (readfds
) count
+= readfds
->fd_count
;
2631 if (writefds
) count
+= writefds
->fd_count
;
2632 if (exceptfds
) count
+= exceptfds
->fd_count
;
2634 if (!count
) return NULL
;
2635 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0])))) return NULL
;
2637 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2639 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
2640 fds
[j
].events
= POLLIN
;
2644 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2646 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
2647 fds
[j
].events
= POLLOUT
;
2651 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2653 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
2654 fds
[j
].events
= POLLHUP
;
2660 /* release the file descriptor obtained in fd_sets_to_poll */
2661 /* must be called with the original fd_set arrays, before calling get_poll_results */
2662 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2663 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
2669 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2670 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
2674 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2675 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
2679 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2680 if (fds
[j
].fd
!= -1)
2682 /* make sure we have a real error before releasing the fd */
2683 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
2684 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
2689 /* map the poll results back into the Windows fd sets */
2690 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
2691 const struct pollfd
*fds
)
2693 int i
, j
= 0, k
, total
= 0;
2697 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
2698 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
2699 readfds
->fd_count
= k
;
2704 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
2705 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
2706 writefds
->fd_count
= k
;
2711 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2712 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
2713 exceptfds
->fd_count
= k
;
2720 /***********************************************************************
2721 * select (WS2_32.18)
2723 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
2724 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
2725 const struct WS_timeval
* ws_timeout
)
2727 struct pollfd
*pollfds
;
2728 int count
, ret
, timeout
= -1;
2730 TRACE("read %p, write %p, excp %p timeout %p\n",
2731 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
2733 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)) && count
)
2735 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2736 return SOCKET_ERROR
;
2739 if (ws_timeout
) timeout
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
2741 ret
= poll( pollfds
, count
, timeout
);
2742 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2744 if (ret
== -1) SetLastError(wsaErrno());
2745 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2746 HeapFree( GetProcessHeap(), 0, pollfds
);
2751 /***********************************************************************
2754 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
2760 wsabuf
.buf
= (char*) buf
;
2762 if ( WSASendTo( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
2763 return SOCKET_ERROR
;
2768 /***********************************************************************
2769 * WSASend (WS2_32.72)
2771 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2772 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2773 LPWSAOVERLAPPED lpOverlapped
,
2774 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2776 return WSASendTo( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
2777 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
2780 /***********************************************************************
2781 * WSASendDisconnect (WS2_32.73)
2783 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
2785 return WS_shutdown( s
, SD_SEND
);
2789 /***********************************************************************
2790 * WSASendTo (WS2_32.74)
2792 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2793 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2794 const struct WS_sockaddr
*to
, int tolen
,
2795 LPWSAOVERLAPPED lpOverlapped
,
2796 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2799 int n
, fd
, err
= WSAENOTSOCK
, flags
, ret
;
2800 struct iovec
* iovec
;
2801 struct ws2_async
*wsa
;
2802 IO_STATUS_BLOCK
* iosb
;
2804 TRACE("socket %04x, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
2805 s
, lpBuffers
, dwBufferCount
, dwFlags
,
2806 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
2808 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &flags
);
2809 TRACE( "fd=%d, flags=%x\n", fd
, flags
);
2811 if ( fd
== -1 ) return SOCKET_ERROR
;
2813 if (flags
& FD_FLAG_SEND_SHUTDOWN
)
2815 WSASetLastError( WSAESHUTDOWN
);
2819 if ( !lpNumberOfBytesSent
)
2825 iovec
= HeapAlloc(GetProcessHeap(), 0, dwBufferCount
* sizeof(struct iovec
) );
2833 for ( i
= 0; i
< dwBufferCount
; i
++ )
2835 iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
2836 iovec
[i
].iov_len
= lpBuffers
[i
].len
;
2839 if ( (lpOverlapped
|| lpCompletionRoutine
) && flags
& FD_FLAG_OVERLAPPED
)
2841 wsa
= WS2_make_async( s
, ws2m_write
, iovec
, dwBufferCount
,
2842 &dwFlags
, (struct WS_sockaddr
*) to
, &tolen
,
2843 lpOverlapped
, lpCompletionRoutine
, &iosb
);
2850 if ( ( ret
= ws2_queue_async( wsa
, iosb
) ) )
2852 err
= NtStatusToWSAError( ret
);
2854 if ( !lpOverlapped
)
2855 HeapFree( GetProcessHeap(), 0, iosb
);
2856 HeapFree( GetProcessHeap(), 0, wsa
);
2859 release_sock_fd( s
, fd
);
2861 /* Try immediate completion */
2864 if ( WSAGetOverlappedResult( s
, lpOverlapped
,
2865 lpNumberOfBytesSent
, FALSE
, &dwFlags
) )
2868 if ( (err
= WSAGetLastError()) != WSA_IO_INCOMPLETE
)
2872 WSASetLastError( WSA_IO_PENDING
);
2873 return SOCKET_ERROR
;
2876 if (_is_blocking(s
))
2878 /* FIXME: exceptfds? */
2879 int timeout
= GET_SNDTIMEO(fd
);
2880 if( !do_block(fd
, POLLOUT
, timeout
)) {
2882 goto err_free
; /* msdn says a timeout in send is fatal */
2886 n
= WS2_send( fd
, iovec
, dwBufferCount
, to
, tolen
, dwFlags
);
2890 if ( err
== WSAEWOULDBLOCK
)
2891 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
2895 TRACE(" -> %i bytes\n", n
);
2896 *lpNumberOfBytesSent
= n
;
2898 HeapFree( GetProcessHeap(), 0, iovec
);
2899 release_sock_fd( s
, fd
);
2903 HeapFree( GetProcessHeap(), 0, iovec
);
2906 release_sock_fd( s
, fd
);
2909 WARN(" -> ERROR %d\n", err
);
2910 WSASetLastError(err
);
2911 return SOCKET_ERROR
;
2914 /***********************************************************************
2915 * sendto (WS2_32.20)
2917 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
2918 const struct WS_sockaddr
*to
, int tolen
)
2924 wsabuf
.buf
= (char*) buf
;
2926 if ( WSASendTo(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
2927 return SOCKET_ERROR
;
2932 /***********************************************************************
2933 * setsockopt (WS2_32.21)
2935 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
2936 const char *optval
, int optlen
)
2940 struct linger linger
;
2941 struct timeval tval
;
2943 TRACE("socket: %04x, level 0x%x, name 0x%x, ptr %p, len %d\n",
2944 s
, level
, optname
, optval
, optlen
);
2951 /* Some options need some conversion before they can be sent to
2952 * setsockopt. The conversions are done here, then they will fall though
2953 * to the general case. Special options that are not passed to
2954 * setsockopt follow below that.*/
2956 case WS_SO_DONTLINGER
:
2957 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
2958 linger
.l_linger
= 0;
2960 optname
= SO_LINGER
;
2961 optval
= (char*)&linger
;
2962 optlen
= sizeof(struct linger
);
2966 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
2967 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
2968 /* FIXME: what is documented behavior if SO_LINGER optval
2971 optname
= SO_LINGER
;
2972 optval
= (char*)&linger
;
2973 optlen
= sizeof(struct linger
);
2977 if (*(const int*)optval
< 2048)
2979 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
2984 /* The options listed here don't need any special handling. Thanks to
2985 * the conversion happening above, options from there will fall through
2987 case WS_SO_ACCEPTCONN
:
2988 case WS_SO_BROADCAST
:
2990 case WS_SO_KEEPALIVE
:
2991 case WS_SO_OOBINLINE
:
2994 convert_sockopt(&level
, &optname
);
2997 /* SO_DEBUG is a privileged operation, ignore it. */
2999 TRACE("Ignoring SO_DEBUG\n");
3002 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
3003 * socket. According to MSDN, this option is silently ignored.*/
3004 case WS_SO_DONTROUTE
:
3005 TRACE("Ignoring SO_DONTROUTE\n");
3008 /* Stops two sockets from being bound to the same port. Always happens
3009 * on unix systems, so just drop it. */
3010 case WS_SO_EXCLUSIVEADDRUSE
:
3011 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
3014 /* SO_OPENTYPE does not require a valid socket handle. */
3015 case WS_SO_OPENTYPE
:
3016 if (!optlen
|| optlen
< sizeof(int) || !optval
)
3018 SetLastError(WSAEFAULT
);
3019 return SOCKET_ERROR
;
3021 get_per_thread_data()->opentype
= *(const int *)optval
;
3022 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
3025 /* SO_REUSEADDR allows two applications to bind to the same port at at
3026 * same time. There is no direct way to do that in unix. While Wineserver
3027 * might do this, it does not seem useful for now, so just ignore it.*/
3028 case WS_SO_REUSEADDR
:
3029 TRACE("Ignoring SO_REUSEADDR, does not translate\n");
3033 case WS_SO_RCVTIMEO
:
3036 case WS_SO_SNDTIMEO
:
3038 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
3039 if (optval
&& optlen
== sizeof(UINT32
)) {
3040 /* WinSock passes miliseconds instead of struct timeval */
3041 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
3042 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
3043 /* min of 500 milisec */
3044 if (tval
.tv_sec
== 0 && tval
.tv_usec
< 500000)
3045 tval
.tv_usec
= 500000;
3046 optlen
= sizeof(struct timeval
);
3047 optval
= (char*)&tval
;
3048 } else if (optlen
== sizeof(struct timeval
)) {
3049 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
3051 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
3054 convert_sockopt(&level
, &optname
);
3059 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
3060 SetLastError(WSAENOPROTOOPT
);
3061 return SOCKET_ERROR
;
3063 break; /* case WS_SOL_SOCKET */
3070 fd
= get_sock_fd( s
, 0, NULL
);
3071 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
3073 /* We try to set the ipx type on ipx socket level. */
3075 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
3077 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
3078 release_sock_fd( s
, fd
);
3079 return SOCKET_ERROR
;
3084 /* Should we retrieve val using a getsockopt call and then
3085 * set the modified one? */
3086 val
.ipx_pt
= *optval
;
3087 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
3090 release_sock_fd( s
, fd
);
3093 case IPX_FILTERPTYPE
:
3094 /* Sets the receive filter packet type, at the moment we don't support it */
3095 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
3096 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3100 FIXME("opt_name:%x\n", optname
);
3101 return SOCKET_ERROR
;
3103 break; /* case NSPROTO_IPX */
3106 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3107 case WS_IPPROTO_TCP
:
3110 case WS_TCP_NODELAY
:
3111 convert_sockopt(&level
, &optname
);
3114 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3115 return SOCKET_ERROR
;
3122 case WS_IP_ADD_MEMBERSHIP
:
3123 case WS_IP_DROP_MEMBERSHIP
:
3127 case WS_IP_MULTICAST_IF
:
3128 case WS_IP_MULTICAST_LOOP
:
3129 case WS_IP_MULTICAST_TTL
:
3133 convert_sockopt(&level
, &optname
);
3136 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3137 return SOCKET_ERROR
;
3142 FIXME("Unknown level: 0x%08x\n", level
);
3143 return SOCKET_ERROR
;
3144 } /* end switch(level) */
3146 /* avoid endianness issues if argument is a 16-bit int */
3147 if (optval
&& optlen
< sizeof(int))
3149 woptval
= *((const INT16
*) optval
);
3150 optval
= (char*) &woptval
;
3153 fd
= get_sock_fd( s
, 0, NULL
);
3154 if (fd
== -1) return SOCKET_ERROR
;
3156 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
3158 release_sock_fd( s
, fd
);
3161 TRACE("Setting socket error, %d\n", wsaErrno());
3162 SetLastError(wsaErrno());
3163 release_sock_fd( s
, fd
);
3165 return SOCKET_ERROR
;
3168 /***********************************************************************
3169 * shutdown (WS2_32.22)
3171 int WINAPI
WS_shutdown(SOCKET s
, int how
)
3173 int fd
, flags
, err
= WSAENOTSOCK
;
3174 unsigned int clear_flags
= 0;
3176 fd
= get_sock_fd( s
, 0, &flags
);
3177 TRACE("socket %04x, how %i %x\n", s
, how
, flags
);
3180 return SOCKET_ERROR
;
3184 case 0: /* drop receives */
3185 clear_flags
|= FD_READ
;
3187 case 1: /* drop sends */
3188 clear_flags
|= FD_WRITE
;
3190 case 2: /* drop all */
3191 clear_flags
|= FD_READ
|FD_WRITE
;
3193 clear_flags
|= FD_WINE_LISTENING
;
3196 if ( flags
& FD_FLAG_OVERLAPPED
) {
3201 err
= WS2_register_async_shutdown( s
, ws2m_sd_read
);
3204 err
= WS2_register_async_shutdown( s
, ws2m_sd_write
);
3208 err
= WS2_register_async_shutdown( s
, ws2m_sd_read
);
3209 if (!err
) err
= WS2_register_async_shutdown( s
, ws2m_sd_write
);
3212 if (err
) goto error
;
3214 else /* non-overlapped mode */
3216 if ( shutdown( fd
, how
) )
3223 release_sock_fd( s
, fd
);
3224 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3225 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
3229 release_sock_fd( s
, fd
);
3230 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3231 WSASetLastError( err
);
3232 return SOCKET_ERROR
;
3235 /***********************************************************************
3236 * socket (WS2_32.23)
3238 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
3240 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
3242 return WSASocketA( af
, type
, protocol
, NULL
, 0,
3243 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
3247 /***********************************************************************
3248 * gethostbyaddr (WS2_32.51)
3250 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
3252 struct WS_hostent
*retval
= NULL
;
3253 struct hostent
* host
;
3255 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3258 struct hostent hostentry
;
3261 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3263 int res
= gethostbyaddr_r(addr
, len
, type
,
3264 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3265 if( res
!= ERANGE
) break;
3267 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3269 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3271 EnterCriticalSection( &csWSgetXXXbyYYY
);
3272 host
= gethostbyaddr(addr
, len
, type
);
3273 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3275 if( host
!= NULL
) retval
= WS_dup_he(host
);
3276 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3277 HeapFree(GetProcessHeap(),0,extrabuf
);
3279 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3281 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
3285 /***********************************************************************
3286 * gethostbyname (WS2_32.52)
3288 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
3290 struct WS_hostent
*retval
= NULL
;
3291 struct hostent
* host
;
3292 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3295 struct hostent hostentry
;
3296 int locerr
= ENOBUFS
;
3301 if( gethostname( buf
, 100) == -1) {
3302 SetLastError( WSAENOBUFS
); /* appropriate ? */
3306 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3308 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3310 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3311 if( res
!= ERANGE
) break;
3313 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3315 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3317 EnterCriticalSection( &csWSgetXXXbyYYY
);
3318 host
= gethostbyname(name
);
3319 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3321 if (host
) retval
= WS_dup_he(host
);
3322 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3323 HeapFree(GetProcessHeap(),0,extrabuf
);
3325 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3327 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3332 /***********************************************************************
3333 * getprotobyname (WS2_32.53)
3335 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
3337 struct WS_protoent
* retval
= NULL
;
3338 #ifdef HAVE_GETPROTOBYNAME
3339 struct protoent
* proto
;
3340 EnterCriticalSection( &csWSgetXXXbyYYY
);
3341 if( (proto
= getprotobyname(name
)) != NULL
)
3343 retval
= WS_dup_pe(proto
);
3346 MESSAGE("protocol %s not found; You might want to add "
3347 "this to /etc/protocols\n", debugstr_a(name
) );
3348 SetLastError(WSANO_DATA
);
3350 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3352 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3357 /***********************************************************************
3358 * getprotobynumber (WS2_32.54)
3360 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
3362 struct WS_protoent
* retval
= NULL
;
3363 #ifdef HAVE_GETPROTOBYNUMBER
3364 struct protoent
* proto
;
3365 EnterCriticalSection( &csWSgetXXXbyYYY
);
3366 if( (proto
= getprotobynumber(number
)) != NULL
)
3368 retval
= WS_dup_pe(proto
);
3371 MESSAGE("protocol number %d not found; You might want to add "
3372 "this to /etc/protocols\n", number
);
3373 SetLastError(WSANO_DATA
);
3375 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3377 TRACE("%i ret %p\n", number
, retval
);
3382 /***********************************************************************
3383 * getservbyname (WS2_32.55)
3385 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
3387 struct WS_servent
* retval
= NULL
;
3388 struct servent
* serv
;
3390 char *proto_str
= NULL
;
3392 if (!(name_str
= strdup_lower(name
))) return NULL
;
3394 if (proto
&& *proto
)
3396 if (!(proto_str
= strdup_lower(proto
)))
3398 HeapFree( GetProcessHeap(), 0, name_str
);
3403 EnterCriticalSection( &csWSgetXXXbyYYY
);
3404 serv
= getservbyname(name_str
, proto_str
);
3407 retval
= WS_dup_se(serv
);
3409 else SetLastError(WSANO_DATA
);
3410 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3411 HeapFree( GetProcessHeap(), 0, proto_str
);
3412 HeapFree( GetProcessHeap(), 0, name_str
);
3413 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
3417 /***********************************************************************
3418 * freeaddrinfo (WS2_32.@)
3420 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
3423 struct WS_addrinfo
*next
;
3425 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
3426 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
3427 next
= res
->ai_next
;
3428 HeapFree(GetProcessHeap(),0,res
);
3433 /* helper functions for getaddrinfo() */
3434 static int convert_aiflag_w2u(int winflags
) {
3435 int i
, unixflags
= 0;
3437 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3438 if (ws_aiflag_map
[i
][0] & winflags
) {
3439 unixflags
|= ws_aiflag_map
[i
][1];
3440 winflags
&= ~ws_aiflag_map
[i
][0];
3443 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
3447 static int convert_aiflag_u2w(int unixflags
) {
3448 int i
, winflags
= 0;
3450 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3451 if (ws_aiflag_map
[i
][1] & unixflags
) {
3452 winflags
|= ws_aiflag_map
[i
][0];
3453 unixflags
&= ~ws_aiflag_map
[i
][1];
3455 if (unixflags
) /* will warn usually */
3456 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
3460 static int convert_eai_u2w(int unixret
) {
3463 for (i
=0;ws_eai_map
[i
][0];i
++)
3464 if (ws_eai_map
[i
][1] == unixret
)
3465 return ws_eai_map
[i
][0];
3469 /***********************************************************************
3470 * getaddrinfo (WS2_32.@)
3472 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
3474 #if HAVE_GETADDRINFO
3475 struct addrinfo
*unixaires
= NULL
;
3477 struct addrinfo unixhints
, *punixhints
= NULL
;
3478 CHAR
*node
= NULL
, *serv
= NULL
;
3481 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
3484 if (!(serv
= strdup_lower(servname
))) {
3485 HeapFree(GetProcessHeap(), 0, node
);
3486 return WSA_NOT_ENOUGH_MEMORY
;
3491 punixhints
= &unixhints
;
3493 memset(&unixhints
, 0, sizeof(unixhints
));
3494 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
3495 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
3496 punixhints
->ai_family
= 0;
3498 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
3499 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
3500 punixhints
->ai_socktype
= 0;
3502 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
3503 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
3504 punixhints
->ai_protocol
= 0;
3506 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
3509 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3510 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
3512 TRACE("%s, %s %p -> %p %d\n", nodename
, servname
, hints
, res
, result
);
3514 HeapFree(GetProcessHeap(), 0, node
);
3515 HeapFree(GetProcessHeap(), 0, serv
);
3518 struct addrinfo
*xuai
= unixaires
;
3519 struct WS_addrinfo
**xai
= res
;
3523 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
3529 *xai
= ai
;xai
= &ai
->ai_next
;
3530 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
3531 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
3532 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
3533 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
3534 if (xuai
->ai_canonname
) {
3535 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
3536 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
3537 if (!ai
->ai_canonname
)
3539 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
3541 len
= xuai
->ai_addrlen
;
3542 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
3545 ai
->ai_addrlen
= len
;
3547 int winlen
= ai
->ai_addrlen
;
3549 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, xuai
->ai_addrlen
, ai
->ai_addr
, &winlen
)) {
3550 ai
->ai_addrlen
= winlen
;
3554 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
3557 ai
->ai_addrlen
= len
;
3559 xuai
= xuai
->ai_next
;
3561 freeaddrinfo(unixaires
);
3563 result
= convert_eai_u2w(result
);
3568 if (*res
) WS_freeaddrinfo(*res
);
3569 if (unixaires
) freeaddrinfo(unixaires
);
3571 return WSA_NOT_ENOUGH_MEMORY
;
3573 FIXME("getaddrinfo() failed, not found during buildtime.\n");
3578 /***********************************************************************
3579 * GetAddrInfoW (WS2_32.@)
3581 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
3583 FIXME("empty stub!\n");
3587 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
3588 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
3590 #if HAVE_GETNAMEINFO
3592 const struct sockaddr
* sa_u
;
3595 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
3596 serv
, servlen
, flags
);
3598 sa_u
= ws_sockaddr_ws2u(sa
, salen
, &size
);
3601 WSASetLastError(WSAEFAULT
);
3602 return WSA_NOT_ENOUGH_MEMORY
;
3604 ret
= getnameinfo(sa_u
, size
, host
, hostlen
, serv
, servlen
, convert_aiflag_w2u(flags
));
3606 ws_sockaddr_free(sa_u
, sa
);
3607 return convert_eai_u2w(ret
);
3609 FIXME("getnameinfo() failed, not found during buildtime.\n");
3614 /***********************************************************************
3615 * getservbyport (WS2_32.56)
3617 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
3619 struct WS_servent
* retval
= NULL
;
3620 #ifdef HAVE_GETSERVBYPORT
3621 struct servent
* serv
;
3622 char *proto_str
= NULL
;
3624 if (proto
&& *proto
)
3626 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
3628 EnterCriticalSection( &csWSgetXXXbyYYY
);
3629 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
3630 retval
= WS_dup_se(serv
);
3632 else SetLastError(WSANO_DATA
);
3633 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3634 HeapFree( GetProcessHeap(), 0, proto_str
);
3636 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
3641 /***********************************************************************
3642 * gethostname (WS2_32.57)
3644 int WINAPI
WS_gethostname(char *name
, int namelen
)
3646 TRACE("name %p, len %d\n", name
, namelen
);
3648 if (gethostname(name
, namelen
) == 0)
3650 TRACE("<- '%s'\n", name
);
3653 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
3654 TRACE("<- ERROR !\n");
3655 return SOCKET_ERROR
;
3659 /* ------------------------------------- Windows sockets extensions -- *
3661 * ------------------------------------------------------------------- */
3663 /***********************************************************************
3664 * WSAEnumNetworkEvents (WS2_32.36)
3666 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
3670 TRACE("%08x, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
3672 SERVER_START_REQ( get_socket_event
)
3674 req
->handle
= SOCKET2HANDLE(s
);
3675 req
->service
= TRUE
;
3676 req
->c_event
= hEvent
;
3677 wine_server_set_reply( req
, lpEvent
->iErrorCode
, sizeof(lpEvent
->iErrorCode
) );
3678 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
3682 SetLastError(WSAEINVAL
);
3683 return SOCKET_ERROR
;
3686 /***********************************************************************
3687 * WSAEventSelect (WS2_32.39)
3689 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, long lEvent
)
3693 TRACE("%08x, hEvent %p, event %08x\n", s
, hEvent
, (unsigned)lEvent
);
3695 SERVER_START_REQ( set_socket_event
)
3697 req
->handle
= SOCKET2HANDLE(s
);
3699 req
->event
= hEvent
;
3702 ret
= wine_server_call( req
);
3706 SetLastError(WSAEINVAL
);
3707 return SOCKET_ERROR
;
3710 /**********************************************************************
3711 * WSAGetOverlappedResult (WS2_32.40)
3713 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
3714 LPDWORD lpcbTransfer
, BOOL fWait
,
3719 TRACE( "socket %04x ovl %p trans %p, wait %d flags %p\n",
3720 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
3722 if ( lpOverlapped
== NULL
)
3724 ERR( "Invalid pointer\n" );
3725 WSASetLastError(WSA_INVALID_PARAMETER
);
3731 if (lpOverlapped
->hEvent
)
3732 while ( WaitForSingleObjectEx(lpOverlapped
->hEvent
,
3733 INFINITE
, TRUE
) == STATUS_USER_APC
);
3734 else /* busy loop */
3735 while ( ((volatile OVERLAPPED
*)lpOverlapped
)->Internal
== STATUS_PENDING
)
3739 else if ( lpOverlapped
->Internal
== STATUS_PENDING
)
3741 /* Wait in order to give APCs a chance to run. */
3742 /* This is cheating, so we must set the event again in case of success -
3743 it may be a non-manual reset event. */
3744 while ( (r
= WaitForSingleObjectEx(lpOverlapped
->hEvent
, 0, TRUE
)) == STATUS_USER_APC
);
3745 if ( r
== WAIT_OBJECT_0
&& lpOverlapped
->hEvent
)
3746 NtSetEvent( lpOverlapped
->hEvent
, NULL
);
3750 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
3753 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
3755 switch ( lpOverlapped
->Internal
)
3757 case STATUS_SUCCESS
:
3759 case STATUS_PENDING
:
3760 WSASetLastError( WSA_IO_INCOMPLETE
);
3761 if (fWait
) ERR("PENDING status after waiting!\n");
3764 WSASetLastError( NtStatusToWSAError( lpOverlapped
->Internal
));
3770 /***********************************************************************
3771 * WSAAsyncSelect (WS2_32.101)
3773 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, long lEvent
)
3777 TRACE("%x, hWnd %p, uMsg %08x, event %08lx\n", s
, hWnd
, uMsg
, lEvent
);
3779 SERVER_START_REQ( set_socket_event
)
3781 req
->handle
= SOCKET2HANDLE(s
);
3786 ret
= wine_server_call( req
);
3790 SetLastError(WSAEINVAL
);
3791 return SOCKET_ERROR
;
3794 /***********************************************************************
3795 * WSACreateEvent (WS2_32.31)
3798 WSAEVENT WINAPI
WSACreateEvent(void)
3800 /* Create a manual-reset event, with initial state: unsignaled */
3803 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
3806 /***********************************************************************
3807 * WSACloseEvent (WS2_32.29)
3810 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
3812 TRACE ("event=%p\n", event
);
3814 return CloseHandle(event
);
3817 /***********************************************************************
3818 * WSASocketA (WS2_32.78)
3821 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
3822 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
3823 GROUP g
, DWORD dwFlags
)
3826 WSAPROTOCOL_INFOW info
;
3828 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3829 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3831 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
3833 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
3834 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
3835 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
3839 WSASetLastError( WSAEINVAL
);
3840 return SOCKET_ERROR
;
3843 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
3846 /***********************************************************************
3847 * WSASocketW (WS2_32.79)
3850 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
3851 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
3852 GROUP g
, DWORD dwFlags
)
3857 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
3858 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
3861 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
3862 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
3864 /* hack for WSADuplicateSocket */
3865 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
3866 ret
= lpProtocolInfo
->dwCatalogEntryId
;
3867 TRACE("\tgot duplicate %04x\n", ret
);
3871 /* check and convert the socket family */
3872 af
= convert_af_w2u(af
);
3875 FIXME("Unsupported socket family %d!\n", af
);
3876 SetLastError(WSAEAFNOSUPPORT
);
3877 return INVALID_SOCKET
;
3880 /* check the socket type */
3881 type
= convert_socktype_w2u(type
);
3884 SetLastError(WSAESOCKTNOSUPPORT
);
3885 return INVALID_SOCKET
;
3888 /* check the protocol type */
3889 if ( protocol
< 0 ) /* don't support negative values */
3891 SetLastError(WSAEPROTONOSUPPORT
);
3892 return INVALID_SOCKET
;
3895 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
3899 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
3901 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
3902 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
3905 SERVER_START_REQ( create_socket
)
3909 req
->protocol
= protocol
;
3910 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
3911 req
->attributes
= OBJ_INHERIT
;
3912 req
->flags
= dwFlags
;
3913 set_error( wine_server_call( req
) );
3914 ret
= HANDLE2SOCKET( reply
->handle
);
3919 TRACE("\tcreated %04x\n", ret
);
3923 if (GetLastError() == WSAEACCES
) /* raw socket denied */
3925 if (type
== SOCK_RAW
)
3926 MESSAGE("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
3928 MESSAGE("WS_SOCKET: not enough privileges to create socket, try running as root\n");
3929 SetLastError(WSAESOCKTNOSUPPORT
);
3932 WARN("\t\tfailed!\n");
3933 return INVALID_SOCKET
;
3936 /***********************************************************************
3937 * WSAJoinLeaf (WS2_32.58)
3940 SOCKET WINAPI
WSAJoinLeaf(
3942 const struct WS_sockaddr
*addr
,
3944 LPWSABUF lpCallerData
,
3945 LPWSABUF lpCalleeData
,
3951 return INVALID_SOCKET
;
3954 /***********************************************************************
3955 * __WSAFDIsSet (WS2_32.151)
3957 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
3959 int i
= set
->fd_count
;
3961 TRACE("(%d,%p(%i))\n", s
, set
, i
);
3964 if (set
->fd_array
[i
] == s
) return 1;
3968 /***********************************************************************
3969 * WSAIsBlocking (WINSOCK.114)
3970 * WSAIsBlocking (WS2_32.114)
3972 BOOL WINAPI
WSAIsBlocking(void)
3974 /* By default WinSock should set all its sockets to non-blocking mode
3975 * and poll in PeekMessage loop when processing "blocking" ones. This
3976 * function is supposed to tell if the program is in this loop. Our
3977 * blocking calls are truly blocking so we always return FALSE.
3979 * Note: It is allowed to call this function without prior WSAStartup().
3986 /***********************************************************************
3987 * WSACancelBlockingCall (WINSOCK.113)
3988 * WSACancelBlockingCall (WS2_32.113)
3990 INT WINAPI
WSACancelBlockingCall(void)
3996 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
3998 FIXME("How was this called?\n");
4003 /***********************************************************************
4004 * WSASetBlockingHook (WS2_32.109)
4006 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
4008 FARPROC prev
= blocking_hook
;
4009 blocking_hook
= lpBlockFunc
;
4010 TRACE("hook %p\n", lpBlockFunc
);
4015 /***********************************************************************
4016 * WSAUnhookBlockingHook (WS2_32.110)
4018 INT WINAPI
WSAUnhookBlockingHook(void)
4020 blocking_hook
= WSA_DefaultBlockingHook
;
4025 /* ----------------------------------- end of API stuff */
4027 /* ----------------------------------- helper functions -
4029 * TODO: Merge WS_dup_..() stuff into one function that
4030 * would operate with a generic structure containing internal
4031 * pointers (via a template of some kind).
4034 static int list_size(char** l
, int item_size
)
4039 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
4040 j
+= (i
+ 1) * sizeof(char*); }
4044 static int list_dup(char** l_src
, char** l_to
, int item_size
)
4049 for (i
= 0; l_src
[i
]; i
++) ;
4050 p
= (char *)(l_to
+ i
+ 1);
4051 for (i
= 0; l_src
[i
]; i
++)
4053 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
4054 memcpy(p
, l_src
[i
], count
);
4059 return p
- (char *)l_to
;
4064 /* duplicate hostent entry
4065 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
4066 * Dito for protoent and servent.
4068 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
4071 struct WS_hostent
*p_to
;
4073 int size
= (sizeof(*p_he
) +
4074 strlen(p_he
->h_name
) + 1 +
4075 list_size(p_he
->h_aliases
, 0) +
4076 list_size(p_he
->h_addr_list
, p_he
->h_length
));
4078 if (!(p_to
= check_buffer_he(size
))) return NULL
;
4079 p_to
->h_addrtype
= p_he
->h_addrtype
;
4080 p_to
->h_length
= p_he
->h_length
;
4082 p
= (char *)(p_to
+ 1);
4084 strcpy(p
, p_he
->h_name
);
4087 p_to
->h_aliases
= (char **)p
;
4088 p
+= list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
4090 p_to
->h_addr_list
= (char **)p
;
4091 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
4095 /* ----- protoent */
4097 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
4100 struct WS_protoent
*p_to
;
4102 int size
= (sizeof(*p_pe
) +
4103 strlen(p_pe
->p_name
) + 1 +
4104 list_size(p_pe
->p_aliases
, 0));
4106 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
4107 p_to
->p_proto
= p_pe
->p_proto
;
4109 p
= (char *)(p_to
+ 1);
4111 strcpy(p
, p_pe
->p_name
);
4114 p_to
->p_aliases
= (char **)p
;
4115 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
4121 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
4124 struct WS_servent
*p_to
;
4126 int size
= (sizeof(*p_se
) +
4127 strlen(p_se
->s_proto
) + 1 +
4128 strlen(p_se
->s_name
) + 1 +
4129 list_size(p_se
->s_aliases
, 0));
4131 if (!(p_to
= check_buffer_se(size
))) return NULL
;
4132 p_to
->s_port
= p_se
->s_port
;
4134 p
= (char *)(p_to
+ 1);
4136 strcpy(p
, p_se
->s_name
);
4140 strcpy(p
, p_se
->s_proto
);
4143 p_to
->s_aliases
= (char **)p
;
4144 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
4148 /* ----------------------------------- error handling */
4152 int loc_errno
= errno
;
4153 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
4157 case EINTR
: return WSAEINTR
;
4158 case EBADF
: return WSAEBADF
;
4160 case EACCES
: return WSAEACCES
;
4161 case EFAULT
: return WSAEFAULT
;
4162 case EINVAL
: return WSAEINVAL
;
4163 case EMFILE
: return WSAEMFILE
;
4164 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
4165 case EINPROGRESS
: return WSAEINPROGRESS
;
4166 case EALREADY
: return WSAEALREADY
;
4167 case ENOTSOCK
: return WSAENOTSOCK
;
4168 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
4169 case EMSGSIZE
: return WSAEMSGSIZE
;
4170 case EPROTOTYPE
: return WSAEPROTOTYPE
;
4171 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
4172 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
4173 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
4174 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
4175 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
4176 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
4177 case EADDRINUSE
: return WSAEADDRINUSE
;
4178 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
4179 case ENETDOWN
: return WSAENETDOWN
;
4180 case ENETUNREACH
: return WSAENETUNREACH
;
4181 case ENETRESET
: return WSAENETRESET
;
4182 case ECONNABORTED
: return WSAECONNABORTED
;
4184 case ECONNRESET
: return WSAECONNRESET
;
4185 case ENOBUFS
: return WSAENOBUFS
;
4186 case EISCONN
: return WSAEISCONN
;
4187 case ENOTCONN
: return WSAENOTCONN
;
4188 case ESHUTDOWN
: return WSAESHUTDOWN
;
4189 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
4190 case ETIMEDOUT
: return WSAETIMEDOUT
;
4191 case ECONNREFUSED
: return WSAECONNREFUSED
;
4192 case ELOOP
: return WSAELOOP
;
4193 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
4194 case EHOSTDOWN
: return WSAEHOSTDOWN
;
4195 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
4196 case ENOTEMPTY
: return WSAENOTEMPTY
;
4198 case EPROCLIM
: return WSAEPROCLIM
;
4201 case EUSERS
: return WSAEUSERS
;
4204 case EDQUOT
: return WSAEDQUOT
;
4207 case ESTALE
: return WSAESTALE
;
4210 case EREMOTE
: return WSAEREMOTE
;
4213 /* just in case we ever get here and there are no problems */
4216 WARN("Unknown errno %d!\n", loc_errno
);
4217 return WSAEOPNOTSUPP
;
4221 UINT
wsaHerrno(int loc_errno
)
4224 WARN("h_errno %d.\n", loc_errno
);
4228 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
4229 case TRY_AGAIN
: return WSATRY_AGAIN
;
4230 case NO_RECOVERY
: return WSANO_RECOVERY
;
4231 case NO_DATA
: return WSANO_DATA
;
4232 case ENOBUFS
: return WSAENOBUFS
;
4236 WARN("Unknown h_errno %d!\n", loc_errno
);
4237 return WSAEOPNOTSUPP
;
4242 /***********************************************************************
4243 * WSARecv (WS2_32.67)
4245 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4246 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
4247 LPWSAOVERLAPPED lpOverlapped
,
4248 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4250 return WSARecvFrom(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
4251 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
);
4254 /***********************************************************************
4255 * WSARecvFrom (WS2_32.69)
4257 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4258 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
4259 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
4260 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4264 int n
, fd
, err
= WSAENOTSOCK
, flags
, ret
;
4265 struct iovec
* iovec
;
4266 struct ws2_async
*wsa
;
4267 IO_STATUS_BLOCK
* iosb
;
4269 TRACE("socket %04x, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4270 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
4271 (lpFromlen
? *lpFromlen
: -1),
4272 lpOverlapped
, lpCompletionRoutine
);
4274 fd
= get_sock_fd( s
, FILE_READ_DATA
, &flags
);
4275 TRACE( "fd=%d, flags=%x\n", fd
, flags
);
4277 if (fd
== -1) return SOCKET_ERROR
;
4279 if (flags
& FD_FLAG_RECV_SHUTDOWN
)
4281 WSASetLastError( WSAESHUTDOWN
);
4285 iovec
= HeapAlloc( GetProcessHeap(), 0, dwBufferCount
* sizeof (struct iovec
) );
4292 for (i
= 0; i
< dwBufferCount
; i
++)
4294 iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
4295 iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4298 if ( (lpOverlapped
|| lpCompletionRoutine
) && flags
& FD_FLAG_OVERLAPPED
)
4300 wsa
= WS2_make_async( s
, ws2m_read
, iovec
, dwBufferCount
,
4301 lpFlags
, lpFrom
, lpFromlen
,
4302 lpOverlapped
, lpCompletionRoutine
, &iosb
);
4310 if ( ( ret
= ws2_queue_async( wsa
, iosb
)) )
4312 err
= NtStatusToWSAError( ret
);
4314 if ( !lpOverlapped
)
4315 HeapFree( GetProcessHeap(), 0, iosb
);
4316 HeapFree( GetProcessHeap(), 0, wsa
);
4319 release_sock_fd( s
, fd
);
4321 /* Try immediate completion */
4324 if ( WSAGetOverlappedResult( s
, lpOverlapped
,
4325 lpNumberOfBytesRecvd
, FALSE
, lpFlags
) )
4328 if ( (err
= WSAGetLastError()) != WSA_IO_INCOMPLETE
)
4332 WSASetLastError( WSA_IO_PENDING
);
4333 return SOCKET_ERROR
;
4336 if ( _is_blocking(s
) )
4339 /* FIXME: OOB and exceptfds? */
4340 int timeout
= GET_RCVTIMEO(fd
);
4341 if( !do_block(fd
, POLLIN
, timeout
)) {
4343 /* a timeout is not fatal */
4344 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4349 n
= WS2_recv( fd
, iovec
, dwBufferCount
, lpFrom
, lpFromlen
, lpFlags
);
4356 TRACE(" -> %i bytes\n", n
);
4357 *lpNumberOfBytesRecvd
= n
;
4359 HeapFree(GetProcessHeap(), 0, iovec
);
4360 release_sock_fd( s
, fd
);
4361 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4366 HeapFree(GetProcessHeap(), 0, iovec
);
4369 release_sock_fd( s
, fd
);
4372 WARN(" -> ERROR %d\n", err
);
4373 WSASetLastError( err
);
4374 return SOCKET_ERROR
;
4377 /***********************************************************************
4378 * WSCInstallProvider (WS2_32.88)
4380 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
4381 LPCWSTR lpszProviderDllPath
,
4382 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
4383 DWORD dwNumberOfEntries
,
4386 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
4387 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
4388 dwNumberOfEntries
, lpErrno
);
4394 /***********************************************************************
4395 * WSCDeinstallProvider (WS2_32.83)
4397 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
4399 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
4405 /***********************************************************************
4406 * WSAAccept (WS2_32.26)
4408 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
4409 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
4412 int ret
= 0, size
= 0;
4413 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
4414 /* QOS SQOS, GQOS; */
4417 SOCKADDR src_addr
, dst_addr
;
4419 TRACE("Socket %04x, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4420 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
4423 size
= sizeof(src_addr
);
4424 cs
= WS_accept(s
, &src_addr
, &size
);
4426 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
4428 CallerId
.buf
= (char *)&src_addr
;
4429 CallerId
.len
= sizeof(src_addr
);
4431 CallerData
.buf
= NULL
;
4432 CallerData
.len
= (ULONG
)NULL
;
4434 WS_getsockname(cs
, &dst_addr
, &size
);
4436 CalleeId
.buf
= (char *)&dst_addr
;
4437 CalleeId
.len
= sizeof(dst_addr
);
4440 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
4441 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
4446 if (addr
&& addrlen
)
4447 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
4450 SERVER_START_REQ( set_socket_deferred
)
4452 req
->handle
= SOCKET2HANDLE(s
);
4453 req
->deferred
= SOCKET2HANDLE(cs
);
4454 if ( !wine_server_call_err ( req
) )
4456 SetLastError( WSATRY_AGAIN
);
4457 WS_closesocket( cs
);
4461 return SOCKET_ERROR
;
4464 SetLastError(WSAECONNREFUSED
);
4465 return SOCKET_ERROR
;
4467 FIXME("Unknown return type from Condition function\n");
4468 SetLastError(WSAENOTSOCK
);
4469 return SOCKET_ERROR
;
4473 /***********************************************************************
4474 * WSADuplicateSocketA (WS2_32.32)
4476 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
4480 TRACE("(%d,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4481 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4482 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
4483 /* I don't know what the real Windoze does next, this is a hack */
4484 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
4485 * the target use the global duplicate, or we could copy a reference to us to the structure
4486 * and let the target duplicate it from us, but let's do it as simple as possible */
4487 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4488 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4489 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4490 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4491 CloseHandle(hProcess
);
4492 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4496 /***********************************************************************
4497 * WSADuplicateSocketW (WS2_32.33)
4499 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
4503 TRACE("(%d,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
4505 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
4506 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
4507 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
4508 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
4509 0, FALSE
, DUPLICATE_SAME_ACCESS
);
4510 CloseHandle(hProcess
);
4511 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
4515 /***********************************************************************
4516 * WSAInstallServiceClassA (WS2_32.48)
4518 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
4520 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
4521 WSASetLastError(WSAEACCES
);
4522 return SOCKET_ERROR
;
4525 /***********************************************************************
4526 * WSAInstallServiceClassW (WS2_32.49)
4528 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
4530 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
4531 WSASetLastError(WSAEACCES
);
4532 return SOCKET_ERROR
;
4535 /***********************************************************************
4536 * WSARemoveServiceClass (WS2_32.70)
4538 int WINAPI
WSARemoveServiceClass(LPGUID info
)
4540 FIXME("Request to remove service %p\n",info
);
4541 WSASetLastError(WSATYPE_NOT_FOUND
);
4542 return SOCKET_ERROR
;
4545 /***********************************************************************
4546 * WSAStringToAddressA (WS2_32.80)
4548 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
4550 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
4551 LPSOCKADDR lpAddress
,
4552 LPINT lpAddressLength
)
4555 struct in_addr inetaddr
;
4556 LPSTR workBuffer
=NULL
,ptrPort
;
4558 TRACE( "(%s, %x, %p, %p, %p)\n", AddressString
, AddressFamily
, lpProtocolInfo
,
4559 lpAddress
, lpAddressLength
);
4561 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4565 workBuffer
= HeapAlloc( GetProcessHeap(), 0, strlen(AddressString
)+1 );
4568 strcpy(workBuffer
,AddressString
);
4569 switch (AddressFamily
)
4572 /* caller wants to know the size of the socket buffer */
4573 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
4575 *lpAddressLength
= sizeof(SOCKADDR_IN
);
4580 /* caller wants to translate an AddressString into a SOCKADDR */
4583 memset(lpAddress
,0,sizeof(SOCKADDR_IN
));
4584 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
4585 ptrPort
= strchr(workBuffer
,':');
4588 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= (WS_u_short
)atoi(ptrPort
+1);
4592 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
4593 if (inet_aton(workBuffer
, &inetaddr
) > 0)
4595 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
4603 FIXME("(%s, %x, %p, %p, %p) - ProtocolInfo not implemented!\n",
4604 AddressString
, AddressFamily
,
4605 lpProtocolInfo
, lpAddress
, lpAddressLength
);
4609 FIXME("(%s, %x, %p, %p, %p) - AddressFamiliy not implemented!\n",
4610 AddressString
, AddressFamily
,
4611 lpProtocolInfo
, lpAddress
, lpAddressLength
);
4613 HeapFree( GetProcessHeap(), 0, workBuffer
);
4616 res
= WSA_NOT_ENOUGH_MEMORY
;
4622 WSASetLastError(res
);
4623 return SOCKET_ERROR
;
4626 /***********************************************************************
4627 * WSAStringToAddressW (WS2_32.81)
4629 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
4630 * If this should be the case, it would be required to map these digits
4631 * to Unicode digits (0-9) using FoldString first.
4633 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
4635 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
4636 LPSOCKADDR lpAddress
,
4637 LPINT lpAddressLength
)
4640 LPSTR workBuffer
=NULL
;
4641 WSAPROTOCOL_INFOA infoA
;
4642 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
4644 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
4645 lpAddress
, lpAddressLength
);
4647 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
4649 /* if ProtocolInfo is available - convert to ANSI variant */
4652 lpProtoInfoA
= &infoA
;
4653 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
4655 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
4656 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
4658 WSASetLastError( WSAEINVAL
);
4659 return SOCKET_ERROR
;
4665 /* Translate AddressString to ANSI code page - assumes that only
4666 standard digits 0-9 are used with this API call */
4667 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
4668 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
4672 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
4673 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
4674 lpAddress
,lpAddressLength
);
4675 HeapFree( GetProcessHeap(), 0, workBuffer
);
4679 res
= WSA_NOT_ENOUGH_MEMORY
;
4684 WSASetLastError(res
);
4685 return SOCKET_ERROR
;
4688 /***********************************************************************
4689 * WSAAddressToStringA (WS2_32.27)
4691 * See WSAAddressToStringW
4693 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
4694 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
4698 CHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4701 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4703 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4704 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4706 /* sin_family is guaranteed to be the first u_short */
4707 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4709 sprintf( buffer
, "%u.%u.%u.%u:%u",
4710 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4711 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4712 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4713 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4714 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4716 p
= strchr( buffer
, ':' );
4717 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4719 size
= strlen( buffer
);
4724 WSASetLastError(WSAEFAULT
);
4725 return SOCKET_ERROR
;
4728 strcpy( string
, buffer
);
4732 /***********************************************************************
4733 * WSAAddressToStringW (WS2_32.28)
4735 * Convert a sockaddr address into a readable address string.
4738 * sockaddr [I] Pointer to a sockaddr structure.
4739 * len [I] Size of the sockaddr structure.
4740 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
4741 * string [I/O] Pointer to a buffer to receive the address string.
4742 * lenstr [I/O] Size of the receive buffer in WCHARs.
4746 * Failure: SOCKET_ERROR
4749 * The 'info' parameter is ignored.
4752 * Only supports AF_INET addresses.
4754 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
4755 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
4759 WCHAR buffer
[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
4760 static const WCHAR format
[] = { '%','u','.','%','u','.','%','u','.','%','u',':','%','u',0 };
4763 TRACE( "(%p, %x, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
4765 if (!sockaddr
|| len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
4766 if (!string
|| !lenstr
) return SOCKET_ERROR
;
4768 /* sin_family is guaranteed to be the first u_short */
4769 if (((SOCKADDR_IN
*)sockaddr
)->sin_family
!= AF_INET
) return SOCKET_ERROR
;
4771 sprintfW( buffer
, format
,
4772 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
4773 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
4774 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
4775 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
4776 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
4778 p
= strchrW( buffer
, ':' );
4779 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
4781 size
= lstrlenW( buffer
);
4786 return SOCKET_ERROR
;
4789 lstrcpyW( string
, buffer
);
4793 /***********************************************************************
4794 * WSAEnumNameSpaceProvidersA (WS2_32.34)
4796 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
4798 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4802 /***********************************************************************
4803 * WSAEnumNameSpaceProvidersW (WS2_32.35)
4805 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
4807 FIXME( "(%p %p) Stub!\n", len
, buffer
);
4811 /***********************************************************************
4812 * WSAGetQOSByName (WS2_32.41)
4814 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
4816 FIXME( "(0x%04x %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
4820 /***********************************************************************
4821 * WSAGetServiceClassInfoA (WS2_32.42)
4823 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
4824 LPWSASERVICECLASSINFOA info
)
4826 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4828 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4829 return SOCKET_ERROR
;
4832 /***********************************************************************
4833 * WSAGetServiceClassInfoW (WS2_32.43)
4835 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
4836 LPWSASERVICECLASSINFOW info
)
4838 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
4840 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4841 return SOCKET_ERROR
;
4844 /***********************************************************************
4845 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
4847 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
4849 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4850 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4851 return SOCKET_ERROR
;
4854 /***********************************************************************
4855 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
4857 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
4859 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
4860 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4861 return SOCKET_ERROR
;
4864 /***********************************************************************
4865 * WSALookupServiceBeginA (WS2_32.59)
4867 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
4868 DWORD dwControlFlags
,
4871 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4873 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4874 return SOCKET_ERROR
;
4877 /***********************************************************************
4878 * WSALookupServiceBeginW (WS2_32.60)
4880 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
4881 DWORD dwControlFlags
,
4884 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
4886 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
4887 return SOCKET_ERROR
;
4890 /***********************************************************************
4891 * WSALookupServiceBeginW (WS2_32.61)
4893 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
4895 FIXME("(%p) Stub!\n", lookup
);
4899 /***********************************************************************
4900 * WSALookupServiceNextA (WS2_32.62)
4902 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
4904 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4908 /***********************************************************************
4909 * WSALookupServiceNextW (WS2_32.63)
4911 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
4913 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
4917 /***********************************************************************
4918 * WSANtohl (WS2_32.64)
4920 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
4922 TRACE( "(0x%04x 0x%08x %p)\n", s
, netlong
, lphostlong
);
4924 if (!lphostlong
) return WSAEFAULT
;
4926 *lphostlong
= ntohl( netlong
);
4930 /***********************************************************************
4931 * WSANtohs (WS2_32.65)
4933 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
4935 TRACE( "(0x%04x 0x%08x %p)\n", s
, netshort
, lphostshort
);
4937 if (!lphostshort
) return WSAEFAULT
;
4939 *lphostshort
= ntohs( netshort
);
4943 /***********************************************************************
4944 * WSAProviderConfigChange (WS2_32.66)
4946 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
4947 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
4949 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
4950 return SOCKET_ERROR
;
4953 /***********************************************************************
4954 * WSARecvDisconnect (WS2_32.68)
4956 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
4958 TRACE( "(0x%04x %p)\n", s
, disconnectdata
);
4960 return WS_shutdown( s
, 0 );
4963 /***********************************************************************
4964 * WSASetServiceA (WS2_32.76)
4966 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
4968 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4972 /***********************************************************************
4973 * WSASetServiceW (WS2_32.77)
4975 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
4977 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
4981 /***********************************************************************
4982 * WSCEnableNSProvider (WS2_32.84)
4984 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
4986 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
4990 /***********************************************************************
4991 * WSCGetProviderPath (WS2_32.86)
4993 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
4995 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
4997 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
4999 *errcode
= WSAEINVAL
;
5000 return SOCKET_ERROR
;
5003 /***********************************************************************
5004 * WSCInstallNameSpace (WS2_32.87)
5006 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
5007 DWORD version
, LPGUID provider
)
5009 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
5010 namespace, version
, debugstr_guid(provider
) );
5014 /***********************************************************************
5015 * WSCUnInstallNameSpace (WS2_32.89)
5017 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
5019 FIXME("(%p) Stub!\n", lpProviderId
);
5023 /***********************************************************************
5024 * WSCWriteProviderOrder (WS2_32.91)
5026 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
5028 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);