一、struct ifreq结构体

这个结构定义在/usr/include/net/if.h,用来配置和获取ip地址,掩码,MTU等接口信息的。

/* Interface request structure used for socket ioctl's. All interface
   ioctl's must have parameter definitions which begin with ifr_name.
   The remainder may be interface specific. */
 
struct ifreq
  {
# define IFHWADDRLEN 6
# define IFNAMSIZ IF_NAMESIZE
    union
      {
        char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
      } ifr_ifrn;
 
    union
      {
        struct sockaddr ifru_addr;
        struct sockaddr ifru_dstaddr;
        struct sockaddr ifru_broadaddr;
        struct sockaddr ifru_netmask;
        struct sockaddr ifru_hwaddr;
        short int ifru_flags;
        int ifru_ivalue;
        int ifru_mtu;
        struct ifmap ifru_map;
        char ifru_slave[IFNAMSIZ]; /* Just fits the size */
        char ifru_newname[IFNAMSIZ];
        __caddr_t ifru_data;
      } ifr_ifru;
  };
# define ifr_name ifr_ifrn.ifrn_name /* interface name */
# define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */
# define ifr_addr ifr_ifru.ifru_addr /* address */
# define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */
# define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
# define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */
# define ifr_flags ifr_ifru.ifru_flags /* flags */
# define ifr_metric ifr_ifru.ifru_ivalue /* metric */
# define ifr_mtu ifr_ifru.ifru_mtu /* mtu */
# define ifr_map ifr_ifru.ifru_map /* device map */
# define ifr_slave ifr_ifru.ifru_slave /* slave device */
# define ifr_data ifr_ifru.ifru_data /* for use by interface */
# define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */
# define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */
# define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */
# define ifr_newname ifr_ifru.ifru_newname /* New name */
# define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
# define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)
-------------------------------------------------------------------------------------------------------------------------------

二、用法:

通过ioctl()函数调用
下表列出了网络相关ioctl请求的request 参数以及arg 地址必须指向的数据类型:

实例一,获取网卡的IP地址:

#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
        int inet_sock;
        struct ifreq ifr;
        inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
 
        strcpy(ifr.ifr_name, "eth0");
        //SIOCGIFADDR标志代表获取接口地址
        if (ioctl(inet_sock, SIOCGIFADDR, &ifr) <  0)
                perror("ioctl");
        printf("%s\n", inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
        return 0;
}
--------------------- ___________________________________________________
实例二,实现简单ifconfig功能:

/**
 * \file getifstat.c
 * \author  wzj
 * \brief 访问这个struct ifconf 修改,查询状态
 * \version 
 * \note  
 * \date: 2012年08月11日星期六22:55:25
 */ 
#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>
 
#define MAX_INTERFACE    (16)
 
void port_status(unsigned int flags);
 
