X-Git-Url: http://git.onelab.eu/?p=distributedratelimiting.git;a=blobdiff_plain;f=drl%2Futil.c;h=c03eced5b907d2c88a5933e979c5c93b4bcb00d1;hp=9f5706af52af41b5a90446d0e492795d1f3d9288;hb=89df43dd6b8cb8df82cfbf395b923014d2826b5a;hpb=0be9704d6b24d09ebd55beedec52758cb88c570b diff --git a/drl/util.c b/drl/util.c index 9f5706a..c03eced 100644 --- a/drl/util.c +++ b/drl/util.c @@ -231,7 +231,7 @@ void map_insert(map_handle map, void *key, int keylen, void *value) { * 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; @@ -253,9 +253,11 @@ char* get_local_ip(){ #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){ @@ -279,20 +281,39 @@ char* get_local_ip(){ /* 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; @@ -347,14 +368,3 @@ int my_lg(int x) { 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):"); - } -}