2 * based on Windows Sockets 1.1 specs
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
5 * Copyright (C) 2005 Marcus Meissner
6 * Copyright (C) 2006-2008 Kai Blin
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * NOTE: If you make any changes to fix a particular app, make sure
23 * they don't break something else like Netscape or telnet and ftp
24 * clients and servers (www.winsite.com got a lot of those).
28 #include "wine/port.h"
33 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 # include <sys/ioctl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 # include <sys/filio.h>
43 #ifdef HAVE_SYS_SOCKIO_H
44 # include <sys/sockio.h>
48 # include <sys/so_ioctl.h>
51 #ifdef HAVE_SYS_PARAM_H
52 # include <sys/param.h>
58 #ifdef HAVE_SYS_WAIT_H
59 # include <sys/wait.h>
64 #ifdef HAVE_SYS_SOCKET_H
65 #include <sys/socket.h>
67 #ifdef HAVE_NETINET_IN_H
68 # include <netinet/in.h>
70 #ifdef HAVE_NETINET_TCP_H
71 # include <netinet/tcp.h>
73 #ifdef HAVE_ARPA_INET_H
74 # include <arpa/inet.h>
79 #ifdef HAVE_SYS_ERRNO_H
80 #include <sys/errno.h>
89 #ifdef HAVE_ARPA_NAMESER_H
90 # include <arpa/nameser.h>
99 #ifdef HAVE_NETIPX_IPX_H
100 # include <netipx/ipx.h>
102 #elif defined(HAVE_LINUX_IPX_H)
103 # ifdef HAVE_ASM_TYPES_H
104 # include <asm/types.h>
106 # ifdef HAVE_LINUX_TYPES_H
107 # include <linux/types.h>
109 # include <linux/ipx.h>
113 #ifdef HAVE_LINUX_IRDA_H
114 # ifdef HAVE_LINUX_TYPES_H
115 # include <linux/types.h>
117 # include <linux/irda.h>
124 #ifdef HAVE_SYS_POLL_H
125 # include <sys/poll.h>
127 #ifdef HAVE_SYS_TIME_H
128 # include <sys/time.h>
131 #define NONAMELESSUNION
132 #define NONAMELESSSTRUCT
133 #include "ntstatus.h"
134 #define WIN32_NO_STATUS
139 #include "winerror.h"
141 #include "winsock2.h"
143 #include "ws2tcpip.h"
149 #include "iphlpapi.h"
150 #include "wine/server.h"
151 #include "wine/debug.h"
152 #include "wine/exception.h"
153 #include "wine/unicode.h"
156 # include "wsnwlink.h"
160 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
161 # define sipx_network sipx_addr.x_net
162 # define sipx_node sipx_addr.x_host.c_host
163 #endif /* __FreeBSD__ */
166 #define INADDR_NONE ~0UL
169 WINE_DEFAULT_DEBUG_CHANNEL(winsock
);
170 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
172 /* critical section to protect some non-reentrant net function */
173 static CRITICAL_SECTION csWSgetXXXbyYYY
;
174 static CRITICAL_SECTION_DEBUG critsect_debug
=
176 0, 0, &csWSgetXXXbyYYY
,
177 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
178 0, 0, { (DWORD_PTR
)(__FILE__
": csWSgetXXXbyYYY") }
180 static CRITICAL_SECTION csWSgetXXXbyYYY
= { &critsect_debug
, -1, 0, 0, 0, 0 };
182 union generic_unix_sockaddr
184 struct sockaddr addr
;
185 char data
[128]; /* should be big enough for all families */
188 static inline const char *debugstr_sockaddr( const struct WS_sockaddr
*a
)
190 if (!a
) return "(nil)";
191 switch (a
->sa_family
)
194 return wine_dbg_sprintf("{ family AF_INET, address %s, port %d }",
195 inet_ntoa(((const struct sockaddr_in
*)a
)->sin_addr
),
196 ntohs(((const struct sockaddr_in
*)a
)->sin_port
));
201 struct WS_sockaddr_in6
*sin
= (struct WS_sockaddr_in6
*)a
;
203 p
= WS_inet_ntop( WS_AF_INET6
, &sin
->sin6_addr
, buf
, sizeof(buf
) );
205 p
= "(unknown IPv6 address)";
206 return wine_dbg_sprintf("{ family AF_INET6, address %s, port %d }",
207 p
, ntohs(sin
->sin6_port
));
213 memcpy( &addr
, ((const SOCKADDR_IRDA
*)a
)->irdaDeviceID
, sizeof(addr
) );
214 addr
= ntohl( addr
);
215 return wine_dbg_sprintf("{ family AF_IRDA, addr %08x, name %s }",
217 ((const SOCKADDR_IRDA
*)a
)->irdaServiceName
);
220 return wine_dbg_sprintf("{ family %d }", a
->sa_family
);
224 /* HANDLE<->SOCKET conversion (SOCKET is UINT_PTR). */
225 #define SOCKET2HANDLE(s) ((HANDLE)(s))
226 #define HANDLE2SOCKET(h) ((SOCKET)(h))
228 /****************************************************************
229 * Async IO declarations
230 ****************************************************************/
232 typedef struct ws2_async
236 LPWSAOVERLAPPED user_overlapped
;
237 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_func
;
238 IO_STATUS_BLOCK local_iosb
;
239 struct WS_sockaddr
*addr
;
242 int val
; /* for send operations */
243 int *ptr
; /* for recv operations */
246 unsigned int n_iovecs
;
247 unsigned int first_iovec
;
248 struct iovec iovec
[1];
251 /****************************************************************/
253 /* ----------------------------------- internal data */
255 /* ws_... struct conversion flags */
257 typedef struct /* WSAAsyncSelect() control struct */
259 HANDLE service
, event
, sock
;
265 #define WS_MAX_SOCKETS_PER_PROCESS 128 /* reasonable guess */
266 #define WS_MAX_UDP_DATAGRAM 1024
267 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
);
269 /* hostent's, servent's and protent's are stored in one buffer per thread,
270 * as documented on MSDN for the functions that return any of the buffers */
271 struct per_thread_data
274 struct WS_hostent
*he_buffer
;
275 struct WS_servent
*se_buffer
;
276 struct WS_protoent
*pe_buffer
;
282 /* internal: routing description information */
289 static INT num_startup
; /* reference counter */
290 static FARPROC blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
292 /* function prototypes */
293 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
);
294 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
);
295 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
);
296 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
);
298 int WSAIOCTL_GetInterfaceCount(void);
299 int WSAIOCTL_GetInterfaceName(int intNumber
, char *intName
);
301 #define MAP_OPTION(opt) { WS_##opt, opt }
303 static const int ws_sock_map
[][2] =
305 MAP_OPTION( SO_DEBUG
),
306 MAP_OPTION( SO_ACCEPTCONN
),
307 MAP_OPTION( SO_REUSEADDR
),
308 MAP_OPTION( SO_KEEPALIVE
),
309 MAP_OPTION( SO_DONTROUTE
),
310 MAP_OPTION( SO_BROADCAST
),
311 MAP_OPTION( SO_LINGER
),
312 MAP_OPTION( SO_OOBINLINE
),
313 MAP_OPTION( SO_SNDBUF
),
314 MAP_OPTION( SO_RCVBUF
),
315 MAP_OPTION( SO_ERROR
),
316 MAP_OPTION( SO_TYPE
),
318 MAP_OPTION( SO_RCVTIMEO
),
321 MAP_OPTION( SO_SNDTIMEO
),
325 static const int ws_tcp_map
[][2] =
328 MAP_OPTION( TCP_NODELAY
),
332 static const int ws_ip_map
[][2] =
334 MAP_OPTION( IP_MULTICAST_IF
),
335 MAP_OPTION( IP_MULTICAST_TTL
),
336 MAP_OPTION( IP_MULTICAST_LOOP
),
337 MAP_OPTION( IP_ADD_MEMBERSHIP
),
338 MAP_OPTION( IP_DROP_MEMBERSHIP
),
339 MAP_OPTION( IP_OPTIONS
),
341 MAP_OPTION( IP_HDRINCL
),
343 MAP_OPTION( IP_TOS
),
344 MAP_OPTION( IP_TTL
),
347 static const int ws_af_map
[][2] =
349 MAP_OPTION( AF_UNSPEC
),
350 MAP_OPTION( AF_INET
),
351 MAP_OPTION( AF_INET6
),
353 MAP_OPTION( AF_IPX
),
356 MAP_OPTION( AF_IRDA
),
358 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
361 static const int ws_socktype_map
[][2] =
363 MAP_OPTION( SOCK_DGRAM
),
364 MAP_OPTION( SOCK_STREAM
),
365 MAP_OPTION( SOCK_RAW
),
366 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
369 static const int ws_proto_map
[][2] =
371 MAP_OPTION( IPPROTO_IP
),
372 MAP_OPTION( IPPROTO_TCP
),
373 MAP_OPTION( IPPROTO_UDP
),
374 MAP_OPTION( IPPROTO_ICMP
),
375 MAP_OPTION( IPPROTO_IGMP
),
376 MAP_OPTION( IPPROTO_RAW
),
377 {FROM_PROTOCOL_INFO
, FROM_PROTOCOL_INFO
},
380 static const int ws_aiflag_map
[][2] =
382 MAP_OPTION( AI_PASSIVE
),
383 MAP_OPTION( AI_CANONNAME
),
384 MAP_OPTION( AI_NUMERICHOST
),
385 /* Linux/UNIX knows a lot more. But Windows only
386 * has 3 as far as I could see. -Marcus
390 static const int ws_niflag_map
[][2] =
392 MAP_OPTION( NI_NOFQDN
),
393 MAP_OPTION( NI_NUMERICHOST
),
394 MAP_OPTION( NI_NAMEREQD
),
395 MAP_OPTION( NI_NUMERICSERV
),
396 MAP_OPTION( NI_DGRAM
),
399 static const int ws_eai_map
[][2] =
401 MAP_OPTION( EAI_AGAIN
),
402 MAP_OPTION( EAI_BADFLAGS
),
403 MAP_OPTION( EAI_FAIL
),
404 MAP_OPTION( EAI_FAMILY
),
405 MAP_OPTION( EAI_MEMORY
),
406 /* Note: EAI_NODATA is deprecated, but still
407 * used by Windows and Linux... We map the newer
408 * EAI_NONAME to EAI_NODATA for now until Windows
412 MAP_OPTION( EAI_NODATA
),
415 { WS_EAI_NODATA
, EAI_NONAME
},
418 MAP_OPTION( EAI_SERVICE
),
419 MAP_OPTION( EAI_SOCKTYPE
),
423 static const char magic_loopback_addr
[] = {127, 12, 34, 56};
425 /* ----------------------------------- error handling */
427 static UINT
wsaErrno(void)
429 int loc_errno
= errno
;
430 WARN("errno %d, (%s).\n", loc_errno
, strerror(loc_errno
));
434 case EINTR
: return WSAEINTR
;
435 case EBADF
: return WSAEBADF
;
437 case EACCES
: return WSAEACCES
;
438 case EFAULT
: return WSAEFAULT
;
439 case EINVAL
: return WSAEINVAL
;
440 case EMFILE
: return WSAEMFILE
;
441 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
442 case EINPROGRESS
: return WSAEINPROGRESS
;
443 case EALREADY
: return WSAEALREADY
;
444 case ENOTSOCK
: return WSAENOTSOCK
;
445 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
446 case EMSGSIZE
: return WSAEMSGSIZE
;
447 case EPROTOTYPE
: return WSAEPROTOTYPE
;
448 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
449 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
450 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
451 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
452 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
453 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
454 case EADDRINUSE
: return WSAEADDRINUSE
;
455 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
456 case ENETDOWN
: return WSAENETDOWN
;
457 case ENETUNREACH
: return WSAENETUNREACH
;
458 case ENETRESET
: return WSAENETRESET
;
459 case ECONNABORTED
: return WSAECONNABORTED
;
461 case ECONNRESET
: return WSAECONNRESET
;
462 case ENOBUFS
: return WSAENOBUFS
;
463 case EISCONN
: return WSAEISCONN
;
464 case ENOTCONN
: return WSAENOTCONN
;
465 case ESHUTDOWN
: return WSAESHUTDOWN
;
466 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
467 case ETIMEDOUT
: return WSAETIMEDOUT
;
468 case ECONNREFUSED
: return WSAECONNREFUSED
;
469 case ELOOP
: return WSAELOOP
;
470 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
471 case EHOSTDOWN
: return WSAEHOSTDOWN
;
472 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
473 case ENOTEMPTY
: return WSAENOTEMPTY
;
475 case EPROCLIM
: return WSAEPROCLIM
;
478 case EUSERS
: return WSAEUSERS
;
481 case EDQUOT
: return WSAEDQUOT
;
484 case ESTALE
: return WSAESTALE
;
487 case EREMOTE
: return WSAEREMOTE
;
490 /* just in case we ever get here and there are no problems */
493 WARN("Unknown errno %d!\n", loc_errno
);
494 return WSAEOPNOTSUPP
;
498 static UINT
wsaHerrno(int loc_errno
)
501 WARN("h_errno %d.\n", loc_errno
);
505 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
506 case TRY_AGAIN
: return WSATRY_AGAIN
;
507 case NO_RECOVERY
: return WSANO_RECOVERY
;
508 case NO_DATA
: return WSANO_DATA
;
509 case ENOBUFS
: return WSAENOBUFS
;
513 WARN("Unknown h_errno %d!\n", loc_errno
);
514 return WSAEOPNOTSUPP
;
518 static inline DWORD
NtStatusToWSAError( const DWORD status
)
520 /* We only need to cover the status codes set by server async request handling */
524 case STATUS_SUCCESS
: wserr
= 0; break;
525 case STATUS_PENDING
: wserr
= WSA_IO_PENDING
; break;
526 case STATUS_OBJECT_TYPE_MISMATCH
: wserr
= WSAENOTSOCK
; break;
527 case STATUS_INVALID_HANDLE
: wserr
= WSAEBADF
; break;
528 case STATUS_INVALID_PARAMETER
: wserr
= WSAEINVAL
; break;
529 case STATUS_PIPE_DISCONNECTED
: wserr
= WSAESHUTDOWN
; break;
530 case STATUS_CANCELLED
: wserr
= WSA_OPERATION_ABORTED
; break;
531 case STATUS_TIMEOUT
: wserr
= WSAETIMEDOUT
; break;
532 case STATUS_NO_MEMORY
: wserr
= WSAEFAULT
; break;
534 if ( status
>= WSABASEERR
&& status
<= WSABASEERR
+1004 )
535 /* It is not an NT status code but a winsock error */
539 wserr
= RtlNtStatusToDosError( status
);
540 FIXME( "Status code %08x converted to DOS error code %x\n", status
, wserr
);
546 /* set last error code from NT status without mapping WSA errors */
547 static inline unsigned int set_error( unsigned int err
)
551 err
= NtStatusToWSAError( err
);
557 static inline int get_sock_fd( SOCKET s
, DWORD access
, unsigned int *options
)
560 if (set_error( wine_server_handle_to_fd( SOCKET2HANDLE(s
), access
, &fd
, options
) ))
565 static inline void release_sock_fd( SOCKET s
, int fd
)
567 wine_server_release_fd( SOCKET2HANDLE(s
), fd
);
570 static void _enable_event( HANDLE s
, unsigned int event
,
571 unsigned int sstate
, unsigned int cstate
)
573 SERVER_START_REQ( enable_socket_event
)
575 req
->handle
= wine_server_obj_handle( s
);
577 req
->sstate
= sstate
;
578 req
->cstate
= cstate
;
579 wine_server_call( req
);
584 static int _is_blocking(SOCKET s
)
587 SERVER_START_REQ( get_socket_event
)
589 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
590 req
->service
= FALSE
;
592 wine_server_call( req
);
593 ret
= (reply
->state
& FD_WINE_NONBLOCKING
) == 0;
599 static unsigned int _get_sock_mask(SOCKET s
)
602 SERVER_START_REQ( get_socket_event
)
604 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
605 req
->service
= FALSE
;
607 wine_server_call( req
);
614 static void _sync_sock_state(SOCKET s
)
616 /* do a dummy wineserver request in order to let
617 the wineserver run through its select loop once */
618 (void)_is_blocking(s
);
621 static int _get_sock_error(SOCKET s
, unsigned int bit
)
623 int events
[FD_MAX_EVENTS
];
625 SERVER_START_REQ( get_socket_event
)
627 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
628 req
->service
= FALSE
;
630 wine_server_set_reply( req
, events
, sizeof(events
) );
631 wine_server_call( req
);
637 static struct per_thread_data
*get_per_thread_data(void)
639 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
640 /* lazy initialization */
643 ptb
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ptb
) );
644 NtCurrentTeb()->WinSockData
= ptb
;
649 static void free_per_thread_data(void)
651 struct per_thread_data
* ptb
= NtCurrentTeb()->WinSockData
;
655 /* delete scratch buffers */
656 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
657 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
658 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
659 ptb
->he_buffer
= NULL
;
660 ptb
->se_buffer
= NULL
;
661 ptb
->pe_buffer
= NULL
;
663 HeapFree( GetProcessHeap(), 0, ptb
);
664 NtCurrentTeb()->WinSockData
= NULL
;
667 /***********************************************************************
668 * DllMain (WS2_32.init)
670 BOOL WINAPI
DllMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
672 TRACE("%p 0x%x %p\n", hInstDLL
, fdwReason
, fImpLoad
);
674 case DLL_PROCESS_ATTACH
:
676 case DLL_PROCESS_DETACH
:
677 free_per_thread_data();
680 case DLL_THREAD_DETACH
:
681 free_per_thread_data();
687 /***********************************************************************
690 * Converts socket flags from Windows format.
691 * Return 1 if converted, 0 if not (error).
693 static int convert_sockopt(INT
*level
, INT
*optname
)
700 for(i
=0; i
<sizeof(ws_sock_map
)/sizeof(ws_sock_map
[0]); i
++) {
701 if( ws_sock_map
[i
][0] == *optname
)
703 *optname
= ws_sock_map
[i
][1];
707 FIXME("Unknown SOL_SOCKET optname 0x%x\n", *optname
);
710 *level
= IPPROTO_TCP
;
711 for(i
=0; i
<sizeof(ws_tcp_map
)/sizeof(ws_tcp_map
[0]); i
++) {
712 if ( ws_tcp_map
[i
][0] == *optname
)
714 *optname
= ws_tcp_map
[i
][1];
718 FIXME("Unknown IPPROTO_TCP optname 0x%x\n", *optname
);
722 for(i
=0; i
<sizeof(ws_ip_map
)/sizeof(ws_ip_map
[0]); i
++) {
723 if (ws_ip_map
[i
][0] == *optname
)
725 *optname
= ws_ip_map
[i
][1];
729 FIXME("Unknown IPPROTO_IP optname 0x%x\n", *optname
);
731 default: FIXME("Unimplemented or unknown socket level\n");
736 /* ----------------------------------- Per-thread info (or per-process?) */
738 static char *strdup_lower(const char *str
)
741 char *ret
= HeapAlloc( GetProcessHeap(), 0, strlen(str
) + 1 );
745 for (i
= 0; str
[i
]; i
++) ret
[i
] = tolower(str
[i
]);
748 else SetLastError(WSAENOBUFS
);
752 static inline int sock_error_p(int s
)
754 unsigned int optval
, optlen
;
756 optlen
= sizeof(optval
);
757 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
758 if (optval
) WARN("\t[%i] error: %d\n", s
, optval
);
762 /* Utility: get the SO_RCVTIMEO or SO_SNDTIMEO socket option
763 * from an fd and return the value converted to milli seconds
764 * or -1 if there is an infinite time out */
765 static inline int get_rcvsnd_timeo( int fd
, int optname
)
768 unsigned int len
= sizeof(tv
);
769 int ret
= getsockopt(fd
, SOL_SOCKET
, optname
, &tv
, &len
);
771 ret
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
772 if( ret
<= 0 ) /* tv == {0,0} means infinite time out */
777 /* macro wrappers for portability */
779 #define GET_RCVTIMEO(fd) get_rcvsnd_timeo( (fd), SO_RCVTIMEO)
781 #define GET_RCVTIMEO(fd) (-1)
785 #define GET_SNDTIMEO(fd) get_rcvsnd_timeo( (fd), SO_SNDTIMEO)
787 #define GET_SNDTIMEO(fd) (-1)
790 /* utility: given an fd, will block until one of the events occurs */
791 static inline int do_block( int fd
, int events
, int timeout
)
799 while ((ret
= poll(&pfd
, 1, timeout
)) < 0)
810 convert_af_w2u(int windowsaf
) {
813 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
814 if (ws_af_map
[i
][0] == windowsaf
)
815 return ws_af_map
[i
][1];
816 FIXME("unhandled Windows address family %d\n", windowsaf
);
821 convert_af_u2w(int unixaf
) {
824 for (i
=0;i
<sizeof(ws_af_map
)/sizeof(ws_af_map
[0]);i
++)
825 if (ws_af_map
[i
][1] == unixaf
)
826 return ws_af_map
[i
][0];
827 FIXME("unhandled UNIX address family %d\n", unixaf
);
832 convert_proto_w2u(int windowsproto
) {
835 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
836 if (ws_proto_map
[i
][0] == windowsproto
)
837 return ws_proto_map
[i
][1];
838 FIXME("unhandled Windows socket protocol %d\n", windowsproto
);
843 convert_proto_u2w(int unixproto
) {
846 for (i
=0;i
<sizeof(ws_proto_map
)/sizeof(ws_proto_map
[0]);i
++)
847 if (ws_proto_map
[i
][1] == unixproto
)
848 return ws_proto_map
[i
][0];
849 FIXME("unhandled UNIX socket protocol %d\n", unixproto
);
854 convert_socktype_w2u(int windowssocktype
) {
857 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
858 if (ws_socktype_map
[i
][0] == windowssocktype
)
859 return ws_socktype_map
[i
][1];
860 FIXME("unhandled Windows socket type %d\n", windowssocktype
);
865 convert_socktype_u2w(int unixsocktype
) {
868 for (i
=0;i
<sizeof(ws_socktype_map
)/sizeof(ws_socktype_map
[0]);i
++)
869 if (ws_socktype_map
[i
][1] == unixsocktype
)
870 return ws_socktype_map
[i
][0];
871 FIXME("unhandled UNIX socket type %d\n", unixsocktype
);
875 /* ----------------------------------- API -----
877 * Init / cleanup / error checking.
880 /***********************************************************************
881 * WSAStartup (WS2_32.115)
883 int WINAPI
WSAStartup(WORD wVersionRequested
, LPWSADATA lpWSAData
)
885 TRACE("verReq=%x\n", wVersionRequested
);
887 if (LOBYTE(wVersionRequested
) < 1)
888 return WSAVERNOTSUPPORTED
;
890 if (!lpWSAData
) return WSAEINVAL
;
894 /* that's the whole of the negotiation for now */
895 lpWSAData
->wVersion
= wVersionRequested
;
896 /* return winsock information */
897 lpWSAData
->wHighVersion
= 0x0202;
898 strcpy(lpWSAData
->szDescription
, "WinSock 2.0" );
899 strcpy(lpWSAData
->szSystemStatus
, "Running" );
900 lpWSAData
->iMaxSockets
= WS_MAX_SOCKETS_PER_PROCESS
;
901 lpWSAData
->iMaxUdpDg
= WS_MAX_UDP_DATAGRAM
;
902 /* don't do anything with lpWSAData->lpVendorInfo */
903 /* (some apps don't allocate the space for this field) */
905 TRACE("succeeded\n");
910 /***********************************************************************
911 * WSACleanup (WS2_32.116)
913 INT WINAPI
WSACleanup(void)
919 SetLastError(WSANOTINITIALISED
);
924 /***********************************************************************
925 * WSAGetLastError (WS2_32.111)
927 INT WINAPI
WSAGetLastError(void)
929 return GetLastError();
932 /***********************************************************************
933 * WSASetLastError (WS2_32.112)
935 void WINAPI
WSASetLastError(INT iError
) {
936 SetLastError(iError
);
939 static struct WS_hostent
*check_buffer_he(int size
)
941 struct per_thread_data
* ptb
= get_per_thread_data();
944 if (ptb
->he_len
>= size
) return ptb
->he_buffer
;
945 HeapFree( GetProcessHeap(), 0, ptb
->he_buffer
);
947 ptb
->he_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->he_len
= size
) );
948 if (!ptb
->he_buffer
) SetLastError(WSAENOBUFS
);
949 return ptb
->he_buffer
;
952 static struct WS_servent
*check_buffer_se(int size
)
954 struct per_thread_data
* ptb
= get_per_thread_data();
957 if (ptb
->se_len
>= size
) return ptb
->se_buffer
;
958 HeapFree( GetProcessHeap(), 0, ptb
->se_buffer
);
960 ptb
->se_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->se_len
= size
) );
961 if (!ptb
->se_buffer
) SetLastError(WSAENOBUFS
);
962 return ptb
->se_buffer
;
965 static struct WS_protoent
*check_buffer_pe(int size
)
967 struct per_thread_data
* ptb
= get_per_thread_data();
970 if (ptb
->pe_len
>= size
) return ptb
->pe_buffer
;
971 HeapFree( GetProcessHeap(), 0, ptb
->pe_buffer
);
973 ptb
->pe_buffer
= HeapAlloc( GetProcessHeap(), 0, (ptb
->pe_len
= size
) );
974 if (!ptb
->pe_buffer
) SetLastError(WSAENOBUFS
);
975 return ptb
->pe_buffer
;
978 /* ----------------------------------- i/o APIs */
980 static inline BOOL
supported_pf(int pf
)
1001 /**********************************************************************/
1003 /* Returns the length of the converted address if successful, 0 if it was too small to
1006 static unsigned int ws_sockaddr_ws2u(const struct WS_sockaddr
* wsaddr
, int wsaddrlen
,
1007 union generic_unix_sockaddr
*uaddr
)
1009 unsigned int uaddrlen
= 0;
1011 switch (wsaddr
->sa_family
)
1016 const struct WS_sockaddr_ipx
* wsipx
=(const struct WS_sockaddr_ipx
*)wsaddr
;
1017 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)uaddr
;
1019 if (wsaddrlen
<sizeof(struct WS_sockaddr_ipx
))
1022 uaddrlen
= sizeof(struct sockaddr_ipx
);
1023 memset( uaddr
, 0, uaddrlen
);
1024 uipx
->sipx_family
=AF_IPX
;
1025 uipx
->sipx_port
=wsipx
->sa_socket
;
1026 /* copy sa_netnum and sa_nodenum to sipx_network and sipx_node
1029 memcpy(&uipx
->sipx_network
,wsipx
->sa_netnum
,sizeof(uipx
->sipx_network
)+sizeof(uipx
->sipx_node
));
1030 #ifdef IPX_FRAME_NONE
1031 uipx
->sipx_type
=IPX_FRAME_NONE
;
1037 struct sockaddr_in6
* uin6
= (struct sockaddr_in6
*)uaddr
;
1038 const struct WS_sockaddr_in6
* win6
= (const struct WS_sockaddr_in6
*)wsaddr
;
1040 /* Note: Windows has 2 versions of the sockaddr_in6 struct, one with
1041 * scope_id, one without.
1043 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6_old
)) {
1044 uaddrlen
= sizeof(struct sockaddr_in6
);
1045 memset( uaddr
, 0, uaddrlen
);
1046 uin6
->sin6_family
= AF_INET6
;
1047 uin6
->sin6_port
= win6
->sin6_port
;
1048 uin6
->sin6_flowinfo
= win6
->sin6_flowinfo
;
1049 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1050 if (wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) uin6
->sin6_scope_id
= win6
->sin6_scope_id
;
1052 memcpy(&uin6
->sin6_addr
,&win6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1055 FIXME("bad size %d for WS_sockaddr_in6\n",wsaddrlen
);
1059 struct sockaddr_in
* uin
= (struct sockaddr_in
*)uaddr
;
1060 const struct WS_sockaddr_in
* win
= (const struct WS_sockaddr_in
*)wsaddr
;
1062 if (wsaddrlen
<sizeof(struct WS_sockaddr_in
))
1064 uaddrlen
= sizeof(struct sockaddr_in
);
1065 memset( uaddr
, 0, uaddrlen
);
1066 uin
->sin_family
= AF_INET
;
1067 uin
->sin_port
= win
->sin_port
;
1068 memcpy(&uin
->sin_addr
,&win
->sin_addr
,4); /* 4 bytes = 32 address bits */
1073 struct sockaddr_irda
*uin
= (struct sockaddr_irda
*)uaddr
;
1074 const SOCKADDR_IRDA
*win
= (const SOCKADDR_IRDA
*)wsaddr
;
1076 if (wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1078 uaddrlen
= sizeof(struct sockaddr_irda
);
1079 memset( uaddr
, 0, uaddrlen
);
1080 uin
->sir_family
= AF_IRDA
;
1081 if (!strncmp( win
->irdaServiceName
, "LSAP-SEL", strlen( "LSAP-SEL" ) ))
1083 unsigned int lsap_sel
;
1085 sscanf( win
->irdaServiceName
, "LSAP-SEL%u", &lsap_sel
);
1086 uin
->sir_lsap_sel
= lsap_sel
;
1090 uin
->sir_lsap_sel
= LSAP_ANY
;
1091 memcpy( uin
->sir_name
, win
->irdaServiceName
, 25 );
1093 memcpy( &uin
->sir_addr
, win
->irdaDeviceID
, sizeof(uin
->sir_addr
) );
1097 case WS_AF_UNSPEC
: {
1098 /* Try to determine the needed space by the passed windows sockaddr space */
1099 switch (wsaddrlen
) {
1100 default: /* likely a ipv4 address */
1101 case sizeof(struct WS_sockaddr_in
):
1102 uaddrlen
= sizeof(struct sockaddr_in
);
1105 case sizeof(struct WS_sockaddr_ipx
):
1106 uaddrlen
= sizeof(struct sockaddr_ipx
);
1110 case sizeof(SOCKADDR_IRDA
):
1111 uaddrlen
= sizeof(struct sockaddr_irda
);
1114 case sizeof(struct WS_sockaddr_in6
):
1115 case sizeof(struct WS_sockaddr_in6_old
):
1116 uaddrlen
= sizeof(struct sockaddr_in6
);
1119 memset( uaddr
, 0, uaddrlen
);
1123 FIXME("Unknown address family %d, return NULL.\n", wsaddr
->sa_family
);
1129 static BOOL
is_sockaddr_bound(const struct sockaddr
*uaddr
, int uaddrlen
)
1131 switch (uaddr
->sa_family
)
1135 FIXME("don't know how to tell if IPX socket is bound, assuming it is!\n");
1140 static const struct sockaddr_in6 emptyAddr
;
1141 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) uaddr
;
1142 return in6
->sin6_port
|| memcmp(&in6
->sin6_addr
, &emptyAddr
.sin6_addr
, sizeof(struct in6_addr
));
1146 static const struct sockaddr_in emptyAddr
;
1147 const struct sockaddr_in
*in
= (const struct sockaddr_in
*) uaddr
;
1148 return in
->sin_port
|| memcmp(&in
->sin_addr
, &emptyAddr
.sin_addr
, sizeof(struct in_addr
));
1153 FIXME("unknown address family %d\n", uaddr
->sa_family
);
1158 /* Returns 0 if successful, -1 if the buffer is too small */
1159 static int ws_sockaddr_u2ws(const struct sockaddr
* uaddr
, struct WS_sockaddr
* wsaddr
, int* wsaddrlen
)
1163 switch(uaddr
->sa_family
)
1168 const struct sockaddr_ipx
* uipx
=(const struct sockaddr_ipx
*)uaddr
;
1169 struct WS_sockaddr_ipx
* wsipx
=(struct WS_sockaddr_ipx
*)wsaddr
;
1172 switch (*wsaddrlen
) /* how much can we copy? */
1176 *wsaddrlen
= sizeof(*wsipx
);
1177 wsipx
->sa_socket
=uipx
->sipx_port
;
1181 memcpy(wsipx
->sa_nodenum
,uipx
->sipx_node
,sizeof(wsipx
->sa_nodenum
));
1189 memcpy(wsipx
->sa_netnum
,&uipx
->sipx_network
,sizeof(wsipx
->sa_netnum
));
1195 wsipx
->sa_family
=WS_AF_IPX
;
1207 const struct sockaddr_irda
*uin
= (const struct sockaddr_irda
*)uaddr
;
1208 SOCKADDR_IRDA
*win
= (SOCKADDR_IRDA
*)wsaddr
;
1210 if (*wsaddrlen
< sizeof(SOCKADDR_IRDA
))
1212 win
->irdaAddressFamily
= WS_AF_IRDA
;
1213 memcpy( win
->irdaDeviceID
, &uin
->sir_addr
, sizeof(win
->irdaDeviceID
) );
1214 if (uin
->sir_lsap_sel
!= LSAP_ANY
)
1215 sprintf( win
->irdaServiceName
, "LSAP-SEL%u", uin
->sir_lsap_sel
);
1217 memcpy( win
->irdaServiceName
, uin
->sir_name
,
1218 sizeof(win
->irdaServiceName
) );
1223 const struct sockaddr_in6
* uin6
= (const struct sockaddr_in6
*)uaddr
;
1224 struct WS_sockaddr_in6_old
* win6old
= (struct WS_sockaddr_in6_old
*)wsaddr
;
1226 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in6_old
))
1228 win6old
->sin6_family
= WS_AF_INET6
;
1229 win6old
->sin6_port
= uin6
->sin6_port
;
1230 win6old
->sin6_flowinfo
= uin6
->sin6_flowinfo
;
1231 memcpy(&win6old
->sin6_addr
,&uin6
->sin6_addr
,16); /* 16 bytes = 128 address bits */
1232 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
1233 if (*wsaddrlen
>= sizeof(struct WS_sockaddr_in6
)) {
1234 struct WS_sockaddr_in6
* win6
= (struct WS_sockaddr_in6
*)wsaddr
;
1235 win6
->sin6_scope_id
= uin6
->sin6_scope_id
;
1236 *wsaddrlen
= sizeof(struct WS_sockaddr_in6
);
1239 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1241 *wsaddrlen
= sizeof(struct WS_sockaddr_in6_old
);
1246 const struct sockaddr_in
* uin
= (const struct sockaddr_in
*)uaddr
;
1247 struct WS_sockaddr_in
* win
= (struct WS_sockaddr_in
*)wsaddr
;
1249 if (*wsaddrlen
< sizeof(struct WS_sockaddr_in
))
1251 win
->sin_family
= WS_AF_INET
;
1252 win
->sin_port
= uin
->sin_port
;
1253 memcpy(&win
->sin_addr
,&uin
->sin_addr
,4); /* 4 bytes = 32 address bits */
1254 memset(win
->sin_zero
, 0, 8); /* Make sure the null padding is null */
1255 *wsaddrlen
= sizeof(struct WS_sockaddr_in
);
1259 memset(wsaddr
,0,*wsaddrlen
);
1263 FIXME("Unknown address family %d\n", uaddr
->sa_family
);
1269 /**************************************************************************
1270 * Functions for handling overlapped I/O
1271 **************************************************************************/
1273 /* user APC called upon async completion */
1274 static void WINAPI
ws2_async_apc( void *arg
, IO_STATUS_BLOCK
*iosb
, ULONG reserved
)
1276 ws2_async
*wsa
= arg
;
1278 if (wsa
->completion_func
) wsa
->completion_func( NtStatusToWSAError(iosb
->u
.Status
),
1279 iosb
->Information
, wsa
->user_overlapped
,
1281 HeapFree( GetProcessHeap(), 0, wsa
);
1284 /***********************************************************************
1285 * WS2_recv (INTERNAL)
1287 * Workhorse for both synchronous and asynchronous recv() operations.
1289 static int WS2_recv( int fd
, struct ws2_async
*wsa
)
1292 union generic_unix_sockaddr unix_sockaddr
;
1295 hdr
.msg_name
= NULL
;
1299 hdr
.msg_namelen
= sizeof(unix_sockaddr
);
1300 hdr
.msg_name
= &unix_sockaddr
;
1303 hdr
.msg_namelen
= 0;
1305 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1306 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1307 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1308 hdr
.msg_accrights
= NULL
;
1309 hdr
.msg_accrightslen
= 0;
1311 hdr
.msg_control
= NULL
;
1312 hdr
.msg_controllen
= 0;
1316 if ( (n
= recvmsg(fd
, &hdr
, wsa
->flags
)) == -1 )
1319 /* if this socket is connected and lpFrom is not NULL, Linux doesn't give us
1320 * msg_name and msg_namelen from recvmsg, but it does set msg_namelen to zero.
1322 * quoting linux 2.6 net/ipv4/tcp.c:
1323 * "According to UNIX98, msg_name/msg_namelen are ignored
1324 * on connected socket. I was just happy when found this 8) --ANK"
1326 * likewise MSDN says that lpFrom and lpFromlen are ignored for
1327 * connection-oriented sockets, so don't try to update lpFrom.
1329 if (wsa
->addr
&& hdr
.msg_namelen
)
1330 ws_sockaddr_u2ws( &unix_sockaddr
.addr
, wsa
->addr
, wsa
->addrlen
.ptr
);
1335 /***********************************************************************
1336 * WS2_async_recv (INTERNAL)
1338 * Handler for overlapped recv() operations.
1340 static NTSTATUS
WS2_async_recv( void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1342 ws2_async
* wsa
= user
;
1347 case STATUS_ALERTED
:
1348 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_READ_DATA
, &fd
, NULL
) ))
1351 result
= WS2_recv( fd
, wsa
);
1352 wine_server_release_fd( wsa
->hSocket
, fd
);
1355 status
= STATUS_SUCCESS
;
1356 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1360 if (errno
== EINTR
|| errno
== EAGAIN
)
1362 status
= STATUS_PENDING
;
1363 _enable_event( wsa
->hSocket
, FD_READ
, 0, 0 );
1368 status
= wsaErrno(); /* FIXME: is this correct ???? */
1373 if (status
!= STATUS_PENDING
)
1375 iosb
->u
.Status
= status
;
1376 iosb
->Information
= result
;
1377 *apc
= ws2_async_apc
;
1382 /***********************************************************************
1383 * WS2_send (INTERNAL)
1385 * Workhorse for both synchronous and asynchronous send() operations.
1387 static int WS2_send( int fd
, struct ws2_async
*wsa
)
1390 union generic_unix_sockaddr unix_addr
;
1392 hdr
.msg_name
= NULL
;
1393 hdr
.msg_namelen
= 0;
1397 hdr
.msg_name
= &unix_addr
;
1398 hdr
.msg_namelen
= ws_sockaddr_ws2u( wsa
->addr
, wsa
->addrlen
.val
, &unix_addr
);
1399 if ( !hdr
.msg_namelen
)
1405 #if defined(HAVE_IPX) && defined(SOL_IPX)
1406 if(wsa
->addr
->sa_family
== WS_AF_IPX
)
1408 struct sockaddr_ipx
* uipx
= (struct sockaddr_ipx
*)hdr
.msg_name
;
1410 unsigned int len
=sizeof(int);
1412 /* The packet type is stored at the ipx socket level; At least the linux kernel seems
1413 * to do something with it in case hdr.msg_name is NULL. Nonetheless can we use it to store
1414 * the packet type and then we can retrieve it using getsockopt. After that we can set the
1415 * ipx type in the sockaddr_opx structure with the stored value.
1417 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, &val
, &len
) != -1)
1418 uipx
->sipx_type
= val
;
1423 hdr
.msg_iov
= wsa
->iovec
+ wsa
->first_iovec
;
1424 hdr
.msg_iovlen
= wsa
->n_iovecs
- wsa
->first_iovec
;
1425 #ifdef HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS
1426 hdr
.msg_accrights
= NULL
;
1427 hdr
.msg_accrightslen
= 0;
1429 hdr
.msg_control
= NULL
;
1430 hdr
.msg_controllen
= 0;
1434 return sendmsg(fd
, &hdr
, wsa
->flags
);
1437 /***********************************************************************
1438 * WS2_async_send (INTERNAL)
1440 * Handler for overlapped send() operations.
1442 static NTSTATUS
WS2_async_send(void* user
, IO_STATUS_BLOCK
* iosb
, NTSTATUS status
, void **apc
)
1444 ws2_async
* wsa
= user
;
1449 case STATUS_ALERTED
:
1450 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, FILE_WRITE_DATA
, &fd
, NULL
) ))
1453 /* check to see if the data is ready (non-blocking) */
1454 result
= WS2_send( fd
, wsa
);
1455 wine_server_release_fd( wsa
->hSocket
, fd
);
1459 int totalLength
= 0;
1461 status
= STATUS_SUCCESS
;
1462 for (i
= 0; i
< wsa
->n_iovecs
; i
++)
1463 totalLength
+= wsa
->iovec
[i
].iov_len
;
1464 if (result
< totalLength
)
1465 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1469 if (errno
== EINTR
|| errno
== EAGAIN
)
1471 status
= STATUS_PENDING
;
1472 _enable_event( wsa
->hSocket
, FD_WRITE
, 0, 0 );
1476 /* We set the status to a winsock error code and check for that
1477 later in NtStatusToWSAError () */
1478 status
= wsaErrno();
1484 if (status
!= STATUS_PENDING
)
1486 iosb
->u
.Status
= status
;
1487 iosb
->Information
= result
;
1488 *apc
= ws2_async_apc
;
1493 /***********************************************************************
1494 * WS2_async_shutdown (INTERNAL)
1496 * Handler for shutdown() operations on overlapped sockets.
1498 static NTSTATUS
WS2_async_shutdown( void* user
, PIO_STATUS_BLOCK iosb
, NTSTATUS status
, void **apc
)
1500 ws2_async
* wsa
= user
;
1505 case STATUS_ALERTED
:
1506 if ((status
= wine_server_handle_to_fd( wsa
->hSocket
, 0, &fd
, NULL
) ))
1509 switch ( wsa
->type
)
1511 case ASYNC_TYPE_READ
: err
= shutdown( fd
, 0 ); break;
1512 case ASYNC_TYPE_WRITE
: err
= shutdown( fd
, 1 ); break;
1514 wine_server_release_fd( wsa
->hSocket
, fd
);
1515 status
= err
? wsaErrno() : STATUS_SUCCESS
;
1518 iosb
->u
.Status
= status
;
1519 iosb
->Information
= 0;
1520 *apc
= ws2_async_apc
;
1524 /***********************************************************************
1525 * WS2_register_async_shutdown (INTERNAL)
1527 * Helper function for WS_shutdown() on overlapped sockets.
1529 static int WS2_register_async_shutdown( SOCKET s
, int type
)
1531 struct ws2_async
*wsa
;
1534 TRACE("s %ld type %d\n", s
, type
);
1536 wsa
= HeapAlloc( GetProcessHeap(), 0, sizeof(*wsa
) );
1540 wsa
->hSocket
= SOCKET2HANDLE(s
);
1542 wsa
->completion_func
= NULL
;
1544 SERVER_START_REQ( register_async
)
1547 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
1548 req
->async
.callback
= wine_server_client_ptr( WS2_async_shutdown
);
1549 req
->async
.iosb
= wine_server_client_ptr( &wsa
->local_iosb
);
1550 req
->async
.arg
= wine_server_client_ptr( wsa
);
1551 req
->async
.cvalue
= 0;
1552 status
= wine_server_call( req
);
1556 if (status
!= STATUS_PENDING
)
1558 HeapFree( GetProcessHeap(), 0, wsa
);
1559 return NtStatusToWSAError( status
);
1564 /***********************************************************************
1567 SOCKET WINAPI
WS_accept(SOCKET s
, struct WS_sockaddr
*addr
,
1574 TRACE("socket %04lx\n", s
);
1575 is_blocking
= _is_blocking(s
);
1578 /* try accepting first (if there is a deferred connection) */
1579 SERVER_START_REQ( accept_socket
)
1581 req
->lhandle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
1582 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
1583 req
->attributes
= OBJ_INHERIT
;
1584 status
= wine_server_call( req
);
1585 as
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
1590 if (addr
) WS_getpeername(as
, addr
, addrlen32
);
1593 if (is_blocking
&& status
== WSAEWOULDBLOCK
)
1595 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1597 do_block(fd
, POLLIN
, -1);
1598 _sync_sock_state(s
); /* let wineserver notice connection */
1599 release_sock_fd( s
, fd
);
1601 } while (is_blocking
&& status
== WSAEWOULDBLOCK
);
1604 return INVALID_SOCKET
;
1607 /***********************************************************************
1610 int WINAPI
WS_bind(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1612 int fd
= get_sock_fd( s
, 0, NULL
);
1613 int res
= SOCKET_ERROR
;
1615 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1619 if (!name
|| (name
->sa_family
&& !supported_pf(name
->sa_family
)))
1621 SetLastError(WSAEAFNOSUPPORT
);
1625 union generic_unix_sockaddr uaddr
;
1626 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1629 SetLastError(WSAEFAULT
);
1634 const struct sockaddr_in6
*in6
= (const struct sockaddr_in6
*) &uaddr
;
1635 if (name
->sa_family
== WS_AF_INET6
&&
1636 !memcmp(&in6
->sin6_addr
, &in6addr_any
, sizeof(struct in6_addr
)))
1639 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &enable
, sizeof(enable
)) == -1)
1641 release_sock_fd( s
, fd
);
1642 SetLastError(WSAEAFNOSUPPORT
);
1643 return SOCKET_ERROR
;
1647 if (name
->sa_family
== WS_AF_INET
)
1649 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
1650 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
1652 /* Trying to bind to the default host interface, using
1653 * INADDR_ANY instead*/
1654 WARN("Trying to bind to magic IP address, using "
1655 "INADDR_ANY instead.\n");
1656 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_ANY
);
1659 if (bind(fd
, &uaddr
.addr
, uaddrlen
) < 0)
1661 int loc_errno
= errno
;
1662 WARN("\tfailure - errno = %i\n", errno
);
1667 SetLastError(WSAENOTSOCK
);
1670 SetLastError(WSAEINVAL
);
1673 SetLastError(wsaErrno());
1679 res
=0; /* success */
1683 release_sock_fd( s
, fd
);
1688 /***********************************************************************
1689 * closesocket (WS2_32.3)
1691 int WINAPI
WS_closesocket(SOCKET s
)
1693 TRACE("socket %04lx\n", s
);
1694 if (CloseHandle(SOCKET2HANDLE(s
))) return 0;
1695 return SOCKET_ERROR
;
1698 /***********************************************************************
1699 * connect (WS2_32.4)
1701 int WINAPI
WS_connect(SOCKET s
, const struct WS_sockaddr
* name
, int namelen
)
1703 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
1705 TRACE("socket %04lx, ptr %p %s, length %d\n", s
, name
, debugstr_sockaddr(name
), namelen
);
1709 union generic_unix_sockaddr uaddr
;
1710 unsigned int uaddrlen
= ws_sockaddr_ws2u(name
, namelen
, &uaddr
);
1714 SetLastError(WSAEFAULT
);
1718 if (name
->sa_family
== WS_AF_INET
)
1720 struct sockaddr_in
*in4
= (struct sockaddr_in
*) &uaddr
;
1721 if (memcmp(&in4
->sin_addr
, magic_loopback_addr
, 4) == 0)
1723 /* Trying to connect to magic replace-loopback address,
1724 * assuming we really want to connect to localhost */
1725 TRACE("Trying to connect to magic IP address, using "
1726 "INADDR_LOOPBACK instead.\n");
1727 in4
->sin_addr
.s_addr
= htonl(WS_INADDR_LOOPBACK
);
1731 if (connect(fd
, &uaddr
.addr
, uaddrlen
) == 0)
1732 goto connect_success
;
1735 if (errno
== EINPROGRESS
)
1737 /* tell wineserver that a connection is in progress */
1738 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1739 FD_CONNECT
|FD_READ
|FD_WRITE
,
1740 FD_WINE_CONNECTED
|FD_WINE_LISTENING
);
1741 if (_is_blocking(s
))
1745 do_block(fd
, POLLIN
| POLLOUT
, -1);
1746 _sync_sock_state(s
); /* let wineserver notice connection */
1747 /* retrieve any error codes from it */
1748 result
= _get_sock_error(s
, FD_CONNECT_BIT
);
1750 SetLastError(result
);
1753 goto connect_success
;
1758 SetLastError(WSAEWOULDBLOCK
);
1763 SetLastError(wsaErrno());
1765 release_sock_fd( s
, fd
);
1767 return SOCKET_ERROR
;
1770 release_sock_fd( s
, fd
);
1771 _enable_event(SOCKET2HANDLE(s
), FD_CONNECT
|FD_READ
|FD_WRITE
,
1772 FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
,
1773 FD_CONNECT
|FD_WINE_LISTENING
);
1777 /***********************************************************************
1778 * WSAConnect (WS2_32.30)
1780 int WINAPI
WSAConnect( SOCKET s
, const struct WS_sockaddr
* name
, int namelen
,
1781 LPWSABUF lpCallerData
, LPWSABUF lpCalleeData
,
1782 LPQOS lpSQOS
, LPQOS lpGQOS
)
1784 if ( lpCallerData
|| lpCalleeData
|| lpSQOS
|| lpGQOS
)
1785 FIXME("unsupported parameters!\n");
1786 return WS_connect( s
, name
, namelen
);
1790 /***********************************************************************
1791 * getpeername (WS2_32.5)
1793 int WINAPI
WS_getpeername(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1798 TRACE("socket: %04lx, ptr %p, len %08x\n", s
, name
, *namelen
);
1800 fd
= get_sock_fd( s
, 0, NULL
);
1805 union generic_unix_sockaddr uaddr
;
1806 unsigned int uaddrlen
= sizeof(uaddr
);
1808 if (getpeername(fd
, &uaddr
.addr
, &uaddrlen
) == 0)
1810 if (!name
|| !namelen
)
1811 SetLastError(WSAEFAULT
);
1812 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1813 /* The buffer was too small */
1814 SetLastError(WSAEFAULT
);
1819 SetLastError(wsaErrno());
1820 release_sock_fd( s
, fd
);
1825 /***********************************************************************
1826 * getsockname (WS2_32.6)
1828 int WINAPI
WS_getsockname(SOCKET s
, struct WS_sockaddr
*name
, int *namelen
)
1833 TRACE("socket: %04lx, ptr %p, len %8x\n", s
, name
, *namelen
);
1835 /* Check if what we've received is valid. Should we use IsBadReadPtr? */
1836 if( (name
== NULL
) || (namelen
== NULL
) )
1838 SetLastError( WSAEFAULT
);
1839 return SOCKET_ERROR
;
1842 fd
= get_sock_fd( s
, 0, NULL
);
1847 union generic_unix_sockaddr uaddr
;
1848 unsigned int uaddrlen
= sizeof(uaddr
);
1850 if (getsockname(fd
, &uaddr
.addr
, &uaddrlen
) != 0)
1852 SetLastError(wsaErrno());
1854 else if (!is_sockaddr_bound(&uaddr
.addr
, uaddrlen
))
1856 SetLastError(WSAEINVAL
);
1858 else if (ws_sockaddr_u2ws(&uaddr
.addr
, name
, namelen
) != 0)
1860 /* The buffer was too small */
1861 SetLastError(WSAEFAULT
);
1867 release_sock_fd( s
, fd
);
1872 /***********************************************************************
1873 * getsockopt (WS2_32.7)
1875 INT WINAPI
WS_getsockopt(SOCKET s
, INT level
,
1876 INT optname
, char *optval
, INT
*optlen
)
1881 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
1882 s
, level
, optname
, optval
, *optlen
);
1890 /* Handle common cases. The special cases are below, sorted
1892 case WS_SO_ACCEPTCONN
:
1893 case WS_SO_BROADCAST
:
1896 case WS_SO_KEEPALIVE
:
1897 case WS_SO_OOBINLINE
:
1899 case WS_SO_REUSEADDR
:
1902 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1903 return SOCKET_ERROR
;
1904 convert_sockopt(&level
, &optname
);
1905 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
1907 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1910 release_sock_fd( s
, fd
);
1913 case WS_SO_DONTLINGER
:
1915 struct linger lingval
;
1916 unsigned int len
= sizeof(struct linger
);
1918 if (!optlen
|| *optlen
< sizeof(BOOL
)|| !optval
)
1920 SetLastError(WSAEFAULT
);
1921 return SOCKET_ERROR
;
1923 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1924 return SOCKET_ERROR
;
1926 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1928 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1933 *(BOOL
*)optval
= (lingval
.l_onoff
) ? FALSE
: TRUE
;
1934 *optlen
= sizeof(BOOL
);
1937 release_sock_fd( s
, fd
);
1941 /* As mentioned in setsockopt, Windows ignores this, so we
1942 * always return true here */
1943 case WS_SO_DONTROUTE
:
1944 if (!optlen
|| *optlen
< sizeof(BOOL
) || !optval
)
1946 SetLastError(WSAEFAULT
);
1947 return SOCKET_ERROR
;
1949 *(BOOL
*)optval
= TRUE
;
1950 *optlen
= sizeof(BOOL
);
1955 struct linger lingval
;
1956 unsigned int len
= sizeof(struct linger
);
1958 /* struct linger and LINGER have different sizes */
1959 if (!optlen
|| *optlen
< sizeof(LINGER
) || !optval
)
1961 SetLastError(WSAEFAULT
);
1962 return SOCKET_ERROR
;
1964 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
1965 return SOCKET_ERROR
;
1967 if (getsockopt(fd
, SOL_SOCKET
, SO_LINGER
, &lingval
, &len
) != 0 )
1969 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
1974 ((LINGER
*)optval
)->l_onoff
= lingval
.l_onoff
;
1975 ((LINGER
*)optval
)->l_linger
= lingval
.l_linger
;
1976 *optlen
= sizeof(struct linger
);
1979 release_sock_fd( s
, fd
);
1983 case WS_SO_MAX_MSG_SIZE
:
1984 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1986 SetLastError(WSAEFAULT
);
1987 return SOCKET_ERROR
;
1989 TRACE("getting global SO_MAX_MSG_SIZE = 65507\n");
1990 *(int *)optval
= 65507;
1991 *optlen
= sizeof(int);
1994 /* SO_OPENTYPE does not require a valid socket handle. */
1995 case WS_SO_OPENTYPE
:
1996 if (!optlen
|| *optlen
< sizeof(int) || !optval
)
1998 SetLastError(WSAEFAULT
);
1999 return SOCKET_ERROR
;
2001 *(int *)optval
= get_per_thread_data()->opentype
;
2002 *optlen
= sizeof(int);
2003 TRACE("getting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
2007 case WS_SO_RCVTIMEO
:
2010 case WS_SO_SNDTIMEO
:
2012 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
2015 unsigned int len
= sizeof(struct timeval
);
2017 if (!optlen
|| *optlen
< sizeof(int)|| !optval
)
2019 SetLastError(WSAEFAULT
);
2020 return SOCKET_ERROR
;
2022 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2023 return SOCKET_ERROR
;
2025 convert_sockopt(&level
, &optname
);
2026 if (getsockopt(fd
, level
, optname
, &tv
, &len
) != 0 )
2028 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2033 *(int *)optval
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
2034 *optlen
= sizeof(int);
2037 release_sock_fd( s
, fd
);
2042 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
2043 SetLastError(WSAENOPROTOOPT
);
2044 return SOCKET_ERROR
;
2045 } /* end switch(optname) */
2046 }/* end case WS_SOL_SOCKET */
2050 struct WS_sockaddr_ipx addr
;
2051 IPX_ADDRESS_DATA
*data
;
2056 if ((fd
= get_sock_fd( s
, 0, NULL
)) == -1) return SOCKET_ERROR
;
2058 if(getsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, (unsigned int*)optlen
) == -1)
2065 socklen_t len
=sizeof(struct ipx
);
2066 if(getsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, &len
) == -1 )
2069 *optval
= (int)val
.ipx_pt
;
2072 TRACE("ptype: %d (fd: %d)\n", *(int*)optval
, fd
);
2073 release_sock_fd( s
, fd
);
2078 * On a Win2000 system with one network card there are usually
2079 * three ipx devices one with a speed of 28.8kbps, 10Mbps and 100Mbps.
2080 * Using this call you can then retrieve info about this all.
2081 * In case of Linux it is a bit different. Usually you have
2082 * only "one" device active and further it is not possible to
2083 * query things like the linkspeed.
2085 FIXME("IPX_ADDRESS\n");
2086 namelen
= sizeof(struct WS_sockaddr_ipx
);
2087 memset(&addr
, 0, sizeof(struct WS_sockaddr_ipx
));
2088 WS_getsockname(s
, (struct WS_sockaddr
*)&addr
, &namelen
);
2090 data
= (IPX_ADDRESS_DATA
*)optval
;
2091 memcpy(data
->nodenum
,addr
.sa_nodenum
,sizeof(data
->nodenum
));
2092 memcpy(data
->netnum
,addr
.sa_netnum
,sizeof(data
->netnum
));
2093 data
->adapternum
= 0;
2094 data
->wan
= FALSE
; /* We are not on a wan for now .. */
2095 data
->status
= FALSE
; /* Since we are not on a wan, the wan link isn't up */
2096 data
->maxpkt
= 1467; /* This value is the default one, at least on Win2k/WinXP */
2097 data
->linkspeed
= 100000; /* Set the line speed in 100bit/s to 10 Mbit;
2098 * note 1MB = 1000kB in this case */
2101 case IPX_MAX_ADAPTER_NUM
:
2102 FIXME("IPX_MAX_ADAPTER_NUM\n");
2103 *(int*)optval
= 1; /* As noted under IPX_ADDRESS we have just one card. */
2107 FIXME("IPX optname:%x\n", optname
);
2108 return SOCKET_ERROR
;
2109 }/* end switch(optname) */
2110 } /* end case NSPROTO_IPX */
2117 case WS_IRLMP_ENUMDEVICES
:
2119 static const int MAX_IRDA_DEVICES
= 10;
2120 char buf
[sizeof(struct irda_device_list
) +
2121 (MAX_IRDA_DEVICES
- 1) * sizeof(struct irda_device_info
)];
2123 socklen_t len
= sizeof(buf
);
2125 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2126 return SOCKET_ERROR
;
2127 res
= getsockopt( fd
, SOL_IRLMP
, IRLMP_ENUMDEVICES
, buf
, &len
);
2130 SetLastError(wsaErrno());
2131 return SOCKET_ERROR
;
2135 struct irda_device_list
*src
= (struct irda_device_list
*)buf
;
2136 DEVICELIST
*dst
= (DEVICELIST
*)optval
;
2137 INT needed
= sizeof(DEVICELIST
), i
;
2140 needed
+= (src
->len
- 1) * sizeof(IRDA_DEVICE_INFO
);
2141 if (*optlen
< needed
)
2143 SetLastError(WSAEFAULT
);
2144 return SOCKET_ERROR
;
2147 TRACE("IRLMP_ENUMDEVICES: %d devices found:\n", src
->len
);
2148 dst
->numDevice
= src
->len
;
2149 for (i
= 0; i
< src
->len
; i
++)
2151 TRACE("saddr = %08x, daddr = %08x, info = %s, hints = %02x%02x\n",
2152 src
->dev
[i
].saddr
, src
->dev
[i
].daddr
,
2153 src
->dev
[i
].info
, src
->dev
[i
].hints
[0],
2154 src
->dev
[i
].hints
[1]);
2155 memcpy( dst
->Device
[i
].irdaDeviceID
,
2157 sizeof(dst
->Device
[i
].irdaDeviceID
) ) ;
2158 memcpy( dst
->Device
[i
].irdaDeviceName
,
2160 sizeof(dst
->Device
[i
].irdaDeviceName
) ) ;
2161 memcpy( &dst
->Device
[i
].irdaDeviceHints1
,
2162 &src
->dev
[i
].hints
[0],
2163 sizeof(dst
->Device
[i
].irdaDeviceHints1
) ) ;
2164 memcpy( &dst
->Device
[i
].irdaDeviceHints2
,
2165 &src
->dev
[i
].hints
[1],
2166 sizeof(dst
->Device
[i
].irdaDeviceHints2
) ) ;
2167 dst
->Device
[i
].irdaCharSet
= src
->dev
[i
].charset
;
2173 FIXME("IrDA optname:0x%x\n", optname
);
2174 return SOCKET_ERROR
;
2176 break; /* case WS_SOL_IRLMP */
2179 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
2180 case WS_IPPROTO_TCP
:
2183 case WS_TCP_NODELAY
:
2184 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2185 return SOCKET_ERROR
;
2186 convert_sockopt(&level
, &optname
);
2187 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2189 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2192 release_sock_fd( s
, fd
);
2195 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
2196 return SOCKET_ERROR
;
2201 case WS_IP_ADD_MEMBERSHIP
:
2202 case WS_IP_DROP_MEMBERSHIP
:
2206 case WS_IP_MULTICAST_IF
:
2207 case WS_IP_MULTICAST_LOOP
:
2208 case WS_IP_MULTICAST_TTL
:
2212 if ( (fd
= get_sock_fd( s
, 0, NULL
)) == -1)
2213 return SOCKET_ERROR
;
2214 convert_sockopt(&level
, &optname
);
2215 if (getsockopt(fd
, level
, optname
, optval
, (unsigned int *)optlen
) != 0 )
2217 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2220 release_sock_fd( s
, fd
);
2222 case WS_IP_DONTFRAGMENT
:
2223 FIXME("WS_IP_DONTFRAGMENT is always false!\n");
2224 *(BOOL
*)optval
= FALSE
;
2227 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
2228 return SOCKET_ERROR
;
2230 case WS_IPPROTO_IPV6
:
2231 FIXME("IPPROTO_IPV6 unimplemented (optname 0x%08x)\n", optname
);
2232 return SOCKET_ERROR
;
2235 WARN("Unknown level: 0x%08x\n", level
);
2236 SetLastError(WSAEINVAL
);
2237 return SOCKET_ERROR
;
2238 } /* end switch(level) */
2241 /***********************************************************************
2244 WS_u_long WINAPI
WS_htonl(WS_u_long hostlong
)
2246 return htonl(hostlong
);
2250 /***********************************************************************
2253 WS_u_short WINAPI
WS_htons(WS_u_short hostshort
)
2255 return htons(hostshort
);
2258 /***********************************************************************
2259 * WSAHtonl (WS2_32.46)
2260 * From MSDN description of error codes, this function should also
2261 * check if WinSock has been initialized and the socket is a valid
2262 * socket. But why? This function only translates a host byte order
2263 * u_long into a network byte order u_long...
2265 int WINAPI
WSAHtonl(SOCKET s
, WS_u_long hostlong
, WS_u_long
*lpnetlong
)
2269 *lpnetlong
= htonl(hostlong
);
2272 WSASetLastError(WSAEFAULT
);
2273 return SOCKET_ERROR
;
2276 /***********************************************************************
2277 * WSAHtons (WS2_32.47)
2278 * From MSDN description of error codes, this function should also
2279 * check if WinSock has been initialized and the socket is a valid
2280 * socket. But why? This function only translates a host byte order
2281 * u_short into a network byte order u_short...
2283 int WINAPI
WSAHtons(SOCKET s
, WS_u_short hostshort
, WS_u_short
*lpnetshort
)
2288 *lpnetshort
= htons(hostshort
);
2291 WSASetLastError(WSAEFAULT
);
2292 return SOCKET_ERROR
;
2296 /***********************************************************************
2297 * inet_addr (WS2_32.11)
2299 WS_u_long WINAPI
WS_inet_addr(const char *cp
)
2301 if (!cp
) return INADDR_NONE
;
2302 return inet_addr(cp
);
2306 /***********************************************************************
2309 WS_u_long WINAPI
WS_ntohl(WS_u_long netlong
)
2311 return ntohl(netlong
);
2315 /***********************************************************************
2318 WS_u_short WINAPI
WS_ntohs(WS_u_short netshort
)
2320 return ntohs(netshort
);
2324 /***********************************************************************
2325 * inet_ntoa (WS2_32.12)
2327 char* WINAPI
WS_inet_ntoa(struct WS_in_addr in
)
2329 /* use "buffer for dummies" here because some applications have a
2330 * propensity to decode addresses in ws_hostent structure without
2331 * saving them first...
2333 static char dbuffer
[16]; /* Yes, 16: 4*3 digits + 3 '.' + 1 '\0' */
2335 char* s
= inet_ntoa(*((struct in_addr
*)&in
));
2341 SetLastError(wsaErrno());
2345 /**********************************************************************
2346 * WSAIoctl (WS2_32.50)
2349 INT WINAPI
WSAIoctl(SOCKET s
,
2350 DWORD dwIoControlCode
,
2353 LPVOID lpbOutBuffer
,
2355 LPDWORD lpcbBytesReturned
,
2356 LPWSAOVERLAPPED lpOverlapped
,
2357 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2359 TRACE("%ld, 0x%08x, %p, %d, %p, %d, %p, %p, %p\n",
2360 s
, dwIoControlCode
, lpvInBuffer
, cbInBuffer
, lpbOutBuffer
,
2361 cbOutBuffer
, lpcbBytesReturned
, lpOverlapped
, lpCompletionRoutine
);
2363 switch( dwIoControlCode
)
2366 if (cbInBuffer
!= sizeof(WS_u_long
)) {
2367 WSASetLastError(WSAEFAULT
);
2368 return SOCKET_ERROR
;
2370 return WS_ioctlsocket( s
, WS_FIONBIO
, lpvInBuffer
);
2373 if (cbOutBuffer
!= sizeof(WS_u_long
)) {
2374 WSASetLastError(WSAEFAULT
);
2375 return SOCKET_ERROR
;
2377 return WS_ioctlsocket( s
, WS_FIONREAD
, lpbOutBuffer
);
2379 case WS_SIO_GET_INTERFACE_LIST
:
2381 INTERFACE_INFO
* intArray
= (INTERFACE_INFO
*)lpbOutBuffer
;
2382 DWORD size
, numInt
, apiReturn
;
2385 TRACE("-> SIO_GET_INTERFACE_LIST request\n");
2389 WSASetLastError(WSAEFAULT
);
2390 return SOCKET_ERROR
;
2392 if (!lpcbBytesReturned
)
2394 WSASetLastError(WSAEFAULT
);
2395 return SOCKET_ERROR
;
2398 fd
= get_sock_fd( s
, 0, NULL
);
2399 if (fd
== -1) return SOCKET_ERROR
;
2401 apiReturn
= GetAdaptersInfo(NULL
, &size
);
2402 if (apiReturn
== ERROR_NO_DATA
)
2406 else if (apiReturn
== ERROR_BUFFER_OVERFLOW
)
2408 PIP_ADAPTER_INFO table
= HeapAlloc(GetProcessHeap(),0,size
);
2412 if (GetAdaptersInfo(table
, &size
) == NO_ERROR
)
2414 PIP_ADAPTER_INFO ptr
;
2416 if (size
*sizeof(INTERFACE_INFO
)/sizeof(IP_ADAPTER_INFO
) > cbOutBuffer
)
2418 WARN("Buffer too small = %u, cbOutBuffer = %u\n", size
, cbOutBuffer
);
2419 HeapFree(GetProcessHeap(),0,table
);
2420 release_sock_fd( s
, fd
);
2421 WSASetLastError(WSAEFAULT
);
2422 return SOCKET_ERROR
;
2424 for (ptr
= table
, numInt
= 0; ptr
;
2425 ptr
= ptr
->Next
, intArray
++, numInt
++)
2427 unsigned int addr
, mask
, bcast
;
2428 struct ifreq ifInfo
;
2430 /* Socket Status Flags */
2431 lstrcpynA(ifInfo
.ifr_name
, ptr
->AdapterName
, IFNAMSIZ
);
2432 if (ioctl(fd
, SIOCGIFFLAGS
, &ifInfo
) < 0)
2434 ERR("Error obtaining status flags for socket!\n");
2435 HeapFree(GetProcessHeap(),0,table
);
2436 release_sock_fd( s
, fd
);
2437 WSASetLastError(WSAEINVAL
);
2438 return SOCKET_ERROR
;
2442 /* set flags; the values of IFF_* are not the same
2443 under Linux and Windows, therefore must generate
2445 intArray
->iiFlags
= 0;
2446 if (ifInfo
.ifr_flags
& IFF_BROADCAST
)
2447 intArray
->iiFlags
|= WS_IFF_BROADCAST
;
2448 #ifdef IFF_POINTOPOINT
2449 if (ifInfo
.ifr_flags
& IFF_POINTOPOINT
)
2450 intArray
->iiFlags
|= WS_IFF_POINTTOPOINT
;
2452 if (ifInfo
.ifr_flags
& IFF_LOOPBACK
)
2453 intArray
->iiFlags
|= WS_IFF_LOOPBACK
;
2454 if (ifInfo
.ifr_flags
& IFF_UP
)
2455 intArray
->iiFlags
|= WS_IFF_UP
;
2456 if (ifInfo
.ifr_flags
& IFF_MULTICAST
)
2457 intArray
->iiFlags
|= WS_IFF_MULTICAST
;
2460 addr
= inet_addr(ptr
->IpAddressList
.IpAddress
.String
);
2461 mask
= inet_addr(ptr
->IpAddressList
.IpMask
.String
);
2462 bcast
= addr
| ~mask
;
2463 intArray
->iiAddress
.AddressIn
.sin_family
= AF_INET
;
2464 intArray
->iiAddress
.AddressIn
.sin_port
= 0;
2465 intArray
->iiAddress
.AddressIn
.sin_addr
.WS_s_addr
=
2467 intArray
->iiNetmask
.AddressIn
.sin_family
= AF_INET
;
2468 intArray
->iiNetmask
.AddressIn
.sin_port
= 0;
2469 intArray
->iiNetmask
.AddressIn
.sin_addr
.WS_s_addr
=
2471 intArray
->iiBroadcastAddress
.AddressIn
.sin_family
=
2473 intArray
->iiBroadcastAddress
.AddressIn
.sin_port
= 0;
2474 intArray
->iiBroadcastAddress
.AddressIn
.sin_addr
.
2480 ERR("Unable to get interface table!\n");
2481 release_sock_fd( s
, fd
);
2482 HeapFree(GetProcessHeap(),0,table
);
2483 WSASetLastError(WSAEINVAL
);
2484 return SOCKET_ERROR
;
2486 HeapFree(GetProcessHeap(),0,table
);
2490 release_sock_fd( s
, fd
);
2491 WSASetLastError(WSAEINVAL
);
2492 return SOCKET_ERROR
;
2497 ERR("Unable to get interface table!\n");
2498 release_sock_fd( s
, fd
);
2499 WSASetLastError(WSAEINVAL
);
2500 return SOCKET_ERROR
;
2502 /* Calculate the size of the array being returned */
2503 *lpcbBytesReturned
= sizeof(INTERFACE_INFO
) * numInt
;
2504 release_sock_fd( s
, fd
);
2508 case WS_SIO_ADDRESS_LIST_CHANGE
:
2509 FIXME("-> SIO_ADDRESS_LIST_CHANGE request: stub\n");
2510 /* FIXME: error and return code depend on whether socket was created
2511 * with WSA_FLAG_OVERLAPPED, but there is no easy way to get this */
2514 case WS_SIO_ADDRESS_LIST_QUERY
:
2518 TRACE("-> SIO_ADDRESS_LIST_QUERY request\n");
2520 if (!lpcbBytesReturned
)
2522 WSASetLastError(WSAEFAULT
);
2523 return SOCKET_ERROR
;
2526 if (GetAdaptersInfo(NULL
, &size
) == ERROR_BUFFER_OVERFLOW
)
2528 IP_ADAPTER_INFO
*p
, *table
= HeapAlloc(GetProcessHeap(), 0, size
);
2531 if (!table
|| GetAdaptersInfo(table
, &size
))
2533 HeapFree(GetProcessHeap(), 0, table
);
2534 WSASetLastError(WSAEINVAL
);
2535 return SOCKET_ERROR
;
2538 for (p
= table
, num
= 0; p
; p
= p
->Next
)
2539 if (p
->IpAddressList
.IpAddress
.String
[0]) num
++;
2541 need
= sizeof(SOCKET_ADDRESS_LIST
) + sizeof(SOCKET_ADDRESS
) * (num
- 1);
2542 need
+= sizeof(SOCKADDR
) * num
;
2543 *lpcbBytesReturned
= need
;
2545 if (need
> cbOutBuffer
)
2547 HeapFree(GetProcessHeap(), 0, table
);
2548 WSASetLastError(WSAEFAULT
);
2549 return SOCKET_ERROR
;
2556 SOCKET_ADDRESS_LIST
*sa_list
= (SOCKET_ADDRESS_LIST
*)lpbOutBuffer
;
2557 SOCKADDR_IN
*sockaddr
;
2559 sa
= sa_list
->Address
;
2560 sockaddr
= (SOCKADDR_IN
*)((char *)sa
+ num
* sizeof(SOCKET_ADDRESS
));
2561 sa_list
->iAddressCount
= num
;
2563 for (p
= table
, i
= 0; p
; p
= p
->Next
)
2565 if (!p
->IpAddressList
.IpAddress
.String
[0]) continue;
2567 sa
[i
].lpSockaddr
= (SOCKADDR
*)&sockaddr
[i
];
2568 sa
[i
].iSockaddrLength
= sizeof(SOCKADDR
);
2570 sockaddr
[i
].sin_family
= AF_INET
;
2571 sockaddr
[i
].sin_port
= 0;
2572 sockaddr
[i
].sin_addr
.WS_s_addr
= inet_addr(p
->IpAddressList
.IpAddress
.String
);
2577 HeapFree(GetProcessHeap(), 0, table
);
2582 WARN("unable to get IP address list\n");
2583 WSASetLastError(WSAEINVAL
);
2584 return SOCKET_ERROR
;
2588 FIXME("SIO_FLUSH: stub.\n");
2591 case WS_SIO_GET_EXTENSION_FUNCTION_POINTER
:
2592 FIXME("SIO_GET_EXTENSION_FUNCTION_POINTER %s: stub\n", debugstr_guid(lpvInBuffer
));
2593 WSASetLastError(WSAEOPNOTSUPP
);
2594 return SOCKET_ERROR
;
2596 case WS_SIO_KEEPALIVE_VALS
:
2599 struct tcp_keepalive
*k
= lpvInBuffer
;
2600 int keepalive
= k
->onoff
? 1 : 0;
2601 int keepidle
= k
->keepalivetime
/ 1000;
2602 int keepintvl
= k
->keepaliveinterval
/ 1000;
2606 WSASetLastError(WSAEINVAL
);
2607 return SOCKET_ERROR
;
2610 TRACE("onoff: %d, keepalivetime: %d, keepaliveinterval: %d\n", keepalive
, keepidle
, keepintvl
);
2612 fd
= get_sock_fd(s
, 0, NULL
);
2613 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&keepalive
, sizeof(int)) == -1)
2615 release_sock_fd(s
, fd
);
2616 WSASetLastError(WSAEINVAL
);
2617 return SOCKET_ERROR
;
2619 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL)
2620 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, (void *)&keepidle
, sizeof(int)) == -1)
2622 release_sock_fd(s
, fd
);
2623 WSASetLastError(WSAEINVAL
);
2624 return SOCKET_ERROR
;
2626 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, (void *)&keepintvl
, sizeof(int)) == -1)
2628 release_sock_fd(s
, fd
);
2629 WSASetLastError(WSAEINVAL
);
2630 return SOCKET_ERROR
;
2633 FIXME("ignoring keepalive interval and timeout\n");
2636 release_sock_fd(s
, fd
);
2640 FIXME("unsupported WS_IOCTL cmd (%08x)\n", dwIoControlCode
);
2641 WSASetLastError(WSAEOPNOTSUPP
);
2642 return SOCKET_ERROR
;
2649 /***********************************************************************
2650 * ioctlsocket (WS2_32.10)
2652 int WINAPI
WS_ioctlsocket(SOCKET s
, LONG cmd
, WS_u_long
*argp
)
2657 TRACE("socket %04lx, cmd %08x, ptr %p\n", s
, cmd
, argp
);
2658 /* broken apps like defcon pass the argp value directly instead of a pointer to it */
2659 if(IS_INTRESOURCE(argp
))
2661 SetLastError(WSAEFAULT
);
2662 return SOCKET_ERROR
;
2672 if( _get_sock_mask(s
) )
2674 /* AsyncSelect()'ed sockets are always nonblocking */
2675 if (*argp
) return 0;
2676 SetLastError(WSAEINVAL
);
2677 return SOCKET_ERROR
;
2680 _enable_event(SOCKET2HANDLE(s
), 0, FD_WINE_NONBLOCKING
, 0);
2682 _enable_event(SOCKET2HANDLE(s
), 0, 0, FD_WINE_NONBLOCKING
);
2690 WARN("Warning: WS1.1 shouldn't be using async I/O\n");
2691 SetLastError(WSAEINVAL
);
2692 return SOCKET_ERROR
;
2694 case SIOCGIFBRDADDR
:
2695 case SIOCGIFNETMASK
:
2697 /* These don't need any special handling. They are used by
2698 WsControl, and are here to suppress an unnecessary warning. */
2702 /* Netscape tries hard to use bogus ioctl 0x667e */
2703 /* FIXME: 0x667e above is ('f' << 8) | 126, and is a low word of
2704 * FIONBIO (_IOW('f', 126, u_long)), how that should be handled?
2706 WARN("\tunknown WS_IOCTL cmd (%08x)\n", cmd
);
2710 fd
= get_sock_fd( s
, 0, NULL
);
2713 if( ioctl(fd
, newcmd
, (char*)argp
) == 0 )
2715 release_sock_fd( s
, fd
);
2718 SetLastError((errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno());
2719 release_sock_fd( s
, fd
);
2721 return SOCKET_ERROR
;
2724 /***********************************************************************
2725 * listen (WS2_32.13)
2727 int WINAPI
WS_listen(SOCKET s
, int backlog
)
2729 int fd
= get_sock_fd( s
, FILE_READ_DATA
, NULL
);
2731 TRACE("socket %04lx, backlog %d\n", s
, backlog
);
2734 if (listen(fd
, backlog
) == 0)
2736 release_sock_fd( s
, fd
);
2737 _enable_event(SOCKET2HANDLE(s
), FD_ACCEPT
,
2739 FD_CONNECT
|FD_WINE_CONNECTED
);
2742 SetLastError(wsaErrno());
2743 release_sock_fd( s
, fd
);
2745 return SOCKET_ERROR
;
2748 /***********************************************************************
2751 int WINAPI
WS_recv(SOCKET s
, char *buf
, int len
, int flags
)
2753 DWORD n
, dwFlags
= flags
;
2759 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, NULL
, NULL
, NULL
, NULL
) == SOCKET_ERROR
)
2760 return SOCKET_ERROR
;
2765 /***********************************************************************
2766 * recvfrom (WS2_32.17)
2768 int WINAPI
WS_recvfrom(SOCKET s
, char *buf
, INT len
, int flags
,
2769 struct WS_sockaddr
*from
, int *fromlen
)
2771 DWORD n
, dwFlags
= flags
;
2777 if ( WSARecvFrom(s
, &wsabuf
, 1, &n
, &dwFlags
, from
, fromlen
, NULL
, NULL
) == SOCKET_ERROR
)
2778 return SOCKET_ERROR
;
2783 /* allocate a poll array for the corresponding fd sets */
2784 static struct pollfd
*fd_sets_to_poll( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2785 const WS_fd_set
*exceptfds
, int *count_ptr
)
2787 unsigned int i
, j
= 0, count
= 0;
2790 if (readfds
) count
+= readfds
->fd_count
;
2791 if (writefds
) count
+= writefds
->fd_count
;
2792 if (exceptfds
) count
+= exceptfds
->fd_count
;
2794 if (!count
) return NULL
;
2795 if (!(fds
= HeapAlloc( GetProcessHeap(), 0, count
* sizeof(fds
[0])))) return NULL
;
2797 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2799 fds
[j
].fd
= get_sock_fd( readfds
->fd_array
[i
], FILE_READ_DATA
, NULL
);
2800 fds
[j
].events
= POLLIN
;
2804 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2806 fds
[j
].fd
= get_sock_fd( writefds
->fd_array
[i
], FILE_WRITE_DATA
, NULL
);
2807 fds
[j
].events
= POLLOUT
;
2811 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2813 fds
[j
].fd
= get_sock_fd( exceptfds
->fd_array
[i
], 0, NULL
);
2814 fds
[j
].events
= POLLHUP
;
2820 /* release the file descriptor obtained in fd_sets_to_poll */
2821 /* must be called with the original fd_set arrays, before calling get_poll_results */
2822 static void release_poll_fds( const WS_fd_set
*readfds
, const WS_fd_set
*writefds
,
2823 const WS_fd_set
*exceptfds
, struct pollfd
*fds
)
2825 unsigned int i
, j
= 0;
2829 for (i
= 0; i
< readfds
->fd_count
; i
++, j
++)
2830 if (fds
[j
].fd
!= -1) release_sock_fd( readfds
->fd_array
[i
], fds
[j
].fd
);
2834 for (i
= 0; i
< writefds
->fd_count
; i
++, j
++)
2835 if (fds
[j
].fd
!= -1) release_sock_fd( writefds
->fd_array
[i
], fds
[j
].fd
);
2839 for (i
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2840 if (fds
[j
].fd
!= -1)
2842 /* make sure we have a real error before releasing the fd */
2843 if (!sock_error_p( fds
[j
].fd
)) fds
[j
].revents
= 0;
2844 release_sock_fd( exceptfds
->fd_array
[i
], fds
[j
].fd
);
2849 /* map the poll results back into the Windows fd sets */
2850 static int get_poll_results( WS_fd_set
*readfds
, WS_fd_set
*writefds
, WS_fd_set
*exceptfds
,
2851 const struct pollfd
*fds
)
2853 unsigned int i
, j
= 0, k
, total
= 0;
2857 for (i
= k
= 0; i
< readfds
->fd_count
; i
++, j
++)
2858 if (fds
[j
].revents
) readfds
->fd_array
[k
++] = readfds
->fd_array
[i
];
2859 readfds
->fd_count
= k
;
2864 for (i
= k
= 0; i
< writefds
->fd_count
; i
++, j
++)
2865 if (fds
[j
].revents
) writefds
->fd_array
[k
++] = writefds
->fd_array
[i
];
2866 writefds
->fd_count
= k
;
2871 for (i
= k
= 0; i
< exceptfds
->fd_count
; i
++, j
++)
2872 if (fds
[j
].revents
) exceptfds
->fd_array
[k
++] = exceptfds
->fd_array
[i
];
2873 exceptfds
->fd_count
= k
;
2880 /***********************************************************************
2881 * select (WS2_32.18)
2883 int WINAPI
WS_select(int nfds
, WS_fd_set
*ws_readfds
,
2884 WS_fd_set
*ws_writefds
, WS_fd_set
*ws_exceptfds
,
2885 const struct WS_timeval
* ws_timeout
)
2887 struct pollfd
*pollfds
;
2888 struct timeval tv1
, tv2
;
2890 int count
, ret
, timeout
= -1;
2892 TRACE("read %p, write %p, excp %p timeout %p\n",
2893 ws_readfds
, ws_writefds
, ws_exceptfds
, ws_timeout
);
2895 if (!(pollfds
= fd_sets_to_poll( ws_readfds
, ws_writefds
, ws_exceptfds
, &count
)) && count
)
2897 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
2898 return SOCKET_ERROR
;
2903 torig
= (ws_timeout
->tv_sec
* 1000) + (ws_timeout
->tv_usec
+ 999) / 1000;
2905 gettimeofday( &tv1
, 0 );
2908 while ((ret
= poll( pollfds
, count
, timeout
)) < 0)
2912 if (!ws_timeout
) continue;
2913 gettimeofday( &tv2
, 0 );
2915 tv2
.tv_sec
-= tv1
.tv_sec
;
2916 tv2
.tv_usec
-= tv1
.tv_usec
;
2917 if (tv2
.tv_usec
< 0)
2919 tv2
.tv_usec
+= 1000000;
2923 timeout
= torig
- (tv2
.tv_sec
* 1000) - (tv2
.tv_usec
+ 999) / 1000;
2924 if (timeout
<= 0) break;
2927 release_poll_fds( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2929 if (ret
== -1) SetLastError(wsaErrno());
2930 else ret
= get_poll_results( ws_readfds
, ws_writefds
, ws_exceptfds
, pollfds
);
2931 HeapFree( GetProcessHeap(), 0, pollfds
);
2935 /* helper to send completion messages for client-only i/o operation case */
2936 static void WS_AddCompletion( SOCKET sock
, ULONG_PTR CompletionValue
, NTSTATUS CompletionStatus
,
2941 SERVER_START_REQ( add_fd_completion
)
2943 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(sock
) );
2944 req
->cvalue
= CompletionValue
;
2945 req
->status
= CompletionStatus
;
2946 req
->information
= Information
;
2947 status
= wine_server_call( req
);
2953 /***********************************************************************
2956 int WINAPI
WS_send(SOCKET s
, const char *buf
, int len
, int flags
)
2962 wsabuf
.buf
= (char*) buf
;
2964 if ( WSASendTo( s
, &wsabuf
, 1, &n
, flags
, NULL
, 0, NULL
, NULL
) == SOCKET_ERROR
)
2965 return SOCKET_ERROR
;
2970 /***********************************************************************
2971 * WSASend (WS2_32.72)
2973 INT WINAPI
WSASend( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2974 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2975 LPWSAOVERLAPPED lpOverlapped
,
2976 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
2978 return WSASendTo( s
, lpBuffers
, dwBufferCount
, lpNumberOfBytesSent
, dwFlags
,
2979 NULL
, 0, lpOverlapped
, lpCompletionRoutine
);
2982 /***********************************************************************
2983 * WSASendDisconnect (WS2_32.73)
2985 INT WINAPI
WSASendDisconnect( SOCKET s
, LPWSABUF lpBuffers
)
2987 return WS_shutdown( s
, SD_SEND
);
2991 /***********************************************************************
2992 * WSASendTo (WS2_32.74)
2994 INT WINAPI
WSASendTo( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
2995 LPDWORD lpNumberOfBytesSent
, DWORD dwFlags
,
2996 const struct WS_sockaddr
*to
, int tolen
,
2997 LPWSAOVERLAPPED lpOverlapped
,
2998 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
3000 unsigned int i
, options
;
3002 struct ws2_async
*wsa
;
3003 int totalLength
= 0;
3004 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
3006 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, to %p, tolen %d, ovl %p, func %p\n",
3007 s
, lpBuffers
, dwBufferCount
, dwFlags
,
3008 to
, tolen
, lpOverlapped
, lpCompletionRoutine
);
3010 fd
= get_sock_fd( s
, FILE_WRITE_DATA
, &options
);
3011 TRACE( "fd=%d, options=%x\n", fd
, options
);
3013 if ( fd
== -1 ) return SOCKET_ERROR
;
3015 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
3021 wsa
->hSocket
= SOCKET2HANDLE(s
);
3022 wsa
->addr
= (struct WS_sockaddr
*)to
;
3023 wsa
->addrlen
.val
= tolen
;
3024 wsa
->flags
= dwFlags
;
3025 wsa
->n_iovecs
= dwBufferCount
;
3026 wsa
->first_iovec
= 0;
3027 for ( i
= 0; i
< dwBufferCount
; i
++ )
3029 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
3030 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
3031 totalLength
+= lpBuffers
[i
].len
;
3034 if (!lpNumberOfBytesSent
)
3042 n
= WS2_send( fd
, wsa
);
3043 if (n
!= -1 || errno
!= EINTR
) break;
3045 if (n
== -1 && errno
!= EAGAIN
)
3048 if (cvalue
) WS_AddCompletion( s
, cvalue
, err
, 0 );
3052 if ((lpOverlapped
|| lpCompletionRoutine
) &&
3053 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3055 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
3057 wsa
->user_overlapped
= lpOverlapped
;
3058 wsa
->completion_func
= lpCompletionRoutine
;
3059 release_sock_fd( s
, fd
);
3063 iosb
->u
.Status
= STATUS_PENDING
;
3064 iosb
->Information
= 0;
3066 SERVER_START_REQ( register_async
)
3068 req
->type
= ASYNC_TYPE_WRITE
;
3069 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
3070 req
->async
.callback
= wine_server_client_ptr( WS2_async_send
);
3071 req
->async
.iosb
= wine_server_client_ptr( iosb
);
3072 req
->async
.arg
= wine_server_client_ptr( wsa
);
3073 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
3074 req
->async
.cvalue
= cvalue
;
3075 err
= wine_server_call( req
);
3079 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
3080 WSASetLastError( NtStatusToWSAError( err
));
3081 return SOCKET_ERROR
;
3084 iosb
->u
.Status
= STATUS_SUCCESS
;
3085 iosb
->Information
= n
;
3086 *lpNumberOfBytesSent
= n
;
3087 if (!wsa
->completion_func
)
3089 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
3090 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
3091 HeapFree( GetProcessHeap(), 0, wsa
);
3093 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
3094 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
3099 if ( _is_blocking(s
) )
3101 /* On a blocking non-overlapped stream socket,
3102 * sending blocks until the entire buffer is sent. */
3103 DWORD timeout_start
= GetTickCount();
3105 *lpNumberOfBytesSent
= 0;
3107 while (wsa
->first_iovec
< dwBufferCount
)
3110 int timeout
= GET_SNDTIMEO(fd
);
3114 *lpNumberOfBytesSent
+= n
;
3115 while (wsa
->first_iovec
< dwBufferCount
&& wsa
->iovec
[wsa
->first_iovec
].iov_len
<= n
)
3116 n
-= wsa
->iovec
[wsa
->first_iovec
++].iov_len
;
3117 if (wsa
->first_iovec
>= dwBufferCount
) break;
3118 wsa
->iovec
[wsa
->first_iovec
].iov_base
= (char*)wsa
->iovec
[wsa
->first_iovec
].iov_base
+ n
;
3119 wsa
->iovec
[wsa
->first_iovec
].iov_len
-= n
;
3124 timeout
-= GetTickCount() - timeout_start
;
3125 if (timeout
< 0) timeout
= 0;
3129 pfd
.events
= POLLOUT
;
3131 if (!timeout
|| !poll( &pfd
, 1, timeout
))
3134 goto error
; /* msdn says a timeout in send is fatal */
3137 n
= WS2_send( fd
, wsa
);
3138 if (n
== -1 && errno
!= EAGAIN
&& errno
!= EINTR
)
3145 else /* non-blocking */
3147 if (n
< totalLength
)
3148 _enable_event(SOCKET2HANDLE(s
), FD_WRITE
, 0, 0);
3151 err
= WSAEWOULDBLOCK
;
3154 *lpNumberOfBytesSent
= n
;
3157 TRACE(" -> %i bytes\n", *lpNumberOfBytesSent
);
3159 HeapFree( GetProcessHeap(), 0, wsa
);
3160 release_sock_fd( s
, fd
);
3165 HeapFree( GetProcessHeap(), 0, wsa
);
3166 release_sock_fd( s
, fd
);
3167 WARN(" -> ERROR %d\n", err
);
3168 WSASetLastError(err
);
3169 return SOCKET_ERROR
;
3172 /***********************************************************************
3173 * sendto (WS2_32.20)
3175 int WINAPI
WS_sendto(SOCKET s
, const char *buf
, int len
, int flags
,
3176 const struct WS_sockaddr
*to
, int tolen
)
3182 wsabuf
.buf
= (char*) buf
;
3184 if ( WSASendTo(s
, &wsabuf
, 1, &n
, flags
, to
, tolen
, NULL
, NULL
) == SOCKET_ERROR
)
3185 return SOCKET_ERROR
;
3190 /***********************************************************************
3191 * setsockopt (WS2_32.21)
3193 int WINAPI
WS_setsockopt(SOCKET s
, int level
, int optname
,
3194 const char *optval
, int optlen
)
3198 struct linger linger
;
3199 struct timeval tval
;
3201 TRACE("socket: %04lx, level 0x%x, name 0x%x, ptr %p, len %d\n",
3202 s
, level
, optname
, optval
, optlen
);
3204 /* some broken apps pass the value directly instead of a pointer to it */
3205 if(IS_INTRESOURCE(optval
))
3207 SetLastError(WSAEFAULT
);
3208 return SOCKET_ERROR
;
3216 /* Some options need some conversion before they can be sent to
3217 * setsockopt. The conversions are done here, then they will fall though
3218 * to the general case. Special options that are not passed to
3219 * setsockopt follow below that.*/
3221 case WS_SO_DONTLINGER
:
3222 linger
.l_onoff
= *((const int*)optval
) ? 0: 1;
3223 linger
.l_linger
= 0;
3225 optname
= SO_LINGER
;
3226 optval
= (char*)&linger
;
3227 optlen
= sizeof(struct linger
);
3231 linger
.l_onoff
= ((LINGER
*)optval
)->l_onoff
;
3232 linger
.l_linger
= ((LINGER
*)optval
)->l_linger
;
3233 /* FIXME: what is documented behavior if SO_LINGER optval
3236 optname
= SO_LINGER
;
3237 optval
= (char*)&linger
;
3238 optlen
= sizeof(struct linger
);
3242 if (*(const int*)optval
< 2048)
3244 WARN("SO_RCVBF for %d bytes is too small: ignored\n", *(const int*)optval
);
3249 /* The options listed here don't need any special handling. Thanks to
3250 * the conversion happening above, options from there will fall through
3252 case WS_SO_ACCEPTCONN
:
3253 case WS_SO_BROADCAST
:
3255 case WS_SO_KEEPALIVE
:
3256 case WS_SO_OOBINLINE
:
3257 /* BSD socket SO_REUSEADDR is not 100% compatible to winsock semantics.
3258 * however, using it the BSD way fixes bug 8513 and seems to be what
3259 * most programmers assume, anyway */
3260 case WS_SO_REUSEADDR
:
3263 convert_sockopt(&level
, &optname
);
3266 /* SO_DEBUG is a privileged operation, ignore it. */
3268 TRACE("Ignoring SO_DEBUG\n");
3271 /* For some reason the game GrandPrixLegends does set SO_DONTROUTE on its
3272 * socket. According to MSDN, this option is silently ignored.*/
3273 case WS_SO_DONTROUTE
:
3274 TRACE("Ignoring SO_DONTROUTE\n");
3277 /* Stops two sockets from being bound to the same port. Always happens
3278 * on unix systems, so just drop it. */
3279 case WS_SO_EXCLUSIVEADDRUSE
:
3280 TRACE("Ignoring SO_EXCLUSIVEADDRUSE, is always set.\n");
3283 /* SO_OPENTYPE does not require a valid socket handle. */
3284 case WS_SO_OPENTYPE
:
3285 if (!optlen
|| optlen
< sizeof(int) || !optval
)
3287 SetLastError(WSAEFAULT
);
3288 return SOCKET_ERROR
;
3290 get_per_thread_data()->opentype
= *(const int *)optval
;
3291 TRACE("setting global SO_OPENTYPE = 0x%x\n", *((int*)optval
) );
3295 case WS_SO_RCVTIMEO
:
3298 case WS_SO_SNDTIMEO
:
3300 #if defined(SO_RCVTIMEO) || defined(SO_SNDTIMEO)
3301 if (optval
&& optlen
== sizeof(UINT32
)) {
3302 /* WinSock passes milliseconds instead of struct timeval */
3303 tval
.tv_usec
= (*(const UINT32
*)optval
% 1000) * 1000;
3304 tval
.tv_sec
= *(const UINT32
*)optval
/ 1000;
3305 /* min of 500 milliseconds */
3306 if (tval
.tv_sec
== 0 && tval
.tv_usec
< 500000)
3307 tval
.tv_usec
= 500000;
3308 optlen
= sizeof(struct timeval
);
3309 optval
= (char*)&tval
;
3310 } else if (optlen
== sizeof(struct timeval
)) {
3311 WARN("SO_SND/RCVTIMEO for %d bytes: assuming unixism\n", optlen
);
3313 WARN("SO_SND/RCVTIMEO for %d bytes is weird: ignored\n", optlen
);
3316 convert_sockopt(&level
, &optname
);
3321 TRACE("Unknown SOL_SOCKET optname: 0x%08x\n", optname
);
3322 SetLastError(WSAENOPROTOOPT
);
3323 return SOCKET_ERROR
;
3325 break; /* case WS_SOL_SOCKET */
3332 fd
= get_sock_fd( s
, 0, NULL
);
3333 TRACE("trying to set IPX_PTYPE: %d (fd: %d)\n", *(const int*)optval
, fd
);
3335 /* We try to set the ipx type on ipx socket level. */
3337 if(setsockopt(fd
, SOL_IPX
, IPX_TYPE
, optval
, optlen
) == -1)
3339 ERR("IPX: could not set ipx option type; expect weird behaviour\n");
3340 release_sock_fd( s
, fd
);
3341 return SOCKET_ERROR
;
3346 /* Should we retrieve val using a getsockopt call and then
3347 * set the modified one? */
3348 val
.ipx_pt
= *optval
;
3349 setsockopt(fd
, 0, SO_DEFAULT_HEADERS
, &val
, sizeof(struct ipx
));
3352 release_sock_fd( s
, fd
);
3355 case IPX_FILTERPTYPE
:
3356 /* Sets the receive filter packet type, at the moment we don't support it */
3357 FIXME("IPX_FILTERPTYPE: %x\n", *optval
);
3358 /* Returning 0 is better for now than returning a SOCKET_ERROR */
3362 FIXME("opt_name:%x\n", optname
);
3363 return SOCKET_ERROR
;
3365 break; /* case NSPROTO_IPX */
3368 /* Levels WS_IPPROTO_TCP and WS_IPPROTO_IP convert directly */
3369 case WS_IPPROTO_TCP
:
3372 case WS_TCP_NODELAY
:
3373 convert_sockopt(&level
, &optname
);
3376 FIXME("Unknown IPPROTO_TCP optname 0x%08x\n", optname
);
3377 return SOCKET_ERROR
;
3384 case WS_IP_ADD_MEMBERSHIP
:
3385 case WS_IP_DROP_MEMBERSHIP
:
3389 case WS_IP_MULTICAST_IF
:
3390 case WS_IP_MULTICAST_LOOP
:
3391 case WS_IP_MULTICAST_TTL
:
3395 convert_sockopt(&level
, &optname
);
3397 case WS_IP_DONTFRAGMENT
:
3398 FIXME("IP_DONTFRAGMENT is silently ignored!\n");
3401 FIXME("Unknown IPPROTO_IP optname 0x%08x\n", optname
);
3402 return SOCKET_ERROR
;
3407 WARN("Unknown level: 0x%08x\n", level
);
3408 SetLastError(WSAEINVAL
);
3409 return SOCKET_ERROR
;
3410 } /* end switch(level) */
3412 /* avoid endianness issues if argument is a 16-bit int */
3413 if (optval
&& optlen
< sizeof(int))
3415 woptval
= *((const INT16
*) optval
);
3416 optval
= (char*) &woptval
;
3419 fd
= get_sock_fd( s
, 0, NULL
);
3420 if (fd
== -1) return SOCKET_ERROR
;
3422 if (setsockopt(fd
, level
, optname
, optval
, optlen
) == 0)
3424 release_sock_fd( s
, fd
);
3427 TRACE("Setting socket error, %d\n", wsaErrno());
3428 SetLastError(wsaErrno());
3429 release_sock_fd( s
, fd
);
3431 return SOCKET_ERROR
;
3434 /***********************************************************************
3435 * shutdown (WS2_32.22)
3437 int WINAPI
WS_shutdown(SOCKET s
, int how
)
3439 int fd
, err
= WSAENOTSOCK
;
3440 unsigned int options
, clear_flags
= 0;
3442 fd
= get_sock_fd( s
, 0, &options
);
3443 TRACE("socket %04lx, how %i %x\n", s
, how
, options
);
3446 return SOCKET_ERROR
;
3450 case 0: /* drop receives */
3451 clear_flags
|= FD_READ
;
3453 case 1: /* drop sends */
3454 clear_flags
|= FD_WRITE
;
3456 case 2: /* drop all */
3457 clear_flags
|= FD_READ
|FD_WRITE
;
3459 clear_flags
|= FD_WINE_LISTENING
;
3462 if (!(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
3467 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3470 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3474 err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_READ
);
3475 if (!err
) err
= WS2_register_async_shutdown( s
, ASYNC_TYPE_WRITE
);
3478 if (err
) goto error
;
3480 else /* non-overlapped mode */
3482 if ( shutdown( fd
, how
) )
3489 release_sock_fd( s
, fd
);
3490 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3491 if ( how
> 1) WSAAsyncSelect( s
, 0, 0, 0 );
3495 release_sock_fd( s
, fd
);
3496 _enable_event( SOCKET2HANDLE(s
), 0, 0, clear_flags
);
3497 WSASetLastError( err
);
3498 return SOCKET_ERROR
;
3501 /***********************************************************************
3502 * socket (WS2_32.23)
3504 SOCKET WINAPI
WS_socket(int af
, int type
, int protocol
)
3506 TRACE("af=%d type=%d protocol=%d\n", af
, type
, protocol
);
3508 return WSASocketA( af
, type
, protocol
, NULL
, 0,
3509 get_per_thread_data()->opentype
? 0 : WSA_FLAG_OVERLAPPED
);
3513 /***********************************************************************
3514 * gethostbyaddr (WS2_32.51)
3516 struct WS_hostent
* WINAPI
WS_gethostbyaddr(const char *addr
, int len
, int type
)
3518 struct WS_hostent
*retval
= NULL
;
3519 struct hostent
* host
;
3521 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3524 struct hostent hostentry
;
3527 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3529 int res
= gethostbyaddr_r(addr
, len
, type
,
3530 &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3531 if( res
!= ERANGE
) break;
3533 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3535 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3537 EnterCriticalSection( &csWSgetXXXbyYYY
);
3538 host
= gethostbyaddr(addr
, len
, type
);
3539 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3541 if( host
!= NULL
) retval
= WS_dup_he(host
);
3542 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3543 HeapFree(GetProcessHeap(),0,extrabuf
);
3545 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3547 TRACE("ptr %p, len %d, type %d ret %p\n", addr
, len
, type
, retval
);
3551 /***********************************************************************
3552 * WS_get_local_ips (INTERNAL)
3554 * Returns the list of local IP addresses by going through the network
3555 * adapters and using the local routing table to sort the addresses
3556 * from highest routing priority to lowest routing priority. This
3557 * functionality is inferred from the description for obtaining local
3558 * IP addresses given in the Knowledge Base Article Q160215.
3560 * Please note that the returned hostent is only freed when the thread
3561 * closes and is replaced if another hostent is requested.
3563 static struct WS_hostent
* WS_get_local_ips( char *hostname
)
3565 int last_metric
, numroutes
= 0, i
, j
;
3566 PIP_ADAPTER_INFO adapters
= NULL
, k
;
3567 struct WS_hostent
*hostlist
= NULL
;
3568 PMIB_IPFORWARDTABLE routes
= NULL
;
3569 struct route
*route_addrs
= NULL
;
3570 DWORD adap_size
, route_size
;
3572 /* Obtain the size of the adapter list and routing table, also allocate memory */
3573 if (GetAdaptersInfo(NULL
, &adap_size
) != ERROR_BUFFER_OVERFLOW
)
3575 if (GetIpForwardTable(NULL
, &route_size
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
)
3577 adapters
= HeapAlloc(GetProcessHeap(), 0, adap_size
);
3578 routes
= HeapAlloc(GetProcessHeap(), 0, route_size
);
3579 route_addrs
= HeapAlloc(GetProcessHeap(), 0, 0); /* HeapReAlloc doesn't work on NULL */
3580 if (adapters
== NULL
|| routes
== NULL
|| route_addrs
== NULL
)
3582 /* Obtain the adapter list and the full routing table */
3583 if (GetAdaptersInfo(adapters
, &adap_size
) != NO_ERROR
)
3585 if (GetIpForwardTable(routes
, &route_size
, FALSE
) != NO_ERROR
)
3587 /* Store the interface associated with each route */
3588 for (i
= 0; i
< routes
->dwNumEntries
; i
++)
3590 DWORD ifindex
, ifmetric
, exists
= FALSE
;
3592 if (routes
->table
[i
].dwForwardType
!= MIB_IPROUTE_TYPE_DIRECT
)
3594 ifindex
= routes
->table
[i
].dwForwardIfIndex
;
3595 ifmetric
= routes
->table
[i
].dwForwardMetric1
;
3596 /* Only store the lowest valued metric for an interface */
3597 for (j
= 0; j
< numroutes
; j
++)
3599 if (route_addrs
[j
].interface
== ifindex
)
3601 if (route_addrs
[j
].metric
> ifmetric
)
3602 route_addrs
[j
].metric
= ifmetric
;
3608 route_addrs
= HeapReAlloc(GetProcessHeap(), 0, route_addrs
, (numroutes
+1)*sizeof(struct route
));
3609 if (route_addrs
== NULL
)
3610 goto cleanup
; /* Memory allocation error, fail gracefully */
3611 route_addrs
[numroutes
].interface
= ifindex
;
3612 route_addrs
[numroutes
].metric
= ifmetric
;
3613 /* If no IP is found in the next step (for whatever reason)
3614 * then fall back to the magic loopback address.
3616 memcpy(&(route_addrs
[numroutes
].addr
.s_addr
), magic_loopback_addr
, 4);
3620 goto cleanup
; /* No routes, fall back to the Magic IP */
3621 /* Find the IP address associated with each found interface */
3622 for (i
= 0; i
< numroutes
; i
++)
3624 for (k
= adapters
; k
!= NULL
; k
= k
->Next
)
3626 char *ip
= k
->IpAddressList
.IpAddress
.String
;
3628 if (route_addrs
[i
].interface
== k
->Index
)
3629 route_addrs
[i
].addr
.s_addr
= (in_addr_t
) inet_addr(ip
);
3632 /* Allocate a hostent and enough memory for all the IPs,
3633 * including the NULL at the end of the list.
3635 hostlist
= WS_create_he(hostname
, 1, numroutes
+1, TRUE
);
3636 if (hostlist
== NULL
)
3637 goto cleanup
; /* Failed to allocate a hostent for the list of IPs */
3638 hostlist
->h_addr_list
[numroutes
] = NULL
; /* NULL-terminate the address list */
3639 hostlist
->h_aliases
[0] = NULL
; /* NULL-terminate the alias list */
3640 hostlist
->h_addrtype
= AF_INET
;
3641 hostlist
->h_length
= sizeof(struct in_addr
); /* = 4 */
3642 /* Reorder the entries when placing them in the host list, Windows expects
3643 * the IP list in order from highest priority to lowest (the critical thing
3644 * is that most applications expect the first IP to be the default route).
3647 for (i
= 0; i
< numroutes
; i
++)
3649 struct in_addr addr
;
3650 int metric
= 0xFFFF;
3652 memcpy(&addr
, magic_loopback_addr
, 4);
3653 for (j
= 0; j
< numroutes
; j
++)
3655 int this_metric
= route_addrs
[j
].metric
;
3657 if (this_metric
> last_metric
&& this_metric
< metric
)
3659 addr
= route_addrs
[j
].addr
;
3660 metric
= this_metric
;
3663 last_metric
= metric
;
3664 (*(struct in_addr
*) hostlist
->h_addr_list
[i
]) = addr
;
3667 /* Cleanup all allocated memory except the address list,
3668 * the address list is used by the calling app.
3671 HeapFree(GetProcessHeap(), 0, route_addrs
);
3672 HeapFree(GetProcessHeap(), 0, adapters
);
3673 HeapFree(GetProcessHeap(), 0, routes
);
3677 /***********************************************************************
3678 * gethostbyname (WS2_32.52)
3680 struct WS_hostent
* WINAPI
WS_gethostbyname(const char* name
)
3682 struct WS_hostent
*retval
= NULL
;
3683 struct hostent
* host
;
3684 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3687 struct hostent hostentry
;
3688 int locerr
= ENOBUFS
;
3691 if( gethostname( hostname
, 100) == -1) {
3692 SetLastError( WSAENOBUFS
); /* appropriate ? */
3695 if( !name
|| !name
[0]) {
3698 /* If the hostname of the local machine is requested then return the
3699 * complete list of local IP addresses */
3700 if(strcmp(name
, hostname
) == 0)
3701 retval
= WS_get_local_ips(hostname
);
3702 /* If any other hostname was requested (or the routing table lookup failed)
3703 * then return the IP found by the host OS */
3706 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3708 extrabuf
=HeapAlloc(GetProcessHeap(),0,ebufsize
) ;
3710 int res
= gethostbyname_r(name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
3711 if( res
!= ERANGE
) break;
3713 extrabuf
=HeapReAlloc(GetProcessHeap(),0,extrabuf
,ebufsize
) ;
3715 if (!host
) SetLastError((locerr
< 0) ? wsaErrno() : wsaHerrno(locerr
));
3717 EnterCriticalSection( &csWSgetXXXbyYYY
);
3718 host
= gethostbyname(name
);
3719 if (!host
) SetLastError((h_errno
< 0) ? wsaErrno() : wsaHerrno(h_errno
));
3721 if (host
) retval
= WS_dup_he(host
);
3722 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
3723 HeapFree(GetProcessHeap(),0,extrabuf
);
3725 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3728 if (retval
&& retval
->h_addr_list
[0][0] == 127 &&
3729 strcmp(name
, "localhost") != 0)
3731 /* hostname != "localhost" but has loopback address. replace by our
3732 * special address.*/
3733 memcpy(retval
->h_addr_list
[0], magic_loopback_addr
, 4);
3735 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3740 /***********************************************************************
3741 * getprotobyname (WS2_32.53)
3743 struct WS_protoent
* WINAPI
WS_getprotobyname(const char* name
)
3745 struct WS_protoent
* retval
= NULL
;
3746 #ifdef HAVE_GETPROTOBYNAME
3747 struct protoent
* proto
;
3748 EnterCriticalSection( &csWSgetXXXbyYYY
);
3749 if( (proto
= getprotobyname(name
)) != NULL
)
3751 retval
= WS_dup_pe(proto
);
3754 MESSAGE("protocol %s not found; You might want to add "
3755 "this to /etc/protocols\n", debugstr_a(name
) );
3756 SetLastError(WSANO_DATA
);
3758 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3760 TRACE( "%s ret %p\n", debugstr_a(name
), retval
);
3765 /***********************************************************************
3766 * getprotobynumber (WS2_32.54)
3768 struct WS_protoent
* WINAPI
WS_getprotobynumber(int number
)
3770 struct WS_protoent
* retval
= NULL
;
3771 #ifdef HAVE_GETPROTOBYNUMBER
3772 struct protoent
* proto
;
3773 EnterCriticalSection( &csWSgetXXXbyYYY
);
3774 if( (proto
= getprotobynumber(number
)) != NULL
)
3776 retval
= WS_dup_pe(proto
);
3779 MESSAGE("protocol number %d not found; You might want to add "
3780 "this to /etc/protocols\n", number
);
3781 SetLastError(WSANO_DATA
);
3783 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3785 TRACE("%i ret %p\n", number
, retval
);
3790 /***********************************************************************
3791 * getservbyname (WS2_32.55)
3793 struct WS_servent
* WINAPI
WS_getservbyname(const char *name
, const char *proto
)
3795 struct WS_servent
* retval
= NULL
;
3796 struct servent
* serv
;
3798 char *proto_str
= NULL
;
3800 if (!(name_str
= strdup_lower(name
))) return NULL
;
3802 if (proto
&& *proto
)
3804 if (!(proto_str
= strdup_lower(proto
)))
3806 HeapFree( GetProcessHeap(), 0, name_str
);
3811 EnterCriticalSection( &csWSgetXXXbyYYY
);
3812 serv
= getservbyname(name_str
, proto_str
);
3815 retval
= WS_dup_se(serv
);
3817 else SetLastError(WSANO_DATA
);
3818 LeaveCriticalSection( &csWSgetXXXbyYYY
);
3819 HeapFree( GetProcessHeap(), 0, proto_str
);
3820 HeapFree( GetProcessHeap(), 0, name_str
);
3821 TRACE( "%s, %s ret %p\n", debugstr_a(name
), debugstr_a(proto
), retval
);
3825 /***********************************************************************
3826 * freeaddrinfo (WS2_32.@)
3828 void WINAPI
WS_freeaddrinfo(struct WS_addrinfo
*res
)
3831 struct WS_addrinfo
*next
;
3833 HeapFree(GetProcessHeap(),0,res
->ai_canonname
);
3834 HeapFree(GetProcessHeap(),0,res
->ai_addr
);
3835 next
= res
->ai_next
;
3836 HeapFree(GetProcessHeap(),0,res
);
3841 /* helper functions for getaddrinfo()/getnameinfo() */
3842 static int convert_aiflag_w2u(int winflags
) {
3846 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3847 if (ws_aiflag_map
[i
][0] & winflags
) {
3848 unixflags
|= ws_aiflag_map
[i
][1];
3849 winflags
&= ~ws_aiflag_map
[i
][0];
3852 FIXME("Unhandled windows AI_xxx flags %x\n", winflags
);
3856 static int convert_niflag_w2u(int winflags
) {
3860 for (i
=0;i
<sizeof(ws_niflag_map
)/sizeof(ws_niflag_map
[0]);i
++)
3861 if (ws_niflag_map
[i
][0] & winflags
) {
3862 unixflags
|= ws_niflag_map
[i
][1];
3863 winflags
&= ~ws_niflag_map
[i
][0];
3866 FIXME("Unhandled windows NI_xxx flags %x\n", winflags
);
3870 static int convert_aiflag_u2w(int unixflags
) {
3874 for (i
=0;i
<sizeof(ws_aiflag_map
)/sizeof(ws_aiflag_map
[0]);i
++)
3875 if (ws_aiflag_map
[i
][1] & unixflags
) {
3876 winflags
|= ws_aiflag_map
[i
][0];
3877 unixflags
&= ~ws_aiflag_map
[i
][1];
3879 if (unixflags
) /* will warn usually */
3880 WARN("Unhandled UNIX AI_xxx flags %x\n", unixflags
);
3884 static int convert_eai_u2w(int unixret
) {
3887 for (i
=0;ws_eai_map
[i
][0];i
++)
3888 if (ws_eai_map
[i
][1] == unixret
)
3889 return ws_eai_map
[i
][0];
3893 /***********************************************************************
3894 * getaddrinfo (WS2_32.@)
3896 int WINAPI
WS_getaddrinfo(LPCSTR nodename
, LPCSTR servname
, const struct WS_addrinfo
*hints
, struct WS_addrinfo
**res
)
3898 #ifdef HAVE_GETADDRINFO
3899 struct addrinfo
*unixaires
= NULL
;
3901 struct addrinfo unixhints
, *punixhints
= NULL
;
3902 CHAR
*node
= NULL
, *serv
= NULL
;
3905 if (!(node
= strdup_lower(nodename
))) return WSA_NOT_ENOUGH_MEMORY
;
3908 if (!(serv
= strdup_lower(servname
))) {
3909 HeapFree(GetProcessHeap(), 0, node
);
3910 return WSA_NOT_ENOUGH_MEMORY
;
3915 punixhints
= &unixhints
;
3917 memset(&unixhints
, 0, sizeof(unixhints
));
3918 punixhints
->ai_flags
= convert_aiflag_w2u(hints
->ai_flags
);
3919 if (hints
->ai_family
== 0) /* wildcard, specific to getaddrinfo() */
3920 punixhints
->ai_family
= 0;
3922 punixhints
->ai_family
= convert_af_w2u(hints
->ai_family
);
3923 if (hints
->ai_socktype
== 0) /* wildcard, specific to getaddrinfo() */
3924 punixhints
->ai_socktype
= 0;
3926 punixhints
->ai_socktype
= convert_socktype_w2u(hints
->ai_socktype
);
3927 if (hints
->ai_protocol
== 0) /* wildcard, specific to getaddrinfo() */
3928 punixhints
->ai_protocol
= 0;
3930 punixhints
->ai_protocol
= convert_proto_w2u(hints
->ai_protocol
);
3933 /* getaddrinfo(3) is thread safe, no need to wrap in CS */
3934 result
= getaddrinfo(nodename
, servname
, punixhints
, &unixaires
);
3936 TRACE("%s, %s %p -> %p %d\n", debugstr_a(nodename
), debugstr_a(servname
), hints
, res
, result
);
3938 HeapFree(GetProcessHeap(), 0, node
);
3939 HeapFree(GetProcessHeap(), 0, serv
);
3942 struct addrinfo
*xuai
= unixaires
;
3943 struct WS_addrinfo
**xai
= res
;
3947 struct WS_addrinfo
*ai
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
, sizeof(struct WS_addrinfo
));
3953 *xai
= ai
;xai
= &ai
->ai_next
;
3954 ai
->ai_flags
= convert_aiflag_u2w(xuai
->ai_flags
);
3955 ai
->ai_family
= convert_af_u2w(xuai
->ai_family
);
3956 ai
->ai_socktype
= convert_socktype_u2w(xuai
->ai_socktype
);
3957 ai
->ai_protocol
= convert_proto_u2w(xuai
->ai_protocol
);
3958 if (xuai
->ai_canonname
) {
3959 TRACE("canon name - %s\n",debugstr_a(xuai
->ai_canonname
));
3960 ai
->ai_canonname
= HeapAlloc(GetProcessHeap(),0,strlen(xuai
->ai_canonname
)+1);
3961 if (!ai
->ai_canonname
)
3963 strcpy(ai
->ai_canonname
,xuai
->ai_canonname
);
3965 len
= xuai
->ai_addrlen
;
3966 ai
->ai_addr
= HeapAlloc(GetProcessHeap(),0,len
);
3969 ai
->ai_addrlen
= len
;
3971 int winlen
= ai
->ai_addrlen
;
3973 if (!ws_sockaddr_u2ws(xuai
->ai_addr
, ai
->ai_addr
, &winlen
)) {
3974 ai
->ai_addrlen
= winlen
;
3978 ai
->ai_addr
= HeapReAlloc(GetProcessHeap(),0,ai
->ai_addr
,len
);
3981 ai
->ai_addrlen
= len
;
3983 xuai
= xuai
->ai_next
;
3985 freeaddrinfo(unixaires
);
3987 result
= convert_eai_u2w(result
);
3993 if (*res
) WS_freeaddrinfo(*res
);
3994 if (unixaires
) freeaddrinfo(unixaires
);
3996 return WSA_NOT_ENOUGH_MEMORY
;
3998 FIXME("getaddrinfo() failed, not found during buildtime.\n");
4003 static struct WS_addrinfoW
*addrinfo_AtoW(const struct WS_addrinfo
*ai
)
4005 struct WS_addrinfoW
*ret
;
4007 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfoW
)))) return NULL
;
4008 ret
->ai_flags
= ai
->ai_flags
;
4009 ret
->ai_family
= ai
->ai_family
;
4010 ret
->ai_socktype
= ai
->ai_socktype
;
4011 ret
->ai_protocol
= ai
->ai_protocol
;
4012 ret
->ai_addrlen
= ai
->ai_addrlen
;
4013 ret
->ai_canonname
= NULL
;
4014 ret
->ai_addr
= NULL
;
4015 ret
->ai_next
= NULL
;
4016 if (ai
->ai_canonname
)
4018 int len
= MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0);
4019 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4021 HeapFree(GetProcessHeap(), 0, ret
);
4024 MultiByteToWideChar(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
);
4028 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4030 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4031 HeapFree(GetProcessHeap(), 0, ret
);
4034 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4039 static struct WS_addrinfoW
*addrinfo_list_AtoW(const struct WS_addrinfo
*info
)
4041 struct WS_addrinfoW
*ret
, *infoW
;
4043 if (!(ret
= infoW
= addrinfo_AtoW(info
))) return NULL
;
4044 while (info
->ai_next
)
4046 if (!(infoW
->ai_next
= addrinfo_AtoW(info
->ai_next
)))
4051 infoW
= infoW
->ai_next
;
4052 info
= info
->ai_next
;
4057 static struct WS_addrinfo
*addrinfo_WtoA(const struct WS_addrinfoW
*ai
)
4059 struct WS_addrinfo
*ret
;
4061 if (!(ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_addrinfo
)))) return NULL
;
4062 ret
->ai_flags
= ai
->ai_flags
;
4063 ret
->ai_family
= ai
->ai_family
;
4064 ret
->ai_socktype
= ai
->ai_socktype
;
4065 ret
->ai_protocol
= ai
->ai_protocol
;
4066 ret
->ai_addrlen
= ai
->ai_addrlen
;
4067 ret
->ai_canonname
= NULL
;
4068 ret
->ai_addr
= NULL
;
4069 ret
->ai_next
= NULL
;
4070 if (ai
->ai_canonname
)
4072 int len
= WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, NULL
, 0, NULL
, NULL
);
4073 if (!(ret
->ai_canonname
= HeapAlloc(GetProcessHeap(), 0, len
)))
4075 HeapFree(GetProcessHeap(), 0, ret
);
4078 WideCharToMultiByte(CP_ACP
, 0, ai
->ai_canonname
, -1, ret
->ai_canonname
, len
, NULL
, NULL
);
4082 if (!(ret
->ai_addr
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct WS_sockaddr
))))
4084 HeapFree(GetProcessHeap(), 0, ret
->ai_canonname
);
4085 HeapFree(GetProcessHeap(), 0, ret
);
4088 memcpy(ret
->ai_addr
, ai
->ai_addr
, sizeof(struct WS_sockaddr
));
4093 /***********************************************************************
4094 * GetAddrInfoW (WS2_32.@)
4096 int WINAPI
GetAddrInfoW(LPCWSTR nodename
, LPCWSTR servname
, const ADDRINFOW
*hints
, PADDRINFOW
*res
)
4099 char *nodenameA
, *servnameA
= NULL
;
4100 struct WS_addrinfo
*resA
, *hintsA
= NULL
;
4102 if (!nodename
) return WSAHOST_NOT_FOUND
;
4104 len
= WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, NULL
, 0, NULL
, NULL
);
4105 if (!(nodenameA
= HeapAlloc(GetProcessHeap(), 0, len
))) return EAI_MEMORY
;
4106 WideCharToMultiByte(CP_ACP
, 0, nodename
, -1, nodenameA
, len
, NULL
, NULL
);
4110 len
= WideCharToMultiByte(CP_ACP
, 0, servname
, -1, NULL
, 0, NULL
, NULL
);
4111 if (!(servnameA
= HeapAlloc(GetProcessHeap(), 0, len
)))
4113 HeapFree(GetProcessHeap(), 0, nodenameA
);
4116 WideCharToMultiByte(CP_ACP
, 0, servname
, -1, servnameA
, len
, NULL
, NULL
);
4119 if (hints
) hintsA
= addrinfo_WtoA(hints
);
4120 ret
= WS_getaddrinfo(nodenameA
, servnameA
, hintsA
, &resA
);
4121 WS_freeaddrinfo(hintsA
);
4125 *res
= addrinfo_list_AtoW(resA
);
4126 WS_freeaddrinfo(resA
);
4129 HeapFree(GetProcessHeap(), 0, nodenameA
);
4130 HeapFree(GetProcessHeap(), 0, servnameA
);
4134 /***********************************************************************
4135 * FreeAddrInfoW (WS2_32.@)
4137 void WINAPI
FreeAddrInfoW(PADDRINFOW ai
)
4142 HeapFree(GetProcessHeap(), 0, ai
->ai_canonname
);
4143 HeapFree(GetProcessHeap(), 0, ai
->ai_addr
);
4145 HeapFree(GetProcessHeap(), 0, ai
);
4150 int WINAPI
WS_getnameinfo(const SOCKADDR
*sa
, WS_socklen_t salen
, PCHAR host
,
4151 DWORD hostlen
, PCHAR serv
, DWORD servlen
, INT flags
)
4153 #ifdef HAVE_GETNAMEINFO
4155 union generic_unix_sockaddr sa_u
;
4158 TRACE("%s %d %p %d %p %d %d\n", debugstr_sockaddr(sa
), salen
, host
, hostlen
,
4159 serv
, servlen
, flags
);
4161 size
= ws_sockaddr_ws2u(sa
, salen
, &sa_u
);
4164 WSASetLastError(WSAEFAULT
);
4165 return WSA_NOT_ENOUGH_MEMORY
;
4167 ret
= getnameinfo(&sa_u
.addr
, size
, host
, hostlen
, serv
, servlen
, convert_niflag_w2u(flags
));
4168 return convert_eai_u2w(ret
);
4170 FIXME("getnameinfo() failed, not found during buildtime.\n");
4175 /***********************************************************************
4176 * getservbyport (WS2_32.56)
4178 struct WS_servent
* WINAPI
WS_getservbyport(int port
, const char *proto
)
4180 struct WS_servent
* retval
= NULL
;
4181 #ifdef HAVE_GETSERVBYPORT
4182 struct servent
* serv
;
4183 char *proto_str
= NULL
;
4185 if (proto
&& *proto
)
4187 if (!(proto_str
= strdup_lower(proto
))) return NULL
;
4189 EnterCriticalSection( &csWSgetXXXbyYYY
);
4190 if( (serv
= getservbyport(port
, proto_str
)) != NULL
) {
4191 retval
= WS_dup_se(serv
);
4193 else SetLastError(WSANO_DATA
);
4194 LeaveCriticalSection( &csWSgetXXXbyYYY
);
4195 HeapFree( GetProcessHeap(), 0, proto_str
);
4197 TRACE("%d (i.e. port %d), %s ret %p\n", port
, (int)ntohl(port
), debugstr_a(proto
), retval
);
4202 /***********************************************************************
4203 * gethostname (WS2_32.57)
4205 int WINAPI
WS_gethostname(char *name
, int namelen
)
4207 TRACE("name %p, len %d\n", name
, namelen
);
4209 if (gethostname(name
, namelen
) == 0)
4211 TRACE("<- '%s'\n", name
);
4214 SetLastError((errno
== EINVAL
) ? WSAEFAULT
: wsaErrno());
4215 TRACE("<- ERROR !\n");
4216 return SOCKET_ERROR
;
4220 /* ------------------------------------- Windows sockets extensions -- *
4222 * ------------------------------------------------------------------- */
4224 /***********************************************************************
4225 * WSAEnumNetworkEvents (WS2_32.36)
4227 int WINAPI
WSAEnumNetworkEvents(SOCKET s
, WSAEVENT hEvent
, LPWSANETWORKEVENTS lpEvent
)
4231 TRACE("%08lx, hEvent %p, lpEvent %p\n", s
, hEvent
, lpEvent
);
4233 SERVER_START_REQ( get_socket_event
)
4235 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
4236 req
->service
= TRUE
;
4237 req
->c_event
= wine_server_obj_handle( hEvent
);
4238 wine_server_set_reply( req
, lpEvent
->iErrorCode
, sizeof(lpEvent
->iErrorCode
) );
4239 if (!(ret
= wine_server_call(req
))) lpEvent
->lNetworkEvents
= reply
->pmask
& reply
->mask
;
4243 SetLastError(WSAEINVAL
);
4244 return SOCKET_ERROR
;
4247 /***********************************************************************
4248 * WSAEventSelect (WS2_32.39)
4250 int WINAPI
WSAEventSelect(SOCKET s
, WSAEVENT hEvent
, LONG lEvent
)
4254 TRACE("%08lx, hEvent %p, event %08x\n", s
, hEvent
, lEvent
);
4256 SERVER_START_REQ( set_socket_event
)
4258 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
4260 req
->event
= wine_server_obj_handle( hEvent
);
4263 ret
= wine_server_call( req
);
4267 SetLastError(WSAEINVAL
);
4268 return SOCKET_ERROR
;
4271 /**********************************************************************
4272 * WSAGetOverlappedResult (WS2_32.40)
4274 BOOL WINAPI
WSAGetOverlappedResult( SOCKET s
, LPWSAOVERLAPPED lpOverlapped
,
4275 LPDWORD lpcbTransfer
, BOOL fWait
,
4280 TRACE( "socket %04lx ovl %p trans %p, wait %d flags %p\n",
4281 s
, lpOverlapped
, lpcbTransfer
, fWait
, lpdwFlags
);
4283 if ( lpOverlapped
== NULL
)
4285 ERR( "Invalid pointer\n" );
4286 WSASetLastError(WSA_INVALID_PARAMETER
);
4290 status
= lpOverlapped
->Internal
;
4291 if (status
== STATUS_PENDING
)
4295 SetLastError( WSA_IO_INCOMPLETE
);
4299 if (WaitForSingleObject( lpOverlapped
->hEvent
? lpOverlapped
->hEvent
: SOCKET2HANDLE(s
),
4300 INFINITE
) == WAIT_FAILED
)
4302 status
= lpOverlapped
->Internal
;
4306 *lpcbTransfer
= lpOverlapped
->InternalHigh
;
4309 *lpdwFlags
= lpOverlapped
->u
.s
.Offset
;
4311 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
4316 /***********************************************************************
4317 * WSAAsyncSelect (WS2_32.101)
4319 INT WINAPI
WSAAsyncSelect(SOCKET s
, HWND hWnd
, UINT uMsg
, LONG lEvent
)
4323 TRACE("%lx, hWnd %p, uMsg %08x, event %08x\n", s
, hWnd
, uMsg
, lEvent
);
4325 SERVER_START_REQ( set_socket_event
)
4327 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
4330 req
->window
= wine_server_user_handle( hWnd
);
4332 ret
= wine_server_call( req
);
4336 SetLastError(WSAEINVAL
);
4337 return SOCKET_ERROR
;
4340 /***********************************************************************
4341 * WSACreateEvent (WS2_32.31)
4344 WSAEVENT WINAPI
WSACreateEvent(void)
4346 /* Create a manual-reset event, with initial state: unsignaled */
4349 return CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
4352 /***********************************************************************
4353 * WSACloseEvent (WS2_32.29)
4356 BOOL WINAPI
WSACloseEvent(WSAEVENT event
)
4358 TRACE ("event=%p\n", event
);
4360 return CloseHandle(event
);
4363 /***********************************************************************
4364 * WSASocketA (WS2_32.78)
4367 SOCKET WINAPI
WSASocketA(int af
, int type
, int protocol
,
4368 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
4369 GROUP g
, DWORD dwFlags
)
4372 WSAPROTOCOL_INFOW info
;
4374 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
4375 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
4377 if (!lpProtocolInfo
) return WSASocketW(af
, type
, protocol
, NULL
, g
, dwFlags
);
4379 memcpy(&info
, lpProtocolInfo
, FIELD_OFFSET(WSAPROTOCOL_INFOW
, szProtocol
));
4380 len
= MultiByteToWideChar(CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
4381 info
.szProtocol
, WSAPROTOCOL_LEN
+ 1);
4385 WSASetLastError( WSAEINVAL
);
4386 return SOCKET_ERROR
;
4389 return WSASocketW(af
, type
, protocol
, &info
, g
, dwFlags
);
4392 /***********************************************************************
4393 * WSASocketW (WS2_32.79)
4396 SOCKET WINAPI
WSASocketW(int af
, int type
, int protocol
,
4397 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
4398 GROUP g
, DWORD dwFlags
)
4403 FIXME: The "advanced" parameters of WSASocketW (lpProtocolInfo,
4404 g, dwFlags except WSA_FLAG_OVERLAPPED) are ignored.
4407 TRACE("af=%d type=%d protocol=%d protocol_info=%p group=%d flags=0x%x\n",
4408 af
, type
, protocol
, lpProtocolInfo
, g
, dwFlags
);
4410 /* hack for WSADuplicateSocket */
4411 if (lpProtocolInfo
&& lpProtocolInfo
->dwServiceFlags4
== 0xff00ff00) {
4412 ret
= lpProtocolInfo
->dwCatalogEntryId
;
4413 TRACE("\tgot duplicate %04lx\n", ret
);
4417 /* convert the socket family and type */
4418 af
= convert_af_w2u(af
);
4419 type
= convert_socktype_w2u(type
);
4423 if (af
== FROM_PROTOCOL_INFO
)
4424 af
= lpProtocolInfo
->iAddressFamily
;
4425 if (type
== FROM_PROTOCOL_INFO
)
4426 type
= lpProtocolInfo
->iSocketType
;
4427 if (protocol
== FROM_PROTOCOL_INFO
)
4428 protocol
= lpProtocolInfo
->iProtocol
;
4431 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
4435 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
4437 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
4438 default: SetLastError(WSAEPROTOTYPE
); return INVALID_SOCKET
;
4441 SERVER_START_REQ( create_socket
)
4445 req
->protocol
= protocol
;
4446 req
->access
= GENERIC_READ
|GENERIC_WRITE
|SYNCHRONIZE
;
4447 req
->attributes
= OBJ_INHERIT
;
4448 req
->flags
= dwFlags
;
4449 set_error( wine_server_call( req
) );
4450 ret
= HANDLE2SOCKET( wine_server_ptr_handle( reply
->handle
));
4455 TRACE("\tcreated %04lx\n", ret
);
4459 if (GetLastError() == WSAEACCES
) /* raw socket denied */
4461 if (type
== SOCK_RAW
)
4462 ERR_(winediag
)("Failed to create a socket of type SOCK_RAW, this requires special permissions.\n");
4464 ERR_(winediag
)("Failed to create socket, this requires special permissions.\n");
4465 SetLastError(WSAESOCKTNOSUPPORT
);
4468 WARN("\t\tfailed!\n");
4469 return INVALID_SOCKET
;
4472 /***********************************************************************
4473 * WSAJoinLeaf (WS2_32.58)
4476 SOCKET WINAPI
WSAJoinLeaf(
4478 const struct WS_sockaddr
*addr
,
4480 LPWSABUF lpCallerData
,
4481 LPWSABUF lpCalleeData
,
4487 return INVALID_SOCKET
;
4490 /***********************************************************************
4491 * __WSAFDIsSet (WS2_32.151)
4493 int WINAPI
__WSAFDIsSet(SOCKET s
, WS_fd_set
*set
)
4495 int i
= set
->fd_count
;
4497 TRACE("(%ld,%p(%i))\n", s
, set
, i
);
4500 if (set
->fd_array
[i
] == s
) return 1;
4504 /***********************************************************************
4505 * WSAIsBlocking (WS2_32.114)
4507 BOOL WINAPI
WSAIsBlocking(void)
4509 /* By default WinSock should set all its sockets to non-blocking mode
4510 * and poll in PeekMessage loop when processing "blocking" ones. This
4511 * function is supposed to tell if the program is in this loop. Our
4512 * blocking calls are truly blocking so we always return FALSE.
4514 * Note: It is allowed to call this function without prior WSAStartup().
4521 /***********************************************************************
4522 * WSACancelBlockingCall (WS2_32.113)
4524 INT WINAPI
WSACancelBlockingCall(void)
4530 static INT WINAPI
WSA_DefaultBlockingHook( FARPROC x
)
4532 FIXME("How was this called?\n");
4537 /***********************************************************************
4538 * WSASetBlockingHook (WS2_32.109)
4540 FARPROC WINAPI
WSASetBlockingHook(FARPROC lpBlockFunc
)
4542 FARPROC prev
= blocking_hook
;
4543 blocking_hook
= lpBlockFunc
;
4544 TRACE("hook %p\n", lpBlockFunc
);
4549 /***********************************************************************
4550 * WSAUnhookBlockingHook (WS2_32.110)
4552 INT WINAPI
WSAUnhookBlockingHook(void)
4554 blocking_hook
= (FARPROC
)WSA_DefaultBlockingHook
;
4559 /* ----------------------------------- end of API stuff */
4561 /* ----------------------------------- helper functions -
4563 * TODO: Merge WS_dup_..() stuff into one function that
4564 * would operate with a generic structure containing internal
4565 * pointers (via a template of some kind).
4568 static int list_size(char** l
, int item_size
)
4573 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
4574 j
+= (i
+ 1) * sizeof(char*); }
4578 static int list_dup(char** l_src
, char** l_to
, int item_size
)
4583 for (i
= 0; l_src
[i
]; i
++) ;
4584 p
= (char *)(l_to
+ i
+ 1);
4585 for (i
= 0; l_src
[i
]; i
++)
4587 int count
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
4588 memcpy(p
, l_src
[i
], count
);
4593 return p
- (char *)l_to
;
4598 /* create a hostent entry
4600 * Creates the entry with enough memory for the name, aliases
4601 * addresses, and the address pointers. Also copies the name
4602 * and sets up all the pointers. If "fill_addresses" is set then
4603 * sufficient memory for the addresses is also allocated and the
4604 * address pointers are set to this memory.
4606 * NOTE: The alias and address lists must be allocated with room
4607 * for the NULL item terminating the list. This is true even if
4608 * the list has no items ("aliases" and "addresses" must be
4609 * at least "1", a truly empty list is invalid).
4611 static struct WS_hostent
*WS_create_he(char *name
, int aliases
, int addresses
, int fill_addresses
)
4613 struct WS_hostent
*p_to
;
4616 int size
= (sizeof(struct WS_hostent
) +
4618 sizeof(char *)*aliases
+
4619 sizeof(char *)*addresses
);
4621 /* Allocate enough memory for the addresses */
4623 size
+= sizeof(struct in_addr
)*addresses
;
4625 if (!(p_to
= check_buffer_he(size
))) return NULL
;
4626 memset(p_to
, 0, size
);
4628 p
= (char *)(p_to
+ 1);
4633 p_to
->h_aliases
= (char **)p
;
4634 p
+= sizeof(char *)*aliases
;
4635 p_to
->h_addr_list
= (char **)p
;
4636 p
+= sizeof(char *)*addresses
;
4641 /* NOTE: h_aliases must be filled in manually, leave these
4642 * pointers NULL (already set to NULL by memset earlier).
4645 /* Fill in the list of address pointers */
4646 for (i
= 0; i
< addresses
; i
++)
4647 p_to
->h_addr_list
[i
] = (p
+= sizeof(struct in_addr
));
4648 p
+= sizeof(struct in_addr
);
4653 /* duplicate hostent entry
4654 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
4655 * Ditto for protoent and servent.
4657 static struct WS_hostent
*WS_dup_he(const struct hostent
* p_he
)
4659 int addresses
= list_size(p_he
->h_addr_list
, p_he
->h_length
);
4660 int aliases
= list_size(p_he
->h_aliases
, 0);
4661 struct WS_hostent
*p_to
;
4663 p_to
= WS_create_he(p_he
->h_name
, aliases
, addresses
, FALSE
);
4665 if (!p_to
) return NULL
;
4666 p_to
->h_addrtype
= p_he
->h_addrtype
;
4667 p_to
->h_length
= p_he
->h_length
;
4669 list_dup(p_he
->h_aliases
, p_to
->h_aliases
, 0);
4670 list_dup(p_he
->h_addr_list
, p_to
->h_addr_list
, p_he
->h_length
);
4674 /* ----- protoent */
4676 static struct WS_protoent
*WS_dup_pe(const struct protoent
* p_pe
)
4679 struct WS_protoent
*p_to
;
4681 int size
= (sizeof(*p_pe
) +
4682 strlen(p_pe
->p_name
) + 1 +
4683 list_size(p_pe
->p_aliases
, 0));
4685 if (!(p_to
= check_buffer_pe(size
))) return NULL
;
4686 p_to
->p_proto
= p_pe
->p_proto
;
4688 p
= (char *)(p_to
+ 1);
4690 strcpy(p
, p_pe
->p_name
);
4693 p_to
->p_aliases
= (char **)p
;
4694 list_dup(p_pe
->p_aliases
, p_to
->p_aliases
, 0);
4700 static struct WS_servent
*WS_dup_se(const struct servent
* p_se
)
4703 struct WS_servent
*p_to
;
4705 int size
= (sizeof(*p_se
) +
4706 strlen(p_se
->s_proto
) + 1 +
4707 strlen(p_se
->s_name
) + 1 +
4708 list_size(p_se
->s_aliases
, 0));
4710 if (!(p_to
= check_buffer_se(size
))) return NULL
;
4711 p_to
->s_port
= p_se
->s_port
;
4713 p
= (char *)(p_to
+ 1);
4715 strcpy(p
, p_se
->s_name
);
4719 strcpy(p
, p_se
->s_proto
);
4722 p_to
->s_aliases
= (char **)p
;
4723 list_dup(p_se
->s_aliases
, p_to
->s_aliases
, 0);
4728 /***********************************************************************
4729 * WSARecv (WS2_32.67)
4731 int WINAPI
WSARecv(SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4732 LPDWORD NumberOfBytesReceived
, LPDWORD lpFlags
,
4733 LPWSAOVERLAPPED lpOverlapped
,
4734 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4736 return WSARecvFrom(s
, lpBuffers
, dwBufferCount
, NumberOfBytesReceived
, lpFlags
,
4737 NULL
, NULL
, lpOverlapped
, lpCompletionRoutine
);
4740 /***********************************************************************
4741 * WSARecvFrom (WS2_32.69)
4743 INT WINAPI
WSARecvFrom( SOCKET s
, LPWSABUF lpBuffers
, DWORD dwBufferCount
,
4744 LPDWORD lpNumberOfBytesRecvd
, LPDWORD lpFlags
, struct WS_sockaddr
*lpFrom
,
4745 LPINT lpFromlen
, LPWSAOVERLAPPED lpOverlapped
,
4746 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
)
4749 unsigned int i
, options
;
4751 struct ws2_async
*wsa
;
4752 DWORD timeout_start
= GetTickCount();
4753 ULONG_PTR cvalue
= (lpOverlapped
&& ((ULONG_PTR
)lpOverlapped
->hEvent
& 1) == 0) ? (ULONG_PTR
)lpOverlapped
: 0;
4755 TRACE("socket %04lx, wsabuf %p, nbufs %d, flags %d, from %p, fromlen %d, ovl %p, func %p\n",
4756 s
, lpBuffers
, dwBufferCount
, *lpFlags
, lpFrom
,
4757 (lpFromlen
? *lpFromlen
: -1),
4758 lpOverlapped
, lpCompletionRoutine
);
4760 fd
= get_sock_fd( s
, FILE_READ_DATA
, &options
);
4761 TRACE( "fd=%d, options=%x\n", fd
, options
);
4763 if (fd
== -1) return SOCKET_ERROR
;
4765 if (!(wsa
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(struct ws2_async
, iovec
[dwBufferCount
]) )))
4771 wsa
->hSocket
= SOCKET2HANDLE(s
);
4772 wsa
->flags
= *lpFlags
;
4774 wsa
->addrlen
.ptr
= lpFromlen
;
4775 wsa
->n_iovecs
= dwBufferCount
;
4776 wsa
->first_iovec
= 0;
4777 for (i
= 0; i
< dwBufferCount
; i
++)
4779 /* check buffer first to trigger write watches */
4780 if (IsBadWritePtr( lpBuffers
[i
].buf
, lpBuffers
[i
].len
))
4785 wsa
->iovec
[i
].iov_base
= lpBuffers
[i
].buf
;
4786 wsa
->iovec
[i
].iov_len
= lpBuffers
[i
].len
;
4791 n
= WS2_recv( fd
, wsa
);
4794 if (errno
== EINTR
) continue;
4795 if (errno
!= EAGAIN
)
4798 if (cvalue
) WS_AddCompletion( s
, cvalue
, err
, 0 );
4803 *lpNumberOfBytesRecvd
= n
;
4805 if ((lpOverlapped
|| lpCompletionRoutine
) &&
4806 !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
)))
4808 IO_STATUS_BLOCK
*iosb
= lpOverlapped
? (IO_STATUS_BLOCK
*)lpOverlapped
: &wsa
->local_iosb
;
4810 wsa
->user_overlapped
= lpOverlapped
;
4811 wsa
->completion_func
= lpCompletionRoutine
;
4812 release_sock_fd( s
, fd
);
4816 iosb
->u
.Status
= STATUS_PENDING
;
4817 iosb
->Information
= 0;
4819 SERVER_START_REQ( register_async
)
4821 req
->type
= ASYNC_TYPE_READ
;
4822 req
->async
.handle
= wine_server_obj_handle( wsa
->hSocket
);
4823 req
->async
.callback
= wine_server_client_ptr( WS2_async_recv
);
4824 req
->async
.iosb
= wine_server_client_ptr( iosb
);
4825 req
->async
.arg
= wine_server_client_ptr( wsa
);
4826 req
->async
.event
= wine_server_obj_handle( lpCompletionRoutine
? 0 : lpOverlapped
->hEvent
);
4827 req
->async
.cvalue
= cvalue
;
4828 err
= wine_server_call( req
);
4832 if (err
!= STATUS_PENDING
) HeapFree( GetProcessHeap(), 0, wsa
);
4833 WSASetLastError( NtStatusToWSAError( err
));
4834 return SOCKET_ERROR
;
4837 iosb
->u
.Status
= STATUS_SUCCESS
;
4838 iosb
->Information
= n
;
4839 if (!wsa
->completion_func
)
4841 if (cvalue
) WS_AddCompletion( s
, cvalue
, STATUS_SUCCESS
, n
);
4842 if (lpOverlapped
->hEvent
) SetEvent( lpOverlapped
->hEvent
);
4843 HeapFree( GetProcessHeap(), 0, wsa
);
4845 else NtQueueApcThread( GetCurrentThread(), (PNTAPCFUNC
)ws2_async_apc
,
4846 (ULONG_PTR
)wsa
, (ULONG_PTR
)iosb
, 0 );
4847 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4853 if ( _is_blocking(s
) )
4856 int timeout
= GET_RCVTIMEO(fd
);
4859 timeout
-= GetTickCount() - timeout_start
;
4860 if (timeout
< 0) timeout
= 0;
4864 pfd
.events
= POLLIN
;
4865 if (*lpFlags
& WS_MSG_OOB
) pfd
.events
|= POLLPRI
;
4867 if (!timeout
|| !poll( &pfd
, 1, timeout
))
4870 /* a timeout is not fatal */
4871 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4877 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4878 err
= WSAEWOULDBLOCK
;
4883 TRACE(" -> %i bytes\n", n
);
4884 HeapFree( GetProcessHeap(), 0, wsa
);
4885 release_sock_fd( s
, fd
);
4886 _enable_event(SOCKET2HANDLE(s
), FD_READ
, 0, 0);
4891 HeapFree( GetProcessHeap(), 0, wsa
);
4892 release_sock_fd( s
, fd
);
4893 WARN(" -> ERROR %d\n", err
);
4894 WSASetLastError( err
);
4895 return SOCKET_ERROR
;
4898 /***********************************************************************
4899 * WSCInstallProvider (WS2_32.88)
4901 INT WINAPI
WSCInstallProvider( const LPGUID lpProviderId
,
4902 LPCWSTR lpszProviderDllPath
,
4903 const LPWSAPROTOCOL_INFOW lpProtocolInfoList
,
4904 DWORD dwNumberOfEntries
,
4907 FIXME("(%s, %s, %p, %d, %p): stub !\n", debugstr_guid(lpProviderId
),
4908 debugstr_w(lpszProviderDllPath
), lpProtocolInfoList
,
4909 dwNumberOfEntries
, lpErrno
);
4915 /***********************************************************************
4916 * WSCDeinstallProvider (WS2_32.83)
4918 INT WINAPI
WSCDeinstallProvider(LPGUID lpProviderId
, LPINT lpErrno
)
4920 FIXME("(%s, %p): stub !\n", debugstr_guid(lpProviderId
), lpErrno
);
4926 /***********************************************************************
4927 * WSAAccept (WS2_32.26)
4929 SOCKET WINAPI
WSAAccept( SOCKET s
, struct WS_sockaddr
*addr
, LPINT addrlen
,
4930 LPCONDITIONPROC lpfnCondition
, DWORD dwCallbackData
)
4933 int ret
= 0, size
= 0;
4934 WSABUF CallerId
, CallerData
, CalleeId
, CalleeData
;
4935 /* QOS SQOS, GQOS; */
4938 SOCKADDR src_addr
, dst_addr
;
4940 TRACE("Socket %04lx, sockaddr %p, addrlen %p, fnCondition %p, dwCallbackData %d\n",
4941 s
, addr
, addrlen
, lpfnCondition
, dwCallbackData
);
4944 size
= sizeof(src_addr
);
4945 cs
= WS_accept(s
, &src_addr
, &size
);
4947 if (cs
== SOCKET_ERROR
) return SOCKET_ERROR
;
4949 if (!lpfnCondition
) return cs
;
4951 CallerId
.buf
= (char *)&src_addr
;
4952 CallerId
.len
= sizeof(src_addr
);
4954 CallerData
.buf
= NULL
;
4957 WS_getsockname(cs
, &dst_addr
, &size
);
4959 CalleeId
.buf
= (char *)&dst_addr
;
4960 CalleeId
.len
= sizeof(dst_addr
);
4963 ret
= (*lpfnCondition
)(&CallerId
, &CallerData
, NULL
, NULL
,
4964 &CalleeId
, &CalleeData
, &g
, dwCallbackData
);
4969 if (addr
&& addrlen
)
4970 addr
= memcpy(addr
, &src_addr
, (*addrlen
> size
) ? size
: *addrlen
);
4973 SERVER_START_REQ( set_socket_deferred
)
4975 req
->handle
= wine_server_obj_handle( SOCKET2HANDLE(s
) );
4976 req
->deferred
= wine_server_obj_handle( SOCKET2HANDLE(cs
) );
4977 if ( !wine_server_call_err ( req
) )
4979 SetLastError( WSATRY_AGAIN
);
4980 WS_closesocket( cs
);
4984 return SOCKET_ERROR
;
4987 SetLastError(WSAECONNREFUSED
);
4988 return SOCKET_ERROR
;
4990 FIXME("Unknown return type from Condition function\n");
4991 SetLastError(WSAENOTSOCK
);
4992 return SOCKET_ERROR
;
4996 /***********************************************************************
4997 * WSADuplicateSocketA (WS2_32.32)
4999 int WINAPI
WSADuplicateSocketA( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOA lpProtocolInfo
)
5003 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5004 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5005 /* FIXME: WS_getsockopt(s, WS_SOL_SOCKET, SO_PROTOCOL_INFO, lpProtocolInfo, sizeof(*lpProtocolInfo)); */
5006 /* I don't know what the real Windoze does next, this is a hack */
5007 /* ...we could duplicate and then use ConvertToGlobalHandle on the duplicate, then let
5008 * the target use the global duplicate, or we could copy a reference to us to the structure
5009 * and let the target duplicate it from us, but let's do it as simple as possible */
5010 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5011 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5012 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5013 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5014 CloseHandle(hProcess
);
5015 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5019 /***********************************************************************
5020 * WSADuplicateSocketW (WS2_32.33)
5022 int WINAPI
WSADuplicateSocketW( SOCKET s
, DWORD dwProcessId
, LPWSAPROTOCOL_INFOW lpProtocolInfo
)
5026 TRACE("(%ld,%x,%p)\n", s
, dwProcessId
, lpProtocolInfo
);
5028 memset(lpProtocolInfo
, 0, sizeof(*lpProtocolInfo
));
5029 hProcess
= OpenProcess(PROCESS_DUP_HANDLE
, FALSE
, dwProcessId
);
5030 DuplicateHandle(GetCurrentProcess(), SOCKET2HANDLE(s
),
5031 hProcess
, (LPHANDLE
)&lpProtocolInfo
->dwCatalogEntryId
,
5032 0, FALSE
, DUPLICATE_SAME_ACCESS
);
5033 CloseHandle(hProcess
);
5034 lpProtocolInfo
->dwServiceFlags4
= 0xff00ff00; /* magic */
5038 /***********************************************************************
5039 * WSAInstallServiceClassA (WS2_32.48)
5041 int WINAPI
WSAInstallServiceClassA(LPWSASERVICECLASSINFOA info
)
5043 FIXME("Request to install service %s\n",debugstr_a(info
->lpszServiceClassName
));
5044 WSASetLastError(WSAEACCES
);
5045 return SOCKET_ERROR
;
5048 /***********************************************************************
5049 * WSAInstallServiceClassW (WS2_32.49)
5051 int WINAPI
WSAInstallServiceClassW(LPWSASERVICECLASSINFOW info
)
5053 FIXME("Request to install service %s\n",debugstr_w(info
->lpszServiceClassName
));
5054 WSASetLastError(WSAEACCES
);
5055 return SOCKET_ERROR
;
5058 /***********************************************************************
5059 * WSARemoveServiceClass (WS2_32.70)
5061 int WINAPI
WSARemoveServiceClass(LPGUID info
)
5063 FIXME("Request to remove service %p\n",info
);
5064 WSASetLastError(WSATYPE_NOT_FOUND
);
5065 return SOCKET_ERROR
;
5068 /***********************************************************************
5069 * inet_ntop (WS2_32.@)
5071 PCSTR WINAPI
WS_inet_ntop( INT family
, PVOID addr
, PSTR buffer
, SIZE_T len
)
5073 #ifdef HAVE_INET_NTOP
5074 struct WS_in6_addr
*in6
;
5075 struct WS_in_addr
*in
;
5078 TRACE("family %d, addr (%p), buffer (%p), len %ld\n", family
, addr
, buffer
, len
);
5081 WSASetLastError( STATUS_INVALID_PARAMETER
);
5090 pdst
= inet_ntop( AF_INET
, &in
->WS_s_addr
, buffer
, len
);
5096 pdst
= inet_ntop( AF_INET6
, in6
->WS_s6_addr
, buffer
, len
);
5100 WSASetLastError( WSAEAFNOSUPPORT
);
5104 if (!pdst
) WSASetLastError( STATUS_INVALID_PARAMETER
);
5107 FIXME( "not supported on this platform\n" );
5108 WSASetLastError( WSAEAFNOSUPPORT
);
5113 /***********************************************************************
5114 * WSAStringToAddressA (WS2_32.80)
5116 INT WINAPI
WSAStringToAddressA(LPSTR AddressString
,
5118 LPWSAPROTOCOL_INFOA lpProtocolInfo
,
5119 LPSOCKADDR lpAddress
,
5120 LPINT lpAddressLength
)
5123 LPSTR workBuffer
=NULL
,ptrPort
;
5125 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_a(AddressString
), AddressFamily
,
5126 lpProtocolInfo
, lpAddress
, lpAddressLength
);
5128 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
5132 WSASetLastError(WSAEINVAL
);
5133 return SOCKET_ERROR
;
5137 FIXME("ProtocolInfo not implemented.\n");
5139 workBuffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
5140 strlen(AddressString
) + 1);
5143 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5144 return SOCKET_ERROR
;
5147 strcpy(workBuffer
, AddressString
);
5149 switch(AddressFamily
)
5153 struct in_addr inetaddr
;
5155 /* If lpAddressLength is too small, tell caller the size we need */
5156 if (*lpAddressLength
< sizeof(SOCKADDR_IN
))
5158 *lpAddressLength
= sizeof(SOCKADDR_IN
);
5162 memset(lpAddress
, 0, sizeof(SOCKADDR_IN
));
5164 ((LPSOCKADDR_IN
)lpAddress
)->sin_family
= AF_INET
;
5166 ptrPort
= strchr(workBuffer
, ':');
5169 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= htons(atoi(ptrPort
+1));
5174 ((LPSOCKADDR_IN
)lpAddress
)->sin_port
= 0;
5177 if(inet_aton(workBuffer
, &inetaddr
) > 0)
5179 ((LPSOCKADDR_IN
)lpAddress
)->sin_addr
.WS_s_addr
= inetaddr
.s_addr
;
5190 struct in6_addr inetaddr
;
5191 /* If lpAddressLength is too small, tell caller the size we need */
5192 if (*lpAddressLength
< sizeof(SOCKADDR_IN6
))
5194 *lpAddressLength
= sizeof(SOCKADDR_IN6
);
5198 #ifdef HAVE_INET_PTON
5199 memset(lpAddress
, 0, sizeof(SOCKADDR_IN6
));
5201 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_family
= WS_AF_INET6
;
5203 /* This one is a bit tricky. An IPv6 address contains colons, so the
5204 * check from IPv4 doesn't work like that. However, IPv6 addresses that
5205 * contain a port are written with braces like [fd12:3456:7890::1]:12345
5206 * so what we will do is to look for ']', check if the next char is a
5207 * colon, and if it is, parse the port as in IPv4. */
5209 ptrPort
= strchr(workBuffer
, ']');
5210 if(ptrPort
&& *(++ptrPort
) == ':')
5212 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= htons(atoi(ptrPort
+1));
5217 ((LPSOCKADDR_IN6
)lpAddress
)->sin6_port
= 0;
5220 if(inet_pton(AF_INET6
, workBuffer
, &inetaddr
) > 0)
5222 memcpy(&((LPSOCKADDR_IN6
)lpAddress
)->sin6_addr
, &inetaddr
,
5223 sizeof(struct in6_addr
));
5227 #endif /* HAVE_INET_PTON */
5233 /* According to MSDN, only AF_INET and AF_INET6 are supported. */
5234 TRACE("Unsupported address family specified: %d.\n", AddressFamily
);
5238 HeapFree(GetProcessHeap(), 0, workBuffer
);
5241 WSASetLastError(res
);
5242 return SOCKET_ERROR
;
5245 /***********************************************************************
5246 * WSAStringToAddressW (WS2_32.81)
5248 * Does anybody know if this functions allows to use hebrew/arabic/chinese... digits?
5249 * If this should be the case, it would be required to map these digits
5250 * to Unicode digits (0-9) using FoldString first.
5252 INT WINAPI
WSAStringToAddressW(LPWSTR AddressString
,
5254 LPWSAPROTOCOL_INFOW lpProtocolInfo
,
5255 LPSOCKADDR lpAddress
,
5256 LPINT lpAddressLength
)
5259 LPSTR workBuffer
=NULL
;
5260 WSAPROTOCOL_INFOA infoA
;
5261 LPWSAPROTOCOL_INFOA lpProtoInfoA
= NULL
;
5263 TRACE( "(%s, %x, %p, %p, %p)\n", debugstr_w(AddressString
), AddressFamily
, lpProtocolInfo
,
5264 lpAddress
, lpAddressLength
);
5266 if (!lpAddressLength
|| !lpAddress
) return SOCKET_ERROR
;
5268 /* if ProtocolInfo is available - convert to ANSI variant */
5271 lpProtoInfoA
= &infoA
;
5272 memcpy( lpProtoInfoA
, lpProtocolInfo
, FIELD_OFFSET( WSAPROTOCOL_INFOA
, szProtocol
) );
5274 if (!WideCharToMultiByte( CP_ACP
, 0, lpProtocolInfo
->szProtocol
, -1,
5275 lpProtoInfoA
->szProtocol
, WSAPROTOCOL_LEN
+1, NULL
, NULL
))
5277 WSASetLastError( WSAEINVAL
);
5278 return SOCKET_ERROR
;
5284 /* Translate AddressString to ANSI code page - assumes that only
5285 standard digits 0-9 are used with this API call */
5286 sBuffer
= WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, NULL
, 0, NULL
, NULL
);
5287 workBuffer
= HeapAlloc( GetProcessHeap(), 0, sBuffer
);
5291 WideCharToMultiByte( CP_ACP
, 0, AddressString
, -1, workBuffer
, sBuffer
, NULL
, NULL
);
5292 res
= WSAStringToAddressA(workBuffer
,AddressFamily
,lpProtoInfoA
,
5293 lpAddress
,lpAddressLength
);
5294 HeapFree( GetProcessHeap(), 0, workBuffer
);
5298 res
= WSA_NOT_ENOUGH_MEMORY
;
5303 WSASetLastError(res
);
5304 return SOCKET_ERROR
;
5307 /***********************************************************************
5308 * WSAAddressToStringA (WS2_32.27)
5310 * See WSAAddressToStringW
5312 INT WINAPI
WSAAddressToStringA( LPSOCKADDR sockaddr
, DWORD len
,
5313 LPWSAPROTOCOL_INFOA info
, LPSTR string
,
5317 CHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
5320 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
5322 if (!sockaddr
) return SOCKET_ERROR
;
5323 if (!string
|| !lenstr
) return SOCKET_ERROR
;
5325 switch(sockaddr
->sa_family
)
5328 if (len
< sizeof(SOCKADDR_IN
)) return SOCKET_ERROR
;
5329 sprintf( buffer
, "%u.%u.%u.%u:%u",
5330 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 24 & 0xff),
5331 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 16 & 0xff),
5332 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) >> 8 & 0xff),
5333 (unsigned int)(ntohl( ((SOCKADDR_IN
*)sockaddr
)->sin_addr
.WS_s_addr
) & 0xff),
5334 ntohs( ((SOCKADDR_IN
*)sockaddr
)->sin_port
) );
5336 p
= strchr( buffer
, ':' );
5337 if (!((SOCKADDR_IN
*)sockaddr
)->sin_port
) *p
= 0;
5342 struct WS_sockaddr_in6
*sockaddr6
= (LPSOCKADDR_IN6
) sockaddr
;
5345 if (len
< sizeof(SOCKADDR_IN6
)) return SOCKET_ERROR
;
5346 if ((sockaddr6
->sin6_port
))
5347 strcpy(buffer
, "[");
5348 if (!WS_inet_ntop(WS_AF_INET6
, &sockaddr6
->sin6_addr
, buffer
+strlen(buffer
), sizeof(buffer
)))
5350 WSASetLastError(WSAEINVAL
);
5351 return SOCKET_ERROR
;
5353 if ((sockaddr6
->sin6_scope_id
))
5354 sprintf(buffer
+strlen(buffer
), "%%%u", sockaddr6
->sin6_scope_id
);
5355 if ((sockaddr6
->sin6_port
))
5356 sprintf(buffer
+strlen(buffer
), "]:%u", ntohs(sockaddr6
->sin6_port
));
5361 WSASetLastError(WSAEINVAL
);
5362 return SOCKET_ERROR
;
5365 size
= strlen( buffer
) + 1;
5370 WSASetLastError(WSAEFAULT
);
5371 return SOCKET_ERROR
;
5375 strcpy( string
, buffer
);
5379 /***********************************************************************
5380 * WSAAddressToStringW (WS2_32.28)
5382 * Convert a sockaddr address into a readable address string.
5385 * sockaddr [I] Pointer to a sockaddr structure.
5386 * len [I] Size of the sockaddr structure.
5387 * info [I] Pointer to a WSAPROTOCOL_INFOW structure (optional).
5388 * string [I/O] Pointer to a buffer to receive the address string.
5389 * lenstr [I/O] Size of the receive buffer in WCHARs.
5393 * Failure: SOCKET_ERROR
5396 * The 'info' parameter is ignored.
5398 INT WINAPI
WSAAddressToStringW( LPSOCKADDR sockaddr
, DWORD len
,
5399 LPWSAPROTOCOL_INFOW info
, LPWSTR string
,
5404 WCHAR buffer
[54]; /* 32 digits + 7':' + '[' + '%" + 5 digits + ']:' + 5 digits + '\0' */
5407 TRACE( "(%p, %d, %p, %p, %p)\n", sockaddr
, len
, info
, string
, lenstr
);
5410 ret
= WSAAddressToStringA(sockaddr
, len
, NULL
, bufAddr
, &size
);
5412 if (ret
) return ret
;
5414 MultiByteToWideChar( CP_ACP
, 0, bufAddr
, size
, buffer
, sizeof( buffer
)/sizeof(WCHAR
));
5419 WSASetLastError(WSAEFAULT
);
5420 return SOCKET_ERROR
;
5424 lstrcpyW( string
, buffer
);
5428 /***********************************************************************
5429 * WSAEnumNameSpaceProvidersA (WS2_32.34)
5431 INT WINAPI
WSAEnumNameSpaceProvidersA( LPDWORD len
, LPWSANAMESPACE_INFOA buffer
)
5433 FIXME( "(%p %p) Stub!\n", len
, buffer
);
5437 /***********************************************************************
5438 * WSAEnumNameSpaceProvidersW (WS2_32.35)
5440 INT WINAPI
WSAEnumNameSpaceProvidersW( LPDWORD len
, LPWSANAMESPACE_INFOW buffer
)
5442 FIXME( "(%p %p) Stub!\n", len
, buffer
);
5446 /***********************************************************************
5447 * WSAGetQOSByName (WS2_32.41)
5449 BOOL WINAPI
WSAGetQOSByName( SOCKET s
, LPWSABUF lpQOSName
, LPQOS lpQOS
)
5451 FIXME( "(0x%04lx %p %p) Stub!\n", s
, lpQOSName
, lpQOS
);
5455 /***********************************************************************
5456 * WSAGetServiceClassInfoA (WS2_32.42)
5458 INT WINAPI
WSAGetServiceClassInfoA( LPGUID provider
, LPGUID service
, LPDWORD len
,
5459 LPWSASERVICECLASSINFOA info
)
5461 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
5463 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5464 return SOCKET_ERROR
;
5467 /***********************************************************************
5468 * WSAGetServiceClassInfoW (WS2_32.43)
5470 INT WINAPI
WSAGetServiceClassInfoW( LPGUID provider
, LPGUID service
, LPDWORD len
,
5471 LPWSASERVICECLASSINFOW info
)
5473 FIXME( "(%s %s %p %p) Stub!\n", debugstr_guid(provider
), debugstr_guid(service
),
5475 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5476 return SOCKET_ERROR
;
5479 /***********************************************************************
5480 * WSAGetServiceClassNameByClassIdA (WS2_32.44)
5482 INT WINAPI
WSAGetServiceClassNameByClassIdA( LPGUID
class, LPSTR service
, LPDWORD len
)
5484 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
5485 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5486 return SOCKET_ERROR
;
5489 /***********************************************************************
5490 * WSAGetServiceClassNameByClassIdW (WS2_32.45)
5492 INT WINAPI
WSAGetServiceClassNameByClassIdW( LPGUID
class, LPWSTR service
, LPDWORD len
)
5494 FIXME( "(%s %p %p) Stub!\n", debugstr_guid(class), service
, len
);
5495 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5496 return SOCKET_ERROR
;
5499 /***********************************************************************
5500 * WSALookupServiceBeginA (WS2_32.59)
5502 INT WINAPI
WSALookupServiceBeginA( LPWSAQUERYSETA lpqsRestrictions
,
5503 DWORD dwControlFlags
,
5506 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
5508 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5509 return SOCKET_ERROR
;
5512 /***********************************************************************
5513 * WSALookupServiceBeginW (WS2_32.60)
5515 INT WINAPI
WSALookupServiceBeginW( LPWSAQUERYSETW lpqsRestrictions
,
5516 DWORD dwControlFlags
,
5519 FIXME("(%p 0x%08x %p) Stub!\n", lpqsRestrictions
, dwControlFlags
,
5521 WSASetLastError(WSA_NOT_ENOUGH_MEMORY
);
5522 return SOCKET_ERROR
;
5525 /***********************************************************************
5526 * WSALookupServiceBeginW (WS2_32.61)
5528 INT WINAPI
WSALookupServiceEnd( HANDLE lookup
)
5530 FIXME("(%p) Stub!\n", lookup
);
5534 /***********************************************************************
5535 * WSALookupServiceNextA (WS2_32.62)
5537 INT WINAPI
WSALookupServiceNextA( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETA results
)
5539 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
5543 /***********************************************************************
5544 * WSALookupServiceNextW (WS2_32.63)
5546 INT WINAPI
WSALookupServiceNextW( HANDLE lookup
, DWORD flags
, LPDWORD len
, LPWSAQUERYSETW results
)
5548 FIXME( "(%p 0x%08x %p %p) Stub!\n", lookup
, flags
, len
, results
);
5552 /***********************************************************************
5553 * WSANtohl (WS2_32.64)
5555 INT WINAPI
WSANtohl( SOCKET s
, WS_u_long netlong
, WS_u_long
* lphostlong
)
5557 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netlong
, lphostlong
);
5559 if (!lphostlong
) return WSAEFAULT
;
5561 *lphostlong
= ntohl( netlong
);
5565 /***********************************************************************
5566 * WSANtohs (WS2_32.65)
5568 INT WINAPI
WSANtohs( SOCKET s
, WS_u_short netshort
, WS_u_short
* lphostshort
)
5570 TRACE( "(0x%04lx 0x%08x %p)\n", s
, netshort
, lphostshort
);
5572 if (!lphostshort
) return WSAEFAULT
;
5574 *lphostshort
= ntohs( netshort
);
5578 /***********************************************************************
5579 * WSAProviderConfigChange (WS2_32.66)
5581 INT WINAPI
WSAProviderConfigChange( LPHANDLE handle
, LPWSAOVERLAPPED overlapped
,
5582 LPWSAOVERLAPPED_COMPLETION_ROUTINE completion
)
5584 FIXME( "(%p %p %p) Stub!\n", handle
, overlapped
, completion
);
5585 return SOCKET_ERROR
;
5588 /***********************************************************************
5589 * WSARecvDisconnect (WS2_32.68)
5591 INT WINAPI
WSARecvDisconnect( SOCKET s
, LPWSABUF disconnectdata
)
5593 TRACE( "(0x%04lx %p)\n", s
, disconnectdata
);
5595 return WS_shutdown( s
, 0 );
5598 /***********************************************************************
5599 * WSASetServiceA (WS2_32.76)
5601 INT WINAPI
WSASetServiceA( LPWSAQUERYSETA query
, WSAESETSERVICEOP operation
, DWORD flags
)
5603 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
5607 /***********************************************************************
5608 * WSASetServiceW (WS2_32.77)
5610 INT WINAPI
WSASetServiceW( LPWSAQUERYSETW query
, WSAESETSERVICEOP operation
, DWORD flags
)
5612 FIXME( "(%p 0x%08x 0x%08x) Stub!\n", query
, operation
, flags
);
5616 /***********************************************************************
5617 * WSCEnableNSProvider (WS2_32.84)
5619 INT WINAPI
WSCEnableNSProvider( LPGUID provider
, BOOL enable
)
5621 FIXME( "(%s 0x%08x) Stub!\n", debugstr_guid(provider
), enable
);
5625 /***********************************************************************
5626 * WSCGetProviderPath (WS2_32.86)
5628 INT WINAPI
WSCGetProviderPath( LPGUID provider
, LPWSTR path
, LPINT len
, LPINT errcode
)
5630 FIXME( "(%s %p %p %p) Stub!\n", debugstr_guid(provider
), path
, len
, errcode
);
5632 if (!errcode
|| !provider
|| !len
) return WSAEFAULT
;
5634 *errcode
= WSAEINVAL
;
5635 return SOCKET_ERROR
;
5638 /***********************************************************************
5639 * WSCInstallNameSpace (WS2_32.87)
5641 INT WINAPI
WSCInstallNameSpace( LPWSTR identifier
, LPWSTR path
, DWORD
namespace,
5642 DWORD version
, LPGUID provider
)
5644 FIXME( "(%s %s 0x%08x 0x%08x %s) Stub!\n", debugstr_w(identifier
), debugstr_w(path
),
5645 namespace, version
, debugstr_guid(provider
) );
5649 /***********************************************************************
5650 * WSCUnInstallNameSpace (WS2_32.89)
5652 INT WINAPI
WSCUnInstallNameSpace( LPGUID lpProviderId
)
5654 FIXME("(%p) Stub!\n", lpProviderId
);
5658 /***********************************************************************
5659 * WSCWriteProviderOrder (WS2_32.91)
5661 INT WINAPI
WSCWriteProviderOrder( LPDWORD entry
, DWORD number
)
5663 FIXME("(%p 0x%08x) Stub!\n", entry
, number
);