Networking Forums

Networking Forums > Computer Networking > Linux Networking > How to get my own IP address without DNS

Reply
Thread Tools Display Modes

How to get my own IP address without DNS

 
 
Kazu
Guest
Posts: n/a

 
      05-31-2005, 10:25 AM
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
 
Reply With Quote
 
 
 
 
Jacques Labuschagne
Guest
Posts: n/a

 
      05-31-2005, 11:55 AM
Kazu wrote:
> 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.
 
Reply With Quote
 
phil_gg04@treefic.com
Guest
Posts: n/a

 
      05-31-2005, 12:09 PM
> 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.

 
Reply With Quote
 
Jose Maria Lopez Hernandez
Guest
Posts: n/a

 
      06-01-2005, 10:05 AM
Kazu wrote:
> 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.

> Kazuh


Regards.

--

Jose Maria Lopez Hernandez
Director Tecnico de bgSEC
(E-Mail 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"
 
Reply With Quote
 
M Kiran Kumar
Guest
Posts: n/a

 
      06-02-2005, 12:23 PM
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

 
Reply With Quote
 
Raqueeb Hassan
Guest
Posts: n/a

 
      06-02-2005, 02:07 PM
> 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!


--
Raqueeb Hassan
Bangladesh

 
Reply With Quote
 
Tauno Voipio
Guest
Posts: n/a

 
      06-04-2005, 12:11 PM
Raqueeb Hassan wrote:
>>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!
>


A deeper treatment is in:

W. Richard Stevens, UNIX Network Programming

--

Tauno Voipio
tauno voipio (at) iki fi

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipex Address: Post Office wont sent registered letter as they say the address is invalid! W Broadband 16 05-13-2008 09:30 PM
Coffee shop wifi will not give me IP address, but gives everyone else IP address. But static IP works! Jennifer R Wireless Internet 5 09-30-2007 06:12 AM
access violation at address 004075e5 module WUSB54G4.exe of address 00000368 Houndog Wireless Internet 10 02-13-2006 06:56 PM
"access violation at address 004075e5 module WUSB54G4.exe of address 00000368 Houndog Windows Networking 0 02-12-2006 09:40 PM
When Linux PC boots, Does it sends RARP packet to get its IP address by embedding its Hardware address? santa19992000@yahoo.com Linux Networking 2 10-16-2005 10:40 PM



1 2 3 4 5 6 7 8 9 10 11