IdStack.pas 41 KB

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