resolve.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. {$MODE OBJFPC}
  2. {$H+}
  3. Unit resolve;
  4. {$ifndef win32}
  5. // Here till BSD supports the netbsd unit.
  6. // MvdV: NetBSD unit? Where?
  7. {$ifdef Unix}
  8. // Undefine this to use the C library resolve routines.
  9. // Don't use under win32, netdb does not work on Win32 (yet) !!
  10. {$define usenetdb}
  11. {$endif Unix}
  12. {$endif}
  13. { --------------------------------------------------------------------
  14. Unit for internet domain calls.
  15. Copyright (C) 2003 Michael Van Canneyt
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 1, or (at your option)
  19. any later version.
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24. You should have received a copy of the GNU General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. ------------------------------------------------------------------- }
  28. interface
  29. uses
  30. Classes,UriParser;
  31. Type
  32. THostAddr = array[1..4] of byte;
  33. PHostAddr = ^THostAddr;
  34. TNetAddr = THostAddr;
  35. PNetAddr = ^TNetAddr;
  36. Const
  37. NoAddress : THostAddr = (0,0,0,0);
  38. NoNet : TNetAddr = (0,0,0,0);
  39. { ---------------------------------------------------------------------
  40. Axuliary routines
  41. ---------------------------------------------------------------------}
  42. function HostAddrToStr (Entry : THostAddr) : String;
  43. function StrToHostAddr (IP : String) : THostAddr;
  44. function NetAddrToStr (Entry : TNetAddr) : String;
  45. function StrToNetAddr (IP : String) : TNetAddr;
  46. Function HostToNet (Host : ThostAddr) : ThostAddr;
  47. Function HostToNet (Host : Longint) : Longint;
  48. Function NetToHost (Net : Longint) : Longint;
  49. Function NetToHost (Net : TNetAddr) : TNetAddr;
  50. Function ShortHostToNet (Host : Word) : Word;
  51. Function ShortNetToHost (Net : Word) : Word;
  52. Type
  53. { ---------------------------------------------------------------------
  54. TResolver
  55. ---------------------------------------------------------------------}
  56. TResolver = Class (TComponent)
  57. Private
  58. FName : String;
  59. FAliases : TStringList;
  60. FRaiseOnError : Boolean;
  61. FLastError: Integer;
  62. Function GetAlias(Index : Integer) : STring;
  63. Function GetAliasCount : Integer;
  64. Function GetAliasSorted : Boolean;
  65. Procedure SetAliasSorted (Value : Boolean);
  66. Protected
  67. Procedure CheckOperation(Msg : String);
  68. Function NameLookup(Const S : String) : Boolean; virtual;
  69. Procedure SaveAliases(P : PPChar);
  70. Public
  71. Constructor Create(AOwner : TComponent); override;
  72. Destructor Destroy; override;
  73. Procedure ClearData; virtual;
  74. Property ResolvedName : String Read FName;
  75. Property Aliases [Index : integer ] : string Read GetAlias;
  76. Property AliasCount : Integer read GetAliasCount;
  77. Property SortAliases : Boolean Read GetAliasSorted Write SetAliasSorted;
  78. Property RaiseOnError : Boolean Read FRaiseOnError Write FRAiseOnError;
  79. Property LastError : Integer Read FlastError;
  80. end;
  81. { ---------------------------------------------------------------------
  82. THostResolver
  83. ---------------------------------------------------------------------}
  84. THostResolver = Class(TResolver)
  85. Private
  86. FHostAddress : THostAddr;
  87. FAddressCount : Integer;
  88. FAddresses : PHostAddr;
  89. Function GetAddress (Index : Integer) : THostAddr;
  90. Function GetNetAddress (Index : Integer) : THostAddr;
  91. Function GetNetHostAddress : THostAddr;
  92. Function GetAsString : String;
  93. Procedure SaveHostEntry (Entry : Pointer);
  94. Public
  95. Procedure ClearData; Override;
  96. Function NameLookup(Const S : String) : Boolean; override;
  97. Function AddressLookup(Const S : String) : Boolean; virtual;
  98. Function AddressLookup(Const Address : THostAddr) : Boolean; virtual;
  99. Property HostAddress : THostAddr Read FHostAddress;
  100. Property NetHostAddress : THostAddr Read GetNetHostAddress;
  101. Property AddressAsString : String Read GetAsString;
  102. Property AddressCount : Integer Read FAddressCount ;
  103. Property Addresses [Index : Integer] : ThostAddr Read GetAddress;
  104. Property NetAddresses [Index : Integer] : ThostAddr Read GetNetAddress;
  105. end;
  106. { ---------------------------------------------------------------------
  107. TNetResolver
  108. ---------------------------------------------------------------------}
  109. TNetResolver = Class(TResolver)
  110. Private
  111. FNetAddress : TNetAddr;
  112. FAddrType : Integer;
  113. Function GetAsString : String;
  114. Procedure SaveNetEntry(Entry : Pointer);
  115. Function GetNetAddress : TNetAddr;
  116. Public
  117. Procedure ClearData; override;
  118. Function NameLookup(Const S : String) : boolean; override;
  119. Function AddressLookup(Const S : String) : Boolean; virtual;
  120. Function AddressLookup(Const Address : TNetAddr) : Boolean; virtual;
  121. Property NetAddress : TNetAddr Read FNetAddress;
  122. Property NetNetAddress : TNetAddr Read GetNetAddress;
  123. Property AddressAsString : String Read GetAsString;
  124. Property AddressType : Integer Read FAddrType;
  125. end;
  126. { ---------------------------------------------------------------------
  127. TServiceResolver
  128. ---------------------------------------------------------------------}
  129. TServiceResolver = Class(TResolver)
  130. private
  131. FProtocol : String;
  132. FPort : Integer;
  133. Procedure SaveServiceEntry(Entry : Pointer);
  134. Function GetNetPort : Integer ;
  135. public
  136. Procedure ClearData; override;
  137. Function NameLookup (Const S : String) : boolean; override;
  138. Function NameLookup (Const S,Proto : String) : Boolean;
  139. Function PortLookup (APort : Longint; Proto: string) : Boolean;
  140. Property Protocol : String Read FProtocol;
  141. Property Port : Integer Read FPort;
  142. Property NetPort : Integer Read GetNetPort;
  143. end;
  144. TURIParser = Class(TComponent)
  145. Private
  146. FActive : Boolean;
  147. FProtocol: String;
  148. FUsername: String;
  149. FPassword: String;
  150. FHost: String;
  151. FPort: Word;
  152. FPath: String;
  153. FDocument: String;
  154. FParams: String;
  155. FBookmark: String;
  156. FURI : String;
  157. Protected
  158. Procedure SetElement (Index : Integer; Value : String);Virtual;
  159. Function GetElement(Index : Integer) : String;
  160. Procedure SetPort(Value : Word);
  161. Procedure SetURI(Value : String);
  162. Public
  163. Procedure Clear;
  164. Procedure ParseUri(AURI : String);
  165. Function ComposeURI : String;
  166. Published
  167. Property Port: Word Read FPort Write SetPort;
  168. Property Protocol: String Index 0 Read GetElement Write SetElement;
  169. Property Username: String Index 1 Read GetElement Write SetElement;
  170. Property Password: String Index 2 Read GetElement Write SetElement;
  171. Property Host: String Index 3 Read GetElement Write SetElement;
  172. Property Path: String index 4 Read GetElement Write SetElement;
  173. Property Document: String index 5 read GetElement Write SetElement;
  174. Property Params: String Index 6 read GetElement Write SetElement;
  175. Property Bookmark: String Index 7 Read GetElement Write SetElement;
  176. Property URI : String Read FURI write SetURI;
  177. Property Active : Boolean Read FActive Write FActive;
  178. end;
  179. Resourcestring
  180. SErrHostByName = 'Host by name';
  181. SErrHostByAddr = 'Host by address';
  182. SErrNetByName = 'Net by name';
  183. SErrServByName = 'Service by name';
  184. SErrServByPort = 'Service by port';
  185. Implementation
  186. { ---------------------------------------------------------------------
  187. Include system dependent stuff.
  188. ---------------------------------------------------------------------}
  189. {$ifdef usenetdb}
  190. uses netdb;
  191. {$else}
  192. {$i resolve.inc}
  193. {$endif}
  194. function HostAddrToStr (Entry : THostAddr) : String;
  195. Var Dummy : String[4];
  196. I : Longint;
  197. begin
  198. HostAddrToStr:='';
  199. For I:=1 to 4 do
  200. begin
  201. Str(Entry[I],Dummy);
  202. HostAddrToStr:=HostAddrToStr+Dummy;
  203. If I<4 Then
  204. HostAddrToStr:=HostAddrToStr+'.';
  205. end;
  206. end;
  207. function StrToHostAddr(IP : String) : THostAddr ;
  208. Var
  209. Dummy : String;
  210. I : Longint;
  211. J : Integer;
  212. Temp : THostAddr;
  213. begin
  214. Result:=NoAddress;
  215. For I:=1 to 4 do
  216. begin
  217. If I<4 Then
  218. begin
  219. J:=Pos('.',IP);
  220. If J=0 then
  221. exit;
  222. Dummy:=Copy(IP,1,J-1);
  223. Delete (IP,1,J);
  224. end
  225. else
  226. Dummy:=IP;
  227. Val (Dummy,Temp[I],J);
  228. If J<>0 then Exit;
  229. end;
  230. Result:=Temp;
  231. end;
  232. function NetAddrToStr (Entry : TNetAddr) : String;
  233. Var Dummy : String[4];
  234. I : Longint;
  235. begin
  236. NetAddrToStr:='';
  237. For I:=4 downto 1 do
  238. begin
  239. Str(Entry[I],Dummy);
  240. NetAddrToStr:=NetAddrToStr+Dummy;
  241. If I>1 Then
  242. NetAddrToStr:=NetAddrToStr+'.';
  243. end;
  244. end;
  245. function StrToNetAddr(IP : String) : TNetAddr;
  246. begin
  247. StrToNetAddr:=TNetAddr(StrToHostAddr(IP));
  248. end;
  249. Function HostToNet (Host : ThostAddr) : THostAddr;
  250. begin
  251. Result[1]:=Host[4];
  252. Result[2]:=Host[3];
  253. Result[3]:=Host[2];
  254. Result[4]:=Host[1];
  255. end;
  256. Function NetToHost (Net : TNetAddr) : TNetAddr;
  257. begin
  258. Result[1]:=Net[4];
  259. Result[2]:=Net[3];
  260. Result[3]:=Net[2];
  261. Result[4]:=Net[1];
  262. end;
  263. Function HostToNet (Host : Longint) : Longint;
  264. begin
  265. Result:=Longint(HostToNet(THostAddr(host)));
  266. end;
  267. Function NetToHost (Net : Longint) : Longint;
  268. begin
  269. Result:=Longint(NetToHost(TNetAddr(Net)));
  270. end;
  271. Function ShortHostToNet (Host : Word) : Word;
  272. begin
  273. ShortHostToNet:=lo(host)*256+Hi(Host);
  274. end;
  275. Function ShortNetToHost (Net : Word) : Word;
  276. begin
  277. ShortNetToHost:=lo(Net)*256+Hi(Net);
  278. end;
  279. { ---------------------------------------------------------------------
  280. TResolver
  281. ---------------------------------------------------------------------}
  282. Constructor TResolver.Create(AOwner : TComponent);
  283. begin
  284. Inherited;
  285. FAliases:=TstringList.Create;
  286. end;
  287. Destructor TResolver.Destroy;
  288. begin
  289. ClearData;
  290. FAliases.Free;
  291. end;
  292. Procedure TResolver.ClearData;
  293. begin
  294. FName:='';
  295. FAliases.Clear;
  296. end;
  297. Function TResolver.GetAlias(Index : Integer) : STring;
  298. begin
  299. Result:=FAliases[Index];
  300. end;
  301. Function TResolver.GetAliasCount : Integer;
  302. begin
  303. Result:=FAliases.Count;
  304. end;
  305. Function TResolver.GetAliasSorted : Boolean;
  306. begin
  307. Result:=FAliases.Sorted;
  308. end;
  309. Procedure TResolver.SetAliasSorted (Value : Boolean);
  310. begin
  311. FAliases.Sorted:=Value;
  312. end;
  313. Procedure TResolver.CheckOperation(Msg : String);
  314. begin
  315. end;
  316. Function TResolver.NameLookup(Const S : String) : Boolean;
  317. begin
  318. ClearData;
  319. FName:=S;
  320. Result:=True;
  321. end;
  322. Procedure TResolver.SaveAliases(P : PPChar);
  323. Var
  324. I : Integer;
  325. begin
  326. If (P<>Nil) then
  327. begin
  328. I:=0;
  329. While P[I]<>Nil do
  330. begin
  331. FAliases.Add(StrPas(P[I]));
  332. Inc(I);
  333. end;
  334. end;
  335. end;
  336. { ---------------------------------------------------------------------
  337. THostResolver
  338. ---------------------------------------------------------------------}
  339. Function THostResolver.GetAddress (Index : Integer) : THostAddr;
  340. begin
  341. If (Index>=0) and (Index<FAddressCount) then
  342. Result:=FAddresses[Index];
  343. end;
  344. Function THostResolver.GetAsString : String;
  345. begin
  346. Result:=HostAddrToStr(FHostAddress);
  347. end;
  348. Procedure THostResolver.ClearData;
  349. begin
  350. Inherited;
  351. FHostAddress:=NoAddress;
  352. If FAddressCount<>0 Then
  353. FreeMem(FAddresses);
  354. FAddressCount:=0;
  355. FAddresses:=Nil;
  356. end;
  357. Function THostResolver.AddressLookup(Const S : String) : Boolean;
  358. begin
  359. Result:=AddressLookup(StrToHostAddr(S));
  360. end;
  361. {$ifdef usenetdb}
  362. Function THostResolver.NameLookup (Const S : String) : Boolean;
  363. Var
  364. H : THostEntry;
  365. begin
  366. Result:=Inherited NameLookup(S);
  367. If Result then
  368. begin
  369. Result:=GetHostByName(S,H);
  370. if not Result then
  371. Result:=ResolveHostByName(S,H);
  372. If Result then
  373. SaveHostEntry(@H);
  374. end;
  375. end;
  376. Function THostResolver.AddressLookup (Const Address: THostAddr) : Boolean;
  377. Var
  378. H : THostEntry;
  379. begin
  380. ClearData;
  381. Result:=ResolveHostByAddr(Address,H);
  382. If Result then
  383. SaveHostEntry(@H);
  384. end;
  385. Procedure THostResolver.SaveHostEntry(Entry : Pointer);
  386. Var
  387. PH : ^THostEntry;
  388. I : Integer;
  389. begin
  390. PH:=ENtry;
  391. FName:=PH^.Name;
  392. FHostAddress:=PH^.Addr;
  393. FAddressCount:=1;
  394. GetMem(FAddresses,SizeOf(THostAddr));
  395. FAddresses[0]:=PH^.Addr;
  396. FAliases.CommaText:=PH^.Aliases;
  397. end;
  398. {$else}
  399. Function THostResolver.NameLookup (Const S : String) : Boolean;
  400. Var
  401. FHostEntry : PHostEntry;
  402. begin
  403. Result:=Inherited NameLookup(S);
  404. If Result then
  405. begin
  406. FHostEntry:=GetHostByName(pchar(FName));
  407. Result:=FHostEntry<>Nil;
  408. If Result then
  409. SaveHostEntry(FHostEntry)
  410. else
  411. begin
  412. FLastError:=GetDNSError;
  413. CheckOperation(SErrHostByName);
  414. end;
  415. end;
  416. end;
  417. Procedure THostResolver.SaveHostEntry(Entry : Pointer);
  418. Var
  419. P : Pointer;
  420. I,Count : Integer;
  421. begin
  422. With PHostEntry(Entry)^ do
  423. begin
  424. FName:=StrPas(H_Name);
  425. FAddressCount:=0;
  426. While H_Addr[FAddressCount]<>Nil do
  427. Inc(FAddressCount);
  428. If FAddressCount>0 then
  429. begin
  430. GetMem(FAddresses,FAddressCount*SizeOf(THostAddr));
  431. For I:=0 to FAddressCount-1 do
  432. FAddresses[I]:=PHostAddr(H_Addr[I])^;
  433. FHostAddress:=FAddresses[0];
  434. end;
  435. SaveAliases(H_Aliases);
  436. end;
  437. end;
  438. Function THostResolver.AddressLookup (Const Address: THostAddr) : Boolean;
  439. Var
  440. FHostEntry : PHostEntry;
  441. begin
  442. ClearData;
  443. FHostEntry:=GetHostByAddr(Pchar(@Address),SizeOf(Address),AF_INET);
  444. Result:=FHostEntry<>Nil;
  445. If Result then
  446. SaveHostEntry(FHostEntry)
  447. else
  448. begin
  449. FLastError:=GetDNSError;
  450. CheckOperation(SErrHostByAddr);
  451. end;
  452. end;
  453. {$endif}
  454. Function THostResolver.GetNetAddress (Index : Integer) : THostAddr;
  455. begin
  456. Result:=HostToNet(Addresses[Index]);
  457. end;
  458. Function THostResolver.GetNetHostAddress : THostAddr;
  459. begin
  460. Result:=HostToNet(FHostAddress);
  461. end;
  462. { ---------------------------------------------------------------------
  463. TNetResolver
  464. ---------------------------------------------------------------------}
  465. {$ifdef usenetdb}
  466. Function TNetResolver.AddressLookup (Const Address: TNetAddr) : boolean;
  467. Var
  468. N : TNetworkEntry;
  469. begin
  470. ClearData;
  471. Result:=GetNetworkByAddr(Address,N);
  472. If Result then
  473. SaveNetEntry(@N);
  474. end;
  475. Function TNetResolver.NameLookup (Const S : String) : Boolean;
  476. Var
  477. N : TNetworkEntry;
  478. begin
  479. Result:=Inherited NameLookup(S);
  480. If Result then
  481. begin
  482. Result:=GetNetworkByName(S,N);
  483. If Result then
  484. SaveNetEntry(@N);
  485. end;
  486. end;
  487. Procedure TNetResolver.SaveNetEntry(Entry : Pointer);
  488. Var
  489. PN : ^TNetworkEntry;
  490. begin
  491. PN:=ENtry;
  492. FName:=PN^.Name;
  493. FNetAddress:=PN^.Addr;
  494. FAliases.CommaText:=PN^.Aliases;
  495. end;
  496. {$else}
  497. Function TNetResolver.NameLookup (Const S : String) : Boolean;
  498. Var
  499. FNetEntry : PNetEntry;
  500. begin
  501. Result:=Inherited NameLookup(S);
  502. If Result then
  503. begin
  504. FNetEntry:=GetNetByName(pchar(S));
  505. Result:=FNetEntry<>Nil;
  506. If Result then
  507. SaveNetEntry(FNetEntry)
  508. else
  509. begin
  510. FLastError:=GetDNSError;
  511. Checkoperation(SErrNetByName);
  512. end;
  513. end;
  514. end;
  515. Procedure TNetResolver.SaveNetEntry(Entry : Pointer);
  516. begin
  517. With PNetEntry(Entry)^ do
  518. begin
  519. FName:=StrPas(N_Name);
  520. FAddrType:=N_addrtype;
  521. FNetAddress:=NetToHost(TNetAddr(N_net));
  522. SaveAliases(N_Aliases);
  523. end;
  524. end;
  525. Function TNetResolver.AddressLookup (Const Address: TNetAddr) : boolean;
  526. Var
  527. FNetEntry : PNetEntry;
  528. begin
  529. ClearData;
  530. {$ifndef win32}
  531. FNetEntry:=GetNetByAddr(Longint(HostToNet(Address)),AF_INET);
  532. {$else}
  533. FNetEntry:=Nil;
  534. {$endif}
  535. Result:=FNetEntry<>Nil;
  536. If Result then
  537. SaveNetEntry(FNetEntry)
  538. else
  539. begin
  540. FLastError:=GetDNSError;
  541. CheckOperation(SErrNetByName);
  542. end;
  543. end;
  544. {$endif}
  545. Function TNetResolver.AddressLookup(Const S : String) : Boolean;
  546. begin
  547. Result:=AddressLookup(StrToNetAddr(S));
  548. end;
  549. Function TNetResolver.GetAsString : String;
  550. begin
  551. Result:=HostAddrToStr(FNetAddress);
  552. end;
  553. Function TNetResolver.GetNetAddress : TNetAddr;
  554. begin
  555. Result:=HostToNet(FNetAddress);
  556. end;
  557. Procedure TNetResolver.ClearData;
  558. begin
  559. Inherited;
  560. FNetAddress:=NoAddress;
  561. FAddrType:=0;
  562. end;
  563. { ---------------------------------------------------------------------
  564. TServiceResolver
  565. ---------------------------------------------------------------------}
  566. Function TServiceResolver.NameLookup (Const S : String) : Boolean;
  567. begin
  568. Result:=NameLookup(S,'');
  569. end;
  570. {$ifdef usenetdb}
  571. Function TServiceResolver.NameLookup (Const S,Proto : String) : Boolean;
  572. Var
  573. E : TServiceEntry;
  574. begin
  575. ClearData;
  576. Result:=GetServiceByName(S,Proto,E);
  577. If Result then
  578. SaveServiceEntry(@E);
  579. end;
  580. Function TServiceResolver.PortLookup (APort: Longint; Proto : String) : Boolean;
  581. Var
  582. E : TServiceEntry;
  583. begin
  584. ClearData;
  585. Result:=GetServiceByPort(APort,Proto,E);
  586. If Result then
  587. SaveServiceEntry(@E);
  588. end;
  589. Procedure TServiceResolver.SaveServiceEntry(Entry : Pointer);
  590. Var
  591. PE : ^TServiceEntry;
  592. begin
  593. PE:=Entry;
  594. FName:=PE^.Name;
  595. FPort:=PE^.Port;
  596. FProtocol:=PE^.Protocol;
  597. FAliases.CommaText:=PE^.Aliases;
  598. end;
  599. {$else}
  600. Function TServiceResolver.NameLookup (Const S,Proto : String) : Boolean;
  601. Var
  602. FServiceEntry : PServEntry;
  603. begin
  604. ClearData;
  605. FName:=S;
  606. FProtocol:=Proto;
  607. If (proto='') then
  608. FServiceEntry:=GetServByName(pchar(S),Nil)
  609. else
  610. FServiceEntry:=GetServByName(pchar(S),PChar(FProtocol));
  611. Result:=FServiceEntry<>Nil;
  612. If Result then
  613. SaveServiceEntry(FServiceEntry)
  614. else
  615. begin
  616. FLastError:=GetDNSError;
  617. CheckOperation(SErrServByName);
  618. end;
  619. end;
  620. Function TServiceResolver.PortLookup (APort: Longint; Proto : String) : Boolean;
  621. Var
  622. FServiceEntry : PServEntry;
  623. begin
  624. ClearData;
  625. APort:=ShortHostToNet(APort);
  626. FProtoCol:=Proto;
  627. If (Proto='') then
  628. FServiceEntry:=GetServByPort(APort,Nil)
  629. else
  630. FServiceEntry:=GetServByPort(APort,pchar(Proto));
  631. Result:=FServiceEntry<>Nil;
  632. If Result then
  633. SaveServiceEntry(FServiceEntry)
  634. else
  635. begin
  636. FLastError:=GetDNSError;
  637. CheckOperation(SErrServByPort);
  638. end;
  639. end;
  640. Procedure TServiceResolver.SaveServiceEntry(Entry : Pointer);
  641. begin
  642. With PServEntry(Entry)^ do
  643. begin
  644. FName:=strpas(s_name);
  645. FPort:=ShortHostToNet(S_port);
  646. FProtocol:=strpas(s_proto);
  647. SaveAliases(S_aliases);
  648. end;
  649. end;
  650. {$endif}
  651. Procedure TServiceResolver.ClearData;
  652. begin
  653. Inherited;
  654. FProtocol:='';
  655. FPort:=0;
  656. end;
  657. Function TServiceResolver.GetNetPort : Integer;
  658. begin
  659. Result:=ShortHostToNet(FPort);
  660. end;
  661. { ---------------------------------------------------------------------
  662. TURIParser
  663. ---------------------------------------------------------------------}
  664. Procedure TURIParser.SetElement (Index : Integer; Value : String);
  665. begin
  666. Case index of
  667. 0 : FProtocol := Value;
  668. 1 : FUsername := Value;
  669. 2 : FPassword := Value;
  670. 3 : FHost := Value;
  671. 4 : FPath := Value;
  672. 5 : FDocument := Value;
  673. 6 : FParams := Value;
  674. 7 : FBookmark := Value;
  675. else
  676. end;
  677. If FActive and not (csLoading in ComponentState) then
  678. FURI:=ComposeURI;
  679. end;
  680. Function TURIParser.GetElement(Index : Integer) : String;
  681. begin
  682. Case Index of
  683. 0 : Result := FProtocol;
  684. 1 : Result := FUsername;
  685. 2 : Result := FPassword;
  686. 3 : Result := FHost ;
  687. 4 : Result := FPath ;
  688. 5 : Result := FDocument;
  689. 6 : Result := FParams ;
  690. 7 : Result := FBookmark;
  691. else
  692. Result:='';
  693. end;
  694. end;
  695. Procedure TURIParser.SetPort(Value : Word);
  696. begin
  697. FPort:=Value;
  698. If FActive and not (csLoading in ComponentState) then
  699. FURI:=ComposeURI;
  700. end;
  701. Procedure TURIParser.SetURI(Value : String);
  702. begin
  703. If Active and not (csLoading in ComponentState) then
  704. begin
  705. Clear;
  706. ParseUri(Value);
  707. end;
  708. FURI:=Value;
  709. end;
  710. Procedure TURIParser.Clear;
  711. begin
  712. FProtocol :='';
  713. FUsername :='';
  714. FPassword :='';
  715. FHost :='';
  716. FPort :=0;
  717. FPath :='';
  718. FDocument :='';
  719. FParams :='';
  720. FBookmark :='';
  721. FURI :='';
  722. end;
  723. Procedure TURIParser.ParseUri(AURI : String);
  724. Var
  725. U : TURI;
  726. begin
  727. U:=UriParser.ParseURI(AUri);
  728. FProtocol := u.Protocol;
  729. FUsername := u.Username;
  730. FPassword := u.Password;
  731. FHost := u.Host ;
  732. FPort := u.Port ;
  733. FPath := u.Path ;
  734. FDocument := u.Document;
  735. FParams := u.Params ;
  736. FBookmark := u.Bookmark;
  737. end;
  738. Function TURIParser.ComposeURI : String;
  739. var
  740. U : TURI;
  741. begin
  742. U.Protocol := FProtocol;
  743. U.Username := FUsername;
  744. U.Password := FPassword;
  745. U.Host := FHost ;
  746. U.Port := FPort ;
  747. U.Path := FPath ;
  748. U.Document := FDocument;
  749. U.Params := FParams ;
  750. U.Bookmark := FBookmark;
  751. Result:=EncodeUri(U);
  752. end;
  753. {$ifdef usenetdb}
  754. Procedure InitResolve;
  755. begin
  756. end;
  757. Procedure FinalResolve;
  758. begin
  759. end;
  760. {$endif}
  761. Initialization
  762. InitResolve;
  763. Finalization
  764. FinalResolve;
  765. end.
  766. {
  767. $Log$
  768. Revision 1.9 2004-02-03 10:37:32 michael
  769. + Fixed win32 compilation
  770. Revision 1.8 2004/02/02 14:42:00 marco
  771. * more small fixes
  772. Revision 1.7 2004/01/31 19:02:50 sg
  773. * Tries to first resolve names locally before contacting the DNS server
  774. Revision 1.6 2003/12/11 09:23:50 marco
  775. * patch from peter
  776. Revision 1.5 2003/12/10 15:50:50 marco
  777. * fpgetcerrno introduction
  778. Revision 1.4 2003/05/17 21:52:37 michael
  779. + Added TURIParser class
  780. Revision 1.3 2003/03/07 20:33:33 michael
  781. Use native pascal netdb on Linux
  782. Revision 1.2 2003/02/03 10:14:12 michael
  783. + Added init/final routines to initialize winsock library
  784. Revision 1.1 2003/02/01 16:50:38 michael
  785. + Added resolve unit for WIndows/unix
  786. }