inet.pp 12 KB

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