/* * ip2country.c - MaxMind GeoIP dlfunc for Exim * https://mta.org.ua/exim-4.88-conf/dlfunc/ip2country/ip2country.c * * Copyright (C) 2006-2018 Victor Ustugov * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Based on source code from http://www.ols.es/exim/dlext/ * by David Saez */ #include "local_scan.h" #include #include #include static GeoIP *gi=NULL; int ip2country(uschar **yield, int argc, uschar *argv[]) { uschar *s; if (argc != 1) { *yield = string_copy((uschar *)"Bad number of arguments"); return ERROR; } if (!gi) { gi = GeoIP_new(GEOIP_STANDARD); if (!gi) { *yield = string_copy((uschar *)"Cannot initialize GeoIP"); return ERROR; } } s = (uschar *)GeoIP_country_code_by_addr(gi, (char *)argv[0]); if (!s) { *yield = string_copy((uschar *)"--"); } else { *yield = string_copy(s); } return OK; } int ip2continent(uschar **yield, int argc, uschar *argv[]) { uschar *s; uschar *country; if (argc != 1) { *yield = string_copy((uschar *)"Bad number of arguments"); return ERROR; } if (!gi) { gi = GeoIP_new(GEOIP_STANDARD); if (!gi) { *yield = string_copy((uschar *)"Cannot initialize GeoIP"); return ERROR; } } country = (uschar *)GeoIP_country_code_by_addr(gi, (char *)argv[0]); if (!country) { *yield = string_copy((uschar *)"--"); } else { s = (uschar *)GeoIP_continent_by_id(GeoIP_id_by_code((char *)country)); if (!s) { *yield = string_copy((uschar *)"--"); } else { *yield = string_copy(s); } } return OK; }