diff --git a/src/ipmodule.c b/src/ipmodule.c index 40ec967..6c0137f 100644 --- a/src/ipmodule.c +++ b/src/ipmodule.c @@ -3,26 +3,36 @@ #include #include #include - +#include #include "redismodule.h" int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { char hostbuffer[256]; - char *IPbuffer; + char IPbuffer[50]; struct hostent *host_entry; int hostname; + struct in_addr **addr_list; + int i; if (RedisModule_Init(ctx,"RedisIP",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR) return REDISMODULE_ERR; hostname = gethostname(hostbuffer, sizeof(hostbuffer)); - // checks /etc/hosts (needs error handling for if hostname isn't present) - host_entry = gethostbyname(hostbuffer); + // checks /etc/hosts + if ((host_entry = gethostbyname(hostbuffer)) == NULL) { + RedisModule_Log(ctx, "warning", "Hostname: NOTFOUND"); + return REDISMODULE_OK; + } - IPbuffer = inet_ntoa(*((struct in_addr*) - host_entry->h_addr_list[0])); + addr_list = (struct in_addr **) host_entry->h_addr_list; + for(i = 0; addr_list[i] != NULL; i++) + { + //Return the first one; + strcpy(IPbuffer , inet_ntoa(*addr_list[i]) ); + break; + } RedisModule_Log(ctx, "warning", "Hostname: %s", hostbuffer); RedisModule_Log(ctx, "warning", "IP: %s", IPbuffer);