Query available Adapters

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more suggestions
on doing this in a program.

Thanks,
-Beth

Take a look at the getifaddrs() function. It may be just what you need.

Murf

Beth wrote:

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more suggestions
on doing this in a program.

Thanks,
-Beth

John, thanks for your suggestion. Yes, getifaddrs() is what I need, but it
seems this API is only supported in QNX 6.2, not in version 6.1.
I am running 6.1A. Is there a similar function like getifaddrs() for QNX 6.1
? I am writing a monitor code to check the status of interfaces
and adapters when they are plugged in/out and when they are up and down.

Appreciate your help.

-Beth

“John A. Murphy” wrote:

Take a look at the getifaddrs() function. It may be just what you need.

Murf

Beth wrote:

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more suggestions
on doing this in a program.

Thanks,
-Beth

Beth <id@address.com> wrote in message news:3E7A2C04.CEA7396E@address.com

John, thanks for your suggestion. Yes, getifaddrs() is what I need, but it
seems this API is only supported in QNX 6.2, not in version 6.1.
I am running 6.1A. Is there a similar function like getifaddrs() for QNX
6.1
? I am writing a monitor code to check the status of interfaces
and adapters when they are plugged in/out and when they are up and down.

Traditional way is use SIOCGIFADDR/SIOCGIFCONF.

-xtang

Appreciate your help.

-Beth

“John A. Murphy” wrote:

Take a look at the getifaddrs() function. It may be just what you need.

Murf

Beth wrote:

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more
suggestions
on doing this in a program.

Thanks,
-Beth

I have the following program using SIOCGIFCONF call to get a list of interface
names.
When my system was running tiny TCP/IP, I got following result:

interface 3: en0
interface 2: en0
interface 1: L

When the system was running full TCP/IP, the result was:

interface 4: lo0
interface 3:
interface 2:
interface 1: KL

From ‘cat /proc/ipstats’ or ‘ifconfig -a’, there should have two interfaces en0
and lo0.
What am I missing in this code?

Also, how do I know which one is virtual interface and which is network
interface?


#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <stdio.h>


int main( int argc, char *argv[] )
{
unsigned int i;
unsigned int n;
static char ifbuf[5000];
static struct ifconf ifc;
static struct ifreq *ifr;

int sock = socket( AF_INET, SOCK_DGRAM, 0 );
if (sock == -1)
{
return(-1);
}

/* load the interface info */
ifc.ifc_buf = ifbuf;
ifc.ifc_len = sizeof(ifbuf);
if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
return (-1);
}
i = ifc.ifc_len / sizeof(struct ifreq);
ifr = ifc.ifc_req;

close(sock);
sock = 0;

n= 0;
for (; i > 0; --i >=0, ifr++)
{
printf(“interface %d: %s\n”, i, ifr->ifr_name);
}
return 0;
}

Appreciate any help.

Thanks,
-Beth

Xiaodan Tang wrote:

Beth <> id@address.com> > wrote in message news:> 3E7A2C04.CEA7396E@address.com> …
John, thanks for your suggestion. Yes, getifaddrs() is what I need, but it
seems this API is only supported in QNX 6.2, not in version 6.1.
I am running 6.1A. Is there a similar function like getifaddrs() for QNX
6.1
? I am writing a monitor code to check the status of interfaces
and adapters when they are plugged in/out and when they are up and down.

Traditional way is use SIOCGIFADDR/SIOCGIFCONF.

-xtang
Appreciate your help.

-Beth

“John A. Murphy” wrote:

Take a look at the getifaddrs() function. It may be just what you need.

Murf

Beth wrote:

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more
suggestions
on doing this in a program.

Thanks,
-Beth

What you’re missing is the variable length of the struct ifreq. Try the enclosed
code. For each interface you’ll get an entry for the AF_LINK family, from which
you can get the MAC address, an entry for the AF_INET family, from which you can
get the IP address, and, with the full stack, at least one entry for the AF_INET6
family, which you can probably ignore. In the case of the AF_INET entries, you may
want to use the SIOCGIFFLAGS ioctl to get the flags so that you can see if the
interface is UP, is a LOOPBACK interface, etc.

#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
unsigned int i;
unsigned int n;
static char ifbuf[5000];
static struct ifconf ifc;
static struct ifreq *ifr;

