-
Notifications
You must be signed in to change notification settings - Fork 2
/
winsock.c
2880 lines (2630 loc) · 62.1 KB
/
winsock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TwinSock - "Troy's Windows Sockets"
*
* Copyright (C) 1994-1995 Troy Rollo <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the license in the file LICENSE.TXT included
* with the TwinSock distribution.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <winsock.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dos.h>
#include <ddeml.h>
#include "twinsock.h"
#include "tx.h"
#include "sockinfo.h"
#include "dns.h"
#ifdef __MSDOS__
#define PORTSWAP(x) ntohs(x)
#else
#define PORTSWAP(x) ntohl(x)
#endif
static int iErrno = 0;
static short idNext = 0;
static struct per_task *pptList = 0;
static struct per_socket *ppsList = 0;
static struct tx_queue *ptxqList = 0;
static HINSTANCE hInstance = 0;
static void FireAsyncRequest(struct tx_queue *ptxq);
static void ContinueDNSQuery(struct per_socket *pps);
static void NextDNSServer( struct per_socket *pps);
static void NextDNSTry( struct per_socket *pps);
#pragma argsused
int CALLBACK
LibMain(HINSTANCE hInst, WORD wOne, WORD wTwo, LPSTR lpstr)
{
hInstance = hInst;
return TRUE;
}
#pragma argsused
int CALLBACK
WEP(int iExitType)
{
return 1;
}
#ifdef __FLAT__
#pragma argsused
BOOL WINAPI
DllEntryPoint( HINSTANCE hinstDLL,
DWORD dwReason,
LPVOID lpvReserved)
{
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
return LibMain(hinstDLL, 0, 0, 0);
case DLL_PROCESS_DETACH:
return WEP(0);
}
return TRUE;
}
#endif
void
CopyDataIn( void *pvSource,
enum arg_type at,
void *pvDest,
int nLen)
{
switch(at)
{
case AT_Int16:
case AT_Int16Ptr:
*(short *) pvDest = ntohs(*(short *) pvSource);
break;
case AT_Int32:
case AT_Int32Ptr:
*(long *) pvDest = ntohl(*(long *) pvSource);
break;
case AT_Char:
*(char *) pvDest = *(char *) pvSource;
break;
case AT_GenPtr:
case AT_String:
memcpy(pvDest, pvSource, nLen);
break;
}
}
void
CopyDataOut( void *pvDest,
enum arg_type at,
void *pvSource,
int nLen)
{
switch(at)
{
case AT_Int16Ptr:
case AT_Int16:
*(short *) pvDest = htons(*(short *) pvSource);
break;
case AT_Int32:
case AT_Int32Ptr:
*(long *) pvDest = htonl(*(long *) pvSource);
break;
case AT_Char:
*(char *) pvDest = *(char *) pvSource;
break;
case AT_GenPtr:
case AT_String:
memcpy(pvDest, pvSource, nLen);
break;
}
}
static struct per_task *
GetAnotherTaskInfo(HTASK htask)
{
struct per_task *ppt;
for (ppt = pptList; ppt; ppt = ppt->pptNext)
{
if (ppt->htask == htask)
return ppt;
}
iErrno = WSANOTINITIALISED;
return 0;
}
static struct per_task *
GetTaskInfo(void)
{
#ifdef __FLAT__
return GetAnotherTaskInfo(0);
#else
return GetAnotherTaskInfo(GetCurrentTask());
#endif
}
static struct per_socket *
GetSocketInfo(SOCKET s)
{
struct per_socket *pps;
for (pps = ppsList; pps; pps = pps->ppsNext)
{
if (pps->s == s)
return pps;
}
iErrno = WSAENOTSOCK;
return 0;
}
static void
Notify(struct per_socket *pps,
int iCode)
{
if (pps->iEvents & iCode)
PostMessage(pps->hWnd, pps->wMsg, pps->s, WSAMAKESELECTREPLY(iCode, 0));
}
static void
NotifyError( struct per_socket *pps,
int iCode,
int iErrno)
{
if (pps->iEvents & iCode)
PostMessage(pps->hWnd, pps->wMsg, pps->s, WSAMAKESELECTREPLY(iCode, iErrno));
}
static struct per_socket *
NewSocket(struct per_task *ppt, SOCKET s)
{
struct per_socket *ppsNew;
ppsNew = (struct per_socket *) malloc(sizeof(struct per_socket));
if (ppsNew == 0)
return (ppsNew);
ppsNew->s = s;
ppsNew->iFlags = 0;
ppsNew->pdIn = 0;
ppsNew->pdOut = 0;
ppsNew->htaskOwner = ppt->htask;
ppsNew->ppsNext = ppsList;
ppsNew->iEvents = 0;
ppsNew->nOutstanding = 0;
ppsNew->pdnsi = 0;
ppsList = ppsNew;
return ppsNew;
}
static void SendEarlyClose(SOCKET s);
static void
RemoveSocket(struct per_socket *pps)
{
struct per_socket **ppps, *ppsParent;
struct data **ppd, *pd;
/* If our parent has noticed we're here, we need to remove ourselves
* from the list of sockets awaiting acception.
*/
for (ppsParent = ppsList; ppsParent; ppsParent = ppsParent->ppsNext)
{
if (!(ppsParent->iFlags & PSF_ACCEPT))
continue;
for (ppd = &ppsParent->pdIn; *ppd;)
{
if ((*ppd)->pchData == (char *) pps)
{
pd = *ppd;
*ppd = pd->pdNext;
free(pd);
}
else
{
ppd = &(*ppd)->pdNext;
}
}
}
/* Find our own position in the list */
for (ppps = &ppsList; *ppps; ppps = &(*ppps)->ppsNext)
{
if (*ppps == pps)
{
*ppps = pps->ppsNext;
/* If we have unacknowledged children, kill them */
while (pps->pdIn)
{
pd = pps->pdIn;
pps->pdIn = pd->pdNext;
if (pps->iFlags & PSF_ACCEPT)
{
SendEarlyClose(((struct per_socket *) pd->pchData)->s);
}
else
{
free(pd->pchData);
}
free(pd);
}
if (!(((int) pps->s) & 1))
ReleaseClientSocket(pps->s);
free(pps);
return;
}
}
}
static void
FunctionReceived(SOCKET s, void *pvData, int nLen, enum Functions ft)
{
struct data *pdNew, **ppdList;
struct per_socket *pps, *ppsNew;
struct per_task *ppt;
int ns;
pps = GetSocketInfo(s);
if (!pps)
{
if (ft == FN_Accept)
{
nLen -= sizeof(struct sockaddr_in);
pvData = (char *) pvData + sizeof(struct sockaddr_in);
if (nLen == sizeof(long))
ns = (int) ntohl(*(long *) pvData);
else
ns = (int) ntohs(*(short*) pvData);
SendEarlyClose(ns);
}
return;
}
for (ppdList = &pps->pdIn; *ppdList; ppdList = &(*ppdList)->pdNext);
pdNew = (struct data *) malloc(sizeof(struct data));
if (pdNew == 0)
return; /* We will get out of sync, but such is life */
pdNew->sin = *(struct sockaddr_in *) pvData;
pdNew->sin.sin_family = ntohs(pdNew->sin.sin_family);
nLen -= sizeof(struct sockaddr_in);
pvData = (char *) pvData + sizeof(struct sockaddr_in);
pdNew->pdNext = 0;
pdNew->nUsed = 0;
*ppdList = pdNew;
/* Note that the calls to Notify below should *really*
* only be made if the data gets put at the head of the
* queue, but some telnet implementations miss notifications
* and this screws us right up. By putting in additional
* notifications we at least have a chance of recovery.
*/
if (pps->iFlags & PSF_ACCEPT)
{
pdNew->iLen = 0;
if (nLen == sizeof(long))
ns = (int) ntohl(*(long *) pvData);
else
ns = (int) ntohs(*(short*) pvData);
ppt = GetAnotherTaskInfo(pps->htaskOwner);
ppsNew = NewSocket(ppt, ns);
pdNew->pchData = (char *) ppsNew;
ppsNew->iFlags |= PSF_CONNECT | PSF_BOUND;
ppsNew->sinLocal = pps->sinLocal;
ppsNew->sinRemote = pdNew->sin;
Notify(pps, FD_ACCEPT);
}
else
{
pdNew->iLen = nLen;
pdNew->pchData = (char *) malloc(nLen);
if (pdNew->pchData == 0)
pdNew->iLen = 0; /* Return EOF to the application */
else
memcpy(pdNew->pchData, pvData, nLen);
if (pps->pdnsi)
ContinueDNSQuery(pps);
else
Notify(pps, nLen ? FD_READ : FD_CLOSE);
}
}
static BOOL
StartBlocking(struct per_task *ppt)
{
if (ppt->bBlocking)
{
iErrno = WSAEINPROGRESS;
return FALSE;
}
else
{
ppt->bBlocking = TRUE;
ppt->bCancel = FALSE;
return TRUE;
}
}
static void
EndBlocking(struct per_task *ppt)
{
ppt->bBlocking = FALSE;
}
BOOL CALLBACK _export
DefBlockFunc(void)
{
MSG msg;
BOOL ret;
ret = (BOOL) PeekMessage(&msg,0,0,0,PM_REMOVE);
if (ret)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
/* Some poorly behaved applications quit without
* checking the return value of WSACancel. It
* *can* fail.
*/
if (msg.message == WM_QUIT)
WSACancelBlockingCall();
}
return ret;
}
static BOOL
FlushMessages(struct per_task *ppt)
{
if (ppt->lpBlockFunc)
return ((BOOL (CALLBACK *)(void)) ppt->lpBlockFunc)();
else
return DefBlockFunc();
}
static void
RemoveTXQ(struct tx_queue *ptxq)
{
struct tx_queue **pptxq;
for (pptxq = &ptxqList; *pptxq; pptxq = &((*pptxq)->ptxqNext))
{
if (*pptxq == ptxq)
{
*pptxq = ptxq->ptxqNext;
free(ptxq->ptxr);
free(ptxq);
return;
}
}
}
static void
RemoveTask(struct per_task *ppt)
{
struct per_task **pppt;
DestroyWindow(ppt->hwndDNS);
for (pppt = &pptList; *pppt; pppt = &((*pppt)->pptNext))
{
if (*pppt == ppt)
{
*pppt = ppt->pptNext;
free(ppt->pClient);
free(ppt);
break;
}
}
};
#pragma argsused
void
ResponseReceived( char *pchData,
int iSize,
long iFrom)
{
int nLen;
int id;
struct tx_queue *ptxq;
enum Functions ft;
struct tx_request *ptxr = (struct tx_request *) pchData;
struct sockaddr_in *psin;
ft = (enum Functions) ntohs(ptxr->iType);
id = ntohs(ptxr->id);
nLen = ntohs(ptxr->nLen);
if (ft == FN_Data || ft == FN_Accept)
{
FunctionReceived(id, ptxr->pchData, nLen - sizeof(short) * 5, ft);
return;
}
for (ptxq = ptxqList; ptxq; ptxq = ptxq->ptxqNext)
{
if (ptxq->id == id)
{
memcpy(ptxq->ptxr, ptxr, nLen);
ptxq->bDone = TRUE;
if (ptxq->pchLocation)
FireAsyncRequest(ptxq);
return;
}
}
if (ft == FN_SendTo || ft == FN_Send || ft == FN_Connect || ft == FN_Close)
{
int iOffset = 0;
int iLen;
enum arg_type at;
long nValue;
struct per_socket *pps;
int i;
int nCode;
switch(ft)
{
case FN_SendTo:
case FN_Send:
nCode = 2;
break;
case FN_Connect:
nCode = 3;
break;
case FN_Close:
nCode = 0;
break;
default:
nCode = -1;
break;
}
for (i = 0; i <= nCode; i++)
{
at = (enum arg_type) ntohs(*(short *) (ptxr->pchData + iOffset));
iOffset += 2;
iLen = ntohs(*(short *) (ptxr->pchData + iOffset));
iOffset += 2;
if (i == 1)
psin = (struct sockaddr_in *) (ptxr->pchData + iOffset);
if (i == 0 || i == nCode)
{
if (at == AT_Int16)
nValue = ntohs(*(short *) (ptxr->pchData + iOffset));
else
nValue = ntohl(*(long *) (ptxr->pchData + iOffset));
if (i == 0)
{
pps = GetSocketInfo(nValue);
if (!pps)
return;
if (ft == FN_Close)
{
RemoveSocket(pps);
}
}
else if (ft == FN_Connect)
{
if (nValue < 0)
{
pps->iConnectResult = ntohs(ptxr->nError);
NotifyError(pps, FD_CONNECT, ntohs(ptxr->nError));
}
else
{
pps->iConnectResult = 0;
pps->iFlags |= PSF_CONNECT;
Notify(pps, FD_CONNECT);
Notify(pps, FD_WRITE);
if (!pps->iFlags & PSF_BOUND)
{
psin->sin_family = ntohs(psin->sin_family);
pps->sinLocal = *psin;
pps->iFlags |= PSF_BOUND;
}
}
}
else
{
if (nValue < 0)
{
pps->nOutstanding = 0;
NotifyError(pps, FD_WRITE, ntohs(ptxr->nError));
}
else
{
pps->nOutstanding -= nValue;
if (pps->nOutstanding < MAX_OUTSTANDING)
Notify(pps, FD_WRITE);
}
}
}
iOffset += iLen;
}
}
}
static struct tx_queue *
TransmitFunction(struct transmit_function *ptf)
{
int i;
int nSize = sizeof(short) * 5;
struct tx_request *ptxr;
struct tx_queue *ptxq, **pptxq;
int iOffset;
struct per_task *ppt = GetTaskInfo();
for (i = 0; i < ptf->nArgs; i++)
nSize += ptf->pfaList[i].iLen + sizeof(short) * 2;
nSize += ptf->pfaResult->iLen + sizeof(short) * 2;
ptxr = (struct tx_request *) malloc(nSize);
if (ptxr == 0)
{
return 0;
}
ptxr->iType = htons((short) ptf->fn);
ptxr->nArgs = htons((short) ptf->nArgs);
ptxr->nError = 0;
ptxr->nLen = htons((short) nSize);
ptxr->id = htons(idNext);
for (iOffset = i = 0; i < ptf->nArgs; i++)
{
*(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaList[i].at);
iOffset += 2;
*(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaList[i].iLen);
iOffset += 2;
CopyDataOut( ptxr->pchData + iOffset,
ptf->pfaList[i].at,
ptf->pfaList[i].pvData,
ptf->pfaList[i].iLen);
iOffset += ptf->pfaList[i].iLen;
}
*(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaResult->at);
iOffset += 2;
*(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaResult->iLen);
iOffset += 2;
CopyDataOut( ptxr->pchData + iOffset,
ptf->pfaResult->at,
ptf->pfaResult->pvData,
ptf->pfaResult->iLen);
iOffset += ptf->pfaResult->iLen;
ptxq = (struct tx_queue *) malloc(sizeof(struct tx_queue));
if (ptxq == 0)
{
free(ptxr);
return 0;
}
ptxq->ptxqNext = 0;
ptxq->id = idNext;
ptxq->ptxr = ptxr;
ptxq->bDone = 0;
ptxq->hwnd = 0;
ptxq->pchLocation = 0;
ptxq->wMsg = 0;
idNext++;
for (pptxq = &ptxqList; *pptxq; pptxq = &((*pptxq)->ptxqNext));
*pptxq = ptxq;
SendDataTo(ppt->pClient, (char *) ptxr, nSize, 0);
return ptxq;
};
static void
SendEarlyClose(SOCKET s)
{
struct func_arg pfaArgs[1];
struct func_arg pfaReturn;
struct transmit_function tf;
int nReturn;
INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) );
INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) );
INIT_TF(tf, FN_Close, 1, pfaArgs, pfaReturn);
RemoveTXQ(TransmitFunction(&tf));
}
static void
SetErrorResult(struct func_arg *pfaResult)
{
switch (pfaResult->at)
{
case AT_Int16:
*(short *) pfaResult->pvData = -1;
break;
case AT_Int32:
*(long *) pfaResult->pvData = -1;
break;
case AT_Char:
*(char *) pfaResult->pvData = -1;
break;
case AT_Int16Ptr:
case AT_Int32Ptr:
case AT_String:
case AT_GenPtr:
*(void **) pfaResult->pvData = 0;
break;
}
}
static int TransmitFunctionAndBlock(
struct per_task *ppt,
struct transmit_function *ptf)
{
struct tx_queue *ptxq;
struct tx_request *ptxr;
int i, iOffset;
BOOL bError = FALSE;
if (!StartBlocking(ppt))
{
iErrno = WSAEINPROGRESS;
SetErrorResult(ptf->pfaResult);
return 0;
}
ptxq = TransmitFunction(ptf);
ptxr = ptxq->ptxr;
while (!ptxq->bDone)
{
FlushMessages(ppt);
if (ppt->bCancel)
{
iErrno = WSAEINTR;
RemoveTXQ(ptxq);
SetErrorResult(ptf->pfaResult);
EndBlocking(ppt);
return 0;
}
}
if (ptxq->ptxr->nError)
{
iErrno = ntohs(ptxq->ptxr->nError);
SetErrorResult(ptf->pfaResult);
bError = TRUE;
}
else
{
for (iOffset = i = 0; i < ptf->nArgs; i++)
{
ptf->pfaList[i].at = (enum arg_type) ntohs(*(short *) (ptxr->pchData + iOffset));
iOffset += 2;
ptf->pfaList[i].iLen = ntohs(*(short *) (ptxr->pchData + iOffset));
iOffset += 2;
if (!ptf->pfaList[i].bConstant)
CopyDataIn( ptxr->pchData + iOffset,
ptf->pfaList[i].at,
ptf->pfaList[i].pvData,
ptf->pfaList[i].iLen);
iOffset += ptf->pfaList[i].iLen;
}
ptf->pfaResult->at = (enum arg_type) ntohs(*(short *) (ptxr->pchData + iOffset));
iOffset += 2;
ptf->pfaResult->iLen = ntohs(*(short *) (ptxr->pchData + iOffset));
iOffset += 2;
CopyDataIn( ptxr->pchData + iOffset,
ptf->pfaResult->at,
ptf->pfaResult->pvData,
ptf->pfaResult->iLen);
}
RemoveTXQ(ptxq);
EndBlocking(ppt);
return bError ? 0 : 1;
}
static void
CopyHostEnt(struct per_task *ppt)
{
int iLocation;
int i;
int nAddrs;
ppt->he.h_addrtype = ntohs(*(short *) ppt->achHostEnt);
ppt->he.h_length = ntohs(*(short *) (ppt->achHostEnt + sizeof(short)));
nAddrs = ntohs(*(short *) (ppt->achHostEnt + sizeof(short) * 2));
iLocation = sizeof(short) * 3;
for (i = 0; i < nAddrs; i++)
{
if (i < MAX_ALTERNATES - 1)
ppt->apchHostAddresses[i] = ppt->achHostEnt + iLocation;
iLocation += 4;
}
if (nAddrs < MAX_ALTERNATES - 1)
ppt->apchHostAddresses[nAddrs] = 0;
else
ppt->apchHostAddresses[MAX_ALTERNATES - 1] = 0;
ppt->he.h_addr_list = ppt->apchHostAddresses;
ppt->he.h_name = ppt->achHostEnt + iLocation;
iLocation += strlen(ppt->achHostEnt + iLocation)+ 1;
for (i = 0; ppt->achHostEnt[iLocation] && i < MAX_ALTERNATES - 1; i++)
{
ppt->apchHostAlii[i] = ppt->achHostEnt + iLocation;
iLocation += strlen(ppt->achHostEnt + iLocation)+ 1;
}
ppt->apchHostAlii[i] = 0;
ppt->he.h_aliases = ppt->apchHostAlii;
ppt->he.h_addr_list = ppt->apchHostAddresses;
}
static int
CopyHostEntTo(struct per_task *ppt, char *pchData)
{
int i;
int nAddrs;
struct hostent *phe;
char *pchOld;
int nAlii;
phe = (struct hostent *) pchData;
memcpy(phe, &ppt->he, sizeof(ppt->he));
pchData += sizeof(*phe);
for (nAddrs = 0; phe->h_addr_list[nAddrs]; nAddrs++);
for (nAlii = 0; phe->h_aliases[nAlii]; nAlii++);
memcpy(pchData, phe->h_addr_list, sizeof(char *) * (nAddrs + 1));
phe->h_addr_list = (char **) pchData;
pchData += sizeof(char *) * (nAddrs + 1);
memcpy(pchData, phe->h_aliases, sizeof(char *) * (nAddrs + 1));
phe->h_aliases = (char **) pchData;
pchData += sizeof(char *) * (nAddrs + 1);
pchOld = phe->h_addr_list[0];
memcpy(pchData, pchOld, 4 * nAddrs);
for (i = 0; i < nAddrs; i++)
phe->h_addr_list[i] = pchData + (phe->h_addr_list[i] - pchOld);
pchData += 4 * nAddrs;
strcpy(pchData, phe->h_name);
phe->h_name = pchData;
pchData += strlen(pchData) + 1;
for (i = 0; i < nAlii; i++)
{
strcpy(pchData, phe->h_aliases[i]);
phe->h_aliases[i] = pchData;
pchData += strlen(pchData) + 1;
}
return (pchData - (char *) phe);
}
static void
CopyServEnt(struct per_task *ppt)
{
int iLocation;
int i;
/* Note that s_port is needed in network byte order */
ppt->se.s_port = *(short *) ppt->achServEnt;
iLocation = sizeof(short);
ppt->se.s_proto = ppt->achServEnt + iLocation;
iLocation += strlen(ppt->achServEnt + iLocation) + 1;
ppt->se.s_name = ppt->achServEnt + iLocation;
iLocation += strlen(ppt->achServEnt + iLocation) + 1;
for (i = 0; ppt->achServEnt[iLocation] && i < MAX_ALTERNATES - 1; i++)
{
ppt->apchServAlii[i] = ppt->achServEnt + iLocation;
iLocation += strlen(ppt->achServEnt + iLocation) + 1;
}
ppt->apchServAlii[i] = 0;
ppt->se.s_aliases = ppt->apchServAlii;
}
static int
CopyServEntTo(struct per_task *ppt, char *pchData)
{
int i;
int nAlii;
struct servent *pse;
CopyServEnt(ppt);
pse = (struct servent *) pchData;
memcpy(pse, &ppt->se, sizeof(ppt->se));
pchData += sizeof(*pse);
for (nAlii = 0; pse->s_aliases[nAlii]; nAlii++);
memcpy(pchData, pse->s_aliases, sizeof(char *) * (nAlii + 1));
pse->s_aliases = (char **) pchData;
pchData += sizeof(char *) * (nAlii + 1);
strcpy(pchData, ppt->se.s_name);
ppt->se.s_name = pchData;
pchData += strlen(pchData) + 1;
strcpy(pchData, ppt->se.s_proto);
ppt->se.s_proto = pchData;
pchData += strlen(pchData) + 1;
for (i = 0; i < nAlii; i++)
{
strcpy(pchData, pse->s_aliases[i]);
pse->s_aliases[i] = pchData;
pchData += strlen(pchData) + 1;
}
return (pchData - (char *) pse);
}
static void
CopyProtoEnt(struct per_task *ppt)
{
int iLocation;
int i;
ppt->pe.p_proto = ntohs(*(short *) ppt->achProtoEnt);
iLocation = sizeof(short);
ppt->pe.p_name = ppt->achProtoEnt + iLocation;
iLocation += strlen(ppt->achProtoEnt + iLocation) + 1;
for (i = 0; ppt->achProtoEnt[iLocation] && i < MAX_ALTERNATES - 1; i++)
{
ppt->apchProtoAlii[i] = ppt->achProtoEnt + iLocation;
iLocation += strlen(ppt->achProtoEnt + iLocation) + 1;
}
ppt->apchProtoAlii[i] = 0;
ppt->pe.p_aliases = ppt->apchProtoAlii;
}
static int
CopyProtoEntTo(struct per_task *ppt, char *pchData)
{
int i;
int nAlii;
struct protoent *ppe;
CopyProtoEnt(ppt);
ppe = (struct protoent *) pchData;
memcpy(ppe, &ppt->pe, sizeof(ppt->pe));
pchData += sizeof(*ppe);
for (nAlii = 0; ppe->p_aliases[nAlii]; nAlii++);
memcpy(pchData, ppe->p_aliases, sizeof(char *) * (nAlii + 1));
ppe->p_aliases = (char **) pchData;
pchData += sizeof(char *) * (nAlii + 1);
strcpy(pchData, ppe->p_name);
ppe->p_name = pchData;
pchData += strlen(pchData) + 1;
for (i = 0; i < nAlii; i++)
{
strcpy(pchData, ppe->p_aliases[i]);
ppe->p_aliases[i] = pchData;
pchData += strlen(pchData) + 1;
}
return (pchData - (char *) ppe);
}
SOCKET CALLBACK _export
accept(SOCKET s, struct sockaddr FAR *addr,
int FAR *addrlen)
{
struct per_task *ppt;
struct per_socket *pps, *ppsNew;
struct data *pd;
if ((ppt = GetTaskInfo()) == 0)
return (INVALID_SOCKET);
if ((pps = GetSocketInfo(s)) == 0)
return (INVALID_SOCKET);
if (!(pps->iFlags & PSF_ACCEPT))
{
iErrno = WSAEINVAL;
return (INVALID_SOCKET);
}
if (!pps->pdIn && (pps->iFlags & PSF_NONBLOCK))
{
iErrno = WSAEWOULDBLOCK;
return (INVALID_SOCKET);
}
if (!StartBlocking(ppt))
return (INVALID_SOCKET);
while (!pps->pdIn)
{
FlushMessages(ppt);
if (ppt->bCancel)
{
iErrno = WSAEINTR;
EndBlocking(ppt);
return (INVALID_SOCKET);
}
}
pd = pps->pdIn;
if (addr && addrlen)
{
memcpy(addr, &pd->sin, sizeof(pd->sin));
*addrlen = sizeof(pd->sin);
}
ppsNew = (struct per_socket *) pd->pchData;
pps->pdIn = pd->pdNext;
free(pd);
if (pps->pdIn)
Notify(pps, FD_ACCEPT);
EndBlocking(ppt);
return ppsNew->s;
}
int CALLBACK _export
bind(SOCKET s, const struct sockaddr FAR *addr, int namelen)
{
struct per_task *ppt;
int nReturn;
struct func_arg pfaArgs[3];
struct func_arg pfaReturn;
struct transmit_function tf;
struct sockaddr *psa;
struct per_socket *pps;
if ((ppt = GetTaskInfo()) == 0)
return -1;
if ((pps = GetSocketInfo(s)) == 0)
return -1;
if ((namelen < sizeof(*psa)) || IsBadReadPtr(addr, sizeof(*psa)))
{
iErrno = WSAEFAULT;
return (SOCKET_ERROR);
}
namelen = sizeof(*psa);
psa = (struct sockaddr *) malloc(namelen);
if (psa == 0)
{
iErrno = WSAENOBUFS;
return (SOCKET_ERROR);
}