Release 940505
[wine] / misc / winsocket.c
1 /*
2  * based on Windows Sockets 1.1 specs
3  * (ftp.microsoft.com:/Advsys/winsock/spec11/WINSOCK.TXT)
4  * 
5  * (C) 1993,1994 John Brezak, Erik Bos.
6  */
7  
8 #include <stdio.h>
9 #include <string.h>
10 #include <signal.h>
11 #include <sys/types.h>
12 #include <sys/ipc.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <netdb.h>
19 #include <unistd.h>
20 #include "heap.h"
21 #include "winsock.h"
22
23 #define DEBUG_WINSOCK
24
25 static WORD wsa_errno;
26 static int wsa_initted;
27 static key_t wine_key = 0;
28 static FARPROC BlockFunction;
29 static fd_set fd_in_use;
30
31 struct ipc_packet {
32         long    mtype;
33         HANDLE  handle;
34         HWND    hWnd;
35         WORD    wMsg;
36         LONG    lParam;
37 };
38 #define IPC_PACKET_SIZE (sizeof(struct ipc_packet) - sizeof(long))
39
40 #define MTYPE 0xb0b0eb05
41
42 struct WinSockHeap {
43         char    ntoa_buffer[32];
44
45         struct  hostent hostent_addr;
46         struct  hostent hostent_name;
47         struct  protoent protoent_name;
48         struct  protoent protoent_number;
49         struct  servent servent_name;
50         struct  servent servent_port;
51
52         struct  hostent WSAhostent_addr;
53         struct  hostent WSAhostent_name;
54         struct  protoent WSAprotoent_name;
55         struct  protoent WSAprotoent_number;    
56         struct  servent WSAservent_name;
57         struct  servent WSAservent_port;
58 };
59 static struct WinSockHeap *heap;
60
61 #define dump_sockaddr(a) \
62         fprintf(stderr, "sockaddr_in: family %d, address %s, port %d\n", \
63                         ((struct sockaddr_in *)a)->sin_family, \
64                         inet_ntoa(((struct sockaddr_in *)a)->sin_addr), \
65                         ntohs(((struct sockaddr_in *)a)->sin_port))
66
67 static WORD wsaerrno(void)
68 {
69 #ifdef DEBUG_WINSOCK
70 #ifndef sun
71 #if defined(__FreeBSD__)
72                 fprintf(stderr, "winsock: errno %d, (%s).\n", 
73                                         errno, sys_errlist[errno]);
74 #else
75                 fprintf(stderr, "winsock: errno %d, (%s).\n", 
76                                         errno, strerror(errno));
77 #endif
78 #else
79                 fprintf(stderr, "winsock: errno %d\n", errno);
80 #endif
81 #endif
82
83         switch(errno)
84         {
85         case EINTR:             return WSAEINTR;
86         case EACCES:            return WSAEACCES;
87         case EFAULT:            return WSAEFAULT;
88         case EINVAL:            return WSAEINVAL;
89         case EMFILE:            return WSAEMFILE;
90         case EWOULDBLOCK:       return WSAEWOULDBLOCK;
91         case EINPROGRESS:       return WSAEINPROGRESS;
92         case EALREADY:          return WSAEALREADY;
93         case EBADF:
94         case ENOTSOCK:          return WSAENOTSOCK;
95         case EDESTADDRREQ:      return WSAEDESTADDRREQ;
96         case EMSGSIZE:          return WSAEMSGSIZE;
97         case EPROTOTYPE:        return WSAEPROTOTYPE;
98         case ENOPROTOOPT:       return WSAENOPROTOOPT;
99         case EPROTONOSUPPORT:   return WSAEPROTONOSUPPORT;
100         case ESOCKTNOSUPPORT:   return WSAESOCKTNOSUPPORT;
101         case EOPNOTSUPP:        return WSAEOPNOTSUPP;
102         case EPFNOSUPPORT:      return WSAEPFNOSUPPORT;
103         case EAFNOSUPPORT:      return WSAEAFNOSUPPORT;
104         case EADDRINUSE:        return WSAEADDRINUSE;
105         case EADDRNOTAVAIL:     return WSAEADDRNOTAVAIL;
106         case ENETDOWN:          return WSAENETDOWN;
107         case ENETUNREACH:       return WSAENETUNREACH;
108         case ENETRESET:         return WSAENETRESET;
109         case ECONNABORTED:      return WSAECONNABORTED;
110         case ECONNRESET:        return WSAECONNRESET;
111         case ENOBUFS:           return WSAENOBUFS;
112         case EISCONN:           return WSAEISCONN;
113         case ENOTCONN:          return WSAENOTCONN;
114         case ESHUTDOWN:         return WSAESHUTDOWN;
115         case ETOOMANYREFS:      return WSAETOOMANYREFS;
116         case ETIMEDOUT:         return WSAETIMEDOUT;
117         case ECONNREFUSED:      return WSAECONNREFUSED;
118         case ELOOP:             return WSAELOOP;
119         case ENAMETOOLONG:      return WSAENAMETOOLONG;
120         case EHOSTDOWN:         return WSAEHOSTDOWN;
121         case EHOSTUNREACH:      return WSAEHOSTUNREACH;
122         case ENOTEMPTY:         return WSAENOTEMPTY;
123 /*      case EPROCLIM:          return WSAEPROCLIM; */
124         case EUSERS:            return WSAEUSERS;
125         case EDQUOT:            return WSAEDQUOT;
126         case ESTALE:            return WSAESTALE;
127         case EREMOTE:           return WSAEREMOTE;
128
129         default:
130                 fprintf(stderr, "winsock: unknown errorno %d!\n", errno);
131                 return WSAEOPNOTSUPP;
132         }
133 }
134
135 static WORD errno_to_wsaerrno(void)
136 {
137         wsa_errno = wsaerrno();
138 }
139  
140 SOCKET Winsock_accept(SOCKET s, struct sockaddr FAR *addr, INT FAR *addrlen)
141 {
142         int sock;
143
144 #ifdef DEBUG_WINSOCK
145         fprintf(stderr, "WSA_accept: socket %d, ptr %8x, length %d\n", s, (int) addr, addrlen);
146 #endif
147
148         if ((sock = accept(s, addr, (int *) addrlen)) < 0) {
149                 errno_to_wsaerrno();
150                 return INVALID_SOCKET;
151         }
152         return sock;
153 }
154
155 INT Winsock_bind(SOCKET s, struct sockaddr FAR *name, INT namelen)
156 {
157 #ifdef DEBUG_WINSOCK
158         fprintf(stderr, "WSA_bind: socket %d, ptr %8x, length %d\n", s, (int) name, namelen);
159         dump_sockaddr(name);
160 #endif
161
162         if (bind(s, name, namelen) < 0) {
163                 errno_to_wsaerrno();
164                 return SOCKET_ERROR;
165         }
166         return 0;
167 }
168
169 INT Winsock_closesocket(SOCKET s)
170 {
171 #ifdef DEBUG_WINSOCK
172         fprintf(stderr, "WSA_closesocket: socket %d\n", s);
173 #endif
174
175         FD_CLR(s, &fd_in_use);
176
177         if (close(s) < 0) {
178                 errno_to_wsaerrno();
179                 return SOCKET_ERROR;
180         }
181         return 0;
182 }
183
184 INT Winsock_connect(SOCKET s, struct sockaddr FAR *name, INT namelen)
185 {
186 #ifdef DEBUG_WINSOCK
187         fprintf(stderr, "WSA_connect: socket %d, ptr %8x, length %d\n", s, (int) name, namelen);
188         dump_sockaddr(name);
189 #endif
190
191         if (connect(s, name, namelen) < 0) {
192                 errno_to_wsaerrno();
193                 return SOCKET_ERROR;
194         }
195         return 0;
196 }
197
198 INT Winsock_getpeername(SOCKET s, struct sockaddr FAR *name, INT FAR *namelen)
199 {
200 #ifdef DEBUG_WINSOCK
201         fprintf(stderr, "WSA_getpeername: socket: %d, ptr %8x, ptr %8x\n", s, (int) name, *namelen);
202         dump_sockaddr(name);
203 #endif
204
205         if (getpeername(s, name, (int *) namelen) < 0) {
206                 errno_to_wsaerrno();
207                 return SOCKET_ERROR;
208         }
209         return 0;
210 }
211
212 INT Winsock_getsockname(SOCKET s, struct sockaddr FAR *name, INT FAR *namelen)
213 {
214 #ifdef DEBUG_WINSOCK
215         fprintf(stderr, "WSA_getsockname: socket: %d, ptr %8x, ptr %8x\n", s, (int) name, (int) *namelen);
216 #endif
217         if (getsockname(s, name, (int *) namelen) < 0) {
218                 errno_to_wsaerrno();
219                 return SOCKET_ERROR;
220         }
221         return 0;
222 }
223
224 INT Winsock_getsockopt(SOCKET s, INT loptname, char FAR *optval, INT FAR *optlen)
225 {
226 #ifdef DEBUG_WINSOCK
227         fprintf(stderr, "WSA_getsockopt: socket: %d, opt %d, ptr %8x, ptr %8x\n", s, loptname, (int) optval, (int) *optlen);
228 #endif
229         if (getsockopt(s, 0, (int) loptname, optval, (int *) optlen) < 0) {
230                 errno_to_wsaerrno();
231                 return SOCKET_ERROR;
232         }
233         return 0;
234 }
235
236 u_long Winsock_htonl(u_long hostlong)
237 {
238         return( htonl(hostlong) );
239 }         
240
241 u_short Winsock_htons(u_short hostshort)
242 {
243         return( htons(hostshort) );
244 }
245
246 u_long Winsock_inet_addr(char FAR *cp)
247 {
248         return( inet_addr(cp) );
249 }
250
251 char *Winsock_inet_ntoa(struct in_addr in)
252 {
253         char *s;
254
255 #ifdef DEBUG_WINSOCK
256         fprintf(stderr, "WSA_inet_ntoa: %8x\n", in);
257 #endif
258
259         if ((s = inet_ntoa(in)) == NULL) {
260                 errno_to_wsaerrno();
261                 return NULL;
262         }
263
264         strncpy(heap->ntoa_buffer, s, sizeof(heap->ntoa_buffer) );
265
266         return (char *) &heap->ntoa_buffer;
267 }
268
269 INT Winsock_ioctlsocket(SOCKET s, long cmd, u_long FAR *argp)
270 {
271 #ifdef DEBUG_WINSOCK
272         fprintf(stderr, "WSA_ioctl: socket %d, cmd %d, ptr %8x\n", s, cmd, (int) argp);
273 #endif
274
275         if (ioctl(s, cmd, argp) < 0) {
276                 errno_to_wsaerrno();
277                 return SOCKET_ERROR;
278         }
279         return 0;
280 }
281
282 INT Winsock_listen(SOCKET s, INT backlog)
283 {
284 #ifdef DEBUG_WINSOCK
285         fprintf(stderr, "WSA_listen: socket %d, backlog %d\n", s, backlog);
286 #endif
287
288         if (listen(s, backlog) < 0) {
289                 errno_to_wsaerrno();
290                 return SOCKET_ERROR;
291         }
292         return 0;
293 }
294
295 u_long Winsock_ntohl(u_long netlong)
296 {
297         return( ntohl(netlong) );
298 }
299
300 u_short Winsock_ntohs(u_short netshort)
301 {
302         return( ntohs(netshort) );
303 }
304
305 INT Winsock_recv(SOCKET s, char FAR *buf, INT len, INT flags)
306 {
307         int length;
308
309 #ifdef DEBUG_WINSOCK
310         fprintf(stderr, "WSA_recv: socket %d, ptr %8x, length %d, flags %d\n", s, (int) buf, len, flags);
311 #endif
312
313         if ((length = recv(s, buf, len, flags)) < 0) {
314                 errno_to_wsaerrno();
315                 return SOCKET_ERROR;
316         }
317         return length;
318 }
319
320 INT Winsock_recvfrom(SOCKET s, char FAR *buf, INT len, INT flags, 
321                 struct sockaddr FAR *from, int FAR *fromlen)
322 {
323         int length;
324
325 #ifdef DEBUG_WINSOCK
326         fprintf(stderr, "WSA_recvfrom: socket %d, ptr %8x, length %d, flags %d\n", s, buf, len, flags);
327 #endif
328
329         if ((length = recvfrom(s, buf, len, flags, from, fromlen)) < 0) {
330                 errno_to_wsaerrno();
331                 return SOCKET_ERROR;
332         }
333         return length;
334 }
335
336 INT Winsock_select(INT nfds, fd_set FAR *readfds, fd_set FAR *writefds,
337         fd_set FAR *exceptfds, struct timeval FAR *timeout)
338 {
339 #ifdef DEBUG_WINSOCK
340         fprintf(stderr, "WSA_select: fd # %d, ptr %8x, ptr %8x, ptr %*X\n", nfds, readfds, writefds, exceptfds);
341 #endif
342
343         return(select(nfds, readfds, writefds, exceptfds, timeout));
344 }
345
346 INT Winsock_send(SOCKET s, char FAR *buf, INT len, INT flags)
347 {
348         int length;
349
350 #ifdef DEBUG_WINSOCK
351         fprintf(stderr, "WSA_send: socket %d, ptr %8x, length %d, flags %d\n", s, buf, len, flags);
352 #endif
353
354         if ((length = send(s, buf, len, flags)) < 0) {
355                 errno_to_wsaerrno();
356                 return SOCKET_ERROR;
357         }
358         return length;
359 }
360
361 INT Winsock_sendto(SOCKET s, char FAR *buf, INT len, INT flags,
362                 struct sockaddr FAR *to, INT tolen)
363 {
364         int length;
365
366 #ifdef DEBUG_WINSOCK
367         fprintf(stderr, "WSA_sendto: socket %d, ptr %8x, length %d, flags %d\n", s, buf, len, flags);
368 #endif
369
370         if ((length = sendto(s, buf, len, flags, to, tolen)) < 0) {
371                 errno_to_wsaerrno();
372                 return SOCKET_ERROR;
373         }
374         return length;
375 }
376
377 INT Winsock_setsockopt(SOCKET s, INT level, INT optname, const char FAR *optval, 
378                         INT optlen)
379 {
380 #ifdef DEBUG_WINSOCK
381         fprintf(stderr, "WSA_setsockopt: socket %d, level %d, opt %d, ptr %8x, len %d\n", s, level, optname, (int) optval, optlen);
382 #endif
383
384         if (setsockopt(s, level, optname, optval, optlen) < 0) {
385                 errno_to_wsaerrno();
386                 return SOCKET_ERROR;
387         }
388         return 0;
389 }                                         
390
391 INT Winsock_shutdown(SOCKET s, INT how)
392 {
393 #ifdef DEBUG_WINSOCK
394         fprintf(stderr, "WSA_shutdown: socket s %d, how %d\n", s, how);
395 #endif
396
397         if (shutdown(s, how) < 0) {
398                 errno_to_wsaerrno();
399                 return SOCKET_ERROR;
400         }
401         return 0;
402 }
403
404 SOCKET Winsock_socket(INT af, INT type, INT protocol)
405 {
406     int sock;
407
408 #ifdef DEBUG_WINSOCK
409     fprintf(stderr, "WSA_socket: af=%d type=%d protocol=%d\n", af, type, protocol);
410 #endif
411
412     if ((sock = socket(af, type, protocol)) < 0) {
413             errno_to_wsaerrno();
414 #ifdef DEBUG_WINSOCK
415     fprintf(stderr, "WSA_socket: failed !\n");
416 #endif
417             return INVALID_SOCKET;
418     }
419     
420     if (sock > 0xffff) {
421         wsa_errno = WSAEMFILE;
422         return INVALID_SOCKET;
423     }
424
425     FD_SET(sock, &fd_in_use);
426
427 #ifdef DEBUG_WINSOCK
428     fprintf(stderr, "WSA_socket: fd %d\n", sock);
429 #endif
430     return sock;
431 }
432
433 struct hostent *Winsock_gethostbyaddr(const char FAR *addr, INT len, INT type)
434 {
435         struct hostent *host;
436
437 #ifdef DEBUG_WINSOCK
438         fprintf(stderr, "WSA_gethostbyaddr: ptr %8x, len %d, type %d\n", (int) addr, len, type);
439 #endif
440
441         if ((host = gethostbyaddr(addr, len, type)) == NULL) {
442                 errno_to_wsaerrno();
443                 return NULL;
444         }
445         memcpy(&heap->hostent_addr, host, sizeof(struct hostent));
446
447         return (struct hostent *) &heap->hostent_addr;
448 }
449
450 struct hostent *Winsock_gethostbyname(const char FAR *name)
451 {
452         struct hostent *host;
453
454 #ifdef DEBUG_WINSOCK
455         fprintf(stderr, "WSA_gethostbyname: name %s\n", name);
456 #endif
457
458         if ((host = gethostbyname(name)) == NULL) {
459                 errno_to_wsaerrno();
460                 return NULL;
461         }
462         memcpy(&heap->hostent_name, host, sizeof(struct hostent));
463
464         return (struct hostent *) &heap->hostent_name;
465 }
466
467 int Winsock_gethostname(char FAR *name, INT namelen)
468 {
469
470 #ifdef DEBUG_WINSOCK
471         fprintf(stderr, "WSA_gethostname: name %d, len %d\n", name, namelen);
472 #endif
473
474         if (gethostname(name, namelen) < 0) {
475                 errno_to_wsaerrno();
476                 return SOCKET_ERROR;
477         }
478         return 0;
479 }          
480
481 struct protoent *Winsock_getprotobyname(char FAR *name)
482 {
483         struct protoent *proto;
484
485 #ifdef DEBUG_WINSOCK
486         fprintf(stderr, "WSA_getprotobyname: name %s\n", name);
487 #endif
488
489         if ((proto = getprotobyname(name)) == NULL) {
490                 errno_to_wsaerrno();
491                 return NULL;
492         }
493         memcpy(&heap->protoent_name, proto, sizeof(struct protoent));
494
495         return (struct protoent *) &heap->protoent_name;
496 }
497
498 struct protoent *Winsock_getprotobynumber(INT number)
499 {
500         struct protoent *proto;
501
502 #ifdef DEBUG_WINSOCK
503         fprintf(stderr, "WSA_getprotobynumber: num %d\n", number);
504 #endif
505
506         if ((proto = getprotobynumber(number)) == NULL) {
507                 errno_to_wsaerrno();
508                 return NULL;
509         }
510         memcpy(&heap->protoent_number, proto, sizeof(struct protoent));
511
512         return (struct protoent *) &heap->protoent_number;
513 }
514
515 struct servent *Winsock_getservbyname(const char FAR *name, const char FAR *proto)
516 {
517         struct servent *service;
518
519 #ifdef DEBUG_WINSOCK
520         fprintf(stderr, "WSA_getservbyname: name %s, proto %s\n", name, proto);
521 #endif
522
523         if ((service = getservbyname(name, proto)) == NULL) {
524                 errno_to_wsaerrno();
525                 return NULL;
526         }
527         memcpy(&heap->servent_name, service, sizeof(struct servent));
528
529         return (struct servent *) &heap->servent_name;
530 }
531
532 struct servent *Winsock_getservbyport(INT port, const char FAR *proto)
533 {
534         struct servent *service;
535
536 #ifdef DEBUG_WINSOCK
537         fprintf(stderr, "WSA_getservbyport: port %d, name %s\n", port, proto);
538 #endif
539
540         if ((service = getservbyport(port, proto)) == NULL) {
541                 errno_to_wsaerrno();
542                 return NULL;
543         }
544         memcpy(&heap->servent_port, service, sizeof(struct servent));
545
546         return (struct servent *) &heap->servent_port;
547 }
548
549 /******************** winsock specific functions ************************
550  *
551  */
552
553 #define AllocWSAHandle() getpid()
554
555 static void recv_message(int sig)
556 {
557         struct ipc_packet message;
558
559         if (msgrcv(wine_key, &message, IPC_PACKET_SIZE, MTYPE, IPC_NOWAIT) == -1)
560                 perror("wine: msgrcv");
561
562         fprintf(stderr, 
563                 "WSA: PostMessage (hwnd %d, wMsg %d, wParam %d, lParam %d)\n",
564                 message.hWnd,
565                 message.wMsg,
566                 message.handle,
567                 message.lParam);
568
569         PostMessage(message.hWnd, message.wMsg, message.handle, message.lParam);
570                 
571         signal(SIGUSR1, recv_message);
572 }
573
574
575 static void send_message(HANDLE handle, HWND hWnd, u_int wMsg, long lParam)
576 {
577         struct ipc_packet message;
578         
579         message.mtype = MTYPE;
580         message.handle = handle;
581         message.hWnd = hWnd;
582         message.wMsg = wMsg;
583         message.lParam = lParam;
584
585         fprintf(stderr, 
586                 "WSA: send (hwnd %d, wMsg %d, handle %d, lParam %d)\n",
587                 hWnd, wMsg, handle, lParam);
588         
589         if (msgsnd(wine_key, &message,  IPC_PACKET_SIZE, IPC_NOWAIT) == -1)
590                 perror("wine: msgsnd");
591                 
592         kill(getppid(), SIGUSR1);
593 }
594
595
596 HANDLE WSAAsyncGetHostByAddr(HWND hWnd, u_int wMsg, const char FAR *addr,
597                  INT len, INT type, char FAR *buf, INT buflen)
598 {
599         HANDLE handle;
600         struct hostent *host;
601
602         handle = AllocWSAHandle();
603
604         if (fork()) {
605                 return handle;
606         } else {
607                 if ((host = gethostbyaddr(addr, len, type)) == NULL) {
608                         send_message(hWnd, wMsg, handle, wsaerrno() << 16);
609                         exit(0);
610                 }
611                 memcpy(buf, host, buflen);
612                 send_message(hWnd, wMsg, handle, 0);
613                 exit(0);
614         }
615 }
616
617
618 HANDLE WSAAsyncGetHostByName(HWND hWnd, u_int wMsg, const char FAR *name, 
619                         char FAR *buf, INT buflen)
620 {
621         HANDLE handle;
622         struct hostent *host;
623
624         handle = AllocWSAHandle();
625
626         if (fork()) {
627                 return handle;
628         } else {
629                 if ((host = gethostbyname(name)) == NULL) {
630                         send_message(hWnd, wMsg, handle, wsaerrno() << 16);
631                         exit(0);
632                 }
633                 memcpy(buf, host, buflen);
634                 send_message(hWnd, wMsg, handle, 0);
635                 exit(0);
636         }
637 }                     
638
639
640 HANDLE WSAAsyncGetProtoByName(HWND hWnd, u_int wMsg, const char FAR *name, 
641                         char FAR *buf, INT buflen)
642 {
643         HANDLE handle;
644         struct protoent *proto;
645
646         handle = AllocWSAHandle();
647
648         if (fork()) {
649                 return handle;
650         } else {
651                 if ((proto = getprotobyname(name)) == NULL) {
652                         send_message(hWnd, wMsg, handle, wsaerrno() << 16);
653                         exit(0);
654                 }
655                 memcpy(buf, proto, buflen);
656                 send_message(hWnd, wMsg, handle, 0);
657                 exit(0);
658         }
659 }
660
661
662 HANDLE WSAAsyncGetProtoByNumber(HWND hWnd, u_int wMsg, INT number, 
663                         char FAR *buf, INT buflen)
664 {
665         HANDLE handle;
666         struct protoent *proto;
667
668         handle = AllocWSAHandle();
669
670         if (fork()) {
671                 return handle;
672         } else {
673                 if ((proto = getprotobynumber(number)) == NULL) {
674                         send_message(hWnd, wMsg, handle, wsaerrno() << 16);
675                         exit(0);
676                 }
677                 memcpy(buf, proto, buflen);
678                 send_message(hWnd, wMsg, handle, 0);
679                 exit(0);
680         }
681 }
682
683
684 HANDLE WSAAsyncGetServByName(HWND hWnd, u_int wMsg, const char FAR *name, 
685                         const char FAR *proto, char FAR *buf, INT buflen)
686 {
687         HANDLE handle;
688         struct servent *service;
689
690         handle = AllocWSAHandle();
691
692         if (fork()) {
693                 return handle;
694         } else {
695                 if ((service = getservbyname(name, proto)) == NULL) {
696                         send_message(hWnd, wMsg, handle, wsaerrno() << 16);
697                         exit(0);
698                 }
699                 memcpy(buf, service, buflen);
700                 send_message(hWnd, wMsg, handle, 0);
701                 exit(0);
702         }
703 }
704
705
706 HANDLE WSAAsyncGetServByPort(HWND hWnd, u_int wMsg, INT port, const char FAR 
707                         *proto, char FAR *buf, INT buflen)
708 {
709         HANDLE handle;
710         struct servent *service;
711
712         handle = AllocWSAHandle();
713
714         if (fork()) {
715                 return handle;
716         } else {
717                 if ((service = getservbyport(port, proto)) == NULL) {
718                         send_message(hWnd, wMsg, handle, wsaerrno() << 16);
719                         exit(0);
720                 }
721                 memcpy(buf, service, buflen);
722                 send_message(hWnd, wMsg, handle, 0);
723                 exit(0);
724         }
725 }
726
727 INT WSAAsyncSelect(SOCKET s, HWND hWnd, u_int wMsg, long lEvent)
728 {
729         long event;
730         fd_set read_fds, write_fds, except_fds;
731
732 #ifdef DEBUG_WINSOCK
733         fprintf(stderr, "WSA_AsyncSelect: socket %d, HWND %d, wMsg %d, event %d\n", s, hWnd, wMsg, lEvent);
734 #endif
735
736         /* remove outstanding asyncselect() processes */
737         /* kill */
738
739         if (wMsg == 0 && lEvent == 0) 
740                 return 0;
741
742         if (fork()) {
743                 return 0;
744         } else {
745                 while (1) {
746                         FD_ZERO(&read_fds);
747                         FD_ZERO(&write_fds);
748                         FD_ZERO(&except_fds);
749
750                         if (lEvent & FD_READ)
751                                 FD_SET(s, &read_fds);
752                         if (lEvent & FD_WRITE)
753                                 FD_SET(s, &write_fds);
754
755                         fcntl(s, F_SETFL, O_NONBLOCK);
756                         select(s + 1, &read_fds, &write_fds, &except_fds, NULL);
757
758                         event = 0;
759                         if (FD_ISSET(s, &read_fds))
760                                 event |= FD_READ;
761                         if (FD_ISSET(s, &write_fds))
762                                 event |= FD_WRITE;
763
764                         send_message(hWnd, wMsg, s, (wsaerrno() << 16) | event);
765                 }
766         }
767 }
768
769 INT WSAFDIsSet(INT fd, fd_set *set)
770 {
771         return( FD_ISSET(fd, set) );
772 }
773
774 INT WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
775 {
776 #ifdef DEBUG_WINSOCK
777         fprintf(stderr, "WSA_AsyncRequest: handle %d\n", hAsyncTaskHandle);
778 #endif
779
780         return 0;
781 }
782
783 INT WSACancelBlockingCall(void)
784 {
785 #ifdef DEBUG_WINSOCK
786         fprintf(stderr, "WSA_CancelBlockCall\n");
787 #endif
788         return 0;
789 }
790           
791 INT WSAGetLastError(void)
792 {
793 #ifdef DEBUG_WINSOCK
794         fprintf(stderr, "WSA_GetLastError\n");
795 #endif
796
797     return wsa_errno;
798 }
799
800 void WSASetLastError(INT iError)
801 {
802 #ifdef DEBUG_WINSOCK
803         fprintf(stderr, "WSA_SetLastErorr %d\n", iError);
804 #endif
805
806     wsa_errno = iError;
807 }
808
809 BOOL WSAIsBlocking(void)
810 {
811 #ifdef DEBUG_WINSOCK
812         fprintf(stderr, "WSA_IsBlocking\n");
813 #endif
814 }
815
816 FARPROC WSASetBlockingHook(FARPROC lpBlockFunc)
817 {
818 #ifdef DEBUG_WINSOCK
819         fprintf(stderr, "WSA_SetBlockHook %8x, STUB!\n", lpBlockFunc);
820 #endif
821         BlockFunction = lpBlockFunc;
822
823         return lpBlockFunc;
824 }
825
826 INT WSAUnhookBlockingHook(void)
827 {
828 #ifdef DEBUG_WINSOCK
829         fprintf(stderr, "WSA_UnhookBlockingHook\n");
830 #endif
831         BlockFunction = NULL;
832
833         return 0;
834 }
835
836 WSADATA Winsock_data = {
837         0x0101,
838         0x0101,
839         "WINE Sockets",
840 #ifdef linux
841         "LINUX/i386",
842 #endif
843 #ifdef __NetBSD__
844         "NetBSD/i386",
845 #endif
846 #ifdef sunos
847         "SunOS",
848 #endif
849         128,
850         1024,
851         NULL
852 };
853
854 INT WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData)
855 {
856     int HeapHandle;
857     MDESC *MyHeap;
858
859 #ifdef DEBUG_WINSOCK
860     fprintf(stderr, "WSAStartup: verReq=%x\n", wVersionRequested);
861 #endif
862
863     if (LOBYTE(wVersionRequested) < 1 ||
864         (LOBYTE(wVersionRequested) == 1 &&
865          HIBYTE(wVersionRequested) < 1))
866         return WSAVERNOTSUPPORTED;
867
868     if (!lpWSAData)
869         return WSAEINVAL;
870
871         /* alloc winsock heap */
872
873     if ((HeapHandle = GlobalAlloc(GMEM_FIXED,sizeof(struct WinSockHeap))) == 0)
874         return WSASYSNOTREADY;
875
876     heap = (struct WinSockHeap *) GlobalLock(HeapHandle);
877     HEAP_Init(&MyHeap, heap, sizeof(struct WinSockHeap));
878     bcopy(&Winsock_data, lpWSAData, sizeof(Winsock_data));
879
880     /* ipc stuff */
881
882     if ((wine_key = msgget(IPC_PRIVATE, 0600)) == -1)
883         perror("wine: msgget");
884
885     signal(SIGUSR1, recv_message);
886
887     /* clear */
888     
889     FD_ZERO(&fd_in_use);
890
891     wsa_initted = 1;
892     return(0);
893 }
894
895 INT WSACleanup(void)
896 {
897         int fd;
898
899         if (wine_key)
900                 if (msgctl(wine_key, IPC_RMID, NULL) == -1)
901                         perror("wine: shmctl");
902
903         for (fd = 0; fd != FD_SETSIZE; fd++)
904                 if (FD_ISSET(fd, &fd_in_use))
905                         close(fd);
906
907         wsa_initted = 0;
908         return 0;
909 }