IdStack.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. {$IFDEF USE_VCL_POSIX}
  129. {$IFDEF ANDROID}
  130. EIdAndroidPermissionNeeded = class(EIdSocketError);
  131. EIdInternetPermissionNeeded = class(EIdAndroidPermissionNeeded);
  132. {$ENDIF}
  133. {$ENDIF}
  134. TIdServeFile = function(ASocket: TIdStackSocketHandle; const AFileName: string): Int64;
  135. TIdPacketInfo = class
  136. protected
  137. FSourceIP: String;
  138. FSourcePort : TIdPort;
  139. FSourceIF: UInt32;
  140. FSourceIPVersion: TIdIPVersion;
  141. FDestIP: String;
  142. FDestPort : TIdPort;
  143. FDestIF: UInt32;
  144. FDestIPVersion: TIdIPVersion;
  145. FTTL: Byte;
  146. public
  147. procedure Reset;
  148. property TTL : Byte read FTTL write FTTL;
  149. //The computer that sent it to you
  150. property SourceIP : String read FSourceIP write FSourceIP;
  151. property SourcePort : TIdPort read FSourcePort write FSourcePort;
  152. property SourceIF : UInt32 read FSourceIF write FSourceIF;
  153. property SourceIPVersion : TIdIPVersion read FSourceIPVersion write FSourceIPVersion;
  154. //you, the receiver - this is provided for multihomed machines
  155. property DestIP : String read FDestIP write FDestIP;
  156. property DestPort : TIdPort read FDestPort write FDestPort;
  157. property DestIF : UInt32 read FDestIF write FDestIF;
  158. property DestIPVersion : TIdIPVersion read FDestIPVersion write FDestIPVersion;
  159. end;
  160. TIdSocketListClass = class of TIdSocketList;
  161. // Descend from only TObject. This objects is created a lot and should be fast
  162. // and small
  163. TIdSocketList = class(TObject)
  164. protected
  165. FLock: TIdCriticalSection;
  166. //
  167. function GetItem(AIndex: Integer): TIdStackSocketHandle; virtual; abstract;
  168. public
  169. constructor Create; virtual;
  170. destructor Destroy; override;
  171. procedure Add(AHandle: TIdStackSocketHandle); virtual; abstract;
  172. function Clone: TIdSocketList; virtual; abstract;
  173. function Count: Integer; virtual; abstract;
  174. class function CreateSocketList: TIdSocketList;
  175. property Items[AIndex: Integer]: TIdStackSocketHandle read GetItem; default;
  176. procedure Remove(AHandle: TIdStackSocketHandle); virtual; abstract;
  177. procedure Clear; virtual; abstract;
  178. function ContainsSocket(AHandle: TIdStackSocketHandle): boolean; virtual; abstract;
  179. procedure Lock;
  180. class function Select(AReadList: TIdSocketList; AWriteList: TIdSocketList;
  181. AExceptList: TIdSocketList; const ATimeout: Integer = IdTimeoutInfinite): Boolean; virtual;
  182. function SelectRead(const ATimeout: Integer = IdTimeoutInfinite): Boolean; virtual; abstract;
  183. function SelectReadList(var VSocketList: TIdSocketList; const ATimeout: Integer = IdTimeoutInfinite): Boolean; virtual; abstract;
  184. procedure Unlock;
  185. end;
  186. TIdStackLocalAddress = class(TCollectionItem)
  187. protected
  188. FIPVersion: TIdIPVersion;
  189. FIPAddress: String;
  190. FInterfaceName: String;
  191. FInterfaceIndex: UInt32;
  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. end;
  199. TIdStackLocalAddressIPv4 = class(TIdStackLocalAddress)
  200. protected
  201. FSubNetMask: String;
  202. public
  203. constructor Create(ACollection: TCollection; const AIPAddress, ASubNetMask: string); reintroduce;
  204. property SubNetMask: String read FSubNetMask;
  205. // TODO: add BroadcastIP
  206. end;
  207. TIdStackLocalAddressIPv6 = class(TIdStackLocalAddress)
  208. public
  209. constructor Create(ACollection: TCollection; const AIPAddress: string); reintroduce;
  210. end;
  211. TIdStackLocalAddressList = class(TCollection)
  212. protected
  213. function GetAddress(AIndex: Integer): TIdStackLocalAddress;
  214. public
  215. constructor Create; reintroduce;
  216. function IndexOfIP(const AIP: String): Integer; overload;
  217. function IndexOfIP(const AIP: String; AIPVersion: TIdIPVersion): Integer; overload;
  218. property Addresses[AIndex: Integer]: TIdStackLocalAddress read GetAddress; default;
  219. end;
  220. TIdStack = class(TObject)
  221. protected
  222. FLocalAddresses: TStrings;
  223. //
  224. procedure IPVersionUnsupported;
  225. function HostByName(const AHostName: string;
  226. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string; virtual; abstract;
  227. function MakeCanonicalIPv6Address(const AAddr: string): string; {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'Use IdGlobal.MakeCanonicalIPv6Address()'{$ENDIF};{$ENDIF}
  228. function ReadHostName: string; virtual; abstract;
  229. function GetLocalAddress: string;
  230. function GetLocalAddresses: TStrings;
  231. public
  232. function Accept(ASocket: TIdStackSocketHandle; var VIP: string; var VPort: TIdPort): TIdStackSocketHandle; overload;
  233. function Accept(ASocket: TIdStackSocketHandle; var VIP: string; var VPort: TIdPort;
  234. var VIPVersion: TIdIPVersion): TIdStackSocketHandle; overload; virtual; abstract;
  235. procedure Bind(ASocket: TIdStackSocketHandle; const AIP: string;
  236. const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION ); virtual; abstract;
  237. procedure Connect(const ASocket: TIdStackSocketHandle; const AIP: string;
  238. const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  239. constructor Create; virtual;
  240. destructor Destroy; override;
  241. procedure Disconnect(ASocket: TIdStackSocketHandle); virtual; abstract;
  242. function IOControl(const s: TIdStackSocketHandle; const cmd: UInt32;
  243. var arg: UInt32): Integer; virtual; abstract;
  244. class procedure IncUsage; //create stack if necessary and inc counter
  245. class procedure DecUsage; //decrement counter and free if it gets to zero
  246. procedure GetPeerName(ASocket: TIdStackSocketHandle; var VIP: string;
  247. var VPort: TIdPort); overload;
  248. procedure GetPeerName(ASocket: TIdStackSocketHandle; var VIP: string;
  249. var VPort: TIdPort; var VIPVersion: TIdIPVersion); overload; virtual; abstract;
  250. procedure GetSocketName(ASocket: TIdStackSocketHandle; var VIP: string;
  251. var VPort: TIdPort); overload;
  252. procedure GetSocketName(ASocket: TIdStackSocketHandle; var VIP: string;
  253. var VPort: TIdPort; var VIPVersion: TIdIPVersion); overload; virtual; abstract;
  254. function HostByAddress(const AAddress: string;
  255. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string; virtual; abstract;
  256. function HostToNetwork(AValue: UInt16): UInt16; overload; virtual; abstract;
  257. function HostToNetwork(AValue: UInt32): UInt32; overload; virtual; abstract;
  258. function HostToNetwork(AValue: TIdUInt64): TIdUInt64; overload; virtual; abstract;
  259. function HostToNetwork(const AValue: TIdIPv6Address): TIdIPv6Address; overload; virtual;
  260. function IsIP(AIP: string): Boolean;
  261. procedure Listen(ASocket: TIdStackSocketHandle; ABackLog: Integer); virtual; abstract;
  262. function WSGetLastError: Integer; virtual; abstract;
  263. procedure WSSetLastError(const AErr : Integer); virtual; abstract;
  264. function WSTranslateSocketErrorMsg(const AErr: integer): string; virtual;
  265. function CheckForSocketError(const AResult: Integer): Integer; overload;
  266. function CheckForSocketError(const AResult: Integer; const AIgnore: array of Integer): Integer; overload;
  267. procedure RaiseLastSocketError;
  268. procedure RaiseSocketError(AErr: integer); virtual;
  269. function NewSocketHandle(const ASocketType: TIdSocketType; const AProtocol: TIdSocketProtocol;
  270. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION; const ANonBlocking: Boolean = False)
  271. : TIdStackSocketHandle; virtual; abstract;
  272. function NetworkToHost(AValue: UInt16): UInt16; overload; virtual; abstract;
  273. function NetworkToHost(AValue: UInt32): UInt32; overload; virtual; abstract;
  274. function NetworkToHost(AValue: TIdUInt64): TIdUInt64; overload; virtual; abstract;
  275. function NetworkToHost(const AValue: TIdIPv6Address): TIdIPv6Address; overload; virtual;
  276. procedure GetSocketOption(ASocket: TIdStackSocketHandle;
  277. ALevel: TIdSocketOptionLevel; AOptName: TIdSocketOption;
  278. out AOptVal: Integer); overload; virtual; abstract;
  279. procedure SetSocketOption(ASocket: TIdStackSocketHandle; ALevel: TIdSocketOptionLevel;
  280. AOptName: TIdSocketOption; AOptVal: Integer); overload; virtual; abstract;
  281. function ResolveHost(const AHost: string;
  282. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string;
  283. // Result:
  284. // > 0: Number of bytes received
  285. // 0: Connection closed gracefully
  286. // Will raise exceptions in other cases
  287. function Receive(ASocket: TIdStackSocketHandle; var VBuffer: TIdBytes): Integer; virtual; abstract;
  288. function Send(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  289. const AOffset: Integer = 0; const ASize: Integer = -1): Integer; virtual; abstract;
  290. function ReceiveFrom(ASocket: TIdStackSocketHandle; var VBuffer: TIdBytes;
  291. var VIP: string; var VPort: TIdPort; var VIPVersion: TIdIPVersion): Integer; virtual; abstract;
  292. function SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  293. const AOffset: Integer; const AIP: string; const APort: TIdPort;
  294. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): Integer; overload;
  295. function SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  296. const AOffset: Integer; const ASize: Integer; const AIP: string;
  297. const APort: TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION)
  298. : Integer; overload; virtual; abstract;
  299. function ReceiveMsg(ASocket: TIdStackSocketHandle; var VBuffer: TIdBytes;
  300. APkt: TIdPacketInfo): UInt32; virtual; abstract;
  301. function SupportsIPv4: Boolean; virtual; abstract;
  302. function SupportsIPv6: Boolean; virtual; abstract;
  303. //multicast stuff Kudzu permitted me to add here.
  304. function IsValidIPv4MulticastGroup(const Value: string): Boolean;
  305. function IsValidIPv6MulticastGroup(const Value: string): Boolean;
  306. procedure SetKeepAliveValues(ASocket: TIdStackSocketHandle;
  307. const AEnabled: Boolean; const ATimeMS, AInterval: Integer); virtual;
  308. procedure SetMulticastTTL(AHandle: TIdStackSocketHandle;
  309. const AValue : Byte; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  310. procedure SetLoopBack(AHandle: TIdStackSocketHandle; const AValue: Boolean;
  311. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  312. procedure DropMulticastMembership(AHandle: TIdStackSocketHandle;
  313. const AGroupIP, ALocalIP : String; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  314. procedure AddMulticastMembership(AHandle: TIdStackSocketHandle;
  315. const AGroupIP, ALocalIP : String; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  316. //I know this looks like an odd place to put a function for calculating a
  317. //packet checksum. There is a reason for it though. The reason is that
  318. //you need it for ICMPv6 and in Windows, you do that with some other stuff
  319. //in the stack descendants
  320. function CalcCheckSum(const AData : TIdBytes): UInt16; virtual;
  321. //In Windows, this writes a checksum into a buffer. In Linux, it would probably
  322. //simply have the kernal write the checksum with something like this (RFC 2292):
  323. //
  324. // int offset = 2;
  325. // setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, sizeof(offset));
  326. //
  327. // Note that this should be called
  328. //IMMEDIATELY before you do a SendTo because the Local IPv6 address might change
  329. procedure WriteChecksum(s : TIdStackSocketHandle;
  330. var VBuffer : TIdBytes; const AOffset : Integer; const AIP : String;
  331. const APort : TIdPort; const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION); virtual; abstract;
  332. //
  333. procedure AddLocalAddressesToList(AAddresses: TStrings); {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'use GetLocalAddressList()'{$ENDIF};{$ENDIF}
  334. procedure GetLocalAddressList(AAddresses: TIdStackLocalAddressList); virtual; abstract;
  335. //
  336. // Properties
  337. //
  338. property HostName: string read ReadHostName;
  339. property LocalAddress: string read GetLocalAddress; // {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'use GetLocalAddressList()'{$ENDIF};{$ENDIF}
  340. property LocalAddresses: TStrings read GetLocalAddresses; // {$IFDEF HAS_DEPRECATED}deprecated{$IFDEF HAS_DEPRECATED_MSG} 'use GetLocalAddressList()'{$ENDIF};{$ENDIF}
  341. end;
  342. TIdStackClass = class of TIdStack;
  343. var
  344. GStack: TIdStack = nil;
  345. GServeFileProc: TIdServeFile = nil;
  346. GSocketListClass: TIdSocketListClass;
  347. // Procedures
  348. procedure SetStackClass( AStackClass: TIdStackClass );
  349. // TODO: move this to IdStackVCLPosix...
  350. {$IFDEF USE_VCL_POSIX}
  351. {$IFDEF ANDROID}
  352. function HasAndroidPermission(const Permission: string): Boolean;
  353. {$ENDIF}
  354. {$ENDIF}
  355. implementation
  356. {$O-}
  357. uses
  358. //done this way so we can have a separate stack for FPC under Unix systems
  359. {$IFDEF DOTNET}
  360. IdStackDotNet,
  361. {$ELSE}
  362. {$IFDEF WINDOWS}
  363. {$IFDEF USE_INLINE}
  364. Windows,
  365. {$ENDIF}
  366. IdStackWindows,
  367. {$ELSE}
  368. {$IFDEF USE_VCL_POSIX}
  369. IdStackVCLPosix,
  370. {$ELSE}
  371. {$IFDEF UNIX}
  372. {$IFDEF KYLIXCOMPAT}
  373. IdStackLibc,
  374. {$ELSE}
  375. {$IFDEF USE_BASEUNIX}
  376. IdStackUnix,
  377. {$ENDIF}
  378. {$ENDIF}
  379. {$ENDIF}
  380. {$ENDIF}
  381. {$ENDIF}
  382. {$ENDIF}
  383. // TODO: move this to IdStackVCLPosix...
  384. {$IFDEF USE_VCL_POSIX}
  385. {$IFDEF ANDROID}
  386. {$IFNDEF VCL_XE6_OR_ABOVE}
  387. // StringToJString() is here in XE5
  388. Androidapi.JNI.JavaTypes,
  389. {$ENDIF}
  390. {$IFNDEF VCL_XE7_OR_ABOVE}
  391. // SharedActivityContext() is here in XE5 and XE6
  392. FMX.Helpers.Android,
  393. {$ENDIF}
  394. {$IFDEF VCL_XE6_OR_ABOVE}
  395. {$IFDEF VCL_10_0_OR_ABOVE}
  396. // StringToJString() is inline in Seattle and later, so we need JavaTypes again...
  397. Androidapi.JNI.JavaTypes,
  398. {$ENDIF}
  399. // StringToJString() was moved here in XE6
  400. // SharedActivityContext() was moved here in XE7
  401. // TAndroidHelper was added here in Seattle
  402. Androidapi.Helpers,
  403. {$ENDIF}
  404. Androidapi.JNI.GraphicsContentViewText,
  405. {$ENDIF}
  406. {$ENDIF}
  407. IdResourceStrings;
  408. var
  409. GStackClass: TIdStackClass = nil;
  410. var
  411. {$IFNDEF USE_OBJECT_ARC}
  412. GInstanceCount: UInt32 = 0;
  413. {$ENDIF}
  414. GStackCriticalSection: TIdCriticalSection = nil;
  415. //for IPv4 Multicast address chacking
  416. const
  417. IPv4MCastLo = 224;
  418. IPv4MCastHi = 239;
  419. procedure SetStackClass(AStackClass: TIdStackClass);
  420. begin
  421. GStackClass := AStackClass;
  422. end;
  423. procedure TIdPacketInfo.Reset;
  424. begin
  425. FSourceIP := '';
  426. FSourcePort := 0;
  427. FSourceIF := 0;
  428. FSourceIPVersion := ID_DEFAULT_IP_VERSION;
  429. FDestIP := '';
  430. FDestPort:= 0;
  431. FDestIF := 0;
  432. FDestIPVersion := ID_DEFAULT_IP_VERSION;
  433. FTTL := 0;
  434. end;
  435. { TIdSocketList }
  436. constructor TIdSocketList.Create;
  437. begin
  438. inherited Create;
  439. FLock := TIdCriticalSection.Create;
  440. end;
  441. class function TIdSocketList.CreateSocketList: TIdSocketList;
  442. Begin
  443. Result := GSocketListClass.Create;
  444. End;
  445. destructor TIdSocketList.Destroy;
  446. begin
  447. FreeAndNil(FLock);
  448. inherited Destroy;
  449. end;
  450. procedure TIdSocketList.Lock;
  451. begin
  452. FLock.Acquire;
  453. end;
  454. class function TIdSocketList.Select(AReadList, AWriteList,
  455. AExceptList: TIdSocketList; const ATimeout: Integer): Boolean;
  456. begin
  457. // C++ Builder cannot have abstract class functions thus we need this base
  458. Result := False;
  459. end;
  460. procedure TIdSocketList.Unlock;
  461. begin
  462. FLock.Release;
  463. end;
  464. { EIdSocketError }
  465. constructor EIdSocketError.CreateError(const AErr: Integer; const AMsg: string);
  466. begin
  467. inherited Create(AMsg);
  468. FLastError := AErr;
  469. end;
  470. { TIdStackLocalAddressList }
  471. constructor TIdStackLocalAddress.Create(ACollection: TCollection; const AIPVersion: TIdIPVersion; const AIPAddress: string);
  472. begin
  473. inherited Create(ACollection);
  474. FIPVersion := AIPVersion;
  475. FIPAddress := AIPAddress;
  476. FInterfaceIndex := 0;
  477. end;
  478. constructor TIdStackLocalAddressIPv4.Create(ACollection: TCollection; const AIPAddress, ASubNetMask: string);
  479. begin
  480. inherited Create(ACollection, Id_IPv4, AIPAddress);
  481. FSubNetMask := ASubNetMask;
  482. end;
  483. constructor TIdStackLocalAddressIPv6.Create(ACollection: TCollection; const AIPAddress: string);
  484. begin
  485. inherited Create(ACollection, Id_IPv6, AIPAddress);
  486. end;
  487. constructor TIdStackLocalAddressList.Create;
  488. begin
  489. inherited Create(TIdStackLocalAddress);
  490. end;
  491. function TIdStackLocalAddressList.GetAddress(AIndex: Integer): TIdStackLocalAddress;
  492. begin
  493. Result := TIdStackLocalAddress(inherited Items[AIndex]);
  494. end;
  495. function TIdStackLocalAddressList.IndexOfIP(const AIP: String): Integer;
  496. var
  497. I: Integer;
  498. begin
  499. Result := -1;
  500. for I := 0 to Count-1 do begin
  501. if Addresses[I].IPAddress = AIP then begin
  502. Result := I;
  503. Exit;
  504. end;
  505. end;
  506. end;
  507. function TIdStackLocalAddressList.IndexOfIP(const AIP: String; AIPVersion: TIdIPVersion): Integer;
  508. var
  509. I: Integer;
  510. LAddr: TIdStackLocalAddress;
  511. begin
  512. Result := -1;
  513. for I := 0 to Count-1 do begin
  514. LAddr := Addresses[I];
  515. if (LAddr.IPVersion = AIPVersion) and (LAddr.IPAddress = AIP) then begin
  516. Result := I;
  517. Exit;
  518. end;
  519. end;
  520. end;
  521. { TIdStack }
  522. constructor TIdStack.Create;
  523. begin
  524. // Here for .net
  525. inherited Create;
  526. end;
  527. destructor TIdStack.Destroy;
  528. begin
  529. FreeAndNil(FLocalAddresses);
  530. inherited Destroy;
  531. end;
  532. procedure TIdStack.IPVersionUnsupported;
  533. begin
  534. raise EIdIPVersionUnsupported.Create(RSIPVersionUnsupported);
  535. end;
  536. function TIdStack.Accept(ASocket: TIdStackSocketHandle; var VIP: string;
  537. var VPort: TIdPort): TIdStackSocketHandle;
  538. var
  539. LIPVersion: TIdIPVersion;
  540. begin
  541. Result := Accept(ASocket, VIP, VPort, LIPVersion);
  542. end;
  543. procedure TIdStack.GetPeerName(ASocket: TIdStackSocketHandle; var VIP: string;
  544. var VPort: TIdPort);
  545. var
  546. LIPVersion: TIdIPVersion;
  547. begin
  548. GetPeerName(ASocket, VIP, VPort, LIPVersion);
  549. end;
  550. procedure TIdStack.GetSocketName(ASocket: TIdStackSocketHandle; var VIP: string;
  551. var VPort: TIdPort);
  552. var
  553. LIPVersion: TIdIPVersion;
  554. begin
  555. GetSocketName(ASocket, VIP, VPort, LIPVersion);
  556. end;
  557. {$I IdDeprecatedImplBugOff.inc}
  558. procedure TIdStack.AddLocalAddressesToList(AAddresses: TStrings);
  559. {$I IdDeprecatedImplBugOn.inc}
  560. var
  561. LList: TIdStackLocalAddressList;
  562. I: Integer;
  563. begin
  564. LList := TIdStackLocalAddressList.Create;
  565. try
  566. // for backwards compatibility, return only IPv4 addresses
  567. GetLocalAddressList(LList);
  568. if LList.Count > 0 then begin
  569. AAddresses.BeginUpdate;
  570. try
  571. for I := 0 to LList.Count-1 do begin
  572. if LList[I].IPVersion = Id_IPv4 then begin
  573. AAddresses.Add(LList[I].IPAddress);
  574. end;
  575. end;
  576. finally
  577. AAddresses.EndUpdate;
  578. end;
  579. end;
  580. finally
  581. LList.Free;
  582. end;
  583. end;
  584. function TIdStack.GetLocalAddresses: TStrings;
  585. var
  586. LList: TIdStackLocalAddressList;
  587. I: Integer;
  588. begin
  589. if FLocalAddresses = nil then begin
  590. FLocalAddresses := TStringList.Create;
  591. end;
  592. FLocalAddresses.BeginUpdate;
  593. try
  594. FLocalAddresses.Clear;
  595. LList := TIdStackLocalAddressList.Create;
  596. try
  597. // for backwards compatibility, return only IPv4 addresses
  598. GetLocalAddressList(LList);
  599. for I := 0 to LList.Count-1 do begin
  600. if LList[I].IPVersion = Id_IPv4 then begin
  601. FLocalAddresses.Add(LList[I].IPAddress);
  602. end;
  603. end;
  604. finally
  605. LList.Free;
  606. end;
  607. finally
  608. FLocalAddresses.EndUpdate;
  609. end;
  610. Result := FLocalAddresses;
  611. end;
  612. function TIdStack.GetLocalAddress: string;
  613. var
  614. LList: TIdStackLocalAddressList;
  615. I: Integer;
  616. begin
  617. // RLebeau: using a local list instead of the LocalAddresses
  618. // property so this method can be thread-safe...
  619. //
  620. // old code:
  621. // Result := LocalAddresses[0];
  622. Result := '';
  623. LList := TIdStackLocalAddressList.Create;
  624. try
  625. // for backwards compatibility, return only IPv4 addresses
  626. GetLocalAddressList(LList);
  627. for I := 0 to LList.Count-1 do begin
  628. if LList[I].IPVersion = Id_IPv4 then begin
  629. Result := LList[I].IPAddress;
  630. Exit;
  631. end;
  632. end;
  633. finally
  634. LList.Free;
  635. end;
  636. end;
  637. function TIdStack.IsIP(AIP: string): Boolean;
  638. var
  639. i: Integer;
  640. begin
  641. // TODO: support IPv6
  642. //
  643. //Result := Result and ((i > 0) and (i < 256));
  644. //
  645. i := IndyStrToInt(Fetch(AIP, '.'), -1); {Do not Localize}
  646. Result := (i > -1) and (i < 256);
  647. i := IndyStrToInt(Fetch(AIP, '.'), -1); {Do not Localize}
  648. Result := Result and ((i > -1) and (i < 256));
  649. i := IndyStrToInt(Fetch(AIP, '.'), -1); {Do not Localize}
  650. Result := Result and ((i > -1) and (i < 256));
  651. i := IndyStrToInt(Fetch(AIP, '.'), -1); {Do not Localize}
  652. Result := Result and ((i > -1) and (i < 256)) and (AIP = '');
  653. end;
  654. {$I IdDeprecatedImplBugOff.inc}
  655. function TIdStack.MakeCanonicalIPv6Address(const AAddr: string): string;
  656. {$I IdDeprecatedImplBugOn.inc}
  657. begin
  658. Result := IdGlobal.MakeCanonicalIPv6Address(AAddr);
  659. end;
  660. function TIdStack.ResolveHost(const AHost: string;
  661. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string;
  662. begin
  663. Result := '';
  664. case AIPVersion of
  665. Id_IPv4: begin
  666. // Sometimes 95 forgets who localhost is
  667. if TextIsSame(AHost, 'LOCALHOST') then begin {Do not Localize}
  668. Result := '127.0.0.1'; {Do not Localize}
  669. end else if IsIP(AHost) then begin
  670. Result := AHost;
  671. end else begin
  672. Result := HostByName(AHost, Id_IPv4);
  673. end;
  674. end;
  675. Id_IPv6: begin
  676. if TextIsSame(AHost, 'LOCALHOST') then begin {Do not Localize}
  677. Result := '::1'; {Do not Localize}
  678. end else begin
  679. Result := IdGlobal.MakeCanonicalIPv6Address(AHost);
  680. if Result = '' then begin
  681. Result := HostByName(AHost, Id_IPv6);
  682. end;
  683. end;
  684. end;
  685. else begin
  686. IPVersionUnsupported;
  687. end;
  688. end;
  689. end;
  690. function TIdStack.SendTo(ASocket: TIdStackSocketHandle; const ABuffer: TIdBytes;
  691. const AOffset: Integer; const AIP: string; const APort: TIdPort;
  692. const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): Integer;
  693. begin
  694. Result := SendTo(ASocket, ABuffer, AOffset, -1, AIP, APort, AIPVersion);
  695. end;
  696. class procedure TIdStack.DecUsage;
  697. var
  698. // under ARC, increment the lock's reference count before working with it
  699. LLock: TIdCriticalSection;
  700. begin
  701. LLock := GStackCriticalSection;
  702. if not Assigned(LLock) then begin
  703. raise EIdStackError.Create('GStackCriticalSection is nil in TIdStack.DecUsage'); {do not localize}
  704. end;
  705. LLock.Acquire;
  706. try
  707. // This CS will guarantee that during the FreeAndNil nobody
  708. // will try to use or construct GStack
  709. {$IFDEF USE_OBJECT_ARC}
  710. if GStack <> nil then begin
  711. if GStack.__ObjRelease = 0 then begin
  712. Pointer(GStack) := nil;
  713. end;
  714. end;
  715. {$ELSE}
  716. if GInstanceCount > 0 then begin
  717. Dec(GInstanceCount);
  718. if GInstanceCount = 0 then begin
  719. FreeAndNil(GStack);
  720. end;
  721. end;
  722. {$ENDIF}
  723. finally
  724. LLock.Release;
  725. end;
  726. end;
  727. class procedure TIdStack.IncUsage;
  728. var
  729. // under ARC, increment the lock's reference count before working with it
  730. LLock: TIdCriticalSection;
  731. begin
  732. LLock := GStackCriticalSection;
  733. if not Assigned(LLock) then begin
  734. raise EIdStackError.Create('GStackCriticalSection is nil in TIdStack.IncUsage'); {do not localize}
  735. end;
  736. LLock.Acquire;
  737. try
  738. {$IFDEF USE_OBJECT_ARC}
  739. if GStack = nil then begin
  740. if GStackClass = nil then begin
  741. raise EIdStackError.Create(RSStackClassUndefined);
  742. end;
  743. GStack := GStackClass.Create;
  744. end else begin
  745. GStack.__ObjAddRef;
  746. end;
  747. {$ELSE}
  748. if GInstanceCount = 0 then begin
  749. if GStack <> nil then begin
  750. raise EIdStackError.Create(RSStackAlreadyCreated);
  751. end;
  752. if GStackClass = nil then begin
  753. raise EIdStackError.Create(RSStackClassUndefined);
  754. end;
  755. GStack := GStackClass.Create;
  756. end;
  757. Inc(GInstanceCount);
  758. {$ENDIF}
  759. finally
  760. LLock.Release;
  761. end;
  762. end;
  763. function TIdStack.CheckForSocketError(const AResult: Integer): Integer;
  764. begin
  765. if AResult = Integer(Id_SOCKET_ERROR) then begin
  766. RaiseLastSocketError;
  767. end;
  768. Result := AResult;
  769. end;
  770. function TIdStack.CheckForSocketError(const AResult: Integer;
  771. const AIgnore: array of integer): Integer;
  772. var
  773. i: Integer;
  774. LLastError: Integer;
  775. begin
  776. Result := AResult;
  777. if AResult = Integer(Id_SOCKET_ERROR) then begin
  778. LLastError := WSGetLastError;
  779. for i := Low(AIgnore) to High(AIgnore) do begin
  780. if LLastError = AIgnore[i] then begin
  781. Result := LLastError;
  782. Exit;
  783. end;
  784. end;
  785. RaiseSocketError(LLastError);
  786. end;
  787. end;
  788. procedure TIdStack.RaiseLastSocketError;
  789. begin
  790. RaiseSocketError(WSGetLastError);
  791. end;
  792. // TODO: move this to IdStackVCLPosix...
  793. {$IFDEF USE_VCL_POSIX}
  794. {$IFDEF ANDROID}
  795. function GetActivityContext: JContext; {$IFDEF USE_INLINE}inline;{$ENDIF}
  796. begin
  797. {$IFDEF HAS_TAndroidHelper}
  798. Result := TAndroidHelper.Context;
  799. {$ELSE}
  800. Result := SharedActivityContext;
  801. {$ENDIF}
  802. end;
  803. function HasAndroidPermission(const Permission: string): Boolean;
  804. begin
  805. Result := GetActivityContext.checkCallingOrSelfPermission(StringToJString(Permission)) = TJPackageManager.JavaClass.PERMISSION_GRANTED;
  806. end;
  807. {$ENDIF}
  808. {$ENDIF}
  809. procedure TIdStack.RaiseSocketError(AErr: integer);
  810. begin
  811. (*
  812. RRRRR EEEEEE AAAA DDDDD MM MM EEEEEE !! !! !!
  813. RR RR EE AA AA DD DD MMMM MMMM EE !! !! !!
  814. RRRRR EEEE AAAAAA DD DD MM MMM MM EEEE !! !! !!
  815. RR RR EE AA AA DD DD MM MM EE
  816. RR RR EEEEEE AA AA DDDDD MM MM EEEEEE .. .. ..
  817. Please read the note in the next comment.
  818. *)
  819. if AErr = Id_WSAENOTSOCK then begin
  820. // You can add this to your exception ignore list for easier debugging.
  821. // However please note that sometimes it is a true error. Your program
  822. // will still run correctly, but the debugger will not stop on it if you
  823. // list it in the ignore list. But for most times its fine to put it in
  824. // the ignore list, it only affects your debugging.
  825. raise EIdNotASocket.CreateError(AErr, WSTranslateSocketErrorMsg(AErr));
  826. end;
  827. // TODO: move this to IdStackVCLPosix...
  828. {$IFDEF USE_VCL_POSIX}
  829. {$IFDEF ANDROID}
  830. if (AErr = 9{EBADF}) or (AErr = 12{EBADR?}) or (AErr = 13{EACCES}) then begin
  831. if not HasAndroidPermission('android.permission.INTERNET') then begin {Do not Localize}
  832. raise EIdInternetPermissionNeeded.CreateError(AErr, WSTranslateSocketErrorMsg(AErr));
  833. end;
  834. end;
  835. {$ENDIF}
  836. {$ENDIF}
  837. (*
  838. It is normal to receive a 10038 exception (10038, NOT others!) here when
  839. *shutting down* (NOT at other times!) servers (NOT clients!).
  840. If you receive a 10038 exception here please see the FAQ at:
  841. http://www.IndyProject.org/
  842. If you insist upon requesting help via our email boxes on the 10038 error
  843. that is already answered in the FAQ and you are simply too slothful to
  844. search for your answer and ask your question in the public forums you may be
  845. publicly flogged, tarred and feathered and your name may be added to every
  846. chain letter / EMail in existence today."
  847. Otherwise, if you DID read the FAQ and have further questions, please feel
  848. free to ask using one of the methods (Carefullly note that these methods do
  849. not list email) listed on the Tech Support link at:
  850. http://www.IndyProject.org/
  851. RRRRR EEEEEE AAAA DDDDD MM MM EEEEEE !! !! !!
  852. RR RR EE AA AA DD DD MMMM MMMM EE !! !! !!
  853. RRRRR EEEE AAAAAA DD DD MM MMM MM EEEE !! !! !!
  854. RR RR EE AA AA DD DD MM MM EE
  855. RR RR EEEEEE AA AA DDDDD MM MM EEEEEE .. .. ..
  856. *)
  857. raise EIdSocketError.CreateError(AErr, WSTranslateSocketErrorMsg(AErr));
  858. end;
  859. function TIdStack.WSTranslateSocketErrorMsg(const AErr: integer): string;
  860. begin
  861. Result := ''; {Do not Localize}
  862. case AErr of
  863. Id_WSAEINTR: Result := RSStackEINTR;
  864. Id_WSAEBADF: Result := RSStackEBADF;
  865. Id_WSAEACCES: Result := RSStackEACCES;
  866. Id_WSAEFAULT: Result := RSStackEFAULT;
  867. Id_WSAEINVAL: Result := RSStackEINVAL;
  868. Id_WSAEMFILE: Result := RSStackEMFILE;
  869. Id_WSAEWOULDBLOCK: Result := RSStackEWOULDBLOCK;
  870. Id_WSAEINPROGRESS: Result := RSStackEINPROGRESS;
  871. Id_WSAEALREADY: Result := RSStackEALREADY;
  872. Id_WSAENOTSOCK: Result := RSStackENOTSOCK;
  873. Id_WSAEDESTADDRREQ: Result := RSStackEDESTADDRREQ;
  874. Id_WSAEMSGSIZE: Result := RSStackEMSGSIZE;
  875. Id_WSAEPROTOTYPE: Result := RSStackEPROTOTYPE;
  876. Id_WSAENOPROTOOPT: Result := RSStackENOPROTOOPT;
  877. Id_WSAEPROTONOSUPPORT: Result := RSStackEPROTONOSUPPORT;
  878. {$IFNDEF BEOS}
  879. Id_WSAESOCKTNOSUPPORT: Result := RSStackESOCKTNOSUPPORT;
  880. {$ENDIF}
  881. Id_WSAEOPNOTSUPP: Result := RSStackEOPNOTSUPP;
  882. Id_WSAEPFNOSUPPORT: Result := RSStackEPFNOSUPPORT;
  883. Id_WSAEAFNOSUPPORT: Result := RSStackEAFNOSUPPORT;
  884. Id_WSAEADDRINUSE: Result := RSStackEADDRINUSE;
  885. Id_WSAEADDRNOTAVAIL: Result := RSStackEADDRNOTAVAIL;
  886. Id_WSAENETDOWN: Result := RSStackENETDOWN;
  887. Id_WSAENETUNREACH: Result := RSStackENETUNREACH;
  888. Id_WSAENETRESET: Result := RSStackENETRESET;
  889. Id_WSAECONNABORTED: Result := RSStackECONNABORTED;
  890. Id_WSAECONNRESET: Result := RSStackECONNRESET;
  891. Id_WSAENOBUFS: Result := RSStackENOBUFS;
  892. Id_WSAEISCONN: Result := RSStackEISCONN;
  893. Id_WSAENOTCONN: Result := RSStackENOTCONN;
  894. Id_WSAESHUTDOWN: Result := RSStackESHUTDOWN;
  895. {$IFNDEF BEOS}
  896. Id_WSAETOOMANYREFS: Result := RSStackETOOMANYREFS;
  897. {$ENDIF}
  898. Id_WSAETIMEDOUT: Result := RSStackETIMEDOUT;
  899. Id_WSAECONNREFUSED: Result := RSStackECONNREFUSED;
  900. Id_WSAELOOP: Result := RSStackELOOP;
  901. Id_WSAENAMETOOLONG: Result := RSStackENAMETOOLONG;
  902. Id_WSAEHOSTDOWN: Result := RSStackEHOSTDOWN;
  903. Id_WSAEHOSTUNREACH: Result := RSStackEHOSTUNREACH;
  904. Id_WSAENOTEMPTY: Result := RSStackENOTEMPTY;
  905. end;
  906. Result := IndyFormat(RSStackError, [AErr, Result]);
  907. end;
  908. function TIdStack.HostToNetwork(const AValue: TIdIPv6Address): TIdIPv6Address;
  909. var
  910. i : Integer;
  911. begin
  912. for i := 0 to 7 do begin
  913. Result[i] := HostToNetwork(AValue[i]);
  914. end;
  915. end;
  916. function TIdStack.NetworkToHost(const AValue: TIdIPv6Address): TIdIPv6Address;
  917. var
  918. i : Integer;
  919. begin
  920. for i := 0 to 7 do begin
  921. Result[i] := NetworkToHost(AValue[i]);
  922. end;
  923. end;
  924. function TIdStack.IsValidIPv4MulticastGroup(const Value: string): Boolean;
  925. var
  926. LIP: string;
  927. LVal: Integer;
  928. begin
  929. Result := False;
  930. if IsIP(Value) then
  931. begin
  932. LIP := Value;
  933. LVal := IndyStrToInt(Fetch(LIP, '.')); {Do not Localize}
  934. Result := (LVal >= IPv4MCastLo) and (LVal <= IPv4MCastHi);
  935. end;
  936. end;
  937. { From "rfc 2373"
  938. 2.7 Multicast Addresses
  939. An IPv6 multicast address is an identifier for a group of nodes. A
  940. node may belong to any number of multicast groups. Multicast
  941. addresses have the following format:
  942. #
  943. | 8 | 4 | 4 | 112 bits |
  944. +------ -+----+----+---------------------------------------------+
  945. |11111111|flgs|scop| group ID |
  946. +--------+----+----+---------------------------------------------+
  947. 11111111 at the start of the address identifies the address as
  948. being a multicast address.
  949. +-+-+-+-+
  950. flgs is a set of 4 flags: |0|0|0|T|
  951. +-+-+-+-+
  952. The high-order 3 flags are reserved, and must be initialized to
  953. 0.
  954. T = 0 indicates a permanently-assigned ("well-known") multicast
  955. address, assigned by the global internet numbering authority.
  956. T = 1 indicates a non-permanently-assigned ("transient")
  957. multicast address.
  958. scop is a 4-bit multicast scope value used to limit the scope of
  959. the multicast group. The values are:
  960. 0 reserved
  961. 1 node-local scope
  962. 2 link-local scope
  963. 3 (unassigned)
  964. 4 (unassigned)
  965. 5 site-local scope
  966. 6 (unassigned)
  967. 7 (unassigned)
  968. 8 organization-local scope
  969. 9 (unassigned)
  970. A (unassigned)
  971. B (unassigned)
  972. C (unassigned)
  973. D (unassigned)
  974. E global scope
  975. F reserved
  976. group ID identifies the multicast group, either permanent or
  977. transient, within the given scope.
  978. The "meaning" of a permanently-assigned multicast address is
  979. independent of the scope value. For example, if the "NTP servers
  980. group" is assigned a permanent multicast address with a group ID of
  981. 101 (hex), then:
  982. FF01:0:0:0:0:0:0:101 means all NTP servers on the same node as the
  983. sender.
  984. FF02:0:0:0:0:0:0:101 means all NTP servers on the same link as the
  985. sender.
  986. FF05:0:0:0:0:0:0:101 means all NTP servers at the same site as the
  987. sender.
  988. FF0E:0:0:0:0:0:0:101 means all NTP servers in the internet.
  989. Non-permanently-assigned multicast addresses are meaningful only
  990. within a given scope. For example, a group identified by the non-
  991. permanent, site-local multicast address FF15:0:0:0:0:0:0:101 at one
  992. site bears no relationship to a group using the same address at a
  993. different site, nor to a non-permanent group using the same group ID
  994. with different scope, nor to a permanent group with the same group
  995. ID.
  996. Multicast addresses must not be used as source addresses in IPv6
  997. packets or appear in any routing header.
  998. }
  999. function TIdStack.IsValidIPv6MulticastGroup(const Value: string): Boolean;
  1000. var
  1001. LTmp : String;
  1002. begin
  1003. LTmp := IdGlobal.MakeCanonicalIPv6Address(Value);
  1004. if LTmp <> '' then
  1005. begin
  1006. Result := TextStartsWith(LTmp, 'FF');
  1007. end else begin
  1008. Result := False;
  1009. end;
  1010. end;
  1011. function TIdStack.CalcCheckSum(const AData: TIdBytes): UInt16;
  1012. var
  1013. i : Integer;
  1014. LSize : Integer;
  1015. LCRC : UInt32;
  1016. begin
  1017. LCRC := 0;
  1018. i := 0;
  1019. LSize := Length(AData);
  1020. while LSize > 1 do
  1021. begin
  1022. LCRC := LCRC + BytesToUInt16(AData, i);
  1023. Dec(LSize, 2);
  1024. Inc(i, 2);
  1025. end;
  1026. if LSize > 0 then begin
  1027. LCRC := LCRC + AData[i];
  1028. end;
  1029. LCRC := (LCRC shr 16) + (LCRC and $ffff); //(LCRC >> 16)
  1030. LCRC := LCRC + (LCRC shr 16);
  1031. Result := not UInt16(LCRC);
  1032. end;
  1033. {$UNDEF HAS_TCP_KEEPIDLE_OR_KEEPINTVL}
  1034. {$IFDEF HAS_TCP_KEEPIDLE}
  1035. {$DEFINE HAS_TCP_KEEPIDLE_OR_KEEPINTVL}
  1036. {$ENDIF}
  1037. {$IFDEF HAS_TCP_KEEPINTVL}
  1038. {$DEFINE HAS_TCP_KEEPIDLE_OR_KEEPINTVL}
  1039. {$ENDIF}
  1040. procedure TIdStack.SetKeepAliveValues(ASocket: TIdStackSocketHandle;
  1041. const AEnabled: Boolean; const ATimeMS, AInterval: Integer);
  1042. begin
  1043. SetSocketOption(ASocket, Id_SOL_SOCKET, Id_SO_KEEPALIVE, iif(AEnabled, 1, 0));
  1044. {$IFDEF HAS_TCP_KEEPIDLE_OR_KEEPINTVL}
  1045. if AEnabled then
  1046. begin
  1047. // TODO: support TCP_KEEPCNT
  1048. {$IFDEF HAS_TCP_KEEPIDLE}
  1049. SetSocketOption(ASocket, Id_SOL_TCP, Id_TCP_KEEPIDLE, ATimeMS);
  1050. {$ENDIF}
  1051. {$IFDEF HAS_TCP_KEEPINTVL}
  1052. SetSocketOption(ASocket, Id_SOL_TCP, Id_TCP_KEEPINTVL, AInterval);
  1053. {$ENDIF}
  1054. end;
  1055. {$ENDIF}
  1056. end;
  1057. initialization
  1058. //done this way so we can have a separate stack just for FPC under Unix systems
  1059. GStackClass :=
  1060. {$IFDEF DOTNET}
  1061. TIdStackDotNet
  1062. {$ELSE}
  1063. {$IFDEF WINDOWS}
  1064. TIdStackWindows
  1065. {$ELSE}
  1066. {$IFDEF USE_VCL_POSIX}
  1067. TIdStackVCLPosix
  1068. {$ELSE}
  1069. {$IFDEF UNIX}
  1070. {$IFDEF KYLIXCOMPAT}
  1071. TIdStackLibc
  1072. {$ELSE}
  1073. {$IFDEF USE_BASEUNIX}
  1074. TIdStackUnix
  1075. {$ENDIF}
  1076. {$ENDIF}
  1077. {$ENDIF}
  1078. {$ENDIF}
  1079. {$ENDIF}
  1080. {$ENDIF}
  1081. ;
  1082. GStackCriticalSection := TIdCriticalSection.Create;
  1083. {$IFNDEF DOTNET}
  1084. {$IFDEF REGISTER_EXPECTED_MEMORY_LEAK}
  1085. IndyRegisterExpectedMemoryLeak(GStackCriticalSection);
  1086. {$ENDIF}
  1087. {$ENDIF}
  1088. finalization
  1089. // Dont Free. If shutdown is from another Init section, it can cause GPF when stack
  1090. // tries to access it. App will kill it off anyways, so just let it leak
  1091. {$IFDEF FREE_ON_FINAL}
  1092. FreeAndNil(GStackCriticalSection);
  1093. {$ENDIF}
  1094. end.