linux之ioctl函数解析
[lingyun@localhost ioctl_1]$ ls
ipconfig.c
[lingyun@localhost ioctl_1]$ cat ipconfig.c
/*********************************************************************************
* Copyright: (C) 2013 fulinux<fulinux@sina.com>
* All rights reserved.
*
* Filename: ioctl.c
* Description: This file
*
* Version: 1.0.0(08/01/2013~)
* Author: fulinux <fulinux@sina.com>
* ChangeLog: 1, Release initial version on "08/01/2013 03:21:50 PM"
*
********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
static void usage()
{
printf("usage: ipconfig interface\n");
exit(0);
}
int main(int argc, char **argv)
{
struct sockaddr_in *addr;
struct ifreq ifr;
char *name,*address;
int sockfd;
if(argc != 2)
usage();
else
name = argv[1];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
if(ioctl(sockfd, SIOCGIFADDR,&ifr) == -1)
perror("ioctl error"), exit(1);
addr = (struct sockaddr_in *)&(ifr.ifr_addr);
address = inet_ntoa(addr->sin_addr);
printf("inet addr: %s\n", address);
if(ioctl(sockfd, SIOCGIFBRDADDR, &ifr) == -1)
perror("ioctl error"),exit(1);
addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;
address = inet_ntoa(addr->sin_addr);
printf("broad addr: %s\n", address);
if(ioctl(sockfd, SIOCGIFNETMASK, &ifr) == -1)
perror("ioctl error"), exit(1);
addr = (struct sockaddr_in *)&ifr.ifr_addr;
address = inet_ntoa(addr->sin_addr);
printf("inet mask: %s\n", address);
printf(" ");
exit(0);
}
[lingyun@localhost ioctl_1]$ gcc -o ipconfig ipconfig.c
[lingyun@localhost ioctl_1]$ ./ipconfig eth0
inet addr: 192.168.1.3
broad addr: 192.168.1.255
inet mask: 255.255.255.0
[lingyun@localhost ioctl_1]$
最新文章
- Html之 IFrame使用,注意几点
- Android编码规范04
- Timusoj 1982. Electrification Plan
- oracel数据泵的使用
- redis 在centos下的安装部署
- ALTER---删除字段
- SQL查询数据库表字段值不为空或Null的所有列
- .net core 2.0学习笔记(四):迁移.net framework 工程到.net core
- 通过pyenv和virtualenv创建多版本Python虚拟环境
- leetcode 20 Valid Parentheses 括号匹配
- 在线激活win10、win8/8.1和office2019、2016、2013等的kms激活工具
- VJTools
- c++算法实现(一) - 递归和初始化
- 美团2016秋招笔试B
- sqlalchemy 使用pymysql连接mysql 1366错误
- acitvemq的jvm监控
- centos7安装mantis
- JVM 监控工具 jstack 和 jvisualvm 的使用
- [转] 对vuex的表象理解(笔记)
- ASP.NET Core 如何实现404错误跳转到主页
热门文章
- java通过jni方式获取硬盘序列号(windows,linux)
- ubuntu maven 安装 设置
- Python连接Redis连接配置
- python加密解密
- crontab 中 python(cx_Oracle)脚本执行时需要用户环境变量,怎么办??
- Hibernate save或者persist 后获取主键ID
- ANDROID_MARS学习笔记_S03_005_Geocoder、AsyncTask
- block的是发送信号的线程,又不是处理槽函数的线程
- 转:三十三、Java图形化界面设计——布局管理器之null布局(空布局)——即SWT中的绝对布局
- POJ_3666_Making_the_Grade_(动态规划)