🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.linux.development.apps
Article #22

Re: Getting eth0 device information in C or perl.

#22
From: michael scott frank
Date: Tue, 05 Dec 2000 00:00
69 lines
3208 bytes
If you want to do it in C, you use the ioctl SIOCGIFADDR.  Here's a small
program i wrote a while ago, modified a bit for what you need.  Its a bit
of work to do it the 'real' way, as you can see.  the ifreq struct is
particularily nasty, if you look at how it is defined in the headers.  If
you are interested in this stuff like i am tho, you should definitely pick
up "UNIX network programming" by W. Richard Stevens.  it is _the_ book on
the subject, and has an excellent chapter on network related ioctls.

#include <stdio.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

int main(void) {
	int fd;
	struct sockaddr_in *addr_ptr;
	struct ifreq interface;
	char IP[INET_ADDRSTRLEN];

	/* tells ioctl which interface to query */
	strcpy(interface.ifr_name, "eth0");	

	/* need a socket to use ioctl */
	fd = socket(AF_INET, SOCK_DGRAM, 0);

	/* get interface ip address */
	if (ioctl(fd, SIOCGIFADDR, &interface) < 0) {
		perror("ERROR (ioctl (SIOCGIFADDR))");
		exit(1);
	}

	/* kinda ugly, but this allows us to use inet_ntop */
	addr_ptr = (struct sockaddr_in *) &interface.ifr_addr;

	/* INET protocol family */
	addr_ptr->sin_family = AF_INET;	

	/* copy ip address */
	inet_ntop(AF_INET, &addr_ptr->sin_addr, IP, INET_ADDRSTRLEN);	

	/* print it */
	printf("IP of eth0: %s\n", IP);
	exit(0);
}




-------------------------  .~.
Michael Frank              /v\
msfrank@u.washington.edu  // \\
------------------------ /(   )\
                          ^`~'^

On Wed, 6 Dec 2000, Joel Pobar wrote:

> Hey all,
> 
> I need to get the IP address from eth0 in a C or Perl program. I was
> wondering if there are any functions that can be called to obtain this
> information.
> 
> Thanks in advance.
> Regards,
> Joel Pobar.
> 
> 
> 
Technical Details
Message-ID: <Pine.A41.4.21.0012052304150.112374-100000@dante11.u.washington.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <FjhX5.92$UI4.5375@nsw.nnrp.telstra.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f5392,ea95fbf156eacc4e,start
X-Google-Attributes: gidf5392,public
X-Google-Thread: 10c696,ea95fbf156eacc4e,start
X-Google-Attributes: gid10c696,public
X-Google-Thread: 10cce4,ea95fbf156eacc4e,start
X-Google-Attributes: gid10cce4,public
X-Google-Thread: 10aedf,ea95fbf156eacc4e,start
X-Google-Attributes: gid10aedf,public
X-Google-Thread: 1078db,ea95fbf156eacc4e,start
X-Google-Attributes: gid1078db,public
X-Google-Thread: 10669a,ea95fbf156eacc4e,start
X-Google-Attributes: gid10669a,public
X-Google-Thread: 102983,ea95fbf156eacc4e,start
X-Google-Attributes: gid102983,public
From: michael scott frank <msfrank@u.washington.edu>
Subject: Re: Getting eth0 device information in C or perl.
Date: 2000/12/05
Message-ID: <Pine.A41.4.21.0012052304150.112374-100000@dante11.u.washington.edu>
X-Deja-AN: 701672113
References: <FjhX5.92$UI4.5375@nsw.nnrp.telstra.net>
Content-Type: TEXT/PLAIN; charset=US-ASCII
X-Complaints-To: help@cac.washington.edu
X-Trace: nntp6.u.washington.edu 976088226 541152 (None) 140.142.17.39
Organization: University of Washington
Mime-Version: 1.0
Newsgroups: comp.os.linux.development.apps,comp.os.linux.development.system,comp.os.linux.networking,linux.dev.apps,linux.dev.c-programming,linux.dev.net,aus.computers.linux
NNTP-Posting-User: msfrank