11-13-2009
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.
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hi !
Does anyone know how can i get information about my network interface ... if it works in half or full duplex mode !!
Thx in adivance !
Witt (4 Replies)
Discussion started by: witt
4 Replies
2. Programming
Hello
I have a problem with retreiving network interface list using IOCTLs. I have 4 interfaces:
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope... (1 Reply)
Discussion started by: xyzt
1 Replies
3. AIX
Hi All,
Please let me know the command to restart the network interface and enable it on boot in AIX, similar to /etc/init.d/network restart in Redhat.
Thanks,
Sunil.K
please watch out to post in the right subforum! (9 Replies)
Discussion started by: sunilrk07
9 Replies
4. Solaris
i need to configure a zone to use different interface (bge2) than global and have connected to completely different network switch & to use its own defaultrouter and hosts file .. is it possible ..if so ..how ?
Thanks (9 Replies)
Discussion started by: skamal4u
9 Replies
5. Linux
Hi,
Can any one please help me increase the arp stale time of an ipv6 interface on linux platform ?
I have tried increasing the variable gc_stale_time but that doesnt work.
Thanks (2 Replies)
Discussion started by: dkothapa
2 Replies
6. UNIX and Linux Applications
Hi,
I'm a italian student. For my thesis I develop a gateway with protocol 6lowpan.
For that I must access to network interface to develope my personal stack based on standard 802.15.4.
Can you help me? I need an explanation for that. (0 Replies)
Discussion started by: berny88
0 Replies
7. IP Networking
Hello,
Please what's the difference between:
AND
Thank you so much (3 Replies)
Discussion started by: chercheur857
3 Replies
8. IP Networking
Hello,
Please, how can i create a network interface with a routable IP address on linux (ubuntu) ?
AND
How can i desactivate an interface?
Thank you so much for help. (1 Reply)
Discussion started by: chercheur857
1 Replies
9. Solaris
I've one Netra 240
After changing main board and system configuration card reader, Network is not accessible any more, Network interfaces are always UP and Running even when there is no cable connected to Network interfaces.
I tried to restart and plumb/unplumb with no luck.
ifconfig -a... (7 Replies)
Discussion started by: samer.odeh
7 Replies
10. UNIX for Dummies Questions & Answers
I have a RHEL 5 system with a bonded interface configure using only one network port (eth0). So I have config file for ifcfg-bond0 and ifcfg-eth. I'd like to configure eth5 to be the second SLAVE in the bond. My question is, after I modify ifcfg-eth5, can I add eth5 to the bond0 interface without... (1 Reply)
Discussion started by: westmoreland
1 Replies
LEARN ABOUT SUNOS
msgsnap
msgsnap(2) System Calls msgsnap(2)
NAME
msgsnap - message queue snapshot operation
SYNOPSIS
#include <sys/msg.h>
msgsnap(int msqid, void *buf, size_t bufsz, long msgtyp);
DESCRIPTION
The msgsnap() function reads all of the messages of type msgtyp from the queue associated with the message queue identifier specified by
msqid and places them in the user-defined buffer pointed to by buf.
The buf argument points to a user-defined buffer that on return will contain first a buffer header structure:
struct msgsnap_head {
size_t msgsnap_size; /* bytes used/required in the buffer */
size_t msgsnap_nmsg; /* number of messages in the buffer */
};
followed by msgsnap_nmsg messages, each of which starts with a message header:
struct msgsnap_mhead {
size_t msgsnap_mlen; /* number of bytes in the message */
long msgsnap_mtype; /* message type */
};
and followed by msgsnap_mlen bytes containing the message contents.
Each subsequent message header is located at the first byte following the previous message contents, rounded up to a sizeof(size_t) bound-
ary.
The bufsz argument specifies the size of buf in bytes. If bufsz is less than sizeof(msgsnap_head), msgsnap() fails with EINVAL. If bufsz
is insufficient to contain all of the requested messages, msgsnap() succeeds but returns with msgsnap_nmsg set to 0 and with msgsnap_size
set to the required size of the buffer in bytes.
The msgtyp argument specifies the types of messages requested as follows:
o If msgtyp is 0, all of the messages on the queue are read.
o If msgtyp is greater than 0, all messages of type msgtyp are read.
o If msgtyp is less than 0, all messages with type less than or equal to the absolute value of msgtyp are read.
The msgsnap() function is a non-destructive operation. Upon completion, no changes are made to the data structures associated with msqid.
RETURN VALUES
Upon successful completion, msgsnap() returns 0. Otherwise, -1 is returned and errno is set to indicate the error.
ERRORS
The msgsnap() function will fail if:
EACCES Operation permission is denied to the calling process. See intro(2).
EINVAL The msqid argument is not a valid message queue identifier or the value of bufsz is less than sizeof(struct msgsnap_head).
EFAULT The buf argument points to an illegal address.
USAGE
The msgsnap() function returns a snapshot of messages on a message queue at one point in time. The queue contents can change immediately
following return from msgsnap().
EXAMPLES
Example 1: msgsnap() example
This is sample C code indicating how to use the msgsnap function (see msgids(2)).
void
process_msgid(int msqid)
{
size_t bufsize;
struct msgsnap_head *buf;
struct msgsnap_mhead *mhead;
int i;
/* allocate a minimum-size buffer */
buf = malloc(bufsize = sizeof(struct msgsnap_head));
/* read all of the messages from the queue */
for (;;) {
if (msgsnap(msqid, buf, bufsize, 0) != 0) {
perror("msgsnap");
free(buf);
return;
}
if (bufsize >= buf->msgsnap_size) /* we got them all */
break;
/* we need a bigger buffer */
buf = realloc(buf, bufsize = buf->msgsnap_size);
}
/* process each message in the queue (there may be none) */
mhead = (struct msgsnap_mhead *)(buf + 1); /* first message */
for (i = 0; i < buf->msgsnap_nmsg; i++) {
size_t mlen = mhead->msgsnap_mlen;
/* process the message contents */
process_message(mhead->msgsnap_mtype, (char *)(mhead+1), mlen);
/* advance to the next message header */
mhead = (struct msgsnap_mhead *)
((char *)mhead + sizeof(struct msgsnap_mhead) +
((mlen + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)));
}
free(buf);
}
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
+-----------------------------+-----------------------------+
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
+-----------------------------+-----------------------------+
|MT-Level |Async-Signal-Safe |
+-----------------------------+-----------------------------+
SEE ALSO
ipcrm(1), ipcs(1), intro(2), msgctl(2), msgget(2), msgids(2), msgrcv(2), msgsnd(2), attributes(5)
SunOS 5.10 8 Mar 2000 msgsnap(2)