| 1 |
{==============================================================================| |
| 2 |
| Project : Ararat Synapse | 009.010.002 | |
| 3 |
|==============================================================================| |
| 4 |
| Content: Library base | |
| 5 |
|==============================================================================| |
| 6 |
| Copyright (c)1999-2021, Lukas Gebauer | |
| 7 |
| All rights reserved. | |
| 8 |
| | |
| 9 |
| Redistribution and use in source and binary forms, with or without | |
| 10 |
| modification, are permitted provided that the following conditions are met: | |
| 11 |
| | |
| 12 |
| Redistributions of source code must retain the above copyright notice, this | |
| 13 |
| list of conditions and the following disclaimer. | |
| 14 |
| | |
| 15 |
| Redistributions in binary form must reproduce the above copyright notice, | |
| 16 |
| this list of conditions and the following disclaimer in the documentation | |
| 17 |
| and/or other materials provided with the distribution. | |
| 18 |
| | |
| 19 |
| Neither the name of Lukas Gebauer nor the names of its contributors may | |
| 20 |
| be used to endorse or promote products derived from this software without | |
| 21 |
| specific prior written permission. | |
| 22 |
| | |
| 23 |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 24 |
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 25 |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 26 |
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR | |
| 27 |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 |
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 29 |
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| 30 |
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 31 |
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 32 |
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
| 33 |
| DAMAGE. | |
| 34 |
|==============================================================================| |
| 35 |
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).| |
| 36 |
| Portions created by Lukas Gebauer are Copyright (c)1999-2021. | |
| 37 |
| All Rights Reserved. | |
| 38 |
|==============================================================================| |
| 39 |
| Contributor(s): | |
| 40 |
|==============================================================================| |
| 41 |
| History: see HISTORY.HTM from distribution package | |
| 42 |
| (Found at URL: http://www.ararat.cz/synapse/) | |
| 43 |
|==============================================================================} |
| 44 |
|
| 45 |
{ |
| 46 |
Special thanks to Gregor Ibic <[email protected]> |
| 47 |
(Intelicom d.o.o., http://www.intelicom.si) |
| 48 |
for good inspiration about SSL programming. |
| 49 |
} |
| 50 |
|
| 51 |
{$DEFINE ONCEWINSOCK} |
| 52 |
{Note about define ONCEWINSOCK: |
| 53 |
If you remove this compiler directive, then socket interface is loaded and |
| 54 |
initialized on constructor of TBlockSocket class for each socket separately. |
| 55 |
Socket interface is used only if your need it. |
| 56 |
|
| 57 |
If you leave this directive here, then socket interface is loaded and |
| 58 |
initialized only once at start of your program! It boost performace on high |
| 59 |
count of created and destroyed sockets. It eliminate possible small resource |
| 60 |
leak on Windows systems too. |
| 61 |
} |
| 62 |
|
| 63 |
//{$DEFINE RAISEEXCEPT} |
| 64 |
{When you enable this define, then is Raiseexcept property is on by default |
| 65 |
} |
| 66 |
|
| 67 |
{:@abstract(Synapse's library core) |
| 68 |
|
| 69 |
Core with implementation basic socket classes. |
| 70 |
} |
| 71 |
|
| 72 |
{$IFDEF FPC} |
| 73 |
{$MODE DELPHI} |
| 74 |
{$ENDIF} |
| 75 |
{$IFDEF VER125} |
| 76 |
{$DEFINE BCB} |
| 77 |
{$ENDIF} |
| 78 |
{$IFDEF BCB} |
| 79 |
{$ObjExportAll On} |
| 80 |
{$ENDIF} |
| 81 |
{$Q-} |
| 82 |
{$H+} |
| 83 |
{$M+} |
| 84 |
{$TYPEDADDRESS OFF} |
| 85 |
|
| 86 |
|
| 87 |
//old Delphi does not have MSWINDOWS define. |
| 88 |
{$IFDEF WIN32} |
| 89 |
{$IFNDEF MSWINDOWS} |
| 90 |
{$DEFINE MSWINDOWS} |
| 91 |
{$ENDIF} |
| 92 |
{$ENDIF} |
| 93 |
|
| 94 |
{$IFDEF UNICODE} |
| 95 |
{$WARN IMPLICIT_STRING_CAST OFF} |
| 96 |
{$WARN IMPLICIT_STRING_CAST_LOSS OFF} |
| 97 |
{$ENDIF} |
| 98 |
|
| 99 |
{$IFDEF NEXTGEN} |
| 100 |
{$ZEROBASEDSTRINGS OFF} |
| 101 |
{$ENDIF} |
| 102 |
|
| 103 |
unit blcksock; |
| 104 |
|
| 105 |
interface |
| 106 |
|
| 107 |
uses |
| 108 |
SysUtils, Classes, |
| 109 |
synafpc, |
| 110 |
synsock, synautil, synacode, synaip |
| 111 |
{$IFDEF POSIX} |
| 112 |
,System.Generics.Collections, System.Generics.Defaults |
| 113 |
{$ENDIF} |
| 114 |
{$IfDef CIL} |
| 115 |
,System.Net |
| 116 |
,System.Net.Sockets |
| 117 |
,System.Text |
| 118 |
{$EndIf} |
| 119 |
; |
| 120 |
|
| 121 |
const |
| 122 |
|
| 123 |
SynapseRelease = '40'; |
| 124 |
|
| 125 |
cLocalhost = '127.0.0.1'; |
| 126 |
cAnyHost = '0.0.0.0'; |
| 127 |
cBroadcast = '255.255.255.255'; |
| 128 |
c6Localhost = '::1'; |
| 129 |
c6AnyHost = '::0'; |
| 130 |
c6Broadcast = 'ffff::1'; |
| 131 |
cAnyPort = '0'; |
| 132 |
CR = #$0d; |
| 133 |
LF = #$0a; |
| 134 |
CRLF = CR + LF; |
| 135 |
c64k = 65536; |
| 136 |
|
| 137 |
type |
| 138 |
|
| 139 |
{:@abstract(Exception clas used by Synapse) |
| 140 |
When you enable generating of exceptions, this exception is raised by |
| 141 |
Synapse's units.} |
| 142 |
ESynapseError = class(Exception) |
| 143 |
private |
| 144 |
FErrorCode: Integer; |
| 145 |
FErrorMessage: string; |
| 146 |
published |
| 147 |
{:Code of error. Value depending on used operating system} |
| 148 |
property ErrorCode: Integer read FErrorCode Write FErrorCode; |
| 149 |
{:Human readable description of error.} |
| 150 |
property ErrorMessage: string read FErrorMessage Write FErrorMessage; |
| 151 |
end; |
| 152 |
|
| 153 |
{:Types of OnStatus events} |
| 154 |
THookSocketReason = ( |
| 155 |
{:Resolving is begin. Resolved IP and port is in parameter in format like: |
| 156 |
'localhost.somewhere.com:25'.} |
| 157 |
HR_ResolvingBegin, |
| 158 |
{:Resolving is done. Resolved IP and port is in parameter in format like: |
| 159 |
'localhost.somewhere.com:25'. It is always same as in HR_ResolvingBegin!} |
| 160 |
HR_ResolvingEnd, |
| 161 |
{:Socket created by CreateSocket method. It reporting Family of created |
| 162 |
socket too!} |
| 163 |
HR_SocketCreate, |
| 164 |
{:Socket closed by CloseSocket method.} |
| 165 |
HR_SocketClose, |
| 166 |
{:Socket binded to IP and Port. Binded IP and Port is in parameter in format |
| 167 |
like: 'localhost.somewhere.com:25'.} |
| 168 |
HR_Bind, |
| 169 |
{:Socket connected to IP and Port. Connected IP and Port is in parameter in |
| 170 |
format like: 'localhost.somewhere.com:25'.} |
| 171 |
HR_Connect, |
| 172 |
{:Called when CanRead method is used with @True result.} |
| 173 |
HR_CanRead, |
| 174 |
{:Called when CanWrite method is used with @True result.} |
| 175 |
HR_CanWrite, |
| 176 |
{:Socket is swithed to Listen mode. (TCP socket only)} |
| 177 |
HR_Listen, |
| 178 |
{:Socket Accepting client connection. (TCP socket only)} |
| 179 |
HR_Accept, |
| 180 |
{:report count of bytes readed from socket. Number is in parameter string. |
| 181 |
If you need is in integer, you must use StrToInt function!} |
| 182 |
HR_ReadCount, |
| 183 |
{:report count of bytes writed to socket. Number is in parameter string. If |
| 184 |
you need is in integer, you must use StrToInt function!} |
| 185 |
HR_WriteCount, |
| 186 |
{:If is limiting of bandwidth on, then this reason is called when sending or |
| 187 |
receiving is stopped for satisfy bandwidth limit. Parameter is count of |
| 188 |
waiting milliseconds.} |
| 189 |
HR_Wait, |
| 190 |
{:report situation where communication error occured. When raiseexcept is |
| 191 |
@true, then exception is called after this Hook reason.} |
| 192 |
HR_Error |
| 193 |
); |
| 194 |
|
| 195 |
{:Procedural type for OnStatus event. Sender is calling TBlockSocket object, |
| 196 |
Reason is one of set Status events and value is optional data.} |
| 197 |
THookSocketStatus = procedure(Sender: TObject; Reason: THookSocketReason; |
| 198 |
const Value: String) of object; |
| 199 |
|
| 200 |
{:This procedural type is used for DataFilter hooks.} |
| 201 |
THookDataFilter = procedure(Sender: TObject; var Value: AnsiString) of object; |
| 202 |
|
| 203 |
{:This procedural type is used for hook OnCreateSocket. By this hook you can |
| 204 |
insert your code after initialisation of socket. (you can set special socket |
| 205 |
options, etc.)} |
| 206 |
THookCreateSocket = procedure(Sender: TObject) of object; |
| 207 |
|
| 208 |
{:This procedural type is used for monitoring of communication.} |
| 209 |
THookMonitor = procedure(Sender: TObject; Writing: Boolean; |
| 210 |
const Buffer: TMemory; Len: Integer) of object; |
| 211 |
|
| 212 |
{:This procedural type is used for hook OnAfterConnect. By this hook you can |
| 213 |
insert your code after TCP socket has been sucessfully connected.} |
| 214 |
THookAfterConnect = procedure(Sender: TObject) of object; |
| 215 |
|
| 216 |
{:This procedural type is used for hook OnVerifyCert. By this hook you can |
| 217 |
insert your additional certificate verification code. Usefull to verify server |
| 218 |
CN against URL. } |
| 219 |
|
| 220 |
THookVerifyCert = function(Sender: TObject):boolean of object; |
| 221 |
|
| 222 |
{:This procedural type is used for hook OnHeartbeat. By this hook you can |
| 223 |
call your code repeately during long socket operations. |
| 224 |
You must enable heartbeats by @Link(HeartbeatRate) property!} |
| 225 |
THookHeartbeat = procedure(Sender: TObject) of object; |
| 226 |
|
| 227 |
{:Specify family of socket.} |
| 228 |
TSocketFamily = ( |
| 229 |
{:Default mode. Socket family is defined by target address for connection. |
| 230 |
It allows instant access to IPv4 and IPv6 nodes. When you need IPv6 address |
| 231 |
as destination, then is used IPv6 mode. othervise is used IPv4 mode. |
| 232 |
However this mode not working properly with preliminary IPv6 supports!} |
| 233 |
SF_Any, |
| 234 |
{:Turn this class to pure IPv4 mode. This mode is totally compatible with |
| 235 |
previous Synapse releases.} |
| 236 |
SF_IP4, |
| 237 |
{:Turn to only IPv6 mode.} |
| 238 |
SF_IP6 |
| 239 |
); |
| 240 |
|
| 241 |
{:specify possible values of SOCKS modes.} |
| 242 |
TSocksType = ( |
| 243 |
ST_Socks5, |
| 244 |
ST_Socks4 |
| 245 |
); |
| 246 |
|
| 247 |
{:Specify requested SSL/TLS version for secure connection.} |
| 248 |
TSSLType = ( |
| 249 |
LT_all, |
| 250 |
LT_SSLv2, |
| 251 |
LT_SSLv3, |
| 252 |
LT_TLSv1, |
| 253 |
LT_TLSv1_1, |
| 254 |
LT_TLSv1_2, |
| 255 |
LT_TLSv1_3, |
| 256 |
LT_SSHv2 |
| 257 |
); |
| 258 |
|
| 259 |
{:Specify type of socket delayed option.} |
| 260 |
TSynaOptionType = ( |
| 261 |
SOT_Linger, |
| 262 |
SOT_RecvBuff, |
| 263 |
SOT_SendBuff, |
| 264 |
SOT_NonBlock, |
| 265 |
SOT_RecvTimeout, |
| 266 |
SOT_SendTimeout, |
| 267 |
SOT_Reuse, |
| 268 |
SOT_TTL, |
| 269 |
SOT_Broadcast, |
| 270 |
SOT_MulticastTTL, |
| 271 |
SOT_MulticastLoop |
| 272 |
); |
| 273 |
|
| 274 |
{:@abstract(this object is used for remember delayed socket option set.)} |
| 275 |
TSynaOption = class(TObject) |
| 276 |
public |
| 277 |
Option: TSynaOptionType; |
| 278 |
Enabled: Boolean; |
| 279 |
Value: Integer; |
| 280 |
end; |
| 281 |
|
| 282 |
TCustomSSL = class; |
| 283 |
TSSLClass = class of TCustomSSL; |
| 284 |
|
| 285 |
TBlockSocket = class; |
| 286 |
|
| 287 |
{$IFDEF POSIX} |
| 288 |
TOptionList = TList<TSynaOption>; |
| 289 |
TSocketList = TList<TBlockSocket>; |
| 290 |
{$ELSE} |
| 291 |
TOptionList = TList; |
| 292 |
TSocketList = TList; |
| 293 |
{$ENDIF} |
| 294 |
|
| 295 |
{:@abstract(Basic IP object.) |
| 296 |
This is parent class for other class with protocol implementations. Do not |
| 297 |
use this class directly! Use @link(TICMPBlockSocket), @link(TRAWBlockSocket), |
| 298 |
@link(TTCPBlockSocket) or @link(TUDPBlockSocket) instead.} |
| 299 |
TBlockSocket = class(TObject) |
| 300 |
private |
| 301 |
FOnStatus: THookSocketStatus; |
| 302 |
FOnReadFilter: THookDataFilter; |
| 303 |
FOnCreateSocket: THookCreateSocket; |
| 304 |
FOnMonitor: THookMonitor; |
| 305 |
FOnHeartbeat: THookHeartbeat; |
| 306 |
FLocalSin: TVarSin; |
| 307 |
FRemoteSin: TVarSin; |
| 308 |
FTag: integer; |
| 309 |
FBuffer: AnsiString; |
| 310 |
FRaiseExcept: Boolean; |
| 311 |
FNonBlockMode: Boolean; |
| 312 |
FMaxLineLength: Integer; |
| 313 |
FMaxSendBandwidth: Integer; |
| 314 |
FNextSend: LongWord; |
| 315 |
FMaxRecvBandwidth: Integer; |
| 316 |
FNextRecv: LongWord; |
| 317 |
FConvertLineEnd: Boolean; |
| 318 |
FLastCR: Boolean; |
| 319 |
FLastLF: Boolean; |
| 320 |
FBinded: Boolean; |
| 321 |
FFamily: TSocketFamily; |
| 322 |
FFamilySave: TSocketFamily; |
| 323 |
FIP6used: Boolean; |
| 324 |
FPreferIP4: Boolean; |
| 325 |
FDelayedOptions: TOptionList; |
| 326 |
FInterPacketTimeout: Boolean; |
| 327 |
{$IFNDEF CIL} |
| 328 |
FFDSet: TFDSet; |
| 329 |
{$ENDIF} |
| 330 |
FRecvCounter: int64; |
| 331 |
FSendCounter: int64; |
| 332 |
FSendMaxChunk: Integer; |
| 333 |
FStopFlag: Boolean; |
| 334 |
FNonblockSendTimeout: Integer; |
| 335 |
FHeartbeatRate: integer; |
| 336 |
FConnectionTimeout: integer; |
| 337 |
{$IFNDEF ONCEWINSOCK} |
| 338 |
FWsaDataOnce: TWSADATA; |
| 339 |
{$ENDIF} |
| 340 |
function GetSizeRecvBuffer: Integer; |
| 341 |
procedure SetSizeRecvBuffer(Size: Integer); |
| 342 |
function GetSizeSendBuffer: Integer; |
| 343 |
procedure SetSizeSendBuffer(Size: Integer); |
| 344 |
procedure SetNonBlockMode(Value: Boolean); |
| 345 |
procedure SetTTL(TTL: integer); |
| 346 |
function GetTTL:integer; |
| 347 |
procedure SetFamily(Value: TSocketFamily); virtual; |
| 348 |
procedure SetSocket(Value: TSocket); virtual; |
| 349 |
function GetWsaData: TWSAData; |
| 350 |
function FamilyToAF(f: TSocketFamily): TAddrFamily; |
| 351 |
protected |
| 352 |
FSocket: TSocket; |
| 353 |
FLastError: Integer; |
| 354 |
FLastErrorDesc: string; |
| 355 |
FOwner: TObject; |
| 356 |
procedure SetDelayedOption(const Value: TSynaOption); |
| 357 |
procedure DelayedOption(const Value: TSynaOption); |
| 358 |
procedure ProcessDelayedOptions; |
| 359 |
procedure InternalCreateSocket(Sin: TVarSin); |
| 360 |
procedure SetSin(var Sin: TVarSin; IP, Port: string); |
| 361 |
function GetSinIP(Sin: TVarSin): string; |
| 362 |
function GetSinPort(Sin: TVarSin): Integer; |
| 363 |
procedure DoStatus(Reason: THookSocketReason; const Value: string); |
| 364 |
procedure DoReadFilter(Buffer: TMemory; var Len: Integer); |
| 365 |
procedure DoMonitor(Writing: Boolean; const Buffer: TMemory; Len: Integer); |
| 366 |
procedure DoCreateSocket; |
| 367 |
procedure DoHeartbeat; |
| 368 |
procedure LimitBandwidth(Length: Integer; MaxB: integer; var Next: LongWord); |
| 369 |
procedure SetBandwidth(Value: Integer); |
| 370 |
function TestStopFlag: Boolean; |
| 371 |
procedure InternalSendStream(const Stream: TStream; WithSize, Indy: boolean); virtual; |
| 372 |
function InternalCanRead(Timeout: Integer): Boolean; virtual; |
| 373 |
function InternalCanWrite(Timeout: Integer): Boolean; virtual; |
| 374 |
public |
| 375 |
constructor Create; |
| 376 |
|
| 377 |
{:Create object and load all necessary socket library. What library is |
| 378 |
loaded is described by STUB parameter. If STUB is empty string, then is |
| 379 |
loaded default libraries.} |
| 380 |
constructor CreateAlternate(Stub: string); |
| 381 |
destructor Destroy; override; |
| 382 |
|
| 383 |
{:If @link(family) is not SF_Any, then create socket with type defined in |
| 384 |
@link(Family) property. If family is SF_Any, then do nothing! (socket is |
| 385 |
created automaticly when you know what type of socket you need to create. |
| 386 |
(i.e. inside @link(Connect) or @link(Bind) call.) When socket is created, |
| 387 |
then is aplyed all stored delayed socket options.} |
| 388 |
procedure CreateSocket; |
| 389 |
|
| 390 |
{:It create socket. Address resolving of Value tells what type of socket is |
| 391 |
created. If Value is resolved as IPv4 IP, then is created IPv4 socket. If |
| 392 |
value is resolved as IPv6 address, then is created IPv6 socket.} |
| 393 |
procedure CreateSocketByName(const Value: String); |
| 394 |
|
| 395 |
{:Destroy socket in use. This method is also automatically called from |
| 396 |
object destructor.} |
| 397 |
procedure CloseSocket; virtual; |
| 398 |
|
| 399 |
{:Abort any work on Socket and destroy them.} |
| 400 |
procedure AbortSocket; virtual; |
| 401 |
|
| 402 |
{:Connects socket to local IP address and PORT. IP address may be numeric or |
| 403 |
symbolic ('192.168.74.50', 'cosi.nekde.cz', 'ff08::1'). The same for PORT |
| 404 |
- it may be number or mnemonic port ('23', 'telnet'). |
| 405 |
|
| 406 |
If port value is '0', system chooses itself and conects unused port in the |
| 407 |
range 1024 to 4096 (this depending by operating system!). Structure |
| 408 |
LocalSin is filled after calling this method. |
| 409 |
|
| 410 |
Note: If you call this on non-created socket, then socket is created |
| 411 |
automaticly. |
| 412 |
|
| 413 |
Warning: when you call : Bind('0.0.0.0','0'); then is nothing done! In this |
| 414 |
case is used implicit system bind instead.} |
| 415 |
procedure Bind(const IP, Port: string); |
| 416 |
|
| 417 |
{:Connects socket to remote IP address and PORT. The same rules as with |
| 418 |
@link(BIND) method are valid. The only exception is that PORT with 0 value |
| 419 |
will not be connected! |
| 420 |
|
| 421 |
Structures LocalSin and RemoteSin will be filled with valid values. |
| 422 |
|
| 423 |
When you call this on non-created socket, then socket is created |
| 424 |
automaticly. Type of created socket is by @link(Family) property. If is |
| 425 |
used SF_IP4, then is created socket for IPv4. If is used SF_IP6, then is |
| 426 |
created socket for IPv6. When you have family on SF_Any (default!), then |
| 427 |
type of created socket is determined by address resolving of destination |
| 428 |
address. (Not work properly on prilimitary winsock IPv6 support!)} |
| 429 |
procedure Connect(IP, Port: string); virtual; |
| 430 |
|
| 431 |
{:Sets socket to receive mode for new incoming connections. It is necessary |
| 432 |
to use @link(TBlockSocket.BIND) function call before this method to select |
| 433 |
receiving port!} |
| 434 |
procedure Listen; virtual; |
| 435 |
|
| 436 |
{:Waits until new incoming connection comes. After it comes a new socket is |
| 437 |
automatically created (socket handler is returned by this function as |
| 438 |
result).} |
| 439 |
function Accept: TSocket; virtual; |
| 440 |
|
| 441 |
{:Sends data of LENGTH from BUFFER address via connected socket. System |
| 442 |
automatically splits data to packets.} |
| 443 |
function SendBuffer(const Buffer: Tmemory; Length: Integer): Integer; virtual; |
| 444 |
|
| 445 |
{:One data BYTE is sent via connected socket.} |
| 446 |
procedure SendByte(Data: Byte); virtual; |
| 447 |
|
| 448 |
{:Send data string via connected socket. Any terminator is not added! If you |
| 449 |
need send true string with CR-LF termination, you must add CR-LF characters |
| 450 |
to sended string! Because any termination is not added automaticly, you can |
| 451 |
use this function for sending any binary data in binary string.} |
| 452 |
procedure SendString(Data: AnsiString); virtual; |
| 453 |
|
| 454 |
{:Send integer as four bytes to socket.} |
| 455 |
procedure SendInteger(Data: integer); virtual; |
| 456 |
|
| 457 |
{:Send data as one block to socket. Each block begin with 4 bytes with |
| 458 |
length of data in block. This 4 bytes is added automaticly by this |
| 459 |
function.} |
| 460 |
procedure SendBlock(const Data: AnsiString); virtual; |
| 461 |
|
| 462 |
{:Send data from stream to socket.} |
| 463 |
procedure SendStreamRaw(const Stream: TStream); virtual; |
| 464 |
|
| 465 |
{:Send content of stream to socket. It using @link(SendBlock) method} |
| 466 |
procedure SendStream(const Stream: TStream); virtual; |
| 467 |
|
| 468 |
{:Send content of stream to socket. It using @link(SendBlock) method and |
| 469 |
this is compatible with streams in Indy library.} |
| 470 |
procedure SendStreamIndy(const Stream: TStream); virtual; |
| 471 |
|
| 472 |
{:Note: This is low-level receive function. You must be sure if data is |
| 473 |
waiting for read before call this function for avoid deadlock! |
| 474 |
|
| 475 |
Waits until allocated buffer is filled by received data. Returns number of |
| 476 |
data received, which equals to LENGTH value under normal operation. If it |
| 477 |
is not equal the communication channel is possibly broken. |
| 478 |
|
| 479 |
On stream oriented sockets if is received 0 bytes, it mean 'socket is |
| 480 |
closed!" |
| 481 |
|
| 482 |
On datagram socket is readed first waiting datagram.} |
| 483 |
function RecvBuffer(Buffer: TMemory; Length: Integer): Integer; virtual; |
| 484 |
|
| 485 |
{:Note: This is high-level receive function. It using internal |
| 486 |
@link(LineBuffer) and you can combine this function freely with other |
| 487 |
high-level functions! |
| 488 |
|
| 489 |
Method waits until data is received. If no data is received within TIMEOUT |
| 490 |
(in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT. Methods |
| 491 |
serves for reading any size of data (i.e. one megabyte...). This method is |
| 492 |
preffered for reading from stream sockets (like TCP).} |
| 493 |
function RecvBufferEx(Buffer: Tmemory; Len: Integer; |
| 494 |
Timeout: Integer): Integer; virtual; |
| 495 |
|
| 496 |
{:Similar to @link(RecvBufferEx), but readed data is stored in binary |
| 497 |
string, not in memory buffer.} |
| 498 |
function RecvBufferStr(Len: Integer; Timeout: Integer): AnsiString; virtual; |
| 499 |
|
| 500 |
{:Note: This is high-level receive function. It using internal |
| 501 |
@link(LineBuffer) and you can combine this function freely with other |
| 502 |
high-level functions. |
| 503 |
|
| 504 |
Waits until one data byte is received which is also returned as function |
| 505 |
result. If no data is received within TIMEOUT (in milliseconds)period, |
| 506 |
@link(LastError) is set to WSAETIMEDOUT and result have value 0.} |
| 507 |
function RecvByte(Timeout: Integer): Byte; virtual; |
| 508 |
|
| 509 |
{:Note: This is high-level receive function. It using internal |
| 510 |
@link(LineBuffer) and you can combine this function freely with other |
| 511 |
high-level functions. |
| 512 |
|
| 513 |
Waits until one four bytes are received and return it as one Ineger Value. |
| 514 |
If no data is received within TIMEOUT (in milliseconds)period, |
| 515 |
@link(LastError) is set to WSAETIMEDOUT and result have value 0.} |
| 516 |
function RecvInteger(Timeout: Integer): Integer; virtual; |
| 517 |
|
| 518 |
{:Note: This is high-level receive function. It using internal |
| 519 |
@link(LineBuffer) and you can combine this function freely with other |
| 520 |
high-level functions. |
| 521 |
|
| 522 |
Method waits until data string is received. This string is terminated by |
| 523 |
CR-LF characters. The resulting string is returned without this termination |
| 524 |
(CR-LF)! If @link(ConvertLineEnd) is used, then CR-LF sequence may not be |
| 525 |
exactly CR-LF. See @link(ConvertLineEnd) description. If no data is |
| 526 |
received within TIMEOUT (in milliseconds) period, @link(LastError) is set |
| 527 |
to WSAETIMEDOUT. You may also specify maximum length of reading data by |
| 528 |
@link(MaxLineLength) property.} |
| 529 |
function RecvString(Timeout: Integer): AnsiString; virtual; |
| 530 |
|
| 531 |
{:Note: This is high-level receive function. It using internal |
| 532 |
@link(LineBuffer) and you can combine this function freely with other |
| 533 |
high-level functions. |
| 534 |
|
| 535 |
Method waits until data string is received. This string is terminated by |
| 536 |
Terminator string. The resulting string is returned without this |
| 537 |
termination. If no data is received within TIMEOUT (in milliseconds) |
| 538 |
period, @link(LastError) is set to WSAETIMEDOUT. You may also specify |
| 539 |
maximum length of reading data by @link(MaxLineLength) property.} |
| 540 |
function RecvTerminated(Timeout: Integer; const Terminator: AnsiString): AnsiString; virtual; |
| 541 |
|
| 542 |
{:Note: This is high-level receive function. It using internal |
| 543 |
@link(LineBuffer) and you can combine this function freely with other |
| 544 |
high-level functions. |
| 545 |
|
| 546 |
Method reads all data waiting for read. If no data is received within |
| 547 |
TIMEOUT (in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT. |
| 548 |
Methods serves for reading unknown size of data. Because before call this |
| 549 |
function you don't know size of received data, returned data is stored in |
| 550 |
dynamic size binary string. This method is preffered for reading from |
| 551 |
stream sockets (like TCP). It is very goot for receiving datagrams too! |
| 552 |
(UDP protocol)} |
| 553 |
function RecvPacket(Timeout: Integer): AnsiString; virtual; |
| 554 |
|
| 555 |
{:Read one block of data from socket. Each block begin with 4 bytes with |
| 556 |
length of data in block. This function read first 4 bytes for get lenght, |
| 557 |
then it wait for reported count of bytes.} |
| 558 |
function RecvBlock(Timeout: Integer): AnsiString; virtual; |
| 559 |
|
| 560 |
{:Read all data from socket to stream until socket is closed (or any error |
| 561 |
occured.)} |
| 562 |
procedure RecvStreamRaw(const Stream: TStream; Timeout: Integer); virtual; |
| 563 |
{:Read requested count of bytes from socket to stream.} |
| 564 |
procedure RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: int64); |
| 565 |
|
| 566 |
{:Receive data to stream. It using @link(RecvBlock) method.} |
| 567 |
procedure RecvStream(const Stream: TStream; Timeout: Integer); virtual; |
| 568 |
|
| 569 |
{:Receive data to stream. This function is compatible with similar function |
| 570 |
in Indy library. It using @link(RecvBlock) method.} |
| 571 |
procedure RecvStreamIndy(const Stream: TStream; Timeout: Integer); virtual; |
| 572 |
|
| 573 |
{:Same as @link(RecvBuffer), but readed data stays in system input buffer. |
| 574 |
Warning: this function not respect data in @link(LineBuffer)! Is not |
| 575 |
recommended to use this function!} |
| 576 |
function PeekBuffer(Buffer: TMemory; Length: Integer): Integer; virtual; |
| 577 |
|
| 578 |
{:Same as @link(RecvByte), but readed data stays in input system buffer. |
| 579 |
Warning: this function not respect data in @link(LineBuffer)! Is not |
| 580 |
recommended to use this function!} |
| 581 |
function PeekByte(Timeout: Integer): Byte; virtual; |
| 582 |
|
| 583 |
{:On stream sockets it returns number of received bytes waiting for picking. |
| 584 |
0 is returned when there is no such data. On datagram socket it returns |
| 585 |
length of the first waiting datagram. Returns 0 if no datagram is waiting.} |
| 586 |
function WaitingData: Integer; virtual; |
| 587 |
|
| 588 |
{:Same as @link(WaitingData), but if exists some of data in @link(Linebuffer), |
| 589 |
return their length instead.} |
| 590 |
function WaitingDataEx: Integer; |
| 591 |
|
| 592 |
{:Clear all waiting data for read from buffers.} |
| 593 |
procedure Purge; |
| 594 |
|
| 595 |
{:Sets linger. Enabled linger means that the system waits another LINGER |
| 596 |
(in milliseconds) time for delivery of sent data. This function is only for |
| 597 |
stream type of socket! (TCP)} |
| 598 |
procedure SetLinger(Enable: Boolean; Linger: Integer); |
| 599 |
|
| 600 |
{:Actualize values in @link(LocalSin).} |
| 601 |
procedure GetSinLocal; |
| 602 |
|
| 603 |
{:Actualize values in @link(RemoteSin).} |
| 604 |
procedure GetSinRemote; |
| 605 |
|
| 606 |
{:Actualize values in @link(LocalSin) and @link(RemoteSin).} |
| 607 |
procedure GetSins; |
| 608 |
|
| 609 |
{:Reset @link(LastError) and @link(LastErrorDesc) to non-error state.} |
| 610 |
procedure ResetLastError; |
| 611 |
|
| 612 |
{:If you "manually" call Socket API functions, forward their return code as |
| 613 |
parameter to this function, which evaluates it, eventually calls |
| 614 |
GetLastError and found error code returns and stores to @link(LastError).} |
| 615 |
function SockCheck(SockResult: Integer): Integer; virtual; |
| 616 |
|
| 617 |
{:If @link(LastError) contains some error code and @link(RaiseExcept) |
| 618 |
property is @true, raise adequate exception.} |
| 619 |
procedure ExceptCheck; |
| 620 |
|
| 621 |
{:Returns local computer name as numerical or symbolic value. It try get |
| 622 |
fully qualified domain name. Name is returned in the format acceptable by |
| 623 |
functions demanding IP as input parameter.} |
| 624 |
function LocalName: string; |
| 625 |
|
| 626 |
{:Try resolve name to all possible IP address. i.e. If you pass as name |
| 627 |
result of @link(LocalName) method, you get all IP addresses used by local |
| 628 |
system.} |
| 629 |
procedure ResolveNameToIP(Name: string; const IPList: TStrings); |
| 630 |
|
| 631 |
{:Try resolve name to primary IP address. i.e. If you pass as name result of |
| 632 |
@link(LocalName) method, you get primary IP addresses used by local system.} |
| 633 |
function ResolveName(Name: string): string; |
| 634 |
|
| 635 |
{:Try resolve IP to their primary domain name. If IP not have domain name, |
| 636 |
then is returned original IP.} |
| 637 |
function ResolveIPToName(IP: string): string; |
| 638 |
|
| 639 |
{:Try resolve symbolic port name to port number. (i.e. 'Echo' to 8)} |
| 640 |
function ResolvePort(Port: string): Word; |
| 641 |
|
| 642 |
{:Set information about remote side socket. It is good for seting remote |
| 643 |
side for sending UDP packet, etc.} |
| 644 |
procedure SetRemoteSin(IP, Port: string); |
| 645 |
|
| 646 |
{:Picks IP socket address from @link(LocalSin).} |
| 647 |
function GetLocalSinIP: string; virtual; |
| 648 |
|
| 649 |
{:Picks IP socket address from @link(RemoteSin).} |
| 650 |
function GetRemoteSinIP: string; virtual; |
| 651 |
|
| 652 |
{:Picks socket PORT number from @link(LocalSin).} |
| 653 |
function GetLocalSinPort: Integer; virtual; |
| 654 |
|
| 655 |
{:Picks socket PORT number from @link(RemoteSin).} |
| 656 |
function GetRemoteSinPort: Integer; virtual; |
| 657 |
|
| 658 |
{:Return @TRUE, if you can read any data from socket or is incoming |
| 659 |
connection on TCP based socket. Status is tested for time Timeout (in |
| 660 |
milliseconds). If value in Timeout is 0, status is only tested and |
| 661 |
continue. If value in Timeout is -1, run is breaked and waiting for read |
| 662 |
data maybe forever. |
| 663 |
|
| 664 |
This function is need only on special cases, when you need use |
| 665 |
@link(RecvBuffer) function directly! read functioms what have timeout as |
| 666 |
calling parameter, calling this function internally.} |
| 667 |
function CanRead(Timeout: Integer): Boolean; virtual; |
| 668 |
|
| 669 |
{:Same as @link(CanRead), but additionally return @TRUE if is some data in |
| 670 |
@link(LineBuffer).} |
| 671 |
function CanReadEx(Timeout: Integer): Boolean; virtual; |
| 672 |
|
| 673 |
{:Return @TRUE, if you can to socket write any data (not full sending |
| 674 |
buffer). Status is tested for time Timeout (in milliseconds). If value in |
| 675 |
Timeout is 0, status is only tested and continue. If value in Timeout is |
| 676 |
-1, run is breaked and waiting for write data maybe forever. |
| 677 |
|
| 678 |
This function is need only on special cases!} |
| 679 |
function CanWrite(Timeout: Integer): Boolean; virtual; |
| 680 |
|
| 681 |
{:Same as @link(SendBuffer), but send datagram to address from |
| 682 |
@link(RemoteSin). Usefull for sending reply to datagram received by |
| 683 |
function @link(RecvBufferFrom).} |
| 684 |
function SendBufferTo(const Buffer: TMemory; Length: Integer): Integer; virtual; |
| 685 |
|
| 686 |
{:Note: This is low-lever receive function. You must be sure if data is |
| 687 |
waiting for read before call this function for avoid deadlock! |
| 688 |
|
| 689 |
Receives first waiting datagram to allocated buffer. If there is no waiting |
| 690 |
one, then waits until one comes. Returns length of datagram stored in |
| 691 |
BUFFER. If length exceeds buffer datagram is truncated. After this |
| 692 |
@link(RemoteSin) structure contains information about sender of UDP packet.} |
| 693 |
function RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; virtual; |
| 694 |
{$IFNDEF CIL} |
| 695 |
{:This function is for check for incoming data on set of sockets. Whitch |
| 696 |
sockets is checked is decribed by SocketList Tlist with TBlockSocket |
| 697 |
objects. TList may have maximal number of objects defined by FD_SETSIZE |
| 698 |
constant. Return @TRUE, if you can from some socket read any data or is |
| 699 |
incoming connection on TCP based socket. Status is tested for time Timeout |
| 700 |
(in milliseconds). If value in Timeout is 0, status is only tested and |
| 701 |
continue. If value in Timeout is -1, run is breaked and waiting for read |
| 702 |
data maybe forever. If is returned @TRUE, CanReadList TList is filled by all |
| 703 |
TBlockSocket objects what waiting for read.} |
| 704 |
function GroupCanRead(const SocketList: TSocketList; Timeout: Integer; |
| 705 |
const CanReadList: TSocketList): Boolean; |
| 706 |
{$ENDIF} |
| 707 |
{:By this method you may turn address reuse mode for local @link(bind). It |
| 708 |
is good specially for UDP protocol. Using this with TCP protocol is |
| 709 |
hazardous!} |
| 710 |
procedure EnableReuse(Value: Boolean); |
| 711 |
|
| 712 |
{:Try set timeout for all sending and receiving operations, if socket |
| 713 |
provider can do it. (It not supported by all socket providers!)} |
| 714 |
procedure SetTimeout(Timeout: Integer); |
| 715 |
|
| 716 |
{:Try set timeout for all sending operations, if socket provider can do it. |
| 717 |
(It not supported by all socket providers!)} |
| 718 |
procedure SetSendTimeout(Timeout: Integer); |
| 719 |
|
| 720 |
{:Try set timeout for all receiving operations, if socket provider can do |
| 721 |
it. (It not supported by all socket providers!)} |
| 722 |
procedure SetRecvTimeout(Timeout: Integer); |
| 723 |
|
| 724 |
{:Return value of socket type.} |
| 725 |
function GetSocketType: integer; Virtual; |
| 726 |
|
| 727 |
{:Return value of protocol type for socket creation.} |
| 728 |
function GetSocketProtocol: integer; Virtual; |
| 729 |
|
| 730 |
{:WSA structure with information about socket provider. On non-windows |
| 731 |
platforms this structure is simulated!} |
| 732 |
property WSAData: TWSADATA read GetWsaData; |
| 733 |
|
| 734 |
{:FDset structure prepared for usage with this socket.} |
| 735 |
property FDset: TFDSet read FFDset; |
| 736 |
|
| 737 |
{:Structure describing local socket side.} |
| 738 |
property LocalSin: TVarSin read FLocalSin write FLocalSin; |
| 739 |
|
| 740 |
{:Structure describing remote socket side.} |
| 741 |
property RemoteSin: TVarSin read FRemoteSin write FRemoteSin; |
| 742 |
|
| 743 |
{:Socket handler. Suitable for "manual" calls to socket API or manual |
| 744 |
connection of socket to a previously created socket (i.e by Accept method |
| 745 |
on TCP socket)} |
| 746 |
property Socket: TSocket read FSocket write SetSocket; |
| 747 |
|
| 748 |
{:Last socket operation error code. Error codes are described in socket |
| 749 |
documentation. Human readable error description is stored in |
| 750 |
@link(LastErrorDesc) property.} |
| 751 |
property LastError: Integer read FLastError; |
| 752 |
|
| 753 |
{:Human readable error description of @link(LastError) code.} |
| 754 |
property LastErrorDesc: string read FLastErrorDesc; |
| 755 |
|
| 756 |
{:Buffer used by all high-level receiving functions. This buffer is used for |
| 757 |
optimized reading of data from socket. In normal cases you not need access |
| 758 |
to this buffer directly!} |
| 759 |
property LineBuffer: AnsiString read FBuffer write FBuffer; |
| 760 |
|
| 761 |
{:Size of Winsock receive buffer. If it is not supported by socket provider, |
| 762 |
it return as size one kilobyte.} |
| 763 |
property SizeRecvBuffer: Integer read GetSizeRecvBuffer write SetSizeRecvBuffer; |
| 764 |
|
| 765 |
{:Size of Winsock send buffer. If it is not supported by socket provider, it |
| 766 |
return as size one kilobyte.} |
| 767 |
property SizeSendBuffer: Integer read GetSizeSendBuffer write SetSizeSendBuffer; |
| 768 |
|
| 769 |
{:If @True, turn class to non-blocking mode. Not all functions are working |
| 770 |
properly in this mode, you must know exactly what you are doing! However |
| 771 |
when you have big experience with non-blocking programming, then you can |
| 772 |
optimise your program by non-block mode!} |
| 773 |
property NonBlockMode: Boolean read FNonBlockMode Write SetNonBlockMode; |
| 774 |
|
| 775 |
{:Set Time-to-live value. (if system supporting it!)} |
| 776 |
property TTL: Integer read GetTTL Write SetTTL; |
| 777 |
|
| 778 |
{:If is @true, then class in in IPv6 mode.} |
| 779 |
property IP6used: Boolean read FIP6used; |
| 780 |
|
| 781 |
{:Return count of received bytes on this socket from begin of current |
| 782 |
connection.} |
| 783 |
property RecvCounter: int64 read FRecvCounter; |
| 784 |
|
| 785 |
{:Return count of sended bytes on this socket from begin of current |
| 786 |
connection.} |
| 787 |
property SendCounter: int64 read FSendCounter; |
| 788 |
published |
| 789 |
{:Return descriptive string for given error code. This is class function. |
| 790 |
You may call it without created object!} |
| 791 |
class function GetErrorDesc(ErrorCode: Integer): string; |
| 792 |
|
| 793 |
{:Return descriptive string for @link(LastError).} |
| 794 |
function GetErrorDescEx: string; virtual; |
| 795 |
|
| 796 |
{:this value is for free use.} |
| 797 |
property Tag: Integer read FTag write FTag; |
| 798 |
|
| 799 |
{:If @true, winsock errors raises exception. Otherwise is setted |
| 800 |
@link(LastError) value only and you must check it from your program! Default |
| 801 |
value is @false.} |
| 802 |
property RaiseExcept: Boolean read FRaiseExcept write FRaiseExcept; |
| 803 |
|
| 804 |
{:Define maximum length in bytes of @link(LineBuffer) for high-level |
| 805 |
receiving functions. If this functions try to read more data then this |
| 806 |
limit, error is returned! If value is 0 (default), no limitation is used. |
| 807 |
This is very good protection for stupid attacks to your server by sending |
| 808 |
lot of data without proper terminator... until all your memory is allocated |
| 809 |
by LineBuffer! |
| 810 |
|
| 811 |
Note: This maximum length is checked only in functions, what read unknown |
| 812 |
number of bytes! (like @link(RecvString) or @link(RecvTerminated))} |
| 813 |
property MaxLineLength: Integer read FMaxLineLength Write FMaxLineLength; |
| 814 |
|
| 815 |
{:Define maximal bandwidth for all sending operations in bytes per second. |
| 816 |
If value is 0 (default), bandwidth limitation is not used.} |
| 817 |
property MaxSendBandwidth: Integer read FMaxSendBandwidth Write FMaxSendBandwidth; |
| 818 |
|
| 819 |
{:Define maximal bandwidth for all receiving operations in bytes per second. |
| 820 |
If value is 0 (default), bandwidth limitation is not used.} |
| 821 |
property MaxRecvBandwidth: Integer read FMaxRecvBandwidth Write FMaxRecvBandwidth; |
| 822 |
|
| 823 |
{:Define maximal bandwidth for all sending and receiving operations in bytes |
| 824 |
per second. If value is 0 (default), bandwidth limitation is not used.} |
| 825 |
property MaxBandwidth: Integer Write SetBandwidth; |
| 826 |
|
| 827 |
{:Do a conversion of non-standard line terminators to CRLF. (Off by default) |
| 828 |
If @True, then terminators like sigle CR, single LF or LFCR are converted |
| 829 |
to CRLF internally. This have effect only in @link(RecvString) method!} |
| 830 |
property ConvertLineEnd: Boolean read FConvertLineEnd Write FConvertLineEnd; |
| 831 |
|
| 832 |
{:Specified Family of this socket. When you are using Windows preliminary |
| 833 |
support for IPv6, then I recommend to set this property!} |
| 834 |
property Family: TSocketFamily read FFamily Write SetFamily; |
| 835 |
|
| 836 |
{:When resolving of domain name return both IPv4 and IPv6 addresses, then |
| 837 |
specify if is used IPv4 (dafault - @true) or IPv6.} |
| 838 |
property PreferIP4: Boolean read FPreferIP4 Write FPreferIP4; |
| 839 |
|
| 840 |
{:By default (@true) is all timeouts used as timeout between two packets in |
| 841 |
reading operations. If you set this to @false, then Timeouts is for overall |
| 842 |
reading operation!} |
| 843 |
property InterPacketTimeout: Boolean read FInterPacketTimeout Write FInterPacketTimeout; |
| 844 |
|
| 845 |
{:All sended datas was splitted by this value.} |
| 846 |
property SendMaxChunk: Integer read FSendMaxChunk Write FSendMaxChunk; |
| 847 |
|
| 848 |
{:By setting this property to @true you can stop any communication. You can |
| 849 |
use this property for soft abort of communication.} |
| 850 |
property StopFlag: Boolean read FStopFlag Write FStopFlag; |
| 851 |
|
| 852 |
{:Timeout for data sending by non-blocking socket mode.} |
| 853 |
property NonblockSendTimeout: Integer read FNonblockSendTimeout Write FNonblockSendTimeout; |
| 854 |
|
| 855 |
{:Timeout for @link(Connect) call. Default value 0 means default system timeout. |
| 856 |
Non-zero value means timeout in millisecond.} |
| 857 |
property ConnectionTimeout: Integer read FConnectionTimeout write FConnectionTimeout; |
| 858 |
|
| 859 |
{:This event is called by various reasons. It is good for monitoring socket, |
| 860 |
create gauges for data transfers, etc.} |
| 861 |
property OnStatus: THookSocketStatus read FOnStatus write FOnStatus; |
| 862 |
|
| 863 |
{:this event is good for some internal thinks about filtering readed datas. |
| 864 |
It is used by telnet client by example.} |
| 865 |
property OnReadFilter: THookDataFilter read FOnReadFilter write FOnReadFilter; |
| 866 |
|
| 867 |
{:This event is called after real socket creation for setting special socket |
| 868 |
options, because you not know when socket is created. (it is depended on |
| 869 |
Ipv4, IPv6 or automatic mode)} |
| 870 |
property OnCreateSocket: THookCreateSocket read FOnCreateSocket write FOnCreateSocket; |
| 871 |
|
| 872 |
{:This event is good for monitoring content of readed or writed datas.} |
| 873 |
property OnMonitor: THookMonitor read FOnMonitor write FOnMonitor; |
| 874 |
|
| 875 |
{:This event is good for calling your code during long socket operations. |
| 876 |
(Example, for refresing UI if class in not called within the thread.) |
| 877 |
Rate of heartbeats can be modified by @link(HeartbeatRate) property.} |
| 878 |
property OnHeartbeat: THookHeartbeat read FOnHeartbeat write FOnHeartbeat; |
| 879 |
|
| 880 |
{:Specify typical rate of @link(OnHeartbeat) event and @link(StopFlag) testing. |
| 881 |
Default value 0 disabling heartbeats! Value is in milliseconds. |
| 882 |
Real rate can be higher or smaller then this value, because it depending |
| 883 |
on real socket operations too! |
| 884 |
Note: Each heartbeat slowing socket processing.} |
| 885 |
property HeartbeatRate: integer read FHeartbeatRate Write FHeartbeatRate; |
| 886 |
{:What class own this socket? Used by protocol implementation classes.} |
| 887 |
property Owner: TObject read FOwner Write FOwner; |
| 888 |
end; |
| 889 |
|
| 890 |
{:@abstract(Support for SOCKS4 and SOCKS5 proxy) |
| 891 |
Layer with definition all necessary properties and functions for |
| 892 |
implementation SOCKS proxy client. Do not use this class directly.} |
| 893 |
TSocksBlockSocket = class(TBlockSocket) |
| 894 |
protected |
| 895 |
FSocksIP: string; |
| 896 |
FSocksPort: string; |
| 897 |
FSocksTimeout: integer; |
| 898 |
FSocksUsername: string; |
| 899 |
FSocksPassword: string; |
| 900 |
FUsingSocks: Boolean; |
| 901 |
FSocksResolver: Boolean; |
| 902 |
FSocksLastError: integer; |
| 903 |
FSocksResponseIP: string; |
| 904 |
FSocksResponsePort: string; |
| 905 |
FSocksLocalIP: string; |
| 906 |
FSocksLocalPort: string; |
| 907 |
FSocksRemoteIP: string; |
| 908 |
FSocksRemotePort: string; |
| 909 |
FBypassFlag: Boolean; |
| 910 |
FSocksType: TSocksType; |
| 911 |
function SocksCode(IP, Port: string): Ansistring; |
| 912 |
function SocksDecode(Value: Ansistring): integer; |
| 913 |
public |
| 914 |
constructor Create; |
| 915 |
|
| 916 |
{:Open connection to SOCKS proxy and if @link(SocksUsername) is set, do |
| 917 |
authorisation to proxy. This is needed only in special cases! (it is called |
| 918 |
internally!)} |
| 919 |
function SocksOpen: Boolean; |
| 920 |
|
| 921 |
{:Send specified request to SOCKS proxy. This is needed only in special |
| 922 |
cases! (it is called internally!)} |
| 923 |
function SocksRequest(Cmd: Byte; const IP, Port: string): Boolean; |
| 924 |
|
| 925 |
{:Receive response to previosly sended request. This is needed only in |
| 926 |
special cases! (it is called internally!)} |
| 927 |
function SocksResponse: Boolean; |
| 928 |
|
| 929 |
{:Is @True when class is using SOCKS proxy.} |
| 930 |
property UsingSocks: Boolean read FUsingSocks; |
| 931 |
|
| 932 |
{:If SOCKS proxy failed, here is error code returned from SOCKS proxy.} |
| 933 |
property SocksLastError: integer read FSocksLastError; |
| 934 |
published |
| 935 |
{:Address of SOCKS server. If value is empty string, SOCKS support is |
| 936 |
disabled. Assingning any value to this property enable SOCKS mode. |
| 937 |
Warning: You cannot combine this mode with HTTP-tunneling mode!} |
| 938 |
property SocksIP: string read FSocksIP write FSocksIP; |
| 939 |
|
| 940 |
{:Port of SOCKS server. Default value is '1080'.} |
| 941 |
property SocksPort: string read FSocksPort write FSocksPort; |
| 942 |
|
| 943 |
{:If you need authorisation on SOCKS server, set username here.} |
| 944 |
property SocksUsername: string read FSocksUsername write FSocksUsername; |
| 945 |
|
| 946 |
{:If you need authorisation on SOCKS server, set password here.} |
| 947 |
property SocksPassword: string read FSocksPassword write FSocksPassword; |
| 948 |
|
| 949 |
{:Specify timeout for communicatin with SOCKS server. Default is one minute.} |
| 950 |
property SocksTimeout: integer read FSocksTimeout write FSocksTimeout; |
| 951 |
|
| 952 |
{:If @True, all symbolic names of target hosts is not translated to IP's |
| 953 |
locally, but resolving is by SOCKS proxy. Default is @True.} |
| 954 |
property SocksResolver: Boolean read FSocksResolver write FSocksResolver; |
| 955 |
|
| 956 |
{:Specify SOCKS type. By default is used SOCKS5, but you can use SOCKS4 too. |
| 957 |
When you select SOCKS4, then if @link(SOCKSResolver) is enabled, then is |
| 958 |
used SOCKS4a. Othervise is used pure SOCKS4.} |
| 959 |
property SocksType: TSocksType read FSocksType write FSocksType; |
| 960 |
end; |
| 961 |
|
| 962 |
{:@abstract(Implementation of TCP socket.) |
| 963 |
Supported features: IPv4, IPv6, SSL/TLS or SSH (depending on used plugin), |
| 964 |
SOCKS5 proxy (outgoing connections and limited incomming), SOCKS4/4a proxy |
| 965 |
(outgoing connections and limited incomming), TCP through HTTP proxy tunnel.} |
| 966 |
TTCPBlockSocket = class(TSocksBlockSocket) |
| 967 |
protected |
| 968 |
FOnAfterConnect: THookAfterConnect; |
| 969 |
FSSL: TCustomSSL; |
| 970 |
FHTTPTunnelIP: string; |
| 971 |
FHTTPTunnelPort: string; |
| 972 |
FHTTPTunnel: Boolean; |
| 973 |
FHTTPTunnelRemoteIP: string; |
| 974 |
FHTTPTunnelRemotePort: string; |
| 975 |
FHTTPTunnelUser: string; |
| 976 |
FHTTPTunnelPass: string; |
| 977 |
FHTTPTunnelTimeout: integer; |
| 978 |
procedure SocksDoConnect(IP, Port: string); |
| 979 |
procedure HTTPTunnelDoConnect(IP, Port: string); |
| 980 |
procedure DoAfterConnect; |
| 981 |
public |
| 982 |
{:Create TCP socket class with default plugin for SSL/TSL/SSH implementation |
| 983 |
(see @link(SSLImplementation))} |
| 984 |
constructor Create; |
| 985 |
|
| 986 |
{:Create TCP socket class with desired plugin for SSL/TSL/SSH implementation} |
| 987 |
constructor CreateWithSSL(SSLPlugin: TSSLClass); |
| 988 |
destructor Destroy; override; |
| 989 |
|
| 990 |
{:See @link(TBlockSocket.CloseSocket)} |
| 991 |
procedure CloseSocket; override; |
| 992 |
|
| 993 |
{:See @link(TBlockSocket.WaitingData)} |
| 994 |
function WaitingData: Integer; override; |
| 995 |
|
| 996 |
{:Sets socket to receive mode for new incoming connections. It is necessary |
| 997 |
to use @link(TBlockSocket.BIND) function call before this method to select |
| 998 |
receiving port! |
| 999 |
|
| 1000 |
If you use SOCKS, activate incoming TCP connection by this proxy. (By BIND |
| 1001 |
method of SOCKS.)} |
| 1002 |
procedure Listen; override; |
| 1003 |
|
| 1004 |
{:Waits until new incoming connection comes. After it comes a new socket is |
| 1005 |
automatically created (socket handler is returned by this function as |
| 1006 |
result). |
| 1007 |
|
| 1008 |
If you use SOCKS, new socket is not created! In this case is used same |
| 1009 |
socket as socket for listening! So, you can accept only one connection in |
| 1010 |
SOCKS mode.} |
| 1011 |
function Accept: TSocket; override; |
| 1012 |
|
| 1013 |
{:Connects socket to remote IP address and PORT. The same rules as with |
| 1014 |
@link(TBlockSocket.BIND) method are valid. The only exception is that PORT |
| 1015 |
with 0 value will not be connected. After call to this method |
| 1016 |
a communication channel between local and remote socket is created. Local |
| 1017 |
socket is assigned automatically if not controlled by previous call to |
| 1018 |
@link(TBlockSocket.BIND) method. Structures @link(TBlockSocket.LocalSin) |
| 1019 |
and @link(TBlockSocket.RemoteSin) will be filled with valid values. |
| 1020 |
|
| 1021 |
If you use SOCKS, activate outgoing TCP connection by SOCKS proxy specified |
| 1022 |
in @link(TSocksBlockSocket.SocksIP). (By CONNECT method of SOCKS.) |
| 1023 |
|
| 1024 |
If you use HTTP-tunnel mode, activate outgoing TCP connection by HTTP |
| 1025 |
tunnel specified in @link(HTTPTunnelIP). (By CONNECT method of HTTP |
| 1026 |
protocol.) |
| 1027 |
|
| 1028 |
Note: If you call this on non-created socket, then socket is created |
| 1029 |
automaticly.} |
| 1030 |
procedure Connect(IP, Port: string); override; |
| 1031 |
|
| 1032 |
{:If you need upgrade existing TCP connection to SSL/TLS (or SSH2, if plugin |
| 1033 |
allows it) mode, then call this method. This method switch this class to |
| 1034 |
SSL mode and do SSL/TSL handshake.} |
| 1035 |
procedure SSLDoConnect; |
| 1036 |
|
| 1037 |
{:By this method you can downgrade existing SSL/TLS connection to normal TCP |
| 1038 |
connection.} |
| 1039 |
procedure SSLDoShutdown; |
| 1040 |
|
| 1041 |
{:If you need use this component as SSL/TLS TCP server, then after accepting |
| 1042 |
of inbound connection you need start SSL/TLS session by this method. Before |
| 1043 |
call this function, you must have assigned all neeeded certificates and |
| 1044 |
keys!} |
| 1045 |
function SSLAcceptConnection: Boolean; |
| 1046 |
|
| 1047 |
{:See @link(TBlockSocket.GetLocalSinIP)} |
| 1048 |
function GetLocalSinIP: string; override; |
| 1049 |
|
| 1050 |
{:See @link(TBlockSocket.GetRemoteSinIP)} |
| 1051 |
function GetRemoteSinIP: string; override; |
| 1052 |
|
| 1053 |
{:See @link(TBlockSocket.GetLocalSinPort)} |
| 1054 |
function GetLocalSinPort: Integer; override; |
| 1055 |
|
| 1056 |
{:See @link(TBlockSocket.GetRemoteSinPort)} |
| 1057 |
function GetRemoteSinPort: Integer; override; |
| 1058 |
|
| 1059 |
{:See @link(TBlockSocket.SendBuffer)} |
| 1060 |
function SendBuffer(const Buffer: TMemory; Length: Integer): Integer; override; |
| 1061 |
|
| 1062 |
{:See @link(TBlockSocket.RecvBuffer)} |
| 1063 |
function RecvBuffer(Buffer: TMemory; Len: Integer): Integer; override; |
| 1064 |
|
| 1065 |
{:Return value of socket type. For TCP return SOCK_STREAM.} |
| 1066 |
function GetSocketType: integer; override; |
| 1067 |
|
| 1068 |
{:Return value of protocol type for socket creation. For TCP return |
| 1069 |
IPPROTO_TCP.} |
| 1070 |
function GetSocketProtocol: integer; override; |
| 1071 |
|
| 1072 |
{:Class implementing SSL/TLS support. It is allways some descendant |
| 1073 |
of @link(TCustomSSL) class. When programmer not select some SSL plugin |
| 1074 |
class, then is used @link(TSSLNone)} |
| 1075 |
property SSL: TCustomSSL read FSSL; |
| 1076 |
|
| 1077 |
{:@True if is used HTTP tunnel mode.} |
| 1078 |
property HTTPTunnel: Boolean read FHTTPTunnel; |
| 1079 |
published |
| 1080 |
{:Return descriptive string for @link(LastError). On case of error |
| 1081 |
in SSL/TLS subsystem, it returns right error description.} |
| 1082 |
function GetErrorDescEx: string; override; |
| 1083 |
|
| 1084 |
{:Specify IP address of HTTP proxy. Assingning non-empty value to this |
| 1085 |
property enable HTTP-tunnel mode. This mode is for tunnelling any outgoing |
| 1086 |
TCP connection through HTTP proxy server. (If policy on HTTP proxy server |
| 1087 |
allow this!) Warning: You cannot combine this mode with SOCK5 mode!} |
| 1088 |
property HTTPTunnelIP: string read FHTTPTunnelIP Write FHTTPTunnelIP; |
| 1089 |
|
| 1090 |
{:Specify port of HTTP proxy for HTTP-tunneling.} |
| 1091 |
property HTTPTunnelPort: string read FHTTPTunnelPort Write FHTTPTunnelPort; |
| 1092 |
|
| 1093 |
{:Specify authorisation username for access to HTTP proxy in HTTP-tunnel |
| 1094 |
mode. If you not need authorisation, then let this property empty.} |
| 1095 |
property HTTPTunnelUser: string read FHTTPTunnelUser Write FHTTPTunnelUser; |
| 1096 |
|
| 1097 |
{:Specify authorisation password for access to HTTP proxy in HTTP-tunnel |
| 1098 |
mode.} |
| 1099 |
property HTTPTunnelPass: string read FHTTPTunnelPass Write FHTTPTunnelPass; |
| 1100 |
|
| 1101 |
{:Specify timeout for communication with HTTP proxy in HTTPtunnel mode.} |
| 1102 |
property HTTPTunnelTimeout: integer read FHTTPTunnelTimeout Write FHTTPTunnelTimeout; |
| 1103 |
|
| 1104 |
{:This event is called after sucessful TCP socket connection.} |
| 1105 |
property OnAfterConnect: THookAfterConnect read FOnAfterConnect write FOnAfterConnect; |
| 1106 |
end; |
| 1107 |
|
| 1108 |
{:@abstract(Datagram based communication) |
| 1109 |
This class implementing datagram based communication instead default stream |
| 1110 |
based communication style.} |
| 1111 |
TDgramBlockSocket = class(TSocksBlockSocket) |
| 1112 |
public |
| 1113 |
{:Fill @link(TBlockSocket.RemoteSin) structure. This address is used for |
| 1114 |
sending data.} |
| 1115 |
procedure Connect(IP, Port: string); override; |
| 1116 |
|
| 1117 |
{:Silently redirected to @link(TBlockSocket.SendBufferTo).} |
| 1118 |
function SendBuffer(const Buffer: TMemory; Length: Integer): Integer; override; |
| 1119 |
|
| 1120 |
{:Silently redirected to @link(TBlockSocket.RecvBufferFrom).} |
| 1121 |
function RecvBuffer(Buffer: TMemory; Length: Integer): Integer; override; |
| 1122 |
end; |
| 1123 |
|
| 1124 |
{:@abstract(Implementation of UDP socket.) |
| 1125 |
NOTE: in this class is all receiving redirected to RecvBufferFrom. You can |
| 1126 |
use for reading any receive function. Preffered is RecvPacket! Similary all |
| 1127 |
sending is redirected to SendbufferTo. You can use for sending UDP packet any |
| 1128 |
sending function, like SendString. |
| 1129 |
|
| 1130 |
Supported features: IPv4, IPv6, unicasts, broadcasts, multicasts, SOCKS5 |
| 1131 |
proxy (only unicasts! Outgoing and incomming.)} |
| 1132 |
TUDPBlockSocket = class(TDgramBlockSocket) |
| 1133 |
protected |
| 1134 |
FSocksControlSock: TTCPBlockSocket; |
| 1135 |
function UdpAssociation: Boolean; |
| 1136 |
procedure SetMulticastTTL(TTL: integer); |
| 1137 |
function GetMulticastTTL:integer; |
| 1138 |
public |
| 1139 |
destructor Destroy; override; |
| 1140 |
|
| 1141 |
{:Enable or disable sending of broadcasts. If seting OK, result is @true. |
| 1142 |
This method is not supported in SOCKS5 mode! IPv6 does not support |
| 1143 |
broadcasts! In this case you must use Multicasts instead.} |
| 1144 |
procedure EnableBroadcast(Value: Boolean); |
| 1145 |
|
| 1146 |
{:See @link(TBlockSocket.SendBufferTo)} |
| 1147 |
function SendBufferTo(const Buffer: TMemory; Length: Integer): Integer; override; |
| 1148 |
|
| 1149 |
{:See @link(TBlockSocket.RecvBufferFrom)} |
| 1150 |
function RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; override; |
| 1151 |
{$IFNDEF CIL} |
| 1152 |
{:Add this socket to given multicast group. You cannot use Multicasts in |
| 1153 |
SOCKS mode!} |
| 1154 |
procedure AddMulticast(MCastIP:string); |
| 1155 |
|
| 1156 |
{:Remove this socket from given multicast group.} |
| 1157 |
procedure DropMulticast(MCastIP:string); |
| 1158 |
{$ENDIF} |
| 1159 |
{:All sended multicast datagrams is loopbacked to your interface too. (you |
| 1160 |
can read your sended datas.) You can disable this feature by this function. |
| 1161 |
This function not working on some Windows systems!} |
| 1162 |
procedure EnableMulticastLoop(Value: Boolean); |
| 1163 |
|
| 1164 |
{:Return value of socket type. For UDP return SOCK_DGRAM.} |
| 1165 |
function GetSocketType: integer; override; |
| 1166 |
|
| 1167 |
{:Return value of protocol type for socket creation. For UDP return |
| 1168 |
IPPROTO_UDP.} |
| 1169 |
function GetSocketProtocol: integer; override; |
| 1170 |
|
| 1171 |
{:Set Time-to-live value for multicasts packets. It define number of routers |
| 1172 |
for transfer of datas. If you set this to 1 (dafault system value), then |
| 1173 |
multicasts packet goes only to you local network. If you need transport |
| 1174 |
multicast packet to worldwide, then increase this value, but be carefull, |
| 1175 |
lot of routers on internet does not transport multicasts packets!} |
| 1176 |
property MulticastTTL: Integer read GetMulticastTTL Write SetMulticastTTL; |
| 1177 |
end; |
| 1178 |
|
| 1179 |
{:@abstract(Implementation of RAW ICMP socket.) |
| 1180 |
For this object you must have rights for creating RAW sockets!} |
| 1181 |
TICMPBlockSocket = class(TDgramBlockSocket) |
| 1182 |
public |
| 1183 |
{:Return value of socket type. For RAW and ICMP return SOCK_RAW.} |
| 1184 |
function GetSocketType: integer; override; |
| 1185 |
|
| 1186 |
{:Return value of protocol type for socket creation. For ICMP returns |
| 1187 |
IPPROTO_ICMP or IPPROTO_ICMPV6} |
| 1188 |
function GetSocketProtocol: integer; override; |
| 1189 |
end; |
| 1190 |
|
| 1191 |
{:@abstract(Implementation of RAW socket.) |
| 1192 |
For this object you must have rights for creating RAW sockets!} |
| 1193 |
TRAWBlockSocket = class(TBlockSocket) |
| 1194 |
public |
| 1195 |
{:Return value of socket type. For RAW and ICMP return SOCK_RAW.} |
| 1196 |
function GetSocketType: integer; override; |
| 1197 |
|
| 1198 |
{:Return value of protocol type for socket creation. For RAW returns |
| 1199 |
IPPROTO_RAW.} |
| 1200 |
function GetSocketProtocol: integer; override; |
| 1201 |
end; |
| 1202 |
|
| 1203 |
{:@abstract(Implementation of PGM-message socket.) |
| 1204 |
Not all systems supports this protocol!} |
| 1205 |
TPGMMessageBlockSocket = class(TBlockSocket) |
| 1206 |
public |
| 1207 |
{:Return value of socket type. For PGM-message return SOCK_RDM.} |
| 1208 |
function GetSocketType: integer; override; |
| 1209 |
|
| 1210 |
{:Return value of protocol type for socket creation. For PGM-message returns |
| 1211 |
IPPROTO_RM.} |
| 1212 |
function GetSocketProtocol: integer; override; |
| 1213 |
end; |
| 1214 |
|
| 1215 |
{:@abstract(Implementation of PGM-stream socket.) |
| 1216 |
Not all systems supports this protocol!} |
| 1217 |
TPGMStreamBlockSocket = class(TBlockSocket) |
| 1218 |
public |
| 1219 |
{:Return value of socket type. For PGM-stream return SOCK_STREAM.} |
| 1220 |
function GetSocketType: integer; override; |
| 1221 |
|
| 1222 |
{:Return value of protocol type for socket creation. For PGM-stream returns |
| 1223 |
IPPROTO_RM.} |
| 1224 |
function GetSocketProtocol: integer; override; |
| 1225 |
end; |
| 1226 |
|
| 1227 |
{:@abstract(Parent class for all SSL plugins.) |
| 1228 |
This is abstract class defining interface for other SSL plugins. |
| 1229 |
|
| 1230 |
Instance of this class will be created for each @link(TTCPBlockSocket). |
| 1231 |
|
| 1232 |
Warning: not all methods and propertis can work in all existing SSL plugins! |
| 1233 |
Please, read documentation of used SSL plugin.} |
| 1234 |
TCustomSSL = class(TObject) |
| 1235 |
private |
| 1236 |
protected |
| 1237 |
FSessionOld: Pointer; |
| 1238 |
FSessionNew: Pointer; |
| 1239 |
FOnVerifyCert: THookVerifyCert; |
| 1240 |
FSocket: TTCPBlockSocket; |
| 1241 |
FSSLEnabled: Boolean; |
| 1242 |
FLastError: integer; |
| 1243 |
FLastErrorDesc: string; |
| 1244 |
FSSLType: TSSLType; |
| 1245 |
FKeyPassword: string; |
| 1246 |
FCiphers: string; |
| 1247 |
FCertificateFile: string; |
| 1248 |
FPrivateKeyFile: string; |
| 1249 |
FCertificate: Ansistring; |
| 1250 |
FPrivateKey: Ansistring; |
| 1251 |
FPFX: Ansistring; |
| 1252 |
FPFXfile: string; |
| 1253 |
FCertCA: Ansistring; |
| 1254 |
FCertCAFile: string; |
| 1255 |
FTrustCertificate: Ansistring; |
| 1256 |
FTrustCertificateFile: string; |
| 1257 |
FVerifyCert: Boolean; |
| 1258 |
FUsername: string; |
| 1259 |
FPassword: string; |
| 1260 |
FSSHChannelType: string; |
| 1261 |
FSSHChannelArg1: string; |
| 1262 |
FSSHChannelArg2: string; |
| 1263 |
FCertComplianceLevel: integer; |
| 1264 |
FSNIHost: string; |
| 1265 |
procedure ReturnError; |
| 1266 |
procedure SetCertCAFile(const Value: string); virtual; |
| 1267 |
function DoVerifyCert:boolean; |
| 1268 |
function CreateSelfSignedCert(Host: string): Boolean; virtual; |
| 1269 |
public |
| 1270 |
{: Create plugin class. it is called internally from @link(TTCPBlockSocket)} |
| 1271 |
constructor Create(const Value: TTCPBlockSocket); virtual; |
| 1272 |
|
| 1273 |
{: Assign settings (certificates and configuration) from another SSL plugin |
| 1274 |
class.} |
| 1275 |
procedure Assign(const Value: TCustomSSL); virtual; |
| 1276 |
|
| 1277 |
{: return description of used plugin. It usually return name and version |
| 1278 |
of used SSL library.} |
| 1279 |
function LibVersion: String; virtual; |
| 1280 |
|
| 1281 |
{: return name of used plugin.} |
| 1282 |
function LibName: String; virtual; |
| 1283 |
|
| 1284 |
{: Do not call this directly. It is used internally by @link(TTCPBlockSocket)! |
| 1285 |
|
| 1286 |
Here is needed code for start SSL connection.} |
| 1287 |
function Connect: boolean; virtual; |
| 1288 |
|
| 1289 |
{: Do not call this directly. It is used internally by @link(TTCPBlockSocket)! |
| 1290 |
|
| 1291 |
Here is needed code for acept new SSL connection.} |
| 1292 |
function Accept: boolean; virtual; |
| 1293 |
|
| 1294 |
{: Do not call this directly. It is used internally by @link(TTCPBlockSocket)! |
| 1295 |
|
| 1296 |
Here is needed code for hard shutdown of SSL connection. (for example, |
| 1297 |
before socket is closed)} |
| 1298 |
function Shutdown: boolean; virtual; |
| 1299 |
|
| 1300 |
{: Do not call this directly. It is used internally by @link(TTCPBlockSocket)! |
| 1301 |
|
| 1302 |
Here is needed code for soft shutdown of SSL connection. (for example, |
| 1303 |
when you need to continue with unprotected connection.)} |
| 1304 |
function BiShutdown: boolean; virtual; |
| 1305 |
|
| 1306 |
{: Do not call this directly. It is used internally by @link(TTCPBlockSocket)! |
| 1307 |
|
| 1308 |
Here is needed code for sending some datas by SSL connection.} |
| 1309 |
function SendBuffer(Buffer: TMemory; Len: Integer): Integer; virtual; |
| 1310 |
|
| 1311 |
{: Do not call this directly. It is used internally by @link(TTCPBlockSocket)! |
| 1312 |
|
| 1313 |
Here is needed code for receiving some datas by SSL connection.} |
| 1314 |
function RecvBuffer(Buffer: TMemory; Len: Integer): Integer; virtual; |
| 1315 |
|
| 1316 |
{: Do not call this directly. It is used internally by @link(TTCPBlockSocket)! |
| 1317 |
|
| 1318 |
Here is needed code for getting count of datas what waiting for read. |
| 1319 |
If SSL plugin not allows this, then it should return 0.} |
| 1320 |
function WaitingData: Integer; virtual; |
| 1321 |
|
| 1322 |
{:Return string with identificator of SSL/TLS version of existing |
| 1323 |
connection.} |
| 1324 |
function GetSSLVersion: string; virtual; |
| 1325 |
|
| 1326 |
{:Return subject of remote SSL peer.} |
| 1327 |
function GetPeerSubject: string; virtual; |
| 1328 |
|
| 1329 |
{:Return Serial number if remote X509 certificate.} |
| 1330 |
function GetPeerSerialNo: integer; virtual; |
| 1331 |
|
| 1332 |
{:Return issuer certificate of remote SSL peer.} |
| 1333 |
function GetPeerIssuer: string; virtual; |
| 1334 |
|
| 1335 |
{:Return peer name from remote side certificate. This is good for verify, |
| 1336 |
if certificate is generated for remote side IP name.} |
| 1337 |
function GetPeerName: string; virtual; |
| 1338 |
|
| 1339 |
{:Returns has of peer name from remote side certificate. This is good |
| 1340 |
for fast remote side authentication.} |
| 1341 |
function GetPeerNameHash: cardinal; virtual; |
| 1342 |
|
| 1343 |
{:Return fingerprint of remote SSL peer. (As binary nonprintable string!)} |
| 1344 |
function GetPeerFingerprint: AnsiString; virtual; |
| 1345 |
|
| 1346 |
{:Return all detailed information about certificate from remote side of |
| 1347 |
SSL/TLS connection. Result string can be multilined! Each plugin can return |
| 1348 |
this informations in different format!} |
| 1349 |
function GetCertInfo: string; virtual; |
| 1350 |
|
| 1351 |
{:Return currently used Cipher.} |
| 1352 |
function GetCipherName: string; virtual; |
| 1353 |
|
| 1354 |
{:Return currently used number of bits in current Cipher algorythm.} |
| 1355 |
function GetCipherBits: integer; virtual; |
| 1356 |
|
| 1357 |
{:Return number of bits in current Cipher algorythm.} |
| 1358 |
function GetCipherAlgBits: integer; virtual; |
| 1359 |
|
| 1360 |
{:Return result value of verify remote side certificate. Look to OpenSSL |
| 1361 |
documentation for possible values. For example 0 is successfuly verified |
| 1362 |
certificate, or 18 is self-signed certificate.} |
| 1363 |
function GetVerifyCert: integer; virtual; |
| 1364 |
|
| 1365 |
{: Resurn @true if SSL mode is enabled on existing cvonnection.} |
| 1366 |
property SSLEnabled: Boolean read FSSLEnabled; |
| 1367 |
|
| 1368 |
{:Return error code of last SSL operation. 0 is OK.} |
| 1369 |
property LastError: integer read FLastError; |
| 1370 |
|
| 1371 |
{:Return error description of last SSL operation.} |
| 1372 |
property LastErrorDesc: string read FLastErrorDesc; |
| 1373 |
|
| 1374 |
{:Used for session resumption } |
| 1375 |
property Session: Pointer read FSessionNew write FSessionOld; |
| 1376 |
published |
| 1377 |
{:Here you can specify requested SSL/TLS mode. Default is autodetection, but |
| 1378 |
on some servers autodetection not working properly. In this case you must |
| 1379 |
specify requested SSL/TLS mode by your hand!} |
| 1380 |
property SSLType: TSSLType read FSSLType write FSSLType; |
| 1381 |
|
| 1382 |
{:Password for decrypting of encoded certificate or key.} |
| 1383 |
property KeyPassword: string read FKeyPassword write FKeyPassword; |
| 1384 |
|
| 1385 |
{:Username for possible credentials.} |
| 1386 |
property Username: string read FUsername write FUsername; |
| 1387 |
|
| 1388 |
{:password for possible credentials.} |
| 1389 |
property Password: string read FPassword write FPassword; |
| 1390 |
|
| 1391 |
{:By this property you can modify default set of SSL/TLS ciphers.} |
| 1392 |
property Ciphers: string read FCiphers write FCiphers; |
| 1393 |
|
| 1394 |
{:Used for loading certificate from disk file. See to plugin documentation |
| 1395 |
if this method is supported and how!} |
| 1396 |
property CertificateFile: string read FCertificateFile write FCertificateFile; |
| 1397 |
|
| 1398 |
{:Used for loading private key from disk file. See to plugin documentation |
| 1399 |
if this method is supported and how!} |
| 1400 |
property PrivateKeyFile: string read FPrivateKeyFile write FPrivateKeyFile; |
| 1401 |
|
| 1402 |
{:Used for loading certificate from binary string. See to plugin documentation |
| 1403 |
if this method is supported and how!} |
| 1404 |
property Certificate: Ansistring read FCertificate write FCertificate; |
| 1405 |
|
| 1406 |
{:Used for loading private key from binary string. See to plugin documentation |
| 1407 |
if this method is supported and how!} |
| 1408 |
property PrivateKey: Ansistring read FPrivateKey write FPrivateKey; |
| 1409 |
|
| 1410 |
{:Used for loading PFX from binary string. See to plugin documentation |
| 1411 |
if this method is supported and how!} |
| 1412 |
property PFX: Ansistring read FPFX write FPFX; |
| 1413 |
|
| 1414 |
{:Used for loading PFX from disk file. See to plugin documentation |
| 1415 |
if this method is supported and how!} |
| 1416 |
property PFXfile: string read FPFXfile write FPFXfile; |
| 1417 |
|
| 1418 |
{:Used for loading trusted certificates from disk file. See to plugin documentation |
| 1419 |
if this method is supported and how!} |
| 1420 |
property TrustCertificateFile: string read FTrustCertificateFile write FTrustCertificateFile; |
| 1421 |
|
| 1422 |
{:Used for loading trusted certificates from binary string. See to plugin documentation |
| 1423 |
if this method is supported and how!} |
| 1424 |
property TrustCertificate: Ansistring read FTrustCertificate write FTrustCertificate; |
| 1425 |
|
| 1426 |
{:Used for loading CA certificates from binary string. See to plugin documentation |
| 1427 |
if this method is supported and how!} |
| 1428 |
property CertCA: Ansistring read FCertCA write FCertCA; |
| 1429 |
|
| 1430 |
{:Used for loading CA certificates from disk file. See to plugin documentation |
| 1431 |
if this method is supported and how!} |
| 1432 |
property CertCAFile: string read FCertCAFile write SetCertCAFile; |
| 1433 |
|
| 1434 |
{:If @true, then is verified client certificate. (it is good for writing |
| 1435 |
SSL/TLS servers.) When you are not server, but you are client, then if this |
| 1436 |
property is @true, verify servers certificate.} |
| 1437 |
property VerifyCert: Boolean read FVerifyCert write FVerifyCert; |
| 1438 |
|
| 1439 |
{:channel type for possible SSH connections} |
| 1440 |
property SSHChannelType: string read FSSHChannelType write FSSHChannelType; |
| 1441 |
|
| 1442 |
{:First argument of channel type for possible SSH connections} |
| 1443 |
property SSHChannelArg1: string read FSSHChannelArg1 write FSSHChannelArg1; |
| 1444 |
|
| 1445 |
{:Second argument of channel type for possible SSH connections} |
| 1446 |
property SSHChannelArg2: string read FSSHChannelArg2 write FSSHChannelArg2; |
| 1447 |
|
| 1448 |
{: Level of standards compliance level |
| 1449 |
(CryptLib: values in cryptlib.pas, -1: use default value ) } |
| 1450 |
property CertComplianceLevel:integer read FCertComplianceLevel write FCertComplianceLevel; |
| 1451 |
|
| 1452 |
{:This event is called when verifying the server certificate immediatally after |
| 1453 |
a successfull verification in the ssl library.} |
| 1454 |
property OnVerifyCert: THookVerifyCert read FOnVerifyCert write FOnVerifyCert; |
| 1455 |
|
| 1456 |
{: Server Name Identification. Host name to send to server. If empty the host name |
| 1457 |
found in URL will be used, which should be the normal use (http Header Host = SNI Host). |
| 1458 |
The value is cleared after the connection is established. |
| 1459 |
(SNI support requires OpenSSL 0.9.8k or later. Cryptlib not supported, yet ) } |
| 1460 |
property SNIHost:string read FSNIHost write FSNIHost; |
| 1461 |
end; |
| 1462 |
|
| 1463 |
{:@abstract(Default SSL plugin with no SSL support.) |
| 1464 |
Dummy SSL plugin implementation for applications without SSL/TLS support.} |
| 1465 |
TSSLNone = class (TCustomSSL) |
| 1466 |
public |
| 1467 |
{:See @inherited} |
| 1468 |
function LibVersion: String; override; |
| 1469 |
{:See @inherited} |
| 1470 |
function LibName: String; override; |
| 1471 |
end; |
| 1472 |
|
| 1473 |
{:@abstract(Record with definition of IP packet header.) |
| 1474 |
For reading data from ICMP or RAW sockets.} |
| 1475 |
TIPHeader = record |
| 1476 |
VerLen: Byte; |
| 1477 |
TOS: Byte; |
| 1478 |
TotalLen: Word; |
| 1479 |
Identifer: Word; |
| 1480 |
FragOffsets: Word; |
| 1481 |
TTL: Byte; |
| 1482 |
Protocol: Byte; |
| 1483 |
CheckSum: Word; |
| 1484 |
SourceIp: LongWord; |
| 1485 |
DestIp: LongWord; |
| 1486 |
Options: LongWord; |
| 1487 |
end; |
| 1488 |
|
| 1489 |
{:@abstract(Parent class of application protocol implementations.) |
| 1490 |
By this class is defined common properties.} |
| 1491 |
TSynaClient = Class(TObject) |
| 1492 |
protected |
| 1493 |
FTargetHost: string; |
| 1494 |
FTargetPort: string; |
| 1495 |
FIPInterface: string; |
| 1496 |
FTimeout: integer; |
| 1497 |
FUserName: string; |
| 1498 |
FPassword: string; |
| 1499 |
public |
| 1500 |
constructor Create; |
| 1501 |
published |
| 1502 |
{:Specify terget server IP (or symbolic name). Default is 'localhost'.} |
| 1503 |
property TargetHost: string read FTargetHost Write FTargetHost; |
| 1504 |
|
| 1505 |
{:Specify terget server port (or symbolic name).} |
| 1506 |
property TargetPort: string read FTargetPort Write FTargetPort; |
| 1507 |
|
| 1508 |
{:Defined local socket address. (outgoing IP address). By default is used |
| 1509 |
'0.0.0.0' as wildcard for default IP.} |
| 1510 |
property IPInterface: string read FIPInterface Write FIPInterface; |
| 1511 |
|
| 1512 |
{:Specify default timeout for socket operations.} |
| 1513 |
property Timeout: integer read FTimeout Write FTimeout; |
| 1514 |
|
| 1515 |
{:If protocol need user authorization, then fill here username.} |
| 1516 |
property UserName: string read FUserName Write FUserName; |
| 1517 |
|
| 1518 |
{:If protocol need user authorization, then fill here password.} |
| 1519 |
property Password: string read FPassword Write FPassword; |
| 1520 |
end; |
| 1521 |
|
| 1522 |
var |
| 1523 |
{:Selected SSL plugin. Default is @link(TSSLNone). |
| 1524 |
|
| 1525 |
Do not change this value directly!!! |
| 1526 |
|
| 1527 |
Just add your plugin unit to your project uses instead. Each plugin unit have |
| 1528 |
initialization code what modify this variable.} |
| 1529 |
SSLImplementation: TSSLClass = TSSLNone; |
| 1530 |
|
| 1531 |
implementation |
| 1532 |
|
| 1533 |
{$IFDEF ONCEWINSOCK} |
| 1534 |
var |
| 1535 |
WsaDataOnce: TWSADATA; |
| 1536 |
e: ESynapseError; |
| 1537 |
{$ENDIF} |
| 1538 |
|
| 1539 |
|
| 1540 |
constructor TBlockSocket.Create; |
| 1541 |
begin |
| 1542 |
CreateAlternate(''); |
| 1543 |
end; |
| 1544 |
|
| 1545 |
constructor TBlockSocket.CreateAlternate(Stub: string); |
| 1546 |
{$IFNDEF ONCEWINSOCK} |
| 1547 |
var |
| 1548 |
e: ESynapseError; |
| 1549 |
{$ENDIF} |
| 1550 |
begin |
| 1551 |
inherited Create; |
| 1552 |
FDelayedOptions := TOptionList.Create; |
| 1553 |
FRaiseExcept := False; |
| 1554 |
{$IFDEF RAISEEXCEPT} |
| 1555 |
FRaiseExcept := True; |
| 1556 |
{$ENDIF} |
| 1557 |
FSocket := INVALID_SOCKET; |
| 1558 |
FBuffer := ''; |
| 1559 |
FLastCR := False; |
| 1560 |
FLastLF := False; |
| 1561 |
FBinded := False; |
| 1562 |
FNonBlockMode := False; |
| 1563 |
FMaxLineLength := 0; |
| 1564 |
FMaxSendBandwidth := 0; |
| 1565 |
FNextSend := 0; |
| 1566 |
FMaxRecvBandwidth := 0; |
| 1567 |
FNextRecv := 0; |
| 1568 |
FConvertLineEnd := False; |
| 1569 |
FFamily := SF_Any; |
| 1570 |
FFamilySave := SF_Any; |
| 1571 |
FIP6used := False; |
| 1572 |
FPreferIP4 := True; |
| 1573 |
FInterPacketTimeout := True; |
| 1574 |
FRecvCounter := 0; |
| 1575 |
FSendCounter := 0; |
| 1576 |
FSendMaxChunk := c64k; |
| 1577 |
FStopFlag := False; |
| 1578 |
FNonblockSendTimeout := 15000; |
| 1579 |
FHeartbeatRate := 0; |
| 1580 |
FConnectionTimeout := 0; |
| 1581 |
FOwner := nil; |
| 1582 |
{$IFNDEF ONCEWINSOCK} |
| 1583 |
if Stub = '' then |
| 1584 |
Stub := DLLStackName; |
| 1585 |
if not InitSocketInterface(Stub) then |
| 1586 |
begin |
| 1587 |
e := ESynapseError.Create('Error loading Socket interface (' + Stub + ')!'); |
| 1588 |
e.ErrorCode := 0; |
| 1589 |
e.ErrorMessage := 'Error loading Socket interface (' + Stub + ')!'; |
| 1590 |
raise e; |
| 1591 |
end; |
| 1592 |
SockCheck(synsock.WSAStartup(WinsockLevel, FWsaDataOnce)); |
| 1593 |
ExceptCheck; |
| 1594 |
{$ENDIF} |
| 1595 |
end; |
| 1596 |
|
| 1597 |
destructor TBlockSocket.Destroy; |
| 1598 |
var |
| 1599 |
n: integer; |
| 1600 |
p: TSynaOption; |
| 1601 |
begin |
| 1602 |
CloseSocket; |
| 1603 |
{$IFNDEF ONCEWINSOCK} |
| 1604 |
synsock.WSACleanup; |
| 1605 |
DestroySocketInterface; |
| 1606 |
{$ENDIF} |
| 1607 |
for n := FDelayedOptions.Count - 1 downto 0 do |
| 1608 |
begin |
| 1609 |
p := TSynaOption(FDelayedOptions[n]); |
| 1610 |
p.Free; |
| 1611 |
end; |
| 1612 |
FDelayedOptions.Free; |
| 1613 |
inherited Destroy; |
| 1614 |
end; |
| 1615 |
|
| 1616 |
function TBlockSocket.FamilyToAF(f: TSocketFamily): TAddrFamily; |
| 1617 |
begin |
| 1618 |
case f of |
| 1619 |
SF_ip4: |
| 1620 |
Result := AF_INET; |
| 1621 |
SF_ip6: |
| 1622 |
Result := AF_INET6; |
| 1623 |
else |
| 1624 |
Result := AF_UNSPEC; |
| 1625 |
end; |
| 1626 |
end; |
| 1627 |
|
| 1628 |
procedure TBlockSocket.SetDelayedOption(const Value: TSynaOption); |
| 1629 |
var |
| 1630 |
li: TLinger; |
| 1631 |
x: integer; |
| 1632 |
buf: TMemory; |
| 1633 |
{$IFNDEF MSWINDOWS} |
| 1634 |
timeval: TTimeval; |
| 1635 |
{$ENDIF} |
| 1636 |
begin |
| 1637 |
case value.Option of |
| 1638 |
SOT_Linger: |
| 1639 |
begin |
| 1640 |
{$IFDEF CIL} |
| 1641 |
li := TLinger.Create(Value.Enabled, Value.Value div 1000); |
| 1642 |
synsock.SetSockOptObj(FSocket, integer(SOL_SOCKET), integer(SO_LINGER), li); |
| 1643 |
{$ELSE} |
| 1644 |
li.l_onoff := Ord(Value.Enabled); |
| 1645 |
li.l_linger := Value.Value div 1000; |
| 1646 |
buf := @li; |
| 1647 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_LINGER), buf, SizeOf(li)); |
| 1648 |
{$ENDIF} |
| 1649 |
end; |
| 1650 |
SOT_RecvBuff: |
| 1651 |
begin |
| 1652 |
{$IFDEF CIL} |
| 1653 |
buf := System.BitConverter.GetBytes(value.Value); |
| 1654 |
{$ELSE} |
| 1655 |
buf := @Value.Value; |
| 1656 |
{$ENDIF} |
| 1657 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVBUF), |
| 1658 |
buf, SizeOf(Value.Value)); |
| 1659 |
end; |
| 1660 |
SOT_SendBuff: |
| 1661 |
begin |
| 1662 |
{$IFDEF CIL} |
| 1663 |
buf := System.BitConverter.GetBytes(value.Value); |
| 1664 |
{$ELSE} |
| 1665 |
buf := @Value.Value; |
| 1666 |
{$ENDIF} |
| 1667 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDBUF), |
| 1668 |
buf, SizeOf(Value.Value)); |
| 1669 |
end; |
| 1670 |
SOT_NonBlock: |
| 1671 |
begin |
| 1672 |
FNonBlockMode := Value.Enabled; |
| 1673 |
x := Ord(FNonBlockMode); |
| 1674 |
synsock.IoctlSocket(FSocket, FIONBIO, x); |
| 1675 |
end; |
| 1676 |
SOT_RecvTimeout: |
| 1677 |
begin |
| 1678 |
{$IFDEF CIL} |
| 1679 |
buf := System.BitConverter.GetBytes(value.Value); |
| 1680 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVTIMEO), |
| 1681 |
buf, SizeOf(Value.Value)); |
| 1682 |
{$ELSE} |
| 1683 |
{$IFDEF MSWINDOWS} |
| 1684 |
buf := @Value.Value; |
| 1685 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVTIMEO), |
| 1686 |
buf, SizeOf(Value.Value)); |
| 1687 |
{$ELSE} |
| 1688 |
timeval.tv_sec:=Value.Value div 1000; |
| 1689 |
timeval.tv_usec:=(Value.Value mod 1000) * 1000; |
| 1690 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVTIMEO), |
| 1691 |
@timeval, SizeOf(timeval)); |
| 1692 |
{$ENDIF} |
| 1693 |
{$ENDIF} |
| 1694 |
end; |
| 1695 |
SOT_SendTimeout: |
| 1696 |
begin |
| 1697 |
{$IFDEF CIL} |
| 1698 |
buf := System.BitConverter.GetBytes(value.Value); |
| 1699 |
{$ELSE} |
| 1700 |
{$IFDEF MSWINDOWS} |
| 1701 |
buf := @Value.Value; |
| 1702 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDTIMEO), |
| 1703 |
buf, SizeOf(Value.Value)); |
| 1704 |
{$ELSE} |
| 1705 |
timeval.tv_sec:=Value.Value div 1000; |
| 1706 |
timeval.tv_usec:=(Value.Value mod 1000) * 1000; |
| 1707 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDTIMEO), |
| 1708 |
@timeval, SizeOf(timeval)); |
| 1709 |
{$ENDIF} |
| 1710 |
{$ENDIF} |
| 1711 |
end; |
| 1712 |
SOT_Reuse: |
| 1713 |
begin |
| 1714 |
x := Ord(Value.Enabled); |
| 1715 |
{$IFDEF CIL} |
| 1716 |
buf := System.BitConverter.GetBytes(x); |
| 1717 |
{$ELSE} |
| 1718 |
buf := @x; |
| 1719 |
{$ENDIF} |
| 1720 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_REUSEADDR), buf, SizeOf(x)); |
| 1721 |
end; |
| 1722 |
SOT_TTL: |
| 1723 |
begin |
| 1724 |
{$IFDEF CIL} |
| 1725 |
buf := System.BitConverter.GetBytes(value.Value); |
| 1726 |
{$ELSE} |
| 1727 |
buf := @Value.Value; |
| 1728 |
{$ENDIF} |
| 1729 |
if FIP6Used then |
| 1730 |
synsock.SetSockOpt(FSocket, integer(IPPROTO_IPV6), integer(IPV6_UNICAST_HOPS), |
| 1731 |
buf, SizeOf(Value.Value)) |
| 1732 |
else |
| 1733 |
synsock.SetSockOpt(FSocket, integer(IPPROTO_IP), integer(IP_TTL), |
| 1734 |
buf, SizeOf(Value.Value)); |
| 1735 |
end; |
| 1736 |
SOT_Broadcast: |
| 1737 |
begin |
| 1738 |
//#todo1 broadcasty na IP6 |
| 1739 |
x := Ord(Value.Enabled); |
| 1740 |
{$IFDEF CIL} |
| 1741 |
buf := System.BitConverter.GetBytes(x); |
| 1742 |
{$ELSE} |
| 1743 |
buf := @x; |
| 1744 |
{$ENDIF} |
| 1745 |
synsock.SetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_BROADCAST), buf, SizeOf(x)); |
| 1746 |
end; |
| 1747 |
SOT_MulticastTTL: |
| 1748 |
begin |
| 1749 |
{$IFDEF CIL} |
| 1750 |
buf := System.BitConverter.GetBytes(value.Value); |
| 1751 |
{$ELSE} |
| 1752 |
buf := @Value.Value; |
| 1753 |
{$ENDIF} |
| 1754 |
if FIP6Used then |
| 1755 |
synsock.SetSockOpt(FSocket, integer(IPPROTO_IPV6), integer(IPV6_MULTICAST_HOPS), |
| 1756 |
buf, SizeOf(Value.Value)) |
| 1757 |
else |
| 1758 |
synsock.SetSockOpt(FSocket, integer(IPPROTO_IP), integer(IP_MULTICAST_TTL), |
| 1759 |
buf, SizeOf(Value.Value)); |
| 1760 |
end; |
| 1761 |
SOT_MulticastLoop: |
| 1762 |
begin |
| 1763 |
x := Ord(Value.Enabled); |
| 1764 |
{$IFDEF CIL} |
| 1765 |
buf := System.BitConverter.GetBytes(x); |
| 1766 |
{$ELSE} |
| 1767 |
buf := @x; |
| 1768 |
{$ENDIF} |
| 1769 |
if FIP6Used then |
| 1770 |
synsock.SetSockOpt(FSocket, integer(IPPROTO_IPV6), integer(IPV6_MULTICAST_LOOP), buf, SizeOf(x)) |
| 1771 |
else |
| 1772 |
synsock.SetSockOpt(FSocket, integer(IPPROTO_IP), integer(IP_MULTICAST_LOOP), buf, SizeOf(x)); |
| 1773 |
end; |
| 1774 |
end; |
| 1775 |
Value.Free; |
| 1776 |
end; |
| 1777 |
|
| 1778 |
procedure TBlockSocket.DelayedOption(const Value: TSynaOption); |
| 1779 |
begin |
| 1780 |
if FSocket = INVALID_SOCKET then |
| 1781 |
begin |
| 1782 |
FDelayedOptions.Insert(0, Value); |
| 1783 |
end |
| 1784 |
else |
| 1785 |
SetDelayedOption(Value); |
| 1786 |
end; |
| 1787 |
|
| 1788 |
procedure TBlockSocket.ProcessDelayedOptions; |
| 1789 |
var |
| 1790 |
n: integer; |
| 1791 |
d: TSynaOption; |
| 1792 |
begin |
| 1793 |
for n := FDelayedOptions.Count - 1 downto 0 do |
| 1794 |
begin |
| 1795 |
d := TSynaOption(FDelayedOptions[n]); |
| 1796 |
SetDelayedOption(d); |
| 1797 |
end; |
| 1798 |
FDelayedOptions.Clear; |
| 1799 |
end; |
| 1800 |
|
| 1801 |
procedure TBlockSocket.SetSin(var Sin: TVarSin; IP, Port: string); |
| 1802 |
var |
| 1803 |
f: TSocketFamily; |
| 1804 |
begin |
| 1805 |
DoStatus(HR_ResolvingBegin, IP + ':' + Port); |
| 1806 |
ResetLastError; |
| 1807 |
//if socket exists, then use their type, else use users selection |
| 1808 |
f := SF_Any; |
| 1809 |
if (FSocket = INVALID_SOCKET) and (FFamily = SF_any) then |
| 1810 |
begin |
| 1811 |
if IsIP(IP) then |
| 1812 |
f := SF_IP4 |
| 1813 |
else |
| 1814 |
if IsIP6(IP) then |
| 1815 |
f := SF_IP6; |
| 1816 |
end |
| 1817 |
else |
| 1818 |
f := FFamily; |
| 1819 |
FLastError := synsock.SetVarSin(sin, ip, port, FamilyToAF(f), |
| 1820 |
GetSocketprotocol, GetSocketType, FPreferIP4); |
| 1821 |
DoStatus(HR_ResolvingEnd, GetSinIP(sin) + ':' + IntTostr(GetSinPort(sin))); |
| 1822 |
end; |
| 1823 |
|
| 1824 |
function TBlockSocket.GetSinIP(Sin: TVarSin): string; |
| 1825 |
begin |
| 1826 |
Result := synsock.GetSinIP(sin); |
| 1827 |
end; |
| 1828 |
|
| 1829 |
function TBlockSocket.GetSinPort(Sin: TVarSin): Integer; |
| 1830 |
begin |
| 1831 |
Result := synsock.GetSinPort(sin); |
| 1832 |
end; |
| 1833 |
|
| 1834 |
procedure TBlockSocket.CreateSocket; |
| 1835 |
var |
| 1836 |
sin: TVarSin; |
| 1837 |
begin |
| 1838 |
//dummy for SF_Any Family mode |
| 1839 |
ResetLastError; |
| 1840 |
if (FFamily <> SF_Any) and (FSocket = INVALID_SOCKET) then |
| 1841 |
begin |
| 1842 |
{$IFDEF CIL} |
| 1843 |
if FFamily = SF_IP6 then |
| 1844 |
sin := TVarSin.Create(IPAddress.Parse('::0'), 0) |
| 1845 |
else |
| 1846 |
sin := TVarSin.Create(IPAddress.Parse('0.0.0.0'), 0); |
| 1847 |
{$ELSE} |
| 1848 |
FillChar(Sin, Sizeof(Sin), 0); |
| 1849 |
if FFamily = SF_IP6 then |
| 1850 |
sin.sin_family := AF_INET6 |
| 1851 |
else |
| 1852 |
sin.sin_family := AF_INET; |
| 1853 |
{$ENDIF} |
| 1854 |
InternalCreateSocket(Sin); |
| 1855 |
end; |
| 1856 |
end; |
| 1857 |
|
| 1858 |
procedure TBlockSocket.CreateSocketByName(const Value: String); |
| 1859 |
var |
| 1860 |
sin: TVarSin; |
| 1861 |
begin |
| 1862 |
ResetLastError; |
| 1863 |
if FSocket = INVALID_SOCKET then |
| 1864 |
begin |
| 1865 |
SetSin(sin, value, '0'); |
| 1866 |
if FLastError = 0 then |
| 1867 |
InternalCreateSocket(Sin); |
| 1868 |
end; |
| 1869 |
end; |
| 1870 |
|
| 1871 |
procedure TBlockSocket.InternalCreateSocket(Sin: TVarSin); |
| 1872 |
begin |
| 1873 |
FStopFlag := False; |
| 1874 |
FRecvCounter := 0; |
| 1875 |
FSendCounter := 0; |
| 1876 |
ResetLastError; |
| 1877 |
if FSocket = INVALID_SOCKET then |
| 1878 |
begin |
| 1879 |
FBuffer := ''; |
| 1880 |
FBinded := False; |
| 1881 |
FIP6Used := Sin.AddressFamily = AF_INET6; |
| 1882 |
FSocket := synsock.Socket(integer(Sin.AddressFamily), GetSocketType, GetSocketProtocol); |
| 1883 |
if FSocket = INVALID_SOCKET then |
| 1884 |
FLastError := synsock.WSAGetLastError; |
| 1885 |
{$IFNDEF CIL} |
| 1886 |
FD_ZERO(FFDSet); |
| 1887 |
FD_SET(FSocket, FFDSet); |
| 1888 |
{$ENDIF} |
| 1889 |
ExceptCheck; |
| 1890 |
if FIP6used then |
| 1891 |
DoStatus(HR_SocketCreate, 'IPv6') |
| 1892 |
else |
| 1893 |
DoStatus(HR_SocketCreate, 'IPv4'); |
| 1894 |
ProcessDelayedOptions; |
| 1895 |
DoCreateSocket; |
| 1896 |
end; |
| 1897 |
end; |
| 1898 |
|
| 1899 |
procedure TBlockSocket.CloseSocket; |
| 1900 |
begin |
| 1901 |
AbortSocket; |
| 1902 |
end; |
| 1903 |
|
| 1904 |
procedure TBlockSocket.AbortSocket; |
| 1905 |
var |
| 1906 |
n: integer; |
| 1907 |
p: TSynaOption; |
| 1908 |
begin |
| 1909 |
if FSocket <> INVALID_SOCKET then |
| 1910 |
synsock.CloseSocket(FSocket); |
| 1911 |
FSocket := INVALID_SOCKET; |
| 1912 |
for n := FDelayedOptions.Count - 1 downto 0 do |
| 1913 |
begin |
| 1914 |
p := TSynaOption(FDelayedOptions[n]); |
| 1915 |
p.Free; |
| 1916 |
end; |
| 1917 |
FDelayedOptions.Clear; |
| 1918 |
FFamily := FFamilySave; |
| 1919 |
DoStatus(HR_SocketClose, ''); |
| 1920 |
end; |
| 1921 |
|
| 1922 |
procedure TBlockSocket.Bind(const IP, Port: string); |
| 1923 |
var |
| 1924 |
Sin: TVarSin; |
| 1925 |
begin |
| 1926 |
ResetLastError; |
| 1927 |
if (FSocket <> INVALID_SOCKET) |
| 1928 |
or not((FFamily = SF_ANY) and (IP = cAnyHost) and (Port = cAnyPort)) then |
| 1929 |
begin |
| 1930 |
SetSin(Sin, IP, Port); |
| 1931 |
if FLastError = 0 then |
| 1932 |
begin |
| 1933 |
if FSocket = INVALID_SOCKET then |
| 1934 |
InternalCreateSocket(Sin); |
| 1935 |
SockCheck(synsock.Bind(FSocket, Sin)); |
| 1936 |
GetSinLocal; |
| 1937 |
FBuffer := ''; |
| 1938 |
FBinded := True; |
| 1939 |
end; |
| 1940 |
ExceptCheck; |
| 1941 |
DoStatus(HR_Bind, IP + ':' + Port); |
| 1942 |
end; |
| 1943 |
end; |
| 1944 |
|
| 1945 |
procedure TBlockSocket.Connect(IP, Port: string); |
| 1946 |
var |
| 1947 |
Sin: TVarSin; |
| 1948 |
b: boolean; |
| 1949 |
begin |
| 1950 |
SetSin(Sin, IP, Port); |
| 1951 |
if FLastError = 0 then |
| 1952 |
begin |
| 1953 |
if FSocket = INVALID_SOCKET then |
| 1954 |
InternalCreateSocket(Sin); |
| 1955 |
if FConnectionTimeout > 0 then |
| 1956 |
begin |
| 1957 |
// connect in non-blocking mode |
| 1958 |
b := NonBlockMode; |
| 1959 |
NonBlockMode := true; |
| 1960 |
SockCheck(synsock.Connect(FSocket, Sin)); |
| 1961 |
if (FLastError = WSAEINPROGRESS) OR (FLastError = WSAEWOULDBLOCK) then |
| 1962 |
if not CanWrite(FConnectionTimeout) then |
| 1963 |
FLastError := WSAETIMEDOUT; |
| 1964 |
NonBlockMode := b; |
| 1965 |
end |
| 1966 |
else |
| 1967 |
SockCheck(synsock.Connect(FSocket, Sin)); |
| 1968 |
if FLastError = 0 then |
| 1969 |
GetSins; |
| 1970 |
FBuffer := ''; |
| 1971 |
FLastCR := False; |
| 1972 |
FLastLF := False; |
| 1973 |
end; |
| 1974 |
ExceptCheck; |
| 1975 |
DoStatus(HR_Connect, IP + ':' + Port); |
| 1976 |
end; |
| 1977 |
|
| 1978 |
procedure TBlockSocket.Listen; |
| 1979 |
begin |
| 1980 |
SockCheck(synsock.Listen(FSocket, SOMAXCONN)); |
| 1981 |
GetSins; |
| 1982 |
ExceptCheck; |
| 1983 |
DoStatus(HR_Listen, ''); |
| 1984 |
end; |
| 1985 |
|
| 1986 |
function TBlockSocket.Accept: TSocket; |
| 1987 |
begin |
| 1988 |
Result := synsock.Accept(FSocket, FRemoteSin); |
| 1989 |
/// SockCheck(Result); |
| 1990 |
ExceptCheck; |
| 1991 |
DoStatus(HR_Accept, ''); |
| 1992 |
end; |
| 1993 |
|
| 1994 |
procedure TBlockSocket.GetSinLocal; |
| 1995 |
begin |
| 1996 |
synsock.GetSockName(FSocket, FLocalSin); |
| 1997 |
end; |
| 1998 |
|
| 1999 |
procedure TBlockSocket.GetSinRemote; |
| 2000 |
begin |
| 2001 |
synsock.GetPeerName(FSocket, FRemoteSin); |
| 2002 |
end; |
| 2003 |
|
| 2004 |
procedure TBlockSocket.GetSins; |
| 2005 |
begin |
| 2006 |
GetSinLocal; |
| 2007 |
GetSinRemote; |
| 2008 |
end; |
| 2009 |
|
| 2010 |
procedure TBlockSocket.SetBandwidth(Value: Integer); |
| 2011 |
begin |
| 2012 |
MaxSendBandwidth := Value; |
| 2013 |
MaxRecvBandwidth := Value; |
| 2014 |
end; |
| 2015 |
|
| 2016 |
procedure TBlockSocket.LimitBandwidth(Length: Integer; MaxB: integer; var Next: LongWord); |
| 2017 |
var |
| 2018 |
x: LongWord; |
| 2019 |
y: LongWord; |
| 2020 |
n: integer; |
| 2021 |
begin |
| 2022 |
if FStopFlag then |
| 2023 |
exit; |
| 2024 |
if MaxB > 0 then |
| 2025 |
begin |
| 2026 |
y := GetTick; |
| 2027 |
if Next > y then |
| 2028 |
begin |
| 2029 |
x := Next - y; |
| 2030 |
if x > 0 then |
| 2031 |
begin |
| 2032 |
DoStatus(HR_Wait, IntToStr(x)); |
| 2033 |
sleep(x mod 250); |
| 2034 |
for n := 1 to x div 250 do |
| 2035 |
if FStopFlag then |
| 2036 |
Break |
| 2037 |
else |
| 2038 |
sleep(250); |
| 2039 |
end; |
| 2040 |
end; |
| 2041 |
Next := GetTick + LongWord(Trunc((Length / MaxB) * 1000)); |
| 2042 |
end; |
| 2043 |
end; |
| 2044 |
|
| 2045 |
function TBlockSocket.TestStopFlag: Boolean; |
| 2046 |
begin |
| 2047 |
DoHeartbeat; |
| 2048 |
Result := FStopFlag; |
| 2049 |
if Result then |
| 2050 |
begin |
| 2051 |
FStopFlag := False; |
| 2052 |
FLastError := WSAECONNABORTED; |
| 2053 |
ExceptCheck; |
| 2054 |
end; |
| 2055 |
end; |
| 2056 |
|
| 2057 |
|
| 2058 |
function TBlockSocket.SendBuffer(const Buffer: TMemory; Length: Integer): Integer; |
| 2059 |
{$IFNDEF CIL} |
| 2060 |
var |
| 2061 |
x, y: integer; |
| 2062 |
l, r: integer; |
| 2063 |
p: Pointer; |
| 2064 |
{$ENDIF} |
| 2065 |
begin |
| 2066 |
Result := 0; |
| 2067 |
if TestStopFlag then |
| 2068 |
Exit; |
| 2069 |
DoMonitor(True, Buffer, Length); |
| 2070 |
{$IFDEF CIL} |
| 2071 |
Result := synsock.Send(FSocket, Buffer, Length, 0); |
| 2072 |
{$ELSE} |
| 2073 |
l := Length; |
| 2074 |
x := 0; |
| 2075 |
while x < l do |
| 2076 |
begin |
| 2077 |
y := l - x; |
| 2078 |
if y > FSendMaxChunk then |
| 2079 |
y := FSendMaxChunk; |
| 2080 |
if y > 0 then |
| 2081 |
begin |
| 2082 |
LimitBandwidth(y, FMaxSendBandwidth, FNextsend); |
| 2083 |
p := IncPoint(Buffer, x); |
| 2084 |
r := synsock.Send(FSocket, p, y, MSG_NOSIGNAL); |
| 2085 |
SockCheck(r); |
| 2086 |
if FLastError = WSAEWOULDBLOCK then |
| 2087 |
begin |
| 2088 |
if CanWrite(FNonblockSendTimeout) then |
| 2089 |
begin |
| 2090 |
r := synsock.Send(FSocket, p, y, MSG_NOSIGNAL); |
| 2091 |
SockCheck(r); |
| 2092 |
end |
| 2093 |
else |
| 2094 |
FLastError := WSAETIMEDOUT; |
| 2095 |
end; |
| 2096 |
if FLastError <> 0 then |
| 2097 |
Break; |
| 2098 |
Inc(x, r); |
| 2099 |
Inc(Result, r); |
| 2100 |
Inc(FSendCounter, r); |
| 2101 |
DoStatus(HR_WriteCount, IntToStr(r)); |
| 2102 |
end |
| 2103 |
else |
| 2104 |
break; |
| 2105 |
end; |
| 2106 |
{$ENDIF} |
| 2107 |
ExceptCheck; |
| 2108 |
end; |
| 2109 |
|
| 2110 |
procedure TBlockSocket.SendByte(Data: Byte); |
| 2111 |
{$IFDEF CIL} |
| 2112 |
var |
| 2113 |
buf: TMemory; |
| 2114 |
{$ENDIF} |
| 2115 |
begin |
| 2116 |
{$IFDEF CIL} |
| 2117 |
setlength(buf, 1); |
| 2118 |
buf[0] := Data; |
| 2119 |
SendBuffer(buf, 1); |
| 2120 |
{$ELSE} |
| 2121 |
SendBuffer(@Data, 1); |
| 2122 |
{$ENDIF} |
| 2123 |
end; |
| 2124 |
|
| 2125 |
procedure TBlockSocket.SendString(Data: AnsiString); |
| 2126 |
var |
| 2127 |
buf: TMemory; |
| 2128 |
begin |
| 2129 |
{$IFDEF CIL} |
| 2130 |
buf := BytesOf(Data); |
| 2131 |
{$ELSE} |
| 2132 |
buf := Pointer(data); |
| 2133 |
{$ENDIF} |
| 2134 |
SendBuffer(buf, Length(Data)); |
| 2135 |
end; |
| 2136 |
|
| 2137 |
procedure TBlockSocket.SendInteger(Data: integer); |
| 2138 |
var |
| 2139 |
buf: TMemory; |
| 2140 |
begin |
| 2141 |
{$IFDEF CIL} |
| 2142 |
buf := System.BitConverter.GetBytes(Data); |
| 2143 |
{$ELSE} |
| 2144 |
buf := @Data; |
| 2145 |
{$ENDIF} |
| 2146 |
SendBuffer(buf, SizeOf(Data)); |
| 2147 |
end; |
| 2148 |
|
| 2149 |
procedure TBlockSocket.SendBlock(const Data: AnsiString); |
| 2150 |
var |
| 2151 |
i: integer; |
| 2152 |
begin |
| 2153 |
i := SwapBytes(Length(data)); |
| 2154 |
SendString(Codelongint(i) + Data); |
| 2155 |
end; |
| 2156 |
|
| 2157 |
procedure TBlockSocket.InternalSendStream(const Stream: TStream; WithSize, Indy: boolean); |
| 2158 |
var |
| 2159 |
l: integer; |
| 2160 |
yr: integer; |
| 2161 |
s: AnsiString; |
| 2162 |
b: boolean; |
| 2163 |
{$IFDEF CIL} |
| 2164 |
buf: TMemory; |
| 2165 |
{$ENDIF} |
| 2166 |
begin |
| 2167 |
b := true; |
| 2168 |
l := 0; |
| 2169 |
if WithSize then |
| 2170 |
begin |
| 2171 |
l := Stream.Size - Stream.Position;; |
| 2172 |
if not Indy then |
| 2173 |
l := synsock.HToNL(l); |
| 2174 |
end; |
| 2175 |
repeat |
| 2176 |
{$IFDEF CIL} |
| 2177 |
Setlength(buf, FSendMaxChunk); |
| 2178 |
yr := Stream.read(buf, FSendMaxChunk); |
| 2179 |
if yr > 0 then |
| 2180 |
begin |
| 2181 |
if WithSize and b then |
| 2182 |
begin |
| 2183 |
b := false; |
| 2184 |
SendString(CodeLongInt(l)); |
| 2185 |
end; |
| 2186 |
SendBuffer(buf, yr); |
| 2187 |
if FLastError <> 0 then |
| 2188 |
break; |
| 2189 |
end |
| 2190 |
{$ELSE} |
| 2191 |
Setlength(s, FSendMaxChunk); |
| 2192 |
yr := Stream.read(Pointer(s)^, FSendMaxChunk); |
| 2193 |
if yr > 0 then |
| 2194 |
begin |
| 2195 |
SetLength(s, yr); |
| 2196 |
if WithSize and b then |
| 2197 |
begin |
| 2198 |
b := false; |
| 2199 |
SendString(CodeLongInt(l) + s); |
| 2200 |
end |
| 2201 |
else |
| 2202 |
SendString(s); |
| 2203 |
if FLastError <> 0 then |
| 2204 |
break; |
| 2205 |
end |
| 2206 |
{$ENDIF} |
| 2207 |
until yr <= 0; |
| 2208 |
end; |
| 2209 |
|
| 2210 |
procedure TBlockSocket.SendStreamRaw(const Stream: TStream); |
| 2211 |
begin |
| 2212 |
InternalSendStream(Stream, false, false); |
| 2213 |
end; |
| 2214 |
|
| 2215 |
procedure TBlockSocket.SendStreamIndy(const Stream: TStream); |
| 2216 |
begin |
| 2217 |
InternalSendStream(Stream, true, true); |
| 2218 |
end; |
| 2219 |
|
| 2220 |
procedure TBlockSocket.SendStream(const Stream: TStream); |
| 2221 |
begin |
| 2222 |
InternalSendStream(Stream, true, false); |
| 2223 |
end; |
| 2224 |
|
| 2225 |
function TBlockSocket.RecvBuffer(Buffer: TMemory; Length: Integer): Integer; |
| 2226 |
begin |
| 2227 |
Result := 0; |
| 2228 |
if TestStopFlag then |
| 2229 |
Exit; |
| 2230 |
LimitBandwidth(Length, FMaxRecvBandwidth, FNextRecv); |
| 2231 |
// Result := synsock.Recv(FSocket, Buffer^, Length, MSG_NOSIGNAL); |
| 2232 |
Result := synsock.Recv(FSocket, Buffer, Length, MSG_NOSIGNAL); |
| 2233 |
if Result = 0 then |
| 2234 |
FLastError := WSAECONNRESET |
| 2235 |
else |
| 2236 |
SockCheck(Result); |
| 2237 |
ExceptCheck; |
| 2238 |
if Result > 0 then |
| 2239 |
begin |
| 2240 |
Inc(FRecvCounter, Result); |
| 2241 |
DoStatus(HR_ReadCount, IntToStr(Result)); |
| 2242 |
DoMonitor(False, Buffer, Result); |
| 2243 |
DoReadFilter(Buffer, Result); |
| 2244 |
end; |
| 2245 |
end; |
| 2246 |
|
| 2247 |
function TBlockSocket.RecvBufferEx(Buffer: TMemory; Len: Integer; |
| 2248 |
Timeout: Integer): Integer; |
| 2249 |
var |
| 2250 |
s: AnsiString; |
| 2251 |
rl, l: integer; |
| 2252 |
ti: LongWord; |
| 2253 |
{$IFDEF CIL} |
| 2254 |
n: integer; |
| 2255 |
b: TMemory; |
| 2256 |
{$ENDIF} |
| 2257 |
begin |
| 2258 |
ResetLastError; |
| 2259 |
Result := 0; |
| 2260 |
if Len > 0 then |
| 2261 |
begin |
| 2262 |
rl := 0; |
| 2263 |
repeat |
| 2264 |
ti := GetTick; |
| 2265 |
s := RecvPacket(Timeout); |
| 2266 |
l := Length(s); |
| 2267 |
if (rl + l) > Len then |
| 2268 |
l := Len - rl; |
| 2269 |
{$IFDEF CIL} |
| 2270 |
b := BytesOf(s); |
| 2271 |
for n := 0 to l do |
| 2272 |
Buffer[rl + n] := b[n]; |
| 2273 |
{$ELSE} |
| 2274 |
Move(Pointer(s)^, IncPoint(Buffer, rl)^, l); |
| 2275 |
{$ENDIF} |
| 2276 |
rl := rl + l; |
| 2277 |
if FLastError <> 0 then |
| 2278 |
Break; |
| 2279 |
if rl >= Len then |
| 2280 |
Break; |
| 2281 |
if not FInterPacketTimeout then |
| 2282 |
begin |
| 2283 |
Timeout := Timeout - integer(TickDelta(ti, GetTick)); |
| 2284 |
if Timeout <= 0 then |
| 2285 |
begin |
| 2286 |
FLastError := WSAETIMEDOUT; |
| 2287 |
Break; |
| 2288 |
end; |
| 2289 |
end; |
| 2290 |
until False; |
| 2291 |
delete(s, 1, l); |
| 2292 |
FBuffer := s; |
| 2293 |
Result := rl; |
| 2294 |
end; |
| 2295 |
end; |
| 2296 |
|
| 2297 |
function TBlockSocket.RecvBufferStr(Len: Integer; Timeout: Integer): AnsiString; |
| 2298 |
var |
| 2299 |
x: integer; |
| 2300 |
{$IFDEF CIL} |
| 2301 |
buf: Tmemory; |
| 2302 |
{$ENDIF} |
| 2303 |
begin |
| 2304 |
Result := ''; |
| 2305 |
if Len > 0 then |
| 2306 |
begin |
| 2307 |
{$IFDEF CIL} |
| 2308 |
Setlength(Buf, Len); |
| 2309 |
x := RecvBufferEx(buf, Len , Timeout); |
| 2310 |
if FLastError = 0 then |
| 2311 |
begin |
| 2312 |
SetLength(Buf, x); |
| 2313 |
Result := StringOf(buf); |
| 2314 |
end |
| 2315 |
else |
| 2316 |
Result := ''; |
| 2317 |
{$ELSE} |
| 2318 |
Setlength(Result, Len); |
| 2319 |
x := RecvBufferEx(Pointer(Result), Len , Timeout); |
| 2320 |
if FLastError = 0 then |
| 2321 |
SetLength(Result, x) |
| 2322 |
else |
| 2323 |
Result := ''; |
| 2324 |
{$ENDIF} |
| 2325 |
end; |
| 2326 |
end; |
| 2327 |
|
| 2328 |
function TBlockSocket.RecvPacket(Timeout: Integer): AnsiString; |
| 2329 |
var |
| 2330 |
x: integer; |
| 2331 |
{$IFDEF CIL} |
| 2332 |
buf: TMemory; |
| 2333 |
{$ENDIF} |
| 2334 |
begin |
| 2335 |
Result := ''; |
| 2336 |
ResetLastError; |
| 2337 |
if FBuffer <> '' then |
| 2338 |
begin |
| 2339 |
Result := FBuffer; |
| 2340 |
FBuffer := ''; |
| 2341 |
end |
| 2342 |
else |
| 2343 |
begin |
| 2344 |
{$IFDEF MSWINDOWS} |
| 2345 |
//not drain CPU on large downloads... |
| 2346 |
Sleep(0); |
| 2347 |
{$ENDIF} |
| 2348 |
x := WaitingData; |
| 2349 |
if x > 0 then |
| 2350 |
begin |
| 2351 |
{$IFDEF CIL} |
| 2352 |
SetLength(Buf, x); |
| 2353 |
x := RecvBuffer(Buf, x); |
| 2354 |
if x >= 0 then |
| 2355 |
begin |
| 2356 |
SetLength(Buf, x); |
| 2357 |
Result := StringOf(Buf); |
| 2358 |
end; |
| 2359 |
{$ELSE} |
| 2360 |
SetLength(Result, x); |
| 2361 |
x := RecvBuffer(Pointer(Result), x); |
| 2362 |
if x >= 0 then |
| 2363 |
SetLength(Result, x); |
| 2364 |
{$ENDIF} |
| 2365 |
end |
| 2366 |
else |
| 2367 |
begin |
| 2368 |
if CanRead(Timeout) then |
| 2369 |
begin |
| 2370 |
x := WaitingData; |
| 2371 |
if x = 0 then |
| 2372 |
FLastError := WSAECONNRESET; |
| 2373 |
if x > 0 then |
| 2374 |
begin |
| 2375 |
{$IFDEF CIL} |
| 2376 |
SetLength(Buf, x); |
| 2377 |
x := RecvBuffer(Buf, x); |
| 2378 |
if x >= 0 then |
| 2379 |
begin |
| 2380 |
SetLength(Buf, x); |
| 2381 |
result := StringOf(Buf); |
| 2382 |
end; |
| 2383 |
{$ELSE} |
| 2384 |
SetLength(Result, x); |
| 2385 |
x := RecvBuffer(Pointer(Result), x); |
| 2386 |
if x >= 0 then |
| 2387 |
SetLength(Result, x); |
| 2388 |
{$ENDIF} |
| 2389 |
end; |
| 2390 |
end |
| 2391 |
else |
| 2392 |
FLastError := WSAETIMEDOUT; |
| 2393 |
end; |
| 2394 |
end; |
| 2395 |
if FConvertLineEnd and (Result <> '') then |
| 2396 |
begin |
| 2397 |
if FLastCR and (Result[1] = LF) then |
| 2398 |
Delete(Result, 1, 1); |
| 2399 |
if FLastLF and (Result[1] = CR) then |
| 2400 |
Delete(Result, 1, 1); |
| 2401 |
FLastCR := False; |
| 2402 |
FLastLF := False; |
| 2403 |
end; |
| 2404 |
ExceptCheck; |
| 2405 |
end; |
| 2406 |
|
| 2407 |
|
| 2408 |
function TBlockSocket.RecvByte(Timeout: Integer): Byte; |
| 2409 |
begin |
| 2410 |
Result := 0; |
| 2411 |
ResetLastError; |
| 2412 |
if FBuffer = '' then |
| 2413 |
FBuffer := RecvPacket(Timeout); |
| 2414 |
if (FLastError = 0) and (FBuffer <> '') then |
| 2415 |
begin |
| 2416 |
Result := Ord(FBuffer[1]); |
| 2417 |
Delete(FBuffer, 1, 1); |
| 2418 |
end; |
| 2419 |
ExceptCheck; |
| 2420 |
end; |
| 2421 |
|
| 2422 |
function TBlockSocket.RecvInteger(Timeout: Integer): Integer; |
| 2423 |
var |
| 2424 |
s: AnsiString; |
| 2425 |
begin |
| 2426 |
Result := 0; |
| 2427 |
s := RecvBufferStr(4, Timeout); |
| 2428 |
if FLastError = 0 then |
| 2429 |
Result := (ord(s[1]) + ord(s[2]) * 256) + (ord(s[3]) + ord(s[4]) * 256) * 65536; |
| 2430 |
end; |
| 2431 |
|
| 2432 |
function TBlockSocket.RecvTerminated(Timeout: Integer; const Terminator: AnsiString): AnsiString; |
| 2433 |
var |
| 2434 |
x: Integer; |
| 2435 |
s: AnsiString; |
| 2436 |
l: Integer; |
| 2437 |
CorCRLF: Boolean; |
| 2438 |
t: AnsiString; |
| 2439 |
tl: integer; |
| 2440 |
ti: LongWord; |
| 2441 |
begin |
| 2442 |
ResetLastError; |
| 2443 |
Result := ''; |
| 2444 |
l := Length(Terminator); |
| 2445 |
if l = 0 then |
| 2446 |
Exit; |
| 2447 |
tl := l; |
| 2448 |
CorCRLF := FConvertLineEnd and (Terminator = CRLF); |
| 2449 |
s := ''; |
| 2450 |
x := 0; |
| 2451 |
repeat |
| 2452 |
//get rest of FBuffer or incomming new data... |
| 2453 |
ti := GetTick; |
| 2454 |
s := s + RecvPacket(Timeout); |
| 2455 |
if FLastError <> 0 then |
| 2456 |
Break; |
| 2457 |
x := 0; |
| 2458 |
if Length(s) > 0 then |
| 2459 |
if CorCRLF then |
| 2460 |
begin |
| 2461 |
t := ''; |
| 2462 |
x := PosCRLF(s, t); |
| 2463 |
tl := Length(t); |
| 2464 |
if t = CR then |
| 2465 |
FLastCR := True; |
| 2466 |
if t = LF then |
| 2467 |
FLastLF := True; |
| 2468 |
end |
| 2469 |
else |
| 2470 |
begin |
| 2471 |
x := pos(Terminator, s); |
| 2472 |
tl := l; |
| 2473 |
end; |
| 2474 |
if (FMaxLineLength <> 0) and (Length(s) > FMaxLineLength) then |
| 2475 |
begin |
| 2476 |
FLastError := WSAENOBUFS; |
| 2477 |
Break; |
| 2478 |
end; |
| 2479 |
if x > 0 then |
| 2480 |
Break; |
| 2481 |
if not FInterPacketTimeout then |
| 2482 |
begin |
| 2483 |
Timeout := Timeout - integer(TickDelta(ti, GetTick)); |
| 2484 |
if Timeout <= 0 then |
| 2485 |
begin |
| 2486 |
FLastError := WSAETIMEDOUT; |
| 2487 |
Break; |
| 2488 |
end; |
| 2489 |
end; |
| 2490 |
until False; |
| 2491 |
if x > 0 then |
| 2492 |
begin |
| 2493 |
Result := Copy(s, 1, x - 1); |
| 2494 |
Delete(s, 1, x + tl - 1); |
| 2495 |
end; |
| 2496 |
FBuffer := s; |
| 2497 |
ExceptCheck; |
| 2498 |
end; |
| 2499 |
|
| 2500 |
function TBlockSocket.RecvString(Timeout: Integer): AnsiString; |
| 2501 |
var |
| 2502 |
s: AnsiString; |
| 2503 |
begin |
| 2504 |
Result := ''; |
| 2505 |
s := RecvTerminated(Timeout, CRLF); |
| 2506 |
if FLastError = 0 then |
| 2507 |
Result := s; |
| 2508 |
end; |
| 2509 |
|
| 2510 |
function TBlockSocket.RecvBlock(Timeout: Integer): AnsiString; |
| 2511 |
var |
| 2512 |
x: integer; |
| 2513 |
begin |
| 2514 |
Result := ''; |
| 2515 |
x := RecvInteger(Timeout); |
| 2516 |
if FLastError = 0 then |
| 2517 |
Result := RecvBufferStr(x, Timeout); |
| 2518 |
end; |
| 2519 |
|
| 2520 |
procedure TBlockSocket.RecvStreamRaw(const Stream: TStream; Timeout: Integer); |
| 2521 |
var |
| 2522 |
s: AnsiString; |
| 2523 |
begin |
| 2524 |
repeat |
| 2525 |
s := RecvPacket(Timeout); |
| 2526 |
if (Length(s) = 0) then |
| 2527 |
Break; |
| 2528 |
if FLastError = 0 then |
| 2529 |
WriteStrToStream(Stream, s); |
| 2530 |
until FLastError <> 0; |
| 2531 |
end; |
| 2532 |
|
| 2533 |
procedure TBlockSocket.RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: int64); |
| 2534 |
var |
| 2535 |
s: AnsiString; |
| 2536 |
n: int64; |
| 2537 |
{$IFDEF CIL} |
| 2538 |
buf: TMemory; |
| 2539 |
{$ENDIF} |
| 2540 |
begin |
| 2541 |
n := Size div int64(FSendMaxChunk); |
| 2542 |
while n > 0 do |
| 2543 |
begin |
| 2544 |
{$IFDEF CIL} |
| 2545 |
SetLength(buf, FSendMaxChunk); |
| 2546 |
RecvBufferEx(buf, FSendMaxChunk, Timeout); |
| 2547 |
if FLastError <> 0 then |
| 2548 |
Exit; |
| 2549 |
Stream.Write(buf, FSendMaxChunk); |
| 2550 |
{$ELSE} |
| 2551 |
s := RecvBufferStr(FSendMaxChunk, Timeout); |
| 2552 |
if FLastError <> 0 then |
| 2553 |
Exit; |
| 2554 |
WriteStrToStream(Stream, s); |
| 2555 |
{$ENDIF} |
| 2556 |
dec(n); |
| 2557 |
end; |
| 2558 |
n := Size mod int64(FSendMaxChunk); |
| 2559 |
if n > 0 then |
| 2560 |
begin |
| 2561 |
{$IFDEF CIL} |
| 2562 |
SetLength(buf, n); |
| 2563 |
RecvBufferEx(buf, n, Timeout); |
| 2564 |
if FLastError <> 0 then |
| 2565 |
Exit; |
| 2566 |
Stream.Write(buf, n); |
| 2567 |
{$ELSE} |
| 2568 |
s := RecvBufferStr(n, Timeout); |
| 2569 |
if FLastError <> 0 then |
| 2570 |
Exit; |
| 2571 |
WriteStrToStream(Stream, s); |
| 2572 |
{$ENDIF} |
| 2573 |
end; |
| 2574 |
end; |
| 2575 |
|
| 2576 |
procedure TBlockSocket.RecvStreamIndy(const Stream: TStream; Timeout: Integer); |
| 2577 |
var |
| 2578 |
x: integer; |
| 2579 |
begin |
| 2580 |
x := RecvInteger(Timeout); |
| 2581 |
x := synsock.NToHL(x); |
| 2582 |
if FLastError = 0 then |
| 2583 |
RecvStreamSize(Stream, Timeout, x); |
| 2584 |
end; |
| 2585 |
|
| 2586 |
procedure TBlockSocket.RecvStream(const Stream: TStream; Timeout: Integer); |
| 2587 |
var |
| 2588 |
x: integer; |
| 2589 |
begin |
| 2590 |
x := RecvInteger(Timeout); |
| 2591 |
if FLastError = 0 then |
| 2592 |
RecvStreamSize(Stream, Timeout, x); |
| 2593 |
end; |
| 2594 |
|
| 2595 |
function TBlockSocket.PeekBuffer(Buffer: TMemory; Length: Integer): Integer; |
| 2596 |
begin |
| 2597 |
{$IFNDEF CIL} |
| 2598 |
// Result := synsock.Recv(FSocket, Buffer^, Length, MSG_PEEK + MSG_NOSIGNAL); |
| 2599 |
Result := synsock.Recv(FSocket, Buffer, Length, MSG_PEEK + MSG_NOSIGNAL); |
| 2600 |
SockCheck(Result); |
| 2601 |
ExceptCheck; |
| 2602 |
{$ENDIF} |
| 2603 |
end; |
| 2604 |
|
| 2605 |
function TBlockSocket.PeekByte(Timeout: Integer): Byte; |
| 2606 |
var |
| 2607 |
s: string; |
| 2608 |
begin |
| 2609 |
{$IFNDEF CIL} |
| 2610 |
Result := 0; |
| 2611 |
if CanRead(Timeout) then |
| 2612 |
begin |
| 2613 |
SetLength(s, 1); |
| 2614 |
PeekBuffer(Pointer(s), 1); |
| 2615 |
if s <> '' then |
| 2616 |
Result := Ord(s[1]); |
| 2617 |
end |
| 2618 |
else |
| 2619 |
FLastError := WSAETIMEDOUT; |
| 2620 |
ExceptCheck; |
| 2621 |
{$ENDIF} |
| 2622 |
end; |
| 2623 |
|
| 2624 |
procedure TBlockSocket.ResetLastError; |
| 2625 |
begin |
| 2626 |
FLastError := 0; |
| 2627 |
FLastErrorDesc := ''; |
| 2628 |
end; |
| 2629 |
|
| 2630 |
function TBlockSocket.SockCheck(SockResult: Integer): Integer; |
| 2631 |
begin |
| 2632 |
ResetLastError; |
| 2633 |
if SockResult = integer(SOCKET_ERROR) then |
| 2634 |
begin |
| 2635 |
FLastError := synsock.WSAGetLastError; |
| 2636 |
FLastErrorDesc := GetErrorDescEx; |
| 2637 |
end; |
| 2638 |
Result := FLastError; |
| 2639 |
end; |
| 2640 |
|
| 2641 |
procedure TBlockSocket.ExceptCheck; |
| 2642 |
var |
| 2643 |
e: ESynapseError; |
| 2644 |
begin |
| 2645 |
FLastErrorDesc := GetErrorDescEx; |
| 2646 |
if (LastError <> 0) and (LastError <> WSAEINPROGRESS) |
| 2647 |
and (LastError <> WSAEWOULDBLOCK) then |
| 2648 |
begin |
| 2649 |
DoStatus(HR_Error, IntToStr(FLastError) + ',' + FLastErrorDesc); |
| 2650 |
if FRaiseExcept then |
| 2651 |
begin |
| 2652 |
e := ESynapseError.Create(Format('Synapse TCP/IP Socket error %d: %s', |
| 2653 |
[FLastError, FLastErrorDesc])); |
| 2654 |
e.ErrorCode := FLastError; |
| 2655 |
e.ErrorMessage := FLastErrorDesc; |
| 2656 |
raise e; |
| 2657 |
end; |
| 2658 |
end; |
| 2659 |
end; |
| 2660 |
|
| 2661 |
function TBlockSocket.WaitingData: Integer; |
| 2662 |
var |
| 2663 |
x: Integer; |
| 2664 |
begin |
| 2665 |
Result := 0; |
| 2666 |
if synsock.IoctlSocket(FSocket, FIONREAD, x) = 0 then |
| 2667 |
Result := x; |
| 2668 |
if Result > c64k then |
| 2669 |
Result := c64k; |
| 2670 |
end; |
| 2671 |
|
| 2672 |
function TBlockSocket.WaitingDataEx: Integer; |
| 2673 |
begin |
| 2674 |
if FBuffer <> '' then |
| 2675 |
Result := Length(FBuffer) |
| 2676 |
else |
| 2677 |
Result := WaitingData; |
| 2678 |
end; |
| 2679 |
|
| 2680 |
procedure TBlockSocket.Purge; |
| 2681 |
begin |
| 2682 |
Sleep(1); |
| 2683 |
try |
| 2684 |
while (Length(FBuffer) > 0) or (WaitingData > 0) do |
| 2685 |
begin |
| 2686 |
RecvPacket(0); |
| 2687 |
if FLastError <> 0 then |
| 2688 |
break; |
| 2689 |
end; |
| 2690 |
except |
| 2691 |
on exception do; |
| 2692 |
end; |
| 2693 |
ResetLastError; |
| 2694 |
end; |
| 2695 |
|
| 2696 |
procedure TBlockSocket.SetLinger(Enable: Boolean; Linger: Integer); |
| 2697 |
var |
| 2698 |
d: TSynaOption; |
| 2699 |
begin |
| 2700 |
d := TSynaOption.Create; |
| 2701 |
d.Option := SOT_Linger; |
| 2702 |
d.Enabled := Enable; |
| 2703 |
d.Value := Linger; |
| 2704 |
DelayedOption(d); |
| 2705 |
end; |
| 2706 |
|
| 2707 |
function TBlockSocket.LocalName: string; |
| 2708 |
begin |
| 2709 |
Result := synsock.GetHostName; |
| 2710 |
if Result = '' then |
| 2711 |
Result := '127.0.0.1'; |
| 2712 |
end; |
| 2713 |
|
| 2714 |
procedure TBlockSocket.ResolveNameToIP(Name: string; const IPList: TStrings); |
| 2715 |
begin |
| 2716 |
IPList.Clear; |
| 2717 |
synsock.ResolveNameToIP(Name, FamilyToAF(FFamily), GetSocketprotocol, GetSocketType, IPList); |
| 2718 |
if IPList.Count = 0 then |
| 2719 |
IPList.Add(cAnyHost); |
| 2720 |
end; |
| 2721 |
|
| 2722 |
function TBlockSocket.ResolveName(Name: string): string; |
| 2723 |
var |
| 2724 |
l: TStringList; |
| 2725 |
begin |
| 2726 |
l := TStringList.Create; |
| 2727 |
try |
| 2728 |
ResolveNameToIP(Name, l); |
| 2729 |
Result := l[0]; |
| 2730 |
finally |
| 2731 |
l.Free; |
| 2732 |
end; |
| 2733 |
end; |
| 2734 |
|
| 2735 |
function TBlockSocket.ResolvePort(Port: string): Word; |
| 2736 |
begin |
| 2737 |
Result := synsock.ResolvePort(Port, FamilyToAF(FFamily), GetSocketProtocol, GetSocketType); |
| 2738 |
end; |
| 2739 |
|
| 2740 |
function TBlockSocket.ResolveIPToName(IP: string): string; |
| 2741 |
begin |
| 2742 |
if not IsIP(IP) and not IsIp6(IP) then |
| 2743 |
IP := ResolveName(IP); |
| 2744 |
Result := synsock.ResolveIPToName(IP, FamilyToAF(FFamily), GetSocketProtocol, GetSocketType); |
| 2745 |
end; |
| 2746 |
|
| 2747 |
procedure TBlockSocket.SetRemoteSin(IP, Port: string); |
| 2748 |
begin |
| 2749 |
SetSin(FRemoteSin, IP, Port); |
| 2750 |
end; |
| 2751 |
|
| 2752 |
function TBlockSocket.GetLocalSinIP: string; |
| 2753 |
begin |
| 2754 |
Result := GetSinIP(FLocalSin); |
| 2755 |
end; |
| 2756 |
|
| 2757 |
function TBlockSocket.GetRemoteSinIP: string; |
| 2758 |
begin |
| 2759 |
Result := GetSinIP(FRemoteSin); |
| 2760 |
end; |
| 2761 |
|
| 2762 |
function TBlockSocket.GetLocalSinPort: Integer; |
| 2763 |
begin |
| 2764 |
Result := GetSinPort(FLocalSin); |
| 2765 |
end; |
| 2766 |
|
| 2767 |
function TBlockSocket.GetRemoteSinPort: Integer; |
| 2768 |
begin |
| 2769 |
Result := GetSinPort(FRemoteSin); |
| 2770 |
end; |
| 2771 |
|
| 2772 |
function TBlockSocket.InternalCanRead(Timeout: Integer): Boolean; |
| 2773 |
{$IFDEF CIL} |
| 2774 |
begin |
| 2775 |
Result := FSocket.Poll(Timeout * 1000, SelectMode.SelectRead); |
| 2776 |
{$ELSE} |
| 2777 |
var |
| 2778 |
TimeVal: PTimeVal; |
| 2779 |
TimeV: TTimeVal; |
| 2780 |
x: Integer; |
| 2781 |
FDSet: TFDSet; |
| 2782 |
begin |
| 2783 |
TimeV.tv_usec := (Timeout mod 1000) * 1000; |
| 2784 |
TimeV.tv_sec := Timeout div 1000; |
| 2785 |
TimeVal := @TimeV; |
| 2786 |
if Timeout = -1 then |
| 2787 |
TimeVal := nil; |
| 2788 |
FDSet := FFdSet; |
| 2789 |
x := synsock.Select(FSocket + 1, @FDSet, nil, nil, TimeVal); |
| 2790 |
SockCheck(x); |
| 2791 |
if FLastError <> 0 then |
| 2792 |
x := 0; |
| 2793 |
Result := x > 0; |
| 2794 |
{$ENDIF} |
| 2795 |
end; |
| 2796 |
|
| 2797 |
function TBlockSocket.CanRead(Timeout: Integer): Boolean; |
| 2798 |
var |
| 2799 |
ti, tr: Integer; |
| 2800 |
n: integer; |
| 2801 |
begin |
| 2802 |
if (FHeartbeatRate <> 0) and (Timeout <> -1) then |
| 2803 |
begin |
| 2804 |
ti := Timeout div FHeartbeatRate; |
| 2805 |
tr := Timeout mod FHeartbeatRate; |
| 2806 |
end |
| 2807 |
else |
| 2808 |
begin |
| 2809 |
ti := 0; |
| 2810 |
tr := Timeout; |
| 2811 |
end; |
| 2812 |
Result := InternalCanRead(tr); |
| 2813 |
if not Result then |
| 2814 |
for n := 0 to ti do |
| 2815 |
begin |
| 2816 |
DoHeartbeat; |
| 2817 |
if FStopFlag then |
| 2818 |
begin |
| 2819 |
Result := False; |
| 2820 |
FStopFlag := False; |
| 2821 |
Break; |
| 2822 |
end; |
| 2823 |
Result := InternalCanRead(FHeartbeatRate); |
| 2824 |
if Result then |
| 2825 |
break; |
| 2826 |
end; |
| 2827 |
ExceptCheck; |
| 2828 |
if Result then |
| 2829 |
DoStatus(HR_CanRead, ''); |
| 2830 |
end; |
| 2831 |
|
| 2832 |
function TBlockSocket.InternalCanWrite(Timeout: Integer): Boolean; |
| 2833 |
{$IFDEF CIL} |
| 2834 |
begin |
| 2835 |
Result := FSocket.Poll(Timeout * 1000, SelectMode.SelectWrite); |
| 2836 |
{$ELSE} |
| 2837 |
var |
| 2838 |
TimeVal: PTimeVal; |
| 2839 |
TimeV: TTimeVal; |
| 2840 |
x: Integer; |
| 2841 |
FDSet: TFDSet; |
| 2842 |
begin |
| 2843 |
TimeV.tv_usec := (Timeout mod 1000) * 1000; |
| 2844 |
TimeV.tv_sec := Timeout div 1000; |
| 2845 |
TimeVal := @TimeV; |
| 2846 |
if Timeout = -1 then |
| 2847 |
TimeVal := nil; |
| 2848 |
FDSet := FFdSet; |
| 2849 |
x := synsock.Select(FSocket + 1, nil, @FDSet, nil, TimeVal); |
| 2850 |
SockCheck(x); |
| 2851 |
if FLastError <> 0 then |
| 2852 |
x := 0; |
| 2853 |
Result := x > 0; |
| 2854 |
{$ENDIF} |
| 2855 |
end; |
| 2856 |
|
| 2857 |
function TBlockSocket.CanWrite(Timeout: Integer): Boolean; |
| 2858 |
var |
| 2859 |
ti, tr: Integer; |
| 2860 |
n: integer; |
| 2861 |
begin |
| 2862 |
if (FHeartbeatRate <> 0) and (Timeout <> -1) then |
| 2863 |
begin |
| 2864 |
ti := Timeout div FHeartbeatRate; |
| 2865 |
tr := Timeout mod FHeartbeatRate; |
| 2866 |
end |
| 2867 |
else |
| 2868 |
begin |
| 2869 |
ti := 0; |
| 2870 |
tr := Timeout; |
| 2871 |
end; |
| 2872 |
Result := InternalCanWrite(tr); |
| 2873 |
if not Result then |
| 2874 |
for n := 0 to ti do |
| 2875 |
begin |
| 2876 |
DoHeartbeat; |
| 2877 |
if FStopFlag then |
| 2878 |
begin |
| 2879 |
Result := False; |
| 2880 |
FStopFlag := False; |
| 2881 |
Break; |
| 2882 |
end; |
| 2883 |
Result := InternalCanWrite(FHeartbeatRate); |
| 2884 |
if Result then |
| 2885 |
break; |
| 2886 |
end; |
| 2887 |
ExceptCheck; |
| 2888 |
if Result then |
| 2889 |
DoStatus(HR_CanWrite, ''); |
| 2890 |
end; |
| 2891 |
|
| 2892 |
function TBlockSocket.CanReadEx(Timeout: Integer): Boolean; |
| 2893 |
begin |
| 2894 |
if FBuffer <> '' then |
| 2895 |
Result := True |
| 2896 |
else |
| 2897 |
Result := CanRead(Timeout); |
| 2898 |
end; |
| 2899 |
|
| 2900 |
function TBlockSocket.SendBufferTo(const Buffer: TMemory; Length: Integer): Integer; |
| 2901 |
begin |
| 2902 |
Result := 0; |
| 2903 |
if TestStopFlag then |
| 2904 |
Exit; |
| 2905 |
DoMonitor(True, Buffer, Length); |
| 2906 |
LimitBandwidth(Length, FMaxSendBandwidth, FNextsend); |
| 2907 |
Result := synsock.SendTo(FSocket, Buffer, Length, MSG_NOSIGNAL, FRemoteSin); |
| 2908 |
SockCheck(Result); |
| 2909 |
ExceptCheck; |
| 2910 |
Inc(FSendCounter, Result); |
| 2911 |
DoStatus(HR_WriteCount, IntToStr(Result)); |
| 2912 |
end; |
| 2913 |
|
| 2914 |
function TBlockSocket.RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; |
| 2915 |
begin |
| 2916 |
Result := 0; |
| 2917 |
if TestStopFlag then |
| 2918 |
Exit; |
| 2919 |
LimitBandwidth(Length, FMaxRecvBandwidth, FNextRecv); |
| 2920 |
Result := synsock.RecvFrom(FSocket, Buffer, Length, MSG_NOSIGNAL, FRemoteSin); |
| 2921 |
SockCheck(Result); |
| 2922 |
ExceptCheck; |
| 2923 |
Inc(FRecvCounter, Result); |
| 2924 |
DoStatus(HR_ReadCount, IntToStr(Result)); |
| 2925 |
DoMonitor(False, Buffer, Result); |
| 2926 |
end; |
| 2927 |
|
| 2928 |
function TBlockSocket.GetSizeRecvBuffer: Integer; |
| 2929 |
var |
| 2930 |
l: Integer; |
| 2931 |
{$IFDEF CIL} |
| 2932 |
buf: TMemory; |
| 2933 |
{$ENDIF} |
| 2934 |
begin |
| 2935 |
{$IFDEF CIL} |
| 2936 |
setlength(buf, 4); |
| 2937 |
SockCheck(synsock.GetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_RCVBUF), buf, l)); |
| 2938 |
Result := System.BitConverter.ToInt32(buf,0); |
| 2939 |
{$ELSE} |
| 2940 |
l := SizeOf(Result); |
| 2941 |
SockCheck(synsock.GetSockOpt(FSocket, SOL_SOCKET, SO_RCVBUF, @Result, l)); |
| 2942 |
if FLastError <> 0 then |
| 2943 |
Result := 1024; |
| 2944 |
ExceptCheck; |
| 2945 |
{$ENDIF} |
| 2946 |
end; |
| 2947 |
|
| 2948 |
procedure TBlockSocket.SetSizeRecvBuffer(Size: Integer); |
| 2949 |
var |
| 2950 |
d: TSynaOption; |
| 2951 |
begin |
| 2952 |
d := TSynaOption.Create; |
| 2953 |
d.Option := SOT_RecvBuff; |
| 2954 |
d.Value := Size; |
| 2955 |
DelayedOption(d); |
| 2956 |
end; |
| 2957 |
|
| 2958 |
function TBlockSocket.GetSizeSendBuffer: Integer; |
| 2959 |
var |
| 2960 |
l: Integer; |
| 2961 |
{$IFDEF CIL} |
| 2962 |
buf: TMemory; |
| 2963 |
{$ENDIF} |
| 2964 |
begin |
| 2965 |
{$IFDEF CIL} |
| 2966 |
setlength(buf, 4); |
| 2967 |
SockCheck(synsock.GetSockOpt(FSocket, integer(SOL_SOCKET), integer(SO_SNDBUF), buf, l)); |
| 2968 |
Result := System.BitConverter.ToInt32(buf,0); |
| 2969 |
{$ELSE} |
| 2970 |
l := SizeOf(Result); |
| 2971 |
SockCheck(synsock.GetSockOpt(FSocket, SOL_SOCKET, SO_SNDBUF, @Result, l)); |
| 2972 |
if FLastError <> 0 then |
| 2973 |
Result := 1024; |
| 2974 |
ExceptCheck; |
| 2975 |
{$ENDIF} |
| 2976 |
end; |
| 2977 |
|
| 2978 |
procedure TBlockSocket.SetSizeSendBuffer(Size: Integer); |
| 2979 |
var |
| 2980 |
d: TSynaOption; |
| 2981 |
begin |
| 2982 |
d := TSynaOption.Create; |
| 2983 |
d.Option := SOT_SendBuff; |
| 2984 |
d.Value := Size; |
| 2985 |
DelayedOption(d); |
| 2986 |
end; |
| 2987 |
|
| 2988 |
procedure TBlockSocket.SetNonBlockMode(Value: Boolean); |
| 2989 |
var |
| 2990 |
d: TSynaOption; |
| 2991 |
begin |
| 2992 |
d := TSynaOption.Create; |
| 2993 |
d.Option := SOT_nonblock; |
| 2994 |
d.Enabled := Value; |
| 2995 |
DelayedOption(d); |
| 2996 |
end; |
| 2997 |
|
| 2998 |
procedure TBlockSocket.SetTimeout(Timeout: Integer); |
| 2999 |
begin |
| 3000 |
SetSendTimeout(Timeout); |
| 3001 |
SetRecvTimeout(Timeout); |
| 3002 |
end; |
| 3003 |
|
| 3004 |
procedure TBlockSocket.SetSendTimeout(Timeout: Integer); |
| 3005 |
var |
| 3006 |
d: TSynaOption; |
| 3007 |
begin |
| 3008 |
d := TSynaOption.Create; |
| 3009 |
d.Option := SOT_sendtimeout; |
| 3010 |
d.Value := Timeout; |
| 3011 |
DelayedOption(d); |
| 3012 |
end; |
| 3013 |
|
| 3014 |
procedure TBlockSocket.SetRecvTimeout(Timeout: Integer); |
| 3015 |
var |
| 3016 |
d: TSynaOption; |
| 3017 |
begin |
| 3018 |
d := TSynaOption.Create; |
| 3019 |
d.Option := SOT_recvtimeout; |
| 3020 |
d.Value := Timeout; |
| 3021 |
DelayedOption(d); |
| 3022 |
end; |
| 3023 |
|
| 3024 |
{$IFNDEF CIL} |
| 3025 |
function TBlockSocket.GroupCanRead(const SocketList: TSocketList; Timeout: Integer; |
| 3026 |
const CanReadList: TSocketList): boolean; |
| 3027 |
var |
| 3028 |
FDSet: TFDSet; |
| 3029 |
TimeVal: PTimeVal; |
| 3030 |
TimeV: TTimeVal; |
| 3031 |
x, n: Integer; |
| 3032 |
Max: Integer; |
| 3033 |
begin |
| 3034 |
TimeV.tv_usec := (Timeout mod 1000) * 1000; |
| 3035 |
TimeV.tv_sec := Timeout div 1000; |
| 3036 |
TimeVal := @TimeV; |
| 3037 |
if Timeout = -1 then |
| 3038 |
TimeVal := nil; |
| 3039 |
FD_ZERO(FDSet); |
| 3040 |
Max := 0; |
| 3041 |
for n := 0 to SocketList.Count - 1 do |
| 3042 |
if TObject(SocketList.Items[n]) is TBlockSocket then |
| 3043 |
begin |
| 3044 |
if TBlockSocket(SocketList.Items[n]).Socket > Max then |
| 3045 |
Max := TBlockSocket(SocketList.Items[n]).Socket; |
| 3046 |
FD_SET(TBlockSocket(SocketList.Items[n]).Socket, FDSet); |
| 3047 |
end; |
| 3048 |
x := synsock.Select(Max + 1, @FDSet, nil, nil, TimeVal); |
| 3049 |
SockCheck(x); |
| 3050 |
ExceptCheck; |
| 3051 |
if FLastError <> 0 then |
| 3052 |
x := 0; |
| 3053 |
Result := x > 0; |
| 3054 |
CanReadList.Clear; |
| 3055 |
if Result then |
| 3056 |
for n := 0 to SocketList.Count - 1 do |
| 3057 |
if TObject(SocketList.Items[n]) is TBlockSocket then |
| 3058 |
if FD_ISSET(TBlockSocket(SocketList.Items[n]).Socket, FDSet) then |
| 3059 |
CanReadList.Add(TBlockSocket(SocketList.Items[n])); |
| 3060 |
end; |
| 3061 |
{$ENDIF} |
| 3062 |
|
| 3063 |
procedure TBlockSocket.EnableReuse(Value: Boolean); |
| 3064 |
var |
| 3065 |
d: TSynaOption; |
| 3066 |
begin |
| 3067 |
d := TSynaOption.Create; |
| 3068 |
d.Option := SOT_reuse; |
| 3069 |
d.Enabled := Value; |
| 3070 |
DelayedOption(d); |
| 3071 |
end; |
| 3072 |
|
| 3073 |
procedure TBlockSocket.SetTTL(TTL: integer); |
| 3074 |
var |
| 3075 |
d: TSynaOption; |
| 3076 |
begin |
| 3077 |
d := TSynaOption.Create; |
| 3078 |
d.Option := SOT_TTL; |
| 3079 |
d.Value := TTL; |
| 3080 |
DelayedOption(d); |
| 3081 |
end; |
| 3082 |
|
| 3083 |
function TBlockSocket.GetTTL:integer; |
| 3084 |
var |
| 3085 |
l: Integer; |
| 3086 |
begin |
| 3087 |
{$IFNDEF CIL} |
| 3088 |
l := SizeOf(Result); |
| 3089 |
if FIP6Used then |
| 3090 |
synsock.GetSockOpt(FSocket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, @Result, l) |
| 3091 |
else |
| 3092 |
synsock.GetSockOpt(FSocket, IPPROTO_IP, IP_TTL, @Result, l); |
| 3093 |
{$ENDIF} |
| 3094 |
end; |
| 3095 |
|
| 3096 |
procedure TBlockSocket.SetFamily(Value: TSocketFamily); |
| 3097 |
begin |
| 3098 |
FFamily := Value; |
| 3099 |
FFamilySave := Value; |
| 3100 |
end; |
| 3101 |
|
| 3102 |
procedure TBlockSocket.SetSocket(Value: TSocket); |
| 3103 |
begin |
| 3104 |
FRecvCounter := 0; |
| 3105 |
FSendCounter := 0; |
| 3106 |
FSocket := Value; |
| 3107 |
{$IFNDEF CIL} |
| 3108 |
FD_ZERO(FFDSet); |
| 3109 |
FD_SET(FSocket, FFDSet); |
| 3110 |
{$ENDIF} |
| 3111 |
GetSins; |
| 3112 |
FIP6Used := FRemoteSin.AddressFamily = AF_INET6; |
| 3113 |
end; |
| 3114 |
|
| 3115 |
function TBlockSocket.GetWsaData: TWSAData; |
| 3116 |
begin |
| 3117 |
{$IFDEF ONCEWINSOCK} |
| 3118 |
Result := WsaDataOnce; |
| 3119 |
{$ELSE} |
| 3120 |
Result := FWsaDataOnce; |
| 3121 |
{$ENDIF} |
| 3122 |
end; |
| 3123 |
|
| 3124 |
function TBlockSocket.GetSocketType: integer; |
| 3125 |
begin |
| 3126 |
Result := 0; |
| 3127 |
end; |
| 3128 |
|
| 3129 |
function TBlockSocket.GetSocketProtocol: integer; |
| 3130 |
begin |
| 3131 |
Result := integer(IPPROTO_IP); |
| 3132 |
end; |
| 3133 |
|
| 3134 |
procedure TBlockSocket.DoStatus(Reason: THookSocketReason; const Value: string); |
| 3135 |
begin |
| 3136 |
if assigned(OnStatus) then |
| 3137 |
OnStatus(Self, Reason, Value); |
| 3138 |
end; |
| 3139 |
|
| 3140 |
procedure TBlockSocket.DoReadFilter(Buffer: TMemory; var Len: Integer); |
| 3141 |
var |
| 3142 |
s: AnsiString; |
| 3143 |
begin |
| 3144 |
if assigned(OnReadFilter) then |
| 3145 |
if Len > 0 then |
| 3146 |
begin |
| 3147 |
{$IFDEF CIL} |
| 3148 |
s := StringOf(Buffer); |
| 3149 |
{$ELSE} |
| 3150 |
SetLength(s, Len); |
| 3151 |
Move(Buffer^, Pointer(s)^, Len); |
| 3152 |
{$ENDIF} |
| 3153 |
OnReadFilter(Self, s); |
| 3154 |
if Length(s) > Len then |
| 3155 |
SetLength(s, Len); |
| 3156 |
Len := Length(s); |
| 3157 |
{$IFDEF CIL} |
| 3158 |
Buffer := BytesOf(s); |
| 3159 |
{$ELSE} |
| 3160 |
Move(Pointer(s)^, Buffer^, Len); |
| 3161 |
{$ENDIF} |
| 3162 |
end; |
| 3163 |
end; |
| 3164 |
|
| 3165 |
procedure TBlockSocket.DoCreateSocket; |
| 3166 |
begin |
| 3167 |
if assigned(OnCreateSocket) then |
| 3168 |
OnCreateSocket(Self); |
| 3169 |
end; |
| 3170 |
|
| 3171 |
procedure TBlockSocket.DoMonitor(Writing: Boolean; const Buffer: TMemory; Len: Integer); |
| 3172 |
begin |
| 3173 |
if assigned(OnMonitor) then |
| 3174 |
begin |
| 3175 |
OnMonitor(Self, Writing, Buffer, Len); |
| 3176 |
end; |
| 3177 |
end; |
| 3178 |
|
| 3179 |
procedure TBlockSocket.DoHeartbeat; |
| 3180 |
begin |
| 3181 |
if assigned(OnHeartbeat) and (FHeartbeatRate <> 0) then |
| 3182 |
begin |
| 3183 |
OnHeartbeat(Self); |
| 3184 |
end; |
| 3185 |
end; |
| 3186 |
|
| 3187 |
function TBlockSocket.GetErrorDescEx: string; |
| 3188 |
begin |
| 3189 |
Result := GetErrorDesc(FLastError); |
| 3190 |
end; |
| 3191 |
|
| 3192 |
class function TBlockSocket.GetErrorDesc(ErrorCode: Integer): string; |
| 3193 |
begin |
| 3194 |
{$IFDEF CIL} |
| 3195 |
if ErrorCode = 0 then |
| 3196 |
Result := '' |
| 3197 |
else |
| 3198 |
begin |
| 3199 |
Result := WSAGetLastErrorDesc; |
| 3200 |
if Result = '' then |
| 3201 |
Result := 'Other Winsock error (' + IntToStr(ErrorCode) + ')'; |
| 3202 |
end; |
| 3203 |
{$ELSE} |
| 3204 |
case ErrorCode of |
| 3205 |
0: |
| 3206 |
Result := ''; |
| 3207 |
WSAEINTR: {10004} |
| 3208 |
Result := 'Interrupted system call'; |
| 3209 |
WSAEBADF: {10009} |
| 3210 |
Result := 'Bad file number'; |
| 3211 |
WSAEACCES: {10013} |
| 3212 |
Result := 'Permission denied'; |
| 3213 |
WSAEFAULT: {10014} |
| 3214 |
Result := 'Bad address'; |
| 3215 |
WSAEINVAL: {10022} |
| 3216 |
Result := 'Invalid argument'; |
| 3217 |
WSAEMFILE: {10024} |
| 3218 |
Result := 'Too many open files'; |
| 3219 |
WSAEWOULDBLOCK: {10035} |
| 3220 |
Result := 'Operation would block'; |
| 3221 |
WSAEINPROGRESS: {10036} |
| 3222 |
Result := 'Operation now in progress'; |
| 3223 |
WSAEALREADY: {10037} |
| 3224 |
Result := 'Operation already in progress'; |
| 3225 |
WSAENOTSOCK: {10038} |
| 3226 |
Result := 'Socket operation on nonsocket'; |
| 3227 |
WSAEDESTADDRREQ: {10039} |
| 3228 |
Result := 'Destination address required'; |
| 3229 |
WSAEMSGSIZE: {10040} |
| 3230 |
Result := 'Message too long'; |
| 3231 |
WSAEPROTOTYPE: {10041} |
| 3232 |
Result := 'Protocol wrong type for Socket'; |
| 3233 |
WSAENOPROTOOPT: {10042} |
| 3234 |
Result := 'Protocol not available'; |
| 3235 |
WSAEPROTONOSUPPORT: {10043} |
| 3236 |
Result := 'Protocol not supported'; |
| 3237 |
WSAESOCKTNOSUPPORT: {10044} |
| 3238 |
Result := 'Socket not supported'; |
| 3239 |
WSAEOPNOTSUPP: {10045} |
| 3240 |
Result := 'Operation not supported on Socket'; |
| 3241 |
WSAEPFNOSUPPORT: {10046} |
| 3242 |
Result := 'Protocol family not supported'; |
| 3243 |
WSAEAFNOSUPPORT: {10047} |
| 3244 |
Result := 'Address family not supported'; |
| 3245 |
WSAEADDRINUSE: {10048} |
| 3246 |
Result := 'Address already in use'; |
| 3247 |
WSAEADDRNOTAVAIL: {10049} |
| 3248 |
Result := 'Can''t assign requested address'; |
| 3249 |
WSAENETDOWN: {10050} |
| 3250 |
Result := 'Network is down'; |
| 3251 |
WSAENETUNREACH: {10051} |
| 3252 |
Result := 'Network is unreachable'; |
| 3253 |
WSAENETRESET: {10052} |
| 3254 |
Result := 'Network dropped connection on reset'; |
| 3255 |
WSAECONNABORTED: {10053} |
| 3256 |
Result := 'Software caused connection abort'; |
| 3257 |
WSAECONNRESET: {10054} |
| 3258 |
Result := 'Connection reset by peer'; |
| 3259 |
WSAENOBUFS: {10055} |
| 3260 |
Result := 'No Buffer space available'; |
| 3261 |
WSAEISCONN: {10056} |
| 3262 |
Result := 'Socket is already connected'; |
| 3263 |
WSAENOTCONN: {10057} |
| 3264 |
Result := 'Socket is not connected'; |
| 3265 |
WSAESHUTDOWN: {10058} |
| 3266 |
Result := 'Can''t send after Socket shutdown'; |
| 3267 |
WSAETOOMANYREFS: {10059} |
| 3268 |
Result := 'Too many references:can''t splice'; |
| 3269 |
WSAETIMEDOUT: {10060} |
| 3270 |
Result := 'Connection timed out'; |
| 3271 |
WSAECONNREFUSED: {10061} |
| 3272 |
Result := 'Connection refused'; |
| 3273 |
WSAELOOP: {10062} |
| 3274 |
Result := 'Too many levels of symbolic links'; |
| 3275 |
WSAENAMETOOLONG: {10063} |
| 3276 |
Result := 'File name is too long'; |
| 3277 |
WSAEHOSTDOWN: {10064} |
| 3278 |
Result := 'Host is down'; |
| 3279 |
WSAEHOSTUNREACH: {10065} |
| 3280 |
Result := 'No route to host'; |
| 3281 |
WSAENOTEMPTY: {10066} |
| 3282 |
Result := 'Directory is not empty'; |
| 3283 |
WSAEPROCLIM: {10067} |
| 3284 |
Result := 'Too many processes'; |
| 3285 |
WSAEUSERS: {10068} |
| 3286 |
Result := 'Too many users'; |
| 3287 |
WSAEDQUOT: {10069} |
| 3288 |
Result := 'Disk quota exceeded'; |
| 3289 |
WSAESTALE: {10070} |
| 3290 |
Result := 'Stale NFS file handle'; |
| 3291 |
WSAEREMOTE: {10071} |
| 3292 |
Result := 'Too many levels of remote in path'; |
| 3293 |
WSASYSNOTREADY: {10091} |
| 3294 |
Result := 'Network subsystem is unusable'; |
| 3295 |
WSAVERNOTSUPPORTED: {10092} |
| 3296 |
Result := 'Winsock DLL cannot support this application'; |
| 3297 |
WSANOTINITIALISED: {10093} |
| 3298 |
Result := 'Winsock not initialized'; |
| 3299 |
WSAEDISCON: {10101} |
| 3300 |
Result := 'Disconnect'; |
| 3301 |
WSAHOST_NOT_FOUND: {11001} |
| 3302 |
Result := 'Host not found'; |
| 3303 |
WSATRY_AGAIN: {11002} |
| 3304 |
Result := 'Non authoritative - host not found'; |
| 3305 |
WSANO_RECOVERY: {11003} |
| 3306 |
Result := 'Non recoverable error'; |
| 3307 |
WSANO_DATA: {11004} |
| 3308 |
Result := 'Valid name, no data record of requested type' |
| 3309 |
else |
| 3310 |
Result := 'Other Winsock error (' + IntToStr(ErrorCode) + ')'; |
| 3311 |
end; |
| 3312 |
{$ENDIF} |
| 3313 |
end; |
| 3314 |
|
| 3315 |
{======================================================================} |
| 3316 |
|
| 3317 |
constructor TSocksBlockSocket.Create; |
| 3318 |
begin |
| 3319 |
inherited Create; |
| 3320 |
FSocksIP:= ''; |
| 3321 |
FSocksPort:= '1080'; |
| 3322 |
FSocksTimeout:= 60000; |
| 3323 |
FSocksUsername:= ''; |
| 3324 |
FSocksPassword:= ''; |
| 3325 |
FUsingSocks := False; |
| 3326 |
FSocksResolver := True; |
| 3327 |
FSocksLastError := 0; |
| 3328 |
FSocksResponseIP := ''; |
| 3329 |
FSocksResponsePort := ''; |
| 3330 |
FSocksLocalIP := ''; |
| 3331 |
FSocksLocalPort := ''; |
| 3332 |
FSocksRemoteIP := ''; |
| 3333 |
FSocksRemotePort := ''; |
| 3334 |
FBypassFlag := False; |
| 3335 |
FSocksType := ST_Socks5; |
| 3336 |
end; |
| 3337 |
|
| 3338 |
function TSocksBlockSocket.SocksOpen: boolean; |
| 3339 |
var |
| 3340 |
Buf: AnsiString; |
| 3341 |
n: integer; |
| 3342 |
begin |
| 3343 |
Result := False; |
| 3344 |
FUsingSocks := False; |
| 3345 |
if FSocksType <> ST_Socks5 then |
| 3346 |
begin |
| 3347 |
FUsingSocks := True; |
| 3348 |
Result := True; |
| 3349 |
end |
| 3350 |
else |
| 3351 |
begin |
| 3352 |
FBypassFlag := True; |
| 3353 |
try |
| 3354 |
if FSocksUsername = '' then |
| 3355 |
Buf := #5 + #1 + #0 |
| 3356 |
else |
| 3357 |
Buf := #5 + #2 + #2 +#0; |
| 3358 |
SendString(Buf); |
| 3359 |
Buf := RecvBufferStr(2, FSocksTimeout); |
| 3360 |
if Length(Buf) < 2 then |
| 3361 |
Exit; |
| 3362 |
if Buf[1] <> #5 then |
| 3363 |
Exit; |
| 3364 |
n := Ord(Buf[2]); |
| 3365 |
case n of |
| 3366 |
0: //not need authorisation |
| 3367 |
; |
| 3368 |
2: |
| 3369 |
begin |
| 3370 |
Buf := #1 + AnsiChar(Length(FSocksUsername)) + FSocksUsername |
| 3371 |
+ AnsiChar(Length(FSocksPassword)) + FSocksPassword; |
| 3372 |
SendString(Buf); |
| 3373 |
Buf := RecvBufferStr(2, FSocksTimeout); |
| 3374 |
if Length(Buf) < 2 then |
| 3375 |
Exit; |
| 3376 |
if Buf[2] <> #0 then |
| 3377 |
Exit; |
| 3378 |
end; |
| 3379 |
else |
| 3380 |
//other authorisation is not supported! |
| 3381 |
Exit; |
| 3382 |
end; |
| 3383 |
FUsingSocks := True; |
| 3384 |
Result := True; |
| 3385 |
finally |
| 3386 |
FBypassFlag := False; |
| 3387 |
end; |
| 3388 |
end; |
| 3389 |
end; |
| 3390 |
|
| 3391 |
function TSocksBlockSocket.SocksRequest(Cmd: Byte; |
| 3392 |
const IP, Port: string): Boolean; |
| 3393 |
var |
| 3394 |
Buf: AnsiString; |
| 3395 |
begin |
| 3396 |
FBypassFlag := True; |
| 3397 |
try |
| 3398 |
if FSocksType <> ST_Socks5 then |
| 3399 |
Buf := #4 + AnsiChar(Cmd) + SocksCode(IP, Port) |
| 3400 |
else |
| 3401 |
Buf := #5 + AnsiChar(Cmd) + #0 + SocksCode(IP, Port); |
| 3402 |
SendString(Buf); |
| 3403 |
Result := FLastError = 0; |
| 3404 |
finally |
| 3405 |
FBypassFlag := False; |
| 3406 |
end; |
| 3407 |
end; |
| 3408 |
|
| 3409 |
function TSocksBlockSocket.SocksResponse: Boolean; |
| 3410 |
var |
| 3411 |
Buf, s: AnsiString; |
| 3412 |
x: integer; |
| 3413 |
begin |
| 3414 |
Result := False; |
| 3415 |
FBypassFlag := True; |
| 3416 |
try |
| 3417 |
FSocksResponseIP := ''; |
| 3418 |
FSocksResponsePort := ''; |
| 3419 |
FSocksLastError := -1; |
| 3420 |
if FSocksType <> ST_Socks5 then |
| 3421 |
begin |
| 3422 |
Buf := RecvBufferStr(8, FSocksTimeout); |
| 3423 |
if FLastError <> 0 then |
| 3424 |
Exit; |
| 3425 |
if Buf[1] <> #0 then |
| 3426 |
Exit; |
| 3427 |
FSocksLastError := Ord(Buf[2]); |
| 3428 |
end |
| 3429 |
else |
| 3430 |
begin |
| 3431 |
Buf := RecvBufferStr(4, FSocksTimeout); |
| 3432 |
if FLastError <> 0 then |
| 3433 |
Exit; |
| 3434 |
if Buf[1] <> #5 then |
| 3435 |
Exit; |
| 3436 |
case Ord(Buf[4]) of |
| 3437 |
1: |
| 3438 |
s := RecvBufferStr(4, FSocksTimeout); |
| 3439 |
3: |
| 3440 |
begin |
| 3441 |
x := RecvByte(FSocksTimeout); |
| 3442 |
if FLastError <> 0 then |
| 3443 |
Exit; |
| 3444 |
s := AnsiChar(x) + RecvBufferStr(x, FSocksTimeout); |
| 3445 |
end; |
| 3446 |
4: |
| 3447 |
s := RecvBufferStr(16, FSocksTimeout); |
| 3448 |
else |
| 3449 |
Exit; |
| 3450 |
end; |
| 3451 |
Buf := Buf + s + RecvBufferStr(2, FSocksTimeout); |
| 3452 |
if FLastError <> 0 then |
| 3453 |
Exit; |
| 3454 |
FSocksLastError := Ord(Buf[2]); |
| 3455 |
end; |
| 3456 |
if ((FSocksLastError <> 0) and (FSocksLastError <> 90)) then |
| 3457 |
Exit; |
| 3458 |
SocksDecode(Buf); |
| 3459 |
Result := True; |
| 3460 |
finally |
| 3461 |
FBypassFlag := False; |
| 3462 |
end; |
| 3463 |
end; |
| 3464 |
|
| 3465 |
function TSocksBlockSocket.SocksCode(IP, Port: string): Ansistring; |
| 3466 |
var |
| 3467 |
ip6: TIp6Bytes; |
| 3468 |
n: integer; |
| 3469 |
begin |
| 3470 |
if FSocksType <> ST_Socks5 then |
| 3471 |
begin |
| 3472 |
Result := CodeInt(ResolvePort(Port)); |
| 3473 |
if not FSocksResolver then |
| 3474 |
IP := ResolveName(IP); |
| 3475 |
if IsIP(IP) then |
| 3476 |
begin |
| 3477 |
Result := Result + IPToID(IP); |
| 3478 |
Result := Result + FSocksUsername + #0; |
| 3479 |
end |
| 3480 |
else |
| 3481 |
begin |
| 3482 |
Result := Result + IPToID('0.0.0.1'); |
| 3483 |
Result := Result + FSocksUsername + #0; |
| 3484 |
Result := Result + IP + #0; |
| 3485 |
end; |
| 3486 |
end |
| 3487 |
else |
| 3488 |
begin |
| 3489 |
if not FSocksResolver then |
| 3490 |
IP := ResolveName(IP); |
| 3491 |
if IsIP(IP) then |
| 3492 |
Result := #1 + IPToID(IP) |
| 3493 |
else |
| 3494 |
if IsIP6(IP) then |
| 3495 |
begin |
| 3496 |
ip6 := StrToIP6(IP); |
| 3497 |
Result := #4; |
| 3498 |
for n := 0 to 15 do |
| 3499 |
Result := Result + AnsiChar(ip6[n]); |
| 3500 |
end |
| 3501 |
else |
| 3502 |
Result := #3 + AnsiChar(Length(IP)) + IP; |
| 3503 |
Result := Result + CodeInt(ResolvePort(Port)); |
| 3504 |
end; |
| 3505 |
end; |
| 3506 |
|
| 3507 |
function TSocksBlockSocket.SocksDecode(Value: Ansistring): integer; |
| 3508 |
var |
| 3509 |
Atyp: Byte; |
| 3510 |
y, n: integer; |
| 3511 |
w: Word; |
| 3512 |
ip6: TIp6Bytes; |
| 3513 |
begin |
| 3514 |
FSocksResponsePort := '0'; |
| 3515 |
Result := 0; |
| 3516 |
if FSocksType <> ST_Socks5 then |
| 3517 |
begin |
| 3518 |
if Length(Value) < 8 then |
| 3519 |
Exit; |
| 3520 |
Result := 3; |
| 3521 |
w := DecodeInt(Value, Result); |
| 3522 |
FSocksResponsePort := IntToStr(w); |
| 3523 |
FSocksResponseIP := Format('%d.%d.%d.%d', |
| 3524 |
[Ord(Value[5]), Ord(Value[6]), Ord(Value[7]), Ord(Value[8])]); |
| 3525 |
Result := 9; |
| 3526 |
end |
| 3527 |
else |
| 3528 |
begin |
| 3529 |
if Length(Value) < 4 then |
| 3530 |
Exit; |
| 3531 |
Atyp := Ord(Value[4]); |
| 3532 |
Result := 5; |
| 3533 |
case Atyp of |
| 3534 |
1: |
| 3535 |
begin |
| 3536 |
if Length(Value) < 10 then |
| 3537 |
Exit; |
| 3538 |
FSocksResponseIP := Format('%d.%d.%d.%d', |
| 3539 |
[Ord(Value[5]), Ord(Value[6]), Ord(Value[7]), Ord(Value[8])]); |
| 3540 |
Result := 9; |
| 3541 |
end; |
| 3542 |
3: |
| 3543 |
begin |
| 3544 |
y := Ord(Value[5]); |
| 3545 |
if Length(Value) < (5 + y + 2) then |
| 3546 |
Exit; |
| 3547 |
for n := 6 to 6 + y - 1 do |
| 3548 |
FSocksResponseIP := FSocksResponseIP + Value[n]; |
| 3549 |
Result := 5 + y + 1; |
| 3550 |
end; |
| 3551 |
4: |
| 3552 |
begin |
| 3553 |
if Length(Value) < 22 then |
| 3554 |
Exit; |
| 3555 |
for n := 0 to 15 do |
| 3556 |
ip6[n] := ord(Value[n + 5]); |
| 3557 |
FSocksResponseIP := IP6ToStr(ip6); |
| 3558 |
Result := 21; |
| 3559 |
end; |
| 3560 |
else |
| 3561 |
Exit; |
| 3562 |
end; |
| 3563 |
w := DecodeInt(Value, Result); |
| 3564 |
FSocksResponsePort := IntToStr(w); |
| 3565 |
Result := Result + 2; |
| 3566 |
end; |
| 3567 |
end; |
| 3568 |
|
| 3569 |
{======================================================================} |
| 3570 |
|
| 3571 |
procedure TDgramBlockSocket.Connect(IP, Port: string); |
| 3572 |
begin |
| 3573 |
SetRemoteSin(IP, Port); |
| 3574 |
InternalCreateSocket(FRemoteSin); |
| 3575 |
FBuffer := ''; |
| 3576 |
DoStatus(HR_Connect, IP + ':' + Port); |
| 3577 |
end; |
| 3578 |
|
| 3579 |
function TDgramBlockSocket.RecvBuffer(Buffer: TMemory; Length: Integer): Integer; |
| 3580 |
begin |
| 3581 |
Result := RecvBufferFrom(Buffer, Length); |
| 3582 |
end; |
| 3583 |
|
| 3584 |
function TDgramBlockSocket.SendBuffer(const Buffer: TMemory; Length: Integer): Integer; |
| 3585 |
begin |
| 3586 |
Result := SendBufferTo(Buffer, Length); |
| 3587 |
end; |
| 3588 |
|
| 3589 |
{======================================================================} |
| 3590 |
|
| 3591 |
destructor TUDPBlockSocket.Destroy; |
| 3592 |
begin |
| 3593 |
if Assigned(FSocksControlSock) then |
| 3594 |
FSocksControlSock.Free; |
| 3595 |
inherited; |
| 3596 |
end; |
| 3597 |
|
| 3598 |
procedure TUDPBlockSocket.EnableBroadcast(Value: Boolean); |
| 3599 |
var |
| 3600 |
d: TSynaOption; |
| 3601 |
begin |
| 3602 |
d := TSynaOption.Create; |
| 3603 |
d.Option := SOT_Broadcast; |
| 3604 |
d.Enabled := Value; |
| 3605 |
DelayedOption(d); |
| 3606 |
end; |
| 3607 |
|
| 3608 |
function TUDPBlockSocket.UdpAssociation: Boolean; |
| 3609 |
var |
| 3610 |
b: Boolean; |
| 3611 |
begin |
| 3612 |
Result := True; |
| 3613 |
FUsingSocks := False; |
| 3614 |
if FSocksIP <> '' then |
| 3615 |
begin |
| 3616 |
Result := False; |
| 3617 |
if not Assigned(FSocksControlSock) then |
| 3618 |
FSocksControlSock := TTCPBlockSocket.Create; |
| 3619 |
FSocksControlSock.CloseSocket; |
| 3620 |
FSocksControlSock.CreateSocketByName(FSocksIP); |
| 3621 |
FSocksControlSock.Connect(FSocksIP, FSocksPort); |
| 3622 |
if FSocksControlSock.LastError <> 0 then |
| 3623 |
Exit; |
| 3624 |
// if not assigned local port, assign it! |
| 3625 |
if not FBinded then |
| 3626 |
Bind(cAnyHost, cAnyPort); |
| 3627 |
//open control TCP connection to SOCKS |
| 3628 |
FSocksControlSock.FSocksUsername := FSocksUsername; |
| 3629 |
FSocksControlSock.FSocksPassword := FSocksPassword; |
| 3630 |
b := FSocksControlSock.SocksOpen; |
| 3631 |
if b then |
| 3632 |
b := FSocksControlSock.SocksRequest(3, GetLocalSinIP, IntToStr(GetLocalSinPort)); |
| 3633 |
if b then |
| 3634 |
b := FSocksControlSock.SocksResponse; |
| 3635 |
if not b and (FLastError = 0) then |
| 3636 |
FLastError := WSANO_RECOVERY; |
| 3637 |
FUsingSocks :=FSocksControlSock.UsingSocks; |
| 3638 |
FSocksRemoteIP := FSocksControlSock.FSocksResponseIP; |
| 3639 |
FSocksRemotePort := FSocksControlSock.FSocksResponsePort; |
| 3640 |
Result := b and (FLastError = 0); |
| 3641 |
end; |
| 3642 |
end; |
| 3643 |
|
| 3644 |
function TUDPBlockSocket.SendBufferTo(const Buffer: TMemory; Length: Integer): Integer; |
| 3645 |
var |
| 3646 |
SIp: string; |
| 3647 |
SPort: integer; |
| 3648 |
Buf: Ansistring; |
| 3649 |
begin |
| 3650 |
Result := 0; |
| 3651 |
FUsingSocks := False; |
| 3652 |
if (FSocksIP <> '') and (not UdpAssociation) then |
| 3653 |
FLastError := WSANO_RECOVERY |
| 3654 |
else |
| 3655 |
begin |
| 3656 |
if FUsingSocks then |
| 3657 |
begin |
| 3658 |
{$IFNDEF CIL} |
| 3659 |
Sip := GetRemoteSinIp; |
| 3660 |
SPort := GetRemoteSinPort; |
| 3661 |
SetRemoteSin(FSocksRemoteIP, FSocksRemotePort); |
| 3662 |
SetLength(Buf,Length); |
| 3663 |
Move(Buffer^, Pointer(Buf)^, Length); |
| 3664 |
Buf := #0 + #0 + #0 + SocksCode(Sip, IntToStr(SPort)) + Buf; |
| 3665 |
Result := inherited SendBufferTo(Pointer(Buf), System.Length(buf)); |
| 3666 |
SetRemoteSin(Sip, IntToStr(SPort)); |
| 3667 |
{$ENDIF} |
| 3668 |
end |
| 3669 |
else |
| 3670 |
Result := inherited SendBufferTo(Buffer, Length); |
| 3671 |
end; |
| 3672 |
end; |
| 3673 |
|
| 3674 |
function TUDPBlockSocket.RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; |
| 3675 |
var |
| 3676 |
Buf: Ansistring; |
| 3677 |
x: integer; |
| 3678 |
begin |
| 3679 |
Result := inherited RecvBufferFrom(Buffer, Length); |
| 3680 |
if FUsingSocks then |
| 3681 |
begin |
| 3682 |
{$IFNDEF CIL} |
| 3683 |
SetLength(Buf, Result); |
| 3684 |
Move(Buffer^, Pointer(Buf)^, Result); |
| 3685 |
x := SocksDecode(Buf); |
| 3686 |
Result := Result - x + 1; |
| 3687 |
Buf := Copy(Buf, x, Result); |
| 3688 |
Move(Pointer(Buf)^, Buffer^, Result); |
| 3689 |
SetRemoteSin(FSocksResponseIP, FSocksResponsePort); |
| 3690 |
{$ENDIF} |
| 3691 |
end; |
| 3692 |
end; |
| 3693 |
|
| 3694 |
{$IFNDEF CIL} |
| 3695 |
procedure TUDPBlockSocket.AddMulticast(MCastIP: string); |
| 3696 |
var |
| 3697 |
Multicast: TIP_mreq; |
| 3698 |
Multicast6: TIPv6_mreq; |
| 3699 |
n: integer; |
| 3700 |
ip6: Tip6bytes; |
| 3701 |
begin |
| 3702 |
if FIP6Used then |
| 3703 |
begin |
| 3704 |
ip6 := StrToIp6(MCastIP); |
| 3705 |
for n := 0 to 15 do |
| 3706 |
Multicast6.ipv6mr_multiaddr.{$IFDEF POSIX}s6_addr{$ELSE}u6_addr8{$ENDIF}[n] := Ip6[n]; |
| 3707 |
Multicast6.ipv6mr_interface := 0; |
| 3708 |
SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IPV6, IPV6_JOIN_GROUP, |
| 3709 |
PAnsiChar(@Multicast6), SizeOf(Multicast6))); |
| 3710 |
end |
| 3711 |
else |
| 3712 |
begin |
| 3713 |
Multicast.imr_multiaddr.S_addr := swapbytes(strtoip(MCastIP)); |
| 3714 |
// Multicast.imr_interface.S_addr := INADDR_ANY; |
| 3715 |
Multicast.imr_interface.S_addr := FLocalSin.sin_addr.S_addr; |
| 3716 |
SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 3717 |
PAnsiChar(@Multicast), SizeOf(Multicast))); |
| 3718 |
end; |
| 3719 |
ExceptCheck; |
| 3720 |
end; |
| 3721 |
|
| 3722 |
procedure TUDPBlockSocket.DropMulticast(MCastIP: string); |
| 3723 |
var |
| 3724 |
Multicast: TIP_mreq; |
| 3725 |
Multicast6: TIPv6_mreq; |
| 3726 |
n: integer; |
| 3727 |
ip6: Tip6bytes; |
| 3728 |
begin |
| 3729 |
if FIP6Used then |
| 3730 |
begin |
| 3731 |
ip6 := StrToIp6(MCastIP); |
| 3732 |
for n := 0 to 15 do |
| 3733 |
Multicast6.ipv6mr_multiaddr.{$IFDEF POSIX}s6_addr{$ELSE}u6_addr8{$ENDIF}[n] := Ip6[n]; |
| 3734 |
Multicast6.ipv6mr_interface := 0; |
| 3735 |
SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IPV6, IPV6_LEAVE_GROUP, |
| 3736 |
PAnsiChar(@Multicast6), SizeOf(Multicast6))); |
| 3737 |
end |
| 3738 |
else |
| 3739 |
begin |
| 3740 |
Multicast.imr_multiaddr.S_addr := swapbytes(strtoip(MCastIP)); |
| 3741 |
// Multicast.imr_interface.S_addr := INADDR_ANY; |
| 3742 |
Multicast.imr_interface.S_addr := FLocalSin.sin_addr.S_addr; |
| 3743 |
SockCheck(synsock.SetSockOpt(FSocket, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
| 3744 |
PAnsiChar(@Multicast), SizeOf(Multicast))); |
| 3745 |
end; |
| 3746 |
ExceptCheck; |
| 3747 |
end; |
| 3748 |
{$ENDIF} |
| 3749 |
|
| 3750 |
procedure TUDPBlockSocket.SetMulticastTTL(TTL: integer); |
| 3751 |
var |
| 3752 |
d: TSynaOption; |
| 3753 |
begin |
| 3754 |
d := TSynaOption.Create; |
| 3755 |
d.Option := SOT_MulticastTTL; |
| 3756 |
d.Value := TTL; |
| 3757 |
DelayedOption(d); |
| 3758 |
end; |
| 3759 |
|
| 3760 |
function TUDPBlockSocket.GetMulticastTTL:integer; |
| 3761 |
var |
| 3762 |
l: Integer; |
| 3763 |
begin |
| 3764 |
{$IFNDEF CIL} |
| 3765 |
l := SizeOf(Result); |
| 3766 |
if FIP6Used then |
| 3767 |
synsock.GetSockOpt(FSocket, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, @Result, l) |
| 3768 |
else |
| 3769 |
synsock.GetSockOpt(FSocket, IPPROTO_IP, IP_MULTICAST_TTL, @Result, l); |
| 3770 |
{$ENDIF} |
| 3771 |
end; |
| 3772 |
|
| 3773 |
procedure TUDPBlockSocket.EnableMulticastLoop(Value: Boolean); |
| 3774 |
var |
| 3775 |
d: TSynaOption; |
| 3776 |
begin |
| 3777 |
d := TSynaOption.Create; |
| 3778 |
d.Option := SOT_MulticastLoop; |
| 3779 |
d.Enabled := Value; |
| 3780 |
DelayedOption(d); |
| 3781 |
end; |
| 3782 |
|
| 3783 |
function TUDPBlockSocket.GetSocketType: integer; |
| 3784 |
begin |
| 3785 |
Result := integer(SOCK_DGRAM); |
| 3786 |
end; |
| 3787 |
|
| 3788 |
function TUDPBlockSocket.GetSocketProtocol: integer; |
| 3789 |
begin |
| 3790 |
Result := integer(IPPROTO_UDP); |
| 3791 |
end; |
| 3792 |
|
| 3793 |
{======================================================================} |
| 3794 |
constructor TTCPBlockSocket.CreateWithSSL(SSLPlugin: TSSLClass); |
| 3795 |
begin |
| 3796 |
inherited Create; |
| 3797 |
FSSL := SSLPlugin.Create(self); |
| 3798 |
FHTTPTunnelIP := ''; |
| 3799 |
FHTTPTunnelPort := ''; |
| 3800 |
FHTTPTunnel := False; |
| 3801 |
FHTTPTunnelRemoteIP := ''; |
| 3802 |
FHTTPTunnelRemotePort := ''; |
| 3803 |
FHTTPTunnelUser := ''; |
| 3804 |
FHTTPTunnelPass := ''; |
| 3805 |
FHTTPTunnelTimeout := 30000; |
| 3806 |
end; |
| 3807 |
|
| 3808 |
constructor TTCPBlockSocket.Create; |
| 3809 |
begin |
| 3810 |
CreateWithSSL(SSLImplementation); |
| 3811 |
end; |
| 3812 |
|
| 3813 |
destructor TTCPBlockSocket.Destroy; |
| 3814 |
begin |
| 3815 |
inherited Destroy; |
| 3816 |
FSSL.Free; |
| 3817 |
end; |
| 3818 |
|
| 3819 |
function TTCPBlockSocket.GetErrorDescEx: string; |
| 3820 |
begin |
| 3821 |
Result := inherited GetErrorDescEx; |
| 3822 |
if (FLastError = WSASYSNOTREADY) and (self.SSL.LastError <> 0) then |
| 3823 |
begin |
| 3824 |
Result := self.SSL.LastErrorDesc; |
| 3825 |
end; |
| 3826 |
end; |
| 3827 |
|
| 3828 |
procedure TTCPBlockSocket.CloseSocket; |
| 3829 |
begin |
| 3830 |
if FSSL.SSLEnabled then |
| 3831 |
FSSL.Shutdown; |
| 3832 |
if (FSocket <> INVALID_SOCKET) and (FLastError = 0) then |
| 3833 |
begin |
| 3834 |
Synsock.Shutdown(FSocket, 1); |
| 3835 |
Purge; |
| 3836 |
end; |
| 3837 |
inherited CloseSocket; |
| 3838 |
end; |
| 3839 |
|
| 3840 |
procedure TTCPBlockSocket.DoAfterConnect; |
| 3841 |
begin |
| 3842 |
if assigned(OnAfterConnect) then |
| 3843 |
begin |
| 3844 |
OnAfterConnect(Self); |
| 3845 |
end; |
| 3846 |
end; |
| 3847 |
|
| 3848 |
function TTCPBlockSocket.WaitingData: Integer; |
| 3849 |
begin |
| 3850 |
Result := 0; |
| 3851 |
if FSSL.SSLEnabled and (FSocket <> INVALID_SOCKET) then |
| 3852 |
Result := FSSL.WaitingData; |
| 3853 |
if Result = 0 then |
| 3854 |
Result := inherited WaitingData; |
| 3855 |
end; |
| 3856 |
|
| 3857 |
procedure TTCPBlockSocket.Listen; |
| 3858 |
var |
| 3859 |
b: Boolean; |
| 3860 |
Sip,SPort: string; |
| 3861 |
begin |
| 3862 |
if FSocksIP = '' then |
| 3863 |
begin |
| 3864 |
inherited Listen; |
| 3865 |
end |
| 3866 |
else |
| 3867 |
begin |
| 3868 |
Sip := GetLocalSinIP; |
| 3869 |
if Sip = cAnyHost then |
| 3870 |
Sip := LocalName; |
| 3871 |
SPort := IntToStr(GetLocalSinPort); |
| 3872 |
inherited Connect(FSocksIP, FSocksPort); |
| 3873 |
b := SocksOpen; |
| 3874 |
if b then |
| 3875 |
b := SocksRequest(2, Sip, SPort); |
| 3876 |
if b then |
| 3877 |
b := SocksResponse; |
| 3878 |
if not b and (FLastError = 0) then |
| 3879 |
FLastError := WSANO_RECOVERY; |
| 3880 |
FSocksLocalIP := FSocksResponseIP; |
| 3881 |
if FSocksLocalIP = cAnyHost then |
| 3882 |
FSocksLocalIP := FSocksIP; |
| 3883 |
FSocksLocalPort := FSocksResponsePort; |
| 3884 |
FSocksRemoteIP := ''; |
| 3885 |
FSocksRemotePort := ''; |
| 3886 |
ExceptCheck; |
| 3887 |
DoStatus(HR_Listen, ''); |
| 3888 |
end; |
| 3889 |
end; |
| 3890 |
|
| 3891 |
function TTCPBlockSocket.Accept: TSocket; |
| 3892 |
begin |
| 3893 |
if FUsingSocks then |
| 3894 |
begin |
| 3895 |
if not SocksResponse and (FLastError = 0) then |
| 3896 |
FLastError := WSANO_RECOVERY; |
| 3897 |
FSocksRemoteIP := FSocksResponseIP; |
| 3898 |
FSocksRemotePort := FSocksResponsePort; |
| 3899 |
Result := FSocket; |
| 3900 |
ExceptCheck; |
| 3901 |
DoStatus(HR_Accept, ''); |
| 3902 |
end |
| 3903 |
else |
| 3904 |
begin |
| 3905 |
result := inherited Accept; |
| 3906 |
end; |
| 3907 |
end; |
| 3908 |
|
| 3909 |
procedure TTCPBlockSocket.Connect(IP, Port: string); |
| 3910 |
begin |
| 3911 |
if FSocksIP <> '' then |
| 3912 |
SocksDoConnect(IP, Port) |
| 3913 |
else |
| 3914 |
if FHTTPTunnelIP <> '' then |
| 3915 |
HTTPTunnelDoConnect(IP, Port) |
| 3916 |
else |
| 3917 |
inherited Connect(IP, Port); |
| 3918 |
if FLasterror = 0 then |
| 3919 |
DoAfterConnect; |
| 3920 |
end; |
| 3921 |
|
| 3922 |
procedure TTCPBlockSocket.SocksDoConnect(IP, Port: string); |
| 3923 |
var |
| 3924 |
b: Boolean; |
| 3925 |
begin |
| 3926 |
inherited Connect(FSocksIP, FSocksPort); |
| 3927 |
if FLastError = 0 then |
| 3928 |
begin |
| 3929 |
b := SocksOpen; |
| 3930 |
if b then |
| 3931 |
b := SocksRequest(1, IP, Port); |
| 3932 |
if b then |
| 3933 |
b := SocksResponse; |
| 3934 |
if not b and (FLastError = 0) then |
| 3935 |
FLastError := WSASYSNOTREADY; |
| 3936 |
FSocksLocalIP := FSocksResponseIP; |
| 3937 |
FSocksLocalPort := FSocksResponsePort; |
| 3938 |
FSocksRemoteIP := IP; |
| 3939 |
FSocksRemotePort := Port; |
| 3940 |
end; |
| 3941 |
ExceptCheck; |
| 3942 |
DoStatus(HR_Connect, IP + ':' + Port); |
| 3943 |
end; |
| 3944 |
|
| 3945 |
procedure TTCPBlockSocket.HTTPTunnelDoConnect(IP, Port: string); |
| 3946 |
//bugfixed by Mike Green ([email protected]) |
| 3947 |
var |
| 3948 |
s: string; |
| 3949 |
begin |
| 3950 |
Port := IntToStr(ResolvePort(Port)); |
| 3951 |
inherited Connect(FHTTPTunnelIP, FHTTPTunnelPort); |
| 3952 |
if FLastError <> 0 then |
| 3953 |
Exit; |
| 3954 |
FHTTPTunnel := False; |
| 3955 |
if IsIP6(IP) then |
| 3956 |
IP := '[' + IP + ']'; |
| 3957 |
SendString('CONNECT ' + IP + ':' + Port + ' HTTP/1.0' + CRLF); |
| 3958 |
if FHTTPTunnelUser <> '' then |
| 3959 |
Sendstring('Proxy-Authorization: Basic ' + |
| 3960 |
EncodeBase64(FHTTPTunnelUser + ':' + FHTTPTunnelPass) + CRLF); |
| 3961 |
SendString(CRLF); |
| 3962 |
repeat |
| 3963 |
s := RecvTerminated(FHTTPTunnelTimeout, #$0a); |
| 3964 |
if FLastError <> 0 then |
| 3965 |
Break; |
| 3966 |
if (Pos('HTTP/', s) = 1) and (Length(s) > 11) then |
| 3967 |
FHTTPTunnel := s[10] = '2'; |
| 3968 |
until (s = '') or (s = #$0d); |
| 3969 |
if (FLasterror = 0) and not FHTTPTunnel then |
| 3970 |
FLastError := WSAECONNREFUSED; |
| 3971 |
FHTTPTunnelRemoteIP := IP; |
| 3972 |
FHTTPTunnelRemotePort := Port; |
| 3973 |
ExceptCheck; |
| 3974 |
end; |
| 3975 |
|
| 3976 |
procedure TTCPBlockSocket.SSLDoConnect; |
| 3977 |
begin |
| 3978 |
ResetLastError; |
| 3979 |
if not FSSL.Connect then |
| 3980 |
FLastError := WSASYSNOTREADY; |
| 3981 |
ExceptCheck; |
| 3982 |
end; |
| 3983 |
|
| 3984 |
procedure TTCPBlockSocket.SSLDoShutdown; |
| 3985 |
begin |
| 3986 |
ResetLastError; |
| 3987 |
FSSL.BiShutdown; |
| 3988 |
end; |
| 3989 |
|
| 3990 |
function TTCPBlockSocket.GetLocalSinIP: string; |
| 3991 |
begin |
| 3992 |
if FUsingSocks then |
| 3993 |
Result := FSocksLocalIP |
| 3994 |
else |
| 3995 |
Result := inherited GetLocalSinIP; |
| 3996 |
end; |
| 3997 |
|
| 3998 |
function TTCPBlockSocket.GetRemoteSinIP: string; |
| 3999 |
begin |
| 4000 |
if FUsingSocks then |
| 4001 |
Result := FSocksRemoteIP |
| 4002 |
else |
| 4003 |
if FHTTPTunnel then |
| 4004 |
Result := FHTTPTunnelRemoteIP |
| 4005 |
else |
| 4006 |
Result := inherited GetRemoteSinIP; |
| 4007 |
end; |
| 4008 |
|
| 4009 |
function TTCPBlockSocket.GetLocalSinPort: Integer; |
| 4010 |
begin |
| 4011 |
if FUsingSocks then |
| 4012 |
Result := StrToIntDef(FSocksLocalPort, 0) |
| 4013 |
else |
| 4014 |
Result := inherited GetLocalSinPort; |
| 4015 |
end; |
| 4016 |
|
| 4017 |
function TTCPBlockSocket.GetRemoteSinPort: Integer; |
| 4018 |
begin |
| 4019 |
if FUsingSocks then |
| 4020 |
Result := ResolvePort(FSocksRemotePort) |
| 4021 |
else |
| 4022 |
if FHTTPTunnel then |
| 4023 |
Result := StrToIntDef(FHTTPTunnelRemotePort, 0) |
| 4024 |
else |
| 4025 |
Result := inherited GetRemoteSinPort; |
| 4026 |
end; |
| 4027 |
|
| 4028 |
function TTCPBlockSocket.RecvBuffer(Buffer: TMemory; Len: Integer): Integer; |
| 4029 |
begin |
| 4030 |
if FSSL.SSLEnabled then |
| 4031 |
begin |
| 4032 |
Result := 0; |
| 4033 |
if TestStopFlag then |
| 4034 |
Exit; |
| 4035 |
ResetLastError; |
| 4036 |
LimitBandwidth(Len, FMaxRecvBandwidth, FNextRecv); |
| 4037 |
Result := FSSL.RecvBuffer(Buffer, Len); |
| 4038 |
if FSSL.LastError <> 0 then |
| 4039 |
FLastError := WSASYSNOTREADY; |
| 4040 |
ExceptCheck; |
| 4041 |
Inc(FRecvCounter, Result); |
| 4042 |
DoStatus(HR_ReadCount, IntToStr(Result)); |
| 4043 |
DoMonitor(False, Buffer, Result); |
| 4044 |
DoReadFilter(Buffer, Result); |
| 4045 |
end |
| 4046 |
else |
| 4047 |
Result := inherited RecvBuffer(Buffer, Len); |
| 4048 |
end; |
| 4049 |
|
| 4050 |
function TTCPBlockSocket.SendBuffer(const Buffer: TMemory; Length: Integer): Integer; |
| 4051 |
var |
| 4052 |
x, y: integer; |
| 4053 |
l, r: integer; |
| 4054 |
{$IFNDEF CIL} |
| 4055 |
p: Pointer; |
| 4056 |
{$ENDIF} |
| 4057 |
begin |
| 4058 |
if FSSL.SSLEnabled then |
| 4059 |
begin |
| 4060 |
Result := 0; |
| 4061 |
if TestStopFlag then |
| 4062 |
Exit; |
| 4063 |
ResetLastError; |
| 4064 |
DoMonitor(True, Buffer, Length); |
| 4065 |
{$IFDEF CIL} |
| 4066 |
Result := FSSL.SendBuffer(Buffer, Length); |
| 4067 |
if FSSL.LastError <> 0 then |
| 4068 |
FLastError := WSASYSNOTREADY; |
| 4069 |
Inc(FSendCounter, Result); |
| 4070 |
DoStatus(HR_WriteCount, IntToStr(Result)); |
| 4071 |
{$ELSE} |
| 4072 |
l := Length; |
| 4073 |
x := 0; |
| 4074 |
while x < l do |
| 4075 |
begin |
| 4076 |
y := l - x; |
| 4077 |
if y > FSendMaxChunk then |
| 4078 |
y := FSendMaxChunk; |
| 4079 |
if y > 0 then |
| 4080 |
begin |
| 4081 |
LimitBandwidth(y, FMaxSendBandwidth, FNextsend); |
| 4082 |
p := IncPoint(Buffer, x); |
| 4083 |
r := FSSL.SendBuffer(p, y); |
| 4084 |
if FSSL.LastError <> 0 then |
| 4085 |
FLastError := WSASYSNOTREADY; |
| 4086 |
if Flasterror <> 0 then |
| 4087 |
Break; |
| 4088 |
Inc(x, r); |
| 4089 |
Inc(Result, r); |
| 4090 |
Inc(FSendCounter, r); |
| 4091 |
DoStatus(HR_WriteCount, IntToStr(r)); |
| 4092 |
end |
| 4093 |
else |
| 4094 |
break; |
| 4095 |
end; |
| 4096 |
{$ENDIF} |
| 4097 |
ExceptCheck; |
| 4098 |
end |
| 4099 |
else |
| 4100 |
Result := inherited SendBuffer(Buffer, Length); |
| 4101 |
end; |
| 4102 |
|
| 4103 |
function TTCPBlockSocket.SSLAcceptConnection: Boolean; |
| 4104 |
begin |
| 4105 |
ResetLastError; |
| 4106 |
if not FSSL.Accept then |
| 4107 |
FLastError := WSASYSNOTREADY; |
| 4108 |
ExceptCheck; |
| 4109 |
Result := FLastError = 0; |
| 4110 |
end; |
| 4111 |
|
| 4112 |
function TTCPBlockSocket.GetSocketType: integer; |
| 4113 |
begin |
| 4114 |
Result := integer(SOCK_STREAM); |
| 4115 |
end; |
| 4116 |
|
| 4117 |
function TTCPBlockSocket.GetSocketProtocol: integer; |
| 4118 |
begin |
| 4119 |
Result := integer(IPPROTO_TCP); |
| 4120 |
end; |
| 4121 |
|
| 4122 |
{======================================================================} |
| 4123 |
|
| 4124 |
function TICMPBlockSocket.GetSocketType: integer; |
| 4125 |
begin |
| 4126 |
Result := integer(SOCK_RAW); |
| 4127 |
end; |
| 4128 |
|
| 4129 |
function TICMPBlockSocket.GetSocketProtocol: integer; |
| 4130 |
begin |
| 4131 |
if FIP6Used then |
| 4132 |
Result := integer(IPPROTO_ICMPV6) |
| 4133 |
else |
| 4134 |
Result := integer(IPPROTO_ICMP); |
| 4135 |
end; |
| 4136 |
|
| 4137 |
{======================================================================} |
| 4138 |
|
| 4139 |
function TRAWBlockSocket.GetSocketType: integer; |
| 4140 |
begin |
| 4141 |
Result := integer(SOCK_RAW); |
| 4142 |
end; |
| 4143 |
|
| 4144 |
function TRAWBlockSocket.GetSocketProtocol: integer; |
| 4145 |
begin |
| 4146 |
Result := integer(IPPROTO_RAW); |
| 4147 |
end; |
| 4148 |
|
| 4149 |
{======================================================================} |
| 4150 |
|
| 4151 |
function TPGMmessageBlockSocket.GetSocketType: integer; |
| 4152 |
begin |
| 4153 |
Result := integer(SOCK_RDM); |
| 4154 |
end; |
| 4155 |
|
| 4156 |
function TPGMmessageBlockSocket.GetSocketProtocol: integer; |
| 4157 |
begin |
| 4158 |
Result := integer(IPPROTO_RM); |
| 4159 |
end; |
| 4160 |
|
| 4161 |
{======================================================================} |
| 4162 |
|
| 4163 |
function TPGMstreamBlockSocket.GetSocketType: integer; |
| 4164 |
begin |
| 4165 |
Result := integer(SOCK_STREAM); |
| 4166 |
end; |
| 4167 |
|
| 4168 |
function TPGMstreamBlockSocket.GetSocketProtocol: integer; |
| 4169 |
begin |
| 4170 |
Result := integer(IPPROTO_RM); |
| 4171 |
end; |
| 4172 |
|
| 4173 |
{======================================================================} |
| 4174 |
|
| 4175 |
constructor TSynaClient.Create; |
| 4176 |
begin |
| 4177 |
inherited Create; |
| 4178 |
FIPInterface := cAnyHost; |
| 4179 |
FTargetHost := cLocalhost; |
| 4180 |
FTargetPort := cAnyPort; |
| 4181 |
FTimeout := 5000; |
| 4182 |
FUsername := ''; |
| 4183 |
FPassword := ''; |
| 4184 |
end; |
| 4185 |
|
| 4186 |
{======================================================================} |
| 4187 |
|
| 4188 |
constructor TCustomSSL.Create(const Value: TTCPBlockSocket); |
| 4189 |
begin |
| 4190 |
inherited Create; |
| 4191 |
FSocket := Value; |
| 4192 |
FSSLEnabled := False; |
| 4193 |
FUsername := ''; |
| 4194 |
FPassword := ''; |
| 4195 |
FLastError := 0; |
| 4196 |
FLastErrorDesc := ''; |
| 4197 |
FVerifyCert := False; |
| 4198 |
FSSLType := LT_all; |
| 4199 |
FKeyPassword := ''; |
| 4200 |
FCiphers := ''; |
| 4201 |
FCertificateFile := ''; |
| 4202 |
FPrivateKeyFile := ''; |
| 4203 |
FCertCAFile := ''; |
| 4204 |
FCertCA := ''; |
| 4205 |
FTrustCertificate := ''; |
| 4206 |
FTrustCertificateFile := ''; |
| 4207 |
FCertificate := ''; |
| 4208 |
FPrivateKey := ''; |
| 4209 |
FPFX := ''; |
| 4210 |
FPFXfile := ''; |
| 4211 |
FSSHChannelType := ''; |
| 4212 |
FSSHChannelArg1 := ''; |
| 4213 |
FSSHChannelArg2 := ''; |
| 4214 |
FCertComplianceLevel := -1; //default |
| 4215 |
FSNIHost := ''; |
| 4216 |
end; |
| 4217 |
|
| 4218 |
procedure TCustomSSL.Assign(const Value: TCustomSSL); |
| 4219 |
begin |
| 4220 |
FUsername := Value.Username; |
| 4221 |
FPassword := Value.Password; |
| 4222 |
FVerifyCert := Value.VerifyCert; |
| 4223 |
FSSLType := Value.SSLType; |
| 4224 |
FKeyPassword := Value.KeyPassword; |
| 4225 |
FCiphers := Value.Ciphers; |
| 4226 |
FCertificateFile := Value.CertificateFile; |
| 4227 |
FPrivateKeyFile := Value.PrivateKeyFile; |
| 4228 |
FCertCAFile := Value.CertCAFile; |
| 4229 |
FCertCA := Value.CertCA; |
| 4230 |
FTrustCertificate := Value.TrustCertificate; |
| 4231 |
FTrustCertificateFile := Value.TrustCertificateFile; |
| 4232 |
FCertificate := Value.Certificate; |
| 4233 |
FPrivateKey := Value.PrivateKey; |
| 4234 |
FPFX := Value.PFX; |
| 4235 |
FPFXfile := Value.PFXfile; |
| 4236 |
FCertComplianceLevel := Value.CertComplianceLevel; |
| 4237 |
FSNIHost := Value.FSNIHost; |
| 4238 |
end; |
| 4239 |
|
| 4240 |
procedure TCustomSSL.ReturnError; |
| 4241 |
begin |
| 4242 |
FLastError := -1; |
| 4243 |
FLastErrorDesc := 'SSL/TLS support is not compiled!'; |
| 4244 |
end; |
| 4245 |
|
| 4246 |
function TCustomSSL.LibVersion: String; |
| 4247 |
begin |
| 4248 |
Result := ''; |
| 4249 |
end; |
| 4250 |
|
| 4251 |
function TCustomSSL.LibName: String; |
| 4252 |
begin |
| 4253 |
Result := ''; |
| 4254 |
end; |
| 4255 |
|
| 4256 |
function TCustomSSL.CreateSelfSignedCert(Host: string): Boolean; |
| 4257 |
begin |
| 4258 |
Result := False; |
| 4259 |
end; |
| 4260 |
|
| 4261 |
function TCustomSSL.Connect: boolean; |
| 4262 |
begin |
| 4263 |
ReturnError; |
| 4264 |
Result := False; |
| 4265 |
end; |
| 4266 |
|
| 4267 |
function TCustomSSL.Accept: boolean; |
| 4268 |
begin |
| 4269 |
ReturnError; |
| 4270 |
Result := False; |
| 4271 |
end; |
| 4272 |
|
| 4273 |
function TCustomSSL.Shutdown: boolean; |
| 4274 |
begin |
| 4275 |
ReturnError; |
| 4276 |
Result := False; |
| 4277 |
end; |
| 4278 |
|
| 4279 |
function TCustomSSL.BiShutdown: boolean; |
| 4280 |
begin |
| 4281 |
ReturnError; |
| 4282 |
Result := False; |
| 4283 |
end; |
| 4284 |
|
| 4285 |
function TCustomSSL.SendBuffer(Buffer: TMemory; Len: Integer): Integer; |
| 4286 |
begin |
| 4287 |
ReturnError; |
| 4288 |
Result := integer(SOCKET_ERROR); |
| 4289 |
end; |
| 4290 |
|
| 4291 |
procedure TCustomSSL.SetCertCAFile(const Value: string); |
| 4292 |
begin |
| 4293 |
FCertCAFile := Value; |
| 4294 |
end; |
| 4295 |
|
| 4296 |
function TCustomSSL.RecvBuffer(Buffer: TMemory; Len: Integer): Integer; |
| 4297 |
begin |
| 4298 |
ReturnError; |
| 4299 |
Result := integer(SOCKET_ERROR); |
| 4300 |
end; |
| 4301 |
|
| 4302 |
function TCustomSSL.WaitingData: Integer; |
| 4303 |
begin |
| 4304 |
ReturnError; |
| 4305 |
Result := 0; |
| 4306 |
end; |
| 4307 |
|
| 4308 |
function TCustomSSL.GetSSLVersion: string; |
| 4309 |
begin |
| 4310 |
Result := ''; |
| 4311 |
end; |
| 4312 |
|
| 4313 |
function TCustomSSL.GetPeerSubject: string; |
| 4314 |
begin |
| 4315 |
Result := ''; |
| 4316 |
end; |
| 4317 |
|
| 4318 |
function TCustomSSL.GetPeerSerialNo: integer; |
| 4319 |
begin |
| 4320 |
Result := -1; |
| 4321 |
end; |
| 4322 |
|
| 4323 |
function TCustomSSL.GetPeerName: string; |
| 4324 |
begin |
| 4325 |
Result := ''; |
| 4326 |
end; |
| 4327 |
|
| 4328 |
function TCustomSSL.GetPeerNameHash: cardinal; |
| 4329 |
begin |
| 4330 |
Result := 0; |
| 4331 |
end; |
| 4332 |
|
| 4333 |
function TCustomSSL.GetPeerIssuer: string; |
| 4334 |
begin |
| 4335 |
Result := ''; |
| 4336 |
end; |
| 4337 |
|
| 4338 |
function TCustomSSL.GetPeerFingerprint: AnsiString; |
| 4339 |
begin |
| 4340 |
Result := ''; |
| 4341 |
end; |
| 4342 |
|
| 4343 |
function TCustomSSL.GetCertInfo: string; |
| 4344 |
begin |
| 4345 |
Result := ''; |
| 4346 |
end; |
| 4347 |
|
| 4348 |
function TCustomSSL.GetCipherName: string; |
| 4349 |
begin |
| 4350 |
Result := ''; |
| 4351 |
end; |
| 4352 |
|
| 4353 |
function TCustomSSL.GetCipherBits: integer; |
| 4354 |
begin |
| 4355 |
Result := 0; |
| 4356 |
end; |
| 4357 |
|
| 4358 |
function TCustomSSL.GetCipherAlgBits: integer; |
| 4359 |
begin |
| 4360 |
Result := 0; |
| 4361 |
end; |
| 4362 |
|
| 4363 |
function TCustomSSL.GetVerifyCert: integer; |
| 4364 |
begin |
| 4365 |
Result := 1; |
| 4366 |
end; |
| 4367 |
|
| 4368 |
function TCustomSSL.DoVerifyCert:boolean; |
| 4369 |
begin |
| 4370 |
if assigned(OnVerifyCert) then |
| 4371 |
begin |
| 4372 |
result:=OnVerifyCert(Self); |
| 4373 |
end |
| 4374 |
else |
| 4375 |
result:=true; |
| 4376 |
end; |
| 4377 |
|
| 4378 |
|
| 4379 |
{======================================================================} |
| 4380 |
|
| 4381 |
function TSSLNone.LibVersion: String; |
| 4382 |
begin |
| 4383 |
Result := 'Without SSL support'; |
| 4384 |
end; |
| 4385 |
|
| 4386 |
function TSSLNone.LibName: String; |
| 4387 |
begin |
| 4388 |
Result := 'ssl_none'; |
| 4389 |
end; |
| 4390 |
|
| 4391 |
{======================================================================} |
| 4392 |
|
| 4393 |
initialization |
| 4394 |
begin |
| 4395 |
{$IFDEF ONCEWINSOCK} |
| 4396 |
if not InitSocketInterface(DLLStackName) then |
| 4397 |
begin |
| 4398 |
e := ESynapseError.Create('Error loading Socket interface (' + DLLStackName + ')!'); |
| 4399 |
e.ErrorCode := 0; |
| 4400 |
e.ErrorMessage := 'Error loading Socket interface (' + DLLStackName + ')!'; |
| 4401 |
raise e; |
| 4402 |
end; |
| 4403 |
synsock.WSAStartup(WinsockLevel, WsaDataOnce); |
| 4404 |
{$ENDIF} |
| 4405 |
end; |
| 4406 |
|
| 4407 |
finalization |
| 4408 |
begin |
| 4409 |
{$IFDEF ONCEWINSOCK} |
| 4410 |
synsock.WSACleanup; |
| 4411 |
DestroySocketInterface; |
| 4412 |
{$ENDIF} |
| 4413 |
end; |
| 4414 |
|
| 4415 |
end. |