gethostname and gethostbyname2

This is a discussion on gethostname and gethostbyname2 within the Linux Networking forums, part of the Linux Forums category; Hi, I have a source code that calls gethostname() and at another point the hostname is an input parameter to ...


Go Back   Usenet Forums > Linux Forums > Linux Networking

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-01-2006
Ural Mutlu
 
Posts: n/a
Default gethostname and gethostbyname2

Hi,

I have a source code that calls gethostname() and at another point the
hostname is an input parameter to gethostbyname2() to get the IP address.

For IPv4 it gives me 127.0.0.1, which is fine, but when I convert the
program to run on IPv6 I don't get a hostent, ie, NULL pointer is returned.

my source code in a simple form is:

r= gethostname(temp, 200);
hent = gethostbyname2(temp, AF_INET6);
if(hent)
//do something
else
//NULL pointer returned

I know I am supposed to be using getaddrinfo() instead of gethostbyname2()
but that will require too many changes in the code.

At the moment my hostname is pure text, eg My_Linux_Machine, and I think
that's why I get the address of the loopback interface only, but that does
not explain the lack of ::1 to me.

My questions are:
1. How exactly does the gethostbyname resolver work?
2. What shall I configure to make it return information on the other
interfaces.
3. Would I get ethX IP addr if I set hostname to be in the name.domain
format, where domain is the network ethX is connected to?

Finally my system:
OS: FC5
kernel: 2.6.16

Regards,
ural
Reply With Quote
  #2 (permalink)  
Old 08-01-2006
Rick Jones
 
Posts: n/a
Default Re: gethostname and gethostbyname2

Ural Mutlu <uralmutlu@gmail.com> wrote:
> I have a source code that calls gethostname() and at another point
> the hostname is an input parameter to gethostbyname2() to get the IP
> address.


> For IPv4 it gives me 127.0.0.1, which is fine, but when I convert
> the program to run on IPv6 I don't get a hostent, ie, NULL pointer
> is returned.


> my source code in a simple form is:


> r= gethostname(temp, 200);
> hent = gethostbyname2(temp, AF_INET6);
> if(hent)
> //do something
> else
> //NULL pointer returned


> I know I am supposed to be using getaddrinfo() instead of
> gethostbyname2() but that will require too many changes in the code.


Better now than later - later there may not even be a gethostbyname2()
call available. And you are unlikely to find many folks willing to
help fix an application that is knowingly using the wrong
interface(s).

> At the moment my hostname is pure text, eg My_Linux_Machine, and I


I was under the impression that underscores '_' were specifically not
allowed in domain names _years_ ago. I could easily see getaddrinfo
or even a gethostbyname2() call enforcing that restriction more
strictly than gethostbyname().

> think that's why I get the address of the loopback interface only,
> but that does not explain the lack of ::1 to me.


> My questions are:
> 1. How exactly does the gethostbyname resolver work?


It takes the name you provide, and looks it up in any number of
"dictionaries"a ranging from /etc/hosts to NIS to DNS servers,
depending on the contents of a file called /etc/nsswitch.conf

> 2. What shall I configure to make it return information on the other
> interfaces.


Nothing. Gethostbyname et al and the IP's of your interfaces are
unrelated. Or at least only indirectly related.

> 3. Would I get ethX IP addr if I set hostname to be in the
> name.domain format, where domain is the network ethX is connected
> to?


Strictly speaking, there is _no_ correlation between the IP addresses
on your ethN interfaces and the IP addresses one gets back from a
gethostbyname or getaddrinfo call. The gethostbyname/getaddrinfo
calls don't particually care what IPs are assigned to your interfaces,
only what mappings exist in the "dictionaries" from the name you
provide to an IP address.

Also, strictly speaking, an ethN interface is not connected to a
domain. It is connected to an IP subnet. There _may_ be a mapping of
a hostname in a domain to that IP address.

rick jones
--
a wide gulf separates "what if" from "if only"
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
Reply With Quote
  #3 (permalink)  
Old 08-01-2006
Ural Mutlu
 
Posts: n/a
Default Re: gethostname and gethostbyname2

morning,

>> I have a source code that calls gethostname() and at another point
>> the hostname is an input parameter to gethostbyname2() to get the IP
>> address.

>
>> For IPv4 it gives me 127.0.0.1, which is fine, but when I convert
>> the program to run on IPv6 I don't get a hostent, ie, NULL pointer
>> is returned.

>
>> my source code in a simple form is:

>
>> r= gethostname(temp, 200);
>> hent = gethostbyname2(temp, AF_INET6);
>> if(hent)
>> //do something
>> else
>> //NULL pointer returned


>> At the moment my hostname is pure text, eg My_Linux_Machine, and I

>
> I was under the impression that underscores '_' were specifically not
> allowed in domain names _years_ ago. I could easily see getaddrinfo
> or even a gethostbyname2() call enforcing that restriction more
> strictly than gethostbyname().


I dont know about underscores, my hostname doesn't have underscore in it.
My_Linux_Machine was just an example, I'll bear your advice in mind.

>> think that's why I get the address of the loopback interface only,
>> but that does not explain the lack of ::1 to me.

>
>> My questions are:
>> 1. How exactly does the gethostbyname resolver work?

>
> It takes the name you provide, and looks it up in any number of
> "dictionaries"a ranging from /etc/hosts to NIS to DNS servers,
> depending on the contents of a file called /etc/nsswitch.conf
>


in /etc/hosts I have 127.0.0.1 Hostname only.

I presume that's why I get 127.0.0.1 in the hostent returned by
gethostbyname, and I also presume, that's the reason why gethostbyname2()
returns a NULL pointer for IPv6 as there is no hostname-address mapping
for IPv6.

thanks
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:37 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0