LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 02-04-2005, 08:55 AM   #1
ro0tless
LQ Newbie
 
Registered: Feb 2005
Location: Tokyo Japan
Posts: 5

Rep: Reputation: 0
Question How to get ip address associated with an interface


Title says it all.

/sbin/ifconfig does it, but I want to do it in a C (or any other compiler) programs, without /sbin/ifconfig. I just want to get IPv4 address from an interface name (eth0,eth1, etc.) in a program to inform my dns service automatically (I hope you know what I mean )

TIA

Last edited by ro0tless; 02-04-2005 at 09:09 AM.
 
Old 02-04-2005, 11:32 AM   #2
ro0tless
LQ Newbie
 
Registered: Feb 2005
Location: Tokyo Japan
Posts: 5

Original Poster
Rep: Reputation: 0
Never mind. I got it.
 
Old 02-07-2005, 01:34 AM   #3
ramnpathy76
LQ Newbie
 
Registered: Feb 2005
Posts: 1

Rep: Reputation: 0
ro0tless, Could you update us with your findings...

Thanks
 
Old 02-07-2005, 03:59 AM   #4
ro0tless
LQ Newbie
 
Registered: Feb 2005
Location: Tokyo Japan
Posts: 5

Original Poster
Rep: Reputation: 0
I found it in a source file of a client of another dns service (www.no-ip.com). It's not directly associated with network, but with the "interface" as a device.

see this code....

struct ifreq ifr;
........(omitted)
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
.........(omitted)
strcpy(ifr.ifr_name, device);
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)

"struct ifreq" is defined in /usr/include/linux/if.h so it can hold ip address, netmask, etc, and interface name.

It's cool, isn't it? But I myself haven't made my own yet


Last edited by ro0tless; 02-07-2005 at 04:08 AM.
 
Old 10-03-2011, 01:18 PM   #5
John Lumby
LQ Newbie
 
Registered: Oct 2008
Posts: 11

Rep: Reputation: 0
ioctl to retrive interface IP address

In case anyone else reads this, ro0tless's post has an incorrect ioctl parameter -

where he says
ioctl(fd, SIOCGIFFLAGS, &ifr)
it should read

ioctl(fd, SIOCGIFADDR, &ifr)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add another IP address to an Interface hcclnoodles Linux - Software 1 08-19-2004 03:50 AM
URGENT: how to get an ip address of sending interface in C dravya Linux - General 4 05-25-2004 04:32 PM
Getting IP address from an interface in C dravya Programming 2 05-25-2004 03:48 PM
multiple ip address for single eth interface blackhound Linux - Networking 2 02-25-2004 07:02 AM
How can I retrieve the IP address of an interface? kobilevi Linux - Networking 2 05-15-2001 08:31 AM


All times are GMT -5. The time now is 11:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration