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

📄 450.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>apue</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="320.htm">上一层</a>][<a href="451.htm">下一篇</a>]
<hr><p align="left"><small> scz 于 2001-7-16 11:24:47 加贴在 绿盟科技论坛(bbs.nsfocus.com)--UNIX系统安全: <br>

#include <stdio.h> <br>

#include <stdlib.h> <br>

#include <string.h> <br>

#include <errno.h> <br>

#include <unistd.h> <br>

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

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

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

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

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

#define inaddrr( x ) ( *( struct in_addr * )&ifr->x[ sizeof( sa.sin_port ) ] ) <br>

#define IFRSIZE      ( ( int )( size * sizeof( struct ifreq ) ) ) <br>

int main ( void ) <br>

{ <br>

    unsigned char *    u; <br>

    int                sockfd, size = 1; <br>

    struct ifreq *     ifr; <br>

    struct ifconf      ifc; <br>

    struct sockaddr_in sa; <br>

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

    { <br>

        fprintf( stderr, "Cannot open socket.\n" ); <br>



        exit( EXIT_FAILURE ); <br>

    } <br>

    ifc.ifc_req = NULL; <br>

    do <br>

    { <br>

        ++size; <br>

        /* realloc buffer size until no overflow occurs  */ <br>

        if ( NULL == ( ifc.ifc_req = realloc( ifc.ifc_req, IFRSIZE ) ) ) <br>

        { <br>

            fprintf( stderr, "Out of memory.\n" ); <br>

            exit( EXIT_FAILURE ); <br>

        } <br>

        ifc.ifc_len = IFRSIZE; <br>

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

        { <br>

            perror( "ioctl SIOCFIFCONF" ); <br>

            exit( EXIT_FAILURE ); <br>

        } <br>

    } while ( IFRSIZE <= ifc.ifc_len ); <br>

    ifr = ifc.ifc_req; <br>

    for ( ; ( char * )ifr < ( char * )ifc.ifc_req + ifc.ifc_len; ++ifr ) <br>

    { <br>

    { <br>

        if ( ifr->ifr_addr.sa_data == ( ifr + 1 )->ifr_addr.sa_data ) <br>

        { <br>

            continue;  /* duplicate, skip it */ <br>

        } <br>

        if ( ioctl( sockfd, SIOCGIFFLAGS, ifr ) ) <br>

        { <br>

            continue;  /* failed to get flags, skip it */ <br>

        } <br>

        printf( "Interface:  %s\n", ifr->ifr_name ); <br>

        if ( 0 == ioctl( sockfd, SIOCGIFHWADDR, ifr ) ) <br>

        { <br>

            switch ( ifr->ifr_hwaddr.sa_family ) <br>

            { <br>

            case ARPHRD_NETROM: <br>

            case ARPHRD_ETHER: <br>

            case ARPHRD_PPP: <br>

            case ARPHRD_EETHER: <br>

            case ARPHRD_IEEE802: <br>

                break; <br>

            default: <br>

                printf( "\n" ); <br>

                continue; <br>



            } <br>

            u = ( unsigned char * )&ifr->ifr_addr.sa_data; <br>

            if ( u[0] + u[1] + u[2] + u[3] + u[4] + u[5] ) <br>

            { <br>

                printf( "HW Address: %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n", <br>

                        u[0], u[1], u[2], u[3], u[4], u[5] ); <br>

            } <br>

        } <br>

        printf( "\n" ); <br>

    }  /* end of for */ <br>

    close( sockfd ); <br>

    return( EXIT_SUCCESS ); <br>

} <br>

</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="320.htm">上一层</a>][<a href="451.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 + -