static GeoIP * geoip_get (void) { int i; GeoIP *gi; /* Return the global if it's ready */ if (likely (_geoip != NULL)) { _geoip_refs += 1; return _geoip; } /* Try to open a GeoIP database */ for (i = 0; i < NUM_DB_TYPES; ++i) { if (GeoIP_db_avail(i)) { gi = GeoIP_open_type (i, GEOIP_STANDARD); if (gi != NULL) { _geoip = gi; _geoip_refs += 1; return _geoip; } } } return NULL; }
int InitTLDStatistics( void ) { TLD *t; SET_SEGV_LOCATION(); tldstatlist = nv_list_create( LISTCOUNT_T_MAX, "StatServ-TLD", nv_ss_tld, NV_FLAGS_RO, NULL ); if( !tldstatlist ) { nlog( LOG_CRITICAL, "Unable to create TLD list" ); return NS_FAILURE; } gi = NULL; /* now open the various DB's */ if( GeoIP_db_avail( GEOIP_COUNTRY_EDITION ) ) { gi = GeoIP_open_type( GEOIP_COUNTRY_EDITION, GEOIP_STANDARD ); if( gi != NULL ) nlog( LOG_NOTICE, "Loaded %s GeoIP Database", GeoIPDBDescription[GEOIP_COUNTRY_EDITION] ); else nlog( LOG_WARNING, "%s Database may be corrupt", GeoIPDBDescription[GEOIP_COUNTRY_EDITION] ); } else { nlog( LOG_WARNING, "GeoIP Database is not available. TLD stats will not be available" ); } t = ns_calloc( sizeof( TLD ) ); ircsnprintf( t->tld, 5, UNKNOWN_COUNTRY_CODE ); strlcpy( t->country, "Unknown", 8 ); lnode_create_append( tldstatlist, t ); DBAOpenTable( TLD_TABLE ); LoadTLDStats(); return NS_SUCCESS; }
static void init_geoip_db(GeoIP **dbp, GeoIPDBTypes edition, GeoIPDBTypes fallback, GeoIPOptions method, const char *name) { char *info; GeoIP *db; REQUIRE(dbp != NULL); db = *dbp; if (db != NULL) { GeoIP_delete(db); db = *dbp = NULL; } if (! GeoIP_db_avail(edition)) { isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER, ISC_LOG_INFO, "GeoIP %s (type %d) DB not available", name, edition); goto fail; } isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER, ISC_LOG_INFO, "initializing GeoIP %s (type %d) DB", name, edition); db = GeoIP_open_type(edition, method); if (db == NULL) { isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER, ISC_LOG_ERROR, "failed to initialize GeoIP %s (type %d) DB%s", name, edition, fallback == 0 ? "geoip matches using this database will fail" : ""); goto fail; } info = GeoIP_database_info(db); if (info != NULL) isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER, ISC_LOG_INFO, "%s", info); *dbp = db; return; fail: if (fallback != 0) init_geoip_db(dbp, fallback, 0, method, name); }
bool is_db_avail(int64 database, bool log_warning = true) { check_enabled(); if (database < 0 || database >= NUM_DB_TYPES) { raise_warning("Database type given is out of bound."); return false; } if (!RuntimeOption::GeoIPAutoreload && s_geoip->gi[database]) { return true; } if (! GeoIP_db_avail(database)) { if (log_warning) raise_warning("Required database not available at %s.", GeoIPDBFileName[database]); return false; } time_t now = time(NULL); // Refresh time is 5 seconds, to avoid to stat files all the time if (now < s_geoip->db_last_checked[database] + 5) { return true; } // We hit refresh timeout, checking file modification time struct stat db_stat; stat(GeoIPDBFileName[GEOIP_COUNTRY_EDITION], &db_stat); s_geoip->db_last_checked[database] = now; if (db_stat.st_mtime <= s_geoip->db_last_modified[database]) { return true; } if (s_geoip->gi[database]) GeoIP_delete(s_geoip->gi[database]); s_geoip->gi[database] = GeoIP_open_type(database, s_geoip->open_flags); s_geoip->db_last_modified[database] = now; Logger::Verbose("Loading %s db", GeoIPDBFileName[database]); if (!s_geoip->gi[database] && log_warning) { raise_warning("Required database not available at %s.", GeoIPDBFileName[database]); } return s_geoip->gi[database] != NULL; }
/* {{{ geoip_change_custom_directory() helper function */ static void geoip_change_custom_directory(char *value) { #if LIBGEOIP_VERSION >= 1004007 GeoIP_cleanup(); #else int i; if (GeoIPDBFileName != NULL) { for (i = 0; i < NUM_DB_TYPES; i++) { if (GeoIPDBFileName[i]) { free(GeoIPDBFileName[i]); } } free(GeoIPDBFileName); GeoIPDBFileName = NULL; } #endif GeoIP_setup_custom_directory(value); GeoIP_db_avail(GEOIP_COUNTRY_EDITION); }
Array f_geoip_db_get_all_info() { Array return_value = Array::Create(); if (!is_db_avail(GEOIP_COUNTRY_EDITION)) return return_value; for (int i=0; i < NUM_DB_TYPES; i++) { if (NULL != GeoIPDBDescription[i]) { Array tmp = Array::Create(); tmp.set("available", GeoIP_db_avail(i)); tmp.set("description", (char *)GeoIPDBDescription[i]); tmp.set("filename", GeoIPDBFileName[i]); return_value.set(i, tmp); } } return return_value; }
int main(int argc, char *argv[]) { char * filename = NULL; char * db_info; GeoIP * gi; int i; char *custom_directory = NULL; char *custom_file = NULL; int version_flag = 0; int charset = GEOIP_CHARSET_UTF8; if (argc < 2) { usage(); exit(1); } i = 1; while (i < argc) { if (strcmp(argv[i], "-v") == 0) { version_flag = 1; } else if (strcmp(argv[i], "-l") == 0) { charset = GEOIP_CHARSET_ISO_8859_1; } else if (strcmp(argv[i], "-i") == 0) { info_flag = 1; } else if (( strcmp(argv[i], "-?" ) == 0 ) || ( strcmp(argv[i], "-h" ) == 0 )) { usage(); exit(0); } else if (strcmp(argv[i], "-f") == 0) { if ((i + 1) < argc) { i++; custom_file = argv[i]; } } else if (strcmp(argv[i], "-d") == 0) { if ((i + 1) < argc) { i++; custom_directory = argv[i]; } } else { filename = argv[i]; } i++; } if (filename == NULL) { usage(); exit(1); } FILE *hostname_file = fopen(filename, "r"); if (hostname_file == NULL) { usage(); exit(1); } if (custom_directory != NULL) { GeoIP_setup_custom_directory(custom_directory); } _GeoIP_setup_dbfilename(); if (custom_file != NULL) { gi = GeoIP_open(custom_file, GEOIP_STANDARD | GEOIP_SILENCE); if (NULL == gi) { printf("%s not available, skipping...\n", custom_file); } else { gi->charset = charset; i = GeoIP_database_edition(gi); if (version_flag == 1) { db_info = GeoIP_database_info(gi); printf("%s: %s\n", GeoIPDBDescription[i], db_info == NULL ? "" : db_info ); free(db_info); } else { char *hostname = NULL; ssize_t read; size_t len = 0; while ((read = getline(&hostname, &len, hostname_file)) != -1) { if (hostname[read - 1] == '\n') { hostname[read - 1] = '\0'; --read; } geoiplookup(gi, hostname, i); } } } GeoIP_delete(gi); } else { /* iterate through different database types */ for (i = 0; i < NUM_DB_TYPES; ++i) { if (GeoIP_db_avail(i)) { gi = GeoIP_open_type(i, GEOIP_STANDARD | GEOIP_SILENCE); if (NULL == gi) { /* Ignore these errors. It's possible * to use the same database name for * different databases. */ ; } else { gi->charset = charset; if (version_flag == 1) { db_info = GeoIP_database_info(gi); printf("%s: %s\n", GeoIPDBDescription[i], db_info == NULL ? "" : db_info ); free(db_info); } else { char *hostname = NULL; ssize_t read; size_t len = 0; while ((read = getline(&hostname, &len, hostname_file)) != -1) { if (hostname[read -1] == '\n') { hostname[read - 1] = '\0'; --read; } geoiplookup(gi, hostname, i); } } } GeoIP_delete(gi); } } } return 0; }
int main(int argc, char *argv[]) { char * hostname = NULL; char * db_info; GeoIP * gi; int i; char *custom_directory = NULL; char *custom_file = NULL; int version_flag = 0; if (argc < 2) { usage(); exit(1); } i = 1; while (i < argc) { if (strcmp(argv[i], "-v") == 0) { version_flag = 1; } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0) { usage(); exit(0); } else if (strcmp(argv[i], "-f") == 0) { if ((i + 1) < argc) { i++; custom_file = argv[i]; } } else if (strcmp(argv[i], "-d") == 0) { if ((i + 1) < argc) { i++; custom_directory = argv[i]; } } else { hostname = argv[i]; } i++; } if (hostname == NULL) { usage(); exit(1); } if (custom_directory != NULL) { GeoIP_setup_custom_directory(custom_directory); } _GeoIP_setup_dbfilename(); if (custom_file != NULL) { gi = GeoIP_open(custom_file, GEOIP_STANDARD); if (NULL == gi) { printf("%s not available, skipping...\n", custom_file); } else { i = GeoIP_database_edition(gi); if (version_flag == 1) { db_info = GeoIP_database_info(gi); printf("%s: %s\n", GeoIPDBDescription[i], db_info == NULL ? "" : db_info ); free(db_info); } else { geoiplookup(gi, hostname, i); } } GeoIP_delete(gi); } else { /* iterate through different database types */ for (i = 0; i < NUM_DB_TYPES; ++i) { if (GeoIP_db_avail(i)) { gi = GeoIP_open_type(i, GEOIP_STANDARD); if (NULL == gi) { /* Ignore these errors. It's possible * to use the same database name for * different databases. */ ; } else { if (version_flag == 1) { db_info = GeoIP_database_info(gi); printf("%s: %s\n", GeoIPDBDescription[i], db_info); free(db_info); } else { geoiplookup(gi, hostname, i); } } GeoIP_delete(gi); } } } return 0; }
int main(int argc, char *argv[]) { struct passwd *pw; int i, rc; socklen_t addrlen = sizeof(struct sockaddr_in6); char str[INET6_ADDRSTRLEN]; #ifdef HAVE_GEOIP_H GeoIP *gi; #endif /* * The next trick is to supply a fake environment variable * FTND_ROOT because this program is started from inetd. * This will setup the variable so InitConfig() will work. * The /etc/passwd must point to the correct homedirectory. */ pw = getpwuid(geteuid()); if (getenv("FTND_ROOT") == NULL) { envptr = xstrcpy((char *)"FTND_ROOT="); envptr = xstrcat(envptr, pw->pw_dir); putenv(envptr); } mypid = getpid(); /* * Read the global configuration data, registrate connection */ InitConfig(); InitMsgs(); InitUser(); InitFidonet(); InitNode(); umask(002); memset(&usrconfig, 0, sizeof(usrconfig)); t_start = time(NULL); InitClient(pw->pw_name, (char *)"ftnnntp", CFG.location, CFG.logfile, CFG.util_loglevel, CFG.error_log, CFG.mgrlog, CFG.debuglog); Syslog(' ', "FTNNNTP v%s", VERSION); IsDoing("Loging in"); #ifdef USE_NEWSGATE WriteError("FTNd is compiled for full newsgate, you cannot use ftnnntp!"); #endif /* * Catch all the signals we can, and ignore the rest. */ for(i = 0; i < NSIG; i++) { if ((i == SIGINT) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV) || (i == SIGTERM) || (i == SIGIOT)) signal(i, (void (*))die); else if (i == SIGCHLD) signal(i, SIG_DFL); else if ((i != SIGKILL) && (i != SIGSTOP)) signal(i, SIG_IGN); } if ((rc = rawport()) != 0) WriteError("Unable to set raw mode"); else { if (getpeername(0,(struct sockaddr*)&peeraddr6,&addrlen) == 0) { /* * Copy IPv4 part into the IPv6 structure. There has to be a better way * to deal with mixed incoming sockets ??? */ memcpy(&peeraddr4, &peeraddr6, sizeof(struct sockaddr_in)); if ((peeraddr6.sin6_family == AF_INET6) && (inet_ntop(AF_INET6, &peeraddr6.sin6_addr, str, sizeof(str)))) { Syslog('+', "Incoming IPv6 connection from %s", str); } else if ((peeraddr4.sin_family == AF_INET) && (inet_ntop(AF_INET, &peeraddr4.sin_addr, str, sizeof(str)))) { Syslog('+', "Incoming IPv4 connection from %s", str); } #ifdef HAVE_GEOIP_H _GeoIP_setup_dbfilename(); if (peeraddr6.sin6_family == AF_INET6) { if (GeoIP_db_avail(GEOIP_COUNTRY_EDITION_V6)) { if ((gi = GeoIP_open_type(GEOIP_COUNTRY_EDITION_V6, GEOIP_STANDARD)) != NULL) { geoiplookup(gi, str, GEOIP_COUNTRY_EDITION_V6); } GeoIP_delete(gi); } } else if (peeraddr6.sin6_family == AF_INET) { if (GeoIP_db_avail(GEOIP_COUNTRY_EDITION)) { if ((gi = GeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_STANDARD)) != NULL) { geoiplookup(gi, str, GEOIP_COUNTRY_EDITION); } GeoIP_delete(gi); } } #endif #ifdef USE_NEWSGATE send_nntp("400 Server closed"); #else if (! check_free()) { send_nntp("400 Server closed"); } else { send_nntp("200 FTNNNTP v%s server ready -- posting allowed", VERSION); nntp(); } #endif } } cookedport(); die(0); return 0; }