int sock = socket( AF_INET, SOCK_DGRAM, 0 );
if (sock == -1)
{
return(-1);
}

/* load the interface info */
ifc.ifc_buf = ifbuf;
ifc.ifc_len = sizeof(ifbuf);
if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
return (-1);
}
i = ifc.ifc_len;
ifr = ifc.ifc_req;

close(sock);
sock = 0;

while (i)
{
printf("%s family=%d", ifr->ifr_name, ifr->ifr_addr.sa_family);
if (ifr->ifr_addr.sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
printf(" %s", inet_ntoa(sin->sin_addr));
}
printf("\n");
n = max(ifr->ifr_addr.sa_len + sizeof(ifr->ifr_name), sizeof(*ifr));
ifr = (struct ifreq *)((char *)ifr + n);
i -= n;
}
return 0;
}

Murf

Beth wrote:

I have the following program using SIOCGIFCONF call to get a list of interface
names.
When my system was running tiny TCP/IP, I got following result:

interface 3: en0
interface 2: en0
interface 1: L

When the system was running full TCP/IP, the result was:

interface 4: lo0
interface 3:
interface 2:
interface 1: KL

From ‘cat /proc/ipstats’ or ‘ifconfig -a’, there should have two interfaces en0
and lo0.
What am I missing in this code?

Also, how do I know which one is virtual interface and which is network
interface?

#include <sys/types.h
#include <fcntl.h
#include <sys/socket.h
#include <netinet/in.h
#include <arpa/inet.h
#include <sys/ioctl.h
#include <net/if.h
#include <net/if_arp.h
#include <stdio.h

int main( int argc, char *argv[] )
{
unsigned int i;
unsigned int n;
static char ifbuf[5000];
static struct ifconf ifc;
static struct ifreq *ifr;

int sock = socket( AF_INET, SOCK_DGRAM, 0 );
if (sock == -1)
{
return(-1);
}

/* load the interface info */
ifc.ifc_buf = ifbuf;
ifc.ifc_len = sizeof(ifbuf);
if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
return (-1);
}
i = ifc.ifc_len / sizeof(struct ifreq);
ifr = ifc.ifc_req;

close(sock);
sock = 0;

n= 0;
for (; i > 0; --i >=0, ifr++)
{
printf(“interface %d: %s\n”, i, ifr->ifr_name);
}
return 0;
}

Appreciate any help.

Thanks,
-Beth

Xiaodan Tang wrote:

Beth <> id@address.com> > wrote in message news:> 3E7A2C04.CEA7396E@address.com> …
John, thanks for your suggestion. Yes, getifaddrs() is what I need, but it
seems this API is only supported in QNX 6.2, not in version 6.1.
I am running 6.1A. Is there a similar function like getifaddrs() for QNX
6.1
? I am writing a monitor code to check the status of interfaces
and adapters when they are plugged in/out and when they are up and down.

Traditional way is use SIOCGIFADDR/SIOCGIFCONF.

-xtang
Appreciate your help.

-Beth

“John A. Murphy” wrote:

Take a look at the getifaddrs() function. It may be just what you need.

Murf

Beth wrote:

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more
suggestions
on doing this in a program.

Thanks,
-Beth

John, thank you very much for the information and the code. After I got a list of the
adapters on the system, I want to get other information by interface name. I have used
SIOCGIFADDR, SIOCGIFNETMASK, SIOCGFIFFLAGS, and SIOCGFIFMTU. What’s missing is the
SIOCGIFHWADDR. Is it not supported?
Also, if my system is running tiny TCP/IP, any reason why the SIOCGIFMTU call would
fail?

Appreciate your help.

-Beth



“John A. Murphy” wrote:

What you’re missing is the variable length of the struct ifreq. Try the enclosed
code. For each interface you’ll get an entry for the AF_LINK family, from which
you can get the MAC address, an entry for the AF_INET family, from which you can
get the IP address, and, with the full stack, at least one entry for the AF_INET6
family, which you can probably ignore. In the case of the AF_INET entries, you may
want to use the SIOCGIFFLAGS ioctl to get the flags so that you can see if the
interface is UP, is a LOOPBACK interface, etc.

#include <sys/types.h
#include <fcntl.h
#include <sys/socket.h
#include <netinet/in.h
#include <arpa/inet.h
#include <sys/ioctl.h
#include <net/if.h
#include <net/if_arp.h
#include <stdio.h
#include <stdlib.h

