source: trunk/IpAddress.pas

Last change on this file was 32, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from units names.
  • Modified: Use TFormEx for all forms.
File size: 5.4 KB
Line 
1unit IpAddress;
2
3interface
4
5uses
6 Classes, SysUtils, Generics;
7
8type
9 { TDomainAddress }
10
11 TDomainAddress = class(TPersistent)
12 private
13 function GetAsString: string;
14 procedure SetAsString(const Value: string);
15 public
16 Levels: TListString;
17 constructor Create;
18 destructor Destroy; override;
19 property AsString: string read GetAsString write SetAsString;
20 end;
21
22 TAddrClass = (acA, acB, acC, acD, acE);
23
24 { TIpAddress }
25
26 TIpAddress = class(TPersistent)
27 private
28 function GetAddrClass: TAddrClass;
29 function GetAsCardinal: Cardinal;
30 function GetAsString: string;
31 function GetBroadcast: Boolean;
32 procedure SetBroadcast(const Value: Boolean);
33 procedure SetAsCardinal(const Value: Cardinal);
34 procedure SetAsString(const Value: string);
35 public
36 Octets: array[0..3] of Byte;
37 procedure Assign(Source: TPersistent); override;
38 function IsAddressString(Value: string): Boolean;
39 property AsCardinal: Cardinal read GetAsCardinal write SetAsCardinal;
40 property AsString: string read GetAsString write SetAsString;
41 property AddrClass: TAddrClass read GetAddrClass;
42 property Broadcast: Boolean read GetBroadcast write SetBroadcast;
43 end;
44
45 THostAddressState = (asDomainName, asIpAddress);
46 THostAddress = class(TPersistent)
47 private
48 function GetAsString: string;
49 procedure SetAsString(const Value: string);
50 public
51 State: THostAddressState;
52 DomainName: TDomainAddress;
53 IpAddress: TIpAddress;
54 constructor Create;
55 destructor Destroy; override;
56 property AsString: string read GetAsString write SetAsString;
57 end;
58
59
60implementation
61
62resourcestring
63 SStringToIPConversionError = 'String to IP address conversion error';
64
65{ TIpAddress }
66
67procedure TIpAddress.Assign(Source: TPersistent);
68var
69 I: Integer;
70begin
71 if Assigned(Source) then begin
72 if Source is TIpAddress then begin
73 for I := 0 to High(Octets) do
74 Octets[I] := TIpAddress(Source).Octets[I];
75 end else inherited;
76 end else inherited;
77end;
78
79function TIpAddress.IsAddressString(Value: string): Boolean;
80var
81 Parts: TListString;
82begin
83 Result := True;
84 try
85 Parts := TListString.Create;
86 Parts.Explode('.', Value);
87 if Parts.Count = 4 then begin
88 if (StrToInt(Parts[3]) < 0) or (StrToInt(Parts[3]) > 255) then Result := False;
89 if (StrToInt(Parts[2]) < 0) or (StrToInt(Parts[2]) > 255) then Result := False;
90 if (StrToInt(Parts[1]) < 0) or (StrToInt(Parts[1]) > 255) then Result := False;
91 if (StrToInt(Parts[0]) < 0) or (StrToInt(Parts[0]) > 255) then Result := False;
92 end else Result := False;
93 finally
94 Parts.Free;
95 end;
96end;
97
98function TIpAddress.GetAddrClass: TAddrClass;
99begin
100 if (Octets[3] and $80) = 0 then Result := acA
101 else begin
102 if (Octets[3] and $40) = 0 then Result := acB
103 else begin
104 if (Octets[3] and $20) = 0 then Result := acC
105 else begin
106 if (Octets[3] and $10) = 0 then Result := acD
107 else Result := acE;
108 end;
109 end;
110 end;
111end;
112
113function TIpAddress.GetAsCardinal: Cardinal;
114begin
115 Result := Octets[0] or (Octets[1] shl 8) or (Octets[2] shl 16) or (Octets[3] shl 24);
116end;
117
118function TIpAddress.GetAsString: string;
119begin
120 Result := IntToStr(Octets[3]) + '.' + IntToStr(Octets[2]) + '.' +
121 IntToStr(Octets[1]) + '.' + IntToStr(Octets[0]);
122end;
123
124function TIpAddress.GetBroadcast: Boolean;
125begin
126 Result := AsCardinal = High(Cardinal);
127end;
128
129procedure TIpAddress.SetAsCardinal(const Value: Cardinal);
130begin
131 Octets[0] := Byte(Value);
132 Octets[1] := Byte(Value shr 8);
133 Octets[2] := Byte(Value shr 16);
134 Octets[3] := Byte(Value shr 24);
135end;
136
137procedure TIpAddress.SetAsString(const Value: string);
138var
139 Parts: TListString;
140begin
141 try
142 Parts := TListString.Create;
143 Parts.Explode('.', Value);
144 try
145// if Length(Parts) = 4 then begin
146 Octets[0] := StrToInt(Parts[3]);
147 Octets[1] := StrToInt(Parts[2]);
148 Octets[2] := StrToInt(Parts[1]);
149 Octets[3] := StrToInt(Parts[0]);
150// end else raise EConvertError.Create('String to IP address conversion error');
151 except
152 raise EConvertError.Create(SStringToIPConversionError);
153 end;
154 finally
155 Parts.Free;
156 end;
157end;
158
159procedure TIpAddress.SetBroadcast(const Value: Boolean);
160begin
161 AsCardinal := High(Cardinal);
162end;
163
164{ TDomainAddress }
165
166function TDomainAddress.GetAsString: string;
167begin
168 try
169 Levels.Reverse;
170 Result := Levels.Implode('.');
171 finally
172 Levels.Reverse;
173 end;
174end;
175
176procedure TDomainAddress.SetAsString(const Value: string);
177begin
178 Levels.Explode('.', Value);
179 Levels.Reverse;
180end;
181
182constructor TDomainAddress.Create;
183begin
184 Levels := TListString.Create;
185end;
186
187destructor TDomainAddress.Destroy;
188begin
189 FreeAndNil(Levels);
190 inherited;
191end;
192
193{ THostAddress }
194
195constructor THostAddress.Create;
196begin
197 DomainName := TDomainAddress.Create;
198 IpAddress := TIpAddress.Create;
199 State := asDomainName;
200 DomainName.AsString := 'localhost';
201end;
202
203destructor THostAddress.Destroy;
204begin
205 FreeAndNil(DomainName);
206 FreeAndNil(IpAddress);
207 inherited;
208end;
209
210function THostAddress.GetAsString: string;
211begin
212 case State of
213 asDomainName: Result := DomainName.AsString;
214 asIpAddress: Result := IpAddress.AsString;
215 end;
216end;
217
218procedure THostAddress.SetAsString(const Value: string);
219begin
220 if IpAddress.IsAddressString(Value) then begin
221 State := asIpAddress;
222 IpAddress.AsString := Value;
223 end else begin
224 State := asDomainName;
225 DomainName.AsString := Value;
226 end;
227end;
228
229end.
230
Note: See TracBrowser for help on using the repository browser.