Recent

Author Topic: [solved (with different method)] host name resolution failed  (Read 10143 times)

billyb123

  • New Member
  • *
  • Posts: 26
[solved (with different method)] host name resolution failed
« on: February 18, 2017, 06:34:06 pm »
i set up fpc and lazarus on openbsd.
my simple program is this
Code: [Select]
{$mode objfpc}
program get_page;
uses
  fphttpclient, sysutils, strutils, classes;

const
  url = 'http://google.com';

var
  page: ansistring;

begin
  page := tfphttpclient.simpleget (url);
  writeln (page);
end.

when i compile it and run this is the output
Code: [Select]
An unhandled exception occurred at $000000000048cc01:
ESocketError: Host name resolution for "google.com" failed.

how to fix it? how to resolve host names in fpc?
« Last Edit: February 22, 2017, 07:59:16 pm by billyb123 »

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: host name resolution failed
« Reply #1 on: February 18, 2017, 06:43:51 pm »
I can confirm this error. Had to use libcurl for downloads on OpenBSD !

billyb123

  • New Member
  • *
  • Posts: 26
Re: host name resolution failed
« Reply #2 on: February 22, 2017, 06:04:53 am »
i used libcurl way back in c, never used it in pascal, i am having a hard time finding pascal documentation for libcurl, i only found this example on fpc git page:
Code: [Select]
{$mode objfpc}
{$h+}
program testcurl;
uses
  libcurl;
var
  url: pchar = 'http://www.freepascal.org';
  hcurl: pcurl;
begin
  hcurl := curl_easy_init;
  if assigned (hcurl) then
    begin
      curl_easy_setopt (hcurl, CURLOPT_VERBOSE, [true]);
      curl_easy_setopt (hcurl, CURLOPT_URL, [url]);
      curl_easy_perform (hcurl);
      curl_easy_cleanup (hcurl);
    end;
end.

i get gazillion errors with linking this thing:
/usr/local/lib//libintl.so.6.0: undefined reference to `tsearch'
/usr/local/lib//libcurl.so.25.5: undefined reference to `clock_gettime'... and so on..
it means linker cannot find object file to link with my program? in c i would add something like -lcurl to gcc and maybe add some include line for headers and libraries.. how to do the same in free pascal? i'm new to free pascal..

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: host name resolution failed
« Reply #3 on: February 22, 2017, 07:32:49 am »
I have an updated unit for libcurl here:
https://github.com/newpascal/fpcupdeluxe/blob/master/fpcuplibcurl.pas

And its implementation inside of:
https://github.com/newpascal/fpcupdeluxe/blob/master/fpcuputil.pas

Perhaps this can help you.

Edit: code.
Code: Pascal  [Select][+][-]
  1. function DoWrite(Ptr : Pointer; Size : size_t; nmemb: size_t; Data : Pointer) : size_t;cdecl;
  2. begin
  3.   if Data=nil then result:=0 else
  4.   begin
  5.     result:=TStream(Data).Write(Ptr^,Size*nmemb);
  6.   end;
  7. end;
  8. function TUseWGetDownloader.LibCurlDownload(Const URL : String; Dest : TStream):boolean;
  9. var
  10.   hCurl : pCurl;
  11.   res: CURLcode;
  12.   UserPass:string;
  13. begin
  14.   result:=false;
  15.  
  16.   if LoadCurlLibrary then
  17.   begin
  18.  
  19.     curl_global_init(CURL_GLOBAL_ALL);
  20.  
  21.     try
  22.       hCurl:= curl_easy_init();
  23.       if Assigned(hCurl) then
  24.       begin
  25.  
  26.         res:=CURLE_OK;
  27.  
  28.         UserPass:='';
  29.         if FUsername <> '' then
  30.         begin
  31.           UserPass:=FUsername+':'+FPassword;
  32.         end
  33.         else
  34.         begin
  35.           if Pos('ftp.freepascal.org',URL)>0 then UserPass:='anonymous:fpc@example.com';
  36.         end;
  37.         if Length(UserPass)>0 then if res=CURLE_OK then res:=curl_easy_setopt(hCurl, CURLOPT_USERPWD, pointer(UserPass));
  38.  
  39.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl,CURLOPT_TCP_KEEPALIVE,1);
  40.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl, CURLOPT_FOLLOWLOCATION, 1);
  41.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl,CURLOPT_MAXREDIRS,50);
  42.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl, CURLOPT_NOPROGRESS,1);
  43.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
  44.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl,CURLOPT_URL,PChar(URL));
  45.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl,CURLOPT_WRITEFUNCTION,@DoWrite);
  46.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl,CURLOPT_WRITEDATA,Pointer(Dest));
  47.         if res=CURLE_OK then res:=curl_easy_setopt(hCurl,CURLOPT_USERAGENT,PChar(CURLUSERAGENT));
  48.  
  49.         if res=CURLE_OK then res := curl_easy_perform(hCurl);
  50.  
  51.         result:=((res=CURLE_OK) AND (Dest.Size>0));
  52.  
  53.         curl_easy_cleanup(hCurl);
  54.       end;
  55.     except
  56.       // swallow libcurl exceptions
  57.     end;
  58.   end;
  59. end;
« Last Edit: February 22, 2017, 07:48:22 am by DonAlfredo »

billyb123

  • New Member
  • *
  • Posts: 26
Re: host name resolution failed
« Reply #4 on: February 22, 2017, 09:06:54 am »
Thanks, will try it out.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: host name resolution failed
« Reply #5 on: February 22, 2017, 09:11:45 am »
This is probably caused by the fact that you have not installed the -dev libraries for curl.
But Alfred's answer works. Just verified that.

