// defines the ifconf, ifreq structures
#include <net/if.h>
// required for socket operations and address resolution
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <memory.h>
// contains declaration of close()
#include <unistd.h>
//
// Returns the address pointer with IP address stored if the
// interface is found else 0
//
const char* getIP(const char *interface, char* address)
{
int sock = socket(PF_INET, SOCK_DGRAM, 0);
char* res = 0;
address[0] = 0;
if (sock >= 0) {
struct ifconf ifc;
struct ifreq* buf;
// the ioctl call requires a preallocated buffer
ifc.ifc_len = 32 * sizeof(*buf);
buf = (struct ifreq*)malloc(ifc.ifc_len);
ifc.ifc_buf = (void*)buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) >= 0) {
int i,n;
for (i = 0, n = ifc.ifc_len / sizeof(*buf); i < n && !res; i++)
{
if (!strcmp(buf[i].ifr_name, interface)) {
struct sockaddr_in* ipaddr = (struct sockaddr_in
*)&(buf[i].ifr_addr);
strcpy(address, inet_ntoa(ipaddr->sin_addr));
res = address;
}
}
}
free(buf);
close(sock);
}
return res;
}
int main(int argc, char** argv)
{
char address[16];
if (getIP(argv[1], address)) {
printf("Addr %s\n", address);
} else {
printf("iface not found\n");
}
return 0;
}
I use Suse 8.0, Kernel 2.4.19, Athlon 700
would be nice if you could fix the problem! I will try to find out where the
problem is.
Markus
Odd, I compiled and ran the code without any errors, though I did have
to insert the following 2 lines
// contains declaration of malloc(), free()
#include <stdlib.h>
// contains declaration of printf()
#include <stdio.h>
try running the code under GDB, and see where your problem occurs, and
track it down from that point.
BTW: Nick did a nice job on improving the original code sample
You can try this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
int main( int argc, char *argv[])
{
FILE *f=fopen("/proc/net/dev","r");
char buf[4096];
char *p;
struct ifreq ifr;
int fd;
if (!f) {
perror("/proc/net/dev");
exit(1);
}
fd = socket(AF_INET,SOCK_DGRAM, 0);
if (fd==-1) {
perror("socket");
exit(1);
}
while (fgets(buf,4096,f)) {
if ((p=strchr(buf,':'))) {
*p=0;
for (p=buf;*p == ' ';++p);
strcpy(ifr.ifr_name, p);
if (ioctl(fd, SIOCGIFADDR, &ifr)) {
perror(p);
} else {
printf( "%s: %s\n", p,inet_ntoa(*(struct
in_addr*)(ifr.ifr_ifru.ifru_addr.sa_data+2)));
}
}
} return 0;
}
Michael Heiming
--
Remove the +SIGNS case mail bounces.
if(argc != 2){
puts("You must specify the interface, e.g. eth0");
return 1;
}
> if (getIP(argv[1], address)) {
> printf("Addr %s\n", address);
> } else {
> printf("iface not found\n");
> }
>
> return 0;
> }
--
Nils Olav Selåsdal <N...@Utel.no>
System Developer, UtelSystems a/s
w w w . u t e l s y s t e m s . c o m
works perfectly, thanks!
Markus
> wouldnt a simple perl script grepping the output from /sbin/ifcongig do
> the same????
... or a bash script?
Sure it would. Perhaps, you don't wan't/can't call a
shell/perl/foobar script.
> On Sun, 08 Sep 2002 15:42:31 -0400, Sputnik wrote:
>
>
>> wouldnt a simple perl script grepping the output from
>> /sbin/ifcongig do the same????
>>
>
> .... or a bash script?
>
#!/bin/sh
NETWORKDEVICES=$(ifconfig |egrep -v "^ |^$"|awk '{print $1}')
for INTERFACE in ${NETWORKDEVICES}; do
echo ${INTERFACE}" "$(/sbin/ifconfig $INTERFACE | grep inet | \
cut -d : -f 2 | cut -d \ -f 1)
done
--
UNIX - Not just for Vestal Virgins anymore
Linux - Choice of a GNU generation
Indeed, and hence my comment about the driver being lacking (I think I
wrote that in the original post), but omitted for brevity as the post
was already long enough and the main() is throw away code and not
meant to be used as a utility. I'm using a modified version of the
routine in an embedded license request generator in some of my
software. The main is just for test purposes.
Not sure about the memory errors someone was having. Perhaps the
program was called without an argument. The routine is correct.
Re the other comments. Using /proc is indeed an alternative, but the
lower level routine to me is preferable as it's ultimately much
simpler to use and may be more portable.