Ifconfig is our view/Set network port state commonly used commands, in fact, this command is a number of system function encapsulation, through the modification of struct ifreq, we can easily set the network port State, get the network port state.
This structure is located in the <net/if.h> header file, open to see it, in contrast with the English annotation, it is easy to find what you want. And some of the network card-related IOCTL operations, that is, for query/modification status of the mark bit, are in <linux/sockios.h>, very clear, find a source to see it. Here is a small example that demonstrates the process of getting information, and, if necessary, enables the function that modifies the state, noting that the power user's permissions are needed when modifying the status tag bit.
/** * \file getifstat.c * \author WZJ * \brief Access this struct ifconf modify, query status * \version * \note * \date:2012 year August 11 Saturday 22:55:25/* #include <net/if.h>/* for ifconf/#include <linux/sockios.h>/* For NET status mask/* #i Nclude <netinet/in.h>/* for sockaddr_in */#include <sys/socket.h> #include <sys/types.h> #include
;sys/ioctl.h> #include <stdio.h> #define Max_interface void port_status (unsigned int flags); /* Set = = 0:do Clean, set = = 1:do set!
* * int set_if_flags (char *pif_name, int sock, int staus, int set) {struct Ifreq IFR;
int ret = 0;
strncpy (Ifr.ifr_name, Pif_name, strlen (pif_name) + 1);
RET = IOCTL (sock, Siocgifflags, &IFR);
if (ret) return-1;
/* Set or Clean */if (set) Ifr.ifr_flags |= status;
else Ifr.ifr_flags &= ~status;
/* Set flags/ret = IOCTL (sock, Siocsifflags, &IFR);
if (ret) return-1;
return 0;
int get_if_info (int fd) {struct ifreq buf[max_interface];struct ifconf ifc;
int ret = 0;
int if_num = 0;
Ifc.ifc_len = sizeof (BUF);
Ifc.ifc_buf = (caddr_t) buf;
RET = IOCTL (FD, siocgifconf, (char*) &ifc);
if (ret) {printf ("Get if config info failed");
return-1;
/* The total number of Ifc.ifc_len should be an access parameter * * if_num = ifc.ifc_len/sizeof (struct ifreq);
printf ("interface num is interface =%d\n", if_num);
while (if_num--> 0) {printf ("NET Device:%s\n", buf[if_num].ifr_name);
/* Get the NTH Web port information * * ret = IOCTL (FD, Siocgifflags, (char*) &buf[if_num));
if (ret) continue;
* * Get the network port State * * * Port_status (buf[if_num].ifr_flags);
/* Get current network card IP address/ret = IOCTL (FD, SIOCGIFADDR, (char*) &buf[if_num]);
if (ret) continue;
printf ("IP address is: \n%s\n", Inet_ntoa ((struct sockaddr_in *) (&BUF[IF_NUM].IFR_ADDR)) (->SIN_ADDR));
/* Get the current NIC's Mac/ret = IOCTL (FD, SIOCGIFHWADDR, (char*) &buf[if_num]);
if (ret) continue; printf ("%02x:%02x:%02x:%02x:%02x:%02x\n\n", (unsigned char) buf[if_num].ifr_hwaddr.sa_Data[0], (unsigned char) buf[if_num].ifr_hwaddr.sa_data[1], (unsigned char) buf[if_num].ifr_hwaddr.sa_data[2], (uns igned char) buf[if_num].ifr_hwaddr.sa_data[3], (unsigned char) buf[if_num].ifr_hwaddr.sa_data[4], (unsigned char) buf[
IF_NUM].IFR_HWADDR.SA_DATA[5]);
} void Port_status (unsigned int flags) {if (Flags & iff_up) {printf ("is up\n");
} if (Flags & iff_broadcast) {printf ("is broadcast\n");
} if (Flags & Iff_loopback) {printf ("is Loop back\n");
} if (Flags & Iff_pointopoint) {printf ("is point to point\n");
} if (Flags & iff_running) {printf ("is running\n");
} if (Flags & Iff_promisc) {printf ("is promisc\n");
int main () {int fd;
FD = socket (af_inet, SOCK_DGRAM, 0);
if (FD > 0) {get_if_info (FD);
Close (FD);
return 0; }