Browse Source

* lots of endian fixes

marco 20 years ago
parent
commit
f683702e94
4 changed files with 190 additions and 311 deletions
  1. 7 131
      fcl/inc/resolve.pp
  2. 8 4
      fcl/inc/ssockets.pp
  3. 6 3
      fcl/net/fpsock.pp
  4. 169 173
      packages/base/netdb/netdb.pp

+ 7 - 131
fcl/inc/resolve.pp

@@ -34,33 +34,14 @@ Unit resolve;
 interface
 interface
 
 
 uses
 uses
-  Classes,UriParser;
+  Sockets,Classes,UriParser;
 
 
 Type
 Type
-  THostAddr = array[1..4] of byte;
+  THostAddr = in_addr;		
   PHostAddr = ^THostAddr;
   PHostAddr = ^THostAddr;
-  TNetAddr = THostAddr;
+  TNetAddr = in_addr;
   PNetAddr = ^TNetAddr;
   PNetAddr = ^TNetAddr;
 
 
-Const
-  NoAddress : THostAddr = (0,0,0,0);
-  NoNet : TNetAddr = (0,0,0,0);
-
-{ ---------------------------------------------------------------------
-  Axuliary routines
-  ---------------------------------------------------------------------}
-
-function HostAddrToStr (Entry : THostAddr) : String;
-function StrToHostAddr (IP : String) : THostAddr;
-function NetAddrToStr (Entry : TNetAddr) : String;
-function StrToNetAddr (IP : String) : TNetAddr;
-Function HostToNet (Host : ThostAddr) : ThostAddr;
-Function HostToNet (Host : Longint) : Longint;
-Function NetToHost (Net : Longint) : Longint;
-Function NetToHost (Net : TNetAddr) : TNetAddr;
-Function ShortHostToNet (Host : Word) : Word;
-Function ShortNetToHost (Net : Word) : Word;
-
 Type
 Type
 
 
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
@@ -218,114 +199,6 @@ uses netdb;
 {$i resolve.inc}
 {$i resolve.inc}
 {$endif}
 {$endif}
 
 
-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
-  Result[1]:=Host[4];
-  Result[2]:=Host[3];
-  Result[3]:=Host[2];
-  Result[4]:=Host[1];
-end;
-
-Function NetToHost (Net : TNetAddr) : TNetAddr;
-
-begin
-  Result[1]:=Net[4];
-  Result[2]:=Net[3];
-  Result[3]:=Net[2];
-  Result[4]:=Net[1];
-end;
-
-Function HostToNet (Host : Longint) : Longint;
-
-begin
-  Result:=Longint(HostToNet(THostAddr(host)));
-end;
-
-Function NetToHost (Net : Longint) : Longint;
-
-begin
-  Result:=Longint(NetToHost(TNetAddr(Net)));
-end;
-
-Function ShortHostToNet (Host : Word) : Word;
-
-begin
-  ShortHostToNet:=lo(host)*256+Hi(Host);
-end;
-
-Function ShortNetToHost (Net : Word) : Word;
-
-begin
-  ShortNetToHost:=lo(Net)*256+Hi(Net);
-end;
-
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
   TResolver
   TResolver
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
@@ -952,7 +825,10 @@ Finalization
 end.
 end.
 {
 {
    $Log$
    $Log$
-   Revision 1.10  2005-02-14 17:13:15  peter
+   Revision 1.11  2005-03-18 10:58:16  marco
+    * lots of endian fixes
+
+   Revision 1.10  2005/02/14 17:13:15  peter
      * truncate log
      * truncate log
 
 
 }
 }

+ 8 - 4
fcl/inc/ssockets.pp

@@ -546,8 +546,8 @@ Var
   addr: TInetSockAddr;
   addr: TInetSockAddr;
 
 
 begin
 begin
-  A := StrToHostAddr(FHost);
-  if A[1] = 0 then
+  A := StrToNetAddr(FHost);
+  if A.s_bytes[4] = 0 then
     With THostResolver.Create(Nil) do
     With THostResolver.Create(Nil) do
       try
       try
         If Not NameLookup(FHost) then
         If Not NameLookup(FHost) then
@@ -558,7 +558,8 @@ begin
       end;
       end;
   addr.family := AF_INET;
   addr.family := AF_INET;
   addr.port := ShortHostToNet(FPort);
   addr.port := ShortHostToNet(FPort);
-  addr.addr := Cardinal(A);
+  addr.addr := a.s_addr; // hosttonet(A).s_addr;
+//Cardinal(A);
 
 
   If not Sockets.Connect(ASocket, addr, sizeof(addr)) then
   If not Sockets.Connect(ASocket, addr, sizeof(addr)) then
     raise ESocketError.Create(seConnectFailed, [Format('%s:%d',[FHost, FPort])]);
     raise ESocketError.Create(seConnectFailed, [Format('%s:%d',[FHost, FPort])]);
@@ -600,7 +601,10 @@ end.
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.25  2005-02-14 17:13:15  peter
+  Revision 1.26  2005-03-18 10:58:16  marco
+   * lots of endian fixes
+
+  Revision 1.25  2005/02/14 17:13:15  peter
     * truncate log
     * truncate log
 
 
 }
 }

+ 6 - 3
fcl/net/fpsock.pp

@@ -482,8 +482,8 @@ procedure TCustomTCPClient.DoResolve;
 var
 var
   HostResolver: THostResolver;
   HostResolver: THostResolver;
 begin
 begin
-  HostAddr := StrToHostAddr(Host);
-  if HostAddr[1] = 0 then
+  HostAddr := StrToNetAddr(Host);
+  if HostAddr.s_bytes[4] = 0 then
   begin
   begin
     HostResolver := THostResolver.Create(nil);
     HostResolver := THostResolver.Create(nil);
     try
     try
