inet.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. Unit inet;
  2. { --------------------------------------------------------------------
  3. Unit for internet domain calls.
  4. Copyright (C) 1997 Michael Van Canneyt
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ChangeLog
  17. ---------
  18. Current version is 0.6
  19. Version Date Remarks
  20. ------- ---- ----
  21. 0.1 07/16/97 Unit started. Michael.
  22. 0.2 07/06/98 Updated for version 0.99.5
  23. 0.4 08/01/98 Objects for name lookup implemented
  24. 0.5 09/10/98 Updated calls for 0.99.8.
  25. 0.6 05/04/99 Added explicit asmmode.
  26. ------------------------------------------------------------------- }
  27. interface
  28. Uses Initc; // link to libc.
  29. Const
  30. { Net type }
  31. AF_INET = 2;
  32. { Error constants. Returned by LastError method of THost, TNet}
  33. NETDB_INTERNAL= -1; { see errno }
  34. NETDB_SUCCESS = 0; { no problem }
  35. HOST_NOT_FOUND= 1; { Authoritative Answer Host not found }
  36. TRY_AGAIN = 2; { Non-Authoritive Host not found, or SERVERFAIL }
  37. NO_RECOVERY = 3; { Non recoverable errors, FORMERR, REFUSED, NOTIMP }
  38. NO_DATA = 4; { Valid name, no data record of requested type }
  39. NO_ADDRESS = NO_DATA; { no address, look for MX record }
  40. Type
  41. THostAddr = array[1..4] of byte;
  42. PHostAddr = ^THostAddr;
  43. Const
  44. NoAddress : THostAddr = (0,0,0,0);
  45. Type
  46. { THostEnt Object }
  47. THostEnt = record
  48. Name : pchar; { Official name }
  49. Aliases : ppchar; { Null-terminated list of aliases}
  50. Addrtype : longint; { Host address type }
  51. Addrlen : longint; { Length of address }
  52. Addrlist : ppchar; { null-terminated list of adresses }
  53. end;
  54. PHostEnt = ^THostEnt;
  55. { TNetEnt object }
  56. TNetEnt = record
  57. Name : pchar; { Official name }
  58. Aliases : ppchar; { Nill-terminated alias list }
  59. AddrType : longint; { Net address type }
  60. net : Longint; { Network number }
  61. end;
  62. PNetEnt = ^TNetEnt;
  63. TServEnt = record
  64. name : pchar; { Service name }
  65. aliases : ppchar; { Null-terminated alias list }
  66. port : longint; { Port number }
  67. proto : pchar; { Protocol to use }
  68. end;
  69. PServEnt = ^TServEnt;
  70. { Pascal Wrapper objects }
  71. TSelectType = (stFirst,stNext,stPrevious);
  72. THost = Object
  73. FHostEntry : PHostEnt;
  74. FAlias,FAddr,FError : Longint;
  75. Constructor NameLookup (HostName : String);
  76. Constructor AddressLookup (Const Address : THostAddr);
  77. Destructor Done;
  78. Function Name : String;
  79. Function GetAddress (Select : TSelectType) : String;
  80. Function GetAlias (Select : TSelectType) : String;
  81. Function IPAddress : THostAddr;
  82. Function IPString : String;
  83. Function LastError : Longint;
  84. end;
  85. TNet = Object
  86. FNetEntry : PNetEnt;
  87. FAlias,FError : Longint;
  88. Constructor NameLookup (NetName : String);
  89. Constructor AddressLookup (Const Address : Longint);
  90. Destructor Done;
  91. Function Name : String;
  92. Function GetAlias (Select : TSelectType) : String;
  93. Function IPAddress : Longint;
  94. Function IPString : String;
  95. Function LastError : Longint;
  96. end;
  97. TService = Object
  98. FServiceEntry : PServEnt;
  99. FAlias,FError : Longint;
  100. Constructor NameLookup (ServiceName,Proto : String);
  101. Constructor PortLookup (APort : Longint; Proto: string);
  102. Destructor Done;
  103. Function Name : String;
  104. Function Protocol : String;
  105. Function GetAlias (Select : TSelectType) : String;
  106. Function Port : Longint;
  107. Function LastError : Longint;
  108. end;
  109. { Pascal style calls }
  110. function HostAddrToStr (Entry : THostAddr) : String;
  111. function StrToHostAddr (IP : String) : THostAddr;
  112. function NetAddrToStr (Entry : Longint) : String;
  113. function StrToNetAddr (IP : String) : Longint;
  114. Function HostToNet (Host : Longint) : Longint;
  115. Function NetToHost (Net : Longint) : Longint;
  116. Function ShortHostToNet (Host : Word) : Word;
  117. Function ShortNetToHost (Net : Word) : Word;
  118. { C style calls, linked in from Libc }
  119. function gethostent : PHostEnt; cdecl; external;
  120. function gethostbyname ( Name : Pchar) : PHostEnt; cdecl; external;
  121. function gethostbyaddr ( Addr : PHostAddr; Len : Longint; HType : Longint) : PHostent ; cdecl; external;
  122. procedure sethostent (stayopen : longint); cdecl; external;
  123. procedure endhostent; cdecl; external;
  124. function getnetent : PNetEnt; cdecl; external;
  125. function getnetbyname ( Name : pchar) : PNetEnt; cdecl; external;
  126. function getnetbyaddr ( Net : Longint; nettype : Longint) : PNetEnt; cdecl; external;
  127. procedure setnetent ( Stayopen : Longint); cdecl; external;
  128. procedure endnetent; cdecl; external;
  129. function getservent : PServEnt; cdecl; external;
  130. function getservbyname (name : pchar ; protocol : pchar) : PServEnt; cdecl; external;
  131. function getservbyport (port : longint; protocol : pchar) : PServEnt; cdecl; external;
  132. procedure setservent (StayOpen : longint); cdecl; external;
  133. procedure endservent; cdecl; external;
  134. Function GetDNSError : libcint;
  135. implementation
  136. Uses strings;
  137. Function GetDNSError:libcint;
  138. begin
  139. GetDNSError:=fpGetCErrno;
  140. end;
  141. function HostAddrToStr (Entry : THostAddr) : String;
  142. Var Dummy : String[4];
  143. I : Longint;
  144. begin
  145. HostAddrToStr:='';
  146. For I:=1 to 4 do
  147. begin
  148. Str(Entry[I],Dummy);
  149. HostAddrToStr:=HostAddrToStr+Dummy;
  150. If I<4 Then HostAddrToStr:=HostAddrToStr+'.';
  151. end;
  152. end;
  153. function StrToHostAddr(IP : String) : THostAddr ;
  154. Var Dummy : String[4];
  155. I : Longint;
  156. J : Integer;
  157. Temp : THostAddr;
  158. begin
  159. StrToHostAddr:=NoAddress;
  160. For I:=1 to 4 do
  161. begin
  162. If I<4 Then
  163. begin
  164. J:=Pos('.',IP);
  165. If J=0 then exit;
  166. Dummy:=Copy(IP,1,J-1);
  167. Delete (IP,1,J);
  168. end
  169. else
  170. Dummy:=IP;
  171. Val (Dummy,Temp[I],J);
  172. If J<>0 then Exit;
  173. end;
  174. StrToHostAddr:=Temp;
  175. end;
  176. function NetAddrToStr (Entry : longint) : String;
  177. Var Dummy : String[4];
  178. I : Longint;
  179. begin
  180. NetAddrToStr:='';
  181. For I:=4 downto 1 do
  182. begin
  183. Str(THostAddr(Entry)[I],Dummy);
  184. NetAddrToStr:=NetAddrToStr+Dummy;
  185. If I>1 Then NetAddrToStr:=NetAddrToStr+'.';
  186. end;
  187. end;
  188. function StrToNetAddr(IP : String) : Longint;
  189. begin
  190. StrToNetAddr:=Longint(StrToHostAddr(IP));
  191. end;
  192. Constructor THost.NameLookup (HostName : String);
  193. begin
  194. HostName:=HostName+#0;
  195. FHostEntry:=GetHostByName(pchar(@HostName[1]));
  196. If FHostEntry=Nil then
  197. FError:=GetDNSError
  198. else
  199. begin
  200. FAlias:=0;
  201. FAddr:=0;
  202. Ferror:=0;
  203. end;
  204. end;
  205. Constructor THost.AddressLookup (Const Address: THostAddr);
  206. begin
  207. FHostEntry:=GetHostByAddr(PHostAddr(@Address),SizeOf(Address),AF_INET);
  208. If FHostEntry=Nil then
  209. FError:=GetDNSError
  210. else
  211. begin
  212. FAlias:=0;
  213. FAddr:=0;
  214. FError:=0;
  215. end;
  216. end;
  217. Function THost.Name : String;
  218. begin
  219. Name:='';
  220. If (FHostEntry=Nil) or (FError<>0) then exit;
  221. Name:=StrPas(FHostEntry^.Name);
  222. end;
  223. Function THost.GetAlias (Select : TSelectType) : String;
  224. begin
  225. GetAlias:='';
  226. If (FHostEntry=Nil) or (FError<>0) then exit;
  227. Case Select of
  228. stFirst : FAlias:=0;
  229. stnext : If FHostEntry^.Aliases[FAlias]<>Nil then
  230. Falias:=Falias+1;
  231. stprevious : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  232. end;
  233. If FHostEntry^.Aliases[FAlias]<>Nil then
  234. GetAlias:=StrPas(FHostEntry^.Aliases[FAlias]);
  235. end;
  236. Function THost.GetAddress (Select : TSelectType) : String;
  237. begin
  238. GetAddress:='';
  239. If (FHostEntry=Nil) or (FError<>0) then exit;
  240. Case Select of
  241. stFirst : FAddr:=0;
  242. stnext : If FHostEntry^.AddrList[FAddr]<>Nil then
  243. FAddr:=FAddr+1;
  244. stprevious : If FAddr=0 Then Exit else FAddr:=FAddr-1;
  245. end;
  246. If FHostEntry^.AddrList[FAddr]<>Nil then
  247. GetAddress:=HostAddrToStr(PHostAddr(FHostEntry^.AddrList[FAddr])^);
  248. end;
  249. Function THost.IPstring : String;
  250. begin
  251. IPString:='';
  252. If (FHostEntry=Nil) or (FError<>0) then exit;
  253. If FHostEntry^.AddrList[0]<>Nil then
  254. IPString:=HostAddrToStr(PHostAddr(FHostEntry^.AddrList[0])^);
  255. end;
  256. Function THost.IPaddress : THostAddr;
  257. begin
  258. IPAddress:=NoAddress;
  259. If (FHostEntry=Nil) or (FError<>0) then exit;
  260. IPAddress:=PHostAddr(FHostEntry^.AddrList[0])^;
  261. end;
  262. Destructor THost.Done;
  263. begin
  264. end;
  265. Function THost.LastError : Longint;
  266. begin
  267. LastError:=FError;
  268. end;
  269. Constructor TNet.NameLookup (NetName : String);
  270. begin
  271. NetName:=NetName+#0;
  272. FNetEntry:=GetNetByName(pchar(@NetName[1]));
  273. If FNetEntry=Nil then
  274. FError:=GetDNSError
  275. else
  276. begin
  277. FAlias:=0;
  278. Ferror:=0;
  279. end;
  280. end;
  281. Constructor TNet.AddressLookup (Const Address: Longint);
  282. begin
  283. FNetEntry:=GetNetByAddr(Address,AF_INET);
  284. If FNetEntry=Nil then
  285. FError:=GetDNSError
  286. else
  287. begin
  288. FAlias:=0;
  289. FError:=0;
  290. end;
  291. end;
  292. Function TNet.Name : String;
  293. begin
  294. Name:='';
  295. If (FNetEntry=Nil) or (FError<>0) then exit;
  296. Name:=StrPas(FNetEntry^.Name);
  297. end;
  298. Function TNet.GetAlias (Select : TSelectType) : String;
  299. begin
  300. GetAlias:='';
  301. If (FNetEntry=Nil) or (FError<>0) then exit;
  302. Case Select of
  303. stFirst : FAlias:=0;
  304. stnext : If FNetEntry^.Aliases[FAlias]<>Nil then
  305. Falias:=Falias+1;
  306. stprevious : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  307. end;
  308. If FNetEntry^.Aliases[FAlias]<>Nil then
  309. GetAlias:=StrPas(FNetEntry^.Aliases[FAlias]);
  310. end;
  311. Function TNet.IPstring : String;
  312. begin
  313. IPString:='';
  314. If (FNetEntry=Nil) or (FError<>0) then exit;
  315. IPString:=NetAddrToStr(FNetEntry^.Net);
  316. end;
  317. Function TNet.IPaddress : Longint;
  318. begin
  319. IPAddress:=0;
  320. If (FNetEntry=Nil) or (FError<>0) then exit;
  321. IPAddress:=FNetEntry^.Net;
  322. end;
  323. Destructor TNet.Done;
  324. begin
  325. end;
  326. Function TNet.LastError : Longint;
  327. begin
  328. LastError:=FError;
  329. end;
  330. Constructor TService.NameLookup (ServiceName,Proto : String);
  331. begin
  332. ServiceName:=ServiceName+#0;
  333. Proto:=Proto+#0;
  334. FServiceEntry:=GetServByName(pchar(@ServiceName[1]),pchar(@Proto[1]));
  335. If FServiceEntry=Nil then
  336. FError:=GetDNSError
  337. else
  338. begin
  339. FAlias:=0;
  340. Ferror:=0;
  341. end;
  342. end;
  343. Constructor TService.PortLookup (APort: Longint; Proto : String);
  344. begin
  345. Proto:=proto+#0;
  346. FServiceEntry:=GetServByPort(APort,pchar(@proto[1]));
  347. If FServiceEntry=Nil then
  348. FError:=GetDNSError
  349. else
  350. begin
  351. FAlias:=0;
  352. FError:=0;
  353. end;
  354. end;
  355. Function TService.Name : String;
  356. begin
  357. Name:='';
  358. If (FServiceEntry=Nil) or (FError<>0) then exit;
  359. Name:=StrPas(FServiceEntry^.Name);
  360. end;
  361. Function TService.GetAlias (Select : TSelectType) : String;
  362. begin
  363. GetAlias:='';
  364. If (FServiceEntry=Nil) or (FError<>0) then exit;
  365. Case Select of
  366. stFirst : FAlias:=0;
  367. stnext : If FServiceEntry^.Aliases[FAlias]<>Nil then
  368. Falias:=Falias+1;
  369. stprevious : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  370. end;
  371. If FServiceEntry^.Aliases[FAlias]<>Nil then
  372. GetAlias:=StrPas(FServiceEntry^.Aliases[FAlias]);
  373. end;
  374. Function TService.Protocol : String;
  375. begin
  376. Protocol:='';
  377. If (FServiceEntry=Nil) or (FError<>0) then exit;
  378. Protocol:=Strpas(FServiceEntry^.proto);
  379. end;
  380. Function TService.Port : Longint;
  381. begin
  382. Port:=0;
  383. If (FServiceEntry=Nil) or (FError<>0) then exit;
  384. Port:=FServiceEntry^.Port;
  385. end;
  386. Destructor TService.Done;
  387. begin
  388. end;
  389. Function TService.LastError : Longint;
  390. begin
  391. LastError:=FError;
  392. end;
  393. Function HostToNet (Host : Longint) : Longint;
  394. begin
  395. HostToNet:=THostAddr(host)[1];
  396. HostToNEt:=HostTONet or ( (THostAddr(host)[2]) shl 8);
  397. HostToNEt:=HostToNet or ( (THostAddr(host)[3]) shl 16);
  398. HostToNEt:=HostToNet or ( (THostAddr(host)[4]) shl 24);
  399. end;
  400. Function NetToHost (Net : Longint) : Longint;
  401. begin
  402. NetToHost:=THostAddr(Net)[1];
  403. NetToHost:=NetToHost or ( (THostAddr(Net)[2]) shl 8);
  404. NetToHost:=NetToHost or ( (THostAddr(Net)[3]) shl 16);
  405. NetToHost:=NetToHost or ( (THostAddr(Net)[4]) shl 24);
  406. end;
  407. Function ShortHostToNet (Host : Word) : Word;
  408. begin
  409. ShortHostToNet:=lo(host)*256+Hi(Host);
  410. end;
  411. Function ShortNetToHost (Net : Word) : Word;
  412. begin
  413. ShortNetToHost:=lo(Net)*256+Hi(Net);
  414. end;
  415. end.
  416. $Log$
  417. Revision 1.3 2003-12-10 12:16:14 marco
  418. * now uses initc
  419. Revision 1.2 2002/09/07 15:42:52 peter
  420. * old logs removed and tabs fixed
  421. Revision 1.1 2002/01/29 17:54:53 peter
  422. * splitted to base and extra
  423. }