You are not logged in.

#1 2010-03-19 01:55:00

y27
Member
Registered: 2009-05-27
Posts: 147
Website

Getting LAN IP address in C

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 smile


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

#2 2010-03-19 02:04:33

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: Getting LAN IP address in C

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

#3 2010-03-19 02:07:42

y27
Member
Registered: 2009-05-27
Posts: 147
Website

Re: Getting LAN IP address in C

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

#4 2010-03-19 02:20:28

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: Getting LAN IP address in C

Offline

#5 2010-03-19 03:15:50

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: Getting LAN IP address in C

The wifi isn't the problem.
Maybe a problem with configuration or something similar.

Offline

#6 2010-03-20 01:18:14

y27
Member
Registered: 2009-05-27
Posts: 147
Website

Re: Getting LAN IP address in C

@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

#7 2010-03-20 01:26:41

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: Getting LAN IP address in C

Comment #2: link

Offline

#8 2010-03-20 01:45:52

y27
Member
Registered: 2009-05-27
Posts: 147
Website

Re: Getting LAN IP address in C

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

#9 2010-03-20 02:13:23

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: Getting LAN IP address in C

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

#10 2010-03-20 15:51:17

y27
Member
Registered: 2009-05-27
Posts: 147
Website

Re: Getting LAN IP address in C

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

#11 2010-03-20 16:17:08

n0dix
Member
Registered: 2009-09-22
Posts: 956

Re: Getting LAN IP address in C

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

Board footer

Powered by FluxBB