IdIPMCastBase.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. Classes,
  33. IdComponent, IdException, IdGlobal, IdSocketHandle,
  34. IdStack;
  35. (*$HPPEMIT '#if defined(_VCL_ALIAS_RECORDS)' *)
  36. (*$HPPEMIT '#if !defined(UNICODE)' *)
  37. (*$HPPEMIT '#pragma alias "@Idipmcastbase@TIdIPMCastBase@SetPortA$qqrxi"="@Idipmcastbase@TIdIPMCastBase@SetPort$qqrxi"' *)
  38. (*$HPPEMIT '#else' *)
  39. (*$HPPEMIT '#pragma alias "@Idipmcastbase@TIdIPMCastBase@SetPortW$qqrxi"="@Idipmcastbase@TIdIPMCastBase@SetPort$qqrxi"' *)
  40. (*$HPPEMIT '#endif' *)
  41. (*$HPPEMIT '#endif' *)
  42. // TODO: when compiling with bcc64x, use this pragma instead:
  43. // #pragma comment(linker, "/alternatename:<name1>=<name2>")
  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. public
  89. constructor Create(AOwner: TComponent); override;
  90. //
  91. function IsValidMulticastGroup(const Value: string): Boolean;
  92. {These two items are helper functions that allow you to specify the scope for
  93. a Variable Scope Multicast Addresses. Some are listed in IdAssignedNumbers
  94. as the Id_IPv6MC_V_ constants. You can't use them out of the box in the
  95. MulticastGroup property because you need to specify the scope. This provides
  96. you with more flexibility than you would get with IPv4 multicasting.}
  97. class function SetIPv6AddrScope(const AVarIPv6Addr : String; const AScope : TIdIPMv6Scope ) : String; overload;
  98. class function SetIPv6AddrScope(const AVarIPv6Addr : String; const AScope : TIdIPMCValidScopes): String; overload;
  99. //
  100. property ReuseSocket: TIdReuseSocket read FReuseSocket write FReuseSocket default rsOSDependent;
  101. published
  102. end;
  103. EIdMCastException = Class(EIdException);
  104. EIdMCastNoBindings = class(EIdMCastException);
  105. EIdMCastNotValidAddress = class(EIdMCastException);
  106. EIdMCastReceiveErrorZeroBytes = class(EIdMCastException);
  107. const
  108. DEF_IPv6_MGROUP = 'FF01:0:0:0:0:0:0:1'; {do not localize}
  109. implementation
  110. uses
  111. IdAssignedNumbers,
  112. IdResourceStringsCore,
  113. SysUtils;
  114. { TIdIPMCastBase }
  115. constructor TIdIPMCastBase.Create(AOwner: TComponent);
  116. begin
  117. inherited Create(AOwner);
  118. FIPVersion := ID_DEFAULT_IP_VERSION;
  119. FMultiCastGroup := {$IFDEF IdIPv6}DEF_IPv6_MGROUP{$ELSE}Id_IPMC_All_Systems{$ENDIF};
  120. FReuseSocket := rsOSDependent;
  121. end;
  122. function TIdIPMCastBase.GetIPVersion: TIdIPVersion;
  123. begin
  124. Result := FIPVersion;
  125. end;
  126. function TIdIPMCastBase.GetActive: Boolean;
  127. begin
  128. Result := FDsgnActive;
  129. end;
  130. function TIdIPMCastBase.IsValidMulticastGroup(const Value: string): Boolean;
  131. begin
  132. //just here to prevent a warning from Delphi about an unitialized result
  133. Result := False;
  134. case FIPVersion of
  135. Id_IPv4 : Result := GStack.IsValidIPv4MulticastGroup(Value);
  136. Id_IPv6 : Result := GStack.IsValidIPv6MulticastGroup(Value);
  137. end;
  138. end;
  139. procedure TIdIPMCastBase.Loaded;
  140. begin
  141. inherited Loaded;
  142. if FDsgnActive then begin
  143. FDsgnActive := False;
  144. Active := True;
  145. end;
  146. end;
  147. procedure TIdIPMCastBase.SetActive(const Value: Boolean);
  148. begin
  149. if IsDesignTime or IsLoading then begin
  150. // don't activate at designtime (or during loading of properties) {Do not Localize}
  151. FDsgnActive := Value;
  152. end
  153. else if Active <> Value then begin
  154. if Value then begin
  155. GetBinding;
  156. end else begin
  157. CloseBinding;
  158. end;
  159. end;
  160. end;
  161. class function TIdIPMCastBase.SetIPv6AddrScope(const AVarIPv6Addr: String;
  162. const AScope: TIdIPMv6Scope): String;
  163. begin
  164. case AScope of
  165. IdIPv6MC_InterfaceLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$1);
  166. IdIPv6MC_LinkLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$2);
  167. IdIPv6MC_AdminLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$4);
  168. IdIPv6MC_SiteLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$5);
  169. IdIPv6MC_OrgLocal : Result := SetIPv6AddrScope(AVarIPv6Addr,$8);
  170. IdIPv6MC_Global : Result := SetIPv6AddrScope(AVarIPv6Addr,$E);
  171. else
  172. Result := AVarIPv6Addr;
  173. end;
  174. end;
  175. class function TIdIPMCastBase.SetIPv6AddrScope(const AVarIPv6Addr: String;
  176. const AScope: TIdIPMCValidScopes): String;
  177. begin
  178. //Replace the X in the Id_IPv6MC_V_ constants with the specified scope
  179. Result := ReplaceOnlyFirst(AVarIPv6Addr, 'X', IntToHex(AScope,1)); {do not localize}
  180. end;
  181. procedure TIdIPMCastBase.SetIPVersion(const AValue: TIdIPVersion);
  182. begin
  183. if AValue <> IPVersion then
  184. begin
  185. Active := False;
  186. FIPVersion := AValue;
  187. if not IsLoading then begin
  188. case AValue of
  189. Id_IPv4: FMulticastGroup := Id_IPMC_All_Systems;
  190. Id_IPv6: FMulticastGroup := DEF_IPv6_MGROUP;
  191. end;
  192. end;
  193. end;
  194. end;
  195. procedure TIdIPMCastBase.SetMulticastGroup(const Value: string);
  196. begin
  197. if FMulticastGroup <> Value then begin
  198. if (not IsLoading) and (not IsValidMulticastGroup(Value)) then begin
  199. raise EIdMCastNotValidAddress.Create(RSIPMCastInvalidMulticastAddress);
  200. end;
  201. Active := False;
  202. FMulticastGroup := Value;
  203. end;
  204. end;
  205. procedure TIdIPMCastBase.SetPort(const Value: integer);
  206. begin
  207. if FPort <> Value then begin
  208. Active := False;
  209. FPort := Value;
  210. end;
  211. end;
  212. end.