Getting IP of interface

Hi,
Hopefully a quick question, what’s an easy way (in code) to get the IP
address of an interface? I want to get the IP address that an “ipconfig
en1” might return without doing a popen() and parsing the data.

TIA,
Ron

Ron Cococcia <cococr@cs.rpi.edu> wrote:

Hi,
Hopefully a quick question, what’s an easy way (in code) to get the IP
address of an interface? I want to get the IP address that an “ipconfig
en1” might return without doing a popen() and parsing the data.

See gethostname() and gethostbyname() (TPC/IP toolkit). However, as far as
I know, this works only with the names of hosts, not with the names of
interfaces. But if you know, that e.g. aaa.net correspond to en2, you can
use the functions.


Martin Gazak, MicroStep-MIS
Ilkovicova 3, 841 04 Bratislava, Slovakia
Tel: ++421 7 60291 816
e-mail:matog@microstep-mis.sk

I’ve done a little more work on this, but still unsuccessful. I found the
ioctl’s that might work in ioctl.h, and the structures that I might need to
pass.

SIOCGIFADDR is the one I’m using. It requires a struct ifreq (defined in
net/inet.h) which has an interface name as a field (“en0” is the example
they have in the inet.h file).

I create a socket, make an ifreq struct and put the interface name into the
field of that struct that requires it, and get an Invalid Address error.
I’m just wondering (QNX staff might know best) what I might be doing wrong
and what else I might have to do, or if I should even give up and try
something else? Any suggestions are greatly appreciated!

Ron


“Martin Gazak” <matog@microstep-mis.sk> wrote in message
news:9h98ls$au7$1@charon.mstep-hdo.sk

Ron Cococcia <> cococr@cs.rpi.edu> > wrote:
Hi,
Hopefully a quick question, what’s an easy way (in code) to get the IP
address of an interface? I want to get the IP address that an “ipconfig
en1” might return without doing a popen() and parsing the data.

See gethostname() and gethostbyname() (TPC/IP toolkit). However, as far as
I know, this works only with the names of hosts, not with the names of
interfaces. But if you know, that e.g. aaa.net correspond to en2, you can
use the functions.


Martin Gazak, MicroStep-MIS
Ilkovicova 3, 841 04 Bratislava, Slovakia
Tel: ++421 7 60291 816
e-mail:> matog@microstep-mis.sk

Ron Cococcia <cococr@cs.rpi.edu> wrote:

I’ve done a little more work on this, but still unsuccessful. I found the
ioctl’s that might work in ioctl.h, and the structures that I might need to
pass.

SIOCGIFADDR is the one I’m using. It requires a struct ifreq (defined in
net/inet.h) which has an interface name as a field (“en0” is the example
they have in the inet.h file).

I create a socket, make an ifreq struct and put the interface name into the
field of that struct that requires it, and get an Invalid Address error.
I’m just wondering (QNX staff might know best) what I might be doing wrong
and what else I might have to do, or if I should even give up and try
something else? Any suggestions are greatly appreciated!

I don’t have a clean install now, but isn’t that a sample under:

/usr/demo/src/socket/if_example.c ?

Anyway, here is the file:

------------------------ if_example.c -------------------
#include <ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>

int
main (int argc, char *argv[])
{
int sock;
struct ifconf d;
struct ifreq *ifr, *end, *cur, *temp;
char buffer[128];

if ((sock= socket (AF_INET, SOCK_DGRAM, 0)) == -1) {
perror (“socket”);
exit (EXIT_FAILURE);
}

/* temporary storage for getting broadcast address */
temp= (struct ifreq *)buffer;

d.ifc_len= 4096;
d.ifc_buf= (char *)malloc (d.ifc_len);
d.ifc_len= -1;
if (ioctl (sock, SIOCGIFCONF, &d) == -1) {
perror (“ioctl (SIOCGIFCONF)”);
exit (EXIT_FAILURE);
}

/*

  • Note that the ifreq structure is variable length so we have to step
  • along the structure by the size of the prev structure
    */
    ifr= (struct ifreq *) d.ifc_req;
    end= (struct ifreq *) ((char ) ifr + d.ifc_len);
    while (ifr < end) {
    cur= ifr;
    /
    step along the array by the size of the current structure */
    ifr=(struct ifreq *)((char *)ifr + ifr->ifr_addr.sa_len + IFNAMSIZ);

/* if this isn’t in the INET address family ignore */
if (cur->ifr_addr.sa_family != AF_INET)
continue;

/* save aside the ifr structure to get the broadcast addr */
memcpy(temp, cur, cur->ifr_addr.sa_len + IFNAMSIZ);

printf ("%s (%s) ", cur->ifr_name, inet_ntoa(((struct sockaddr_in *)
&cur->ifr_addr)->sin_addr));

/* get the flags for this interface */
if (ioctl (sock, SIOCGIFFLAGS, (char *) cur) < 0) {
perror (“ioctl (SIOCGIFFLAGS)”);
exit (EXIT_FAILURE);
}

/* if the interface is up print out some information about it */
if (cur->ifr_flags & IFF_UP) {
if (cur->ifr_flags & IFF_BROADCAST) {
printf ("broadcast “);
if (ioctl(sock, SIOCGIFBRDADDR, (char *)temp) != -1)
printf(”%s ", inet_ntoa(((struct sockaddr_in *)
&temp->ifr_addr)->sin_addr));
else {
perror(“ioctl (SIOCGIFBRDADDR)”);
exit(EXIT_FAILURE);
}
}

if (cur->ifr_flags & IFF_POINTOPOINT) {
printf ("point-to-point dst “);
if (ioctl(sock, SIOCGIFDSTADDR, (char *)temp) != -1)
printf(”%s ", inet_ntoa(((struct sockaddr_in *)
&cur->ifr_addr)->sin_addr));
else {
perror(“ioctl (SIOCGIFDSTADDR)”);
exit(EXIT_FAILURE);
}
}
}
putc (’\n’, stdout);
}

return EXIT_SUCCESS;
}
---------------------- if_example.c ---------------------------------

-xtang