hs.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. {$Ifdef ENDIAN_LITTLE}
  59. Result[1]:=Host[4];
  60. Result[2]:=Host[3];
  61. Result[3]:=Host[2];
  62. Result[4]:=Host[1];
  63. {$ELSE}
  64. result:=host;
  65. {$endif}
  66. end;
  67. Function NetToHost (Net : TNetAddr) : TNetAddr;
  68. begin
  69. {$Ifdef ENDIAN_LITTLE}
  70. Result[1]:=Net[4];
  71. Result[2]:=Net[3];
  72. Result[3]:=Net[2];
  73. Result[4]:=Net[1];
  74. {$ELSE}
  75. result:=net;
  76. {$endif}
  77. end;
  78. Function HostToNet (Host : Longint) : Longint;
  79. begin
  80. {$Ifdef ENDIAN_LITTLE}
  81. Result:=Longint(HostToNet(THostAddr(host)));
  82. {$ELSE}
  83. result:=host;
  84. {$endif}
  85. end;
  86. Function NetToHost (Net : Longint) : Longint;
  87. begin
  88. {$Ifdef ENDIAN_LITTLE}
  89. Result:=Longint(NetToHost(TNetAddr(Net)));
  90. {$ELSE}
  91. result:=net;
  92. {$endif}
  93. end;
  94. Function ShortHostToNet (Host : Word) : Word;
  95. begin
  96. {$Ifdef ENDIAN_LITTLE}
  97. ShortHostToNet:=lo(host)*256+Hi(Host);
  98. {$ELSE}
  99. result:=host;
  100. {$endif}
  101. end;
  102. Function ShortNetToHost (Net : Word) : Word;
  103. begin
  104. {$Ifdef ENDIAN_LITTLE}
  105. ShortNetToHost:=lo(Net)*256+Hi(Net);
  106. {$ELSE}
  107. result:=net;
  108. {$endif}
  109. end;
  110. function HostAddrToStr6 (Entry : THostAddr6) : String;
  111. var
  112. i: byte;
  113. zr1,zr2: set of byte;
  114. zc1,zc2: byte;
  115. have_skipped: boolean;
  116. begin
  117. zr1 := [];
  118. zr2 := [];
  119. zc1 := 0;
  120. zc2 := 0;
  121. for i := 0 to 7 do begin
  122. if Entry[i] = 0 then begin
  123. include(zr2, i);
  124. inc(zc2);
  125. end else begin
  126. if zc1 < zc2 then begin
  127. zc1 := zc2;
  128. zr1 := zr2;
  129. zc2 := 0; zr2 := [];
  130. end;
  131. end;
  132. end;
  133. if zc1 < zc2 then begin
  134. zc1 := zc2;
  135. zr1 := zr2;
  136. end;
  137. SetLength(HostAddrToStr6, 8*5-1);
  138. SetLength(HostAddrToStr6, 0);
  139. have_skipped := false;
  140. for i := 0 to 7 do begin
  141. if not (i in zr1) then begin
  142. if have_skipped then begin
  143. if HostAddrToStr6 = ''
  144. then HostAddrToStr6 := '::'
  145. else HostAddrToStr6 := HostAddrToStr6 + ':';
  146. have_skipped := false;
  147. end;
  148. // FIXME: is that shortnettohost really proper there? I wouldn't be too sure...
  149. HostAddrToStr6 := HostAddrToStr6 + IntToHex(ShortNetToHost(Entry[i]), 1) + ':';
  150. end else begin
  151. have_skipped := true;
  152. end;
  153. end;
  154. if have_skipped then
  155. if HostAddrToStr6 = ''
  156. then HostAddrToStr6 := '::'
  157. else HostAddrToStr6 := HostAddrToStr6 + ':';
  158. if HostAddrToStr6 = '' then HostAddrToStr6 := '::';
  159. if not (7 in zr1) then
  160. SetLength(HostAddrToStr6, Length(HostAddrToStr6)-1);
  161. end;
  162. function StrToHostAddr6(IP : String) : THostAddr6;
  163. begin
  164. end;
  165. function NetAddrToStr6 (Entry : TNetAddr6) : String;
  166. begin
  167. Result := HostAddrToStr6(Entry);
  168. end;
  169. function StrToNetAddr6(IP : String) : TNetAddr6;
  170. begin
  171. Result := StrToHostAddr6(IP);
  172. end;