cwindowslinuxwinsockip

How to get internal IP, external IP and default gateway for UPnP


I'm wondering how I'd go about getting the:

in Windows (WinSock) and Unix systems.

Thanks in advance,


Solution

  • Solved thanks to: http://www.codeguru.com/forum/showthread.php?t=233261


    #include <winsock2.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #pragma comment(lib, "ws2_32.lib")
    
    int main(int nArgumentCount, char **ppArguments)
    {
        WSADATA WSAData;
    
        // Initialize WinSock DLL
        if(WSAStartup(MAKEWORD(1, 0), &WSAData))
        {
            // Error handling
        }
    
        // Get local host name
        char szHostName[128] = "";
    
        if(gethostname(szHostName, sizeof(szHostName)))
        {
            // Error handling -> call 'WSAGetLastError()'
        }
    
        SOCKADDR_IN socketAddress;
        hostent *pHost        = 0;
    
        // Try to get the host ent
        pHost = gethostbyname(szHostName);
        if(!pHost)
        {
            // Error handling -> call 'WSAGetLastError()'
        }
    
        char ppszIPAddresses[10][16]; // maximum of ten IP addresses
        for(int iCnt = 0; (pHost->h_addr_list[iCnt]) && (iCnt < 10); ++iCnt)
        {
            memcpy(&socketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
            strcpy(ppszIPAddresses[iCnt], inet_ntoa(socketAddress.sin_addr));
    
            printf("Found interface address: %s\n", ppszIPAddresses[iCnt]);
        }
    
        // Cleanup
        WSACleanup();
    }