Exemple #1
0
static PyObject * GeoIP_country_code_by_name_Py(PyObject *self, PyObject *args) {
  char * name;
  const char * retval;
  GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
  if (!PyArg_ParseTuple(args, "s", &name)) {
    return NULL;
  }
  retval = GeoIP_country_code_by_name(GeoIP->gi, name);
  return Py_BuildValue("s", retval);
}
Exemple #2
0
static gboolean
geoip_parser_process(LogParser *s, LogMessage **pmsg,
                     const LogPathOptions *path_options,
                     const gchar *input, gsize input_len)
{
  GeoIPParser *self = (GeoIPParser *) s;
  LogMessage *msg = log_msg_make_writable(pmsg, path_options);
  GeoIPRecord *record;
  SBGString *value;

  if (!self->dest.country_code &&
      !self->dest.latitude &&
      !self->dest.longitude)
    return TRUE;

  record = GeoIP_record_by_name(self->gi, input);

  if (!record)
    {
      const char *country;

      country = GeoIP_country_code_by_name(self->gi, input);
      if (country)
        log_msg_set_value_by_name(msg, self->dest.country_code,
                                  country,
                                  strlen(country));

      return TRUE;
    }

  if (record->country_code)
    log_msg_set_value_by_name(msg, self->dest.country_code,
                              record->country_code,
                              strlen(record->country_code));

  value = sb_gstring_acquire();

  g_string_printf(sb_gstring_string(value), "%f",
                  record->latitude);
  log_msg_set_value_by_name(msg, self->dest.latitude,
                            sb_gstring_string(value)->str,
                            sb_gstring_string(value)->len);

  g_string_printf(sb_gstring_string(value), "%f",
                  record->longitude);
  log_msg_set_value_by_name(msg, self->dest.longitude,
                            sb_gstring_string(value)->str,
                            sb_gstring_string(value)->len);

  GeoIPRecord_delete(record);
  sb_gstring_release(value);

  return TRUE;
}
Exemple #3
0
Variant f_geoip_country_code_by_name(CStrRef hostname) {
  if (!is_db_avail(GEOIP_COUNTRY_EDITION))
    return null_variant;

  const char * country_code = GeoIP_country_code_by_name(s_geoip->gi[GEOIP_COUNTRY_EDITION], hostname.data());
  if (country_code == NULL) {
    raise_notice("Host %s not found", hostname.data());
    return false;
  }

  return String(country_code, CopyString);
}
Exemple #4
0
static gboolean
tf_geoip(LogMessage *msg, gint argc, GString *argv[], GString *result)
{
  const char *country;

  if (argc != 1)
    {
      msg_debug("tfgeoip takes only one argument",
                evt_tag_int("count", argc),
                NULL);
      return FALSE;
    }

  country = GeoIP_country_code_by_name(local_state->gi, argv[0]->str);
  g_string_append(result, country);

  return TRUE;
}
Exemple #5
0
ServerInfo::ServerInfo(MainWindow* m, QWidget *parent) :
    QWidget(parent),mainWindow(m),gi(NULL),latitude(0.0),longitude(0.0),
    settingsFile(QLatin1String("config.ini")),
    ui(new Ui::ServerInfo)
{

    qDebug() << __PRETTY_FUNCTION__ << " called ...";

    ui->setupUi(this);


/*****************************************/
/* GeoIP sanity checking */
/*****************************************/

/* FIXME: Proxy checker module (worker.cpp) already loads GeoIP.dat databse.
 *
 * AFAIK, GeoLiteCity.dat also provides country like GeoIP.dat.
 * So in theory you should only need GeoLiteCity.dat database and not both
 * (GeoIP.dat and GeoLiteCity.dat). Otherwise this is a waste of disk space and RAM.
 *
 * Just have to decide where to load & initialize GeoLiteCity.dat so that *both*
 * modules (this and worker.cpp)can share it ... */

    if(this->gi == NULL) {
        this->gi = GeoIP_open("./GeoLiteCity.dat",GEOIP_INDEX_CACHE | GEOIP_CHECK_CACHE);
        if(gi == NULL) {
            qDebug() << "Error opening database!!!";
        }

        /* make sure GeoIP deals with invalid query gracefully */
        if (GeoIP_country_code_by_addr(this->gi, NULL) != NULL) {
            qDebug() <<  "Invalid Query test failed, got non NULL, expected NULL";
        }

        if (GeoIP_country_code_by_name(this->gi, NULL) != NULL) {
            qDebug() << "Invalid Query test failed, got non NULL, expected NULL";
        }
    }
}
Exemple #6
0
int main(void)
{
      const char *ipAddress = "193.140.74.29";
      char expectedCountry[3];
      const char *returnedCountry;
      GeoIP *gi;
      int i;
 
      if (0 == i) {
	  /* Read from filesystem, check for updated file */
	  gi = GeoIP_open("/usr/share/GeoIP/GeoIP.dat",  GEOIP_STANDARD | GEOIP_CHECK_CACHE);
      } else {
	  /* Read from memory, faster but takes up more memory */
	  gi = GeoIP_open("/usr/share/GeoIP/GeoIP.dat", GEOIP_MEMORY_CACHE);
      }

      if (gi == NULL) {
	fprintf(stderr, "Error opening database\n");
	exit(1);
      }
  
      returnedCountry = GeoIP_country_code_by_addr(gi, NULL);
      if (returnedCountry != NULL) {
	  fprintf(stderr, "Invalid Query test failed, got non NULL, expected NULL\n");
	  exit(EXIT_FAILURE);  
      }
  
      returnedCountry = GeoIP_country_code_by_name(gi, NULL);
      if (returnedCountry != NULL) {
	fprintf(stderr,"Invalid Query test failed, got non NULL, expected NULL\n");
      }
  
      returnedCountry = GeoIP_country_code3_by_addr(gi, ipAddress);
      printf("%s => %s\n", ipAddress, returnedCountry);
      
      return 0;
}
Exemple #7
0
int get_flux(const char * host, struct flux_entry ** results)
{
    struct addrinfo hints, *res, *cur;
    int errcode;
    int count;
    uint32_t isflux=0;
    char * addr_str=NULL;
    void * addr_ptr;
    GeoIP * gi;
    
    *results = NULL;

    memset (&hints, 0, sizeof (hints));
//    memset (&addr_str, 0, sizeof(addr_str));

    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags |= AI_CANONNAME;

    errcode = getaddrinfo (host, NULL, &hints, &res);
    if (errcode < 0)
    {
        return -1;
    }

    /* count hints and allocate return buffer
     */
    for (count = 1, cur = res; cur; cur = cur->ai_next, ++count)
        ;

    if (count == 1)
    {
        /* no entries found, so just fail here
         */
        return -1;
    }
    *results = calloc(count, sizeof(struct flux_entry));
    if (!*results)
    {
        return -1;
    }
//   memset(*results, 0, sizeof(struct flux_entry) * count);
    // init GeoIP //
    gi = GeoIP_new(GEOIP_STANDARD);
    for (isflux = 0, cur = res; cur; cur = cur ->ai_next, ++isflux)
    {
        int size;

        switch (cur->ai_family)
        {
            case AF_INET:
                addr_ptr = &((struct sockaddr_in *) cur->ai_addr)->sin_addr;
                size = sizeof ("xxx.xxx.xxx.xxx");
                break;
            case AF_INET6:
                addr_ptr = &((struct sockaddr_in6 *) cur->ai_addr)->sin6_addr;
                size = sizeof ("xx:xx:xx:xx:xx:xx:xx:xx");
                break;
            default:
                continue;
        }
        addr_str = malloc(size);
        if (!addr_str) continue;

        inet_ntop (cur->ai_family, addr_ptr, addr_str, size);
        (*results)[isflux].addr_str = addr_str;

        if((GeoIP_country_code_by_name(gi, addr_str)) == NULL)
        {
            break;
        }
        else
        {
            memcpy(  &(*results)[isflux].cc,
                     GeoIP_country_code_by_name(gi, addr_str), 
                     sizeof (**results).cc);
        }
    }

    (*results)[isflux].addr_str = NULL;
    (*results)[isflux].cc[0] = 0;
    (*results)[isflux].cc[1] = 0;

    GeoIP_delete(gi);

    return isflux;
}
Exemple #8
0
int main () {
  FILE *f;
  char ipAddress[30];
  char expectedCountry[3];
  char expectedCountry3[4];
  const char * returnedCountry;
	GeoIP * gi;
	int failed = 0;
	int test_num = 1;

	int i;
	for (i = 0; i < 2; ++i) {
		if (0 == i) {
			/* Read from filesystem, check for updated file */
			gi = GeoIP_open(SRCDIR"/data/GeoIP.dat", GEOIP_STANDARD | GEOIP_CHECK_CACHE);
		} else {
			/* Read from memory, faster but takes up more memory */
			gi = GeoIP_open(SRCDIR"/data/GeoIP.dat", GEOIP_MEMORY_CACHE);
		}

		if (gi == NULL) {
			fprintf(stderr, "Error opening database\n");
			exit(1);
		}

		/* make sure GeoIP deals with invalid query gracefully */
		returnedCountry = GeoIP_country_code_by_addr(gi,NULL);
		if (returnedCountry != NULL) {
			fprintf(stderr,"Invalid Query test failed, got non NULL, expected NULL\n");
			failed = 1;
		}

		returnedCountry = GeoIP_country_code_by_name(gi,NULL);
		if (returnedCountry != NULL) {
			fprintf(stderr,"Invalid Query test failed, got non NULL, expected NULL\n");
			failed = 1;
		}

		f = fopen(SRCDIR"/test/country_test.txt","r");

		while (fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3) != EOF) {
			returnedCountry = GeoIP_country_code_by_addr(gi,ipAddress);
			if (returnedCountry == NULL || strcmp(returnedCountry, expectedCountry) != 0) {
				fprintf(stderr,"Test addr %d for %s failed, got %s, expected %s\n",test_num,ipAddress,returnedCountry,expectedCountry);
				failed = 1;
			}
			returnedCountry = GeoIP_country_code_by_name(gi,ipAddress);
			if (returnedCountry == NULL || strcmp(returnedCountry, expectedCountry) != 0) {
				fprintf(stderr,"Test name %d for %s failed, got %s, expected %s\n",test_num,ipAddress,returnedCountry,expectedCountry);
				failed = 1;
			}
			returnedCountry = GeoIP_country_code3_by_addr(gi,ipAddress);
			if (returnedCountry == NULL || strcmp(returnedCountry, expectedCountry3) != 0) {
				fprintf(stderr,"Test addr %d for %s failed, got %s, expected %s\n",test_num,ipAddress,returnedCountry,expectedCountry);
				failed = 1;
			}
			returnedCountry = GeoIP_country_code3_by_name(gi,ipAddress);
			if (returnedCountry == NULL || strcmp(returnedCountry, expectedCountry3) != 0) {
				fprintf(stderr,"Test name %d for %s failed, got %s, expected %s\n",test_num,ipAddress,returnedCountry,expectedCountry);
				failed = 1;
			}
			test_num++;
		}
		fclose(f);

		f = fopen(SRCDIR"/test/country_test2.txt","r");
		while (fscanf(f, "%s%s", ipAddress, expectedCountry ) != EOF) {
			returnedCountry = GeoIP_country_code_by_addr(gi,ipAddress);
			if (returnedCountry == NULL || strcmp(returnedCountry, expectedCountry) != 0) {
				fprintf(stderr,"Test addr %d %s failed, got %s, expected %s\n",test_num,ipAddress,returnedCountry,expectedCountry);
				failed = 1;
			}
			test_num++;
		}
		fclose(f);

		f = fopen(SRCDIR"/test/country_test_name.txt","r");
		while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) {
			returnedCountry = GeoIP_country_code_by_name(gi,ipAddress);
			if (returnedCountry == NULL || strcmp(returnedCountry, expectedCountry) != 0) {
				fprintf(stderr,"Test addr %d %s failed, got %s, expected %s\n",test_num,ipAddress,returnedCountry,expectedCountry);
				failed = 1;
			}
			test_num++;
		}

		fclose(f);
		GeoIP_delete(gi);
	}
	return failed;
}
gint
main (gint argc, gchar **argv)
{
    GeoIP *geoip;
    const gchar *remote_addr;
    const gchar *path_info;
    gchar **path_parts;
    const gchar *country;
    gchar *action;
    gchar *input;
    gchar *domain;
    gchar *affiliate_code;
    gchar *p;
    gchar *dest_url = NULL;
    gboolean direct_url = FALSE;

    country = NULL;
    input = NULL;
    action = NULL;

    remote_addr = g_getenv ("REMOTE_ADDR");

    path_parts = g_strsplit (path_info = g_getenv ("PATH_INFO"), "/", 4);
    if (path_parts != NULL &&
        path_parts[0] != NULL &&
        (country = path_parts[1]) != NULL &&
        (action = path_parts[2]) != NULL) {
        input = path_parts[3];
    }

    if ((p = strrchr (remote_addr, ':')) != NULL) {
        remote_addr = p + 1;
    }

    if (country == NULL || strcmp (country, "geo") == 0) {
        geoip = GeoIP_new (GEOIP_STANDARD);
        country = GeoIP_country_code_by_name (geoip, remote_addr);
    }

    // We ask that no one change these affiliate codes. ALL (100%) revenue
    // generated by these affiliate IDs is sent directly to the GNOME
    // Foundation. The GNOME Foundation controls/owns these affiliate IDs.
    // Please help support Free Software through the GNOME Foundation!
    if (country == NULL || strcmp (country, "US") == 0) {
        domain = "com";
        affiliate_code = "banshee-20";
    } else if (strcmp (country, "FR") == 0) {
        domain = "fr";
        affiliate_code = "banshee-fr-21";
    } else if (strcmp (country, "UK") == 0) {
        domain = "co.uk";
        affiliate_code = "banshee-uk-21";
    } else if (strcmp (country, "DE") == 0 ||
        strcmp (country, "CH") == 0 ||
        strcmp (country, "AT") == 0) {
        domain = "de";
        affiliate_code = "banshee-de-21";
    } else if (strcmp (country, "JP") == 0) {
        domain = "co.jp";
        affiliate_code = "banshee-jp-22";
    } else {
        domain = "com";
        affiliate_code = "banshee-20";
    }

    if (strcmp (action, "search") == 0) {
        dest_url = g_strdup_printf ("http://www.amazon.%s/s/ref=nb_sb_noss?url=search-alias%%3Ddigital-music&field-keywords=%s", domain, input);
    } else if (strcmp (action, "sign_out") == 0) {
        dest_url = g_strdup_printf ("http://www.amazon.%s/gp/help/customer/sign-out.html/ref=ya__lo?ie=UTF8&returnPath=%%2Fmp3", domain);
    } else if (strcmp (action, "about") == 0) {
        dest_url = g_strdup ("http://banshee.fm/about/revenue/");
        direct_url = TRUE;
    }

    if (dest_url == NULL) {
        dest_url = g_strdup_printf ("http://www.amazon.%s/mp3/", domain);
    }

    if (direct_url) {
        printf ("Location: %s" "\n\n", dest_url);
    } else {
        printf ("Location: http://www.amazon.%s/gp/redirect.html?ie=UTF8&location=%s&tag=%s" "\n\n",
            domain,
            g_uri_escape_string (dest_url, NULL, TRUE),
            affiliate_code
        );
    }

    return 0;
}
    GeoIP * gi = GeoIP_open(PARAM(0), PARAM_INT(1));
    RETURN_NUMBER((SYS_INT)gi)
END_IMPL
//------------------------------------------------------------------------
CONCEPT_FUNCTION_IMPL(GeoIP_new, 1)
    T_NUMBER(GeoIP_new, 0)

    GeoIP * gi = GeoIP_new(PARAM_INT(0));
    RETURN_NUMBER((SYS_INT)gi)
END_IMPL
//------------------------------------------------------------------------
CONCEPT_FUNCTION_IMPL(GeoIP_country_code_by_name, 2)
    T_HANDLE(GeoIP_country_code_by_name, 0)
    T_STRING(GeoIP_country_code_by_name, 1)

    const char *s = GeoIP_country_code_by_name((GeoIP *)PARAM_INT(0), PARAM(1));
    if (s) {
        RETURN_STRING(s)
    } else {
        RETURN_STRING("")
    }
END_IMPL
//------------------------------------------------------------------------
CONCEPT_FUNCTION_IMPL(GeoIP_setup_custom_directory, 1)
    T_STRING(GeoIP_setup_custom_directory, 0)     // char*

    GeoIP_setup_custom_directory((char *)PARAM(0));
    RETURN_NUMBER(0)
END_IMPL
//------------------------------------------------------------------------
CONCEPT_FUNCTION_IMPL(GeoIP_db_avail, 1)