2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
5 * (C) 1993,1994,1996 John Brezak, Erik Bos, Alex Korobka.
7 * TODO: Merge Async...() handling with X event loop by adding
8 * thread/task fdset to select(2) in windows/event.c.
9 * Also fork dns lookup helper during the startup (with a pipe
10 * for communication) and make it fork for a database request
11 * instead of forking the main process (i.e. something like
18 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <sys/filio.h>
23 #include <sys/ioccom.h>
24 #include <sys/sockio.h>
27 #include <sys/so_ioctl.h>
28 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
49 #define dump_sockaddr(a) \
50 fprintf(stderr, "sockaddr_in: family %d, address %s, port %d\n", \
51 ((struct sockaddr_in *)a)->sin_family, \
52 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
53 ntohs(((struct sockaddr_in *)a)->sin_port))
57 /* ----------------------------------- internal data */
60 extern void __sigio(int);
62 ws_async_ctl async_ctl
;
65 static HANDLE32 _WSHeap
= 0;
66 static unsigned char* _ws_stub
= NULL
;
67 static LPWSINFO _wsi_list
= NULL
;
69 #define WS_ALLOC(size) \
70 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
71 #define WS_FREE(ptr) \
72 HeapFree(_WSHeap, 0, (ptr) )
74 #define WS_PTR2HANDLE(ptr) \
75 ((short)((int)(ptr) - (int)_ws_stub))
76 #define WS_HANDLE2PTR(handle) \
77 ((unsigned)((int)_ws_stub + (int)handle))
79 #define WSI_CHECK_RANGE(pwsi, pws) \
80 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
81 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
83 static INT16 _ws_sock_ops
[] =
84 { WS_SO_DEBUG
, WS_SO_REUSEADDR
, WS_SO_KEEPALIVE
, WS_SO_DONTROUTE
,
85 WS_SO_BROADCAST
, WS_SO_LINGER
, WS_SO_OOBINLINE
, WS_SO_SNDBUF
,
86 WS_SO_RCVBUF
, WS_SO_ERROR
, WS_SO_TYPE
, WS_SO_DONTLINGER
, 0 };
87 static int _px_sock_ops
[] =
88 { SO_DEBUG
, SO_REUSEADDR
, SO_KEEPALIVE
, SO_DONTROUTE
, SO_BROADCAST
,
89 SO_LINGER
, SO_OOBINLINE
, SO_SNDBUF
, SO_RCVBUF
, SO_ERROR
, SO_TYPE
,
92 static INT16
init_async_select(ws_socket
* pws
, HWND16 hWnd
, UINT16 uMsg
, UINT32 lEvent
);
93 static int notify_client(ws_socket
* pws
, unsigned flag
);
95 static int _check_ws(LPWSINFO pwsi
, ws_socket
* pws
);
96 static int _check_buffer(LPWSINFO pwsi
, int size
);
98 static void fixup_wshe(struct ws_hostent
* p_wshe
, SEGPTR base
);
99 static void fixup_wspe(struct ws_protoent
* p_wspe
, SEGPTR base
);
100 static void fixup_wsse(struct ws_servent
* p_wsse
, SEGPTR base
);
102 static int cancel_async_select(ws_socket
*);
104 static void convert_sockopt(INT16
*level
, INT16
*optname
)
111 for(i
=0; _ws_sock_ops
[i
]; i
++)
112 if( _ws_sock_ops
[i
] == *optname
) break;
113 if( _ws_sock_ops
[i
] ) *optname
= (INT16
)_px_sock_ops
[i
];
114 else fprintf(stderr
, "convert_sockopt() unknown optname %d\n", *optname
);
117 *optname
= IPPROTO_TCP
;
121 static void _ws_global_init()
125 _WSHeap
= HeapCreate(HEAP_ZERO_MEMORY
, 8120, 32768);
126 if( !(_ws_stub
= WS_ALLOC(0x10)) )
127 fprintf(stderr
,"Fatal: failed to create WinSock heap\n");
129 if( async_qid
== -1 )
130 if( (async_qid
= msgget(IPC_PRIVATE
, IPC_CREAT
| 0x1FF)) == -1 )
131 fprintf(stderr
,"Fatal: failed to create WinSock resource\n");
134 /* ----------------------------------- Per-thread info */
136 static void wsi_link(LPWSINFO pwsi
)
137 { if( _wsi_list
) _wsi_list
->prev
= pwsi
;
138 pwsi
->next
= _wsi_list
; _wsi_list
= pwsi
;
141 static void wsi_unlink(LPWSINFO pwsi
)
143 if( pwsi
== _wsi_list
) _wsi_list
= pwsi
->next
;
145 { pwsi
->prev
->next
= pwsi
->next
;
146 if( pwsi
->next
) pwsi
->next
->prev
= pwsi
->prev
; }
149 static LPWSINFO
wsi_find(HTASK16 hTask
)
150 { LPWSINFO pwsi
= _wsi_list
;
151 while( pwsi
&& pwsi
->tid
!= hTask
) pwsi
= pwsi
->next
;
155 static ws_socket
* wsi_alloc_socket(LPWSINFO pwsi
, int fd
)
157 if( pwsi
->last_free
>= 0 )
159 int i
= pwsi
->last_free
;
161 pwsi
->last_free
= pwsi
->sock
[i
].flags
;
162 pwsi
->sock
[i
].fd
= fd
;
163 pwsi
->sock
[i
].flags
= 0;
164 return &pwsi
->sock
[i
];
169 static void fd_set_normalize(fd_set
* fds
, LPWSINFO pwsi
, ws_fd_set
* ws
, int* highfd
)
176 for(i
=0;i
<(ws
->fd_count
);i
++)
178 pws
= (ws_socket
*)WS_HANDLE2PTR(ws
->fd_array
[i
]);
179 if( _check_ws(pwsi
, pws
) )
181 if( pws
->fd
> *highfd
) *highfd
= pws
->fd
;
182 FD_SET(pws
->fd
, fds
);
189 * Note weirdness here: sockets with errors belong in exceptfds, but
190 * are given to us in readfds or writefds, so move them to exceptfds if
191 * there is an error. Note that this means that exceptfds may have mysterious
192 * sockets set in it that the program never asked for.
195 __inline__
static int sock_error_p(int s
)
197 unsigned int optval
, optlen
;
199 optlen
= sizeof(optval
);
200 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, &optval
, &optlen
);
201 if (optval
) dprintf_winsock(stddeb
, "error: %d\n", optval
);
205 static void fd_set_update(LPWSINFO pwsi
, fd_set
* fds
, ws_fd_set
* ws
,
210 int i
, j
, count
= ws
->fd_count
;
212 for( i
= 0, j
= 0; i
< count
; i
++ )
214 ws_socket
*pws
= (ws_socket
*)WS_HANDLE2PTR(ws
->fd_array
[i
]);
217 if( _check_ws(pwsi
, pws
) && FD_ISSET(fd
, fds
) )
219 /* if error, move to errorfds */
220 if (errorfds
&& (FD_ISSET(fd
, errorfds
) || sock_error_p(fd
)))
221 FD_SET(fd
, errorfds
);
223 ws
->fd_array
[j
++] = ws
->fd_array
[i
];
227 dprintf_winsock(stddeb
, "\n");
232 static void fd_set_update_except(LPWSINFO pwsi
, fd_set
*fds
, ws_fd_set
*ws
,
237 int i
, j
, count
= ws
->fd_count
;
239 for (i
=j
=0; i
< count
; i
++)
241 ws_socket
*pws
= (ws_socket
*)WS_HANDLE2PTR(ws
->fd_array
[i
]);
243 if (_check_ws(pwsi
, pws
) && (FD_ISSET(pws
->fd
, fds
)
244 || FD_ISSET(pws
->fd
, errorfds
)))
245 ws
->fd_array
[j
++] = ws
->fd_array
[i
];
252 /* ----------------------------------- API -----
254 * Init / cleanup / error checking.
257 INT16
WSAStartup(UINT16 wVersionRequested
, LPWSADATA lpWSAData
)
259 WSADATA WINSOCK_data
= { 0x0101, 0x0101,
263 #elif defined(__NetBSD__)
267 #elif defined(__FreeBSD__)
269 #elif defined(__OpenBSD__)
274 WS_MAX_SOCKETS_PER_THREAD
,
275 WS_MAX_UDP_DATAGRAM
, NULL
};
276 HTASK16 tid
= GetCurrentTask();
279 dprintf_winsock(stddeb
, "WSAStartup: verReq=%x\n", wVersionRequested
);
281 if (LOBYTE(wVersionRequested
) < 1 || (LOBYTE(wVersionRequested
) == 1 &&
282 HIBYTE(wVersionRequested
) < 1)) return WSAVERNOTSUPPORTED
;
284 if (!lpWSAData
) return WSAEINVAL
;
287 if( _WSHeap
== 0 ) return WSASYSNOTREADY
;
289 pwsi
= wsi_find(GetCurrentTask());
292 if( (pwsi
= (LPWSINFO
)WS_ALLOC( sizeof(WSINFO
))) )
296 for( i
= 0; i
< WS_MAX_SOCKETS_PER_THREAD
; i
++ )
298 pwsi
->sock
[i
].fd
= -1;
299 pwsi
->sock
[i
].flags
= i
+ 1;
301 pwsi
->sock
[WS_MAX_SOCKETS_PER_THREAD
- 1].flags
= -1;
303 else return WSASYSNOTREADY
;
305 } else pwsi
->num_startup
++;
307 /* return winsock information */
308 memcpy(lpWSAData
, &WINSOCK_data
, sizeof(WINSOCK_data
));
310 dprintf_winsock(stddeb
, "WSAStartup: succeeded\n");
314 void WINSOCK_Shutdown()
316 if( async_qid
!= -1 )
317 if( msgctl(async_qid
, IPC_RMID
, NULL
) == -1 )
318 fprintf(stderr
,"failed to delete WS message queue.\n");
322 INT16
WSACleanup(void)
324 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
326 /* FIXME: do global cleanup if no current task */
328 dprintf_winsock(stddeb
, "WSACleanup(%08x)\n", (unsigned)pwsi
);
333 if( pwsi
->num_startup
-- ) return 0;
335 SIGNAL_MaskAsyncEvents( TRUE
);
336 WINSOCK_cancel_async_op(GetCurrentTask());
337 SIGNAL_MaskAsyncEvents( FALSE
);
340 if( _wsi_list
== NULL
) WINSOCK_Shutdown();
342 if( pwsi
->flags
& WSI_BLOCKINGCALL
)
343 dprintf_winsock(stddeb
,"\tinside blocking call!\n");
344 if( pwsi
->num_async_rq
)
345 dprintf_winsock(stddeb
,"\thave %i outstanding async ops!\n", pwsi
->num_async_rq
);
347 for(i
= 0, j
= 0, n
= 0; i
< WS_MAX_SOCKETS_PER_THREAD
; i
++)
348 if( pwsi
->sock
[i
].fd
!= -1 )
350 n
+= cancel_async_select(&pwsi
->sock
[i
]);
351 close(pwsi
->sock
[i
].fd
); j
++;
354 dprintf_winsock(stddeb
,"\tclosed %i sockets, killed %i async selects!\n", j
, n
);
356 if( pwsi
->buffer
) SEGPTR_FREE(pwsi
->buffer
);
357 if( pwsi
->dbuffer
) SEGPTR_FREE(pwsi
->dbuffer
);
364 INT16
WSAGetLastError(void)
366 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
369 dprintf_winsock(stddeb
, "WSAGetLastError(%08x)", (unsigned)pwsi
);
371 ret
= (pwsi
) ? pwsi
->err
: WSANOTINITIALISED
;
373 dprintf_winsock(stddeb
, " = %i\n", (int)ret
);
377 void WSASetLastError(INT16 iError
)
379 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
381 dprintf_winsock(stddeb
, "WSASetLastError(%08x): %d\n", (unsigned)pwsi
, (int)iError
);
383 if( pwsi
) pwsi
->err
= iError
;
386 int _check_ws(LPWSINFO pwsi
, ws_socket
* pws
)
389 if( pwsi
->flags
& WSI_BLOCKINGCALL
) pwsi
->err
= WSAEINPROGRESS
;
390 else if( WSI_CHECK_RANGE(pwsi
, pws
) ) return 1;
391 else pwsi
->err
= WSAENOTSOCK
;
395 int _check_buffer(LPWSINFO pwsi
, int size
)
397 if( pwsi
->buffer
&& pwsi
->buflen
>= size
) return 1;
398 else SEGPTR_FREE(pwsi
->buffer
);
399 pwsi
->buffer
= (char*)SEGPTR_ALLOC((pwsi
->buflen
= size
));
400 return (pwsi
->buffer
!= NULL
);
403 /* ----- socket operations */
405 SOCKET16
WINSOCK_accept(SOCKET16 s
, struct sockaddr
*addr
, INT16
*addrlen16
)
407 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
408 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
410 dprintf_winsock(stddeb
, "WS_ACCEPT(%08x): socket %04x\n",
411 (unsigned)pwsi
, (UINT16
)s
);
412 if( _check_ws(pwsi
, pws
) )
414 int sock
, fd_flags
, addrlen32
= *addrlen16
;
416 /* this is how block info is supposed to be used -
417 * WSAIsBlocking() would then check WSI_BLOCKINGCALL bit.
420 fd_flags
= fcntl(pws
->fd
, F_GETFL
, 0);
421 if( !(fd_flags
& O_NONBLOCK
) ) pwsi
->flags
|= WSI_BLOCKINGCALL
;
423 if( (sock
= accept(pws
->fd
, addr
, &addrlen32
)) >= 0 )
425 ws_socket
* pnew
= wsi_alloc_socket(pwsi
, sock
);
426 notify_client(pws
, WS_FD_ACCEPT
);
430 init_async_select(pnew
, pws
->p_aop
->hWnd
,
432 pws
->p_aop
->flags
& ~WS_FD_ACCEPT
);
434 pwsi
->flags
&= ~WSI_BLOCKINGCALL
;
435 return (SOCKET16
)WS_PTR2HANDLE(pnew
);
437 else pwsi
->err
= WSAENOBUFS
;
439 else pwsi
->err
= wsaErrno();
441 pwsi
->flags
&= ~WSI_BLOCKINGCALL
;
443 return INVALID_SOCKET
;
446 INT16
WINSOCK_bind(SOCKET16 s
, struct sockaddr
*name
, INT16 namelen
)
448 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
449 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
451 dprintf_winsock(stddeb
, "WS_BIND(%08x): socket %04x, ptr %8x, length %d\n",
452 (unsigned)pwsi
, s
, (int) name
, namelen
);
457 if ( _check_ws(pwsi
, pws
) )
458 if (namelen
>= sizeof(*name
))
459 if ( ((struct sockaddr_in
*)name
)->sin_family
== AF_INET
)
460 if ( bind(pws
->fd
, name
, namelen
) < 0 )
462 int loc_errno
= errno
;
463 dprintf_winsock(stddeb
,"\tfailure - errno = %i\n", errno
);
467 case EBADF
: pwsi
->err
= WSAENOTSOCK
; break;
468 case EADDRNOTAVAIL
: pwsi
->err
= WSAEINVAL
; break;
469 default: pwsi
->err
= wsaErrno();
473 else pwsi
->err
= WSAEAFNOSUPPORT
;
474 else pwsi
->err
= WSAEFAULT
;
478 INT16
WINSOCK_closesocket(SOCKET16 s
)
480 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
481 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
483 dprintf_winsock(stddeb
, "WS_CLOSE(%08x): socket %08x\n", (unsigned)pwsi
, s
);
485 if( _check_ws(pwsi
, pws
) )
489 cancel_async_select(pws
);
491 pws
->flags
= (unsigned)pwsi
->last_free
;
492 pwsi
->last_free
= pws
- &pwsi
->sock
[0];
493 if (close(fd
) == 0) return 0;
494 pwsi
->err
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
499 INT16
WINSOCK_connect(SOCKET16 s
, struct sockaddr
*name
, INT16 namelen
)
501 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
502 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
504 dprintf_winsock(stddeb
, "WS_CONNECT(%08x): socket %04x, ptr %8x, length %d\n",
505 (unsigned)pwsi
, s
, (int) name
, namelen
);
510 if( _check_ws(pwsi
, pws
) )
512 if (connect(pws
->fd
, name
, namelen
) == 0)
515 /* we need to notify handler process if
516 * connect() succeeded NOT in response to winsock message
518 notify_client(pws
, WS_FD_CONNECTED
);
520 pws
->flags
&= ~(WS_FD_INACTIVE
| WS_FD_CONNECT
);
523 pwsi
->err
= (errno
== EINPROGRESS
) ? WSAEWOULDBLOCK
: wsaErrno();
528 INT16
WINSOCK_getpeername(SOCKET16 s
, struct sockaddr
*name
, INT16
*namelen
)
530 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
531 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
533 dprintf_winsock(stddeb
, "WS_GETPEERNAME(%08x): socket: %04x, ptr %8x, ptr %8x\n",
534 (unsigned)pwsi
, s
, (int) name
, *namelen
);
535 if( _check_ws(pwsi
, pws
) )
537 int namelen32
= *namelen
;
538 if (getpeername(pws
->fd
, name
, &namelen32
) == 0)
543 *namelen
= (INT16
)namelen32
;
546 pwsi
->err
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
551 INT16
WINSOCK_getsockname(SOCKET16 s
, struct sockaddr
*name
, INT16
*namelen
)
553 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
554 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
556 dprintf_winsock(stddeb
, "WS_GETSOCKNAME(%08x): socket: %04x, ptr %8x, ptr %8x\n",
557 (unsigned)pwsi
, s
, (int) name
, (int) *namelen
);
558 if( _check_ws(pwsi
, pws
) )
560 int namelen32
= *namelen
;
561 if (getsockname(pws
->fd
, name
, &namelen32
) == 0)
563 *namelen
= (INT16
)namelen32
;
566 pwsi
->err
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
571 INT16
WINSOCK_getsockopt(SOCKET16 s
, INT16 level
,
572 INT16 optname
, char *optval
, INT16
*optlen
)
574 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
575 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
577 dprintf_winsock(stddeb
, "WS_GETSOCKOPT(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
578 (unsigned)pwsi
, s
, level
, (int) optval
, (int) *optlen
);
580 if( _check_ws(pwsi
, pws
) )
582 int optlen32
= *optlen
;
584 convert_sockopt(&level
, &optname
);
585 if (getsockopt(pws
->fd
, (int) level
, optname
, optval
, &optlen32
) == 0 )
586 { *optlen
= (INT16
)optlen32
; return 0; }
587 pwsi
->err
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
592 u_long
WINSOCK_htonl(u_long hostlong
) { return( htonl(hostlong
) ); }
593 u_short
WINSOCK_htons(u_short hostshort
) { return( htons(hostshort
) ); }
594 u_long
WINSOCK_inet_addr(char *cp
) { return( inet_addr(cp
) ); }
595 u_long
WINSOCK_ntohl(u_long netlong
) { return( ntohl(netlong
) ); }
596 u_short
WINSOCK_ntohs(u_short netshort
) { return( ntohs(netshort
) ); }
598 SEGPTR
WINSOCK_inet_ntoa(struct in_addr in
)
600 /* use "buffer for dummies" here because some applications have
601 * propensity to decode addresses in ws_hostent structure without
602 * saving them first...
605 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
609 char* s
= inet_ntoa(in
);
612 if( pwsi
->dbuffer
== NULL
)
613 if((pwsi
->dbuffer
= (char*) SEGPTR_ALLOC(32)) == NULL
)
615 pwsi
->err
= WSAENOBUFS
;
618 strncpy(pwsi
->dbuffer
, s
, 32 );
619 return SEGPTR_GET(pwsi
->dbuffer
);
621 pwsi
->err
= wsaErrno();
626 INT16
WINSOCK_ioctlsocket(SOCKET16 s
, UINT32 cmd
, UINT32
*argp
)
628 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
629 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
631 dprintf_winsock(stddeb
, "WS_IOCTL(%08x): socket %04x, cmd %08x, ptr %8x\n",
632 (unsigned)pwsi
, s
, cmd
, (unsigned) argp
);
633 if( _check_ws(pwsi
, pws
) )
645 if( pws
->p_aop
&& *argp
== 0 )
647 pwsi
->err
= WSAEINVAL
;
656 case WS_IOW('f',125,u_long
):
657 fprintf(stderr
,"Warning: WS1.1 shouldn't be using async I/O\n");
658 pwsi
->err
= WSAEINVAL
;
662 /* Netscape tries hard to use bogus ioctl 0x667e */
663 dprintf_winsock(stddeb
,"\tunknown WS_IOCTL cmd (%08x)\n", cmd
);
665 if( ioctl(pws
->fd
, newcmd
, (char*)argp
) == 0 ) return 0;
666 pwsi
->err
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
671 INT16
WINSOCK_listen(SOCKET16 s
, INT16 backlog
)
673 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
674 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
676 dprintf_winsock(stddeb
, "WS_LISTEN(%08x): socket %04x, backlog %d\n",
677 (unsigned)pwsi
, s
, backlog
);
678 if( _check_ws(pwsi
, pws
) )
682 int fd_flags
= fcntl(pws
->fd
, F_GETFL
, 0);
683 if( !(fd_flags
& O_NONBLOCK
) ) pws
->flags
|= WS_FD_ACCEPT
;
685 else notify_client(pws
, WS_FD_ACCEPT
);
687 if (listen(pws
->fd
, backlog
) == 0) return 0;
688 pwsi
->err
= wsaErrno();
693 INT16
WINSOCK_recv(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
)
695 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
696 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
698 dprintf_winsock(stddeb
, "WS_RECV(%08x): socket %04x, buf %8x, len %d, flags %d",
699 (unsigned)pwsi
, s
, (unsigned)buf
, len
, flags
);
700 if( _check_ws(pwsi
, pws
) )
703 if ((length
= recv(pws
->fd
, buf
, len
, flags
)) >= 0)
705 dprintf_winsock(stddeb
, " -> %i bytes\n", length
);
706 notify_client(pws
, WS_FD_READ
);
707 return (INT16
)length
;
709 pwsi
->err
= wsaErrno();
711 dprintf_winsock(stddeb
, " -> ERROR\n");
715 INT16
WINSOCK_recvfrom(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
,
716 struct sockaddr
*from
, INT16
*fromlen16
)
718 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
719 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
721 dprintf_winsock(stddeb
, "WS_RECVFROM(%08x): socket %04x, ptr %08x, len %d, flags %d\n",
722 (unsigned)pwsi
, s
, (unsigned)buf
, len
, flags
);
723 if( _check_ws(pwsi
, pws
) )
725 int length
, fromlen32
= *fromlen16
;
727 if ((length
= recvfrom(pws
->fd
, buf
, len
, flags
, from
, &fromlen32
)) >= 0)
729 *fromlen16
= fromlen32
;
730 notify_client(pws
, WS_FD_READ
);
731 return (INT16
)length
;
733 pwsi
->err
= wsaErrno();
738 INT16
WINSOCK_select(INT16 nfds
, ws_fd_set
*ws_readfds
,
739 ws_fd_set
*ws_writefds
,
740 ws_fd_set
*ws_exceptfds
, struct timeval
*timeout
)
742 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
744 dprintf_winsock(stddeb
, "WS_SELECT(%08x): nfds %d (ignored), read %8x, write %8x, excp %8x\n",
745 (unsigned) pwsi
, nfds
, (unsigned) ws_readfds
, (unsigned) ws_writefds
, (unsigned) ws_exceptfds
);
750 fd_set readfds
, writefds
, exceptfds
, errorfds
;
752 fd_set_normalize(&readfds
, pwsi
, ws_readfds
, &highfd
);
753 fd_set_normalize(&writefds
, pwsi
, ws_writefds
, &highfd
);
754 fd_set_normalize(&exceptfds
, pwsi
, ws_exceptfds
, &highfd
);
757 if( (highfd
= select(highfd
+ 1, &readfds
, &writefds
, &exceptfds
, timeout
)) >= 0 )
761 fd_set_update(pwsi
, &readfds
, ws_readfds
, &errorfds
);
762 fd_set_update(pwsi
, &writefds
, ws_writefds
, &errorfds
);
763 fd_set_update_except(pwsi
, &exceptfds
, ws_exceptfds
, &errorfds
);
767 pwsi
->err
= wsaErrno();
772 INT16
WINSOCK_send(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
)
774 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
775 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
777 dprintf_winsock(stddeb
, "WS_SEND(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
778 (unsigned)pwsi
, s
, (unsigned) buf
, len
, flags
);
779 if( _check_ws(pwsi
, pws
) )
782 if ((length
= send(pws
->fd
, buf
, len
, flags
)) < 0 )
784 length
= SOCKET_ERROR
;
785 pwsi
->err
= wsaErrno();
787 notify_client(pws
, WS_FD_WRITE
);
788 return (INT16
)length
;
793 INT16
WINSOCK_sendto(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
,
794 struct sockaddr
*to
, INT16 tolen
)
796 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
797 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
799 dprintf_winsock(stddeb
, "WS_SENDTO(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
800 (unsigned)pwsi
, s
, (unsigned) buf
, len
, flags
);
801 if( _check_ws(pwsi
, pws
) )
805 if ((length
= sendto(pws
->fd
, buf
, len
, flags
, to
, tolen
)) < 0 )
807 length
= SOCKET_ERROR
;
808 pwsi
->err
= wsaErrno();
810 notify_client(pws
, WS_FD_WRITE
);
811 return (INT16
)length
;
816 INT16
WINSOCK_setsockopt(SOCKET16 s
, INT16 level
, INT16 optname
,
817 char *optval
, INT16 optlen
)
819 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
820 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
822 dprintf_winsock(stddeb
, "WS_SETSOCKOPT(%08x): socket %04x, level %d, opt %d, ptr %08x, len %d\n",
823 (unsigned)pwsi
, s
, level
, optname
, (int) optval
, optlen
);
824 if( _check_ws(pwsi
, pws
) )
827 convert_sockopt(&level
, &optname
);
828 if( optname
== SO_LINGER
)
830 INT16
* ptr
= (INT16
*)optval
;
831 linger32
[0] = ptr
[0];
832 linger32
[1] = ptr
[1];
833 optval
= (char*)&linger32
;
834 optlen
= sizeof(linger32
);
836 if (setsockopt(pws
->fd
, level
, optname
, optval
, optlen
) == 0) return 0;
837 pwsi
->err
= wsaErrno();
842 INT16
WINSOCK_shutdown(SOCKET16 s
, INT16 how
)
844 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
845 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
847 dprintf_winsock(stddeb
, "WS_SHUTDOWN(%08x): socket %04x, how %i\n",
848 (unsigned)pwsi
, s
, how
);
849 if( _check_ws(pwsi
, pws
) )
851 pws
->flags
|= WS_FD_INACTIVE
;
852 cancel_async_select(pws
);
854 if (shutdown(pws
->fd
, how
) == 0) return 0;
855 pwsi
->err
= wsaErrno();
860 SOCKET16
WINSOCK_socket(INT16 af
, INT16 type
, INT16 protocol
)
862 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
864 dprintf_winsock(stddeb
, "WS_SOCKET(%08x): af=%d type=%d protocol=%d\n",
865 (unsigned)pwsi
, af
, type
, protocol
);
871 /* check the socket family */
875 case AF_UNSPEC
: break;
876 default: pwsi
->err
= WSAEAFNOSUPPORT
; return INVALID_SOCKET
;
879 /* check the socket type */
884 case SOCK_RAW
: break;
885 default: pwsi
->err
= WSAESOCKTNOSUPPORT
; return INVALID_SOCKET
;
888 /* check the protocol type */
889 if ( protocol
< 0 ) /* don't support negative values */
890 { pwsi
->err
= WSAEPROTONOSUPPORT
; return INVALID_SOCKET
; }
892 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
896 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
898 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
899 default: pwsi
->err
= WSAEPROTOTYPE
; return INVALID_SOCKET
;
902 if ((sock
= socket(af
, type
, protocol
)) >= 0)
904 ws_socket
* pnew
= wsi_alloc_socket(pwsi
, sock
);
906 dprintf_winsock(stddeb
,"\tcreated %04x (handle %i)\n", sock
, (UINT16
)WS_PTR2HANDLE(pnew
));
908 if( pnew
) return (SOCKET16
)WS_PTR2HANDLE(pnew
);
911 pwsi
->err
= WSAENOBUFS
;
912 return INVALID_SOCKET
;
916 if (errno
== EPERM
) /* raw socket denied */
918 fprintf(stderr
, "WS_SOCKET: not enough privileges\n");
919 pwsi
->err
= WSAESOCKTNOSUPPORT
;
920 } else pwsi
->err
= wsaErrno();
923 dprintf_winsock(stddeb
, "\t\tfailed!\n");
924 return INVALID_SOCKET
;
928 /* ----- database functions
930 * Note that ws_...ent structures we return have SEGPTR pointers inside them.
933 static char* NULL_STRING
= "NULL";
938 SEGPTR
WINSOCK_gethostbyaddr(const char *addr
, INT16 len
, INT16 type
)
940 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
942 dprintf_winsock(stddeb
, "WS_GetHostByAddr(%08x): ptr %8x, len %d, type %d\n",
943 (unsigned)pwsi
, (unsigned) addr
, len
, type
);
946 struct hostent
* host
;
947 if( (host
= gethostbyaddr(addr
, len
, type
)) != NULL
)
948 if( WS_dup_he(pwsi
, host
, WS_DUP_SEGPTR
) )
949 return SEGPTR_GET(pwsi
->buffer
);
950 else pwsi
->err
= WSAENOBUFS
;
951 else pwsi
->err
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
959 SEGPTR
WINSOCK_gethostbyname(const char *name
)
961 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
963 dprintf_winsock(stddeb
, "WS_GetHostByName(%08x): %s\n",
964 (unsigned)pwsi
, (name
)?name
:"NULL");
967 struct hostent
* host
;
968 if( (host
= gethostbyname(name
)) != NULL
)
969 if( WS_dup_he(pwsi
, host
, WS_DUP_SEGPTR
) )
970 return SEGPTR_GET(pwsi
->buffer
);
971 else pwsi
->err
= WSAENOBUFS
;
972 else pwsi
->err
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
977 INT16
WINSOCK_gethostname(char *name
, INT16 namelen
)
979 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
981 dprintf_winsock(stddeb
, "WS_GetHostName(%08x): name %s, len %d\n",
982 (unsigned)pwsi
, (name
)?name
:NULL_STRING
, namelen
);
985 if (gethostname(name
, namelen
) == 0) return 0;
986 pwsi
->err
= (errno
== EINVAL
) ? WSAEFAULT
: wsaErrno();
992 struct WIN_protoent *
994 SEGPTR
WINSOCK_getprotobyname(char *name
)
996 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
998 dprintf_winsock(stddeb
, "WS_GetProtoByName(%08x): %s\n",
999 (unsigned)pwsi
, (name
)?name
:NULL_STRING
);
1002 struct protoent
* proto
;
1003 if( (proto
= getprotobyname(name
)) != NULL
)
1004 if( WS_dup_pe(pwsi
, proto
, WS_DUP_SEGPTR
) )
1005 return SEGPTR_GET(pwsi
->buffer
);
1006 else pwsi
->err
= WSAENOBUFS
;
1007 else pwsi
->err
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
1013 struct WIN_protoent *
1015 SEGPTR
WINSOCK_getprotobynumber(INT16 number
)
1017 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1019 dprintf_winsock(stddeb
, "WS_GetProtoByNumber(%08x): %i\n", (unsigned)pwsi
, number
);
1023 struct protoent
* proto
;
1024 if( (proto
= getprotobynumber(number
)) != NULL
)
1025 if( WS_dup_pe(pwsi
, proto
, WS_DUP_SEGPTR
) )
1026 return SEGPTR_GET(pwsi
->buffer
);
1027 else pwsi
->err
= WSAENOBUFS
;
1028 else pwsi
->err
= WSANO_DATA
;
1034 struct WIN_servent *
1036 SEGPTR
WINSOCK_getservbyname(const char *name
, const char *proto
)
1038 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1040 dprintf_winsock(stddeb
, "WS_GetServByName(%08x): '%s', '%s'\n",
1041 (unsigned)pwsi
, (name
)?name
:NULL_STRING
, (proto
)?proto
:NULL_STRING
);
1045 struct servent
* serv
;
1046 if( (serv
= getservbyname(name
, proto
)) != NULL
)
1047 if( WS_dup_se(pwsi
, serv
, WS_DUP_SEGPTR
) )
1048 return SEGPTR_GET(pwsi
->buffer
);
1049 else pwsi
->err
= WSAENOBUFS
;
1050 else pwsi
->err
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
1056 struct WIN_servent *
1058 SEGPTR
WINSOCK_getservbyport(INT16 port
, const char *proto
)
1060 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1062 dprintf_winsock(stddeb
, "WS_GetServByPort(%08x): %i, '%s'\n",
1063 (unsigned)pwsi
, (int)port
, (proto
)?proto
:NULL_STRING
);
1066 struct servent
* serv
;
1067 if( (serv
= getservbyport(port
, proto
)) != NULL
)
1068 if( WS_dup_se(pwsi
, serv
, WS_DUP_SEGPTR
) )
1069 return SEGPTR_GET(pwsi
->buffer
);
1070 else pwsi
->err
= WSAENOBUFS
;
1071 else pwsi
->err
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
1077 /* ----------------------------------- Windows sockets extensions -- *
1079 * ----------------------------------------------------------------- */
1081 static int aop_control(ws_async_op
* p_aop
, int flag
)
1085 read(p_aop
->fd
[0], &lLength
, sizeof(unsigned));
1086 if( LOWORD(lLength
) )
1087 if( (int)LOWORD(lLength
) <= p_aop
->buflen
)
1089 char* buffer
= (char*)PTR_SEG_TO_LIN(p_aop
->buffer_base
);
1090 read(p_aop
->fd
[0], buffer
, LOWORD(lLength
));
1091 switch( p_aop
->flags
)
1093 case WSMSG_ASYNC_HOSTBYNAME
:
1094 case WSMSG_ASYNC_HOSTBYADDR
:
1095 fixup_wshe((struct ws_hostent
*)buffer
, p_aop
->buffer_base
); break;
1096 case WSMSG_ASYNC_PROTOBYNAME
:
1097 case WSMSG_ASYNC_PROTOBYNUM
:
1098 fixup_wspe((struct ws_protoent
*)buffer
, p_aop
->buffer_base
); break;
1099 case WSMSG_ASYNC_SERVBYNAME
:
1100 case WSMSG_ASYNC_SERVBYPORT
:
1101 fixup_wsse((struct ws_servent
*)buffer
, p_aop
->buffer_base
); break;
1103 if( p_aop
->flags
) fprintf(stderr
,"Received unknown async request!\n");
1104 return AOP_CONTROL_REMOVE
;
1107 else lLength
= ((UINT32
)LOWORD(lLength
)) | ((unsigned)WSAENOBUFS
<< 16);
1110 printf("async op completed: hWnd [%04x], uMsg [%04x], aop [%04x], event [%08x]\n",
1111 p_aop
->hWnd
, p_aop
->uMsg
, (HANDLE16
)WS_PTR2HANDLE(p_aop
), (LPARAM
)lLength
);
1114 PostMessage16(p_aop
->hWnd
, p_aop
->uMsg
, (HANDLE16
)WS_PTR2HANDLE(p_aop
), (LPARAM
)lLength
);
1115 return AOP_CONTROL_REMOVE
;
1119 static HANDLE16
__WSAsyncDBQuery(LPWSINFO pwsi
, HWND16 hWnd
, UINT16 uMsg
, LPCSTR init
,
1120 INT16 len
, INT16 type
, SEGPTR sbuf
, INT16 buflen
, UINT32 flag
)
1122 /* queue 'flag' request and fork off its handler */
1124 async_ctl
.ws_aop
= (ws_async_op
*)WS_ALLOC(sizeof(ws_async_op
));
1126 if( async_ctl
.ws_aop
)
1128 HANDLE16 handle
= (HANDLE16
)WS_PTR2HANDLE(async_ctl
.ws_aop
);
1130 if( pipe(async_ctl
.ws_aop
->fd
) == 0 )
1132 async_ctl
.init
= (char*)init
;
1133 async_ctl
.lLength
= len
;
1134 async_ctl
.lEvent
= type
;
1136 async_ctl
.ws_aop
->hWnd
= hWnd
;
1137 async_ctl
.ws_aop
->uMsg
= uMsg
;
1139 async_ctl
.ws_aop
->buffer_base
= sbuf
; async_ctl
.ws_aop
->buflen
= buflen
;
1140 async_ctl
.ws_aop
->flags
= flag
;
1141 async_ctl
.ws_aop
->aop_control
= &aop_control
;
1142 WINSOCK_link_async_op( async_ctl
.ws_aop
);
1144 async_ctl
.ws_aop
->pid
= fork();
1145 if( async_ctl
.ws_aop
->pid
)
1147 close(async_ctl
.ws_aop
->fd
[1]); /* write endpoint */
1149 /* Damn, BSD'ish SIGIO doesn't work on pipes/streams
1151 * async_io(async_ctl.ws_aop->fd[0], 1);
1154 dprintf_winsock(stddeb
, "\tasync_op = %04x (child %i)\n",
1155 handle
, async_ctl
.ws_aop
->pid
);
1160 close(async_ctl
.ws_aop
->fd
[0]); /* read endpoint */
1163 case WSMSG_ASYNC_HOSTBYADDR
:
1164 case WSMSG_ASYNC_HOSTBYNAME
:
1165 WS_do_async_gethost(pwsi
,flag
);
1166 case WSMSG_ASYNC_PROTOBYNUM
:
1167 case WSMSG_ASYNC_PROTOBYNAME
:
1168 WS_do_async_getproto(pwsi
,flag
);
1169 case WSMSG_ASYNC_SERVBYPORT
:
1170 case WSMSG_ASYNC_SERVBYNAME
:
1171 WS_do_async_getserv(pwsi
,flag
);
1173 _exit(0); /* skip atexit()'ed cleanup */
1176 WS_FREE(async_ctl
.ws_aop
);
1177 pwsi
->err
= wsaErrno();
1178 } else pwsi
->err
= WSAEWOULDBLOCK
;
1182 HANDLE16
WSAAsyncGetHostByAddr(HWND16 hWnd
, UINT16 uMsg
, LPCSTR addr
,
1183 INT16 len
, INT16 type
, SEGPTR sbuf
, INT16 buflen
)
1185 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1187 dprintf_winsock(stddeb
, "WS_AsyncGetHostByAddr(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1188 (unsigned)pwsi
, hWnd
, uMsg
, (unsigned)addr
, len
);
1191 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, addr
, len
,
1192 type
, sbuf
, buflen
, WSMSG_ASYNC_HOSTBYADDR
);
1197 HANDLE16
WSAAsyncGetHostByName(HWND16 hWnd
, UINT16 uMsg
, LPCSTR name
,
1198 SEGPTR sbuf
, INT16 buflen
)
1200 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1202 dprintf_winsock(stddeb
, "WS_AsyncGetHostByName(%08x): hwnd %04x, msg %04x, host %s, buffer %i\n",
1203 (unsigned)pwsi
, hWnd
, uMsg
, (name
)?name
:NULL_STRING
, (int)buflen
);
1206 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, name
, 0,
1207 0, sbuf
, buflen
, WSMSG_ASYNC_HOSTBYNAME
);
1212 HANDLE16
WSAAsyncGetProtoByName(HWND16 hWnd
, UINT16 uMsg
, LPCSTR name
,
1213 SEGPTR sbuf
, INT16 buflen
)
1215 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1217 dprintf_winsock(stddeb
, "WS_AsyncGetProtoByName(%08x): hwnd %04x, msg %04x, protocol %s\n",
1218 (unsigned)pwsi
, hWnd
, uMsg
, (name
)?name
:NULL_STRING
);
1221 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, name
, 0,
1222 0, sbuf
, buflen
, WSMSG_ASYNC_PROTOBYNAME
);
1227 HANDLE16
WSAAsyncGetProtoByNumber(HWND16 hWnd
, UINT16 uMsg
, INT16 number
,
1228 SEGPTR sbuf
, INT16 buflen
)
1230 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1232 dprintf_winsock(stddeb
, "WS_AsyncGetProtoByNumber(%08x): hwnd %04x, msg %04x, num %i\n",
1233 (unsigned)pwsi
, hWnd
, uMsg
, number
);
1236 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, NULL
, 0,
1237 number
, sbuf
, buflen
, WSMSG_ASYNC_PROTOBYNUM
);
1242 HANDLE16
WSAAsyncGetServByName(HWND16 hWnd
, UINT16 uMsg
, LPCSTR name
,
1243 LPCSTR proto
, SEGPTR sbuf
, INT16 buflen
)
1245 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1247 dprintf_winsock(stddeb
, "WS_AsyncGetServByName(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
1248 (unsigned)pwsi
, hWnd
, uMsg
, (name
)?name
:NULL_STRING
, (proto
)?proto
:NULL_STRING
);
1252 async_ctl
.buffer
= (char*)proto
;
1253 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, name
, 0,
1254 0, sbuf
, buflen
, WSMSG_ASYNC_SERVBYNAME
);
1260 HANDLE16
WSAAsyncGetServByPort(HWND16 hWnd
, UINT16 uMsg
, INT16 port
,
1261 LPCSTR proto
, SEGPTR sbuf
, INT16 buflen
)
1263 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1265 dprintf_winsock(stddeb
, "WS_AsyncGetServByPort(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
1266 (unsigned)pwsi
, hWnd
, uMsg
, port
, (proto
)?proto
:NULL_STRING
);
1269 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, proto
, 0,
1270 port
, sbuf
, buflen
, WSMSG_ASYNC_SERVBYPORT
);
1274 INT16
WSACancelAsyncRequest(HANDLE16 hAsyncTaskHandle
)
1276 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1277 ws_async_op
* p_aop
= (ws_async_op
*)WS_HANDLE2PTR(hAsyncTaskHandle
);
1279 dprintf_winsock(stddeb
, "WS_CancelAsyncRequest(%08x): handle %04x\n",
1280 (unsigned)pwsi
, hAsyncTaskHandle
);
1282 if( WINSOCK_check_async_op(p_aop
) )
1284 kill(p_aop
->pid
, SIGKILL
);
1285 waitpid(p_aop
->pid
, NULL
, 0); /* just in case */
1286 close(p_aop
->fd
[0]);
1287 WINSOCK_unlink_async_op(p_aop
);
1291 else pwsi
->err
= WSAEINVAL
;
1292 return SOCKET_ERROR
;
1295 /* ----- asynchronous select() */
1297 int cancel_async_select(ws_socket
* pws
)
1301 kill(pws
->p_aop
->pid
, SIGKILL
);
1302 waitpid(pws
->p_aop
->pid
, NULL
, 0);
1303 WS_FREE(pws
->p_aop
);
1310 void _sigusr1_handler_parent(int sig
)
1312 /* child process puts MTYPE_CLIENT data packet into the
1313 * 'async_qid' message queue and signals us with SIGUSR1.
1314 * This handler reads the queue and posts 'uMsg' notification
1320 signal( SIGUSR1
, _sigusr1_handler_parent
);
1321 while( msgrcv(async_qid
, (struct msgbuf
*)&ipack
,
1322 MTYPE_CLIENT_SIZE
, MTYPE_CLIENT
, IPC_NOWAIT
) != -1 )
1324 if( ipack
.wParam
&& abs((short)ipack
.wParam
) < 32768 )
1326 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(ipack
.wParam
);
1327 if( pws
->p_aop
&& abs((char*)_ws_stub
- (char*)pws
->p_aop
) < 32768 )
1329 pws
->flags
&= ~(ipack
.lParam
);
1331 printf("async event - hWnd %04x, uMsg %04x [%08x]\n",
1332 pws
->p_aop
->hWnd
, pws
->p_aop
->uMsg
, ipack
.lParam
);
1334 PostMessage16(pws
->p_aop
->hWnd
, pws
->p_aop
->uMsg
,
1335 (WPARAM16
)ipack
.wParam
, (LPARAM
)ipack
.lParam
);
1337 else fprintf(stderr
,"AsyncSelect:stray async_op in socket %04x!\n", ipack
.wParam
);
1339 else fprintf(stderr
,"AsyncSelect:stray socket at %04x!\n", ipack
.wParam
);
1343 int notify_client( ws_socket
* pws
, unsigned flag
)
1345 if( pws
->p_aop
&& ((pws
->p_aop
->flags
& flag
) ||
1346 (flag
== WS_FD_CONNECTED
&& pws
->flags
& WS_FD_CONNECT
)) )
1348 async_ctl
.ip
.mtype
= MTYPE_PARENT
;
1349 async_ctl
.ip
.lParam
= flag
;
1350 while( msgsnd(async_qid
, (struct msgbuf
*)&(async_ctl
.ip
),
1351 MTYPE_PARENT_SIZE
, 0) == -1 )
1353 if( errno
== EINTR
) continue;
1356 perror("AsyncSelect(parent)");
1357 cancel_async_select(pws
);
1358 pws
->flags
&= WS_FD_INTERNAL
;
1362 kill(pws
->p_aop
->pid
, SIGUSR1
);
1368 INT16
init_async_select(ws_socket
* pws
, HWND16 hWnd
, UINT16 uMsg
, UINT32 lEvent
)
1372 if( cancel_async_select(pws
) ) /* delete old async handler if any */
1373 pws
->flags
&= WS_FD_INTERNAL
;
1375 if( lEvent
== 0 ) return 0;
1377 /* setup async handler - some data may be redundant */
1379 WINSOCK_unblock_io(pws
->fd
, 1);
1380 if( (p_aop
= (ws_async_op
*)WS_ALLOC(sizeof(ws_async_op
))) )
1386 async_ctl
.lEvent
= p_aop
->flags
= lEvent
;
1387 async_ctl
.ws_sock
= pws
;
1388 async_ctl
.ip
.wParam
= (UINT16
)WS_PTR2HANDLE(pws
);
1389 async_ctl
.ip
.lParam
= 0;
1391 p_aop
->pid
= fork();
1392 if( p_aop
->pid
!= -1 )
1393 if( p_aop
->pid
== 0 ) WINSOCK_do_async_select(); /* child process */
1394 else pws
->flags
|= lEvent
;
1396 signal( SIGUSR1
, _sigusr1_handler_parent
);
1397 return 0; /* Wine process */
1399 return SOCKET_ERROR
;
1402 INT16
WSAAsyncSelect(SOCKET16 s
, HWND16 hWnd
, UINT16 uMsg
, UINT32 lEvent
)
1404 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
1405 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1407 dprintf_winsock(stddeb
, "WS_AsyncSelect(%08x): %04x, hWnd %04x, uMsg %04x, event %08x\n",
1408 (unsigned)pwsi
, s
, hWnd
, uMsg
, (unsigned)lEvent
);
1409 if( _check_ws(pwsi
, pws
) )
1410 if( init_async_select(pws
, hWnd
, uMsg
, lEvent
) == 0 ) return 0;
1411 else pwsi
->err
= WSAENOBUFS
;
1412 return SOCKET_ERROR
;
1415 /* ----- miscellaneous */
1417 INT16
__WSAFDIsSet(SOCKET16 fd
, ws_fd_set
*set
)
1419 int i
= set
->fd_count
;
1421 dprintf_winsock(stddeb
, "__WSAFDIsSet(%d,%8lx)\n",fd
,(unsigned long)set
);
1424 if (set
->fd_array
[i
] == fd
) return 1;
1428 BOOL16
WSAIsBlocking(void)
1430 /* By default WinSock should set all its sockets to non-blocking mode
1431 * and poll in PeekMessage loop when processing "blocking" ones. This
1432 * function * is supposed to tell if program is in this loop. Our
1433 * blocking calls are truly blocking so we always return FALSE.
1435 * Note: It is allowed to call this function without prior WSAStartup().
1438 dprintf_winsock(stddeb
, "WS_IsBlocking()\n");
1442 INT16
WSACancelBlockingCall(void)
1444 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1446 dprintf_winsock(stddeb
, "WS_CancelBlockingCall(%08x)\n", (unsigned)pwsi
);
1448 if( pwsi
) return 0;
1449 return SOCKET_ERROR
;
1452 FARPROC16
WSASetBlockingHook16(FARPROC16 lpBlockFunc
)
1455 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1457 dprintf_winsock(stddeb
, "WS_SetBlockingHook16(%08x): hook %08x\n",
1458 (unsigned)pwsi
, (unsigned) lpBlockFunc
);
1461 prev
= pwsi
->blocking_hook
;
1462 pwsi
->blocking_hook
= lpBlockFunc
;
1468 FARPROC32
WSASetBlockingHook32(FARPROC32 lpBlockFunc
)
1470 fprintf( stderr
, "WSASetBlockingHook32(%p): empty stub\n", lpBlockFunc
);
1474 INT16
WSAUnhookBlockingHook16(void)
1476 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1478 dprintf_winsock(stddeb
, "WS_UnhookBlockingHook(%08x)\n", (unsigned)pwsi
);
1479 if( pwsi
) return (INT16
)(INT32
)(pwsi
->blocking_hook
= (FARPROC16
)NULL
);
1480 return SOCKET_ERROR
;
1483 INT32
WSAUnhookBlockingHook32(void)
1485 fprintf( stderr
, "WSAUnhookBlockingHook32(): empty stub\n");
1490 WsControl(DWORD x1
,DWORD x2
,LPDWORD x3
,LPDWORD x4
,LPDWORD x5
,LPDWORD x6
)
1492 fprintf(stdnimp
,"WsControl(%lx,%lx,%p,%p,%p,%p)\n",
1495 fprintf(stdnimp
,"WsControl(x,x,%lx,%lx,%lx,%lx)\n",
1496 x3
?*x3
:0,x4
?*x4
:0,x5
?*x5
:0,x6
?*x6
:0
1500 /* ----------------------------------- end of API stuff */
1504 /* ----------------------------------- helper functions */
1506 static int list_size(char** l
, int item_size
)
1511 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
1512 j
+= (i
+ 1) * sizeof(char*); }
1516 static int list_dup(char** l_src
, char* ref
, char* base
, int item_size
)
1518 /* base is either either equal to ref or 0 or SEGPTR */
1521 char** l_to
= (char**)ref
;
1524 for(j
=0;l_src
[j
];j
++) ;
1525 p
+= (j
+ 1) * sizeof(char*);
1527 { l_to
[i
] = base
+ (p
- ref
);
1528 k
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
1529 memcpy(p
, l_src
[i
], k
); p
+= k
; }
1536 static int hostent_size(struct hostent
* p_he
)
1540 { size
= sizeof(struct hostent
);
1541 size
+= strlen(p_he
->h_name
) + 1;
1542 size
+= list_size(p_he
->h_aliases
, 0);
1543 size
+= list_size(p_he
->h_addr_list
, p_he
->h_length
); }
1547 int WS_dup_he(LPWSINFO pwsi
, struct hostent
* p_he
, int flag
)
1549 /* Duplicate hostent structure and flatten data (with its pointers)
1550 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
1551 * relative to 0 depending on "flag" value. Return data size (also
1552 * in the pwsi->buflen).
1555 int size
= hostent_size(p_he
);
1558 char* p_name
,*p_aliases
,*p_addr
,*p_base
,*p
;
1560 _check_buffer(pwsi
, size
);
1562 p_base
= (flag
& WS_DUP_OFFSET
) ? NULL
1563 : ((flag
& WS_DUP_SEGPTR
) ? (char*)SEGPTR_GET(p
) : p
);
1564 p
+= (flag
& WS_DUP_SEGPTR
) ? sizeof(struct ws_hostent
) : sizeof(struct hostent
);
1566 strcpy(p
, p_he
->h_name
); p
+= strlen(p
) + 1;
1568 p
+= list_dup(p_he
->h_aliases
, p
, p_base
+ (p
- pwsi
->buffer
), 0);
1570 list_dup(p_he
->h_addr_list
, p
, p_base
+ (p
- pwsi
->buffer
), p_he
->h_length
);
1571 if( !(flag
& WS_DUP_SEGPTR
) )
1572 { struct hostent
* p_to
= (struct hostent
*)pwsi
->buffer
;
1573 p_to
->h_addrtype
= p_he
->h_addrtype
; p_to
->h_length
= p_he
->h_length
;
1574 p_to
->h_name
= p_base
+ (p_name
- pwsi
->buffer
);
1575 p_to
->h_aliases
= (char**)(p_base
+ (p_aliases
- pwsi
->buffer
));
1576 p_to
->h_addr_list
= (char**)(p_base
+ (p_addr
- pwsi
->buffer
)); }
1578 { struct ws_hostent
* p_to
= (struct ws_hostent
*)pwsi
->buffer
;
1579 p_to
->h_addrtype
= (INT16
)p_he
->h_addrtype
;
1580 p_to
->h_length
= (INT16
)p_he
->h_length
;
1581 p_to
->h_name
= (SEGPTR
)(p_base
+ (p_name
- pwsi
->buffer
));
1582 p_to
->h_aliases
= (SEGPTR
)(p_base
+ (p_aliases
- pwsi
->buffer
));
1583 p_to
->h_addr_list
= (SEGPTR
)(p_base
+ (p_addr
- pwsi
->buffer
));
1585 return (size
+ sizeof(struct ws_hostent
) - sizeof(struct hostent
)); }
1590 void fixup_wshe(struct ws_hostent
* p_wshe
, SEGPTR base
)
1592 /* add 'base' to ws_hostent pointers to convert them from offsets */
1595 unsigned* p_aliases
,*p_addr
;
1597 p_aliases
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_aliases
);
1598 p_addr
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_addr_list
);
1599 ((unsigned)(p_wshe
->h_name
)) += (unsigned)base
;
1600 ((unsigned)(p_wshe
->h_aliases
)) += (unsigned)base
;
1601 ((unsigned)(p_wshe
->h_addr_list
)) += (unsigned)base
;
1602 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
1603 for(i
=0;p_addr
[i
];i
++) p_addr
[i
] += (unsigned)base
;
1606 /* ----- protoent */
1608 static int protoent_size(struct protoent
* p_pe
)
1612 { size
= sizeof(struct protoent
);
1613 size
+= strlen(p_pe
->p_name
) + 1;
1614 size
+= list_size(p_pe
->p_aliases
, 0); }
1618 int WS_dup_pe(LPWSINFO pwsi
, struct protoent
* p_pe
, int flag
)
1620 int size
= protoent_size(p_pe
);
1623 char* p_name
,*p_aliases
,*p_base
,*p
;
1625 _check_buffer(pwsi
, size
);
1627 p_base
= (flag
& WS_DUP_OFFSET
) ? NULL
1628 : ((flag
& WS_DUP_SEGPTR
) ? (char*)SEGPTR_GET(p
) : p
);
1629 p
+= (flag
& WS_DUP_SEGPTR
)? sizeof(struct ws_protoent
) : sizeof(struct protoent
);
1631 strcpy(p
, p_pe
->p_name
); p
+= strlen(p
) + 1;
1633 list_dup(p_pe
->p_aliases
, p
, p_base
+ (p
- pwsi
->buffer
), 0);
1634 if( !(flag
& WS_DUP_NATIVE
) )
1635 { struct protoent
* p_to
= (struct protoent
*)pwsi
->buffer
;
1636 p_to
->p_proto
= p_pe
->p_proto
;
1637 p_to
->p_name
= p_base
+ (p_name
- pwsi
->buffer
);
1638 p_to
->p_aliases
= (char**)(p_base
+ (p_aliases
- pwsi
->buffer
)); }
1640 { struct ws_protoent
* p_to
= (struct ws_protoent
*)pwsi
->buffer
;
1641 p_to
->p_proto
= (INT16
)p_pe
->p_proto
;
1642 p_to
->p_name
= (SEGPTR
)(p_base
) + (p_name
- pwsi
->buffer
);
1643 p_to
->p_aliases
= (SEGPTR
)((p_base
) + (p_aliases
- pwsi
->buffer
));
1644 return (size
+ sizeof(struct ws_protoent
) - sizeof(struct protoent
)); }
1649 void fixup_wspe(struct ws_protoent
* p_wspe
, SEGPTR base
)
1652 unsigned* p_aliases
= (unsigned*)((char*)p_wspe
+ (unsigned)p_wspe
->p_aliases
);
1653 ((unsigned)(p_wspe
->p_name
)) += (unsigned)base
;
1654 ((unsigned)(p_wspe
->p_aliases
)) += (unsigned)base
;
1655 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
1660 static int servent_size(struct servent
* p_se
)
1664 { size
+= sizeof(struct servent
);
1665 size
+= strlen(p_se
->s_proto
) + strlen(p_se
->s_name
) + 2;
1666 size
+= list_size(p_se
->s_aliases
, 0); }
1670 int WS_dup_se(LPWSINFO pwsi
, struct servent
* p_se
, int flag
)
1672 int size
= servent_size(p_se
);
1675 char* p_name
,*p_aliases
,*p_proto
,*p_base
,*p
;
1677 _check_buffer(pwsi
, size
);
1679 p_base
= (flag
& WS_DUP_OFFSET
) ? NULL
1680 : ((flag
& WS_DUP_SEGPTR
) ? (char*)SEGPTR_GET(p
) : p
);
1681 p
+= (flag
& WS_DUP_SEGPTR
)? sizeof(struct ws_servent
) : sizeof(struct servent
);
1683 strcpy(p
, p_se
->s_name
); p
+= strlen(p
) + 1;
1685 strcpy(p
, p_se
->s_proto
); p
+= strlen(p
) + 1;
1687 list_dup(p_se
->s_aliases
, p
, p_base
+ (p
- pwsi
->buffer
), 0);
1689 if( !(flag
& WS_DUP_SEGPTR
) )
1690 { struct servent
* p_to
= (struct servent
*)pwsi
->buffer
;
1691 p_to
->s_port
= p_se
->s_port
;
1692 p_to
->s_name
= p_base
+ (p_name
- pwsi
->buffer
);
1693 p_to
->s_proto
= p_base
+ (p_proto
- pwsi
->buffer
);
1694 p_to
->s_aliases
= (char**)(p_base
+ (p_aliases
- pwsi
->buffer
)); }
1696 { struct ws_servent
* p_to
= (struct ws_servent
*)pwsi
->buffer
;
1697 p_to
->s_port
= (INT16
)p_se
->s_port
;
1698 p_to
->s_name
= (SEGPTR
)(p_base
+ (p_name
- pwsi
->buffer
));
1699 p_to
->s_proto
= (SEGPTR
)(p_base
+ (p_proto
- pwsi
->buffer
));
1700 p_to
->s_aliases
= (SEGPTR
)(p_base
+ (p_aliases
- pwsi
->buffer
));
1701 return (size
+ sizeof(struct ws_servent
) - sizeof(struct servent
)); }
1706 void fixup_wsse(struct ws_servent
* p_wsse
, SEGPTR base
)
1709 unsigned* p_aliases
= (unsigned*)((char*)p_wsse
+ (unsigned)p_wsse
->s_aliases
);
1710 ((unsigned)(p_wsse
->s_name
)) += (unsigned)base
;
1711 ((p_wsse
->s_proto
)) += (unsigned)base
;
1712 ((p_wsse
->s_aliases
)) += (unsigned)base
;
1713 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
1716 /* ----------------------------------- error handling */
1718 UINT16
wsaErrno(void)
1720 int loc_errno
= errno
;
1721 #if defined(__FreeBSD__)
1722 dprintf_winsock(stderr
, "winsock: errno %d, (%s).\n",
1723 errno
, sys_errlist
[errno
]);
1725 dprintf_winsock(stderr
, "winsock: errno %d\n", errno
);
1730 case EINTR
: return WSAEINTR
;
1731 case EBADF
: return WSAEBADF
;
1732 case EACCES
: return WSAEACCES
;
1733 case EFAULT
: return WSAEFAULT
;
1734 case EINVAL
: return WSAEINVAL
;
1735 case EMFILE
: return WSAEMFILE
;
1736 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1737 case EINPROGRESS
: return WSAEINPROGRESS
;
1738 case EALREADY
: return WSAEALREADY
;
1739 case ENOTSOCK
: return WSAENOTSOCK
;
1740 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1741 case EMSGSIZE
: return WSAEMSGSIZE
;
1742 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1743 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1744 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1745 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1746 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1747 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1748 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1749 case EADDRINUSE
: return WSAEADDRINUSE
;
1750 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1751 case ENETDOWN
: return WSAENETDOWN
;
1752 case ENETUNREACH
: return WSAENETUNREACH
;
1753 case ENETRESET
: return WSAENETRESET
;
1754 case ECONNABORTED
: return WSAECONNABORTED
;
1755 case ECONNRESET
: return WSAECONNRESET
;
1756 case ENOBUFS
: return WSAENOBUFS
;
1757 case EISCONN
: return WSAEISCONN
;
1758 case ENOTCONN
: return WSAENOTCONN
;
1759 case ESHUTDOWN
: return WSAESHUTDOWN
;
1760 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1761 case ETIMEDOUT
: return WSAETIMEDOUT
;
1762 case ECONNREFUSED
: return WSAECONNREFUSED
;
1763 case ELOOP
: return WSAELOOP
;
1764 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1765 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1766 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1767 case ENOTEMPTY
: return WSAENOTEMPTY
;
1769 case EPROCLIM
: return WSAEPROCLIM
;
1772 case EUSERS
: return WSAEUSERS
;
1775 case EDQUOT
: return WSAEDQUOT
;
1778 case ESTALE
: return WSAESTALE
;
1781 case EREMOTE
: return WSAEREMOTE
;
1784 /* just in case we ever get here and there are no problems */
1787 fprintf(stderr
, "winsock: unknown errno %d!\n", errno
);
1788 return WSAEOPNOTSUPP
;
1792 UINT16
wsaHerrno(void)
1794 int loc_errno
= h_errno
;
1796 #if defined(__FreeBSD__)
1797 dprintf_winsock(stderr
, "winsock: h_errno %d, (%s).\n",
1798 h_errno
, sys_errlist
[h_errno
]);
1800 dprintf_winsock(stderr
, "winsock: h_errno %d.\n", h_errno
);
1802 if( debugging_winsock
) herror("wine: winsock: wsaherrno");
1808 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
1809 case TRY_AGAIN
: return WSATRY_AGAIN
;
1810 case NO_RECOVERY
: return WSANO_RECOVERY
;
1811 case NO_DATA
: return WSANO_DATA
;
1815 fprintf(stderr
, "winsock: unknown h_errno %d!\n", h_errno
);
1816 return WSAEOPNOTSUPP
;