Release 970329
[wine/multimedia.git] / misc / winsock.c
blob6652b8a415e9ebc43828deff12010687b0e6364b
1 /*
2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
4 *
5 * (C) 1993,1994,1996 John Brezak, Erik Bos, Alex Korobka.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <sys/ipc.h>
13 #include <sys/ioctl.h>
14 #if defined(__svr4__)
15 #include <sys/filio.h>
16 #include <sys/ioccom.h>
17 #include <sys/sockio.h>
18 #endif
19 #if defined(__EMX__)
20 #include <sys/so_ioctl.h>
21 #include <sys/param.h>
22 #endif
23 #include <sys/msg.h>
24 #include <sys/wait.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <unistd.h>
33 #include "windows.h"
34 #include "winnt.h"
35 #include "heap.h"
36 #include "ldt.h"
37 #include "winsock.h"
38 #include "stddebug.h"
39 #include "debug.h"
41 #define dump_sockaddr(a) \
42 fprintf(stderr, "sockaddr_in: family %d, address %s, port %d\n", \
43 ((struct sockaddr_in *)a)->sin_family, \
44 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
45 ntohs(((struct sockaddr_in *)a)->sin_port))
47 extern void SIGNAL_MaskAsyncEvents( BOOL32 );
49 #pragma pack(4)
51 /* ----------------------------------- internal data */
53 extern int h_errno;
54 extern void __sigio(int);
56 ws_async_ctl async_ctl;
57 int async_qid = -1;
59 static HANDLE32 _WSHeap = 0;
60 static unsigned char* _ws_stub = NULL;
61 static LPWSINFO _wsi_list = NULL;
63 #define WS_ALLOC(size) \
64 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
65 #define WS_FREE(ptr) \
66 HeapFree(_WSHeap, 0, (ptr) )
68 #define WS_PTR2HANDLE(ptr) \
69 ((short)((int)(ptr) - (int)_ws_stub))
70 #define WS_HANDLE2PTR(handle) \
71 ((unsigned)((int)_ws_stub + (int)handle))
73 #define WSI_CHECK_RANGE(pwsi, pws) \
74 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
75 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
77 static INT16 _ws_sock_ops[] =
78 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
79 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
80 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE, WS_SO_DONTLINGER, 0 };
81 static int _px_sock_ops[] =
82 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
83 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
84 SO_LINGER };
86 static INT16 init_async_select(ws_socket* pws, HWND16 hWnd, UINT16 uMsg, UINT32 lEvent);
87 static int notify_client(ws_socket* pws, unsigned flag);
89 static int _check_ws(LPWSINFO pwsi, ws_socket* pws);
90 static int _check_buffer(LPWSINFO pwsi, int size);
92 static void fixup_wshe(struct ws_hostent* p_wshe, SEGPTR base);
93 static void fixup_wspe(struct ws_protoent* p_wspe, SEGPTR base);
94 static void fixup_wsse(struct ws_servent* p_wsse, SEGPTR base);
96 static int cancel_async_select(ws_socket*);
98 static void convert_sockopt(INT16 *level, INT16 *optname)
100 int i;
101 switch (*level)
103 case WS_SOL_SOCKET:
104 *level = SOL_SOCKET;
105 for(i=0; _ws_sock_ops[i]; i++)
106 if( _ws_sock_ops[i] == *optname ) break;
107 if( _ws_sock_ops[i] ) *optname = (INT16)_px_sock_ops[i];
108 else fprintf(stderr, "convert_sockopt() unknown optname %d\n", *optname);
109 break;
110 case WS_IPPROTO_TCP:
111 *optname = IPPROTO_TCP;
115 static void _ws_global_init()
117 if( !_ws_stub )
119 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
120 if( !(_ws_stub = WS_ALLOC(0x10)) )
121 fprintf(stderr,"Fatal: failed to create WinSock heap\n");
123 if( async_qid == -1 )
124 if( (async_qid = msgget(IPC_PRIVATE, IPC_CREAT | 0x1FF)) == -1 )
125 fprintf(stderr,"Fatal: failed to create WinSock resource\n");
128 /* ----------------------------------- Per-thread info */
130 static void wsi_link(LPWSINFO pwsi)
131 { if( _wsi_list ) _wsi_list->prev = pwsi;
132 pwsi->next = _wsi_list; _wsi_list = pwsi;
135 static void wsi_unlink(LPWSINFO pwsi)
137 if( pwsi == _wsi_list ) _wsi_list = pwsi->next;
138 else
139 { pwsi->prev->next = pwsi->next;
140 if( pwsi->next ) pwsi->next->prev = pwsi->prev; }
143 static LPWSINFO wsi_find(HTASK16 hTask)
144 { LPWSINFO pwsi = _wsi_list;
145 while( pwsi && pwsi->tid != hTask ) pwsi = pwsi->next;
146 return pwsi;
149 static ws_socket* wsi_alloc_socket(LPWSINFO pwsi, int fd)
151 if( pwsi->last_free >= 0 )
153 int i = pwsi->last_free;
155 pwsi->last_free = pwsi->sock[i].flags;
156 pwsi->sock[i].fd = fd;
157 pwsi->sock[i].flags = 0;
158 return &pwsi->sock[i];
160 return NULL;
163 static void fd_set_normalize(fd_set* fds, LPWSINFO pwsi, ws_fd_set* ws, int* highfd)
165 FD_ZERO(fds);
166 if(ws)
168 int i;
169 ws_socket* pws;
170 for(i=0;i<(ws->fd_count);i++)
172 pws = (ws_socket*)WS_HANDLE2PTR(ws->fd_array[i]);
173 if( _check_ws(pwsi, pws) )
175 if( pws->fd > *highfd ) *highfd = pws->fd;
176 FD_SET(pws->fd, fds);
183 * Note weirdness here: sockets with errors belong in exceptfds, but
184 * are given to us in readfds or writefds, so move them to exceptfds if
185 * there is an error. Note that this means that exceptfds may have mysterious
186 * sockets set in it that the program never asked for.
189 static int inline sock_error_p(int s)
191 unsigned int optval, optlen;
193 optlen = sizeof(optval);
194 getsockopt(s, SOL_SOCKET, SO_ERROR, &optval, &optlen);
195 if (optval) dprintf_winsock(stddeb, "error: %d\n", optval);
196 return optval != 0;
199 static void fd_set_update(LPWSINFO pwsi, fd_set* fds, ws_fd_set* ws,
200 fd_set *errorfds)
202 if( ws )
204 int i, j, count = ws->fd_count;
206 for( i = 0, j = 0; i < count; i++ )
208 ws_socket *pws = (ws_socket*)WS_HANDLE2PTR(ws->fd_array[i]);
209 int fd = pws->fd;
211 if( _check_ws(pwsi, pws) && FD_ISSET(fd, fds) )
213 /* if error, move to errorfds */
214 if (errorfds && (FD_ISSET(fd, errorfds) || sock_error_p(fd)))
215 FD_SET(fd, errorfds);
216 else
217 ws->fd_array[j++] = ws->fd_array[i];
220 ws->fd_count = j;
221 dprintf_winsock(stddeb, "\n");
223 return;
226 static void fd_set_update_except(LPWSINFO pwsi, fd_set *fds, ws_fd_set *ws,
227 fd_set *errorfds)
229 if (ws)
231 int i, j, count = ws->fd_count;
233 for (i=j=0; i < count; i++)
235 ws_socket *pws = (ws_socket *)WS_HANDLE2PTR(ws->fd_array[i]);
237 if (_check_ws(pwsi, pws) && (FD_ISSET(pws->fd, fds)
238 || FD_ISSET(pws->fd, errorfds)))
239 ws->fd_array[j++] = ws->fd_array[i];
241 ws->fd_count = j;
243 return;
246 /* ----------------------------------- API -----
248 * Init / cleanup / error checking.
251 INT16 WSAStartup(UINT16 wVersionRequested, LPWSADATA lpWSAData)
253 WSADATA WINSOCK_data = { 0x0101, 0x0101,
254 "WINE Sockets 1.1",
255 #ifdef linux
256 "Linux/i386",
257 #elif defined(__NetBSD__)
258 "NetBSD/i386",
259 #elif defined(sunos)
260 "SunOS",
261 #elif defined(__FreeBSD__)
262 "FreeBSD",
263 #else
264 "Unknown",
265 #endif
266 WS_MAX_SOCKETS_PER_THREAD,
267 WS_MAX_UDP_DATAGRAM, NULL };
268 HTASK16 tid = GetCurrentTask();
269 LPWSINFO pwsi;
271 dprintf_winsock(stddeb, "WSAStartup: verReq=%x\n", wVersionRequested);
273 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
274 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
276 if (!lpWSAData) return WSAEINVAL;
278 _ws_global_init();
279 if( _WSHeap == 0 ) return WSASYSNOTREADY;
281 pwsi = wsi_find(GetCurrentTask());
282 if( pwsi == NULL )
284 if( (pwsi = (LPWSINFO)WS_ALLOC( sizeof(WSINFO))) )
286 int i = 0;
287 pwsi->tid = tid;
288 for( i = 0; i < WS_MAX_SOCKETS_PER_THREAD; i++ )
290 pwsi->sock[i].fd = -1;
291 pwsi->sock[i].flags = i + 1;
293 pwsi->sock[WS_MAX_SOCKETS_PER_THREAD - 1].flags = -1;
295 else return WSASYSNOTREADY;
296 wsi_link(pwsi);
297 } else pwsi->num_startup++;
299 /* return winsock information */
300 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
302 dprintf_winsock(stddeb, "WSAStartup: succeeded\n");
303 return(0);
306 void WINSOCK_Shutdown()
308 if( async_qid != -1 )
309 if( msgctl(async_qid, IPC_RMID, NULL) == -1 )
310 fprintf(stderr,"failed to delete WS message queue.\n");
311 else async_qid = -1;
314 INT16 WSACleanup(void)
316 LPWSINFO pwsi = wsi_find(GetCurrentTask());
318 /* FIXME: do global cleanup if no current task */
320 dprintf_winsock(stddeb, "WSACleanup(%08x)\n", (unsigned)pwsi);
321 if( pwsi )
323 int i, j, n;
325 if( pwsi->num_startup-- ) return 0;
327 SIGNAL_MaskAsyncEvents( TRUE );
328 WINSOCK_cancel_async_op(GetCurrentTask());
329 SIGNAL_MaskAsyncEvents( FALSE );
331 wsi_unlink(pwsi);
332 if( _wsi_list == NULL ) WINSOCK_Shutdown();
334 if( pwsi->flags & WSI_BLOCKINGCALL )
335 dprintf_winsock(stddeb,"\tinside blocking call!\n");
336 if( pwsi->num_async_rq )
337 dprintf_winsock(stddeb,"\thave %i outstanding async ops!\n", pwsi->num_async_rq );
339 for(i = 0, j = 0, n = 0; i < WS_MAX_SOCKETS_PER_THREAD; i++)
340 if( pwsi->sock[i].fd != -1 )
342 n += cancel_async_select(&pwsi->sock[i]);
343 close(pwsi->sock[i].fd); j++;
345 if( j )
346 dprintf_winsock(stddeb,"\tclosed %i sockets, killed %i async selects!\n", j, n);
348 if( pwsi->buffer ) SEGPTR_FREE(pwsi->buffer);
349 if( pwsi->dbuffer ) SEGPTR_FREE(pwsi->dbuffer);
350 WS_FREE(pwsi);
351 return 0;
353 return SOCKET_ERROR;
356 INT16 WSAGetLastError(void)
358 LPWSINFO pwsi = wsi_find(GetCurrentTask());
359 INT16 ret;
361 dprintf_winsock(stddeb, "WSAGetLastError(%08x)", (unsigned)pwsi);
363 ret = (pwsi) ? pwsi->err : WSANOTINITIALISED;
365 dprintf_winsock(stddeb, " = %i\n", (int)ret);
366 return ret;
369 void WSASetLastError(INT16 iError)
371 LPWSINFO pwsi = wsi_find(GetCurrentTask());
373 dprintf_winsock(stddeb, "WSASetLastError(%08x): %d\n", (unsigned)pwsi, (int)iError);
375 if( pwsi ) pwsi->err = iError;
378 int _check_ws(LPWSINFO pwsi, ws_socket* pws)
380 if( pwsi )
381 if( pwsi->flags & WSI_BLOCKINGCALL ) pwsi->err = WSAEINPROGRESS;
382 else if( WSI_CHECK_RANGE(pwsi, pws) ) return 1;
383 else pwsi->err = WSAENOTSOCK;
384 return 0;
387 int _check_buffer(LPWSINFO pwsi, int size)
389 if( pwsi->buffer && pwsi->buflen >= size ) return 1;
390 else SEGPTR_FREE(pwsi->buffer);
391 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
392 return (pwsi->buffer != NULL);
395 /* ----- socket operations */
397 SOCKET16 WINSOCK_accept(SOCKET16 s, struct sockaddr *addr, INT16 *addrlen16)
399 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
400 LPWSINFO pwsi = wsi_find(GetCurrentTask());
402 dprintf_winsock(stddeb, "WS_ACCEPT(%08x): socket %04x\n",
403 (unsigned)pwsi, (UINT16)s);
404 if( _check_ws(pwsi, pws) )
406 int sock, fd_flags, addrlen32 = *addrlen16;
408 /* this is how block info is supposed to be used -
409 * WSAIsBlocking() would then check WSI_BLOCKINGCALL bit.
412 fd_flags = fcntl(pws->fd, F_GETFL, 0);
413 if( !(fd_flags & O_NONBLOCK) ) pwsi->flags |= WSI_BLOCKINGCALL;
415 if( (sock = accept(pws->fd, addr, &addrlen32)) >= 0 )
417 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
418 notify_client(pws, WS_FD_ACCEPT);
419 if( pnew )
421 if( pws->p_aop )
422 init_async_select(pnew, pws->p_aop->hWnd,
423 pws->p_aop->uMsg,
424 pws->p_aop->flags & ~WS_FD_ACCEPT );
426 pwsi->flags &= ~WSI_BLOCKINGCALL;
427 return (SOCKET16)WS_PTR2HANDLE(pnew);
429 else pwsi->err = WSAENOBUFS;
431 else pwsi->err = wsaErrno();
433 pwsi->flags &= ~WSI_BLOCKINGCALL;
435 return INVALID_SOCKET;
438 INT16 WINSOCK_bind(SOCKET16 s, struct sockaddr *name, INT16 namelen)
440 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
441 LPWSINFO pwsi = wsi_find(GetCurrentTask());
443 dprintf_winsock(stddeb, "WS_BIND(%08x): socket %04x, ptr %8x, length %d\n",
444 (unsigned)pwsi, s, (int) name, namelen);
445 #if 0
446 dump_sockaddr(name);
447 #endif
449 if ( _check_ws(pwsi, pws) )
450 if (namelen >= sizeof(*name))
451 if ( ((struct sockaddr_in *)name)->sin_family == AF_INET )
452 if ( bind(pws->fd, name, namelen) < 0 )
454 int loc_errno = errno;
455 dprintf_winsock(stddeb,"\tfailure - errno = %i\n", errno);
456 errno = loc_errno;
457 switch(errno)
459 case EBADF: pwsi->err = WSAENOTSOCK; break;
460 case EADDRNOTAVAIL: pwsi->err = WSAEINVAL; break;
461 default: pwsi->err = wsaErrno();
464 else return 0;
465 else pwsi->err = WSAEAFNOSUPPORT;
466 else pwsi->err = WSAEFAULT;
467 return SOCKET_ERROR;
470 INT16 WINSOCK_closesocket(SOCKET16 s)
472 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
473 LPWSINFO pwsi = wsi_find(GetCurrentTask());
475 dprintf_winsock(stddeb, "WS_CLOSE(%08x): socket %08x\n", (unsigned)pwsi, s);
477 if( _check_ws(pwsi, pws) )
479 int fd = pws->fd;
481 cancel_async_select(pws);
482 pws->fd = -1;
483 pws->flags = (unsigned)pwsi->last_free;
484 pwsi->last_free = pws - &pwsi->sock[0];
485 if (close(fd) == 0) return 0;
486 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
488 return SOCKET_ERROR;
491 INT16 WINSOCK_connect(SOCKET16 s, struct sockaddr *name, INT16 namelen)
493 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
494 LPWSINFO pwsi = wsi_find(GetCurrentTask());
496 dprintf_winsock(stddeb, "WS_CONNECT(%08x): socket %04x, ptr %8x, length %d\n",
497 (unsigned)pwsi, s, (int) name, namelen);
498 #if 0
499 dump_sockaddr(name);
500 #endif
502 if( _check_ws(pwsi, pws) )
504 if (connect(pws->fd, name, namelen) == 0)
506 if( pws->p_aop )
507 /* we need to notify handler process if
508 * connect() succeeded NOT in response to winsock message
510 notify_client(pws, WS_FD_CONNECTED);
512 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT);
513 return 0;
515 pwsi->err = (errno == EINPROGRESS) ? WSAEWOULDBLOCK : wsaErrno();
517 return SOCKET_ERROR;
520 INT16 WINSOCK_getpeername(SOCKET16 s, struct sockaddr *name, INT16 *namelen)
522 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
523 LPWSINFO pwsi = wsi_find(GetCurrentTask());
525 dprintf_winsock(stddeb, "WS_GETPEERNAME(%08x): socket: %04x, ptr %8x, ptr %8x\n",
526 (unsigned)pwsi, s, (int) name, *namelen);
527 if( _check_ws(pwsi, pws) )
529 int namelen32 = *namelen;
530 if (getpeername(pws->fd, name, &namelen32) == 0)
532 #if 0
533 dump_sockaddr(name);
534 #endif
535 *namelen = (INT16)namelen32;
536 return 0;
538 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
540 return SOCKET_ERROR;
543 INT16 WINSOCK_getsockname(SOCKET16 s, struct sockaddr *name, INT16 *namelen)
545 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
546 LPWSINFO pwsi = wsi_find(GetCurrentTask());
548 dprintf_winsock(stddeb, "WS_GETSOCKNAME(%08x): socket: %04x, ptr %8x, ptr %8x\n",
549 (unsigned)pwsi, s, (int) name, (int) *namelen);
550 if( _check_ws(pwsi, pws) )
552 int namelen32 = *namelen;
553 if (getsockname(pws->fd, name, &namelen32) == 0)
555 *namelen = (INT16)namelen32;
556 return 0;
558 pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
560 return SOCKET_ERROR;
563 INT16 WINSOCK_getsockopt(SOCKET16 s, INT16 level,
564 INT16 optname, char *optval, INT16 *optlen)
566 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
567 LPWSINFO pwsi = wsi_find(GetCurrentTask());
569 dprintf_winsock(stddeb, "WS_GETSOCKOPT(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
570 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
572 if( _check_ws(pwsi, pws) )
574 int optlen32 = *optlen;
576 convert_sockopt(&level, &optname);
577 if (getsockopt(pws->fd, (int) level, optname, optval, &optlen32) == 0 )
578 { *optlen = (INT16)optlen32; return 0; }
579 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
581 return SOCKET_ERROR;
584 u_long WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
585 u_short WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
586 u_long WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
587 u_long WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
588 u_short WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
590 SEGPTR WINSOCK_inet_ntoa(struct in_addr in)
592 /* use "buffer for dummies" here because some applications have
593 * propensity to decode addresses in ws_hostent structure without
594 * saving them first...
597 LPWSINFO pwsi = wsi_find(GetCurrentTask());
599 if( pwsi )
601 char* s = inet_ntoa(in);
602 if( s )
604 if( pwsi->dbuffer == NULL )
605 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(32)) == NULL )
607 pwsi->err = WSAENOBUFS;
608 return (SEGPTR)NULL;
610 strncpy(pwsi->dbuffer, s, 32 );
611 return SEGPTR_GET(pwsi->dbuffer);
613 pwsi->err = wsaErrno();
615 return (SEGPTR)NULL;
618 INT16 WINSOCK_ioctlsocket(SOCKET16 s, UINT32 cmd, UINT32 *argp)
620 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
621 LPWSINFO pwsi = wsi_find(GetCurrentTask());
623 dprintf_winsock(stddeb, "WS_IOCTL(%08x): socket %04x, cmd %08x, ptr %8x\n",
624 (unsigned)pwsi, s, cmd, (unsigned) argp);
625 if( _check_ws(pwsi, pws) )
627 long newcmd = cmd;
629 switch( cmd )
631 case WS_FIONREAD: newcmd=FIONREAD; break;
632 case WS_FIONBIO: newcmd=FIONBIO;
633 if( pws->p_aop && *argp == 0 )
635 pwsi->err = WSAEINVAL;
636 return SOCKET_ERROR;
638 break;
639 case WS_SIOCATMARK: newcmd=SIOCATMARK; break;
640 case WS_IOW('f',125,u_long):
641 fprintf(stderr,"Warning: WS1.1 shouldn't be using async I/O\n");
642 pwsi->err = WSAEINVAL; return SOCKET_ERROR;
643 default: fprintf(stderr,"Warning: Unknown WS_IOCTL cmd (%08x)\n", cmd);
645 if( ioctl(pws->fd, newcmd, (char*)argp ) == 0 ) return 0;
646 pwsi->err = (errno == EBADF) ? WSAENOTSOCK : wsaErrno();
648 return SOCKET_ERROR;
651 INT16 WINSOCK_listen(SOCKET16 s, INT16 backlog)
653 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
654 LPWSINFO pwsi = wsi_find(GetCurrentTask());
656 dprintf_winsock(stddeb, "WS_LISTEN(%08x): socket %04x, backlog %d\n",
657 (unsigned)pwsi, s, backlog);
658 if( _check_ws(pwsi, pws) )
660 if( !pws->p_aop )
662 int fd_flags = fcntl(pws->fd, F_GETFL, 0);
663 if( !(fd_flags & O_NONBLOCK) ) pws->flags |= WS_FD_ACCEPT;
665 else notify_client(pws, WS_FD_ACCEPT);
667 if (listen(pws->fd, backlog) == 0) return 0;
668 pwsi->err = wsaErrno();
670 return SOCKET_ERROR;
673 INT16 WINSOCK_recv(SOCKET16 s, char *buf, INT16 len, INT16 flags)
675 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
676 LPWSINFO pwsi = wsi_find(GetCurrentTask());
678 dprintf_winsock(stddeb, "WS_RECV(%08x): socket %04x, buf %8x, len %d, flags %d",
679 (unsigned)pwsi, s, (unsigned)buf, len, flags);
680 if( _check_ws(pwsi, pws) )
682 int length;
683 if ((length = recv(pws->fd, buf, len, flags)) >= 0)
685 dprintf_winsock(stddeb, " -> %i bytes\n", length);
686 notify_client(pws, WS_FD_READ);
687 return (INT16)length;
689 pwsi->err = wsaErrno();
691 dprintf_winsock(stddeb, " -> ERROR\n");
692 return SOCKET_ERROR;
695 INT16 WINSOCK_recvfrom(SOCKET16 s, char *buf, INT16 len, INT16 flags,
696 struct sockaddr *from, INT16 *fromlen16)
698 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
699 LPWSINFO pwsi = wsi_find(GetCurrentTask());
701 dprintf_winsock(stddeb, "WS_RECVFROM(%08x): socket %04x, ptr %08x, len %d, flags %d\n",
702 (unsigned)pwsi, s, (unsigned)buf, len, flags);
703 if( _check_ws(pwsi, pws) )
705 int length, fromlen32 = *fromlen16;
707 if ((length = recvfrom(pws->fd, buf, len, flags, from, &fromlen32)) >= 0 );
709 *fromlen16 = fromlen32;
710 notify_client(pws, WS_FD_READ);
711 return (INT16)length;
713 pwsi->err = wsaErrno();
715 return SOCKET_ERROR;
718 INT16 WINSOCK_select(INT16 nfds, ws_fd_set *ws_readfds,
719 ws_fd_set *ws_writefds,
720 ws_fd_set *ws_exceptfds, struct timeval *timeout)
722 LPWSINFO pwsi = wsi_find(GetCurrentTask());
724 dprintf_winsock(stddeb, "WS_SELECT(%08x): nfds %d (ignored), read %8x, write %8x, excp %8x\n",
725 (unsigned) pwsi, nfds, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
727 if( pwsi )
729 int highfd = 0;
730 fd_set readfds, writefds, exceptfds, errorfds;
732 fd_set_normalize(&readfds, pwsi, ws_readfds, &highfd);
733 fd_set_normalize(&writefds, pwsi, ws_writefds, &highfd);
734 fd_set_normalize(&exceptfds, pwsi, ws_exceptfds, &highfd);
735 FD_ZERO(&errorfds);
737 if( (highfd = select(highfd + 1, &readfds, &writefds, &exceptfds, timeout)) >= 0 )
739 if( highfd )
741 fd_set_update(pwsi, &readfds, ws_readfds, &errorfds);
742 fd_set_update(pwsi, &writefds, ws_writefds, &errorfds);
743 fd_set_update_except(pwsi, &exceptfds, ws_exceptfds, &errorfds);
745 return highfd;
747 pwsi->err = wsaErrno();
749 return SOCKET_ERROR;
752 INT16 WINSOCK_send(SOCKET16 s, char *buf, INT16 len, INT16 flags)
754 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
755 LPWSINFO pwsi = wsi_find(GetCurrentTask());
757 dprintf_winsock(stddeb, "WS_SEND(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
758 (unsigned)pwsi, s, (unsigned) buf, len, flags);
759 if( _check_ws(pwsi, pws) )
761 int length;
762 if ((length = send(pws->fd, buf, len, flags)) < 0 )
764 length = SOCKET_ERROR;
765 pwsi->err = wsaErrno();
767 notify_client(pws, WS_FD_WRITE);
768 return (INT16)length;
770 return SOCKET_ERROR;
773 INT16 WINSOCK_sendto(SOCKET16 s, char *buf, INT16 len, INT16 flags,
774 struct sockaddr *to, INT16 tolen)
776 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
777 LPWSINFO pwsi = wsi_find(GetCurrentTask());
779 dprintf_winsock(stddeb, "WS_SENDTO(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
780 (unsigned)pwsi, s, (unsigned) buf, len, flags);
781 if( _check_ws(pwsi, pws) )
783 int length;
785 if ((length = sendto(pws->fd, buf, len, flags, to, tolen)) < 0 )
787 length = SOCKET_ERROR;
788 pwsi->err = wsaErrno();
790 notify_client(pws, WS_FD_WRITE);
791 return (INT16)length;
793 return SOCKET_ERROR;
796 INT16 WINSOCK_setsockopt(SOCKET16 s, INT16 level, INT16 optname,
797 char *optval, INT16 optlen)
799 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
800 LPWSINFO pwsi = wsi_find(GetCurrentTask());
802 dprintf_winsock(stddeb, "WS_SETSOCKOPT(%08x): socket %04x, level %d, opt %d, ptr %08x, len %d\n",
803 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
804 if( _check_ws(pwsi, pws) )
806 int linger32[2];
807 convert_sockopt(&level, &optname);
808 if( optname == SO_LINGER )
810 INT16* ptr = (INT16*)optval;
811 linger32[0] = ptr[0];
812 linger32[1] = ptr[1];
813 optval = (char*)&linger32;
814 optlen = sizeof(linger32);
816 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
817 pwsi->err = wsaErrno();
819 return SOCKET_ERROR;
822 INT16 WINSOCK_shutdown(SOCKET16 s, INT16 how)
824 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
825 LPWSINFO pwsi = wsi_find(GetCurrentTask());
827 dprintf_winsock(stddeb, "WS_SHUTDOWN(%08x): socket %04x, how %i\n",
828 (unsigned)pwsi, s, how );
829 if( _check_ws(pwsi, pws) )
831 pws->flags |= WS_FD_INACTIVE;
832 cancel_async_select(pws);
834 if (shutdown(pws->fd, how) == 0) return 0;
835 pwsi->err = wsaErrno();
837 return SOCKET_ERROR;
840 SOCKET16 WINSOCK_socket(INT16 af, INT16 type, INT16 protocol)
842 LPWSINFO pwsi = wsi_find(GetCurrentTask());
844 dprintf_winsock(stddeb, "WS_SOCKET(%08x): af=%d type=%d protocol=%d\n",
845 (unsigned)pwsi, af, type, protocol);
847 if( pwsi )
849 int sock;
851 /* check the socket family */
852 switch(af)
854 case AF_INET:
855 case AF_UNSPEC: break;
856 default: pwsi->err = WSAEAFNOSUPPORT; return INVALID_SOCKET;
859 /* check the socket type */
860 switch(type)
862 case SOCK_STREAM:
863 case SOCK_DGRAM:
864 case SOCK_RAW: break;
865 default: pwsi->err = WSAESOCKTNOSUPPORT; return INVALID_SOCKET;
868 /* check the protocol type */
869 if ( protocol < 0 ) /* don't support negative values */
870 { pwsi->err = WSAEPROTONOSUPPORT; return INVALID_SOCKET; }
872 if ( af == AF_UNSPEC) /* did they not specify the address family? */
873 switch(protocol)
875 case IPPROTO_TCP:
876 if (type == SOCK_STREAM) { af = AF_INET; break; }
877 case IPPROTO_UDP:
878 if (type == SOCK_DGRAM) { af = AF_INET; break; }
879 default: pwsi->err = WSAEPROTOTYPE; return INVALID_SOCKET;
882 if ((sock = socket(af, type, protocol)) >= 0)
884 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
886 dprintf_winsock(stddeb,"\tcreated %04x (handle %i)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
888 if( pnew ) return (SOCKET16)WS_PTR2HANDLE(pnew);
890 close(sock);
891 pwsi->err = WSAENOBUFS;
892 return INVALID_SOCKET;
896 if (errno == EPERM) /* raw socket denied */
898 fprintf(stderr, "WS_SOCKET: not enough privileges\n");
899 pwsi->err = WSAESOCKTNOSUPPORT;
900 } else pwsi->err = wsaErrno();
903 dprintf_winsock(stddeb, "\t\tfailed!\n");
904 return INVALID_SOCKET;
908 /* ----- database functions
910 * Note that ws_...ent structures we return have SEGPTR pointers inside them.
913 static char* NULL_STRING = "NULL";
916 struct WIN_hostent *
918 SEGPTR WINSOCK_gethostbyaddr(const char *addr, INT16 len, INT16 type)
920 LPWSINFO pwsi = wsi_find(GetCurrentTask());
922 dprintf_winsock(stddeb, "WS_GetHostByAddr(%08x): ptr %8x, len %d, type %d\n",
923 (unsigned)pwsi, (unsigned) addr, len, type);
924 if( pwsi )
926 struct hostent* host;
927 if( (host = gethostbyaddr(addr, len, type)) != NULL )
928 if( WS_dup_he(pwsi, host, WS_DUP_SEGPTR) )
929 return SEGPTR_GET(pwsi->buffer);
930 else pwsi->err = WSAENOBUFS;
931 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
933 return NULL;
937 struct WIN_hostent *
939 SEGPTR WINSOCK_gethostbyname(const char *name)
941 LPWSINFO pwsi = wsi_find(GetCurrentTask());
943 dprintf_winsock(stddeb, "WS_GetHostByName(%08x): %s\n",
944 (unsigned)pwsi, (name)?name:"NULL");
945 if( pwsi )
947 struct hostent* host;
948 if( (host = gethostbyname(name)) != NULL )
949 if( WS_dup_he(pwsi, host, WS_DUP_SEGPTR) )
950 return SEGPTR_GET(pwsi->buffer);
951 else pwsi->err = WSAENOBUFS;
952 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
954 return NULL;
957 INT16 WINSOCK_gethostname(char *name, INT16 namelen)
959 LPWSINFO pwsi = wsi_find(GetCurrentTask());
961 dprintf_winsock(stddeb, "WS_GetHostName(%08x): name %s, len %d\n",
962 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
963 if( pwsi )
965 if (gethostname(name, namelen) == 0) return 0;
966 pwsi->err = (errno == EINVAL) ? WSAEFAULT : wsaErrno();
968 return SOCKET_ERROR;
972 struct WIN_protoent *
974 SEGPTR WINSOCK_getprotobyname(char *name)
976 LPWSINFO pwsi = wsi_find(GetCurrentTask());
978 dprintf_winsock(stddeb, "WS_GetProtoByName(%08x): %s\n",
979 (unsigned)pwsi, (name)?name:NULL_STRING);
980 if( pwsi )
982 struct protoent* proto;
983 if( (proto = getprotobyname(name)) != NULL )
984 if( WS_dup_pe(pwsi, proto, WS_DUP_SEGPTR) )
985 return SEGPTR_GET(pwsi->buffer);
986 else pwsi->err = WSAENOBUFS;
987 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
989 return NULL;
993 struct WIN_protoent *
995 SEGPTR WINSOCK_getprotobynumber(INT16 number)
997 LPWSINFO pwsi = wsi_find(GetCurrentTask());
999 dprintf_winsock(stddeb, "WS_GetProtoByNumber(%08x): %i\n", (unsigned)pwsi, number);
1001 if( pwsi )
1003 struct protoent* proto;
1004 if( (proto = getprotobynumber(number)) != NULL )
1005 if( WS_dup_pe(pwsi, proto, WS_DUP_SEGPTR) )
1006 return SEGPTR_GET(pwsi->buffer);
1007 else pwsi->err = WSAENOBUFS;
1008 else pwsi->err = WSANO_DATA;
1010 return NULL;
1014 struct WIN_servent *
1016 SEGPTR WINSOCK_getservbyname(const char *name, const char *proto)
1018 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1020 dprintf_winsock(stddeb, "WS_GetServByName(%08x): '%s', '%s'\n",
1021 (unsigned)pwsi, (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
1023 if( pwsi )
1025 struct servent* serv;
1026 if( (serv = getservbyname(name, proto)) != NULL )
1027 if( WS_dup_se(pwsi, serv, WS_DUP_SEGPTR) )
1028 return SEGPTR_GET(pwsi->buffer);
1029 else pwsi->err = WSAENOBUFS;
1030 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1032 return NULL;
1036 struct WIN_servent *
1038 SEGPTR WINSOCK_getservbyport(INT16 port, const char *proto)
1040 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1042 dprintf_winsock(stddeb, "WS_GetServByPort(%08x): %i, '%s'\n",
1043 (unsigned)pwsi, (int)port, (proto)?proto:NULL_STRING);
1044 if( pwsi )
1046 struct servent* serv;
1047 if( (serv = getservbyport(port, proto)) != NULL )
1048 if( WS_dup_se(pwsi, serv, WS_DUP_SEGPTR) )
1049 return SEGPTR_GET(pwsi->buffer);
1050 else pwsi->err = WSAENOBUFS;
1051 else pwsi->err = (h_errno < 0) ? wsaErrno() : wsaHerrno();
1053 return NULL;
1057 /* ----------------------------------- Windows sockets extensions -- *
1059 * ----------------------------------------------------------------- */
1061 static int aop_control(ws_async_op* p_aop, int flag )
1063 unsigned lLength;
1065 read(p_aop->fd[0], &lLength, sizeof(unsigned));
1066 if( LOWORD(lLength) )
1067 if( (int)LOWORD(lLength) <= p_aop->buflen )
1069 char* buffer = (char*)PTR_SEG_TO_LIN(p_aop->buffer_base);
1070 read(p_aop->fd[0], buffer, LOWORD(lLength));
1071 switch( p_aop->flags )
1073 case WSMSG_ASYNC_HOSTBYNAME:
1074 case WSMSG_ASYNC_HOSTBYADDR:
1075 fixup_wshe((struct ws_hostent*)buffer, p_aop->buffer_base); break;
1076 case WSMSG_ASYNC_PROTOBYNAME:
1077 case WSMSG_ASYNC_PROTOBYNUM:
1078 fixup_wspe((struct ws_protoent*)buffer, p_aop->buffer_base); break;
1079 case WSMSG_ASYNC_SERVBYNAME:
1080 case WSMSG_ASYNC_SERVBYPORT:
1081 fixup_wsse((struct ws_servent*)buffer, p_aop->buffer_base); break;
1082 default:
1083 if( p_aop->flags ) fprintf(stderr,"Received unknown async request!\n");
1084 return AOP_CONTROL_REMOVE;
1087 else lLength = ((UINT32)LOWORD(lLength)) | ((unsigned)WSAENOBUFS << 16);
1089 #if 0
1090 printf("async op completed: hWnd [%04x], uMsg [%04x], aop [%04x], event [%08x]\n",
1091 p_aop->hWnd, p_aop->uMsg, (HANDLE16)WS_PTR2HANDLE(p_aop), (LPARAM)lLength);
1092 #endif
1094 PostMessage16(p_aop->hWnd, p_aop->uMsg, (HANDLE16)WS_PTR2HANDLE(p_aop), (LPARAM)lLength);
1095 return AOP_CONTROL_REMOVE;
1099 static HANDLE16 __WSAsyncDBQuery(LPWSINFO pwsi, HWND16 hWnd, UINT16 uMsg, LPCSTR init,
1100 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen, UINT32 flag)
1102 /* queue 'flag' request and fork off its handler */
1104 async_ctl.ws_aop = (ws_async_op*)WS_ALLOC(sizeof(ws_async_op));
1106 if( async_ctl.ws_aop )
1108 HANDLE16 handle = (HANDLE16)WS_PTR2HANDLE(async_ctl.ws_aop);
1110 if( pipe(async_ctl.ws_aop->fd) == 0 )
1112 async_ctl.init = (char*)init;
1113 async_ctl.lLength = len;
1114 async_ctl.lEvent = type;
1116 async_ctl.ws_aop->hWnd = hWnd;
1117 async_ctl.ws_aop->uMsg = uMsg;
1119 async_ctl.ws_aop->buffer_base = sbuf; async_ctl.ws_aop->buflen = buflen;
1120 async_ctl.ws_aop->flags = flag;
1121 async_ctl.ws_aop->aop_control = &aop_control;
1122 WINSOCK_link_async_op( async_ctl.ws_aop );
1124 async_ctl.ws_aop->pid = fork();
1125 if( async_ctl.ws_aop->pid )
1127 close(async_ctl.ws_aop->fd[1]); /* write endpoint */
1129 /* Damn, BSD'ish SIGIO doesn't work on pipes/streams
1131 * async_io(async_ctl.ws_aop->fd[0], 1);
1134 dprintf_winsock(stddeb, "\tasync_op = %04x (child %i)\n",
1135 handle, async_ctl.ws_aop->pid);
1136 return handle;
1137 } else
1138 /* child process */
1140 close(async_ctl.ws_aop->fd[0]); /* read endpoint */
1141 switch(flag)
1143 case WSMSG_ASYNC_HOSTBYADDR:
1144 case WSMSG_ASYNC_HOSTBYNAME:
1145 WS_do_async_gethost(pwsi,flag);
1146 case WSMSG_ASYNC_PROTOBYNUM:
1147 case WSMSG_ASYNC_PROTOBYNAME:
1148 WS_do_async_getproto(pwsi,flag);
1149 case WSMSG_ASYNC_SERVBYPORT:
1150 case WSMSG_ASYNC_SERVBYNAME:
1151 WS_do_async_getserv(pwsi,flag);
1153 _exit(0); /* skip atexit()'ed cleanup */
1156 WS_FREE(async_ctl.ws_aop);
1157 pwsi->err = wsaErrno();
1158 } else pwsi->err = WSAEWOULDBLOCK;
1159 return 0;
1162 HANDLE16 WSAAsyncGetHostByAddr(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
1163 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
1165 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1167 dprintf_winsock(stddeb, "WS_AsyncGetHostByAddr(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1168 (unsigned)pwsi, hWnd, uMsg, (unsigned)addr , len );
1170 if( pwsi )
1171 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, addr, len,
1172 type, sbuf, buflen, WSMSG_ASYNC_HOSTBYADDR );
1173 return 0;
1177 HANDLE16 WSAAsyncGetHostByName(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1178 SEGPTR sbuf, INT16 buflen)
1180 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1182 dprintf_winsock(stddeb, "WS_AsyncGetHostByName(%08x): hwnd %04x, msg %04x, host %s, buffer %i\n",
1183 (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING, (int)buflen );
1185 if( pwsi )
1186 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, name, 0,
1187 0, sbuf, buflen, WSMSG_ASYNC_HOSTBYNAME );
1188 return 0;
1192 HANDLE16 WSAAsyncGetProtoByName(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1193 SEGPTR sbuf, INT16 buflen)
1195 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1197 dprintf_winsock(stddeb, "WS_AsyncGetProtoByName(%08x): hwnd %04x, msg %04x, protocol %s\n",
1198 (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING );
1200 if( pwsi )
1201 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, name, 0,
1202 0, sbuf, buflen, WSMSG_ASYNC_PROTOBYNAME );
1203 return 0;
1207 HANDLE16 WSAAsyncGetProtoByNumber(HWND16 hWnd, UINT16 uMsg, INT16 number,
1208 SEGPTR sbuf, INT16 buflen)
1210 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1212 dprintf_winsock(stddeb, "WS_AsyncGetProtoByNumber(%08x): hwnd %04x, msg %04x, num %i\n",
1213 (unsigned)pwsi, hWnd, uMsg, number );
1215 if( pwsi )
1216 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, NULL, 0,
1217 number, sbuf, buflen, WSMSG_ASYNC_PROTOBYNUM );
1218 return 0;
1222 HANDLE16 WSAAsyncGetServByName(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
1223 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
1225 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1227 dprintf_winsock(stddeb, "WS_AsyncGetServByName(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
1228 (unsigned)pwsi, hWnd, uMsg, (name)?name:NULL_STRING, (proto)?proto:NULL_STRING );
1230 if( pwsi )
1232 async_ctl.buffer = (char*)proto;
1233 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, name, 0,
1234 0, sbuf, buflen, WSMSG_ASYNC_SERVBYNAME );
1236 return 0;
1240 HANDLE16 WSAAsyncGetServByPort(HWND16 hWnd, UINT16 uMsg, INT16 port,
1241 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
1243 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1245 dprintf_winsock(stddeb, "WS_AsyncGetServByPort(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
1246 (unsigned)pwsi, hWnd, uMsg, port, (proto)?proto:NULL_STRING );
1248 if( pwsi )
1249 return __WSAsyncDBQuery(pwsi, hWnd, uMsg, proto, 0,
1250 port, sbuf, buflen, WSMSG_ASYNC_SERVBYPORT );
1251 return 0;
1254 INT16 WSACancelAsyncRequest(HANDLE16 hAsyncTaskHandle)
1256 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1257 ws_async_op* p_aop = (ws_async_op*)WS_HANDLE2PTR(hAsyncTaskHandle);
1259 dprintf_winsock(stddeb, "WS_CancelAsyncRequest(%08x): handle %04x\n",
1260 (unsigned)pwsi, hAsyncTaskHandle);
1261 if( pwsi )
1262 if( WINSOCK_check_async_op(p_aop) )
1264 kill(p_aop->pid, SIGKILL);
1265 waitpid(p_aop->pid, NULL, 0); /* just in case */
1266 close(p_aop->fd[0]);
1267 WINSOCK_unlink_async_op(p_aop);
1268 WS_FREE(p_aop);
1269 return 0;
1271 else pwsi->err = WSAEINVAL;
1272 return SOCKET_ERROR;
1275 /* ----- asynchronous select() */
1277 int cancel_async_select(ws_socket* pws)
1279 if( pws->p_aop )
1281 kill(pws->p_aop->pid, SIGKILL);
1282 waitpid(pws->p_aop->pid, NULL, 0);
1283 WS_FREE(pws->p_aop);
1284 pws->p_aop = NULL;
1285 return 1;
1287 return 0;
1290 void _sigusr1_handler_parent(int sig)
1292 /* child process puts MTYPE_CLIENT data packet into the
1293 * 'async_qid' message queue and signals us with SIGUSR1.
1294 * This handler reads the queue and posts 'uMsg' notification
1295 * message.
1298 ipc_packet ipack;
1300 signal( SIGUSR1, _sigusr1_handler_parent);
1301 while( msgrcv(async_qid, (struct msgbuf*)&ipack,
1302 MTYPE_CLIENT_SIZE, MTYPE_CLIENT, IPC_NOWAIT) != -1 )
1304 if( ipack.wParam && abs((short)ipack.wParam) < 32768 )
1306 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(ipack.wParam);
1307 if( pws->p_aop && abs((char*)_ws_stub - (char*)pws->p_aop) < 32768 )
1309 pws->flags &= ~(ipack.lParam);
1310 #if 0
1311 printf("async event - hWnd %04x, uMsg %04x [%08x]\n",
1312 pws->p_aop->hWnd, pws->p_aop->uMsg, ipack.lParam );
1313 #endif
1314 PostMessage16(pws->p_aop->hWnd, pws->p_aop->uMsg,
1315 (WPARAM16)ipack.wParam, (LPARAM)ipack.lParam );
1317 else fprintf(stderr,"AsyncSelect:stray async_op in socket %04x!\n", ipack.wParam);
1319 else fprintf(stderr,"AsyncSelect:stray socket at %04x!\n", ipack.wParam);
1323 int notify_client( ws_socket* pws, unsigned flag )
1325 if( pws->p_aop && ((pws->p_aop->flags & flag) ||
1326 (flag == WS_FD_CONNECTED && pws->flags & WS_FD_CONNECT)) )
1328 async_ctl.ip.mtype = MTYPE_PARENT;
1329 async_ctl.ip.lParam = flag;
1330 while( msgsnd(async_qid, (struct msgbuf*)&(async_ctl.ip),
1331 MTYPE_PARENT_SIZE, 0) == -1 )
1333 if( errno == EINTR ) continue;
1334 else
1336 perror("AsyncSelect(parent)");
1337 cancel_async_select(pws);
1338 pws->flags &= WS_FD_INTERNAL;
1339 return 0;
1342 kill(pws->p_aop->pid, SIGUSR1);
1343 return 1;
1345 return 0;
1348 INT16 init_async_select(ws_socket* pws, HWND16 hWnd, UINT16 uMsg, UINT32 lEvent)
1350 ws_async_op* p_aop;
1352 if( cancel_async_select(pws) ) /* delete old async handler if any */
1353 pws->flags &= WS_FD_INTERNAL;
1355 if( lEvent == 0 ) return 0;
1357 /* setup async handler - some data may be redundant */
1359 WINSOCK_unblock_io(pws->fd, 1);
1360 if( (p_aop = (ws_async_op*)WS_ALLOC(sizeof(ws_async_op))) )
1362 p_aop->hWnd = hWnd;
1363 p_aop->uMsg = uMsg;
1364 pws->p_aop = p_aop;
1366 async_ctl.lEvent = p_aop->flags = lEvent;
1367 async_ctl.ws_sock = pws;
1368 async_ctl.ip.wParam = (UINT16)WS_PTR2HANDLE(pws);
1369 async_ctl.ip.lParam = 0;
1371 p_aop->pid = fork();
1372 if( p_aop->pid != -1 )
1373 if( p_aop->pid == 0 ) WINSOCK_do_async_select(); /* child process */
1374 else pws->flags |= lEvent;
1376 signal( SIGUSR1, _sigusr1_handler_parent );
1377 return 0; /* Wine process */
1379 return SOCKET_ERROR;
1382 INT16 WSAAsyncSelect(SOCKET16 s, HWND16 hWnd, UINT16 uMsg, UINT32 lEvent)
1384 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1385 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1387 dprintf_winsock(stddeb, "WS_AsyncSelect(%08x): %04x, hWnd %04x, uMsg %04x, event %08x\n",
1388 (unsigned)pwsi, s, hWnd, uMsg, (unsigned)lEvent );
1389 if( _check_ws(pwsi, pws) )
1390 if( init_async_select(pws, hWnd, uMsg, lEvent) == 0 ) return 0;
1391 else pwsi->err = WSAENOBUFS;
1392 return SOCKET_ERROR;
1395 /* ----- miscellaneous */
1397 INT16 __WSAFDIsSet(SOCKET16 fd, ws_fd_set *set)
1399 int i = set->fd_count;
1401 dprintf_winsock(stddeb, "__WSAFDIsSet(%d,%8lx)\n",fd,(unsigned long)set);
1403 while (i--)
1404 if (set->fd_array[i] == fd) return 1;
1405 return 0;
1408 BOOL16 WSAIsBlocking(void)
1410 /* By default WinSock should set all its sockets to non-blocking mode
1411 * and poll in PeekMessage loop when processing "blocking" ones. This
1412 * function * is supposed to tell if program is in this loop. Our
1413 * blocking calls are truly blocking so we always return FALSE.
1415 * Note: It is allowed to call this function without prior WSAStartup().
1418 dprintf_winsock(stddeb, "WS_IsBlocking()\n");
1419 return FALSE;
1422 INT16 WSACancelBlockingCall(void)
1424 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1426 dprintf_winsock(stddeb, "WS_CancelBlockingCall(%08x)\n", (unsigned)pwsi);
1428 if( pwsi ) return 0;
1429 return SOCKET_ERROR;
1432 FARPROC16 WSASetBlockingHook(FARPROC16 lpBlockFunc)
1434 FARPROC16 prev;
1435 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1437 dprintf_winsock(stddeb, "WS_SetBlockingHook(%08x): hook %08x\n",
1438 (unsigned)pwsi, (unsigned) lpBlockFunc);
1440 if( pwsi ) {
1441 prev = pwsi->blocking_hook;
1442 pwsi->blocking_hook = lpBlockFunc;
1443 return prev;
1445 return 0;
1448 INT16 WSAUnhookBlockingHook(void)
1450 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1452 dprintf_winsock(stddeb, "WS_UnhookBlockingHook(%08x)\n", (unsigned)pwsi);
1453 if( pwsi ) return (INT16)(INT32)(pwsi->blocking_hook = (FARPROC16)NULL);
1454 return SOCKET_ERROR;
1457 VOID
1458 WsControl(DWORD x1,DWORD x2,LPDWORD x3,LPDWORD x4,LPDWORD x5,LPDWORD x6)
1460 fprintf(stdnimp,"WsControl(%lx,%lx,%p,%p,%p,%p)\n",
1461 x1,x2,x3,x4,x5,x6
1463 fprintf(stdnimp,"WsControl(x,x,%lx,%lx,%lx,%lx)\n",
1464 x3?*x3:0,x4?*x4:0,x5?*x5:0,x6?*x6:0
1466 return;
1468 /* ----------------------------------- end of API stuff */
1472 /* ----------------------------------- helper functions */
1474 static int list_size(char** l, int item_size)
1476 int i,j = 0;
1477 if(l)
1478 { for(i=0;l[i];i++)
1479 j += (item_size) ? item_size : strlen(l[i]) + 1;
1480 j += (i + 1) * sizeof(char*); }
1481 return j;
1484 static int list_dup(char** l_src, char* ref, char* base, int item_size)
1486 /* base is either either equal to ref or 0 or SEGPTR */
1488 char* p = ref;
1489 char** l_to = (char**)ref;
1490 int i,j,k;
1492 for(j=0;l_src[j];j++) ;
1493 p += (j + 1) * sizeof(char*);
1494 for(i=0;i<j;i++)
1495 { l_to[i] = base + (p - ref);
1496 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
1497 memcpy(p, l_src[i], k); p += k; }
1498 l_to[i] = NULL;
1499 return (p - ref);
1502 /* ----- hostent */
1504 static int hostent_size(struct hostent* p_he)
1506 int size = 0;
1507 if( p_he )
1508 { size = sizeof(struct hostent);
1509 size += strlen(p_he->h_name) + 1;
1510 size += list_size(p_he->h_aliases, 0);
1511 size += list_size(p_he->h_addr_list, p_he->h_length ); }
1512 return size;
1515 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
1517 /* Duplicate hostent structure and flatten data (with its pointers)
1518 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
1519 * relative to 0 depending on "flag" value. Return data size (also
1520 * in the pwsi->buflen).
1523 int size = hostent_size(p_he);
1524 if( size )
1526 char* p_name,*p_aliases,*p_addr,*p_base,*p;
1528 _check_buffer(pwsi, size);
1529 p = pwsi->buffer;
1530 p_base = (flag & WS_DUP_OFFSET) ? NULL
1531 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
1532 p += (flag & WS_DUP_SEGPTR) ? sizeof(struct ws_hostent) : sizeof(struct hostent);
1533 p_name = p;
1534 strcpy(p, p_he->h_name); p += strlen(p) + 1;
1535 p_aliases = p;
1536 p += list_dup(p_he->h_aliases, p, p_base + (p - pwsi->buffer), 0);
1537 p_addr = p;
1538 list_dup(p_he->h_addr_list, p, p_base + (p - pwsi->buffer), p_he->h_length);
1539 if( !(flag & WS_DUP_SEGPTR) )
1540 { struct hostent* p_to = (struct hostent*)pwsi->buffer;
1541 p_to->h_addrtype = p_he->h_addrtype; p_to->h_length = p_he->h_length;
1542 p_to->h_name = p_base + (p_name - pwsi->buffer);
1543 p_to->h_aliases = (char**)(p_base + (p_aliases - pwsi->buffer));
1544 p_to->h_addr_list = (char**)(p_base + (p_addr - pwsi->buffer)); }
1545 else
1546 { struct ws_hostent* p_to = (struct ws_hostent*)pwsi->buffer;
1547 p_to->h_addrtype = (INT16)p_he->h_addrtype;
1548 p_to->h_length = (INT16)p_he->h_length;
1549 p_to->h_name = (SEGPTR)(p_base + (p_name - pwsi->buffer));
1550 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - pwsi->buffer));
1551 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - pwsi->buffer));
1553 return (size + sizeof(struct ws_hostent) - sizeof(struct hostent)); }
1555 return size;
1558 void fixup_wshe(struct ws_hostent* p_wshe, SEGPTR base)
1560 /* add 'base' to ws_hostent pointers to convert them from offsets */
1562 int i;
1563 unsigned* p_aliases,*p_addr;
1565 p_aliases = (unsigned*)((char*)p_wshe + (unsigned)p_wshe->h_aliases);
1566 p_addr = (unsigned*)((char*)p_wshe + (unsigned)p_wshe->h_addr_list);
1567 ((unsigned)(p_wshe->h_name)) += (unsigned)base;
1568 ((unsigned)(p_wshe->h_aliases)) += (unsigned)base;
1569 ((unsigned)(p_wshe->h_addr_list)) += (unsigned)base;
1570 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;
1571 for(i=0;p_addr[i];i++) p_addr[i] += (unsigned)base;
1574 /* ----- protoent */
1576 static int protoent_size(struct protoent* p_pe)
1578 int size = 0;
1579 if( p_pe )
1580 { size = sizeof(struct protoent);
1581 size += strlen(p_pe->p_name) + 1;
1582 size += list_size(p_pe->p_aliases, 0); }
1583 return size;
1586 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
1588 int size = protoent_size(p_pe);
1589 if( size )
1591 char* p_name,*p_aliases,*p_base,*p;
1593 _check_buffer(pwsi, size);
1594 p = pwsi->buffer;
1595 p_base = (flag & WS_DUP_OFFSET) ? NULL
1596 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
1597 p += (flag & WS_DUP_SEGPTR)? sizeof(struct ws_protoent) : sizeof(struct protoent);
1598 p_name = p;
1599 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
1600 p_aliases = p;
1601 list_dup(p_pe->p_aliases, p, p_base + (p - pwsi->buffer), 0);
1602 if( !(flag & WS_DUP_NATIVE) )
1603 { struct protoent* p_to = (struct protoent*)pwsi->buffer;
1604 p_to->p_proto = p_pe->p_proto;
1605 p_to->p_name = p_base + (p_name - pwsi->buffer);
1606 p_to->p_aliases = (char**)(p_base + (p_aliases - pwsi->buffer)); }
1607 else
1608 { struct ws_protoent* p_to = (struct ws_protoent*)pwsi->buffer;
1609 p_to->p_proto = (INT16)p_pe->p_proto;
1610 p_to->p_name = (SEGPTR)(p_base) + (p_name - pwsi->buffer);
1611 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - pwsi->buffer));
1612 return (size + sizeof(struct ws_protoent) - sizeof(struct protoent)); }
1614 return size;
1617 void fixup_wspe(struct ws_protoent* p_wspe, SEGPTR base)
1619 int i;
1620 unsigned* p_aliases = (unsigned*)((char*)p_wspe + (unsigned)p_wspe->p_aliases);
1621 ((unsigned)(p_wspe->p_name)) += (unsigned)base;
1622 ((unsigned)(p_wspe->p_aliases)) += (unsigned)base;
1623 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;
1626 /* ----- servent */
1628 static int servent_size(struct servent* p_se)
1630 int size = 0;
1631 if( p_se )
1632 { size += sizeof(struct servent);
1633 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
1634 size += list_size(p_se->s_aliases, 0); }
1635 return size;
1638 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
1640 int size = servent_size(p_se);
1641 if( size )
1643 char* p_name,*p_aliases,*p_proto,*p_base,*p;
1645 _check_buffer(pwsi, size);
1646 p = pwsi->buffer;
1647 p_base = (flag & WS_DUP_OFFSET) ? NULL
1648 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
1649 p += (flag & WS_DUP_SEGPTR)? sizeof(struct ws_servent) : sizeof(struct servent);
1650 p_name = p;
1651 strcpy(p, p_se->s_name); p += strlen(p) + 1;
1652 p_proto = p;
1653 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
1654 p_aliases = p;
1655 list_dup(p_se->s_aliases, p, p_base + (p - pwsi->buffer), 0);
1657 if( !(flag & WS_DUP_SEGPTR) )
1658 { struct servent* p_to = (struct servent*)pwsi->buffer;
1659 p_to->s_port = p_se->s_port;
1660 p_to->s_name = p_base + (p_name - pwsi->buffer);
1661 p_to->s_proto = p_base + (p_proto - pwsi->buffer);
1662 p_to->s_aliases = (char**)(p_base + (p_aliases - pwsi->buffer)); }
1663 else
1664 { struct ws_servent* p_to = (struct ws_servent*)pwsi->buffer;
1665 p_to->s_port = (INT16)p_se->s_port;
1666 p_to->s_name = (SEGPTR)(p_base + (p_name - pwsi->buffer));
1667 p_to->s_proto = (SEGPTR)(p_base + (p_proto - pwsi->buffer));
1668 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - pwsi->buffer));
1669 return (size + sizeof(struct ws_servent) - sizeof(struct servent)); }
1671 return size;
1674 void fixup_wsse(struct ws_servent* p_wsse, SEGPTR base)
1676 int i;
1677 unsigned* p_aliases = (unsigned*)((char*)p_wsse + (unsigned)p_wsse->s_aliases);
1678 ((unsigned)(p_wsse->s_name)) += (unsigned)base;
1679 ((p_wsse->s_proto)) += (unsigned)base;
1680 ((p_wsse->s_aliases)) += (unsigned)base;
1681 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;
1684 /* ----------------------------------- error handling */
1686 UINT16 wsaErrno(void)
1688 int loc_errno = errno;
1689 #if defined(__FreeBSD__)
1690 dprintf_winsock(stderr, "winsock: errno %d, (%s).\n",
1691 errno, sys_errlist[errno]);
1692 #else
1693 dprintf_winsock(stderr, "winsock: errno %d\n", errno);
1694 #endif
1696 switch(loc_errno)
1698 case EINTR: return WSAEINTR;
1699 case EBADF: return WSAEBADF;
1700 case EACCES: return WSAEACCES;
1701 case EFAULT: return WSAEFAULT;
1702 case EINVAL: return WSAEINVAL;
1703 case EMFILE: return WSAEMFILE;
1704 case EWOULDBLOCK: return WSAEWOULDBLOCK;
1705 case EINPROGRESS: return WSAEINPROGRESS;
1706 case EALREADY: return WSAEALREADY;
1707 case ENOTSOCK: return WSAENOTSOCK;
1708 case EDESTADDRREQ: return WSAEDESTADDRREQ;
1709 case EMSGSIZE: return WSAEMSGSIZE;
1710 case EPROTOTYPE: return WSAEPROTOTYPE;
1711 case ENOPROTOOPT: return WSAENOPROTOOPT;
1712 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
1713 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
1714 case EOPNOTSUPP: return WSAEOPNOTSUPP;
1715 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
1716 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
1717 case EADDRINUSE: return WSAEADDRINUSE;
1718 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
1719 case ENETDOWN: return WSAENETDOWN;
1720 case ENETUNREACH: return WSAENETUNREACH;
1721 case ENETRESET: return WSAENETRESET;
1722 case ECONNABORTED: return WSAECONNABORTED;
1723 case ECONNRESET: return WSAECONNRESET;
1724 case ENOBUFS: return WSAENOBUFS;
1725 case EISCONN: return WSAEISCONN;
1726 case ENOTCONN: return WSAENOTCONN;
1727 case ESHUTDOWN: return WSAESHUTDOWN;
1728 case ETOOMANYREFS: return WSAETOOMANYREFS;
1729 case ETIMEDOUT: return WSAETIMEDOUT;
1730 case ECONNREFUSED: return WSAECONNREFUSED;
1731 case ELOOP: return WSAELOOP;
1732 case ENAMETOOLONG: return WSAENAMETOOLONG;
1733 case EHOSTDOWN: return WSAEHOSTDOWN;
1734 case EHOSTUNREACH: return WSAEHOSTUNREACH;
1735 case ENOTEMPTY: return WSAENOTEMPTY;
1736 #ifdef EPROCLIM
1737 case EPROCLIM: return WSAEPROCLIM;
1738 #endif
1739 #ifdef EUSERS
1740 case EUSERS: return WSAEUSERS;
1741 #endif
1742 #ifdef EDQUOT
1743 case EDQUOT: return WSAEDQUOT;
1744 #endif
1745 #ifdef ESTALE
1746 case ESTALE: return WSAESTALE;
1747 #endif
1748 #ifdef EREMOTE
1749 case EREMOTE: return WSAEREMOTE;
1750 #endif
1752 /* just in case we ever get here and there are no problems */
1753 case 0: return 0;
1754 default:
1755 fprintf(stderr, "winsock: unknown errno %d!\n", errno);
1756 return WSAEOPNOTSUPP;
1760 UINT16 wsaHerrno(void)
1762 int loc_errno = h_errno;
1764 #if defined(__FreeBSD__)
1765 dprintf_winsock(stderr, "winsock: h_errno %d, (%s).\n",
1766 h_errno, sys_errlist[h_errno]);
1767 #else
1768 dprintf_winsock(stderr, "winsock: h_errno %d.\n", h_errno);
1769 #ifndef sun
1770 if( debugging_winsock ) herror("wine: winsock: wsaherrno");
1771 #endif
1772 #endif
1774 switch(loc_errno)
1776 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
1777 case TRY_AGAIN: return WSATRY_AGAIN;
1778 case NO_RECOVERY: return WSANO_RECOVERY;
1779 case NO_DATA: return WSANO_DATA;
1781 case 0: return 0;
1782 default:
1783 fprintf(stderr, "winsock: unknown h_errno %d!\n", h_errno);
1784 return WSAEOPNOTSUPP;