int main( int argc, char *argv[] )
{
unsigned int i;
unsigned int n;
static char ifbuf[5000];
static struct ifconf ifc;
static struct ifreq *ifr;

int sock = socket( AF_INET, SOCK_DGRAM, 0 );
if (sock == -1)
{
return(-1);
}

/* load the interface info */
ifc.ifc_buf = ifbuf;
ifc.ifc_len = sizeof(ifbuf);
if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
return (-1);
}
i = ifc.ifc_len;
ifr = ifc.ifc_req;

close(sock);
sock = 0;

while (i)
{
printf("%s family=%d", ifr->ifr_name, ifr->ifr_addr.sa_family);
if (ifr->ifr_addr.sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
printf(" %s", inet_ntoa(sin->sin_addr));
}
printf("\n");
n = max(ifr->ifr_addr.sa_len + sizeof(ifr->ifr_name), sizeof(*ifr));
ifr = (struct ifreq *)((char *)ifr + n);
i -= n;
}
return 0;
}

Murf

Beth wrote:

I have the following program using SIOCGIFCONF call to get a list of interface
names.
When my system was running tiny TCP/IP, I got following result:

interface 3: en0
interface 2: en0
interface 1: L

When the system was running full TCP/IP, the result was:

interface 4: lo0
interface 3:
interface 2:
interface 1: KL

From ‘cat /proc/ipstats’ or ‘ifconfig -a’, there should have two interfaces en0
and lo0.
What am I missing in this code?

Also, how do I know which one is virtual interface and which is network
interface?

#include <sys/types.h
#include <fcntl.h
#include <sys/socket.h
#include <netinet/in.h
#include <arpa/inet.h
#include <sys/ioctl.h
#include <net/if.h
#include <net/if_arp.h
#include <stdio.h

int main( int argc, char *argv[] )
{
unsigned int i;
unsigned int n;
static char ifbuf[5000];
static struct ifconf ifc;
static struct ifreq *ifr;

int sock = socket( AF_INET, SOCK_DGRAM, 0 );
if (sock == -1)
{
return(-1);
}

/* load the interface info */
ifc.ifc_buf = ifbuf;
ifc.ifc_len = sizeof(ifbuf);
if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
return (-1);
}
i = ifc.ifc_len / sizeof(struct ifreq);
ifr = ifc.ifc_req;

close(sock);
sock = 0;

n= 0;
for (; i > 0; --i >=0, ifr++)
{
printf(“interface %d: %s\n”, i, ifr->ifr_name);
}
return 0;
}

Appreciate any help.

Thanks,
-Beth

Xiaodan Tang wrote:

Beth <> id@address.com> > wrote in message news:> 3E7A2C04.CEA7396E@address.com> …
John, thanks for your suggestion. Yes, getifaddrs() is what I need, but it
seems this API is only supported in QNX 6.2, not in version 6.1.
I am running 6.1A. Is there a similar function like getifaddrs() for QNX
6.1
? I am writing a monitor code to check the status of interfaces
and adapters when they are plugged in/out and when they are up and down.

Traditional way is use SIOCGIFADDR/SIOCGIFCONF.

-xtang
Appreciate your help.

-Beth

“John A. Murphy” wrote:

Take a look at the getifaddrs() function. It may be just what you need.

Murf

Beth wrote:

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more
suggestions
on doing this in a program.

Thanks,
-Beth

You can get the mac addr by changing the code below to look for
ifr->ifr_addr.sa_family == AF_LINK.

-seanb

Beth <id@address.com> wrote:

John, thank you very much for the information and the code. After I got a list of the
adapters on the system, I want to get other information by interface name. I have used
SIOCGIFADDR, SIOCGIFNETMASK, SIOCGFIFFLAGS, and SIOCGFIFMTU. What’s missing is the
SIOCGIFHWADDR. Is it not supported?
Also, if my system is running tiny TCP/IP, any reason why the SIOCGIFMTU call would
fail?

Appreciate your help.

-Beth



“John A. Murphy” wrote:

