一 代码
#include <net/if.h> /* for ifconf */
#include <linux/sockios.h> /* for net status mask */
#include <netinet/in.h> /* for sockaddr_in */
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h> //for close
#include <arpa/inet.h>
#include <string.h>
#define MAX_INTERFACE (16)
void port_status(unsigned int flags);
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;
}
/* 网口总数 ifc.ifc_len 应该是一个出入参数 */