You are not logged in.
Pages: 1
Hi,
I'm trying to get my local LAN address (as in, usually something like 192.168.1.x), but I can't find any way to do it.
I googled and found pages which said that it's supposed to work if you use gethostname() to get your hostname, then using gethostbyname() and looking in h_addr_list, which should contain your IP(s).
However, for me, h_addr_list always has only two elements, and both are "127.0.0.1", which is of course absolutely useless.
By the way, I need to this to be cross platform (at least linux and win32), so parsing ifconfig output and such is out of the question.
Thanks a lot for any pointers
P.S.
Here is code I found on some forum, it's supposed to work, but it doesn't. Look below for output it gives me.
#include <sys/unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
int main()
{
char hostname[128];
int i;
struct hostent *he;
struct in_addr **addr_list;
struct in_addr addr;
gethostname(hostname, sizeof hostname);
printf("My hostname: %s\n", hostname);
he = gethostbyname(hostname);
if (he == NULL) { // do some error checking
herror("gethostbyname"); // herror(), NOT perror()
return 1;
}
// print information about this host:
printf("Official name is: %s\n", he->h_name);
printf("IP address: %s\n", inet_ntoa(*(struct in_addr*)he->h_addr));
printf("All addresses: ");
addr_list = (struct in_addr **)he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++) {
printf("%s ", inet_ntoa(*addr_list[i]));
}
printf("\n");
return 0;
}
$ gcc iptest.c
$ ./a.out
My hostname: aaone
Official name is: aaone.localdomain
IP address: 127.0.0.1
All addresses: 127.0.0.1 127.0.0.1
Offline
I test your code in other computer and i get the correct output:
My hostname: leto
Official name is: leto....
IP address: 159.90.10.xx
All addresses: 159.90.10.xx
Offline
Interesting; the problem must then definitively be on my end. Does it matter if I'm using WiFi? I assumed not, since the hostname is always the same, it should give all interfaces' IPs, but maybe that could be the problem?
Offline
If you haven't read it already: http://beej.us/guide/bgnet/output/html/ … index.html
Offline
The wifi isn't the problem.
Maybe a problem with configuration or something similar.
Offline
@tomd123:
Part of it, which said that what I was doing in the beginning that I read in manpages is correct.
Moreover, as it worked for n0dix, it appears that I'm doing it right, but for some reason it doesn't work on my machine?
Any thoughts?
If it helps, I use wicd, and here is the ifconfig output (I want to make a program that would find the 192.168.2.2 in wlan0)
~$ ifconfig
eth0 Link encap:Ethernet HWaddr SOMETHING
[...]
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
[...]
wlan0 Link encap:Ethernet HWaddr SOMETHING
inet addr:192.168.2.2 Bcast:192.168.2.255 Mask:255.255.255.0
[...]
Offline
Comment #2: link
Offline
Well yes, it should go through /etc/hosts, but how come it works for you and not for me? In my /etc/hosts I have nothing special, only localhost and aaone both pointing to 127.0.0.1, but that (unless you changed it) or something similar should be in your setup as well (so why doesn't it work here?), and router pointing to my router's IP for convenience.
Also, in my case it's useless if "real URLs and IP addresses" work, because I only need to get the local IP address.
Offline
No wait. It works in a machine of my University.
In my local machine didn't work.
I looked for special configuration in /etc/hosts and /etc/resolv.conf but i didn't find anything relevant.
Offline
Hmm. Now I tried to compile & run it on a ubuntu machine, which insisted that inet_ntoa returns an int and not a char *, and therefore happily segfaults.
This way seems to be quite unreliable, is there any other method?
Offline
The reality is inet_ntoa is deprecated because not support Ipv6, however still use for compatibility. The function Ipv6 is inet_ntop:
const char *
inet_ntop(int af, const void * restrict src, char * restrict dst, socklen_t size);
Look the arguments change.
Btw, i don't understand by in Ubuntu doesn't work. I try in Arch Linux and Debian without issue.
Offline
Pages: 1