hs.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. function HostAddrToStr (Entry : THostAddr) : String;
  2. Var Dummy : String[4];
  3. I : Longint;
  4. begin
  5. HostAddrToStr:='';
  6. For I:=1 to 4 do
  7. begin
  8. Str(Entry[I],Dummy);
  9. HostAddrToStr:=HostAddrToStr+Dummy;
  10. If I<4 Then
  11. HostAddrToStr:=HostAddrToStr+'.';
  12. end;
  13. end;
  14. function StrToHostAddr(IP : String) : THostAddr ;
  15. Var
  16. Dummy : String;
  17. I : Longint;
  18. J : Integer;
  19. Temp : THostAddr;
  20. begin
  21. Result:=NoAddress;
  22. For I:=1 to 4 do
  23. begin
  24. If I<4 Then
  25. begin
  26. J:=Pos('.',IP);
  27. If J=0 then
  28. exit;
  29. Dummy:=Copy(IP,1,J-1);
  30. Delete (IP,1,J);
  31. end
  32. else
  33. Dummy:=IP;
  34. Val (Dummy,Temp[I],J);
  35. If J<>0 then Exit;
  36. end;
  37. Result:=Temp;
  38. end;
  39. function NetAddrToStr (Entry : TNetAddr) : String;
  40. Var Dummy : String[4];
  41. I : Longint;
  42. begin
  43. NetAddrToStr:='';
  44. For I:=4 downto 1 do
  45. begin
  46. Str(Entry[I],Dummy);
  47. NetAddrToStr:=NetAddrToStr+Dummy;
  48. If I>1 Then
  49. NetAddrToStr:=NetAddrToStr+'.';
  50. end;
  51. end;
  52. function StrToNetAddr(IP : String) : TNetAddr;
  53. begin
  54. StrToNetAddr:=TNetAddr(StrToHostAddr(IP));
  55. end;
  56. Function HostToNet (Host : ThostAddr) : THostAddr;
  57. begin
  58. Result[1]:=Host[4];
  59. Result[2]:=Host[3];
  60. Result[3]:=Host[2];
  61. Result[4]:=Host[1];
  62. end;
  63. Function NetToHost (Net : TNetAddr) : TNetAddr;
  64. begin
  65. Result[1]:=Net[4];
  66. Result[2]:=Net[3];
  67. Result[3]:=Net[2];
  68. Result[4]:=Net[1];
  69. end;
  70. Function HostToNet (Host : Longint) : Longint;
  71. begin
  72. Result:=Longint(HostToNet(THostAddr(host)));
  73. end;
  74. Function NetToHost (Net : Longint) : Longint;
  75. begin
  76. Result:=Longint(NetToHost(TNetAddr(Net)));
  77. end;
  78. Function ShortHostToNet (Host : Word) : Word;
  79. begin
  80. ShortHostToNet:=lo(host)*256+Hi(Host);
  81. end;
  82. Function ShortNetToHost (Net : Word) : Word;
  83. begin
  84. ShortNetToHost:=lo(Net)*256+Hi(Net);
  85. end;
  86. function HostAddrToStr6 (Entry : THostAddr6) : String;
  87. var
  88. i: byte;
  89. zr1,zr2: set of byte;
  90. zc1,zc2: byte;
  91. have_skipped: boolean;
  92. begin
  93. zr1 := [];
  94. zr2 := [];
  95. zc1 := 0;
  96. zc2 := 0;
  97. for i := 0 to 7 do begin
  98. if Entry[i] = 0 then begin
  99. include(zr2, i);
  100. inc(zc2);
  101. end else begin
  102. if zc1 < zc2 then begin
  103. zc1 := zc2;
  104. zr1 := zr2;
  105. zc2 := 0; zr2 := [];
  106. end;
  107. end;
  108. end;
  109. if zc1 < zc2 then begin
  110. zc1 := zc2;
  111. zr1 := zr2;
  112. end;
  113. SetLength(HostAddrToStr6, 8*5-1);
  114. SetLength(HostAddrToStr6, 0);
  115. have_skipped := false;
  116. for i := 0 to 7 do begin
  117. if not (i in zr1) then begin
  118. if have_skipped then begin
  119. if HostAddrToStr6 = ''
  120. then HostAddrToStr6 := '::'
  121. else HostAddrToStr6 := HostAddrToStr6 + ':';
  122. have_skipped := false;
  123. end;
  124. // FIXME: is that shortnettohost really proper there? I wouldn't be too sure...
  125. HostAddrToStr6 := HostAddrToStr6 + IntToHex(ShortNetToHost(Entry[i]), 1) + ':';
  126. end else begin
  127. have_skipped := true;
  128. end;
  129. end;
  130. if have_skipped then
  131. if HostAddrToStr6 = ''
  132. then HostAddrToStr6 := '::'
  133. else HostAddrToStr6 := HostAddrToStr6 + ':';
  134. if HostAddrToStr6 = '' then HostAddrToStr6 := '::';
  135. if not (7 in zr1) then
  136. SetLength(HostAddrToStr6, Length(HostAddrToStr6)-1);
  137. end;
  138. function StrToHostAddr6(IP : String) : THostAddr6;
  139. begin
  140. end;
  141. function NetAddrToStr6 (Entry : TNetAddr6) : String;
  142. begin
  143. Result := HostAddrToStr6(Entry);
  144. end;
  145. function StrToNetAddr6(IP : String) : TNetAddr6;
  146. begin
  147. Result := StrToHostAddr6(IP);
  148. end;