@@ -588,7 +588,10 @@ end.
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.6  2005-02-14 17:13:15  peter
+  Revision 1.7  2005-03-18 10:58:16  marco
+   * lots of endian fixes
+
+  Revision 1.6  2005/02/14 17:13:15  peter
     * truncate log
     * truncate log
 
 
 }
 }

+ 169 - 173
packages/base/netdb/netdb.pp

@@ -4,7 +4,7 @@
     Copyright (c) 2003 by the Free Pascal development team
     Copyright (c) 2003 by the Free Pascal development team
 
 
     Implement networking routines.
     Implement networking routines.
-
+    
     See the file COPYING.FPC, included in this distribution,
     See the file COPYING.FPC, included in this distribution,
     for details about the copyright.
     for details about the copyright.
 
 
@@ -28,20 +28,27 @@ unit netdb;
 
 
 Interface
 Interface
 
 
-{$i hsh.inc} // disappears if part of resolve.pp !!
+{ i hsh.inc} // disappears if part of resolve.pp !!
+
+Uses Sockets;
+
+Type
+  THostAddr = in_addr;		// historical aliases for these.
+  THostAddr6= Tin6_addr;
+  TNetAddr  = THostAddr;	// but in net order.
 
 
 Const
 Const
   DNSPort        = 53;
   DNSPort        = 53;
   MaxServers     = 4;
   MaxServers     = 4;
   MaxResolveAddr = 10;
   MaxResolveAddr = 10;
   SResolveFile   = '/etc/resolv.conf';
   SResolveFile   = '/etc/resolv.conf';
-  SServicesFile  = '/etc/services';
+  SServicesFile  = '/etc/services'; 
   SHostsFile     = '/etc/hosts';
   SHostsFile     = '/etc/hosts';
   SNetworksFile  = '/etc/networks';
   SNetworksFile  = '/etc/networks';
 
 
   MaxRecursion = 10;
   MaxRecursion = 10;
   MaxIP4Mapped = 10;
   MaxIP4Mapped = 10;
-
+  
 Type
 Type
   TDNSServerArray = Array[1..MaxServers] of THostAddr;
   TDNSServerArray = Array[1..MaxServers] of THostAddr;
   TServiceEntry = record
   TServiceEntry = record
@@ -50,27 +57,27 @@ Type
     Port     : Word;
     Port     : Word;
     Aliases  : String;
     Aliases  : String;
   end;
   end;
-
+     
   THostEntry = record
   THostEntry = record
     Name : String;
     Name : String;
     Addr : THostAddr;
     Addr : THostAddr;
     Aliases : String;
     Aliases : String;
-  end;
-
+  end;  
+  
   TNetworkEntry = Record
   TNetworkEntry = Record
     Name : String;
     Name : String;
     Addr : TNetAddr;
     Addr : TNetAddr;
     Aliases : String;
     Aliases : String;
-  end;
-
-Var
+  end;  
+  
+Var  
   DNSServers            : TDNSServerArray;
   DNSServers            : TDNSServerArray;
   DNSServerCount        : Integer;
   DNSServerCount        : Integer;
   DefaultDomainList     : String;
   DefaultDomainList     : String;
-  CheckResolveFileAge   : Boolean;
+  CheckResolveFileAge   : Boolean; 
   TimeOutS,TimeOutMS    : Longint;
   TimeOutS,TimeOutMS    : Longint;
-
-
+  
+  
 Function GetDNSServers(FN : String) : Integer;
 Function GetDNSServers(FN : String) : Integer;
 
 
 Function ResolveName(HostName : String; Var Addresses : Array of THostAddr) : Integer;
 Function ResolveName(HostName : String; Var Addresses : Array of THostAddr) : Integer;
