Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
When /etc/hosts isn't present, the hostname
lookup does not crash the redis-server
  • Loading branch information
Sam Partee committed Apr 2, 2021
1 parent 3b53a56 commit bf92215
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/ipmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include<string.h>
#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);

Expand Down

0 comments on commit bf92215

Please sign in to comment.