473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to get my own IP address without DNS

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
Jul 23 '05 #1
6 12957
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.
Jul 23 '05 #2
> 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.

Jul 23 '05 #3
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
jk******@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"
Jul 23 '05 #4
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

Jul 23 '05 #5
> 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

Jul 23 '05 #6
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

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: crypto_stonelock | last post by:
Hello, I was wondering if anybody knew how to retrieve the IP address attributed dynamically by your ISP without having to use the info collected on a socket with say .getLocalAddress. I'm...
3
by: serge calderara | last post by:
Dear all, I have a tool which is able to receive any kind of data from a remote system. For that I need to get the ipadress of the remote system. If the system is not connected to a DHCP...
12
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone...
8
by: pcchong | last post by:
what is the simplest way to put the email address on the homepage so that it is not readable by email extractor or search engine? I remember someone wrote a simple script to do that, but I can't...
117
by: Steevo | last post by:
Any suggestions as to the best programs for cloaking email addresses? Many thanks -- Steevo
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
7
by: econobeing | last post by:
a guy in my class made a program in VB6 where he could use the internet because he used the IP address of a site instead of the DNS address. i want to make my own browser now that can do that,...
3
by: elvira_wang | last post by:
heya, what sort of address is displayed when this instruction for instance is executed printf("myvar location is 0x%lx\n", (long) &myvar); is it logical address or linear address, i.e. with...
20
by: sethukr | last post by:
hi, i need to store a value to a particular memory location without having a variable. So, how can i access a 'memory address' without using variables?? Is it possible in C??? Plz, help...
36
by: Ajay | last post by:
Hi All, I got a segmentation fault while accessing a structure element. I pasted the output from gdb and the backtrace. However if I am able successfully access the structure using the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.