Added SHDoDragDrop() stub.
[wine/multimedia.git] / misc / winsock.c
blob70d800ebc01bb37b341f9cb8c3fe68c9b0196ce3
1 /*
2 * based on Windows Sockets 1.1 specs
3 * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
4 *
5 * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
7 * NOTE: If you make any changes to fix a particular app, make sure
8 * they don't break something else like Netscape or telnet and ftp
9 * clients and servers (www.winsite.com got a lot of those).
13 #include "config.h"
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/ipc.h>
18 #include <sys/ioctl.h>
19 #ifdef HAVE_SYS_FILIO_H
20 # include <sys/filio.h>
21 #endif
22 #if defined(__svr4__) || defined(__sun)
23 #include <sys/ioccom.h>
24 #include <sys/sockio.h>
25 #endif
27 #if defined(__EMX__)
28 # include <sys/so_ioctl.h>
29 #endif
31 #ifdef HAVE_SYS_PARAM_H
32 # include <sys/param.h>
33 #endif
35 #include <sys/msg.h>
36 #include <sys/wait.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <ctype.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <sys/errno.h>
44 #include <netdb.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #ifdef HAVE_ARPA_NAMESER_H
48 # include <arpa/nameser.h>
49 #endif
50 #ifdef HAVE_RESOLV_H
51 # include <resolv.h>
52 #endif
54 #include "wine/winbase16.h"
55 #include "winsock.h"
56 #include "winnt.h"
57 #include "heap.h"
58 #include "task.h"
59 #include "message.h"
60 #include "miscemu.h"
61 #include "debug.h"
63 DEFAULT_DEBUG_CHANNEL(winsock)
65 #define DEBUG_SOCKADDR 0
66 #define dump_sockaddr(a) \
67 DUMP("sockaddr_in: family %d, address %s, port %d\n", \
68 ((struct sockaddr_in *)a)->sin_family, \
69 inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
70 ntohs(((struct sockaddr_in *)a)->sin_port))
72 /* ----------------------------------- internal data */
74 static HANDLE _WSHeap = 0;
75 static unsigned char* _ws_stub = NULL;
76 static LPWSINFO _wsi_list = NULL;
78 #define WS_ALLOC(size) \
79 HeapAlloc(_WSHeap, HEAP_ZERO_MEMORY, (size) )
80 #define WS_FREE(ptr) \
81 HeapFree(_WSHeap, 0, (ptr) )
83 #define WS_PTR2HANDLE(ptr) \
84 ((short)((int)(ptr) - (int)_ws_stub))
85 #define WS_HANDLE2PTR(handle) \
86 ((unsigned)((int)_ws_stub + (int)(handle)))
88 #define WSI_CHECK_RANGE(pwsi, pws) \
89 ( ((unsigned)(pws) > (unsigned)(pwsi)) && \
90 ((unsigned)(pws) < ((unsigned)(pwsi) + sizeof(WSINFO))) )
92 static INT _ws_sock_ops[] =
93 { WS_SO_DEBUG, WS_SO_REUSEADDR, WS_SO_KEEPALIVE, WS_SO_DONTROUTE,
94 WS_SO_BROADCAST, WS_SO_LINGER, WS_SO_OOBINLINE, WS_SO_SNDBUF,
95 WS_SO_RCVBUF, WS_SO_ERROR, WS_SO_TYPE, WS_SO_DONTLINGER,
96 #ifdef SO_RCVTIMEO
97 WS_SO_RCVTIMEO,
98 #endif
99 0 };
100 static int _px_sock_ops[] =
101 { SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST,
102 SO_LINGER, SO_OOBINLINE, SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE,
103 SO_LINGER,
104 #ifdef SO_RCVTIMEO
105 SO_RCVTIMEO,
106 #endif
109 static INT _ws_tcp_ops[] = {
110 #ifdef TCP_NODELAY
111 WS_TCP_NODELAY,
112 #endif
115 static int _px_tcp_ops[] = {
116 #ifdef TCP_NODELAY
117 TCP_NODELAY,
118 #endif
122 static int _check_ws(LPWSINFO pwsi, ws_socket* pws);
123 static char* _check_buffer(LPWSINFO pwsi, int size);
125 /* ----------------------------------- non-blocking I/O */
127 static int WINSOCK_unblock_io(int fd, int noblock)
129 int fd_flags;
131 fd_flags = fcntl(fd, F_GETFL, 0);
132 if (fcntl(fd, F_SETFL, (noblock)? fd_flags | O_NONBLOCK
133 : fd_flags & ~O_NONBLOCK ) != -1) return 0;
134 return -1;
137 /***********************************************************************
138 * convert_sockopt()
140 * Converts socket flags from Windows format.
142 static void convert_sockopt(INT *level, INT *optname)
144 int i;
145 switch (*level)
147 case WS_SOL_SOCKET:
148 *level = SOL_SOCKET;
149 for(i=0; _ws_sock_ops[i]; i++)
150 if( _ws_sock_ops[i] == *optname ) break;
151 if( _ws_sock_ops[i] ) *optname = _px_sock_ops[i];
152 else FIXME(winsock, "Unknown SOL_SOCKET optname %d\n", *optname);
153 break;
154 case WS_IPPROTO_TCP:
155 *level = IPPROTO_TCP;
156 for(i=0; _ws_tcp_ops[i]; i++)
157 if ( _ws_tcp_ops[i] == *optname ) break;
158 if( _ws_tcp_ops[i] ) *optname = _px_tcp_ops[i];
159 else FIXME(winsock, "Unknown IPPROTO_TCP optname %d\n", *optname);
160 break;
164 /* ----------------------------------- Per-thread info (or per-process?) */
166 static LPWSINFO wsi_find(HTASK16 hTask)
168 TDB* pTask = (TDB*)GlobalLock16(hTask);
169 if( pTask )
171 if( pTask->pwsi ) return pTask->pwsi;
172 else
174 LPWSINFO pwsi = _wsi_list;
175 while( pwsi && pwsi->tid != hTask ) pwsi = pwsi->next;
176 if( pwsi )
177 WARN(winsock,"(pwsi=0x%08x,task=0x%04x):Loose wsi struct! \n",
178 (unsigned)pwsi, hTask );
179 return pwsi;
182 return NULL;
185 static ws_socket* wsi_alloc_socket(LPWSINFO pwsi, int fd)
187 /* Initialize a new entry in the socket table */
189 if( pwsi->last_free >= 0 )
191 int i = pwsi->last_free;
193 pwsi->last_free = pwsi->sock[i].flags; /* free list */
194 pwsi->sock[i].fd = fd;
195 pwsi->sock[i].flags = 0;
196 return &pwsi->sock[i];
198 return NULL;
201 static int wsi_strtolo(LPWSINFO pwsi, const char* name, const char* opt)
203 /* Stuff a lowercase copy of the string into the local buffer */
205 int i = strlen(name) + 2;
206 char* p = _check_buffer(pwsi, i + ((opt)?strlen(opt):0));
208 if( p )
210 do *p++ = tolower(*name); while(*name++);
211 i = (p - (char*)(pwsi->buffer));
212 if( opt ) do *p++ = tolower(*opt); while(*opt++);
213 return i;
215 return 0;
218 static fd_set* fd_set_import( fd_set* fds, LPWSINFO pwsi, void* wsfds, int* highfd, BOOL b32 )
220 /* translate Winsock fd set into local fd set */
222 if( wsfds )
224 #define wsfds16 ((ws_fd_set16*)wsfds)
225 #define wsfds32 ((ws_fd_set32*)wsfds)
226 ws_socket* pws;
227 int i, count;
229 FD_ZERO(fds);
230 count = b32 ? wsfds32->fd_count : wsfds16->fd_count;
232 for( i = 0; i < count; i++ )
234 pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
235 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
236 if( _check_ws(pwsi, pws) )
238 if( pws->fd > *highfd ) *highfd = pws->fd;
239 FD_SET(pws->fd, fds);
242 #undef wsfds32
243 #undef wsfds16
244 return fds;
246 return NULL;
249 inline static int sock_error_p(int s)
251 unsigned int optval, optlen;
253 optlen = sizeof(optval);
254 getsockopt(s, SOL_SOCKET, SO_ERROR, &optval, &optlen);
255 if (optval) WARN(winsock, "\t[%i] error: %d\n", s, optval);
256 return optval != 0;
259 static int fd_set_export( LPWSINFO pwsi, fd_set* fds, fd_set* exceptfds, void* wsfds, BOOL b32 )
261 int num_err = 0;
263 /* translate local fd set into Winsock fd set, adding
264 * errors to exceptfds (only if app requested it) */
266 if( wsfds )
268 #define wsfds16 ((ws_fd_set16*)wsfds)
269 #define wsfds32 ((ws_fd_set32*)wsfds)
270 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
272 for( i = 0, j = 0; i < count; i++ )
274 ws_socket *pws = (b32) ? (ws_socket*)WS_HANDLE2PTR(wsfds32->fd_array[i])
275 : (ws_socket*)WS_HANDLE2PTR(wsfds16->fd_array[i]);
276 if( _check_ws(pwsi, pws) )
278 int fd = pws->fd;
280 if( FD_ISSET(fd, fds) )
282 if ( exceptfds && sock_error_p(fd) )
284 FD_SET(fd, exceptfds);
285 num_err++;
287 else if( b32 )
288 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
289 else
290 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
295 if( b32 ) wsfds32->fd_count = j;
296 else wsfds16->fd_count = j;
298 TRACE(winsock, "\n");
299 #undef wsfds32
300 #undef wsfds16
302 return num_err;
305 HANDLE16 __ws_gethandle( void* ptr )
307 return (HANDLE16)WS_PTR2HANDLE(ptr);
310 void* __ws_memalloc( int size )
312 return WS_ALLOC(size);
315 void __ws_memfree(void* ptr)
317 WS_FREE(ptr);
321 * Event handling helper routines
323 * FIXME: This is all a hack; winsock event handling should be moved
324 * to the services thread ...
327 #define EVENT_IO_READ 0
328 #define EVENT_IO_WRITE 1
329 #define EVENT_IO_EXCEPT 2
331 static fd_set __winsock_io_set[3];
332 static int __winsock_max_fd = 0;
333 static int __wakeup_pipe[2];
335 static CRITICAL_SECTION __winsock_crst;
336 static HANDLE __winsock_thread = INVALID_HANDLE_VALUE;
338 BOOL WINSOCK_HandleIO( int* max_fd, int num_pending,
339 fd_set pending_set[3], fd_set event_set[3] );
341 /***********************************************************************
342 * WINSOCK_Init
344 * Initialize network IO.
346 BOOL WINSOCK_Init(void)
348 int i;
349 for( i = 0; i < 3; i++ )
350 FD_ZERO( __winsock_io_set + i );
352 /* pipe used to wake up the winsock thread */
353 pipe(__wakeup_pipe);
355 /* make the pipe non-blocking */
356 fcntl(__wakeup_pipe[0], F_SETFL, O_NONBLOCK);
357 fcntl(__wakeup_pipe[1], F_SETFL, O_NONBLOCK);
359 FD_SET( __wakeup_pipe[0], &__winsock_io_set[EVENT_IO_READ] );
360 __winsock_max_fd = __wakeup_pipe[0];
361 __winsock_max_fd++;
363 /* Inititalize critical section */
364 InitializeCriticalSection( &__winsock_crst );
365 MakeCriticalSectionGlobal( &__winsock_crst );
367 return TRUE;
370 /***********************************************************************
371 * WINSOCK_Shutdown
373 void WINSOCK_Shutdown()
375 /* Called on exit(), has to remove all outstanding async DNS processes. */
377 if ( __winsock_thread != INVALID_HANDLE_VALUE )
379 TerminateThread( __winsock_thread, 0 );
380 __winsock_thread = INVALID_HANDLE_VALUE;
384 /***********************************************************************
385 * WINSOCK_Thread
387 static DWORD CALLBACK WINSOCK_Thread( LPVOID arg )
389 while ( TRUE )
391 int num_pending, max_fd;
392 fd_set io_set[3];
394 EnterCriticalSection( &__winsock_crst );
395 memcpy( io_set, __winsock_io_set, sizeof(io_set) );
396 max_fd = __winsock_max_fd;
397 LeaveCriticalSection( &__winsock_crst );
399 num_pending = select( max_fd, &io_set[EVENT_IO_READ],
400 &io_set[EVENT_IO_WRITE],
401 &io_set[EVENT_IO_EXCEPT], NULL );
403 if ( num_pending == -1 )
405 /* Error - signal, invalid arguments, out of memory */
406 continue;
409 /* Flush the wake-up pipe */
410 if ( FD_ISSET( __wakeup_pipe[0], &io_set[EVENT_IO_READ] ) )
412 char tmpBuf[10];
413 ssize_t ret;
415 while ( (ret = read(__wakeup_pipe[0], &tmpBuf, 10)) == 10 );
416 num_pending--;
419 /* Handle actual IO */
420 if ( num_pending > 0 )
422 EnterCriticalSection( &__winsock_crst );
423 WINSOCK_HandleIO( &__winsock_max_fd, num_pending, io_set, __winsock_io_set );
424 LeaveCriticalSection( &__winsock_crst );
429 /***********************************************************************
430 * WINSOCK_WakeUp
432 * Wake up the winsock thread.
434 static void WINSOCK_WakeUp(void)
436 if ( __winsock_thread == INVALID_HANDLE_VALUE )
438 __winsock_thread = CreateThread( NULL, 0, WINSOCK_Thread, NULL, 0, NULL );
439 __winsock_thread = ConvertToGlobalHandle( __winsock_thread );
442 if (write (__wakeup_pipe[1], "A", 1) != 1)
443 ERR(winsock, "unable to write in wakeup_pipe\n");
446 /***********************************************************************
447 * EVENT_AddIO
449 static void EVENT_AddIO(int fd, unsigned io_type)
451 EnterCriticalSection( &__winsock_crst );
452 FD_SET( fd, &__winsock_io_set[io_type] );
453 if( __winsock_max_fd <= fd ) __winsock_max_fd = fd + 1;
454 LeaveCriticalSection( &__winsock_crst );
456 WINSOCK_WakeUp();
459 /***********************************************************************
460 * EVENT_DeleteIO
462 static void EVENT_DeleteIO(int fd, unsigned io_type)
464 EnterCriticalSection( &__winsock_crst );
465 FD_CLR( fd, &__winsock_io_set[io_type] );
466 LeaveCriticalSection( &__winsock_crst );
468 WINSOCK_WakeUp();
472 /* ----------------------------------- API -----
474 * Init / cleanup / error checking.
477 /***********************************************************************
478 * WSAStartup16() (WINSOCK.115)
480 * Create socket control struct, attach it to the global list and
481 * update a pointer in the task struct.
483 INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA lpWSAData)
485 WSADATA WINSOCK_data = { 0x0101, 0x0101,
486 "WINE Sockets 1.1",
487 #ifdef linux
488 "Linux/i386",
489 #elif defined(__NetBSD__)
490 "NetBSD/i386",
491 #elif defined(sunos)
492 "SunOS",
493 #elif defined(__FreeBSD__)
494 "FreeBSD",
495 #elif defined(__OpenBSD__)
496 "OpenBSD/i386",
497 #else
498 "Unknown",
499 #endif
500 WS_MAX_SOCKETS_PER_PROCESS,
501 WS_MAX_UDP_DATAGRAM, (SEGPTR)NULL };
502 HTASK16 tid = GetCurrentTask();
503 LPWSINFO pwsi;
505 TRACE(winsock, "verReq=%x\n", wVersionRequested);
507 if (LOBYTE(wVersionRequested) < 1 || (LOBYTE(wVersionRequested) == 1 &&
508 HIBYTE(wVersionRequested) < 1)) return WSAVERNOTSUPPORTED;
510 if (!lpWSAData) return WSAEINVAL;
512 /* initialize socket heap */
514 if( !_ws_stub )
516 _WSHeap = HeapCreate(HEAP_ZERO_MEMORY, 8120, 32768);
517 if( !(_ws_stub = WS_ALLOC(0x10)) )
519 ERR(winsock,"Fatal: failed to create WinSock heap\n");
520 return 0;
523 if( _WSHeap == 0 ) return WSASYSNOTREADY;
525 /* create socket array for this task */
527 pwsi = wsi_find(GetCurrentTask());
528 if( pwsi == NULL )
530 TDB* pTask = (TDB*)GlobalLock16( tid );
532 if( (pwsi = (LPWSINFO)WS_ALLOC( sizeof(WSINFO))) )
534 int i = 0;
535 pwsi->tid = tid;
536 for( i = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++ )
538 pwsi->sock[i].fd = -1;
539 pwsi->sock[i].flags = i + 1;
541 pwsi->sock[WS_MAX_SOCKETS_PER_PROCESS - 1].flags = -1;
543 else return WSASYSNOTREADY;
545 /* add this control struct to the global list */
547 pwsi->prev = NULL;
548 if( _wsi_list )
549 _wsi_list->prev = pwsi;
550 pwsi->next = _wsi_list;
551 _wsi_list = pwsi;
552 pTask->pwsi = pwsi;
554 else pwsi->num_startup++;
556 /* return winsock information */
558 memcpy(lpWSAData, &WINSOCK_data, sizeof(WINSOCK_data));
560 TRACE(winsock, "succeeded\n");
561 return 0;
564 /***********************************************************************
565 * WSAStartup32() (WSOCK32.115)
567 INT WINAPI WSAStartup(UINT wVersionRequested, LPWSADATA lpWSAData)
569 return WSAStartup16( wVersionRequested, lpWSAData );
572 /***********************************************************************
573 * WSACleanup() (WINSOCK.116)
575 * Cleanup functions of varying impact.
578 INT WINSOCK_DeleteTaskWSI( TDB* pTask, LPWSINFO pwsi )
580 /* WSACleanup() backend, called on task termination as well.
581 * Real DLL would have registered its own signal handler with
582 * TaskSetSignalHandler() and waited until USIG_TERMINATION/USIG_GPF
583 * but this scheme is much more straightforward.
586 int i, j, n;
588 if( --pwsi->num_startup > 0 ) return 0;
590 /* unlink socket control struct */
592 if( pwsi == _wsi_list )
593 _wsi_list = pwsi->next;
594 else
595 pwsi->prev->next = pwsi->next;
596 if( pwsi->next ) pwsi->next->prev = pwsi->prev;
598 if( _wsi_list == NULL )
599 WINSOCK_Shutdown(); /* just in case */
601 if( pwsi->flags & WSI_BLOCKINGCALL )
602 TRACE(winsock,"\tinside blocking call!\n");
604 /* FIXME: aop_control() doesn't decrement pwsi->num_async_rq
606 * if( pwsi->num_async_rq )
607 * WARN(winsock,"\thave %i outstanding async ops!\n", pwsi->num_async_rq );
610 for(i = 0, j = 0, n = 0; i < WS_MAX_SOCKETS_PER_PROCESS; i++)
611 if( pwsi->sock[i].fd != -1 )
613 if( pwsi->sock[i].psop )
615 n++;
616 WSAAsyncSelect( (SOCKET16)WS_PTR2HANDLE(pwsi->sock + i), 0, 0, 0 );
618 close(pwsi->sock[i].fd); j++;
620 if( j )
621 TRACE(winsock,"\tclosed %i sockets, killed %i async selects!\n", j, n);
623 /* delete scratch buffers */
625 if( pwsi->buffer ) SEGPTR_FREE(pwsi->buffer);
626 if( pwsi->dbuffer ) SEGPTR_FREE(pwsi->dbuffer);
628 if( pTask )
629 pTask->pwsi = NULL;
630 memset( pwsi, 0, sizeof(WSINFO) );
631 WS_FREE(pwsi);
632 return 0;
635 INT WINAPI WSACleanup(void)
637 HTASK16 hTask = GetCurrentTask();
639 TRACE(winsock, "(%04x)\n", hTask );
640 if( hTask )
642 LPWSINFO pwsi = wsi_find(hTask);
643 if( pwsi )
644 return WINSOCK_DeleteTaskWSI( (TDB*)GlobalLock16(hTask), pwsi );
645 return SOCKET_ERROR;
647 else
648 WINSOCK_Shutdown(); /* remove all outstanding DNS requests */
649 return 0;
653 /***********************************************************************
654 * WSAGetLastError() (WSOCK32.111)(WINSOCK.111)
656 INT WINAPI WSAGetLastError(void)
658 return GetLastError();
661 /***********************************************************************
662 * WSASetLastError32() (WSOCK32.112)
664 void WINAPI WSASetLastError(INT iError) {
665 SetLastError(iError);
668 /***********************************************************************
669 * WSASetLastError16() (WINSOCK.112)
671 void WINAPI WSASetLastError16(INT16 iError)
673 WSASetLastError(iError);
676 int _check_ws(LPWSINFO pwsi, ws_socket* pws)
678 if( pwsi )
680 if( pwsi->flags & WSI_BLOCKINGCALL ) SetLastError(WSAEINPROGRESS);
681 else if( WSI_CHECK_RANGE(pwsi, pws) ) return 1;
682 else SetLastError(WSAENOTSOCK);
684 return 0;
687 char* _check_buffer(LPWSINFO pwsi, int size)
689 if( pwsi->buffer && pwsi->buflen >= size ) return pwsi->buffer;
690 else SEGPTR_FREE(pwsi->buffer);
692 pwsi->buffer = (char*)SEGPTR_ALLOC((pwsi->buflen = size));
693 return pwsi->buffer;
696 struct ws_hostent* _check_buffer_he(LPWSINFO pwsi, int size)
698 if( pwsi->he && pwsi->helen >= size ) return pwsi->he;
699 else SEGPTR_FREE(pwsi->he);
701 pwsi->he = (struct ws_hostent*)SEGPTR_ALLOC((pwsi->helen = size));
702 return pwsi->he;
705 struct ws_servent* _check_buffer_se(LPWSINFO pwsi, int size)
707 if( pwsi->se && pwsi->selen >= size ) return pwsi->se;
708 else SEGPTR_FREE(pwsi->se);
710 pwsi->se = (struct ws_servent*)SEGPTR_ALLOC((pwsi->selen = size));
711 return pwsi->se;
714 struct ws_protoent* _check_buffer_pe(LPWSINFO pwsi, int size)
716 if( pwsi->pe && pwsi->pelen >= size ) return pwsi->pe;
717 else SEGPTR_FREE(pwsi->pe);
719 pwsi->pe = (struct ws_protoent*)SEGPTR_ALLOC((pwsi->pelen = size));
720 return pwsi->pe;
723 /* ----------------------------------- i/o APIs */
725 /***********************************************************************
726 * accept() (WSOCK32.1)
728 SOCKET WINAPI WINSOCK_accept(SOCKET s, struct sockaddr *addr,
729 INT *addrlen32)
731 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR((SOCKET16)s);
732 LPWSINFO pwsi = wsi_find(GetCurrentTask());
733 #ifdef HAVE_IPX
734 struct ws_sockaddr_ipx* addr2 = (struct ws_sockaddr_ipx *)addr;
735 #endif
737 TRACE(winsock, "(%08x): socket %04x\n",
738 (unsigned)pwsi, (UINT16)s );
739 if( _check_ws(pwsi, pws) )
741 int sock, fd_flags;
743 fd_flags = fcntl(pws->fd, F_GETFL, 0);
745 if( (sock = accept(pws->fd, addr, addrlen32)) >= 0 )
747 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
748 if( pnew )
750 s = (SOCKET)WS_PTR2HANDLE(pnew);
751 if( pws->psop && pws->flags & WS_FD_ACCEPT )
753 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
755 /* async select the accept()'ed socket */
756 WSAAsyncSelect( s, pws->psop->hWnd, pws->psop->uMsg,
757 pws->flags & ~WS_FD_ACCEPT );
759 #ifdef HAVE_IPX
760 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
761 addr = (struct sockaddr *)
762 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
763 memcpy(addr, addr2,
764 addrlen32 ? *addrlen32 : sizeof(*addr2));
765 addr2->sipx_family = WS_AF_IPX;
766 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
767 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
768 memcpy(addr2->sipx_node,
769 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
770 free(addr);
772 #endif
773 return s;
775 else SetLastError(WSAENOBUFS);
776 } else SetLastError(wsaErrno());
778 #ifdef HAVE_IPX
779 if (addr && ((struct sockaddr_ipx *)addr)->sipx_family == AF_IPX) {
780 addr = (struct sockaddr *)
781 malloc(addrlen32 ? *addrlen32 : sizeof(*addr2));
782 memcpy(addr, addr2, addrlen32 ? *addrlen32 : sizeof(*addr2));
783 addr2->sipx_family = WS_AF_IPX;
784 addr2->sipx_network = ((struct sockaddr_ipx *)addr)->sipx_network;
785 addr2->sipx_port = ((struct sockaddr_ipx *)addr)->sipx_port;
786 memcpy(addr2->sipx_node,
787 ((struct sockaddr_ipx *)addr)->sipx_node, IPX_NODE_LEN);
788 free(addr);
790 #endif
791 return INVALID_SOCKET;
794 /***********************************************************************
795 * accept() (WINSOCK.1)
797 SOCKET16 WINAPI WINSOCK_accept16(SOCKET16 s, struct sockaddr* addr,
798 INT16* addrlen16 )
800 INT addrlen32 = addrlen16 ? *addrlen16 : 0;
801 SOCKET retSocket = WINSOCK_accept( s, addr, &addrlen32 );
802 if( addrlen16 ) *addrlen16 = (INT16)addrlen32;
803 return (SOCKET16)retSocket;
806 /***********************************************************************
807 * bind() (WSOCK32.2)
809 INT WINAPI WINSOCK_bind(SOCKET s, struct sockaddr *name, INT namelen)
811 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
812 LPWSINFO pwsi = wsi_find(GetCurrentTask());
813 #ifdef HAVE_IPX
814 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
815 #endif
817 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
818 (unsigned)pwsi, s, (int) name, namelen);
819 #if DEBUG_SOCKADDR
820 dump_sockaddr(name);
821 #endif
823 if ( _check_ws(pwsi, pws) )
825 /* FIXME: what family does this really map to on the Unix side? */
826 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
827 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
828 #ifdef HAVE_IPX
829 else if (name &&
830 ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
832 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
833 memset(name, '\0', sizeof(struct sockaddr_ipx));
834 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
835 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
836 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
837 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
838 name2->sipx_node, IPX_NODE_LEN);
839 namelen = sizeof(struct sockaddr_ipx);
841 #endif
842 if ( namelen >= sizeof(*name) )
844 if ( name && (((struct ws_sockaddr_in *)name)->sin_family == AF_INET
845 #ifdef HAVE_IPX
846 || ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX
847 #endif
850 if ( bind(pws->fd, name, namelen) < 0 )
852 int loc_errno = errno;
853 WARN(winsock, "\tfailure - errno = %i\n", errno);
854 errno = loc_errno;
855 switch(errno)
857 case EBADF: SetLastError(WSAENOTSOCK); break;
858 case EADDRNOTAVAIL: SetLastError(WSAEINVAL); break;
859 default: SetLastError(wsaErrno());break;
862 else {
863 #ifdef HAVE_IPX
864 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
865 free(name);
866 #endif
867 return 0; /* success */
869 } else SetLastError(WSAEAFNOSUPPORT);
870 } else SetLastError(WSAEFAULT);
871 #ifdef HAVE_IPX
872 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
873 free(name);
874 #endif
876 return SOCKET_ERROR;
879 /***********************************************************************
880 * bind() (WINSOCK.2)
882 INT16 WINAPI WINSOCK_bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
884 return (INT16)WINSOCK_bind( s, name, namelen );
887 /***********************************************************************
888 * closesocket() (WSOCK32.3)
890 INT WINAPI WINSOCK_closesocket(SOCKET s)
892 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
893 LPWSINFO pwsi = wsi_find(GetCurrentTask());
895 TRACE(winsock, "(%08x): socket %08x\n", (unsigned)pwsi, s);
897 if( _check_ws(pwsi, pws) )
899 int fd = pws->fd;
901 if( pws->psop ) WSAAsyncSelect( s, 0, 0, 0 );
903 pws->fd = -1;
904 pws->flags = (unsigned)pwsi->last_free;
905 pwsi->last_free = pws - &pwsi->sock[0]; /* add to free list */
907 if( close(fd) == 0 )
908 return 0;
909 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
911 return SOCKET_ERROR;
914 /***********************************************************************
915 * closesocket() (WINSOCK.3)
917 INT16 WINAPI WINSOCK_closesocket16(SOCKET16 s)
919 return (INT16)WINSOCK_closesocket(s);
922 /***********************************************************************
923 * connect() (WSOCK32.4)
925 INT WINAPI WINSOCK_connect(SOCKET s, struct sockaddr *name, INT namelen)
927 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
928 LPWSINFO pwsi = wsi_find(GetCurrentTask());
929 #ifdef HAVE_IPX
930 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
931 #endif
933 TRACE(winsock, "(%08x): socket %04x, ptr %8x, length %d\n",
934 (unsigned)pwsi, s, (int) name, namelen);
935 #if DEBUG_SOCKADDR
936 dump_sockaddr(name);
937 #endif
939 if( _check_ws(pwsi, pws) )
941 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_PUP)
942 ((struct ws_sockaddr_ipx *)name)->sipx_family = AF_UNSPEC;
943 #ifdef HAVE_IPX
944 else if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == WS_AF_IPX)
946 name = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
947 memset(name, '\0', sizeof(struct sockaddr_ipx));
948 ((struct sockaddr_ipx *)name)->sipx_family = AF_IPX;
949 ((struct sockaddr_ipx *)name)->sipx_port = name2->sipx_port;
950 ((struct sockaddr_ipx *)name)->sipx_network = name2->sipx_network;
951 memcpy(((struct sockaddr_ipx *)name)->sipx_node,
952 name2->sipx_node, IPX_NODE_LEN);
953 namelen = sizeof(struct sockaddr_ipx);
955 #endif
956 if (connect(pws->fd, name, namelen) == 0)
958 if( pws->psop && (pws->flags & WS_FD_CONNECT) )
960 /* application did AsyncSelect() but then went
961 * ahead and called connect() without waiting for
962 * notification.
964 * FIXME: Do we have to post a notification message
965 * in this case?
968 if( !(pws->flags & WS_FD_CONNECTED) )
970 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
971 EVENT_AddIO( pws->fd, EVENT_IO_READ );
972 else
973 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
974 if( pws->flags & WS_FD_WRITE )
975 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
976 else
977 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
980 pws->flags |= WS_FD_CONNECTED;
981 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_LISTENING);
982 #ifdef HAVE_IPX
983 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
984 free(name);
985 #endif
986 return 0;
988 SetLastError((errno == EINPROGRESS) ? WSAEWOULDBLOCK : wsaErrno());
990 #ifdef HAVE_IPX
991 if (name && ((struct sockaddr_ipx *)name)->sipx_family == AF_IPX)
992 free(name);
993 #endif
994 return SOCKET_ERROR;
997 /***********************************************************************
998 * connect() (WINSOCK.4)
1000 INT16 WINAPI WINSOCK_connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
1002 return (INT16)WINSOCK_connect( s, name, namelen );
1005 /***********************************************************************
1006 * getpeername() (WSOCK32.5)
1008 INT WINAPI WINSOCK_getpeername(SOCKET s, struct sockaddr *name,
1009 INT *namelen)
1011 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1012 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1013 #ifdef HAVE_IPX
1014 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
1015 #endif
1017 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
1018 (unsigned)pwsi, s, (int) name, *namelen);
1019 if( _check_ws(pwsi, pws) )
1021 if (getpeername(pws->fd, name, namelen) == 0) {
1022 #ifdef HAVE_IPX
1023 if (((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1024 name = (struct sockaddr *)
1025 malloc(namelen ? *namelen : sizeof(*name2));
1026 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1027 name2->sipx_family = WS_AF_IPX;
1028 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1029 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1030 memcpy(name2->sipx_node,
1031 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1032 free(name);
1034 #endif
1035 return 0;
1037 SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1039 #ifdef HAVE_IPX
1040 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1041 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
1042 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1043 name2->sipx_family = WS_AF_IPX;
1044 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1045 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1046 memcpy(name2->sipx_node,
1047 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1048 free(name);
1050 #endif
1051 return SOCKET_ERROR;
1054 /***********************************************************************
1055 * getpeername() (WINSOCK.5)
1057 INT16 WINAPI WINSOCK_getpeername16(SOCKET16 s, struct sockaddr *name,
1058 INT16 *namelen16)
1060 INT namelen32 = *namelen16;
1061 INT retVal = WINSOCK_getpeername( s, name, &namelen32 );
1063 #if DEBUG_SOCKADDR
1064 dump_sockaddr(name);
1065 #endif
1067 *namelen16 = namelen32;
1068 return (INT16)retVal;
1071 /***********************************************************************
1072 * getsockname() (WSOCK32.6)
1074 INT WINAPI WINSOCK_getsockname(SOCKET s, struct sockaddr *name,
1075 INT *namelen)
1077 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1078 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1079 #ifdef HAVE_IPX
1080 struct ws_sockaddr_ipx* name2 = (struct ws_sockaddr_ipx *)name;
1081 #endif
1083 TRACE(winsock, "(%08x): socket: %04x, ptr %8x, ptr %8x\n",
1084 (unsigned)pwsi, s, (int) name, (int) *namelen);
1085 if( _check_ws(pwsi, pws) )
1087 if (getsockname(pws->fd, name, namelen) == 0) {
1088 #ifdef HAVE_IPX
1089 if (((struct sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1090 name = (struct sockaddr *)
1091 malloc(namelen ? *namelen : sizeof(*name2));
1092 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1093 name2->sipx_family = WS_AF_IPX;
1094 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1095 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1096 memcpy(name2->sipx_node,
1097 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1098 free(name);
1100 #endif
1101 return 0;
1103 SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1105 #ifdef HAVE_IPX
1106 if (name && ((struct ws_sockaddr_ipx *)name)->sipx_family == AF_IPX) {
1107 name = (struct sockaddr *) malloc(namelen ? *namelen : sizeof(*name2));
1108 memcpy(name, name2, namelen ? *namelen : sizeof(*name2));
1109 name2->sipx_family = WS_AF_IPX;
1110 name2->sipx_network = ((struct sockaddr_ipx *)name)->sipx_network;
1111 name2->sipx_port = ((struct sockaddr_ipx *)name)->sipx_port;
1112 memcpy(name2->sipx_node,
1113 ((struct sockaddr_ipx *)name)->sipx_node, IPX_NODE_LEN);
1114 free(name);
1116 #endif
1117 return SOCKET_ERROR;
1120 /***********************************************************************
1121 * getsockname() (WINSOCK.6)
1123 INT16 WINAPI WINSOCK_getsockname16(SOCKET16 s, struct sockaddr *name,
1124 INT16 *namelen16)
1126 INT retVal;
1128 if( namelen16 )
1130 INT namelen32 = *namelen16;
1131 retVal = WINSOCK_getsockname( s, name, &namelen32 );
1132 *namelen16 = namelen32;
1134 #if DEBUG_SOCKADDR
1135 dump_sockaddr(name);
1136 #endif
1139 else retVal = SOCKET_ERROR;
1140 return (INT16)retVal;
1144 /***********************************************************************
1145 * getsockopt() (WSOCK32.7)
1147 INT WINAPI WINSOCK_getsockopt(SOCKET s, INT level,
1148 INT optname, char *optval, INT *optlen)
1150 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1151 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1153 TRACE(winsock, "(%08x): socket: %04x, opt %d, ptr %8x, ptr %8x\n",
1154 (unsigned)pwsi, s, level, (int) optval, (int) *optlen);
1155 if( _check_ws(pwsi, pws) )
1157 convert_sockopt(&level, &optname);
1158 if (getsockopt(pws->fd, (int) level, optname, optval, optlen) == 0 )
1159 return 0;
1160 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1162 return SOCKET_ERROR;
1165 /***********************************************************************
1166 * getsockopt() (WINSOCK.7)
1168 INT16 WINAPI WINSOCK_getsockopt16(SOCKET16 s, INT16 level,
1169 INT16 optname, char *optval, INT16 *optlen)
1171 INT optlen32;
1172 INT *p = &optlen32;
1173 INT retVal;
1174 if( optlen ) optlen32 = *optlen; else p = NULL;
1175 retVal = WINSOCK_getsockopt( s, (UINT16)level, optname, optval, p );
1176 if( optlen ) *optlen = optlen32;
1177 return (INT16)retVal;
1180 /***********************************************************************
1181 * htonl() (WINSOCK.8)(WSOCK32.8)
1183 u_long WINAPI WINSOCK_htonl(u_long hostlong) { return( htonl(hostlong) ); }
1184 /***********************************************************************
1185 * htons() (WINSOCK.9)(WSOCK32.9)
1187 u_short WINAPI WINSOCK_htons(u_short hostshort) { return( htons(hostshort) ); }
1188 /***********************************************************************
1189 * inet_addr() (WINSOCK.10)(WSOCK32.10)
1191 u_long WINAPI WINSOCK_inet_addr(char *cp) { return( inet_addr(cp) ); }
1192 /***********************************************************************
1193 * htohl() (WINSOCK.14)(WSOCK32.14)
1195 u_long WINAPI WINSOCK_ntohl(u_long netlong) { return( ntohl(netlong) ); }
1196 /***********************************************************************
1197 * ntohs() (WINSOCK.15)(WSOCK32.15)
1199 u_short WINAPI WINSOCK_ntohs(u_short netshort) { return( ntohs(netshort) ); }
1201 /***********************************************************************
1202 * inet_ntoa() (WINSOCK.11)(WSOCK32.11)
1204 char* WINAPI WINSOCK_inet_ntoa(struct in_addr in)
1206 /* use "buffer for dummies" here because some applications have
1207 * propensity to decode addresses in ws_hostent structure without
1208 * saving them first...
1211 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1213 if( pwsi )
1215 char* s = inet_ntoa(in);
1216 if( s )
1218 if( pwsi->dbuffer == NULL )
1219 if((pwsi->dbuffer = (char*) SEGPTR_ALLOC(32)) == NULL )
1221 SetLastError(WSAENOBUFS);
1222 return NULL;
1224 strncpy(pwsi->dbuffer, s, 32 );
1225 return pwsi->dbuffer;
1227 SetLastError(wsaErrno());
1229 return NULL;
1232 SEGPTR WINAPI WINSOCK_inet_ntoa16(struct in_addr in)
1234 char* retVal = WINSOCK_inet_ntoa(in);
1235 return retVal ? SEGPTR_GET(retVal) : (SEGPTR)NULL;
1238 /***********************************************************************
1239 * ioctlsocket() (WSOCK32.12)
1241 INT WINAPI WINSOCK_ioctlsocket(SOCKET s, UINT cmd, UINT *argp)
1243 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1244 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1246 TRACE(winsock, "(%08x): socket %04x, cmd %08x, ptr %8x\n",
1247 (unsigned)pwsi, s, cmd, (unsigned) argp);
1248 if( _check_ws(pwsi, pws) )
1250 long newcmd = cmd;
1252 switch( cmd )
1254 case WS_FIONREAD:
1255 newcmd=FIONREAD;
1256 break;
1258 case WS_FIONBIO:
1259 newcmd=FIONBIO;
1260 if( pws->psop && *argp == 0 )
1262 /* AsyncSelect()'ed sockets are always nonblocking */
1263 SetLastError(WSAEINVAL);
1264 return SOCKET_ERROR;
1266 break;
1268 case WS_SIOCATMARK:
1269 newcmd=SIOCATMARK;
1270 break;
1272 case WS_IOW('f',125,u_long):
1273 WARN(winsock,"Warning: WS1.1 shouldn't be using async I/O\n");
1274 SetLastError(WSAEINVAL);
1275 return SOCKET_ERROR;
1277 default:
1278 /* Netscape tries hard to use bogus ioctl 0x667e */
1279 WARN(winsock, "\tunknown WS_IOCTL cmd (%08x)\n", cmd);
1281 if( ioctl(pws->fd, newcmd, (char*)argp ) == 0 ) return 0;
1282 SetLastError((errno == EBADF) ? WSAENOTSOCK : wsaErrno());
1284 return SOCKET_ERROR;
1287 /***********************************************************************
1288 * ioctlsocket() (WINSOCK.12)
1290 INT16 WINAPI WINSOCK_ioctlsocket16(SOCKET16 s, UINT cmd, UINT *argp)
1292 return (INT16)WINSOCK_ioctlsocket( s, cmd, argp );
1296 /***********************************************************************
1297 * listen() (WSOCK32.13)
1299 INT WINAPI WINSOCK_listen(SOCKET s, INT backlog)
1301 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1302 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1304 TRACE(winsock, "(%08x): socket %04x, backlog %d\n",
1305 (unsigned)pwsi, s, backlog);
1306 if( _check_ws(pwsi, pws) )
1308 if (listen(pws->fd, backlog) == 0)
1310 if( !pws->psop )
1312 int fd_flags = fcntl(pws->fd, F_GETFL, 0);
1313 if( !(fd_flags & O_NONBLOCK) ) pws->flags |= WS_FD_ACCEPT;
1315 pws->flags |= WS_FD_LISTENING;
1316 pws->flags &= ~(WS_FD_INACTIVE | WS_FD_CONNECT | WS_FD_CONNECTED); /* just in case */
1317 return 0;
1319 SetLastError(wsaErrno());
1321 else SetLastError(WSAENOTSOCK);
1322 return SOCKET_ERROR;
1325 /***********************************************************************
1326 * listen() (WINSOCK.13)
1328 INT16 WINAPI WINSOCK_listen16(SOCKET16 s, INT16 backlog)
1330 return (INT16)WINSOCK_listen( s, backlog );
1334 /***********************************************************************
1335 * recv() (WSOCK32.16)
1337 INT WINAPI WINSOCK_recv(SOCKET s, char *buf, INT len, INT flags)
1339 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1340 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1342 TRACE(winsock, "(%08x): socket %04x, buf %8x, len %d, "
1343 "flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1344 len, flags);
1345 if( _check_ws(pwsi, pws) )
1347 INT length;
1348 if ((length = recv(pws->fd, buf, len, flags)) >= 0)
1350 TRACE(winsock, " -> %i bytes\n", length);
1352 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1353 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1355 return length;
1357 SetLastError(wsaErrno());
1359 else SetLastError(WSAENOTSOCK);
1360 WARN(winsock, " -> ERROR\n");
1361 return SOCKET_ERROR;
1364 /***********************************************************************
1365 * recv() (WINSOCK.16)
1367 INT16 WINAPI WINSOCK_recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1369 return (INT16)WINSOCK_recv( s, buf, len, flags );
1373 /***********************************************************************
1374 * recvfrom() (WSOCK32.17)
1376 INT WINAPI WINSOCK_recvfrom(SOCKET s, char *buf, INT len, INT flags,
1377 struct sockaddr *from, INT *fromlen32)
1379 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1380 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1381 #ifdef HAVE_IPX
1382 struct ws_sockaddr_ipx* from2 = (struct ws_sockaddr_ipx *)from;
1383 #endif
1385 TRACE(winsock, "(%08x): socket %04x, ptr %08x, "
1386 "len %d, flags %d\n", (unsigned)pwsi, s, (unsigned)buf,
1387 len, flags);
1388 #if DEBUG_SOCKADDR
1389 if( from ) dump_sockaddr(from);
1390 else DUMP("from = NULL\n");
1391 #endif
1393 if( _check_ws(pwsi, pws) )
1395 int length;
1397 if ((length = recvfrom(pws->fd, buf, len, flags, from, fromlen32)) >= 0)
1399 TRACE(winsock, " -> %i bytes\n", length);
1401 if( pws->psop && (pws->flags & (WS_FD_READ | WS_FD_CLOSE)) )
1402 EVENT_AddIO( pws->fd, EVENT_IO_READ ); /* reenabler */
1404 #ifdef HAVE_IPX
1405 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1406 from = (struct sockaddr *)
1407 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1408 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1409 from2->sipx_family = WS_AF_IPX;
1410 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1411 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1412 memcpy(from2->sipx_node,
1413 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1414 free(from);
1416 #endif
1417 return (INT16)length;
1419 SetLastError(wsaErrno());
1421 else SetLastError(WSAENOTSOCK);
1422 WARN(winsock, " -> ERROR\n");
1423 #ifdef HAVE_IPX
1424 if (from && ((struct sockaddr_ipx *)from)->sipx_family == AF_IPX) {
1425 from = (struct sockaddr *)
1426 malloc(fromlen32 ? *fromlen32 : sizeof(*from2));
1427 memcpy(from, from2, fromlen32 ? *fromlen32 : sizeof(*from2));
1428 from2->sipx_family = WS_AF_IPX;
1429 from2->sipx_network = ((struct sockaddr_ipx *)from)->sipx_network;
1430 from2->sipx_port = ((struct sockaddr_ipx *)from)->sipx_port;
1431 memcpy(from2->sipx_node,
1432 ((struct sockaddr_ipx *)from)->sipx_node, IPX_NODE_LEN);
1433 free(from);
1435 #endif
1436 return SOCKET_ERROR;
1439 /***********************************************************************
1440 * recvfrom() (WINSOCK.17)
1442 INT16 WINAPI WINSOCK_recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1443 struct sockaddr *from, INT16 *fromlen16)
1445 INT fromlen32;
1446 INT *p = &fromlen32;
1447 INT retVal;
1449 if( fromlen16 ) fromlen32 = *fromlen16; else p = NULL;
1450 retVal = WINSOCK_recvfrom( s, buf, len, flags, from, p );
1451 if( fromlen16 ) *fromlen16 = fromlen32;
1452 return (INT16)retVal;
1455 /***********************************************************************
1456 * select() (WINSOCK.18)(WSOCK32.18)
1458 static INT __ws_select( BOOL b32, void *ws_readfds, void *ws_writefds, void *ws_exceptfds,
1459 struct timeval *timeout )
1461 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1463 TRACE(winsock, "(%08x): read %8x, write %8x, excp %8x\n",
1464 (unsigned) pwsi, (unsigned) ws_readfds, (unsigned) ws_writefds, (unsigned) ws_exceptfds);
1466 if( pwsi )
1468 int highfd = 0;
1469 fd_set readfds, writefds, exceptfds;
1470 fd_set *p_read, *p_write, *p_except;
1472 p_read = fd_set_import(&readfds, pwsi, ws_readfds, &highfd, b32);
1473 p_write = fd_set_import(&writefds, pwsi, ws_writefds, &highfd, b32);
1474 p_except = fd_set_import(&exceptfds, pwsi, ws_exceptfds, &highfd, b32);
1476 if( (highfd = select(highfd + 1, p_read, p_write, p_except, timeout)) > 0 )
1478 fd_set_export(pwsi, &readfds, p_except, ws_readfds, b32);
1479 fd_set_export(pwsi, &writefds, p_except, ws_writefds, b32);
1481 if (p_except && ws_exceptfds)
1483 #define wsfds16 ((ws_fd_set16*)ws_exceptfds)
1484 #define wsfds32 ((ws_fd_set32*)ws_exceptfds)
1485 int i, j, count = (b32) ? wsfds32->fd_count : wsfds16->fd_count;
1487 for (i = j = 0; i < count; i++)
1489 ws_socket *pws = (b32) ? (ws_socket *)WS_HANDLE2PTR(wsfds32->fd_array[i])
1490 : (ws_socket *)WS_HANDLE2PTR(wsfds16->fd_array[i]);
1491 if( _check_ws(pwsi, pws) && FD_ISSET(pws->fd, &exceptfds) )
1493 if( b32 )
1494 wsfds32->fd_array[j++] = wsfds32->fd_array[i];
1495 else
1496 wsfds16->fd_array[j++] = wsfds16->fd_array[i];
1499 if( b32 )
1500 wsfds32->fd_count = j;
1501 else
1502 wsfds16->fd_count = j;
1503 #undef wsfds32
1504 #undef wsfds16
1506 return highfd;
1508 if( ws_readfds ) ((ws_fd_set32*)ws_readfds)->fd_count = 0;
1509 if( ws_writefds ) ((ws_fd_set32*)ws_writefds)->fd_count = 0;
1510 if( ws_exceptfds ) ((ws_fd_set32*)ws_exceptfds)->fd_count = 0;
1512 if( highfd == 0 ) return 0;
1513 SetLastError(wsaErrno());
1515 return SOCKET_ERROR;
1518 INT16 WINAPI WINSOCK_select16(INT16 nfds, ws_fd_set16 *ws_readfds,
1519 ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
1520 struct timeval *timeout)
1522 return (INT16)__ws_select( FALSE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1525 INT WINAPI WINSOCK_select(INT nfds, ws_fd_set32 *ws_readfds,
1526 ws_fd_set32 *ws_writefds, ws_fd_set32 *ws_exceptfds,
1527 struct timeval *timeout)
1529 /* struct timeval is the same for both 32- and 16-bit code */
1530 return (INT)__ws_select( TRUE, ws_readfds, ws_writefds, ws_exceptfds, timeout );
1534 /***********************************************************************
1535 * send() (WSOCK32.19)
1537 INT WINAPI WINSOCK_send(SOCKET s, char *buf, INT len, INT flags)
1539 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1540 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1542 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1543 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1544 if( _check_ws(pwsi, pws) )
1546 int length;
1548 if ((length = send(pws->fd, buf, len, flags)) < 0 )
1550 SetLastError(wsaErrno());
1551 if( GetLastError() == WSAEWOULDBLOCK &&
1552 pws->psop && pws->flags & WS_FD_WRITE )
1553 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1555 else return (INT16)length;
1557 else SetLastError(WSAENOTSOCK);
1558 return SOCKET_ERROR;
1561 /***********************************************************************
1562 * send() (WINSOCK.19)
1564 INT16 WINAPI WINSOCK_send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
1566 return WINSOCK_send( s, buf, len, flags );
1569 /***********************************************************************
1570 * sendto() (WSOCK32.20)
1572 INT WINAPI WINSOCK_sendto(SOCKET s, char *buf, INT len, INT flags,
1573 struct sockaddr *to, INT tolen)
1575 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1576 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1577 #ifdef HAVE_IPX
1578 struct ws_sockaddr_ipx* to2 = (struct ws_sockaddr_ipx *)to;
1579 #endif
1581 TRACE(winsock, "(%08x): socket %04x, ptr %08x, length %d, flags %d\n",
1582 (unsigned)pwsi, s, (unsigned) buf, len, flags);
1583 if( _check_ws(pwsi, pws) )
1585 INT length;
1587 if (to && ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_PUP)
1588 ((struct ws_sockaddr_ipx *)to)->sipx_family = AF_UNSPEC;
1589 #ifdef HAVE_IPX
1590 else if (to &&
1591 ((struct ws_sockaddr_ipx *)to)->sipx_family == WS_AF_IPX)
1593 to = (struct sockaddr *) malloc(sizeof(struct sockaddr_ipx));
1594 memset(to, '\0', sizeof(struct sockaddr_ipx));
1595 ((struct sockaddr_ipx *)to)->sipx_family = AF_IPX;
1596 ((struct sockaddr_ipx *)to)->sipx_port = to2->sipx_port;
1597 ((struct sockaddr_ipx *)to)->sipx_network = to2->sipx_network;
1598 memcpy(((struct sockaddr_ipx *)to)->sipx_node,
1599 to2->sipx_node, IPX_NODE_LEN);
1600 tolen = sizeof(struct sockaddr_ipx);
1602 #endif
1603 if ((length = sendto(pws->fd, buf, len, flags, to, tolen)) < 0 )
1605 SetLastError(wsaErrno());
1606 if( GetLastError() == WSAEWOULDBLOCK &&
1607 pws->psop && pws->flags & WS_FD_WRITE )
1608 EVENT_AddIO( pws->fd, EVENT_IO_WRITE ); /* reenabler */
1610 else {
1611 #ifdef HAVE_IPX
1612 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1613 free(to);
1615 #endif
1616 return length;
1619 else SetLastError(WSAENOTSOCK);
1620 #ifdef HAVE_IPX
1621 if (to && ((struct sockaddr_ipx *)to)->sipx_family == AF_IPX) {
1622 free(to);
1624 #endif
1625 return SOCKET_ERROR;
1628 /***********************************************************************
1629 * sendto() (WINSOCK.20)
1631 INT16 WINAPI WINSOCK_sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
1632 struct sockaddr *to, INT16 tolen)
1634 return (INT16)WINSOCK_sendto( s, buf, len, flags, to, tolen );
1637 /***********************************************************************
1638 * setsockopt() (WSOCK32.21)
1640 INT WINAPI WINSOCK_setsockopt(SOCKET16 s, INT level, INT optname,
1641 char *optval, INT optlen)
1643 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1644 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1646 TRACE(winsock, "(%08x): socket %04x, lev %d, opt %d, ptr %08x, len %d\n",
1647 (unsigned)pwsi, s, level, optname, (int) optval, optlen);
1648 if( _check_ws(pwsi, pws) )
1650 struct linger linger;
1652 convert_sockopt(&level, &optname);
1653 if (optname == SO_LINGER && optval) {
1654 /* yes, uses unsigned short in both win16/win32 */
1655 linger.l_onoff = ((UINT16*)optval)[0];
1656 linger.l_linger = ((UINT16*)optval)[1];
1657 /* FIXME: what is documented behavior if SO_LINGER optval
1658 is null?? */
1659 optval = (char*)&linger;
1660 optlen = sizeof(struct linger);
1662 if (setsockopt(pws->fd, level, optname, optval, optlen) == 0) return 0;
1663 SetLastError(wsaErrno());
1665 else SetLastError(WSAENOTSOCK);
1666 return SOCKET_ERROR;
1669 /***********************************************************************
1670 * setsockopt() (WINSOCK.21)
1672 INT16 WINAPI WINSOCK_setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
1673 char *optval, INT16 optlen)
1675 if( !optval ) return SOCKET_ERROR;
1676 return (INT16)WINSOCK_setsockopt( s, (UINT16)level, optname, optval, optlen );
1680 /***********************************************************************
1681 * shutdown() (WSOCK32.22)
1683 INT WINAPI WINSOCK_shutdown(SOCKET s, INT how)
1685 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
1686 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1688 TRACE(winsock, "(%08x): socket %04x, how %i\n",
1689 (unsigned)pwsi, s, how );
1690 if( _check_ws(pwsi, pws) )
1692 if( pws->psop )
1693 switch( how )
1695 case 0: /* drop receives */
1696 if( pws->flags & (WS_FD_READ | WS_FD_CLOSE) )
1697 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
1698 pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE);
1699 #ifdef SHUT_RD
1700 how = SHUT_RD;
1701 #endif
1702 break;
1704 case 1: /* drop sends */
1705 if( pws->flags & WS_FD_WRITE )
1706 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
1707 pws->flags &= ~WS_FD_WRITE;
1708 #ifdef SHUT_WR
1709 how = SHUT_WR;
1710 #endif
1711 break;
1713 case 2: /* drop all */
1714 #ifdef SHUT_RDWR
1715 how = SHUT_RDWR;
1716 #endif
1717 default:
1718 WSAAsyncSelect( s, 0, 0, 0 );
1719 break;
1722 if (shutdown(pws->fd, how) == 0)
1724 if( how > 1 )
1726 pws->flags &= ~(WS_FD_CONNECTED | WS_FD_LISTENING);
1727 pws->flags |= WS_FD_INACTIVE;
1729 return 0;
1731 SetLastError(wsaErrno());
1733 else SetLastError(WSAENOTSOCK);
1734 return SOCKET_ERROR;
1737 /***********************************************************************
1738 * shutdown() (WINSOCK.22)
1740 INT16 WINAPI WINSOCK_shutdown16(SOCKET16 s, INT16 how)
1742 return (INT16)WINSOCK_shutdown( s, how );
1746 /***********************************************************************
1747 * socket() (WSOCK32.23)
1749 SOCKET WINAPI WINSOCK_socket(INT af, INT type, INT protocol)
1751 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1753 TRACE(winsock, "(%08x): af=%d type=%d protocol=%d\n",
1754 (unsigned)pwsi, af, type, protocol);
1756 if( pwsi )
1758 int sock;
1760 /* check the socket family */
1761 switch(af)
1763 #ifdef HAVE_IPX
1764 case WS_AF_IPX: af = AF_IPX;
1765 #endif
1766 case AF_INET:
1767 case AF_UNSPEC: break;
1768 default: SetLastError(WSAEAFNOSUPPORT);
1769 return INVALID_SOCKET;
1772 /* check the socket type */
1773 switch(type)
1775 case SOCK_STREAM:
1776 case SOCK_DGRAM:
1777 case SOCK_RAW: break;
1778 default: SetLastError(WSAESOCKTNOSUPPORT);
1779 return INVALID_SOCKET;
1782 /* check the protocol type */
1783 if ( protocol < 0 ) /* don't support negative values */
1784 { SetLastError(WSAEPROTONOSUPPORT); return INVALID_SOCKET; }
1786 if ( af == AF_UNSPEC) /* did they not specify the address family? */
1787 switch(protocol)
1789 case IPPROTO_TCP:
1790 if (type == SOCK_STREAM) { af = AF_INET; break; }
1791 case IPPROTO_UDP:
1792 if (type == SOCK_DGRAM) { af = AF_INET; break; }
1793 default: SetLastError(WSAEPROTOTYPE); return INVALID_SOCKET;
1796 if ((sock = socket(af, type, protocol)) >= 0)
1798 ws_socket* pnew = wsi_alloc_socket(pwsi, sock);
1800 TRACE(winsock,"\tcreated %i (handle %04x)\n", sock, (UINT16)WS_PTR2HANDLE(pnew));
1802 if( pnew )
1804 pnew->flags |= WS_FD_INACTIVE;
1805 return (SOCKET16)WS_PTR2HANDLE(pnew);
1808 close(sock);
1809 SetLastError(WSAENOBUFS);
1810 return INVALID_SOCKET;
1813 if (errno == EPERM) /* raw socket denied */
1815 if (type == SOCK_RAW)
1816 MSG("WARNING: Trying to create a socket of type SOCK_RAW, will fail unless running as root\n");
1817 else
1818 MSG("WS_SOCKET: not enough privileges to create socket, try running as root\n");
1819 SetLastError(WSAESOCKTNOSUPPORT);
1820 } else SetLastError(wsaErrno());
1823 WARN(winsock, "\t\tfailed!\n");
1824 return INVALID_SOCKET;
1827 /***********************************************************************
1828 * socket() (WINSOCK.23)
1830 SOCKET16 WINAPI WINSOCK_socket16(INT16 af, INT16 type, INT16 protocol)
1832 return (SOCKET16)WINSOCK_socket( af, type, protocol );
1836 /* ----------------------------------- DNS services
1838 * IMPORTANT: 16-bit API structures have SEGPTR pointers inside them.
1839 * Also, we have to use wsock32 stubs to convert structures and
1840 * error codes from Unix to WSA, hence there is no direct mapping in
1841 * the relay32/wsock32.spec.
1844 static char* NULL_STRING = "NULL";
1846 /***********************************************************************
1847 * gethostbyaddr() (WINSOCK.51)(WSOCK32.51)
1849 static struct WIN_hostent* __ws_gethostbyaddr(const char *addr, int len, int type, int dup_flag)
1851 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1853 if( pwsi )
1855 struct hostent* host;
1856 if( (host = gethostbyaddr(addr, len, type)) != NULL )
1857 if( WS_dup_he(pwsi, host, dup_flag) )
1858 return (struct WIN_hostent*)(pwsi->he);
1859 else
1860 SetLastError(WSAENOBUFS);
1861 else
1862 SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1864 return NULL;
1867 SEGPTR WINAPI WINSOCK_gethostbyaddr16(const char *addr, INT16 len, INT16 type)
1869 struct WIN_hostent* retval;
1870 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1871 (unsigned) addr, len, type);
1872 retval = __ws_gethostbyaddr( addr, len, type, WS_DUP_SEGPTR );
1873 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1876 struct WIN_hostent* WINAPI WINSOCK_gethostbyaddr(const char *addr, INT len,
1877 INT type)
1879 TRACE(winsock, "ptr %08x, len %d, type %d\n",
1880 (unsigned) addr, len, type);
1881 return __ws_gethostbyaddr(addr, len, type, WS_DUP_LINEAR);
1884 /***********************************************************************
1885 * gethostbyname() (WINSOCK.52)(WSOCK32.52)
1887 static struct WIN_hostent * __ws_gethostbyname(const char *name, int dup_flag)
1889 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1891 if( pwsi )
1893 struct hostent* host;
1894 if( (host = gethostbyname(name)) != NULL )
1895 if( WS_dup_he(pwsi, host, dup_flag) )
1896 return (struct WIN_hostent*)(pwsi->he);
1897 else SetLastError(WSAENOBUFS);
1898 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1900 return NULL;
1903 SEGPTR WINAPI WINSOCK_gethostbyname16(const char *name)
1905 struct WIN_hostent* retval;
1906 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1907 retval = __ws_gethostbyname( name, WS_DUP_SEGPTR );
1908 return (retval)? SEGPTR_GET(retval) : ((SEGPTR)NULL) ;
1911 struct WIN_hostent* WINAPI WINSOCK_gethostbyname(const char* name)
1913 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1914 return __ws_gethostbyname( name, WS_DUP_LINEAR );
1918 /***********************************************************************
1919 * getprotobyname() (WINSOCK.53)(WSOCK32.53)
1921 static struct WIN_protoent* __ws_getprotobyname(const char *name, int dup_flag)
1923 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1925 if( pwsi )
1927 struct protoent* proto;
1928 if( (proto = getprotobyname(name)) != NULL )
1929 if( WS_dup_pe(pwsi, proto, dup_flag) )
1930 return (struct WIN_protoent*)(pwsi->pe);
1931 else SetLastError(WSAENOBUFS);
1932 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
1934 return NULL;
1937 SEGPTR WINAPI WINSOCK_getprotobyname16(const char *name)
1939 struct WIN_protoent* retval;
1940 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1941 retval = __ws_getprotobyname(name, WS_DUP_SEGPTR);
1942 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1945 struct WIN_protoent* WINAPI WINSOCK_getprotobyname(const char* name)
1947 TRACE(winsock, "%s\n", (name)?name:NULL_STRING);
1948 return __ws_getprotobyname(name, WS_DUP_LINEAR);
1952 /***********************************************************************
1953 * getprotobynumber() (WINSOCK.54)(WSOCK32.54)
1955 static struct WIN_protoent* __ws_getprotobynumber(int number, int dup_flag)
1957 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1959 if( pwsi )
1961 struct protoent* proto;
1962 if( (proto = getprotobynumber(number)) != NULL )
1963 if( WS_dup_pe(pwsi, proto, dup_flag) )
1964 return (struct WIN_protoent*)(pwsi->pe);
1965 else SetLastError(WSAENOBUFS);
1966 else SetLastError(WSANO_DATA);
1968 return NULL;
1971 SEGPTR WINAPI WINSOCK_getprotobynumber16(INT16 number)
1973 struct WIN_protoent* retval;
1974 TRACE(winsock, "%i\n", number);
1975 retval = __ws_getprotobynumber(number, WS_DUP_SEGPTR);
1976 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
1979 struct WIN_protoent* WINAPI WINSOCK_getprotobynumber(INT number)
1981 TRACE(winsock, "%i\n", number);
1982 return __ws_getprotobynumber(number, WS_DUP_LINEAR);
1986 /***********************************************************************
1987 * getservbyname() (WINSOCK.55)(WSOCK32.55)
1989 struct WIN_servent* __ws_getservbyname(const char *name, const char *proto, int dup_flag)
1991 LPWSINFO pwsi = wsi_find(GetCurrentTask());
1993 if( pwsi )
1995 struct servent* serv;
1996 int i = wsi_strtolo( pwsi, name, proto );
1998 if( i )
1999 if( (serv = getservbyname(pwsi->buffer, pwsi->buffer + i)) != NULL )
2000 if( WS_dup_se(pwsi, serv, dup_flag) )
2001 return (struct WIN_servent*)(pwsi->se);
2002 else SetLastError(WSAENOBUFS);
2003 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
2004 else SetLastError(WSAENOBUFS);
2006 return NULL;
2009 SEGPTR WINAPI WINSOCK_getservbyname16(const char *name, const char *proto)
2011 struct WIN_servent* retval;
2012 TRACE(winsock, "'%s', '%s'\n",
2013 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
2014 retval = __ws_getservbyname(name, proto, WS_DUP_SEGPTR);
2015 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
2018 struct WIN_servent* WINAPI WINSOCK_getservbyname(const char *name, const char *proto)
2020 TRACE(winsock, "'%s', '%s'\n",
2021 (name)?name:NULL_STRING, (proto)?proto:NULL_STRING);
2022 return __ws_getservbyname(name, proto, WS_DUP_LINEAR);
2026 /***********************************************************************
2027 * getservbyport() (WINSOCK.56)(WSOCK32.56)
2029 static struct WIN_servent* __ws_getservbyport(int port, const char* proto, int dup_flag)
2031 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2033 if( pwsi )
2035 struct servent* serv;
2036 int i = wsi_strtolo( pwsi, proto, NULL );
2038 if( i )
2039 if( (serv = getservbyport(port, pwsi->buffer)) != NULL )
2040 if( WS_dup_se(pwsi, serv, dup_flag) )
2041 return (struct WIN_servent*)(pwsi->se);
2042 else SetLastError(WSAENOBUFS);
2043 else SetLastError((h_errno < 0) ? wsaErrno() : wsaHerrno());
2044 else SetLastError(WSAENOBUFS);
2046 return NULL;
2049 SEGPTR WINAPI WINSOCK_getservbyport16(INT16 port, const char *proto)
2051 struct WIN_servent* retval;
2052 TRACE(winsock, "%i, '%s'\n",
2053 (int)port, (proto)?proto:NULL_STRING);
2054 retval = __ws_getservbyport(port, proto, WS_DUP_SEGPTR);
2055 return retval ? SEGPTR_GET(retval) : ((SEGPTR)NULL);
2058 struct WIN_servent* WINAPI WINSOCK_getservbyport(INT port, const char *proto)
2060 TRACE(winsock, "%i, '%s'\n",
2061 (int)port, (proto)?proto:NULL_STRING);
2062 return __ws_getservbyport(port, proto, WS_DUP_LINEAR);
2066 /***********************************************************************
2067 * gethostname() (WSOCK32.57)
2069 INT WINAPI WINSOCK_gethostname(char *name, INT namelen)
2071 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2073 TRACE(winsock, "(%08x): name %s, len %d\n",
2074 (unsigned)pwsi, (name)?name:NULL_STRING, namelen);
2075 if( pwsi )
2077 if (gethostname(name, namelen) == 0) return 0;
2078 SetLastError((errno == EINVAL) ? WSAEFAULT : wsaErrno());
2080 return SOCKET_ERROR;
2083 /***********************************************************************
2084 * gethostname() (WINSOCK.57)
2086 INT16 WINAPI WINSOCK_gethostname16(char *name, INT16 namelen)
2088 return (INT16)WINSOCK_gethostname(name, namelen);
2092 /* ------------------------------------- Windows sockets extensions -- *
2094 * ------------------------------------------------------------------- */
2095 /***********************************************************************
2096 * WSAAsyncSelect() (WINSOCK.101)(WSOCK32.101)
2099 static ws_select_op* __ws_select_list = NULL;
2101 BOOL WINSOCK_HandleIO( int* max_fd, int num_pending,
2102 fd_set pending_set[3], fd_set event_set[3] )
2104 /* This function is called by the event dispatcher
2105 * with the pending_set[] containing the result of select() and
2106 * the event_set[] containing all fd that are being watched */
2108 ws_select_op* psop = __ws_select_list;
2109 BOOL bPost = FALSE;
2110 DWORD dwEvent, dwErrBytes;
2111 int num_posted;
2113 TRACE(winsock,"%i pending descriptors\n", num_pending );
2115 for( num_posted = dwEvent = 0 ; psop; psop = psop->next )
2117 unsigned flags = psop->pws->flags;
2118 int fd = psop->pws->fd;
2119 int r, w, e;
2121 w = 0;
2122 if( (r = FD_ISSET( fd, &pending_set[EVENT_IO_READ] )) ||
2123 (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) ||
2124 (e = FD_ISSET( fd, &pending_set[EVENT_IO_EXCEPT] )) )
2126 /* This code removes WS_FD flags on one-shot events (WS_FD_CLOSE,
2127 * WS_FD_CONNECT), otherwise it clears descriptors in the io_set.
2128 * Reenabling calls turn them back on.
2131 TRACE(winsock,"\tchecking psop = 0x%08x\n", (unsigned) psop );
2133 num_pending--;
2135 /* Now figure out what kind of event we've got. The worst problem
2136 * we have to contend with is that some out of control applications
2137 * really want to use mutually exclusive AsyncSelect() flags all at
2138 * the same time.
2141 if((flags & WS_FD_ACCEPT) && (flags & WS_FD_LISTENING))
2143 /* WS_FD_ACCEPT is valid only if the socket is in the
2144 * listening state */
2146 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2147 if( r )
2149 FD_CLR( fd, &event_set[EVENT_IO_READ] ); /* reenabled by the next accept() */
2150 dwEvent = WSAMAKESELECTREPLY( WS_FD_ACCEPT, 0 );
2151 bPost = TRUE;
2153 else continue;
2155 else if( flags & WS_FD_CONNECT )
2157 /* connecting socket */
2159 if( w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] )) )
2161 /* ready to write means that socket is connected
2163 * FIXME: Netscape calls AsyncSelect( s, ... WS_FD_CONNECT .. )
2164 * right after s = socket() and somehow "s" becomes writeable
2165 * before it goes through connect()!?!?
2168 psop->pws->flags |= WS_FD_CONNECTED;
2169 psop->pws->flags &= ~(WS_FD_CONNECT | WS_FD_INACTIVE);
2170 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, 0 );
2172 if( flags & (WS_FD_READ | WS_FD_CLOSE))
2173 FD_SET( fd, &event_set[EVENT_IO_READ] );
2174 else
2175 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2176 if( flags & WS_FD_WRITE )
2177 FD_SET( fd, &event_set[EVENT_IO_WRITE] );
2178 else
2179 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2180 bPost = TRUE;
2182 else if( r )
2184 /* failure - do read() to get correct errno */
2186 if( read( fd, &dwErrBytes, sizeof(dwErrBytes) ) == -1 )
2188 dwEvent = WSAMAKESELECTREPLY( WS_FD_CONNECT, wsaErrno() );
2189 bPost = TRUE;
2192 /* otherwise bPost stays FALSE, should probably clear event_set */
2194 else
2196 /* connected socket, no WS_FD_OOB code for now. */
2198 if( flags & WS_FD_WRITE &&
2199 (w || (w = FD_ISSET( fd, &pending_set[EVENT_IO_WRITE] ))) )
2201 /* this will be reenabled when send() or sendto() fail with
2202 * WSAEWOULDBLOCK */
2204 if( PostMessageA( psop->hWnd, psop->uMsg, (WPARAM)WS_PTR2HANDLE(psop->pws),
2205 (LPARAM)WSAMAKESELECTREPLY( WS_FD_WRITE, 0 ) ) )
2207 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2208 psop->hWnd, psop->uMsg, (unsigned)MAKELONG(WS_FD_WRITE, 0) );
2209 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2210 num_posted++;
2214 if( r && (flags & (WS_FD_READ | WS_FD_CLOSE)) )
2216 int val = (flags & WS_FD_RAW);
2218 /* WS_FD_RAW is set by the WSAAsyncSelect() init */
2220 bPost = TRUE;
2221 if( !val && ioctl( fd, FIONREAD, (char*)&dwErrBytes) == -1 )
2223 /* weirdness */
2225 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, wsaErrno() );
2227 else if( val || dwErrBytes )
2229 /* got pending data, will be reenabled by recv() or recvfrom() */
2231 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2232 dwEvent = WSAMAKESELECTREPLY( WS_FD_READ, 0 );
2234 else
2236 /* 0 bytes to read - connection reset by peer? */
2239 val = read( fd, (char*)&dwErrBytes, sizeof(dwErrBytes));
2240 while( errno == EINTR );
2241 if( errno != EWOULDBLOCK )
2243 switch( val )
2245 case 0: errno = ENETDOWN; /* soft reset, fall through */
2246 case -1: /* hard reset */
2247 dwEvent = WSAMAKESELECTREPLY( WS_FD_CLOSE, wsaErrno() );
2248 break;
2250 default: bPost = FALSE;
2251 continue; /* FIXME: this is real bad */
2254 else { bPost = FALSE; continue; } /* more weirdness */
2256 /* this is it, this socket is closed */
2258 psop->pws->flags &= ~(WS_FD_READ | WS_FD_CLOSE | WS_FD_WRITE);
2259 FD_CLR( fd, &event_set[EVENT_IO_READ] );
2260 FD_CLR( fd, &event_set[EVENT_IO_WRITE] );
2262 if( *max_fd == (fd + 1) ) (*max_fd)--;
2267 if( bPost )
2269 TRACE(winsock, "\t hwnd %04x - %04x, %08x\n",
2270 psop->hWnd, psop->uMsg, (unsigned)dwEvent );
2271 PostMessageA( psop->hWnd, psop->uMsg,
2272 (WPARAM)WS_PTR2HANDLE(psop->pws), (LPARAM)dwEvent );
2273 bPost = FALSE;
2274 num_posted++;
2277 if( num_pending <= 0 ) break;
2280 TRACE(winsock, "\tdone, %i posted events\n", num_posted );
2281 return ( num_posted ) ? TRUE : FALSE;
2284 INT WINAPI WSAAsyncSelect(SOCKET s, HWND hWnd, UINT uMsg, UINT lEvent)
2286 ws_socket* pws = (ws_socket*)WS_HANDLE2PTR(s);
2287 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2289 TRACE(winsock, "(%08x): %04x, hWnd %04x, uMsg %08x, event %08x\n",
2290 (unsigned)pwsi, (SOCKET16)s, (HWND16)hWnd, uMsg, (unsigned)lEvent );
2291 if( _check_ws(pwsi, pws) )
2293 ws_select_op* psop;
2295 if( (psop = pws->psop) )
2297 /* delete previous control struct */
2299 if( psop == __ws_select_list )
2300 __ws_select_list = psop->next;
2301 else
2302 psop->prev->next = psop->next;
2303 if( psop->next ) psop->next->prev = psop->prev;
2305 if( pws->flags & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2306 EVENT_DeleteIO( pws->fd, EVENT_IO_READ );
2307 if( pws->flags & (WS_FD_CONNECT | WS_FD_WRITE) )
2308 EVENT_DeleteIO( pws->fd, EVENT_IO_WRITE );
2310 TRACE(winsock,"\tremoving psop = 0x%08x\n", (unsigned) psop );
2312 WS_FREE( pws->psop );
2313 pws->flags &= ~(WS_FD_RAW | WS_FD_ACCEPT | WS_FD_CONNECT |
2314 WS_FD_READ | WS_FD_WRITE | WS_FD_CLOSE);
2315 pws->psop = NULL;
2318 if( lEvent )
2320 psop = (ws_select_op*)WS_ALLOC(sizeof(ws_select_op));
2321 if( psop )
2323 int sock_type, bytes = sizeof(int);
2325 WINSOCK_unblock_io( pws->fd, TRUE );
2327 psop->prev = NULL;
2328 psop->next = __ws_select_list;
2329 if( __ws_select_list )
2330 __ws_select_list->prev = psop;
2331 __ws_select_list = psop;
2333 psop->pws = pws;
2334 psop->hWnd = hWnd;
2335 psop->uMsg = uMsg;
2337 pws->psop = psop;
2338 pws->flags |= (0x0000FFFF & lEvent);
2339 getsockopt(pws->fd, SOL_SOCKET, SO_TYPE, &sock_type, &bytes);
2340 if( sock_type == SOCK_RAW ) pws->flags |= WS_FD_RAW;
2342 if( lEvent & (WS_FD_ACCEPT | WS_FD_CONNECT | WS_FD_READ | WS_FD_CLOSE) )
2343 EVENT_AddIO( pws->fd, EVENT_IO_READ );
2344 if( lEvent & (WS_FD_CONNECT | WS_FD_WRITE) )
2345 EVENT_AddIO( pws->fd, EVENT_IO_WRITE );
2347 /* TODO: handle WS_FD_ACCEPT right away if the socket is readable */
2349 TRACE(winsock,"\tcreating psop = 0x%08x\n", (unsigned)psop );
2351 return 0; /* success */
2353 else SetLastError(WSAENOBUFS);
2355 else return 0;
2357 else SetLastError(WSAEINVAL);
2358 return SOCKET_ERROR;
2361 INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, UINT lEvent)
2363 return (INT16)WSAAsyncSelect( s, hWnd, wMsg, lEvent );
2367 /***********************************************************************
2368 * __WSAFDIsSet() (WINSOCK.151)
2370 INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
2372 int i = set->fd_count;
2374 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2376 while (i--)
2377 if (set->fd_array[i] == s) return 1;
2378 return 0;
2381 /***********************************************************************
2382 * __WSAFDIsSet() (WSOCK32.151)
2384 INT WINAPI __WSAFDIsSet(SOCKET s, ws_fd_set32 *set)
2386 int i = set->fd_count;
2388 TRACE(winsock, "(%d,%8lx(%i))\n", s,(unsigned long)set, i);
2390 while (i--)
2391 if (set->fd_array[i] == s) return 1;
2392 return 0;
2395 /***********************************************************************
2396 * WSAIsBlocking() (WINSOCK.114)(WSOCK32.114)
2398 BOOL WINAPI WSAIsBlocking(void)
2400 /* By default WinSock should set all its sockets to non-blocking mode
2401 * and poll in PeekMessage loop when processing "blocking" ones. This
2402 * function is supposed to tell if the program is in this loop. Our
2403 * blocking calls are truly blocking so we always return FALSE.
2405 * Note: It is allowed to call this function without prior WSAStartup().
2408 TRACE(winsock, "\n");
2409 return FALSE;
2412 /***********************************************************************
2413 * WSACancelBlockingCall() (WINSOCK.113)(WSOCK32.113)
2415 INT WINAPI WSACancelBlockingCall(void)
2417 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2419 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2421 if( pwsi ) return 0;
2422 return SOCKET_ERROR;
2426 /***********************************************************************
2427 * WSASetBlockingHook16() (WINSOCK.109)
2429 FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
2431 FARPROC16 prev;
2432 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2434 TRACE(winsock, "(%08x): hook %08x\n",
2435 (unsigned)pwsi, (unsigned) lpBlockFunc);
2436 if( pwsi )
2438 prev = (FARPROC16)pwsi->blocking_hook;
2439 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2440 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2441 return prev;
2443 return 0;
2447 /***********************************************************************
2448 * WSASetBlockingHook32()
2450 FARPROC WINAPI WSASetBlockingHook(FARPROC lpBlockFunc)
2452 FARPROC prev;
2453 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2455 TRACE(winsock, "(%08x): hook %08x\n",
2456 (unsigned)pwsi, (unsigned) lpBlockFunc);
2457 if( pwsi ) {
2458 prev = (FARPROC)pwsi->blocking_hook;
2459 pwsi->blocking_hook = (DWORD)lpBlockFunc;
2460 pwsi->flags |= WSI_BLOCKINGHOOK;
2461 return prev;
2463 return NULL;
2467 /***********************************************************************
2468 * WSAUnhookBlockingHook16() (WINSOCK.110)
2470 INT16 WINAPI WSAUnhookBlockingHook16(void)
2472 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2474 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2475 if( pwsi ) return (INT16)(pwsi->blocking_hook = 0);
2476 return SOCKET_ERROR;
2480 /***********************************************************************
2481 * WSAUnhookBlockingHook32()
2483 INT WINAPI WSAUnhookBlockingHook(void)
2485 LPWSINFO pwsi = wsi_find(GetCurrentTask());
2487 TRACE(winsock, "(%08x)\n", (unsigned)pwsi);
2488 if( pwsi )
2490 pwsi->blocking_hook = 0;
2491 pwsi->flags &= ~WSI_BLOCKINGHOOK;
2492 return 0;
2494 return SOCKET_ERROR;
2498 * TCP/IP action codes.
2502 #define WSCNTL_TCPIP_QUERY_INFO 0x00000000
2503 #define WSCNTL_TCPIP_SET_INFO 0x00000001
2504 #define WSCNTL_TCPIP_ICMP_ECHO 0x00000002
2505 #define WSCNTL_TCPIP_TEST 0x00000003
2508 /***********************************************************************
2509 * WsControl()
2511 * WsControl seems to be an undocumented Win95 function. A lot of
2512 * discussion about WsControl can be found on the net, e.g.
2513 * Subject: Re: WSOCK32.DLL WsControl Exported Function
2514 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
2515 * Date: 1997/08/17
2518 DWORD WINAPI WsControl(DWORD protocoll,DWORD action,
2519 LPVOID inbuf,LPDWORD inbuflen,
2520 LPVOID outbuf,LPDWORD outbuflen)
2523 switch (action) {
2524 case WSCNTL_TCPIP_ICMP_ECHO:
2526 unsigned int addr = *(unsigned int*)inbuf;
2527 #if 0
2528 int timeout= *(unsigned int*)(inbuf+4);
2529 short x1 = *(unsigned short*)(inbuf+8);
2530 short sendbufsize = *(unsigned short*)(inbuf+10);
2531 char x2 = *(unsigned char*)(inbuf+12);
2532 char ttl = *(unsigned char*)(inbuf+13);
2533 char service = *(unsigned char*)(inbuf+14);
2534 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
2535 #endif
2537 FIXME(winsock,"(ICMP_ECHO) to 0x%08x stub \n", addr);
2538 break;
2540 default:
2541 FIXME(winsock,"(%lx,%lx,%p,%p,%p,%p) stub\n",
2542 protocoll,action,inbuf,inbuflen,outbuf,outbuflen);
2544 return FALSE;
2546 /*********************************************************
2547 * WS_s_perror WSOCK32.1108
2549 void WINAPI WS_s_perror(LPCSTR message)
2551 FIXME(winsock,"(%s): stub\n",message);
2552 return;
2556 /* ----------------------------------- end of API stuff */
2558 /* ----------------------------------- helper functions -
2560 * TODO: Merge WS_dup_..() stuff into one function that
2561 * would operate with a generic structure containing internal
2562 * pointers (via a template of some kind).
2565 static int list_size(char** l, int item_size)
2567 int i,j = 0;
2568 if(l)
2569 { for(i=0;l[i];i++)
2570 j += (item_size) ? item_size : strlen(l[i]) + 1;
2571 j += (i + 1) * sizeof(char*); }
2572 return j;
2575 static int list_dup(char** l_src, char* ref, char* base, int item_size)
2577 /* base is either either equal to ref or 0 or SEGPTR */
2579 char* p = ref;
2580 char** l_to = (char**)ref;
2581 int i,j,k;
2583 for(j=0;l_src[j];j++) ;
2584 p += (j + 1) * sizeof(char*);
2585 for(i=0;i<j;i++)
2586 { l_to[i] = base + (p - ref);
2587 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
2588 memcpy(p, l_src[i], k); p += k; }
2589 l_to[i] = NULL;
2590 return (p - ref);
2593 /* ----- hostent */
2595 static int hostent_size(struct hostent* p_he)
2597 int size = 0;
2598 if( p_he )
2599 { size = sizeof(struct hostent);
2600 size += strlen(p_he->h_name) + 1;
2601 size += list_size(p_he->h_aliases, 0);
2602 size += list_size(p_he->h_addr_list, p_he->h_length ); }
2603 return size;
2606 int WS_dup_he(LPWSINFO pwsi, struct hostent* p_he, int flag)
2608 /* Convert hostent structure into ws_hostent so that the data fits
2609 * into pwsi->buffer. Internal pointers can be linear, SEGPTR, or
2610 * relative to pwsi->buffer depending on "flag" value. Returns size
2611 * of the data copied (also in the pwsi->buflen).
2614 int size = hostent_size(p_he);
2616 if( size )
2618 struct ws_hostent* p_to;
2619 char* p_name,*p_aliases,*p_addr,*p_base,*p;
2621 _check_buffer_he(pwsi, size);
2622 p_to = (struct ws_hostent*)pwsi->he;
2623 p = (char*)pwsi->he;
2624 p_base = (flag & WS_DUP_OFFSET) ? NULL
2625 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2626 p += sizeof(struct ws_hostent);
2627 p_name = p;
2628 strcpy(p, p_he->h_name); p += strlen(p) + 1;
2629 p_aliases = p;
2630 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)pwsi->he), 0);
2631 p_addr = p;
2632 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)pwsi->he), p_he->h_length);
2634 p_to->h_addrtype = (INT16)p_he->h_addrtype;
2635 p_to->h_length = (INT16)p_he->h_length;
2636 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->he));
2637 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->he));
2638 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)pwsi->he));
2640 size += (sizeof(struct ws_hostent) - sizeof(struct hostent));
2642 return size;
2645 /* ----- protoent */
2647 static int protoent_size(struct protoent* p_pe)
2649 int size = 0;
2650 if( p_pe )
2651 { size = sizeof(struct protoent);
2652 size += strlen(p_pe->p_name) + 1;
2653 size += list_size(p_pe->p_aliases, 0); }
2654 return size;
2657 int WS_dup_pe(LPWSINFO pwsi, struct protoent* p_pe, int flag)
2659 int size = protoent_size(p_pe);
2660 if( size )
2662 struct ws_protoent* p_to;
2663 char* p_name,*p_aliases,*p_base,*p;
2665 _check_buffer_pe(pwsi, size);
2666 p_to = (struct ws_protoent*)pwsi->pe;
2667 p = (char*)pwsi->pe;
2668 p_base = (flag & WS_DUP_OFFSET) ? NULL
2669 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2670 p += sizeof(struct ws_protoent);
2671 p_name = p;
2672 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
2673 p_aliases = p;
2674 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)pwsi->pe), 0);
2676 p_to->p_proto = (INT16)p_pe->p_proto;
2677 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)pwsi->pe);
2678 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)pwsi->pe));
2680 size += (sizeof(struct ws_protoent) - sizeof(struct protoent));
2682 return size;
2685 /* ----- servent */
2687 static int servent_size(struct servent* p_se)
2689 int size = 0;
2690 if( p_se )
2691 { size += sizeof(struct servent);
2692 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
2693 size += list_size(p_se->s_aliases, 0); }
2694 return size;
2697 int WS_dup_se(LPWSINFO pwsi, struct servent* p_se, int flag)
2699 int size = servent_size(p_se);
2700 if( size )
2702 struct ws_servent* p_to;
2703 char* p_name,*p_aliases,*p_proto,*p_base,*p;
2705 _check_buffer_se(pwsi, size);
2706 p_to = (struct ws_servent*)pwsi->se;
2707 p = (char*)pwsi->se;
2708 p_base = (flag & WS_DUP_OFFSET) ? NULL
2709 : ((flag & WS_DUP_SEGPTR) ? (char*)SEGPTR_GET(p) : p);
2710 p += sizeof(struct ws_servent);
2711 p_name = p;
2712 strcpy(p, p_se->s_name); p += strlen(p) + 1;
2713 p_proto = p;
2714 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
2715 p_aliases = p;
2716 list_dup(p_se->s_aliases, p, p_base + (p - (char*)pwsi->se), 0);
2718 p_to->s_port = (INT16)p_se->s_port;
2719 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)pwsi->se));
2720 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)pwsi->se));
2721 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)pwsi->se));
2723 size += (sizeof(struct ws_servent) - sizeof(struct servent));
2725 return size;
2728 /* ----------------------------------- error handling */
2730 UINT16 wsaErrno(void)
2732 int loc_errno = errno;
2733 #ifdef HAVE_STRERROR
2734 WARN(winsock, "errno %d, (%s).\n", loc_errno, strerror(loc_errno));
2735 #else
2736 WARN(winsock, "errno %d\n", loc_errno);
2737 #endif
2739 switch(loc_errno)
2741 case EINTR: return WSAEINTR;
2742 case EBADF: return WSAEBADF;
2743 case EACCES: return WSAEACCES;
2744 case EFAULT: return WSAEFAULT;
2745 case EINVAL: return WSAEINVAL;
2746 case EMFILE: return WSAEMFILE;
2747 case EWOULDBLOCK: return WSAEWOULDBLOCK;
2748 case EINPROGRESS: return WSAEINPROGRESS;
2749 case EALREADY: return WSAEALREADY;
2750 case ENOTSOCK: return WSAENOTSOCK;
2751 case EDESTADDRREQ: return WSAEDESTADDRREQ;
2752 case EMSGSIZE: return WSAEMSGSIZE;
2753 case EPROTOTYPE: return WSAEPROTOTYPE;
2754 case ENOPROTOOPT: return WSAENOPROTOOPT;
2755 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
2756 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
2757 case EOPNOTSUPP: return WSAEOPNOTSUPP;
2758 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
2759 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
2760 case EADDRINUSE: return WSAEADDRINUSE;
2761 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
2762 case ENETDOWN: return WSAENETDOWN;
2763 case ENETUNREACH: return WSAENETUNREACH;
2764 case ENETRESET: return WSAENETRESET;
2765 case ECONNABORTED: return WSAECONNABORTED;
2766 case EPIPE:
2767 case ECONNRESET: return WSAECONNRESET;
2768 case ENOBUFS: return WSAENOBUFS;
2769 case EISCONN: return WSAEISCONN;
2770 case ENOTCONN: return WSAENOTCONN;
2771 case ESHUTDOWN: return WSAESHUTDOWN;
2772 case ETOOMANYREFS: return WSAETOOMANYREFS;
2773 case ETIMEDOUT: return WSAETIMEDOUT;
2774 case ECONNREFUSED: return WSAECONNREFUSED;
2775 case ELOOP: return WSAELOOP;
2776 case ENAMETOOLONG: return WSAENAMETOOLONG;
2777 case EHOSTDOWN: return WSAEHOSTDOWN;
2778 case EHOSTUNREACH: return WSAEHOSTUNREACH;
2779 case ENOTEMPTY: return WSAENOTEMPTY;
2780 #ifdef EPROCLIM
2781 case EPROCLIM: return WSAEPROCLIM;
2782 #endif
2783 #ifdef EUSERS
2784 case EUSERS: return WSAEUSERS;
2785 #endif
2786 #ifdef EDQUOT
2787 case EDQUOT: return WSAEDQUOT;
2788 #endif
2789 #ifdef ESTALE
2790 case ESTALE: return WSAESTALE;
2791 #endif
2792 #ifdef EREMOTE
2793 case EREMOTE: return WSAEREMOTE;
2794 #endif
2796 /* just in case we ever get here and there are no problems */
2797 case 0: return 0;
2798 default:
2799 WARN(winsock, "Unknown errno %d!\n", loc_errno);
2800 return WSAEOPNOTSUPP;
2804 UINT16 wsaHerrno(void)
2806 int loc_errno = h_errno;
2808 WARN(winsock, "h_errno %d.\n", loc_errno);
2810 switch(loc_errno)
2812 case HOST_NOT_FOUND: return WSAHOST_NOT_FOUND;
2813 case TRY_AGAIN: return WSATRY_AGAIN;
2814 case NO_RECOVERY: return WSANO_RECOVERY;
2815 case NO_DATA: return WSANO_DATA;
2817 case 0: return 0;
2818 default:
2819 WARN(winsock,"Unknown h_errno %d!\n", loc_errno);
2820 return WSAEOPNOTSUPP;