ioctl在socket中的一些用法及示例
定义 : <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.\n"), 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.\n"), exit(0); addr = (struct sockaddr_in *)&(ifr.ifr_addr);
address = inet_ntoa(addr->sin_addr); printf("inet addr: %s\n",address); //get Mask
if( ioctl( sockfd, SIOCGIFNETMASK, &ifr) == -1)
printf("ioctl error.\n"), exit(0); addr = (struct sockaddr_in *)&ifr.ifr_addr;
address = inet_ntoa(addr->sin_addr); printf("Mask: %s\n",address); //get HWaddr
u_int8_t hd[6];
if(ioctl(sockfd, SIOCGIFHWADDR, &ifr) == -1)
printf("hwaddr error.\n"), exit(0); memcpy( hd, ifr.ifr_hwaddr.sa_data, sizeof(hd));
printf("HWaddr: %02X:%02X:%02X:%02X:%02X:%02X\n", 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.\n"), 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.\n"), exit(0); struct sockaddr_in *addr = (struct sockaddr_in *)&(ifr.ifr_addr);
char * address = inet_ntoa(addr->sin_addr); printf("current inet addr: %s\n",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.\n"), exit(0);
else
printf("change inet addr to: %s\n", ip); //any OS need active dev.
/*ifr.ifr_flags |= IFF_UP;
if( ioctl( sockfd, SIOCSIFFLAGS, &ifr) == -1)
printf("active fault.\n"), exit(0);
else
printf("%s[%s] is working...\n", dev, ip);
*/ close(sockfd);
exit(1);
//end
}
屏蔽的代码用于设置IP后, 激活新设置. 多数系统不需要这步操作.
而且这步仅作演示. 真实使用的时候, 至少应该
1. 获取当前ifr.ifr_flags
2. ifr.ifr_flags |= IFF_UP;
以上是ioctl的一些示例, 实战中灵活使用、举一反三.
ioctl在socket中的一些用法及示例的更多相关文章
- Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa)
Socket中常见的几个转换函数(htonl,htons,ntohl,ntohs,inet_addr,inet_ntoa) htonl() htons() ntohl() ntohs()及inet_n ...
- 网络编程------socketserver模块以及socket模块的更多用法.
socketserver模块 内置模块 (其实现原理为并发) socketserver这个模块主要是为了解决: TCP协议中,服务器不能同时连接多个客户端的问题 是处于socket抽象层和应用层之间的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- SQL中distinct的用法
SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- jQuery中Animate进阶用法(一)
jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...
- [转载]js中return的用法
一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return; 在大多数情况下,为事件处理函 ...
- js中this的用法
经过近几周的模拟面试题,我查询了一些资料,今天就来说说,在js中this的用法吧.方法有四:第一,用作全局变量,第二,用作表该对象,第三,用作构造函数,第四,用作call和applay
随机推荐
- scrapy的UA池和代理池
一.下载中间件(Downloader Middlewares) 框架图如下 下载中间件(Downloader Middlewares)位于scrapy引擎和下载器之间的一层组件. - 作用: (1)引 ...
- 设置myeclipse的JSP、HTML的页面编码格式
JSP编码格式: 点击菜单上的window--->preferences 在弹出的对话框中点击MyEclise--->Files and Editors--->JSP, 在Encod ...
- 参加2016华为codecraft编程精英挑战赛后感
2016年4月参加了华为的软件比赛. 关于比赛:给了一道图论的np-hard问题.刚开始完全不知道怎么入手,请教过师兄,自己也琢磨过,没有什么万全的解决方法.注意,这里说的是万全的办法.本科搞算法时候 ...
- Laravel5.1学习笔记16 数据库2 查询构造器(这个不用看,不如用EloquentORM)
Introduction Retrieving Results Aggregates Selects Joins Unions Where Clauses Advanced Where Clauses ...
- android中TextView内容竖向显示
项目中遇到需要textview内容竖着排的需求,如图所示: 网上那些“教程”并不能达到需要的效果,发现有一个属性可以支持这种效果,android:ems=“*”,这是属性表示一行只显示*个字符. 具体 ...
- php用户注册常用检测、写入
// 判断数据库是否已经存在 $check_sql = "select * from user where idNumber='$idNumber'"; $check_query ...
- Java 基础入门随笔(2) JavaSE版——关键字、进制转换、类型转换
1.Java语言-关键字 关键字:被java语言赋予了特殊含义的词,特点是所有的字母都为小写. java涉及到的关键字整理: 用于定义数据类型的关键字 class interface byte sho ...
- (转)学习淘淘商城第二十二课(KindEditor富文本编辑器的使用)
http://blog.csdn.net/u012453843/article/details/70184155 上节课我们一起学习了怎样解决KindEditor富文本编辑器上传图片的浏览器兼容性问题 ...
- C# 扩展类的内置方法
public class A//先定义A类 { } public static class Extension//必须先声明一个静态类,类名随意 { public static int ToInt32 ...
- Requirejs常用配置和应用
requirejs.require方法冲突 如果加载了多个requirejs脚本,每个requirejs会判断是否浏览器已经实现了require和define方法.如果浏览器已经自带require和d ...