How to get my own IP address without DNS

K

Kazu

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
 
J

Jacques Labuschagne

Kazu said:
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)

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.
 
P

phil_gg04

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.
 
J

Jose Maria Lopez Hernandez

Kazu said:
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)

The easiest way is to parse the /etc/hosts file, that your system
should use if there's no DNS server.

Regards.

--

Jose Maria Lopez Hernandez
Director Tecnico de bgSEC
(e-mail address removed)
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

M Kiran Kumar

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
 
R

Raqueeb Hassan

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

<snip>

Pretty clever!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,074
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top