Search code examples
c++dynamicdnsgetaddrinfo

C++ - getaddrinfo() - getting own external IP with dynamic dns fails


I'm currently working on a program that tells me, among other things, my external IP. To achieve this, I'm using the function getaddrinfo().
For testing purpose I passed google.com as node name, it worked fine.
If I'm passing my dynamic dns to the function, I receive 0.0.0.0 as IP, but I'm expecting my own external IP. I also tried a dynamic dns from a friend with the same result.
As far as I can get the IP from google.com, the problem shouldn't be my code.
Is it not possible, to receive the IP from a dynamic dns with getaddrinfo() or is there anything else I'm doing wrong?

Edit:

WSADATA wsaD {};
addrinfo hints = {AI_NON_AUTHORITATIVE, AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, NULL, NULL, NULL};
addrinfo *infoAddress;
WSAStartup(MAKEWORD(2, 2), &wsaD);
getaddrinfo("daniel-nas.computer", "80", &hints, &infoAddress);
struct sockaddr_in *ipAddress = reinterpret_cast<sockaddr_in*>&infoAddress->ai_addr);
inet_ntop(AF_INET, &ipAddress->sin_addr.s_addr, IP, sizeof(IP));
WSACleanup();

IP is globaly defined: char IP[INET_ADDRSTRLEN];
Is there anybody else who came over this issue?


Solution

  • [Solution]
    As a kind of solution, I'm using the function gethostbyname(). Nevertheless I'm not really pleased with it, because the function is deprecated, but I'm now able to get my IP. Therefore I think, the issue is either my code, although I can resolve other hostnames, or the combination of the function getaddrinfo() and DDNS.