IdIPMCastBase.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.4 2004.02.03 5:43:52 PM czhower
  18. Name changes
  19. Rev 1.3 1/21/2004 3:11:06 PM JPMugaas
  20. InitComponent
  21. Rev 1.2 10/26/2003 09:11:50 AM JPMugaas
  22. Should now work in NET.
  23. Rev 1.1 2003.10.12 4:03:56 PM czhower
  24. compile todos
  25. Rev 1.0 11/13/2002 07:55:16 AM JPMugaas
  26. }
  27. unit IdIPMCastBase;
  28. interface
  29. {$I IdCompilerDefines.inc}
  30. //here to flip FPC into Delphi mode
  31. uses
  32. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  33. Classes,
  34. {$ENDIF}
  35. IdComponent, IdException, IdGlobal, IdSocketHandle,
  36. IdStack;
  37. (*$HPPEMIT '#if defined(_VCL_ALIAS_RECORDS)' *)
  38. (*$HPPEMIT '#if !defined(UNICODE)' *)
  39. (*$HPPEMIT '#pragma alias "@Idipmcastbase@TIdIPMCastBase@SetPortA$qqrxi"="@Idipmcastbase@TIdIPMCastBase@SetPort$qqrxi"' *)
  40. (*$HPPEMIT '#else' *)
  41. (*$HPPEMIT '#pragma alias "@Idipmcastbase@TIdIPMCastBase@SetPortW$qqrxi"="@Idipmcastbase@TIdIPMCastBase@SetPort$qqrxi"' *)
  42. (*$HPPEMIT '#endif' *)
  43. (*$HPPEMIT '#endif' *)
  44. const
  45. IPMCastLo = 224;
  46. IPMCastHi = 239;
  47. type
  48. TIdIPMv6Scope = ( IdIPv6MC_InterfaceLocal,
  49. { Interface-Local scope spans only a single interface on a node
  50. and is useful only for loopback transmission of multicast.}
  51. IdIPv6MC_LinkLocal,
  52. { Link-Local multicast scope spans the same topological region as
  53. the corresponding unicast scope. }
  54. IdIPv6MC_AdminLocal,
  55. { Admin-Local scope is the smallest scope that must be
  56. administratively configured, i.e., not automatically derived
  57. from physical connectivity or other, non-multicast-related
  58. configuration.}
  59. IdIPv6MC_SiteLocal,
  60. { Site-Local scope is intended to span a single site. }
  61. IdIPv6MC_OrgLocal,
  62. {Organization-Local scope is intended to span multiple sites
  63. belonging to a single organization.}
  64. IdIPv6MC_Global);
  65. TIdIPMCValidScopes = 0..$F;
  66. TIdIPMCastBase = class(TIdComponent)
  67. protected
  68. FDsgnActive: Boolean;
  69. FMulticastGroup: String;
  70. FPort: Integer;
  71. FIPVersion: TIdIPVersion;
  72. FReuseSocket: TIdReuseSocket;
  73. //
  74. procedure CloseBinding; virtual; abstract;
  75. function GetActive: Boolean; virtual;
  76. function GetBinding: TIdSocketHandle; virtual; abstract;
  77. procedure Loaded; override;
  78. procedure SetActive(const Value: Boolean); virtual;
  79. procedure SetMulticastGroup(const Value: string); virtual;
  80. procedure SetPort(const Value: integer); virtual;
  81. function GetIPVersion: TIdIPVersion; virtual;
  82. procedure SetIPVersion(const AValue: TIdIPVersion); virtual;
  83. //
  84. property Active: Boolean read GetActive write SetActive Default False;
  85. property MulticastGroup: string read FMulticastGroup write SetMulticastGroup;
  86. property Port: Integer read FPort write SetPort;
  87. property IPVersion: TIdIPVersion read GetIPVersion write SetIPVersion default ID_DEFAULT_IP_VERSION;
  88. procedure InitComponent; override;
  89. public
  90. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  91. constructor Create(AOwner: TComponent); reintroduce; overload;
  92. {$ENDIF}
  93. function IsValidMulticastGroup(const Value: string): Boolean;
  94. {These two items are helper functions that allow you to specify the scope for
  95. a Variable Scope Multicast Addresses. Some are listed in IdAssignedNumbers
  96. as the Id_IPv6MC_V_ constants. You can't use them out of the box in the
  97. MulticastGroup property because you need to specify the scope. This provides
  98. you with more flexibility than you would get with IPv4 multicasting.}
  99. class function SetIPv6AddrScope(const AVarIPv6Addr : String; const AScope : TIdIPMv6Scope ) : String; overload;
  100. class function SetIPv6AddrScope(const AVarIPv6Addr : String; const AScope : TIdIPMCValidScopes): String; overload;
  101. //
  102. property ReuseSocket: TIdReuseSocket read FReuseSocket write FReuseSocket default rsOSDependent;
  103. published
  104. end;
  105. EIdMCastException = Class(EIdException);
  106. EIdMCastNoBindings = class(EIdMCastException);
  107. EIdMCastNotValidAddress = class(EIdMCastException);
  108. EIdMCastReceiveErrorZeroBytes = class(EIdMCastException);
  109. const
  110. DEF_IPv6_MGROUP = 'FF01:0:0:0:0:0:0:1'; {do not localize}
  111. implementation
  112. uses
  113. IdAssignedNumbers,
  114. IdResourceStringsCore, IdStackConsts, SysUtils;
  115. { TIdIPMCastBase }
  116. {$IFDEF WORKAROUND_INLINE_CONSTRUCTORS}
  117. constructor TIdIPMCastBase.Create(AOwner: TComponent);
  118. begin
  119. inherited Create(AOwner);
  120. end;
  121. {$ENDIF}
  122. function TIdIPMCastBase.GetIPVersion: TIdIPVersion;
  123. begin
  124. Result := FIPVersion;
  125. end;
  126. procedure TIdIPMCastBase.InitComponent;
  127. begin
  128. inherited InitComponent;
  129. FIPVersion := ID_DEFAULT_IP_VERSION;
  130. FMultiCastGroup := {$IFDEF IdIPv6}DEF_IPv6_MGROUP{$ELSE}Id_IPMC_All_Systems{$ENDIF};
  131. FReuseSocket := rsOSDependent;
  132. end;
  133. function TIdIPMCastBase.GetActive: Boolean;
  134. begin
  135. Result := FDsgnActive;
  136. end;
  137. function TIdIPMCastBase.IsValidMulticastGroup(const Value: string): Boolean;
  138. begin
  139. //just here to prevent a warning from Delphi about an unitialized result
  140. Result := False;
  141. case FIPVersion of
  142. Id_IPv4 : Result := GStack.IsValidIPv4MulticastGroup(Value);
  143. Id_IPv6 : Result := GStack.IsValidIPv6MulticastGroup(Value);
  144. end;
  145. end;
  146. procedure TIdIPMCastBase.Loaded;
  147. begin
  148. inherited Loaded;
  149. if FDsgnActive then begin
  150. FDsgnActive := False;
  151. Active := True;
  152. end;
  153. end;
  154. procedure TIdIPMCastBase.SetActive(const Value: Boolean);
  155. begin
  156. if IsDesignTime or IsLoading then begin
  157. // don't activate at designtime (or during loading of properties) {Do not Localize}
  158. FDsgnActive := Value;
  159. end
  160. else if Active <> Value then begin
  161. if Value then begin
  162. GetBinding;
  163. end else begin
  164. CloseBinding;
  165. end;
  166. end;
  167. end;
  168. class function TIdIPMCastBase.SetIPv6AddrScope(const AVarIPv6Addr: String;
  169. const AScope: TIdIPMv6Scope): String;
  170. begin
  171. case AScope of
  172. IdIPv6MC_InterfaceLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$1);
  173. IdIPv6MC_LinkLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$2);
  174. IdIPv6MC_AdminLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$4);
  175. IdIPv6MC_SiteLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$5);
  176. IdIPv6MC_OrgLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$8);
  177. IdIPv6MC_Global : Result := SetIPv6AddrScope(AVarIPv6Addr,$E);
  178. else
  179. Result := AVarIPv6Addr;
  180. end;
  181. end;
  182. class function TIdIPMCastBase.SetIPv6AddrScope(const AVarIPv6Addr: String;
  183. const AScope: TIdIPMCValidScopes): String;
  184. begin
  185. //Replace the X in the Id_IPv6MC_V_ constants with the specified scope
  186. Result := ReplaceOnlyFirst(AVarIPv6Addr, 'X', IntToHex(AScope,1)); {do not localize}
  187. end;
  188. procedure TIdIPMCastBase.SetIPVersion(const AValue: TIdIPVersion);
  189. begin
  190. if AValue <> IPVersion then
  191. begin
  192. Active := False;
  193. FIPVersion := AValue;
  194. if not IsLoading then begin
  195. case AValue of
  196. Id_IPv4: FMulticastGroup := Id_IPMC_All_Systems;
  197. Id_IPv6: FMulticastGroup := DEF_IPv6_MGROUP;
  198. end;
  199. end;
  200. end;
  201. end;
  202. procedure TIdIPMCastBase.SetMulticastGroup(const Value: string);
  203. begin
  204. if FMulticastGroup <> Value then begin
  205. if (not IsLoading) and (not IsValidMulticastGroup(Value)) then begin
  206. raise EIdMCastNotValidAddress.Create(RSIPMCastInvalidMulticastAddress);
  207. end;
  208. Active := False;
  209. FMulticastGroup := Value;
  210. end;
  211. end;
  212. procedure TIdIPMCastBase.SetPort(const Value: integer);
  213. begin
  214. if FPort <> Value then begin
  215. Active := False;
  216. FPort := Value;
  217. end;
  218. end;
  219. end.