函数 : 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 |
说明 |
数据类型 |
套 |
SIOCATMARK |
是否位于带外标记 |
int |
文 |
FIONBIN |
设置/ 清除非阻塞I/O 标志 |
int |
接 |
SIOCGIFCONF |
获取所有接口的清单 |
struct ifconf |
ARP |
SIOCSARP |
创建/ 修改ARP 表项 |
struct arpreq |
路 |
SIOCADDRT |
增加路径 |
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的一些示例, 实战中灵活使用、举一反三.