@@ -96,23 +103,22 @@ Function GetServiceByPort(Port : Word;Const Proto : String; Var E : TServiceEntr
 
 
 Implementation
 Implementation
 
 
-uses
+uses 
 {$ifdef VER1_0}
 {$ifdef VER1_0}
    Linux,
    Linux,
 {$else}
 {$else}
    BaseUnix,
    BaseUnix,
 {$endif}
 {$endif}
-   sockets,sysutils;
+   sysutils;
 
 
-{$i hs.inc}
 
 
 const
 const
   { from http://www.iana.org/assignments/dns-parameters }
   { from http://www.iana.org/assignments/dns-parameters }
-  DNSQRY_A     = 1;                     // name to IP address
+  DNSQRY_A     = 1;                     // name to IP address 
   DNSQRY_AAAA  = 28;                    // name to IP6 address
   DNSQRY_AAAA  = 28;                    // name to IP6 address
   DNSQRY_A6    = 38;                    // name to IP6 (new)
   DNSQRY_A6    = 38;                    // name to IP6 (new)
-  DNSQRY_PTR   = 12;                    // IP address to name
-  DNSQRY_MX    = 15;                    // name to MX
+  DNSQRY_PTR   = 12;                    // IP address to name 
+  DNSQRY_MX    = 15;                    // name to MX 
   DNSQRY_TXT   = 16;                    // name to TXT
   DNSQRY_TXT   = 16;                    // name to TXT
   DNSQRY_CNAME = 5;
   DNSQRY_CNAME = 5;
 
 
@@ -129,20 +135,20 @@ const
   QF_RCODE  = $0F;
   QF_RCODE  = $0F;
 
 
 
 
-
-Type
+   
+Type 
   TPayLoad  = Array[0..511] of char;
   TPayLoad  = Array[0..511] of char;
   TQueryData = packed Record
   TQueryData = packed Record
     id      : Array[0..1] of Byte;
     id      : Array[0..1] of Byte;
     flags1  : Byte;
     flags1  : Byte;
-    flags2  : Byte;
+    flags2  : Byte; 
     qdcount : word;
     qdcount : word;
     ancount : word;
     ancount : word;
     nscount : word;
     nscount : word;
     arcount : word;
     arcount : word;
     Payload : TPayLoad;
     Payload : TPayLoad;
   end;
   end;
-
+  
   TRRData = Packed record       // RR record
   TRRData = Packed record       // RR record
     Atype    : Word;            // Answer type
     Atype    : Word;            // Answer type
     AClass   : Word;
     AClass   : Word;
@@ -154,52 +160,10 @@ Var
   ResolveFileAge  : Longint;
   ResolveFileAge  : Longint;
   ResolveFileName : String;
   ResolveFileName : String;
 
 
-{ ---------------------------------------------------------------------
-    Auxiliary functions.
-  ---------------------------------------------------------------------}
-
-function htonl(const i:integer):integer;
-begin
-  {$ifdef ENDIAN_LITTLE}
-  result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);
-  {$else}
-  result := i;
-  {$endif}
-end;
-
-Function htons(const W : Word) : word;
-
-begin
-  {$ifdef ENDIAN_LITTLE}
-  result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);
-  {$else}
-  result := w;
-  {$endif}
-end;
-
-Function ntohs(const W : Word) : Word;
-
-begin
-  {$ifdef ENDIAN_LITTLE}
-  result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);
-  {$else}
-  result := w;
-  {$endif}
-end;
-
-function ntohl(const i:integer):integer;
-begin
-  {$ifdef ENDIAN_LITTLE}
-  result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);
-  {$else}
-  result := i;
-  {$endif}
-end;
-
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
    Resolve.conf handling
    Resolve.conf handling
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
-
+  
 Function GetDNSServers(Fn : String) : Integer;
 Function GetDNSServers(Fn : String) : Integer;
 
 
 Var
 Var
@@ -209,10 +173,10 @@ Var
   H : THostAddr;
   H : THostAddr;
 
 
   Function CheckDirective(Dir : String) : Boolean;
   Function CheckDirective(Dir : String) : Boolean;
-
+  
   Var
   Var
     P : Integer;
     P : Integer;
-
+  
   begin
   begin
     P:=Pos(Dir,L);
     P:=Pos(Dir,L);
     Result:=(P<>0);
     Result:=(P<>0);
@@ -222,7 +186,7 @@ Var
       L:=Trim(L);
       L:=Trim(L);
       end;
       end;
   end;
   end;
-
+   
 begin
 begin
   Result:=0;
   Result:=0;
   ResolveFileName:=Fn;
   ResolveFileName:=Fn;
@@ -231,9 +195,9 @@ begin
   Assign(R,FN);
   Assign(R,FN);
   Reset(R);
   Reset(R);
   {$i+}
   {$i+}
-  If (IOResult<>0) then
+  If (IOResult<>0) then 
     exit;
     exit;
-  Try
+  Try  
     While not EOF(R) do
     While not EOF(R) do
       begin
       begin
       Readln(R,L);
       Readln(R,L);
@@ -243,7 +207,7 @@ begin
       If CheckDirective('nameserver') then
       If CheckDirective('nameserver') then
         begin
         begin
         H:=HostToNet(StrToHostAddr(L));
         H:=HostToNet(StrToHostAddr(L));
-        If (H[1]<>0) then
+        If H.s_bytes[1]<>0 then
           begin
           begin
           Inc(Result);
           Inc(Result);
           DNSServers[Result]:=H;
           DNSServers[Result]:=H;
@@ -256,7 +220,7 @@ begin
       end;
       end;
   Finally
   Finally
     Close(R);
     Close(R);
-  end;
+  end;    
   DNSServerCount:=Result;
   DNSServerCount:=Result;
 end;
 end;
 
 
@@ -271,37 +235,37 @@ begin
     F:=FileAge(ResolveFileName);
     F:=FileAge(ResolveFileName);
     If ResolveFileAge<F then
     If ResolveFileAge<F then
       GetDnsServers(ResolveFileName);
       GetDnsServers(ResolveFileName);
-    end;
+    end;  
 end;
 end;
 
 
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
     Payload handling functions.
     Payload handling functions.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
-
+  
 
 
 Procedure DumpPayLoad(Q : TQueryData; L : Integer);
 Procedure DumpPayLoad(Q : TQueryData; L : Integer);
 
 
-Var
+Var 
   i : Integer;
   i : Integer;
 
 
 begin
 begin
   Writeln('Payload : ',l);
   Writeln('Payload : ',l);
   For I:=0 to L-1 do
   For I:=0 to L-1 do
     Write(Byte(Q.Payload[i]),' ');
     Write(Byte(Q.Payload[i]),' ');
-  Writeln;
+  Writeln;  
 end;
 end;
-
+  
 Function BuildPayLoad(Var Q : TQueryData; Name : String; RR : Word; QClass : Word) : Integer;
 Function BuildPayLoad(Var Q : TQueryData; Name : String; RR : Word; QClass : Word) : Integer;
 
 
 Var
 Var
   P : PByte;
   P : PByte;
   l,S : Integer;
   l,S : Integer;
-
+  
 begin
 begin
   Result:=-1;
   Result:=-1;
   If length(Name)>506 then
   If length(Name)>506 then
     Exit;
     Exit;
-  Result:=0;
+  Result:=0;  
   P:[email protected];
   P:[email protected];
   Repeat
   Repeat
     L:=Pos('.',Name);
     L:=Pos('.',Name);
