2 * Server-side socket management
4 * Copyright (C) 1999 Marcus Meissner, Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * FIXME: we use read|write access in all cases. Shouldn't we depend that
21 * on the access of the current handle?
33 #ifdef HAVE_SYS_ERRNO_H
34 # include <sys/errno.h>
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_FILIO_H
45 # include <sys/filio.h>
51 #define WIN32_NO_STATUS
62 /* To avoid conflicts with the Unix socket headers. Plus we only need a few
70 struct object obj
; /* object header */
71 struct fd
*fd
; /* socket file descriptor */
72 unsigned int state
; /* status bits */
73 unsigned int mask
; /* event mask */
74 unsigned int hmask
; /* held (blocked) events */
75 unsigned int pmask
; /* pending events */
76 unsigned int flags
; /* socket flags */
77 int polling
; /* is socket being polled? */
78 unsigned short type
; /* socket type */
79 unsigned short family
; /* socket family */
80 struct event
*event
; /* event object */
81 user_handle_t window
; /* window to send the message to */
82 unsigned int message
; /* message to send */
83 obj_handle_t wparam
; /* message wparam (socket handle) */
84 int errors
[FD_MAX_EVENTS
]; /* event errors */
85 struct sock
*deferred
; /* socket that waits for a deferred accept */
86 struct async_queue
*read_q
; /* queue for asynchronous reads */
87 struct async_queue
*write_q
; /* queue for asynchronous writes */
88 obj_handle_t hListen
; /* socket listening for connections for this one */
89 int incCon
; /* number of incomming connect attempts will will try to wake for */
92 static void sock_dump( struct object
*obj
, int verbose
);
93 static int sock_signaled( struct object
*obj
, struct thread
*thread
);
94 static struct fd
*sock_get_fd( struct object
*obj
);
95 static void sock_destroy( struct object
*obj
);
97 static int sock_get_poll_events( struct fd
*fd
);
98 static void sock_poll_event( struct fd
*fd
, int event
);
99 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
);
100 static void sock_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
);
101 static void sock_queue_async_l( struct fd
*fd
, const async_data_t
*data
, int type
, int count
, obj_handle_t locator
);
102 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
103 static void sock_cancel_async( struct fd
*fd
);
104 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
105 static void sock_async_close_l( struct sock
*sock
, obj_handle_t locator
);
108 static int sock_get_error( int err
);
109 static void sock_set_error(void);
111 static const struct object_ops sock_ops
=
113 sizeof(struct sock
), /* size */
114 sock_dump
, /* dump */
115 no_get_type
, /* get_type */
116 add_queue
, /* add_queue */
117 remove_queue
, /* remove_queue */
118 sock_signaled
, /* signaled */
119 no_satisfied
, /* satisfied */
120 no_signal
, /* signal */
121 sock_get_fd
, /* get_fd */
122 default_fd_map_access
, /* map_access */
123 default_get_sd
, /* get_sd */
124 default_set_sd
, /* set_sd */
125 no_lookup_name
, /* lookup_name */
126 no_open_file
, /* open_file */
127 sock_close_handle
, /* close_handle */
128 sock_destroy
/* destroy */
131 static const struct fd_ops sock_fd_ops
=
133 sock_get_poll_events
, /* get_poll_events */
134 sock_poll_event
, /* poll_event */
135 no_flush
, /* flush */
136 sock_get_fd_type
, /* get_file_info */
137 default_fd_ioctl
, /* ioctl */
138 sock_queue_async
, /* queue_async */
139 sock_reselect_async
, /* reselect_async */
140 sock_cancel_async
, /* cancel_async */
141 sock_queue_async_l
/* quque_async_l */
145 /* Permutation of 0..FD_MAX_EVENTS - 1 representing the order in which
146 * we post messages if there are multiple events. Used to send
147 * messages. The problem is if there is both a FD_CONNECT event and,
148 * say, an FD_READ event available on the same socket, we want to
149 * notify the app of the connect event first. Otherwise it may
150 * discard the read event because it thinks it hasn't connected yet.
152 static const int event_bitorder
[FD_MAX_EVENTS
] =
160 6, 7, 8, 9 /* leftovers */
163 /* Flags that make sense only for SOCK_STREAM sockets */
164 #define STREAM_FLAG_MASK ((unsigned int) (FD_CONNECT | FD_ACCEPT | FD_WINE_LISTENING | FD_WINE_CONNECTED))
167 SOCK_SHUTDOWN_ERROR
= -1,
168 SOCK_SHUTDOWN_EOF
= 0,
169 SOCK_SHUTDOWN_POLLHUP
= 1
172 static sock_shutdown_t sock_shutdown_type
= SOCK_SHUTDOWN_ERROR
;
174 static sock_shutdown_t
sock_check_pollhup(void)
176 sock_shutdown_t ret
= SOCK_SHUTDOWN_ERROR
;
181 if ( socketpair( AF_UNIX
, SOCK_STREAM
, 0, fd
) ) goto out
;
182 if ( shutdown( fd
[0], 1 ) ) goto out
;
188 n
= poll( &pfd
, 1, 0 );
189 if ( n
!= 1 ) goto out
; /* error or timeout */
190 if ( pfd
.revents
& POLLHUP
)
191 ret
= SOCK_SHUTDOWN_POLLHUP
;
192 else if ( pfd
.revents
& POLLIN
&&
193 read( fd
[1], &dummy
, 1 ) == 0 )
194 ret
= SOCK_SHUTDOWN_EOF
;
204 sock_shutdown_type
= sock_check_pollhup();
206 switch ( sock_shutdown_type
)
208 case SOCK_SHUTDOWN_EOF
:
209 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes EOF\n" );
211 case SOCK_SHUTDOWN_POLLHUP
:
212 if (debug_level
) fprintf( stderr
, "sock_init: shutdown() causes POLLHUP\n" );
215 fprintf( stderr
, "sock_init: ERROR in sock_check_pollhup()\n" );
216 sock_shutdown_type
= SOCK_SHUTDOWN_EOF
;
220 static int sock_reselect( struct sock
*sock
)
222 int ev
= sock_get_poll_events( sock
->fd
);
225 fprintf(stderr
,"sock_reselect(%p): new mask %x\n", sock
, ev
);
227 if (!sock
->polling
) /* FIXME: should find a better way to do this */
229 /* previously unconnected socket, is this reselect supposed to connect it? */
230 if (!(sock
->state
& ~FD_WINE_NONBLOCKING
)) return 0;
231 /* ok, it is, attach it to the wineserver's main poll loop */
234 /* update condition mask */
235 set_fd_events( sock
->fd
, ev
);
239 /* After POLLHUP is received, the socket will no longer be in the main select loop.
240 This function is used to signal pending events nevertheless */
241 static void sock_try_event( struct sock
*sock
, int event
)
243 event
= check_fd_events( sock
->fd
, event
);
246 if ( debug_level
) fprintf( stderr
, "sock_try_event: %x\n", event
);
247 sock_poll_event( sock
->fd
, event
);
251 /* wake anybody waiting on the socket event or send the associated message */
252 static void sock_wake_up( struct sock
*sock
, int pollev
)
254 unsigned int events
= sock
->pmask
& sock
->mask
;
256 int async_active
= 0;
258 /*fprintf(stderr,"sock: %p. pollev & (POLLIN|POLLPRI):%d. async_waiting( sock->read_q ):%d,sock->read_q:%p\n",sock,pollev & (POLLIN|POLLPRI),async_waiting( sock->read_q ),sock->read_q);*/
260 if ( pollev
& (POLLIN
|POLLPRI
) && async_waiting( sock
->read_q
))
262 /* fprintf(stderr,"waking_read_queue pending:%d\n",sock->incCon);*/
264 if (debug_level
) fprintf( stderr
, "activating read queue for socket %p\n", sock
);
265 async_wake_up( sock
->read_q
, STATUS_ALERTED
);
268 if ( pollev
& POLLOUT
&& async_waiting( sock
->write_q
))
270 if (debug_level
) fprintf( stderr
, "activating write queue for socket %p\n", sock
);
271 async_wake_up( sock
->write_q
, STATUS_ALERTED
);
275 /* Do not signal events if there are still pending asynchronous IO requests */
276 /* We need this to delay FD_CLOSE events until all pending overlapped requests are processed */
277 if ( !events
|| async_active
) return;
281 if (debug_level
) fprintf(stderr
, "signalling events %x ptr %p\n", events
, sock
->event
);
282 set_event( sock
->event
);
286 if (debug_level
) fprintf(stderr
, "signalling events %x win %08x\n", events
, sock
->window
);
287 for (i
= 0; i
< FD_MAX_EVENTS
; i
++)
289 int event
= event_bitorder
[i
];
290 if (sock
->pmask
& (1 << event
))
292 lparam_t lparam
= (1 << event
) | (sock
->errors
[event
] << 16);
293 post_message( sock
->window
, sock
->message
, sock
->wparam
, lparam
);
297 sock_reselect( sock
);
301 static inline int sock_error( struct fd
*fd
)
303 unsigned int optval
= 0, optlen
;
305 optlen
= sizeof(optval
);
306 getsockopt( get_unix_fd(fd
), SOL_SOCKET
, SO_ERROR
, (void *) &optval
, &optlen
);
307 return optval
? sock_get_error(optval
) : 0;
310 static void sock_poll_event( struct fd
*fd
, int event
)
312 struct sock
*sock
= get_fd_user( fd
);
315 assert( sock
->obj
.ops
== &sock_ops
);
317 fprintf(stderr
, "socket %p select event: %x\n", sock
, event
);
318 if (sock
->state
& FD_CONNECT
)
323 /* we got connected */
324 sock
->state
|= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
325 sock
->state
&= ~FD_CONNECT
;
326 sock
->pmask
|= FD_CONNECT
;
327 sock
->errors
[FD_CONNECT_BIT
] = 0;
329 fprintf(stderr
, "socket %p connection success\n", sock
);
331 else if (event
& (POLLERR
|POLLHUP
))
333 /* we didn't get connected? */
334 sock
->state
&= ~FD_CONNECT
;
335 sock
->pmask
|= FD_CONNECT
;
336 sock
->errors
[FD_CONNECT_BIT
] = sock_error( fd
);
338 fprintf(stderr
, "socket %p connection failure\n", sock
);
341 else if (sock
->state
& FD_WINE_LISTENING
)
347 /* incoming connection */
349 sock
->pmask
|= FD_ACCEPT
;
350 sock
->errors
[FD_ACCEPT_BIT
] = 0;
351 sock
->hmask
|= FD_ACCEPT
;
352 /* fprintf(stderr,"sock: %p. incomming connection. pending: %d\n",sock,sock->incCon);*/
355 else if (event
& (POLLERR
|POLLHUP
))
357 /* failed incoming connection? */
358 sock
->pmask
|= FD_ACCEPT
;
359 sock
->errors
[FD_ACCEPT_BIT
] = sock_error( fd
);
360 sock
->hmask
|= FD_ACCEPT
;
365 /* normal data flow */
366 if ( sock
->type
== SOCK_STREAM
&& ( event
& POLLIN
) )
371 /* Linux 2.4 doesn't report POLLHUP if only one side of the socket
372 * has been closed, so we need to check for it explicitly here */
373 nr
= recv( get_unix_fd( fd
), &dummy
, 1, MSG_PEEK
);
377 sock
->pmask
|= FD_READ
;
378 sock
->hmask
|= (FD_READ
|FD_CLOSE
);
379 sock
->errors
[FD_READ_BIT
] = 0;
381 fprintf(stderr
, "socket %p is readable\n", sock
);
387 /* EAGAIN can happen if an async recv() falls between the server's poll()
388 call and the invocation of this routine */
389 if ( errno
== EAGAIN
)
394 fprintf( stderr
, "recv error on socket %p: %d\n", sock
, errno
);
400 else if ( sock_shutdown_type
== SOCK_SHUTDOWN_POLLHUP
&& (event
& POLLHUP
) )
404 else if ( event
& POLLIN
) /* POLLIN for non-stream socket */
406 sock
->pmask
|= FD_READ
;
407 sock
->hmask
|= (FD_READ
|FD_CLOSE
);
408 sock
->errors
[FD_READ_BIT
] = 0;
410 fprintf(stderr
, "socket %p is readable\n", sock
);
416 sock
->pmask
|= FD_WRITE
;
417 sock
->hmask
|= FD_WRITE
;
418 sock
->errors
[FD_WRITE_BIT
] = 0;
420 fprintf(stderr
, "socket %p is writable\n", sock
);
424 sock
->pmask
|= FD_OOB
;
425 sock
->hmask
|= FD_OOB
;
426 sock
->errors
[FD_OOB_BIT
] = 0;
428 fprintf(stderr
, "socket %p got OOB data\n", sock
);
430 /* According to WS2 specs, FD_CLOSE is only delivered when there is
431 no more data to be read (i.e. hangup_seen = 1) */
432 else if ( hangup_seen
&& (sock
->state
& (FD_READ
|FD_WRITE
) ))
434 sock
->errors
[FD_CLOSE_BIT
] = sock_error( fd
);
435 if ( (event
& POLLERR
) || ( sock_shutdown_type
== SOCK_SHUTDOWN_EOF
&& (event
& POLLHUP
) ))
436 sock
->state
&= ~FD_WRITE
;
437 sock
->pmask
|= FD_CLOSE
;
438 sock
->hmask
|= FD_CLOSE
;
440 fprintf(stderr
, "socket %p aborted by error %d, event: %x - removing from select loop\n",
441 sock
, sock
->errors
[FD_CLOSE_BIT
], event
);
445 if ( sock
->pmask
& FD_CLOSE
|| event
& (POLLERR
|POLLHUP
) )
448 fprintf( stderr
, "removing socket %p from select loop\n", sock
);
449 set_fd_events( sock
->fd
, -1 );
452 sock_reselect( sock
);
454 /* wake up anyone waiting for whatever just happened */
455 if ( sock
->pmask
& sock
->mask
|| sock
->flags
& WSA_FLAG_OVERLAPPED
)
457 /* fprintf(stderr,"sock: %p. pending: %d. 1st: %d. 2nd: %d.\n",sock,sock->incCon, sock->pmask & sock->mask, sock->flags & WSA_FLAG_OVERLAPPED);*/
459 sock_wake_up( sock
, event
);
461 /* fprintf(stderr,"sock: %p. pending: %d. 1st: %d. 2nd: %d.\n",sock,sock->incCon, sock->pmask & sock->mask, sock->flags & WSA_FLAG_OVERLAPPED);*/
464 /* if anyone is stupid enough to wait on the socket object itself,
465 * maybe we should wake them up too, just in case? */
466 wake_up( &sock
->obj
, 0 );
469 static void sock_dump( struct object
*obj
, int verbose
)
471 struct sock
*sock
= (struct sock
*)obj
;
472 assert( obj
->ops
== &sock_ops
);
473 printf( "Socket fd=%p, state=%x, mask=%x, pending=%x, held=%x\n",
474 sock
->fd
, sock
->state
,
475 sock
->mask
, sock
->pmask
, sock
->hmask
);
478 static int sock_signaled( struct object
*obj
, struct thread
*thread
)
480 struct sock
*sock
= (struct sock
*)obj
;
481 assert( obj
->ops
== &sock_ops
);
483 return check_fd_events( sock
->fd
, sock_get_poll_events( sock
->fd
) ) != 0;
486 static int sock_get_poll_events( struct fd
*fd
)
488 struct sock
*sock
= get_fd_user( fd
);
489 unsigned int mask
= sock
->mask
& sock
->state
& ~sock
->hmask
;
492 assert( sock
->obj
.ops
== &sock_ops
);
494 if (sock
->state
& FD_CONNECT
)
495 /* connecting, wait for writable */
497 if (sock
->state
& FD_WINE_LISTENING
)
498 /* listening, wait for readable */
499 return (sock
->hmask
& FD_ACCEPT
) ? 0 : POLLIN
;
501 if (mask
& FD_READ
|| async_waiting( sock
->read_q
)) ev
|= POLLIN
| POLLPRI
;
502 if (mask
& FD_WRITE
|| async_waiting( sock
->write_q
)) ev
|= POLLOUT
;
503 /* We use POLLIN with 0 bytes recv() as FD_CLOSE indication for stream sockets. */
504 if ( sock
->type
== SOCK_STREAM
&& ( sock
->mask
& ~sock
->hmask
& FD_CLOSE
) )
510 static enum server_fd_type
sock_get_fd_type( struct fd
*fd
)
512 return FD_TYPE_SOCKET
;
515 static void sock_queue_async_l( struct fd
*fd
, const async_data_t
*data
, int type
, int count
, obj_handle_t locator
)
517 struct sock
*sock
= get_fd_user( fd
);
518 struct async_queue
*queue
;
521 assert( sock
->obj
.ops
== &sock_ops
);
525 case ASYNC_TYPE_READ
:
526 if (!sock
->read_q
&& !(sock
->read_q
= create_async_queue( sock
->fd
))) return;
527 queue
= sock
->read_q
;
528 sock
->hmask
&= ~FD_CLOSE
;
530 case ASYNC_TYPE_WRITE
:
531 if (!sock
->write_q
&& !(sock
->write_q
= create_async_queue( sock
->fd
))) return;
532 queue
= sock
->write_q
;
535 set_error( STATUS_INVALID_PARAMETER
);
539 if ( ( !( sock
->state
& (FD_READ
|FD_WINE_LISTENING
) ) && type
== ASYNC_TYPE_READ
) ||
540 ( !( sock
->state
& FD_WRITE
) && type
== ASYNC_TYPE_WRITE
) )
542 set_error( STATUS_PIPE_DISCONNECTED
);
547 if (!(async
= create_async_l( current
, queue
, data
, locator
))) return;
548 release_object( async
);
549 set_error( STATUS_PENDING
);
552 pollev
= sock_reselect( sock
);
553 if ( pollev
) sock_try_event( sock
, pollev
);
555 static void sock_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
)
557 sock_queue_async_l(fd
,data
,type
,count
,0);
560 static void sock_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
562 struct sock
*sock
= get_fd_user( fd
);
563 int events
= sock_reselect( sock
);
564 if (events
) sock_try_event( sock
, events
);
567 static void sock_cancel_async( struct fd
*fd
)
569 struct sock
*sock
= get_fd_user( fd
);
570 assert( sock
->obj
.ops
== &sock_ops
);
571 async_wake_up( sock
->read_q
, STATUS_CANCELLED
);
572 async_wake_up( sock
->write_q
, STATUS_CANCELLED
);
575 static int sock_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
577 struct sock
*sock
= (struct sock
*) obj
;
578 struct sock
*l
= NULL
;
579 assert( sock
->obj
.ops
== &sock_ops
);
580 async_wake_up( sock
->read_q
, STATUS_HANDLES_CLOSED
);
581 async_wake_up( sock
->write_q
, STATUS_HANDLES_CLOSED
);
582 if(current
&& sock
){ /*avoid fatal error when updating wine config files*/
583 l
= (struct sock
*)get_handle_obj( process
, sock
->hListen
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
586 sock_async_close_l( l
, handle
);
593 return fd_close_handle(obj
,process
,handle
);
596 /*Closes all async operations on a socket with the corresponding locator */
597 static void sock_async_close_l( struct sock
*sock
, obj_handle_t locator
)
599 async_wake_up_l( sock
->read_q
, STATUS_HANDLES_CLOSED
, locator
);
600 async_wake_up_l( sock
->write_q
, STATUS_HANDLES_CLOSED
, locator
);
603 static struct fd
*sock_get_fd( struct object
*obj
)
605 struct sock
*sock
= (struct sock
*)obj
;
606 return (struct fd
*)grab_object( sock
->fd
);
609 static void sock_destroy( struct object
*obj
)
611 struct sock
*sock
= (struct sock
*)obj
;
612 assert( obj
->ops
== &sock_ops
);
614 /* FIXME: special socket shutdown stuff? */
616 if ( sock
->deferred
)
617 release_object( sock
->deferred
);
619 free_async_queue( sock
->read_q
);
620 free_async_queue( sock
->write_q
);
621 if (sock
->event
) release_object( sock
->event
);
624 /* shut the socket down to force pending poll() calls in the client to return */
625 shutdown( get_unix_fd(sock
->fd
), SHUT_RDWR
);
626 release_object( sock
->fd
);
630 /* create a new and unconnected socket */
631 static struct object
*create_socket( int family
, int type
, int protocol
, unsigned int flags
)
636 sockfd
= socket( family
, type
, protocol
);
638 fprintf(stderr
,"socket(%d,%d,%d)=%d\n",family
,type
,protocol
,sockfd
);
644 fcntl(sockfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
645 if (!(sock
= alloc_object( &sock_ops
)))
650 sock
->state
= (type
!= SOCK_STREAM
) ? (FD_READ
|FD_WRITE
) : 0;
657 sock
->family
= family
;
662 sock
->deferred
= NULL
;
664 sock
->write_q
= NULL
;
666 if (!(sock
->fd
= create_anonymous_fd( &sock_fd_ops
, sockfd
, &sock
->obj
,
667 (flags
& WSA_FLAG_OVERLAPPED
) ? 0 : FILE_SYNCHRONOUS_IO_NONALERT
)))
669 release_object( sock
);
672 sock_reselect( sock
);
677 /* accept a socket (creates a new fd) */
678 static struct sock
*accept_socket( obj_handle_t handle
, obj_handle_t ahandle
)
680 struct sock
*acceptsock
= NULL
;
683 struct sockaddr saddr
;
685 sock
= (struct sock
*)get_handle_obj( current
->process
, handle
, FILE_READ_DATA
, &sock_ops
);
690 if ( sock
->deferred
)
692 acceptsock
= sock
->deferred
;
693 sock
->deferred
= NULL
;
698 /* Try to accept(2). We can't be safe that this an already connected socket
699 * or that accept() is allowed on it. In those cases we will get -1/errno
702 unsigned int slen
= sizeof(saddr
);
703 acceptfd
= accept( get_unix_fd(sock
->fd
), &saddr
, &slen
);
707 release_object( sock
);
712 acceptsock
= (struct sock
*)get_handle_obj(current
->process
, ahandle
, FILE_READ_DATA
| FILE_WRITE_DATA
, &sock_ops
);
714 if(acceptsock
->event
)
715 release_object( acceptsock
->event
);
716 release_object( acceptsock
->fd
);
719 acceptsock
= alloc_object( &sock_ops
);
724 release_object( sock
);
729 fcntl(acceptfd
, F_SETFL
, O_NONBLOCK
); /* make socket nonblocking */
730 acceptsock
->state
= FD_WINE_CONNECTED
|FD_READ
|FD_WRITE
;
731 acceptsock
->hmask
= 0;
732 acceptsock
->pmask
= 0;
733 acceptsock
->polling
= 0;
734 acceptsock
->event
= NULL
;
735 acceptsock
->deferred
= NULL
;
736 acceptsock
->read_q
= NULL
;
737 acceptsock
->write_q
= NULL
;
738 acceptsock
->wparam
= 0;
739 acceptsock
->incCon
= 0;
740 /* newly created socket gets the same properties of the listening socket */
741 if (sock
->state
& FD_WINE_NONBLOCKING
)
742 acceptsock
->state
|= FD_WINE_NONBLOCKING
;
743 acceptsock
->mask
= sock
->mask
;
744 acceptsock
->type
= sock
->type
;
745 acceptsock
->family
= sock
->family
;
746 acceptsock
->window
= sock
->window
;
747 acceptsock
->message
= sock
->message
;
748 if (sock
->event
) acceptsock
->event
= (struct event
*)grab_object( sock
->event
);
749 acceptsock
->flags
= sock
->flags
;
750 if (!(acceptsock
->fd
= create_anonymous_fd( &sock_fd_ops
, acceptfd
, &acceptsock
->obj
,
751 get_fd_options( sock
->fd
) )))
753 release_object( acceptsock
);
754 release_object( sock
);
759 sock
->pmask
&= ~FD_ACCEPT
;
760 sock
->hmask
&= ~FD_ACCEPT
;
762 //fprintf(stderr,"accept: pending: %d\n",sock->incCon);
764 sock_reselect( sock
);
765 release_object( sock
);
769 /* set the last error depending on errno */
770 static int sock_get_error( int err
)
774 case EINTR
: return WSAEINTR
;
775 case EBADF
: return WSAEBADF
;
777 case EACCES
: return WSAEACCES
;
778 case EFAULT
: return WSAEFAULT
;
779 case EINVAL
: return WSAEINVAL
;
780 case EMFILE
: return WSAEMFILE
;
781 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
782 case EINPROGRESS
: return WSAEINPROGRESS
;
783 case EALREADY
: return WSAEALREADY
;
784 case ENOTSOCK
: return WSAENOTSOCK
;
785 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
786 case EMSGSIZE
: return WSAEMSGSIZE
;
787 case EPROTOTYPE
: return WSAEPROTOTYPE
;
788 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
789 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
790 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
791 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
792 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
793 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
794 case EADDRINUSE
: return WSAEADDRINUSE
;
795 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
796 case ENETDOWN
: return WSAENETDOWN
;
797 case ENETUNREACH
: return WSAENETUNREACH
;
798 case ENETRESET
: return WSAENETRESET
;
799 case ECONNABORTED
: return WSAECONNABORTED
;
801 case ECONNRESET
: return WSAECONNRESET
;
802 case ENOBUFS
: return WSAENOBUFS
;
803 case EISCONN
: return WSAEISCONN
;
804 case ENOTCONN
: return WSAENOTCONN
;
805 case ESHUTDOWN
: return WSAESHUTDOWN
;
806 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
807 case ETIMEDOUT
: return WSAETIMEDOUT
;
808 case ECONNREFUSED
: return WSAECONNREFUSED
;
809 case ELOOP
: return WSAELOOP
;
810 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
811 case EHOSTDOWN
: return WSAEHOSTDOWN
;
812 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
813 case ENOTEMPTY
: return WSAENOTEMPTY
;
815 case EPROCLIM
: return WSAEPROCLIM
;
818 case EUSERS
: return WSAEUSERS
;
821 case EDQUOT
: return WSAEDQUOT
;
824 case ESTALE
: return WSAESTALE
;
827 case EREMOTE
: return WSAEREMOTE
;
831 perror("wineserver: sock_get_error() can't map error");
836 /* set the last error depending on errno */
837 static void sock_set_error(void)
839 set_error( sock_get_error( errno
) );
842 /* create a socket */
843 DECL_HANDLER(create_socket
)
848 if ((obj
= create_socket( req
->family
, req
->type
, req
->protocol
, req
->flags
)) != NULL
)
850 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, req
->attributes
);
851 release_object( obj
);
855 /* accept a socket */
856 DECL_HANDLER(accept_socket
)
861 if ((sock
= accept_socket( req
->lhandle
, req
->ahandle
)) != NULL
)
864 reply
->handle
= req
->ahandle
;
867 reply
->handle
= alloc_handle( current
->process
, &sock
->obj
, req
->access
, req
->attributes
);
868 sock
->wparam
= reply
->handle
; /* wparam for message is the socket handle */
869 sock_reselect( sock
);
871 release_object( &sock
->obj
);//I think this needs togo in the else. But i want to check.
872 //We can't release the object unless we allocate a handle for it.
873 //Do we need to se the wparam? What is this? No... maybe... i dont know.
877 /* Sets which socket is listening for connections for this socket */
878 DECL_HANDLER(set_socket_listener
)
881 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
883 set_error( STATUS_INVALID_HANDLE
);
886 sock
->hListen
= req
->hListen
;
887 release_object( &sock
->obj
);
888 set_error( STATUS_SUCCESS
);
892 /* set socket event parameters */
893 DECL_HANDLER(set_socket_event
)
896 struct event
*old_event
;
899 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
900 FILE_WRITE_ATTRIBUTES
, &sock_ops
))) return;
901 old_event
= sock
->event
;
902 sock
->mask
= req
->mask
;
903 sock
->hmask
&= ~req
->mask
; /* re-enable held events */
905 sock
->window
= req
->window
;
906 sock
->message
= req
->msg
;
907 sock
->wparam
= req
->handle
; /* wparam is the socket handle */
908 if (req
->event
) sock
->event
= get_event_obj( current
->process
, req
->event
, EVENT_MODIFY_STATE
);
910 if (debug_level
&& sock
->event
) fprintf(stderr
, "event ptr: %p\n", sock
->event
);
912 pollev
= sock_reselect( sock
);
913 if ( pollev
) sock_try_event( sock
, pollev
);
916 sock
->state
|= FD_WINE_NONBLOCKING
;
918 /* if a network event is pending, signal the event object
919 it is possible that FD_CONNECT or FD_ACCEPT network events has happened
920 before a WSAEventSelect() was done on it.
921 (when dealing with Asynchronous socket) */
922 if (sock
->pmask
& sock
->mask
) sock_wake_up( sock
, pollev
);
924 if (old_event
) release_object( old_event
); /* we're through with it */
925 release_object( &sock
->obj
);
928 /* get socket event parameters */
929 DECL_HANDLER(get_socket_event
)
933 sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
, &sock_ops
);
939 set_error( WSAENOTSOCK
);
942 reply
->mask
= sock
->mask
;
943 reply
->pmask
= sock
->pmask
;
944 reply
->state
= sock
->state
;
945 set_reply_data( sock
->errors
, min( get_reply_max_size(), sizeof(sock
->errors
) ));
951 struct event
*cevent
= get_event_obj( current
->process
, req
->c_event
,
952 EVENT_MODIFY_STATE
);
955 reset_event( cevent
);
956 release_object( cevent
);
960 sock_reselect( sock
);
962 release_object( &sock
->obj
);
965 /* re-enable pending socket events */
966 DECL_HANDLER(enable_socket_event
)
971 if (!(sock
= (struct sock
*)get_handle_obj( current
->process
, req
->handle
,
972 FILE_WRITE_ATTRIBUTES
, &sock_ops
)))
975 sock
->pmask
&= ~req
->mask
; /* is this safe? */
976 sock
->hmask
&= ~req
->mask
;
977 if ( req
->mask
& FD_READ
)
978 sock
->hmask
&= ~FD_CLOSE
;
979 sock
->state
|= req
->sstate
;
980 sock
->state
&= ~req
->cstate
;
981 if ( sock
->type
!= SOCK_STREAM
) sock
->state
&= ~STREAM_FLAG_MASK
;
983 pollev
= sock_reselect( sock
);
984 if ( pollev
) sock_try_event( sock
, pollev
);
986 release_object( &sock
->obj
);
989 DECL_HANDLER(set_socket_deferred
)
991 struct sock
*sock
, *acceptsock
;
993 sock
=(struct sock
*)get_handle_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
, &sock_ops
);
996 set_error( WSAENOTSOCK
);
999 acceptsock
= (struct sock
*)get_handle_obj( current
->process
, req
->deferred
, 0, &sock_ops
);
1002 release_object( sock
);
1003 set_error( WSAENOTSOCK
);
1006 sock
->deferred
= acceptsock
;
1007 release_object( sock
);