* 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){
+ printlog(LOG_DEBUG, "get_local_ip: checking new addr.\n");
if (ifa->ifa_addr->sa_family == AF_INET){
salen = sizeof(struct sockaddr_in);
} else if( ifa->ifa_addr->sa_family == AF_INET6){
/* is it MSB first? */
- if ( (addrbyte != 192) && (addrbyte != 172)
- && (addrbyte != 10) && (addrbyte != 127) ){
+ /* We don't want to choose the loopback. */
+ if (addrbyte == 127) {
+ printlog(LOG_DEBUG, "get_local_ip: ignoring the loopback.\n");
+ continue;
+ }
+
+ printlog(LOG_DEBUG, "get_local_ip: found address starting with %hhu\n", addrbyte);
+
+ /* 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);
+ }
+ printlog(LOG_DEBUG, "get_local_ip: returning routable address %s\n", ip);
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) {
+ printlog(LOG_DEBUG, "get_local_ip: returning local address %s\n", localip);
+ return localip;
+ }
+ else
+ return(NULL);
}
static FILE *urandfd = NULL;
return c;
}
-
-/* print out some error information if a system call failes */
-void print_system_error(int ret){
- if (WIFSIGNALED(ret) &&
- (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)){
- /* received some kind of signal during program execution */
- printf("enforce received signal during program execution.");
- } else {
- perror("enforce failed (ret==-1):");
- }
-}