Home > Archive > Unix Programming > May 2005 > Why I can not get the host's ip with the following program
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Why I can not get the host's ip with the following program
|
|
| Zheng Da 2005-05-18, 4:00 pm |
| I run the following program in Linux, and want to get the host's ip,
but ip the program gets is only 127.0.0.2
I don't understand why it cannot work correctly
struct utsname myname;
if(uname(&myname) < 0)
return;
char host_buf[4096];
int my_err;
struct hostent host_ent;
struct hostent *hp = 0;
//here just search for IPv4
//if want to search for IPv6, use RES_USE_INET6 option or use
gethostbyname2
int ret=gethostbyname_r(myname.nodename , &host_ent , host_buf ,
4096 , &hp
, &my_err);
if (hp == NULL || ret != 0)
return;
if (hp->h_length > (int)sizeof(struct in_addr))
hp->h_length = sizeof(struct in_addr);
printf("%s\n" , inet_ntoa(*((struct in_addr
*)hp->h_addr_list[0])));
while(hp->h_addr_list[1]){
hp->h_addr_list++;
printf("%s\n" , inet_ntoa(*((struct in_addr
*)hp->h_addr_list[0])));
}
| |
| SM Ryan 2005-05-18, 4:00 pm |
| "Zheng Da" <zhengda1936@gmail.com> wrote:
# I run the following program in Linux, and want to get the host's ip,
# but ip the program gets is only 127.0.0.2
If you're trying to get your own IP address, one possibility is to parse the
output of ifconfig. On Darwin
@ ifconfig | grep 'inet '
inet 127.0.0.1 netmask 0xff000000
inet 192.168.2.1 netmask 0xffffff00 broadcast 192.168.2.255
inet 10.10.10.40 netmask 0xffffff00 broadcast 10.10.10.255
inet 198.144.214.72 --> 207.201.34.1 netmask 0xffffff00
If memory serves ifconfig is one of those commands that has different options
and output on each unix flavour.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
You face forward, or you face the possibility of shock and damage.
| |
| Floyd L. Davidson 2005-05-18, 4:00 pm |
| "Zheng Da" <zhengda1936@gmail.com> wrote:
>I run the following program in Linux, and want to get the host's ip,
That was *not* a program. It's a snippet of code which will not
compile without considerable additional code.
But other than being somewhat ugly... it works if you do add
the right code to get it to compile.
>but ip the program gets is only 127.0.0.2
What is in your /etc/hosts file? It looks like your hostname
is set to 127.0.0.2...
>I don't understand why it cannot work correctly
Here's a complete program based on your original code. Some of
the changes are a matter of style (indenting and such), while
other differences are more important.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/utsname.h>
int main(void)
{
char buf[BUFSIZ];
int err;
int ret;
struct hostent he;
struct hostent *hp = 0;
struct utsname ut;
if (uname(&ut) < 0) {
return EXIT_FAILURE;
}
if (*ut.nodename) {
printf("Node name: %s\n", ut.nodename);
}
ret = gethostbyname_r(ut.nodename, &he, buf, sizeof buf, &hp, &err);
if (hp == NULL || ret != 0) {
return EXIT_FAILURE;
}
if (hp->h_length > (int) sizeof (struct in_addr)) {
hp->h_length = sizeof (struct in_addr);
}
while (*hp->h_addr_list) {
printf(" %s\n", inet_ntoa(*((struct in_addr*)*hp->h_addr_list)));
hp->h_addr_list++;
}
return EXIT_SUCCESS;
}
--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
| |
| Zheng Da 2005-05-18, 4:00 pm |
| I can get a host's ip by ioctl().
But now I's surprised why the above program can not get the correct ip
of my computer.
| |
| Zheng Da 2005-05-19, 4:01 pm |
| I have tried your program, and get the same result.
My /etc/hosts is as follow:
127.0.0.1 localhost
# special IPv6 addresses
::1 localhost ipv6-localhost ipv6-loopback
fe00::0 ipv6-localnet
ff00::0 ipv6-mcastprefix
ff02::1 ipv6-allnodes
ff02::2 ipv6-allrouters
ff02::3 ipv6-allhosts
127.0.0.2 linux.site linux
There is really 127.0.0.2 in /etc/hosts.
And I change 127.0.0.2 to 10.171.97.240, and the program print
10.171.97.240
But it seems the above program can not get my own host's ip.
If uname can not get the right nodename, it will not work well.
And I am surprised where does uname get the system information.
| |
| Floyd L. Davidson 2005-05-19, 8:58 pm |
| "Zheng Da" <zhengda1936@gmail.com> wrote:
>I have tried your program, and get the same result.
>My /etc/hosts is as follow:
>127.0.0.1 localhost
># special IPv6 addresses
>::1 localhost ipv6-localhost ipv6-loopback
>fe00::0 ipv6-localnet
>ff00::0 ipv6-mcastprefix
>ff02::1 ipv6-allnodes
>ff02::2 ipv6-allrouters
>ff02::3 ipv6-allhosts
>127.0.0.2 linux.site linux
>There is really 127.0.0.2 in /etc/hosts.
>And I change 127.0.0.2 to 10.171.97.240, and the program print
>10.171.97.240
>But it seems the above program can not get my own host's ip.
But it *is* giving you exactly what you are asking for.
>If uname can not get the right nodename, it will not work well.
>And I am surprised where does uname get the system information.
From the command line, try these various commands:
hostname -f # the full name of your machine
hostname -i # the IP address associated with above name
uname -n # the node name your program is seeing
I'm guessing that your box was configured with the name "linux".
It was also configured to boot without a network, which is why
it assigns that name to the loopback device with the 127.0.0.2
IP address.
If you have an ethernet interface installed, you'll probably
want to change the above, and have the official name be the one
that is assigned to that inferface.
I'm not sure how or where it is done on your particular
distribution, but ultimately the command "hostname _name_" is
what does the trick. Commonly that name is saved to
/etc/HOSTNAME, so if you find such a file, just edit it and run
hostname $(cat /etc/HOSTNAME)
Otherwise, look through the installation documentation for your
distribution and find out how it sets the machines name.
--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@barrow.com
| |
| Zheng Da 2005-05-20, 3:59 pm |
| I know now.
Thank you
|
|
|
|
|