Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://developerweb.net/viewtopic.php?pid=21773#p21773" rel="nofollow noreferrer">Jeremy's solution</a> on UNIX Socket FAQ:</p> <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt; #ifdef WIN32 # include &lt;windows.h&gt; # include &lt;winsock.h&gt; # include &lt;iphlpapi.h&gt; #else # include &lt;unistd.h&gt; # include &lt;stdlib.h&gt; # include &lt;sys/socket.h&gt; # include &lt;netdb.h&gt; # include &lt;netinet/in.h&gt; # include &lt;net/if.h&gt; # include &lt;sys/ioctl.h&gt; #endif #include &lt;string.h&gt; #include &lt;sys/stat.h&gt; typedef unsigned long uint32; #if defined(__FreeBSD__) || defined(BSD) || defined(__APPLE__) || defined(__linux__) # define USE_GETIFADDRS 1 # include &lt;ifaddrs.h&gt; static uint32 SockAddrToUint32(struct sockaddr * a) { return ((a)&amp;&amp;(a-&gt;sa_family == AF_INET)) ? ntohl(((struct sockaddr_in *)a)-&gt;sin_addr.s_addr) : 0; } #endif // convert a numeric IP address into its string representation static void Inet_NtoA(uint32 addr, char * ipbuf) { sprintf(ipbuf, "%li.%li.%li.%li", (addr&gt;&gt;24)&amp;0xFF, (addr&gt;&gt;16)&amp;0xFF, (addr&gt;&gt;8)&amp;0xFF, (addr&gt;&gt;0)&amp;0xFF); } // convert a string represenation of an IP address into its numeric equivalent static uint32 Inet_AtoN(const char * buf) { // net_server inexplicably doesn't have this function; so I'll just fake it uint32 ret = 0; int shift = 24; // fill out the MSB first bool startQuad = true; while((shift &gt;= 0)&amp;&amp;(*buf)) { if (startQuad) { unsigned char quad = (unsigned char) atoi(buf); ret |= (((uint32)quad) &lt;&lt; shift); shift -= 8; } startQuad = (*buf == '.'); buf++; } return ret; } static void PrintNetworkInterfaceInfos() { #if defined(USE_GETIFADDRS) // BSD-style implementation struct ifaddrs * ifap; if (getifaddrs(&amp;ifap) == 0) { struct ifaddrs * p = ifap; while(p) { uint32 ifaAddr = SockAddrToUint32(p-&gt;ifa_addr); uint32 maskAddr = SockAddrToUint32(p-&gt;ifa_netmask); uint32 dstAddr = SockAddrToUint32(p-&gt;ifa_dstaddr); if (ifaAddr &gt; 0) { char ifaAddrStr[32]; Inet_NtoA(ifaAddr, ifaAddrStr); char maskAddrStr[32]; Inet_NtoA(maskAddr, maskAddrStr); char dstAddrStr[32]; Inet_NtoA(dstAddr, dstAddrStr); printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", p-&gt;ifa_name, "unavailable", ifaAddrStr, maskAddrStr, dstAddrStr); } p = p-&gt;ifa_next; } freeifaddrs(ifap); } #elif defined(WIN32) // Windows XP style implementation // Adapted from example code at http://msdn2.microsoft.com/en-us/library/aa365917.aspx // Now get Windows' IPv4 addresses table. Once again, we gotta call GetIpAddrTable() // multiple times in order to deal with potential race conditions properly. MIB_IPADDRTABLE * ipTable = NULL; { ULONG bufLen = 0; for (int i=0; i&lt;5; i++) { DWORD ipRet = GetIpAddrTable(ipTable, &amp;bufLen, false); if (ipRet == ERROR_INSUFFICIENT_BUFFER) { free(ipTable); // in case we had previously allocated it ipTable = (MIB_IPADDRTABLE *) malloc(bufLen); } else if (ipRet == NO_ERROR) break; else { free(ipTable); ipTable = NULL; break; } } } if (ipTable) { // Try to get the Adapters-info table, so we can given useful names to the IP // addresses we are returning. Gotta call GetAdaptersInfo() up to 5 times to handle // the potential race condition between the size-query call and the get-data call. // I love a well-designed API :^P IP_ADAPTER_INFO * pAdapterInfo = NULL; { ULONG bufLen = 0; for (int i=0; i&lt;5; i++) { DWORD apRet = GetAdaptersInfo(pAdapterInfo, &amp;bufLen); if (apRet == ERROR_BUFFER_OVERFLOW) { free(pAdapterInfo); // in case we had previously allocated it pAdapterInfo = (IP_ADAPTER_INFO *) malloc(bufLen); } else if (apRet == ERROR_SUCCESS) break; else { free(pAdapterInfo); pAdapterInfo = NULL; break; } } } for (DWORD i=0; i&lt;ipTable-&gt;dwNumEntries; i++) { const MIB_IPADDRROW &amp; row = ipTable-&gt;table[i]; // Now lookup the appropriate adaptor-name in the pAdaptorInfos, if we can find it const char * name = NULL; const char * desc = NULL; if (pAdapterInfo) { IP_ADAPTER_INFO * next = pAdapterInfo; while((next)&amp;&amp;(name==NULL)) { IP_ADDR_STRING * ipAddr = &amp;next-&gt;IpAddressList; while(ipAddr) { if (Inet_AtoN(ipAddr-&gt;IpAddress.String) == ntohl(row.dwAddr)) { name = next-&gt;AdapterName; desc = next-&gt;Description; break; } ipAddr = ipAddr-&gt;Next; } next = next-&gt;Next; } } char buf[128]; if (name == NULL) { sprintf(buf, "unnamed-%i", i); name = buf; } uint32 ipAddr = ntohl(row.dwAddr); uint32 netmask = ntohl(row.dwMask); uint32 baddr = ipAddr &amp; netmask; if (row.dwBCastAddr) baddr |= ~netmask; char ifaAddrStr[32]; Inet_NtoA(ipAddr, ifaAddrStr); char maskAddrStr[32]; Inet_NtoA(netmask, maskAddrStr); char dstAddrStr[32]; Inet_NtoA(baddr, dstAddrStr); printf(" Found interface: name=[%s] desc=[%s] address=[%s] netmask=[%s] broadcastAddr=[%s]\n", name, desc?desc:"unavailable", ifaAddrStr, maskAddrStr, dstAddrStr); } free(pAdapterInfo); free(ipTable); } #else // Dunno what we're running on here! # error "Don't know how to implement PrintNetworkInterfaceInfos() on this OS!" #endif } int main(int, char **) { PrintNetworkInterfaceInfos(); return 0; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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