高效的方式来存储IPv4 / IPv6地址 [英] Efficient way to store IPv4/IPv6 addresses

查看:718
本文介绍了高效的方式来存储IPv4 / IPv6地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C / C ++网络项目,它应该能够使用IPv4和IPv6网络堆栈。
该项目仅在Linux上工作。
所以,我试图找到一种有效的方式来存储IP地址和区分协议族。
第一种方法是建立一个联合:

  struct ip_addr {
uint8_t fam; // socket family type
union {
struct in_addr ipv4_sin_addr;
struct in6_addr ipv6_sin_addr;
} addr;
};



第二种方法是定义a typedef std :: vector< unsigned char> IPAddressNumber ,并在向量中的字节数之后产生差异。





第三种方法是使用gcc中的int128_t / uint128_t或__int128_t。





我想知道从哪个版本的GCC支持这些类型,对于哪些平台(特别是IA-32 / IA-64)以及如果有任何已知的错误。

解决方案

正如第一个答案 128位整数支持自GCC 4.6.4起可用。



您的问题不是128位整数支持,而是 如何正确表示IPv6地址

正确的答案是使用套接字提供的 struct 定义API



这些对于IPv6堆栈的各种操作系统实现是可用和标准化的。

此外,您不需要担心效率使用这些。对齐打包将正常工作,您不必关心实际表示的字节序与网络字节顺序问题。






对于您的修改:



您不必重新发明轮子!已有合适的

检查这些资源是否正确。 更详细的说明:





我们在生产中有一个 IpAddr 类, $ c> sockaddr_in * sockaddr_in6 * 指针根据 sockaddr *基于结构的 sa_family 成员的指针和 reinterpret_cast<> >

I am working on a C/C++ networking project that it should be able to both use the IPv4 and IPv6 networking stacks. The project works only on Linux. So, I tried to find an efficient way to store the IP addresses and differentiate between the protocol families. The first approach was to have a union:

struct ip_addr {
   uint8_t fam; // socket family type
   union {
       struct in_addr ipv4_sin_addr;
       struct in6_addr ipv6_sin_addr;
   }addr;
};

The second approach was to define a typedef std::vector<unsigned char> IPAddressNumberand make the difference after the number of bytes from the vector.

The third approach was to use int128_t/uint128_t or __int128_t from gcc.

For this last case, I would like to know from which version of GCC these types are supported, for which platforms (especially IA-32/IA-64) and also if there are any known bugs. Also, which of the above solutions might be the most convenient one?

解决方案

As stated in the 1st answer 128 bit integer support is available since GCC 4.6.4.

Your problem isn't 128 bit integer support, but how to represent an IPv6 address correctly.
The correct answer for this, is to use the struct definitions available from the socket API.

These are available and standardized for various operating system implementations of the IPv6 stack.
Also you don't need to worry about efficiency using these. Alignment packing will do its work properly, and you don't have to care about endianess vs network byte order issues of the actual representation.


As for your edits:

You don't have to reinvent the wheel! There are already appropriate struct definitions available respecting the AF_xxx family type correctly.

Check these resources for more detailed explanations:

We have an IpAddr class in production, that uses the opaque sockaddr_in* and sockaddr_in6* pointers to encapsulate either an IPv4 or IPv6 address based on a sockaddr* pointer, and reinterpret_cast<> based on the sa_family member of the structure.

这篇关于高效的方式来存储IPv4 / IPv6地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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