⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 87.htm

📁 unix高级编程原吗
💻 HTM
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>123</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center">               ● UNIX网络编程                       (BM: clown)                </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p   align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="54.htm">上一层</a>][<a href="88.htm">下一篇</a>]
<hr><p align="left"><small>发信人: clown (梧桐叶), 信区: UNP <br>

标  题: 得到网卡基地址的程序 <br>

发信站: UNIX编程 (2001年09月26日21:00:00 星期三), 站内信件 <br>

  <br>

#include <stdio.h> <br>

#include <stdlib.h> <br>

#include <sys/ioctl.h> <br>

#include <sys/types.h> <br>

#include <sys/socket.h> <br>

#include <net/if.h> <br>

#include <netinet/in.h> <br>

#include <arpa/inet.h> <br>

#include <string.h> <br>

  <br>

struct ifconf ifc; <br>

short GetIfconf(); <br>

  <br>

//get the ethernet's ioaddr <br>

  <br>

int main() <br>

{ <br>

    struct ifreq *ifr; <br>

    int sockfd; <br>



    void *ptr; <br>

    unsigned long ioaddr; <br>

    if(GetIfconf() == -1) <br>

    //Warning: the result between kernel 2.0 and 2.2 is not the same!!! <br>

    //In kernel 2.0, it doesn't display the netcard name whose status <br>

    //is down <br>

        return -1; <br>

    sockfd=socket(AF_INET, SOCK_DGRAM, 0); <br>

    for(ptr=ifc.ifc_buf; (caddr_t)ptr < ifc.ifc_buf+ifc.ifc_len; ){ <br>

        ifr=(struct ifreq *)ptr; <br>

        //for the next one in buffer <br>

        ptr += sizeof(ifr->ifr_name)+sizeof(struct sockaddr); <br>

        //get the socket interface flag <br>

        ioctl(sockfd, SIOCGIFMAP, ifr); <br>

            ioaddr = ifr->ifr_map.base_addr; <br>

        printf("%s base addr: 0x%x %ul\n", ifr->ifr_name, ioaddr, ioaddr); <br>

    } <br>

    return 0; <br>

} <br>

  <br>

//get the value of struct ifconf ifc <br>

//global variable: struct ifconf ifc <br>



short GetIfconf() <br>

{ <br>

    int sockfd; <br>

    int len, lastlen; <br>

    void *buf; <br>

  <br>

    sockfd=socket(AF_INET, SOCK_DGRAM, 0); <br>

    lastlen=0; <br>

    len=10*sizeof(struct ifconf); <br>

    //size of struct ifconf: 8 <br>

    //size of struct ifreq: 32 <br>

    for(;;){ <br>

        buf=malloc(len); <br>

        ifc.ifc_len=len; <br>

        ifc.ifc_buf=buf; <br>

        if(ioctl(sockfd, SIOCGIFCONF, &ifc) <0) { <br>

            free(buf); <br>

            return -1; <br>

        } <br>

        else{ <br>

            if(ifc.ifc_len == lastlen) <br>

                break;  //success, the only point to exit for() <br>



            lastlen=ifc.ifc_len; <br>

        } <br>

        len += 10*sizeof(struct ifconf); <br>

        free(buf); <br>

    } <br>

    return 0; <br>

} <br>

  <br>

结果: <br>

lo base addr: 0x0 0l <br>

eth0 base addr: 0xe800 59392l <br>

eth1 base addr: 0xe400 58368l <br>

  <br>

-- <br>

易朽的是生命,似那转瞬即谢的花朵;然而永存的,是对未来的渴望, <br>

是那生生世世传递下来的,不朽的,生的激情。每一朵勇敢开放的花, <br>

都是一个死亡唇边的微笑。 <br>

※ 来源:·UNIX编程 www.tiaozhan.com/unixbbs/·[FROM: 202.114.1.61] <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="54.htm">上一层</a>][<a href="88.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -