获取本机Ipv6的几种方法

这篇博客介绍了两种C语言方法来获取本机的IPv6地址:通过`byeth0getipv6.c`和`bymulgetipv6.c`实现。详细代码和过程在原文链接中提供。
摘要由CSDN通过智能技术生成

 以下有几个方法可以获取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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

just_run丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值