以下有几个方法可以获取IPV6地址:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <net/if.h>
#define MAX_NUM 64
typedef struct
{
unsigned char ipv6_addr[16];
unsigned char prefix_len;
}IfInfo_t;
int main(int argc, char **argv)
{
int ret = 0;
IfInfo_t ifInfo[MAX_NUM];
FILE* fp = NULL;
char cmd[512] = {0};
uint8_t mac[6] = {0};
char *pmatch = NULL;
char *pend = NULL;
char *p_prefix = NULL;
unsigned char ipv6_tmp[INET6_ADDRSTRLEN], ipv6_addr[16], prefix_len;
int i = 0;
do
{
if(argc < 2 )
{
printf("Usage: ./x ethx\r\n");
return 0;
}
snprintf(cmd, sizeof(cmd), "ifconfig %s", argv[1]);
//printf(" Execute \"%s\"!\n", cmd);
fp = popen(cmd, "r");
if (fp == NULL)
{
printf(" Error: Can't execute \"%s\"!\n", cmd);
ret = -1;
break;
}
while(fgets(cmd, sizeof(cmd), fp) != NULL)
{
//printf("%s", cmd);
if ((pmatch = strstr(cmd, "HWaddr ")) != NULL)
{
if (sscanf(pmatch, "HWaddr %x:%x:%x:%x:%x: