高效IP地址c/c++ UNIX 上的库
是否有一个好的高级库可用于 IP 地址操作?我需要执行以下操作:
- 给定一个字符串,找出它是否是有效的 IPv4/IPv6 地址。
- 具有像 ntop 和 pton
- 等
功能,我可以使用低级 inet_ntop() 等。但是有没有更好的库可以更好、更快地处理这些(c/c++/python)?
Is there a good high level library that can be used for IP address manipulation? I need to do things like:
- Given a string find out if it is a valid IPv4/IPv6 address.
- Have functionality like ntop and pton
- etc
I can use the low level inet_ntop() etc. But is there a better library that handles these better and fast (c/c++/python)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Poco 库 有一组非常好的主机名/IP 地址操作例程,以及许多其他非常棒的东西。 FreeBSD 端口有点过时了,但我们也许可以用尖棍戳一下端口维护者,让他更新它。哦,等等...:)
The Poco Library has a very nice set of hostname/IP address manipulation routines, and a lot of other really great stuff. The FreeBSD port is a bit out of date, but we might be able to poke the port-maintainer with a sharp stick and get him to update it. Oh, wait... :)
我很想用 ipv4 / ipv6 来验证正则表达式,这些正则表达式相当长,而且生成起来并不简单。如果你愿意的话我可以分享。
I have the mind boogling ipv4 / ipv6 validating regexps around, which are quite long and non-trivial to produce. I can share if you want.
对于 C++,标准的高级答案是 boost.asio< /a>.具体来说,将字符串转换为它所具有的 IP 地址
ip::address::from_string 并获取 IP 地址的字符串表示形式,它具有 to_string。
请查看其他构造函数对于 IP 地址对象——使用原始字节可能比字符串更有效。
For C++, the standard high-level answer would be boost.asio. Specifically, to convert a string into an IP address it has
ip::address::from_string and to obtain a string representation of an ip address, it has to_string.
Do check out other constructors for the ip address objects -- using raw bytes is likely to be more efficient than strings.
如果您正在编写套接字应用程序,那么地址操作不太可能成为您最重要的考虑因素。当您需要担心网络 I/O 时,请不要在这方面浪费时间。
If you are writing a sockets app it's highly unlikely that address manipulation is going to be your most important consideration. Don't waste time on this when you have network I/O to worry about.
inet_ntop
和inet_pton
不支持 IPv6 区域,因此推荐的 API 只是getaddrinfo
和getnameinfo
,它们是方便的 IP版本无关。传递诸如
NI_NUMERICHOST
之类的标志来强制 IP 地址验证而不进行 DNS 解析。http://msdn.microsoft.com/en-us/库/ms738532(VS.85).aspx
示例:
inet_ntop
andinet_pton
do not support IPv6 zones and so the recommend APIs are simplygetaddrinfo
andgetnameinfo
which are conveniently IP version agnostic.Pass flags such as
NI_NUMERICHOST
to force IP address validation without DNS resolution.http://msdn.microsoft.com/en-us/library/ms738532(VS.85).aspx
example: