zoukankan      html  css  js  c++  java
  • ioctl在socket中的一些用法及示例

    -------------------------------------------------------------------
    ioctl在socket中的一些用法及示例

    函数 : ioctl(int fd, int request, void * arg)
    定义 : <sys/ioctl.h>
    功能 : 控制I/O设备, 提供了一种获得设备信息和向设备发送控制参数的手段.
    参数 : int  fd      文件句柄. 用于socket时, 是socket套接字.
           int  request 函数定义的所有操作. 关于socket的操作, 定义在<linux/sockios.h>文件中.
           void *arg    指针的类型依赖于request参数.
     
    以下表格从网上收集了request - arg指针类型的对应关系

    类别

    Request

    说明

    数据类型

     
     

    SIOCATMARK 
    SIOCSPGRP 
    SIOCGPGRP

    是否位于带外标记 
    设置套接口的进程ID 或进程组ID 
    获取套接口的进程ID 或进程组ID

    int 
    int 
    int

     

    FIONBIN 
    FIOASYNC 
    FIONREAD 
    FIOSETOWN 
    FIOGETOWN

    设置/ 清除非阻塞I/O 标志 
    设置/ 清除信号驱动异步I/O 标志 
    获取接收缓存区中的字节数 
    设置文件的进程ID 或进程组ID 
    获取文件的进程ID 或进程组ID

    int 
    int 
    int 
    int 
    int

     

    SIOCGIFCONF 
    SIOCSIFADDR 
    SIOCGIFADDR 
    SIOCSIFFLAGS 
    SIOCGIFFLAGS 
    SIOCSIFDSTADDR 
    SIOCGIFDSTADDR 
    SIOCGIFBRDADDR 
    SIOCSIFBRDADDR 
    SIOCGIFNETMASK 
    SIOCSIFNETMASK 
    SIOCGIFMETRIC 
    SIOCSIFMETRIC 
    SIOCGIFMTU 
    SIOCxxx

    获取所有接口的清单 
    设置接口地址 
    获取接口地址 
    设置接口标志 
    获取接口标志 
    设置点到点地址 
    获取点到点地址 
    获取广播地址 
    设置广播地址 
    获取子网掩码 
    设置子网掩码 
    获取接口的测度 
    设置接口的测度 
    获取接口MTU 
    (还有很多取决于系统的实现)

    struct ifconf 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq 
    struct ifreq

    ARP

    SIOCSARP 
    SIOCGARP 
    SIOCDARP

    创建/ 修改ARP 表项 
    获取ARP 表项 
    删除ARP 表项

    struct arpreq 
    struct arpreq 
    struct arpreq

     

    SIOCADDRT 
    SIOCDELRT

    增加路径 
    删除路径

    struct rtentry 
    struct rtentry

    I_xxx

     

     

    socket最常用到的结构体: struct ifreq 定义在<net/if.h>.(包括struct ifconf/ifr_flags等的定义)

    一、获取

    以下例程通过ioctl获取设备"eth0"的IP/掩码/硬件址

    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    
    #include "net/if.h"
    #include "arpa/inet.h"
    #include "linux/sockios.h"
    
    int main(int argc,char *argv[])
    {
        struct sockaddr_in *addr;
        struct ifreq ifr;
        char*address;
        int sockfd;
    
        char *name = "eth0";
        if( strlen(name) >= IFNAMSIZ)
            printf("device name is error.
    "), exit(0);
            
        strcpy( ifr.ifr_name, name);
            
        sockfd = socket(AF_INET,SOCK_DGRAM,0);
    
        //get inet addr
        if( ioctl( sockfd, SIOCGIFADDR, &ifr) == -1)
            printf("ioctl error.
    "), exit(0);
    
        addr = (struct sockaddr_in *)&(ifr.ifr_addr);
        address = inet_ntoa(addr->sin_addr);
    
        printf("inet addr: %s
    ",address);
    
        //get Mask
        if( ioctl( sockfd, SIOCGIFNETMASK, &ifr) == -1)
            printf("ioctl error.
    "), exit(0);
    
        addr = (struct sockaddr_in *)&ifr.ifr_addr;
        address = inet_ntoa(addr->sin_addr);
    
        printf("Mask: %s
    ",address);
    
        //get HWaddr 
        u_int8_t hd[6];
        if(ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1)
            printf("hwaddr error.
    "), exit(0);
    
        memcpy( hd, ifr.ifr_hwaddr.sa_data, sizeof(hd));
        printf("HWaddr: %02X:%02X:%02X:%02X:%02X:%02X
    ", hd[0], hd[1], hd[2], hd[3], hd[4], hd[5]);
        
        exit(0);
    }
    

      

     

     二、设置

    以下例程设置eth0的IP地址.

    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    
    #include "net/if.h"
    #include "arpa/inet.h"
    #include "linux/sockios.h"
    
    int main(int argc,char *argv[])
    {
        char *dev = "eth0";
        char *ip = "192.168.1.252";
        
        struct ifreq ifr;
        if( strlen(dev) >= IFNAMSIZ)
            printf("device name error.
    "), exit(0);
        else
            strcpy( ifr.ifr_name, dev);
        
        int sockfd = socket(AF_INET,SOCK_DGRAM,0);
    
        //get inet addr
        if( ioctl( sockfd, SIOCGIFADDR, &ifr) == -1)
            printf("ioctl error.
    "), exit(0);
        
        struct sockaddr_in *addr = (struct sockaddr_in *)&(ifr.ifr_addr);
        char * address = inet_ntoa(addr->sin_addr);
    
        printf("current inet addr: %s
    ",address);
    
        //set inet addr
        struct sockaddr_in *p = (struct sockaddr_in *)&(ifr.ifr_addr);
    
        p->sin_family = AF_INET;
        inet_aton( ip, &(p->sin_addr));
    
        if( ioctl( sockfd, SIOCSIFADDR, &ifr) == -1)
         printf("ioctl error.
    "), exit(0);
        else    
            printf("change inet addr to: %s
    ", ip);
    
        //any OS need active dev.
        /*ifr.ifr_flags |= IFF_UP;
        if( ioctl( sockfd, SIOCSIFFLAGS, &ifr) == -1)
            printf("active fault.
    "), exit(0);
        else
            printf("%s[%s] is working...
    ", dev, ip);
        */
            
        close(sockfd);
        exit(1);
        //end
    }
    

      

    屏蔽的代码用于设置IP后, 激活新设置. 多数系统不需要这步操作. 
    而且这步仅作演示. 真实使用的时候, 至少应该
    1. 获取当前ifr.ifr_flags
    2. ifr.ifr_flags |= IFF_UP;

    以上是ioctl的一些示例, 实战中灵活使用、举一反三.

  • 相关阅读:
    java面试和笔试大全
    Struts+Spring整合方式
    四、hibernate实体对象,事务管理,锁
    jdbc知识问答
    关于Struts框架简介
    EJB知识问答
    五、hibernate的Session操作, 查询过滤, 缓存利用, 批量处理
    spring IoC编程实例
    Hibernate简介
    简单的MySQL数据库连接例子
  • 原文地址:https://www.cnblogs.com/oxspirt/p/7478321.html
Copyright © 2011-2022 走看看