如何更改IPV6地址的字节顺序(从网络到主机,反之亦然)? [英] How can I change the byte order (from network to host, and vice versa) of an IPV6 address?
问题描述
我知道 ntoh {s,l}
和 hton {s,l}
2和4个字节的整数。
现在,我面临转换16个字节长的IPv6地址的问题。
I am aware of ntoh{s,l}
and hton{s,l}
, which work on integers of 2 and 4 bytes.
Now, I am facing the problem to translate an IPv6 address, which is 16 bytes long.
是否有用于此目的的现成函数?
Is there a ready-made function for that purpose?
TIA,
Jir
TIA, Jir
推荐答案
我不是确保 ntoh
和 hton
在IPv6中相关。您不是本地的128位类型吗?
I'm not sure that ntoh
and hton
are relevant in IPv6. You don't have a native 128-bit type, do you?
根据 http://www.mail-archive.com/users@ipv6.org/msg00195.html :
IPv6地址只要采用二进制形式(在
上是主机,路由器上,
等)。在其他地方,请参阅RFC
2553,第3.2节。
IPv6 addresses are expected to be representd in network byte order whenever they are in binary form (on the wire, on a host, in a router, etc). Among other places, see RFC 2553, section 3.2.
来自RFC 2553 :
3.2 IPv6地址结构
3.2 IPv6 Address Structure
一个新的in6_addr结构包含一个IPv6地址,并且由于包含以下内容而被定义:
A new in6_addr structure holds a single IPv6 address and is defined as a result of including :
struct in6_addr {
uint8_t s6_addr[16]; /* IPv6 address */
};
此数据结构包含十六个8位元素的数组,这些元素组成一个128位IPv6地址。 IPv6地址以网络字节顺序存储。
This data structure contains an array of sixteen 8-bit elements, which make up one 128-bit IPv6 address. The IPv6 address is stored in network byte order.
上面的in6_addr结构通常是通过嵌入式联合实现的,该联合具有额外的字段,这些字段以类似于 struct in_addr的BSD实现。为简单起见,此处省略了这些其他实现细节。
The structure in6_addr above is usually implemented with an embedded union with extra fields that force the desired alignment level in a manner similar to BSD implementations of "struct in_addr". Those additional implementation details are omitted here for simplicity.
示例如下:
struct in6_addr {
union {
uint8_t _S6_u8[16];
uint32_t _S6_u32[4];
uint64_t _S6_u64[2];
} _S6_un;
};
#define s6_addr _S6_un._S6_u8
这篇关于如何更改IPV6地址的字节顺序(从网络到主机,反之亦然)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!