Recent

Author Topic: Easy way for resolve dns with Indy  (Read 6539 times)

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Easy way for resolve dns with Indy
« on: September 18, 2012, 03:21:29 pm »
Y try a simple function for convert dns name to ip address.

Y try with IdDNSResolver1, but not work

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var x:integer;
begin
    with IdDNSResolver1 do begin
        try
            Host := '8.8.8.8';
            QueryType := [qtA];
            Resolve('www.facebook.com',nil,0);
            if QueryResult.Count > 0 then  begin
                  Memo1.Lines.Add(QueryResult.Items[0].name);
                end
                else begin
                Memo1.Lines.Add('no work');
            end;
        finally
            Free;
        end;
    end;
end;                   

Any idea
Thanks



BeniBela

  • Hero Member
  • *****
  • Posts: 869
    • homepage
Re: Easy way for resolve dns with Indy
« Reply #1 on: September 18, 2012, 10:55:48 pm »
Why do you want to use Indy for that?

Just fpc alone works like that:

 
Code: [Select]
uses netdb;
var
  temp: THostEntry;
begin
  ResolveHostByName('google.com', temp);
  writeln(temp.Addr.s_bytes[1],'.',temp.Addr.s_bytes[2],'.',temp.Addr.s_bytes[3],'.',temp.Addr.s_bytes[4]);
end.

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: Easy way for resolve dns with Indy
« Reply #2 on: September 18, 2012, 11:23:05 pm »
Thanks BeniBela

this works fine.

BeniBela

  • Hero Member
  • *****
  • Posts: 869
    • homepage
Re: Easy way for resolve dns with Indy
« Reply #3 on: September 19, 2012, 11:23:51 am »
Some things you might want to be aware of:

- need another function for ip6 looks up

- on unix, it uses the nameservers from the resolv.conf, it has read at program start => when the file  later changes it is not automatically updated => it may fail if the program is started (e.g. autostart) before an internet connection exists

- there is an option to reload a changed resolv.conf, but then it is probably not thread safe anymore

piola

  • Full Member
  • ***
  • Posts: 114
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: Easy way for resolve dns with Indy
« Reply #4 on: January 10, 2023, 04:49:14 pm »
Why do you want to use Indy for that?

Just fpc alone works like that:

 
Code: [Select]
uses netdb;
var
  temp: THostEntry;
begin
  ResolveHostByName('google.com', temp);
  writeln(temp.Addr.s_bytes[1],'.',temp.Addr.s_bytes[2],'.',temp.Addr.s_bytes[3],'.',temp.Addr.s_bytes[4]);
end.

I'm sorry for replying to this 10-year-old topic, but it's exactly my problem. I just want to do a simple DNS lookup and have been googling quite a lot now. I found dozens of suggestions to use fcl-net and I'd really like to do so. The example BeniBela has posted is all I want to do.

Bo no-one has ever mentioned that fcl-net is obviously restricted to *nix. It doesn't compile on Windows because of the use of BaseUnix in netdb.pas.

I'd really appreciate a similar example of how to do not more than an hostname-to-IP-address lookup on Windows 🙈

MarkMLl

  • Hero Member
  • *****
  • Posts: 6083
Re: Easy way for resolve dns with Indy
« Reply #5 on: January 10, 2023, 05:13:18 pm »
You might be better posting directly into the Windows-specific part of the forum.

Speaking as somebody who by and large doesn't use Indy etc., I can say that the fcl-net stuff does still work, and one possibility would be looking to see what makes it unix-specific: WinSock is actually fairly close to the de-facto standard Berkeley Sockets stuff... in fact my notes suggest that the Resolve unit might work with it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

loaded

  • Hero Member
  • *****
  • Posts: 757
Re: Easy way for resolve dns with Indy
« Reply #6 on: January 10, 2023, 06:01:31 pm »
This is how I use it in my applications.

Code: Pascal  [Select][+][-]
  1. uses resolve,StrUtils;
  2.  
  3. function GetHostByName(HostName:String):String;
  4. var
  5.   host:THostResolver;
  6. begin
  7.   host := THostResolver.Create(nil);
  8.   if host.NameLookup(HostName) then
  9.      result := host.AddressAsString
  10.    else
  11.      result := '';
  12.   host.Free;
  13. end;
  14.  
  15. procedure TForm1.Button1Click(Sender: TObject);
  16. var
  17.   host: string;
  18.   tip: TStringArray;
  19. begin
  20.  Host:='https://google.com/';
  21.  Host:=ReplaceStr(ReplaceStr(ReplaceStr(ReplaceStr(ReplaceStr(Host,'https',''),'http',''),'/',''),'\',''),':','');
  22.  tip:=GetHostByName(Host).Split('.');
  23.  showmessage(tip[0]+'.'+tip[1]+'.'+tip[2]+'.'+tip[3]);
  24. end;
  25.      

Also, I think you can do it directly with the ping command. But some more code may be required.
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

piola

  • Full Member
  • ***
  • Posts: 114
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: Easy way for resolve dns with Indy
« Reply #7 on: January 10, 2023, 06:11:04 pm »
This is working! Thank you!

 

TinyPortal © 2005-2018