Created
July 30, 2013 06:27
-
-
Save markusfisch/6110750 to your computer and use it in GitHub Desktop.
Return first local IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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