resolve.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2022 by Michael van Canney and other members of the
  4. Free Pascal development team
  5. Amiga parts of the resolver
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. uses
  13. Sysutils;
  14. const
  15. { Net type }
  16. socklib = 'c';
  17. AF_INET = 2;
  18. { Error constants. Returned by LastError method of THost, TNet}
  19. NETDB_INTERNAL= -1; { see errno }
  20. NETDB_SUCCESS = 0; { no problem }
  21. HOST_NOT_FOUND= 1; { Authoritative Answer Host not found }
  22. TRY_AGAIN = 2; { Non-Authoritive Host not found, or SERVERFAIL }
  23. NO_RECOVERY = 3; { Non recoverable errors, FORMERR, REFUSED, NOTIMP }
  24. NO_DATA = 4; { Valid name, no data record of requested type }
  25. NO_ADDRESS = NO_DATA; { no address, look for MX record }
  26. Type
  27. { THostEnt Object }
  28. THostEnt = record
  29. H_Name : PAnsiChar; { Official name }
  30. H_Aliases : PPAnsiChar; { Null-terminated list of aliases}
  31. H_Addrtype : longint; { Host address type }
  32. H_length : longint; { Length of address }
  33. H_Addr : PPAnsiChar; { null-terminated list of adresses }
  34. end;
  35. PHostEntry = ^THostEnt;
  36. { TNetEnt object }
  37. TNetEnt = record
  38. N_Name : PAnsiChar; { Official name }
  39. N_Aliases : PPAnsiChar; { Nill-terminated alias list }
  40. N_AddrType : longint; { Net address type }
  41. N_net : Cardinal; { Network number }
  42. end;
  43. PNetEntry = ^TNetEnt;
  44. TServEnt = record
  45. s_name : PAnsiChar; { Service name }
  46. s_aliases : PPAnsiChar; { Null-terminated alias list }
  47. s_port : longint; { Port number }
  48. s_proto : PAnsiChar; { Protocol to use }
  49. end;
  50. PServEntry = ^TServEnt;
  51. { remember, classic style calls are also used on MorphOS, so don't test for AMIGA68K }
  52. {$ifndef AMIGAOS4}
  53. function fpgethostbyname(Name: PAnsiChar location 'a0'): PHostEntry; syscall SocketBase 210;
  54. function getnetbyname(Name: PAnsiChar location 'a0'): PNetEntry; syscall SocketBase 222;
  55. function getnetbyaddr(Net: Longint location 'd0'; NetType: Longint location 'd1'): PNetEntry; syscall SocketBase 228;
  56. function getservbyname(Name: PAnsiChar location 'a0'; Protocol: PAnsiChar location 'a1'): PServEntry; syscall SocketBase 234;
  57. function getservbyport(Port: LongInt location 'd0'; Protocol: PAnsiChar location 'a0'): PServEntry; syscall SocketBase 240;
  58. procedure setnetent(Stayopen: Longint location 'd0'); syscall SocketBase 516;
  59. procedure endnetent; syscall SocketBase 522;
  60. function getnetent: PNetEntry; syscall SocketBase 528;
  61. procedure setservent(StayOpen: longint location 'd0'); syscall SocketBase 552;
  62. procedure endservent; syscall SocketBase 558;
  63. function getservent: PServEntry; syscall SocketBase 564;
  64. {$else AMIGAOS4}
  65. function fpgethostbyname(const Name: PAnsiChar): PHostEntry; syscall ISocket 196;
  66. function getnetbyname(Name: PAnsiChar): PNetEntry; syscall ISocket 204;
  67. function getnetbyaddr(Net: Longint; NetType: Longint): PNetEntry; syscall ISocket 208;
  68. function getservbyname(Name: PAnsiChar; Protocol: PAnsiChar): PServEntry; syscall ISocket 212;
  69. function getservbyport(Port: LongInt; Protocol: PAnsiChar): PServEntry; syscall ISocket 216;
  70. procedure setnetent(Stayopen: Longint); syscall ISocket 456;
  71. procedure endnetent; syscall ISocket 460;
  72. function getnetent: PNetEntry; syscall ISocket 464;
  73. procedure setservent(StayOpen: longint); syscall ISocket 480;
  74. procedure endservent; syscall ISocket 484;
  75. function getservent: PServEntry; syscall ISocket 488;
  76. {$endif AMIGAOS4}
  77. function gethostbyname(Name: PAnsiChar): PHostEntry;
  78. begin
  79. if Assigned(SocketBase) then
  80. gethostbyname := fpgethostbyname(Name)
  81. else
  82. gethostbyname := nil;
  83. end;
  84. function gethostbyaddr(Addr: PAnsiChar; Len: Longint; HType: Longint): PHostentry;
  85. var
  86. addr1,
  87. addr2: in_addr;
  88. IP: PPLongInt;
  89. begin
  90. gethostbyaddr := nil;
  91. if not Assigned(SocketBase) then
  92. Exit;
  93. //
  94. Addr1 := in_addr(PHostAddr(Addr)^);
  95. Addr2.s_addr := htonl(Addr1.s_addr);
  96. gethostbyaddr := Pointer(bsd_GetHostByAddr(Pointer(@Addr2.s_addr), Len, HType));
  97. if Assigned(gethostbyaddr) then
  98. begin
  99. ip := Pointer(gethostbyaddr^.H_Addr);
  100. if Assigned(ip) then
  101. begin
  102. repeat
  103. ip^^ := ntohl(ip^^);
  104. Inc(IP);
  105. until ip^ = nil;
  106. end;
  107. end;
  108. end;
  109. function GetDNSError: integer;
  110. begin
  111. GetDNSError := 0;
  112. if assigned(SocketBase) then
  113. GetDNSError:=bsd_Errno;
  114. end;
  115. Function InitResolve : Boolean;
  116. begin
  117. Result:=Assigned(SocketBase);
  118. end;
  119. Function FinalResolve : Boolean;
  120. begin
  121. Result:=True;
  122. end;