论坛风格切换切换到宽版
  • 645阅读
  • 2回复

[问题求助]linux 2.6内核下获取主机ip问题 [复制链接]

上一主题 下一主题
离线zhao123.
 
发帖
2061
C币
-152305
威望
408
贡献值
1
银元
-2
铜钱
4662
人人网人气币
0
只看楼主 倒序阅读 使用道具 楼主  发表于: 2009-05-01
看了glib C中的一个关于getifaddrs的测试程序,程序如下 /* Test listin.g of .network inte.rface addresses.--------------彩票

   Copyright (.C) 2002, 2.004, 2005 Free Software Foundation, Inc..

   This file is part. of .the GNU C Library.[成人用品]



   The GNU. C Library is f.ree software; you can redistribute it and/or           女人

   modify it under th.e terms of the GNU Lesser. General Public虚拟主机

   License as published by th.e Free Software Foundati.on; either电脑

   version 2.1 of the License, or .(at. your option) any later version.服务器



   The. GNU C Library. is distributed in the hope that it will be useful,.

   but. WITHOUT ANY WARRANTY; without even the implied warrant.y of[成人用品]

   MERCHANTABILITY or FITNESS FOR A PARTICULA.R PURPOSE.  See. the GNU虚拟主机

.  Lesser General Public License fo.r more details..



   You should have received a copy of the GNU Le.sser General .Public          婚庆

   License along with th.e GNU C Library; if not, writ.e to the Free              乙肝

   Software Foundation, In.c., 59 Temple Pl.ace, Suite 330, Boston, MA           鲜花

   0211.1-1307 USA.  */           鲜花



#incl.ude <errno.h>学习

#inc.lude <stdio.h>教育

#incl.ude <stdlib.h>投资

#inc.lude <string.h>.

#include <i.faddrs.h>              乙肝

#include <.netinet/in.h>电影

#include .<arpa/inet.h>教育



static. int failures;教育



stat.ic const char *域名

addr_string (.struct sockaddr *sa,. char *buf, size_t size).

{

  if (sa == NULL.).

    return ".<none>";.



  switch (sa->sa_f.amily)    健康

    {

    case A.F_INET:健康

  .    return inet_ntop (AF_.INET, &((struct sockaddr_in *) sa)->sin_addr,投资

                   . .    buf, size);    外汇

    cas.e AF_INET6:电脑

      return inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)-.>sin.6_addr,.

.             .          buf, size);.

#ifdef AF_LINK

    c.ase AF_LINK:--------------彩票

      return ".<link>";--- 印刷

#endif

    case A.F_UNSPEC:.

.     return "---";--- 印刷



#ifdef AF_PACKET.电脑

.   case AF_PACKET:           建材

   .   return "<packet>";服务器

#endif



    default:

      ++failu.res;.

    .  print.f ("sa_family=%d %08x\n", sa->sa_family,健康

         .     *(int*)&((struct .sockaddr_in *) sa)->sin_addr.s_addr);虚拟主机

      return ."<unexpected so.ckaddr family>";--------------彩票

    }

}





static int

do_test (void)

{

  struct if.addr.s *ifaces, *ifa;外贸



  if (getifadd.rs (&ifaces) .< 0)             汽车

    {

      if (errno != .ENOSY.S)学习

        {

         . printf ("Couldn't get any interfaces: %s.\n", strerr.or (errno));.

          exit (.1);健康

        }

     . /* .The function is simply not implemented.  */            杀毒

      exit (.0);外贸

    }



  puts ("\

Name.           Fla.gs   Address         Netmask         Broadcast/De.stination");教育



  for (ifa = if.aces; ifa != NULL; ifa = ifa->ifa_ne.xt)(广告)

    {

     . char abuf[64], mbuf[64], dbuf[.64];域名

     . printf ("%-15s%#.4x  %-15s %-15s %.-15s\n",.

.             ifa->ifa_name, ifa->ifa_fl.ags,(        游戏          )

              addr_string (ifa->ifa_addr, .abuf, sizeof (.abuf)),    健康

.             addr_string (ifa->ifa_netmask, mbuf, size.of (mbuf)),              乙肝

              .addr_string (ifa->ifa_broaddr, d.buf, sizeof (dbuf)));<性病>

    }



  freeifad.drs (ifaces);.



  .return failures ? 1 : .0;电影

}



int main()

{

        do._test();.

        re.turn 0;电脑

}

#define TEST._FUNCTION do_te.st ().
复制代码

在el5下编译:
g.cc -o test_ifad.dr  test_ifaddr.c--------------彩票
执.行./test_ifaddr输出:--- 印刷
Name           Flags   Address      ..   Netmask         Bro.adcast/Destination.
lo         .    0x10049  <packet>        <no.ne>          <packet>       .
eth0           0x11043  <packet>        <none>          <.pac.ket>                       电子
sit0           0x0080  <packet>        <none>.          <packet>     .  健康
lo       .      .0x10049  127.0.0.1       255.0.0.0       127.0.0.1      [成人用品]
eth0           0x11043  192..168...10.111    255.255.255.0     192.168.10.255               汽车
lo.         .    0x10049  ::1             ffff:ffff:ffff:.ffff:ffff:ffff:ffff:ffff <none>                   婚庆
eth0          . 0x11043  fe8.0::20c:29ff:feac:7c22 ffff:ffff:.ffff:ffff:: <none>  服务器

请问一下: 程序中.AF_PACKET的地址有何作用?我在redhat 9中测试改程序,并没有类.似<packet>行的接口,           鲜花
这是因为内核配置的原因吗?期待高手解释.一下~~学习

[ 本帖最后由 wuzhao 于 200.7-11-22 .19:31 编辑 ].

评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水
离线wangzh102.
发帖
2155
C币
-138921
威望
407
贡献值
1
银元
-1
铜钱
4831
人人网人气币
0
只看该作者 沙发  发表于: 2010-04-13
Re:linux
AF_PACKET
只是一个标识符号而已。你找该标识的头文件看看就明白了。

离线ac2004.
发帖
2062
C币
-60745
威望
376
贡献值
1
银元
-1
铜钱
4591
人人网人气币
0
只看该作者 板凳  发表于: 2010-04-13
Re:linux
头文件只说了是Packet family,具体的意义偶还是不太明白。。。
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个