Quick.Network.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. { ***************************************************************************
  2. Copyright (c) 2015-2017 Kike Pérez
  3. Unit : Quick.Network
  4. Description : Network related functions
  5. Author : Kike Pérez
  6. Version : 1.2
  7. Created : 11/07/2017
  8. Modified : 09/10/2017
  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. interface
  23. uses
  24. Classes,
  25. SysUtils,
  26. Math;
  27. function IntToIPv4(IPv4: LongWord): string;
  28. function IPv4ToInt(const IPv4: string) : LongWord;
  29. function IPv4ToIntReverse(const IPv4: string) : LongWord;
  30. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : string); overload;
  31. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : LongWord); overload;
  32. procedure GetIPRange(const cIP, cMask : string; out LowIP, HighIP : string);
  33. implementation
  34. function IntToIPv4(IPv4: LongWord): string;
  35. var
  36. Retvar : string;
  37. iSeg,iShift,
  38. i, iMask : LongWord;
  39. begin
  40. Retvar := '';
  41. iShift := 24;
  42. iMask := $FF000000;
  43. for i := 1 to 4 do
  44. begin
  45. iSeg := (IPv4 and iMask) shr iShift;
  46. Retvar := Retvar + IntToStr(iSeg);
  47. if i < 4 then Retvar := Retvar + '.';
  48. iMask := iMask shr 8;
  49. dec(iShift,8);
  50. end;
  51. Result := Retvar;
  52. end;
  53. function IPv4ToInt(const IPv4: string) : LongWord;
  54. var
  55. S : TStrings;
  56. begin
  57. S := TStringList.Create;
  58. try
  59. S.Delimiter := '.';
  60. S.DelimitedText := IPv4;
  61. if S.Count <> 4 then raise Exception.Create('Invalid IP4 Address string');
  62. Result := (StrToInt(S[0]) shl 24) + (StrToInt(S[1]) shl 16) + (StrToInt(S[2]) shl 8) + StrToInt(S[3]);
  63. finally
  64. S.Free;
  65. end;
  66. end;
  67. function IPv4ToIntReverse(const IPv4: string) : LongWord;
  68. var
  69. S : TStrings;
  70. begin
  71. S := TStringList.Create;
  72. try
  73. S.Delimiter := '.';
  74. S.DelimitedText := IPv4;
  75. if S.Count <> 4 then raise Exception.Create('Invalid IP4 Address string');
  76. Result := (StrToInt(S[3]) shl 24) + (StrToInt(S[2]) shl 16) + (StrToInt(S[1]) shl 8) + StrToInt(S[0]);
  77. finally
  78. S.Free;
  79. end;
  80. end;
  81. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : string);
  82. var
  83. Mask : Integer;
  84. begin
  85. Mask := StrToInt(Copy(CIDR,Pos('/',CIDR)+1,CIDR.Length));
  86. CIDR := Copy(CIDR,0,Pos('/',CIDR)-1);
  87. MinIP := IntToIPv4((IPv4ToInt(CIDR)) and ((-1 shl (32 - Mask))));
  88. MaxIP := IntToIPv4((IPv4ToInt(MinIP)) + Round(Power(2,(32 - Mask))) - 1);
  89. end;
  90. procedure CIDRToRange(CIDR : string; out MinIP,MaxIP : LongWord);
  91. var
  92. Mask : Integer;
  93. aux : string;
  94. begin
  95. Mask := StrToInt(Copy(CIDR,Pos('/',CIDR)+1,CIDR.Length));
  96. CIDR := Copy(CIDR,0,Pos('/',CIDR)-1);
  97. aux := IntToIPv4((IPv4ToInt(CIDR)) and ((-1 shl (32 - Mask))));
  98. MinIP := IPv4ToInt(aux);
  99. aux := IntToIPv4((IPv4ToInt(aux)) + Round(Power(2,(32 - Mask))) - 1);
  100. MaxIP := IPv4ToInt(aux);
  101. end;
  102. procedure GetIPRange(const cIP, cMask : string; out LowIP, HighIP : string);
  103. begin
  104. try
  105. LowIP := IntToIPv4((IPv4ToInt(cIP) and (IPv4ToInt(cMask)) + 1));
  106. HighIP := IntToIPv4(IPv4ToInt(cIP) or not(IPv4ToInt(cMask))-1);
  107. except
  108. on E : Exception do raise Exception.Create(Format('GetIPRange error: %s',[e.Message]));
  109. end;
  110. end;
  111. end.