IPv6下网络字节顺序没有意义吗? [英] Is network byte order pointless under IPv6?

查看:108
本文介绍了IPv6下网络字节顺序没有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们使用32位整数存储IPv4地址,则必须考虑整数的字节顺序.

If we use a 32-bit integer to store an IPv4 address, then the byte order of the integer must be considered.

但是,由于几乎在所有平台上都没有内置的128位整数类型,因此必须将IPv6地址存储到字节数组中,因此,我认为字节顺序不再是问题.

However, as there is no built-in 128-bit integer type under almost all platforms, an IPv6 address must be stored into a byte array, so, I think the byte order is no longer a problem.

我正确吗?还是有针对IPv6的对应功能htonlXXX?

Am I correct? Or is there a corresponding function htonlXXX for IPv6?

推荐答案

IPv6确实要求ipv6地址的网络字节顺序.hton和ntoh都是将地址从您在代码中的存储方式转换为需要在数据包中存储的方式(反之亦然).因此,问题就变成了如何将其存储在代码中.

IPv6 does require network byte order for ipv6 addresses. hton and ntoh are all about converting the address from how you have it stored in your code, to how it needs to be stored in the packet (and vice-versa). So the issue becomes how you have it stored in your code.

代码中的IPv6地址的定义可以提供更多的寻址方式,而不仅仅是字节数组:

Also the definition of an IPv6 address in code can allow more ways to address it than just an array of bytes:

struct in6_addr
{
    union 
    {
        __u8 u6_addr8[16];
        __u16 u6_addr16[8];
        __u32 u6_addr32[4];
    } in6_u;
#define s6_addr in6_u.u6_addr8
#define s6_addr16 in6_u.u6_addr16
#define s6_addr32 in6_u.u6_addr32
};

给用户的

IPv6地址表示为8个16位值.如果您的地址在代码中存储为8个16位值,则需要使用uton_addr16 []数组将htons放入数据包中,并在每个16位值上使用htons,并在检索每个16位值时使用ntohsu6_addr16 []的位值.

IPv6 addresses, to the user, are represented as 8 16-bit values. If you have the address stored as 8 16-bit values in your code, then you will need to use htons on each 16 bit value as you place it into the packet using the u6_addr16[] array, and use ntohs as you retrieve each 16-bit value from u6_addr16[].

这些链接很有帮助:

http://msdn.microsoft.com/en-us/library/ee175867.aspx

http://en.wikipedia.org/wiki/IPv6_address (尤其是该图位于右上方)

http://en.wikipedia.org/wiki/IPv6_address (especially the diagram at top right)

这篇关于IPv6下网络字节顺序没有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