22#include " Logger.hpp"
33#include < sstream>
44
5- namespace {
6- nlohmann::json entryToJson (const MMDB_s &db, const MMDB_entry_data_s &entry) {
7- switch (entry.type ) {
5+ namespace
6+ {
7+ nlohmann::json entryToJson (const MMDB_s &db, const MMDB_entry_data_s &entry)
8+ {
9+ switch (entry.type )
10+ {
811 case MMDB_DATA_TYPE_UTF8_STRING:
912 return std::string (entry.utf8_string , entry.data_size );
1013 case MMDB_DATA_TYPE_DOUBLE:
@@ -21,18 +24,22 @@ nlohmann::json entryToJson(const MMDB_s &db, const MMDB_entry_data_s &entry) {
2124 return entry.uint64 ;
2225 case MMDB_DATA_TYPE_BOOLEAN:
2326 return static_cast <bool >(entry.boolean );
24- case MMDB_DATA_TYPE_MAP: {
27+ case MMDB_DATA_TYPE_MAP:
28+ {
2529 MMDB_entry_s sub{&db, entry.offset };
2630 MMDB_entry_data_list_s *list = nullptr ;
27- if (MMDB_get_entry_data_list (&sub, &list) == MMDB_SUCCESS && list) {
31+ if (MMDB_get_entry_data_list (&sub, &list) == MMDB_SUCCESS && list)
32+ {
2833 nlohmann::json obj = nlohmann::json::object ();
2934 MMDB_entry_data_list_s *ptr = list;
30- while (ptr && ptr->next ) {
35+ while (ptr && ptr->next )
36+ {
3137 auto key = ptr->entry_data ;
3238 ptr = ptr->next ;
3339 auto val = ptr->entry_data ;
3440 ptr = ptr->next ;
35- if (key.type != MMDB_DATA_TYPE_UTF8_STRING) continue ;
41+ if (key.type != MMDB_DATA_TYPE_UTF8_STRING)
42+ continue ;
3643 std::string k (key.utf8_string , key.data_size );
3744 obj[k] = entryToJson (db, val);
3845 }
@@ -41,13 +48,16 @@ nlohmann::json entryToJson(const MMDB_s &db, const MMDB_entry_data_s &entry) {
4148 }
4249 break ;
4350 }
44- case MMDB_DATA_TYPE_ARRAY: {
51+ case MMDB_DATA_TYPE_ARRAY:
52+ {
4553 MMDB_entry_s sub{&db, entry.offset };
4654 MMDB_entry_data_list_s *list = nullptr ;
47- if (MMDB_get_entry_data_list (&sub, &list) == MMDB_SUCCESS && list) {
55+ if (MMDB_get_entry_data_list (&sub, &list) == MMDB_SUCCESS && list)
56+ {
4857 nlohmann::json arr = nlohmann::json::array ();
4958 MMDB_entry_data_list_s *ptr = list;
50- while (ptr) {
59+ while (ptr)
60+ {
5161 arr.push_back (entryToJson (db, ptr->entry_data ));
5262 ptr = ptr->next ;
5363 }
@@ -58,61 +68,78 @@ nlohmann::json entryToJson(const MMDB_s &db, const MMDB_entry_data_s &entry) {
5868 }
5969 default :
6070 break ;
71+ }
72+ return {};
6173 }
62- return {};
63- }
6474} // namespace
6575
6676GeoIP::GeoIP (const std::string &path, const std::vector<std::string> &k)
67- : keys(k) {
77+ : keys(k)
78+ {
6879 int status = MMDB_open (path.c_str (), MMDB_MODE_MMAP, &mmdb);
69- if (status != MMDB_SUCCESS) {
80+ if (status != MMDB_SUCCESS)
81+ {
7082 Logger::error (std::string (" GeoIP open failed: " ) + path + " " + MMDB_strerror (status));
7183 loaded = false ;
72- } else {
84+ }
85+ else
86+ {
7387 loaded = true ;
7488 }
7589}
7690
77- GeoIP::~GeoIP () {
78- if (loaded) {
91+ GeoIP::~GeoIP ()
92+ {
93+ if (loaded)
94+ {
7995 MMDB_close (&mmdb);
8096 }
8197}
8298
83- nlohmann::json GeoIP::lookup (const std::string &ip) const {
99+ nlohmann::json GeoIP::lookup (const std::string &ip) const
100+ {
84101 nlohmann::json result;
85- if (!loaded || ip.empty ()) return result;
102+ if (!loaded || ip.empty ())
103+ return result;
86104
87105 int gai_error = 0 , mmdb_error = 0 ;
88106 MMDB_lookup_result_s res = MMDB_lookup_string (&mmdb, ip.c_str (), &gai_error, &mmdb_error);
89- if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !res.found_entry ) {
107+ if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !res.found_entry )
108+ {
90109 return result;
91110 }
92111
93- for (const auto &key : keys) {
112+ for (const auto &key : keys)
113+ {
94114 // Split dotted key path into parts
95115 std::vector<std::string> parts;
96116 std::stringstream ss (key);
97117 std::string part;
98- while (std::getline (ss, part, ' .' )) parts.push_back (part);
118+ while (std::getline (ss, part, ' .' ))
119+ parts.push_back (part);
99120
100- std::vector<const char *> path;
101- for (const auto &p : parts) path.push_back (p.c_str ());
121+ std::vector<const char *> path;
122+ for (const auto &p : parts)
123+ path.push_back (p.c_str ());
102124 path.push_back (nullptr );
103125
104126 MMDB_entry_data_s entry{};
105127 int status = MMDB_aget_value (&res.entry , &entry, path.data ());
106- if (status != MMDB_SUCCESS || !entry.has_data ) continue ;
107-
128+ if (status != MMDB_SUCCESS || !entry.has_data )
129+ continue ;
130+
108131 const std::string &field = parts.back ();
109132
110133 nlohmann::json value = entryToJson (mmdb, entry);
111134
112- if (!value.is_null () && !(value.is_object () && value.empty ())) {
113- if (parts.size () == 1 ) {
135+ if (!value.is_null () && !(value.is_object () && value.empty ()))
136+ {
137+ if (parts.size () == 1 )
138+ {
114139 result[parts[0 ]] = value;
115- } else {
140+ }
141+ else
142+ {
116143 result[field] = value;
117144 }
118145 }
@@ -121,14 +148,18 @@ nlohmann::json GeoIP::lookup(const std::string &ip) const {
121148}
122149
123150void GeoIP::enrich (const std::string &src_ip, const std::string &dst_ip,
124- nlohmann::json &out) const {
125- if (!loaded) return ;
151+ nlohmann::json &out) const
152+ {
153+ if (!loaded)
154+ return ;
126155 auto src = lookup (src_ip);
127- if (!src.empty ()) {
156+ if (!src.empty ())
157+ {
128158 out[" src_geoip2_city" ] = src;
129159 }
130160 auto dst = lookup (dst_ip);
131- if (!dst.empty ()) {
161+ if (!dst.empty ())
162+ {
132163 out[" dst_geoip2_city" ] = dst;
133164 }
134165}
0 commit comments