File: hs.inc

package info (click to toggle)
fpc 2.0.0-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 89,476 kB
  • ctags: 133,433
  • sloc: pascal: 1,075,377; makefile: 310,704; xml: 64,343; perl: 7,703; yacc: 3,297; ansic: 2,265; lex: 839; php: 447; sh: 412; sed: 132; asm: 71; csh: 34; cpp: 26; tcl: 7
file content (203 lines) | stat: -rw-r--r-- 3,757 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

function HostAddrToStr (Entry : THostAddr) : String;

Var Dummy : String[4];
    I : Longint;

begin
  HostAddrToStr:='';
  For I:=1 to 4 do
   begin
   Str(Entry[I],Dummy);
   HostAddrToStr:=HostAddrToStr+Dummy;
   If I<4 Then
     HostAddrToStr:=HostAddrToStr+'.';
   end;
end;

function StrToHostAddr(IP : String) : THostAddr ;

Var
    Dummy : String;
    I     : Longint;
    J     : Integer;
    Temp : THostAddr;

begin
  Result:=NoAddress;
  For I:=1 to 4 do
   begin
   If I<4 Then
     begin
     J:=Pos('.',IP);
     If J=0 then
       exit;
     Dummy:=Copy(IP,1,J-1);
     Delete (IP,1,J);
     end
   else
     Dummy:=IP;
   Val (Dummy,Temp[I],J);
   If J<>0 then Exit;
   end;
 Result:=Temp;
end;

function NetAddrToStr (Entry : TNetAddr) : String;

Var Dummy : String[4];
    I : Longint;

begin
  NetAddrToStr:='';
  For I:=4 downto 1 do
   begin
   Str(Entry[I],Dummy);
   NetAddrToStr:=NetAddrToStr+Dummy;
   If I>1 Then
     NetAddrToStr:=NetAddrToStr+'.';
   end;
end;

function StrToNetAddr(IP : String) : TNetAddr;

begin
  StrToNetAddr:=TNetAddr(StrToHostAddr(IP));
end;

Function HostToNet (Host : ThostAddr) : THostAddr;

begin
{$Ifdef ENDIAN_LITTLE}
  Result[1]:=Host[4];
  Result[2]:=Host[3];
  Result[3]:=Host[2];
  Result[4]:=Host[1];
{$ELSE}
  result:=host;
{$endif}
end;

Function NetToHost (Net : TNetAddr) : TNetAddr;

begin
{$Ifdef ENDIAN_LITTLE}
  Result[1]:=Net[4];
  Result[2]:=Net[3];
  Result[3]:=Net[2];
  Result[4]:=Net[1];
{$ELSE}
  result:=net;
{$endif}
end;

Function HostToNet (Host : Longint) : Longint;

begin
{$Ifdef ENDIAN_LITTLE}
  Result:=Longint(HostToNet(THostAddr(host)));
{$ELSE}
  result:=host;
{$endif}
end;

Function NetToHost (Net : Longint) : Longint;

begin
{$Ifdef ENDIAN_LITTLE}
  Result:=Longint(NetToHost(TNetAddr(Net)));
{$ELSE}
  result:=net;
{$endif}
end;

Function ShortHostToNet (Host : Word) : Word;

begin
{$Ifdef ENDIAN_LITTLE}
  ShortHostToNet:=lo(host)*256+Hi(Host);
{$ELSE}
  result:=host;
{$endif}

end;

Function ShortNetToHost (Net : Word) : Word;

begin
{$Ifdef ENDIAN_LITTLE}
  ShortNetToHost:=lo(Net)*256+Hi(Net);
{$ELSE}
  result:=net;
{$endif}
end;



function HostAddrToStr6 (Entry : THostAddr6) : String;
var
  i: byte;
  zr1,zr2: set of byte;
  zc1,zc2: byte;
  have_skipped: boolean;
begin
  zr1 := [];
  zr2 := [];
  zc1 := 0;
  zc2 := 0;
  for i := 0 to 7 do begin
    if Entry[i] = 0 then begin
      include(zr2, i);
      inc(zc2);
    end else begin
      if zc1 < zc2 then begin
        zc1 := zc2;
        zr1 := zr2;
        zc2 := 0; zr2 := [];
      end;
    end;
  end;
  if zc1 < zc2 then begin
    zc1 := zc2;
    zr1 := zr2;
  end;
  SetLength(HostAddrToStr6, 8*5-1);
  SetLength(HostAddrToStr6, 0);
  have_skipped := false;
  for i := 0 to 7 do begin
    if not (i in zr1) then begin
      if have_skipped then begin
        if HostAddrToStr6 = ''
          then HostAddrToStr6 := '::'
          else HostAddrToStr6 := HostAddrToStr6 + ':';
        have_skipped := false;
      end;
      // FIXME: is that shortnettohost really proper there? I wouldn't be too sure...
      HostAddrToStr6 := HostAddrToStr6 + IntToHex(ShortNetToHost(Entry[i]), 1) + ':';
    end else begin
      have_skipped := true;
    end;
  end;
  if have_skipped then
    if HostAddrToStr6 = ''
      then HostAddrToStr6 := '::'
      else HostAddrToStr6 := HostAddrToStr6 + ':';

  if HostAddrToStr6 = '' then HostAddrToStr6 := '::';
  if not (7 in zr1) then
    SetLength(HostAddrToStr6, Length(HostAddrToStr6)-1);
end;

function StrToHostAddr6(IP : String) : THostAddr6;
begin
end;

function NetAddrToStr6 (Entry : TNetAddr6) : String;
begin
  Result := HostAddrToStr6(Entry);
end;

function StrToNetAddr6(IP : String) : TNetAddr6;
begin
  Result := StrToHostAddr6(IP);
end;