resolve.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 linux}
  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 linux}
  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. uses initc;
  193. {$i resolve.inc}
  194. {$endif}
  195. function HostAddrToStr (Entry : THostAddr) : String;
  196. Var Dummy : String[4];
  197. I : Longint;
  198. begin
  199. HostAddrToStr:='';
  200. For I:=1 to 4 do
  201. begin
  202. Str(Entry[I],Dummy);
  203. HostAddrToStr:=HostAddrToStr+Dummy;
  204. If I<4 Then
  205. HostAddrToStr:=HostAddrToStr+'.';
  206. end;
  207. end;
  208. function StrToHostAddr(IP : String) : THostAddr ;
  209. Var
  210. Dummy : String;
  211. I : Longint;
  212. J : Integer;
  213. Temp : THostAddr;
  214. begin
  215. Result:=NoAddress;
  216. For I:=1 to 4 do
  217. begin
  218. If I<4 Then
  219. begin
  220. J:=Pos('.',IP);
  221. If J=0 then
  222. exit;
  223. Dummy:=Copy(IP,1,J-1);
  224. Delete (IP,1,J);
  225. end
  226. else
  227. Dummy:=IP;
  228. Val (Dummy,Temp[I],J);
  229. If J<>0 then Exit;
  230. end;
  231. Result:=Temp;
  232. end;
  233. function NetAddrToStr (Entry : TNetAddr) : String;
  234. Var Dummy : String[4];
  235. I : Longint;
  236. begin
  237. NetAddrToStr:='';
  238. For I:=4 downto 1 do
  239. begin
  240. Str(Entry[I],Dummy);
  241. NetAddrToStr:=NetAddrToStr+Dummy;
  242. If I>1 Then
  243. NetAddrToStr:=NetAddrToStr+'.';
  244. end;
  245. end;
  246. function StrToNetAddr(IP : String) : TNetAddr;
  247. begin
  248. StrToNetAddr:=TNetAddr(StrToHostAddr(IP));
  249. end;
  250. Function HostToNet (Host : ThostAddr) : THostAddr;
  251. begin
  252. Result[1]:=Host[4];
  253. Result[2]:=Host[3];
  254. Result[3]:=Host[2];
  255. Result[4]:=Host[1];
  256. end;
  257. Function NetToHost (Net : TNetAddr) : TNetAddr;
  258. begin
  259. Result[1]:=Net[4];
  260. Result[2]:=Net[3];
  261. Result[3]:=Net[2];
  262. Result[4]:=Net[1];
  263. end;
  264. Function HostToNet (Host : Longint) : Longint;
  265. begin
  266. Result:=Longint(HostToNet(THostAddr(host)));
  267. end;
  268. Function NetToHost (Net : Longint) : Longint;
  269. begin
  270. Result:=Longint(NetToHost(TNetAddr(Net)));
  271. end;
  272. Function ShortHostToNet (Host : Word) : Word;
  273. begin
  274. ShortHostToNet:=lo(host)*256+Hi(Host);
  275. end;
  276. Function ShortNetToHost (Net : Word) : Word;
  277. begin
  278. ShortNetToHost:=lo(Net)*256+Hi(Net);
  279. end;
  280. { ---------------------------------------------------------------------
  281. TResolver
  282. ---------------------------------------------------------------------}
  283. Constructor TResolver.Create(AOwner : TComponent);
  284. begin
  285. Inherited;
  286. FAliases:=TstringList.Create;
  287. end;
  288. Destructor TResolver.Destroy;
  289. begin
  290. ClearData;
  291. FAliases.Free;
  292. end;
  293. Procedure TResolver.ClearData;
  294. begin
  295. FName:='';
  296. FAliases.Clear;
  297. end;
  298. Function TResolver.GetAlias(Index : Integer) : STring;
  299. begin
  300. Result:=FAliases[Index];
  301. end;
  302. Function TResolver.GetAliasCount : Integer;
  303. begin
  304. Result:=FAliases.Count;
  305. end;
  306. Function TResolver.GetAliasSorted : Boolean;
  307. begin
  308. Result:=FAliases.Sorted;
  309. end;
  310. Procedure TResolver.SetAliasSorted (Value : Boolean);
  311. begin
  312. FAliases.Sorted:=Value;
  313. end;
  314. Procedure TResolver.CheckOperation(Msg : String);
  315. begin
  316. end;
  317. Function TResolver.NameLookup(Const S : String) : Boolean;
  318. begin
  319. ClearData;
  320. FName:=S;
  321. Result:=True;
  322. end;
  323. Procedure TResolver.SaveAliases(P : PPChar);
  324. Var
  325. I : Integer;
  326. begin
  327. If (P<>Nil) then
  328. begin
  329. I:=0;
  330. While P[I]<>Nil do
  331. begin
  332. FAliases.Add(StrPas(P[I]));
  333. Inc(I);
  334. end;
  335. end;
  336. end;
  337. { ---------------------------------------------------------------------
  338. THostResolver
  339. ---------------------------------------------------------------------}
  340. Function THostResolver.GetAddress (Index : Integer) : THostAddr;
  341. begin
  342. If (Index>=0) and (Index<FAddressCount) then
  343. Result:=FAddresses[Index];
  344. end;
  345. Function THostResolver.GetAsString : String;
  346. begin
  347. Result:=HostAddrToStr(FHostAddress);
  348. end;
  349. Procedure THostResolver.ClearData;
  350. begin
  351. Inherited;
  352. FHostAddress:=NoAddress;
  353. If FAddressCount<>0 Then
  354. FreeMem(FAddresses);
  355. FAddressCount:=0;
  356. FAddresses:=Nil;
  357. end;
  358. Function THostResolver.AddressLookup(Const S : String) : Boolean;
  359. begin
  360. Result:=AddressLookup(StrToHostAddr(S));
  361. end;
  362. {$ifdef usenetdb}
  363. Function THostResolver.NameLookup (Const S : String) : Boolean;
  364. Var
  365. H : THostEntry;
  366. begin
  367. Result:=Inherited NameLookup(S);
  368. If Result then
  369. begin
  370. Result:=GetHostByName(S,H);
  371. If Result then
  372. SaveHostEntry(@H);
  373. end;
  374. end;
  375. Function THostResolver.AddressLookup (Const Address: THostAddr) : Boolean;
  376. Var
  377. H : THostEntry;
  378. begin
  379. ClearData;
  380. Result:=GetHostByAddr(Address,H);
  381. If Result then
  382. SaveHostEntry(@H);
  383. end;
  384. Procedure THostResolver.SaveHostEntry(Entry : Pointer);
  385. Var
  386. PH : ^THostEntry;
  387. I : Integer;
  388. begin
  389. PH:=ENtry;
  390. FName:=PH^.Name;
  391. FHostAddress:=PH^.Addr;
  392. FAddressCount:=1;
  393. GetMem(FAddresses,SizeOf(THostAddr));
  394. FAddresses[0]:=PH^.Addr;
  395. FAliases.CommaText:=PH^.Aliases;
  396. end;
  397. {$else}
  398. Function THostResolver.NameLookup (Const S : String) : Boolean;
  399. Var
  400. FHostEntry : PHostEntry;
  401. begin
  402. Result:=Inherited NameLookup(S);
  403. If Result then
  404. begin
  405. FHostEntry:=GetHostByName(pchar(FName));
  406. Result:=FHostEntry<>Nil;
  407. If Result then
  408. SaveHostEntry(FHostEntry)
  409. else
  410. begin
  411. FLastError:=GetDNSError;
  412. CheckOperation(SErrHostByName);
  413. end;
  414. end;
  415. end;
  416. Procedure THostResolver.SaveHostEntry(Entry : Pointer);
  417. Var
  418. P : Pointer;
  419. I,Count : Integer;
  420. begin
  421. With PHostEntry(Entry)^ do
  422. begin
  423. FName:=StrPas(H_Name);
  424. FAddressCount:=0;
  425. While H_Addr[FAddressCount]<>Nil do
  426. Inc(FAddressCount);
  427. If FAddressCount>0 then
  428. begin
  429. GetMem(FAddresses,FAddressCount*SizeOf(THostAddr));
  430. For I:=0 to FAddressCount-1 do
  431. FAddresses[I]:=PHostAddr(H_Addr[I])^;
  432. FHostAddress:=FAddresses[0];
  433. end;
  434. SaveAliases(H_Aliases);
  435. end;
  436. end;
  437. Function THostResolver.AddressLookup (Const Address: THostAddr) : Boolean;
  438. Var
  439. FHostEntry : PHostEntry;
  440. begin
  441. ClearData;
  442. FHostEntry:=GetHostByAddr(Pchar(@Address),SizeOf(Address),AF_INET);
  443. Result:=FHostEntry<>Nil;
  444. If Result then
  445. SaveHostEntry(FHostEntry)
  446. else
  447. begin
  448. FLastError:=GetDNSError;
  449. CheckOperation(SErrHostByAddr);
  450. end;
  451. end;
  452. {$endif}
  453. Function THostResolver.GetNetAddress (Index : Integer) : THostAddr;
  454. begin
  455. Result:=HostToNet(Addresses[Index]);
  456. end;
  457. Function THostResolver.GetNetHostAddress : THostAddr;
  458. begin
  459. Result:=HostToNet(FHostAddress);
  460. end;
  461. { ---------------------------------------------------------------------
  462. TNetResolver
  463. ---------------------------------------------------------------------}
  464. {$ifdef usenetdb}
  465. Function TNetResolver.AddressLookup (Const Address: TNetAddr) : boolean;
  466. Var
  467. N : TNetworkEntry;
  468. begin
  469. ClearData;
  470. Result:=GetNetworkByAddr(Address,N);
  471. If Result then
  472. SaveNetEntry(@N);
  473. end;
  474. Function TNetResolver.NameLookup (Const S : String) : Boolean;
  475. Var
  476. N : TNetworkEntry;
  477. begin
  478. Result:=Inherited NameLookup(S);
  479. If Result then
  480. begin
  481. Result:=GetNetworkByName(S,N);
  482. If Result then
  483. SaveNetEntry(@N);
  484. end;
  485. end;
  486. Procedure TNetResolver.SaveNetEntry(Entry : Pointer);
  487. Var
  488. PN : ^TNetworkEntry;
  489. begin
  490. PN:=ENtry;
  491. FName:=PN^.Name;
  492. FNetAddress:=PN^.Addr;
  493. FAliases.CommaText:=PN^.Aliases;
  494. end;
  495. {$else}
  496. Function TNetResolver.NameLookup (Const S : String) : Boolean;
  497. Var
  498. FNetEntry : PNetEntry;
  499. begin
  500. Result:=Inherited NameLookup(S);
  501. If Result then
  502. begin
  503. FNetEntry:=GetNetByName(pchar(S));
  504. Result:=FNetEntry<>Nil;
  505. If Result then
  506. SaveNetEntry(FNetEntry)
  507. else
  508. begin
  509. FLastError:=GetDNSError;
  510. Checkoperation(SErrNetByName);
  511. end;
  512. end;
  513. end;
  514. Procedure TNetResolver.SaveNetEntry(Entry : Pointer);
  515. begin
  516. With PNetEntry(Entry)^ do
  517. begin
  518. FName:=StrPas(N_Name);
  519. FAddrType:=N_addrtype;
  520. FNetAddress:=NetToHost(TNetAddr(N_net));
  521. SaveAliases(N_Aliases);
  522. end;
  523. end;
  524. Function TNetResolver.AddressLookup (Const Address: TNetAddr) : boolean;
  525. Var
  526. FNetEntry : PNetEntry;
  527. begin
  528. ClearData;
  529. {$ifndef win32}
  530. FNetEntry:=GetNetByAddr(Longint(HostToNet(Address)),AF_INET);
  531. {$else}
  532. FNetEntry:=Nil;
  533. {$endif}
  534. Result:=FNetEntry<>Nil;
  535. If Result then
  536. SaveNetEntry(FNetEntry)
  537. else
  538. begin
  539. FLastError:=GetDNSError;
  540. CheckOperation(SErrNetByName);
  541. end;
  542. end;
  543. {$endif}
  544. Function TNetResolver.AddressLookup(Const S : String) : Boolean;
  545. begin
  546. Result:=AddressLookup(StrToNetAddr(S));
  547. end;
  548. Function TNetResolver.GetAsString : String;
  549. begin
  550. Result:=HostAddrToStr(FNetAddress);
  551. end;
  552. Function TNetResolver.GetNetAddress : TNetAddr;
  553. begin
  554. Result:=HostToNet(FNetAddress);
  555. end;
  556. Procedure TNetResolver.ClearData;
  557. begin
  558. Inherited;
  559. FNetAddress:=NoAddress;
  560. FAddrType:=0;
  561. end;
  562. { ---------------------------------------------------------------------
  563. TServiceResolver
  564. ---------------------------------------------------------------------}
  565. Function TServiceResolver.NameLookup (Const S : String) : Boolean;
  566. begin
  567. Result:=NameLookup(S,'');
  568. end;
  569. {$ifdef usenetdb}
  570. Function TServiceResolver.NameLookup (Const S,Proto : String) : Boolean;
  571. Var
  572. E : TServiceEntry;
  573. begin
  574. ClearData;
  575. Result:=GetServiceByName(S,Proto,E);
  576. If Result then
  577. SaveServiceEntry(@E);
  578. end;
  579. Function TServiceResolver.PortLookup (APort: Longint; Proto : String) : Boolean;
  580. Var
  581. E : TServiceEntry;
  582. begin
  583. ClearData;
  584. Result:=GetServiceByPort(APort,Proto,E);
  585. If Result then
  586. SaveServiceEntry(@E);
  587. end;
  588. Procedure TServiceResolver.SaveServiceEntry(Entry : Pointer);
  589. Var
  590. PE : ^TServiceEntry;
  591. begin
  592. PE:=Entry;
  593. FName:=PE^.Name;
  594. FPort:=PE^.Port;
  595. FProtocol:=PE^.Protocol;
  596. FAliases.CommaText:=PE^.Aliases;
  597. end;
  598. {$else}
  599. Function TServiceResolver.NameLookup (Const S,Proto : String) : Boolean;
  600. Var
  601. FServiceEntry : PServEntry;
  602. begin
  603. ClearData;
  604. FName:=S;
  605. FProtocol:=Proto;
  606. If (proto='') then
  607. FServiceEntry:=GetServByName(pchar(S),Nil)
  608. else
  609. FServiceEntry:=GetServByName(pchar(S),PChar(FProtocol));
  610. Result:=FServiceEntry<>Nil;
  611. If Result then
  612. SaveServiceEntry(FServiceEntry)
  613. else
  614. begin
  615. FLastError:=GetDNSError;
  616. CheckOperation(SErrServByName);
  617. end;
  618. end;
  619. Function TServiceResolver.PortLookup (APort: Longint; Proto : String) : Boolean;
  620. Var
  621. FServiceEntry : PServEntry;
  622. begin
  623. ClearData;
  624. APort:=ShortHostToNet(APort);
  625. FProtoCol:=Proto;
  626. If (Proto='') then
  627. FServiceEntry:=GetServByPort(APort,Nil)
  628. else
  629. FServiceEntry:=GetServByPort(APort,pchar(Proto));
  630. Result:=FServiceEntry<>Nil;
  631. If Result then
  632. SaveServiceEntry(FServiceEntry)
  633. else
  634. begin
  635. FLastError:=GetDNSError;
  636. CheckOperation(SErrServByPort);
  637. end;
  638. end;
  639. Procedure TServiceResolver.SaveServiceEntry(Entry : Pointer);
  640. begin
  641. With PServEntry(Entry)^ do
  642. begin
  643. FName:=strpas(s_name);
  644. FPort:=ShortHostToNet(S_port);
  645. FProtocol:=strpas(s_proto);
  646. SaveAliases(S_aliases);
  647. end;
  648. end;
  649. {$endif}
  650. Procedure TServiceResolver.ClearData;
  651. begin
  652. Inherited;
  653. FProtocol:='';
  654. FPort:=0;
  655. end;
  656. Function TServiceResolver.GetNetPort : Integer;
  657. begin
  658. Result:=ShortHostToNet(FPort);
  659. end;
  660. { ---------------------------------------------------------------------
  661. TURIParser
  662. ---------------------------------------------------------------------}
  663. Procedure TURIParser.SetElement (Index : Integer; Value : String);
  664. begin
  665. Case index of
  666. 0 : FProtocol := Value;
  667. 1 : FUsername := Value;
  668. 2 : FPassword := Value;
  669. 3 : FHost := Value;
  670. 4 : FPath := Value;
  671. 5 : FDocument := Value;
  672. 6 : FParams := Value;
  673. 7 : FBookmark := Value;
  674. else
  675. end;
  676. If FActive and not (csLoading in ComponentState) then
  677. FURI:=ComposeURI;
  678. end;
  679. Function TURIParser.GetElement(Index : Integer) : String;
  680. begin
  681. Case Index of
  682. 0 : Result := FProtocol;
  683. 1 : Result := FUsername;
  684. 2 : Result := FPassword;
  685. 3 : Result := FHost ;
  686. 4 : Result := FPath ;
  687. 5 : Result := FDocument;
  688. 6 : Result := FParams ;
  689. 7 : Result := FBookmark;
  690. else
  691. Result:='';
  692. end;
  693. end;
  694. Procedure TURIParser.SetPort(Value : Word);
  695. begin
  696. FPort:=Value;
  697. If FActive and not (csLoading in ComponentState) then
  698. FURI:=ComposeURI;
  699. end;
  700. Procedure TURIParser.SetURI(Value : String);
  701. begin
  702. If Active and not (csLoading in ComponentState) then
  703. begin
  704. Clear;
  705. ParseUri(Value);
  706. end;
  707. FURI:=Value;
  708. end;
  709. Procedure TURIParser.Clear;
  710. begin
  711. FProtocol :='';
  712. FUsername :='';
  713. FPassword :='';
  714. FHost :='';
  715. FPort :=0;
  716. FPath :='';
  717. FDocument :='';
  718. FParams :='';
  719. FBookmark :='';
  720. FURI :='';
  721. end;
  722. Procedure TURIParser.ParseUri(AURI : String);
  723. Var
  724. U : TURI;
  725. begin
  726. U:=UriParser.ParseURI(AUri);
  727. FProtocol := u.Protocol;
  728. FUsername := u.Username;
  729. FPassword := u.Password;
  730. FHost := u.Host ;
  731. FPort := u.Port ;
  732. FPath := u.Path ;
  733. FDocument := u.Document;
  734. FParams := u.Params ;
  735. FBookmark := u.Bookmark;
  736. end;
  737. Function TURIParser.ComposeURI : String;
  738. var
  739. U : TURI;
  740. begin
  741. U.Protocol := FProtocol;
  742. U.Username := FUsername;
  743. U.Password := FPassword;
  744. U.Host := FHost ;
  745. U.Port := FPort ;
  746. U.Path := FPath ;
  747. U.Document := FDocument;
  748. U.Params := FParams ;
  749. U.Bookmark := FBookmark;
  750. Result:=EncodeUri(U);
  751. end;
  752. {$ifdef usenetdb}
  753. Procedure InitResolve;
  754. begin
  755. end;
  756. Procedure FinalResolve;
  757. begin
  758. end;
  759. {$endif}
  760. Initialization
  761. InitResolve;
  762. Finalization
  763. FinalResolve;
  764. end.
  765. {
  766. $Log$
  767. Revision 1.5 2003-12-10 15:50:50 marco
  768. * fpgetcerrno introduction
  769. Revision 1.4 2003/05/17 21:52:37 michael
  770. + Added TURIParser class
  771. Revision 1.3 2003/03/07 20:33:33 michael
  772. Use native pascal netdb on Linux
  773. Revision 1.2 2003/02/03 10:14:12 michael
  774. + Added init/final routines to initialize winsock library
  775. Revision 1.1 2003/02/01 16:50:38 michael
  776. + Added resolve unit for WIndows/unix
  777. }