@@ -332,8 +296,7 @@ Var
   I : Integer;
   I : Integer;
   HaveName : Boolean;
   HaveName : Boolean;
   PA : ^TRRData;
   PA : ^TRRData;
-  RClass,RType : Word;
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   I:=Start;
   I:=Start;
@@ -344,7 +307,7 @@ begin
       Inc(I,2)
       Inc(I,2)
     else If Payload[i]=#0 then // Null termination of label, skip.
     else If Payload[i]=#0 then // Null termination of label, skip.
       Inc(i)
       Inc(i)
-    else
+    else  
       begin
       begin
       Inc(I,Ord(Payload[i])+1); // Label, continue scan.
       Inc(I,Ord(Payload[i])+1); // Label, continue scan.
       HaveName:=False;
       HaveName:=False;
@@ -362,11 +325,11 @@ Function BuildName (Const PayLoad : TPayLoad; Start,len : Integer) : String;
 
 
 Const
 Const
   FIREDNS_POINTER_VALUE = $C000;
   FIREDNS_POINTER_VALUE = $C000;
-
+  
 Var
 Var
   I,O : Integer;
   I,O : Integer;
   P : Word;
   P : Word;
-
+  
 begin
 begin
   SetLength(Result,512);
   SetLength(Result,512);
   I:=Start;
   I:=Start;
@@ -385,19 +348,19 @@ begin
         Result[O]:='.';
         Result[O]:='.';
         Inc(O);
         Inc(O);
         end;
         end;
-      P:=Ord(Payload[i]);
+      P:=Ord(Payload[i]);  
       Move(Payload[i+1],Result[o],P);
       Move(Payload[i+1],Result[o],P);
       Inc(I,P+1);
       Inc(I,P+1);
       Inc(O,P);
       Inc(O,P);
       end;
       end;
