[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函数解析的更多相关文章

  1. Linux下ioctl函数理解

    一. 什么是ioctl ioctl是设备驱动程序中对设备的I/O通道进行管理的函数.所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率.马达的转速等等.它的调用个数如下: i ...

  2. Linux exec族函数解析

    背景 在提到 vfork 函数时,我们提到了这个概念.为了更好地学习与运用,我们对exec族函数进行展开. exec函数族 介绍 有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中 ...

  3. linux之unlink函数解析

    [lingyun@localhost unlink]$ cat unlink.c  /********************************************************* ...

  4. linux之access函数解析

    [lingyun@localhost access_1]$ ls access.c 实例一: [lingyun@localhost access_1]$ cat access.c  /******** ...

  5. linux之umask函数解析

    [lingyun@localhost umask_1]$ vim umask.c  + umask.c                                                 ...

  6. linux之utime函数解析

    [lingyun@localhost utime]$ ls hello  utime.c  world [lingyun@localhost utime]$ cat utime.c  /******* ...

  7. linux之chdir函数解析

    [lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c  /********************* ...

  8. linux之getcwd函数解析

    [lingyun@localhost getcwd]$ cat getcwd.c /********************************************************** ...

  9. linux之stat函数解析

    [lingyun@localhost stat_1]$ vim stat.c  + stat.c                                                     ...

随机推荐

  1. redis百度百科和维基百科知识总结:

    1. 百度百科知识总结: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis ...

  2. vc6静态库的生成和调用

    转自vc6静态库的生成和调用 1.静态库的生成: 在vc6.0++中Ctrl+N选择Projects下的Win32 Static Library,Project name:SumLib,点击OK,下一 ...

  3. OneAlert 入门(二)——事件分析

    OneAlert 是国内首个 SaaS 模式的云告警平台,集成国内外主流监控/支撑系统,实现一个平台上集中处理所有 IT 事件,提升 IT 可靠性.有了 OneAlert,你可以更快更合理地为事件划分 ...

  4. ubuntu service

    http://blog.chinaunix.net/uid-21528208-id-2399656.html

  5. OA学习笔记-006-SPRING2.5与hibernate3.5整合

    一.为什么要整合 1,管理SessionFactory实例(只需要一个) 2,声明式事务管理 spirng的作用 IOC 管理对象.. AOP 事务管理.. 二.整合步骤 1.整合sessionFac ...

  6. Android 颜色Color

    Android中使用4个数字来表示颜色,分别是alpha.红(red).绿(green).蓝(blue)四个颜色值(ARGB).每个数字取值0-255,因此一个颜色可以用一个整数来表示.为了运行效率, ...

  7. 12.URL重写

    为什么要URL重写?1.有利于SEO(搜索引擎优化),带参数的RUL权重较低.2.地址看起来更正规,推广uid. 如我们一般在访问网页是会带参数,http://aaa.com/view.htm?id= ...

  8. 【HDOJ】1198 Farm Irrigation

    其实就是并查集,写麻烦了,同样的代码第一次提交wa了,第二次就过了. #include <stdio.h> #include <string.h> #define MAXNUM ...

  9. wcf异常汇总

    1.确保客户端可以接收到服务端的异常 2.部署wcf出错,http错误404.3 3.无法自动调试 未能调试远程过程.这通常说明未在服务器上启用调试 WCF 托管在IIS上 4.ChannelFact ...

  10. c#中使用SESSION需要注意的几个问题

    C#的SESSION和其它程序中的SESSSION可能有一点的不同,下面讲下哪飞网程序员遇到的一个地方使用SESSION的问题.希望对大家有所帮助 一.在页面中用SESSION,存值session[& ...