Quick.Network.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. { ***************************************************************************
  2. Copyright (c) 2015-2018 Kike Pérez
  3. Unit : Quick.Network
  4. Description : Network related functions
  5. Author : Kike Pérez
  6. Version : 1.4
  7. Created : 11/07/2017
  8. Modified : 07/04/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Network;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. Math;
  28. function IntToIPv4(IPv4: LongWord): string;
  29. function IPv4ToInt(const IPv4: string) : LongWord;
  30. function IPv4ToIntReverse(const IPv4: string) : LongWord;
  31. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : string); overload;
  32. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : LongWord); overload;
  33. procedure GetIPRange(const cIP, cMask : string; out LowIP, HighIP : string);
  34. implementation
  35. function IntToIPv4(IPv4: LongWord): string;
  36. var
  37. Retvar : string;
  38. iSeg,iShift,
  39. i, iMask : LongWord;
  40. begin
  41. Retvar := '';
  42. iShift := 24;
  43. iMask := $FF000000;
  44. for i := 1 to 4 do
  45. begin
  46. iSeg := (IPv4 and iMask) shr iShift;
  47. Retvar := Retvar + IntToStr(iSeg);
  48. if i < 4 then Retvar := Retvar + '.';
  49. iMask := iMask shr 8;
  50. dec(iShift,8);
  51. end;
  52. Result := Retvar;
  53. end;
  54. function IPv4ToInt(const IPv4: string) : LongWord;
  55. var
  56. S : TStrings;
  57. begin
  58. S := TStringList.Create;
  59. try
  60. S.Delimiter := '.';
  61. S.DelimitedText := IPv4;
  62. if S.Count <> 4 then raise Exception.Create('Invalid IP4 Address string');
  63. Result := (StrToInt(S[0]) shl 24) + (StrToInt(S[1]) shl 16) + (StrToInt(S[2]) shl 8) + StrToInt(S[3]);
  64. finally
  65. S.Free;
  66. end;
  67. end;
  68. function IPv4ToIntReverse(const IPv4: string) : LongWord;
  69. var
  70. S : TStrings;
  71. begin
  72. S := TStringList.Create;
  73. try
  74. S.Delimiter := '.';
  75. S.DelimitedText := IPv4;
  76. if S.Count <> 4 then raise Exception.Create('Invalid IP4 Address string');
  77. Result := (StrToInt(S[3]) shl 24) + (StrToInt(S[2]) shl 16) + (StrToInt(S[1]) shl 8) + StrToInt(S[0]);
  78. finally
  79. S.Free;
  80. end;
  81. end;
  82. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : string);
  83. var
  84. Mask : Integer;
  85. begin
  86. Mask := StrToInt(Copy(CIDR,Pos('/',CIDR)+1,CIDR.Length));
  87. CIDR := Copy(CIDR,0,Pos('/',CIDR)-1);
  88. MinIP := IntToIPv4((IPv4ToInt(CIDR)) and ((-1 shl (32 - Mask))));
  89. MaxIP := IntToIPv4((IPv4ToInt(MinIP)) + Round(Power(2,(32 - Mask))) - 1);
  90. end;
  91. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : LongWord);
  92. var
  93. Mask : Integer;
  94. aux : string;
  95. begin
  96. Mask := StrToInt(Copy(CIDR,Pos('/',CIDR)+1,CIDR.Length));
  97. CIDR := Copy(CIDR,0,Pos('/',CIDR)-1);
  98. aux := IntToIPv4((IPv4ToInt(CIDR)) and ((-1 shl (32 - Mask))));
  99. MinIP := IPv4ToInt(aux);
  100. aux := IntToIPv4((IPv4ToInt(aux)) + Round(Power(2,(32 - Mask))) - 1);
  101. MaxIP := IPv4ToInt(aux);
  102. end;
  103. procedure GetIPRange(const cIP, cMask : string; out LowIP, HighIP : string);
  104. begin
  105. try
  106. LowIP := IntToIPv4((IPv4ToInt(cIP) and (IPv4ToInt(cMask)) + 1));
  107. HighIP := IntToIPv4(IPv4ToInt(cIP) or not(IPv4ToInt(cMask))-1);
  108. except
  109. on E : Exception do raise Exception.Create(Format('GetIPRange error: %s',[e.Message]));
  110. end;
  111. end;
  112. end.