首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用libnet获取接口的ipv6地址?

如何使用libnet获取接口的ipv6地址?
EN

Stack Overflow用户
提问于 2012-03-27 21:09:59
回答 1查看 556关注 0票数 0

我看到了一个获取接口的mac和ipv4地址的示例代码。我稍微修改了一下,试图获得该接口的ipv6地址,但程序失败了,提示"27:13: error: incompatible when assigning to type‘u_int32_t’from type‘struct libnet_in6_addr“

代码语言:javascript
复制
#include <stdlib.h>
#include <libnet.h>
#include <stdint.h>

int main() {

  libnet_t *l;  /* libnet context */
  char errbuf[LIBNET_ERRBUF_SIZE];
  u_int32_t ip_addr;
  struct libnet_ether_addr *mac_addr;

  l = libnet_init(LIBNET_RAW4, NULL, errbuf);
  if ( l == NULL ) {
    fprintf(stderr, "libnet_init() failed: %s\n", errbuf);
    exit(EXIT_FAILURE);
  }

  ip_addr = libnet_get_ipaddr4(l);
  if ( ip_addr != -1 )
    printf("IP address: %s\n", libnet_addr2name4(ip_addr,\
                            LIBNET_DONT_RESOLVE));
  else
    fprintf(stderr, "Couldn't get own IP address: %s\n",\
                    libnet_geterror(l));

  u_int32_t ipv6_addr;
  ipv6_addr = libnet_get_ipaddr6(l);
  if ( ip_addr != -1 )
    printf("IPv6 address: %s\n", libnet_addr2name6(ip_addr,\
                            LIBNET_DONT_RESOLVE));
  else
    fprintf(stderr, "Couldn't get own IPv6 address: %s\n",\
                    libnet_geterror(l));

  mac_addr = libnet_get_hwaddr(l);
  if ( mac_addr != NULL )
    printf("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",\
        mac_addr->ether_addr_octet[0],\
        mac_addr->ether_addr_octet[1],\
        mac_addr->ether_addr_octet[2],\
        mac_addr->ether_addr_octet[3],\
        mac_addr->ether_addr_octet[4],\
        mac_addr->ether_addr_octet[5]);
  else
    fprintf(stderr, "Couldn't get own MAC address: %s\n",\
                    libnet_geterror(l));

  libnet_destroy(l);
  return 0;
}

我不知道用什么来存储ipv6地址,所以我使用了u_int32_t,我尝试了u_int64_t,它不起作用。我想知道如果再次遇到这样的基本问题,我应该在哪里找到解决方案。我找不到有关ipv6地址的示例。

EN

回答 1

Stack Overflow用户

发布于 2012-03-27 22:28:01

首先,我不了解libnet。

也就是说,可以从您的代码中推导出答案。

如果

代码语言:javascript
复制
u_int32_t ipv6_addr;
ipv6_addr = libnet_get_ipaddr6(l);

失败,错误为

代码语言:javascript
复制
error: incompatible types when assigning to type ‘u_int32_t’ from type ‘struct libnet_in6_addr’

,则可以说ipv6_addr应该具有struct libnet_in6_addr类型。

此外,

代码语言:javascript
复制
if ( ip_addr != -1 )
    printf("IPv6 address: %s\n", libnet_addr2name6(ip_addr,\
                        LIBNET_DONT_RESOLVE));

应由以下人员更改

无论在其documentation.中推荐什么,

  • 都会用ip_addr6
  • changing (ip_addr != -1)替换ip_addr

以便知道是否找到了IPv6地址。

顺便说一句:可能有1个以上的IPv6地址。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9890187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档