Ural Mutlu <(E-Mail Removed)> 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...