1 /* @(#) $Id: ./src/analysisd/decoders/geoip.c, 2014/03/08 dcid Exp $
4 /* Copyright (C) 2014 Daniel Cid
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
14 /* GeoIP - Every IP address will have its geolocation added to it */
16 #ifdef LIBGEOIP_ENABLED
20 #include "os_regex/os_regex.h"
21 #include "eventinfo.h"
22 #include "alerts/alerts.h"
25 #include "GeoIPCity.h"
28 char *GetGeoInfobyIP(char *ip_addr)
30 GeoIPRecord *geoiprecord;
32 char geobuffer[256 +1];
44 geoiprecord = GeoIP_record_by_name(geoipdb, (const char *)ip_addr);
45 if(geoiprecord == NULL)
50 if(geoiprecord->country_code == NULL)
52 GeoIPRecord_delete(geoiprecord);
56 if(strlen(geoiprecord->country_code) < 2)
58 GeoIPRecord_delete(geoiprecord);
63 if(geoiprecord->region != NULL && geoiprecord->region[0] != '\0')
65 const char *regionname = NULL;
66 regionname = GeoIP_region_name_by_code(geoiprecord->country_code, geoiprecord->region);
67 if(regionname != NULL)
69 snprintf(geobuffer, 255, "%s / %s", geoiprecord->country_code, regionname);
70 geobuffer[255] = '\0';
71 geodata = strdup(geobuffer);
75 geodata = strdup(geoiprecord->country_code);
80 geodata = strdup(geoiprecord->country_code);
83 GeoIPRecord_delete(geoiprecord);