本文介绍了有没有任何代码bitwise和ipv6地址和网络掩码(前缀)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想询问关于ipv6网络和主机端的计算。

I want to ask about calculation of ipv6 network and host side.

例如,我有IPv6地址 2001:470:1f15 :1bcd:34 :: 41 和前缀 96 。

For example, I have the IPv6 address 2001:470:1f15:1bcd:34::41 and prefix 96.

简单的方法来做位地址和在IPv6地址和前缀之间?

Do you know a easy way to do bitwise and between IPv6 address and prefix?

根据IPv4:

192.168.1.2  255.255.255.0  network : 192.168.1.0

这么简单。

我想对IPv6地址做同样的事情。但是IPv6地址是16字节,所以你不能使用 unsigned int 。

I want to do the same thing to IPv6 address. But IPv6 address is 16 bytes, so you can't use unsigned int for that.

去做这个?

推荐答案

我解决了我的问题源代码下面使用它,并继续编码:D:警告该函数假设IPv6地址有效,
我的类型是:

guys i solved my problem the source code is below use it and go on coding :D : Warning the function assume the IPv6 address is valid.,my type is:

typedef uint16_t ip6_addr[8];


void ipv6_app_mask(const char *ip6addr, unsigned int mask, ip6_addr ip6){

    ip6_addr in_ip6;
    inet_pton(PF_INET6, ip6addr, ip6);


    for(int i = 0; i < 8; i++){
        in_ip6[i] = ntohs(ip6[i]);
    }

    int index = (int) (mask / 16);
    int remain_mask = mask % 16;

     if(remain_mask == 0 && index == 8)
      return;

     switch(remain_mask){
        case 0:in_ip6[index++] = 0; break;
        case 1:in_ip6[index++]&=0x8000; break;
        case 2:in_ip6[index++]&=0xc000; break;
        case 3:in_ip6[index++]&=0xe000; break;
        case 4:in_ip6[index++]&=0xf000; break;

        case 5:in_ip6[index++]&=0xf800; break;
        case 6:in_ip6[index++]&=0xfc00; break;
        case 7:in_ip6[index++]&=0xfe00; break;
        case 8:in_ip6[index++]&=0xff00; break;

        case  9:in_ip6[index++]&=0xff80; break;
        case 10:in_ip6[index++]&=0xffc0; break;
        case 11:in_ip6[index++]&=0xffe0; break;
        case 12:in_ip6[index++]&=0xfff0; break;

        case 13:in_ip6[index++]&=0xfff8; break;
        case 14:in_ip6[index++]&=0xfffc; break;
        case 15:in_ip6[index++]&=0xfffe; break;
    }

    for (int i = index; i < 8; i++){
       in_ip6[i] = 0;
    }

    for(int i = 0; i < 8; i++){
       ip6[i] = htons(in_ip6[i]);
    } 

 return;
}

这篇关于有没有任何代码bitwise和ipv6地址和网络掩码(前缀)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 03:03