/*
* Test. cpp
*
* Created on: 2011-10-26
* Author: Root
*/
# Include <stdio. h>
# Include <sys/types. h>
# Include <sys/Param. h>
# Include <sys/IOCTL. h>
# Include <sys/socket. h>
# Include <net/If. h>
# Include <netinet/in. h>
# Include <net/if_arp.h>
# Include <ARPA/inet. h>
# Include <unistd. h>
# Define maxinterfaces 16
Int main (INT argc, char ** argv)
{
Int FD, intrface, retn = 0;
Struct ifreq Buf [maxinterfaces];
Struct arpreq ARP;
Struct ifconf IFC;
FD = socket (af_inet, sock_dgram, 0 );
If (FD> = 0)
{
IFC. ifc_len = sizeof (BUF );
IFC. ifc_buf = (caddr_t) BUF;
If (! IOCTL (FD, siocgifconf, (char *) & IFC ))
{
Intrface = IFC. ifc_len/sizeof (struct ifreq );
Printf ("interface num is intrface = % d \ n", intrface );
While (intrface --> 0)
{
Printf ("net device % s \ n", Buf [intrface]. ifr_name );
/* Jugde whether the net card status is promisc */
If (! (IOCTL (FD, siocgifflags, (char *) & Buf [intrface])
{
If (BUF [intrface]. ifr_flags & iff_promisc)
{
Puts ("the interface is promisc ");
Retn ++;
}
} Else
{
Char STR [256];
Sprintf (STR, "CPM: IOCTL device % s", Buf [intrface]. ifr_name );
Perror (STR );
}
/* Jugde whether the net card status is up */
If (BUF [intrface]. ifr_flags & iff_up)
{
Puts ("the interface status is up ");
} Else
{
Puts ("the interface status is down ");
}
/* Get IP of the net card */
If (! (IOCTL (FD, siocgifaddr, (char *) & Buf [intrface])
{
Puts ("IP address is :");
Puts (inet_ntoa (struct sockaddr_in *) (& Buf [intrface]. ifr_addr)-> sin_addr ));
Puts ("");
// Puts (BUF [intrface]. ifr_addr.sa_data );
} Else
{
Char STR [256];
Sprintf (STR, "CPM: IOCTL device % s", Buf [intrface]. ifr_name );
Perror (STR );
}
/* Get HW address of the net card */
If (! (IOCTL (FD, siocgifhwaddr, (char *) & Buf [intrface])
{
Puts ("HW address is :");
Printf (
"% 02x: % 02x: % 02x: % 02x: % 02x: % 02x \ n ",
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [0],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [1],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [2],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [3],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [4],
(Unsigned char) BUF [intrface]. ifr_hwaddr.sa_data [5]);
Puts ("");
Puts ("");
} Else
{
Char STR [256];
Sprintf (STR, "CPM: IOCTL device % s", Buf [intrface]. ifr_name );
Perror (STR );
}
}
} Else
Perror ("CPM: IOCTL ");
} Else
Perror ("CPM: socket ");
Close (FD );
Return retn;
}