* return the string.
*/
char* get_local_ip(){
- char *ip;
+ char *ip, *localip;
struct ifaddrs *ifa = NULL, *ifp = NULL;
struct sockaddr *addr;
uint32_t ho_addr;
#define STRINGSIZE 200
ip = (char *)malloc(STRINGSIZE);
+ localip = NULL;
for (ifa = ifp; ifa; ifa=ifa->ifa_next){
/* is it MSB first? */
- if ( (addrbyte != 192) && (addrbyte != 172)
- && (addrbyte != 10) && (addrbyte != 127) ){
+ /* We don't want to choose the loopback. */
+ if (addrbyte == 127)
+ continue;
+
+ /* If there's a non-local address, use that. */
+ if ((addrbyte != 192) && (addrbyte != 172) && (addrbyte != 10)){
printlog(LOG_WARN, " Using ip: %s\n",ip);
freeifaddrs(ifp);
-#if 0
- return (ifa->ifa_addr->sin_addr);
-#else
+ if (localip != NULL) {
+ free(localip);
+ }
return(ip); /* for now return the IP address */
-#endif
+ } else {
+ if (localip == NULL) {
+ localip = malloc(STRINGSIZE);
+ }
+ strncpy(localip, ip, STRINGSIZE);
}
}
freeifaddrs(ifp);
free(ip);
- return(NULL);
+
+ if (localip != NULL)
+ return localip;
+ else
+ return(NULL);
}
static FILE *urandfd = NULL;