What you’re missing is the variable length of the struct ifreq. Try the enclosed
code. For each interface you’ll get an entry for the AF_LINK family, from which
you can get the MAC address, an entry for the AF_INET family, from which you can
get the IP address, and, with the full stack, at least one entry for the AF_INET6
family, which you can probably ignore. In the case of the AF_INET entries, you may
want to use the SIOCGIFFLAGS ioctl to get the flags so that you can see if the
interface is UP, is a LOOPBACK interface, etc.

#include <sys/types.h
#include <fcntl.h
#include <sys/socket.h
#include <netinet/in.h
#include <arpa/inet.h
#include <sys/ioctl.h
#include <net/if.h
#include <net/if_arp.h
#include <stdio.h
#include <stdlib.h

int main( int argc, char *argv[] )
{
unsigned int i;
unsigned int n;
static char ifbuf[5000];
static struct ifconf ifc;
static struct ifreq *ifr;

int sock = socket( AF_INET, SOCK_DGRAM, 0 );
if (sock == -1)
{
return(-1);
}

/* load the interface info */
ifc.ifc_buf = ifbuf;
ifc.ifc_len = sizeof(ifbuf);
if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
return (-1);
}
i = ifc.ifc_len;
ifr = ifc.ifc_req;

close(sock);
sock = 0;

while (i)
{
printf("%s family=%d", ifr->ifr_name, ifr->ifr_addr.sa_family);
if (ifr->ifr_addr.sa_family == AF_INET) {
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
printf(" %s", inet_ntoa(sin->sin_addr));
}
printf("\n");
n = max(ifr->ifr_addr.sa_len + sizeof(ifr->ifr_name), sizeof(*ifr));
ifr = (struct ifreq *)((char *)ifr + n);
i -= n;
}
return 0;
}

Murf

Beth wrote:

I have the following program using SIOCGIFCONF call to get a list of interface
names.
When my system was running tiny TCP/IP, I got following result:

interface 3: en0
interface 2: en0
interface 1: L

When the system was running full TCP/IP, the result was:

interface 4: lo0
interface 3:
interface 2:
interface 1: KL

From ‘cat /proc/ipstats’ or ‘ifconfig -a’, there should have two interfaces en0
and lo0.
What am I missing in this code?

Also, how do I know which one is virtual interface and which is network
interface?

#include <sys/types.h
#include <fcntl.h
#include <sys/socket.h
#include <netinet/in.h
#include <arpa/inet.h
#include <sys/ioctl.h
#include <net/if.h
#include <net/if_arp.h
#include <stdio.h

int main( int argc, char *argv[] )
{
unsigned int i;
unsigned int n;
static char ifbuf[5000];
static struct ifconf ifc;
static struct ifreq *ifr;

int sock = socket( AF_INET, SOCK_DGRAM, 0 );
if (sock == -1)
{
return(-1);
}

/* load the interface info */
ifc.ifc_buf = ifbuf;
ifc.ifc_len = sizeof(ifbuf);
if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) {
return (-1);
}
i = ifc.ifc_len / sizeof(struct ifreq);
ifr = ifc.ifc_req;

close(sock);
sock = 0;

n= 0;
for (; i > 0; --i >=0, ifr++)
{
printf(“interface %d: %s\n”, i, ifr->ifr_name);
}
return 0;
}

Appreciate any help.

Thanks,
-Beth

Xiaodan Tang wrote:

Beth <> id@address.com> > wrote in message news:> 3E7A2C04.CEA7396E@address.com> …
John, thanks for your suggestion. Yes, getifaddrs() is what I need, but it
seems this API is only supported in QNX 6.2, not in version 6.1.
I am running 6.1A. Is there a similar function like getifaddrs() for QNX
6.1
? I am writing a monitor code to check the status of interfaces
and adapters when they are plugged in/out and when they are up and down.

Traditional way is use SIOCGIFADDR/SIOCGIFCONF.

-xtang
Appreciate your help.

-Beth

“John A. Murphy” wrote:

Take a look at the getifaddrs() function. It may be just what you need.

Murf

Beth wrote:

What is the best way to query the available adapters/interfaces of the
system from a program? I need status information of all available
Ethernet adapters, wireless adapters and ppp interfaces, when they are
coming up or down and the IP address associate with these adapters.
Both for Tiny TCP/IP and full TCP/IP.
I know of one way is to parse the /proc/ipstats file for tiny tcp/ip,
and use ifconfig for the full tcp/ip. I am looking for more
suggestions
on doing this in a program.

Thanks,
-Beth