-
Notifications
You must be signed in to change notification settings - Fork 2
/
twinsock.c
1325 lines (1193 loc) · 25.4 KB
/
twinsock.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 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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "twinsock.h"
#include "tx.h"
#include "script.h"
#include "sockinfo.h"
typedef struct __tws_request
{
short idRequest;
long iSender;
short idSenderRequest;
struct __tws_request *prqNext;
} tws_request;
typedef struct __tws_socket
{
tws_sockinfo si;
long iOwner;
struct __tws_socket *psckNext;
} tws_socket;
short iRequestOut = 0;
tws_request *prqList = 0;
tws_request **pprqTail = &prqList;
tws_socket *psckList = 0;
tws_socket **ppsckTail = &psckList;
HINSTANCE hinst;
static BOOL bUseNotify = TRUE;
static void SendInitRequest(void);
void Shutdown(void);
void OpenPort(void);
BOOL ResolvEdit(HWND hwndParent);
extern int iPortChanged;
static int iBufferSize;
#define READ_MAX 1024
#define TIMER_ID_SEND 1
#define TIMER_ID_RECEIVE 2
#define TIMER_ID_FLUSH 3
#define TIMER_ID_BREAK 4
#define TIMER_ID_COMMCHECK 5
#define TIMER_ID_SCRIPT 6
#define READ_COMPLETED (WM_USER + 1000)
#define WRITE_COMPLETED (WM_USER + 1001)
#ifdef __FLAT__
static HANDLE idComm;
#else
static int idComm;
#endif
static HWND hwnd;
static BOOL bFlushing = FALSE;
static BOOL bTerminal = TRUE;
#define SCREEN_COLUMNS 80
#define SCREEN_ROWS 25
static char aachScreen[SCREEN_ROWS][SCREEN_COLUMNS];
static int iScrollRow = 0;
static int iRow = 0, iColumn = 0;
static int cyRow, cxColumn;
#define ROW_INDEX(x) ((x + iScrollRow) % SCREEN_ROWS)
static char const achProtoInit[] = "@$TSStart$@";
extern enum Encoding eLine;
static int iInitChar = 0;
static char *pchCollect = 0;
static char *pchCollEnd = 0;
long nDiscarded = 0;
long nBytesRecvd = 0;
#ifdef __FLAT__
char *
GetLastErrorText(void)
{
char *pchError;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
0,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &pchError,
0, 0);
return pchError;
}
#endif
u_short CALLBACK htons (u_short hostshort)
{
char *pchValue = (char *) &hostshort;
char c;
c = pchValue[0];
pchValue[0] = pchValue[1];
pchValue[1] = c;
return hostshort;
}
u_long CALLBACK htonl (u_long hostlong)
{
char *pchValue = (char *) &hostlong;
char c;
c = pchValue[0];
pchValue[0] = pchValue[3];
pchValue[3] = c;
c = pchValue[1];
pchValue[1] = pchValue[2];
pchValue[2] = c;
return hostlong;
}
u_short CALLBACK ntohs (u_short netshort)
{
char *pchValue = (char *) &netshort;
char c;
c = pchValue[0];
pchValue[0] = pchValue[1];
pchValue[1] = c;
return netshort;
}
u_long CALLBACK ntohl (u_long netlong)
{
char *pchValue = (char *) &netlong;
char c;
c = pchValue[0];
pchValue[0] = pchValue[3];
pchValue[3] = c;
c = pchValue[1];
pchValue[1] = pchValue[2];
pchValue[2] = c;
return netlong;
}
extern void PacketReceiveData(void *pvData, int iLen);
void SetTransmitTimeout(void)
{
KillTimer(hwnd, TIMER_ID_SEND);
SetTimer(hwnd, TIMER_ID_SEND, 10000, 0);
}
void KillTransmitTimeout(void)
{
KillTimer(hwnd, TIMER_ID_SEND);
}
void SetReceiveTimeout(void)
{
KillTimer(hwnd, TIMER_ID_RECEIVE);
SetTimer(hwnd, TIMER_ID_RECEIVE, 1500, 0);
}
void KillReceiveTimeout(void)
{
KillTimer(hwnd, TIMER_ID_RECEIVE);
}
void FlushInput(void)
{
KillTimer(hwnd, TIMER_ID_FLUSH);
bFlushing = TRUE;
SetTimer(hwnd, TIMER_ID_FLUSH, 1500, 0);
}
void
SendToScreen(char c)
{
RECT rcClient;
RECT rcRedraw;
if (c == '\r')
{
iColumn = 0;
}
else if (c == '\b')
{
if (iColumn > 0)
iColumn--;
}
else if (c == '\n')
{
if (iRow < SCREEN_ROWS - 1)
{
iRow++;
}
else
{
memset(aachScreen[iScrollRow], 0x20, sizeof(aachScreen[iScrollRow]));
iScrollRow++;
while (iScrollRow >= SCREEN_ROWS)
iScrollRow -= SCREEN_ROWS;
GetClientRect(hwnd, &rcClient);
ScrollWindow(hwnd, 0, -cyRow, &rcClient, 0);
UpdateWindow(hwnd);
}
}
else if (c >= 0x20 && c <= 0x7e)
{
aachScreen[ROW_INDEX(iRow)][iColumn] = c;
rcRedraw.top = iRow * cyRow;
rcRedraw.left = iColumn * cxColumn;
rcRedraw.bottom = rcRedraw.top + cyRow;
rcRedraw.right = rcRedraw.left + cxColumn;
InvalidateRect(hwnd, &rcRedraw, TRUE);
if (iColumn < SCREEN_COLUMNS - 1)
iColumn++;
}
}
static void
WriteToScreen(char *pch, int iBytes)
{
while (iBytes--)
SendToScreen(*pch++);
}
static void
FillResolverInfo(void)
{
static char achBuffer[256];
GetTwinSockSetting("Resolver", "Override", "0", achBuffer, sizeof(achBuffer));
if (*hostinfo.achDomainName &&
hostinfo.nHosts &&
achBuffer[0] == '0')
return;
GetTwinSockSetting("Resolver", "Domain", "", achBuffer, sizeof(achBuffer));
if (*achBuffer)
strcpy(hostinfo.achDomainName, achBuffer);
else if (!hostinfo.achDomainName)
strcpy(hostinfo.achDomainName, "no-domain-specified");
GetTwinSockSetting("Resolver", "Server1", "", achBuffer, sizeof(achBuffer));
if (*achBuffer)
{
strcpy(hostinfo.aachHosts[0], achBuffer);
hostinfo.nHosts = 1;
}
else
{
if (!hostinfo.nHosts)
{
strcpy(hostinfo.aachHosts[0], "127.0.0.1");
hostinfo.nHosts++;
}
return;
}
GetTwinSockSetting("Resolver", "Server2", "", achBuffer, sizeof(achBuffer));
if (*achBuffer)
{
strcpy(hostinfo.aachHosts[1], achBuffer);
hostinfo.nHosts = 2;
}
else
{
return;
}
GetTwinSockSetting("Resolver", "Server3", "", achBuffer, sizeof(achBuffer));
if (*achBuffer)
{
strcpy(hostinfo.aachHosts[2], achBuffer);
hostinfo.nHosts = 3;
}
else
{
return;
}
}
static void
AddChar(char c)
{
static BOOL bStillCollect = FALSE;
SendToScreen(c);
if (pchCollect &&
pchCollect != pchCollEnd &&
c != ':' && c != '#' && c != '*' && c != '$' && c != '@')
{
*pchCollect++ = c;
*pchCollect = 0;
bStillCollect = TRUE;
return;
}
else if (c == achProtoInit[iInitChar])
{
iInitChar++;
if (iInitChar == strlen(achProtoInit))
{
FillResolverInfo();
if (*hostinfo.achHostName &&
!strchr(hostinfo.achHostName, '.'))
{
strcat(hostinfo.achHostName, ".");
strcat(hostinfo.achHostName,
hostinfo.achDomainName);
}
WriteToScreen("\r\nStarting Protocol, host=", 27);
WriteToScreen(hostinfo.achHostName, strlen(hostinfo.achHostName));
WriteToScreen("\r\n", 2);
iInitChar = 0;
bTerminal = 0;
InitProtocol();
SendInitRequest();
}
}
else if (iInitChar == 9 && isdigit(c))
{
eLine = (enum Encoding) (c - '0');
}
else if (iInitChar == 9 && c == '#')
{
pchCollect = hostinfo.achDomainName;
pchCollEnd = pchCollect + sizeof(hostinfo.achDomainName) - 1;
bStillCollect = TRUE;
}
else if (iInitChar == 9 && c == ':')
{
pchCollect = hostinfo.achHostName;
pchCollEnd = pchCollect + sizeof(hostinfo.achHostName) - 1;
bStillCollect = TRUE;
}
else if (iInitChar == 9 && c == '*' && hostinfo.nHosts < 4)
{
pchCollect = hostinfo.aachHosts[hostinfo.nHosts++];
pchCollEnd = pchCollect + sizeof(hostinfo.aachHosts[0]) - 1;
bStillCollect = TRUE;
}
else if (iInitChar)
{
iInitChar = 0;
eLine = E_6Bit;
}
if (!bStillCollect)
pchCollect = 0;
CheckScripts(hwnd, c);
}
static void
ProcessReceivedData(char *pchBuffer,
int nRead)
{
int i;
if (bTerminal)
{
HideCaret(hwnd);
for (i = 0; i < nRead; i++)
AddChar(pchBuffer[i]);
SetCaretPos(iColumn * cxColumn, iRow * cyRow);
ShowCaret(hwnd);
}
else if (bFlushing)
{
nDiscarded += nRead;
FlushInput();
}
else
{
nBytesRecvd += nRead;
PacketReceiveData(pchBuffer, nRead);
}
}
#ifdef __FLAT__
static char achReadBuffer[2048];
static DWORD dwRead;
static DWORD dwWrite;
static OVERLAPPED olRead;
static OVERLAPPED olWrite;
static HANDLE heventRead = 0;
static HANDLE heventWrite = 0;
static void PrepareToRead(void);
#pragma argsused
static void
ReceiveCompleted(void)
{
ProcessReceivedData(achReadBuffer, (int) dwRead);
PrepareToRead();
}
static void
PrepareToRead(void)
{
olRead.Internal = olRead.InternalHigh = olRead.Offset = olRead.OffsetHigh = 0;
olRead.hEvent = heventRead;
while (ReadFile(idComm,
achReadBuffer, sizeof(achReadBuffer),
&dwRead, &olRead));
if (GetLastError() != ERROR_IO_PENDING)
{
char *pchError = GetLastErrorText();
MessageBox(hwnd, pchError, "Fatal error reading communications port", MB_OK);
if (pchError)
LocalFree(pchError);
}
}
#else
static void DoReading(void)
{
static char achBuffer[READ_MAX];
static BOOL bAlreadyHere = FALSE;
int nRead;
COMSTAT cs;
int i;
if (bAlreadyHere)
return;
bAlreadyHere = TRUE;
do
{
nRead = ReadComm(idComm, achBuffer, READ_MAX);
if (nRead <= 0)
{
GetCommError(idComm, &cs);
nRead = -nRead;
}
if (nRead)
{
ProcessReceivedData(achBuffer, nRead);
}
} while (nRead || cs.cbInQue);
bAlreadyHere = FALSE;
}
#endif
#ifdef __FLAT__
typedef struct __tws_outdata
{
char *pchWrite;
int nWrite;
struct __tws_outdata *podNext;
} tws_outdata;
static tws_outdata *podList = 0, **ppodTail = &podList;
static BOOL bWaiting = FALSE;
void
AddToDataQueue(void *pvData, int iDataLen)
{
tws_outdata *podNow = (tws_outdata *) malloc(sizeof(tws_outdata));
*ppodTail = podNow;
ppodTail = &podNow->podNext;
podNow->pchWrite = (char *) malloc(iDataLen);
memcpy(podNow->pchWrite, pvData, iDataLen);
podNow->nWrite = iDataLen;
podNow->podNext = 0;
}
#pragma argsused
void WINAPI
SendCompleted( void)
{
tws_outdata *pod;
while (podList)
{
pod = podList;
podList = podList->podNext;
if (!podList)
ppodTail = &podList;
olWrite.Internal = olWrite.InternalHigh = olWrite.Offset = olWrite.OffsetHigh = 0;
olWrite.hEvent = heventWrite;
if (!WriteFile(idComm, pod->pchWrite, pod->nWrite, &dwWrite, &olWrite))
{
if (GetLastError() != ERROR_IO_PENDING)
{
char *pchError = GetLastErrorText();
MessageBox(hwnd, pchError, "Fatal error writing communications port", MB_OK);
if (pchError)
LocalFree(pchError);
}
free(pod->pchWrite);
free(pod);
return;
}
free(pod->pchWrite);
free(pod);
}
bWaiting = FALSE;
}
int SendData(void *pvData, int iDataLen)
{
if (bFlushing)
return iDataLen; /* Lie */
if (bWaiting)
{
AddToDataQueue(pvData, iDataLen);
return iDataLen;
}
olWrite.Internal = olWrite.InternalHigh = olWrite.Offset = olWrite.OffsetHigh = 0;
olWrite.hEvent = heventWrite;
if (!WriteFile(idComm, pvData, iDataLen, &dwWrite, &olWrite))
{
if (GetLastError() != ERROR_IO_PENDING)
{
char *pchError = GetLastErrorText();
MessageBox(hwnd, pchError, "Fatal error writing communications port", MB_OK);
if (pchError)
LocalFree(pchError);
}
else
{
bWaiting = TRUE;
}
}
return iDataLen;
}
#else
int SendData(void *pvData, int iDataLen)
{
int nWritten;
COMSTAT cs;
int iLen;
if (bFlushing)
return iDataLen; /* Lie */
iLen = iDataLen;
do
{
/* If the write would overflow the buffer, pretend we wrote it OK
* and return immediately
*/
GetCommError(idComm, &cs);
if (cs.cbOutQue + iLen >= iBufferSize)
return iDataLen;
nWritten = WriteComm(idComm, pvData, iLen);
if (nWritten < 0)
{
GetCommError(idComm, &cs);
nWritten = -nWritten;
}
iLen -= nWritten;
pvData = (char *) pvData + nWritten;
} while (iLen);
return iDataLen;
}
#endif
#ifdef __FLAT__
static HANDLE hThread = 0;
static DWORD dwThreadID;
#pragma argsused
DWORD
CommsReader(LPDWORD lpdwParam)
{
DWORD dwEvents;
HANDLE ahEvents[2];
ahEvents[0] = heventRead;
ahEvents[1] = heventWrite;
while (1)
{
switch(WaitForMultipleObjects(2, ahEvents, FALSE, INFINITE))
{
case WAIT_OBJECT_0:
GetOverlappedResult(idComm, &olRead, &dwRead, TRUE);
ResetEvent(heventRead);
PostMessage(hwnd, READ_COMPLETED, 0, 0);
break;
case WAIT_OBJECT_0 + 1:
GetOverlappedResult(idComm, &olWrite, &dwWrite, TRUE);
ResetEvent(heventWrite);
PostMessage(hwnd, WRITE_COMPLETED, 0, 0);
break;
}
}
}
void
StopCommThread(void)
{
if (hThread)
{
CloseHandle(hThread);
hThread = 0;
}
}
void
StartCommThread(void)
{
StopCommThread();
if (!heventRead)
heventRead = CreateEvent(0, TRUE, 0, 0);
if (!heventWrite)
heventWrite = CreateEvent(0, TRUE, 0, 0);
hThread = CreateThread( 0,
8192,
(LPTHREAD_START_ROUTINE) CommsReader,
0,
0,
&dwThreadID);
}
#endif
void
PaintScreen( HWND hWnd)
{
int i, iRow;
int iCol;
TEXTMETRIC tm;
HFONT hfontOld;
HFONT hfontFixed;
PAINTSTRUCT ps;
int yPos;
BeginPaint(hWnd, &ps);
hfontFixed = (HFONT) GetStockObject(SYSTEM_FIXED_FONT);
hfontOld = (HFONT) SelectObject(ps.hdc, (HGDIOBJ) hfontFixed);
GetTextMetrics(ps.hdc, &tm);
for (i = 0; i < SCREEN_ROWS; i++)
{
iRow = ROW_INDEX(i);
TextOut(ps.hdc, 0, i * cyRow, aachScreen[iRow], SCREEN_COLUMNS);
}
SelectObject(ps.hdc, (HGDIOBJ) hfontOld);
EndPaint(hWnd, &ps);
}
LRESULT CALLBACK _export
WindowProc( HWND hWnd,
UINT wMsg,
WPARAM wParam,
LPARAM lParam)
{
char c;
switch(wMsg)
{
case WM_SYSCOMMAND:
CheckScriptSysCommands(hWnd, wParam, lParam);
break;
case WM_COMMAND:
if (CheckScriptCommands(hWnd, wParam, &bTerminal))
break;
switch(wParam)
{
case 100:
if (CommsEdit(hWnd))
{
if (iPortChanged)
{
#ifdef __FLAT__
StopCommThread();
CloseHandle(idComm);
#else
CloseComm(idComm);
#endif
OpenPort();
}
else
{
InitComm(idComm);
}
}
break;
case 102:
if (!bTerminal)
{
Shutdown();
SendData("\030\030\030\030\030", 5);
}
break;
case 103:
PostQuitMessage(0);
break;
case 104:
if (bTerminal)
{
SetCommBreak(idComm);
SetTimer(hwnd, TIMER_ID_BREAK, 1500, 0);
}
break;
case 105:
if (bTerminal)
DialNumber(hWnd);
break;
case 106:
if (!bTerminal)
ShowProtoInfo(hWnd);
break;
case 107:
if (bTerminal)
ResolvEdit(hWnd);
break;
case 201:
About(hWnd);
break;
case 301:
WinHelp(hWnd, "TWINSOCK.HLP", HELP_CONTENTS, 0);
break;
}
break;
#ifdef __FLAT__
case READ_COMPLETED:
ReceiveCompleted();
break;
case WRITE_COMPLETED:
SendCompleted();
break;
#else
case WM_COMMNOTIFY:
switch(LOWORD(lParam))
{
case CN_RECEIVE:
DoReading();
break;
}
break;
#endif
case WM_CHAR:
PerhapsKillScript(hWnd);
if (bTerminal)
{
c = wParam;
SendData(&c, 1);
}
break;
case WM_TIMER:
switch(wParam)
{
case TIMER_ID_SEND:
TimeoutReceived();
break;
case TIMER_ID_RECEIVE:
KillTimer(hWnd, TIMER_ID_RECEIVE);
PacketReceiveData(0, 0);
break;
case TIMER_ID_FLUSH:
KillTimer(hWnd, TIMER_ID_FLUSH);
bFlushing = FALSE;
break;
case TIMER_ID_BREAK:
ClearCommBreak(idComm);
KillTimer(hWnd, TIMER_ID_BREAK);
break;
#ifndef __FLAT__
case TIMER_ID_COMMCHECK:
DoReading();
break;
#endif
case TIMER_ID_SCRIPT:
ScriptTimeOut(hWnd);
break;
}
break;
case WM_PAINT:
PaintScreen(hWnd);
return 0;
case WM_SETFOCUS:
CreateCaret(hWnd, 0, cxColumn, cyRow);
SetCaretPos(iColumn * cxColumn, iRow * cyRow);
ShowCaret(hWnd);
break;
case WM_KILLFOCUS:
DestroyCaret();
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
/* Process data received from a client */
#pragma argsused
void
ResponseReceived( char *pchData,
int iSize,
long iFrom)
{
struct tx_request *ptxr = (struct tx_request *) pchData;
tws_request *prq;
tws_socket *psck, **ppsck;
enum Functions ft;
int i;
char *c;
prq = (tws_request *) malloc(sizeof(tws_request));
prq->prqNext = 0;
prq->idRequest = iRequestOut++;;
prq->iSender = iFrom;
prq->idSenderRequest = ntohs(ptxr->id);
*pprqTail = prq;
pprqTail = &prq->prqNext;
ptxr->id = htons(prq->idRequest);
ft = (enum Functions) ntohs(ptxr->iType);
if (HasSocketArg(ft))
{
/* If this is a socket call, we need to create an entry
* for the new socket
*/
if (ft == FN_Socket)
{
c = ptxr->pchData;
if (ntohs(*(short *) (c + 2)) == 2)
i = ntohs(*(short *) (c + 4));
else
i = (short) ntohl(*(long *) (c + 4));
if (i != -1)
{
psck = (tws_socket *) malloc(sizeof(tws_socket));
psck->iOwner = iFrom;
psck->si.iClientSocket = i;
psck->si.iServerSocket = GetClientSocket();
psck->psckNext = 0;
*ppsckTail = psck;
ppsckTail = &psck->psckNext;
}
}
/* Now change the client's idea of the socket number into
* our idea of the socket number.
*/
c = ptxr->pchData;
if (ntohs(*(short *) (c + 2)) == 2)
i = ntohs(*(short *) (c + 4));
else
i = (short) ntohl(*(long *) (c + 4));
for (psck = psckList;
psck->si.iClientSocket != i || psck->iOwner != iFrom;
psck = psck->psckNext);
i = psck->si.iServerSocket;
if (ntohs(*(short *) (c + 2)) == 2)
*(short *) (c + 4) = htons(i);
else
*(long *) (c + 4) = htonl(i);
}
PacketTransmitData(pchData, iSize, 0);
#ifndef __FLAT__
DoReading();
#endif
}
long
AdjustDataIn( struct tx_request *ptxr,
enum Functions ft)
{
tws_socket *psck;
short idRequest;
long iOwner;
int i;
BOOL bLong;
char *c;
idRequest = ntohs(ptxr->id);
for (psck = psckList;
psck && psck->si.iServerSocket != idRequest;
psck = psck->psckNext);
if (!psck)
return 0;
iOwner = psck->iOwner;
ptxr->id = htons(psck->si.iClientSocket);
if (ft == FN_Accept)
{
if (ntohs(ptxr->nLen) ==
sizeof(struct sockaddr_in) +
sizeof(short) * 6)
i = ntohs(*(short *) (ptxr->pchData +
sizeof(struct sockaddr_in)));
else
i = (int) ntohl(*(long *) (ptxr->pchData +
sizeof(struct sockaddr_in)));
psck = (tws_socket *) malloc(sizeof(tws_socket));
psck->iOwner = iOwner;
/* Because this one is allocated by tshost, and is unique over the
* range of all applications using TwinSock, we can use it both ways
* as is
*/
psck->si.iServerSocket = i;
psck->si.iClientSocket = i;
psck->psckNext = 0;
*ppsckTail = psck;
ppsckTail = &psck->psckNext;
}
return iOwner;
}
long
AdjustRequestIn(struct tx_request *ptxr,
enum Functions ft)
{
short idRequest;
long iSender;
tws_request **pprq, *prqTemp;
tws_socket **ppsck, *psck;
int i;
char *c;
idRequest = ntohs(ptxr->id);
for (pprq = &prqList;
*pprq && (*pprq)->idRequest != idRequest;
pprq = &(*pprq)->prqNext);
if (!*pprq)
return 0;
iSender = (*pprq)->iSender;
ptxr->id = htons((*pprq)->idSenderRequest);
prqTemp = (*pprq)->prqNext;
free(*pprq);
*pprq = prqTemp;
if (!prqTemp)
pprqTail = pprq;
if (HasSocketArg(ft))
{
c = ptxr->pchData;
if (ntohs(*(short *) (c + 2)) == 2)
i = ntohs(*(short *) (c + 4));
else
i = (short) ntohl(*(long *) (c + 4));
for (psck = psckList;
psck && psck->si.iServerSocket != i;
psck = psck->psckNext);
if (psck)
{
i = psck->si.iClientSocket;
if (ntohs(*(short *) (c + 2)) == 2)
*(short *) (c + 4) = htons(i);
else
*(long *) (c + 4) = htonl(i);
}
/* If this is a close call, remove our entry describing the
* socket.
*/
if (ft == FN_Close)
{
c = ptxr->pchData;
if (ntohs(*(short *) (c + 2)) == 2)