-   Until (Payload[I]=#0);
+   Until (Payload[I]=#0);    
 end;
 end;
 
 
 
 
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
     QueryData handling functions
     QueryData handling functions
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
-
+  
 Function CheckAnswer(Const Qry : TQueryData; Var Ans : TQueryData) : Boolean;
 Function CheckAnswer(Const Qry : TQueryData; Var Ans : TQueryData) : Boolean;
 
 
 begin
 begin
@@ -406,15 +369,15 @@ begin
     begin
     begin
     // Check ID.
     // Check ID.
     If (ID[1]<>QRY.ID[1]) or (ID[0]<>Qry.ID[0]) then
     If (ID[1]<>QRY.ID[1]) or (ID[0]<>Qry.ID[0]) then
-      exit;
+      exit;  
     // Flags ?
     // Flags ?
     If (Flags1 and QF_QR)=0 then
     If (Flags1 and QF_QR)=0 then
       exit;
       exit;
-    if (Flags1 and QF_OPCODE)<>0 then
-      exit;
+    if (Flags1 and QF_OPCODE)<>0 then 
+      exit;  
     if (Flags2 and QF_RCODE)<>0 then
     if (Flags2 and QF_RCODE)<>0 then
-      exit;
-    // Number of answers ?
+      exit;  
+    // Number of answers ?  
     AnCount := htons(Ancount);
     AnCount := htons(Ancount);
     If Ancount<1 then
     If Ancount<1 then
       Exit;
       Exit;
@@ -434,7 +397,7 @@ begin
     qdcount := htons(qdcount);
     qdcount := htons(qdcount);
     i:=0;
     i:=0;
     q:=0;
     q:=0;
-    While (Q<qdcount) and (i<l) do
+    While (Q<qdcount) and (i<l) do  
       begin
       begin
       If Ord(Payload[i])>63 then
       If Ord(Payload[i])>63 then
         begin
         begin
@@ -449,26 +412,26 @@ begin
           Inc(I,5);
           Inc(I,5);
           end
           end
         else
         else
-          Inc(I,Ord(Payload[i])+1);
-        end;
+          Inc(I,Ord(Payload[i])+1);  
+        end;  
       end;
       end;
-    Result:=I;
-    end;
+    Result:=I;  
+    end;  
 end;
 end;
 
 
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
     DNS Query functions.
     DNS Query functions.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
-
+  
 
 
 Function Query(Resolver : Integer; Var Qry,Ans : TQueryData; QryLen : Integer; Var AnsLen : Integer) : Boolean;
 Function Query(Resolver : Integer; Var Qry,Ans : TQueryData; QryLen : Integer; Var AnsLen : Integer) : Boolean;
 
 
 Var
 Var
-  BA,SA : TInetSockAddr;
-  Sock,L,I : Longint;
+  SA : TInetSockAddr;
+  Sock,L : Longint;
   Al,RTO : Longint;
   Al,RTO : Longint;
   ReadFDS : {$ifdef VER1_0}FDSet{$ELSE}TFDSet{$ENDIF};
   ReadFDS : {$ifdef VER1_0}FDSet{$ELSE}TFDSet{$ENDIF};
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   With Qry do
   With Qry do
@@ -483,13 +446,13 @@ begin
     arcount:=0;
     arcount:=0;
     end;
     end;
   Sock:=Socket(PF_INET,SOCK_DGRAM,0);
   Sock:=Socket(PF_INET,SOCK_DGRAM,0);
-  If Sock=-1 then
+  If Sock=-1 then 
     exit;
     exit;
   With SA do
   With SA do
     begin
     begin
     family:=AF_INET;
     family:=AF_INET;
     port:=htons(DNSport);
     port:=htons(DNSport);
-    addr:=cardinal(HostToNet(DNSServers[Resolver]));
+    addr:=cardinal(DNSServers[Resolver]); // dnsservers already in net order
     end;
     end;
   sendto(sock,qry,qrylen+12,0,SA,SizeOf(SA));
   sendto(sock,qry,qrylen+12,0,SA,SizeOf(SA));
   // Wait for answer.
   // Wait for answer.
@@ -511,9 +474,9 @@ begin
   // Check lenght answer and fields in header data.
   // Check lenght answer and fields in header data.
   If (L<12) or not CheckAnswer(Qry,Ans) Then
   If (L<12) or not CheckAnswer(Qry,Ans) Then
     exit;
     exit;
-  // Return Payload length.
-  Anslen:=L-12;
-  Result:=True;
+  // Return Payload length.  
+  Anslen:=L-12;  
+  Result:=True;  
 end;
 end;
 
 
 function stringfromlabel(pl: TPayLoad; start: integer): string;
 function stringfromlabel(pl: TPayLoad; start: integer): string;
@@ -549,7 +512,7 @@ begin
   QryLen:=BuildPayLoad(Qry,HostName,DNSQRY_A,1);
   QryLen:=BuildPayLoad(Qry,HostName,DNSQRY_A,1);
   If Not Query(Resolver,Qry,Ans,QryLen,AnsLen) then
   If Not Query(Resolver,Qry,Ans,QryLen,AnsLen) then
     Result:=-1
     Result:=-1
-  else
+  else  
     begin
     begin
     AnsStart:=SkipAnsQueries(Ans,AnsLen);
     AnsStart:=SkipAnsQueries(Ans,AnsLen);
     MaxAnswer:=Ans.AnCount-1;
     MaxAnswer:=Ans.AnCount-1;
@@ -578,7 +541,7 @@ begin
           end;
           end;
         end;
         end;
         Inc(I);
         Inc(I);
-      end;
+      end;  
     end;
     end;
 end;
 end;
 
 
@@ -598,8 +561,10 @@ begin
     end;
     end;
 end;
 end;
 
 
-Function ResolveNameAt6(Resolver : Integer; HostName : String; Var Addresses : Array of THostAddr6; Recurse: Integer) : Integer;
+//const NoAddress6 : array[0..7] of word = (0,0,0,0,0,0,0,0);
 
 
+Function ResolveNameAt6(Resolver : Integer; HostName : String; Var Addresses : Array of THostAddr6; Recurse: Integer) : Integer;
+                                                                                                                                        
 Var
 Var
   Qry, Ans            : TQueryData;
   Qry, Ans            : TQueryData;
   MaxAnswer,I,QryLen,
   MaxAnswer,I,QryLen,
@@ -608,7 +573,7 @@ Var
   cname               : string;
   cname               : string;
   LIP4mapped: array[0..MaxIP4Mapped-1] of THostAddr;
   LIP4mapped: array[0..MaxIP4Mapped-1] of THostAddr;
   LIP4count: Longint;
   LIP4count: Longint;
-
+                                                                                                                                        
 begin
 begin
   Result:=0;
   Result:=0;
   QryLen:=BuildPayLoad(Qry,HostName,DNSQRY_AAAA,1);
   QryLen:=BuildPayLoad(Qry,HostName,DNSQRY_AAAA,1);
@@ -618,15 +583,15 @@ begin
     if LIP4Count > 0 then begin
     if LIP4Count > 0 then begin
       inc(LIP4Count); // we loop to LIP4Count-1 later
       inc(LIP4Count); // we loop to LIP4Count-1 later
       if LIP4Count > MaxIP4Mapped then LIP4Count := MaxIP4Mapped;
       if LIP4Count > MaxIP4Mapped then LIP4Count := MaxIP4Mapped;
-{$ifdef VER1_0}
+{$ifdef VER1_0}      
       if LIP4Count > High(Addresses)+1 then LIP4Count := High(Addresses)+1;
       if LIP4Count > High(Addresses)+1 then LIP4Count := High(Addresses)+1;
-{$else}
+{$else}      
       if LIP4Count > Length(Addresses) then LIP4Count := Length(Addresses);
       if LIP4Count > Length(Addresses) then LIP4Count := Length(Addresses);
-{$endif}
+{$endif}      
       for i := 0 to LIP4Count-2 do begin
       for i := 0 to LIP4Count-2 do begin
         Addresses[i] := NoAddress6;
         Addresses[i] := NoAddress6;
-        Addresses[i][5] := $FFFF;
-        Move(LIP4Mapped[i], Addresses[i][6], 4);
+        Addresses[i].u6_addr16[5] := $FFFF;
+        Move(LIP4Mapped[i], Addresses[i].u6_addr16[6], 4);
       end;
       end;
       Result := LIP4Count;
       Result := LIP4Count;
     end else begin
     end else begin
@@ -665,7 +630,7 @@ begin
       end;
       end;
     end;
     end;
 end;
 end;
-
+                                                                                                                                        
 
 
 
 
 Function ResolveName6(HostName: String; Var Addresses: Array of THostAddr6) : Integer;
 Function ResolveName6(HostName: String; Var Addresses: Array of THostAddr6) : Integer;
@@ -689,13 +654,13 @@ Var
   MaxAnswer,I,QryLen,
   MaxAnswer,I,QryLen,
   AnsLen,AnsStart     : Longint;
   AnsLen,AnsStart     : Longint;
   RR                  : TRRData;
   RR                  : TRRData;
-  S                   : String;
+
 begin
 begin
   Result:=0;
   Result:=0;
   QryLen:=BuildPayLoad(Qry,Address,DNSQRY_PTR,1);
   QryLen:=BuildPayLoad(Qry,Address,DNSQRY_PTR,1);
   If Not Query(Resolver,Qry,Ans,QryLen,AnsLen) then
   If Not Query(Resolver,Qry,Ans,QryLen,AnsLen) then
     Result:=-1
     Result:=-1
-  else
+  else  
     begin
     begin
     AnsStart:=SkipAnsQueries(Ans,AnsLen);
     AnsStart:=SkipAnsQueries(Ans,AnsLen);
     MaxAnswer:=Ans.AnCount-1;
     MaxAnswer:=Ans.AnCount-1;
@@ -712,7 +677,7 @@ begin
         Inc(AnsStart,RR.RDLength);
         Inc(AnsStart,RR.RDLength);
         end;
         end;
       Inc(I);
       Inc(I);
-      end;
+      end;  
     end;
     end;
 end;
 end;
 
 
@@ -722,12 +687,14 @@ Function ResolveAddress(HostAddr : THostAddr; Var Addresses : Array of String) :
 Var
 Var
   I : Integer;
   I : Integer;
   S : String;
   S : String;
-
+  nt : tnetaddr;
+  
 begin
 begin
   CheckResolveFile;
   CheckResolveFile;
   I:=1;
   I:=1;
   Result:=0;
   Result:=0;
-  S:=Format('%d.%d.%d.%d.in-addr.arpa',[HostAddr[4],HostAddr[3],HostAddr[2],HostAddr[1]]);
+  nt:=hosttonet(hostaddr);
+  S:=Format('%d.%d.%d.%d.in-addr.arpa',[nt.s_bytes[4],nt.s_bytes[3],nt.s_bytes[2],nt.s_bytes[1]]);
   While (Result=0) and (I<=DNSServerCount) do
   While (Result=0) and (I<=DNSServerCount) do
     begin
     begin
     Result:=ResolveAddressAt(I,S,Addresses);
     Result:=ResolveAddressAt(I,S,Addresses);
@@ -739,20 +706,20 @@ Function ResolveAddress6(HostAddr : THostAddr6; Var Addresses : Array of String)
 
 
 const
 const
   hexdig: string[16] = '0123456789abcdef';
   hexdig: string[16] = '0123456789abcdef';
-
+                                                                                
 Var
 Var
   I : Integer;
   I : Integer;
   S : ShortString;
   S : ShortString;
-
+                                                                                
 begin
 begin
   CheckResolveFile;
   CheckResolveFile;
   Result:=0;
   Result:=0;
   S := '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.int';
   S := '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.int';
   for i := 7 downto 0 do begin
   for i := 7 downto 0 do begin
-    S[5+(7-i)*8] := hexdig[1+(HostAddr[i] and $000F) shr 00];
-    S[7+(7-i)*8] := hexdig[1+(HostAddr[i] and $00F0) shr 04];
-    S[1+(7-i)*8] := hexdig[1+(HostAddr[i] and $0F00) shr 08];
-    S[3+(7-i)*8] := hexdig[1+(HostAddr[i] and $F000) shr 12];
+    S[5+(7-i)*8] := hexdig[1+(HostAddr.u6_addr16[i] and $000F) shr 00];
+    S[7+(7-i)*8] := hexdig[1+(HostAddr.u6_addr16[i] and $00F0) shr 04];
+    S[1+(7-i)*8] := hexdig[1+(HostAddr.u6_addr16[i] and $0F00) shr 08];
+    S[3+(7-i)*8] := hexdig[1+(HostAddr.u6_addr16[i] and $F000) shr 12];
   end;
   end;
   I := 1;
   I := 1;
   While (Result=0) and (I<=DNSServerCount) do
   While (Result=0) and (I<=DNSServerCount) do
@@ -764,13 +731,13 @@ end;
 
 
 function IN6_IS_ADDR_V4MAPPED(HostAddr: THostAddr6): boolean;
 function IN6_IS_ADDR_V4MAPPED(HostAddr: THostAddr6): boolean;
 begin
 begin
-  Result :=
-   (HostAddr[0] = 0) and
-   (HostAddr[1] = 0) and
-   (HostAddr[2] = 0) and
-   (HostAddr[3] = 0) and
-   (HostAddr[4] = 0) and
-   (HostAddr[5] = $FFFF);
+  Result := 
+   (HostAddr.u6_addr16[0] = 0) and
+   (HostAddr.u6_addr16[1] = 0) and
+   (HostAddr.u6_addr16[2] = 0) and
+   (HostAddr.u6_addr16[3] = 0) and
+   (HostAddr.u6_addr16[4] = 0) and
+   (HostAddr.u6_addr16[5] = $FFFF);
 end;
 end;
 
 
 
 
@@ -779,7 +746,7 @@ Function ResolveHostByName(HostName : String; Var H : THostEntry) : Boolean;
 Var
 Var
   Address : Array[1..MaxResolveAddr] of THostAddr;
   Address : Array[1..MaxResolveAddr] of THostAddr;
   L : Integer;
   L : Integer;
-
+  
 begin
 begin
   L:=ResolveName(HostName,Address);
   L:=ResolveName(HostName,Address);
   Result:=(L>0);
   Result:=(L>0);
@@ -797,7 +764,7 @@ Function ResolveHostByAddr(HostAddr : THostAddr; Var H : THostEntry) : Boolean;
 Var
 Var
   Names : Array[1..MaxResolveAddr] of String;
   Names : Array[1..MaxResolveAddr] of String;
   I,L : Integer;
   I,L : Integer;
-
+  
 begin
 begin
   L:=ResolveAddress(HostAddr,Names);
   L:=ResolveAddress(HostAddr,Names);
   Result:=(L>0);
   Result:=(L>0);
@@ -810,7 +777,7 @@ begin
       For I:=2 to L do
       For I:=2 to L do
         If (I=2) then
         If (I=2) then
           H.Aliases:=Names[i]
           H.Aliases:=Names[i]
-        else
+        else  
           H.Aliases:=H.Aliases+','+Names[i];
           H.Aliases:=H.Aliases+','+Names[i];
     end;
     end;
 end;
 end;
@@ -819,12 +786,12 @@ end;
     Some Parsing routines
     Some Parsing routines
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
-Const
+Const 
   Whitespace = [' ',#9];
   Whitespace = [' ',#9];
 
 
 Function NextWord(Var Line : String) : String;
 Function NextWord(Var Line : String) : String;
 
 
-Var
+Var 
   I,J : Integer;
   I,J : Integer;
 
 
 begin
 begin
@@ -834,10 +801,10 @@ begin
   J:=I;
   J:=I;
   While (J<=Length(Line)) and Not (Line[J] in WhiteSpace) do
   While (J<=Length(Line)) and Not (Line[J] in WhiteSpace) do
     inc(j);
     inc(j);
-  Result:=Copy(Line,I,J-1);
-  Delete(Line,1,J);
+  Result:=Copy(Line,I,J-1);  
+  Delete(Line,1,J);  
 end;
 end;
-
+  
 Procedure StripComment(Var line : String);
 Procedure StripComment(Var line : String);
 
 
 Var
 Var
@@ -877,8 +844,7 @@ Function GetNextHostEntry(var F : Text; Var H : THostEntry): boolean;
 
 
 Var
 Var
   Line,S : String;
   Line,S : String;
-  P : Integer;
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   Repeat
   Repeat
@@ -887,8 +853,8 @@ begin
     S:=NextWord(Line);
     S:=NextWord(Line);
     If (S<>'') then
     If (S<>'') then
       begin
       begin
-      H.Addr:=StrToHostAddr(S);
-      if (H.Addr[1]<>0) then
+      H.Addr:=StrTonetAddr(S);		// endianness problem here. (fixed)
+      if (H.Addr.s_bytes[4]<>0) then
         begin
         begin
         S:=NextWord(Line);
         S:=NextWord(Line);
         If (S<>'') then
         If (S<>'') then
@@ -902,20 +868,20 @@ begin
               If (H.Aliases='') then
               If (H.Aliases='') then
                 H.Aliases:=S
                 H.Aliases:=S
               else
               else
-                H.Aliases:=H.Aliases+','+S;
+                H.Aliases:=H.Aliases+','+S;  
           until (S='');
           until (S='');
           end;
           end;
-        end;
+        end;      
       end;
       end;
   until Result or EOF(F);
   until Result or EOF(F);
-end;
+end;  
 
 
 Function FindHostEntryInHostsFile(N: String; Addr: THostAddr; Var H : THostEntry) : boolean;
 Function FindHostEntryInHostsFile(N: String; Addr: THostAddr; Var H : THostEntry) : boolean;
 
 
 Var
 Var
   F : Text;
   F : Text;
   HE : THostEntry;
   HE : THostEntry;
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   If FileExists(SHostsFile) then
   If FileExists(SHostsFile) then
@@ -932,7 +898,7 @@ begin
           Result:=MatchNameOrAlias(N,HE.Name,HE.Aliases)
           Result:=MatchNameOrAlias(N,HE.Name,HE.Aliases)
         else
         else
           Result:=Cardinal(Addr)=Cardinal(HE.Addr);
           Result:=Cardinal(Addr)=Cardinal(HE.Addr);
-        end;
+        end; 
       Close(f);
       Close(f);
       If Result then
       If Result then
         begin
         begin
@@ -940,10 +906,12 @@ begin
         H.Addr:=HE.Addr;
         H.Addr:=HE.Addr;
         H.Aliases:=HE.Aliases;
         H.Aliases:=HE.Aliases;
         end;
         end;
-      end;
+      end;  
     end;
     end;
 end;
 end;
 
 
+//const NoAddress : in_addr = (s_addr: 0);
+
 Function GetHostByName(HostName: String;  Var H : THostEntry) : boolean;
 Function GetHostByName(HostName: String;  Var H : THostEntry) : boolean;
 
 
 begin
 begin
@@ -965,9 +933,8 @@ Function GetNextNetworkEntry(var F : Text; Var N : TNetworkEntry): boolean;
 
 
 Var
 Var
   NN,Line,S : String;
   NN,Line,S : String;
-  P : Integer;
   A : TNetAddr;
   A : TNetAddr;
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   Repeat
   Repeat
@@ -977,24 +944,24 @@ begin
     If (S<>'') then
     If (S<>'') then
       begin
       begin
       NN:=S;
       NN:=S;
-      A:=StrToHostAddr(NextWord(Line));
-      Result:=(NN<>'') and (A[1]<>0); // Valid addr.
+      A:=StrToHostAddr(NextWord(Line));	// endianness?
+      Result:=(NN<>'') and (A.s_bytes[1]<>0); // Valid addr.
       If result then
       If result then
         begin
         begin
         N.Addr:=A;
         N.Addr:=A;
         N.Name:=NN;
         N.Name:=NN;
         N.Aliases:='';
         N.Aliases:='';
-        end;
+        end;      
       end;
       end;
   until Result or EOF(F);
   until Result or EOF(F);
-end;
+end;  
 
 
 Function FindNetworkEntryInNetworksFile(Net: String; Addr: TNetAddr; Var N : TNetworkEntry) : boolean;
 Function FindNetworkEntryInNetworksFile(Net: String; Addr: TNetAddr; Var N : TNetworkEntry) : boolean;
 
 
 Var
 Var
   F : Text;
   F : Text;
   NE : TNetworkEntry;
   NE : TNetworkEntry;
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   If FileExists(SNetworksFile) then
   If FileExists(SNetworksFile) then
@@ -1011,7 +978,7 @@ begin
           Result:=MatchNameOrAlias(Net,NE.Name,NE.Aliases)
           Result:=MatchNameOrAlias(Net,NE.Name,NE.Aliases)
         else
         else
           Result:=Cardinal(Addr)=Cardinal(NE.Addr);
           Result:=Cardinal(Addr)=Cardinal(NE.Addr);
-        end;
+        end; 
       Close(f);
       Close(f);
       If Result then
       If Result then
         begin
         begin
@@ -1019,10 +986,12 @@ begin
         N.Addr:=NE.Addr;
         N.Addr:=NE.Addr;
         N.Aliases:=NE.Aliases;
         N.Aliases:=NE.Aliases;
         end;
         end;
-      end;
+      end;  
     end;
     end;
 end;
 end;
 
 
+Const NoNet : in_addr = (s_addr:0);
+  
 Function GetNetworkByName(NetName: String; Var N : TNetworkEntry) : boolean;
 Function GetNetworkByName(NetName: String; Var N : TNetworkEntry) : boolean;
 
 
 begin
 begin
@@ -1045,7 +1014,7 @@ Function GetNextServiceEntry(Var F : Text; Var E : TServiceEntry) : Boolean;
 Var
 Var
   Line,S : String;
   Line,S : String;
   P : INteger;
   P : INteger;
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   Repeat
   Repeat
@@ -1070,7 +1039,7 @@ begin
             If (S<>'') then
             If (S<>'') then
               If (Length(E.Aliases)=0) then
               If (Length(E.Aliases)=0) then
                 E.aliases:=S
                 E.aliases:=S
-              else
+              else  
                 E.Aliases:=E.Aliases+','+S;
                 E.Aliases:=E.Aliases+','+S;
           until (S='');
           until (S='');
           end;
           end;
@@ -1085,7 +1054,7 @@ Function FindServiceEntryInFile(Const Name,Proto : String; Port : Integer; Var E
 Var
 Var
   F : Text;
   F : Text;
   TE : TServiceEntry;
   TE : TServiceEntry;
-
+  
 begin
 begin
   Result:=False;
   Result:=False;
   If FileExists(SServicesFile) then
   If FileExists(SServicesFile) then
@@ -1100,11 +1069,11 @@ begin
         begin
         begin
         If (Port=-1) then
         If (Port=-1) then
           Result:=MatchNameOrAlias(Name,TE.Name,TE.Aliases)
           Result:=MatchNameOrAlias(Name,TE.Name,TE.Aliases)
-        else
+        else 
           Result:=(Port=TE.Port);
           Result:=(Port=TE.Port);
         If Result and (Proto<>'') then
         If Result and (Proto<>'') then
           Result:=(Proto=TE.Protocol);
           Result:=(Proto=TE.Protocol);
-        end;
+        end; 
       Close(f);
       Close(f);
       If Result then
       If Result then
         begin
         begin
@@ -1113,20 +1082,20 @@ begin
         E.Protocol:=TE.Protocol;
         E.Protocol:=TE.Protocol;
         E.Aliases:=TE.Aliases;
         E.Aliases:=TE.Aliases;
         end;
         end;
-      end;
+      end;  
     end;
     end;
 end;
 end;
 
 
 Function GetServiceByName(Const Name,Proto : String; Var E : TServiceEntry) : Boolean;
 Function GetServiceByName(Const Name,Proto : String; Var E : TServiceEntry) : Boolean;
 
 
 begin
 begin
-  Result:=FindServiceEntryInFile(Name,Proto,-1,E);
+  Result:=FindServiceEntryInFile(Name,Proto,-1,E);  
 end;
 end;
 
 
 Function GetServiceByPort(Port : Word;Const Proto : String; Var E : TServiceEntry) : Boolean;
 Function GetServiceByPort(Port : Word;Const Proto : String; Var E : TServiceEntry) : Boolean;
 
 
 begin
 begin
-  Result:=FindServiceEntryInFile('',Proto,Port,E);
+  Result:=FindServiceEntryInFile('',Proto,Port,E);  
 end;
 end;
 
 
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
@@ -1153,10 +1122,37 @@ end.
 
 
 {
 {
   $Log$
   $Log$
-  Revision 1.13  2005-02-14 17:13:19  peter
-    * truncate log
+  Revision 1.14  2005-03-18 10:58:16  marco
+   * lots of endian fixes
 
 
   Revision 1.12  2005/02/07 14:12:31  marco
   Revision 1.12  2005/02/07 14:12:31  marco
    * fixed endianess ugliness (3636)
    * fixed endianess ugliness (3636)
 
 
-}
+  Revision 1.11  2004/02/20 21:35:00  peter
+    * 1.0.x fix
+
+  Revision 1.10  2004/01/24 12:23:10  michael
+  + Patch from Johannes Berg
+
+  Revision 1.9  2003/12/12 20:50:18  michael
+  + Fixed trimming of nameserver entries
+
+  Revision 1.8  2003/11/22 23:17:50  michael
+  Patch for ipv6 and CNAME record support from Johannes Berg
+
+  Revision 1.7  2003/09/29 19:21:19  marco
+   * ; added to line 150
+
+  Revision 1.6  2003/09/29 07:44:11  michael
+  + Endian patch from bas [email protected]
+
+  Revision 1.5  2003/09/28 09:34:02  peter
+    * unix fix for 1.0.x
+
+  Revision 1.4  2003/09/18 16:30:23  marco
+   * unixreform fix
+
+  Revision 1.3  2003/05/17 20:54:03  michael
+  + uriparser unit added. Header/Footer blocks added
+
+}