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

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


函数 : ioctl(int fd, int request, void * arg)
定义 : 
功能 : 控制I/O设备, 提供了一种获得设备信息和向设备发送控制参数的手段.
参数 : int  fd      文件句柄. 用于socket时, 是socket套接字.
       int  request 函数定义的所有操作. 关于socket的操作, 定义在文件中.
       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 定义在.(包括struct ifconf/ifr_flags等的定义)

 

一、获取

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

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4.  
  5. #include "net/if.h"
  6. #include "arpa/inet.h"
  7. #include "linux/sockios.h"
  8.  
  9. int main(int argc,char *argv[])
  10. {
  11.     struct sockaddr_in *addr;
  12.     struct ifreq ifr;
  13.     char*address;
  14.     int sockfd;
  15.  
  16.     char *name = "eth0";
  17.     if( strlen(name) >= IFNAMSIZ)
  18.         printf("device name is error.\n"), exit(0);
  19.         
  20.     strcpy( ifr.ifr_name, name);
  21.         
  22.     sockfd = socket(AF_INET,SOCK_DGRAM,0);
  23.  
  24.     //get inet addr
  25.     if( ioctl( sockfd, SIOCGIFADDR, &ifr) == -1)
  26.         printf("ioctl error.\n"), exit(0);
  27.  
  28.     addr = (struct sockaddr_in *)&(ifr.ifr_addr);
  29.     address = inet_ntoa(addr->sin_addr);
  30.  
  31.     printf("inet addr: %s\n",address);
  32.  
  33.     //get Mask
  34.     if( ioctl( sockfd, SIOCGIFNETMASK, &ifr) == -1)
  35.         printf("ioctl error.\n"), exit(0);
  36.  
  37.     addr = (struct sockaddr_in *)&ifr.ifr_addr;
  38.     address = inet_ntoa(addr->sin_addr);
  39.  
  40.     printf("Mask: %s\n",address);
  41.  
  42.     //get HWaddr 
  43.     u_int8_t hd[6];
  44.     if(ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1)
  45.         printf("hwaddr error.\n"), exit(0);
  46.  
  47.     memcpy( hd, ifr.ifr_hwaddr.sa_data, sizeof(hd));
  48.     printf("HWaddr: %02X:%02X:%02X:%02X:%02X:%02X\n", hd[0], hd[1], hd[2], hd[3], hd[4], hd[5]);
  49.     
  50.     exit(0);
  51. }


 二、设置

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

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4.  
  5. #include "net/if.h"
  6. #include "arpa/inet.h"
  7. #include "linux/sockios.h"
  8.  
  9. int main(int argc,char *argv[])
  10. {
  11.     char *dev = "eth0";
  12.     char *ip = "192.168.1.252";
  13.     
  14.     struct ifreq ifr;
  15.     if( strlen(dev) >= IFNAMSIZ)
  16.         printf("device name error.\n"), exit(0);
  17.     else
  18.         strcpy( ifr.ifr_name, dev);
  19.     
  20.     int sockfd = socket(AF_INET,SOCK_DGRAM,0);
  21.  
  22.     //get inet addr
  23.     if( ioctl( sockfd, SIOCGIFADDR, &ifr) == -1)
  24.         printf("ioctl error.\n"), exit(0);
  25.     
  26.     struct sockaddr_in *addr = (struct sockaddr_in *)&(ifr.ifr_addr);
  27.     char * address = inet_ntoa(addr->sin_addr);
  28.  
  29.     printf("current inet addr: %s\n",address);
  30.  
  31.     //set inet addr
  32.     struct sockaddr_in *= (struct sockaddr_in *)&(ifr.ifr_addr);
  33.  
  34.     p->sin_family = AF_INET;
  35.     inet_aton( ip, &(p->sin_addr));
  36.  
  37.     if( ioctl( sockfd, SIOCSIFADDR, &ifr) == -1)
  38.      printf("ioctl error.\n"), exit(0);
  39.     else    
  40.         printf("change inet addr to: %s\n", ip);
  41.  
  42.     //any OS need active dev.
  43.     /*ifr.ifr_flags |= IFF_UP;
  44.     if( ioctl( sockfd, SIOCSIFFLAGS, &ifr) == -1)
  45.         printf("active fault.\n"), exit(0);
  46.     else
  47.         printf("%s[%s] is working...\n", dev, ip);
  48.     */
  49.         
  50.     close(sockfd);
  51.     exit(1);
  52.     //end
  53. }

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

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

猜你喜欢

转载自blog.csdn.net/xiaolei251990/article/details/83036726