1999-10-13 18:05:37 +02:00
|
|
|
|
/*
|
|
|
|
|
* Server-side socket management
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 1999 Marcus Meissner, Ove K<EFBFBD>ven
|
|
|
|
|
*
|
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>
|
1999-10-25 00:13:47 +02:00
|
|
|
|
#ifdef HAVE_SYS_ERRNO_H
|
|
|
|
|
# include <sys/errno.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>
|
|
|
|
|
|
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"
|
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
|
|
|
|
|
2001-11-14 22:26:23 +01:00
|
|
|
|
/* To avoid conflicts with the Unix socket headers. Plus we only need a few
|
|
|
|
|
* macros anyway.
|
|
|
|
|
*/
|
|
|
|
|
#define USE_WS_PREFIX
|
|
|
|
|
#include "winsock2.h"
|
|
|
|
|
|
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 */
|
1999-10-13 18:05:37 +02:00
|
|
|
|
unsigned int state; /* status bits */
|
|
|
|
|
unsigned int mask; /* event mask */
|
|
|
|
|
unsigned int hmask; /* held (blocked) events */
|
|
|
|
|
unsigned int pmask; /* pending events */
|
2002-01-09 22:16:24 +01:00
|
|
|
|
unsigned int flags; /* socket flags */
|
2003-02-19 01:33:32 +01:00
|
|
|
|
int polling; /* is socket being polled? */
|
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) */
|
1999-10-13 18:05:37 +02:00
|
|
|
|
int errors[FD_MAX_EVENTS]; /* event errors */
|
2005-01-14 20:54:38 +01:00
|
|
|
|
struct sock *deferred; /* socket that waits for a deferred accept */
|
2005-02-24 18:06:31 +01:00
|
|
|
|
struct list read_q; /* queue for asynchronous reads */
|
|
|
|
|
struct list write_q; /* queue for asynchronous writes */
|
1999-10-13 18:05:37 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void sock_dump( struct object *obj, int verbose );
|
|
|
|
|
static int sock_signaled( struct object *obj, struct thread *thread );
|
2003-02-19 01:33:32 +01:00
|
|
|
|
static struct fd *sock_get_fd( struct object *obj );
|
2005-12-12 16:46:17 +01:00
|
|
|
|
static unsigned int sock_map_access( struct object *obj, unsigned int access );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
static void sock_destroy( struct object *obj );
|
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 );
|
2004-08-18 02:04:58 +02:00
|
|
|
|
static int sock_get_info( struct fd *fd );
|
2005-01-14 20:54:38 +01:00
|
|
|
|
static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
|
|
|
|
|
static void sock_cancel_async( struct fd *fd );
|
2003-02-14 21:27:09 +01:00
|
|
|
|
|
2000-03-26 20:21:16 +02:00
|
|
|
|
static int sock_get_error( int err );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
static void sock_set_error(void);
|
|
|
|
|
|
|
|
|
|
static const struct object_ops sock_ops =
|
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
|
sizeof(struct sock), /* size */
|
|
|
|
|
sock_dump, /* dump */
|
|
|
|
|
add_queue, /* add_queue */
|
|
|
|
|
remove_queue, /* remove_queue */
|
|
|
|
|
sock_signaled, /* signaled */
|
|
|
|
|
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 */
|
2005-12-12 16:46:17 +01:00
|
|
|
|
sock_map_access, /* map_access */
|
2005-11-22 15:55:42 +01:00
|
|
|
|
no_lookup_name, /* lookup_name */
|
2005-06-09 17:39:52 +02:00
|
|
|
|
no_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 */
|
|
|
|
|
no_flush, /* flush */
|
2001-10-05 21:45:45 +02:00
|
|
|
|
sock_get_info, /* get_file_info */
|
2005-01-14 20:54:38 +01:00
|
|
|
|
sock_queue_async, /* queue_async */
|
|
|
|
|
sock_cancel_async /* cancel_async */
|
1999-10-13 18:05:37 +02:00
|
|
|
|
};
|
|
|
|
|
|
2002-03-23 21:43:52 +01:00
|
|
|
|
|
|
|
|
|
/* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
|
|
|
|
|
* we post messages if there are multiple events. Used to send
|
|
|
|
|
* messages. The problem is if there is both a FD_CONNECT event and,
|
|
|
|
|
* say, an FD_READ event available on the same socket, we want to
|
|
|
|
|
* notify the app of the connect event first. Otherwise it may
|
|
|
|
|
* discard the read event because it thinks it hasn't connected yet.
|
|
|
|
|
*/
|
|
|
|
|
static const int event_bitorder[FD_MAX_EVENTS] =
|
|
|
|
|
{
|
|
|
|
|
FD_CONNECT_BIT,
|
|
|
|
|
FD_ACCEPT_BIT,
|
|
|
|
|
FD_OOB_BIT,
|
|
|
|
|
FD_WRITE_BIT,
|
|
|
|
|
FD_READ_BIT,
|
|
|
|
|
FD_CLOSE_BIT,
|
|
|
|
|
6, 7, 8, 9 /* leftovers */
|
|
|
|
|
};
|
|
|
|
|
|
2002-07-03 23:04:09 +02:00
|
|
|
|
/* Flags that make sense only for SOCK_STREAM sockets */
|
|
|
|
|
#define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if ( socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) ) goto out;
|
|
|
|
|
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;
|
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
|
n = poll( &pfd, 1, 0 );
|
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
|
|
|
|
if (!sock->polling) /* FIXME: should find a better way to do this */
|
|
|
|
|
{
|
2000-09-06 21:40:23 +02:00
|
|
|
|
/* previously unconnected socket, is this reselect supposed to connect it? */
|
2002-04-25 23:31:11 +02:00
|
|
|
|
if (!(sock->state & ~FD_WINE_NONBLOCKING)) return 0;
|
2000-09-06 21:40:23 +02:00
|
|
|
|
/* ok, it is, attach it to the wineserver's main poll loop */
|
2003-02-19 01:33:32 +01:00
|
|
|
|
sock->polling = 1;
|
2000-09-06 21:40:23 +02:00
|
|
|
|
}
|
|
|
|
|
/* update condition mask */
|
2003-02-19 01:33:32 +01:00
|
|
|
|
set_fd_events( sock->fd, ev );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
return ev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* After POLLHUP is received, the socket will no longer be in the main select loop.
|
|
|
|
|
This function is used to signal pending events nevertheless */
|
2005-01-14 20:54:38 +01:00
|
|
|
|
static void sock_try_event( struct sock *sock, int event )
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
2003-02-19 01:33:32 +01:00
|
|
|
|
event = check_fd_events( sock->fd, event );
|
2003-02-14 21:27:09 +01:00
|
|
|
|
if (event)
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if ( debug_level ) fprintf( stderr, "sock_try_event: %x\n", event );
|
|
|
|
|
sock_poll_event( sock->fd, event );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
}
|
2002-03-23 21:43:52 +01:00
|
|
|
|
}
|
2000-03-25 22:41:43 +01:00
|
|
|
|
|
2002-03-23 21:43:52 +01:00
|
|
|
|
/* wake anybody waiting on the socket event or send the associated message */
|
2002-04-25 23:31:11 +02:00
|
|
|
|
static void sock_wake_up( struct sock *sock, int pollev )
|
2002-03-23 21:43:52 +01:00
|
|
|
|
{
|
|
|
|
|
unsigned int events = sock->pmask & sock->mask;
|
|
|
|
|
int i;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
int async_active = 0;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
|
2004-08-18 01:37:55 +02:00
|
|
|
|
if ( sock->flags & WSA_FLAG_OVERLAPPED )
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
2005-02-24 18:06:31 +01:00
|
|
|
|
if ( pollev & (POLLIN|POLLPRI) && !list_empty( &sock->read_q ))
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if (debug_level) fprintf( stderr, "activating read queue for socket %p\n", sock );
|
2005-02-24 18:06:31 +01:00
|
|
|
|
async_terminate_head( &sock->read_q, STATUS_ALERTED );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
async_active = 1;
|
|
|
|
|
}
|
2005-02-24 18:06:31 +01:00
|
|
|
|
if ( pollev & POLLOUT && !list_empty( &sock->write_q ))
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if (debug_level) fprintf( stderr, "activating write queue for socket %p\n", sock );
|
2005-02-24 18:06:31 +01:00
|
|
|
|
async_terminate_head( &sock->write_q, STATUS_ALERTED );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
async_active = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Do not signal events if there are still pending asynchronous IO requests */
|
|
|
|
|
/* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
|
|
|
|
|
if ( !events || async_active ) return;
|
|
|
|
|
|
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 );
|
|
|
|
|
set_event( sock->event );
|
|
|
|
|
}
|
|
|
|
|
if (sock->window)
|
|
|
|
|
{
|
2002-10-19 03:00:59 +02:00
|
|
|
|
if (debug_level) fprintf(stderr, "signalling events %x win %p\n", events, sock->window );
|
2002-03-23 21:43:52 +01:00
|
|
|
|
for (i = 0; i < FD_MAX_EVENTS; i++)
|
|
|
|
|
{
|
|
|
|
|
int event = event_bitorder[i];
|
|
|
|
|
if (sock->pmask & (1 << event))
|
|
|
|
|
{
|
|
|
|
|
unsigned int lparam = (1 << event) | (sock->errors[event] << 16);
|
2006-06-08 10:01:54 +02:00
|
|
|
|
post_message( sock->window, sock->message, (unsigned long)sock->wparam, lparam );
|
2002-03-23 21:43:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sock->pmask = 0;
|
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
}
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
|
inline static int sock_error( struct fd *fd )
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
2000-03-26 20:21:16 +02:00
|
|
|
|
unsigned int optval = 0, optlen;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
optlen = sizeof(optval);
|
2003-02-14 21:27:09 +01:00
|
|
|
|
getsockopt( get_unix_fd(fd), SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen);
|
2000-03-26 20:21:16 +02:00
|
|
|
|
return optval ? sock_get_error(optval) : 0;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
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);
|
2001-08-24 21:14:56 +02:00
|
|
|
|
if (sock->state & FD_CONNECT)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
/* connecting */
|
1999-12-13 01:16:44 +01:00
|
|
|
|
if (event & POLLOUT)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
/* we got connected */
|
2001-08-24 21:14:56 +02:00
|
|
|
|
sock->state |= FD_WINE_CONNECTED|FD_READ|FD_WRITE;
|
|
|
|
|
sock->state &= ~FD_CONNECT;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
sock->pmask |= FD_CONNECT;
|
|
|
|
|
sock->errors[FD_CONNECT_BIT] = 0;
|
1999-10-23 18:53:34 +02:00
|
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
|
fprintf(stderr, "socket %p connection success\n", sock);
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
1999-12-13 01:16:44 +01:00
|
|
|
|
else if (event & (POLLERR|POLLHUP))
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
/* we didn't get connected? */
|
2001-08-24 21:14:56 +02:00
|
|
|
|
sock->state &= ~FD_CONNECT;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
sock->pmask |= FD_CONNECT;
|
2003-02-14 21:27:09 +01:00
|
|
|
|
sock->errors[FD_CONNECT_BIT] = sock_error( fd );
|
1999-10-23 18:53:34 +02:00
|
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
|
fprintf(stderr, "socket %p connection failure\n", sock);
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
2005-01-14 20:54:38 +01:00
|
|
|
|
}
|
|
|
|
|
else if (sock->state & FD_WINE_LISTENING)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
/* listening */
|
1999-12-13 01:16:44 +01:00
|
|
|
|
if (event & POLLIN)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
/* incoming connection */
|
|
|
|
|
sock->pmask |= FD_ACCEPT;
|
|
|
|
|
sock->errors[FD_ACCEPT_BIT] = 0;
|
|
|
|
|
sock->hmask |= FD_ACCEPT;
|
|
|
|
|
}
|
1999-12-13 01:16:44 +01:00
|
|
|
|
else if (event & (POLLERR|POLLHUP))
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
/* failed incoming connection? */
|
|
|
|
|
sock->pmask |= FD_ACCEPT;
|
2003-02-14 21:27:09 +01:00
|
|
|
|
sock->errors[FD_ACCEPT_BIT] = sock_error( fd );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
sock->hmask |= FD_ACCEPT;
|
|
|
|
|
}
|
2005-01-14 20:54:38 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
/* normal data flow */
|
2002-07-03 23:04:09 +02:00
|
|
|
|
if ( sock->type == 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 );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
if ( nr > 0 )
|
2001-08-24 01:25:33 +02:00
|
|
|
|
{
|
|
|
|
|
/* incoming data */
|
|
|
|
|
sock->pmask |= FD_READ;
|
2002-04-27 03:06:09 +02:00
|
|
|
|
sock->hmask |= (FD_READ|FD_CLOSE);
|
2001-08-24 01:25:33 +02:00
|
|
|
|
sock->errors[FD_READ_BIT] = 0;
|
|
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
|
fprintf(stderr, "socket %p is readable\n", sock );
|
2001-08-24 01:25:33 +02:00
|
|
|
|
}
|
2002-04-25 23:31:11 +02:00
|
|
|
|
else if ( nr == 0 )
|
2002-07-03 23:04:09 +02:00
|
|
|
|
hangup_seen = 1;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* EAGAIN can happen if an async recv() falls between the server's poll()
|
|
|
|
|
call and the invocation of this routine */
|
|
|
|
|
if ( errno == EAGAIN )
|
|
|
|
|
event &= ~POLLIN;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
event = POLLERR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
2002-07-03 23:04:09 +02:00
|
|
|
|
else if ( sock_shutdown_type == SOCK_SHUTDOWN_POLLHUP && (event & POLLHUP) )
|
|
|
|
|
{
|
|
|
|
|
hangup_seen = 1;
|
|
|
|
|
}
|
|
|
|
|
else if ( event & POLLIN ) /* POLLIN for non-stream socket */
|
|
|
|
|
{
|
|
|
|
|
sock->pmask |= FD_READ;
|
|
|
|
|
sock->hmask |= (FD_READ|FD_CLOSE);
|
|
|
|
|
sock->errors[FD_READ_BIT] = 0;
|
|
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
|
fprintf(stderr, "socket %p is readable\n", sock );
|
2002-07-03 23:04:09 +02:00
|
|
|
|
|
|
|
|
|
}
|
2002-06-21 21:06:28 +02:00
|
|
|
|
|
1999-12-13 01:16:44 +01:00
|
|
|
|
if (event & POLLOUT)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
sock->pmask |= FD_WRITE;
|
|
|
|
|
sock->hmask |= FD_WRITE;
|
|
|
|
|
sock->errors[FD_WRITE_BIT] = 0;
|
1999-10-23 18:53:34 +02:00
|
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
|
fprintf(stderr, "socket %p is writable\n", sock);
|
2000-01-01 01:56:27 +01:00
|
|
|
|
}
|
|
|
|
|
if (event & POLLPRI)
|
|
|
|
|
{
|
|
|
|
|
sock->pmask |= FD_OOB;
|
|
|
|
|
sock->hmask |= FD_OOB;
|
2000-03-25 22:41:43 +01:00
|
|
|
|
sock->errors[FD_OOB_BIT] = 0;
|
2000-01-01 01:56:27 +01:00
|
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
|
fprintf(stderr, "socket %p got OOB data\n", sock);
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
2002-04-25 23:31:11 +02:00
|
|
|
|
/* According to WS2 specs, FD_CLOSE is only delivered when there is
|
2002-07-03 23:04:09 +02:00
|
|
|
|
no more data to be read (i.e. hangup_seen = 1) */
|
|
|
|
|
else if ( hangup_seen && (sock->state & (FD_READ|FD_WRITE) ))
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
|
sock->errors[FD_CLOSE_BIT] = sock_error( fd );
|
2002-07-03 23:04:09 +02:00
|
|
|
|
if ( (event & POLLERR) || ( sock_shutdown_type == SOCK_SHUTDOWN_EOF && (event & POLLHUP) ))
|
2003-01-28 01:24:04 +01:00
|
|
|
|
sock->state &= ~FD_WRITE;
|
2000-01-01 01:56:27 +01:00
|
|
|
|
sock->pmask |= FD_CLOSE;
|
2002-04-27 03:06:09 +02:00
|
|
|
|
sock->hmask |= FD_CLOSE;
|
2000-01-01 01:56:27 +01:00
|
|
|
|
if (debug_level)
|
2003-02-14 21:27:09 +01:00
|
|
|
|
fprintf(stderr, "socket %p aborted by error %d, event: %x - removing from select loop\n",
|
|
|
|
|
sock, sock->errors[FD_CLOSE_BIT], event);
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-26 20:31:19 +02:00
|
|
|
|
if ( sock->pmask & FD_CLOSE || event & (POLLERR|POLLHUP) )
|
|
|
|
|
{
|
|
|
|
|
if ( debug_level )
|
2005-01-14 20:54:38 +01:00
|
|
|
|
fprintf( stderr, "removing socket %p from select loop\n", sock );
|
2003-02-19 01:33:32 +01:00
|
|
|
|
set_fd_events( sock->fd, -1 );
|
2002-04-26 20:31:19 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
sock_reselect( sock );
|
2002-03-23 21:43:52 +01:00
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
/* wake up anyone waiting for whatever just happened */
|
2004-08-18 01:37:55 +02:00
|
|
|
|
if ( sock->pmask & sock->mask || sock->flags & WSA_FLAG_OVERLAPPED ) sock_wake_up( sock, event );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
|
|
|
|
|
/* if anyone is stupid enough to wait on the socket object itself,
|
|
|
|
|
* maybe we should wake them up too, just in case? */
|
|
|
|
|
wake_up( &sock->obj, 0 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sock_dump( struct object *obj, int verbose )
|
|
|
|
|
{
|
|
|
|
|
struct sock *sock = (struct sock *)obj;
|
|
|
|
|
assert( obj->ops == &sock_ops );
|
2003-02-14 21:27:09 +01:00
|
|
|
|
printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
|
2003-02-19 01:33:32 +01:00
|
|
|
|
sock->fd, sock->state,
|
1999-10-23 18:53:34 +02:00
|
|
|
|
sock->mask, sock->pmask, sock->hmask );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
|
static int sock_signaled( struct object *obj, struct thread *thread )
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
|
struct sock *sock = (struct sock *)obj;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
assert( obj->ops == &sock_ops );
|
|
|
|
|
|
2003-02-19 01:33:32 +01:00
|
|
|
|
return check_fd_events( sock->fd, sock_get_poll_events( sock->fd ) ) != 0;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-12-12 16:46:17 +01:00
|
|
|
|
static unsigned int sock_map_access( struct object *obj, unsigned int access )
|
|
|
|
|
{
|
|
|
|
|
if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
|
|
|
|
|
if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
|
|
|
|
|
if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
|
|
|
|
|
if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
|
|
|
|
|
return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
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 );
|
2000-01-01 01:56:27 +01:00
|
|
|
|
unsigned int mask = sock->mask & sock->state & ~sock->hmask;
|
|
|
|
|
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
|
|
|
|
|
2001-08-24 21:14:56 +02:00
|
|
|
|
if (sock->state & FD_CONNECT)
|
2000-01-01 01:56:27 +01:00
|
|
|
|
/* connecting, wait for writable */
|
|
|
|
|
return POLLOUT;
|
2001-08-24 21:14:56 +02:00
|
|
|
|
if (sock->state & FD_WINE_LISTENING)
|
2000-01-01 01:56:27 +01:00
|
|
|
|
/* listening, wait for readable */
|
|
|
|
|
return (sock->hmask & FD_ACCEPT) ? 0 : POLLIN;
|
|
|
|
|
|
2005-02-24 18:06:31 +01:00
|
|
|
|
if (mask & (FD_READ) || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->read_q )))
|
2002-04-25 23:31:11 +02:00
|
|
|
|
ev |= POLLIN | POLLPRI;
|
2005-02-24 18:06:31 +01:00
|
|
|
|
if (mask & FD_WRITE || (sock->flags & WSA_FLAG_OVERLAPPED && !list_empty( &sock->write_q )))
|
2002-04-25 23:31:11 +02:00
|
|
|
|
ev |= POLLOUT;
|
2002-07-03 23:04:09 +02:00
|
|
|
|
/* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
|
|
|
|
|
if ( sock->type == SOCK_STREAM && ( sock->mask & ~sock->hmask & FD_CLOSE) )
|
2002-04-25 23:31:11 +02:00
|
|
|
|
ev |= POLLIN;
|
|
|
|
|
|
2000-01-01 01:56:27 +01:00
|
|
|
|
return ev;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2004-08-18 02:04:58 +02:00
|
|
|
|
static int sock_get_info( struct fd *fd )
|
2001-10-05 21:45:45 +02:00
|
|
|
|
{
|
2004-08-18 02:04:58 +02:00
|
|
|
|
int flags = FD_FLAG_AVAILABLE;
|
2003-02-14 21:27:09 +01:00
|
|
|
|
struct sock *sock = get_fd_user( fd );
|
2005-01-14 20:54:38 +01:00
|
|
|
|
assert( sock->obj.ops == &sock_ops );
|
2002-01-09 22:16:24 +01:00
|
|
|
|
|
2004-08-18 02:04:58 +02:00
|
|
|
|
if (sock->flags & WSA_FLAG_OVERLAPPED) flags |= FD_FLAG_OVERLAPPED;
|
2003-01-28 01:24:04 +01:00
|
|
|
|
if ( sock->type != SOCK_STREAM || sock->state & FD_WINE_CONNECTED )
|
|
|
|
|
{
|
2004-08-18 02:04:58 +02:00
|
|
|
|
if ( !(sock->state & FD_READ ) ) flags |= FD_FLAG_RECV_SHUTDOWN;
|
|
|
|
|
if ( !(sock->state & FD_WRITE ) ) flags |= FD_FLAG_SEND_SHUTDOWN;
|
2003-01-28 01:24:04 +01:00
|
|
|
|
}
|
2004-08-18 02:04:58 +02:00
|
|
|
|
return flags;
|
2001-10-05 21:45:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
|
static void sock_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
|
|
|
|
|
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 );
|
2005-02-24 18:06:31 +01:00
|
|
|
|
struct list *queue;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
int pollev;
|
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
|
|
|
|
|
|
|
|
|
if ( !(sock->flags & WSA_FLAG_OVERLAPPED) )
|
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
set_error( STATUS_INVALID_HANDLE );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
|
switch (type)
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
|
|
|
|
case ASYNC_TYPE_READ:
|
2005-02-24 18:06:31 +01:00
|
|
|
|
queue = &sock->read_q;
|
2002-04-27 03:06:09 +02:00
|
|
|
|
sock->hmask &= ~FD_CLOSE;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
break;
|
|
|
|
|
case ASYNC_TYPE_WRITE:
|
2005-02-24 18:06:31 +01:00
|
|
|
|
queue = &sock->write_q;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if ( ( !( sock->state & FD_READ ) && type == ASYNC_TYPE_READ ) ||
|
|
|
|
|
( !( sock->state & FD_WRITE ) && type == ASYNC_TYPE_WRITE ) )
|
2002-04-25 23:31:11 +02:00
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
set_error( STATUS_PIPE_DISCONNECTED );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2005-05-25 20:41:09 +02:00
|
|
|
|
if (!create_async( current, NULL, queue, apc, user, iosb ))
|
2005-01-14 20:54:38 +01:00
|
|
|
|
return;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-01-14 20:54:38 +01:00
|
|
|
|
pollev = sock_reselect( sock );
|
|
|
|
|
if ( pollev ) sock_try_event( sock, pollev );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sock_cancel_async( struct fd *fd )
|
|
|
|
|
{
|
|
|
|
|
struct sock *sock = get_fd_user( fd );
|
|
|
|
|
assert( sock->obj.ops == &sock_ops );
|
|
|
|
|
|
|
|
|
|
async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
|
|
|
|
|
async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
|
2002-04-25 23:31:11 +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 );
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
static void sock_destroy( struct object *obj )
|
|
|
|
|
{
|
|
|
|
|
struct sock *sock = (struct sock *)obj;
|
|
|
|
|
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
|
|
|
|
|
2002-01-09 22:16:24 +01:00
|
|
|
|
if ( sock->flags & WSA_FLAG_OVERLAPPED )
|
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
async_terminate_queue( &sock->read_q, STATUS_CANCELLED );
|
|
|
|
|
async_terminate_queue( &sock->write_q, STATUS_CANCELLED );
|
2002-01-09 22:16:24 +01:00
|
|
|
|
}
|
2002-03-23 21:43:52 +01:00
|
|
|
|
if (sock->event) release_object( sock->event );
|
2003-02-19 01:33:32 +01:00
|
|
|
|
if (sock->fd) release_object( sock->fd );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* create a new and unconnected socket */
|
2002-01-09 22:16:24 +01:00
|
|
|
|
static struct object *create_socket( int family, int type, int protocol, unsigned int flags )
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
struct sock *sock;
|
1999-11-07 06:23:02 +01:00
|
|
|
|
int sockfd;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
|
1999-11-07 06:23:02 +01:00
|
|
|
|
sockfd = socket( family, type, protocol );
|
|
|
|
|
if (debug_level)
|
|
|
|
|
fprintf(stderr,"socket(%d,%d,%d)=%d\n",family,type,protocol,sockfd);
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if (sockfd == -1)
|
|
|
|
|
{
|
1999-11-07 06:23:02 +01:00
|
|
|
|
sock_set_error();
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
fcntl(sockfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
|
2003-02-19 01:33:32 +01:00
|
|
|
|
if (!(sock = alloc_object( &sock_ops )))
|
|
|
|
|
{
|
|
|
|
|
close( sockfd );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2001-08-24 21:14:56 +02:00
|
|
|
|
sock->state = (type != SOCK_STREAM) ? (FD_READ|FD_WRITE) : 0;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
sock->mask = 0;
|
|
|
|
|
sock->hmask = 0;
|
|
|
|
|
sock->pmask = 0;
|
2003-02-19 01:33:32 +01:00
|
|
|
|
sock->polling = 0;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
sock->flags = flags;
|
2002-07-03 23:04:09 +02:00
|
|
|
|
sock->type = type;
|
|
|
|
|
sock->family = family;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
sock->event = NULL;
|
|
|
|
|
sock->window = 0;
|
|
|
|
|
sock->message = 0;
|
|
|
|
|
sock->wparam = 0;
|
2002-04-24 00:03:42 +02:00
|
|
|
|
sock->deferred = NULL;
|
2003-03-12 23:38:14 +01:00
|
|
|
|
if (!(sock->fd = create_anonymous_fd( &sock_fd_ops, sockfd, &sock->obj )))
|
2003-02-19 01:33:32 +01:00
|
|
|
|
{
|
|
|
|
|
release_object( sock );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2005-02-24 18:06:31 +01:00
|
|
|
|
list_init( &sock->read_q );
|
|
|
|
|
list_init( &sock->write_q );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
clear_error();
|
1999-10-13 18:05:37 +02:00
|
|
|
|
return &sock->obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* accept a socket (creates a new fd) */
|
2002-05-30 22:12:58 +02:00
|
|
|
|
static struct sock *accept_socket( obj_handle_t handle )
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
|
|
|
|
struct sock *acceptsock;
|
|
|
|
|
struct sock *sock;
|
|
|
|
|
int acceptfd;
|
|
|
|
|
struct sockaddr saddr;
|
|
|
|
|
|
2005-12-12 16:46:17 +01:00
|
|
|
|
sock = (struct sock *)get_handle_obj( current->process, handle, FILE_READ_DATA, &sock_ops );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
if (!sock)
|
|
|
|
|
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
|
|
|
|
|
{
|
2002-04-24 00:03:42 +02:00
|
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2005-08-09 12:37:50 +02:00
|
|
|
|
unsigned int slen = sizeof(saddr);
|
2003-02-19 01:33:32 +01:00
|
|
|
|
acceptfd = accept( get_unix_fd(sock->fd), &saddr, &slen);
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if (acceptfd==-1)
|
|
|
|
|
{
|
2002-04-24 00:03:42 +02:00
|
|
|
|
sock_set_error();
|
|
|
|
|
release_object( sock );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2003-02-19 01:33:32 +01:00
|
|
|
|
if (!(acceptsock = alloc_object( &sock_ops )))
|
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
|
|
|
|
release_object( sock );
|
|
|
|
|
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 */
|
|
|
|
|
fcntl(acceptfd, F_SETFL, O_NONBLOCK); /* make socket nonblocking */
|
|
|
|
|
acceptsock->state = FD_WINE_CONNECTED|FD_READ|FD_WRITE;
|
|
|
|
|
if (sock->state & FD_WINE_NONBLOCKING)
|
|
|
|
|
acceptsock->state |= FD_WINE_NONBLOCKING;
|
|
|
|
|
acceptsock->mask = sock->mask;
|
|
|
|
|
acceptsock->hmask = 0;
|
|
|
|
|
acceptsock->pmask = 0;
|
2003-02-19 01:33:32 +01:00
|
|
|
|
acceptsock->polling = 0;
|
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->event = NULL;
|
|
|
|
|
acceptsock->window = sock->window;
|
|
|
|
|
acceptsock->message = sock->message;
|
|
|
|
|
acceptsock->wparam = 0;
|
|
|
|
|
if (sock->event) acceptsock->event = (struct event *)grab_object( sock->event );
|
|
|
|
|
acceptsock->flags = sock->flags;
|
2005-05-25 20:41:09 +02:00
|
|
|
|
acceptsock->deferred = NULL;
|
2003-03-12 23:38:14 +01:00
|
|
|
|
if (!(acceptsock->fd = create_anonymous_fd( &sock_fd_ops, acceptfd, &acceptsock->obj )))
|
2003-02-19 01:33:32 +01:00
|
|
|
|
{
|
|
|
|
|
release_object( acceptsock );
|
|
|
|
|
release_object( sock );
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2005-02-24 18:06:31 +01:00
|
|
|
|
list_init( &acceptsock->read_q );
|
|
|
|
|
list_init( &acceptsock->write_q );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
}
|
1999-10-13 18:05:37 +02:00
|
|
|
|
clear_error();
|
|
|
|
|
sock->pmask &= ~FD_ACCEPT;
|
|
|
|
|
sock->hmask &= ~FD_ACCEPT;
|
2000-10-13 19:04:52 +02:00
|
|
|
|
sock_reselect( sock );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
release_object( sock );
|
2002-03-23 21:43:52 +01:00
|
|
|
|
return acceptsock;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* set the last error depending on errno */
|
2000-03-26 20:21:16 +02:00
|
|
|
|
static 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;
|
|
|
|
|
case EWOULDBLOCK: return WSAEWOULDBLOCK;
|
|
|
|
|
case EINPROGRESS: return WSAEINPROGRESS;
|
|
|
|
|
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
|
2005-01-14 20:54:38 +01:00
|
|
|
|
default: errno=err; perror("sock_set_error"); return WSAEFAULT;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-03-26 20:21:16 +02:00
|
|
|
|
/* set the last error depending on errno */
|
|
|
|
|
static void sock_set_error(void)
|
|
|
|
|
{
|
|
|
|
|
set_error( sock_get_error( errno ) );
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
/* create a socket */
|
|
|
|
|
DECL_HANDLER(create_socket)
|
|
|
|
|
{
|
|
|
|
|
struct object *obj;
|
|
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
|
reply->handle = 0;
|
2002-01-09 22:16:24 +01:00
|
|
|
|
if ((obj = create_socket( req->family, req->type, req->protocol, req->flags )) != NULL)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
2005-12-09 13:58:25 +01:00
|
|
|
|
reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
release_object( obj );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* accept a socket */
|
|
|
|
|
DECL_HANDLER(accept_socket)
|
|
|
|
|
{
|
2002-03-23 21:43:52 +01:00
|
|
|
|
struct sock *sock;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
|
reply->handle = 0;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
if ((sock = accept_socket( req->lhandle )) != NULL)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
2005-12-09 13:58:25 +01:00
|
|
|
|
reply->handle = alloc_handle( current->process, &sock->obj, req->access, req->attributes );
|
2002-03-23 21:43:52 +01:00
|
|
|
|
sock->wparam = reply->handle; /* wparam for message is the socket handle */
|
|
|
|
|
sock_reselect( sock );
|
|
|
|
|
release_object( &sock->obj );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* set socket event parameters */
|
|
|
|
|
DECL_HANDLER(set_socket_event)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sock;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
struct event *old_event;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
int pollev;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
|
2005-12-12 16:46:17 +01:00
|
|
|
|
if (!(sock = (struct sock *)get_handle_obj( current->process, req->handle,
|
|
|
|
|
FILE_WRITE_ATTRIBUTES, &sock_ops))) return;
|
2002-03-23 21:43:52 +01:00
|
|
|
|
old_event = sock->event;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
sock->mask = req->mask;
|
2006-10-21 08:49:40 +02:00
|
|
|
|
sock->hmask &= ~req->mask; /* re-enable held events */
|
2002-03-23 21:43:52 +01:00
|
|
|
|
sock->event = NULL;
|
|
|
|
|
sock->window = req->window;
|
|
|
|
|
sock->message = req->msg;
|
|
|
|
|
sock->wparam = req->handle; /* wparam is the socket handle */
|
|
|
|
|
if (req->event) sock->event = get_event_obj( current->process, req->event, EVENT_MODIFY_STATE );
|
|
|
|
|
|
1999-10-23 18:53:34 +02:00
|
|
|
|
if (debug_level && sock->event) fprintf(stderr, "event ptr: %p\n", sock->event);
|
2002-04-25 23:31:11 +02:00
|
|
|
|
|
|
|
|
|
pollev = sock_reselect( sock );
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if ( pollev ) sock_try_event( sock, pollev );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
if (sock->mask)
|
2001-08-24 21:14:56 +02:00
|
|
|
|
sock->state |= FD_WINE_NONBLOCKING;
|
2000-07-16 20:14:01 +02:00
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
|
/* if a network event is pending, signal the event object
|
2000-07-16 20:14:01 +02:00
|
|
|
|
it is possible that FD_CONNECT or FD_ACCEPT network events has happened
|
2002-06-01 01:06:46 +02:00
|
|
|
|
before a WSAEventSelect() was done on it.
|
2000-07-16 20:14:01 +02:00
|
|
|
|
(when dealing with Asynchronous socket) */
|
2002-04-25 23:31:11 +02:00
|
|
|
|
if (sock->pmask & sock->mask) sock_wake_up( sock, pollev );
|
2002-03-23 21:43:52 +01:00
|
|
|
|
|
|
|
|
|
if (old_event) release_object( old_event ); /* we're through with it */
|
1999-10-13 18:05:37 +02:00
|
|
|
|
release_object( &sock->obj );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* get socket event parameters */
|
|
|
|
|
DECL_HANDLER(get_socket_event)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sock;
|
|
|
|
|
|
2005-12-12 16:46:17 +01:00
|
|
|
|
sock = (struct sock *)get_handle_obj( current->process, req->handle, FILE_READ_ATTRIBUTES, &sock_ops );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
if (!sock)
|
|
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
|
reply->mask = 0;
|
|
|
|
|
reply->pmask = 0;
|
|
|
|
|
reply->state = 0;
|
|
|
|
|
set_error( WSAENOTSOCK );
|
|
|
|
|
return;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
2001-11-30 19:46:42 +01:00
|
|
|
|
reply->mask = sock->mask;
|
|
|
|
|
reply->pmask = sock->pmask;
|
|
|
|
|
reply->state = sock->state;
|
|
|
|
|
set_reply_data( sock->errors, min( get_reply_max_size(), sizeof(sock->errors) ));
|
2000-08-31 03:59:51 +02:00
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
if (req->service)
|
|
|
|
|
{
|
2002-03-23 21:43:52 +01:00
|
|
|
|
if (req->c_event)
|
1999-10-13 18:05:37 +02:00
|
|
|
|
{
|
2002-03-23 21:43:52 +01:00
|
|
|
|
struct event *cevent = get_event_obj( current->process, req->c_event,
|
|
|
|
|
EVENT_MODIFY_STATE );
|
|
|
|
|
if (cevent)
|
2000-03-26 20:21:16 +02:00
|
|
|
|
{
|
|
|
|
|
reset_event( cevent );
|
|
|
|
|
release_object( cevent );
|
|
|
|
|
}
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
2002-03-23 21:43:52 +01:00
|
|
|
|
sock->pmask = 0;
|
|
|
|
|
sock_reselect( sock );
|
1999-10-13 18:05:37 +02:00
|
|
|
|
}
|
|
|
|
|
release_object( &sock->obj );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* re-enable pending socket events */
|
|
|
|
|
DECL_HANDLER(enable_socket_event)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sock;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
int pollev;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
|
2002-03-23 21:43:52 +01:00
|
|
|
|
if (!(sock = (struct sock*)get_handle_obj( current->process, req->handle,
|
2005-12-12 16:46:17 +01:00
|
|
|
|
FILE_WRITE_ATTRIBUTES, &sock_ops)))
|
2002-03-23 21:43:52 +01:00
|
|
|
|
return;
|
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
sock->pmask &= ~req->mask; /* is this safe? */
|
|
|
|
|
sock->hmask &= ~req->mask;
|
2002-04-27 03:06:09 +02:00
|
|
|
|
if ( req->mask & FD_READ )
|
|
|
|
|
sock->hmask &= ~FD_CLOSE;
|
1999-10-13 18:05:37 +02:00
|
|
|
|
sock->state |= req->sstate;
|
|
|
|
|
sock->state &= ~req->cstate;
|
2002-07-03 23:04:09 +02:00
|
|
|
|
if ( sock->type != SOCK_STREAM ) sock->state &= ~STREAM_FLAG_MASK;
|
2002-04-25 23:31:11 +02:00
|
|
|
|
|
|
|
|
|
pollev = sock_reselect( sock );
|
2005-01-14 20:54:38 +01:00
|
|
|
|
if ( pollev ) sock_try_event( sock, pollev );
|
2002-04-25 23:31:11 +02:00
|
|
|
|
|
1999-10-13 18:05:37 +02:00
|
|
|
|
release_object( &sock->obj );
|
|
|
|
|
}
|
2002-04-24 00:03:42 +02:00
|
|
|
|
|
|
|
|
|
DECL_HANDLER(set_socket_deferred)
|
|
|
|
|
{
|
|
|
|
|
struct sock *sock, *acceptsock;
|
|
|
|
|
|
2005-12-12 16:46:17 +01:00
|
|
|
|
sock=(struct sock *)get_handle_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES, &sock_ops );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
if ( !sock )
|
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
set_error( WSAENOTSOCK );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2005-12-12 16:46:17 +01:00
|
|
|
|
acceptsock = (struct sock *)get_handle_obj( current->process, req->deferred, 0, &sock_ops );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
if ( !acceptsock )
|
|
|
|
|
{
|
2005-01-14 20:54:38 +01:00
|
|
|
|
release_object( sock );
|
|
|
|
|
set_error( WSAENOTSOCK );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
sock->deferred = acceptsock;
|
2005-01-14 20:54:38 +01:00
|
|
|
|
release_object( sock );
|
2002-04-24 00:03:42 +02:00
|
|
|
|
}
|