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]$
linux之ioctl函数解析的更多相关文章
- Linux下ioctl函数理解
一. 什么是ioctl ioctl是设备驱动程序中对设备的I/O通道进行管理的函数.所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率.马达的转速等等.它的调用个数如下: i ...
- Linux exec族函数解析
背景 在提到 vfork 函数时,我们提到了这个概念.为了更好地学习与运用,我们对exec族函数进行展开. exec函数族 介绍 有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中 ...
- linux之unlink函数解析
[lingyun@localhost unlink]$ cat unlink.c /********************************************************* ...
- linux之access函数解析
[lingyun@localhost access_1]$ ls access.c 实例一: [lingyun@localhost access_1]$ cat access.c /******** ...
- linux之umask函数解析
[lingyun@localhost umask_1]$ vim umask.c + umask.c ...
- linux之utime函数解析
[lingyun@localhost utime]$ ls hello utime.c world [lingyun@localhost utime]$ cat utime.c /******* ...
- linux之chdir函数解析
[lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c /********************* ...
- linux之getcwd函数解析
[lingyun@localhost getcwd]$ cat getcwd.c /********************************************************** ...
- linux之stat函数解析
[lingyun@localhost stat_1]$ vim stat.c + stat.c ...
随机推荐
- ACMer程序员智力拾遗
浏览网页偶得,遂记录下来,每天进步一点点-- 博客园真是个不错的平台,今天我让师姐也注册了-- 学会分享吧,孩子们-- 一.编程中无穷大量的设置 ...
- 浅析PC机串口通讯流控制
转自浅析PC机串口通讯流控制 我们在串行通讯处理中,常常看到RTS/CTS和XON/XOFF这两个选项,这就是两个流控制的选项,目前流控制主要应用于调制解调器的数据通讯中,但对普通RS232编程,了解 ...
- Swifter初体验;按照惯例,来一个Swift版本的:iOS图片验证码?
不多解释,上图,上代码:代码
- 设计模式之装饰(Decorator)模式 代码详解
/** * Created with IntelliJ IDEA. * User: HYY * Date: 13-10-27 * Time: 下午10:49 * To change this temp ...
- Spring In Action 第4版笔记-第一章-001架构
1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...
- gunicorn启动报错gunicorn.errors.HaltServer
启动gunicorn报错: # gunicorn -b :9008 -w 2 webserver:app 2013-12-10 09:12:58 [29701] [INFO] Starting gun ...
- git stash的使用
https://git-scm.com/docs/git-stash 在git svn的时候使用,提交记录的时候,有部分文件的修改不需要commit. 在向svn进行git svn dcommit的时 ...
- vijosP1903学姐的实习工资
描述 学姐去实习了, 一共实习了N天, 每一天都可以得到实习工资V[i], 这里V[1..N]被看作是整数序列.因为学姐很厉害, 所以V[1..N]是不下降的.也就是说学姐每天的工资只会越来越多, 不 ...
- android 使用android.support.v7 添加ActionBar
当需要在 android 7或更高的版本使用 ActionBar,则可以通过继承ActionBarActivity来实现, 网上有一个开源项目来兼容老版本显示ActionBar的效果:ActionBa ...
- 应用程序域 z
应用程序域(AppDomain)已经不是一个新名词了,只要熟悉.net的都知道它的存在,不过我们还是先一起来重新认识下应用程序域吧,究竟它是何方神圣. 应用程序域 众所周知,进程是代码执行和资源分配的 ...