PDA

View Full Version : Get server's ip/hostname


twistedmind
03-23-2003, 03:02 PM
Hi all!
I have initialized the server's address with INADDR_ANY and need to retrieve the server's ip after that how do I retrieve the ip address? I have tried inet_ntoa() which returns 0.0.0.0. Could anyone help??

Thanks!

RobSeace
03-23-2003, 06:00 PM
Well, INADDR_ANY is 0.0.0.0, so that's what you should get...

If you want to get the IP in use for any connected socket (only after
you've accept()'d in a server, or connect()'d in a client), you can use
getsockname() on the connected socket...

If you want to get the main external IP for the server, you can do a
gethostname() (or uname()), then look that up with gethostbyname()
or whatever, to get the IP... Or, you can look through all configured
network interfaces, looking for the first up non-loopback one, and
extract the IP from that... But, realize, hosts can have multiple external
interfaces, and so can have multiple IPs, so who knows which you will
find in such a case... And, who knows which one you would WANT to
find, for that matter... It depends on what you're trying to do with it...

twistedmind
03-23-2003, 06:59 PM
Thank you! You have been most helpful!! :D

grf
04-23-2003, 03:52 PM
Hi,

I have the same problem and tried to solve this like your way:


return_value = gethostname(name_buffer,sizeof(name_buffer));

if (return_value == 0) { //got address

s_hostent = gethostbyname(name_buffer);

if (s_hostent != NULL) { //got name of server
for ( i = 0; s_hostent->h_addr_list[i] != 0; i++) {
in_server_address.s_addr = *s_hostent->h_addr_list[i];
printf("Server is running on %s...\n",inet_ntoa(in_server_address));
}

}


My computer is connected to the network and it got a IP via DHCP. /sbin/ifconfig shows me both IPs (loopback and the IP from DHCP).
However, if I try to read the list (*s_hostent->h_addr_list[i]), I get only one entry - the loopback address 127.0.0.0.

What can I do?

Michael.

RobSeace
04-23-2003, 09:00 PM
This sounds like another recent question (http://www.developerweb.net/forum/viewtopic.php?t=443)... And, there the
answer was that there was a bogus "/etc/hosts" entry,
which listed 127.0.0.1 for the hostname... Is that by any
chance the case here, too? Or, perhaps the hostname is
misconfigured to be "localhost", or something?

But, if you get your IP dynamically, chances are you aren't
going to be able to look it up like that, anyway, unless you
have a dynamic DNS entry for it somewhere, or something
like that... But, an alternative method of getting the local IP
is to just traverse through all configured local interfaces, via
obscure ioctl() calls... There are other (http://www.developerweb.net/forum/viewtopic.php?t=297) questions (http://www.developerweb.net/forum/viewtopic.php?t=161) here (and,
on the old site) which deal with that method in further depth...

grf
04-24-2003, 11:02 AM
Hi,

thank you for the fast help.

I looked into my "hosts" and found these two entrys:

127.0.0.1 localhost
127.0.0.2 realnameofmycomputer.domainname realnameofmycomputer

This second entry was written by the installation (SuSE 8.1). Do I really need this entry "127.0.0.2..." with the name of my computer?
With this configuration, I got only "127.0.0.2" from gethostbyname(). After I removed the second entry (127.0.0.2...), I get only the IP given by DHCP. This IP is, which I want to get from gethostbyname(). Now I'm happy. :)

Only for my information:
After reading again the manual of gethostbyname(), I found the information, that the default order for searching the IP is "nameserver->/etc/hosts". Ok, but if I have this second entry in "hosts", I expect a list like:

1. xxx.xxx.xxx.xxx (address given via DHCP)
2. 127.0.0.2

However, I get only the "127.0.0.2"... :?

Bye,

Michael.

PS: Sorry, but my English is not the best one...

RobSeace
04-24-2003, 12:56 PM
Yeah, you definitely don't want that "/etc/hosts" entry... I don't
know why, but it seems a lot of newer distros are starting to do
similarly stupid things on install... I just installed a new RedHat 9
system, and it also mapped the real hostname to 127.0.0.1 in
"/etc/hosts", even though I gave it a fixed real IP at install time... ;-/
Highly annoying... I don't know whose bright idea this tactic was,
but whoever it was should be smacked in the head... It's just
wrong, wrong, wrong...

As for search orders, that's usually defined in "/etc/nsswitch.conf",
on Linux systems... You should have a line in there probably like:
"hosts: files dns"... So, it always checks the "/etc/hosts" file before
trying DNS... And, if it finds it in "/etc/hosts", it doesn't bother doing
the DNS lookup at all, which is why you don't see the DNS IP...

grf
04-24-2003, 01:44 PM
Many thanks! Now the behavoir of this function is easier to understand.

Bye,

Michael.

mlampkin
04-24-2003, 11:17 PM
Only a guess... but I believe redhat is doing it because they do they determine if you can get automatic updates according to a system id which is set during install... the id consists of (among other things) the system address... by setting it to the loopback (which they started doing in version 8) always, they make it "simpler" in some ways to track (i.e. if you just re-install and keep the same host name but change the ip addr)...

That is only a guess... but sort makes sense since they have begun cutting back on how long you get auto updates without paying additional fees to them...

Michael

RobSeace
04-25-2003, 01:43 PM
Well, that's a pathetically stupid reason for it, then, if true... ;-)
It's just a horribly broken and wrong way of doing things... A
machine's true hostname should NEVER resolve to 127.0.0.1,
unless it truly has no real IP and no network interface other
than loopback... I still maintain whoever thought up doing it
should be beaten... ;-)