inet.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. {$LINKLIB c}
  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 : integer) : integer;
  117. Function ShortNetToHost (Net : integer) : integer;
  118. { C style calls, linked in from Libc }
  119. function GetHostEnt : PHostEnt;cdecl;
  120. function GetHostByName ( HostName : Pchar) : PHostEnt;cdecl;
  121. function GetHostByAddr ( Addr : PHostAddr; Len : Longint; HType : Longint) : PHostEnt;cdecl;
  122. procedure SetHostEnt (stayopen : longint);cdecl;
  123. procedure EndHostEnt;cdecl;
  124. function GetNetEnt : PNetEnt;cdecl;
  125. function GetNetByName ( Name : pchar) : PNetEnt;cdecl;
  126. function GetNetByAddr ( Net : Longint; NetType : Longint) : PNetEnt;cdecl;
  127. procedure SetNetEnt ( Stayopen : Longint);cdecl;
  128. procedure EndNetEnt;cdecl;
  129. function getservent : PServEnt;cdecl;
  130. function getservbyname (name : pchar ; protocol : pchar) : PServEnt;cdecl;
  131. function getservbyport (port : longint; protocol : pchar) : PServEnt;cdecl;
  132. procedure setservent (StayOpen : longint);cdecl;
  133. procedure endservent;cdecl;
  134. implementation
  135. Uses strings;
  136. function gethostent : PHostEnt; cdecl; external;
  137. function gethostbyname ( Name : Pchar) : PHostEnt; cdecl; external;
  138. function gethostbyaddr ( Addr : PHostAddr; Len : Longint; HType : Longint) : PHostent ; cdecl; external;
  139. procedure sethostent (stayopen : longint); cdecl; external;
  140. procedure endhostent; cdecl; external;
  141. function getnetent : PNetEnt; cdecl; external;
  142. function getnetbyname ( Name : pchar) : PNetEnt; cdecl; external;
  143. function getnetbyaddr ( Net : Longint; nettype : Longint) : PNetEnt; cdecl; external;
  144. procedure setnetent ( Stayopen : Longint); cdecl; external;
  145. procedure endnetent; cdecl; external;
  146. function getservent : PServEnt; cdecl; external;
  147. function getservbyname (name : pchar ; protocol : pchar) : PServEnt; cdecl; external;
  148. function getservbyport (port : longint; protocol : pchar) : PServEnt; cdecl; external;
  149. procedure setservent (StayOpen : longint); cdecl; external;
  150. procedure endservent; cdecl; external;
  151. Function GetDNSError : Longint;
  152. {$asmmode direct}
  153. begin
  154. asm
  155. movl h_errno,%eax
  156. movl %eax,__RESULT
  157. end ['EAX'];
  158. end;
  159. function HostAddrToStr (Entry : THostAddr) : String;
  160. Var Dummy : String[4];
  161. I : Longint;
  162. begin
  163. HostAddrToStr:='';
  164. For I:=1 to 4 do
  165. begin
  166. Str(Entry[I],Dummy);
  167. HostAddrToStr:=HostAddrToStr+Dummy;
  168. If I<4 Then HostAddrToStr:=HostAddrToStr+'.';
  169. end;
  170. end;
  171. function StrToHostAddr(IP : String) : THostAddr ;
  172. Var Dummy : String[4];
  173. I : Longint;
  174. J : Integer;
  175. Temp : THostAddr;
  176. begin
  177. StrToHostAddr:=NoAddress;
  178. For I:=1 to 4 do
  179. begin
  180. If I<4 Then
  181. begin
  182. J:=Pos('.',IP);
  183. If J=0 then exit;
  184. Dummy:=Copy(IP,1,J-1);
  185. Delete (IP,1,J);
  186. end
  187. else
  188. Dummy:=IP;
  189. Val (Dummy,Temp[I],J);
  190. If J<>0 then Exit;
  191. end;
  192. StrToHostAddr:=Temp;
  193. end;
  194. function NetAddrToStr (Entry : longint) : String;
  195. Var Dummy : String[4];
  196. I : Longint;
  197. begin
  198. NetAddrToStr:='';
  199. For I:=4 downto 1 do
  200. begin
  201. Str(THostAddr(Entry)[I],Dummy);
  202. NetAddrToStr:=NetAddrToStr+Dummy;
  203. If I>1 Then NetAddrToStr:=NetAddrToStr+'.';
  204. end;
  205. end;
  206. function StrToNetAddr(IP : String) : Longint;
  207. begin
  208. StrToNetAddr:=Longint(StrToHostAddr(IP));
  209. end;
  210. Constructor THost.NameLookup (HostName : String);
  211. begin
  212. HostName:=HostName+#0;
  213. FHostEntry:=GetHostByName(pchar(@HostName[1]));
  214. If FHostEntry=Nil then
  215. FError:=GetDNSError
  216. else
  217. begin
  218. FAlias:=0;
  219. FAddr:=0;
  220. Ferror:=0;
  221. end;
  222. end;
  223. Constructor THost.AddressLookup (Const Address: THostAddr);
  224. begin
  225. FHostEntry:=GetHostByAddr(PHostAddr(@Address),SizeOf(Address),AF_INET);
  226. If FHostEntry=Nil then
  227. FError:=GetDNSError
  228. else
  229. begin
  230. FAlias:=0;
  231. FAddr:=0;
  232. FError:=0;
  233. end;
  234. end;
  235. Function THost.Name : String;
  236. begin
  237. Name:='';
  238. If (FHostEntry=Nil) or (FError<>0) then exit;
  239. Name:=StrPas(FHostEntry^.Name);
  240. end;
  241. Function THost.GetAlias (Select : TSelectType) : String;
  242. begin
  243. GetAlias:='';
  244. If (FHostEntry=Nil) or (FError<>0) then exit;
  245. Case Select of
  246. stFirst : FAlias:=0;
  247. stnext : If FHostEntry^.Aliases[FAlias]<>Nil then
  248. Falias:=Falias+1;
  249. stprevious : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  250. end;
  251. If FHostEntry^.Aliases[FAlias]<>Nil then
  252. GetAlias:=StrPas(FHostEntry^.Aliases[FAlias]);
  253. end;
  254. Function THost.GetAddress (Select : TSelectType) : String;
  255. begin
  256. GetAddress:='';
  257. If (FHostEntry=Nil) or (FError<>0) then exit;
  258. Case Select of
  259. stFirst : FAddr:=0;
  260. stnext : If FHostEntry^.AddrList[FAddr]<>Nil then
  261. FAddr:=FAddr+1;
  262. stprevious : If FAddr=0 Then Exit else FAddr:=FAddr-1;
  263. end;
  264. If FHostEntry^.AddrList[FAddr]<>Nil then
  265. GetAddress:=HostAddrToStr(PHostAddr(FHostEntry^.AddrList[FAddr])^);
  266. end;
  267. Function THost.IPstring : String;
  268. begin
  269. IPString:='';
  270. If (FHostEntry=Nil) or (FError<>0) then exit;
  271. If FHostEntry^.AddrList[0]<>Nil then
  272. IPString:=HostAddrToStr(PHostAddr(FHostEntry^.AddrList[0])^);
  273. end;
  274. Function THost.IPaddress : THostAddr;
  275. begin
  276. IPAddress:=NoAddress;
  277. If (FHostEntry=Nil) or (FError<>0) then exit;
  278. IPAddress:=PHostAddr(FHostEntry^.AddrList[0])^;
  279. end;
  280. Destructor THost.Done;
  281. begin
  282. end;
  283. Function THost.LastError : Longint;
  284. begin
  285. LastError:=FError;
  286. end;
  287. Constructor TNet.NameLookup (NetName : String);
  288. begin
  289. NetName:=NetName+#0;
  290. FNetEntry:=GetNetByName(pchar(@NetName[1]));
  291. If FNetEntry=Nil then
  292. FError:=GetDNSError
  293. else
  294. begin
  295. FAlias:=0;
  296. Ferror:=0;
  297. end;
  298. end;
  299. Constructor TNet.AddressLookup (Const Address: Longint);
  300. begin
  301. FNetEntry:=GetNetByAddr(Address,AF_INET);
  302. If FNetEntry=Nil then
  303. FError:=GetDNSError
  304. else
  305. begin
  306. FAlias:=0;
  307. FError:=0;
  308. end;
  309. end;
  310. Function TNet.Name : String;
  311. begin
  312. Name:='';
  313. If (FNetEntry=Nil) or (FError<>0) then exit;
  314. Name:=StrPas(FNetEntry^.Name);
  315. end;
  316. Function TNet.GetAlias (Select : TSelectType) : String;
  317. begin
  318. GetAlias:='';
  319. If (FNetEntry=Nil) or (FError<>0) then exit;
  320. Case Select of
  321. stFirst : FAlias:=0;
  322. stnext : If FNetEntry^.Aliases[FAlias]<>Nil then
  323. Falias:=Falias+1;
  324. stprevious : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  325. end;
  326. If FNetEntry^.Aliases[FAlias]<>Nil then
  327. GetAlias:=StrPas(FNetEntry^.Aliases[FAlias]);
  328. end;
  329. Function TNet.IPstring : String;
  330. begin
  331. IPString:='';
  332. If (FNetEntry=Nil) or (FError<>0) then exit;
  333. IPString:=NetAddrToStr(FNetEntry^.Net);
  334. end;
  335. Function TNet.IPaddress : Longint;
  336. begin
  337. IPAddress:=0;
  338. If (FNetEntry=Nil) or (FError<>0) then exit;
  339. IPAddress:=FNetEntry^.Net;
  340. end;
  341. Destructor TNet.Done;
  342. begin
  343. end;
  344. Function TNet.LastError : Longint;
  345. begin
  346. LastError:=FError;
  347. end;
  348. Constructor TService.NameLookup (ServiceName,Proto : String);
  349. begin
  350. ServiceName:=ServiceName+#0;
  351. Proto:=Proto+#0;
  352. FServiceEntry:=GetServByName(pchar(@ServiceName[1]),pchar(@Proto[1]));
  353. If FServiceEntry=Nil then
  354. FError:=GetDNSError
  355. else
  356. begin
  357. FAlias:=0;
  358. Ferror:=0;
  359. end;
  360. end;
  361. Constructor TService.PortLookup (APort: Longint; Proto : String);
  362. begin
  363. Proto:=proto+#0;
  364. FServiceEntry:=GetServByPort(APort,pchar(@proto[1]));
  365. If FServiceEntry=Nil then
  366. FError:=GetDNSError
  367. else
  368. begin
  369. FAlias:=0;
  370. FError:=0;
  371. end;
  372. end;
  373. Function TService.Name : String;
  374. begin
  375. Name:='';
  376. If (FServiceEntry=Nil) or (FError<>0) then exit;
  377. Name:=StrPas(FServiceEntry^.Name);
  378. end;
  379. Function TService.GetAlias (Select : TSelectType) : String;
  380. begin
  381. GetAlias:='';
  382. If (FServiceEntry=Nil) or (FError<>0) then exit;
  383. Case Select of
  384. stFirst : FAlias:=0;
  385. stnext : If FServiceEntry^.Aliases[FAlias]<>Nil then
  386. Falias:=Falias+1;
  387. stprevious : If FAlias=0 Then Exit else FAlias:=FAlias-1;
  388. end;
  389. If FServiceEntry^.Aliases[FAlias]<>Nil then
  390. GetAlias:=StrPas(FServiceEntry^.Aliases[FAlias]);
  391. end;
  392. Function TService.Protocol : String;
  393. begin
  394. Protocol:='';
  395. If (FServiceEntry=Nil) or (FError<>0) then exit;
  396. Protocol:=Strpas(FServiceEntry^.proto);
  397. end;
  398. Function TService.Port : Longint;
  399. begin
  400. Port:=0;
  401. If (FServiceEntry=Nil) or (FError<>0) then exit;
  402. Port:=FServiceEntry^.Port;
  403. end;
  404. Destructor TService.Done;
  405. begin
  406. end;
  407. Function TService.LastError : Longint;
  408. begin
  409. LastError:=FError;
  410. end;
  411. Function HostToNet (Host : Longint) : Longint;
  412. begin
  413. HostToNet:=THostAddr(host)[1];
  414. HostToNEt:=HostTONet or ( (THostAddr(host)[2]) shl 8);
  415. HostToNEt:=HostToNet or ( (THostAddr(host)[3]) shl 16);
  416. HostToNEt:=HostToNet or ( (THostAddr(host)[4]) shl 24);
  417. end;
  418. Function NetToHost (Net : Longint) : Longint;
  419. begin
  420. NetToHost:=THostAddr(Net)[1];
  421. NetToHost:=NetToHost or ( (THostAddr(Net)[2]) shl 8);
  422. NetToHost:=NetToHost or ( (THostAddr(Net)[3]) shl 16);
  423. NetToHost:=NetToHost or ( (THostAddr(Net)[4]) shl 24);
  424. end;
  425. Function ShortHostToNet (Host : integer) : integer;
  426. begin
  427. ShortHostToNet:=lo(host)*256+Hi(Host);
  428. end;
  429. Function ShortNetToHost (Net : integer) : integer;
  430. begin
  431. ShortNetToHost:=lo(Net)*256+Hi(Net);
  432. end;
  433. end.