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.
11 #include <sys/types.h>
13 #include <sys/ioctl.h>
15 #include <sys/filio.h>
16 #include <sys/ioccom.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
36 #define dump_sockaddr(a) \
37 fprintf(stderr, "sockaddr_in: family %d, address %s, port %d\n", \
38 ((struct sockaddr_in *)a)->sin_family, \
39 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
40 ntohs(((struct sockaddr_in *)a)->sin_port))
42 extern void SIGNAL_MaskAsyncEvents( BOOL32
);
46 /* ----------------------------------- internal data */
49 extern void __sigio(int);
51 ws_async_ctl async_ctl
;
54 static HANDLE32 _WSHeap
= 0;
55 static unsigned char* _ws_stub
= NULL
;
56 static LPWSINFO _wsi_list
= NULL
;
58 #define WS_ALLOC(size) \
59 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
60 #define WS_FREE(ptr) \
61 HeapFree(_WSHeap, 0, (ptr) )
63 #define WS_PTR2HANDLE(ptr) \
64 ((short)((int)(ptr) - (int)_ws_stub))
65 #define WS_HANDLE2PTR(handle) \
66 ((unsigned)((int)_ws_stub + (int)handle))
68 #define WSI_CHECK_RANGE(pwsi, pws) \
69 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
70 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
72 static INT16 _ws_sock_ops
[] =
73 { WS_SO_DEBUG
, WS_SO_REUSEADDR
, WS_SO_KEEPALIVE
, WS_SO_DONTROUTE
,
74 WS_SO_BROADCAST
, WS_SO_LINGER
, WS_SO_OOBINLINE
, WS_SO_SNDBUF
,
75 WS_SO_RCVBUF
, WS_SO_ERROR
, WS_SO_TYPE
, WS_SO_DONTLINGER
, 0 };
76 static int _px_sock_ops
[] =
77 { SO_DEBUG
, SO_REUSEADDR
, SO_KEEPALIVE
, SO_DONTROUTE
, SO_BROADCAST
,
78 SO_LINGER
, SO_OOBINLINE
, SO_SNDBUF
, SO_RCVBUF
, SO_ERROR
, SO_TYPE
,
81 static INT16
init_async_select(ws_socket
* pws
, HWND16 hWnd
, UINT16 uMsg
, UINT32 lEvent
);
82 static int notify_client(ws_socket
* pws
, unsigned flag
);
84 static int _check_ws(LPWSINFO pwsi
, ws_socket
* pws
);
85 static int _check_buffer(LPWSINFO pwsi
, int size
);
87 static void fixup_wshe(struct ws_hostent
* p_wshe
, SEGPTR base
);
88 static void fixup_wspe(struct ws_protoent
* p_wspe
, SEGPTR base
);
89 static void fixup_wsse(struct ws_servent
* p_wsse
, SEGPTR base
);
91 static int cancel_async_select(ws_socket
*);
93 static void convert_sockopt(INT16
*level
, INT16
*optname
)
100 for(i
=0; _ws_sock_ops
[i
]; i
++)
101 if( _ws_sock_ops
[i
] == *optname
) break;
102 if( _ws_sock_ops
[i
] ) *optname
= (INT16
)_px_sock_ops
[i
];
103 else fprintf(stderr
, "convert_sockopt() unknown optname %d\n", *optname
);
106 *optname
= IPPROTO_TCP
;
110 static void _ws_global_init()
114 _WSHeap
= HeapCreate(HEAP_ZERO_MEMORY
, 8120, 32768);
115 if( !(_ws_stub
= WS_ALLOC(0x10)) )
116 fprintf(stderr
,"Fatal: failed to create WinSock heap\n");
118 if( async_qid
== -1 )
119 if( (async_qid
= msgget(IPC_PRIVATE
, IPC_CREAT
| 0x1FF)) == -1 )
120 fprintf(stderr
,"Fatal: failed to create WinSock resource\n");
123 /* ----------------------------------- Per-thread info */
125 static void wsi_link(LPWSINFO pwsi
)
126 { if( _wsi_list
) _wsi_list
->prev
= pwsi
;
127 pwsi
->next
= _wsi_list
; _wsi_list
= pwsi
;
130 static void wsi_unlink(LPWSINFO pwsi
)
132 if( pwsi
== _wsi_list
) _wsi_list
= pwsi
->next
;
134 { pwsi
->prev
->next
= pwsi
->next
;
135 if( pwsi
->next
) pwsi
->next
->prev
= pwsi
->prev
; }
138 static LPWSINFO
wsi_find(HTASK16 hTask
)
139 { LPWSINFO pwsi
= _wsi_list
;
140 while( pwsi
&& pwsi
->tid
!= hTask
) pwsi
= pwsi
->next
;
144 static ws_socket
* wsi_alloc_socket(LPWSINFO pwsi
, int fd
)
146 if( pwsi
->last_free
>= 0 )
148 int i
= pwsi
->last_free
;
150 pwsi
->last_free
= pwsi
->sock
[i
].flags
;
151 pwsi
->sock
[i
].fd
= fd
;
152 pwsi
->sock
[i
].flags
= 0;
153 return &pwsi
->sock
[i
];
158 static void fd_set_normalize(fd_set
* fds
, LPWSINFO pwsi
, ws_fd_set
* ws
, int* highfd
)
165 for(i
=0;i
<(ws
->fd_count
);i
++)
167 pws
= (ws_socket
*)WS_HANDLE2PTR(ws
->fd_array
[i
]);
168 if( _check_ws(pwsi
, pws
) )
170 if( pws
->fd
> *highfd
) *highfd
= pws
->fd
;
171 FD_SET(pws
->fd
, fds
);
178 * Note weirdness here: sockets with errors belong in exceptfds, but
179 * are given to us in readfds or writefds, so move them to exceptfds if
180 * there is an error. Note that this means that exceptfds may have mysterious
181 * sockets set in it that the program never asked for.
184 static int inline sock_error_p(int s
)
186 unsigned int optval
, optlen
;
188 optlen
= sizeof(optval
);
189 getsockopt(s
, SOL_SOCKET
, SO_ERROR
, &optval
, &optlen
);
190 if (optval
) dprintf_winsock(stddeb
, "error: %d\n", optval
);
194 static void fd_set_update(LPWSINFO pwsi
, fd_set
* fds
, ws_fd_set
* ws
,
199 int i
, j
, count
= ws
->fd_count
;
201 for( i
= 0, j
= 0; i
< count
; i
++ )
203 ws_socket
*pws
= (ws_socket
*)WS_HANDLE2PTR(ws
->fd_array
[i
]);
206 if( _check_ws(pwsi
, pws
) && FD_ISSET(fd
, fds
) )
208 /* if error, move to errorfds */
209 if (errorfds
&& (FD_ISSET(fd
, errorfds
) || sock_error_p(fd
)))
210 FD_SET(fd
, errorfds
);
212 ws
->fd_array
[j
++] = ws
->fd_array
[i
];
216 dprintf_winsock(stddeb
, "\n");
221 static void fd_set_update_except(LPWSINFO pwsi
, fd_set
*fds
, ws_fd_set
*ws
,
226 int i
, j
, count
= ws
->fd_count
;
228 for (i
=j
=0; i
< count
; i
++)
230 ws_socket
*pws
= (ws_socket
*)WS_HANDLE2PTR(ws
->fd_array
[i
]);
232 if (_check_ws(pwsi
, pws
) && (FD_ISSET(pws
->fd
, fds
)
233 || FD_ISSET(pws
->fd
, errorfds
)))
234 ws
->fd_array
[j
++] = ws
->fd_array
[i
];
241 /* ----------------------------------- API -----
243 * Init / cleanup / error checking.
246 INT16
WSAStartup(UINT16 wVersionRequested
, LPWSADATA lpWSAData
)
248 WSADATA WINSOCK_data
= { 0x0101, 0x0101,
252 #elif defined(__NetBSD__)
256 #elif defined(__FreeBSD__)
261 WS_MAX_SOCKETS_PER_THREAD
,
262 WS_MAX_UDP_DATAGRAM
, NULL
};
263 HTASK16 tid
= GetCurrentTask();
266 dprintf_winsock(stddeb
, "WSAStartup: verReq=%x\n", wVersionRequested
);
268 if (LOBYTE(wVersionRequested
) < 1 || (LOBYTE(wVersionRequested
) == 1 &&
269 HIBYTE(wVersionRequested
) < 1)) return WSAVERNOTSUPPORTED
;
271 if (!lpWSAData
) return WSAEINVAL
;
274 if( _WSHeap
== 0 ) return WSASYSNOTREADY
;
276 pwsi
= wsi_find(GetCurrentTask());
279 if( (pwsi
= (LPWSINFO
)WS_ALLOC( sizeof(WSINFO
))) )
283 for( i
= 0; i
< WS_MAX_SOCKETS_PER_THREAD
; i
++ )
285 pwsi
->sock
[i
].fd
= -1;
286 pwsi
->sock
[i
].flags
= i
+ 1;
288 pwsi
->sock
[WS_MAX_SOCKETS_PER_THREAD
- 1].flags
= -1;
290 else return WSASYSNOTREADY
;
292 } else pwsi
->num_startup
++;
294 /* return winsock information */
295 memcpy(lpWSAData
, &WINSOCK_data
, sizeof(WINSOCK_data
));
297 dprintf_winsock(stddeb
, "WSAStartup: succeeded\n");
301 void WINSOCK_Shutdown()
303 if( async_qid
!= -1 )
304 if( msgctl(async_qid
, IPC_RMID
, NULL
) == -1 )
305 fprintf(stderr
,"failed to delete WS message queue.\n");
309 INT16
WSACleanup(void)
311 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
313 /* FIXME: do global cleanup if no current task */
315 dprintf_winsock(stddeb
, "WSACleanup(%08x)\n", (unsigned)pwsi
);
320 if( pwsi
->num_startup
-- ) return 0;
322 SIGNAL_MaskAsyncEvents( TRUE
);
323 WINSOCK_cancel_async_op(GetCurrentTask());
324 SIGNAL_MaskAsyncEvents( FALSE
);
327 if( _wsi_list
== NULL
) WINSOCK_Shutdown();
329 if( pwsi
->flags
& WSI_BLOCKINGCALL
)
330 dprintf_winsock(stddeb
,"\tinside blocking call!\n");
331 if( pwsi
->num_async_rq
)
332 dprintf_winsock(stddeb
,"\thave %i outstanding async ops!\n", pwsi
->num_async_rq
);
334 for(i
= 0, j
= 0, n
= 0; i
< WS_MAX_SOCKETS_PER_THREAD
; i
++)
335 if( pwsi
->sock
[i
].fd
!= -1 )
337 n
+= cancel_async_select(&pwsi
->sock
[i
]);
338 close(pwsi
->sock
[i
].fd
); j
++;
341 dprintf_winsock(stddeb
,"\tclosed %i sockets, killed %i async selects!\n", j
, n
);
343 if( pwsi
->buffer
) SEGPTR_FREE(pwsi
->buffer
);
344 if( pwsi
->dbuffer
) SEGPTR_FREE(pwsi
->dbuffer
);
351 INT16
WSAGetLastError(void)
353 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
356 dprintf_winsock(stddeb
, "WSAGetLastError(%08x)", (unsigned)pwsi
);
358 ret
= (pwsi
) ? pwsi
->errno
: WSANOTINITIALISED
;
360 dprintf_winsock(stddeb
, " = %i\n", (int)ret
);
364 void WSASetLastError(INT16 iError
)
366 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
368 dprintf_winsock(stddeb
, "WSASetLastError(%08x): %d\n", (unsigned)pwsi
, (int)iError
);
370 if( pwsi
) pwsi
->errno
= iError
;
373 int _check_ws(LPWSINFO pwsi
, ws_socket
* pws
)
376 if( pwsi
->flags
& WSI_BLOCKINGCALL
) pwsi
->errno
= WSAEINPROGRESS
;
377 else if( WSI_CHECK_RANGE(pwsi
, pws
) ) return 1;
378 else pwsi
->errno
= WSAENOTSOCK
;
382 int _check_buffer(LPWSINFO pwsi
, int size
)
384 if( pwsi
->buffer
&& pwsi
->buflen
>= size
) return 1;
385 else SEGPTR_FREE(pwsi
->buffer
);
386 pwsi
->buffer
= (char*)SEGPTR_ALLOC((pwsi
->buflen
= size
));
387 return (pwsi
->buffer
!= NULL
);
390 /* ----- socket operations */
392 SOCKET16
WINSOCK_accept(SOCKET16 s
, struct sockaddr
*addr
, INT16
*addrlen16
)
394 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
395 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
397 dprintf_winsock(stddeb
, "WS_ACCEPT(%08x): socket %04x\n",
398 (unsigned)pwsi
, (UINT16
)s
);
399 if( _check_ws(pwsi
, pws
) )
401 int sock
, fd_flags
, addrlen32
= *addrlen16
;
403 /* this is how block info is supposed to be used -
404 * WSAIsBlocking() would then check WSI_BLOCKINGCALL bit.
407 fd_flags
= fcntl(pws
->fd
, F_GETFL
, 0);
408 if( !(fd_flags
& O_NONBLOCK
) ) pwsi
->flags
|= WSI_BLOCKINGCALL
;
410 if( (sock
= accept(pws
->fd
, addr
, &addrlen32
)) >= 0 )
412 ws_socket
* pnew
= wsi_alloc_socket(pwsi
, sock
);
413 notify_client(pws
, WS_FD_ACCEPT
);
417 init_async_select(pnew
, pws
->p_aop
->hWnd
,
419 pws
->p_aop
->flags
& ~WS_FD_ACCEPT
);
421 pwsi
->flags
&= ~WSI_BLOCKINGCALL
;
422 return (SOCKET16
)WS_PTR2HANDLE(pnew
);
424 else pwsi
->errno
= WSAENOBUFS
;
426 else pwsi
->errno
= wsaErrno();
428 pwsi
->flags
&= ~WSI_BLOCKINGCALL
;
430 return INVALID_SOCKET
;
433 INT16
WINSOCK_bind(SOCKET16 s
, struct sockaddr
*name
, INT16 namelen
)
435 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
436 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
438 dprintf_winsock(stddeb
, "WS_BIND(%08x): socket %04x, ptr %8x, length %d\n",
439 (unsigned)pwsi
, s
, (int) name
, namelen
);
444 if ( _check_ws(pwsi
, pws
) )
445 if (namelen
>= sizeof(*name
))
446 if ( ((struct sockaddr_in
*)name
)->sin_family
== AF_INET
)
447 if ( bind(pws
->fd
, name
, namelen
) < 0 )
449 int loc_errno
= errno
;
450 dprintf_winsock(stddeb
,"\tfailure - errno = %i\n", errno
);
454 case EBADF
: pwsi
->errno
= WSAENOTSOCK
; break;
455 case EADDRNOTAVAIL
: pwsi
->errno
= WSAEINVAL
; break;
456 default: pwsi
->errno
= wsaErrno();
460 else pwsi
->errno
= WSAEAFNOSUPPORT
;
461 else pwsi
->errno
= WSAEFAULT
;
465 INT16
WINSOCK_closesocket(SOCKET16 s
)
467 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
468 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
470 dprintf_winsock(stddeb
, "WS_CLOSE(%08x): socket %08x\n", (unsigned)pwsi
, s
);
472 if( _check_ws(pwsi
, pws
) )
476 cancel_async_select(pws
);
478 pws
->flags
= (unsigned)pwsi
->last_free
;
479 pwsi
->last_free
= pws
- &pwsi
->sock
[0];
480 if (close(fd
) == 0) return 0;
481 pwsi
->errno
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
486 INT16
WINSOCK_connect(SOCKET16 s
, struct sockaddr
*name
, INT16 namelen
)
488 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
489 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
491 dprintf_winsock(stddeb
, "WS_CONNECT(%08x): socket %04x, ptr %8x, length %d\n",
492 (unsigned)pwsi
, s
, (int) name
, namelen
);
497 if( _check_ws(pwsi
, pws
) )
499 if (connect(pws
->fd
, name
, namelen
) == 0)
502 /* we need to notify handler process if
503 * connect() succeeded NOT in response to winsock message
505 notify_client(pws
, WS_FD_CONNECTED
);
507 pws
->flags
&= ~(WS_FD_INACTIVE
| WS_FD_CONNECT
);
510 pwsi
->errno
= (errno
== EINPROGRESS
) ? WSAEWOULDBLOCK
: wsaErrno();
515 INT16
WINSOCK_getpeername(SOCKET16 s
, struct sockaddr
*name
, INT16
*namelen
)
517 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
518 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
520 dprintf_winsock(stddeb
, "WS_GETPEERNAME(%08x): socket: %04x, ptr %8x, ptr %8x\n",
521 (unsigned)pwsi
, s
, (int) name
, *namelen
);
522 if( _check_ws(pwsi
, pws
) )
524 int namelen32
= *namelen
;
525 if (getpeername(pws
->fd
, name
, &namelen32
) == 0)
530 *namelen
= (INT16
)namelen32
;
533 pwsi
->errno
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
538 INT16
WINSOCK_getsockname(SOCKET16 s
, struct sockaddr
*name
, INT16
*namelen
)
540 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
541 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
543 dprintf_winsock(stddeb
, "WS_GETSOCKNAME(%08x): socket: %04x, ptr %8x, ptr %8x\n",
544 (unsigned)pwsi
, s
, (int) name
, (int) *namelen
);
545 if( _check_ws(pwsi
, pws
) )
547 int namelen32
= *namelen
;
548 if (getsockname(pws
->fd
, name
, &namelen32
) == 0)
550 *namelen
= (INT16
)namelen32
;
553 pwsi
->errno
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
558 INT16
WINSOCK_getsockopt(SOCKET16 s
, INT16 level
,
559 INT16 optname
, char *optval
, INT16
*optlen
)
561 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
562 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
564 dprintf_winsock(stddeb
, "WS_GETSOCKOPT(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
565 (unsigned)pwsi
, s
, level
, (int) optval
, (int) *optlen
);
567 if( _check_ws(pwsi
, pws
) )
569 int optlen32
= *optlen
;
571 convert_sockopt(&level
, &optname
);
572 if (getsockopt(pws
->fd
, (int) level
, optname
, optval
, &optlen32
) == 0 )
573 { *optlen
= (INT16
)optlen32
; return 0; }
574 pwsi
->errno
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
579 u_long
WINSOCK_htonl(u_long hostlong
) { return( htonl(hostlong
) ); }
580 u_short
WINSOCK_htons(u_short hostshort
) { return( htons(hostshort
) ); }
581 u_long
WINSOCK_inet_addr(char *cp
) { return( inet_addr(cp
) ); }
582 u_long
WINSOCK_ntohl(u_long netlong
) { return( ntohl(netlong
) ); }
583 u_short
WINSOCK_ntohs(u_short netshort
) { return( ntohs(netshort
) ); }
585 SEGPTR
WINSOCK_inet_ntoa(struct in_addr in
)
587 /* use "buffer for dummies" here because some applications have
588 * propensity to decode addresses in ws_hostent structure without
589 * saving them first...
592 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
596 char* s
= inet_ntoa(in
);
599 if( pwsi
->dbuffer
== NULL
)
600 if((pwsi
->dbuffer
= (char*) SEGPTR_ALLOC(32)) == NULL
)
602 pwsi
->errno
= WSAENOBUFS
;
605 strncpy(pwsi
->dbuffer
, s
, 32 );
606 return SEGPTR_GET(pwsi
->dbuffer
);
608 pwsi
->errno
= wsaErrno();
613 INT16
WINSOCK_ioctlsocket(SOCKET16 s
, UINT32 cmd
, UINT32
*argp
)
615 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
616 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
618 dprintf_winsock(stddeb
, "WS_IOCTL(%08x): socket %04x, cmd %08x, ptr %8x\n",
619 (unsigned)pwsi
, s
, cmd
, (unsigned) argp
);
620 if( _check_ws(pwsi
, pws
) )
626 case WS_FIONREAD
: newcmd
=FIONREAD
; break;
627 case WS_FIONBIO
: newcmd
=FIONBIO
;
628 if( pws
->p_aop
&& *argp
== 0 )
630 pwsi
->errno
= WSAEINVAL
;
634 case WS_SIOCATMARK
: newcmd
=SIOCATMARK
; break;
635 case WS_IOW('f',125,u_long
):
636 fprintf(stderr
,"Warning: WS1.1 shouldn't be using async I/O\n");
637 pwsi
->errno
= WSAEINVAL
; return SOCKET_ERROR
;
638 default: fprintf(stderr
,"Warning: Unknown WS_IOCTL cmd (%08x)\n", cmd
);
640 if( ioctl(pws
->fd
, newcmd
, (char*)argp
) == 0 ) return 0;
641 pwsi
->errno
= (errno
== EBADF
) ? WSAENOTSOCK
: wsaErrno();
646 INT16
WINSOCK_listen(SOCKET16 s
, INT16 backlog
)
648 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
649 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
651 dprintf_winsock(stddeb
, "WS_LISTEN(%08x): socket %04x, backlog %d\n",
652 (unsigned)pwsi
, s
, backlog
);
653 if( _check_ws(pwsi
, pws
) )
657 int fd_flags
= fcntl(pws
->fd
, F_GETFL
, 0);
658 if( !(fd_flags
& O_NONBLOCK
) ) pws
->flags
|= WS_FD_ACCEPT
;
660 else notify_client(pws
, WS_FD_ACCEPT
);
662 if (listen(pws
->fd
, backlog
) == 0) return 0;
663 pwsi
->errno
= wsaErrno();
668 INT16
WINSOCK_recv(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
)
670 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
671 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
673 dprintf_winsock(stddeb
, "WS_RECV(%08x): socket %04x, buf %8x, len %d, flags %d",
674 (unsigned)pwsi
, s
, (unsigned)buf
, len
, flags
);
675 if( _check_ws(pwsi
, pws
) )
678 if ((length
= recv(pws
->fd
, buf
, len
, flags
)) >= 0)
680 dprintf_winsock(stddeb
, " -> %i bytes\n", length
);
681 notify_client(pws
, WS_FD_READ
);
682 return (INT16
)length
;
684 pwsi
->errno
= wsaErrno();
686 dprintf_winsock(stddeb
, " -> ERROR\n");
690 INT16
WINSOCK_recvfrom(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
,
691 struct sockaddr
*from
, INT16
*fromlen16
)
693 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
694 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
696 dprintf_winsock(stddeb
, "WS_RECVFROM(%08x): socket %04x, ptr %08x, len %d, flags %d\n",
697 (unsigned)pwsi
, s
, (unsigned)buf
, len
, flags
);
698 if( _check_ws(pwsi
, pws
) )
700 int length
, fromlen32
= *fromlen16
;
702 if ((length
= recvfrom(pws
->fd
, buf
, len
, flags
, from
, &fromlen32
)) >= 0 );
704 *fromlen16
= fromlen32
;
705 notify_client(pws
, WS_FD_READ
);
706 return (INT16
)length
;
708 pwsi
->errno
= wsaErrno();
713 INT16
WINSOCK_select(INT16 nfds
, ws_fd_set
*ws_readfds
,
714 ws_fd_set
*ws_writefds
,
715 ws_fd_set
*ws_exceptfds
, struct timeval
*timeout
)
717 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
719 dprintf_winsock(stddeb
, "WS_SELECT(%08x): nfds %d (ignored), read %8x, write %8x, excp %8x\n",
720 (unsigned) pwsi
, nfds
, (unsigned) ws_readfds
, (unsigned) ws_writefds
, (unsigned) ws_exceptfds
);
725 fd_set readfds
, writefds
, exceptfds
, errorfds
;
727 fd_set_normalize(&readfds
, pwsi
, ws_readfds
, &highfd
);
728 fd_set_normalize(&writefds
, pwsi
, ws_writefds
, &highfd
);
729 fd_set_normalize(&exceptfds
, pwsi
, ws_exceptfds
, &highfd
);
732 if( (highfd
= select(highfd
+ 1, &readfds
, &writefds
, &exceptfds
, timeout
)) >= 0 )
736 fd_set_update(pwsi
, &readfds
, ws_readfds
, &errorfds
);
737 fd_set_update(pwsi
, &writefds
, ws_writefds
, &errorfds
);
738 fd_set_update_except(pwsi
, &exceptfds
, ws_exceptfds
, &errorfds
);
742 pwsi
->errno
= wsaErrno();
747 INT16
WINSOCK_send(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
)
749 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
750 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
752 dprintf_winsock(stddeb
, "WS_SEND(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
753 (unsigned)pwsi
, s
, (unsigned) buf
, len
, flags
);
754 if( _check_ws(pwsi
, pws
) )
757 if ((length
= send(pws
->fd
, buf
, len
, flags
)) < 0 )
759 length
= SOCKET_ERROR
;
760 pwsi
->errno
= wsaErrno();
762 notify_client(pws
, WS_FD_WRITE
);
763 return (INT16
)length
;
768 INT16
WINSOCK_sendto(SOCKET16 s
, char *buf
, INT16 len
, INT16 flags
,
769 struct sockaddr
*to
, INT16 tolen
)
771 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
772 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
774 dprintf_winsock(stddeb
, "WS_SENDTO(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
775 (unsigned)pwsi
, s
, (unsigned) buf
, len
, flags
);
776 if( _check_ws(pwsi
, pws
) )
780 if ((length
= sendto(pws
->fd
, buf
, len
, flags
, to
, tolen
)) < 0 )
782 length
= SOCKET_ERROR
;
783 pwsi
->errno
= wsaErrno();
785 notify_client(pws
, WS_FD_WRITE
);
786 return (INT16
)length
;
791 INT16
WINSOCK_setsockopt(SOCKET16 s
, INT16 level
, INT16 optname
,
792 char *optval
, INT16 optlen
)
794 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
795 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
797 dprintf_winsock(stddeb
, "WS_SETSOCKOPT(%08x): socket %04x, level %d, opt %d, ptr %08x, len %d\n",
798 (unsigned)pwsi
, s
, level
, optname
, (int) optval
, optlen
);
799 if( _check_ws(pwsi
, pws
) )
802 convert_sockopt(&level
, &optname
);
803 if( optname
== SO_LINGER
)
805 INT16
* ptr
= (INT16
*)optval
;
806 linger32
[0] = ptr
[0];
807 linger32
[1] = ptr
[1];
808 optval
= (char*)&linger32
;
809 optlen
= sizeof(linger32
);
811 if (setsockopt(pws
->fd
, level
, optname
, optval
, optlen
) == 0) return 0;
812 pwsi
->errno
= wsaErrno();
817 INT16
WINSOCK_shutdown(SOCKET16 s
, INT16 how
)
819 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
820 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
822 dprintf_winsock(stddeb
, "WS_SHUTDOWN(%08x): socket %04x, how %i\n",
823 (unsigned)pwsi
, s
, how
);
824 if( _check_ws(pwsi
, pws
) )
826 pws
->flags
|= WS_FD_INACTIVE
;
827 cancel_async_select(pws
);
829 if (shutdown(pws
->fd
, how
) == 0) return 0;
830 pwsi
->errno
= wsaErrno();
835 SOCKET16
WINSOCK_socket(INT16 af
, INT16 type
, INT16 protocol
)
837 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
839 dprintf_winsock(stddeb
, "WS_SOCKET(%08x): af=%d type=%d protocol=%d\n",
840 (unsigned)pwsi
, af
, type
, protocol
);
846 /* check the socket family */
850 case AF_UNSPEC
: break;
851 default: pwsi
->errno
= WSAEAFNOSUPPORT
; return INVALID_SOCKET
;
854 /* check the socket type */
859 case SOCK_RAW
: break;
860 default: pwsi
->errno
= WSAESOCKTNOSUPPORT
; return INVALID_SOCKET
;
863 /* check the protocol type */
864 if ( protocol
< 0 ) /* don't support negative values */
865 { pwsi
->errno
= WSAEPROTONOSUPPORT
; return INVALID_SOCKET
; }
867 if ( af
== AF_UNSPEC
) /* did they not specify the address family? */
871 if (type
== SOCK_STREAM
) { af
= AF_INET
; break; }
873 if (type
== SOCK_DGRAM
) { af
= AF_INET
; break; }
874 default: pwsi
->errno
= WSAEPROTOTYPE
; return INVALID_SOCKET
;
877 if ((sock
= socket(af
, type
, protocol
)) >= 0)
879 ws_socket
* pnew
= wsi_alloc_socket(pwsi
, sock
);
881 dprintf_winsock(stddeb
,"\tcreated %04x (handle %i)\n", sock
, (UINT16
)WS_PTR2HANDLE(pnew
));
883 if( pnew
) return (SOCKET16
)WS_PTR2HANDLE(pnew
);
886 pwsi
->errno
= WSAENOBUFS
;
887 return INVALID_SOCKET
;
891 if (errno
== EPERM
) /* raw socket denied */
893 fprintf(stderr
, "WS_SOCKET: not enough privileges\n");
894 pwsi
->errno
= WSAESOCKTNOSUPPORT
;
895 } else pwsi
->errno
= wsaErrno();
898 dprintf_winsock(stddeb
, "\t\tfailed!\n");
899 return INVALID_SOCKET
;
903 /* ----- database functions
905 * Note that ws_...ent structures we return have SEGPTR pointers inside them.
908 static char* NULL_STRING
= "NULL";
913 SEGPTR
WINSOCK_gethostbyaddr(const char *addr
, INT16 len
, INT16 type
)
915 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
917 dprintf_winsock(stddeb
, "WS_GetHostByAddr(%08x): ptr %8x, len %d, type %d\n",
918 (unsigned)pwsi
, (unsigned) addr
, len
, type
);
921 struct hostent
* host
;
922 if( (host
= gethostbyaddr(addr
, len
, type
)) != NULL
)
923 if( WS_dup_he(pwsi
, host
, WS_DUP_SEGPTR
) )
924 return SEGPTR_GET(pwsi
->buffer
);
925 else pwsi
->errno
= WSAENOBUFS
;
926 else pwsi
->errno
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
934 SEGPTR
WINSOCK_gethostbyname(const char *name
)
936 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
938 dprintf_winsock(stddeb
, "WS_GetHostByName(%08x): %s\n",
939 (unsigned)pwsi
, (name
)?name
:"NULL");
942 struct hostent
* host
;
943 if( (host
= gethostbyname(name
)) != NULL
)
944 if( WS_dup_he(pwsi
, host
, WS_DUP_SEGPTR
) )
945 return SEGPTR_GET(pwsi
->buffer
);
946 else pwsi
->errno
= WSAENOBUFS
;
947 else pwsi
->errno
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
952 INT16
WINSOCK_gethostname(char *name
, INT16 namelen
)
954 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
956 dprintf_winsock(stddeb
, "WS_GetHostName(%08x): name %s, len %d\n",
957 (unsigned)pwsi
, (name
)?name
:NULL_STRING
, namelen
);
960 if (gethostname(name
, namelen
) == 0) return 0;
961 pwsi
->errno
= (errno
== EINVAL
) ? WSAEFAULT
: wsaErrno();
967 struct WIN_protoent *
969 SEGPTR
WINSOCK_getprotobyname(char *name
)
971 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
973 dprintf_winsock(stddeb
, "WS_GetProtoByName(%08x): %s\n",
974 (unsigned)pwsi
, (name
)?name
:NULL_STRING
);
977 struct protoent
* proto
;
978 if( (proto
= getprotobyname(name
)) != NULL
)
979 if( WS_dup_pe(pwsi
, proto
, WS_DUP_SEGPTR
) )
980 return SEGPTR_GET(pwsi
->buffer
);
981 else pwsi
->errno
= WSAENOBUFS
;
982 else pwsi
->errno
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
988 struct WIN_protoent *
990 SEGPTR
WINSOCK_getprotobynumber(INT16 number
)
992 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
994 dprintf_winsock(stddeb
, "WS_GetProtoByNumber(%08x): %i\n", (unsigned)pwsi
, number
);
998 struct protoent
* proto
;
999 if( (proto
= getprotobynumber(number
)) != NULL
)
1000 if( WS_dup_pe(pwsi
, proto
, WS_DUP_SEGPTR
) )
1001 return SEGPTR_GET(pwsi
->buffer
);
1002 else pwsi
->errno
= WSAENOBUFS
;
1003 else pwsi
->errno
= WSANO_DATA
;
1009 struct WIN_servent *
1011 SEGPTR
WINSOCK_getservbyname(const char *name
, const char *proto
)
1013 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1015 dprintf_winsock(stddeb
, "WS_GetServByName(%08x): '%s', '%s'\n",
1016 (unsigned)pwsi
, (name
)?name
:NULL_STRING
, (proto
)?proto
:NULL_STRING
);
1020 struct servent
* serv
;
1021 if( (serv
= getservbyname(name
, proto
)) != NULL
)
1022 if( WS_dup_se(pwsi
, serv
, WS_DUP_SEGPTR
) )
1023 return SEGPTR_GET(pwsi
->buffer
);
1024 else pwsi
->errno
= WSAENOBUFS
;
1025 else pwsi
->errno
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
1031 struct WIN_servent *
1033 SEGPTR
WINSOCK_getservbyport(INT16 port
, const char *proto
)
1035 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1037 dprintf_winsock(stddeb
, "WS_GetServByPort(%08x): %i, '%s'\n",
1038 (unsigned)pwsi
, (int)port
, (proto
)?proto
:NULL_STRING
);
1041 struct servent
* serv
;
1042 if( (serv
= getservbyport(port
, proto
)) != NULL
)
1043 if( WS_dup_se(pwsi
, serv
, WS_DUP_SEGPTR
) )
1044 return SEGPTR_GET(pwsi
->buffer
);
1045 else pwsi
->errno
= WSAENOBUFS
;
1046 else pwsi
->errno
= (h_errno
< 0) ? wsaErrno() : wsaHerrno();
1052 /* ----------------------------------- Windows sockets extensions -- *
1054 * ----------------------------------------------------------------- */
1056 static int aop_control(ws_async_op
* p_aop
, int flag
)
1060 read(p_aop
->fd
[0], &lLength
, sizeof(unsigned));
1061 if( LOWORD(lLength
) )
1062 if( (int)LOWORD(lLength
) <= p_aop
->buflen
)
1064 char* buffer
= (char*)PTR_SEG_TO_LIN(p_aop
->buffer_base
);
1065 read(p_aop
->fd
[0], buffer
, LOWORD(lLength
));
1066 switch( p_aop
->flags
)
1068 case WSMSG_ASYNC_HOSTBYNAME
:
1069 case WSMSG_ASYNC_HOSTBYADDR
:
1070 fixup_wshe((struct ws_hostent
*)buffer
, p_aop
->buffer_base
); break;
1071 case WSMSG_ASYNC_PROTOBYNAME
:
1072 case WSMSG_ASYNC_PROTOBYNUM
:
1073 fixup_wspe((struct ws_protoent
*)buffer
, p_aop
->buffer_base
); break;
1074 case WSMSG_ASYNC_SERVBYNAME
:
1075 case WSMSG_ASYNC_SERVBYPORT
:
1076 fixup_wsse((struct ws_servent
*)buffer
, p_aop
->buffer_base
); break;
1078 if( p_aop
->flags
) fprintf(stderr
,"Received unknown async request!\n");
1079 return AOP_CONTROL_REMOVE
;
1082 else lLength
= ((UINT32
)LOWORD(lLength
)) | ((unsigned)WSAENOBUFS
<< 16);
1085 printf("async op completed: hWnd [%04x], uMsg [%04x], aop [%04x], event [%08x]\n",
1086 p_aop
->hWnd
, p_aop
->uMsg
, (HANDLE16
)WS_PTR2HANDLE(p_aop
), (LPARAM
)lLength
);
1089 PostMessage(p_aop
->hWnd
, p_aop
->uMsg
, (HANDLE16
)WS_PTR2HANDLE(p_aop
), (LPARAM
)lLength
);
1090 return AOP_CONTROL_REMOVE
;
1094 static HANDLE16
__WSAsyncDBQuery(LPWSINFO pwsi
, HWND16 hWnd
, UINT16 uMsg
, LPCSTR init
,
1095 INT16 len
, INT16 type
, SEGPTR sbuf
, INT16 buflen
, UINT32 flag
)
1097 /* queue 'flag' request and fork off its handler */
1099 async_ctl
.ws_aop
= (ws_async_op
*)WS_ALLOC(sizeof(ws_async_op
));
1101 if( async_ctl
.ws_aop
)
1103 HANDLE16 handle
= (HANDLE16
)WS_PTR2HANDLE(async_ctl
.ws_aop
);
1105 if( pipe(async_ctl
.ws_aop
->fd
) == 0 )
1107 async_ctl
.init
= (char*)init
;
1108 async_ctl
.lLength
= len
;
1109 async_ctl
.lEvent
= type
;
1111 async_ctl
.ws_aop
->hWnd
= hWnd
;
1112 async_ctl
.ws_aop
->uMsg
= uMsg
;
1114 async_ctl
.ws_aop
->buffer_base
= sbuf
; async_ctl
.ws_aop
->buflen
= buflen
;
1115 async_ctl
.ws_aop
->flags
= flag
;
1116 async_ctl
.ws_aop
->aop_control
= &aop_control
;
1117 WINSOCK_link_async_op( async_ctl
.ws_aop
);
1119 async_ctl
.ws_aop
->pid
= fork();
1120 if( async_ctl
.ws_aop
->pid
)
1122 close(async_ctl
.ws_aop
->fd
[1]); /* write endpoint */
1124 /* Damn, BSD'ish SIGIO doesn't work on pipes/streams
1126 * async_io(async_ctl.ws_aop->fd[0], 1);
1129 dprintf_winsock(stddeb
, "\tasync_op = %04x (child %i)\n",
1130 handle
, async_ctl
.ws_aop
->pid
);
1135 close(async_ctl
.ws_aop
->fd
[0]); /* read endpoint */
1138 case WSMSG_ASYNC_HOSTBYADDR
:
1139 case WSMSG_ASYNC_HOSTBYNAME
:
1140 WS_do_async_gethost(pwsi
,flag
);
1141 case WSMSG_ASYNC_PROTOBYNUM
:
1142 case WSMSG_ASYNC_PROTOBYNAME
:
1143 WS_do_async_getproto(pwsi
,flag
);
1144 case WSMSG_ASYNC_SERVBYPORT
:
1145 case WSMSG_ASYNC_SERVBYNAME
:
1146 WS_do_async_getserv(pwsi
,flag
);
1148 _exit(0); /* skip atexit()'ed cleanup */
1151 WS_FREE(async_ctl
.ws_aop
);
1152 pwsi
->errno
= wsaErrno();
1153 } else pwsi
->errno
= WSAEWOULDBLOCK
;
1157 HANDLE16
WSAAsyncGetHostByAddr(HWND16 hWnd
, UINT16 uMsg
, LPCSTR addr
,
1158 INT16 len
, INT16 type
, SEGPTR sbuf
, INT16 buflen
)
1160 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1162 dprintf_winsock(stddeb
, "WS_AsyncGetHostByAddr(%08x): hwnd %04x, msg %04x, addr %08x[%i]\n",
1163 (unsigned)pwsi
, hWnd
, uMsg
, (unsigned)addr
, len
);
1166 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, addr
, len
,
1167 type
, sbuf
, buflen
, WSMSG_ASYNC_HOSTBYADDR
);
1172 HANDLE16
WSAAsyncGetHostByName(HWND16 hWnd
, UINT16 uMsg
, LPCSTR name
,
1173 SEGPTR sbuf
, INT16 buflen
)
1175 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1177 dprintf_winsock(stddeb
, "WS_AsyncGetHostByName(%08x): hwnd %04x, msg %04x, host %s, buffer %i\n",
1178 (unsigned)pwsi
, hWnd
, uMsg
, (name
)?name
:NULL_STRING
, (int)buflen
);
1181 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, name
, 0,
1182 0, sbuf
, buflen
, WSMSG_ASYNC_HOSTBYNAME
);
1187 HANDLE16
WSAAsyncGetProtoByName(HWND16 hWnd
, UINT16 uMsg
, LPCSTR name
,
1188 SEGPTR sbuf
, INT16 buflen
)
1190 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1192 dprintf_winsock(stddeb
, "WS_AsyncGetProtoByName(%08x): hwnd %04x, msg %04x, protocol %s\n",
1193 (unsigned)pwsi
, hWnd
, uMsg
, (name
)?name
:NULL_STRING
);
1196 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, name
, 0,
1197 0, sbuf
, buflen
, WSMSG_ASYNC_PROTOBYNAME
);
1202 HANDLE16
WSAAsyncGetProtoByNumber(HWND16 hWnd
, UINT16 uMsg
, INT16 number
,
1203 SEGPTR sbuf
, INT16 buflen
)
1205 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1207 dprintf_winsock(stddeb
, "WS_AsyncGetProtoByNumber(%08x): hwnd %04x, msg %04x, num %i\n",
1208 (unsigned)pwsi
, hWnd
, uMsg
, number
);
1211 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, NULL
, 0,
1212 number
, sbuf
, buflen
, WSMSG_ASYNC_PROTOBYNUM
);
1217 HANDLE16
WSAAsyncGetServByName(HWND16 hWnd
, UINT16 uMsg
, LPCSTR name
,
1218 LPCSTR proto
, SEGPTR sbuf
, INT16 buflen
)
1220 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1222 dprintf_winsock(stddeb
, "WS_AsyncGetServByName(%08x): hwnd %04x, msg %04x, name %s, proto %s\n",
1223 (unsigned)pwsi
, hWnd
, uMsg
, (name
)?name
:NULL_STRING
, (proto
)?proto
:NULL_STRING
);
1227 async_ctl
.buffer
= (char*)proto
;
1228 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, name
, 0,
1229 0, sbuf
, buflen
, WSMSG_ASYNC_SERVBYNAME
);
1235 HANDLE16
WSAAsyncGetServByPort(HWND16 hWnd
, UINT16 uMsg
, INT16 port
,
1236 LPCSTR proto
, SEGPTR sbuf
, INT16 buflen
)
1238 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1240 dprintf_winsock(stddeb
, "WS_AsyncGetServByPort(%08x): hwnd %04x, msg %04x, port %i, proto %s\n",
1241 (unsigned)pwsi
, hWnd
, uMsg
, port
, (proto
)?proto
:NULL_STRING
);
1244 return __WSAsyncDBQuery(pwsi
, hWnd
, uMsg
, proto
, 0,
1245 port
, sbuf
, buflen
, WSMSG_ASYNC_SERVBYPORT
);
1249 INT16
WSACancelAsyncRequest(HANDLE16 hAsyncTaskHandle
)
1251 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1252 ws_async_op
* p_aop
= (ws_async_op
*)WS_HANDLE2PTR(hAsyncTaskHandle
);
1254 dprintf_winsock(stddeb
, "WS_CancelAsyncRequest(%08x): handle %04x\n",
1255 (unsigned)pwsi
, hAsyncTaskHandle
);
1257 if( WINSOCK_check_async_op(p_aop
) )
1259 kill(p_aop
->pid
, SIGKILL
);
1260 waitpid(p_aop
->pid
, NULL
, 0); /* just in case */
1261 close(p_aop
->fd
[0]);
1262 WINSOCK_unlink_async_op(p_aop
);
1266 else pwsi
->errno
= WSAEINVAL
;
1267 return SOCKET_ERROR
;
1270 /* ----- asynchronous select() */
1272 int cancel_async_select(ws_socket
* pws
)
1276 kill(pws
->p_aop
->pid
, SIGKILL
);
1277 waitpid(pws
->p_aop
->pid
, NULL
, 0);
1278 WS_FREE(pws
->p_aop
);
1285 void _sigusr1_handler_parent(int sig
)
1287 /* child process puts MTYPE_CLIENT data packet into the
1288 * 'async_qid' message queue and signals us with SIGUSR1.
1289 * This handler reads the queue and posts 'uMsg' notification
1295 signal( SIGUSR1
, _sigusr1_handler_parent
);
1296 while( msgrcv(async_qid
, (struct msgbuf
*)&ipack
,
1297 MTYPE_CLIENT_SIZE
, MTYPE_CLIENT
, IPC_NOWAIT
) != -1 )
1299 if( ipack
.wParam
&& abs((short)ipack
.wParam
) < 32768 )
1301 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(ipack
.wParam
);
1302 if( pws
->p_aop
&& abs((char*)_ws_stub
- (char*)pws
->p_aop
) < 32768 )
1304 pws
->flags
&= ~(ipack
.lParam
);
1306 printf("async event - hWnd %04x, uMsg %04x [%08x]\n",
1307 pws
->p_aop
->hWnd
, pws
->p_aop
->uMsg
, ipack
.lParam
);
1309 PostMessage(pws
->p_aop
->hWnd
, pws
->p_aop
->uMsg
,
1310 (WPARAM16
)ipack
.wParam
, (LPARAM
)ipack
.lParam
);
1312 else fprintf(stderr
,"AsyncSelect:stray async_op in socket %04x!\n", ipack
.wParam
);
1314 else fprintf(stderr
,"AsyncSelect:stray socket at %04x!\n", ipack
.wParam
);
1318 int notify_client( ws_socket
* pws
, unsigned flag
)
1320 if( pws
->p_aop
&& ((pws
->p_aop
->flags
& flag
) ||
1321 (flag
== WS_FD_CONNECTED
&& pws
->flags
& WS_FD_CONNECT
)) )
1323 async_ctl
.ip
.mtype
= MTYPE_PARENT
;
1324 async_ctl
.ip
.lParam
= flag
;
1325 while( msgsnd(async_qid
, (struct msgbuf
*)&(async_ctl
.ip
),
1326 MTYPE_PARENT_SIZE
, 0) == -1 )
1328 if( errno
== EINTR
) continue;
1331 perror("AsyncSelect(parent)");
1332 cancel_async_select(pws
);
1333 pws
->flags
&= WS_FD_INTERNAL
;
1337 kill(pws
->p_aop
->pid
, SIGUSR1
);
1343 INT16
init_async_select(ws_socket
* pws
, HWND16 hWnd
, UINT16 uMsg
, UINT32 lEvent
)
1347 if( cancel_async_select(pws
) ) /* delete old async handler if any */
1348 pws
->flags
&= WS_FD_INTERNAL
;
1350 if( lEvent
== 0 ) return 0;
1352 /* setup async handler - some data may be redundant */
1354 WINSOCK_unblock_io(pws
->fd
, 1);
1355 if( (p_aop
= (ws_async_op
*)WS_ALLOC(sizeof(ws_async_op
))) )
1361 async_ctl
.lEvent
= p_aop
->flags
= lEvent
;
1362 async_ctl
.ws_sock
= pws
;
1363 async_ctl
.ip
.wParam
= (UINT16
)WS_PTR2HANDLE(pws
);
1364 async_ctl
.ip
.lParam
= 0;
1366 p_aop
->pid
= fork();
1367 if( p_aop
->pid
!= -1 )
1368 if( p_aop
->pid
== 0 ) WINSOCK_do_async_select(); /* child process */
1369 else pws
->flags
|= lEvent
;
1371 signal( SIGUSR1
, _sigusr1_handler_parent
);
1372 return 0; /* Wine process */
1374 return SOCKET_ERROR
;
1377 INT16
WSAAsyncSelect(SOCKET16 s
, HWND16 hWnd
, UINT16 uMsg
, UINT32 lEvent
)
1379 ws_socket
* pws
= (ws_socket
*)WS_HANDLE2PTR(s
);
1380 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1382 dprintf_winsock(stddeb
, "WS_AsyncSelect(%08x): %04x, hWnd %04x, uMsg %04x, event %08x\n",
1383 (unsigned)pwsi
, s
, hWnd
, uMsg
, (unsigned)lEvent
);
1384 if( _check_ws(pwsi
, pws
) )
1385 if( init_async_select(pws
, hWnd
, uMsg
, lEvent
) == 0 ) return 0;
1386 else pwsi
->errno
= WSAENOBUFS
;
1387 return SOCKET_ERROR
;
1390 /* ----- miscellaneous */
1392 INT16
__WSAFDIsSet(SOCKET16 fd
, ws_fd_set
*set
)
1394 int i
= set
->fd_count
;
1396 dprintf_winsock(stddeb
, "__WSAFDIsSet(%d,%8lx)\n",fd
,(unsigned long)set
);
1399 if (set
->fd_array
[i
] == fd
) return 1;
1403 BOOL16
WSAIsBlocking(void)
1405 /* By default WinSock should set all its sockets to non-blocking mode
1406 * and poll in PeekMessage loop when processing "blocking" ones. This
1407 * function * is supposed to tell if program is in this loop. Our
1408 * blocking calls are truly blocking so we always return FALSE.
1410 * Note: It is allowed to call this function without prior WSAStartup().
1413 dprintf_winsock(stddeb
, "WS_IsBlocking()\n");
1417 INT16
WSACancelBlockingCall(void)
1419 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1421 dprintf_winsock(stddeb
, "WS_CancelBlockingCall(%08x)\n", (unsigned)pwsi
);
1423 if( pwsi
) return 0;
1424 return SOCKET_ERROR
;
1427 FARPROC16
WSASetBlockingHook(FARPROC16 lpBlockFunc
)
1430 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1432 dprintf_winsock(stddeb
, "WS_SetBlockingHook(%08x): hook %08x\n",
1433 (unsigned)pwsi
, (unsigned) lpBlockFunc
);
1436 prev
= pwsi
->blocking_hook
;
1437 pwsi
->blocking_hook
= lpBlockFunc
;
1443 INT16
WSAUnhookBlockingHook(void)
1445 LPWSINFO pwsi
= wsi_find(GetCurrentTask());
1447 dprintf_winsock(stddeb
, "WS_UnhookBlockingHook(%08x)\n", (unsigned)pwsi
);
1448 if( pwsi
) return (INT16
)(INT32
)(pwsi
->blocking_hook
= (FARPROC16
)NULL
);
1449 return SOCKET_ERROR
;
1453 WsControl(DWORD x1
,DWORD x2
,LPDWORD x3
,LPDWORD x4
,LPDWORD x5
,LPDWORD x6
)
1455 fprintf(stdnimp
,"WsControl(%lx,%lx,%p,%p,%p,%p)\n",
1458 fprintf(stdnimp
,"WsControl(x,x,%lx,%lx,%lx,%lx)\n",
1459 x3
?*x3
:0,x4
?*x4
:0,x5
?*x5
:0,x6
?*x6
:0
1463 /* ----------------------------------- end of API stuff */
1467 /* ----------------------------------- helper functions */
1469 static int list_size(char** l
, int item_size
)
1474 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
1475 j
+= (i
+ 1) * sizeof(char*); }
1479 static int list_dup(char** l_src
, char* ref
, char* base
, int item_size
)
1481 /* base is either either equal to ref or 0 or SEGPTR */
1484 char** l_to
= (char**)ref
;
1487 for(j
=0;l_src
[j
];j
++) ;
1488 p
+= (j
+ 1) * sizeof(char*);
1490 { l_to
[i
] = base
+ (p
- ref
);
1491 k
= ( item_size
) ? item_size
: strlen(l_src
[i
]) + 1;
1492 memcpy(p
, l_src
[i
], k
); p
+= k
; }
1499 static int hostent_size(struct hostent
* p_he
)
1503 { size
= sizeof(struct hostent
);
1504 size
+= strlen(p_he
->h_name
) + 1;
1505 size
+= list_size(p_he
->h_aliases
, 0);
1506 size
+= list_size(p_he
->h_addr_list
, p_he
->h_length
); }
1510 int WS_dup_he(LPWSINFO pwsi
, struct hostent
* p_he
, int flag
)
1512 /* Duplicate hostent structure and flatten data (with its pointers)
1513 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
1514 * relative to 0 depending on "flag" value. Return data size (also
1515 * in the pwsi->buflen).
1518 int size
= hostent_size(p_he
);
1521 char* p_name
,*p_aliases
,*p_addr
,*p_base
,*p
;
1523 _check_buffer(pwsi
, size
);
1525 p_base
= (flag
& WS_DUP_OFFSET
) ? NULL
1526 : ((flag
& WS_DUP_SEGPTR
) ? (char*)SEGPTR_GET(p
) : p
);
1527 p
+= (flag
& WS_DUP_SEGPTR
) ? sizeof(struct ws_hostent
) : sizeof(struct hostent
);
1529 strcpy(p
, p_he
->h_name
); p
+= strlen(p
) + 1;
1531 p
+= list_dup(p_he
->h_aliases
, p
, p_base
+ (p
- pwsi
->buffer
), 0);
1533 list_dup(p_he
->h_addr_list
, p
, p_base
+ (p
- pwsi
->buffer
), p_he
->h_length
);
1534 if( !(flag
& WS_DUP_SEGPTR
) )
1535 { struct hostent
* p_to
= (struct hostent
*)pwsi
->buffer
;
1536 p_to
->h_addrtype
= p_he
->h_addrtype
; p_to
->h_length
= p_he
->h_length
;
1537 p_to
->h_name
= p_base
+ (p_name
- pwsi
->buffer
);
1538 p_to
->h_aliases
= (char**)(p_base
+ (p_aliases
- pwsi
->buffer
));
1539 p_to
->h_addr_list
= (char**)(p_base
+ (p_addr
- pwsi
->buffer
)); }
1541 { struct ws_hostent
* p_to
= (struct ws_hostent
*)pwsi
->buffer
;
1542 p_to
->h_addrtype
= (INT16
)p_he
->h_addrtype
;
1543 p_to
->h_length
= (INT16
)p_he
->h_length
;
1544 p_to
->h_name
= (SEGPTR
)(p_base
+ (p_name
- pwsi
->buffer
));
1545 p_to
->h_aliases
= (SEGPTR
)(p_base
+ (p_aliases
- pwsi
->buffer
));
1546 p_to
->h_addr_list
= (SEGPTR
)(p_base
+ (p_addr
- pwsi
->buffer
));
1548 return (size
+ sizeof(struct ws_hostent
) - sizeof(struct hostent
)); }
1553 void fixup_wshe(struct ws_hostent
* p_wshe
, SEGPTR base
)
1555 /* add 'base' to ws_hostent pointers to convert them from offsets */
1558 unsigned* p_aliases
,*p_addr
;
1560 p_aliases
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_aliases
);
1561 p_addr
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_addr_list
);
1562 ((unsigned)(p_wshe
->h_name
)) += (unsigned)base
;
1563 ((unsigned)(p_wshe
->h_aliases
)) += (unsigned)base
;
1564 ((unsigned)(p_wshe
->h_addr_list
)) += (unsigned)base
;
1565 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
1566 for(i
=0;p_addr
[i
];i
++) p_addr
[i
] += (unsigned)base
;
1569 /* ----- protoent */
1571 static int protoent_size(struct protoent
* p_pe
)
1575 { size
= sizeof(struct protoent
);
1576 size
+= strlen(p_pe
->p_name
) + 1;
1577 size
+= list_size(p_pe
->p_aliases
, 0); }
1581 int WS_dup_pe(LPWSINFO pwsi
, struct protoent
* p_pe
, int flag
)
1583 int size
= protoent_size(p_pe
);
1586 char* p_name
,*p_aliases
,*p_base
,*p
;
1588 _check_buffer(pwsi
, size
);
1590 p_base
= (flag
& WS_DUP_OFFSET
) ? NULL
1591 : ((flag
& WS_DUP_SEGPTR
) ? (char*)SEGPTR_GET(p
) : p
);
1592 p
+= (flag
& WS_DUP_SEGPTR
)? sizeof(struct ws_protoent
) : sizeof(struct protoent
);
1594 strcpy(p
, p_pe
->p_name
); p
+= strlen(p
) + 1;
1596 list_dup(p_pe
->p_aliases
, p
, p_base
+ (p
- pwsi
->buffer
), 0);
1597 if( !(flag
& WS_DUP_NATIVE
) )
1598 { struct protoent
* p_to
= (struct protoent
*)pwsi
->buffer
;
1599 p_to
->p_proto
= p_pe
->p_proto
;
1600 p_to
->p_name
= p_base
+ (p_name
- pwsi
->buffer
);
1601 p_to
->p_aliases
= (char**)(p_base
+ (p_aliases
- pwsi
->buffer
)); }
1603 { struct ws_protoent
* p_to
= (struct ws_protoent
*)pwsi
->buffer
;
1604 p_to
->p_proto
= (INT16
)p_pe
->p_proto
;
1605 p_to
->p_name
= (SEGPTR
)(p_base
) + (p_name
- pwsi
->buffer
);
1606 p_to
->p_aliases
= (SEGPTR
)((p_base
) + (p_aliases
- pwsi
->buffer
));
1607 return (size
+ sizeof(struct ws_protoent
) - sizeof(struct protoent
)); }
1612 void fixup_wspe(struct ws_protoent
* p_wspe
, SEGPTR base
)
1615 unsigned* p_aliases
= (unsigned*)((char*)p_wspe
+ (unsigned)p_wspe
->p_aliases
);
1616 ((unsigned)(p_wspe
->p_name
)) += (unsigned)base
;
1617 ((unsigned)(p_wspe
->p_aliases
)) += (unsigned)base
;
1618 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
1623 static int servent_size(struct servent
* p_se
)
1627 { size
+= sizeof(struct servent
);
1628 size
+= strlen(p_se
->s_proto
) + strlen(p_se
->s_name
) + 2;
1629 size
+= list_size(p_se
->s_aliases
, 0); }
1633 int WS_dup_se(LPWSINFO pwsi
, struct servent
* p_se
, int flag
)
1635 int size
= servent_size(p_se
);
1638 char* p_name
,*p_aliases
,*p_proto
,*p_base
,*p
;
1640 _check_buffer(pwsi
, size
);
1642 p_base
= (flag
& WS_DUP_OFFSET
) ? NULL
1643 : ((flag
& WS_DUP_SEGPTR
) ? (char*)SEGPTR_GET(p
) : p
);
1644 p
+= (flag
& WS_DUP_SEGPTR
)? sizeof(struct ws_servent
) : sizeof(struct servent
);
1646 strcpy(p
, p_se
->s_name
); p
+= strlen(p
) + 1;
1648 strcpy(p
, p_se
->s_proto
); p
+= strlen(p
) + 1;
1650 list_dup(p_se
->s_aliases
, p
, p_base
+ (p
- pwsi
->buffer
), 0);
1652 if( !(flag
& WS_DUP_SEGPTR
) )
1653 { struct servent
* p_to
= (struct servent
*)pwsi
->buffer
;
1654 p_to
->s_port
= p_se
->s_port
;
1655 p_to
->s_name
= p_base
+ (p_name
- pwsi
->buffer
);
1656 p_to
->s_proto
= p_base
+ (p_proto
- pwsi
->buffer
);
1657 p_to
->s_aliases
= (char**)(p_base
+ (p_aliases
- pwsi
->buffer
)); }
1659 { struct ws_servent
* p_to
= (struct ws_servent
*)pwsi
->buffer
;
1660 p_to
->s_port
= (INT16
)p_se
->s_port
;
1661 p_to
->s_name
= (SEGPTR
)(p_base
+ (p_name
- pwsi
->buffer
));
1662 p_to
->s_proto
= (SEGPTR
)(p_base
+ (p_proto
- pwsi
->buffer
));
1663 p_to
->s_aliases
= (SEGPTR
)(p_base
+ (p_aliases
- pwsi
->buffer
));
1664 return (size
+ sizeof(struct ws_servent
) - sizeof(struct servent
)); }
1669 void fixup_wsse(struct ws_servent
* p_wsse
, SEGPTR base
)
1672 unsigned* p_aliases
= (unsigned*)((char*)p_wsse
+ (unsigned)p_wsse
->s_aliases
);
1673 ((unsigned)(p_wsse
->s_name
)) += (unsigned)base
;
1674 ((p_wsse
->s_proto
)) += (unsigned)base
;
1675 ((p_wsse
->s_aliases
)) += (unsigned)base
;
1676 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
1679 /* ----------------------------------- error handling */
1681 UINT16
wsaErrno(void)
1683 int loc_errno
= errno
;
1684 #if defined(__FreeBSD__)
1685 dprintf_winsock(stderr
, "winsock: errno %d, (%s).\n",
1686 errno
, sys_errlist
[errno
]);
1688 dprintf_winsock(stderr
, "winsock: errno %d\n", errno
);
1693 case EINTR
: return WSAEINTR
;
1694 case EBADF
: return WSAEBADF
;
1695 case EACCES
: return WSAEACCES
;
1696 case EFAULT
: return WSAEFAULT
;
1697 case EINVAL
: return WSAEINVAL
;
1698 case EMFILE
: return WSAEMFILE
;
1699 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
1700 case EINPROGRESS
: return WSAEINPROGRESS
;
1701 case EALREADY
: return WSAEALREADY
;
1702 case ENOTSOCK
: return WSAENOTSOCK
;
1703 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
1704 case EMSGSIZE
: return WSAEMSGSIZE
;
1705 case EPROTOTYPE
: return WSAEPROTOTYPE
;
1706 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
1707 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
1708 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
1709 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
1710 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
1711 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
1712 case EADDRINUSE
: return WSAEADDRINUSE
;
1713 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
1714 case ENETDOWN
: return WSAENETDOWN
;
1715 case ENETUNREACH
: return WSAENETUNREACH
;
1716 case ENETRESET
: return WSAENETRESET
;
1717 case ECONNABORTED
: return WSAECONNABORTED
;
1718 case ECONNRESET
: return WSAECONNRESET
;
1719 case ENOBUFS
: return WSAENOBUFS
;
1720 case EISCONN
: return WSAEISCONN
;
1721 case ENOTCONN
: return WSAENOTCONN
;
1722 case ESHUTDOWN
: return WSAESHUTDOWN
;
1723 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
1724 case ETIMEDOUT
: return WSAETIMEDOUT
;
1725 case ECONNREFUSED
: return WSAECONNREFUSED
;
1726 case ELOOP
: return WSAELOOP
;
1727 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
1728 case EHOSTDOWN
: return WSAEHOSTDOWN
;
1729 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
1730 case ENOTEMPTY
: return WSAENOTEMPTY
;
1732 case EPROCLIM
: return WSAEPROCLIM
;
1735 case EUSERS
: return WSAEUSERS
;
1738 case EDQUOT
: return WSAEDQUOT
;
1740 case ESTALE
: return WSAESTALE
;
1741 case EREMOTE
: return WSAEREMOTE
;
1743 /* just in case we ever get here and there are no problems */
1746 fprintf(stderr
, "winsock: unknown errno %d!\n", errno
);
1747 return WSAEOPNOTSUPP
;
1751 UINT16
wsaHerrno(void)
1753 int loc_errno
= h_errno
;
1755 #if defined(__FreeBSD__)
1756 dprintf_winsock(stderr
, "winsock: h_errno %d, (%s).\n",
1757 h_errno
, sys_errlist
[h_errno
]);
1759 dprintf_winsock(stderr
, "winsock: h_errno %d.\n", h_errno
);
1761 if( debugging_winsock
) herror("wine: winsock: wsaherrno");
1767 case HOST_NOT_FOUND
: return WSAHOST_NOT_FOUND
;
1768 case TRY_AGAIN
: return WSATRY_AGAIN
;
1769 case NO_RECOVERY
: return WSANO_RECOVERY
;
1770 case NO_DATA
: return WSANO_DATA
;
1774 fprintf(stderr
, "winsock: unknown h_errno %d!\n", h_errno
);
1775 return WSAEOPNOTSUPP
;