1999-10-13 18:05:37 +02:00
|
|
|
/*
|
|
|
|
* Server-side socket management
|
|
|
|
*
|
2008-10-18 19:22:26 +02:00
|
|
|
* Copyright (C) 1999 Marcus Meissner, Ove Kåven
|
1999-10-13 18:05:37 +02:00
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
1999-10-13 18:05:37 +02:00
|
|
|
* FIXME: we use read|write access in all cases. Shouldn't we depend that
|
|
|
|
* on the access of the current handle?
|
|
|
|
*/
|
|
|
|
|
1999-10-25 00:13:47 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
1999-10-13 18:05:37 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2021-06-16 06:13:27 +02:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
# include <ifaddrs.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NET_IF_H
|
|
|
|
# include <net/if.h>
|
|
|
|
#endif
|
2020-09-30 17:37:50 +02:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
2014-06-01 11:00:29 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
# include <poll.h>
|
|
|
|
#endif
|
1999-10-13 18:05:37 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
1999-10-25 00:13:47 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
2003-02-11 23:27:13 +01:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
1999-10-13 18:05:37 +02:00
|
|
|
#include <sys/ioctl.h>
|
2003-02-11 23:27:13 +01:00
|
|
|
#endif
|
1999-10-25 00:13:47 +02:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
# include <sys/filio.h>
|
|
|
|
#endif
|
1999-10-13 18:05:37 +02:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2014-05-06 16:49:52 +02:00
|
|
|
#include <limits.h>
|
2021-06-16 06:13:27 +02:00
|
|
|
#ifdef HAVE_LINUX_FILTER_H
|
|
|
|
# include <linux/filter.h>
|
|
|
|
#endif
|
2014-05-06 16:49:52 +02:00
|
|
|
#ifdef HAVE_LINUX_RTNETLINK_H
|
|
|
|
# include <linux/rtnetlink.h>
|
|
|
|
#endif
|
1999-10-13 18:05:37 +02:00
|
|
|
|
2020-09-30 17:37:50 +02:00
|
|
|
#ifdef HAVE_NETIPX_IPX_H
|
|
|
|
# include <netipx/ipx.h>
|
|
|
|
#elif defined(HAVE_LINUX_IPX_H)
|
|
|
|
# ifdef HAVE_ASM_TYPES_H
|
|
|
|
# include <asm/types.h>
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_LINUX_TYPES_H
|
|
|
|
# include <linux/types.h>
|
|
|
|
# endif
|
|
|
|
# include <linux/ipx.h>
|
|
|
|
#endif
|
|
|
|
#if defined(SOL_IPX) || defined(SO_DEFAULT_HEADERS)
|
|
|
|
# define HAS_IPX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_LINUX_IRDA_H
|
|
|
|
# ifdef HAVE_LINUX_TYPES_H
|
|
|
|
# include <linux/types.h>
|
|
|
|
# endif
|
|
|
|
# include <linux/irda.h>
|
|
|
|
# define HAS_IRDA
|
|
|
|
#endif
|
|
|
|
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#include "winternl.h"
|
2010-03-19 10:16:04 +01:00
|
|
|
#include "winerror.h"
|
2014-04-03 17:21:51 +02:00
|
|
|
#define USE_WS_PREFIX
|
|
|
|
#include "winsock2.h"
|
2020-10-19 18:21:18 +02:00
|
|
|
#include "ws2tcpip.h"
|
2020-09-30 17:37:50 +02:00
|
|
|
#include "wsipx.h"
|
2020-10-22 11:06:23 +02:00
|
|
|
#include "af_irda.h"
|
2020-09-20 00:13:36 +02:00
|
|
|
#include "wine/afd.h"
|
2003-01-30 01:26:44 +01:00
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
#include "process.h"
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "file.h"
|
1999-10-13 18:05:37 +02:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "request.h"
|
2002-03-23 21:43:52 +01:00
|
|
|
#include "user.h"
|
1999-10-13 18:05:37 +02:00
|
|
|
|
2021-06-16 06:13:27 +02:00
|
|
|
#if defined(linux) && !defined(IP_UNICAST_IF)
|
|
|
|
#define IP_UNICAST_IF 50
|
|
|
|
#endif
|
|
|
|
|
2021-06-17 06:33:10 +02:00
|
|
|
static const char magic_loopback_addr[] = {127, 12, 34, 56};
|
|
|
|
|
2021-06-16 06:13:28 +02:00
|
|
|
union win_sockaddr
|
|
|
|
{
|
|
|
|
struct WS_sockaddr addr;
|
|
|
|
struct WS_sockaddr_in in;
|
|
|
|
struct WS_sockaddr_in6 in6;
|
|
|
|
struct WS_sockaddr_ipx ipx;
|
|
|
|
SOCKADDR_IRDA irda;
|
|
|
|
};
|
|
|
|
|
2021-05-25 06:13:41 +02:00
|
|
|
static struct list poll_list = LIST_INIT( poll_list );
|
|
|
|
|
|
|
|
struct poll_req
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
struct async *async;
|
|
|
|
struct iosb *iosb;
|
|
|
|
struct timeout_user *timeout;
|
|
|
|
unsigned int count;
|
|
|
|
struct poll_socket_output *output;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
struct sock *sock;
|
|
|
|
int flags;
|
|
|
|
} sockets[1];
|
|
|
|
};
|
|
|
|
|
2020-10-19 18:21:18 +02:00
|
|
|
struct accept_req
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
struct async *async;
|
2021-02-09 00:40:39 +01:00
|
|
|
struct iosb *iosb;
|
2021-02-09 00:40:41 +01:00
|
|
|
struct sock *sock, *acceptsock;
|
2020-10-19 18:21:18 +02:00
|
|
|
int accepted;
|
|
|
|
unsigned int recv_len, local_len;
|
|
|
|
};
|
|
|
|
|
2021-05-18 04:29:59 +02:00
|
|
|
struct connect_req
|
|
|
|
{
|
|
|
|
struct async *async;
|
|
|
|
struct iosb *iosb;
|
|
|
|
struct sock *sock;
|
|
|
|
unsigned int addr_len, send_len, send_cursor;
|
|
|
|
};
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
enum connection_state
|
|
|
|
{
|
|
|
|
SOCK_LISTENING,
|
|
|
|
SOCK_UNCONNECTED,
|
|
|
|
SOCK_CONNECTING,
|
|
|
|
SOCK_CONNECTED,
|
|
|
|
SOCK_CONNECTIONLESS,
|
|
|
|
};
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
struct sock
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
2003-02-19 01:33:32 +01:00
|
|
|
struct fd *fd; /* socket file descriptor */
|
2021-06-11 02:36:26 +02:00
|
|
|
enum connection_state state; /* connection state */
|
1999-10-13 18:05:37 +02:00
|
|
|
unsigned int mask; /* event mask */
|
2021-06-14 07:41:47 +02:00
|
|
|
/* pending AFD_POLL_* events which have not yet been reported to the application */
|
2021-05-12 04:19:49 +02:00
|
|
|
unsigned int pending_events;
|
2021-06-14 07:41:47 +02:00
|
|
|
/* AFD_POLL_* events which have already been reported and should not be
|
|
|
|
* selected for again until reset by a relevant call.
|
2021-05-12 04:19:49 +02:00
|
|
|
*
|
2021-06-14 07:41:47 +02:00
|
|
|
* For example, if AFD_POLL_READ is set here and not in pending_events, it
|
|
|
|
* has already been reported and consumed, and we should not report it
|
|
|
|
* again, even if POLLIN is signaled, until it is reset by e.g recv().
|
2021-05-12 04:19:49 +02:00
|
|
|
*
|
|
|
|
* If an event has been signaled and not consumed yet, it will be set in
|
|
|
|
* both pending_events and reported_events (as we should only ever report
|
|
|
|
* any event once until it is reset.) */
|
|
|
|
unsigned int reported_events;
|
2002-01-09 22:16:24 +01:00
|
|
|
unsigned int flags; /* socket flags */
|
2013-08-31 05:14:44 +02:00
|
|
|
unsigned short proto; /* socket protocol */
|
2002-07-03 23:04:09 +02:00
|
|
|
unsigned short type; /* socket type */
|
|
|
|
unsigned short family; /* socket family */
|
1999-10-13 18:05:37 +02:00
|
|
|
struct event *event; /* event object */
|
2002-03-23 21:43:52 +01:00
|
|
|
user_handle_t window; /* window to send the message to */
|
|
|
|
unsigned int message; /* message to send */
|
2002-07-31 21:26:03 +02:00
|
|
|
obj_handle_t wparam; /* message wparam (socket handle) */
|
2021-06-14 07:41:48 +02:00
|
|
|
int errors[AFD_POLL_BIT_COUNT]; /* event errors */
|
2014-11-12 03:42:14 +01:00
|
|
|
timeout_t connect_time;/* time the socket was connected */
|
2005-01-14 20:54:38 +01:00
|
|
|
struct sock *deferred; /* socket that waits for a deferred accept */
|
2017-07-04 15:26:19 +02:00
|
|
|
struct async_queue read_q; /* queue for asynchronous reads */
|
|
|
|
struct async_queue write_q; /* queue for asynchronous writes */
|
|
|
|
struct async_queue ifchange_q; /* queue for interface change notifications */
|
2020-10-19 18:21:18 +02:00
|
|
|
struct async_queue accept_q; /* queue for asynchronous accepts */
|
2021-05-18 04:29:59 +02:00
|
|
|
struct async_queue connect_q; /* queue for asynchronous connects */
|
2021-05-25 06:13:41 +02:00
|
|
|
struct async_queue poll_q; /* queue for asynchronous polls */
|
2014-05-06 16:39:18 +02:00
|
|
|
struct object *ifchange_obj; /* the interface change notification object */
|
|
|
|
struct list ifchange_entry; /* entry in ifchange notification list */
|
2020-10-19 18:21:18 +02:00
|
|
|
struct list accept_list; /* list of pending accept requests */
|
|
|
|
struct accept_req *accept_recv_req; /* pending accept-into request which will recv on this socket */
|
2021-05-18 04:29:59 +02:00
|
|
|
struct connect_req *connect_req; /* pending connection request */
|
2021-06-16 06:13:28 +02:00
|
|
|
union win_sockaddr addr; /* socket name */
|
|
|
|
int addr_len; /* socket name length */
|
2021-06-26 03:21:06 +02:00
|
|
|
unsigned int rcvbuf; /* advisory recv buffer size */
|
2021-06-30 06:25:40 +02:00
|
|
|
unsigned int sndbuf; /* advisory send buffer size */
|
2021-06-29 18:43:20 +02:00
|
|
|
unsigned int rcvtimeo; /* receive timeout in ms */
|
2021-06-30 06:25:43 +02:00
|
|
|
unsigned int sndtimeo; /* send timeout in ms */
|
2021-06-11 02:36:25 +02:00
|
|
|
unsigned int rd_shutdown : 1; /* is the read end shut down? */
|
|
|
|
unsigned int wr_shutdown : 1; /* is the write end shut down? */
|
2021-06-11 02:36:24 +02:00
|
|
|
unsigned int wr_shutdown_pending : 1; /* is a write shutdown pending? */
|
2021-07-26 18:53:43 +02:00
|
|
|
unsigned int hangup : 1; /* has the read end received a hangup? */
|
2021-07-26 18:53:45 +02:00
|
|
|
unsigned int aborted : 1; /* did we get a POLLERR or irregular POLLHUP? */
|
2021-06-11 02:36:24 +02:00
|
|
|
unsigned int nonblocking : 1; /* is the socket nonblocking? */
|
2021-06-16 06:13:28 +02:00
|
|
|
unsigned int bound : 1; /* is the socket bound? */
|
1999-10-13 18:05:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void sock_dump( struct object *obj, int verbose );
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *sock_get_fd( struct object *obj );
|
2021-02-09 00:40:40 +01:00
|
|
|
static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
|
1999-10-13 18:05:37 +02:00
|
|
|
static void sock_destroy( struct object *obj );
|
2017-06-30 15:08:09 +02:00
|
|
|
static struct object *sock_get_ifchange( struct sock *sock );
|
|
|
|
static void sock_release_ifchange( struct sock *sock );
|
2003-02-14 21:27:09 +01:00
|
|
|
|
|
|
|
static int sock_get_poll_events( struct fd *fd );
|
|
|
|
static void sock_poll_event( struct fd *fd, int event );
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type sock_get_fd_type( struct fd *fd );
|
2017-06-26 12:20:10 +02:00
|
|
|
static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
|
2017-02-15 22:12:45 +01:00
|
|
|
static void sock_queue_async( struct fd *fd, struct async *async, int type, int count );
|
2007-04-10 17:07:27 +02:00
|
|
|
static void sock_reselect_async( struct fd *fd, struct async_queue *queue );
|
2003-02-14 21:27:09 +01:00
|
|
|
|
2020-10-19 18:21:18 +02:00
|
|
|
static int accept_into_socket( struct sock *sock, struct sock *acceptsock );
|
2020-10-19 18:21:19 +02:00
|
|
|
static struct sock *accept_socket( struct sock *sock );
|
2010-03-22 19:56:21 +01:00
|
|
|
static int sock_get_ntstatus( int err );
|
2018-11-20 12:57:42 +01:00
|
|
|
static unsigned int sock_get_error( int err );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
|
|
|
static const struct object_ops sock_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sizeof(struct sock), /* size */
|
2021-02-04 13:44:18 +01:00
|
|
|
&file_type, /* type */
|
2000-01-01 01:56:27 +01:00
|
|
|
sock_dump, /* dump */
|
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2021-03-18 06:41:25 +01:00
|
|
|
default_fd_signaled, /* signaled */
|
2000-01-01 01:56:27 +01:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-02-19 01:33:32 +01:00
|
|
|
sock_get_fd, /* get_fd */
|
2021-02-05 12:10:44 +01:00
|
|
|
default_map_access, /* map_access */
|
2007-10-03 14:10:37 +02:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 16:52:31 +02:00
|
|
|
no_get_full_name, /* get_full_name */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2016-02-04 13:07:19 +01:00
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
2007-03-22 11:44:29 +01:00
|
|
|
no_open_file, /* open_file */
|
2019-03-25 14:37:09 +01:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2021-02-09 00:40:40 +01:00
|
|
|
sock_close_handle, /* close_handle */
|
2003-01-30 01:26:44 +01:00
|
|
|
sock_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops sock_fd_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sock_get_poll_events, /* get_poll_events */
|
|
|
|
sock_poll_event, /* poll_event */
|
2010-08-28 13:14:37 +02:00
|
|
|
sock_get_fd_type, /* get_fd_type */
|
2015-05-05 05:17:45 +02:00
|
|
|
no_fd_read, /* read */
|
|
|
|
no_fd_write, /* write */
|
|
|
|
no_fd_flush, /* flush */
|
2018-10-23 17:45:06 +02:00
|
|
|
default_fd_get_file_info, /* get_file_info */
|
2017-10-02 16:42:16 +02:00
|
|
|
no_fd_get_volume_info, /* get_volume_info */
|
2014-04-03 17:21:51 +02:00
|
|
|
sock_ioctl, /* ioctl */
|
2005-01-14 20:54:38 +01:00
|
|
|
sock_queue_async, /* queue_async */
|
2016-12-01 12:10:34 +01:00
|
|
|
sock_reselect_async /* reselect_async */
|
1999-10-13 18:05:37 +02:00
|
|
|
};
|
|
|
|
|
2020-10-19 18:21:18 +02:00
|
|
|
union unix_sockaddr
|
|
|
|
{
|
|
|
|
struct sockaddr addr;
|
|
|
|
struct sockaddr_in in;
|
|
|
|
struct sockaddr_in6 in6;
|
|
|
|
#ifdef HAS_IPX
|
|
|
|
struct sockaddr_ipx ipx;
|
|
|
|
#endif
|
|
|
|
#ifdef HAS_IRDA
|
|
|
|
struct sockaddr_irda irda;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sockaddr_from_unix( const union unix_sockaddr *uaddr, struct WS_sockaddr *wsaddr, socklen_t wsaddrlen )
|
|
|
|
{
|
|
|
|
memset( wsaddr, 0, wsaddrlen );
|
|
|
|
|
|
|
|
switch (uaddr->addr.sa_family)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct WS_sockaddr_in win = {0};
|
|
|
|
|
|
|
|
if (wsaddrlen < sizeof(win)) return -1;
|
|
|
|
win.sin_family = WS_AF_INET;
|
|
|
|
win.sin_port = uaddr->in.sin_port;
|
|
|
|
memcpy( &win.sin_addr, &uaddr->in.sin_addr, sizeof(win.sin_addr) );
|
|
|
|
memcpy( wsaddr, &win, sizeof(win) );
|
|
|
|
return sizeof(win);
|
|
|
|
}
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct WS_sockaddr_in6 win = {0};
|
|
|
|
|
2021-07-23 18:39:08 +02:00
|
|
|
if (wsaddrlen < sizeof(win)) return -1;
|
2020-10-19 18:21:18 +02:00
|
|
|
win.sin6_family = WS_AF_INET6;
|
|
|
|
win.sin6_port = uaddr->in6.sin6_port;
|
|
|
|
win.sin6_flowinfo = uaddr->in6.sin6_flowinfo;
|
|
|
|
memcpy( &win.sin6_addr, &uaddr->in6.sin6_addr, sizeof(win.sin6_addr) );
|
|
|
|
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
|
|
|
|
win.sin6_scope_id = uaddr->in6.sin6_scope_id;
|
|
|
|
#endif
|
2021-07-23 18:39:08 +02:00
|
|
|
memcpy( wsaddr, &win, sizeof(win) );
|
|
|
|
return sizeof(win);
|
2020-10-19 18:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_IPX
|
|
|
|
case AF_IPX:
|
|
|
|
{
|
|
|
|
struct WS_sockaddr_ipx win = {0};
|
|
|
|
|
|
|
|
if (wsaddrlen < sizeof(win)) return -1;
|
|
|
|
win.sa_family = WS_AF_IPX;
|
|
|
|
memcpy( win.sa_netnum, &uaddr->ipx.sipx_network, sizeof(win.sa_netnum) );
|
|
|
|
memcpy( win.sa_nodenum, &uaddr->ipx.sipx_node, sizeof(win.sa_nodenum) );
|
|
|
|
win.sa_socket = uaddr->ipx.sipx_port;
|
|
|
|
memcpy( wsaddr, &win, sizeof(win) );
|
|
|
|
return sizeof(win);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_IRDA
|
|
|
|
case AF_IRDA:
|
|
|
|
{
|
|
|
|
SOCKADDR_IRDA win;
|
|
|
|
|
|
|
|
if (wsaddrlen < sizeof(win)) return -1;
|
|
|
|
win.irdaAddressFamily = WS_AF_IRDA;
|
|
|
|
memcpy( win.irdaDeviceID, &uaddr->irda.sir_addr, sizeof(win.irdaDeviceID) );
|
|
|
|
if (uaddr->irda.sir_lsap_sel != LSAP_ANY)
|
|
|
|
snprintf( win.irdaServiceName, sizeof(win.irdaServiceName), "LSAP-SEL%u", uaddr->irda.sir_lsap_sel );
|
|
|
|
else
|
|
|
|
memcpy( win.irdaServiceName, uaddr->irda.sir_name, sizeof(win.irdaServiceName) );
|
|
|
|
memcpy( wsaddr, &win, sizeof(win) );
|
|
|
|
return sizeof(win);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case AF_UNSPEC:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2002-03-23 21:43:52 +01:00
|
|
|
|
2021-06-16 06:13:27 +02:00
|
|
|
static socklen_t sockaddr_to_unix( const struct WS_sockaddr *wsaddr, int wsaddrlen, union unix_sockaddr *uaddr )
|
|
|
|
{
|
|
|
|
memset( uaddr, 0, sizeof(*uaddr) );
|
|
|
|
|
|
|
|
switch (wsaddr->sa_family)
|
|
|
|
{
|
|
|
|
case WS_AF_INET:
|
|
|
|
{
|
|
|
|
struct WS_sockaddr_in win = {0};
|
|
|
|
|
|
|
|
if (wsaddrlen < sizeof(win)) return 0;
|
|
|
|
memcpy( &win, wsaddr, sizeof(win) );
|
|
|
|
uaddr->in.sin_family = AF_INET;
|
|
|
|
uaddr->in.sin_port = win.sin_port;
|
|
|
|
memcpy( &uaddr->in.sin_addr, &win.sin_addr, sizeof(win.sin_addr) );
|
|
|
|
return sizeof(uaddr->in);
|
|
|
|
}
|
|
|
|
|
|
|
|
case WS_AF_INET6:
|
|
|
|
{
|
|
|
|
struct WS_sockaddr_in6 win = {0};
|
|
|
|
|
2021-07-23 18:39:08 +02:00
|
|
|
if (wsaddrlen < sizeof(win)) return 0;
|
|
|
|
memcpy( &win, wsaddr, sizeof(win) );
|
2021-06-16 06:13:27 +02:00
|
|
|
uaddr->in6.sin6_family = AF_INET6;
|
|
|
|
uaddr->in6.sin6_port = win.sin6_port;
|
|
|
|
uaddr->in6.sin6_flowinfo = win.sin6_flowinfo;
|
|
|
|
memcpy( &uaddr->in6.sin6_addr, &win.sin6_addr, sizeof(win.sin6_addr) );
|
|
|
|
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
|
2021-07-23 18:39:08 +02:00
|
|
|
uaddr->in6.sin6_scope_id = win.sin6_scope_id;
|
2021-06-16 06:13:27 +02:00
|
|
|
#endif
|
|
|
|
return sizeof(uaddr->in6);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_IPX
|
|
|
|
case WS_AF_IPX:
|
|
|
|
{
|
|
|
|
struct WS_sockaddr_ipx win = {0};
|
|
|
|
|
|
|
|
if (wsaddrlen < sizeof(win)) return 0;
|
|
|
|
memcpy( &win, wsaddr, sizeof(win) );
|
|
|
|
uaddr->ipx.sipx_family = AF_IPX;
|
|
|
|
memcpy( &uaddr->ipx.sipx_network, win.sa_netnum, sizeof(win.sa_netnum) );
|
|
|
|
memcpy( &uaddr->ipx.sipx_node, win.sa_nodenum, sizeof(win.sa_nodenum) );
|
|
|
|
uaddr->ipx.sipx_port = win.sa_socket;
|
|
|
|
return sizeof(uaddr->ipx);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_IRDA
|
|
|
|
case WS_AF_IRDA:
|
|
|
|
{
|
|
|
|
SOCKADDR_IRDA win = {0};
|
|
|
|
unsigned int lsap_sel;
|
|
|
|
|
|
|
|
if (wsaddrlen < sizeof(win)) return 0;
|
|
|
|
memcpy( &win, wsaddr, sizeof(win) );
|
|
|
|
uaddr->irda.sir_family = AF_IRDA;
|
|
|
|
if (sscanf( win.irdaServiceName, "LSAP-SEL%u", &lsap_sel ) == 1)
|
2021-06-17 06:33:08 +02:00
|
|
|
uaddr->irda.sir_lsap_sel = lsap_sel;
|
2021-06-16 06:13:27 +02:00
|
|
|
else
|
|
|
|
{
|
2021-06-17 06:33:08 +02:00
|
|
|
uaddr->irda.sir_lsap_sel = LSAP_ANY;
|
2021-06-16 06:13:27 +02:00
|
|
|
memcpy( uaddr->irda.sir_name, win.irdaServiceName, sizeof(win.irdaServiceName) );
|
|
|
|
}
|
|
|
|
memcpy( &uaddr->irda.sir_addr, win.irdaDeviceID, sizeof(win.irdaDeviceID) );
|
|
|
|
return sizeof(uaddr->irda);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case WS_AF_UNSPEC:
|
|
|
|
switch (wsaddrlen)
|
|
|
|
{
|
|
|
|
default: /* likely an ipv4 address */
|
|
|
|
case sizeof(struct WS_sockaddr_in):
|
|
|
|
return sizeof(uaddr->in);
|
|
|
|
|
|
|
|
#ifdef HAS_IPX
|
|
|
|
case sizeof(struct WS_sockaddr_ipx):
|
|
|
|
return sizeof(uaddr->ipx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_IRDA
|
|
|
|
case sizeof(SOCKADDR_IRDA):
|
|
|
|
return sizeof(uaddr->irda);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case sizeof(struct WS_sockaddr_in6):
|
|
|
|
return sizeof(uaddr->in6);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 07:41:47 +02:00
|
|
|
/* some events are generated at the same time but must be sent in a particular
|
|
|
|
* order (e.g. CONNECT must be sent before READ) */
|
|
|
|
static const enum afd_poll_bit event_bitorder[] =
|
|
|
|
{
|
|
|
|
AFD_POLL_BIT_CONNECT,
|
|
|
|
AFD_POLL_BIT_CONNECT_ERR,
|
|
|
|
AFD_POLL_BIT_ACCEPT,
|
|
|
|
AFD_POLL_BIT_OOB,
|
|
|
|
AFD_POLL_BIT_WRITE,
|
|
|
|
AFD_POLL_BIT_READ,
|
|
|
|
AFD_POLL_BIT_RESET,
|
|
|
|
AFD_POLL_BIT_HUP,
|
|
|
|
AFD_POLL_BIT_CLOSE,
|
2002-03-23 21:43:52 +01:00
|
|
|
};
|
|
|
|
|
2002-07-03 23:04:09 +02:00
|
|
|
typedef enum {
|
|
|
|
SOCK_SHUTDOWN_ERROR = -1,
|
|
|
|
SOCK_SHUTDOWN_EOF = 0,
|
|
|
|
SOCK_SHUTDOWN_POLLHUP = 1
|
|
|
|
} sock_shutdown_t;
|
|
|
|
|
|
|
|
static sock_shutdown_t sock_shutdown_type = SOCK_SHUTDOWN_ERROR;
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
static sock_shutdown_t sock_check_pollhup(void)
|
2002-07-03 23:04:09 +02:00
|
|
|
{
|
|
|
|
sock_shutdown_t ret = SOCK_SHUTDOWN_ERROR;
|
|
|
|
int fd[2], n;
|
|
|
|
struct pollfd pfd;
|
|
|
|
char dummy;
|
|
|
|
|
2013-12-31 17:50:56 +01:00
|
|
|
if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) return ret;
|
2005-01-14 20:54:38 +01:00
|
|
|
if ( shutdown( fd[0], 1 ) ) goto out;
|
2002-07-03 23:04:09 +02:00
|
|
|
|
|
|
|
pfd.fd = fd[1];
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
pfd.revents = 0;
|
|
|
|
|
2010-11-26 18:08:30 +01:00
|
|
|
/* Solaris' poll() sometimes returns nothing if given a 0ms timeout here */
|
|
|
|
n = poll( &pfd, 1, 1 );
|
2002-07-03 23:04:09 +02:00
|
|
|
if ( n != 1 ) goto out; /* error or timeout */
|
|
|
|
if ( pfd.revents & POLLHUP )
|
|
|
|
ret = SOCK_SHUTDOWN_POLLHUP;
|
|
|
|
else if ( pfd.revents & POLLIN &&
|
2005-01-14 20:54:38 +01:00
|
|
|
read( fd[1], &dummy, 1 ) == 0 )
|
2002-07-03 23:04:09 +02:00
|
|
|
ret = SOCK_SHUTDOWN_EOF;
|
|
|
|
|
|
|
|
out:
|
2005-01-14 20:54:38 +01:00
|
|
|
close( fd[0] );
|
|
|
|
close( fd[1] );
|
2002-07-03 23:04:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sock_init(void)
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
sock_shutdown_type = sock_check_pollhup();
|
2002-07-03 23:04:09 +02:00
|
|
|
|
|
|
|
switch ( sock_shutdown_type )
|
|
|
|
{
|
|
|
|
case SOCK_SHUTDOWN_EOF:
|
2005-01-14 20:54:38 +01:00
|
|
|
if (debug_level) fprintf( stderr, "sock_init: shutdown() causes EOF\n" );
|
2002-07-03 23:04:09 +02:00
|
|
|
break;
|
|
|
|
case SOCK_SHUTDOWN_POLLHUP:
|
2005-01-14 20:54:38 +01:00
|
|
|
if (debug_level) fprintf( stderr, "sock_init: shutdown() causes POLLHUP\n" );
|
2002-07-03 23:04:09 +02:00
|
|
|
break;
|
|
|
|
default:
|
2005-01-14 20:54:38 +01:00
|
|
|
fprintf( stderr, "sock_init: ERROR in sock_check_pollhup()\n" );
|
2002-07-03 23:04:09 +02:00
|
|
|
sock_shutdown_type = SOCK_SHUTDOWN_EOF;
|
|
|
|
}
|
|
|
|
}
|
2002-03-23 21:43:52 +01:00
|
|
|
|
2002-04-25 23:31:11 +02:00
|
|
|
static int sock_reselect( struct sock *sock )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
int ev = sock_get_poll_events( sock->fd );
|
2000-03-25 22:41:43 +01:00
|
|
|
|
1999-10-23 18:53:34 +02:00
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
fprintf(stderr,"sock_reselect(%p): new mask %x\n", sock, ev);
|
2000-09-06 21:40:23 +02:00
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
set_fd_events( sock->fd, ev );
|
2002-04-25 23:31:11 +02:00
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2021-06-14 07:41:47 +02:00
|
|
|
static unsigned int afd_poll_flag_to_win32( unsigned int flags )
|
|
|
|
{
|
|
|
|
static const unsigned int map[] =
|
|
|
|
{
|
|
|
|
FD_READ, /* READ */
|
|
|
|
FD_OOB, /* OOB */
|
|
|
|
FD_WRITE, /* WRITE */
|
|
|
|
FD_CLOSE, /* HUP */
|
|
|
|
FD_CLOSE, /* RESET */
|
|
|
|
0, /* CLOSE */
|
|
|
|
FD_CONNECT, /* CONNECT */
|
|
|
|
FD_ACCEPT, /* ACCEPT */
|
|
|
|
FD_CONNECT, /* CONNECT_ERR */
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int i, ret = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(map); ++i)
|
|
|
|
{
|
|
|
|
if (flags & (1 << i)) ret |= map[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-03-23 21:43:52 +01:00
|
|
|
/* wake anybody waiting on the socket event or send the associated message */
|
2010-05-15 07:18:20 +02:00
|
|
|
static void sock_wake_up( struct sock *sock )
|
2002-03-23 21:43:52 +01:00
|
|
|
{
|
2021-05-12 04:19:48 +02:00
|
|
|
unsigned int events = sock->pending_events & sock->mask;
|
2002-03-23 21:43:52 +01:00
|
|
|
int i;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
2002-03-23 21:43:52 +01:00
|
|
|
if (sock->event)
|
|
|
|
{
|
|
|
|
if (debug_level) fprintf(stderr, "signalling events %x ptr %p\n", events, sock->event );
|
2021-05-14 05:54:36 +02:00
|
|
|
if (events)
|
|
|
|
set_event( sock->event );
|
2002-03-23 21:43:52 +01:00
|
|
|
}
|
|
|
|
if (sock->window)
|
|
|
|
{
|
2008-12-08 16:57:38 +01:00
|
|
|
if (debug_level) fprintf(stderr, "signalling events %x win %08x\n", events, sock->window );
|
2021-06-14 07:41:47 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(event_bitorder); i++)
|
2002-03-23 21:43:52 +01:00
|
|
|
{
|
2021-06-14 07:41:47 +02:00
|
|
|
enum afd_poll_bit event = event_bitorder[i];
|
2021-05-13 04:59:23 +02:00
|
|
|
if (events & (1 << event))
|
2002-03-23 21:43:52 +01:00
|
|
|
{
|
2021-06-14 07:41:48 +02:00
|
|
|
lparam_t lparam = afd_poll_flag_to_win32(1 << event) | (sock_get_error( sock->errors[event] ) << 16);
|
2008-12-10 16:01:50 +01:00
|
|
|
post_message( sock->window, sock->message, sock->wparam, lparam );
|
2002-03-23 21:43:52 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 04:19:48 +02:00
|
|
|
sock->pending_events = 0;
|
2002-03-23 21:43:52 +01:00
|
|
|
sock_reselect( sock );
|
|
|
|
}
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2007-03-17 11:52:14 +01:00
|
|
|
static inline int sock_error( struct fd *fd )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2013-03-06 13:40:38 +01:00
|
|
|
unsigned int optval = 0;
|
|
|
|
socklen_t optlen = sizeof(optval);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
|
2010-03-22 19:56:21 +01:00
|
|
|
return optval;
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 00:40:42 +01:00
|
|
|
static void free_accept_req( void *private )
|
2020-10-19 18:21:18 +02:00
|
|
|
{
|
2021-02-09 00:40:42 +01:00
|
|
|
struct accept_req *req = private;
|
2020-10-19 18:21:18 +02:00
|
|
|
list_remove( &req->entry );
|
2021-02-09 00:40:41 +01:00
|
|
|
if (req->acceptsock)
|
|
|
|
{
|
|
|
|
req->acceptsock->accept_recv_req = NULL;
|
|
|
|
release_object( req->acceptsock );
|
|
|
|
}
|
2020-10-19 18:21:18 +02:00
|
|
|
release_object( req->async );
|
2021-02-09 00:40:39 +01:00
|
|
|
release_object( req->iosb );
|
2021-02-09 00:40:41 +01:00
|
|
|
release_object( req->sock );
|
2020-10-19 18:21:18 +02:00
|
|
|
free( req );
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:40:39 +01:00
|
|
|
static void fill_accept_output( struct accept_req *req )
|
2020-10-19 18:21:18 +02:00
|
|
|
{
|
2021-02-09 00:40:39 +01:00
|
|
|
struct iosb *iosb = req->iosb;
|
2020-10-19 18:21:18 +02:00
|
|
|
union unix_sockaddr unix_addr;
|
|
|
|
struct WS_sockaddr *win_addr;
|
2021-01-01 19:01:36 +01:00
|
|
|
unsigned int remote_len;
|
2020-10-19 18:21:18 +02:00
|
|
|
socklen_t unix_len;
|
|
|
|
int fd, size = 0;
|
|
|
|
char *out_data;
|
|
|
|
int win_len;
|
|
|
|
|
|
|
|
if (!(out_data = mem_alloc( iosb->out_size ))) return;
|
|
|
|
|
|
|
|
fd = get_unix_fd( req->acceptsock->fd );
|
|
|
|
|
|
|
|
if (req->recv_len && (size = recv( fd, out_data, req->recv_len, 0 )) < 0)
|
|
|
|
{
|
|
|
|
if (!req->accepted && errno == EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
req->accepted = 1;
|
|
|
|
sock_reselect( req->acceptsock );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-05 04:42:31 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
2020-10-19 18:21:18 +02:00
|
|
|
free( out_data );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->local_len)
|
|
|
|
{
|
|
|
|
if (req->local_len < sizeof(int))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
free( out_data );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unix_len = sizeof(unix_addr);
|
|
|
|
win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + sizeof(int));
|
|
|
|
if (getsockname( fd, &unix_addr.addr, &unix_len ) < 0 ||
|
2021-01-01 19:01:36 +01:00
|
|
|
(win_len = sockaddr_from_unix( &unix_addr, win_addr, req->local_len - sizeof(int) )) < 0)
|
2020-10-19 18:21:18 +02:00
|
|
|
{
|
2021-05-05 04:42:31 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
2020-10-19 18:21:18 +02:00
|
|
|
free( out_data );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy( out_data + req->recv_len, &win_len, sizeof(int) );
|
|
|
|
}
|
|
|
|
|
|
|
|
unix_len = sizeof(unix_addr);
|
|
|
|
win_addr = (struct WS_sockaddr *)(out_data + req->recv_len + req->local_len + sizeof(int));
|
2021-01-01 19:01:36 +01:00
|
|
|
remote_len = iosb->out_size - req->recv_len - req->local_len;
|
2020-10-19 18:21:18 +02:00
|
|
|
if (getpeername( fd, &unix_addr.addr, &unix_len ) < 0 ||
|
2021-01-01 19:01:36 +01:00
|
|
|
(win_len = sockaddr_from_unix( &unix_addr, win_addr, remote_len - sizeof(int) )) < 0)
|
2020-10-19 18:21:18 +02:00
|
|
|
{
|
2021-05-05 04:42:31 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
2020-10-19 18:21:18 +02:00
|
|
|
free( out_data );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy( out_data + req->recv_len + req->local_len, &win_len, sizeof(int) );
|
|
|
|
|
|
|
|
iosb->status = STATUS_SUCCESS;
|
|
|
|
iosb->result = size;
|
|
|
|
iosb->out_data = out_data;
|
|
|
|
set_error( STATUS_ALERTED );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void complete_async_accept( struct sock *sock, struct accept_req *req )
|
|
|
|
{
|
|
|
|
struct sock *acceptsock = req->acceptsock;
|
|
|
|
struct async *async = req->async;
|
|
|
|
|
|
|
|
if (debug_level) fprintf( stderr, "completing accept request for socket %p\n", sock );
|
|
|
|
|
2020-10-19 18:21:19 +02:00
|
|
|
if (acceptsock)
|
|
|
|
{
|
|
|
|
if (!accept_into_socket( sock, acceptsock )) return;
|
2021-02-09 00:40:39 +01:00
|
|
|
fill_accept_output( req );
|
2020-10-19 18:21:19 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-09 00:40:39 +01:00
|
|
|
struct iosb *iosb = req->iosb;
|
2020-10-19 18:21:19 +02:00
|
|
|
obj_handle_t handle;
|
|
|
|
|
|
|
|
if (!(acceptsock = accept_socket( sock ))) return;
|
|
|
|
handle = alloc_handle_no_access_check( async_get_thread( async )->process, &acceptsock->obj,
|
|
|
|
GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
|
|
|
|
acceptsock->wparam = handle;
|
|
|
|
release_object( acceptsock );
|
|
|
|
if (!handle) return;
|
|
|
|
|
2021-02-09 00:40:39 +01:00
|
|
|
if (!(iosb->out_data = malloc( sizeof(handle) ))) return;
|
|
|
|
|
2020-10-19 18:21:19 +02:00
|
|
|
iosb->status = STATUS_SUCCESS;
|
|
|
|
iosb->out_size = sizeof(handle);
|
|
|
|
memcpy( iosb->out_data, &handle, sizeof(handle) );
|
|
|
|
set_error( STATUS_ALERTED );
|
|
|
|
}
|
2020-10-19 18:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void complete_async_accept_recv( struct accept_req *req )
|
|
|
|
{
|
|
|
|
if (debug_level) fprintf( stderr, "completing accept recv request for socket %p\n", req->acceptsock );
|
|
|
|
|
|
|
|
assert( req->recv_len );
|
|
|
|
|
2021-02-09 00:40:39 +01:00
|
|
|
fill_accept_output( req );
|
2020-10-19 18:21:18 +02:00
|
|
|
}
|
|
|
|
|
2021-05-18 04:29:59 +02:00
|
|
|
static void free_connect_req( void *private )
|
|
|
|
{
|
|
|
|
struct connect_req *req = private;
|
|
|
|
|
|
|
|
req->sock->connect_req = NULL;
|
|
|
|
release_object( req->async );
|
|
|
|
release_object( req->iosb );
|
|
|
|
release_object( req->sock );
|
|
|
|
free( req );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void complete_async_connect( struct sock *sock )
|
|
|
|
{
|
|
|
|
struct connect_req *req = sock->connect_req;
|
|
|
|
const char *in_buffer;
|
|
|
|
struct iosb *iosb;
|
|
|
|
size_t len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (debug_level) fprintf( stderr, "completing connect request for socket %p\n", sock );
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = SOCK_CONNECTED;
|
2021-05-18 04:29:59 +02:00
|
|
|
|
|
|
|
if (!req->send_len)
|
|
|
|
{
|
|
|
|
set_error( STATUS_SUCCESS );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iosb = req->iosb;
|
|
|
|
in_buffer = (const char *)iosb->in_data + sizeof(struct afd_connect_params) + req->addr_len;
|
|
|
|
len = req->send_len - req->send_cursor;
|
|
|
|
|
|
|
|
ret = send( get_unix_fd( sock->fd ), in_buffer + req->send_cursor, len, 0 );
|
|
|
|
if (ret < 0 && errno != EWOULDBLOCK)
|
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
|
|
|
else if (ret == len)
|
|
|
|
{
|
|
|
|
iosb->result = req->send_len;
|
|
|
|
iosb->status = STATUS_SUCCESS;
|
|
|
|
set_error( STATUS_ALERTED );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
req->send_cursor += ret;
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 06:13:41 +02:00
|
|
|
static void free_poll_req( void *private )
|
|
|
|
{
|
|
|
|
struct poll_req *req = private;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (req->timeout) remove_timeout_user( req->timeout );
|
|
|
|
|
|
|
|
for (i = 0; i < req->count; ++i)
|
|
|
|
release_object( req->sockets[i].sock );
|
|
|
|
release_object( req->async );
|
|
|
|
release_object( req->iosb );
|
|
|
|
list_remove( &req->entry );
|
|
|
|
free( req );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_oobinline( struct sock *sock )
|
|
|
|
{
|
|
|
|
int oobinline;
|
|
|
|
socklen_t len = sizeof(oobinline);
|
|
|
|
return !getsockopt( get_unix_fd( sock->fd ), SOL_SOCKET, SO_OOBINLINE, (char *)&oobinline, &len ) && oobinline;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_poll_flags( struct sock *sock, int event )
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
/* A connection-mode socket which has never been connected does not return
|
|
|
|
* write or hangup events, but Linux reports POLLOUT | POLLHUP. */
|
2021-06-11 02:36:26 +02:00
|
|
|
if (sock->state == SOCK_UNCONNECTED)
|
2021-05-25 06:13:41 +02:00
|
|
|
event &= ~(POLLOUT | POLLHUP);
|
|
|
|
|
|
|
|
if (event & POLLIN)
|
|
|
|
{
|
2021-06-11 02:36:26 +02:00
|
|
|
if (sock->state == SOCK_LISTENING)
|
2021-05-25 06:13:41 +02:00
|
|
|
flags |= AFD_POLL_ACCEPT;
|
|
|
|
else
|
|
|
|
flags |= AFD_POLL_READ;
|
|
|
|
}
|
|
|
|
if (event & POLLPRI)
|
|
|
|
flags |= is_oobinline( sock ) ? AFD_POLL_READ : AFD_POLL_OOB;
|
|
|
|
if (event & POLLOUT)
|
|
|
|
flags |= AFD_POLL_WRITE;
|
2021-06-11 02:36:26 +02:00
|
|
|
if (sock->state == SOCK_CONNECTED)
|
2021-05-25 06:13:41 +02:00
|
|
|
flags |= AFD_POLL_CONNECT;
|
|
|
|
if (event & POLLHUP)
|
|
|
|
flags |= AFD_POLL_HUP;
|
|
|
|
if (event & POLLERR)
|
|
|
|
flags |= AFD_POLL_CONNECT_ERR;
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void complete_async_polls( struct sock *sock, int event, int error )
|
|
|
|
{
|
|
|
|
int flags = get_poll_flags( sock, event );
|
|
|
|
struct poll_req *req, *next;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( req, next, &poll_list, struct poll_req, entry )
|
|
|
|
{
|
|
|
|
struct iosb *iosb = req->iosb;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (iosb->status != STATUS_PENDING) continue;
|
|
|
|
|
|
|
|
for (i = 0; i < req->count; ++i)
|
|
|
|
{
|
|
|
|
if (req->sockets[i].sock != sock) continue;
|
|
|
|
if (!(req->sockets[i].flags & flags)) continue;
|
|
|
|
|
|
|
|
if (debug_level)
|
|
|
|
fprintf( stderr, "completing poll for socket %p, wanted %#x got %#x\n",
|
|
|
|
sock, req->sockets[i].flags, flags );
|
|
|
|
|
|
|
|
req->output[i].flags = req->sockets[i].flags & flags;
|
|
|
|
req->output[i].status = sock_get_ntstatus( error );
|
|
|
|
|
|
|
|
iosb->status = STATUS_SUCCESS;
|
|
|
|
iosb->out_data = req->output;
|
|
|
|
iosb->out_size = req->count * sizeof(*req->output);
|
|
|
|
async_terminate( req->async, STATUS_ALERTED );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void async_poll_timeout( void *private )
|
|
|
|
{
|
|
|
|
struct poll_req *req = private;
|
|
|
|
struct iosb *iosb = req->iosb;
|
|
|
|
|
|
|
|
req->timeout = NULL;
|
|
|
|
|
|
|
|
if (iosb->status != STATUS_PENDING) return;
|
|
|
|
|
|
|
|
iosb->status = STATUS_TIMEOUT;
|
|
|
|
iosb->out_data = req->output;
|
|
|
|
iosb->out_size = req->count * sizeof(*req->output);
|
|
|
|
async_terminate( req->async, STATUS_ALERTED );
|
|
|
|
}
|
|
|
|
|
2010-07-27 06:16:18 +02:00
|
|
|
static int sock_dispatch_asyncs( struct sock *sock, int event, int error )
|
2010-05-19 22:14:08 +02:00
|
|
|
{
|
2020-10-19 18:21:18 +02:00
|
|
|
if (event & (POLLIN | POLLPRI))
|
|
|
|
{
|
|
|
|
struct accept_req *req;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( req, &sock->accept_list, struct accept_req, entry )
|
|
|
|
{
|
2021-02-09 00:40:42 +01:00
|
|
|
if (req->iosb->status == STATUS_PENDING && !req->accepted)
|
2020-10-19 18:21:18 +02:00
|
|
|
{
|
|
|
|
complete_async_accept( sock, req );
|
|
|
|
if (get_error() != STATUS_PENDING)
|
|
|
|
async_terminate( req->async, get_error() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:40:42 +01:00
|
|
|
if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
|
2020-10-19 18:21:18 +02:00
|
|
|
{
|
|
|
|
complete_async_accept_recv( sock->accept_recv_req );
|
|
|
|
if (get_error() != STATUS_PENDING)
|
|
|
|
async_terminate( sock->accept_recv_req->async, get_error() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 04:29:59 +02:00
|
|
|
if ((event & POLLOUT) && sock->connect_req && sock->connect_req->iosb->status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
complete_async_connect( sock );
|
|
|
|
if (get_error() != STATUS_PENDING)
|
|
|
|
async_terminate( sock->connect_req->async, get_error() );
|
|
|
|
}
|
|
|
|
|
2021-05-22 05:08:48 +02:00
|
|
|
if (event & (POLLIN | POLLPRI) && async_waiting( &sock->read_q ))
|
|
|
|
{
|
|
|
|
if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
|
|
|
|
async_wake_up( &sock->read_q, STATUS_ALERTED );
|
|
|
|
event &= ~(POLLIN | POLLPRI);
|
|
|
|
}
|
|
|
|
|
2021-05-27 06:36:53 +02:00
|
|
|
if (event & POLLOUT && async_waiting( &sock->write_q ))
|
2010-05-19 22:14:08 +02:00
|
|
|
{
|
2021-05-27 06:36:53 +02:00
|
|
|
if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
|
|
|
|
async_wake_up( &sock->write_q, STATUS_ALERTED );
|
|
|
|
event &= ~POLLOUT;
|
2020-10-19 18:21:18 +02:00
|
|
|
}
|
2010-07-18 04:06:58 +02:00
|
|
|
|
2020-10-19 18:21:18 +02:00
|
|
|
if (event & (POLLERR | POLLHUP))
|
|
|
|
{
|
|
|
|
int status = sock_get_ntstatus( error );
|
|
|
|
struct accept_req *req, *next;
|
|
|
|
|
2021-07-26 18:53:43 +02:00
|
|
|
if (sock->rd_shutdown || sock->hangup)
|
2020-10-19 18:21:18 +02:00
|
|
|
async_wake_up( &sock->read_q, status );
|
2021-06-11 02:36:25 +02:00
|
|
|
if (sock->wr_shutdown)
|
2020-10-19 18:21:18 +02:00
|
|
|
async_wake_up( &sock->write_q, status );
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( req, next, &sock->accept_list, struct accept_req, entry )
|
2021-02-09 00:40:42 +01:00
|
|
|
{
|
|
|
|
if (req->iosb->status == STATUS_PENDING)
|
|
|
|
async_terminate( req->async, status );
|
|
|
|
}
|
2020-10-19 18:21:18 +02:00
|
|
|
|
2021-02-09 00:40:42 +01:00
|
|
|
if (sock->accept_recv_req && sock->accept_recv_req->iosb->status == STATUS_PENDING)
|
2020-10-19 18:21:18 +02:00
|
|
|
async_terminate( sock->accept_recv_req->async, status );
|
2021-05-18 04:29:59 +02:00
|
|
|
|
|
|
|
if (sock->connect_req)
|
|
|
|
async_terminate( sock->connect_req->async, status );
|
2010-05-19 22:14:08 +02:00
|
|
|
}
|
2020-10-19 18:21:18 +02:00
|
|
|
|
2010-07-27 06:16:18 +02:00
|
|
|
return event;
|
2010-05-19 22:14:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 07:41:48 +02:00
|
|
|
static void post_socket_event( struct sock *sock, enum afd_poll_bit event_bit, int error )
|
2021-05-12 04:19:47 +02:00
|
|
|
{
|
|
|
|
unsigned int event = (1 << event_bit);
|
|
|
|
|
2021-05-13 04:59:25 +02:00
|
|
|
if (!(sock->reported_events & event))
|
|
|
|
{
|
|
|
|
sock->pending_events |= event;
|
|
|
|
sock->reported_events |= event;
|
|
|
|
sock->errors[event_bit] = error;
|
|
|
|
}
|
2021-05-12 04:19:47 +02:00
|
|
|
}
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
static void sock_dispatch_events( struct sock *sock, enum connection_state prevstate, int event, int error )
|
2010-07-18 04:06:49 +02:00
|
|
|
{
|
2021-06-11 02:36:26 +02:00
|
|
|
switch (prevstate)
|
2010-07-18 04:06:49 +02:00
|
|
|
{
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_UNCONNECTED:
|
|
|
|
break;
|
2010-07-18 04:06:49 +02:00
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_CONNECTING:
|
2021-06-14 07:41:47 +02:00
|
|
|
if (event & POLLOUT)
|
2021-07-07 06:48:41 +02:00
|
|
|
{
|
2021-06-14 07:41:47 +02:00
|
|
|
post_socket_event( sock, AFD_POLL_BIT_CONNECT, 0 );
|
2021-07-07 06:48:41 +02:00
|
|
|
sock->errors[AFD_POLL_BIT_CONNECT_ERR] = 0;
|
|
|
|
}
|
2021-06-14 07:41:47 +02:00
|
|
|
if (event & (POLLERR | POLLHUP))
|
2021-06-14 07:41:48 +02:00
|
|
|
post_socket_event( sock, AFD_POLL_BIT_CONNECT_ERR, error );
|
2021-06-11 02:36:26 +02:00
|
|
|
break;
|
2010-07-18 04:06:49 +02:00
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_LISTENING:
|
|
|
|
if (event & (POLLIN | POLLERR | POLLHUP))
|
2021-06-14 07:41:48 +02:00
|
|
|
post_socket_event( sock, AFD_POLL_BIT_ACCEPT, error );
|
2021-06-11 02:36:26 +02:00
|
|
|
break;
|
2010-07-18 04:06:49 +02:00
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_CONNECTED:
|
|
|
|
case SOCK_CONNECTIONLESS:
|
|
|
|
if (event & POLLIN)
|
2021-06-14 07:41:47 +02:00
|
|
|
post_socket_event( sock, AFD_POLL_BIT_READ, 0 );
|
2021-06-11 02:36:26 +02:00
|
|
|
|
|
|
|
if (event & POLLOUT)
|
2021-06-14 07:41:47 +02:00
|
|
|
post_socket_event( sock, AFD_POLL_BIT_WRITE, 0 );
|
2010-07-18 04:06:49 +02:00
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
if (event & POLLPRI)
|
2021-06-14 07:41:47 +02:00
|
|
|
post_socket_event( sock, AFD_POLL_BIT_OOB, 0 );
|
2021-06-11 02:36:26 +02:00
|
|
|
|
|
|
|
if (event & (POLLERR | POLLHUP))
|
2021-06-14 07:41:48 +02:00
|
|
|
post_socket_event( sock, AFD_POLL_BIT_HUP, error );
|
2021-06-11 02:36:26 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-05-12 04:19:47 +02:00
|
|
|
|
2010-07-18 04:06:49 +02:00
|
|
|
sock_wake_up( sock );
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static void sock_poll_event( struct fd *fd, int event )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct sock *sock = get_fd_user( fd );
|
2002-07-03 23:04:09 +02:00
|
|
|
int hangup_seen = 0;
|
2021-06-11 02:36:26 +02:00
|
|
|
enum connection_state prevstate = sock->state;
|
2010-07-18 04:06:49 +02:00
|
|
|
int error = 0;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
assert( sock->obj.ops == &sock_ops );
|
1999-10-23 18:53:34 +02:00
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
fprintf(stderr, "socket %p select event: %x\n", sock, event);
|
2010-05-13 22:41:58 +02:00
|
|
|
|
|
|
|
/* we may change event later, remove from loop here */
|
|
|
|
if (event & (POLLERR|POLLHUP)) set_fd_events( sock->fd, -1 );
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
switch (sock->state)
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_UNCONNECTED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SOCK_CONNECTING:
|
2011-01-24 06:21:15 +01:00
|
|
|
if (event & (POLLERR|POLLHUP))
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = SOCK_UNCONNECTED;
|
2011-01-24 06:21:15 +01:00
|
|
|
event &= ~POLLOUT;
|
|
|
|
error = sock_error( fd );
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
2011-01-24 06:21:15 +01:00
|
|
|
else if (event & POLLOUT)
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = SOCK_CONNECTED;
|
2014-11-12 03:42:14 +01:00
|
|
|
sock->connect_time = current_time;
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
2021-06-11 02:36:26 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SOCK_LISTENING:
|
2010-07-18 04:06:49 +02:00
|
|
|
if (event & (POLLERR|POLLHUP))
|
|
|
|
error = sock_error( fd );
|
2021-06-11 02:36:26 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SOCK_CONNECTED:
|
|
|
|
case SOCK_CONNECTIONLESS:
|
2020-09-30 17:37:52 +02:00
|
|
|
if (sock->type == WS_SOCK_STREAM && (event & POLLIN))
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2001-08-24 01:25:33 +02:00
|
|
|
char dummy;
|
2002-04-25 23:31:11 +02:00
|
|
|
int nr;
|
2001-08-24 01:25:33 +02:00
|
|
|
|
|
|
|
/* Linux 2.4 doesn't report POLLHUP if only one side of the socket
|
|
|
|
* has been closed, so we need to check for it explicitly here */
|
2003-02-14 21:27:09 +01:00
|
|
|
nr = recv( get_unix_fd( fd ), &dummy, 1, MSG_PEEK );
|
2010-05-12 07:59:01 +02:00
|
|
|
if ( nr == 0 )
|
2010-05-13 22:41:58 +02:00
|
|
|
{
|
2002-07-03 23:04:09 +02:00
|
|
|
hangup_seen = 1;
|
2010-05-13 22:41:58 +02:00
|
|
|
event &= ~POLLIN;
|
|
|
|
}
|
2010-05-12 07:59:01 +02:00
|
|
|
else if ( nr < 0 )
|
2002-04-25 23:31:11 +02:00
|
|
|
{
|
2010-05-13 22:41:58 +02:00
|
|
|
event &= ~POLLIN;
|
2002-04-25 23:31:11 +02:00
|
|
|
/* EAGAIN can happen if an async recv() falls between the server's poll()
|
|
|
|
call and the invocation of this routine */
|
2010-05-13 22:41:58 +02:00
|
|
|
if ( errno != EAGAIN )
|
2002-04-25 23:31:11 +02:00
|
|
|
{
|
2010-07-18 04:06:53 +02:00
|
|
|
error = errno;
|
|
|
|
event |= POLLERR;
|
2002-04-25 23:31:11 +02:00
|
|
|
if ( debug_level )
|
2005-01-14 20:54:38 +01:00
|
|
|
fprintf( stderr, "recv error on socket %p: %d\n", sock, errno );
|
2002-04-25 23:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
2002-07-03 23:04:09 +02:00
|
|
|
}
|
2010-05-12 07:59:01 +02:00
|
|
|
|
2021-07-26 18:53:43 +02:00
|
|
|
if (hangup_seen || (sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP)))
|
|
|
|
{
|
|
|
|
sock->hangup = 1;
|
|
|
|
}
|
|
|
|
else if (event & (POLLHUP | POLLERR))
|
2002-04-25 23:31:11 +02:00
|
|
|
{
|
2021-07-26 18:53:45 +02:00
|
|
|
sock->aborted = 1;
|
2010-05-12 07:59:04 +02:00
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
if (debug_level)
|
2021-07-26 18:53:43 +02:00
|
|
|
fprintf( stderr, "socket %p aborted by error %d, event %#x\n", sock, error, event );
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2010-05-13 22:41:58 +02:00
|
|
|
if (hangup_seen)
|
|
|
|
event |= POLLHUP;
|
2021-06-11 02:36:26 +02:00
|
|
|
break;
|
2002-04-26 20:31:19 +02:00
|
|
|
}
|
2002-03-23 21:43:52 +01:00
|
|
|
|
2021-05-25 06:13:41 +02:00
|
|
|
complete_async_polls( sock, event, error );
|
|
|
|
|
2010-07-27 06:16:18 +02:00
|
|
|
event = sock_dispatch_asyncs( sock, event, error );
|
2010-07-18 04:06:49 +02:00
|
|
|
sock_dispatch_events( sock, prevstate, event, error );
|
2010-05-15 07:18:20 +02:00
|
|
|
|
2010-05-13 22:41:58 +02:00
|
|
|
sock_reselect( sock );
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sock_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct sock *sock = (struct sock *)obj;
|
|
|
|
assert( obj->ops == &sock_ops );
|
2021-05-12 04:19:49 +02:00
|
|
|
fprintf( stderr, "Socket fd=%p, state=%x, mask=%x, pending=%x, reported=%x\n",
|
2003-02-19 01:33:32 +01:00
|
|
|
sock->fd, sock->state,
|
2021-05-12 04:19:49 +02:00
|
|
|
sock->mask, sock->pending_events, sock->reported_events );
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 06:13:41 +02:00
|
|
|
static int poll_flags_from_afd( struct sock *sock, int flags )
|
|
|
|
{
|
|
|
|
int ev = 0;
|
|
|
|
|
|
|
|
/* A connection-mode socket which has never been connected does
|
|
|
|
* not return write or hangup events, but Linux returns
|
|
|
|
* POLLOUT | POLLHUP. */
|
2021-06-11 02:36:26 +02:00
|
|
|
if (sock->state == SOCK_UNCONNECTED)
|
2021-05-25 06:13:41 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (flags & (AFD_POLL_READ | AFD_POLL_ACCEPT))
|
|
|
|
ev |= POLLIN;
|
|
|
|
if ((flags & AFD_POLL_HUP) && sock->type == WS_SOCK_STREAM)
|
|
|
|
ev |= POLLIN;
|
|
|
|
if (flags & AFD_POLL_OOB)
|
|
|
|
ev |= is_oobinline( sock ) ? POLLIN : POLLPRI;
|
|
|
|
if (flags & AFD_POLL_WRITE)
|
|
|
|
ev |= POLLOUT;
|
|
|
|
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
static int sock_get_poll_events( struct fd *fd )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct sock *sock = get_fd_user( fd );
|
2021-05-12 04:19:49 +02:00
|
|
|
unsigned int mask = sock->mask & ~sock->reported_events;
|
2021-05-25 06:13:41 +02:00
|
|
|
struct poll_req *req;
|
2000-01-01 01:56:27 +01:00
|
|
|
int ev = 0;
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
assert( sock->obj.ops == &sock_ops );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
2021-05-27 06:36:48 +02:00
|
|
|
if (!sock->type) /* not initialized yet */
|
|
|
|
return -1;
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
switch (sock->state)
|
|
|
|
{
|
|
|
|
case SOCK_UNCONNECTED:
|
|
|
|
/* A connection-mode Windows socket which has never been connected does
|
|
|
|
* not return any events, but Linux returns POLLOUT | POLLHUP. Hence we
|
|
|
|
* need to return -1 here, to prevent the socket from being polled on at
|
|
|
|
* all. */
|
2021-05-27 06:36:48 +02:00
|
|
|
return -1;
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_CONNECTING:
|
2000-01-01 01:56:27 +01:00
|
|
|
return POLLOUT;
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_LISTENING:
|
2021-06-14 07:41:47 +02:00
|
|
|
if (!list_empty( &sock->accept_list ) || (mask & AFD_POLL_ACCEPT))
|
2021-06-11 02:36:25 +02:00
|
|
|
ev |= POLLIN;
|
2021-06-11 02:36:26 +02:00
|
|
|
break;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
case SOCK_CONNECTED:
|
|
|
|
case SOCK_CONNECTIONLESS:
|
2021-07-26 18:53:44 +02:00
|
|
|
if (sock->hangup && sock->wr_shutdown && !sock->wr_shutdown_pending)
|
|
|
|
{
|
|
|
|
/* Linux returns POLLHUP if a socket is both SHUT_RD and SHUT_WR, or
|
|
|
|
* if both the socket and its peer are SHUT_WR.
|
|
|
|
*
|
|
|
|
* We don't use SHUT_RD, so we can only encounter this in the latter
|
|
|
|
* case. In that case there can't be any pending read requests (they
|
|
|
|
* would have already been completed with a length of zero), the
|
|
|
|
* above condition ensures that we don't have any pending write
|
|
|
|
* requests, and nothing that can change about the socket state that
|
|
|
|
* would complete a pending poll request. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:53:45 +02:00
|
|
|
if (sock->aborted)
|
|
|
|
return -1;
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
if (sock->accept_recv_req)
|
|
|
|
{
|
|
|
|
ev |= POLLIN;
|
|
|
|
}
|
|
|
|
else if (async_queued( &sock->read_q ))
|
|
|
|
{
|
|
|
|
if (async_waiting( &sock->read_q )) ev |= POLLIN | POLLPRI;
|
|
|
|
}
|
2021-06-15 17:39:44 +02:00
|
|
|
else
|
|
|
|
{
|
2021-07-26 18:53:43 +02:00
|
|
|
/* Don't ask for POLLIN if we got a hangup. We won't receive more
|
|
|
|
* data anyway, but we will get POLLIN if SOCK_SHUTDOWN_EOF. */
|
|
|
|
if (!sock->hangup)
|
2021-06-15 17:39:44 +02:00
|
|
|
{
|
|
|
|
if (mask & AFD_POLL_READ)
|
|
|
|
ev |= POLLIN;
|
|
|
|
if (mask & AFD_POLL_OOB)
|
|
|
|
ev |= POLLPRI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We use POLLIN with 0 bytes recv() as hangup indication for stream sockets. */
|
|
|
|
if (sock->state == SOCK_CONNECTED && (mask & AFD_POLL_HUP) && !(sock->reported_events & AFD_POLL_READ))
|
|
|
|
ev |= POLLIN;
|
|
|
|
}
|
2021-06-11 02:36:26 +02:00
|
|
|
|
|
|
|
if (async_queued( &sock->write_q ))
|
|
|
|
{
|
|
|
|
if (async_waiting( &sock->write_q )) ev |= POLLOUT;
|
|
|
|
}
|
2021-06-14 07:41:47 +02:00
|
|
|
else if (!sock->wr_shutdown && (mask & AFD_POLL_WRITE))
|
2021-06-11 02:36:26 +02:00
|
|
|
{
|
|
|
|
ev |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2010-05-19 07:38:29 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 06:13:41 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( req, &poll_list, struct poll_req, entry )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < req->count; ++i)
|
|
|
|
{
|
|
|
|
if (req->sockets[i].sock != sock) continue;
|
|
|
|
|
|
|
|
ev |= poll_flags_from_afd( sock, req->sockets[i].flags );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
return ev;
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2007-04-10 22:26:23 +02:00
|
|
|
static enum server_fd_type sock_get_fd_type( struct fd *fd )
|
2001-10-05 21:45:45 +02:00
|
|
|
{
|
2006-11-20 14:14:04 +01:00
|
|
|
return FD_TYPE_SOCKET;
|
2001-10-05 21:45:45 +02:00
|
|
|
}
|
|
|
|
|
2017-02-15 22:12:45 +01:00
|
|
|
static void sock_queue_async( struct fd *fd, struct async *async, int type, int count )
|
2002-04-25 23:31:11 +02:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct sock *sock = get_fd_user( fd );
|
2007-04-02 20:09:29 +02:00
|
|
|
struct async_queue *queue;
|
2005-02-24 18:06:31 +01:00
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
assert( sock->obj.ops == &sock_ops );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
switch (type)
|
2002-04-25 23:31:11 +02:00
|
|
|
{
|
|
|
|
case ASYNC_TYPE_READ:
|
2021-06-11 02:36:25 +02:00
|
|
|
if (sock->rd_shutdown)
|
|
|
|
{
|
|
|
|
set_error( STATUS_PIPE_DISCONNECTED );
|
|
|
|
return;
|
|
|
|
}
|
2017-07-04 15:26:19 +02:00
|
|
|
queue = &sock->read_q;
|
2002-04-25 23:31:11 +02:00
|
|
|
break;
|
2021-06-11 02:36:25 +02:00
|
|
|
|
2002-04-25 23:31:11 +02:00
|
|
|
case ASYNC_TYPE_WRITE:
|
2021-06-11 02:36:25 +02:00
|
|
|
if (sock->wr_shutdown)
|
|
|
|
{
|
|
|
|
set_error( STATUS_PIPE_DISCONNECTED );
|
|
|
|
return;
|
|
|
|
}
|
2017-07-04 15:26:19 +02:00
|
|
|
queue = &sock->write_q;
|
2002-04-25 23:31:11 +02:00
|
|
|
break;
|
2021-06-11 02:36:25 +02:00
|
|
|
|
2002-04-25 23:31:11 +02:00
|
|
|
default:
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
if (sock->state != SOCK_CONNECTED)
|
2002-04-25 23:31:11 +02:00
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
set_error( STATUS_PIPE_DISCONNECTED );
|
2010-08-01 05:21:28 +02:00
|
|
|
return;
|
2005-01-14 20:54:38 +01:00
|
|
|
}
|
2010-08-01 05:21:28 +02:00
|
|
|
|
2017-02-15 22:12:28 +01:00
|
|
|
queue_async( queue, async );
|
2010-05-13 22:42:04 +02:00
|
|
|
sock_reselect( sock );
|
2010-08-01 05:21:28 +02:00
|
|
|
|
|
|
|
set_error( STATUS_PENDING );
|
2005-01-14 20:54:38 +01:00
|
|
|
}
|
|
|
|
|
2007-04-10 17:07:27 +02:00
|
|
|
static void sock_reselect_async( struct fd *fd, struct async_queue *queue )
|
|
|
|
{
|
|
|
|
struct sock *sock = get_fd_user( fd );
|
2020-10-19 18:21:18 +02:00
|
|
|
|
2021-05-19 05:35:49 +02:00
|
|
|
if (sock->wr_shutdown_pending && list_empty( &sock->write_q.queue ))
|
2021-07-26 18:53:44 +02:00
|
|
|
{
|
2021-05-19 05:35:49 +02:00
|
|
|
shutdown( get_unix_fd( sock->fd ), SHUT_WR );
|
2021-07-26 18:53:44 +02:00
|
|
|
sock->wr_shutdown_pending = 0;
|
|
|
|
}
|
2021-05-19 05:35:49 +02:00
|
|
|
|
2021-05-10 17:19:44 +02:00
|
|
|
/* Don't reselect the ifchange queue; we always ask for POLLIN.
|
|
|
|
* Don't reselect an uninitialized socket; we can't call set_fd_events() on
|
|
|
|
* a pseudo-fd. */
|
|
|
|
if (queue != &sock->ifchange_q && sock->type)
|
2016-10-12 17:11:50 +02:00
|
|
|
sock_reselect( sock );
|
2007-04-10 17:07:27 +02:00
|
|
|
}
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
static struct fd *sock_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct sock *sock = (struct sock *)obj;
|
|
|
|
return (struct fd *)grab_object( sock->fd );
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:40:40 +01:00
|
|
|
static int sock_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
|
|
|
struct sock *sock = (struct sock *)obj;
|
2020-10-19 18:21:18 +02:00
|
|
|
|
2021-02-09 00:40:40 +01:00
|
|
|
if (sock->obj.handle_count == 1) /* last handle */
|
|
|
|
{
|
2021-05-25 06:13:41 +02:00
|
|
|
struct accept_req *accept_req, *accept_next;
|
|
|
|
struct poll_req *poll_req, *poll_next;
|
|
|
|
|
2021-02-09 00:40:40 +01:00
|
|
|
if (sock->accept_recv_req)
|
|
|
|
async_terminate( sock->accept_recv_req->async, STATUS_CANCELLED );
|
|
|
|
|
2021-05-25 06:13:41 +02:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( accept_req, accept_next, &sock->accept_list, struct accept_req, entry )
|
|
|
|
async_terminate( accept_req->async, STATUS_CANCELLED );
|
2021-05-18 04:29:59 +02:00
|
|
|
|
|
|
|
if (sock->connect_req)
|
|
|
|
async_terminate( sock->connect_req->async, STATUS_CANCELLED );
|
2021-05-25 06:13:41 +02:00
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( poll_req, poll_next, &poll_list, struct poll_req, entry )
|
|
|
|
{
|
|
|
|
struct iosb *iosb = poll_req->iosb;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (iosb->status != STATUS_PENDING) continue;
|
|
|
|
|
|
|
|
for (i = 0; i < poll_req->count; ++i)
|
|
|
|
{
|
|
|
|
if (poll_req->sockets[i].sock == sock)
|
|
|
|
{
|
|
|
|
iosb->status = STATUS_SUCCESS;
|
|
|
|
poll_req->output[i].flags = AFD_POLL_CLOSE;
|
|
|
|
poll_req->output[i].status = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iosb->status != STATUS_PENDING)
|
|
|
|
{
|
|
|
|
iosb->out_data = poll_req->output;
|
|
|
|
iosb->out_size = poll_req->count * sizeof(*poll_req->output);
|
|
|
|
async_terminate( poll_req->async, STATUS_ALERTED );
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 05:04:30 +01:00
|
|
|
return 1;
|
2021-02-09 00:40:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sock_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct sock *sock = (struct sock *)obj;
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
assert( obj->ops == &sock_ops );
|
|
|
|
|
|
|
|
/* FIXME: special socket shutdown stuff? */
|
2002-01-09 22:16:24 +01:00
|
|
|
|
2002-04-24 00:03:42 +02:00
|
|
|
if ( sock->deferred )
|
2005-01-14 20:54:38 +01:00
|
|
|
release_object( sock->deferred );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
2017-07-04 15:26:19 +02:00
|
|
|
async_wake_up( &sock->ifchange_q, STATUS_CANCELLED );
|
2017-06-30 15:08:09 +02:00
|
|
|
sock_release_ifchange( sock );
|
2017-07-04 15:26:19 +02:00
|
|
|
free_async_queue( &sock->read_q );
|
|
|
|
free_async_queue( &sock->write_q );
|
|
|
|
free_async_queue( &sock->ifchange_q );
|
2020-10-19 18:21:18 +02:00
|
|
|
free_async_queue( &sock->accept_q );
|
2021-05-18 04:29:59 +02:00
|
|
|
free_async_queue( &sock->connect_q );
|
2021-05-25 06:13:41 +02:00
|
|
|
free_async_queue( &sock->poll_q );
|
2002-03-23 21:43:52 +01:00
|
|
|
if (sock->event) release_object( sock->event );
|
2006-12-04 18:02:02 +01:00
|
|
|
if (sock->fd)
|
|
|
|
{
|
|
|
|
/* shut the socket down to force pending poll() calls in the client to return */
|
|
|
|
shutdown( get_unix_fd(sock->fd), SHUT_RDWR );
|
|
|
|
release_object( sock->fd );
|
|
|
|
}
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 00:13:35 +02:00
|
|
|
static struct sock *create_socket(void)
|
2010-08-30 00:09:53 +02:00
|
|
|
{
|
2020-09-20 00:13:35 +02:00
|
|
|
struct sock *sock;
|
|
|
|
|
|
|
|
if (!(sock = alloc_object( &sock_ops ))) return NULL;
|
|
|
|
sock->fd = NULL;
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = SOCK_UNCONNECTED;
|
2010-08-30 00:09:53 +02:00
|
|
|
sock->mask = 0;
|
2021-05-12 04:19:48 +02:00
|
|
|
sock->pending_events = 0;
|
2021-05-12 04:19:49 +02:00
|
|
|
sock->reported_events = 0;
|
2010-08-30 00:09:53 +02:00
|
|
|
sock->flags = 0;
|
2020-09-20 00:13:35 +02:00
|
|
|
sock->proto = 0;
|
2010-08-30 00:09:53 +02:00
|
|
|
sock->type = 0;
|
|
|
|
sock->family = 0;
|
|
|
|
sock->event = NULL;
|
|
|
|
sock->window = 0;
|
|
|
|
sock->message = 0;
|
|
|
|
sock->wparam = 0;
|
2014-11-12 03:42:14 +01:00
|
|
|
sock->connect_time = 0;
|
2010-08-30 00:09:53 +02:00
|
|
|
sock->deferred = NULL;
|
2014-05-06 16:39:18 +02:00
|
|
|
sock->ifchange_obj = NULL;
|
2020-10-19 18:21:18 +02:00
|
|
|
sock->accept_recv_req = NULL;
|
2021-05-18 04:29:59 +02:00
|
|
|
sock->connect_req = NULL;
|
2021-06-16 06:13:28 +02:00
|
|
|
memset( &sock->addr, 0, sizeof(sock->addr) );
|
|
|
|
sock->addr_len = 0;
|
2021-06-11 02:36:25 +02:00
|
|
|
sock->rd_shutdown = 0;
|
|
|
|
sock->wr_shutdown = 0;
|
2021-06-11 02:36:24 +02:00
|
|
|
sock->wr_shutdown_pending = 0;
|
2021-07-26 18:53:43 +02:00
|
|
|
sock->hangup = 0;
|
2021-07-26 18:53:45 +02:00
|
|
|
sock->aborted = 0;
|
2021-06-11 02:36:24 +02:00
|
|
|
sock->nonblocking = 0;
|
2021-06-16 06:13:28 +02:00
|
|
|
sock->bound = 0;
|
2021-06-26 03:21:06 +02:00
|
|
|
sock->rcvbuf = 0;
|
2021-06-30 06:25:40 +02:00
|
|
|
sock->sndbuf = 0;
|
2021-06-29 18:43:20 +02:00
|
|
|
sock->rcvtimeo = 0;
|
2021-06-30 06:25:43 +02:00
|
|
|
sock->sndtimeo = 0;
|
2017-07-04 15:26:19 +02:00
|
|
|
init_async_queue( &sock->read_q );
|
|
|
|
init_async_queue( &sock->write_q );
|
|
|
|
init_async_queue( &sock->ifchange_q );
|
2020-10-19 18:21:18 +02:00
|
|
|
init_async_queue( &sock->accept_q );
|
2021-05-18 04:29:59 +02:00
|
|
|
init_async_queue( &sock->connect_q );
|
2021-05-25 06:13:41 +02:00
|
|
|
init_async_queue( &sock->poll_q );
|
2010-08-30 00:09:53 +02:00
|
|
|
memset( sock->errors, 0, sizeof(sock->errors) );
|
2020-10-19 18:21:18 +02:00
|
|
|
list_init( &sock->accept_list );
|
2020-09-20 00:13:35 +02:00
|
|
|
return sock;
|
2010-08-30 00:09:53 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 17:37:50 +02:00
|
|
|
static int get_unix_family( int family )
|
|
|
|
{
|
|
|
|
switch (family)
|
|
|
|
{
|
|
|
|
case WS_AF_INET: return AF_INET;
|
|
|
|
case WS_AF_INET6: return AF_INET6;
|
|
|
|
#ifdef HAS_IPX
|
|
|
|
case WS_AF_IPX: return AF_IPX;
|
|
|
|
#endif
|
|
|
|
#ifdef AF_IRDA
|
|
|
|
case WS_AF_IRDA: return AF_IRDA;
|
|
|
|
#endif
|
|
|
|
case WS_AF_UNSPEC: return AF_UNSPEC;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_unix_type( int type )
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case WS_SOCK_DGRAM: return SOCK_DGRAM;
|
|
|
|
case WS_SOCK_RAW: return SOCK_RAW;
|
|
|
|
case WS_SOCK_STREAM: return SOCK_STREAM;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_unix_protocol( int protocol )
|
|
|
|
{
|
|
|
|
if (protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
|
|
|
|
return protocol;
|
|
|
|
|
|
|
|
switch (protocol)
|
|
|
|
{
|
|
|
|
case WS_IPPROTO_ICMP: return IPPROTO_ICMP;
|
|
|
|
case WS_IPPROTO_IGMP: return IPPROTO_IGMP;
|
|
|
|
case WS_IPPROTO_IP: return IPPROTO_IP;
|
2021-04-27 18:07:19 +02:00
|
|
|
case WS_IPPROTO_IPV4: return IPPROTO_IPIP;
|
2020-09-30 17:37:50 +02:00
|
|
|
case WS_IPPROTO_IPV6: return IPPROTO_IPV6;
|
|
|
|
case WS_IPPROTO_RAW: return IPPROTO_RAW;
|
|
|
|
case WS_IPPROTO_TCP: return IPPROTO_TCP;
|
|
|
|
case WS_IPPROTO_UDP: return IPPROTO_UDP;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 17:37:51 +02:00
|
|
|
static void set_dont_fragment( int fd, int level, int value )
|
|
|
|
{
|
|
|
|
int optname;
|
|
|
|
|
|
|
|
if (level == IPPROTO_IP)
|
|
|
|
{
|
|
|
|
#ifdef IP_DONTFRAG
|
|
|
|
optname = IP_DONTFRAG;
|
|
|
|
#elif defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO) && defined(IP_PMTUDISC_DONT)
|
|
|
|
optname = IP_MTU_DISCOVER;
|
|
|
|
value = value ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
|
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef IPV6_DONTFRAG
|
|
|
|
optname = IPV6_DONTFRAG;
|
|
|
|
#elif defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO) && defined(IPV6_PMTUDISC_DONT)
|
|
|
|
optname = IPV6_MTU_DISCOVER;
|
|
|
|
value = value ? IPV6_PMTUDISC_DO : IPV6_PMTUDISC_DONT;
|
|
|
|
#else
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
setsockopt( fd, level, optname, &value, sizeof(value) );
|
|
|
|
}
|
|
|
|
|
2020-09-20 00:13:35 +02:00
|
|
|
static int init_socket( struct sock *sock, int family, int type, int protocol, unsigned int flags )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2020-09-25 05:21:00 +02:00
|
|
|
unsigned int options = 0;
|
2021-06-26 03:21:06 +02:00
|
|
|
int sockfd, unix_type, unix_family, unix_protocol, value;
|
|
|
|
socklen_t len;
|
2020-09-30 17:37:50 +02:00
|
|
|
|
|
|
|
unix_family = get_unix_family( family );
|
|
|
|
unix_type = get_unix_type( type );
|
|
|
|
unix_protocol = get_unix_protocol( protocol );
|
|
|
|
|
|
|
|
if (unix_protocol < 0)
|
|
|
|
{
|
|
|
|
if (type && unix_type < 0)
|
|
|
|
set_win32_error( WSAESOCKTNOSUPPORT );
|
|
|
|
else
|
|
|
|
set_win32_error( WSAEPROTONOSUPPORT );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (unix_family < 0)
|
|
|
|
{
|
|
|
|
if (family >= 0 && unix_type < 0)
|
|
|
|
set_win32_error( WSAESOCKTNOSUPPORT );
|
|
|
|
else
|
|
|
|
set_win32_error( WSAEAFNOSUPPORT );
|
|
|
|
return -1;
|
|
|
|
}
|
1999-10-13 18:05:37 +02:00
|
|
|
|
2020-09-30 17:37:50 +02:00
|
|
|
sockfd = socket( unix_family, unix_type, unix_protocol );
|
2005-01-14 20:54:38 +01:00
|
|
|
if (sockfd == -1)
|
|
|
|
{
|
2018-11-20 14:50:21 +01:00
|
|
|
if (errno == EINVAL) set_win32_error( WSAESOCKTNOSUPPORT );
|
|
|
|
else set_win32_error( sock_get_error( errno ));
|
2020-09-20 00:13:35 +02:00
|
|
|
return -1;
|
1999-11-07 06:23:02 +01:00
|
|
|
}
|
|
|
|
fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
|
2020-09-30 17:37:51 +02:00
|
|
|
|
|
|
|
if (family == WS_AF_IPX && protocol >= WS_NSPROTO_IPX && protocol <= WS_NSPROTO_IPX + 255)
|
|
|
|
{
|
|
|
|
#ifdef HAS_IPX
|
|
|
|
int ipx_type = protocol - WS_NSPROTO_IPX;
|
|
|
|
|
|
|
|
#ifdef SOL_IPX
|
|
|
|
setsockopt( sockfd, SOL_IPX, IPX_TYPE, &ipx_type, sizeof(ipx_type) );
|
|
|
|
#else
|
|
|
|
struct ipx val;
|
|
|
|
/* Should we retrieve val using a getsockopt call and then
|
|
|
|
* set the modified one? */
|
|
|
|
val.ipx_pt = ipx_type;
|
|
|
|
setsockopt( sockfd, 0, SO_DEFAULT_HEADERS, &val, sizeof(val) );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unix_family == AF_INET || unix_family == AF_INET6)
|
|
|
|
{
|
|
|
|
/* ensure IP_DONTFRAGMENT is disabled for SOCK_DGRAM and SOCK_RAW, enabled for SOCK_STREAM */
|
|
|
|
if (unix_type == SOCK_DGRAM || unix_type == SOCK_RAW) /* in Linux the global default can be enabled */
|
|
|
|
set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, FALSE );
|
|
|
|
else if (unix_type == SOCK_STREAM)
|
|
|
|
set_dont_fragment( sockfd, unix_family == AF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP, TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
if (unix_family == AF_INET6)
|
|
|
|
{
|
|
|
|
static const int enable = 1;
|
|
|
|
setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &enable, sizeof(enable) );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-26 03:21:06 +02:00
|
|
|
len = sizeof(value);
|
|
|
|
if (!getsockopt( sockfd, SOL_SOCKET, SO_RCVBUF, &value, &len ))
|
|
|
|
sock->rcvbuf = value;
|
|
|
|
|
2021-06-30 06:25:40 +02:00
|
|
|
len = sizeof(value);
|
|
|
|
if (!getsockopt( sockfd, SOL_SOCKET, SO_SNDBUF, &value, &len ))
|
|
|
|
sock->sndbuf = value;
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = (type == WS_SOCK_STREAM ? SOCK_UNCONNECTED : SOCK_CONNECTIONLESS);
|
2010-08-30 00:09:53 +02:00
|
|
|
sock->flags = flags;
|
2020-09-30 17:37:52 +02:00
|
|
|
sock->proto = protocol;
|
|
|
|
sock->type = type;
|
|
|
|
sock->family = family;
|
2010-08-30 00:09:53 +02:00
|
|
|
|
2020-09-25 05:21:00 +02:00
|
|
|
if (sock->fd)
|
|
|
|
{
|
|
|
|
options = get_fd_options( sock->fd );
|
|
|
|
release_object( sock->fd );
|
|
|
|
}
|
2020-09-20 00:13:36 +02:00
|
|
|
|
2020-09-25 05:21:00 +02:00
|
|
|
if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj, options )))
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
2020-09-20 00:13:35 +02:00
|
|
|
return -1;
|
2003-02-19 01:33:32 +01:00
|
|
|
}
|
2021-05-27 06:36:47 +02:00
|
|
|
|
|
|
|
/* We can't immediately allow caching for a connection-mode socket, since it
|
|
|
|
* might be accepted into (changing the underlying fd object.) */
|
|
|
|
if (sock->type != WS_SOCK_STREAM) allow_fd_caching( sock->fd );
|
|
|
|
|
2020-09-20 00:13:35 +02:00
|
|
|
return 0;
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 00:09:53 +02:00
|
|
|
/* accepts a socket and inits it */
|
|
|
|
static int accept_new_fd( struct sock *sock )
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Try to accept(2). We can't be safe that this an already connected socket
|
|
|
|
* or that accept() is allowed on it. In those cases we will get -1/errno
|
|
|
|
* return.
|
|
|
|
*/
|
|
|
|
struct sockaddr saddr;
|
2013-03-06 13:40:38 +01:00
|
|
|
socklen_t slen = sizeof(saddr);
|
2018-11-20 15:53:30 +01:00
|
|
|
int acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen );
|
|
|
|
if (acceptfd != -1)
|
|
|
|
fcntl( acceptfd, F_SETFL, O_NONBLOCK );
|
|
|
|
else
|
2021-05-05 04:42:31 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ));
|
2010-08-30 00:09:53 +02:00
|
|
|
return acceptfd;
|
|
|
|
}
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
/* accept a socket (creates a new fd) */
|
2020-10-02 06:13:57 +02:00
|
|
|
static struct sock *accept_socket( struct sock *sock )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
|
|
|
struct sock *acceptsock;
|
|
|
|
int acceptfd;
|
|
|
|
|
2020-09-20 00:13:35 +02:00
|
|
|
if (get_unix_fd( sock->fd ) == -1) return NULL;
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
if ( sock->deferred )
|
|
|
|
{
|
2002-04-24 00:03:42 +02:00
|
|
|
acceptsock = sock->deferred;
|
|
|
|
sock->deferred = NULL;
|
2005-01-14 20:54:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-16 06:13:28 +02:00
|
|
|
union unix_sockaddr unix_addr;
|
|
|
|
socklen_t unix_len;
|
|
|
|
|
2020-10-02 06:13:57 +02:00
|
|
|
if ((acceptfd = accept_new_fd( sock )) == -1) return NULL;
|
2020-09-20 00:13:35 +02:00
|
|
|
if (!(acceptsock = create_socket()))
|
2002-04-24 00:03:42 +02:00
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
close( acceptfd );
|
2002-04-24 00:03:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-07 20:15:19 +01:00
|
|
|
|
2002-04-24 00:03:42 +02:00
|
|
|
/* newly created socket gets the same properties of the listening socket */
|
2021-06-11 02:36:26 +02:00
|
|
|
acceptsock->state = SOCK_CONNECTED;
|
2021-06-16 06:13:28 +02:00
|
|
|
acceptsock->bound = 1;
|
2021-06-11 02:36:24 +02:00
|
|
|
acceptsock->nonblocking = sock->nonblocking;
|
2002-04-24 00:03:42 +02:00
|
|
|
acceptsock->mask = sock->mask;
|
2013-08-31 05:14:44 +02:00
|
|
|
acceptsock->proto = sock->proto;
|
2002-07-03 23:04:09 +02:00
|
|
|
acceptsock->type = sock->type;
|
|
|
|
acceptsock->family = sock->family;
|
2002-04-24 00:03:42 +02:00
|
|
|
acceptsock->window = sock->window;
|
|
|
|
acceptsock->message = sock->message;
|
2014-11-12 03:42:14 +01:00
|
|
|
acceptsock->connect_time = current_time;
|
2002-04-24 00:03:42 +02:00
|
|
|
if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
|
|
|
|
acceptsock->flags = sock->flags;
|
2007-04-10 22:25:07 +02:00
|
|
|
if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
|
|
|
|
get_fd_options( sock->fd ) )))
|
2003-02-19 01:33:32 +01:00
|
|
|
{
|
|
|
|
release_object( acceptsock );
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-16 06:13:28 +02:00
|
|
|
unix_len = sizeof(unix_addr);
|
|
|
|
if (!getsockname( acceptfd, &unix_addr.addr, &unix_len ))
|
|
|
|
acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
|
2002-04-24 00:03:42 +02:00
|
|
|
}
|
1999-10-13 18:05:37 +02:00
|
|
|
clear_error();
|
2021-06-14 07:41:47 +02:00
|
|
|
sock->pending_events &= ~AFD_POLL_ACCEPT;
|
|
|
|
sock->reported_events &= ~AFD_POLL_ACCEPT;
|
2000-10-13 19:04:52 +02:00
|
|
|
sock_reselect( sock );
|
2002-03-23 21:43:52 +01:00
|
|
|
return acceptsock;
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
|
2010-08-30 00:09:53 +02:00
|
|
|
static int accept_into_socket( struct sock *sock, struct sock *acceptsock )
|
|
|
|
{
|
2021-06-16 06:13:28 +02:00
|
|
|
union unix_sockaddr unix_addr;
|
|
|
|
socklen_t unix_len;
|
2010-08-30 00:09:53 +02:00
|
|
|
int acceptfd;
|
|
|
|
struct fd *newfd;
|
2020-09-20 00:13:35 +02:00
|
|
|
|
|
|
|
if (get_unix_fd( sock->fd ) == -1) return FALSE;
|
|
|
|
|
2010-08-30 00:09:53 +02:00
|
|
|
if ( sock->deferred )
|
|
|
|
{
|
|
|
|
newfd = dup_fd_object( sock->deferred->fd, 0, 0,
|
|
|
|
get_fd_options( acceptsock->fd ) );
|
|
|
|
if ( !newfd )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
set_fd_user( newfd, &sock_fd_ops, &acceptsock->obj );
|
|
|
|
|
|
|
|
release_object( sock->deferred );
|
|
|
|
sock->deferred = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((acceptfd = accept_new_fd( sock )) == -1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!(newfd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj,
|
|
|
|
get_fd_options( acceptsock->fd ) )))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
acceptsock->state = SOCK_CONNECTED;
|
2021-05-12 04:19:48 +02:00
|
|
|
acceptsock->pending_events = 0;
|
2021-05-12 04:19:49 +02:00
|
|
|
acceptsock->reported_events = 0;
|
2014-11-12 03:42:11 +01:00
|
|
|
acceptsock->proto = sock->proto;
|
2010-08-30 00:09:53 +02:00
|
|
|
acceptsock->type = sock->type;
|
|
|
|
acceptsock->family = sock->family;
|
|
|
|
acceptsock->wparam = 0;
|
|
|
|
acceptsock->deferred = NULL;
|
2014-11-12 03:42:14 +01:00
|
|
|
acceptsock->connect_time = current_time;
|
2015-03-16 14:16:50 +01:00
|
|
|
fd_copy_completion( acceptsock->fd, newfd );
|
2011-06-14 13:38:45 +02:00
|
|
|
release_object( acceptsock->fd );
|
2010-08-30 00:09:53 +02:00
|
|
|
acceptsock->fd = newfd;
|
|
|
|
|
2021-06-16 06:13:28 +02:00
|
|
|
unix_len = sizeof(unix_addr);
|
|
|
|
if (!getsockname( get_unix_fd( newfd ), &unix_addr.addr, &unix_len ))
|
|
|
|
acceptsock->addr_len = sockaddr_from_unix( &unix_addr, &acceptsock->addr.addr, sizeof(acceptsock->addr) );
|
|
|
|
|
2010-08-30 00:09:53 +02:00
|
|
|
clear_error();
|
2021-06-14 07:41:47 +02:00
|
|
|
sock->pending_events &= ~AFD_POLL_ACCEPT;
|
|
|
|
sock->reported_events &= ~AFD_POLL_ACCEPT;
|
2010-08-30 00:09:53 +02:00
|
|
|
sock_reselect( sock );
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:13:27 +02:00
|
|
|
#ifdef IP_BOUND_IF
|
|
|
|
|
|
|
|
static int bind_to_index( int fd, in_addr_t bind_addr, unsigned int index )
|
|
|
|
{
|
|
|
|
return setsockopt( fd, IPPROTO_IP, IP_BOUND_IF, &index, sizeof(index) );
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(IP_UNICAST_IF) && defined(SO_ATTACH_FILTER)
|
|
|
|
|
|
|
|
struct interface_filter
|
|
|
|
{
|
|
|
|
struct sock_filter iface_memaddr;
|
|
|
|
struct sock_filter iface_rule;
|
|
|
|
struct sock_filter ip_memaddr;
|
|
|
|
struct sock_filter ip_rule;
|
|
|
|
struct sock_filter return_keep;
|
|
|
|
struct sock_filter return_dump;
|
|
|
|
};
|
|
|
|
# define FILTER_JUMP_DUMP(here) (u_char)(offsetof(struct interface_filter, return_dump) \
|
|
|
|
-offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
|
|
|
|
/sizeof(struct sock_filter)
|
|
|
|
# define FILTER_JUMP_KEEP(here) (u_char)(offsetof(struct interface_filter, return_keep) \
|
|
|
|
-offsetof(struct interface_filter, here)-sizeof(struct sock_filter)) \
|
|
|
|
/sizeof(struct sock_filter)
|
|
|
|
# define FILTER_JUMP_NEXT() (u_char)(0)
|
|
|
|
# define SKF_NET_DESTIP 16 /* offset in the network header to the destination IP */
|
|
|
|
static struct interface_filter generic_interface_filter =
|
|
|
|
{
|
|
|
|
/* This filter rule allows incoming packets on the specified interface, which works for all
|
|
|
|
* remotely generated packets and for locally generated broadcast packets. */
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_AD_OFF+SKF_AD_IFINDEX),
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(iface_rule), FILTER_JUMP_NEXT()),
|
|
|
|
/* This rule allows locally generated packets targeted at the specific IP address of the chosen
|
|
|
|
* adapter (local packets not destined for the broadcast address do not have IFINDEX set) */
|
|
|
|
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, SKF_NET_OFF+SKF_NET_DESTIP),
|
|
|
|
BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xdeadbeef, FILTER_JUMP_KEEP(ip_rule), FILTER_JUMP_DUMP(ip_rule)),
|
|
|
|
BPF_STMT(BPF_RET+BPF_K, (u_int)-1), /* keep packet */
|
|
|
|
BPF_STMT(BPF_RET+BPF_K, 0) /* dump packet */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int bind_to_index( int fd, in_addr_t bind_addr, unsigned int index )
|
|
|
|
{
|
|
|
|
in_addr_t ifindex = htonl( index );
|
|
|
|
struct interface_filter specific_interface_filter;
|
|
|
|
struct sock_fprog filter_prog;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = setsockopt( fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex) )) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
specific_interface_filter = generic_interface_filter;
|
|
|
|
specific_interface_filter.iface_rule.k = index;
|
|
|
|
specific_interface_filter.ip_rule.k = htonl( bind_addr );
|
|
|
|
filter_prog.len = sizeof(generic_interface_filter) / sizeof(struct sock_filter);
|
|
|
|
filter_prog.filter = (struct sock_filter *)&specific_interface_filter;
|
|
|
|
return setsockopt( fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog) );
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int bind_to_index( int fd, in_addr_t bind_addr, unsigned int index )
|
|
|
|
{
|
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LINUX_BOUND_IF */
|
|
|
|
|
|
|
|
/* Take bind() calls on any name corresponding to a local network adapter and
|
|
|
|
* restrict the given socket to operating only on the specified interface. This
|
|
|
|
* restriction consists of two components:
|
|
|
|
* 1) An outgoing packet restriction suggesting the egress interface for all
|
|
|
|
* packets.
|
|
|
|
* 2) An incoming packet restriction dropping packets not meant for the
|
|
|
|
* interface.
|
|
|
|
* If the function succeeds in placing these restrictions, then the name for the
|
|
|
|
* bind() may safely be changed to INADDR_ANY, permitting the transmission and
|
|
|
|
* receipt of broadcast packets on the socket. This behavior is only relevant to
|
|
|
|
* UDP sockets and is needed for applications that expect to be able to receive
|
|
|
|
* broadcast packets on a socket that is bound to a specific network interface.
|
|
|
|
*/
|
|
|
|
static int bind_to_interface( struct sock *sock, const struct sockaddr_in *addr )
|
|
|
|
{
|
|
|
|
in_addr_t bind_addr = addr->sin_addr.s_addr;
|
|
|
|
struct ifaddrs *ifaddrs, *ifaddr;
|
|
|
|
int fd = get_unix_fd( sock->fd );
|
|
|
|
static const int enable = 1;
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
if (bind_addr == htonl( INADDR_ANY ) || bind_addr == htonl( INADDR_LOOPBACK ))
|
|
|
|
return 0;
|
|
|
|
if (sock->type != WS_SOCK_DGRAM)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (getifaddrs( &ifaddrs ) < 0) return 0;
|
|
|
|
|
|
|
|
for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
|
|
|
|
{
|
|
|
|
if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET
|
|
|
|
&& ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr == bind_addr)
|
|
|
|
{
|
|
|
|
index = if_nametoindex( ifaddr->ifa_name );
|
|
|
|
if (!index)
|
|
|
|
{
|
|
|
|
if (debug_level)
|
|
|
|
fprintf( stderr, "Unable to look up interface index for %s: %s\n",
|
|
|
|
ifaddr->ifa_name, strerror( errno ) );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs( ifaddrs );
|
|
|
|
|
|
|
|
if (bind_to_index( fd, bind_addr, index ) < 0)
|
|
|
|
{
|
|
|
|
if (debug_level)
|
|
|
|
fprintf( stderr, "failed to bind to interface: %s\n", strerror( errno ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable) ) < 0)
|
|
|
|
{
|
|
|
|
if (debug_level)
|
|
|
|
fprintf( stderr, "failed to reuse address: %s\n", strerror( errno ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 03:34:53 +02:00
|
|
|
|
|
|
|
freeifaddrs( ifaddrs );
|
2021-06-16 06:13:27 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 03:34:54 +02:00
|
|
|
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
|
|
|
|
static unsigned int get_ipv6_interface_index( const struct in6_addr *addr )
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifaddrs, *ifaddr;
|
|
|
|
|
|
|
|
if (getifaddrs( &ifaddrs ) < 0) return 0;
|
|
|
|
|
|
|
|
for (ifaddr = ifaddrs; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
|
|
|
|
{
|
|
|
|
if (ifaddr->ifa_addr && ifaddr->ifa_addr->sa_family == AF_INET6
|
|
|
|
&& !memcmp( &((struct sockaddr_in6 *)ifaddr->ifa_addr)->sin6_addr, addr, sizeof(*addr) ))
|
|
|
|
{
|
|
|
|
unsigned int index = if_nametoindex( ifaddr->ifa_name );
|
|
|
|
|
|
|
|
if (!index)
|
|
|
|
{
|
|
|
|
if (debug_level)
|
|
|
|
fprintf( stderr, "Unable to look up interface index for %s: %s\n",
|
|
|
|
ifaddr->ifa_name, strerror( errno ) );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs( ifaddrs );
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs( ifaddrs );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-21 19:42:25 +02:00
|
|
|
/* return an errno value mapped to a WSA error */
|
2018-11-20 12:57:42 +01:00
|
|
|
static unsigned int sock_get_error( int err )
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2000-03-26 20:21:16 +02:00
|
|
|
switch (err)
|
1999-10-13 18:05:37 +02:00
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
case EINTR: return WSAEINTR;
|
|
|
|
case EBADF: return WSAEBADF;
|
1999-10-13 18:05:37 +02:00
|
|
|
case EPERM:
|
2005-01-14 20:54:38 +01:00
|
|
|
case EACCES: return WSAEACCES;
|
|
|
|
case EFAULT: return WSAEFAULT;
|
|
|
|
case EINVAL: return WSAEINVAL;
|
|
|
|
case EMFILE: return WSAEMFILE;
|
2021-05-06 04:41:49 +02:00
|
|
|
case EINPROGRESS:
|
2005-01-14 20:54:38 +01:00
|
|
|
case EWOULDBLOCK: return WSAEWOULDBLOCK;
|
|
|
|
case EALREADY: return WSAEALREADY;
|
|
|
|
case ENOTSOCK: return WSAENOTSOCK;
|
|
|
|
case EDESTADDRREQ: return WSAEDESTADDRREQ;
|
|
|
|
case EMSGSIZE: return WSAEMSGSIZE;
|
|
|
|
case EPROTOTYPE: return WSAEPROTOTYPE;
|
|
|
|
case ENOPROTOOPT: return WSAENOPROTOOPT;
|
|
|
|
case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
|
|
|
|
case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
|
|
|
|
case EOPNOTSUPP: return WSAEOPNOTSUPP;
|
|
|
|
case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
|
|
|
|
case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
|
|
|
|
case EADDRINUSE: return WSAEADDRINUSE;
|
|
|
|
case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
|
|
|
|
case ENETDOWN: return WSAENETDOWN;
|
|
|
|
case ENETUNREACH: return WSAENETUNREACH;
|
|
|
|
case ENETRESET: return WSAENETRESET;
|
|
|
|
case ECONNABORTED: return WSAECONNABORTED;
|
1999-10-13 18:05:37 +02:00
|
|
|
case EPIPE:
|
2005-01-14 20:54:38 +01:00
|
|
|
case ECONNRESET: return WSAECONNRESET;
|
|
|
|
case ENOBUFS: return WSAENOBUFS;
|
|
|
|
case EISCONN: return WSAEISCONN;
|
|
|
|
case ENOTCONN: return WSAENOTCONN;
|
|
|
|
case ESHUTDOWN: return WSAESHUTDOWN;
|
|
|
|
case ETOOMANYREFS: return WSAETOOMANYREFS;
|
|
|
|
case ETIMEDOUT: return WSAETIMEDOUT;
|
|
|
|
case ECONNREFUSED: return WSAECONNREFUSED;
|
|
|
|
case ELOOP: return WSAELOOP;
|
|
|
|
case ENAMETOOLONG: return WSAENAMETOOLONG;
|
|
|
|
case EHOSTDOWN: return WSAEHOSTDOWN;
|
|
|
|
case EHOSTUNREACH: return WSAEHOSTUNREACH;
|
|
|
|
case ENOTEMPTY: return WSAENOTEMPTY;
|
1999-10-13 18:05:37 +02:00
|
|
|
#ifdef EPROCLIM
|
2005-01-14 20:54:38 +01:00
|
|
|
case EPROCLIM: return WSAEPROCLIM;
|
1999-10-13 18:05:37 +02:00
|
|
|
#endif
|
|
|
|
#ifdef EUSERS
|
2005-01-14 20:54:38 +01:00
|
|
|
case EUSERS: return WSAEUSERS;
|
1999-10-13 18:05:37 +02:00
|
|
|
#endif
|
|
|
|
#ifdef EDQUOT
|
2005-01-14 20:54:38 +01:00
|
|
|
case EDQUOT: return WSAEDQUOT;
|
1999-10-13 18:05:37 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ESTALE
|
2005-01-14 20:54:38 +01:00
|
|
|
case ESTALE: return WSAESTALE;
|
1999-10-13 18:05:37 +02:00
|
|
|
#endif
|
|
|
|
#ifdef EREMOTE
|
2005-01-14 20:54:38 +01:00
|
|
|
case EREMOTE: return WSAEREMOTE;
|
1999-10-13 18:05:37 +02:00
|
|
|
#endif
|
2010-03-22 19:56:21 +01:00
|
|
|
|
|
|
|
case 0: return 0;
|
2007-09-04 15:51:39 +02:00
|
|
|
default:
|
|
|
|
errno = err;
|
|
|
|
perror("wineserver: sock_get_error() can't map error");
|
|
|
|
return WSAEFAULT;
|
1999-10-13 18:05:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-22 19:56:21 +01:00
|
|
|
static int sock_get_ntstatus( int err )
|
|
|
|
{
|
|
|
|
switch ( err )
|
|
|
|
{
|
|
|
|
case EBADF: return STATUS_INVALID_HANDLE;
|
|
|
|
case EBUSY: return STATUS_DEVICE_BUSY;
|
|
|
|
case EPERM:
|
|
|
|
case EACCES: return STATUS_ACCESS_DENIED;
|
2021-05-06 04:41:47 +02:00
|
|
|
case EFAULT: return STATUS_ACCESS_VIOLATION;
|
2010-03-22 19:56:21 +01:00
|
|
|
case EINVAL: return STATUS_INVALID_PARAMETER;
|
|
|
|
case ENFILE:
|
|
|
|
case EMFILE: return STATUS_TOO_MANY_OPENED_FILES;
|
2021-05-06 04:41:49 +02:00
|
|
|
case EINPROGRESS:
|
2021-05-05 04:42:31 +02:00
|
|
|
case EWOULDBLOCK: return STATUS_DEVICE_NOT_READY;
|
2010-03-22 19:56:21 +01:00
|
|
|
case EALREADY: return STATUS_NETWORK_BUSY;
|
|
|
|
case ENOTSOCK: return STATUS_OBJECT_TYPE_MISMATCH;
|
|
|
|
case EDESTADDRREQ: return STATUS_INVALID_PARAMETER;
|
|
|
|
case EMSGSIZE: return STATUS_BUFFER_OVERFLOW;
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
case ESOCKTNOSUPPORT:
|
|
|
|
case EPFNOSUPPORT:
|
|
|
|
case EAFNOSUPPORT:
|
|
|
|
case EPROTOTYPE: return STATUS_NOT_SUPPORTED;
|
|
|
|
case ENOPROTOOPT: return STATUS_INVALID_PARAMETER;
|
|
|
|
case EOPNOTSUPP: return STATUS_NOT_SUPPORTED;
|
2021-05-05 04:42:34 +02:00
|
|
|
case EADDRINUSE: return STATUS_SHARING_VIOLATION;
|
2021-07-22 03:34:55 +02:00
|
|
|
/* Linux returns ENODEV when specifying an invalid sin6_scope_id;
|
|
|
|
* Windows returns STATUS_INVALID_ADDRESS_COMPONENT */
|
|
|
|
case ENODEV:
|
2021-06-16 06:13:27 +02:00
|
|
|
case EADDRNOTAVAIL: return STATUS_INVALID_ADDRESS_COMPONENT;
|
2010-03-22 19:56:21 +01:00
|
|
|
case ECONNREFUSED: return STATUS_CONNECTION_REFUSED;
|
|
|
|
case ESHUTDOWN: return STATUS_PIPE_DISCONNECTED;
|
2021-05-06 04:41:45 +02:00
|
|
|
case ENOTCONN: return STATUS_INVALID_CONNECTION;
|
2010-03-22 19:56:21 +01:00
|
|
|
case ETIMEDOUT: return STATUS_IO_TIMEOUT;
|
|
|
|
case ENETUNREACH: return STATUS_NETWORK_UNREACHABLE;
|
2010-05-25 12:18:12 +02:00
|
|
|
case EHOSTUNREACH: return STATUS_HOST_UNREACHABLE;
|
2010-03-22 19:56:21 +01:00
|
|
|
case ENETDOWN: return STATUS_NETWORK_BUSY;
|
|
|
|
case EPIPE:
|
|
|
|
case ECONNRESET: return STATUS_CONNECTION_RESET;
|
|
|
|
case ECONNABORTED: return STATUS_CONNECTION_ABORTED;
|
2021-05-18 04:29:59 +02:00
|
|
|
case EISCONN: return STATUS_CONNECTION_ACTIVE;
|
2010-03-22 19:56:21 +01:00
|
|
|
|
|
|
|
case 0: return STATUS_SUCCESS;
|
|
|
|
default:
|
|
|
|
errno = err;
|
|
|
|
perror("wineserver: sock_get_ntstatus() can't map error");
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:40:41 +01:00
|
|
|
static struct accept_req *alloc_accept_req( struct sock *sock, struct sock *acceptsock, struct async *async,
|
2020-10-19 18:21:18 +02:00
|
|
|
const struct afd_accept_into_params *params )
|
|
|
|
{
|
|
|
|
struct accept_req *req = mem_alloc( sizeof(*req) );
|
|
|
|
|
|
|
|
if (req)
|
|
|
|
{
|
|
|
|
req->async = (struct async *)grab_object( async );
|
2021-02-09 00:40:39 +01:00
|
|
|
req->iosb = async_get_iosb( async );
|
2021-02-09 00:40:41 +01:00
|
|
|
req->sock = (struct sock *)grab_object( sock );
|
2020-10-19 18:21:18 +02:00
|
|
|
req->acceptsock = acceptsock;
|
2021-02-09 00:40:41 +01:00
|
|
|
if (acceptsock) grab_object( acceptsock );
|
2020-10-19 18:21:18 +02:00
|
|
|
req->accepted = 0;
|
|
|
|
req->recv_len = 0;
|
|
|
|
req->local_len = 0;
|
2020-10-19 18:21:19 +02:00
|
|
|
if (params)
|
|
|
|
{
|
|
|
|
req->recv_len = params->recv_len;
|
|
|
|
req->local_len = params->local_len;
|
|
|
|
}
|
2020-10-19 18:21:18 +02:00
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:21:02 +02:00
|
|
|
static int sock_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
|
|
|
|
{
|
|
|
|
struct sock *sock = get_fd_user( fd );
|
2021-05-14 05:54:39 +02:00
|
|
|
int unix_fd;
|
2020-09-25 05:21:02 +02:00
|
|
|
|
|
|
|
assert( sock->obj.ops == &sock_ops );
|
|
|
|
|
2021-05-14 05:54:39 +02:00
|
|
|
if (code != IOCTL_AFD_WINE_CREATE && (unix_fd = get_unix_fd( fd )) < 0) return 0;
|
2020-09-25 05:21:02 +02:00
|
|
|
|
|
|
|
switch(code)
|
|
|
|
{
|
2021-05-14 05:54:37 +02:00
|
|
|
case IOCTL_AFD_WINE_CREATE:
|
2020-09-25 05:21:02 +02:00
|
|
|
{
|
|
|
|
const struct afd_create_params *params = get_req_data();
|
|
|
|
|
|
|
|
if (get_req_data_size() != sizeof(*params))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
init_socket( sock, params->family, params->type, params->protocol, params->flags );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-14 05:54:37 +02:00
|
|
|
case IOCTL_AFD_WINE_ACCEPT:
|
2020-10-02 06:13:57 +02:00
|
|
|
{
|
|
|
|
struct sock *acceptsock;
|
|
|
|
obj_handle_t handle;
|
|
|
|
|
|
|
|
if (get_reply_max_size() != sizeof(handle))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-19 18:21:19 +02:00
|
|
|
if (!(acceptsock = accept_socket( sock )))
|
|
|
|
{
|
|
|
|
struct accept_req *req;
|
|
|
|
|
2021-06-11 02:36:24 +02:00
|
|
|
if (sock->nonblocking) return 0;
|
2021-05-05 04:42:31 +02:00
|
|
|
if (get_error() != STATUS_DEVICE_NOT_READY) return 0;
|
2020-10-19 18:21:19 +02:00
|
|
|
|
2021-02-09 00:40:41 +01:00
|
|
|
if (!(req = alloc_accept_req( sock, NULL, async, NULL ))) return 0;
|
2020-10-19 18:21:19 +02:00
|
|
|
list_add_tail( &sock->accept_list, &req->entry );
|
|
|
|
|
2021-02-09 00:40:42 +01:00
|
|
|
async_set_completion_callback( async, free_accept_req, req );
|
2020-10-19 18:21:19 +02:00
|
|
|
queue_async( &sock->accept_q, async );
|
|
|
|
sock_reselect( sock );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return 1;
|
|
|
|
}
|
2020-10-02 06:13:57 +02:00
|
|
|
handle = alloc_handle( current->process, &acceptsock->obj,
|
|
|
|
GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, OBJ_INHERIT );
|
|
|
|
acceptsock->wparam = handle;
|
|
|
|
release_object( acceptsock );
|
|
|
|
set_reply_data( &handle, sizeof(handle) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-14 05:54:37 +02:00
|
|
|
case IOCTL_AFD_WINE_ACCEPT_INTO:
|
2020-10-02 06:14:00 +02:00
|
|
|
{
|
|
|
|
static const int access = FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | FILE_READ_DATA;
|
2020-10-19 18:21:18 +02:00
|
|
|
const struct afd_accept_into_params *params = get_req_data();
|
2020-10-02 06:14:00 +02:00
|
|
|
struct sock *acceptsock;
|
2020-10-19 18:21:18 +02:00
|
|
|
unsigned int remote_len;
|
|
|
|
struct accept_req *req;
|
2020-10-02 06:14:00 +02:00
|
|
|
|
2020-10-19 18:21:18 +02:00
|
|
|
if (get_req_data_size() != sizeof(*params) ||
|
|
|
|
get_reply_max_size() < params->recv_len ||
|
|
|
|
get_reply_max_size() - params->recv_len < params->local_len)
|
2020-10-02 06:14:00 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-19 18:21:18 +02:00
|
|
|
remote_len = get_reply_max_size() - params->recv_len - params->local_len;
|
|
|
|
if (remote_len < sizeof(int))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
2020-10-02 06:14:00 +02:00
|
|
|
return 0;
|
2020-10-19 18:21:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(acceptsock = (struct sock *)get_handle_obj( current->process, params->accept_handle, access, &sock_ops )))
|
|
|
|
return 0;
|
|
|
|
|
2020-10-19 18:21:20 +02:00
|
|
|
if (acceptsock->accept_recv_req)
|
|
|
|
{
|
|
|
|
release_object( acceptsock );
|
2021-05-05 04:42:31 +02:00
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
2020-10-19 18:21:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-09 00:40:41 +01:00
|
|
|
if (!(req = alloc_accept_req( sock, acceptsock, async, params )))
|
2021-01-22 01:16:42 +01:00
|
|
|
{
|
|
|
|
release_object( acceptsock );
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-19 18:21:18 +02:00
|
|
|
list_add_tail( &sock->accept_list, &req->entry );
|
|
|
|
acceptsock->accept_recv_req = req;
|
2020-10-02 06:14:00 +02:00
|
|
|
release_object( acceptsock );
|
2020-10-19 18:21:18 +02:00
|
|
|
|
|
|
|
acceptsock->wparam = params->accept_handle;
|
2021-02-09 00:40:42 +01:00
|
|
|
async_set_completion_callback( async, free_accept_req, req );
|
2020-10-19 18:21:18 +02:00
|
|
|
queue_async( &sock->accept_q, async );
|
|
|
|
sock_reselect( sock );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return 1;
|
2020-10-02 06:14:00 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 05:54:39 +02:00
|
|
|
case IOCTL_AFD_LISTEN:
|
|
|
|
{
|
|
|
|
const struct afd_listen_params *params = get_req_data();
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(*params))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-17 06:33:09 +02:00
|
|
|
if (!sock->bound)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-14 05:54:39 +02:00
|
|
|
if (listen( unix_fd, params->backlog ) < 0)
|
|
|
|
{
|
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = SOCK_LISTENING;
|
2021-05-14 05:54:39 +02:00
|
|
|
|
2021-05-27 06:36:47 +02:00
|
|
|
/* a listening socket can no longer be accepted into */
|
|
|
|
allow_fd_caching( sock->fd );
|
|
|
|
|
2021-06-14 07:41:47 +02:00
|
|
|
/* we may already be selecting for AFD_POLL_ACCEPT */
|
2021-05-14 05:54:39 +02:00
|
|
|
sock_reselect( sock );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-18 04:29:59 +02:00
|
|
|
case IOCTL_AFD_WINE_CONNECT:
|
|
|
|
{
|
|
|
|
const struct afd_connect_params *params = get_req_data();
|
2021-06-17 06:33:10 +02:00
|
|
|
const struct WS_sockaddr *addr;
|
2021-06-16 06:13:28 +02:00
|
|
|
union unix_sockaddr unix_addr;
|
2021-05-18 04:29:59 +02:00
|
|
|
struct connect_req *req;
|
2021-06-16 06:13:28 +02:00
|
|
|
socklen_t unix_len;
|
2021-05-18 04:29:59 +02:00
|
|
|
int send_len, ret;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(*params) ||
|
|
|
|
get_req_data_size() - sizeof(*params) < params->addr_len)
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
send_len = get_req_data_size() - sizeof(*params) - params->addr_len;
|
2021-06-17 06:33:10 +02:00
|
|
|
addr = (const struct WS_sockaddr *)(params + 1);
|
2021-05-18 04:29:59 +02:00
|
|
|
|
2021-06-17 06:33:11 +02:00
|
|
|
if (!params->synchronous && !sock->bound)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-18 04:29:59 +02:00
|
|
|
if (sock->accept_recv_req)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->connect_req)
|
|
|
|
{
|
2021-06-10 19:36:36 +02:00
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
2021-05-18 04:29:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:48:43 +02:00
|
|
|
switch (sock->state)
|
2021-06-10 19:36:37 +02:00
|
|
|
{
|
2021-07-07 06:48:43 +02:00
|
|
|
case SOCK_LISTENING:
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SOCK_CONNECTING:
|
|
|
|
/* FIXME: STATUS_ADDRESS_ALREADY_ASSOCIATED probably isn't right,
|
|
|
|
* but there's no status code that maps to WSAEALREADY... */
|
|
|
|
set_error( params->synchronous ? STATUS_ADDRESS_ALREADY_ASSOCIATED : STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SOCK_CONNECTED:
|
|
|
|
set_error( STATUS_CONNECTION_ACTIVE );
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SOCK_UNCONNECTED:
|
|
|
|
case SOCK_CONNECTIONLESS:
|
|
|
|
break;
|
2021-06-10 19:36:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 06:33:10 +02:00
|
|
|
unix_len = sockaddr_to_unix( addr, params->addr_len, &unix_addr );
|
|
|
|
if (!unix_len)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_ADDRESS );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (unix_addr.addr.sa_family == AF_INET && !memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 ))
|
|
|
|
unix_addr.in.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
|
|
|
|
|
|
|
|
ret = connect( unix_fd, &unix_addr.addr, unix_len );
|
2021-05-18 04:29:59 +02:00
|
|
|
if (ret < 0 && errno != EINPROGRESS)
|
|
|
|
{
|
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-27 06:36:47 +02:00
|
|
|
/* a connected or connecting socket can no longer be accepted into */
|
|
|
|
allow_fd_caching( sock->fd );
|
|
|
|
|
2021-06-16 06:13:28 +02:00
|
|
|
unix_len = sizeof(unix_addr);
|
|
|
|
if (!sock->bound && !getsockname( unix_fd, &unix_addr.addr, &unix_len ))
|
|
|
|
sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
|
|
|
|
sock->bound = 1;
|
|
|
|
|
2021-05-18 04:29:59 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = SOCK_CONNECTED;
|
2021-05-18 04:29:59 +02:00
|
|
|
|
|
|
|
if (!send_len) return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(req = mem_alloc( sizeof(*req) )))
|
|
|
|
return 0;
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
sock->state = SOCK_CONNECTING;
|
2021-05-18 04:29:59 +02:00
|
|
|
|
2021-06-11 02:36:24 +02:00
|
|
|
if (params->synchronous && sock->nonblocking)
|
2021-05-18 04:29:59 +02:00
|
|
|
{
|
|
|
|
sock_reselect( sock );
|
|
|
|
set_error( STATUS_DEVICE_NOT_READY );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->async = (struct async *)grab_object( async );
|
|
|
|
req->iosb = async_get_iosb( async );
|
|
|
|
req->sock = (struct sock *)grab_object( sock );
|
|
|
|
req->addr_len = params->addr_len;
|
|
|
|
req->send_len = send_len;
|
|
|
|
req->send_cursor = 0;
|
|
|
|
|
|
|
|
async_set_completion_callback( async, free_connect_req, req );
|
|
|
|
sock->connect_req = req;
|
|
|
|
queue_async( &sock->connect_q, async );
|
|
|
|
sock_reselect( sock );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-19 05:35:49 +02:00
|
|
|
case IOCTL_AFD_WINE_SHUTDOWN:
|
|
|
|
{
|
|
|
|
unsigned int how;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(int))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
how = *(int *)get_req_data();
|
|
|
|
|
|
|
|
if (how > SD_BOTH)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-11 02:36:26 +02:00
|
|
|
if (sock->state != SOCK_CONNECTED && sock->state != SOCK_CONNECTIONLESS)
|
2021-05-19 05:35:49 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_CONNECTION );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (how != SD_SEND)
|
|
|
|
{
|
2021-06-11 02:36:25 +02:00
|
|
|
sock->rd_shutdown = 1;
|
2021-05-19 05:35:49 +02:00
|
|
|
}
|
|
|
|
if (how != SD_RECEIVE)
|
|
|
|
{
|
2021-06-11 02:36:25 +02:00
|
|
|
sock->wr_shutdown = 1;
|
2021-05-19 05:35:49 +02:00
|
|
|
if (list_empty( &sock->write_q.queue ))
|
|
|
|
shutdown( unix_fd, SHUT_WR );
|
|
|
|
else
|
|
|
|
sock->wr_shutdown_pending = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (how == SD_BOTH)
|
|
|
|
{
|
|
|
|
if (sock->event) release_object( sock->event );
|
|
|
|
sock->event = NULL;
|
|
|
|
sock->window = 0;
|
|
|
|
sock->mask = 0;
|
2021-06-11 02:36:24 +02:00
|
|
|
sock->nonblocking = 1;
|
2021-05-19 05:35:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sock_reselect( sock );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-14 05:54:37 +02:00
|
|
|
case IOCTL_AFD_WINE_ADDRESS_LIST_CHANGE:
|
2021-06-04 23:24:18 +02:00
|
|
|
{
|
|
|
|
int force_async;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(int))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
force_async = *(int *)get_req_data();
|
|
|
|
|
2021-06-11 02:36:24 +02:00
|
|
|
if (sock->nonblocking && !force_async)
|
2020-09-25 05:21:02 +02:00
|
|
|
{
|
2021-05-05 04:42:32 +02:00
|
|
|
set_error( STATUS_DEVICE_NOT_READY );
|
2020-09-25 05:21:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!sock_get_ifchange( sock )) return 0;
|
|
|
|
queue_async( &sock->ifchange_q, async );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return 1;
|
2021-06-04 23:24:18 +02:00
|
|
|
}
|
2020-09-25 05:21:02 +02:00
|
|
|
|
2021-06-02 17:30:15 +02:00
|
|
|
case IOCTL_AFD_WINE_FIONBIO:
|
|
|
|
if (get_req_data_size() < sizeof(int))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (*(int *)get_req_data())
|
|
|
|
{
|
2021-06-11 02:36:24 +02:00
|
|
|
sock->nonblocking = 1;
|
2021-06-02 17:30:15 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sock->mask)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-11 02:36:24 +02:00
|
|
|
sock->nonblocking = 0;
|
2021-06-02 17:30:15 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
2021-06-15 17:39:43 +02:00
|
|
|
case IOCTL_AFD_GET_EVENTS:
|
|
|
|
{
|
|
|
|
struct afd_get_events_params params = {0};
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(params))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
params.flags = sock->pending_events & sock->mask;
|
|
|
|
for (i = 0; i < ARRAY_SIZE( params.status ); ++i)
|
|
|
|
params.status[i] = sock_get_ntstatus( sock->errors[i] );
|
|
|
|
|
|
|
|
sock->pending_events = 0;
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
|
|
|
set_reply_data( ¶ms, sizeof(params) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-14 07:41:49 +02:00
|
|
|
case IOCTL_AFD_EVENT_SELECT:
|
|
|
|
{
|
|
|
|
struct event *event = NULL;
|
|
|
|
obj_handle_t event_handle;
|
|
|
|
int mask;
|
|
|
|
|
|
|
|
if (is_machine_64bit( current->process->machine ))
|
|
|
|
{
|
|
|
|
const struct afd_event_select_params_64 *params = get_req_data();
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(params))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_handle = params->event;
|
|
|
|
mask = params->mask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const struct afd_event_select_params_32 *params = get_req_data();
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(params))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_handle = params->event;
|
|
|
|
mask = params->mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((event_handle || mask) &&
|
|
|
|
!(event = get_event_obj( current->process, event_handle, EVENT_MODIFY_STATE )))
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->event) release_object( sock->event );
|
|
|
|
sock->event = event;
|
|
|
|
sock->mask = mask;
|
|
|
|
sock->window = 0;
|
|
|
|
sock->message = 0;
|
|
|
|
sock->wparam = 0;
|
|
|
|
sock->nonblocking = 1;
|
2021-06-15 17:39:47 +02:00
|
|
|
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_AFD_WINE_MESSAGE_SELECT:
|
|
|
|
{
|
|
|
|
const struct afd_message_select_params *params = get_req_data();
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(params))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock->event) release_object( sock->event );
|
|
|
|
|
|
|
|
if (params->window)
|
|
|
|
{
|
|
|
|
sock->pending_events = 0;
|
|
|
|
sock->reported_events = 0;
|
|
|
|
}
|
|
|
|
sock->event = NULL;
|
|
|
|
sock->mask = params->mask;
|
|
|
|
sock->window = params->window;
|
|
|
|
sock->message = params->message;
|
|
|
|
sock->wparam = params->handle;
|
|
|
|
sock->nonblocking = 1;
|
2021-06-14 07:41:49 +02:00
|
|
|
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:13:27 +02:00
|
|
|
case IOCTL_AFD_BIND:
|
|
|
|
{
|
|
|
|
const struct afd_bind_params *params = get_req_data();
|
2021-06-16 06:13:28 +02:00
|
|
|
union unix_sockaddr unix_addr, bind_addr;
|
2021-06-16 06:13:27 +02:00
|
|
|
data_size_t in_size;
|
|
|
|
socklen_t unix_len;
|
|
|
|
|
|
|
|
/* the ioctl is METHOD_NEITHER, so ntdll gives us the output buffer as
|
|
|
|
* input */
|
|
|
|
if (get_req_data_size() < get_reply_max_size())
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
in_size = get_req_data_size() - get_reply_max_size();
|
|
|
|
if (in_size < offsetof(struct afd_bind_params, addr.sa_data)
|
2021-07-23 18:39:07 +02:00
|
|
|
|| get_reply_max_size() < in_size - sizeof(int))
|
2021-06-16 06:13:27 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:13:31 +02:00
|
|
|
if (sock->bound)
|
|
|
|
{
|
|
|
|
set_error( STATUS_ADDRESS_ALREADY_ASSOCIATED );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:13:27 +02:00
|
|
|
unix_len = sockaddr_to_unix( ¶ms->addr, in_size - sizeof(int), &unix_addr );
|
|
|
|
if (!unix_len)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_ADDRESS );
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-16 06:13:28 +02:00
|
|
|
bind_addr = unix_addr;
|
2021-06-16 06:13:27 +02:00
|
|
|
|
2021-07-22 03:34:54 +02:00
|
|
|
if (unix_addr.addr.sa_family == AF_INET)
|
2021-06-16 06:13:27 +02:00
|
|
|
{
|
|
|
|
if (!memcmp( &unix_addr.in.sin_addr, magic_loopback_addr, 4 )
|
|
|
|
|| bind_to_interface( sock, &unix_addr.in ))
|
2021-06-16 06:13:28 +02:00
|
|
|
bind_addr.in.sin_addr.s_addr = htonl( INADDR_ANY );
|
2021-06-16 06:13:27 +02:00
|
|
|
}
|
2021-07-22 03:34:54 +02:00
|
|
|
else if (unix_addr.addr.sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
|
|
|
|
/* Windows allows specifying zero to use the default scope. Linux
|
|
|
|
* interprets it as an interface index and requires that it be
|
|
|
|
* nonzero. */
|
|
|
|
if (!unix_addr.in6.sin6_scope_id)
|
|
|
|
bind_addr.in6.sin6_scope_id = get_ipv6_interface_index( &unix_addr.in6.sin6_addr );
|
|
|
|
#endif
|
|
|
|
}
|
2021-06-16 06:13:27 +02:00
|
|
|
|
2021-06-16 06:13:28 +02:00
|
|
|
if (bind( unix_fd, &bind_addr.addr, unix_len ) < 0)
|
2021-06-16 06:13:27 +02:00
|
|
|
{
|
|
|
|
if (errno == EADDRINUSE)
|
|
|
|
{
|
|
|
|
int reuse;
|
|
|
|
socklen_t len = sizeof(reuse);
|
|
|
|
|
|
|
|
if (!getsockopt( unix_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, &len ) && reuse)
|
|
|
|
errno = EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
2021-06-16 06:13:28 +02:00
|
|
|
return 1;
|
2021-06-16 06:13:27 +02:00
|
|
|
}
|
2021-06-16 06:13:28 +02:00
|
|
|
|
|
|
|
sock->bound = 1;
|
|
|
|
|
|
|
|
unix_len = sizeof(bind_addr);
|
|
|
|
if (!getsockname( unix_fd, &bind_addr.addr, &unix_len ))
|
|
|
|
{
|
|
|
|
/* store the interface or magic loopback address instead of the
|
|
|
|
* actual unix address */
|
|
|
|
if (bind_addr.addr.sa_family == AF_INET)
|
|
|
|
bind_addr.in.sin_addr = unix_addr.in.sin_addr;
|
|
|
|
sock->addr_len = sockaddr_from_unix( &bind_addr, &sock->addr.addr, sizeof(sock->addr) );
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:13:29 +02:00
|
|
|
if (get_reply_max_size() >= sock->addr_len)
|
|
|
|
set_reply_data( &sock->addr, sock->addr_len );
|
2021-06-16 06:13:27 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:13:28 +02:00
|
|
|
case IOCTL_AFD_GETSOCKNAME:
|
|
|
|
if (!sock->bound)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sock->addr_len)
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_reply_data( &sock->addr, sock->addr_len );
|
|
|
|
return 1;
|
|
|
|
|
2021-06-18 03:58:22 +02:00
|
|
|
case IOCTL_AFD_WINE_DEFER:
|
|
|
|
{
|
|
|
|
const obj_handle_t *handle = get_req_data();
|
|
|
|
struct sock *acceptsock;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(*handle))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptsock = (struct sock *)get_handle_obj( current->process, *handle, 0, &sock_ops );
|
|
|
|
if (!acceptsock) return 0;
|
|
|
|
|
|
|
|
sock->deferred = acceptsock;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-20 18:05:55 +02:00
|
|
|
case IOCTL_AFD_WINE_GET_INFO:
|
|
|
|
{
|
|
|
|
struct afd_get_info_params params;
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(params))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
params.family = sock->family;
|
|
|
|
params.type = sock->type;
|
|
|
|
params.protocol = sock->proto;
|
|
|
|
set_reply_data( ¶ms, sizeof(params) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-23 18:23:42 +02:00
|
|
|
case IOCTL_AFD_WINE_GET_SO_ACCEPTCONN:
|
|
|
|
{
|
|
|
|
int listening = (sock->state == SOCK_LISTENING);
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(listening))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_reply_data( &listening, sizeof(listening) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-24 07:00:10 +02:00
|
|
|
case IOCTL_AFD_WINE_GET_SO_ERROR:
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
socklen_t len = sizeof(error);
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(error))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getsockopt( unix_fd, SOL_SOCKET, SO_ERROR, (char *)&error, &len ) < 0)
|
|
|
|
{
|
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
for (i = 0; i < ARRAY_SIZE( sock->errors ); ++i)
|
|
|
|
{
|
|
|
|
if (sock->errors[i])
|
|
|
|
{
|
2021-07-06 02:14:17 +02:00
|
|
|
error = sock_get_error( sock->errors[i] );
|
2021-06-24 07:00:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_reply_data( &error, sizeof(error) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-26 03:21:06 +02:00
|
|
|
case IOCTL_AFD_WINE_GET_SO_RCVBUF:
|
|
|
|
{
|
|
|
|
int rcvbuf = sock->rcvbuf;
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(rcvbuf))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_reply_data( &rcvbuf, sizeof(rcvbuf) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-26 03:21:05 +02:00
|
|
|
case IOCTL_AFD_WINE_SET_SO_RCVBUF:
|
|
|
|
{
|
|
|
|
DWORD rcvbuf;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(rcvbuf))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rcvbuf = *(DWORD *)get_req_data();
|
|
|
|
|
2021-06-26 03:21:06 +02:00
|
|
|
if (!setsockopt( unix_fd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf, sizeof(rcvbuf) ))
|
|
|
|
sock->rcvbuf = rcvbuf;
|
|
|
|
else
|
2021-06-26 03:21:05 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-29 18:43:20 +02:00
|
|
|
case IOCTL_AFD_WINE_GET_SO_RCVTIMEO:
|
|
|
|
{
|
|
|
|
DWORD rcvtimeo = sock->rcvtimeo;
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(rcvtimeo))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_reply_data( &rcvtimeo, sizeof(rcvtimeo) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_AFD_WINE_SET_SO_RCVTIMEO:
|
|
|
|
{
|
|
|
|
DWORD rcvtimeo;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(rcvtimeo))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rcvtimeo = *(DWORD *)get_req_data();
|
|
|
|
|
|
|
|
sock->rcvtimeo = rcvtimeo;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-30 06:25:40 +02:00
|
|
|
case IOCTL_AFD_WINE_GET_SO_SNDBUF:
|
|
|
|
{
|
|
|
|
int sndbuf = sock->sndbuf;
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(sndbuf))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_reply_data( &sndbuf, sizeof(sndbuf) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-30 06:25:39 +02:00
|
|
|
case IOCTL_AFD_WINE_SET_SO_SNDBUF:
|
|
|
|
{
|
|
|
|
DWORD sndbuf;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(sndbuf))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sndbuf = *(DWORD *)get_req_data();
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (!sndbuf)
|
|
|
|
{
|
|
|
|
/* setsockopt fails if a zero value is passed */
|
2021-06-30 06:25:40 +02:00
|
|
|
sock->sndbuf = sndbuf;
|
2021-06-30 06:25:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-30 06:25:40 +02:00
|
|
|
if (!setsockopt( unix_fd, SOL_SOCKET, SO_SNDBUF, (char *)&sndbuf, sizeof(sndbuf) ))
|
|
|
|
sock->sndbuf = sndbuf;
|
|
|
|
else
|
2021-06-30 06:25:39 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-30 06:25:43 +02:00
|
|
|
case IOCTL_AFD_WINE_GET_SO_SNDTIMEO:
|
|
|
|
{
|
|
|
|
DWORD sndtimeo = sock->sndtimeo;
|
|
|
|
|
|
|
|
if (get_reply_max_size() < sizeof(sndtimeo))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_reply_data( &sndtimeo, sizeof(sndtimeo) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IOCTL_AFD_WINE_SET_SO_SNDTIMEO:
|
|
|
|
{
|
|
|
|
DWORD sndtimeo;
|
|
|
|
|
|
|
|
if (get_req_data_size() < sizeof(sndtimeo))
|
|
|
|
{
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sndtimeo = *(DWORD *)get_req_data();
|
|
|
|
|
|
|
|
sock->sndtimeo = sndtimeo;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:21:02 +02:00
|
|
|
default:
|
|
|
|
set_error( STATUS_NOT_SUPPORTED );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 02:14:18 +02:00
|
|
|
static int poll_single_socket( struct sock *sock, int mask )
|
|
|
|
{
|
|
|
|
struct pollfd pollfd;
|
|
|
|
|
|
|
|
pollfd.fd = get_unix_fd( sock->fd );
|
|
|
|
pollfd.events = poll_flags_from_afd( sock, mask );
|
|
|
|
if (pollfd.events < 0 || poll( &pollfd, 1, 0 ) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((mask & AFD_POLL_HUP) && (pollfd.revents & POLLIN) && sock->type == WS_SOCK_STREAM)
|
|
|
|
{
|
|
|
|
char dummy;
|
|
|
|
|
|
|
|
if (!recv( get_unix_fd( sock->fd ), &dummy, 1, MSG_PEEK ))
|
|
|
|
{
|
|
|
|
pollfd.revents &= ~POLLIN;
|
|
|
|
pollfd.revents |= POLLHUP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_poll_flags( sock, pollfd.revents ) & mask;
|
|
|
|
}
|
|
|
|
|
2021-05-25 06:13:41 +02:00
|
|
|
static int poll_socket( struct sock *poll_sock, struct async *async, timeout_t timeout,
|
|
|
|
unsigned int count, const struct poll_socket_input *input )
|
|
|
|
{
|
|
|
|
struct poll_socket_output *output;
|
|
|
|
struct poll_req *req;
|
|
|
|
unsigned int i, j;
|
|
|
|
|
|
|
|
if (!(output = mem_alloc( count * sizeof(*output) )))
|
|
|
|
return 0;
|
|
|
|
memset( output, 0, count * sizeof(*output) );
|
|
|
|
|
|
|
|
if (!(req = mem_alloc( offsetof( struct poll_req, sockets[count] ) )))
|
|
|
|
{
|
|
|
|
free( output );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->timeout = NULL;
|
|
|
|
if (timeout && timeout != TIMEOUT_INFINITE &&
|
|
|
|
!(req->timeout = add_timeout_user( timeout, async_poll_timeout, req )))
|
|
|
|
{
|
|
|
|
free( req );
|
|
|
|
free( output );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
req->sockets[i].sock = (struct sock *)get_handle_obj( current->process, input[i].socket, 0, &sock_ops );
|
|
|
|
if (!req->sockets[i].sock)
|
|
|
|
{
|
|
|
|
for (j = 0; j < i; ++j) release_object( req->sockets[i].sock );
|
|
|
|
if (req->timeout) remove_timeout_user( req->timeout );
|
|
|
|
free( req );
|
|
|
|
free( output );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
req->sockets[i].flags = input[i].flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
req->count = count;
|
|
|
|
req->async = (struct async *)grab_object( async );
|
|
|
|
req->iosb = async_get_iosb( async );
|
|
|
|
req->output = output;
|
|
|
|
|
|
|
|
list_add_tail( &poll_list, &req->entry );
|
|
|
|
async_set_completion_callback( async, free_poll_req, req );
|
|
|
|
queue_async( &poll_sock->poll_q, async );
|
|
|
|
|
|
|
|
if (!timeout) req->iosb->status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
struct sock *sock = req->sockets[i].sock;
|
2021-07-06 02:14:18 +02:00
|
|
|
int mask = req->sockets[i].flags;
|
|
|
|
int flags = poll_single_socket( sock, mask );
|
2021-05-25 06:13:41 +02:00
|
|
|
|
|
|
|
if (flags)
|
|
|
|
{
|
|
|
|
req->iosb->status = STATUS_SUCCESS;
|
|
|
|
output[i].flags = flags;
|
|
|
|
output[i].status = sock_get_ntstatus( sock_error( sock->fd ) );
|
|
|
|
}
|
2021-07-06 02:14:19 +02:00
|
|
|
|
|
|
|
/* FIXME: do other error conditions deserve a similar treatment? */
|
|
|
|
if (sock->state != SOCK_CONNECTING && sock->errors[AFD_POLL_BIT_CONNECT_ERR] && (mask & AFD_POLL_CONNECT_ERR))
|
|
|
|
{
|
|
|
|
req->iosb->status = STATUS_SUCCESS;
|
|
|
|
output[i].flags |= AFD_POLL_CONNECT_ERR;
|
|
|
|
output[i].status = sock_get_ntstatus( sock->errors[AFD_POLL_BIT_CONNECT_ERR] );
|
|
|
|
}
|
2021-05-25 06:13:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req->iosb->status != STATUS_PENDING)
|
|
|
|
{
|
|
|
|
req->iosb->out_data = output;
|
|
|
|
req->iosb->out_size = count * sizeof(*output);
|
|
|
|
async_terminate( req->async, STATUS_ALERTED );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < req->count; ++i)
|
|
|
|
sock_reselect( req->sockets[i].sock );
|
|
|
|
set_error( STATUS_PENDING );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-05-06 16:49:52 +02:00
|
|
|
#ifdef HAVE_LINUX_RTNETLINK_H
|
|
|
|
|
|
|
|
/* only keep one ifchange object around, all sockets waiting for wakeups will look to it */
|
|
|
|
static struct object *ifchange_object;
|
|
|
|
|
|
|
|
static void ifchange_dump( struct object *obj, int verbose );
|
|
|
|
static struct fd *ifchange_get_fd( struct object *obj );
|
|
|
|
static void ifchange_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static int ifchange_get_poll_events( struct fd *fd );
|
|
|
|
static void ifchange_poll_event( struct fd *fd, int event );
|
|
|
|
|
|
|
|
struct ifchange
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
struct fd *fd; /* interface change file descriptor */
|
|
|
|
struct list sockets; /* list of sockets to send interface change notifications */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct object_ops ifchange_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct ifchange), /* size */
|
2021-02-04 13:44:18 +01:00
|
|
|
&no_type, /* type */
|
2014-05-06 16:49:52 +02:00
|
|
|
ifchange_dump, /* dump */
|
2021-03-18 06:41:23 +01:00
|
|
|
no_add_queue, /* add_queue */
|
2014-05-06 16:49:52 +02:00
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
no_satisfied, /* satisfied */
|
|
|
|
no_signal, /* signal */
|
|
|
|
ifchange_get_fd, /* get_fd */
|
2021-02-05 12:10:44 +01:00
|
|
|
default_map_access, /* map_access */
|
2014-05-06 16:49:52 +02:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 16:52:31 +02:00
|
|
|
no_get_full_name, /* get_full_name */
|
2014-05-06 16:49:52 +02:00
|
|
|
no_lookup_name, /* lookup_name */
|
2016-02-04 13:07:19 +01:00
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
2014-05-06 16:49:52 +02:00
|
|
|
no_open_file, /* open_file */
|
2019-03-25 14:37:09 +01:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2014-05-06 16:49:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
|
|
|
ifchange_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct fd_ops ifchange_fd_ops =
|
|
|
|
{
|
|
|
|
ifchange_get_poll_events, /* get_poll_events */
|
|
|
|
ifchange_poll_event, /* poll_event */
|
|
|
|
NULL, /* get_fd_type */
|
2015-05-05 05:17:45 +02:00
|
|
|
no_fd_read, /* read */
|
|
|
|
no_fd_write, /* write */
|
|
|
|
no_fd_flush, /* flush */
|
2017-12-21 16:12:01 +01:00
|
|
|
no_fd_get_file_info, /* get_file_info */
|
2017-10-02 16:42:16 +02:00
|
|
|
no_fd_get_volume_info, /* get_volume_info */
|
2015-05-05 05:17:45 +02:00
|
|
|
no_fd_ioctl, /* ioctl */
|
2014-05-06 16:49:52 +02:00
|
|
|
NULL, /* queue_async */
|
2016-12-01 12:10:34 +01:00
|
|
|
NULL /* reselect_async */
|
2014-05-06 16:49:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void ifchange_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
assert( obj->ops == &ifchange_ops );
|
|
|
|
fprintf( stderr, "Interface change\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct fd *ifchange_get_fd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct ifchange *ifchange = (struct ifchange *)obj;
|
|
|
|
return (struct fd *)grab_object( ifchange->fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ifchange_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct ifchange *ifchange = (struct ifchange *)obj;
|
|
|
|
assert( obj->ops == &ifchange_ops );
|
|
|
|
|
|
|
|
release_object( ifchange->fd );
|
|
|
|
|
|
|
|
/* reset the global ifchange object so that it will be recreated if it is needed again */
|
|
|
|
assert( obj == ifchange_object );
|
|
|
|
ifchange_object = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ifchange_get_poll_events( struct fd *fd )
|
2014-05-06 16:39:18 +02:00
|
|
|
{
|
2014-05-06 16:49:52 +02:00
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wake up all the sockets waiting for a change notification event */
|
|
|
|
static void ifchange_wake_up( struct object *obj, unsigned int status )
|
|
|
|
{
|
|
|
|
struct ifchange *ifchange = (struct ifchange *)obj;
|
|
|
|
struct list *ptr, *next;
|
|
|
|
assert( obj->ops == &ifchange_ops );
|
|
|
|
assert( obj == ifchange_object );
|
|
|
|
|
|
|
|
LIST_FOR_EACH_SAFE( ptr, next, &ifchange->sockets )
|
|
|
|
{
|
|
|
|
struct sock *sock = LIST_ENTRY( ptr, struct sock, ifchange_entry );
|
|
|
|
|
2017-06-30 15:08:09 +02:00
|
|
|
assert( sock->ifchange_obj );
|
2017-07-04 15:26:19 +02:00
|
|
|
async_wake_up( &sock->ifchange_q, status ); /* issue ifchange notification for the socket */
|
2017-06-30 15:08:09 +02:00
|
|
|
sock_release_ifchange( sock ); /* remove socket from list and decrement ifchange refcount */
|
2014-05-06 16:49:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ifchange_poll_event( struct fd *fd, int event )
|
|
|
|
{
|
|
|
|
struct object *ifchange = get_fd_user( fd );
|
|
|
|
unsigned int status = STATUS_PENDING;
|
|
|
|
char buffer[PIPE_BUF];
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = recv( get_unix_fd(fd), buffer, sizeof(buffer), MSG_DONTWAIT );
|
|
|
|
if (r < 0)
|
|
|
|
{
|
2016-08-30 12:08:23 +02:00
|
|
|
if (errno == EWOULDBLOCK || (EWOULDBLOCK != EAGAIN && errno == EAGAIN))
|
|
|
|
return; /* retry when poll() says the socket is ready */
|
2014-05-06 16:49:52 +02:00
|
|
|
status = sock_get_ntstatus( errno );
|
|
|
|
}
|
|
|
|
else if (r > 0)
|
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
|
|
|
|
for (nlh = (struct nlmsghdr *)buffer; NLMSG_OK(nlh, r); nlh = NLMSG_NEXT(nlh, r))
|
|
|
|
{
|
|
|
|
if (nlh->nlmsg_type == NLMSG_DONE)
|
|
|
|
break;
|
|
|
|
if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
|
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else status = STATUS_CANCELLED;
|
|
|
|
|
|
|
|
if (status != STATUS_PENDING) ifchange_wake_up( ifchange, status );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* we only need one of these interface notification objects, all of the sockets dependent upon
|
|
|
|
* it will wake up when a notification event occurs */
|
|
|
|
static struct object *get_ifchange( void )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LINUX_RTNETLINK_H
|
|
|
|
struct ifchange *ifchange;
|
|
|
|
struct sockaddr_nl addr;
|
|
|
|
int unix_fd;
|
|
|
|
|
|
|
|
if (ifchange_object)
|
|
|
|
{
|
|
|
|
/* increment the refcount for each socket that uses the ifchange object */
|
|
|
|
return grab_object( ifchange_object );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the socket we need for processing interface change notifications */
|
|
|
|
unix_fd = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
|
|
|
|
if (unix_fd == -1)
|
|
|
|
{
|
2021-05-05 04:42:32 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ));
|
2014-05-06 16:49:52 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fcntl( unix_fd, F_SETFL, O_NONBLOCK ); /* make socket nonblocking */
|
|
|
|
memset( &addr, 0, sizeof(addr) );
|
|
|
|
addr.nl_family = AF_NETLINK;
|
|
|
|
addr.nl_groups = RTMGRP_IPV4_IFADDR;
|
|
|
|
/* bind the socket to the special netlink kernel interface */
|
|
|
|
if (bind( unix_fd, (struct sockaddr *)&addr, sizeof(addr) ) == -1)
|
|
|
|
{
|
|
|
|
close( unix_fd );
|
2021-05-05 04:42:32 +02:00
|
|
|
set_error( sock_get_ntstatus( errno ));
|
2014-05-06 16:49:52 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(ifchange = alloc_object( &ifchange_ops )))
|
|
|
|
{
|
|
|
|
close( unix_fd );
|
|
|
|
set_error( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
list_init( &ifchange->sockets );
|
|
|
|
if (!(ifchange->fd = create_anonymous_fd( &ifchange_fd_ops, unix_fd, &ifchange->obj, 0 )))
|
|
|
|
{
|
|
|
|
release_object( ifchange );
|
|
|
|
set_error( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
set_fd_events( ifchange->fd, POLLIN ); /* enable read wakeup on the file descriptor */
|
|
|
|
|
|
|
|
/* the ifchange object is now successfully configured */
|
|
|
|
ifchange_object = &ifchange->obj;
|
|
|
|
return &ifchange->obj;
|
|
|
|
#else
|
2014-05-06 16:39:18 +02:00
|
|
|
set_error( STATUS_NOT_SUPPORTED );
|
|
|
|
return NULL;
|
2014-05-06 16:49:52 +02:00
|
|
|
#endif
|
2014-05-06 16:39:18 +02:00
|
|
|
}
|
|
|
|
|
2014-05-06 16:49:52 +02:00
|
|
|
/* add the socket to the interface change notification list */
|
2014-05-06 16:39:18 +02:00
|
|
|
static void ifchange_add_sock( struct object *obj, struct sock *sock )
|
|
|
|
{
|
2014-05-06 16:49:52 +02:00
|
|
|
#ifdef HAVE_LINUX_RTNETLINK_H
|
|
|
|
struct ifchange *ifchange = (struct ifchange *)obj;
|
|
|
|
|
|
|
|
list_add_tail( &ifchange->sockets, &sock->ifchange_entry );
|
|
|
|
#endif
|
2014-05-06 16:39:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new ifchange queue for a specific socket or, if one already exists, reuse the existing one */
|
2017-06-30 15:08:09 +02:00
|
|
|
static struct object *sock_get_ifchange( struct sock *sock )
|
2014-05-06 16:39:18 +02:00
|
|
|
{
|
|
|
|
struct object *ifchange;
|
|
|
|
|
2017-06-30 15:08:09 +02:00
|
|
|
if (sock->ifchange_obj) /* reuse existing ifchange_obj for this socket */
|
|
|
|
return sock->ifchange_obj;
|
2014-05-06 16:39:18 +02:00
|
|
|
|
|
|
|
if (!(ifchange = get_ifchange()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* add the socket to the ifchange notification list */
|
|
|
|
ifchange_add_sock( ifchange, sock );
|
|
|
|
sock->ifchange_obj = ifchange;
|
2017-06-30 15:08:09 +02:00
|
|
|
return ifchange;
|
2014-05-06 16:39:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* destroy an existing ifchange queue for a specific socket */
|
2017-06-30 15:08:09 +02:00
|
|
|
static void sock_release_ifchange( struct sock *sock )
|
2014-05-06 16:39:18 +02:00
|
|
|
{
|
2017-06-30 15:08:09 +02:00
|
|
|
if (sock->ifchange_obj)
|
2014-05-06 16:39:18 +02:00
|
|
|
{
|
|
|
|
list_remove( &sock->ifchange_entry );
|
|
|
|
release_object( sock->ifchange_obj );
|
2017-06-30 15:08:09 +02:00
|
|
|
sock->ifchange_obj = NULL;
|
2014-05-06 16:39:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 00:13:34 +02:00
|
|
|
static void socket_device_dump( struct object *obj, int verbose );
|
2020-11-25 19:42:42 +01:00
|
|
|
static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
|
|
|
|
unsigned int attr, struct object *root );
|
2020-09-20 00:13:34 +02:00
|
|
|
static struct object *socket_device_open_file( struct object *obj, unsigned int access,
|
|
|
|
unsigned int sharing, unsigned int options );
|
|
|
|
|
|
|
|
static const struct object_ops socket_device_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct object), /* size */
|
2021-02-04 13:44:18 +01:00
|
|
|
&device_type, /* type */
|
2020-09-20 00:13:34 +02:00
|
|
|
socket_device_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
no_satisfied, /* satisfied */
|
|
|
|
no_signal, /* signal */
|
|
|
|
no_get_fd, /* get_fd */
|
2021-02-05 12:10:44 +01:00
|
|
|
default_map_access, /* map_access */
|
2020-09-20 00:13:34 +02:00
|
|
|
default_get_sd, /* get_sd */
|
|
|
|
default_set_sd, /* set_sd */
|
2020-09-22 16:52:31 +02:00
|
|
|
default_get_full_name, /* get_full_name */
|
2020-09-20 00:13:34 +02:00
|
|
|
socket_device_lookup_name, /* lookup_name */
|
|
|
|
directory_link_name, /* link_name */
|
|
|
|
default_unlink_name, /* unlink_name */
|
|
|
|
socket_device_open_file, /* open_file */
|
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
|
|
|
no_close_handle, /* close_handle */
|
|
|
|
no_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void socket_device_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
fputs( "Socket device\n", stderr );
|
|
|
|
}
|
|
|
|
|
2020-11-25 19:42:42 +01:00
|
|
|
static struct object *socket_device_lookup_name( struct object *obj, struct unicode_str *name,
|
|
|
|
unsigned int attr, struct object *root )
|
2020-09-20 00:13:34 +02:00
|
|
|
{
|
2021-05-25 06:13:40 +02:00
|
|
|
if (name) name->len = 0;
|
2020-09-20 00:13:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct object *socket_device_open_file( struct object *obj, unsigned int access,
|
|
|
|
unsigned int sharing, unsigned int options )
|
|
|
|
{
|
2020-09-20 00:13:35 +02:00
|
|
|
struct sock *sock;
|
|
|
|
|
|
|
|
if (!(sock = create_socket())) return NULL;
|
|
|
|
if (!(sock->fd = alloc_pseudo_fd( &sock_fd_ops, &sock->obj, options )))
|
|
|
|
{
|
|
|
|
release_object( sock );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &sock->obj;
|
2020-09-20 00:13:34 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 11:32:47 +02:00
|
|
|
struct object *create_socket_device( struct object *root, const struct unicode_str *name,
|
|
|
|
unsigned int attr, const struct security_descriptor *sd )
|
2020-09-20 00:13:34 +02:00
|
|
|
{
|
2020-09-23 11:32:47 +02:00
|
|
|
return create_named_object( root, &socket_device_ops, name, attr, sd );
|
2020-09-20 00:13:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-22 05:08:48 +02:00
|
|
|
DECL_HANDLER(recv_socket)
|
|
|
|
{
|
|
|
|
struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
|
|
|
|
unsigned int status = req->status;
|
|
|
|
timeout_t timeout = 0;
|
|
|
|
struct async *async;
|
|
|
|
struct fd *fd;
|
|
|
|
|
|
|
|
if (!sock) return;
|
|
|
|
fd = sock->fd;
|
|
|
|
|
|
|
|
/* recv() returned EWOULDBLOCK, i.e. no data available yet */
|
2021-06-11 02:36:24 +02:00
|
|
|
if (status == STATUS_DEVICE_NOT_READY && !sock->nonblocking)
|
2021-05-22 05:08:48 +02:00
|
|
|
{
|
|
|
|
/* Set a timeout on the async if necessary.
|
|
|
|
*
|
|
|
|
* We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
|
|
|
|
* If the client gave us STATUS_PENDING, it expects the async to always
|
|
|
|
* block (it was triggered by WSARecv*() with a valid OVERLAPPED
|
|
|
|
* structure) and for the timeout not to be respected. */
|
2021-06-29 18:43:20 +02:00
|
|
|
if (is_fd_overlapped( fd ))
|
|
|
|
timeout = (timeout_t)sock->rcvtimeo * -10000;
|
2021-05-22 05:08:48 +02:00
|
|
|
|
|
|
|
status = STATUS_PENDING;
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:53:42 +02:00
|
|
|
if ((status == STATUS_PENDING || status == STATUS_DEVICE_NOT_READY) && sock->rd_shutdown)
|
|
|
|
status = STATUS_PIPE_DISCONNECTED;
|
2021-05-22 05:08:48 +02:00
|
|
|
|
2021-06-14 07:41:47 +02:00
|
|
|
sock->pending_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
|
|
|
|
sock->reported_events &= ~(req->oob ? AFD_POLL_OOB : AFD_POLL_READ);
|
2021-05-22 05:08:48 +02:00
|
|
|
|
|
|
|
if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
|
|
|
|
{
|
|
|
|
int success = 0;
|
|
|
|
|
|
|
|
if (status == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
struct iosb *iosb = async_get_iosb( async );
|
|
|
|
iosb->result = req->total;
|
|
|
|
release_object( iosb );
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
else if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
set_error( status );
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
|
|
|
|
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
queue_async( &sock->read_q, async );
|
|
|
|
|
|
|
|
/* always reselect; we changed reported_events above */
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
|
|
|
reply->wait = async_handoff( async, success, NULL, 0 );
|
|
|
|
reply->options = get_fd_options( fd );
|
|
|
|
release_object( async );
|
|
|
|
}
|
|
|
|
release_object( sock );
|
|
|
|
}
|
2021-05-25 06:13:41 +02:00
|
|
|
|
|
|
|
DECL_HANDLER(poll_socket)
|
|
|
|
{
|
|
|
|
struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
|
|
|
|
const struct poll_socket_input *input = get_req_data();
|
|
|
|
struct async *async;
|
|
|
|
unsigned int count;
|
|
|
|
|
|
|
|
if (!sock) return;
|
|
|
|
|
|
|
|
count = get_req_data_size() / sizeof(*input);
|
|
|
|
|
|
|
|
if ((async = create_request_async( sock->fd, get_fd_comp_flags( sock->fd ), &req->async )))
|
|
|
|
{
|
|
|
|
reply->wait = async_handoff( async, poll_socket( sock, async, req->timeout, count, input ), NULL, 0 );
|
|
|
|
reply->options = get_fd_options( sock->fd );
|
|
|
|
release_object( async );
|
|
|
|
}
|
|
|
|
|
|
|
|
release_object( sock );
|
|
|
|
}
|
2021-05-27 06:36:53 +02:00
|
|
|
|
|
|
|
DECL_HANDLER(send_socket)
|
|
|
|
{
|
|
|
|
struct sock *sock = (struct sock *)get_handle_obj( current->process, req->async.handle, 0, &sock_ops );
|
|
|
|
unsigned int status = req->status;
|
|
|
|
timeout_t timeout = 0;
|
|
|
|
struct async *async;
|
|
|
|
struct fd *fd;
|
|
|
|
|
|
|
|
if (!sock) return;
|
|
|
|
fd = sock->fd;
|
|
|
|
|
2021-06-16 06:13:28 +02:00
|
|
|
if (sock->type == WS_SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
/* sendto() and sendmsg() implicitly binds a socket */
|
|
|
|
union unix_sockaddr unix_addr;
|
|
|
|
socklen_t unix_len = sizeof(unix_addr);
|
|
|
|
|
|
|
|
if (!sock->bound && !getsockname( get_unix_fd( fd ), &unix_addr.addr, &unix_len ))
|
|
|
|
sock->addr_len = sockaddr_from_unix( &unix_addr, &sock->addr.addr, sizeof(sock->addr) );
|
|
|
|
sock->bound = 1;
|
|
|
|
}
|
|
|
|
|
2021-05-27 06:36:53 +02:00
|
|
|
if (status != STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
/* send() calls only clear and reselect events if unsuccessful. */
|
2021-06-14 07:41:47 +02:00
|
|
|
sock->pending_events &= ~AFD_POLL_WRITE;
|
|
|
|
sock->reported_events &= ~AFD_POLL_WRITE;
|
2021-05-27 06:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we had a short write and the socket is nonblocking (and the client is
|
|
|
|
* not trying to force the operation to be asynchronous), return success.
|
|
|
|
* Windows actually refuses to send any data in this case, and returns
|
|
|
|
* EWOULDBLOCK, but we have no way of doing that. */
|
2021-06-11 02:36:24 +02:00
|
|
|
if (status == STATUS_DEVICE_NOT_READY && req->total && sock->nonblocking)
|
2021-05-27 06:36:53 +02:00
|
|
|
status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
/* send() returned EWOULDBLOCK or a short write, i.e. cannot send all data yet */
|
2021-06-11 02:36:24 +02:00
|
|
|
if (status == STATUS_DEVICE_NOT_READY && !sock->nonblocking)
|
2021-05-27 06:36:53 +02:00
|
|
|
{
|
|
|
|
/* Set a timeout on the async if necessary.
|
|
|
|
*
|
|
|
|
* We want to do this *only* if the client gave us STATUS_DEVICE_NOT_READY.
|
|
|
|
* If the client gave us STATUS_PENDING, it expects the async to always
|
|
|
|
* block (it was triggered by WSASend*() with a valid OVERLAPPED
|
|
|
|
* structure) and for the timeout not to be respected. */
|
2021-06-30 06:25:43 +02:00
|
|
|
if (is_fd_overlapped( fd ))
|
|
|
|
timeout = (timeout_t)sock->sndtimeo * -10000;
|
2021-05-27 06:36:53 +02:00
|
|
|
|
|
|
|
status = STATUS_PENDING;
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:53:42 +02:00
|
|
|
if ((status == STATUS_PENDING || status == STATUS_DEVICE_NOT_READY) && sock->wr_shutdown)
|
|
|
|
status = STATUS_PIPE_DISCONNECTED;
|
2021-05-27 06:36:53 +02:00
|
|
|
|
|
|
|
if ((async = create_request_async( fd, get_fd_comp_flags( fd ), &req->async )))
|
|
|
|
{
|
|
|
|
int success = 0;
|
|
|
|
|
|
|
|
if (status == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
struct iosb *iosb = async_get_iosb( async );
|
|
|
|
iosb->result = req->total;
|
|
|
|
release_object( iosb );
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
else if (status == STATUS_PENDING)
|
|
|
|
{
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
set_error( status );
|
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
async_set_timeout( async, timeout, STATUS_IO_TIMEOUT );
|
|
|
|
|
|
|
|
if (status == STATUS_PENDING)
|
|
|
|
queue_async( &sock->write_q, async );
|
|
|
|
|
|
|
|
/* always reselect; we changed reported_events above */
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
|
|
|
reply->wait = async_handoff( async, success, NULL, 0 );
|
|
|
|
reply->options = get_fd_options( fd );
|
|
|
|
release_object( async );
|
|
|
|
}
|
|
|
|
release_object( sock );
|
|
|
|
}
|