| 1 | unit UIPAddress;
|
|---|
| 2 |
|
|---|
| 3 | // Date: 2010-05-14
|
|---|
| 4 |
|
|---|
| 5 | {$mode objfpc}{$H+}
|
|---|
| 6 |
|
|---|
| 7 | interface
|
|---|
| 8 |
|
|---|
| 9 | uses
|
|---|
| 10 | Classes, SysUtils;
|
|---|
| 11 |
|
|---|
| 12 | type
|
|---|
| 13 |
|
|---|
| 14 | { TIPAddress }
|
|---|
| 15 |
|
|---|
| 16 | TIPAddress = class
|
|---|
| 17 | Address: Cardinal;
|
|---|
| 18 | Prefix: Byte;
|
|---|
| 19 | function GetNetMask: Cardinal;
|
|---|
| 20 | function GetString: string;
|
|---|
| 21 | procedure SetString(Value: string);
|
|---|
| 22 | constructor Create;
|
|---|
| 23 | procedure Assign(Source: TObject);
|
|---|
| 24 | property AsString: string read GetString write SetString;
|
|---|
| 25 | end;
|
|---|
| 26 |
|
|---|
| 27 | implementation
|
|---|
| 28 |
|
|---|
| 29 | { TIPAddress }
|
|---|
| 30 |
|
|---|
| 31 | function TIPAddress.GetNetMask: Cardinal;
|
|---|
| 32 | begin
|
|---|
| 33 | Result := Cardinal((1 shl (32 - Prefix)) - 1) xor $ffffffff;
|
|---|
| 34 | end;
|
|---|
| 35 |
|
|---|
| 36 | function TIPAddress.GetString: string;
|
|---|
| 37 | begin
|
|---|
| 38 | Result := IntToStr((Address shr 24) and $ff) + '.' + IntToStr((Address shr 16) and $ff) + '.' +
|
|---|
| 39 | IntToStr((Address shr 8) and $ff) + '.' + IntToStr((Address shr 0) and $ff);
|
|---|
| 40 | if(Prefix <> 32) then Result := Result + '/' + IntToStr(Prefix);
|
|---|
| 41 | end;
|
|---|
| 42 |
|
|---|
| 43 | procedure TIPAddress.SetString(Value: string);
|
|---|
| 44 | var
|
|---|
| 45 | Part: string;
|
|---|
| 46 | Octet: Integer;
|
|---|
| 47 | TempValue: Integer;
|
|---|
| 48 | begin
|
|---|
| 49 | if Pos('/', Value) > 0 then begin
|
|---|
| 50 | if TryStrToInt(Copy(Value, Pos('/', Value) + 1, Length(Value)), TempValue) then
|
|---|
| 51 | Prefix := TempValue else Prefix := 32;
|
|---|
| 52 | Delete(Value, Pos('/', Value), Length(Value));
|
|---|
| 53 | end else Prefix := 32;
|
|---|
| 54 | if Pos('.', Value) > 0 then begin
|
|---|
| 55 | Part := Copy(Value, 1, Pos('.', Value) - 1);
|
|---|
| 56 | Delete(Value, 1, Pos('.', Value));
|
|---|
| 57 | if TryStrToInt(Part, TempValue) then Octet := TempValue
|
|---|
| 58 | else Octet := 0;
|
|---|
| 59 | Address := Address or Cardinal((Octet and $ff) shl 24);
|
|---|
| 60 | end;
|
|---|
| 61 | if Pos('.', Value) > 0 then begin
|
|---|
| 62 | Part := Copy(Value, 1, Pos('.', Value) - 1);
|
|---|
| 63 | Delete(Value, 1, Pos('.', Value));
|
|---|
| 64 | if TryStrToInt(Part, TempValue) then Octet := TempValue
|
|---|
| 65 | else Octet := 0;
|
|---|
| 66 | Address := Address or Cardinal((Octet and $ff) shl 16);
|
|---|
| 67 | end;
|
|---|
| 68 | if Pos('.', Value) > 0 then begin
|
|---|
| 69 | Part := Copy(Value, 1, Pos('.', Value) - 1);
|
|---|
| 70 | Delete(Value, 1, Pos('.', Value));
|
|---|
| 71 | if TryStrToInt(Part, TempValue) then Octet := TempValue
|
|---|
| 72 | else Octet := 0;
|
|---|
| 73 | Address := Address or Cardinal((Octet and $ff) shl 8);
|
|---|
| 74 | end;
|
|---|
| 75 | if TryStrToInt(Value, TempValue) then Octet := TempValue
|
|---|
| 76 | else Octet := 0;
|
|---|
| 77 | Address := Address or Cardinal((Octet and $ff) shl 0);
|
|---|
| 78 | end;
|
|---|
| 79 |
|
|---|
| 80 | constructor TIPAddress.Create;
|
|---|
| 81 | begin
|
|---|
| 82 | Address := 0;
|
|---|
| 83 | Prefix := 32;
|
|---|
| 84 | end;
|
|---|
| 85 |
|
|---|
| 86 | procedure TIPAddress.Assign(Source: TObject);
|
|---|
| 87 | begin
|
|---|
| 88 | if Source is TIPAddress then begin
|
|---|
| 89 | Address := TIPAddress(Source).Address;
|
|---|
| 90 | Prefix := TIPAddress(Source).Prefix;
|
|---|
| 91 | end;
|
|---|
| 92 | end;
|
|---|
| 93 |
|
|---|
| 94 | end.
|
|---|
| 95 |
|
|---|