0

I saw an example code that get the mac and ipv4 address of an interface. I changed it a bit trying to get the ipv6 address of that interface but the program fail saying "27:13: error: incompatible types when assigning to type ‘u_int32_t’ from type ‘struct libnet_in6_addr"

#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;
}

I don't know what to use to store ipv6 address so I use u_int32_t ,I tried u_int64_t it's not working. I want to know if come across such kind of basic problem again where should I find solution. I can't find examples concerning ipv6 address.

Allan Ruin
  • 5,229
  • 7
  • 37
  • 42

1 Answers1

0

First, I don't know libnet.

That said, it is possible to derive the answer out of your code.

If

u_int32_t ipv6_addr;
ipv6_addr = libnet_get_ipaddr6(l);

fails with

error: incompatible types when assigning to type ‘u_int32_t’ from type ‘struct libnet_in6_addr’

, it is possible to say that ipv6_addr should have the type struct libnet_in6_addr.

Furthermore,

if ( ip_addr != -1 )
    printf("IPv6 address: %s\n", libnet_addr2name6(ip_addr,\
                        LIBNET_DONT_RESOLVE));

should be changed by

  • replacing ip_addr with ip_addr6
  • changing (ip_addr != -1) to whatever libnet recommends in its documentation.

in order to know if an IPv6 address was found.

BTW: There could be more thatn 1 IPv6 address.

glglgl
  • 89,107
  • 13
  • 149
  • 217