-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathICQSock.pas
1711 lines (1525 loc) · 47.2 KB
/
ICQSock.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
unit ICQSock;
{(C) Alex Demchenko}
{ Modified by NighTrader 02-04-2003 }
{Modified by Oleg Kozachok (oleg@kozachok.net.ua) 2006-2007}
//{$DEFINE DEBUG} {Show debug errors}
//{$DEFINE PARSE} {Dump packets into file}
interface
uses
{$IFDEF DEBUGDC}
dbugintf,
{$ENDIF}
{TODO: Óáðàòü SysUtils}
Windows, WinSock, Classes, ICQWorks, ICQLang, SysUtils;
const
CNetPktLen = $FFFF; {Size of network packet}
DefaultSockType = True; {True = non-blocking using blocking sockets architecture; False = blocking}
type
PNetPacket = ^TNetPacket;
TNetPacket = record
Buf: array[0..CNetPktLen - 1] of Byte;
BufLen: Word;
Offset: Word;
Next: PNetPacket;
end;
{Thread-safe implementation of software buffer}
TNetBuffer = class(TObject)
private
FPkt: PNetPacket;
Shared: Integer;
CS: TRTLCriticalSection;
public
constructor Create;
destructor Destroy; override;
procedure Enter;
procedure Leave;
procedure Clear;
procedure AddPacket(Buffer: Pointer; BufLen: LongWord);
procedure DelPacket;
function GetPacket(Buffer: Pointer): LongWord;
function SkipData(Len: Word): Boolean;
procedure AddStr(const Value: RawByteString);
function GetStr: RawByteString;
function GetLength: LongWord;
end;
TOnSocketThreadException = procedure (Sender: TObject; AExceptionStr: RawByteString) of object;
{Thread-safe implementation of Berkeley sockets}
TCustomSocket = class(TThread)
private
FSocket: TSocket;
FIp: Integer;
FDoConnect: Boolean;
FWorking: Boolean;
FConnected: Boolean;
FAssync: Boolean;
FBuffer: TNetBuffer;
FDataSentEvent: Boolean;
FErrLang: TICQLangType;
FOnSocketThreadException: TOnSocketThreadException;
procedure ProcessBuffer;
protected
FHost: RawByteString;
FPort: Word;
FLastError: Word;
FLastErrMsg: RawByteString;
Buffer: Pointer;
BufLen: LongWord;
function Resolve(const Host: RawByteString): Integer;
procedure Execute; override;
procedure FreeSocket;
procedure OnError; virtual;
procedure OnConnect; virtual;
procedure OnConnectError; virtual;
procedure OnDisconnect; virtual;
procedure OnReceive; virtual;
procedure OnDataSent; virtual;
public
constructor Create;
destructor Destroy; override;
procedure StartWork(Socket: TSocket);
procedure Connect;
procedure SendData(Buffer: Pointer; Len: LongWord);
procedure SendStr(const Value: RawByteString);
property Host: RawByteString read FHost write FHost;
property Port: Word read FPort write FPort;
property Working: Boolean read FWorking write FWorking;
property Connected: Boolean read FConnected write FConnected;
property Assync: Boolean read FAssync write FAssync;
property ErrorLanguage: TICQLangType read FErrLang write FErrLang;
property OnSocketThreadException: TOnSocketThreadException read FOnSocketThreadException write FOnSocketThreadException;
end;
TProxySocket = class(TCustomSocket)
private
FDestHost: RawByteString;
FDestIp: Integer;
FDestPort: Word;
FProxyUser: RawByteString;
FProxyPass: RawByteString;
FProxyAuth: Boolean;
FProxyReady: Boolean;
FProxyResolve: Boolean;
protected
procedure Execute; override;
public
property Host: RawByteString read FDestHost write FDestHost; {Real Host}
property Port: Word read FDestPort write FDestPort; {Real Port}
property ProxyHost: RawByteString read FHost write FHost;
property ProxyPort: Word read FPort write FPort;
property ProxyUser: RawByteString read FProxyUser write FProxyUser;
property ProxyPass: RawByteString read FProxyPass write FProxyPass;
property ProxyAuth: Boolean read FProxyAuth write FProxyAuth default False;
property ProxyResolve: Boolean read FProxyResolve write FProxyResolve default False;
property ProxyReady: Boolean read FProxyReady write FProxyReady default False;
end;
TOnError = procedure(Sender: TObject; ErrorType: TErrorType; ErrorMsg: RawByteString) of object;
TOnRecveive = procedure(Sender: TObject; Buffer: Pointer; BufLen: LongWord) of object;
{Extends TProxySocket with events}
TEventSocket = class(TProxySocket)
private
FOnConnect: TNotifyEvent;
FOnError: TOnError;
FOnDisconnect: TNotifyEvent;
FOnConnectError: TNotifyEvent;
FOnReceive: TOnRecveive;
FOnDataSent: TNotifyEvent;
protected
procedure OnConnect; override;
procedure OnError; override;
procedure OnConnectError; override;
procedure OnDisconnect; override;
procedure OnReceive; override;
procedure OnDataSent; override;
published
property _OnConnect: TNotifyEvent read FOnConnect write FOnConnect;
property _OnError: TOnError read FOnError write FOnError;
property _OnConnectError: TNotifyEvent read FOnConnectError write FOnConnectError;
property _OnDisconnect: TNotifyEvent read FOnDisconnect write FOnDisconnect;
property _OnReceive: TOnRecveive read FOnReceive write FOnReceive;
property _OnDataSent: TNotifyEvent read FOnDataSent write FOnDataSent;
end;
TSOCKSSocket = class(TEventSocket)
private
FSrcBuf: array[0..255] of Byte;
FSrcLen: Word;
end;
TSOCKS4Socket = class(TSOCKSSocket)
protected
procedure OnConnect; override;
procedure OnReceive; override;
end;
TSOCKS5Socket = class(TSOCKSSocket)
private
FSocksProgress: Word;
protected
procedure OnConnect; override;
procedure OnReceive; override;
end;
THTTPSocket = class(TEventSocket)
private
FBuf: array[0..$FFFE] of Byte;
FCurLen, FLen: LongWord;
protected
procedure OnConnect; override;
procedure OnReceive; override;
end;
THTTPSSocket = class(TEventSocket)
private
FBuf: array[0..8191] of Byte;
FCurLen: Word;
protected
procedure OnConnect; override;
procedure OnReceive; override;
end;
TMySocket = class;
TOnClientConnected = procedure(Sender: TObject; Socket: TMySocket) of object;
TOnSrvSockConnected = procedure(Sender: TObject; Socket: TSocket) of object;
TTCPServer = class(TThread)
private
FSocket: TSocket;
FPort: Word;
FClient: TSocket;
FLastError: Word;
FLastErrMsg: RawByteString;
FErrLang: TICQLangType;
FOnError: TOnError;
FOnClientConnected: TOnSrvSockConnected;
protected
procedure Execute; override;
procedure WaitForConnection;
procedure FreeSocket;
public
constructor Create;
destructor Destroy; override;
function Start: Boolean;
procedure OnError; virtual;
procedure OnClientConnected;
property Port: Word read FPort write FPort;
property ErrorLanguage: TICQLangType read FErrLang write FErrLang;
property _OnError: TOnError read FOnError write FOnError;
property _OnClientConnected: TOnSrvSockConnected read FOnClientConnected write FOnClientConnected;
end;
TOnAdvPktParse = procedure(Sender: TObject; Host: RawByteString; Port: Word; Buffer: Pointer; BufLen: LongWord; Incoming: Boolean) of object;
TOnRecv = procedure(Sender: TObject; Socket: TSocket; Buffer: Pointer; BufLen: LongWord) of object;
TServerSocket = class(TTCPServer)
private
fOnCliConn:TOnSrvSockConnected;
procedure DoConnEvent;
public
procedure OnClientConnected; virtual;
property OnConnected: TOnSrvSockConnected read fOnCliConn write fOnCliConn;
End;
{ TSrvSock like in MyScoket.pas }
TSrvSocket = class(TObject)
private
fSrv:TTCPServer;
fPort:word;
FOnClientConnected: TOnClientConnected;
FOnError:TOnError;
fIsListening:Boolean;
procedure OnSrvConnProc(Sender: TObject; Socket: TSocket);
procedure OnSrvErrProc(Sender: TObject; ErrorType: TErrorType; ErrorMsg: RawByteString);
function GetPort:Word;
procedure SetPort( aPort: Word);
Public
constructor Create;
destructor Destroy; override;
function StartServer(Port: Word): Boolean;
function StopServer: Boolean;
property Port: Word read GetPort write SetPort;
published
property OnError:TOnError read fOnError write fOnError;
property OnClientConnected: TOnClientConnected read FOnClientConnected write FOnClientConnected;
End;
{ TMySock like in MySocket.pas }
TMySocket = class(TObject)
private
FHost:RawByteString;
fPort:Word;
// Threaded Socket
FEventSocket:TEventSocket;
FSocket:TSocket;
// Events
FOnConnectError: TNotifyEvent;
FOnDisconnect: TNotifyEvent;
FOnPktParse: TOnAdvPktParse;
FOnError: TOnError;
FOnRecv: TOnRecv;
FOnConnectProc: TNotifyEvent;
FOnDataSent: TNotifyEvent;
function GetClientSocket: TSocket;
procedure SetClientSocket(Socket: TSocket);
function IsConnected: Boolean;
{$HINTS OFF} // added by eraser 1.6.2003
Procedure SetHost( aHost: RawByteString);
Procedure SetPort( aPort: Word);
Function GetHost: RawByteString;
Function GetPort: Word;
{$HINTS ON} // added by eraser 1.6.2003
Procedure SetProxyType( aProxyType: TProxyType);
Procedure SetProxyHost( aProxyHost: RawByteString);
Procedure SetProxyPort( aProxyPort: Word);
Procedure SetProxyAuth( aProxyAuth: Boolean);
Procedure SetProxyPass( aProxyPass: RawByteString);
Procedure SetProxyUser( aProxyUser: RawByteString);
Procedure SetProxyRslv( aProxyRslv: Boolean);
Function GetProxyType: TProxyType;
Function GetProxyHost: RawByteString;
Function GetProxyPort: Word;
Function GetProxyAuth: Boolean;
Function GetProxyPass: RawByteString;
Function GetProxyUser: RawByteString;
Function GetProxyRslv: Boolean;
Procedure OnConnectErrorProc(Sender: TObject);
Procedure OnDisconnectProc(Sender: TObject);
Procedure OnConnect(Sender: TObject);
Procedure OnErrorProc(Sender: TObject; ErrorType: TErrorType; ErrorMsg: RawByteString);
Procedure OnReceive(Sender: TObject; Buffer: Pointer; BufLen: LongWord);
Procedure OnDataSentProc(Sender: TObject);
public
constructor Create;
destructor Destroy; override;
procedure Connect; dynamic;
procedure Disconnect;
procedure SendData(var Buf; BufLen: LongWord);
property Host: RawByteString read fHost write fHost;
property Port: Word read fPort write fPort;
property ProxyType: TProxyType read GetProxyType write SetProxyType;
property ProxyHost: RawByteString read GetProxyHost write SetProxyHost;
property ProxyPort: Word read GetProxyPort write SetProxyPort;
property ProxyUser: RawByteString read GetProxyUser write SetProxyUser;
property ProxyAuth: Boolean read GetProxyAuth write SetProxyAuth;
property ProxyPass: RawByteString read GetProxyPass write SetProxyPass;
property ProxyResolve: Boolean read GetProxyRslv write SetProxyRslv default False;
// property WndHandle: THandle read GetWndHandle;
published
property OnConnectError: TNotifyEvent read FOnConnectError write FOnConnectError;
property OnDisconnect: TNotifyEvent read FOnDisconnect write FOnDisconnect;
property OnPktParseA: TOnAdvPktParse read FOnPktParse write FOnPktParse;
property OnError: TOnError read FOnError write FOnError;
property OnReceiveProc: TOnRecv read FOnRecv write FOnRecv;
property OnConnectProc: TNotifyEvent read FOnConnectProc write FOnConnectProc;
property OnDataSent: TNotifyEvent read FOnDataSent write FOnDataSent;
property ClientSocket: TSocket read GetClientSocket write SetClientSocket;
property Connected: Boolean read IsConnected;
End;
var
WSAData: TWSAData;
WSAStarted: Boolean;
function WaitForRead(Sock: TSocket; Timeout: DWord): Boolean;
function GetHTTPStatus(List: TStringList): RawByteString;
function WSockAddrToIp(Value: LongWord): AnsiString;
function WSAErrorToStr(ErrorNo: Integer): RawByteString;
function GetLocalIP: Integer;
function FindBindPort: Word;
implementation
const
b64alphabet: PAnsiChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function EncodeBase64(Value: RawByteString): RawByteString;
const
pad: PAnsiChar = '====';
function EncodeChunk(const Chunk: RawByteString): RawByteString;
var
W: LongWord;
i, n: Byte;
begin
n := Length(Chunk); W := 0;
for i := 0 to n - 1 do
W := W + Ord(Chunk[i + 1]) shl ((2 - i) * 8);
Result := b64alphabet[(W shr 18) and $3f] +
b64alphabet[(W shr 12) and $3f] +
b64alphabet[(W shr 06) and $3f] +
b64alphabet[(W shr 00) and $3f];
if n <> 3 then
Result := Copy(Result, 0, n + 1) + Copy(pad, 0, 3 - n); //add padding when out len isn't 24 bits
end;
begin
Result := '';
while Length(Value) > 0 do
begin
Result := Result + EncodeChunk(Copy(Value, 0, 3));
Delete(Value, 1, 3);
end;
end;
function DecodeBase64(Value: RawByteString): RawByteString;
function DecodeChunk(const Chunk: RawByteString): RawByteString;
var
W: LongWord;
i: Byte;
begin
W := 0; Result := '';
for i := 1 to 4 do
if Pos(Chunk[i], b64alphabet) <> 0 then
W := W + Word((Pos(Chunk[i], b64alphabet) - 1)) shl ((4 - i) * 6);
for i := 1 to 3 do
Result := Result + AnsiChar(W shr ((3 - i) * 8) and $ff);
end;
begin
Result := '';
if Length(Value) mod 4 <> 0 then Exit;
while Length(Value) > 0 do
begin
Result := Result + DecodeChunk(Copy(Value, 0, 4));
Delete(Value, 1, 4);
end;
end;
procedure ShowMessage(const Msg: RawByteString);
begin
MessageBoxA(0, PAnsiChar(Msg), 'Message', 0);
end;
function WaitForRead(Sock: TSocket; Timeout: DWord): Boolean;
var
readfd: TFDSet;
tv: TimeVal;
begin
tv.tv_sec := 0; tv.tv_usec := Timeout;
FD_ZERO(readfd); FD_SET(Sock, readfd);
if select(0, @readfd, nil, nil, @tv) < 1 then
Result := False
else
Result := True;
end;
function GetHTTPStatus(List: TStringList): RawByteString;
var
i, c: Word;
S: RawByteString;
begin
Result := '';
if List.Count < 1 then Exit;
S := List.Strings[0]; c := 0;
for i := 1 to Length(S) do
if c = 1 then
Result := Result + S[i]
else
if S[i] = ' ' then Inc(c);
end;
{ TMySocket }
constructor TMySocket.Create;
Begin
Inherited Create;
FEventSocket := TEventSocket.Create;
FEventSocket._OnConnectError := OnConnectErrorProc;
FEventSocket._OnConnect := OnConnect;
FEventSocket._OnDisconnect := OnDisconnectProc;
FEventSocket._OnError := OnErrorProc;
FEventSocket._OnReceive := OnReceive;
FEventSocket._OnDataSent := OnDataSentProc;
fSocket := INVALID_SOCKET;
End;
destructor TMySocket.Destroy;
Begin
FEventSocket.Free;
End;
Procedure TMySocket.OnConnectErrorProc(Sender: TObject);
Begin
If Assigned(fOnConnectError) then
fOnConnectError(Self);
End;
Procedure TMySocket.OnDisconnectProc(Sender: TObject);
Begin
If Assigned(fOnDisconnect) then
fOnDisconnect(Self);
fEventSocket.FreeSocket;
End;
Procedure TMySocket.OnConnect(Sender: TObject);
Begin
If Assigned(FOnConnectProc) then
fOnConnectProc(Self);
End;
Procedure TMySocket.OnErrorProc(Sender: TObject; ErrorType: TErrorType; ErrorMsg: RawByteString);
Begin
If Assigned(fOnError) then
fOnError(Self, ErrorType, ErrorMsg);
End;
Procedure TMySocket.OnReceive(Sender: TObject; Buffer: Pointer; BufLen: LongWord);
Begin
If Assigned(fOnRecv) then
fOnRecv(Self, ClientSocket, Buffer, BufLen);
End;
Procedure TMySocket.OnDataSentProc(Sender: TObject);
Begin
If Assigned(fOnDataSent) then
fOnDataSent(Self);
End;
function TMySocket.GetClientSocket: TSocket;
Begin
If FSocket = INVALID_SOCKET then
Result := FEventSocket.FSocket
Else
Result := FSocket;
End;
procedure TMySocket.SetClientSocket(Socket: TSocket);
Begin
fSocket := Socket;
fEventSocket.ProxyReady := True;
FEventSocket.StartWork(fSocket);
End;
function TMySocket.IsConnected: Boolean;
Begin
Result := FEventSocket.Connected;
End;
procedure TMySocket.Connect;
Begin
If FEventSocket.Connected Then exit;
FEventSocket.Host := fHost;
FEventSocket.Port := fPort;
FEventSocket.ProxyReady := True;
FEventSocket.ProxyHost := fHost;
FEventSocket.ProxyPort := fPort;
FEventSocket.Connect;
End;
procedure TMySocket.Disconnect;
Begin
If Not FEventSocket.Connected then Exit;
FEventSocket.FreeSocket;
End;
procedure TMySocket.SendData(var Buf; BufLen: LongWord);
Begin
FEventSocket.SendData(@Buf, BufLen);
End;
Procedure TMySocket.SetHost( aHost: RawByteString);
Begin
FEventSocket.Host := aHost;
End;
Procedure TMySocket.SetPort( aPort: Word);
Begin
FEventSocket.Port := aPort
End;
Function TMySocket.GetHost: RawByteString;
Begin
Result := FEventSocket.Host;
End;
Function TMySocket.GetPort: Word;
Begin
Result := FEventSocket.Port;
End;
Procedure TMySocket.SetProxyType( aProxyType: TProxyType);
Begin
//
End;
Procedure TMySocket.SetProxyHost( aProxyHost: RawByteString);
Begin
FEventSocket.ProxyHost := aProxyHost;
End;
Procedure TMySocket.SetProxyPort( aProxyPort: Word);
Begin
FEventSocket.ProxyPort := aProxyPort;
End;
Procedure TMySocket.SetProxyAuth( aProxyAuth: Boolean);
Begin
FEventSocket.ProxyAuth := aProxyAuth;
End;
Procedure TMySocket.SetProxyPass( aProxyPass: RawByteString);
Begin
FEventSocket.ProxyPass := aProxyPass;
End;
Procedure TMySocket.SetProxyUser( aProxyUser: RawByteString);
Begin
FEventSocket.ProxyUser := aProxyUser;
End;
Procedure TMySocket.SetProxyRslv( aProxyRslv: Boolean);
Begin
FEventSocket.ProxyResolve := aProxyRslv;
End;
Function TMySocket.GetProxyType: TProxyType;
Begin
Result := P_NONE;
End;
Function TMySocket.GetProxyHost: RawByteString;
Begin
Result := FEventSocket.ProxyHost;
End;
Function TMySocket.GetProxyPort: Word;
Begin
Result := FEventSocket.ProxyPort;
End;
Function TMySocket.GetProxyAuth: Boolean;
Begin
Result := FEventSocket.ProxyAuth;
End;
Function TMySocket.GetProxyPass: RawByteString;
Begin
Result := FEventSocket.ProxyPass;
End;
Function TMySocket.GetProxyUser: RawByteString;
Begin
Result := FEventSocket.ProxyUser;
End;
Function TMySocket.GetProxyRslv: Boolean;
Begin
Result := FEventSocket.ProxyResolve;
End;
{ TSrvSocket }
constructor TSrvSocket.Create;
Begin
inherited Create;
fSrv := TServerSocket.Create;
//fSrv.OnConnected := OnCliConnProc;
fSrv.FreeOnTerminate := False;
fSrv._OnClientConnected := OnSrvConnProc;
fSrv._OnError := OnSrvErrProc;
fIsListening := False;
End;
destructor TSrvSocket.Destroy;
Begin
fSrv.FreeOnTerminate := True;
StopServer;
inherited Destroy;
End;
procedure TSrvSocket.OnSrvErrProc(Sender: TObject; ErrorType: TErrorType; ErrorMsg: RawByteString);
Begin
If assigned(fOnError) then
fOnError(Sender, ErrorType, ErrorMsg);
End;
procedure TSrvSocket.OnSrvConnProc(Sender: TObject; Socket: TSocket);
Var
aMS:TMySocket;
Begin
If Assigned(fOnClientConnected) then Begin
aMS := TMySocket.Create;
aMS.ProxyType := P_None;
aMS.ClientSocket := Socket;
fOnClientConnected(Self, aMS);
End;
End;
function TSrvSocket.GetPort:Word;
Begin
Result := fSrv.Port;
End;
procedure TSrvSocket.SetPort( aPort: Word);
Begin
fSrv.Port := aPort;
fPort := aPort;
End;
function TSrvSocket.StartServer(Port: Word): Boolean;
Begin
fSrv.Port := Port;
fPort := Port;
Result := fSrv.Start;
fIsListening := Result;
End;
function TSrvSocket.StopServer: Boolean;
Begin
fSrv.FreeSocket;
fIsListening := False;
Result := True;
End;
{ TServerSocket }
procedure TServerSocket.OnClientConnected;
Begin
If assigned(fOnCliConn) then
Synchronize(DoConnEvent);
End;
procedure TServerSocket.DoConnEvent;
Begin
fOnCliConn(Self, fClient);
End;
constructor TNetBuffer.Create;
begin
inherited Create;
FPkt := nil;
Shared := 0;
end;
destructor TNetBuffer.Destroy;
begin
Clear;
if Shared = 1 then DeleteCriticalSection(CS);
inherited;
end;
{Swap pointers}
procedure XChg(var Critical, Normal); assembler;
asm
mov ecx, [edx]
xchg [eax], ecx
mov [edx], ecx
end;
procedure TNetBuffer.Enter; {Synchronization - enter critical section}
var
j: Integer;
begin
j := 1; XChg(Shared, j); if j = 0 then InitializeCriticalSection(CS);
EnterCriticalSection(CS);
end;
procedure TNetBuffer.Leave; {Synchronization - leave critical section}
begin
LeaveCriticalSection(CS);
end;
procedure TNetBuffer.Clear;
var
p: Pointer;
begin
while FPkt <> nil do begin
p := FPkt^.Next;
FreeMem(FPkt);
FPkt := p;
end;
end;
procedure TNetBuffer.AddPacket(Buffer: Pointer; BufLen: LongWord);
var
p: PNetPacket;
begin
if BufLen > CNetPktLen then BufLen := CNetPktLen;
if FPkt = nil then begin
GetMem(FPkt, SizeOf(TNetPacket));
p := FPkt;
end else begin
p := FPkt;
while p <> nil do begin
if p^.Next = nil then Break;
p := p^.Next;
end;
GetMem(p^.Next, SizeOf(TNetPacket));
p := p^.Next;
end;
p^.BufLen := BufLen;
p^.Offset := 0;
p^.Next := nil;
Move(Buffer^, p^.Buf, BufLen);
end;
procedure TNetBuffer.DelPacket;
var
p: PNetPacket;
begin
if (FPkt = nil) then Exit;
if FPkt^.Next <> nil then
begin
p := FPkt^.Next;
FreeMem(FPkt);
FPkt := p;
end else
begin
FreeMem(FPkt);
FPkt := nil;
end;
end;
function TNetBuffer.GetPacket(Buffer: Pointer): LongWord;
begin
if (FPkt = nil) or (FPkt^.Offset >= FPkt^.BufLen) then
Result := 0
else begin
Move(Ptr(LongWord(@FPkt^.Buf) + FPkt^.Offset)^, Buffer^, FPkt^.BufLen - FPkt^.Offset);
Result := FPkt^.BufLen - FPkt^.Offset;
end;
end;
function TNetBuffer.SkipData(Len: Word): Boolean;
begin
if FPkt = nil then
Result := True
else begin
Inc(FPkt^.Offset, Len);
Result := FPkt^.Offset >= FPkt^.BufLen;
end;
end;
procedure TNetBuffer.AddStr(const Value: RawByteString);
begin
AddPacket(@Value[1], Length(Value));
end;
function TNetBuffer.GetStr: RawByteString;
var
p: array[0..CNetPktLen] of Char;
begin
p[GetPacket(@p)] := #0;
Result := PAnsiChar(@p);
end;
function TNetBuffer.GetLength: LongWord;
var
p: PNetPacket;
begin
Result := 0;
p := FPkt;
while p <> nil do begin
Inc(Result, p^.BufLen);
p := p^.Next;
end;
end;
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
{ TCustromSocket }
{** CONSTRUCTOR **}
constructor TCustomSocket.Create;
begin
inherited Create(True);
FConnected := False;
FWorking := False;
FAssync := DefaultSockType;
FBuffer := TNetBuffer.Create;
end;
{** DESTRUCTOR **}
destructor TCustomSocket.Destroy;
begin
FreeSocket;
if FBuffer <> nil then FBuffer.Free;
inherited Destroy;
end;
procedure TCustomSocket.ProcessBuffer;
var
ret: Integer;
Buf: array[0..CNetPktLen - 1] of Byte;
begin
if FSocket <> INVALID_SOCKET then
while True do
begin
FBuffer.Enter;
ret := FBuffer.GetPacket(@Buf);
FBuffer.Leave;
if (ret < 1) then begin {All data has been sent}
if (not FDataSentEvent) then
Synchronize(OnDataSent);
FDataSentEvent := True;
Exit;
end;
if send(FSocket, Buf, ret, 0) = SOCKET_ERROR then begin
FLastError := WSAGetLastError;
FLastErrMsg := ICQLanguages[FErrLang].Translate(IMSG_ESOCK_SEND);
Synchronize(OnError);
Break;
end else
FBuffer.Enter;
if FBuffer.SkipData(ret) then
FBuffer.DelPacket;
FBuffer.Leave;
end;
end;
{Tries to resolve host and stores result in FIp, returns False on error}
function TCustomSocket.Resolve(const Host: RawByteString): Integer;
var
he: PHostEnt;
begin
Result := inet_addr(PAnsiChar(Host));
if DWord(Result) = DWord(INADDR_NONE) then
begin
he := gethostbyname(PAnsiChar(Host));
if he = nil then Exit;
Result := PInteger(he^.h_addr_list^)^;
end;
end;
{** Main Thread **}
procedure TCustomSocket.Execute;
var
sin: sockaddr_in;
buf: array[0..CNetPktLen - 1] of AnsiChar;
rc: Integer;
begin
try
if (FDoConnect) and (not Terminated) then begin
{Resolving Host}
FIp := Resolve(FHost);
{$IFDEF DEBUGDC}
SendDebugFmtEx('Resolved FHost=%s to FIp=%d', [FHost, FIP], mtInformation);
{$ENDIF}
if DWord(FIp) = DWord(INADDR_NONE) then begin
if (not Terminated) then begin
FLastError := WSAGetLastError;
FLastErrMsg := ICQLanguages[FErrLang].Translate(IMSG_ESOCK_RESOLVE);
Synchronize(OnError);
end;
Exit;
end else begin
sin.sin_family := PF_INET; sin.sin_addr.S_addr := FIp; sin.sin_port := htons(FPort);
{Connecting...}
if WinSock.connect(FSocket, sin, SizeOf(sin)) = SOCKET_ERROR then begin
FLastError := WSAGetLastError;
FLastErrMsg := ICQLanguages[FErrLang].Translate(IMSG_ESOCK_CONNECT);
if (not Terminated) then Synchronize(OnConnectError);
Exit;
end;
end;
end;
{Connected successfully!}
FConnected := True;
{$IFDEF DEBUGDC}
SendDebugEx('Connected', mtInformation);
{$ENDIF}
Synchronize(OnConnect);
{Receiving data if any avaible}
while not Terminated do begin
{
If no data arrived in 100ms we send qued data to the connected socket.
That slows sometimes connection, but doesn't require another thread
for sending.
}
if Assync and (not WaitForRead(FSocket, 10)) then begin
ProcessBuffer;
end else begin
{$IFDEF DEBUGDC}
SendDebugEx('Using OnRecv.', mtInformation);
{$ENDIF}
rc := recv(FSocket, buf, SizeOf(buf), 0);
if (rc = 0) and (not Terminated) then begin Synchronize(OnDisconnect); Break; end;
if (rc = SOCKET_ERROR) then begin
rc := WSAGetLastError;
if ((rc = WSAECONNRESET) or (rc = WSAECONNABORTED)) and (not Terminated) then begin Synchronize(OnDisconnect); Break; end;
if (not Terminated) then begin
FLastError := rc;
FLastErrMsg := ICQLanguages[FErrLang].Translate(IMSG_ESOCK_RECV);
Synchronize(OnError);
end;
Break;
end;
Buffer := @buf;
BufLen := rc;
if rc > 0 then
if Assync then
Synchronize(OnReceive)
else
OnReceive;
end;
end;
except on E:Exception do
begin
if Assigned(OnSocketThreadException) then
OnSocketThreadException(Self, E.Message);
end;
end;
end;
{Start work w/o allocating the new socket}
procedure TCustomSocket.StartWork(Socket: TSocket);
begin
if FWorking then Exit else FWorking := True;
FDoConnect := False; FConnected := False;
FSocket := Socket;
Resume;
end;
{Start work with new socket}
procedure TCustomSocket.Connect;
begin
if FWorking then Exit else FWorking := True;
FDoConnect := True; FConnected := False;