简体   繁体   English

C++/Qt:如何检测主机名或 IP 地址何时引用当前系统?

[英]C++ / Qt: How can I detect when a hostname or IP address refers to the current system?

I'm writing a macOS C++ application using Qt that acts as both a UDP client and UDP server.我正在使用 Qt 编写一个 macOS C++ 应用程序,它同时充当 UDP 客户端和 UDP 服务器。 The server functionality allows the user to configure which port the UDP packets will be received on, and the client functionality allows specifying both a target host and a port number.服务器功能允许用户配置接收 UDP 数据包的端口,客户端功能允许指定目标主机和端口号。 The host can be either a hostname or an IP address, including addresses or hostnames that resolve to a multicast or broadcast address.主机可以是主机名或 IP 地址,包括解析为多播或广播地址的地址或主机名。

In order to help prevent circularity, when the server functionality is enabled I need to be able to warn the user when the host and port they've entered for the client would send the packets directly to the app's server.为了帮助防止循环,当启用服务器功能时,我需要能够在他们为客户端输入的主机和端口将数据包直接发送到应用程序的服务器时警告用户。 Of course it's easy to check if the port numbers match, but this means I need to know whether the host they've entered refers to the current system.当然,检查端口号是否匹配很容易,但这意味着我需要知道他们输入的主机是否指的是当前系统。

Examples of hostnames or IP addresses that would typically be problematic:通常会出现问题的主机名或 IP 地址示例:

  • 127.0.0.1
  • localhost
  • 192.168.1.255 (assuming the system is on a 192.168.1.0/24 subnet) 192.168.1.255 (假设系统在192.168.1.0/24子网上)
  • any of the IP addresses assigned to the current system's network interfaces分配给当前系统网络接口的任何 IP 地址
  • the system's local DNS name系统的本地 DNS 名称
  • any other loopback addresses that may be configured other than 127.0.0.1除了127.0.0.1之外,可以配置的任何其他环回地址

How could I go about detecting this?我怎么能去检测这个?

Since this app is being written using Qt, a solution that exclusively uses Qt's framework would be ideal.由于此应用程序是使用 Qt 编写的,因此专门使用 Qt 框架的解决方案将是理想的选择。 But if that's not possible (since Qt doesn't handle every possible use case) a macOS-specific solution will work too.但如果这是不可能的(因为 Qt 不能处理所有可能的用例),macOS 特定的解决方案也将起作用。

QNetworkInterface class should provide the most information you may need. QNetworkInterface类应该提供您可能需要的最多信息。 You can obtain a list of IP addresses using QNetworkInterface::allAddresses() static function.您可以使用QNetworkInterface::allAddresses()静态函数获取 IP 地址列表。

You can also get a list of all interfaces using QNetworkInterface::allInterfaces() .您还可以使用QNetworkInterface::allInterfaces()获取所有接口的列表。

Calling QNetworkInterface::addressEntries() for each QNetworkInterface returned by QNetworkInterface::allInterfaces() will give you more information about address entries for each interface.QNetworkInterface::allInterfaces()返回的每个QNetworkInterface调用QNetworkInterface:: addressEntries()将为您提供有关每个接口的地址条目的更多信息。

auto ifs = QNetworkInterface::allInterfaces();
foreach (auto interface , ifs){
    auto addresses = interface.addressEntries();
    foreach ( auto addy , addresses){
        ///play with the addy here.
    }
}

You can also test hostnames against those ip addresses which you are going to ban, using QDnsLookup class.您还可以使用QDnsLookup类针对您要禁止的那些 IP 地址测试主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM