IdStack.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.7 1/17/2005 7:25:48 PM JPMugaas
  18. Moved some stack management code here to so that we can reuse it in
  19. non-TIdComponent classes.
  20. Made HostToNetwork and NetworkToHost byte order overload functions for IPv6
  21. addresses.
  22. Rev 1.6 10/26/2004 8:12:30 PM JPMugaas
  23. Now uses TIdStrings and TIdStringList for portability.
  24. Rev 1.5 6/30/2004 12:41:14 PM BGooijen
  25. Added SetStackClass
  26. Rev 1.4 6/11/2004 8:28:50 AM DSiders
  27. Added "Do not Localize" comments.
  28. Rev 1.3 4/18/04 2:45:38 PM RLebeau
  29. Conversion support for Int64 values
  30. Rev 1.2 2004.03.07 11:45:22 AM czhower
  31. Flushbuffer fix + other minor ones found
  32. Rev 1.1 3/6/2004 5:16:20 PM JPMugaas
  33. Bug 67 fixes. Do not write to const values.
  34. Rev 1.0 2004.02.03 3:14:42 PM czhower
  35. Move and updates
  36. Rev 1.39 2/1/2004 6:10:50 PM JPMugaas
  37. GetSockOpt.
  38. Rev 1.38 2/1/2004 3:28:24 AM JPMugaas
  39. Changed WSGetLocalAddress to GetLocalAddress and moved into IdStack since
  40. that will work the same in the DotNET as elsewhere. This is required to
  41. reenable IPWatch.
  42. Rev 1.37 2/1/2004 1:54:56 AM JPMugaas
  43. Missapplied fix. IP 0.0.0.0 should now be accepted.
  44. Rev 1.36 1/31/2004 4:39:12 PM JPMugaas
  45. Removed empty methods.
  46. Rev 1.35 1/31/2004 1:13:04 PM JPMugaas
  47. Minor stack changes required as DotNET does support getting all IP addresses
  48. just like the other stacks.
  49. Rev 1.34 2004.01.22 5:59:10 PM czhower
  50. IdCriticalSection
  51. Rev 1.33 1/18/2004 11:15:52 AM JPMugaas
  52. IsIP was not handling "0" in an IP address. This caused the address
  53. "127.0.0.1" to be treated as a hostname.
  54. Rev 1.32 12/4/2003 3:14:50 PM BGooijen
  55. Added HostByAddress
  56. Rev 1.31 1/3/2004 12:21:44 AM BGooijen
  57. Added function SupportsIPv6
  58. Rev 1.30 12/31/2003 9:54:16 PM BGooijen
  59. Added IPv6 support
  60. Rev 1.29 2003.12.31 3:47:42 PM czhower
  61. Changed to use TextIsSame
  62. Rev 1.28 10/21/2003 9:24:32 PM BGooijen
  63. Started on SendTo, ReceiveFrom
  64. Rev 1.27 10/19/2003 5:21:28 PM BGooijen
  65. SetSocketOption
  66. Rev 1.26 10/15/2003 7:21:02 PM DSiders
  67. Added resource strings in TIdStack.Make.
  68. Rev 1.25 2003.10.11 5:51:02 PM czhower
  69. -VCL fixes for servers
  70. -Chain suport for servers (Super core)
  71. -Scheduler upgrades
  72. -Full yarn support
  73. Rev 1.24 10/5/2003 9:55:30 PM BGooijen
  74. TIdTCPServer works on D7 and DotNet now
  75. Rev 1.23 04/10/2003 22:31:56 HHariri
  76. moving of WSNXXX method to IdStack and renaming of the DotNet ones
  77. Rev 1.22 10/2/2003 7:31:18 PM BGooijen
  78. .net
  79. Rev 1.21 10/2/2003 6:05:16 PM GGrieve
  80. DontNet
  81. Rev 1.20 2003.10.02 10:16:30 AM czhower
  82. .Net
  83. Rev 1.19 2003.10.01 9:11:20 PM czhower
  84. .Net
  85. Rev 1.18 2003.10.01 5:05:16 PM czhower
  86. .Net
  87. Rev 1.17 2003.10.01 2:30:40 PM czhower
  88. .Net
  89. Rev 1.16 2003.10.01 12:30:08 PM czhower
  90. .Net
  91. Rev 1.14 2003.10.01 1:37:36 AM czhower
  92. .Net
  93. Rev 1.12 9/30/2003 7:15:46 PM BGooijen
  94. IdCompilerDefines.inc is included now
  95. Rev 1.11 2003.09.30 1:23:04 PM czhower
  96. Stack split for DotNet
  97. }
  98. unit IdStack;
  99. interface
  100. {$I IdCompilerDefines.inc}
  101. uses
  102. Classes,
  103. IdException, IdStackConsts, IdGlobal, SysUtils;
  104. type
  105. EIdSocketError = class(EIdException)
  106. protected
  107. FLastError: Integer;
  108. public
  109. // Params must be in this order to avoid conflict with CreateHelp
  110. // constructor in CBuilder as CB does not differentiate constructors
  111. // by name as Delphi does
  112. constructor CreateError(const AErr: Integer; const AMsg: string); virtual;
  113. //
  114. property LastError: Integer read FLastError;
  115. end;
  116. { resolving hostnames }
  117. EIdStackError = class (EIdException);
  118. EIdIPVersionUnsupported = class (EIdStackError);
  119. {$IFDEF UNIX}
  120. EIdResolveError = class(EIdSocketError);
  121. EIdReverseResolveError = class(EIdSocketError);
  122. EIdMaliciousPtrRecord = class(EIdReverseResolveError);
  123. {$ELSE}
  124. EIdMaliciousPtrRecord = class(EIdSocketError);
  125. {$ENDIF}
  126. EIdNotASocket = class(EIdSocketError);
  127. // TODO: move this to IdStackVCLPosix...
  128. {$IF DEFINED(USE_VCL_POSIX) AND DEFINED(ANDROID)}
  129. EIdAndroidPermissionNeeded = class(EIdSocketError);
  130. EIdInternetPermissionNeeded = class(EIdAndroidPermissionNeeded);
  131. {$IFEND}
  132. TIdServeFile = function(ASocket: TIdStackSocketHandle; const AFileName: string): Int64;
  133. TIdPacketInfo = class
  134. protected
  135. FSourceIP: String;
  136. FSourcePort : TIdPort;
  137. FSourceIF: UInt32;
  138. FSourceIPVersion: TIdIPVersion;
  139. FDestIP: String;
  140. FDestPort : TIdPort;
  141. FDestIF: UInt32;
  142. FDestIPVersion: TIdIPVersion;
  143. FTTL: Byte;
  144. public
  145. procedure Reset;
  146. property TTL : Byte read FTTL write FTTL;
  147. //The computer that sent it to you
  148. property SourceIP : String read FSourceIP write FSourceIP;
  149. property SourcePort : TIdPort read FSourcePort write FSourcePort;
  150. property SourceIF : UInt32 read FSourceIF write FSourceIF;
  151. property SourceIPVersion : TIdIPVersion read FSourceIPVersion write FSourceIPVersion;
  152. //you, the receiver - this is provided for multihomed machines
  153. property DestIP : String read FDestIP write FDestIP;
  154. property DestPort : TIdPort read FDestPort write FDestPort;
  155. property DestIF : UInt32 read FDestIF write FDestIF;
  156. property DestIPVersion : TIdIPVersion read FDestIPVersion write FDestIPVersion;
  157. end;
  158. TIdSocketListClass = class of TIdSocketList;
  159. // Descend from only TObject. This objects is created a lot and should be fast
  160. // and small
  161. TIdSocketList = class(TObject)
  162. protected
  163. FLock: TIdCriticalSection;
  164. //
  165. function GetItem(AIndex: Integer): TIdStackSocketHandle; virtual; abstract;
  166. public
  167. constructor Create; virtual;
  168. destructor Destroy; override;
  169. procedure Add(AHandle: TIdStackSocketHandle); virtual; abstract;
  170. function Clone: TIdSocketList; virtual; abstract;
  171. function Count: Integer; virtual; abstract;
  172. class function CreateSocketList: TIdSocketList;
  173. property Items[AIndex: Integer]: TIdStackSocketHandle read GetItem; default;
  174. procedure Remove(AHandle: TIdStackSocketHandle); virtual; abstract;
  175. procedure Clear; virtual; abstract;
  176. function ContainsSocket(AHandle: TIdStackSocketHandle): boolean; virtual; abstract;
  177. procedure Lock;
  178. class function Select(AReadList: TIdSocketList; AWriteList: TIdSocketList;
  179. AExceptList: TIdSocketList; const ATimeout: Integer = IdTimeoutInfinite): Boolean; virtual;
  180. function SelectRead(const ATimeout: Integer = IdTimeoutInfinite): Boolean; virtual; abstract;
  181. function SelectReadList(var VSocketList: TIdSocketList; const ATimeout: Integer = IdTimeoutInfinite): Boolean; virtual; abstract;
  182. procedure Unlock;
  183. end;
  184. TIdStackLocalAddress = class(TCollectionItem)
  185. protected
  186. FIPVersion: TIdIPVersion;
  187. FIPAddress: String;
  188. FInterfaceName: String;
  189. FInterfaceIndex: UInt32;
  190. FDescription: String;
  191. FFriendlyName: String;
  192. public
  193. constructor Create(ACollection: TCollection; const AIPVersion: TIdIPVersion; const AIPAddress: string); reintroduce;
  194. property IPVersion: TIdIPVersion read FIPVersion;
  195. property IPAddress: String read FIPAddress;
  196. property InterfaceName: String read FInterfaceName;
  197. property InterfaceIndex: UInt32 read FInterfaceIndex;
  198. property Description: String read FDescription;
  199. property FriendlyName: String read FFriendlyName;
  200. end;
  201. TIdStackLocalAddressIPv4 = class(TIdStackLocalAddress)
  202. protected
  203. FSubNetMask: String;
  204. FBroadcastIP: String;
  205. public
  206. constructor Create(ACollection: TCollection; const AIPAddress: string; const ASubNetMask: string = ''; const ABroadcastIP: string = ''); reintroduce;
  207. property SubNetMask: String read FSubNetMask;
  208. property BroadcastIP: String read FBroadcastIP;
  209. end;
  210. TIdStackLocalAddressIPv6 = class(TIdStackLocalAddress)
  211. public
  212. constructor Create(ACollection: TCollection; const AIPAddress: string); reintroduce;
  213. end;
  214. TIdStackLocalAddressList = class(TCollection)
  215. protected
  216. function GetAddress(AIndex: Integer): TIdStackLocalAddress;
  217. public
  218. constructor Create; reintroduce;
  219. function FirstIPOf(AIPVersion: TIdIPVersion): TIdStackLocalAddress;
  220. function HasAnyIPOf(AIPVersion: TIdIPVersion): Boolean;
  221. function IndexOfIP(const AIP: String): Integer; overload;
  222. function IndexOfIP(const AIP: String; AIPVersion: TIdIPVersion): Integer; overload;
  223. property Addresses[AIndex: Integer]: TIdStackLocalAddress read GetAddress; default;
  224. end;
  225. TIdStack = class(TObject)
  226. protected
  227. procedure IPVersionUnsupported;
  228. function HostByName(const AHostName: string;
  229. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string; virtual; abstract;
  230. function ReadHostName: string; virtual; abstract;
  231. function GetLocalAddress: string;
  232. public
  233. function Accept(ASocket: TIdStackSocketHandle; var VIP: string; var VPort: TIdPort): TIdStackSocketHandle; overload;
  234. function Accept(ASocket: TIdStackSocketHandle; var VIP: string; var VPort: TIdPort;
  235. var VIPVersion: TIdIPVersion): TIdStackSocketHandle; overload; virtual; abstract;
  236. procedure Bind(ASocket: TIdStackSocketHandle; const AIP: string;
  237. const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION ); virtual; abstract;
  238. procedure Connect(const ASocket: TIdStackSocketHandle; const AIP: string;
  239. const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  240. constructor Create; virtual;
  241. destructor Destroy; override;
  242. procedure Disconnect(ASocket: TIdStackSocketHandle); virtual; abstract;
  243. function IOControl(const s: TIdStackSocketHandle; const cmd: UInt32;
  244. var arg: UInt32): Integer; virtual; abstract;
  245. class procedure IncUsage; //create stack if necessary and inc counter
  246. class procedure DecUsage; //decrement counter and free if it gets to zero
  247. procedure GetPeerName(ASocket: TIdStackSocketHandle; var VIP: string;
  248. var VPort: TIdPort); overload;
  249. procedure GetPeerName(ASocket: TIdStackSocketHandle; var VIP: string;
  250. var VPort: TIdPort; var VIPVersion: TIdIPVersion); overload; virtual; abstract;
  251. procedure GetSocketName(ASocket: TIdStackSocketHandle; var VIP: string;
  252. var VPort: TIdPort); overload;
  253. procedure GetSocketName(ASocket: TIdStackSocketHandle; var VIP: string;
  254. var VPort: TIdPort; var VIPVersion: TIdIPVersion); overload; virtual; abstract;
  255. function HostByAddress(const AAddress: string;
  256. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string; virtual; abstract;
  257. function HostToNetwork(AValue: UInt16): UInt16; overload; virtual; abstract;
  258. function HostToNetwork(AValue: UInt32): UInt32; overload; virtual; abstract;
  259. function HostToNetwork(AValue: UInt64): UInt64; overload; virtual; abstract;
  260. function HostToNetwork(const AValue: TIdIPv6Address): TIdIPv6Address; overload; virtual;
  261. function IsIP(AIP: string): Boolean;
  262. procedure Listen(ASocket: TIdStackSocketHandle; ABackLog: Integer); virtual; abstract;
  263. function WSGetLastError: Integer; virtual; abstract;
  264. procedure WSSetLastError(const AErr : Integer); virtual; abstract;
  265. function WSTranslateSocketErrorMsg(const AErr: integer): string; virtual;
  266. function CheckForSocketError(const AResult: Integer): Integer; overload;
  267. function CheckForSocketError(const AResult: Integer; const AIgnore: array of Integer): Integer; overload;
  268. procedure RaiseLastSocketError;
  269. procedure RaiseSocketError(AErr: integer); virtual;
  270. function NewSocketHandle(const ASocketType: TIdSocketType; const AProtocol: TIdSocketProtocol;
  271. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION; const ANonBlocking: Boolean = False)
  272. : TIdStackSocketHandle; virtual; abstract;
  273. function NetworkToHost(AValue: UInt16): UInt16; overload; virtual; abstract;
  274. function NetworkToHost(AValue: UInt32): UInt32; overload; virtual; abstract;
  275. function NetworkToHost(AValue: UInt64): UInt64; overload; virtual; abstract;
  276. function NetworkToHost(const AValue: TIdIPv6Address): TIdIPv6Address; overload; virtual;
  277. procedure GetSocketOption(ASocket: TIdStackSocketHandle;
  278. ALevel: TIdSocketOptionLevel; AOptName: TIdSocketOption;
  279. out AOptVal: Integer); overload; virtual; abstract;
  280. procedure SetSocketOption(ASocket: TIdStackSocketHandle; ALevel: TIdSocketOptionLevel;
  281. AOptName: TIdSocketOption; AOptVal: Integer); overload; virtual; abstract;
  282. function ResolveHost(const AHost: string;
  283. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string;
  284. // Result:
  285. // > 0: Number of bytes received
  286. // 0: Connection closed gracefully
  287. // Will raise exceptions in other cases
  288. function Receive(ASocket: TIdStackSocketHandle; var VBuffer: TIdBytes): Integer; virtual; abstract;
  289. function Send(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  290. const AOffset: Integer = 0; const ASize: Integer = -1): Integer; virtual; abstract;
  291. function ReceiveFrom(ASocket: TIdStackSocketHandle; var VBuffer: TIdBytes;
  292. var VIP: string; var VPort: TIdPort; var VIPVersion: TIdIPVersion): Integer; virtual; abstract;
  293. function SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  294. const AOffset: Integer; const AIP: string; const APort: TIdPort;
  295. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): Integer; overload;
  296. function SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  297. const AOffset: Integer; const ASize: Integer; const AIP: string;
  298. const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION)
  299. : Integer; overload; virtual; abstract;
  300. function ReceiveMsg(ASocket: TIdStackSocketHandle; var VBuffer: TIdBytes;
  301. APkt: TIdPacketInfo): UInt32; virtual; abstract;
  302. function SupportsIPv4: Boolean; virtual; abstract;
  303. function SupportsIPv6: Boolean; virtual; abstract;
  304. //multicast stuff Kudzu permitted me to add here.
  305. function IsValidIPv4MulticastGroup(const Value: string): Boolean;
  306. function IsValidIPv6MulticastGroup(const Value: string): Boolean;
  307. procedure SetKeepAliveValues(ASocket: TIdStackSocketHandle;
  308. const AEnabled: Boolean; const ATimeMS, AInterval: Integer); virtual;
  309. procedure SetMulticastTTL(AHandle: TIdStackSocketHandle;
  310. const AValue : Byte; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  311. procedure SetLoopBack(AHandle: TIdStackSocketHandle; const AValue: Boolean;
  312. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  313. procedure DropMulticastMembership(AHandle: TIdStackSocketHandle;
  314. const AGroupIP, ALocalIP : String; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  315. procedure AddMulticastMembership(AHandle: TIdStackSocketHandle;
  316. const AGroupIP, ALocalIP : String; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  317. //I know this looks like an odd place to put a function for calculating a
  318. //packet checksum. There is a reason for it though. The reason is that
  319. //you need it for ICMPv6 and in Windows, you do that with some other stuff
  320. //in the stack descendants
  321. function CalcCheckSum(const AData : TIdBytes): UInt16; virtual;
  322. //In Windows, this writes a checksum into a buffer. In Linux, it would probably
  323. //simply have the kernal write the checksum with something like this (RFC 2292):
  324. //
  325. // int offset = 2;
  326. // setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, sizeof(offset));
  327. //
  328. // Note that this should be called
  329. //IMMEDIATELY before you do a SendTo because the Local IPv6 address might change
  330. procedure WriteChecksum(s : TIdStackSocketHandle;
  331. var VBuffer : TIdBytes; const AOffset : Integer; const AIP : String;
  332. const APort : TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  333. //
  334. procedure GetLocalAddressList(AAddresses: TIdStackLocalAddressList); virtual; abstract;
  335. //
  336. // Properties
  337. //
  338. property HostName: string read ReadHostName;
  339. property LocalAddress: string read GetLocalAddress; // deprecated 'use GetLocalAddressList()';
  340. end;
  341. TIdStackClass = class of TIdStack;
  342. var
  343. GStack: TIdStack = nil;
  344. GServeFileProc: TIdServeFile = nil;
  345. GSocketListClass: TIdSocketListClass;
  346. // Procedures
  347. procedure SetStackClass( AStackClass: TIdStackClass );
  348. // TODO: move this to IdStackVCLPosix...
  349. {$IF DEFINED(USE_VCL_POSIX) AND DEFINED(ANDROID)}
  350. function HasAndroidPermission(const Permission: string): Boolean;
  351. {$IFEND}
  352. implementation
  353. {$I IdOptimizationsOff.inc}
  354. uses
  355. //done this way so we can have a separate stack for FPC under Unix systems
  356. {$IF DEFINED(WINDOWS)}
  357. {$IFDEF USE_INLINE}
  358. Windows,
  359. {$ENDIF}
  360. IdStackWindows,
  361. {$ELSEIF DEFINED(USE_VCL_POSIX)}
  362. IdStackVCLPosix,
  363. {$ELSEIF DEFINED(UNIX)}
  364. {$IF DEFINED(KYLIXCOMPAT)}
  365. IdStackLibc,
  366. {$ELSEIF DEFINED(USE_BASEUNIX)}
  367. IdStackUnix,
  368. {$IFEND}
  369. {$IFEND}
  370. // TODO: move this to IdStackVCLPosix...
  371. {$IF DEFINED(USE_VCL_POSIX) AND DEFINED(ANDROID)}
  372. {$IF (NOT DEFINED(DCC_XE6_OR_ABOVE)) OR DEFINED(DCC_10_0_OR_ABOVE)}
  373. // StringToJString() is here in XE5
  374. // StringToJString() is inline in Seattle and later
  375. Androidapi.JNI.JavaTypes,
  376. {$ENDIF}
  377. {$IFNDEF DCC_XE7_OR_ABOVE}
  378. // SharedActivityContext() is here in XE5 and XE6
  379. FMX.Helpers.Android,
  380. {$ENDIF}
  381. {$IFDEF DCC_XE6_OR_ABOVE}
  382. // StringToJString() was moved here in XE6
  383. // SharedActivityContext() was moved here in XE7
  384. // TAndroidHelper was added here in Seattle
  385. Androidapi.Helpers,
  386. {$ENDIF}
  387. Androidapi.JNI.GraphicsContentViewText,
  388. {$IFEND}
  389. IdResourceStrings;
  390. var
  391. GStackClass: TIdStackClass = nil;
  392. var
  393. {$IFNDEF USE_OBJECT_ARC}
  394. GInstanceCount: UInt32 = 0;
  395. {$ENDIF}
  396. GStackCriticalSection: TIdCriticalSection = nil;
  397. //for IPv4 Multicast address chacking
  398. const
  399. IPv4MCastLo = 224;
  400. IPv4MCastHi = 239;
  401. procedure SetStackClass(AStackClass: TIdStackClass);
  402. begin
  403. GStackClass := AStackClass;
  404. end;
  405. procedure TIdPacketInfo.Reset;
  406. begin
  407. FSourceIP := '';
  408. FSourcePort := 0;
  409. FSourceIF := 0;
  410. FSourceIPVersion := ID_DEFAULT_IP_VERSION;
  411. FDestIP := '';
  412. FDestPort:= 0;
  413. FDestIF := 0;
  414. FDestIPVersion := ID_DEFAULT_IP_VERSION;
  415. FTTL := 0;
  416. end;
  417. { TIdSocketList }
  418. constructor TIdSocketList.Create;
  419. begin
  420. inherited Create;
  421. FLock := TIdCriticalSection.Create;
  422. end;
  423. class function TIdSocketList.CreateSocketList: TIdSocketList;
  424. Begin
  425. Result := GSocketListClass.Create;
  426. End;
  427. destructor TIdSocketList.Destroy;
  428. begin
  429. FLock.Free;
  430. inherited Destroy;
  431. end;
  432. procedure TIdSocketList.Lock;
  433. begin
  434. FLock.Acquire;
  435. end;
  436. class function TIdSocketList.Select(AReadList, AWriteList,
  437. AExceptList: TIdSocketList; const ATimeout: Integer): Boolean;
  438. begin
  439. // C++ Builder cannot have abstract class functions thus we need this base
  440. Result := False;
  441. end;
  442. procedure TIdSocketList.Unlock;
  443. begin
  444. FLock.Release;
  445. end;
  446. { EIdSocketError }
  447. constructor EIdSocketError.CreateError(const AErr: Integer; const AMsg: string);
  448. begin
  449. inherited Create(AMsg);
  450. FLastError := AErr;
  451. end;
  452. { TIdStackLocalAddressList }
  453. constructor TIdStackLocalAddress.Create(ACollection: TCollection; const AIPVersion: TIdIPVersion; const AIPAddress: string);
  454. begin
  455. inherited Create(ACollection);
  456. FIPVersion := AIPVersion;
  457. FIPAddress := AIPAddress;
  458. FInterfaceIndex := 0;
  459. end;
  460. constructor TIdStackLocalAddressIPv4.Create(ACollection: TCollection; const AIPAddress: string;
  461. const ASubNetMask: string = ''; const ABroadcastIP: string = '');
  462. begin
  463. inherited Create(ACollection, Id_IPv4, AIPAddress);
  464. FSubNetMask := ASubNetMask;
  465. FBroadcastIP := ABroadcastIP;
  466. end;
  467. constructor TIdStackLocalAddressIPv6.Create(ACollection: TCollection; const AIPAddress: string);
  468. begin
  469. inherited Create(ACollection, Id_IPv6, AIPAddress);
  470. end;
  471. constructor TIdStackLocalAddressList.Create;
  472. begin
  473. inherited Create(TIdStackLocalAddress);
  474. end;
  475. function TIdStackLocalAddressList.FirstIPOf(AIPVersion: TIdIPVersion): TIdStackLocalAddress;
  476. var
  477. I: Integer;
  478. LAddr: TIdStackLocalAddress;
  479. begin
  480. Result := nil;
  481. for I := 0 to Count-1 do begin
  482. LAddr := Addresses[I];
  483. if LAddr.IPVersion = AIPVersion then begin
  484. Result := LAddr;
  485. Exit;
  486. end;
  487. end;
  488. end;
  489. function TIdStackLocalAddressList.GetAddress(AIndex: Integer): TIdStackLocalAddress;
  490. begin
  491. Result := TIdStackLocalAddress(inherited Items[AIndex]);
  492. end;
  493. function TIdStackLocalAddressList.HasAnyIPOf(AIPVersion: TIdIPVersion): Boolean;
  494. begin
  495. Result := (FirstIPOf(AIPVersion) <> nil);
  496. end;
  497. function TIdStackLocalAddressList.IndexOfIP(const AIP: String): Integer;
  498. var
  499. I: Integer;
  500. begin
  501. Result := -1;
  502. for I := 0 to Count-1 do begin
  503. if Addresses[I].IPAddress = AIP then begin
  504. Result := I;
  505. Exit;
  506. end;
  507. end;
  508. end;
  509. function TIdStackLocalAddressList.IndexOfIP(const AIP: String; AIPVersion: TIdIPVersion): Integer;
  510. var
  511. I: Integer;
  512. LAddr: TIdStackLocalAddress;
  513. begin
  514. Result := -1;
  515. for I := 0 to Count-1 do begin
  516. LAddr := Addresses[I];
  517. if (LAddr.IPVersion = AIPVersion) and (LAddr.IPAddress = AIP) then begin
  518. Result := I;
  519. Exit;
  520. end;
  521. end;
  522. end;
  523. { TIdStack }
  524. constructor TIdStack.Create;
  525. begin
  526. // Here for .net
  527. inherited Create;
  528. end;
  529. destructor TIdStack.Destroy;
  530. begin
  531. inherited Destroy;
  532. end;
  533. procedure TIdStack.IPVersionUnsupported;
  534. begin
  535. raise EIdIPVersionUnsupported.Create(RSIPVersionUnsupported);
  536. end;
  537. function TIdStack.Accept(ASocket: TIdStackSocketHandle; var VIP: string;
  538. var VPort: TIdPort): TIdStackSocketHandle;
  539. var
  540. LIPVersion: TIdIPVersion;
  541. begin
  542. Result := Accept(ASocket, VIP, VPort, LIPVersion);
  543. end;
  544. procedure TIdStack.GetPeerName(ASocket: TIdStackSocketHandle; var VIP: string;
  545. var VPort: TIdPort);
  546. var
  547. LIPVersion: TIdIPVersion;
  548. begin
  549. GetPeerName(ASocket, VIP, VPort, LIPVersion);
  550. end;
  551. procedure TIdStack.GetSocketName(ASocket: TIdStackSocketHandle; var VIP: string;
  552. var VPort: TIdPort);
  553. var
  554. LIPVersion: TIdIPVersion;
  555. begin
  556. GetSocketName(ASocket, VIP, VPort, LIPVersion);
  557. end;
  558. function TIdStack.GetLocalAddress: string;
  559. var
  560. LList: TIdStackLocalAddressList;
  561. LAddr: TIdStackLocalAddress;
  562. I: Integer;
  563. begin
  564. Result := '';
  565. LList := TIdStackLocalAddressList.Create;
  566. try
  567. GetLocalAddressList(LList);
  568. // for backwards compatibility, return only IPv4 addresses
  569. // TODO: if connected to an IPv6-only network, return an IPv6 address
  570. LAddr := List.FirstIPOf(Id_IPv4);
  571. {if LAddr = nil then begin
  572. LAddr := List.FirstIPOf(Id_IPv6);
  573. end;}
  574. if LAddr <> nil then begin
  575. Result := LAddr.IPAddress;
  576. end;
  577. finally
  578. LList.Free;
  579. end;
  580. end;
  581. function TIdStack.IsIP(AIP: string): Boolean;
  582. var
  583. LErr: Boolean;
  584. begin
  585. // TODO: support IPv6
  586. IdGlobal.IPv4ToUInt32(AIP, LErr);
  587. Result := not LErr;
  588. end;
  589. function TIdStack.ResolveHost(const AHost: string;
  590. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string;
  591. begin
  592. Result := '';
  593. case AIPVersion of
  594. Id_IPv4: begin
  595. // Sometimes 95 forgets who localhost is
  596. if TextIsSame(AHost, 'LOCALHOST') then begin {Do not Localize}
  597. Result := '127.0.0.1'; {Do not Localize}
  598. end else if IsIP(AHost) then begin
  599. Result := AHost;
  600. end else begin
  601. Result := HostByName(AHost, Id_IPv4);
  602. end;
  603. end;
  604. Id_IPv6: begin
  605. if TextIsSame(AHost, 'LOCALHOST') then begin {Do not Localize}
  606. Result := '::1'; {Do not Localize}
  607. end else begin
  608. Result := IdGlobal.MakeCanonicalIPv6Address(AHost);
  609. if Result = '' then begin
  610. Result := HostByName(AHost, Id_IPv6);
  611. end;
  612. end;
  613. end;
  614. else begin
  615. IPVersionUnsupported;
  616. end;
  617. end;
  618. end;
  619. function TIdStack.SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  620. const AOffset: Integer; const AIP: string; const APort: TIdPort;
  621. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): Integer;
  622. begin
  623. Result := SendTo(ASocket, ABuffer, AOffset, -1, AIP, APort, AIPVersion);
  624. end;
  625. class procedure TIdStack.DecUsage;
  626. var
  627. // under ARC, increment the lock's reference count before working with it
  628. LLock: TIdCriticalSection;
  629. begin
  630. LLock := GStackCriticalSection;
  631. if not Assigned(LLock) then begin
  632. raise EIdStackError.Create('GStackCriticalSection is nil in TIdStack.DecUsage'); {do not localize}
  633. end;
  634. LLock.Acquire;
  635. try
  636. // This CS will guarantee that during the FreeAndNil nobody
  637. // will try to use or construct GStack
  638. {$IFDEF USE_OBJECT_ARC}
  639. if GStack <> nil then begin
  640. if GStack.__ObjRelease = 0 then begin
  641. Pointer(GStack) := nil;
  642. end;
  643. end;
  644. {$ELSE}
  645. if GInstanceCount > 0 then begin
  646. Dec(GInstanceCount);
  647. if GInstanceCount = 0 then begin
  648. FreeAndNil(GStack);
  649. end;
  650. end;
  651. {$ENDIF}
  652. finally
  653. LLock.Release;
  654. end;
  655. end;
  656. class procedure TIdStack.IncUsage;
  657. var
  658. // under ARC, increment the lock's reference count before working with it
  659. LLock: TIdCriticalSection;
  660. begin
  661. LLock := GStackCriticalSection;
  662. if not Assigned(LLock) then begin
  663. raise EIdStackError.Create('GStackCriticalSection is nil in TIdStack.IncUsage'); {do not localize}
  664. end;
  665. LLock.Acquire;
  666. try
  667. {$IFDEF USE_OBJECT_ARC}
  668. if GStack = nil then begin
  669. if GStackClass = nil then begin
  670. raise EIdStackError.Create(RSStackClassUndefined);
  671. end;
  672. GStack := GStackClass.Create;
  673. end else begin
  674. GStack.__ObjAddRef;
  675. end;
  676. {$ELSE}
  677. if GInstanceCount = 0 then begin
  678. if GStack <> nil then begin
  679. raise EIdStackError.Create(RSStackAlreadyCreated);
  680. end;
  681. if GStackClass = nil then begin
  682. raise EIdStackError.Create(RSStackClassUndefined);
  683. end;
  684. GStack := GStackClass.Create;
  685. end;
  686. Inc(GInstanceCount);
  687. {$ENDIF}
  688. finally
  689. LLock.Release;
  690. end;
  691. end;
  692. function TIdStack.CheckForSocketError(const AResult: Integer): Integer;
  693. begin
  694. if AResult = Integer(Id_SOCKET_ERROR) then begin
  695. RaiseLastSocketError;
  696. end;
  697. Result := AResult;
  698. end;
  699. function TIdStack.CheckForSocketError(const AResult: Integer;
  700. const AIgnore: array of integer): Integer;
  701. var
  702. i: Integer;
  703. LLastError: Integer;
  704. begin
  705. Result := AResult;
  706. if AResult = Integer(Id_SOCKET_ERROR) then begin
  707. LLastError := WSGetLastError;
  708. for i := Low(AIgnore) to High(AIgnore) do begin
  709. if LLastError = AIgnore[i] then begin
  710. Result := LLastError;
  711. Exit;
  712. end;
  713. end;
  714. RaiseSocketError(LLastError);
  715. end;
  716. end;
  717. procedure TIdStack.RaiseLastSocketError;
  718. begin
  719. RaiseSocketError(WSGetLastError);
  720. end;
  721. // TODO: move this to IdStackVCLPosix...
  722. {$IF DEFINED(USE_VCL_POSIX) AND DEFINED(ANDROID)}
  723. function GetActivityContext: JContext; {$IFDEF USE_INLINE}inline;{$ENDIF}
  724. begin
  725. {$IFDEF HAS_TAndroidHelper}
  726. Result := TAndroidHelper.Context;
  727. {$ELSE}
  728. Result := SharedActivityContext;
  729. {$ENDIF}
  730. end;
  731. function HasAndroidPermission(const Permission: string): Boolean;
  732. begin
  733. Result := GetActivityContext.checkCallingOrSelfPermission(StringToJString(Permission)) = TJPackageManager.JavaClass.PERMISSION_GRANTED;
  734. end;
  735. {$IFEND}
  736. procedure TIdStack.RaiseSocketError(AErr: integer);
  737. begin
  738. (*
  739. RRRRR EEEEEE AAAA DDDDD MM MM EEEEEE !! !! !!
  740. RR RR EE AA AA DD DD MMMM MMMM EE !! !! !!
  741. RRRRR EEEE AAAAAA DD DD MM MMM MM EEEE !! !! !!
  742. RR RR EE AA AA DD DD MM MM EE
  743. RR RR EEEEEE AA AA DDDDD MM MM EEEEEE .. .. ..
  744. Please read the note in the next comment.
  745. *)
  746. if AErr = Id_WSAENOTSOCK then begin
  747. // You can add this to your exception ignore list for easier debugging.
  748. // However please note that sometimes it is a true error. Your program
  749. // will still run correctly, but the debugger will not stop on it if you
  750. // list it in the ignore list. But for most times its fine to put it in
  751. // the ignore list, it only affects your debugging.
  752. raise EIdNotASocket.CreateError(AErr, WSTranslateSocketErrorMsg(AErr));
  753. end;
  754. // TODO: move this to IdStackVCLPosix...
  755. {$IF DEFINED(USE_VCL_POSIX) AND DEFINED(ANDROID)}
  756. if (AErr = 9{EBADF}) or (AErr = 12{EBADR?}) or (AErr = 13{EACCES}) then begin
  757. if not HasAndroidPermission('android.permission.INTERNET') then begin {Do not Localize}
  758. raise EIdInternetPermissionNeeded.CreateError(AErr, WSTranslateSocketErrorMsg(AErr));
  759. end;
  760. end;
  761. {$IFEND}
  762. (*
  763. It is normal to receive a 10038 exception (10038, NOT others!) here when
  764. *shutting down* (NOT at other times!) servers (NOT clients!).
  765. If you receive a 10038 exception here please see the FAQ at:
  766. http://www.IndyProject.org/
  767. If you insist upon requesting help via our email boxes on the 10038 error
  768. that is already answered in the FAQ and you are simply too slothful to
  769. search for your answer and ask your question in the public forums you may be
  770. publicly flogged, tarred and feathered and your name may be added to every
  771. chain letter / EMail in existence today."
  772. Otherwise, if you DID read the FAQ and have further questions, please feel
  773. free to ask using one of the methods (Carefullly note that these methods do
  774. not list email) listed on the Tech Support link at:
  775. http://www.IndyProject.org/
  776. RRRRR EEEEEE AAAA DDDDD MM MM EEEEEE !! !! !!
  777. RR RR EE AA AA DD DD MMMM MMMM EE !! !! !!
  778. RRRRR EEEE AAAAAA DD DD MM MMM MM EEEE !! !! !!
  779. RR RR EE AA AA DD DD MM MM EE
  780. RR RR EEEEEE AA AA DDDDD MM MM EEEEEE .. .. ..
  781. *)
  782. raise EIdSocketError.CreateError(AErr, WSTranslateSocketErrorMsg(AErr));
  783. end;
  784. function TIdStack.WSTranslateSocketErrorMsg(const AErr: integer): string;
  785. begin
  786. Result := ''; {Do not Localize}
  787. case AErr of
  788. Id_WSAEINTR: Result := RSStackEINTR;
  789. Id_WSAEBADF: Result := RSStackEBADF;
  790. Id_WSAEACCES: Result := RSStackEACCES;
  791. Id_WSAEFAULT: Result := RSStackEFAULT;
  792. Id_WSAEINVAL: Result := RSStackEINVAL;
  793. Id_WSAEMFILE: Result := RSStackEMFILE;
  794. Id_WSAEWOULDBLOCK: Result := RSStackEWOULDBLOCK;
  795. Id_WSAEINPROGRESS: Result := RSStackEINPROGRESS;
  796. Id_WSAEALREADY: Result := RSStackEALREADY;
  797. Id_WSAENOTSOCK: Result := RSStackENOTSOCK;
  798. Id_WSAEDESTADDRREQ: Result := RSStackEDESTADDRREQ;
  799. Id_WSAEMSGSIZE: Result := RSStackEMSGSIZE;
  800. Id_WSAEPROTOTYPE: Result := RSStackEPROTOTYPE;
  801. Id_WSAENOPROTOOPT: Result := RSStackENOPROTOOPT;
  802. Id_WSAEPROTONOSUPPORT: Result := RSStackEPROTONOSUPPORT;
  803. {$IFNDEF BEOS}
  804. Id_WSAESOCKTNOSUPPORT: Result := RSStackESOCKTNOSUPPORT;
  805. {$ENDIF}
  806. Id_WSAEOPNOTSUPP: Result := RSStackEOPNOTSUPP;
  807. Id_WSAEPFNOSUPPORT: Result := RSStackEPFNOSUPPORT;
  808. Id_WSAEAFNOSUPPORT: Result := RSStackEAFNOSUPPORT;
  809. Id_WSAEADDRINUSE: Result := RSStackEADDRINUSE;
  810. Id_WSAEADDRNOTAVAIL: Result := RSStackEADDRNOTAVAIL;
  811. Id_WSAENETDOWN: Result := RSStackENETDOWN;
  812. Id_WSAENETUNREACH: Result := RSStackENETUNREACH;
  813. Id_WSAENETRESET: Result := RSStackENETRESET;
  814. Id_WSAECONNABORTED: Result := RSStackECONNABORTED;
  815. Id_WSAECONNRESET: Result := RSStackECONNRESET;
  816. Id_WSAENOBUFS: Result := RSStackENOBUFS;
  817. Id_WSAEISCONN: Result := RSStackEISCONN;
  818. Id_WSAENOTCONN: Result := RSStackENOTCONN;
  819. Id_WSAESHUTDOWN: Result := RSStackESHUTDOWN;
  820. {$IFNDEF BEOS}
  821. Id_WSAETOOMANYREFS: Result := RSStackETOOMANYREFS;
  822. {$ENDIF}
  823. Id_WSAETIMEDOUT: Result := RSStackETIMEDOUT;
  824. Id_WSAECONNREFUSED: Result := RSStackECONNREFUSED;
  825. Id_WSAELOOP: Result := RSStackELOOP;
  826. Id_WSAENAMETOOLONG: Result := RSStackENAMETOOLONG;
  827. Id_WSAEHOSTDOWN: Result := RSStackEHOSTDOWN;
  828. Id_WSAEHOSTUNREACH: Result := RSStackEHOSTUNREACH;
  829. Id_WSAENOTEMPTY: Result := RSStackENOTEMPTY;
  830. end;
  831. Result := IndyFormat(RSStackError, [AErr, Result]);
  832. end;
  833. function TIdStack.HostToNetwork(const AValue: TIdIPv6Address): TIdIPv6Address;
  834. var
  835. i : Integer;
  836. begin
  837. for i := 0 to 7 do begin
  838. Result[i] := HostToNetwork(AValue[i]);
  839. end;
  840. end;
  841. function TIdStack.NetworkToHost(const AValue: TIdIPv6Address): TIdIPv6Address;
  842. var
  843. i : Integer;
  844. begin
  845. for i := 0 to 7 do begin
  846. Result[i] := NetworkToHost(AValue[i]);
  847. end;
  848. end;
  849. function TIdStack.IsValidIPv4MulticastGroup(const Value: string): Boolean;
  850. var
  851. LIP: string;
  852. LVal: Integer;
  853. begin
  854. Result := False;
  855. if IsIP(Value) then
  856. begin
  857. LIP := Value;
  858. LVal := IndyStrToInt(Fetch(LIP, '.')); {Do not Localize}
  859. Result := (LVal >= IPv4MCastLo) and (LVal <= IPv4MCastHi);
  860. end;
  861. end;
  862. { From "rfc 2373"
  863. 2.7 Multicast Addresses
  864. An IPv6 multicast address is an identifier for a group of nodes. A
  865. node may belong to any number of multicast groups. Multicast
  866. addresses have the following format:
  867. #
  868. | 8 | 4 | 4 | 112 bits |
  869. +------ -+----+----+---------------------------------------------+
  870. |11111111|flgs|scop| group ID |
  871. +--------+----+----+---------------------------------------------+
  872. 11111111 at the start of the address identifies the address as
  873. being a multicast address.
  874. +-+-+-+-+
  875. flgs is a set of 4 flags: |0|0|0|T|
  876. +-+-+-+-+
  877. The high-order 3 flags are reserved, and must be initialized to
  878. 0.
  879. T = 0 indicates a permanently-assigned ("well-known") multicast
  880. address, assigned by the global internet numbering authority.
  881. T = 1 indicates a non-permanently-assigned ("transient")
  882. multicast address.
  883. scop is a 4-bit multicast scope value used to limit the scope of
  884. the multicast group. The values are:
  885. 0 reserved
  886. 1 node-local scope
  887. 2 link-local scope
  888. 3 (unassigned)
  889. 4 (unassigned)
  890. 5 site-local scope
  891. 6 (unassigned)
  892. 7 (unassigned)
  893. 8 organization-local scope
  894. 9 (unassigned)
  895. A (unassigned)
  896. B (unassigned)
  897. C (unassigned)
  898. D (unassigned)
  899. E global scope
  900. F reserved
  901. group ID identifies the multicast group, either permanent or
  902. transient, within the given scope.
  903. The "meaning" of a permanently-assigned multicast address is
  904. independent of the scope value. For example, if the "NTP servers
  905. group" is assigned a permanent multicast address with a group ID of
  906. 101 (hex), then:
  907. FF01:0:0:0:0:0:0:101 means all NTP servers on the same node as the
  908. sender.
  909. FF02:0:0:0:0:0:0:101 means all NTP servers on the same link as the
  910. sender.
  911. FF05:0:0:0:0:0:0:101 means all NTP servers at the same site as the
  912. sender.
  913. FF0E:0:0:0:0:0:0:101 means all NTP servers in the internet.
  914. Non-permanently-assigned multicast addresses are meaningful only
  915. within a given scope. For example, a group identified by the non-
  916. permanent, site-local multicast address FF15:0:0:0:0:0:0:101 at one
  917. site bears no relationship to a group using the same address at a
  918. different site, nor to a non-permanent group using the same group ID
  919. with different scope, nor to a permanent group with the same group
  920. ID.
  921. Multicast addresses must not be used as source addresses in IPv6
  922. packets or appear in any routing header.
  923. }
  924. function TIdStack.IsValidIPv6MulticastGroup(const Value: string): Boolean;
  925. var
  926. LTmp : String;
  927. begin
  928. LTmp := IdGlobal.MakeCanonicalIPv6Address(Value);
  929. if LTmp <> '' then
  930. begin
  931. Result := TextStartsWith(LTmp, 'FF');
  932. end else begin
  933. Result := False;
  934. end;
  935. end;
  936. function TIdStack.CalcCheckSum(const AData: TIdBytes): UInt16;
  937. var
  938. i : Integer;
  939. LSize : Integer;
  940. LCRC : UInt32;
  941. begin
  942. LCRC := 0;
  943. i := 0;
  944. LSize := Length(AData);
  945. while LSize > 1 do
  946. begin
  947. LCRC := LCRC + BytesToUInt16(AData, i);
  948. Dec(LSize, 2);
  949. Inc(i, 2);
  950. end;
  951. if LSize > 0 then begin
  952. LCRC := LCRC + AData[i];
  953. end;
  954. LCRC := (LCRC shr 16) + (LCRC and $ffff); //(LCRC >> 16)
  955. LCRC := LCRC + (LCRC shr 16);
  956. Result := not UInt16(LCRC);
  957. end;
  958. procedure TIdStack.SetKeepAliveValues(ASocket: TIdStackSocketHandle;
  959. const AEnabled: Boolean; const ATimeMS, AInterval: Integer);
  960. begin
  961. SetSocketOption(ASocket, Id_SOL_SOCKET, Id_SO_KEEPALIVE, iif(AEnabled, 1, 0));
  962. {$IF DEFINED(HAS_TCP_KEEPIDLE) OR DEFINED(HAS_TCP_KEEPINTVL)}
  963. if AEnabled then
  964. begin
  965. // TODO: support TCP_KEEPCNT
  966. {$IFDEF HAS_TCP_KEEPIDLE}
  967. SetSocketOption(ASocket, Id_SOL_TCP, Id_TCP_KEEPIDLE, ATimeMS div MSecsPerSec);
  968. {$ENDIF}
  969. {$IFDEF HAS_TCP_KEEPINTVL}
  970. SetSocketOption(ASocket, Id_SOL_TCP, Id_TCP_KEEPINTVL, AInterval div MSecsPerSec);
  971. {$ENDIF}
  972. end;
  973. {$IFEND}
  974. end;
  975. initialization
  976. //done this way so we can have a separate stack just for FPC under Unix systems
  977. GStackClass :=
  978. {$IF DEFINED(WINDOWS)}
  979. TIdStackWindows
  980. {$ELSEIF DEFINED(USE_VCL_POSIX)}
  981. TIdStackVCLPosix
  982. {$ELSEIF DEFINED(UNIX)}
  983. {$IF DEFINED(KYLIXCOMPAT)}
  984. TIdStackLibc
  985. {$ELSEIF DEFINED(USE_BASEUNIX)}
  986. TIdStackUnix
  987. {$IFEND}
  988. {$IFEND}
  989. ;
  990. GStackCriticalSection := TIdCriticalSection.Create;
  991. {$IFDEF REGISTER_EXPECTED_MEMORY_LEAK}
  992. IndyRegisterExpectedMemoryLeak(GStackCriticalSection);
  993. {$ENDIF}
  994. finalization
  995. // Dont Free. If shutdown is from another Init section, it can cause GPF when stack
  996. // tries to access it. App will kill it off anyways, so just let it leak
  997. {$IFDEF FREE_ON_FINAL}
  998. FreeAndNil(GStackCriticalSection);
  999. {$ENDIF}
  1000. {$I IdOptimizationsOn.inc}
  1001. end.