extensions/geoip/extension.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jan 26 02:04:07 2007 +0000 (2007-01-26)
changeset 373 bf3335acefdd
parent 355 bbe250abf402
child 757 b799caa9a190
permissions -rw-r--r--
added license headers to extensions
dvander@373
     1
/**
dvander@373
     2
 * GeoIP SourceMod Extension, (C)2007 AlliedModders LLC.  All rights reserved.
dvander@373
     3
 *
dvander@373
     4
 * This program is free software; you can redistribute it and/or
dvander@373
     5
 * modify it under the terms of the GNU General Public License
dvander@373
     6
 * as published by the Free Software Foundation; either version 2
dvander@373
     7
 * of the License, or (at your option) any later version.
dvander@373
     8
 * This program is distributed in the hope that it will be useful,
dvander@373
     9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
dvander@373
    10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dvander@373
    11
 * GNU General Public License for more details.
dvander@373
    12
 * 
dvander@373
    13
 * You should have received a copy of the GNU General Public License
dvander@373
    14
 * along with this program; if not, write to the Free Software
dvander@373
    15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
dvander@373
    16
 *
dvander@373
    17
 * Version: $Id$
dvander@373
    18
 */
dvander@373
    19
faluco@316
    20
#include "extension.h"
faluco@316
    21
#include "GeoIP.h"
faluco@316
    22
faluco@316
    23
GeoIP_Extension g_GeoIP;
faluco@316
    24
GeoIP *gi = NULL;
faluco@316
    25
faluco@316
    26
SMEXT_LINK(&g_GeoIP);
faluco@316
    27
faluco@316
    28
bool GeoIP_Extension::SDK_OnLoad(char *error, size_t err_max, bool late)
faluco@316
    29
{
faluco@321
    30
	char path[PLATFORM_MAX_PATH+1];
faluco@321
    31
faluco@321
    32
	g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat");
faluco@316
    33
	gi = GeoIP_open(path, GEOIP_MEMORY_CACHE);
faluco@316
    34
faluco@316
    35
	if (!gi)
faluco@316
    36
	{
faluco@321
    37
		snprintf(error, err_max, "Failed to instantiate GeoIP!");
faluco@316
    38
		return false;
faluco@316
    39
	}
faluco@321
    40
faluco@321
    41
	g_pShareSys->AddNatives(myself, geoip_natives);
faluco@321
    42
	g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi));
faluco@321
    43
faluco@316
    44
	return true;
faluco@316
    45
}
faluco@316
    46
faluco@316
    47
void GeoIP_Extension::SDK_OnUnload()
faluco@316
    48
{
faluco@316
    49
	GeoIP_delete(gi);
faluco@316
    50
	gi = NULL;
faluco@316
    51
}
faluco@316
    52
faluco@316
    53
/*******************************
faluco@316
    54
*                              *
faluco@316
    55
* GEOIP NATIVE IMPLEMENTATIONS *
faluco@316
    56
*                              *
faluco@316
    57
*******************************/
faluco@316
    58
faluco@316
    59
inline void StripPort(char *ip)
faluco@316
    60
{
faluco@316
    61
	char *tmp = strchr(ip, ':');
faluco@316
    62
	if (!tmp)
faluco@316
    63
		return;
faluco@316
    64
	*tmp = '\0';
faluco@316
    65
}
faluco@316
    66
faluco@316
    67
static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params)
faluco@316
    68
{
faluco@316
    69
	char *ip;
faluco@316
    70
	const char *ccode;
faluco@316
    71
faluco@316
    72
	pCtx->LocalToString(params[1], &ip);
faluco@316
    73
	StripPort(ip);
faluco@316
    74
faluco@316
    75
	ccode = GeoIP_country_code_by_addr(gi, ip);
faluco@316
    76
	pCtx->StringToLocal(params[2], 3, (ccode) ? ccode : "er");
faluco@316
    77
faluco@316
    78
	return 1;
faluco@316
    79
}
faluco@316
    80
faluco@316
    81
static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params)
faluco@316
    82
{
faluco@316
    83
	char *ip;
faluco@316
    84
	const char *ccode;
faluco@316
    85
faluco@316
    86
	pCtx->LocalToString(params[1], &ip);
faluco@316
    87
	StripPort(ip);
faluco@316
    88
faluco@316
    89
	ccode = GeoIP_country_code3_by_addr(gi, ip);
faluco@316
    90
	pCtx->StringToLocal(params[2], 4, (ccode) ? ccode : "err");
faluco@316
    91
faluco@316
    92
	return 1;
faluco@316
    93
}
faluco@316
    94
faluco@316
    95
static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params)
faluco@316
    96
{
faluco@316
    97
	char *ip;
faluco@316
    98
	const char *ccode;
faluco@316
    99
faluco@316
   100
	pCtx->LocalToString(params[1], &ip);
faluco@316
   101
	StripPort(ip);
faluco@316
   102
faluco@316
   103
	ccode = GeoIP_country_name_by_addr(gi, ip);
faluco@316
   104
	pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : "error");
faluco@316
   105
faluco@316
   106
	return 1;
faluco@316
   107
}
faluco@321
   108
faluco@321
   109
const sp_nativeinfo_t geoip_natives[] = 
faluco@321
   110
{
faluco@321
   111
	{"GeoipCode2",			sm_Geoip_Code2},
faluco@321
   112
	{"GeoipCode3",			sm_Geoip_Code3},
faluco@321
   113
	{"GeoipCountry",		sm_Geoip_Country},
faluco@321
   114
	{NULL,					NULL},
dvander@355
   115
};
dvander@355
   116