Date: Tue, 1 May 2012 07:48:12 -0700 (PDT) From: Unga <unga888@yahoo.com> To: Matthew Seaman <matthew@FreeBSD.org> Cc: "freebsd-questions@freebsd.org" <freebsd-questions@FreeBSD.org> Subject: Re: localhost not recognised in getaddrinfo(3) in FreeBSD 9.0-STABLE Message-ID: <1335883692.40276.YahooMailNeo@web160104.mail.bf1.yahoo.com> In-Reply-To: <4F9FC2AD.30905@FreeBSD.org> References: <1335866914.5649.YahooMailNeo@web160106.mail.bf1.yahoo.com> <4F9FC2AD.30905@FreeBSD.org>
next in thread | previous in thread | raw e-mail | index | archive | help
> From: Matthew Seaman <matthew@FreeBSD.org>
> To: Unga <unga888@yahoo.com>
> Cc: "freebsd-questions@freebsd.org" <freebsd-questions@FreeBSD.org>
> Sent: Tuesday, May 1, 2012 11:02 AM
> Subject: Re: localhost not recognised in getaddrinfo(3) in FreeBSD 9.0-STABLE
>
> On 01/05/2012 11:08, Unga wrote:
>> Following code fragment works in FreeBSD 8.1-RELEASE, but not in FreeBSD
> 9.0-STABLE:
>>
>> error = getaddrinfo("localhost", port, &hints, &res0);
>> if (error)
>> {
>> fprintf(stderr,"getaddrinfo failed - %s\n",
> gai_strerror(error));
>> exit(1);
>> }
>>
>> It complains: getaddrinfo failed - hostname nor servname provided, or not
> known
>>
>> Any idea why?
>>
>
> So, what is the variable 'port' initialized to? It should be a const
> char* with the name of a network service found in /etc/services or else
> the string representation of a port number in decimal.
>
#define MYPORT 4321
char port[10];
snprintf(port, sizeof(port), "%d", MYPORT);
error = getaddrinfo("localhost", port, &hints, &res0);
Pls note, using 127.0.0.1 instead of localhost works. But I prefer to use localhost.
> Failing that, this is almost certainly a configuration snafu on your
> 9.0-STABLE box.
>
> Does this machine have an entry for localhost in /etc/hosts ? Can it
> resolve localhost via the DNS? Or through any other means such as NIS or
> LDAP?
>
> What does:
>
> % getent hosts localhost
>
> return?
>
$ getent hosts localhost
127.0.0.1 localhost localhost.my.domain ftp.sam511.lan
> If that fails, sanity check /etc/nsswitch.conf and /etc/resolv.conf
>
cat /etc/nsswitch.conf
group: compat
group_compat: nis
hosts: files dns
networks: files
passwd: compat
passwd_compat: nis
shells: files
services: compat
services_compat: nis
protocols: files
rpc: files
cat /etc/resolv.conf
nameserver 192.168.1.1
Regards
Unga
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1335883692.40276.YahooMailNeo>
