PageRenderTime 76ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/blcksock.pas

https://bitbucket.org/bsquared/fpctwit-git
Pascal | 4352 lines | 2990 code | 440 blank | 922 comment | 245 complexity | 8d865df43fcba42ca248a959ffd0b30c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. {==============================================================================|
  2. | Project : Ararat Synapse | 009.009.000 |
  3. |==============================================================================|
  4. | Content: Library base |
  5. |==============================================================================|
  6. | Copyright (c)1999-2012, 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-2012. |
  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. Special thanks to Gregor Ibic <gregor.ibic@intelicom.si>
  46. (Intelicom d.o.o., http://www.intelicom.si)
  47. for good inspiration about SSL programming.
  48. }
  49. {$DEFINE ONCEWINSOCK}
  50. {Note about define ONCEWINSOCK:
  51. If you remove this compiler directive, then socket interface is loaded and
  52. initialized on constructor of TBlockSocket class for each socket separately.
  53. Socket interface is used only if your need it.
  54. If you leave this directive here, then socket interface is loaded and
  55. initialized only once at start of your program! It boost performace on high
  56. count of created and destroyed sockets. It eliminate possible small resource
  57. leak on Windows systems too.
  58. }
  59. //{$DEFINE RAISEEXCEPT}
  60. {When you enable this define, then is Raiseexcept property is on by default
  61. }
  62. {:@abstract(Synapse's library core)
  63. Core with implementation basic socket classes.
  64. }
  65. {$IFDEF FPC}
  66. {$MODE DELPHI}
  67. {$ENDIF}
  68. {$IFDEF VER125}
  69. {$DEFINE BCB}
  70. {$ENDIF}
  71. {$IFDEF BCB}
  72. {$ObjExportAll On}
  73. {$ENDIF}
  74. {$Q-}
  75. {$H+}
  76. {$M+}
  77. {$TYPEDADDRESS OFF}
  78. //old Delphi does not have MSWINDOWS define.
  79. {$IFDEF WIN32}
  80. {$IFNDEF MSWINDOWS}
  81. {$DEFINE MSWINDOWS}
  82. {$ENDIF}
  83. {$ENDIF}
  84. {$IFDEF UNICODE}
  85. {$WARN IMPLICIT_STRING_CAST OFF}
  86. {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  87. {$ENDIF}
  88. unit blcksock;
  89. interface
  90. uses
  91. SysUtils, Classes,
  92. synafpc,
  93. synsock, synautil, synacode, synaip
  94. {$IFDEF CIL}
  95. ,System.Net
  96. ,System.Net.Sockets
  97. ,System.Text
  98. {$ENDIF}
  99. ;
  100. const
  101. SynapseRelease = '40';
  102. cLocalhost = '127.0.0.1';
  103. cAnyHost = '0.0.0.0';
  104. cBroadcast = '255.255.255.255';
  105. c6Localhost = '::1';
  106. c6AnyHost = '::0';
  107. c6Broadcast = 'ffff::1';
  108. cAnyPort = '0';
  109. CR = #$0d;
  110. LF = #$0a;
  111. CRLF = CR + LF;
  112. c64k = 65536;
  113. type
  114. {:@abstract(Exception clas used by Synapse)
  115. When you enable generating of exceptions, this exception is raised by
  116. Synapse's units.}
  117. ESynapseError = class(Exception)
  118. private
  119. FErrorCode: Integer;
  120. FErrorMessage: string;
  121. published
  122. {:Code of error. Value depending on used operating system}
  123. property ErrorCode: Integer read FErrorCode Write FErrorCode;
  124. {:Human readable description of error.}
  125. property ErrorMessage: string read FErrorMessage Write FErrorMessage;
  126. end;
  127. {:Types of OnStatus events}
  128. THookSocketReason = (
  129. {:Resolving is begin. Resolved IP and port is in parameter in format like:
  130. 'localhost.somewhere.com:25'.}
  131. HR_ResolvingBegin,
  132. {:Resolving is done. Resolved IP and port is in parameter in format like:
  133. 'localhost.somewhere.com:25'. It is always same as in HR_ResolvingBegin!}
  134. HR_ResolvingEnd,
  135. {:Socket created by CreateSocket method. It reporting Family of created
  136. socket too!}
  137. HR_SocketCreate,
  138. {:Socket closed by CloseSocket method.}
  139. HR_SocketClose,
  140. {:Socket binded to IP and Port. Binded IP and Port is in parameter in format
  141. like: 'localhost.somewhere.com:25'.}
  142. HR_Bind,
  143. {:Socket connected to IP and Port. Connected IP and Port is in parameter in
  144. format like: 'localhost.somewhere.com:25'.}
  145. HR_Connect,
  146. {:Called when CanRead method is used with @True result.}
  147. HR_CanRead,
  148. {:Called when CanWrite method is used with @True result.}
  149. HR_CanWrite,
  150. {:Socket is swithed to Listen mode. (TCP socket only)}
  151. HR_Listen,
  152. {:Socket Accepting client connection. (TCP socket only)}
  153. HR_Accept,
  154. {:report count of bytes readed from socket. Number is in parameter string.
  155. If you need is in integer, you must use StrToInt function!}
  156. HR_ReadCount,
  157. {:report count of bytes writed to socket. Number is in parameter string. If
  158. you need is in integer, you must use StrToInt function!}
  159. HR_WriteCount,
  160. {:If is limiting of bandwidth on, then this reason is called when sending or
  161. receiving is stopped for satisfy bandwidth limit. Parameter is count of
  162. waiting milliseconds.}
  163. HR_Wait,
  164. {:report situation where communication error occured. When raiseexcept is
  165. @true, then exception is called after this Hook reason.}
  166. HR_Error
  167. );
  168. {:Procedural type for OnStatus event. Sender is calling TBlockSocket object,
  169. Reason is one of set Status events and value is optional data.}
  170. THookSocketStatus = procedure(Sender: TObject; Reason: THookSocketReason;
  171. const Value: String) of object;
  172. {:This procedural type is used for DataFilter hooks.}
  173. THookDataFilter = procedure(Sender: TObject; var Value: AnsiString) of object;
  174. {:This procedural type is used for hook OnCreateSocket. By this hook you can
  175. insert your code after initialisation of socket. (you can set special socket
  176. options, etc.)}
  177. THookCreateSocket = procedure(Sender: TObject) of object;
  178. {:This procedural type is used for monitoring of communication.}
  179. THookMonitor = procedure(Sender: TObject; Writing: Boolean;
  180. const Buffer: TMemory; Len: Integer) of object;
  181. {:This procedural type is used for hook OnAfterConnect. By this hook you can
  182. insert your code after TCP socket has been sucessfully connected.}
  183. THookAfterConnect = procedure(Sender: TObject) of object;
  184. {:This procedural type is used for hook OnVerifyCert. By this hook you can
  185. insert your additional certificate verification code. Usefull to verify server
  186. CN against URL. }
  187. THookVerifyCert = function(Sender: TObject):boolean of object;
  188. {:This procedural type is used for hook OnHeartbeat. By this hook you can
  189. call your code repeately during long socket operations.
  190. You must enable heartbeats by @Link(HeartbeatRate) property!}
  191. THookHeartbeat = procedure(Sender: TObject) of object;
  192. {:Specify family of socket.}
  193. TSocketFamily = (
  194. {:Default mode. Socket family is defined by target address for connection.
  195. It allows instant access to IPv4 and IPv6 nodes. When you need IPv6 address
  196. as destination, then is used IPv6 mode. othervise is used IPv4 mode.
  197. However this mode not working properly with preliminary IPv6 supports!}
  198. SF_Any,
  199. {:Turn this class to pure IPv4 mode. This mode is totally compatible with
  200. previous Synapse releases.}
  201. SF_IP4,
  202. {:Turn to only IPv6 mode.}
  203. SF_IP6
  204. );
  205. {:specify possible values of SOCKS modes.}
  206. TSocksType = (
  207. ST_Socks5,
  208. ST_Socks4
  209. );
  210. {:Specify requested SSL/TLS version for secure connection.}
  211. TSSLType = (
  212. LT_all,
  213. LT_SSLv2,
  214. LT_SSLv3,
  215. LT_TLSv1,
  216. LT_TLSv1_1,
  217. LT_SSHv2
  218. );
  219. {:Specify type of socket delayed option.}
  220. TSynaOptionType = (
  221. SOT_Linger,
  222. SOT_RecvBuff,
  223. SOT_SendBuff,
  224. SOT_NonBlock,
  225. SOT_RecvTimeout,
  226. SOT_SendTimeout,
  227. SOT_Reuse,
  228. SOT_TTL,
  229. SOT_Broadcast,
  230. SOT_MulticastTTL,
  231. SOT_MulticastLoop
  232. );
  233. {:@abstract(this object is used for remember delayed socket option set.)}
  234. TSynaOption = class(TObject)
  235. public
  236. Option: TSynaOptionType;
  237. Enabled: Boolean;
  238. Value: Integer;
  239. end;
  240. TCustomSSL = class;
  241. TSSLClass = class of TCustomSSL;
  242. {:@abstract(Basic IP object.)
  243. This is parent class for other class with protocol implementations. Do not
  244. use this class directly! Use @link(TICMPBlockSocket), @link(TRAWBlockSocket),
  245. @link(TTCPBlockSocket) or @link(TUDPBlockSocket) instead.}
  246. TBlockSocket = class(TObject)
  247. private
  248. FOnStatus: THookSocketStatus;
  249. FOnReadFilter: THookDataFilter;
  250. FOnCreateSocket: THookCreateSocket;
  251. FOnMonitor: THookMonitor;
  252. FOnHeartbeat: THookHeartbeat;
  253. FLocalSin: TVarSin;
  254. FRemoteSin: TVarSin;
  255. FTag: integer;
  256. FBuffer: AnsiString;
  257. FRaiseExcept: Boolean;
  258. FNonBlockMode: Boolean;
  259. FMaxLineLength: Integer;
  260. FMaxSendBandwidth: Integer;
  261. FNextSend: LongWord;
  262. FMaxRecvBandwidth: Integer;
  263. FNextRecv: LongWord;
  264. FConvertLineEnd: Boolean;
  265. FLastCR: Boolean;
  266. FLastLF: Boolean;
  267. FBinded: Boolean;
  268. FFamily: TSocketFamily;
  269. FFamilySave: TSocketFamily;
  270. FIP6used: Boolean;
  271. FPreferIP4: Boolean;
  272. FDelayedOptions: TList;
  273. FInterPacketTimeout: Boolean;
  274. {$IFNDEF CIL}
  275. FFDSet: TFDSet;
  276. {$ENDIF}
  277. FRecvCounter: Integer;
  278. FSendCounter: Integer;
  279. FSendMaxChunk: Integer;
  280. FStopFlag: Boolean;
  281. FNonblockSendTimeout: Integer;
  282. FHeartbeatRate: integer;
  283. FConnectionTimeout: integer;
  284. {$IFNDEF ONCEWINSOCK}
  285. FWsaDataOnce: TWSADATA;
  286. {$ENDIF}
  287. function GetSizeRecvBuffer: Integer;
  288. procedure SetSizeRecvBuffer(Size: Integer);
  289. function GetSizeSendBuffer: Integer;
  290. procedure SetSizeSendBuffer(Size: Integer);
  291. procedure SetNonBlockMode(Value: Boolean);
  292. procedure SetTTL(TTL: integer);
  293. function GetTTL:integer;
  294. procedure SetFamily(Value: TSocketFamily); virtual;
  295. procedure SetSocket(Value: TSocket); virtual;
  296. function GetWsaData: TWSAData;
  297. function FamilyToAF(f: TSocketFamily): TAddrFamily;
  298. protected
  299. FSocket: TSocket;
  300. FLastError: Integer;
  301. FLastErrorDesc: string;
  302. FOwner: TObject;
  303. procedure SetDelayedOption(const Value: TSynaOption);
  304. procedure DelayedOption(const Value: TSynaOption);
  305. procedure ProcessDelayedOptions;
  306. procedure InternalCreateSocket(Sin: TVarSin);
  307. procedure SetSin(var Sin: TVarSin; IP, Port: string);
  308. function GetSinIP(Sin: TVarSin): string;
  309. function GetSinPort(Sin: TVarSin): Integer;
  310. procedure DoStatus(Reason: THookSocketReason; const Value: string);
  311. procedure DoReadFilter(Buffer: TMemory; var Len: Integer);
  312. procedure DoMonitor(Writing: Boolean; const Buffer: TMemory; Len: Integer);
  313. procedure DoCreateSocket;
  314. procedure DoHeartbeat;
  315. procedure LimitBandwidth(Length: Integer; MaxB: integer; var Next: LongWord);
  316. procedure SetBandwidth(Value: Integer);
  317. function TestStopFlag: Boolean;
  318. procedure InternalSendStream(const Stream: TStream; WithSize, Indy: boolean); virtual;
  319. function InternalCanRead(Timeout: Integer): Boolean; virtual;
  320. public
  321. constructor Create;
  322. {:Create object and load all necessary socket library. What library is
  323. loaded is described by STUB parameter. If STUB is empty string, then is
  324. loaded default libraries.}
  325. constructor CreateAlternate(Stub: string);
  326. destructor Destroy; override;
  327. {:If @link(family) is not SF_Any, then create socket with type defined in
  328. @link(Family) property. If family is SF_Any, then do nothing! (socket is
  329. created automaticly when you know what type of socket you need to create.
  330. (i.e. inside @link(Connect) or @link(Bind) call.) When socket is created,
  331. then is aplyed all stored delayed socket options.}
  332. procedure CreateSocket;
  333. {:It create socket. Address resolving of Value tells what type of socket is
  334. created. If Value is resolved as IPv4 IP, then is created IPv4 socket. If
  335. value is resolved as IPv6 address, then is created IPv6 socket.}
  336. procedure CreateSocketByName(const Value: String);
  337. {:Destroy socket in use. This method is also automatically called from
  338. object destructor.}
  339. procedure CloseSocket; virtual;
  340. {:Abort any work on Socket and destroy them.}
  341. procedure AbortSocket; virtual;
  342. {:Connects socket to local IP address and PORT. IP address may be numeric or
  343. symbolic ('192.168.74.50', 'cosi.nekde.cz', 'ff08::1'). The same for PORT
  344. - it may be number or mnemonic port ('23', 'telnet').
  345. If port value is '0', system chooses itself and conects unused port in the
  346. range 1024 to 4096 (this depending by operating system!). Structure
  347. LocalSin is filled after calling this method.
  348. Note: If you call this on non-created socket, then socket is created
  349. automaticly.
  350. Warning: when you call : Bind('0.0.0.0','0'); then is nothing done! In this
  351. case is used implicit system bind instead.}
  352. procedure Bind(IP, Port: string);
  353. {:Connects socket to remote IP address and PORT. The same rules as with
  354. @link(BIND) method are valid. The only exception is that PORT with 0 value
  355. will not be connected!
  356. Structures LocalSin and RemoteSin will be filled with valid values.
  357. When you call this on non-created socket, then socket is created
  358. automaticly. Type of created socket is by @link(Family) property. If is
  359. used SF_IP4, then is created socket for IPv4. If is used SF_IP6, then is
  360. created socket for IPv6. When you have family on SF_Any (default!), then
  361. type of created socket is determined by address resolving of destination
  362. address. (Not work properly on prilimitary winsock IPv6 support!)}
  363. procedure Connect(IP, Port: string); virtual;
  364. {:Sets socket to receive mode for new incoming connections. It is necessary
  365. to use @link(TBlockSocket.BIND) function call before this method to select
  366. receiving port!}
  367. procedure Listen; virtual;
  368. {:Waits until new incoming connection comes. After it comes a new socket is
  369. automatically created (socket handler is returned by this function as
  370. result).}
  371. function Accept: TSocket; virtual;
  372. {:Sends data of LENGTH from BUFFER address via connected socket. System
  373. automatically splits data to packets.}
  374. function SendBuffer(Buffer: Tmemory; Length: Integer): Integer; virtual;
  375. {:One data BYTE is sent via connected socket.}
  376. procedure SendByte(Data: Byte); virtual;
  377. {:Send data string via connected socket. Any terminator is not added! If you
  378. need send true string with CR-LF termination, you must add CR-LF characters
  379. to sended string! Because any termination is not added automaticly, you can
  380. use this function for sending any binary data in binary string.}
  381. procedure SendString(Data: AnsiString); virtual;
  382. {:Send integer as four bytes to socket.}
  383. procedure SendInteger(Data: integer); virtual;
  384. {:Send data as one block to socket. Each block begin with 4 bytes with
  385. length of data in block. This 4 bytes is added automaticly by this
  386. function.}
  387. procedure SendBlock(const Data: AnsiString); virtual;
  388. {:Send data from stream to socket.}
  389. procedure SendStreamRaw(const Stream: TStream); virtual;
  390. {:Send content of stream to socket. It using @link(SendBlock) method}
  391. procedure SendStream(const Stream: TStream); virtual;
  392. {:Send content of stream to socket. It using @link(SendBlock) method and
  393. this is compatible with streams in Indy library.}
  394. procedure SendStreamIndy(const Stream: TStream); virtual;
  395. {:Note: This is low-level receive function. You must be sure if data is
  396. waiting for read before call this function for avoid deadlock!
  397. Waits until allocated buffer is filled by received data. Returns number of
  398. data received, which equals to LENGTH value under normal operation. If it
  399. is not equal the communication channel is possibly broken.
  400. On stream oriented sockets if is received 0 bytes, it mean 'socket is
  401. closed!"
  402. On datagram socket is readed first waiting datagram.}
  403. function RecvBuffer(Buffer: TMemory; Length: Integer): Integer; virtual;
  404. {:Note: This is high-level receive function. It using internal
  405. @link(LineBuffer) and you can combine this function freely with other
  406. high-level functions!
  407. Method waits until data is received. If no data is received within TIMEOUT
  408. (in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT. Methods
  409. serves for reading any size of data (i.e. one megabyte...). This method is
  410. preffered for reading from stream sockets (like TCP).}
  411. function RecvBufferEx(Buffer: Tmemory; Len: Integer;
  412. Timeout: Integer): Integer; virtual;
  413. {:Similar to @link(RecvBufferEx), but readed data is stored in binary
  414. string, not in memory buffer.}
  415. function RecvBufferStr(Len: Integer; Timeout: Integer): AnsiString; virtual;
  416. {:Note: This is high-level receive function. It using internal
  417. @link(LineBuffer) and you can combine this function freely with other
  418. high-level functions.
  419. Waits until one data byte is received which is also returned as function
  420. result. If no data is received within TIMEOUT (in milliseconds)period,
  421. @link(LastError) is set to WSAETIMEDOUT and result have value 0.}
  422. function RecvByte(Timeout: Integer): Byte; virtual;
  423. {:Note: This is high-level receive function. It using internal
  424. @link(LineBuffer) and you can combine this function freely with other
  425. high-level functions.
  426. Waits until one four bytes are received and return it as one Ineger Value.
  427. If no data is received within TIMEOUT (in milliseconds)period,
  428. @link(LastError) is set to WSAETIMEDOUT and result have value 0.}
  429. function RecvInteger(Timeout: Integer): Integer; virtual;
  430. {:Note: This is high-level receive function. It using internal
  431. @link(LineBuffer) and you can combine this function freely with other
  432. high-level functions.
  433. Method waits until data string is received. This string is terminated by
  434. CR-LF characters. The resulting string is returned without this termination
  435. (CR-LF)! If @link(ConvertLineEnd) is used, then CR-LF sequence may not be
  436. exactly CR-LF. See @link(ConvertLineEnd) description. If no data is
  437. received within TIMEOUT (in milliseconds) period, @link(LastError) is set
  438. to WSAETIMEDOUT. You may also specify maximum length of reading data by
  439. @link(MaxLineLength) property.}
  440. function RecvString(Timeout: Integer): AnsiString; virtual;
  441. {:Note: This is high-level receive function. It using internal
  442. @link(LineBuffer) and you can combine this function freely with other
  443. high-level functions.
  444. Method waits until data string is received. This string is terminated by
  445. Terminator string. The resulting string is returned without this
  446. termination. If no data is received within TIMEOUT (in milliseconds)
  447. period, @link(LastError) is set to WSAETIMEDOUT. You may also specify
  448. maximum length of reading data by @link(MaxLineLength) property.}
  449. function RecvTerminated(Timeout: Integer; const Terminator: AnsiString): AnsiString; virtual;
  450. {:Note: This is high-level receive function. It using internal
  451. @link(LineBuffer) and you can combine this function freely with other
  452. high-level functions.
  453. Method reads all data waiting for read. If no data is received within
  454. TIMEOUT (in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT.
  455. Methods serves for reading unknown size of data. Because before call this
  456. function you don't know size of received data, returned data is stored in
  457. dynamic size binary string. This method is preffered for reading from
  458. stream sockets (like TCP). It is very goot for receiving datagrams too!
  459. (UDP protocol)}
  460. function RecvPacket(Timeout: Integer): AnsiString; virtual;
  461. {:Read one block of data from socket. Each block begin with 4 bytes with
  462. length of data in block. This function read first 4 bytes for get lenght,
  463. then it wait for reported count of bytes.}
  464. function RecvBlock(Timeout: Integer): AnsiString; virtual;
  465. {:Read all data from socket to stream until socket is closed (or any error
  466. occured.)}
  467. procedure RecvStreamRaw(const Stream: TStream; Timeout: Integer); virtual;
  468. {:Read requested count of bytes from socket to stream.}
  469. procedure RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: Integer);
  470. {:Receive data to stream. It using @link(RecvBlock) method.}
  471. procedure RecvStream(const Stream: TStream; Timeout: Integer); virtual;
  472. {:Receive data to stream. This function is compatible with similar function
  473. in Indy library. It using @link(RecvBlock) method.}
  474. procedure RecvStreamIndy(const Stream: TStream; Timeout: Integer); virtual;
  475. {:Same as @link(RecvBuffer), but readed data stays in system input buffer.
  476. Warning: this function not respect data in @link(LineBuffer)! Is not
  477. recommended to use this function!}
  478. function PeekBuffer(Buffer: TMemory; Length: Integer): Integer; virtual;
  479. {:Same as @link(RecvByte), but readed data stays in input system buffer.
  480. Warning: this function not respect data in @link(LineBuffer)! Is not
  481. recommended to use this function!}
  482. function PeekByte(Timeout: Integer): Byte; virtual;
  483. {:On stream sockets it returns number of received bytes waiting for picking.
  484. 0 is returned when there is no such data. On datagram socket it returns
  485. length of the first waiting datagram. Returns 0 if no datagram is waiting.}
  486. function WaitingData: Integer; virtual;
  487. {:Same as @link(WaitingData), but if exists some of data in @link(Linebuffer),
  488. return their length instead.}
  489. function WaitingDataEx: Integer;
  490. {:Clear all waiting data for read from buffers.}
  491. procedure Purge;
  492. {:Sets linger. Enabled linger means that the system waits another LINGER
  493. (in milliseconds) time for delivery of sent data. This function is only for
  494. stream type of socket! (TCP)}
  495. procedure SetLinger(Enable: Boolean; Linger: Integer);
  496. {:Actualize values in @link(LocalSin).}
  497. procedure GetSinLocal;
  498. {:Actualize values in @link(RemoteSin).}
  499. procedure GetSinRemote;
  500. {:Actualize values in @link(LocalSin) and @link(RemoteSin).}
  501. procedure GetSins;
  502. {:Reset @link(LastError) and @link(LastErrorDesc) to non-error state.}
  503. procedure ResetLastError;
  504. {:If you "manually" call Socket API functions, forward their return code as
  505. parameter to this function, which evaluates it, eventually calls
  506. GetLastError and found error code returns and stores to @link(LastError).}
  507. function SockCheck(SockResult: Integer): Integer; virtual;
  508. {:If @link(LastError) contains some error code and @link(RaiseExcept)
  509. property is @true, raise adequate exception.}
  510. procedure ExceptCheck;
  511. {:Returns local computer name as numerical or symbolic value. It try get
  512. fully qualified domain name. Name is returned in the format acceptable by
  513. functions demanding IP as input parameter.}
  514. function LocalName: string;
  515. {:Try resolve name to all possible IP address. i.e. If you pass as name
  516. result of @link(LocalName) method, you get all IP addresses used by local
  517. system.}
  518. procedure ResolveNameToIP(Name: string; const IPList: TStrings);
  519. {:Try resolve name to primary IP address. i.e. If you pass as name result of
  520. @link(LocalName) method, you get primary IP addresses used by local system.}
  521. function ResolveName(Name: string): string;
  522. {:Try resolve IP to their primary domain name. If IP not have domain name,
  523. then is returned original IP.}
  524. function ResolveIPToName(IP: string): string;
  525. {:Try resolve symbolic port name to port number. (i.e. 'Echo' to 8)}
  526. function ResolvePort(Port: string): Word;
  527. {:Set information about remote side socket. It is good for seting remote
  528. side for sending UDP packet, etc.}
  529. procedure SetRemoteSin(IP, Port: string);
  530. {:Picks IP socket address from @link(LocalSin).}
  531. function GetLocalSinIP: string; virtual;
  532. {:Picks IP socket address from @link(RemoteSin).}
  533. function GetRemoteSinIP: string; virtual;
  534. {:Picks socket PORT number from @link(LocalSin).}
  535. function GetLocalSinPort: Integer; virtual;
  536. {:Picks socket PORT number from @link(RemoteSin).}
  537. function GetRemoteSinPort: Integer; virtual;
  538. {:Return @TRUE, if you can read any data from socket or is incoming
  539. connection on TCP based socket. Status is tested for time Timeout (in
  540. milliseconds). If value in Timeout is 0, status is only tested and
  541. continue. If value in Timeout is -1, run is breaked and waiting for read
  542. data maybe forever.
  543. This function is need only on special cases, when you need use
  544. @link(RecvBuffer) function directly! read functioms what have timeout as
  545. calling parameter, calling this function internally.}
  546. function CanRead(Timeout: Integer): Boolean; virtual;
  547. {:Same as @link(CanRead), but additionally return @TRUE if is some data in
  548. @link(LineBuffer).}
  549. function CanReadEx(Timeout: Integer): Boolean; virtual;
  550. {:Return @TRUE, if you can to socket write any data (not full sending
  551. buffer). Status is tested for time Timeout (in milliseconds). If value in
  552. Timeout is 0, status is only tested and continue. If value in Timeout is
  553. -1, run is breaked and waiting for write data maybe forever.
  554. This function is need only on special cases!}
  555. function CanWrite(Timeout: Integer): Boolean; virtual;
  556. {:Same as @link(SendBuffer), but send datagram to address from
  557. @link(RemoteSin). Usefull for sending reply to datagram received by
  558. function @link(RecvBufferFrom).}
  559. function SendBufferTo(Buffer: TMemory; Length: Integer): Integer; virtual;
  560. {:Note: This is low-lever receive function. You must be sure if data is
  561. waiting for read before call this function for avoid deadlock!
  562. Receives first waiting datagram to allocated buffer. If there is no waiting
  563. one, then waits until one comes. Returns length of datagram stored in
  564. BUFFER. If length exceeds buffer datagram is truncated. After this
  565. @link(RemoteSin) structure contains information about sender of UDP packet.}
  566. function RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; virtual;
  567. {$IFNDEF CIL}
  568. {:This function is for check for incoming data on set of sockets. Whitch
  569. sockets is checked is decribed by SocketList Tlist with TBlockSocket
  570. objects. TList may have maximal number of objects defined by FD_SETSIZE
  571. constant. Return @TRUE, if you can from some socket read any data or is
  572. incoming connection on TCP based socket. Status is tested for time Timeout
  573. (in milliseconds). If value in Timeout is 0, status is only tested and
  574. continue. If value in Timeout is -1, run is breaked and waiting for read
  575. data maybe forever. If is returned @TRUE, CanReadList TList is filled by all
  576. TBlockSocket objects what waiting for read.}
  577. function GroupCanRead(const SocketList: TList; Timeout: Integer;
  578. const CanReadList: TList): Boolean;
  579. {$ENDIF}
  580. {:By this method you may turn address reuse mode for local @link(bind). It
  581. is good specially for UDP protocol. Using this with TCP protocol is
  582. hazardous!}
  583. procedure EnableReuse(Value: Boolean);
  584. {:Try set timeout for all sending and receiving operations, if socket
  585. provider can do it. (It not supported by all socket providers!)}
  586. procedure SetTimeout(Timeout: Integer);
  587. {:Try set timeout for all sending operations, if socket provider can do it.
  588. (It not supported by all socket providers!)}
  589. procedure SetSendTimeout(Timeout: Integer);
  590. {:Try set timeout for all receiving operations, if socket provider can do
  591. it. (It not supported by all socket providers!)}
  592. procedure SetRecvTimeout(Timeout: Integer);
  593. {:Return value of socket type.}
  594. function GetSocketType: integer; Virtual;
  595. {:Return value of protocol type for socket creation.}
  596. function GetSocketProtocol: integer; Virtual;
  597. {:WSA structure with information about socket provider. On non-windows
  598. platforms this structure is simulated!}
  599. property WSAData: TWSADATA read GetWsaData;
  600. {:FDset structure prepared for usage with this socket.}
  601. property FDset: TFDSet read FFDset;
  602. {:Structure describing local socket side.}
  603. property LocalSin: TVarSin read FLocalSin write FLocalSin;
  604. {:Structure describing remote socket side.}
  605. property RemoteSin: TVarSin read FRemoteSin write FRemoteSin;
  606. {:Socket handler. Suitable for "manual" calls to socket API or manual
  607. connection of socket to a previously created socket (i.e by Accept method
  608. on TCP socket)}
  609. property Socket: TSocket read FSocket write SetSocket;
  610. {:Last socket operation error code. Error codes are described in socket
  611. documentation. Human readable error description is stored in
  612. @link(LastErrorDesc) property.}
  613. property LastError: Integer read FLastError;
  614. {:Human readable error description of @link(LastError) code.}
  615. property LastErrorDesc: string read FLastErrorDesc;
  616. {:Buffer used by all high-level receiving functions. This buffer is used for
  617. optimized reading of data from socket. In normal cases you not need access
  618. to this buffer directly!}
  619. property LineBuffer: AnsiString read FBuffer write FBuffer;
  620. {:Size of Winsock receive buffer. If it is not supported by socket provider,
  621. it return as size one kilobyte.}
  622. property SizeRecvBuffer: Integer read GetSizeRecvBuffer write SetSizeRecvBuffer;
  623. {:Size of Winsock send buffer. If it is not supported by socket provider, it
  624. return as size one kilobyte.}
  625. property SizeSendBuffer: Integer read GetSizeSendBuffer write SetSizeSendBuffer;
  626. {:If @True, turn class to non-blocking mode. Not all functions are working
  627. properly in this mode, you must know exactly what you are doing! However
  628. when you have big experience with non-blocking programming, then you can
  629. optimise your program by non-block mode!}
  630. property NonBlockMode: Boolean read FNonBlockMode Write SetNonBlockMode;
  631. {:Set Time-to-live value. (if system supporting it!)}
  632. property TTL: Integer read GetTTL Write SetTTL;
  633. {:If is @true, then class in in IPv6 mode.}
  634. property IP6used: Boolean read FIP6used;
  635. {:Return count of received bytes on this socket from begin of current
  636. connection.}
  637. property RecvCounter: Integer read FRecvCounter;
  638. {:Return count of sended bytes on this socket from begin of current
  639. connection.}
  640. property SendCounter: Integer read FSendCounter;
  641. published
  642. {:Return descriptive string for given error code. This is class function.
  643. You may call it without created object!}
  644. class function GetErrorDesc(ErrorCode: Integer): string;
  645. {:Return descriptive string for @link(LastError).}
  646. function GetErrorDescEx: string; virtual;
  647. {:this value is for free use.}
  648. property Tag: Integer read FTag write FTag;
  649. {:If @true, winsock errors raises exception. Otherwise is setted
  650. @link(LastError) value only and you must check it from your program! Default
  651. value is @false.}
  652. property RaiseExcept: Boolean read FRaiseExcept write FRaiseExcept;
  653. {:Define maximum length in bytes of @link(LineBuffer) for high-level
  654. receiving functions. If this functions try to read more data then this
  655. limit, error is returned! If value is 0 (default), no limitation is used.
  656. This is very good protection for stupid attacks to your server by sending
  657. lot of data without proper terminator... until all your memory is allocated
  658. by LineBuffer!
  659. Note: This maximum length is checked only in functions, what read unknown
  660. number of bytes! (like @link(RecvString) or @link(RecvTerminated))}
  661. property MaxLineLength: Integer read FMaxLineLength Write FMaxLineLength;
  662. {:Define maximal bandwidth for all sending operations in bytes per second.
  663. If value is 0 (default), bandwidth limitation is not used.}
  664. property MaxSendBandwidth: Integer read FMaxSendBandwidth Write FMaxSendBandwidth;
  665. {:Define maximal bandwidth for all receiving operations in bytes per second.
  666. If value is 0 (default), bandwidth limitation is not used.}
  667. property MaxRecvBandwidth: Integer read FMaxRecvBandwidth Write FMaxRecvBandwidth;
  668. {:Define maximal bandwidth for all sending and receiving operations in bytes
  669. per second. If value is 0 (default), bandwidth limitation is not used.}
  670. property MaxBandwidth: Integer Write SetBandwidth;
  671. {:Do a conversion of non-standard line terminators to CRLF. (Off by default)
  672. If @True, then terminators like sigle CR, single LF or LFCR are converted
  673. to CRLF internally. This have effect only in @link(RecvString) method!}
  674. property ConvertLineEnd: Boolean read FConvertLineEnd Write FConvertLineEnd;
  675. {:Specified Family of this socket. When you are using Windows preliminary
  676. support for IPv6, then I recommend to set this property!}
  677. property Family: TSocketFamily read FFamily Write SetFamily;
  678. {:When resolving of domain name return both IPv4 and IPv6 addresses, then
  679. specify if is used IPv4 (dafault - @true) or IPv6.}
  680. property PreferIP4: Boolean read FPreferIP4 Write FPreferIP4;
  681. {:By default (@true) is all timeouts used as timeout between two packets in
  682. reading operations. If you set this to @false, then Timeouts is for overall
  683. reading operation!}
  684. property InterPacketTimeout: Boolean read FInterPacketTimeout Write FInterPacketTimeout;
  685. {:All sended datas was splitted by this value.}
  686. property SendMaxChunk: Integer read FSendMaxChunk Write FSendMaxChunk;
  687. {:By setting this property to @true you can stop any communication. You can
  688. use this property for soft abort of communication.}
  689. property StopFlag: Boolean read FStopFlag Write FStopFlag;
  690. {:Timeout for data sending by non-blocking socket mode.}
  691. property NonblockSendTimeout: Integer read FNonblockSendTimeout Write FNonblockSendTimeout;
  692. {:Timeout for @link(Connect) call. Default value 0 means default system timeout.
  693. Non-zero value means timeout in millisecond.}
  694. property ConnectionTimeout: Integer read FConnectionTimeout write FConnectionTimeout;
  695. {:This event is called by various reasons. It is good for monitoring socket,
  696. create gauges for data transfers, etc.}
  697. property OnStatus: THookSocketStatus read FOnStatus write FOnStatus;
  698. {:this event is good for some internal thinks about filtering readed datas.
  699. It is used by telnet client by example.}
  700. property OnReadFilter: THookDataFilter read FOnReadFilter write FOnReadFilter;
  701. {:This event is called after real socket creation for setting special socket
  702. options, because you not know when socket is created. (it is depended on
  703. Ipv4, IPv6 or automatic mode)}
  704. property OnCreateSocket: THookCreateSocket read FOnCreateSocket write FOnCreateSocket;
  705. {:This event is good for monitoring content of readed or writed datas.}
  706. property OnMonitor: THookMonitor read FOnMonitor write FOnMonitor;
  707. {:This event is good for calling your code during long socket operations.
  708. (Example, for refresing UI if class in not called within the thread.)
  709. Rate of heartbeats can be modified by @link(HeartbeatRate) property.}
  710. property OnHeartbeat: THookHeartbeat read FOnHeartbeat write FOnHeartbeat;
  711. {:Specify typical rate of @link(OnHeartbeat) event and @link(StopFlag) testing.
  712. Default value 0 disabling heartbeats! Value is in milliseconds.
  713. Real rate can be higher or smaller then this value, because it depending
  714. on real socket operations too!
  715. Note: Each heartbeat slowing socket processing.}
  716. property HeartbeatRate: integer read FHeartbeatRate Write FHeartbeatRate;
  717. {:What class own this socket? Used by protocol implementation classes.}
  718. property Owner: TObject read FOwner Write FOwner;
  719. end;
  720. {:@abstract(Support for SOCKS4 and SOCKS5 proxy)
  721. Layer with definition all necessary properties and functions for
  722. implementation SOCKS proxy client. Do not use this class directly.}
  723. TSocksBlockSocket = class(TBlockSocket)
  724. protected
  725. FSocksIP: string;
  726. FSocksPort: string;
  727. FSocksTimeout: integer;
  728. FSocksUsername: string;
  729. FSocksPassword: string;
  730. FUsingSocks: Boolean;
  731. FSocksResolver: Boolean;
  732. FSocksLastError: integer;
  733. FSocksResponseIP: string;
  734. FSocksResponsePort: string;
  735. FSocksLocalIP: string;
  736. FSocksLocalPort: string;
  737. FSocksRemoteIP: string;
  738. FSocksRemotePort: string;
  739. FBypassFlag: Boolean;
  740. FSocksType: TSocksType;
  741. function SocksCode(IP, Port: string): Ansistring;
  742. function SocksDecode(Value: Ansistring): integer;
  743. public
  744. constructor Create;
  745. {:Open connection to SOCKS proxy and if @link(SocksUsername) is set, do
  746. authorisation to proxy. This is needed only in special cases! (it is called
  747. internally!)}
  748. function SocksOpen: Boolean;
  749. {:Send specified request to SOCKS proxy. This is needed only in special
  750. cases! (it is called internally!)}
  751. function SocksRequest(Cmd: Byte; const IP, Port: string): Boolean;
  752. {:Receive response to previosly sended request. This is needed only in
  753. special cases! (it is called internally!)}
  754. function SocksResponse: Boolean;
  755. {:Is @True when class is using SOCKS proxy.}
  756. property UsingSocks: Boolean read FUsingSocks;
  757. {:If SOCKS proxy failed, here is error code returned from SOCKS proxy.}
  758. property SocksLastError: integer read FSocksLastError;
  759. published
  760. {:Address of SOCKS server. If value is empty string, SOCKS support is
  761. disabled. Assingning any value to this property enable SOCKS mode.
  762. Warning: You cannot combine this mode with HTTP-tunneling mode!}
  763. property SocksIP: string read FSocksIP write FSocksIP;
  764. {:Port of SOCKS server. Default value is '1080'.}
  765. property SocksPort: string read FSocksPort write FSocksPort;
  766. {:If you need authorisation on SOCKS server, set username here.}
  767. property SocksUsername: string read FSocksUsername write FSocksUsername;
  768. {:If you need authorisation on SOCKS server, set password here.}
  769. property SocksPassword: string read FSocksPassword write FSocksPassword;
  770. {:Specify timeout for communicatin with SOCKS server. Default is one minute.}
  771. property SocksTimeout: integer read FSocksTimeout write FSocksTimeout;
  772. {:If @True, all symbolic names of target hosts is not translated to IP's
  773. locally, but resolving is by SOCKS proxy. Default is @True.}
  774. property SocksResolver: Boolean read FSocksResolver write FSocksResolver;
  775. {:Specify SOCKS type. By default is used SOCKS5, but you can use SOCKS4 too.
  776. When you select SOCKS4, then if @link(SOCKSResolver) is enabled, then is
  777. used SOCKS4a. Othervise is used pure SOCKS4.}
  778. property SocksType: TSocksType read FSocksType write FSocksType;
  779. end;
  780. {:@abstract(Implementation of TCP socket.)
  781. Supported features: IPv4, IPv6, SSL/TLS or SSH (depending on used plugin),
  782. SOCKS5 proxy (outgoing connections and limited incomming), SOCKS4/4a proxy
  783. (outgoing connections and limited incomming), TCP through HTTP proxy tunnel.}
  784. TTCPBlockSocket = class(TSocksBlockSocket)
  785. protected
  786. FOnAfterConnect: THookAfterConnect;
  787. FSSL: TCustomSSL;
  788. FHTTPTunnelIP: string;
  789. FHTTPTunnelPort: string;
  790. FHTTPTunnel: Boolean;
  791. FHTTPTunnelRemoteIP: string;
  792. FHTTPTunnelRemotePort: string;
  793. FHTTPTunnelUser: string;
  794. FHTTPTunnelPass: string;
  795. FHTTPTunnelTimeout: integer;
  796. procedure SocksDoConnect(IP, Port: string);
  797. procedure HTTPTunnelDoConnect(IP, Port: string);
  798. procedure DoAfterConnect;
  799. public
  800. {:Create TCP socket class with default plugin for SSL/TSL/SSH implementation
  801. (see @link(SSLImplementation))}
  802. constructor Create;
  803. {:Create TCP socket class with desired plugin for SSL/TSL/SSH implementation}
  804. constructor CreateWithSSL(SSLPlugin: TSSLClass);
  805. destructor Destroy; override;
  806. {:See @link(TBlockSocket.CloseSocket)}
  807. procedure CloseSocket; override;
  808. {:See @link(TBlockSocket.WaitingData)}
  809. function WaitingData: Integer; override;
  810. {:Sets socket to receive mode for new incoming connections. It is necessary
  811. to use @link(TBlockSocket.BIND) function call before this method to select
  812. receiving port!
  813. If you use SOCKS, activate incoming TCP connection by this proxy. (By BIND
  814. method of SOCKS.)}
  815. procedure Listen; override;
  816. {:Waits until new incoming connection comes. After it comes a new socket is
  817. automatically created (socket handler is returned by this function as
  818. result).
  819. If you use SOCKS, new socket is not created! In this case is used same
  820. socket as socket for listening! So, you can accept only one connection in
  821. SOCKS mode.}
  822. function Accept: TSocket; override;
  823. {:Connects socket to remote IP address and PORT. The same rules as with
  824. @link(TBlockSocket.BIND) method are valid. The only exception is that PORT
  825. with 0 value will not be connected. After call to this method
  826. a communication channel between local and remote socket is created. Local
  827. socket is assigned automatically if not controlled by previous call to
  828. @link(TBlockSocket.BIND) method. Structures @link(TBlockSocket.LocalSin)
  829. and @link(TBlockSocket.RemoteSin) will be filled with valid values.
  830. If you use SOCKS, activate outgoing TCP connection by SOCKS proxy specified
  831. in @link(TSocksBlockSocket.SocksIP). (By CONNECT method of SOCKS.)
  832. If you use HTTP-tunnel mode, activate outgoing TCP connection by HTTP
  833. tunnel specified in @link(HTTPTunnelIP). (By CONNECT method of HTTP
  834. protocol.)
  835. Note: If you call this on non-created socket, then socket is created
  836. automaticly.}
  837. procedure Connect(IP, Port: string); override;
  838. {:If you need upgrade existing TCP connection to SSL/TLS (or SSH2, if plugin
  839. allows it) mode, then call this method. This method switch this class to
  840. SSL mode and do SSL/TSL handshake.}
  841. procedure SSLDoConnect;
  842. {:By this method you can downgrade existing SSL/TLS connection to normal TCP
  843. connection.}
  844. procedure SSLDoShutdown;
  845. {:If you need use this component as SSL/TLS TCP server, then after accepting
  846. of inbound connection you need start SSL/TLS session by this method. Before
  847. call this function, you must have assigned all neeeded certificates and
  848. keys!}
  849. function SSLAcceptConnection: Boolean;
  850. {:See @link(TBlockSocket.GetLocalSinIP)}
  851. function GetLocalSinIP: string; override;
  852. {:See @link(TBlockSocket.GetRemoteSinIP)}
  853. function GetRemoteSinIP: string; override;
  854. {:See @link(TBlockSocket.GetLocalSinPort)}
  855. function GetLocalSinPort: Integer; override;
  856. {:See @link(TBlockSocket.GetRemoteSinPort)}
  857. function GetRemoteSinPort: Integer; override;
  858. {:See @link(TBlockSocket.SendBuffer)}
  859. function SendBuffer(Buffer: TMemory; Length: Integer): Integer; override;
  860. {:See @link(TBlockSocket.RecvBuffer)}
  861. function RecvBuffer(Buffer: TMemory; Len: Integer): Integer; override;
  862. {:Return value of socket type. For TCP return SOCK_STREAM.}
  863. function GetSocketType: integer; override;
  864. {:Return value of protocol type for socket creation. For TCP return
  865. IPPROTO_TCP.}
  866. function GetSocketProtocol: integer; override;
  867. {:Class implementing SSL/TLS support. It is allways some descendant
  868. of @link(TCustomSSL) class. When programmer not select some SSL plugin
  869. class, then is used @link(TSSLNone)}
  870. property SSL: TCustomSSL read FSSL;
  871. {:@True if is used HTTP tunnel mode.}
  872. property HTTPTunnel: Boolean read FHTTPTunnel;
  873. published
  874. {:Return descriptive string for @link(LastError). On case of error
  875. in SSL/TLS subsystem, it returns right error description.}
  876. function GetErrorDescEx: string; override;
  877. {:Specify IP address of HTTP proxy. Assingning non-empty value to this
  878. property enable HTTP-tunnel mode. This mode is for tunnelling any outgoing
  879. TCP connection through HTTP proxy server. (If policy on HTTP proxy server
  880. allow this!) Warning: You cannot combine this mode with SOCK5 mode!}
  881. property HTTPTunnelIP: string read FHTTPTunnelIP Write FHTTPTunnelIP;
  882. {:Specify port of HTTP proxy for HTTP-tunneling.}
  883. property HTTPTunnelPort: string read FHTTPTunnelPort Write FHTTPTunnelPort;
  884. {:Specify authorisation username for access to HTTP proxy in HTTP-tunnel
  885. mode. If you not need authorisation, then let this property empty.}
  886. property HTTPTunnelUser: string read FHTTPTunnelUser Write FHTTPTunnelUser;
  887. {:Specify authorisation password for access to HTTP proxy in HTTP-tunnel
  888. mode.}
  889. property HTTPTunnelPass: string read FHTTPTunnelPass Write FHTTPTunnelPass;
  890. {:Specify timeout for communication with HTTP proxy in HTTPtunnel mode.}
  891. property HTTPTunnelTimeout: integer read FHTTPTunnelTimeout Write FHTTPTunnelTimeout;
  892. {:This event is called after sucessful TCP socket connection.}
  893. property OnAfterConnect: THookAfterConnect read FOnAfterConnect write FOnAfterConnect;
  894. end;
  895. {:@abstract(Datagram based communication)
  896. This class implementing datagram based communication instead default stream
  897. based communication style.}
  898. TDgramBlockSocket = class(TSocksBlockSocket)
  899. public
  900. {:Fill @link(TBlockSocket.RemoteSin) structure. This address is used for
  901. sending data.}
  902. procedure Connect(IP, Port: string); override;
  903. {:Silently redirected to @link(TBlockSocket.SendBufferTo).}
  904. function SendBuffer(Buffer: TMemory; Length: Integer): Integer; override;
  905. {:Silently redirected to @link(TBlockSocket.RecvBufferFrom).}
  906. function RecvBuffer(Buffer: TMemory; Length: Integer): Integer; override;
  907. end;
  908. {:@abstract(Implementation of UDP socket.)
  909. NOTE: in this class is all receiving redirected to RecvBufferFrom. You can
  910. use for reading any receive function. Preffered is RecvPacket! Similary all
  911. sending is redirected to SendbufferTo. You can use for sending UDP packet any
  912. sending function, like SendString.
  913. Supported features: IPv4, IPv6, unicasts, broadcasts, multicasts, SOCKS5
  914. proxy (only unicasts! Outgoing and incomming.)}
  915. TUDPBlockSocket = class(TDgramBlockSocket)
  916. protected
  917. FSocksControlSock: TTCPBlockSocket;
  918. function UdpAssociation: Boolean;
  919. procedure SetMulticastTTL(TT

Large files files are truncated, but you can click here to view the full file