extensions/geoip/extension.cpp
changeset 316 1b691b2ff277
child 321 f8b0e8ecea14
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/geoip/extension.cpp	Fri Jan 19 01:50:42 2007 +0000
     1.3 @@ -0,0 +1,83 @@
     1.4 +#include "extension.h"
     1.5 +#include "GeoIP.h"
     1.6 +
     1.7 +GeoIP_Extension g_GeoIP;
     1.8 +GeoIP *gi = NULL;
     1.9 +
    1.10 +SMEXT_LINK(&g_GeoIP);
    1.11 +
    1.12 +bool GeoIP_Extension::SDK_OnLoad(char *error, size_t err_max, bool late)
    1.13 +{
    1.14 +	char *path = "GeoIP.dat"; //:TODO: build a real path here
    1.15 +	//:TODO: log any errors on load.
    1.16 +	gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
    1.17 +
    1.18 +	if (!gi)
    1.19 +	{
    1.20 +		//:TODO: log
    1.21 +		return false;
    1.22 +	}
    1.23 +	return true;
    1.24 +}
    1.25 +
    1.26 +void GeoIP_Extension::SDK_OnUnload()
    1.27 +{
    1.28 +	GeoIP_delete(gi);
    1.29 +	gi = NULL;
    1.30 +}
    1.31 +
    1.32 +/*******************************
    1.33 +*                              *
    1.34 +* GEOIP NATIVE IMPLEMENTATIONS *
    1.35 +*                              *
    1.36 +*******************************/
    1.37 +
    1.38 +inline void StripPort(char *ip)
    1.39 +{
    1.40 +	char *tmp = strchr(ip, ':');
    1.41 +	if (!tmp)
    1.42 +		return;
    1.43 +	*tmp = '\0';
    1.44 +}
    1.45 +
    1.46 +static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
    1.47 +{
    1.48 +	char *ip;
    1.49 +	const char *ccode;
    1.50 +
    1.51 +	pCtx->LocalToString(params[1], &ip);
    1.52 +	StripPort(ip);
    1.53 +
    1.54 +	ccode = GeoIP_country_code_by_addr(gi, ip);
    1.55 +	pCtx->StringToLocal(params[2], 3, (ccode) ? ccode : "er");
    1.56 +
    1.57 +	return 1;
    1.58 +}
    1.59 +
    1.60 +static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
    1.61 +{
    1.62 +	char *ip;
    1.63 +	const char *ccode;
    1.64 +
    1.65 +	pCtx->LocalToString(params[1], &ip);
    1.66 +	StripPort(ip);
    1.67 +
    1.68 +	ccode = GeoIP_country_code3_by_addr(gi, ip);
    1.69 +	pCtx->StringToLocal(params[2], 4, (ccode) ? ccode : "err");
    1.70 +
    1.71 +	return 1;
    1.72 +}
    1.73 +
    1.74 +static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
    1.75 +{
    1.76 +	char *ip;
    1.77 +	const char *ccode;
    1.78 +
    1.79 +	pCtx->LocalToString(params[1], &ip);
    1.80 +	StripPort(ip);
    1.81 +
    1.82 +	ccode = GeoIP_country_name_by_addr(gi, ip);
    1.83 +	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "error");
    1.84 +
    1.85 +	return 1;
    1.86 +}