Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get my own IP address without DNS

Kazu
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi.

I am trying to get my own IP address. I know that there is a way to use
gethostname and gethostbyname, but this has a problem.
1. I cannot use this if no DNS is specified.
2. If several IP addresses are given for my domain name and
the ordering is round robin, I cannot tell which one is my real IP address.

Is there any way to get my own IP address in C or C++ without using DNS?
(such as ifconfig)

Kazuh

Jacques Labuschagne
Guest
 
Posts: n/a
#2: Jul 23 '05

re: How to get my own IP address without DNS


Kazu wrote:[color=blue]
> Hi.
>
> I am trying to get my own IP address. I know that there is a way to use
> gethostname and gethostbyname, but this has a problem.
> 1. I cannot use this if no DNS is specified.
> 2. If several IP addresses are given for my domain name and
> the ordering is round robin, I cannot tell which one is my real IP address.
>
> Is there any way to get my own IP address in C or C++ without using DNS?
> (such as ifconfig)[/color]

This question is off topic for comp.lang.c++.

But as a hint: Yes, it's possible. Get a list of the interfaces and
their addresses. Java does this with
NetworkInterface.getNetworkInterfaces(). The C if_nameindex() function
gets you your interface name, but it's not obvious to me how you get
from there to an if_addr structure. See net/if.h.

Jacques.
phil_gg04@treefic.com
Guest
 
Posts: n/a
#3: Jul 23 '05

re: How to get my own IP address without DNS


> I am trying to get my own IP address.

What platform? I'll assume Unix. comp.unix.programmer might be a
better place to ask.

Try "strace /sbin/ifconfig" (or read the source) to see how it does it.
I think it will read from /proc on Linux.
It might be hard to do this in a portable way. It could be better to
popen("ifconfig") and parse its output.

--Phil.

Jose Maria Lopez Hernandez
Guest
 
Posts: n/a
#4: Jul 23 '05

re: How to get my own IP address without DNS


Kazu wrote:[color=blue]
> Hi.
>
> I am trying to get my own IP address. I know that there is a way to use
> gethostname and gethostbyname, but this has a problem.
> 1. I cannot use this if no DNS is specified.
> 2. If several IP addresses are given for my domain name and
> the ordering is round robin, I cannot tell which one is my real IP address.
>
> Is there any way to get my own IP address in C or C++ without using DNS?
> (such as ifconfig)[/color]

The easiest way is to parse the /etc/hosts file, that your system
should use if there's no DNS server.
[color=blue]
> Kazuh[/color]

Regards.

--

Jose Maria Lopez Hernandez
Director Tecnico de bgSEC
jkerouac@bgsec.com
bgSEC Seguridad y Consultoria de Sistemas
http://www.bgsec.com
ESPAŅA

The only people for me are the mad ones -- the ones who are mad to live,
mad to talk, mad to be saved, desirous of everything at the same time,
the ones who never yawn or say a commonplace thing, but burn, burn, burn
like fabulous yellow Roman candles.
-- Jack Kerouac, "On the Road"
M Kiran Kumar
Guest
 
Posts: n/a
#5: Jul 23 '05

re: How to get my own IP address without DNS


you can use system call ioctl with SIOCGIFADDR as shown below

1) struct ifreq ifr; struct sockaddr_in saddr;
2) fd=socket(PF_INET,SOCK_STREAM,0)
3) strcpy(ifr.ifr_name,"name of interface");
4) ioctl(fd,SIOCGIFADDR,&ifr);
5) saddr=*((struct sockaddr_in *)(&(ifr.ifr_addr))); /* is the address
*/
6) saddr.sin_addr.s_addr is the address of the interface in integer
format

Raqueeb Hassan
Guest
 
Posts: n/a
#6: Jul 23 '05

re: How to get my own IP address without DNS


> 1) struct ifreq ifr; struct sockaddr_in saddr;[color=blue]
> 2) fd=socket(PF_INET,SOCK_STREAM,*0)
> 3) strcpy(ifr.ifr_name,"name of interface");
> 4) ioctl(fd,SIOCGIFADDR,&ifr);
> 5) saddr=*((struct sockaddr_in *)(&(ifr.ifr_addr))); /* is the address[/color]

<snip>

Pretty clever!


--
Raqueeb Hassan
Bangladesh

Tauno Voipio
Guest
 
Posts: n/a
#7: Jul 23 '05

re: How to get my own IP address without DNS


Raqueeb Hassan wrote:[color=blue][color=green]
>>1) struct ifreq ifr; struct sockaddr_in saddr;
>>2) fd=socket(PF_INET,SOCK_STREAM,*0)
>>3) strcpy(ifr.ifr_name,"name of interface");
>>4) ioctl(fd,SIOCGIFADDR,&ifr);
>>5) saddr=*((struct sockaddr_in *)(&(ifr.ifr_addr))); /* is the address[/color]
>
>
> <snip>
>
> Pretty clever!
>[/color]

A deeper treatment is in:

W. Richard Stevens, UNIX Network Programming

--

Tauno Voipio
tauno voipio (at) iki fi

Closed Thread