11-13-2009
3,
0
Join Date: Aug 2009
Last Activity: 14 November 2009, 12:20 AM EST
Posts: 3
Thanks Given: 0
Thanked 0 Times in 0 Posts
AIX platform: network interface list
Hi,
I am trying to list all the interface present in the given system.
Here is sample program.
i.e. cat 1.c
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/dlpi.h>
#include <net/if.h>
/*#include <sys/sockio.h>*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <netdb.h>
void num_of_interfaces(void);
void
in_getaddr(char *s, struct sockaddr *saddr)
{
struct sockaddr_in *sin = (struct sockaddr_in *) saddr;
struct hostent *hp;
struct netent *np;
int error;
char str[BUFSIZ];
(void) memset(sin, 0, sizeof (*sin));
(void) strncpy(str, s, sizeof(str));
hp = getipnodebyname(str, AF_INET, 0, &error);
if (!hp) {
perror("getipnodebyname");
return;
}
freehostent(hp);
sin->sin_family = AF_INET;
(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
}
void num_of_interfaces()
{
struct ifreq lifr;
int n;
char *buf;
int s = socket(AF_INET, SOCK_DGRAM, 0);
//struct ifnum lifn;
struct ifconf lifc;
struct ifreq *lifrp;
struct ifreq lifrl; /* Local lifreq struct */
int numifs;
unsigned bufsize;
// int64_t lifc_flags = LIFC_NOXMIT;
// lifn.lifn_family = AF_UNSPEC;
// lifn.lifn_flags = lifc_flags;
// if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
/* Could not determine number of interfaces */
// exit(2);
//}
numifs = 10;
// printf("numifs is %d\n",numifs);
bufsize = numifs * sizeof (struct ifreq);
if ((buf = (char *)malloc(bufsize)) == NULL) {
/* out of memory */
close(s);
exit(1);
}
memset(buf, 0, sizeof(bufsize));
// lifc.ifc_family = AF_UNSPEC;
// lifc.ifc_flags = lifc_flags;
lifc.ifc_len = bufsize;
lifc.ifc_buf = buf;
if (ioctl(s, SIOCGIFCONF, (char *)&lifc) < 0) {
free(buf);
exit(1);
}
lifrp = lifc.ifc_req;
for (n = numifs ; n > 0; n--, lifrp++) {
(void) close(s);
if (s == -1) {
exit(1);
}
/* print the name of the interface */
printf("%s\n", lifrp->ifr_name);
}
if (buf != NULL)
free(buf);
}
int
main (int argc, char *argv[])
{
int errors = 0,choice=0;
choice=atoi(argv[1]);
if (choice == 0)
num_of_interfaces();
return 0;
}
output:
bash-3.00$ ./a.out 0
en0
Actual machine has 4 interface but it is listing first one only.
Pls let me know what could be the problem.