diff --git a/llarp/util/geoip.cpp b/llarp/util/geoip.cpp index e75703d..ec151ae 100644 --- a/llarp/util/geoip.cpp +++ b/llarp/util/geoip.cpp @@ -40,12 +40,19 @@ namespace llarp::util Pimpl() { + // GeoIP_new returns nullptr when the database file cannot be opened; + // every later call must treat that as "no geoip available" rather than + // handing the null handle to libGeoIP (which crashes on it). geoip_ptr = ::GeoIP_new(GEOIP_STANDARD); + if (geoip_ptr == nullptr) + log::warning( + logcat, "GeoIP database could not be opened; geoip based peer filtering is disabled"); } ~Pimpl() { - ::GeoIP_delete(geoip_ptr); + if (geoip_ptr) + ::GeoIP_delete(geoip_ptr); } static constexpr auto CacheEntryDuration = 30min; @@ -54,6 +61,8 @@ namespace llarp::util std::optional get_country_code(const SockAddr& addr, Cache_t& cache) const { + if (geoip_ptr == nullptr) + return std::nullopt; // no database; warned at startup std::string country_code{}; if (auto itr = cache.find(addr); itr != cache.end()) country_code = itr->second.country_code;