/* set == 0: do clean , set == 1: do set! */
int set_if_flags(char *pif_name, int sock, int status, 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;
    }
    /* 网口总数 ifc.ifc_len 应该是一个出入参数 */    
    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);    
        /* 获取第n个网口信息 */
        ret = ioctl(fd, SIOCGIFFLAGS, (char*)&buf[if_num]);
        if(ret)
            continue;
 
        /* 获取网口状态 */
        port_status(buf[if_num].ifr_flags);
        
        /* 获取当前网卡的ip地址 */
        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));
 
        /* 获取当前网卡的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],
            (unsigned 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;
}
---------------------

运行结果:

interface num is interface = 2
net device: eth0
is up
is broadcast
is running
IP address is: 
192.168.100.200
54:be:f7:33:57:26

net device: lo
is up
is loop back
is running
IP address is: 
127.0.0.1
00:00:00:00:00:00
---------------------

struct ifreq 学习,现实ifconfig 功能相关推荐

  1. 使用struct ifreq实现ifconfig

    转自:http://blog.csdn.net/joker0910/article/details/7855998 ifconfig是我们查看/设定网口状态常用的命令,其实这个命令就是对一些系统函数的 ...

  2. 《Linux网络接口》---------struct ifreq struct ifconf

    网络接口--------------struct ifconf,struct ifreq 网络相关的ioctl请求的request参数及arg地址必须指向的数据类型如下表所示: 接口 SIOCGIFC ...

  3. struct ifconf和struct ifreq,获取网线插入状态

    struct ifreq 这个结构定义在include/net/if.h,用来配置ip地址,激活接口,配置MTU等接口信息的 struct ifconf 通常是用来保存所有接口信息的 应用 想要获取当 ...

  4. 发那科pmc编程手册_如何学习FANUC PMC功能指令

    在之前的学习课程中我们学习了FANUC内置编程器以及LADDER III软件的操作,知道如何根据报警信息查找报警位置等相关的小技巧,但是呢,对于FANUC PMC的学习还有一部分是对程序的理解.这样才 ...

  5. 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf

    http://blog.csdn.net/windeal3203/article/details/39320605 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq ...

  6. python青年大学习一键提醒功能(团支书、辅导员必备)(一)

    青年大学习一键提醒功能,以我自己的学校为例子. 原因: 上学期做了班级团支书,发现每周都要提醒班上同学做青年大学习,有些同学总是会忘记. 为了让同学们能及时学习新思想,争做新青年!!! 然后就想做一个 ...

  7. 三星推出增加了深度学习和图像处理功能的Exynos 7应用处理器

    文章来源:ATYUN AI平台 三星新推出的中端移动应用处理器Exynos 7 9610具有深度学习和更快的图像处理能力,并将于2018年下半年推出. 三星电子(Samsung Electronics ...

  8. 数据结构__树的学习及宠物店功能实现

    前言 因为工作的需要深刻认识到了数据结构的重要性,故现在重新学习了数据结构相关知识,谨以此文来总结一下树的操作和基本知识 声明:因个人能力有限,本文仅是个人的学习记录笔记,有错误之处还望指出 二叉树的 ...

  9. Matlab 结构体(struct)学习

    http://blog.csdn.net/wangzhix123/article/details/8807737 在matlab里面,struct结构体数组功能强大,在处理很多复杂数据上很有用,刚好最 ...

最新文章

  1. leetcode精选
  2. 【ASM 翻译系列第二弹:ASM 12C 版本新特性】
  3. ISA CMAK 网络访问隔离区
  4. 计算机设计学校,计算机设计制作大赛
  5. 一步步使SSH连接您的github仓库
  6. 【华为云技术分享】Python 中的异常和错误
  7. apache环境下web站点禁止用服务器ip访问
  8. easyswoole数据库连接池_Swoole Redis 连接池的实现
  9. Class 学习 (Es6阮一峰)
  10. 35岁 计算机 学 什么好,35岁一事无成, 想重新学习, 应该学习哪方面的技能?
  11. win这样设置能够让你的耳机/音响拥有更好的效果-音效增强
  12. java 公历 农历_java中怎么把公历日期转成农历日期
  13. 微信公众号申请开通微信支付
  14. 现代化个人博客系统 ModStartBlog v5.7.0 简约纯白主题,富文本大升级
  15. R语言 观测异常值并改进
  16. 宝塔php漏洞,宝塔面板 phpmyadmin 未授权访问漏洞 BUG ip:888/pma的问题分析
  17. ElasticSearch Index Settings
  18. hmcl启动器安装游戏版本失败_HMCL启动器,无法解决的问题(急需解决)
  19. inode网卡被禁用解决方法
  20. Python在2020的新增功能:第1部分

热门文章

  1. 奥威BI—数字化转型首选,以数据驱动企业发展
  2. 如何解决caffe和video-caffe不能使用cudnn8编译的问题
  3. 报错:cannot overwrite directory ‘xxx‘ with non-directory
  4. 关于pytorch安装之后但是没有办法导入pycharm的原因
  5. 《Foreground-Aware Relation Network for Geospatial Object ......Remote Sensing Imagery》
  6. Heroku部署vue项目失败:sh: 1: vue-cli-service: not found
  7. 私库如何区分正式和测试环境独立的库
  8. 并发编程系列---【线程池七大核心参数】
  9. SPOJ FUNPROB - Yanu in Movie theatre
  10. 邮件服务器篇:三大邮件服务器软件“华山论剑”