source: trunk/src/wsock32/wsock32.cpp@ 120

Last change on this file since 120 was 120, checked in by sandervl, 26 years ago

Include os2wrap.h instead of os2.h

File size: 28.0 KB
Line 
1/* $Id: wsock32.cpp,v 1.3 1999-06-19 10:54:48 sandervl Exp $ */
2
3/*
4 *
5 * Project Odin Software License can be found in LICENSE.TXT
6 *
7 */
8/*
9 * Win32 SOCK32 for OS/2
10 *
11 * 1998/08/25 Vince Vielhaber
12 *
13 * @(#) wsock32.c 1.0.0 1998/08/25 VV initial release
14 * 1.0.1 1999/04/27 VV cleanup and implement select.
15 *
16 */
17
18#define INCL_DOSPROCESS /* Process and thread values */
19#define INCL_DOSFILEMGR /* for DosRead and DosWrite */
20#define INCL_DOSQUEUES /* for unnamed pipes */
21#define INCL_DOSERRORS /* DOS error values */
22#define INCL_WINMESSAGEMGR /* Window Message Functions */
23#define INCL_WINMENUS /* Window Menu Functions */
24
25/* this is for IBM TCP/IP 5.0 headers as present in the current Warp 4 toolkit */
26#define TCPV40HDRS 1
27
28#define VV_BSD_SELECT /* undefine this if it interferes with other routines */
29
30#ifdef VV_BSD_SELECT
31# define BSD_SELECT
32#endif
33
34#include <os2wrap.h> //Odin32 OS/2 api wrappers
35#include <stdio.h>
36#include <stdlib.h>
37#include <stddef.h>
38#include <string.h>
39#include <ctype.h>
40#include <types.h>
41#include <netdb.h>
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <netinet/in.h>
45
46#ifdef VV_BSD_SELECT
47# include <sys/select.h>
48#endif
49
50#include <sys/time.h>
51#include <win32type.h>
52#include "wsock32.h"
53#include "misc.h"
54
55
56#define FAR
57//#define WIN32API APIENTRY
58
59static WHOSTENT whsnt;
60static WSERVENT wsvnt;
61static WPROTOENT wptnt;
62
63 /*+-------------------------------------------------------------------+*/
64 /*| _CRT_init is the C run-time environment initialization function. |*/
65 /*|It will return 0 to indicate success and -1 to indicate failure. |*/
66 /*+-------------------------------------------------------------------+*/
67
68int _CRT_init (void);
69
70 /*+-------------------------------------------------------------------+*/
71 /*| _CRT_term is the C run-time environment termination function. |*/
72 /*+-------------------------------------------------------------------+*/
73
74void _CRT_term (unsigned long);
75
76void _System AsyncLoop(ULONG);
77void CheckThreads(AsyncStatus *);
78void NotifyApp(int,AsyncStatus *);
79int Notify(AsyncStatus *,int);
80
81int NotifyWSA(HWND hw,u_int msg,UINT wp,LONG lp);
82
83void _System WSAFunct(ULONG); // for the wsa database calls
84void SetErrForDatabaseCalls(void);
85
86
87size_t nSize;
88int *pArray;
89
90int MsgSent;
91int LocalErrorNumber = 0;
92
93
94TID tidAsyncLoop = 0; /* ID of AsyncSelect (AsyncLoop) thread */
95
96/*
97
98
99typedef struct AsyncStatus {
100 HWND hwnd; // owner's hwindow
101 u_int msg; // message to send when event occurs
102 long event; // event that may occur
103 SOCKET socket; // the socket
104 int status; // blocking yes/no
105 TID threadID; // Thread ID for async
106 int MsgStat; // has message been sent yet?
107 struct AsyncStatus *Next; // pointer to next AsyncStatus in the list
108 struct AsyncStatus *Prev; // pointer to previous AsyncStatus in the list
109} AsyncStatus;
110
111
112
113*/
114
115AsyncStatus *TopASY = 0;
116
117
118#define BLOCKING 0
119#define NONBLOCKING 1
120
121
122
123typedef struct WSAStruct {
124 int CallingWhat;
125 HWND hw;
126 u_int msg;
127 char *carg1;
128 char *carg2;
129 int iarg1;
130 int iarg2;
131 char *buf;
132 int buflen;
133} WSAStruct;
134
135
136#define GETSERVBYNAME 1
137#define GETSERVBYPORT 2
138#define GETPROTOBYNAME 3
139#define GETPROTOBYNUMBER 4
140#define GETHOSTBYNAME 5
141#define GETHOSTBYADDR 6
142
143
144
145
146typedef struct PipeStruct {
147 AsyncStatus as;
148 int MsgLoop;
149 HFILE rcv;
150 HFILE snd;
151} PipeStruct;
152
153PipeStruct PS;
154
155
156
157#ifdef FD_CLR
158#undef FD_CLR
159#define FD_CLR(x,y) WFD_CLR(x,y)
160#undef FD_SET
161#define FD_SET(x,y) WFD_SET(x,y)
162#undef FD_ZERO
163#define FD_ZERO(x) WFD_ZERO(x)
164#undef FD_ISSET
165#define FD_ISSET(x,y) WFD_SET(x,y)
166#endif
167
168
169
170
171
172
173int WIN32API OS2__WSAFDIsSet(SOCKET fd, Wfd_set FAR *set)
174{
175int i = set->fd_count;
176
177#ifdef DEBUG
178 WriteLog("WSOCK32: __WSAFDIsSet\n");
179#endif
180
181 while (i--)
182 if (set->fd_array[i] == fd)
183 return 1;
184
185return 0;
186}
187
188
189
190AsyncStatus * FindASY(SOCKET s)
191{
192AsyncStatus *as;
193
194 for(as = TopASY; as; as = as->Next)
195 if(as->socket == s)
196 return as;
197
198return 0;
199}
200
201
202
203SOCKET WIN32API OS2accept (SOCKET s, struct sockaddr *addr,
204 int *addrlen) {
205#ifdef DEBUG
206 WriteLog("WSOCK32: accept\n");
207#endif
208
209 return accept(s,addr,addrlen);
210}
211
212int WIN32API OS2bind (SOCKET s, const struct sockaddr *addr, int namelen)
213{
214 int rc;
215
216#ifdef DEBUG
217 WriteLog("WSOCK32: bind\n");
218#endif
219
220 rc = bind(s,(struct sockaddr *)addr,namelen);
221 dprintf(("bind returned %X\n", rc));
222 return(rc);
223}
224
225int WIN32API OS2closesocket (SOCKET s)
226{
227AsyncStatus *as;
228#ifdef DEBUG
229 WriteLog("WSOCK32: closesocket\n");
230#endif
231
232 as = FindASY(s);
233 if(as == NULL) {
234 LocalErrorNumber = 10038;
235 return -1;
236 }
237
238 CheckThreads(as);
239
240#ifdef DEBUG
241 WriteLog("WSOCK32: closesocket\n");
242#endif
243 if(as->Prev && as->Next)
244 as->Prev->Prev = as->Next->Next; // I SURE HOPE THIS IS RIGHT!!!!!!!!
245 free(as);
246
247 return soclose((int)s);
248}
249
250int WIN32API OS2connect (SOCKET s, const struct sockaddr *name, int namelen)
251{
252#ifdef DEBUG
253//char message[512];
254//struct Wsockaddr_in *xname;
255
256 WriteLog("WSOCK32: connect\n");
257
258
259#endif
260
261 return connect(s,(struct sockaddr *)name,namelen);
262}
263
264int WIN32API OS2ioctlsocket (SOCKET s, long cmd, u_long *argp)
265{
266 int rc;
267
268#ifdef DEBUG
269 WriteLog("WSOCK32: ioctlsocket unimplemented\n");
270#endif
271
272 rc = ioctl(s, cmd, (char *)argp, 4);
273 dprintf(("ioctl returned %X\n", rc));
274 return(rc);
275}
276
277int WIN32API OS2getpeername (SOCKET s, struct sockaddr *name,
278 int * namelen)
279{
280#ifdef DEBUG
281 WriteLog("WSOCK32: getpeername\n");
282#endif
283
284 return getpeername(s,name,namelen);
285}
286
287int WIN32API OS2getsockname (SOCKET s, struct sockaddr *name,
288 int * namelen)
289{
290#ifdef DEBUG
291 WriteLog("WSOCK32: getsockname\n");
292#endif
293
294 return getsockname(s,name,namelen);
295}
296
297int WIN32API OS2getsockopt (SOCKET s, int level, int optname,
298 char * optval, int *optlen)
299{
300#ifdef DEBUG
301 WriteLog("WSOCK32: getsockopt\n");
302#endif
303
304 return getsockopt(s,level,optname,optval,optlen);
305}
306
307u_long WIN32API OS2htonl (u_long hostlong)
308{
309#ifdef DEBUG
310 WriteLog("WSOCK32: htonl\n");
311#endif
312
313 return htonl(hostlong);
314}
315
316u_short WIN32API OS2htons (u_short hostshort)
317{
318#ifdef DEBUG
319 WriteLog("WSOCK32: htons\n");
320#endif
321
322 return htons(hostshort);
323}
324
325unsigned long WIN32API OS2inet_addr (const char * cp)
326{
327#ifdef DEBUG
328 WriteLog("WSOCK32: inet_addr\n");
329#endif
330
331 return inet_addr((char *)cp);
332}
333
334char * WIN32API OS2inet_ntoa (struct in_addr in)
335{
336#ifdef DEBUG
337 WriteLog("WSOCK32: inet_ntoa\n");
338#endif
339
340 return inet_ntoa(in);
341}
342
343int WIN32API OS2listen (SOCKET s, int backlog)
344{
345#ifdef DEBUG
346 WriteLog("WSOCK32: listen\n");
347#endif
348
349 return listen(s,backlog);
350}
351
352u_long WIN32API OS2ntohl (u_long netlong)
353{
354#ifdef DEBUG
355 WriteLog("WSOCK32: ntohl\n");
356#endif
357
358 return ntohl(netlong);
359}
360
361u_short WIN32API OS2ntohs (u_short netshort)
362{
363#ifdef DEBUG
364 WriteLog("WSOCK32: ntohs\n");
365#endif
366
367 return ntohs(netshort);
368}
369
370int WIN32API OS2recv (SOCKET s, char * buf, int len, int flags)
371{
372unsigned long ii;
373int xx,yy;
374char buff[200];
375AsyncStatus *as;
376#ifdef DEBUG
377 WriteLog("WSOCK32: recv socket: %u\n",(unsigned int)s);
378 WriteLog("WSOCK32: recv len: %d\n",len);
379 WriteLog("WSOCK32: recv flags: %d\n",flags);
380
381#endif
382
383 PS.MsgLoop = 0;
384
385 as = FindASY(s);
386
387 if(as != NULL) as->MsgStat = 2;
388
389
390 xx = recv(s,buf,len,flags);
391
392return xx;
393}
394
395
396
397int WIN32API OS2recvfrom (SOCKET s, char * buf, int len, int flags,
398 struct sockaddr *from, int * fromlen)
399{
400 int rc;
401
402#ifdef DEBUG
403 WriteLog("WSOCK32: recvfrom\n");
404#endif
405
406 rc = recvfrom(s,buf,len,flags,from,fromlen);
407 dprintf(("recvfrom returned %X\n", rc));
408 return(rc);
409}
410
411
412
413#ifdef VV_BSD_SELECT
414
415
416int WIN32API OS2select (int nfds, Wfd_set *readfds, Wfd_set *writefds,
417 Wfd_set *exceptfds, const struct Wtimeval *timeout)
418{
419#ifdef DEBUG
420 WriteLog("WSOCK32: select\n");
421#endif
422
423
424 return select(nfds,(fd_set *)readfds,(fd_set *)writefds,(fd_set *)exceptfds,(timeval *)timeout);
425
426}
427
428
429
430#else
431
432
433int WIN32API OS2select (int nfds, Wfd_set *readfds, Wfd_set *writefds,
434 Wfd_set *exceptfds, const struct Wtimeval *timeout)
435{
436#ifdef DEBUG
437 WriteLog("WSOCK32: select\n");
438#endif
439
440 // NEED TO DO THIS ONE!!!!!
441
442 return 0;
443}
444#endif
445
446
447
448int WIN32API OS2send (SOCKET s, const char * buf, int len, int flags)
449{
450#ifdef DEBUG
451 WriteLog("WSOCK32: send socket: %u\n",(unsigned int)s);
452// WriteLog("WSOCK32: send buf: %s\n",buf);
453 WriteLog("WSOCK32: send len: %d\n",len);
454 WriteLog("WSOCK32: send flags: %d\n",flags);
455#endif
456
457 return send(s,(char *)buf,len,flags);
458}
459
460int WIN32API OS2sendto (SOCKET s, const char * buf, int len, int flags,
461 const struct sockaddr *to, int tolen)
462{
463 int rc;
464
465#ifdef DEBUG
466 WriteLog("WSOCK32: sendto\n");
467#endif
468
469 rc = sendto(s,(char *)buf,len,flags,(struct sockaddr *)to,tolen);
470 dprintf(("sendto returned %X\n", rc));
471 return(rc);
472}
473
474int WIN32API OS2setsockopt (SOCKET s, int level, int optname,
475 const char * optval, int optlen)
476{
477struct Wlinger *yy;
478struct linger xx;
479
480#ifdef DEBUG
481 WriteLog("WSOCK32: setsockopt\n");
482#endif
483
484 if(level == SOL_SOCKET && optname == SO_LINGER) {
485 yy = (struct Wlinger *)optval;
486 xx.l_onoff = (int)yy->l_onoff;
487 xx.l_linger = (int)yy->l_linger;
488
489 return setsockopt(s,level,optname,(char *)&xx,optlen);
490 }
491
492 else return setsockopt(s,level,optname,(char *)optval,optlen);
493
494}
495
496int WIN32API OS2shutdown (SOCKET s, int how)
497{
498#ifdef DEBUG
499 WriteLog("WSOCK32: shutdown\n");
500#endif
501
502 return shutdown(s,how);
503}
504
505SOCKET WIN32API OS2socket (int af, int type, int protocol)
506{
507SOCKET s;
508AsyncStatus *as;
509#ifdef DEBUG
510 WriteLog("WSOCK32: socket\n");
511#endif
512
513 s = (SOCKET)socket(af,type,protocol);
514
515 if(s > 0) {
516 as = (AsyncStatus *)malloc(sizeof(AsyncStatus));
517 if(as != NULL) {
518 as->hwnd = (HWND)0;
519 as->msg = 0;
520 as->event = 0L;
521 as->socket = s;
522 as->status = BLOCKING;
523 as->threadID = 0;
524 as->MsgStat = 0;
525 as->Next = TopASY;
526 as->Prev = NULL;
527 if(TopASY) TopASY->Prev = as;
528 TopASY = as;
529 } else {
530 soclose(s);
531 return -1;
532 }
533 }
534 dprintf(("WSOCK32: socket returned %X\n", s));
535
536return s;
537}
538
539/* Database function prototypes */
540
541
542void SetErrForDatabaseCalls(void)
543{
544
545 switch(h_errno) {
546 case 1:
547 OS2WSASetLastError(11001); // host not found
548 break;
549 case 2:
550 OS2WSASetLastError(11002); // try again later
551 break;
552 case 3:
553 OS2WSASetLastError(11003); // No recovery
554 break;
555 case 4:
556 OS2WSASetLastError(11004); // No address or no data
557 break;
558 default:
559 OS2WSASetLastError(0); // unknown error and should never get here
560#ifdef DEBUG
561WriteLog("WSOCK32: Unknown error condition: %d\n",h_errno);
562#endif
563 break;
564 }
565
566return;
567}
568
569
570
571
572WHOSTENT * WIN32API OS2gethostbyaddr(const char * addr,
573 int len, int type)
574{
575WHOSTENT *yy;
576struct hostent *xx;
577#ifdef DEBUG
578 WriteLog("WSOCK32: gethostbyaddr\n");
579#endif
580
581 xx = gethostbyaddr((char *)addr,len,type);
582
583 if(xx == NULL) {
584 SetErrForDatabaseCalls();
585 return (WHOSTENT *)NULL;
586 }
587
588 whsnt.h_name = xx->h_name;
589 whsnt.h_aliases = xx->h_aliases;
590 whsnt.h_addrtype = (short)xx->h_addrtype;
591 whsnt.h_length = (short)xx->h_length;
592 whsnt.h_addr_list = xx->h_addr_list;
593
594return &whsnt;
595}
596
597
598
599
600WHOSTENT * WIN32API OS2gethostbyname(const char * name)
601{
602WHOSTENT *yy;
603struct hostent *xx;
604#ifdef DEBUG
605 WriteLog("WSOCK32: gethostbyname: %s\n",name);
606#endif
607
608 xx = gethostbyname((char *)name);
609
610 if(xx == NULL) {
611 SetErrForDatabaseCalls();
612 return (WHOSTENT *)NULL;
613 }
614
615 whsnt.h_name = xx->h_name;
616 whsnt.h_aliases = xx->h_aliases;
617 whsnt.h_addrtype = (short)xx->h_addrtype;
618 whsnt.h_length = (short)xx->h_length;
619 whsnt.h_addr_list = xx->h_addr_list;
620
621return &whsnt;
622}
623
624
625
626
627
628
629int WIN32API _OS2gethostname (char * name, int namelen)
630{
631#ifdef DEBUG
632 WriteLog("WSOCK32: _gethostname\n");
633#endif
634
635 return gethostname(name,namelen);
636}
637
638
639
640int WIN32API OS2gethostname (char * name, int namelen)
641{
642#ifdef DEBUG
643 WriteLog("WSOCK32: gethostname\n");
644#endif
645
646 return gethostname(name,namelen);
647}
648
649WSERVENT * WIN32API OS2getservbyport(int port, const char * proto)
650{
651struct servent *xx;
652#ifdef DEBUG
653 WriteLog("WSOCK32: getservbyport\n");
654#endif
655
656 xx = getservbyport(port,(char *)proto);
657
658 if(xx == NULL) { // this call doesn't generate an error message
659 return (WSERVENT *)NULL;
660 }
661
662 wsvnt.s_name = xx->s_name;
663 wsvnt.s_aliases = xx->s_aliases;
664 wsvnt.s_port = (short)xx->s_port;
665 wsvnt.s_proto = xx->s_proto;
666
667return &wsvnt;
668}
669
670WSERVENT * WIN32API OS2getservbyname(const char * name,
671 const char * proto)
672{
673WSERVENT *yy;
674struct servent *xx;
675#ifdef DEBUG
676 WriteLog("WSOCK32: getservbyname\n");
677#endif
678
679 xx = getservbyname((char *)name,(char *)proto);
680
681 if(xx == NULL) { // this call doesn't generate an error message
682 return (WSERVENT *)NULL;
683 }
684
685 wsvnt.s_name = xx->s_name;
686 wsvnt.s_aliases = xx->s_aliases;
687 wsvnt.s_port = (short)xx->s_port;
688 wsvnt.s_proto = xx->s_proto;
689
690return &wsvnt;
691}
692
693WPROTOENT * WIN32API OS2getprotobynumber(int proto)
694{
695struct protoent *xx;
696#ifdef DEBUG
697 WriteLog("WSOCK32: getprotobynumber\n");
698#endif
699
700 xx = getprotobynumber(proto);
701
702 if(xx == NULL) { // this call doesn't generate an error message
703 return (WPROTOENT *)NULL;
704 }
705
706 wptnt.p_name = xx->p_name;
707 wptnt.p_aliases = xx->p_aliases;
708 wptnt.p_proto = (short)xx->p_proto;
709
710return &wptnt;
711}
712
713WPROTOENT * WIN32API OS2getprotobyname(const char * name)
714{
715struct protoent *xx;
716#ifdef DEBUG
717 WriteLog("WSOCK32: getprotobyname\n");
718#endif
719
720 xx = getprotobyname((char *)name);
721
722 if(xx == NULL) { // this call doesn't generate an error message
723 return (WPROTOENT *)NULL;
724 }
725
726 wptnt.p_name = xx->p_name;
727 wptnt.p_aliases = xx->p_aliases;
728 wptnt.p_proto = (short)xx->p_proto;
729
730return &wptnt;
731}
732
733
734
735
736int WIN32API OS2WSAStartup(USHORT wVersionRequired, LPWSADATA lpWsaData)
737{
738APIRET rc;
739int ii;
740#ifdef DEBUG
741 WriteLog("WSOCK32: WSAStartup\n");
742#endif
743 /* Make sure that the version requested is >= 1.1. */
744 /* The low byte is the major version and the high */
745 /* byte is the minor version. */
746
747 if ( LOBYTE( wVersionRequired ) < 1 ||
748 ( LOBYTE( wVersionRequired ) == 1 &&
749 HIBYTE( wVersionRequired ) < 1 )) {
750 return WSAVERNOTSUPPORTED;
751 }
752
753 /* Since we only support 1.1, set both wVersion and */
754 /* wHighVersion to 1.1. */
755
756 lpWsaData->wVersion = MAKEWORD( 1, 1 );
757 lpWsaData->wHighVersion = MAKEWORD( 1, 1 );
758 strcpy(lpWsaData->szDescription,"Win32OS2 WSOCK32.DLL Ver. 1.1");
759 lpWsaData->iMaxUdpDg = 32767;
760 lpWsaData->iMaxSockets = 2048;
761 strcpy(lpWsaData->szSystemStatus,"No Status");
762
763 LocalErrorNumber = 0;
764
765 if(sock_init() == 0) {
766#ifdef DEBUG
767 WriteLog("WSOCK32: WSAStartup sock_init returned 0\n");
768#endif
769 return 0;
770 }
771 else ii = sock_errno();
772#ifdef DEBUG
773 WriteLog("WSOCK32: WSAStartup exiting: %d\n",ii);
774#endif
775return ii;
776}
777
778
779
780
781
782
783int WIN32API OS2WSACleanup(void)
784{
785#ifdef DEBUG
786 WriteLog("WSOCK32: WSACleanup\n");
787#endif
788
789 CheckThreads((AsyncStatus *)NULL);
790
791 return 0;
792}
793
794void WIN32API OS2WSASetLastError(int iError)
795{
796#ifdef DEBUG
797 WriteLog("WSOCK32: WSASetLastError(%d)\n",iError);
798#endif
799
800 LocalErrorNumber = iError;
801
802 return;
803}
804
805int WIN32API OS2WSAGetLastError(void)
806{
807int ii;
808#ifdef DEBUG
809 WriteLog("WSOCK32: WSAGetLastError\n");
810#endif
811
812 if(LocalErrorNumber == 0) {
813 ii = sock_errno(); // WSAGetLastError();
814#ifdef DEBUG
815 WriteLog("WSOCK32: WSAGetLastError: %d\n",ii);
816#endif
817 return ii;
818 }
819 else {
820#ifdef DEBUG
821 WriteLog("WSOCK32: WSAGetLastError: %d\n",LocalErrorNumber);
822#endif
823 return LocalErrorNumber;
824 }
825}
826
827BOOL WIN32API OS2WSAIsBlocking(void)
828{
829#ifdef DEBUG
830 WriteLog("WSOCK32: WSAIsBlocking unimplemented\n");
831#endif
832
833 return -5000; //WSAIsBlocking();
834}
835
836int WIN32API OS2WSAUnhookBlockingHook(void)
837{
838#ifdef DEBUG
839 WriteLog("WSOCK32: WSAUnhookBlockingHook unimplemented\n");
840#endif
841
842 return -5000; //WSAUnhookBlockingHook();
843}
844
845FARPROC WIN32API OS2WSASetBlockingHook(FARPROC lpBlockFunc)
846{
847#ifdef DEBUG
848 WriteLog("WSOCK32: WSASetBlockingHook Unimplemented\n");
849#endif
850
851 return (FARPROC)NULL;
852}
853
854int WIN32API OS2WSACancelBlockingCall(void)
855{
856#ifdef DEBUG
857 WriteLog("WSOCK32: WSACancelBlockingCall unimplemented\n");
858#endif
859
860 return -5000; //WSACancelBlockingCall();
861}
862
863
864
865
866
867// The following 6 calls need to start a new thread, perform
868// the operation, copy ALL the info to the buffer provided then
869// notify the sender.
870
871
872LHANDLE WIN32API OS2WSAAsyncGetServByName(HWND hWnd, u_int wMsg,
873 const char * name,
874 const char * proto,
875 char * buf, int buflen)
876{
877WSAStruct *wsa;
878PFNTHREAD pfnAsyncThread = &WSAFunct; /* Address of thread program */
879ULONG ulThreadParm = 100; /* Parameter to thread routine */
880APIRET rc = NO_ERROR; /* Return code */
881TID tid;
882
883
884OS2WSASetLastError(WSAEWOULDBLOCK);
885return 0;
886
887#ifdef DEBUG
888WriteLog("WSOCK32: WSAAsyncGetServByName. name: %s, proto: %s \n",name,proto);
889#endif
890
891 wsa = (WSAStruct *)malloc(sizeof(WSAStruct));
892 if(wsa == NULL) {
893 OS2WSASetLastError(WSAEWOULDBLOCK);
894 return 0;
895 }
896
897 wsa->CallingWhat = GETSERVBYNAME;
898 wsa->hw = hWnd;
899 wsa->msg = wMsg;
900 wsa->carg1 = strdup(name);
901 wsa->carg2 = strdup(proto);
902 wsa->buf = buf;
903 wsa->buflen = buflen;
904
905 ulThreadParm = (ULONG)wsa;
906
907 rc = DosCreateThread(&tid, /* Thread ID (returned by function) */
908 pfnAsyncThread, /* Address of thread program */
909 ulThreadParm, /* Parameter passed to ThreadProc */
910 CREATE_READY | /* Thread is ready when created */
911 STACK_SPARSE, /* Do not pre-commit stack pages */
912 8192L); /* Stack size, rounded to page bdy */
913 if (rc != NO_ERROR) {
914#ifdef DEBUG
915 WriteLog("WSOCK32: DosCreateThread error in WSAAsyncGetServByName: return code = %u\n", rc);
916#endif
917 OS2WSASetLastError(rc);
918 free(wsa);
919 return 0;
920 }
921
922#ifdef DEBUG
923 WriteLog("WSOCK32 THREAD: DosCreateThread's tid: %lu, ThreadParm = %p\n",(unsigned long)tid,ulThreadParm);
924 WriteLog("WSOCK32 THREAD: hwnd: %p\n",wsa->hw);
925#endif
926
927 return (LHANDLE)tid; //WSAAsyncGetServByName(hWnd,wMsg,name,proto,buf,buflen);
928}
929
930LHANDLE WIN32API OS2WSAAsyncGetServByPort(HWND hWnd, u_int wMsg, int port,
931 const char * proto, char * buf,
932 int buflen)
933{
934#ifdef DEBUG
935 WriteLog("WSOCK32: WSAAsyncGetServByPort unimplemented\n");
936#endif
937
938 return -5000; //WSAAsyncGetServByPort(hWnd,wMsg,port,proto,buf,buflen);
939}
940
941LHANDLE WIN32API OS2WSAAsyncGetProtoByName(HWND hWnd, u_int wMsg,
942 const char * name, char * buf,
943 int buflen)
944{
945#ifdef DEBUG
946 WriteLog("WSOCK32: WSAAsyncGetProtoByName unimplemented\n");
947#endif
948
949 return -5000; //WSAAsyncGetProtoByName(hWnd,wMsg,name,buf,buflen);
950}
951
952LHANDLE WIN32API OS2WSAAsyncGetProtoByNumber(HWND hWnd, u_int wMsg,
953 int number, char * buf,
954 int buflen)
955{
956#ifdef DEBUG
957 WriteLog("WSOCK32: WSAAsyncGetProtoByNumber unimplemented\n");
958#endif
959
960 return -5000; //WSAAsyncGetProtoByNumber(hWnd,wMsg,number,buf,buflen);
961}
962
963LHANDLE WIN32API OS2WSAAsyncGetHostByName(HWND hWnd, u_int wMsg,
964 const char * name, char * buf,
965 int buflen)
966{
967#ifdef DEBUG
968 WriteLog("WSOCK32: WSAAsyncGetHostByName unimplemented\n");
969#endif
970
971 return -5000; //WSAAsyncGetHostByName(hWnd,wMsg,name,buf,buflen);
972}
973
974LHANDLE WIN32API OS2WSAAsyncGetHostByAddr(HWND hWnd, u_int wMsg,
975 const char * addr, int len, int type,
976 char * buf, int buflen)
977{
978#ifdef DEBUG
979 WriteLog("WSOCK32: WSAAsyncGetHostByAddr unimplemented\n");
980#endif
981
982
983
984
985
986 return -5000; //WSAAsyncGetHostByAddr(hWnd,wMsg,addr,len,type,buf,buflen);
987}
988
989
990
991
992
993
994
995int WIN32API OS2WSACancelAsyncRequest(LHANDLE hAsyncTaskHandle)
996{
997TID tid;
998APIRET rc;
999#ifdef DEBUG
1000 WriteLog("WSOCK32: WSACancelAsyncRequest unimplemented\n");
1001#endif
1002
1003 rc = 0;
1004
1005 tid = (LHANDLE)hAsyncTaskHandle;
1006
1007 if(tid == 0) rc = WSAEINVAL;
1008
1009 if(!rc) rc = DosKillThread(tid);
1010
1011 switch(rc) {
1012 case 0: // SUCCESS!!
1013 return 0;
1014 case 170:
1015 rc = WSAEINPROGRESS;
1016 break;
1017 case 309:
1018 rc = WSAEINVAL;
1019 break;
1020 default:
1021 rc = -5000;
1022 break;
1023 } // end switch
1024
1025 OS2WSASetLastError(rc);
1026 return SOCKET_ERROR; // ERROR!
1027}
1028
1029
1030
1031
1032
1033int WIN32API OS2WSAAsyncSelect(SOCKET s, HWND hWnd, u_int wMsg,
1034 long lEvent)
1035{
1036PFNTHREAD pfnAsyncThread = &AsyncLoop; /* Address of thread program */
1037ULONG ulThreadParm = 100; /* Parameter to thread routine */
1038APIRET rc = NO_ERROR; /* Return code */
1039unsigned long ii;
1040AsyncStatus *as;
1041#ifdef DEBUG
1042char buf[150];
1043sprintf(buf,"WSOCK32: WSAAsyncSelect\n Message: %x\n Event: %ld\n hwindow: %x\n",
1044 wMsg,lEvent,(HWND)hWnd);
1045 WriteLog(buf);
1046#endif
1047
1048 as = FindASY(s);
1049
1050 if(as == NULL) return 0;
1051
1052 CheckThreads(as);
1053
1054 as->hwnd = hWnd;
1055 as->msg = wMsg;
1056 as->event = lEvent;
1057
1058 ulThreadParm = (ULONG)as;
1059
1060 rc = DosCreateThread(&(as->threadID), /* Thread ID (returned by function) */
1061 pfnAsyncThread, /* Address of thread program */
1062 ulThreadParm, /* Parameter passed to ThreadProc */
1063 CREATE_READY | /* Thread is ready when created */
1064 STACK_SPARSE, /* Do not pre-commit stack pages */
1065 8192L); /* Stack size, rounded to page bdy */
1066 if (rc != NO_ERROR) {
1067#ifdef DEBUG
1068 WriteLog("WSOCK32: DosCreateThread error: return code = %u\n", rc);
1069#endif
1070 OS2WSASetLastError(rc);
1071 return 0;
1072 }
1073
1074 return 1; //WSAAsyncSelect(s,hWnd,wMsg,lEvent);
1075}
1076
1077
1078
1079void _System AsyncLoop(ULONG ASP)
1080{
1081int socks[1],r,w,e,rc,ii;
1082AsyncStatus *as;
1083
1084 as = (AsyncStatus *)ASP;
1085
1086 r = w = e = 0;
1087 if(as->event & FD_READ) r = 1;
1088 if(as->event & FD_WRITE) w = 1;
1089 if(as->event & FD_OOB) e = 1;
1090
1091 socks[0] = (int)as->socket;
1092
1093 if((r+w+e) == 0) {
1094#ifdef DEBUG
1095 WriteLog("WSOCK32: Turning off async\n");
1096#endif
1097 ii = 0;
1098 rc = ioctl(socks[0],FIONBIO,(char *)&ii,sizeof(ii));
1099 as->threadID = 0;
1100 as->hwnd = 0;
1101 as->msg = 0;
1102 as->event = 0;
1103 as->status = BLOCKING;
1104 return;
1105 } // end if
1106 else
1107 {
1108#ifdef DEBUG
1109 WriteLog("WSOCK32: Setting up non-blocking sockets\n");
1110#endif
1111 ii = 1;
1112 rc = ioctl(socks[0],FIONBIO,(char *)&ii,sizeof(ii));
1113 if(rc != 0) {
1114#ifdef DEBUG
1115 WriteLog("WSOCK32: ioctl failed trying to non-block.\n");
1116#endif
1117 return;
1118 }
1119 as->status = NONBLOCKING;
1120 } // end else
1121
1122 do {
1123 rc = select(socks[0],(fd_set*)&r,0,0,0); // ioctl may be better for this.
1124 if(rc > 0) {
1125 rc = ioctl(socks[0],FIONREAD,(char *)&ii,sizeof(ii));
1126 if(rc == 0 && ii > 0) {
1127 /* data is ready */
1128 NotifyApp(FD_READ,as);
1129//#ifdef DEBUG
1130// WriteLog("WSOCK32: Data Waiting\n");
1131//#endif
1132 }
1133 }
1134 if(rc < 0) {
1135 rc = sock_errno();
1136 /* something ain't right */
1137 if(rc == 10038) { // Connection closed
1138 NotifyApp(FD_CLOSE,as);
1139 DosSleep(500);
1140 return;
1141 }
1142#ifdef DEBUG
1143 WriteLog("WSOCK32: Select error: %d\n",rc);
1144#endif
1145 } // end if
1146 DosSleep(50);
1147 } while(1);
1148
1149return;
1150}
1151
1152
1153
1154void CheckThreads(AsyncStatus *as)
1155{
1156AsyncStatus *asy;
1157
1158 if(as != NULL)
1159 if(as->threadID != 0) DosKillThread(as->threadID);
1160
1161 for(asy = TopASY; asy; asy = asy->Next)
1162 if(asy->threadID != 0) DosKillThread(asy->threadID);
1163
1164return;
1165}
1166
1167
1168
1169void NotifyApp(int xx,AsyncStatus *as)
1170{
1171 BOOL fResult; /* message-posted indicator */
1172 unsigned long ii;
1173
1174
1175//#ifdef DEBUG
1176// WriteLog("WSOCK32: Notifying the caller. rc = %d\n",xx);
1177//#endif
1178
1179 if(as->MsgStat == 0) {
1180 fResult = Notify(as,xx);
1181#ifdef DEBUG
1182 WriteLog("WSOCK32: Notify returns: %d\n",fResult);
1183#endif
1184 } // end if
1185
1186 if(as->MsgStat == 2) as->MsgStat = 0;
1187 else as->MsgStat = 1;
1188
1189return;
1190}
1191
1192
1193
1194void _System WSAFunct(ULONG xx)
1195{
1196WSAStruct *wsa;
1197WSERVENT *ooo;
1198char *yy;
1199int ii;
1200size_t ss;
1201UINT wp;
1202LONG lp;
1203int id = *_threadid;
1204
1205 wsa = (WSAStruct *)xx;
1206
1207#ifdef DEBUG
1208 WriteLog("WSOCK32: WSAFunct: xx = %p, hwnd = %p\n",xx,wsa->hw);
1209 WriteLog("WSOCK32: WSAFunct info carg1 = %s, carg2 = %s\n",wsa->carg1,wsa->carg2);
1210 WriteLog("WSOCK32: WSAFunct info buf = %p, %d\n",wsa->buf,wsa->buflen);
1211#endif
1212
1213 switch (wsa->CallingWhat) {
1214 case GETSERVBYNAME:
1215 yy = (char *)OS2getservbyname(wsa->carg1,wsa->carg2);
1216 ss = sizeof(WSERVENT);
1217 break;
1218 case GETSERVBYPORT:
1219 yy = (char *)OS2getservbyport(wsa->iarg1,wsa->carg1);
1220 break;
1221 case GETPROTOBYNUMBER:
1222 yy = (char *)OS2getprotobynumber(wsa->iarg1);
1223 break;
1224 case GETPROTOBYNAME:
1225 yy = (char *)OS2getprotobyname(wsa->carg1);
1226 break;
1227 case GETHOSTBYNAME:
1228 yy = (char *)OS2gethostbyname(wsa->carg1);
1229 break;
1230 case GETHOSTBYADDR:
1231 yy = (char *)OS2gethostbyaddr(wsa->carg1,wsa->iarg1,wsa->iarg2);
1232 break;
1233 default:
1234 yy = (char *)NULL;
1235 OS2WSASetLastError(-5000);
1236 break;
1237 } // end switch
1238
1239#ifdef DEBUG
1240 WriteLog("WSOCK32: THREAD id = %lu\n",(unsigned long)id);
1241 if(yy) {
1242 ooo = (WSERVENT *)yy;
1243 WriteLog("WSOCK32: WSAFunct service name = %s\n",ooo->s_name);
1244 WriteLog("WSOCK32: WSAFunct service port = %d\n",(int)ooo->s_port);
1245 }
1246#endif
1247
1248 wp = id;
1249
1250 if(yy == (char *)NULL) {
1251#ifdef DEBUG
1252 WriteLog("WSOCK32: WSAFunct error\n");
1253 WriteLog("WSOCK32: WSAFunct error carg1 = %s, carg2 = %s\n",wsa->carg1,wsa->carg2);
1254#endif
1255 ii = OS2WSAGetLastError();
1256 lp = OS2WSAMAKEASYNCREPLY(0,ii);
1257 } // end if
1258
1259 else {
1260 if(wsa->buflen < ss) ii = WSAENOBUFS;
1261 else ii = 0;
1262 lp = OS2WSAMAKEASYNCREPLY(ss,ii);
1263 if(ii == 0) memmove(wsa->buf,yy,ss);
1264 }
1265
1266#ifdef DEBUG
1267#endif
1268
1269
1270 do {
1271 if(WinQueryAnchorBlock(wsa->hw))
1272 ii = NotifyWSA(wsa->hw,wsa->msg,wp,lp);
1273 } while(ii != TRUE);
1274
1275 free(wsa);
1276
1277return;
1278}
1279
1280
1281
1282
1283
1284int WIN32API OS2WSARecvEx (SOCKET s, char FAR * buf, int len, int FAR *flags)
1285{
1286#ifdef DEBUG
1287 WriteLog("WSOCK32: WSARecvEx not implemented.\n");
1288#endif
1289
1290// return WSARecvEx(s,buf,len,flags);
1291 return 0;
1292}
1293
1294void WIN32API OS2s_perror(char *pszMessage,
1295 void *pUnknown)
1296{
1297 perror(pszMessage);
1298}
1299
1300
1301//typedef struct _TRANSMIT_FILE_BUFFERS {
1302// PVOID Head;
1303// DWORD HeadLength;
1304// PVOID Tail;
1305// DWORD TailLength;
1306//} TRANSMIT_FILE_BUFFERS, *PTRANSMIT_FILE_BUFFERS, *LPTRANSMIT_FILE_BUFFERS;
1307//
1308//BOOL WIN32API OS2TransmitFile (
1309// IN SOCKET hSocket,
1310// IN HANDLE hFile,
1311// IN DWORD nNumberOfBytesToWrite,
1312// IN DWORD nNumberOfBytesPerSend,
1313// IN LPOVERLAPPED lpOverlapped,
1314// IN LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
1315// IN DWORD dwReserved)
1316//{
1317// return FALSE;
1318//}
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
Note: See TracBrowser for help on using the repository browser.