高效IP地址c/c++ UNIX 上的库

发布于 2024-11-04 17:50:41 字数 198 浏览 0 评论 0原文

是否有一个好的高级库可用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

撕心裂肺的伤痛 2024-11-11 17:50:42

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... :)

强者自强 2024-11-11 17:50:42

我很想用 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.

感悟人生的甜 2024-11-11 17:50:41

对于 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.

椵侞 2024-11-11 17:50:41

如果您正在编写套接字应用程序,那么地址操作不太可能成为您最重要的考虑因素。当您需要担心网络 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.

水染的天色ゝ 2024-11-11 17:50:41

inet_ntopinet_pton 不支持 IPv6 区域,因此推荐的 API 只是 getaddrinfogetnameinfo,它们是方便的 IP版本无关。

传递诸如 NI_NUMERICHOST 之类的标志来强制 IP 地址验证而不进行 DNS 解析。

http://msdn.microsoft.com/en-us/库/ms738532(VS.85).aspx

示例:

char* input_str
struct addrinfo *result = NULL, hints;
ZeroMemory( &hints, sizeof(hints) );
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
dwRetval = getaddrinfo( input_string, NULL, &hints, &result );
if ( dwRetval != 0 ) {
     /* failure */
}

inet_ntop and inet_pton do not support IPv6 zones and so the recommend APIs are simply getaddrinfo and getnameinfo 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:

char* input_str
struct addrinfo *result = NULL, hints;
ZeroMemory( &hints, sizeof(hints) );
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
dwRetval = getaddrinfo( input_string, NULL, &hints, &result );
if ( dwRetval != 0 ) {
     /* failure */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文