[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. python学习之---函数进阶

    一,递归函数: 做程序应该都知道,在一个函数的内部还可以调用其它函数,这叫函数的调用,但是有一种特殊的情况,在一个函数内部对自身函数的调用,我们成这为函数的递归调用. 在此,使用一个家喻户晓的例子来演 ...

  2. NOI冲刺计划

    省选过了,剩下大概是NOI冲刺了吧.中间还有一大堆诸如会考,CTSC,APIO等东西. 最近先不急着天天刷八中了吧,多在不同网站见一些题,然后再着重提高一下代码准确性.重点把DP这个板块多练习一下,八 ...

  3. GITLAB的版本回退(非命令行)

    今天遇到小韩的问题,大约解决如下:

  4. 【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)

    The Stable Marriage Problem   Description The stable marriage problem consists of matching members o ...

  5. iOS开发控制器之间传值的几种小方法

    在IOS开发中或面试中,经常会遇到,两个或者多个控制器之间传值的问题 ,总结的集中方法仅供参考! 问题 :将B控制器中的textField 输入内容,传到A控制器中的label上显示出来,如何传值? ...

  6. [topcoder]IncreasingSubsequences

    http://community.topcoder.com/stat?c=problem_statement&pm=7753&rd=10672 http://community.top ...

  7. Android开源项目发现---Layout 篇(持续更新)

    1. achartengine 强大的图标绘制工具 支持折线图.面积图.散点图.时间图.柱状图.条图.饼图.气泡图.圆环图.范围(高至低)条形图.拨号图/表.立方线图及各种图的结合 项目地址:http ...

  8. Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView

    ExpandableListView: List中的每一项可以展开收缩. 一种伸缩式的ListView. android:cacheColorHint="#00000000" 这个 ...

  9. AHOI2006文本编辑器editor

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1885  Solved: 683[Submit ...

  10. 【转】Android bluetooth介绍(二): android blueZ蓝牙代码架构及其uart 到rfcomm流程

    原文网址:http://blog.sina.com.cn/s/blog_602c72c50102uzoj.html 关键词:蓝牙blueZ  UART  HCI_UART H4  HCI  L2CAP ...