Note the curl headers from FPC are in dire need of an update.
« Last Edit: February 22, 2017, 09:19:01 am by Thaddy »
Specialize a type, not a var.

billyb123

  • New Member
  • *
  • Posts: 26
Re: host name resolution failed
« Reply #6 on: February 22, 2017, 07:58:51 pm »
@donalfredo -- i just tried your unit, it is working like a charm. thanks again

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: [solved (with different method)] host name resolution failed
« Reply #7 on: February 23, 2017, 09:52:26 am »
Hi there,

Pardon me if I'm coming across a bit harsh:

Why are you using either such a basic component or adding a dependency on an external lib?

My suggestion would be to use the Synapse network lib.

It's implemented in 100% Object Pascal.

It's easy to use and has all kind of goodies for internet services. Can even deal with SSL.

And if you don't like that one, there's also LNet.

AND, if that one doesn't tickle your fancy, there's also the Indy that has a port to Lazarus, somewhere...

The easiest way to get Indy, LNet and Synapse is to install the Online Package Manager package that should already come with your Lazarus install. Have a look under menu "Package"->"Install/Uninstall Packages..."

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: [solved (with different method)] host name resolution failed
« Reply #8 on: February 23, 2017, 10:42:51 am »
@gcarreno
I cannot speak for other, but fpcupdeluxe must work in many, very different circumstances.
Under some of these conditions, Synapse does not work (fpcupdeluxe uses Synapse in many places).
Therefor, a fallback is available. In this case, libcurl is used. If that fails, wget is tried.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1111
  • Professional amateur ;-P
Re: [solved (with different method)] host name resolution failed
« Reply #9 on: February 24, 2017, 06:18:40 am »
@gcarreno
I cannot speak for other, but fpcupdeluxe must work in many, very different circumstances.
Under some of these conditions, Synapse does not work (fpcupdeluxe uses Synapse in many places).
Therefor, a fallback is available. In this case, libcurl is used. If that fails, wget is tried.

Well, in case of fpcupdeluxe, it's quite understandable. You are in the harsh environment that is multi-platforms.

But, unless I missed something, the OP wanted something simple. And, while I admit it's quite good, curl and it's API is not the simplest nor the Object Pascalest of the options in the wild.

But, said that, my intention was to give the OP more options, not school him on the "only way is my way".

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

billyb123

  • New Member
  • *
  • Posts: 26
Re: [solved (with different method)] host name resolution failed
« Reply #10 on: February 27, 2017, 01:42:56 am »
Hi there,

Pardon me if I'm coming across a bit harsh:

Why are you using either such a basic component or adding a dependency on an external lib?

My suggestion would be to use the Synapse network lib.

https in synapse doesnt work in openbsd. thats why.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: [solved (with different method)] host name resolution failed
« Reply #11 on: February 27, 2017, 04:13:02 am »
Maybe changing the $define usenetdb in fcl-net/src/resolve.pp to   "define netdb" and recompiling everything (beware of already installed .ppu's!) would help.

Possible cause: wrong unix domain socket structures in sockets.pp for openbsd

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: [solved (with different method)] host name resolution failed
« Reply #12 on: February 27, 2017, 11:58:01 am »
Possible cause: wrong unix domain socket structures in sockets.pp for openbsd
I think the structures are ok, but some of the const differ... Try $20000 instead of $4000 (doesn't tell you too much, does it..).
This is on top of my head, so you have the search the code why I think of that.
I will do it for you later but I usually know these things and I am almost sure that it is correct. Grep the source. (Windows and linux against BSD's)
It is part of sockets....
« Last Edit: February 27, 2017, 12:03:11 pm by Thaddy »
Specialize a type, not a var.

billyb123

  • New Member
  • *
  • Posts: 26
Re: [solved (with different method)] host name resolution failed
« Reply #13 on: February 27, 2017, 03:08:13 pm »
Possible cause: wrong unix domain socket structures in sockets.pp for openbsd
I think the structures are ok, but some of the const differ... Try $20000 instead of $4000 (doesn't tell you too much, does it..).

in ssfpc.inc file from synapse source there is MSG_NOSIGNAL const that is set to $20000. but synapse still doesnt work for me in openbsd.
i installed fpc in openbsd with "pkg_add fpc" and so i dont have the fpc source on my system, but i downloaded fpc 3.0.2 source code to try thaddy's suggestion of editing fcl-net/src/resolve.pp but i am not sure how to compile fpc from source.. "make" gives me (literally) zillion errors "parse error: need an operator in ...", google is not helping..
aha.. its gmake instead of make -- but still it gives me "error: path ./ does not exist"... googling to find solution to that..
« Last Edit: February 27, 2017, 03:16:34 pm by billyb123 »

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: [solved (with different method)] host name resolution failed
« Reply #14 on: June 08, 2017, 03:19:57 pm »
As a sidenote.
The latest (very fresh) release of fpcupdeluxe contains binaries for OpenBSD.
So, you could try to install (trunk) and experiment.

i386:
https://github.com/newpascal/fpcupdeluxe/releases/download/1.4.0h/fpcupdeluxe-i386-openbsd

x86_64:
https://github.com/newpascal/fpcupdeluxe/releases/download/1.4.0h/fpcupdeluxe-x86_64-openbsd

Do not forget: you have to patch FPC to compile and work on OpenBSD !
Use the supplied patches and setup fpcupdeluxe (use setup+) to auto-apply them.
https://github.com/newpascal/fpcupdeluxe/tree/master/patchfpc

And have subversion and wget installed !

 

TinyPortal © 2005-2018