1 | unit IpAddress;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
60 | implementation
|
---|
61 |
|
---|
62 | resourcestring
|
---|
63 | SStringToIPConversionError = 'String to IP address conversion error';
|
---|
64 |
|
---|
65 | { TIpAddress }
|
---|
66 |
|
---|
67 | procedure TIpAddress.Assign(Source: TPersistent);
|
---|
68 | var
|
---|
69 | I: Integer;
|
---|
70 | begin
|
---|
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;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | function TIpAddress.IsAddressString(Value: string): Boolean;
|
---|
80 | var
|
---|
81 | Parts: TListString;
|
---|
82 | begin
|
---|
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;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | function TIpAddress.GetAddrClass: TAddrClass;
|
---|
99 | begin
|
---|
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;
|
---|
111 | end;
|
---|
112 |
|
---|
113 | function TIpAddress.GetAsCardinal: Cardinal;
|
---|
114 | begin
|
---|
115 | Result := Octets[0] or (Octets[1] shl 8) or (Octets[2] shl 16) or (Octets[3] shl 24);
|
---|
116 | end;
|
---|
117 |
|
---|
118 | function TIpAddress.GetAsString: string;
|
---|
119 | begin
|
---|
120 | Result := IntToStr(Octets[3]) + '.' + IntToStr(Octets[2]) + '.' +
|
---|
121 | IntToStr(Octets[1]) + '.' + IntToStr(Octets[0]);
|
---|
122 | end;
|
---|
123 |
|
---|
124 | function TIpAddress.GetBroadcast: Boolean;
|
---|
125 | begin
|
---|
126 | Result := AsCardinal = High(Cardinal);
|
---|
127 | end;
|
---|
128 |
|
---|
129 | procedure TIpAddress.SetAsCardinal(const Value: Cardinal);
|
---|
130 | begin
|
---|
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);
|
---|
135 | end;
|
---|
136 |
|
---|
137 | procedure TIpAddress.SetAsString(const Value: string);
|
---|
138 | var
|
---|
139 | Parts: TListString;
|
---|
140 | begin
|
---|
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;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TIpAddress.SetBroadcast(const Value: Boolean);
|
---|
160 | begin
|
---|
161 | AsCardinal := High(Cardinal);
|
---|
162 | end;
|
---|
163 |
|
---|
164 | { TDomainAddress }
|
---|
165 |
|
---|
166 | function TDomainAddress.GetAsString: string;
|
---|
167 | begin
|
---|
168 | try
|
---|
169 | Levels.Reverse;
|
---|
170 | Result := Levels.Implode('.');
|
---|
171 | finally
|
---|
172 | Levels.Reverse;
|
---|
173 | end;
|
---|
174 | end;
|
---|
175 |
|
---|
176 | procedure TDomainAddress.SetAsString(const Value: string);
|
---|
177 | begin
|
---|
178 | Levels.Explode('.', Value);
|
---|
179 | Levels.Reverse;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | constructor TDomainAddress.Create;
|
---|
183 | begin
|
---|
184 | Levels := TListString.Create;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | destructor TDomainAddress.Destroy;
|
---|
188 | begin
|
---|
189 | FreeAndNil(Levels);
|
---|
190 | inherited;
|
---|
191 | end;
|
---|
192 |
|
---|
193 | { THostAddress }
|
---|
194 |
|
---|
195 | constructor THostAddress.Create;
|
---|
196 | begin
|
---|
197 | DomainName := TDomainAddress.Create;
|
---|
198 | IpAddress := TIpAddress.Create;
|
---|
199 | State := asDomainName;
|
---|
200 | DomainName.AsString := 'localhost';
|
---|
201 | end;
|
---|
202 |
|
---|
203 | destructor THostAddress.Destroy;
|
---|
204 | begin
|
---|
205 | FreeAndNil(DomainName);
|
---|
206 | FreeAndNil(IpAddress);
|
---|
207 | inherited;
|
---|
208 | end;
|
---|
209 |
|
---|
210 | function THostAddress.GetAsString: string;
|
---|
211 | begin
|
---|
212 | case State of
|
---|
213 | asDomainName: Result := DomainName.AsString;
|
---|
214 | asIpAddress: Result := IpAddress.AsString;
|
---|
215 | end;
|
---|
216 | end;
|
---|
217 |
|
---|
218 | procedure THostAddress.SetAsString(const Value: string);
|
---|
219 | begin
|
---|
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;
|
---|
227 | end;
|
---|
228 |
|
---|
229 | end.
|
---|
230 |
|
---|