How to find out the IP(s) of your machine

Joachim Ziegler <[email protected]>
Newsgroups gmane.comp.mozilla.devel.nspr
Message-ID <[email protected]>
Hello list,

what's the easiest way to find out the IP address(es) of your local
machine (as a string value)?

I've found an incredibly complex way (at least incredible for me) to do
this and I cannot imagine that there is not something simpler:

  string getMyIP() const throw(PRException)
  {
    const int HOSTLEN = 255;
    char hostname[HOSTLEN];
    char hostbuffer[PR_NETDB_BUF_SIZE];
    PRHostEnt hostent;
    PRNetAddr hostaddress;
    const PRUint32 IPBUFFERSIZE = 20; /* aaa.bbb.ccc.ddd\0 */
    char hostIPbuffer[IPBUFFERSIZE];

    if( PR_GetSystemInfo(PR_SI_HOSTNAME, hostname, HOSTLEN) != PR_SUCCESS )
      throw PRException("PR_GetSystemInfo() in getMyIP()");

    if( PR_GetHostByName( hostname, hostbuffer, PR_NETDB_BUF_SIZE,
&hostent )
        != PR_SUCCESS )
      throw PRException("PR_GetHostByName() in getMyIP()");

    if( PR_EnumerateHostEnt(0, &hostent, 0, &hostaddress) == -1 )
      throw PRException("PR_EnumerateHostEnt() in getMyIP()");

    if( PR_NetAddrToString( &hostaddress, hostIPbuffer, IPBUFFERSIZE )
        != PR_SUCCESS )
      throw PRException("PR_NetAddrToString() in getMyIP()");

    return string( hostIPbuffer );
  }

Joachim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.