sockaddr_storage大小为128字节
sockaddr_storage size of 128 bytes
我只是想知道为什么sockaddr_storage是128字节。我知道IPv6至少需要28个字节,但比sockaddr_in6多出100个字节似乎有点过分。这只是为了让存储结构经得起未来考验,还是有理由现在就需要它?
您将在rfc 2553的§3.10和本SO文章中找到问题的答案。
原因是的组合至少应保持ip6和其他协议数据和64位对齐以提高效率。
来自RFC的相关部分:
插座API的一个简单添加,可以帮助应用程序
writers是"structsockaddr_storage"。此数据结构可以
简化编写可跨多个地址族移植的代码,
平台。此数据结构的设计目标如下。- It has a large enough implementation specific maximum size to store the desired set of protocol specific socket address data structures. Specifically, it is at least large enough to accommodate sockaddr_in and sockaddr_in6 and possibly other protocol specific socket addresses too. - It is aligned at an appropriate boundary so protocol specific socket address data structure pointers can be cast to it and access their fields without alignment problems. (e.g. pointers to sockaddr_in6 and/or sockaddr_in can be cast to it and access fields without alignment problems). - It has the initial field(s) isomorphic to the fields of the "struct sockaddr" data structure on that implementation which can be used as a discriminants for deriving the protocol in use. These initial field(s) would on most implementations either be a single field of type "sa_family_t" (isomorphic to sa_family field, 16 bits) or two fields of type uint8_t and sa_family_t respectively, (isomorphic to sa_len and sa_family_t, 8 bits each).
可能有一些系统也希望能够在此结构中适应struct sockaddr_un
。后者具有大约100到110个字符的系统相关路径长度。128是一个漂亮的偶数。
相关文章:
- 将字节数组转换为带有字节序问题的指针
- 将字节打印为长字节
- 函数获取参数的变量列表并将它们转换为一系列字节?
- boost::asio::async_read 无限循环,接收数据为零字节
- 如何在 CPP 中将 Typedef basic_string字符串转换为<char>字节数组
- 如何将 lua 脚本转换为 lua 字节码
- 如果字节中的所有位都设置为高,则设置为高字节
- 如何在-120到120之间更改int到char(或其他简单保存为一个字节的简单方法)
- 搜索一组点,其长度总和最小为矩形.算法是什么
- 使用位操作将8字节数字中的每个字节中的一位转换为单个字节
- 为什么以下程序中的输出为 12 字节,而总大小仅为 6 位
- 在将其编译为EVM字节码的过程中,坚固性会降低到LLL吗?
- 在java中将两个字节(一个正字节和另一个负字节)合并为短字节
- 将主机字节顺序转换为网络字节顺序有时会导致奇怪的结果
- 将浮点数划分为单个字节
- 字符串大小始终为 32 字节
- 如何将BitString转换为ctypes字节数组
- 将8个布尔值转换为一个字节的最佳方式
- 在 Azure 中将 int 转换为 C++ 中 blobId 的小端格式字节
- sockaddr_storage大小为128字节