[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. bzoj 3781: 小B的询问 分块

    3781: 小B的询问 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 196  Solved: 135[Submit][Status] Descrip ...

  2. Standford CoreNLP--Sentiment Analysis初探

    Stanford CoreNLP功能之一是Sentiment Analysis(情感分析),可以标识出语句的正面或者负面情绪,包括:Positive,Neutral,Negative三个值. 运行有两 ...

  3. UVA 1513 Movie collection

    #include<stdio.h> #include<string.h> #include<stdlib.h> #define N 200010 #define l ...

  4. Java中x+=y和x=x+y两种实现的区别

    先看下边两段代码,各有什么错? 例一: short s1 = 1; s1 = s1 + 1; 例二: short s1 = 1; s1 += 1; 第一段代码无法通过编译,由于 s1+1 在运算时会自 ...

  5. ORA-32001: write to SPFILE requested but no SPFILE specified at startup

    SQL> alter system set  sga_max_size=2048M scope=spfile; alter system set  sga_max_size=2048M scop ...

  6. cssViewer牛逼的chrome插件

    很牛逼,功能很强大.

  7. ashx文件的使用

    转自:http://www.cnblogs.com/Tally/archive/2013/02/19/2916499.html ashx是什么文件 .ashx 文件用于写web handler的..a ...

  8. (转载)1248 - Every derived table must have its own alias

    (转载)http://hi.baidu.com/lylegend13/item/a79f17eb51f5dff7e0a5d43b 1. select count(distinct CName) fro ...

  9. bzoj 2761 [JLOI2011]不重复数字(哈希表)

    2761: [JLOI2011]不重复数字 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3210  Solved: 1186[Submit][Sta ...

  10. Joomla的在线视频播放插件:AllVideos

    一个很好的插件,只需要在文章中插入一条简单的语句就可以实现视频播放,视频可以位于网站服务器上或其他视频网站的. 例如:{f4v}ShaHua-H264{/f4v}   我在使用中只有一个地方觉得需要更 ...