獲取本機Ipv6的幾種方法

 以下有幾個方法可以獲取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:%x", mac, mac+1, mac+2, mac+3, mac+4, mac+5) != 6)
                {
                    ret = -1;
                    break;
                }
				
				printf("\n  HWaddr:%02X:%02X:%02X:%02X:%02X:%02X\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);            

            }

            if ((pmatch = strstr(cmd, "inet6 addr: ")) != NULL)
            {
                pend=strchr(pmatch,':');         /*strchr查找字符第一次出現的位置*/
				pend+=2;
        		p_prefix=strchr(pend,'/');

				memset(ipv6_tmp, 0, sizeof(ipv6_tmp));	/*用之前先用memset清空*/
				strncpy(ipv6_tmp, pend, p_prefix - pend);	   /*strncpy字符串拷貝,注意每個指針地址*/
				
				if(sscanf(p_prefix, "/%d", &prefix_len) != 1)
				{
					ret = -1;
					break;
				}						
				inet_pton(AF_INET6, ipv6_tmp, ipv6_addr);
				memcpy(ifInfo[i].ipv6_addr, ipv6_addr, 16);
				ifInfo[i].prefix_len = prefix_len;
				i++;
            }		
		
			memset(cmd, 0, sizeof(cmd));
        }
        
        pclose(fp);

        if(ret != 0)
        {
            break;
        }
		
		
		
    }while(0);
	
		while(i--)
		{
			printf("\n  IPv6 address:%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x prefix_len %u\n\n",
					ifInfo[i].ipv6_addr[0],  ifInfo[i].ipv6_addr[1],   ifInfo[i].ipv6_addr[2],  ifInfo[i].ipv6_addr[3],  ifInfo[i].ipv6_addr[4], 
					ifInfo[i].ipv6_addr[5],  ifInfo[i].ipv6_addr[6],   ifInfo[i].ipv6_addr[7],  ifInfo[i].ipv6_addr[8],  ifInfo[i].ipv6_addr[9], 
					ifInfo[i].ipv6_addr[10], ifInfo[i].ipv6_addr[11],  ifInfo[i].ipv6_addr[12], ifInfo[i].ipv6_addr[13], ifInfo[i].ipv6_addr[14], 
					ifInfo[i].ipv6_addr[15], ifInfo[i].prefix_len);				
		}


    return ret;
}

運行結果:

[root@localhost buildroot]# ./get_host_ipv6 eth0.100

  HWaddr:00:0C:29:A5:23:E2

  IPv6 address:20 01 00 00 00 00 00 00 00 00 00 00 00 00 12 34 prefix_len 0


  IPv6 address:fe 80 00 00 00 00 00 00 02 0c 29 ff fe a5 23 e2 prefix_len 64


  IPv6 address:20 01 00 00 00 00 00 00 00 00 00 00 00 00 12 35 prefix_len 98

 

 

以下爲轉載:

  原文地址:https://blog.csdn.net/u013889753/article/details/32315625

mynet.h

#include  <netinet/in.h>
 
#include  <sys/param.h>
#include  <sys/stat.h>
#include  <sys/ioctl.h>
#include  <sys/socket.h>
#include  <sys/socket.h>
#include  <sys/time.h>
#include  <sys/file.h>
#include  <sys/types.h>
 
#include   <netinet/ip.h>
#include  <arpa/ftp.h>
#include  <arpa/inet.h>
#include  <arpa/telnet.h>
 
#include  <stdio.h>
#include  <signal.h>
#include  <string.h>
#include  <net/if.h>
#include  <stdlib.h>
#include  <unistd.h>
#include  <error.h>
#include  <netdb.h>
#include  <ifaddrs.h> 
#include  <errno.h> 

方法一:byeth0getipv6.c

#include "mynet.h"
      
int  main(int argc, char *argv[]){  
        struct ifaddrs *ifaddr, *ifa;  
        int family, s;  
        char host[NI_MAXHOST];  
	char* ifaname;     
 
	printf("NI_MAXHOST:%d\n",NI_MAXHOST);	 
        if (getifaddrs(&ifaddr) == -1) {  
            printf("getifaddrs\n");  
            return -1;  
        }  
      
        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {  
            if (ifa->ifa_addr == NULL)  
                continue;  
      
            family = ifa->ifa_addr->sa_family;  
            ifaname = ifa->ifa_name; 
            if (family == AF_INET6 && memcmp(ifaname, "eth0", 4) == 0 ) {
                s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),  
                        host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);  
                if (s != 0) {  
                    printf("getnameinfo() failed: %s\n", gai_strerror(s));  
                    return -1;  
                }
		if (strchr(host, '%') == NULL){
			printf("%s IPv6 addr:%s\n", ifa->ifa_name, host);
		}
	    }else{
		continue;
  	    }      
        }  
      
        freeifaddrs(ifaddr);
	return 0;
}  

方法二:bymulgetipv6.c

#include "mynet.h"
 
int main(){
	const char* muladdr = "ff0e::2";
	int sockfd;
	int len;
	int on = 1;
	struct sockaddr_in6 iface_out;
	struct sockaddr_in6 remote;
	char address[45];
	memset(&remote, 0, sizeof( struct sockaddr_in6  ));
 
	remote.sin6_family = AF_INET6;
	inet_pton(AF_INET6, muladdr, &remote.sin6_addr);
	remote.sin6_port = htons(10086);
 
	memset(&iface_out, 0, sizeof( struct sockaddr_in6));
	sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
	if ( setsockopt(sockfd , SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1 ) {
		perror("DEBUG: [get_output_if] setsockopt(SOL_SOCKET, SO_BROADCAST");
		close(sockfd);
		return -1;
	}
	if ( connect(sockfd, (struct sockaddr *) &remote, sizeof(struct sockaddr_in6)) == -1)
	{
		perror("IPv6 DEBUG: [get_output_if] connect");
		close(sockfd);
		return -1;
	}
 
	len = sizeof(iface_out);
	if (getsockname(sockfd, (struct sockaddr *) &iface_out, &len) == -1) {
		perror("DEBUG: [get_output_if] getsockname");
		close(sockfd);
		return -1;
	}
 
	close(sockfd);
 
	if (iface_out.sin6_addr.s6_addr == 0) {	/* what is this case?? */
		return -1;
	}
	
	inet_ntop(AF_INET6, (const void *) &iface_out.sin6_addr, address, 45);
	printf("IPV6 address:%s\n",address); 
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章