1

Good morning,

I am doing some get requests using QNetworkAccessManager from Qt5.12. When I create the QNetworkRequest, I use a url containing a hostname. I would like to know the exact IP of the server that is answering (since the hostname can be bound to many addresses, and the addresses change).

I could not find anything in QNetworkReply that I get (maybe is the wrong class to analyse? I am not sure)

Someone knows where to get that information?

UPDATE:

As state in the comments, the "hack" I found (since Qt5.13) is:

  • resolve the hostname by myself (and print it)
  • use the IP to send the request
  • use setPeerVerifyName in the request with the hostname for the certificate validation

But what I would really like is an API like the following, once I have the QNetworkReply (and I received the "finished" signal)

if ( http_code != HTTP_OK )
{
    print network_reply.getIp()
}
n3mo
  • 663
  • 8
  • 23
  • I believe you should request the IP address of the host from your DNS server. `QNetworkReply` doesn't contain such information. – vahancho Sep 23 '20 at 07:51
  • Yes, I though of that...but I am scared that between my DNS request and the second request from Qt, something could change (for example the DNS server itself could change)...shouldn't the IP be contained in the IP packet at least? could I reach that information? – n3mo Sep 23 '20 at 08:00
  • One "hack" that I have though was: asking the ip from DNS by myself and in the QNetworkRequest just set the IP directly instead of the hostname...but I would prefer something in Qt itself... – n3mo Sep 23 '20 at 08:08
  • It's not clear why do you need this information? – vahancho Sep 23 '20 at 08:25
  • For debugging purposes. The servers I send the request to are also our own servers. Sometimes the get requests fails, and we were not able to reproduce, or to understand why. One idea is that a particular server is somehow broken/misconfigured. Logging the ip could help us. – n3mo Sep 23 '20 at 08:28
  • If you need to track activity by IPs, just use the IP addresses instead of host names. – vahancho Sep 23 '20 at 08:30
  • I can not use the IP hard coded, they are many and very volatile. I "could" resolve the hostname by myself, log the IP and create the request using the IP itself. But what I would like is to get that information in the reply, so I could have something like `if (reply_code != HTTP_OK) { log the ip }` – n3mo Sep 23 '20 at 08:35
  • If it were python, probably I would use something like [that](https://stackoverflow.com/questions/22492484/how-do-i-get-the-ip-address-from-a-http-request-using-the-requests-library) – n3mo Sep 23 '20 at 08:38
  • Actually, the "hack" I said some comments ago, will not work, because the certificates are bound to the hostname, and the TLS connection will be broken....So at the moment I don't have any working solution.... – n3mo Sep 23 '20 at 11:43
  • I think I could resolve the hostname by myself and use the IP directly...for the certificate validation, I could use https://doc.qt.io/qt-5/qnetworkrequest.html#setPeerVerifyName (but only since Qt5.13) – n3mo Sep 23 '20 at 14:11

0 Answers0