Skip to content

Instantly share code, notes, and snippets.

@markusfisch
Created July 30, 2013 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusfisch/6110750 to your computer and use it in GitHub Desktop.
Save markusfisch/6110750 to your computer and use it in GitHub Desktop.
Return first local IP address
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
/**
* Local IP address
*
* @param buf - buffer to take ip address
* @param len - length of buffer
*/
char *local_ip( char *buf, int len )
{
char hn[256];
struct hostent *he;
if( gethostname( hn, sizeof( hn ) ) )
return NULL;
if( !(he = gethostbyname( hn )) )
return NULL;
strncpy(
buf,
inet_ntoa( *((struct in_addr *)*he->h_addr_list) ),
len );
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment