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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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... |
|
|||
|
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 |