-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wsockc.pas
1585 lines (1393 loc) · 51.4 KB
/
Wsockc.pas
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
{ **************************************************************
Package: XWB - Kernel RPCBroker
Date Created: Sept 18, 1997 (Version 1.1)
Site Name: Oakland, OI Field Office, Dept of Veteran Affairs
Developers: Danila Manapsal, Don Craven, Joel Ivey
Description: manages Winsock connections and creates/parses
messages
Current Release: Version 1.1 Patch 40 (Sept. 22, 2004)
*************************************************************** }
unit Wsockc;
{
Changes in v1.1.13 (JLI -- 8/23/00) -- XWB*1.1*13
Made changes to cursor dependent on current cursor being crDefault so
that the application can set it to a different cursor for long or
repeated processes without the cursor 'flashing' repeatedly.
Changes in v1.1.8 (REM -- 6/18/99) -- XWB*1.1*8
Update version 'BrokerVer'.
Changes in v1.1.6 (DPC -- 6/7/99) -- XWB*1.1*6
In tCall function, made changing cursor to hourglass conditional:
don't do it if XWB IM HERE RPC is being invoked.
Changes in V1.1.4 (DCM - 9/18/98)-XWB*1.1*4
1. Changed the ff line in NetStart from:
if inet_addr(PChar(Server)) <> INADDR_NONE then
to
if inet_addr(PChar(Server)) <> longint(INADDR_NONE) then
Reason: true 64 bit types in Delphi 4
2. Changed the ff line in NetStart from:
$else
hSocket := accept(hSocketListen, DHCPHost, AddrLen);{ -- returns new socket
to
$else
hSocket := accept(hSocketListen, @DHCPHost, @AddrLen);{ -- returns new socket
Reason: Incompatible types when recompiling
3. In NetStop, if socket <= 0, restore the default cursor.
Reason: Gave the impression of a busy process after the Kernel login
process timesout.
Changes in V1.1T3 [Feb 5, 1997]
1. Connect string now includes workstation name. This is used by kernel
security.
2. Code is 32bit compliant for Delphi 2.0
3. A great majority of PChars changed to default string (ansi-string)
4. Reading is done in 32k chunks during a loop. Intermediate data is
buffered into a string. At the end, a PChar is allocated and
returned to maintain compatibility with the original broker interface.
It is expected that shortly this will change once the broker component
changes its usage of tcall to expect a string return. Total read
can now exceed 32K up to workstation OS limits.
5. Creation of Hostent and Address structures has been streamlined.
Changes in V1.0T12
1. Inclusion of hSocket as a parameter on most API calls
Changes in V1.0T11
1. Reference parameter type is included. i.e. $J will be evaluated rather
than sending "$J".
2. Fully integrated with the TRPCB component interface.
3. This low level module is now called from an intermediate DLL.
Changes in V1.0T10
1. Fixed various memory leaks.
Changes in V1.0T9
1. Supports word processing fields.
2. Added a new exception type EBrokerError. This is raised when errors occur
in NetCall, NetworkConnect, and NetworkDisconnect
Changes in V1.0T8
1. Fix a problem in BuildPar in the case of a single list parameter with many
entries.
2. List parameters (arrays) can be large up to 65520 bytes
3. Introduction of sCallV and tCallV which use the Delphi Pascal open array
syntax (sCallFV and tCallV developed by Kevin Meldrum)
4. A new brokerDataRec type, null has been introduced to represent M calls
with no parameters, i.e. D FUN^LIB().
5. If you want to send a null parameter "", i.e. D FUN^LIB(""), Value
should be set to ''.
6. Fixed bug where a single ^ passed to sCall would generate error (confused
as a global reference.
7. Fixed a bug where a first position dot (.) in a literal parameter would
cause an error at the server end.
8. Fixed a bug where null strings (as white space in a memo box for example)
would not be correctly received at the server.
Changes in V1.0T7
1. Procedure NetworkConnect has been changed to Function NetworkConnect
returning BOOL
2. global variable IsConnected (BOOL) can be used to determine connection
state
3. Function cRight has been fixed to preserve head pointer to input PChar
string
4. New message format which includes length calculations for input parameters
*******************************************************************
A 32-bit high level interface to the Winsock API in Delphi Pascal.
This implementation allows communications between Delphi forms and
DHCP back end servers through the use of the DHCP Request Broker.
Usage: Put wsock in your Uses clause of your Delphi form. See additional
specs for Request Broker message formats, etc.
Programmer: Enrique Gomez - VA San Francisco ISC - April 1995
}
interface
Uses
SysUtils, winsock, xwbut1, WinProcs, Wintypes,
classes, dialogs, forms, controls,
stdctrls, ClipBrd, Trpcb, RpcbErr;
type
TXWBWinsock = class(TObject)
private
FCountWidth: Integer;
FIsBackwardsCompatible: Boolean;
FOldConnectionOnly: Boolean;
public
XNetCallPending, xFlush: boolean;
SocketError, XHookTimeOut: integer;
XNetTimerStart: TDateTime;
BROKERSERVER: string;
SecuritySegment, ApplicationSegment: string;
IsConnected: Boolean;
// NetBlockingHookVar: Function(): Bool; export;
function NetCall(hSocket: integer; imsg: string): PChar;
function tCall(hSocket: integer; api, apVer: String; Parameters: TParams;
var Sec, App: PChar; TimeOut: integer): PChar;
function cRight( z: PChar; n: longint): PChar;
function cLeft( z: PChar; n: longint): PChar;
function BuildApi ( n,p: string; f: longint): string;
function BuildHdr ( wkid: string; winh: string; prch: string;
wish: string): string;
function BuildPar(hSocket: integer; api, RPCVer: string;
const Parameters: TParams): string;
function StrPack ( n: string; p: integer): string;
function VarPack(n: string): string;
function NetStart(ForegroundM: boolean; Server: string; ListenerPort: integer;
var hSocket: integer): integer;
function NetworkConnect(ForegroundM: boolean; Server: string; ListenerPort,
TimeOut: integer): Integer;
function libSynGetHostIP(s: string): string;
function libNetCreate (lpWSData : TWSAData) : integer;
function libNetDestroy: integer;
function GetServerPacket(hSocket: integer): string;
// function NetBlockingHook: BOOL; export;
procedure NetworkDisconnect(hSocket: integer);
procedure NetStop(hSocket: integer);
procedure CloseSockSystem(hSocket: integer; s: string);
constructor Create;
procedure NetError(Action: string; ErrType: integer);
function NetStart1(ForegroundM: boolean; Server: string; ListenerPort: integer;
var hSocket: integer): Integer; virtual;
function BuildPar1(hSocket: integer; api, RPCVer: string; const Parameters:
TParams): String; virtual;
property CountWidth: Integer read FCountWidth write FCountWidth;
property IsBackwardsCompatible: Boolean read FIsBackwardsCompatible write
FIsBackwardsCompatible;
property OldConnectionOnly: Boolean read FOldConnectionOnly write
FOldConnectionOnly;
end;
function LPack(Str: String; NDigits: Integer): String;
function SPack(Str: String): String;
function NetBlockingHook: BOOL; export;
var
HookTimeOut: Integer;
NetCallPending: Boolean;
NetTimerStart: TDateTime;
Const
WINSOCK1_1 = $0101;
DHCP_NAME = 'BROKERSERVER';
M_DEBUG = True;
M_NORMAL = False;
BrokerVer = '1.108';
Buffer64K = 65520;
Buffer32K = 32767;
Buffer24K = 24576;
Buffer16K = 16384;
Buffer8K = 8192;
Buffer4K = 4096;
DefBuffer = 256;
DebugOn: boolean = False;
XWBBASEERR = {WSABASEERR + 1} 20000;
{Broker Application Error Constants}
XWB_NO_HEAP = XWBBASEERR + 1;
XWB_M_REJECT = XWBBASEERR + 2;
XWB_BadSignOn = XWBBASEERR + 4;
XWB_BadReads = XWBBASEERR + 8;
XWB_ExeNoMem = XWBBASEERR + 100;
XWB_ExeNoFile = XWB_ExeNoMem + 2;
XWB_ExeNoPath = XWB_ExeNoMem + 3;
XWB_ExeShare = XWB_ExeNoMem + 5;
XWB_ExeSepSeg = XWB_ExeNoMem + 6;
XWB_ExeLoMem = XWB_ExeNoMem + 8;
XWB_ExeWinVer = XWB_ExeNoMem + 10;
XWB_ExeBadExe = XWB_ExeNoMem + 11;
XWB_ExeDifOS = XWB_ExeNoMem + 12;
XWB_RpcNotReg = XWBBASEERR + 201;
implementation
uses fDebugInfo; {P36} //, TRPCB;
var
Prefix: String;
{
function LPack
Prepends the length of the string in NDigits characters to the value of Str
e.g., LPack('DataValue',4)
returns '0009DataValue'
}
function LPack(Str: String; NDigits: Integer): String;
Var
r: Integer;
t: String;
Width: Integer;
Ex1: Exception;
begin
r := Length(Str);
// check for enough space in NDigits characters
t := IntToStr(r);
Width := Length(t);
if NDigits < Width then
begin
Ex1 := Exception.Create('In generation of message to server, call to LPack where Length of string of '+IntToStr(Width)+' chars exceeds number of chars for output length ('+IntToStr(NDigits)+')');
Raise Ex1;
end;
t := '000000000' + IntToStr(r); {eg 11-1-96}
Result := Copy(t, length(t)-(NDigits-1),length(t)) + Str;
end;
{
function SPack
Prepends the length of the string in one byte to the value of Str, thus Str must be less than 256 characters.
e.g., SPack('DataValue')
returns #9 + 'DataValue'
}
function SPack(Str: String): String;
Var
r: Integer;
Ex1: Exception;
begin
r := Length(Str);
// check for enough space in one byte
if r > 255 then
begin
Ex1 := Exception.Create('In generation of message to server, call to SPack with Length of string of '+IntToStr(r)+' chars which exceeds max of 255 chars');
Raise Ex1;
end;
// t := Byte(r);
Result := Char(r) + Str;
end;
function TXWBWinsock.libNetCreate (lpWSData : TWSAData) : integer;
begin
Result := WSAStartup(WINSOCK1_1, lpWSData); {hard coded for Winsock
version 1.1}
end;
function TXWBWinsock.libNetDestroy :integer;
begin
WSAUnhookBlockingHook; { -- restore the default mechanism};
WSACleanup; { -- shutdown TCP API};
Result := 1;
end;
function TXWBWinsock.libSynGetHostIP(s: string): string;
var
HostName: PChar;
HostAddr: TSockAddr;
TCPResult: PHostEnt;
test: longint;
ChangeCursor: Boolean;
begin
{ -- set up a hook for blocking calls so there is no automatic DoEvents
in the background }
xFlush := False;
NetTimerStart := Now;
NetCallPending := True;
HookTimeOut := XHookTimeOut;
WSASetBlockingHook(@NetBlockingHook);
if Screen.Cursor = crDefault then
ChangeCursor := True
else
ChangeCursor := False;
if ChangeCursor then
Screen.Cursor := crHourGlass;
HostName := StrNew(PChar(s));
test := inet_addr(HostName);
if test > INADDR_ANY then
begin
Result := s;
StrDispose(Hostname);
if ChangeCursor then
Screen.Cursor := crDefault;
exit;
end;
try
begin
TCPResult := gethostbyname(HostName);
if TCPResult = nil then
begin
if ChangeCursor then
Screen.Cursor := crDefault;
WSAUnhookBlockingHook;
Result := '';
StrDispose(HostName);
exit;
end;
HostAddr.sin_addr.S_addr := longint(plongint(TCPResult^.h_addr_list^)^);
end;
except on EInvalidPointer do
begin
Result := '';
Screen.Cursor := crDefault;
StrDispose(HostName);
exit;
end;
end;
if ChangeCursor then
Screen.Cursor := crDefault;
WSAUnhookBlockingHook;
Result := StrPas(inet_ntoa(HostAddr.sin_addr));
StrDispose(HostName);
end;
function TXWBWinsock.cRight;
var
i,t: longint;
begin
t := strlen(z);
if n < t then
begin
for i := 0 to n do
z[i] := z[t-n+i];
z[n] := chr(0);
end;
cRight := z;
end;
function TXWBWinsock.cLeft;
var
t: longint;
begin
t := strlen(z);
if n > t then n := t;
z[n] := chr(0);
cLeft := z;
end;
function TXWBWinsock.BuildApi ( n,p: string; f: longint): string;
Var
x,s: string;
begin
str(f,x);
s := StrPack(p,5);
result := StrPack(x + n + '^' + s,5);
end;
function TXWBWinsock.NetworkConnect(ForegroundM: boolean; Server: string;
ListenerPort, TimeOut: integer): Integer;
var
status: integer;
hSocket: integer;
BrokerError: EBrokerError;
begin
Prefix := '[XWB]';
xFlush := False;
IsConnected := False;
XHookTimeOut := TimeOut;
if not OldConnectionOnly then
try
status := NetStart(ForeGroundM, server, ListenerPort, hSocket);
except
on E: EBrokerError do
begin
if IsBackwardsCompatible then // remove DSM specific error message, and just go with any error
begin
status := NetStart1(ForeGroundM, server, ListenerPort, hSocket);
end
else if ((Pos('connection lost',E.Message) > 0) // DSM
or ((Pos('recv',E.Message) > 0) and (Pos('WSAECONNRESET',E.Message) > 0))) then // Cache
begin
BrokerError := EBrokerError.Create('Broker requires a UCX or single connection protocol and this port uses the callback protocol.'+' The application is specified to be non-backwards compatible. Installing patch XWB*1.1*35 and activating this port number for UCX connections will correct the problem.');
raise BrokerError;
end
else
raise;
end;
end
else // OldConnectionOnly
status := NetStart1(ForeGroundM, server, ListenerPort, hSocket);
if status = 0 then IsConnected := True;
Result := hSocket; {return the newly established socket}
end;
procedure TXWBWinsock.NetworkDisconnect(hSocket: integer);
begin
xFlush := False;
if IsConnected then
try
NetStop(hSocket);
except on EBrokerError do
begin
SocketError := WSAUnhookBlockingHook; { -- rest deflt mechanism}
SocketError := WSACleanup; { -- shutdown TCP API}
end;
end;
end;
function TXWBWinsock.BuildHdr ( wkid: string; winh: string; prch: string;
wish: string): string;
Var
t: string;
begin
t := wkid + ';' + winh + ';' + prch + ';' + wish + ';';
Result := StrPack(t,3);
end;
function TXWBWinsock.BuildPar(hSocket: integer; api, RPCVer: string;
const Parameters: TParams): string;
var
i,ParamCount: integer;
param: string;
tResult: string;
subscript: string;
IsSeen: Boolean;
BrokerError: EBrokerError;
Str: String;
begin
param := '5';
if Parameters = nil then ParamCount := 0
else ParamCount := Parameters.Count;
for i := 0 to ParamCount - 1 do
begin
if Parameters[i].PType <> undefined then
begin
// Make sure that new parameter types are only used with non-callback server.
if IsBackwardsCompatible and ((Parameters[i].PType = global) or (Parameters[i].PType = empty) or (Parameters[i].PType = stream)) then
begin
if Parameters[i].PType = global then
Str := 'global'
else if Parameters[i].PType = empty then
Str := 'empty'
else
Str := 'stream';
BrokerError := EBrokerError.Create('Use of ' + Str + ' parameter type requires setting the TRPCBroker IsBackwardsCompatible property to FALSE');
raise BrokerError;
end;
with Parameters[i] do
begin
// if PType= null then
// param:='';
if PType = literal then
param := param + '0'+LPack(Value,CountWidth)+'f'; // 030107 new message protocol
if PType = reference then
param := param + '1'+LPack(Value,CountWidth)+'f'; // 030107 new message protocol
if PType = empty then
param := param + '4f';
if (PType = list) or (PType = global) then
begin
if PType = list then // 030107 new message protocol
param := param + '2'
else
param := param + '3';
IsSeen := False;
subscript := Mult.First;
while subscript <> '' do
begin
if IsSeen then
param := param + 't';
if Mult[subscript] = '' then
Mult[subscript] := #1;
param := param + LPack(subscript,CountWidth)+LPack(Mult[subscript],CountWidth);
IsSeen := True;
subscript := Mult.Order(subscript,1);
end; // while subscript <> ''
if not IsSeen then // 040922 added to take care of list/global parameters with no values
param := param + LPack('',CountWidth);
param := param + 'f';
end;
if PType = stream then
begin
param := param + '5' + LPack(Value,CountWidth) + 'f';
end;
end; // with Parameters[i] do
end; // if Parameters[i].PType <> undefined
end; // for i := 0
if param = '5' then
param := param + '4f';
tresult := Prefix + '11' + IntToStr(CountWidth) + '0' + '2' + SPack(RPCVer) + SPack(api) + param + #4;
// Application.ProcessMessages; // removed 040716 jli not needed and may impact some programs
Result := tresult;
end;
{ // previous message protocol
sin := TStringList.Create;
sin.clear;
x := '';
param := '';
arr := 0;
if Parameters = nil then ParamCount := 0
else ParamCount := Parameters.Count;
for i := 0 to ParamCount - 1 do
if Parameters[i].PType <> undefined then begin
with Parameters[i] do begin
// if PType= null then
// param:='';
if PType = literal then
param := param + strpack('0' + Value,3);
if PType = reference then
param := param + strpack('1' + Value,3);
if (PType = list) or (PType = global) then begin
Value := '.x';
param := param + strpack('2' + Value,3);
if Pos('.',Value) >0 then
x := Copy(Value,2,length(Value));
// if PType = wordproc then dec(last);
subscript := Mult.First;
while subscript <> '' do begin
if Mult[subscript] = '' then Mult[subscript] := #1;
sin.Add(StrPack(subscript,3) + StrPack(Mult[subscript],3));
subscript := Mult.Order(subscript,1);
end; // while
sin.Add('000');
arr := 1;
end; // if
end; // with
end; // if
param := Copy(param,1,Length(param));
tsize := 0;
tResult := '';
tout := '';
hdr := BuildHdr('XWB','','','');
strout := strpack(hdr + BuildApi(api,param,arr),5);
num :=0;
RPCVersion := '';
RPCVersion := VarPack(RPCVer);
if sin.Count-1 > 0 then num := sin.Count-1;
if num > 0 then
begin
for i := 0 to num do
tsize := tsize + length(sin.strings[i]);
x := '00000' + IntToStr(tsize + length(strout)+ length(RPCVersion));
end;
if num = 0 then
begin
x := '00000' + IntToStr(length(strout)+ length(RPCVersion));
end;
psize := x;
psize := Copy(psize,length(psize)-5,5);
tResult := psize;
tResult := ConCat(tResult, RPCVersion);
tout := strout;
tResult := ConCat(tResult, tout);
if num > 0 then
begin
for i := 0 to num do
tResult := ConCat(tResult, sin.strings[i]);
end;
sin.free;
frmBrokerExample.Edit1.Text := tResult;
Result := tResult; // return result
end;
}
function TXWBWinsock.StrPack(n: string; p: integer): String;
Var
s,l: integer;
t,x,zero: shortstring;
y: string;
begin
s := Length(n);
fillchar(zero,p+1, '0');
SetLength(zero, p);
str(s,x);
t := zero + x;
l := length(x)+1;
y := Copy(t, l , p);
y := y + n;
Result := y;
end;
function TXWBWinsock.VarPack(n: string): string;
var
s: integer;
begin
if n = '' then
n := '0';
s := Length(n);
SetLength(Result, s+2);
Result := '|' + chr(s) + n;
end;
const
OneSecond = 0.000011574;
function NetBlockingHook: BOOL;
var
TimeOut: double;
//TimeOut = 30 * OneSecond;
begin
if HookTimeOut > 0 then
TimeOut := HookTimeOut * OneSecond
else
TimeOut := OneSecond / 20;
Result := False;
if NetCallPending then
if Now > (NetTimerStart + TimeOut) then WSACancelBlockingCall;
end;
function TXWBWinsock.NetCall(hSocket: integer; imsg: string): PChar;
var
BufSend, BufRecv, BufPtr: PChar;
sBuf: string;
OldTimeOut: integer;
BytesRead, BytesLeft, BytesTotal: longint;
TryNumber: Integer;
BadXfer: Boolean;
xString: String;
begin
{ -- clear receive buffer prior to sending rpc }
if xFlush = True then begin
OldTimeOut := HookTimeOut;
HookTimeOut := 0;
WSASetBlockingHook(@NetBlockingHook);
NetCallPending := True;
BufRecv := StrAlloc(Buffer32k);
NetTimerStart := Now;
BytesRead := recv(hSocket, BufRecv^, Buffer32k, 0);
if BytesRead > 0 then
while BufRecv[BytesRead-1] <> #4 do begin
BytesRead := recv(hSocket, BufRecv^, Buffer32k, 0);
end;
StrDispose(BufRecv);
xFlush := False;
//Buf := nil; //P14
HookTimeOut := OldTimeOut;
end;
{ -- provide variables for blocking hook }
TryNumber := 0;
BadXfer := True;
{ -- send message length + message to server }
//BytesTotal := length(Prefix) + length(imsg) + 1 // p14
//Buf := StrAlloc(BytesTotal);
//Buf[0] := #0;
if Prefix = '[XWB]' then
BufSend := StrNew(PChar({Prefix +} imsg)) //; //moved in P14
else
BufSend := StrNew(PChar({Prefix +} imsg));
BufRecv := StrAlloc(Buffer32k);
Result := PChar('');
// try
while BadXfer and (TryNumber < 4) do
begin
NetCallPending := True;
NetTimerStart := Now;
TryNumber := TryNumber + 1;
BadXfer := False;
{Clipboard.SetTextBuf(buf);
ShowMessage('In Clipboard');}
SocketError := send(hSocket, BufSend^, StrLen(BufSend), 0);
if SocketError = SOCKET_ERROR then
NetError('send', 0);
{
finally
StrDispose(Buf);
//Buf := nil; //P14
end;
}
BufRecv[0] := #0;
try
BufPtr := BufRecv;
BytesLeft := Buffer32k;
BytesTotal := 0;
{Get Security and Application packets}
SecuritySegment := GetServerPacket(hSocket);
ApplicationSegment := GetServerPacket(hSocket);
sBuf := '';
{ -- loop reading TCP buffer until server is finished sending reply }
repeat
BytesRead := recv(hSocket, BufPtr^, BytesLeft, 0);
if BytesRead > 0 then begin
if BufPtr[BytesRead-1] = #4 then
begin
sBuf := ConCat(sBuf, BufPtr);
end
else
begin
BufPtr[BytesRead] := #0;
sBuf := ConCat(sBuf, BufPtr);
end;
Inc(BytesTotal, BytesRead);
end;
if BytesRead <= 0 then
begin
if BytesRead = SOCKET_ERROR then
NetError('recv', 0)
else
NetError('connection lost', 0);
break;
end;
until BufPtr[BytesRead-1] = #4;
sBuf := Copy(sBuf, 1, BytesTotal - 1);
StrDispose(BufRecv);
BufRecv := StrAlloc(BytesTotal+1); // cause of many memory leaks
StrCopy(BufRecv, PChar(sBuf));
Result := BufRecv;
if ApplicationSegment = 'U411' then
BadXfer := True;
NetCallPending := False;
finally
sBuf := '';
end;
end;
if BadXfer then
begin
StrDispose(BufRecv);
NetError(StrPas('Repeated Incomplete Reads on the server'), XWB_BadReads);
Result := StrNew('');
end;
{ -- if there was on error on the server, display the error code }
if Result[0] = #24 then
begin
xString := StrPas(@Result[1]);
StrDispose(BufRecv);
NetError(xString, XWB_M_REJECT);
// NetCall := #0;
Result := StrNew('');
end;
end;
function TXWBWinsock.tCall(hSocket: integer; api, apVer: String; Parameters: TParams;
var Sec , App: PChar; TimeOut: integer ): PChar;
var
tmp: string;
ChangeCursor: Boolean;
begin
HookTimeOut := TimeOut;
if (string(Api) <> 'XWB IM HERE') and (Screen.Cursor = crDefault) then
ChangeCursor := True
else
ChangeCursor := False;
if ChangeCursor then
Screen.Cursor := crHourGlass; //P6
if Prefix = '[XWB]' then
tmp := BuildPar(hSocket, api, apVer, Parameters)
else
tmp := BuildPar1(hSocket, api, apVer, Parameters);
// xFlush := True; // Have it clear input buffers prior to call
Result := NetCall(hSocket, tmp);
StrPCopy(Sec, SecuritySegment);
StrPCopy(App, ApplicationSegment);
if ChangeCursor then
Screen.Cursor := crDefault;
end;
function TXWBWinsock.NetStart (ForegroundM: boolean; Server: string;
ListenerPort: integer; var hSocket: integer): integer;
Var
WinSockData: TWSADATA;
LocalHost, DHCPHost: TSockAddr;
LocalName, workstation, pDHCPName: string;
y, tmp, upArrow, rAccept, rLost: string;
tmpPchar: PChar;
pLocalname: array [0..255] of char;
r: integer;
HostBuf,DHCPBuf: PHostEnt;
lin: TLinger;
s_lin: array [0..3] of char absolute lin;
ChangeCursor: Boolean;
begin
{ ForegroundM is a boolean value, TRUE means the M handling process is
running interactively a pointer rather than passing address length
by value) }
{ -- initialize Windows Sockets API for this task }
if Screen.Cursor = crDefault then
ChangeCursor := True
else
ChangeCursor := False;
if ChangeCursor then
Screen.Cursor := crHourGlass;
upArrow := string('^');
rAccept := string('accept');
rLost := string('(connection lost)');
SocketError := WSAStartup(WINSOCK1_1, WinSockData);
If SocketError >0 Then
NetError( 'WSAStartup',0);
{ -- set up a hook for blocking calls so there is no automatic DoEvents
in the background }
NetCallPending := False;
if ForeGroundM = False then if WSASetBlockingHook(@NetBlockingHook) = nil
then NetError('WSASetBlockingHook',0);
{ -- establish HostEnt and Address structure for local machine}
SocketError := gethostname(pLocalName, 255); { -- name of local system}
If SocketError >0 Then
NetError ('gethostname (local)',0);
HostBuf := gethostbyname(pLocalName); { -- info for local name}
If HostBuf = nil Then
NetError( 'gethostbyname',0);
LocalHost.sin_addr.S_addr := longint(plongint(HostBuf^.h_addr_list^)^);
LocalName := inet_ntoa(LocalHost.sin_addr);
workstation := string(HostBuf.h_name);
{ -- establish HostEnt and Address structure for remote machine }
if inet_addr(PChar(Server)) <> longint(INADDR_NONE) then
begin
DHCPHost.sin_addr.S_addr := inet_addr(PChar(Server));
DHCPBuf := gethostbyaddr(@DHCPHost.sin_addr.S_addr,sizeof(DHCPHost),PF_INET);
end
else
DHCPBuf := gethostbyname(PChar(Server)); { -- info for DHCP system}
If DHCPBuf = nil Then
begin
{ modification to take care of problems with 10-dot addresses that weren't registered - solution found by Shawn Hardenbrook }
// NetError ('Error Identifying Remote Host ' + Server,0);
// NetStart := 10001;
// exit;
DHCPHost.sin_addr.S_addr := inet_addr(PChar(Server));
pDHCPName := 'UNKNOWN';
end
else
begin;
DHCPHost.sin_addr.S_addr := longint(plongint(DHCPBuf^.h_addr_list^)^);
pDHCPName := inet_ntoa(DHCPHost.sin_addr);
end;
DHCPHost.sin_family := PF_INET; { -- internet address type}
DHCPHost.sin_port := htons(ListenerPort); { -- port to connect to}
{ -- make connection to DHCP }
hSocket := socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
If hSocket = INVALID_SOCKET Then
NetError( 'socket',0);
SocketError := connect(hSocket, DHCPHost, SizeOf(DHCPHost));
If SocketError = SOCKET_ERROR Then
NetError( 'connect',0);
HookTimeOut := 30;
{ -- remove setup of hSocketListen
// establish local IP now that connection is done
AddrLen := SizeOf(LocalHost);
SocketError := getsockname(hSocket, LocalHost, AddrLen);
if SocketError = SOCKET_ERROR then
NetError ('getsockname',0);
LocalName := inet_ntoa(LocalHost.sin_addr);
// -- set up listening socket for DHCP return connect
hSocketListen := socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // -- new socket
If hSocketListen = INVALID_SOCKET Then
NetError ('socket (listening)',0);
LocalHost.sin_family := PF_INET; // -- internet address type
LocalHost.sin_port := 0; // -- local listening port
SocketError := bind(hSocketListen, LocalHost,
SizeOf(LocalHost)); // -- bind socket to address
If SocketError = SOCKET_ERROR Then
NetError( 'bind',0);
AddrLen := sizeof(LocalHost);
SocketError := getsockname(hSocketListen, LocalHost,
AddrLen); // -- get listening port #
If SocketError = SOCKET_ERROR Then
NetError( 'getsockname',0);
LocalPort := ntohs(LocalHost.sin_port); // -- put in proper byte order
SocketError := listen(hSocketListen, 1); // -- put socket in listen mode
If SocketError = SOCKET_ERROR Then
NetError( 'listen',0);
}
{ -- send IP address + port + workstation name and wait for OK : eg 1-30-97}
{
RPCVersion := VarPack(BrokerVer); // eg 11-1-96
x := string('TCPconnect^');
x := ConCat(x, LocalName, upArrow); // local ip address
t := IntToStr(LocalPort); // callback port
x := ConCat(x, t, upArrow, workstation, upArrow); // workstation name
r := length(x) + length(RPCVersion) + 5;
t := string('00000') + IntToStr(r); // eg 11-1-96
y := Copy(t, length(t)-4,length(t));
y := ConCat(y, RPCVersion, StrPack(x,5)); // rpc version
}
{ new protocol 030107 }
// y := '[XWB]10' +IntToStr(CountWidth)+ '0' + '4'+#$A+'TCPConnect50'+ LPack(LocalName,CountWidth)+'f0'+LPack(IntToStr(LocalPort),CountWidth)+'f0'+LPack(workstation,CountWidth)+'f'+#4;
y := Prefix + '10' +IntToStr(CountWidth)+ '0' + '4'+#$A +'TCPConnect50'+ LPack(LocalName,CountWidth)+'f0'+LPack(IntToStr(0),CountWidth)+'f0'+LPack(workstation,CountWidth)+'f'+#4;
{ // need to remove selecting port etc from client, since it will now be handled on the server P36
if ForeGroundM = True then
begin
if ChangeCursor then
Screen.Cursor := crDefault;
t := 'Start M job D EN^XWBTCP' + #13 + #10 + 'Addr = ' +
LocalName + #13 + #10 + 'Port = ' + IntToStr(LocalPort);
frmDebugInfo := TfrmDebugInfo.Create(Application.MainForm);
try
frmDebugInfo.lblDebugInfo.Caption := t;
ShowApplicationAndFocusOK(Application);
frmDebugInfo.ShowModal;
finally
frmDebugInfo.Free
end;
// ShowMessage(t); //TODO
end;
} // remove debug mode from client
tmpPChar := NetCall(hSocket, PChar(y)); {eg 11-1-96}
tmp := tmpPchar;
StrDispose(tmpPchar);
if CompareStr(tmp, rlost) = 0 then
begin
lin.l_onoff := 1;
lin.l_linger := 0;
SocketError := setsockopt(hSocket, SOL_SOCKET, SO_LINGER,
s_lin, sizeof(lin));
If SocketError = SOCKET_ERROR Then
NetError( 'setsockopt (connect)',0);
closesocket(hSocket);
WSACleanup;
Result := 10002;
exit;
end;
r := CompareStr(tmp, rAccept);
If r <> 0 Then
NetError ('NetCall',XWB_M_REJECT);
{ // JLI 021217 remove disconnect and reconnect code -- use UCX connection directly.
lin.l_onoff := 1;
lin.l_linger := 0;