resolve.pp 21 KB

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