Note that there are some explanatory texts on larger screens.

plurals
  1. POGot local host name, working on windows, but not in Linux
    primarykey
    data
    text
    <p>I wrote a program to get local host name. if it is not obvious, I mean to get the host name of the local machine similar to <code>gethostname</code> method, not to get the string <code>localhost</code></p> <p>I'm using <code>getaddrinfo</code> with <code>NULL</code> for host name and then call to <code>getnameinfo</code> with the first address.</p> <p>It works perfectly on windows machines, and giving me the canonical host name, but in Linux it failed to give me the local host name and gives me <code>::</code> for IPv6 or <code>0.0.0.0</code> for IPv4.</p> <p>Note that I'm using <code>AI_PASSIVE</code> flag in <code>getaddrinfo</code>, means it gives me no more than two addresses.</p> <p>In the <code>getnameinfo</code> if I use the <code>NI_NAMEREQD</code> it fail.</p> <p>I'm doing so because I have a server, sometimes I want to listen to all interfaces and sometimes to a specific interface, I'm already have the <code>addrinfo</code> and I just want to get the hostname from it if possible.</p> <p>How can I make it work on Linux? did I missed anything?</p> <p>Thanks for any help.</p> <p>Here is my code: (cross platform, windows and Linux, just copy &amp; past) You can compile and run on both and see the difference.</p> <pre><code>#include &lt;iostream&gt; #ifdef WIN32 #ifdef UNICODE #undef UNICODE /* don't want Unicode, to keep code compatibility */ #endif #include &lt;WS2tcpip.h&gt; #pragma comment(lib, "ws2_32.lib") class CWSAInitializer{ public: CWSAInitializer(){ WSADATA data; WSAStartup(MAKEWORD(2,2),&amp;data); } ~CWSAInitializer(){ WSACleanup(); } }autoInit; #else #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netdb.h&gt; #endif const char* get_hostname(struct addrinfo* info, char buff[], int buff_len) { int addrlen = (int)info-&gt;ai_addrlen; int res = getnameinfo(info-&gt;ai_addr, addrlen, buff, buff_len, NULL, 0, 0); if(res){ return NULL; } return buff; } int main () { char temp[100]; addrinfo *ai,hints; memset(&amp;hints,0,sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICSERV; hints.ai_flags |= AI_PASSIVE; int res = getaddrinfo(NULL, "0", &amp;hints, &amp;ai); if(res){ std::cerr&lt;&lt;gai_strerror(res)&lt;&lt;std::endl; return -1; } std::cout&lt;&lt; get_hostname(ai,temp,100)&lt;&lt;std::endl; freeaddrinfo(ai); return 0; } </code></pre> <p><strong>EDIT</strong></p> <ol> <li><p>The server should accept client from other machines as well, so it can't report listening on <code>::</code> or <code>0.0.0.0</code>. </p></li> <li><p>I know I can patch it like this: </p></li> </ol> <p><code>if(std::string("::") == hostname || std::string("0.0.0.0") == hostname) gethostname(hostname,100)</code></p> <ol start="3"> <li>I don't want to get <code>localhost</code> as result, unless the address is <code>127.0.0.1</code> or <code>::1</code>.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload