[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. Accessing Scoped Variables

    To permit the JSP page to access the data, the servlet needs to use setAttribute to store the data i ...

  2. Android 向系统发送一条短信

    s //向系统写一条短信 ContentValues contentValues = new ContentValues(); contentValues.put("body",& ...

  3. 如何配置svn服务器(通过VisualServer服务器)

    如果你已经安装好了VisualServer服务器,现在让我们一起来配置svn服务器吧

  4. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  5. 【Xamarin开发 Android 系列 2】VS2015跨平台开发的几种方式

    原文:[Xamarin开发 Android 系列 2]VS2015跨平台开发的几种方式 在微软Build大会上,微软宣布在VS2015中支持三种方式进行跨平台的开发. 1. Xamarin 2. Co ...

  6. Rewriting History with Git Rebase

    http://code.tutsplus.com/tutorials/rewriting-history-with-git-rebase--cms-23191 1. Rebasing for a Li ...

  7. JBPM4.4部署到tomcat6异常解决办法

    java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.ser ...

  8. 数据表列名与数据库关键字冲突,在Hibernate下的解决办法

    设计了一个数据库,某一个列名字是key,这与mysql数据库关键字冲突了,Hibernate下save总是报错. 在mysql命令中,解决办法很简单,只需要将关键字key用引号括起来就好了. 在Hib ...

  9. (转载)PHP获取客户端、PHP获取服务器相关信息

    (转载)http://www.php100.com/html/webkaifa/PHP/PHP/2009/1027/3446.html 服务器变量 $_SERVER 详解: 1.$_SESSION[' ...

  10. vi find和grep

    linux grep和find命令 linux中强大且常用命令:find.grep 源码搜索:find . -name "*.xml" | xargs grep -Hna &quo ...