|
dvander@373
|
1 |
/** |
|
ds@757
|
2 |
* vim: set ts=4 : |
|
ds@1330
|
3 |
* ============================================================================= |
|
ds@757
|
4 |
* SourceMod GeoIP Extension |
|
dvander@1226
|
5 |
* Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved. |
|
ds@1330
|
6 |
* ============================================================================= |
|
dvander@373
|
7 |
* |
|
ds@1330
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
ds@1330
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
ds@1330
|
10 |
* Free Software Foundation. |
|
dvander@1226
|
11 |
* |
|
ds@1330
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
ds@1330
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
ds@1330
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
ds@1330
|
15 |
* details. |
|
dvander@1226
|
16 |
* |
|
ds@1330
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
ds@1330
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
dvander@1226
|
19 |
* |
|
ds@1330
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
ds@1330
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
ds@1330
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
ds@1330
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
ds@1330
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
ds@1330
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
ds@1330
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
ds@1330
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
dvander@373
|
28 |
* |
|
dvander@373
|
29 |
* Version: $Id$ |
|
dvander@373
|
30 |
*/ |
|
dvander@373
|
31 |
|
|
faluco@316
|
32 |
#include "extension.h" |
|
faluco@316
|
33 |
#include "GeoIP.h" |
|
faluco@316
|
34 |
|
|
ds@757
|
35 |
/** |
|
ds@757
|
36 |
* @file extension.cpp |
|
ds@757
|
37 |
* @brief Implement extension code here. |
|
ds@757
|
38 |
*/ |
|
faluco@316
|
39 |
GeoIP_Extension g_GeoIP; |
|
faluco@316
|
40 |
GeoIP *gi = NULL; |
|
faluco@316
|
41 |
|
|
faluco@316
|
42 |
SMEXT_LINK(&g_GeoIP); |
|
faluco@316
|
43 |
|
|
ds@757
|
44 |
bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late) |
|
faluco@316
|
45 |
{ |
|
ds@757
|
46 |
char path[PLATFORM_MAX_PATH]; |
|
faluco@321
|
47 |
|
|
faluco@321
|
48 |
g_pSM->BuildPath(Path_SM, path, sizeof(path), "configs/geoip/GeoIP.dat"); |
|
faluco@316
|
49 |
gi = GeoIP_open(path, GEOIP_MEMORY_CACHE); |
|
faluco@316
|
50 |
|
|
faluco@316
|
51 |
if (!gi) |
|
faluco@316
|
52 |
{ |
|
dvander@1272
|
53 |
snprintf(error, maxlength, "Could not load configs/geoip/GeoIP.dat"); |
|
faluco@316
|
54 |
return false; |
|
faluco@316
|
55 |
} |
|
faluco@321
|
56 |
|
|
faluco@321
|
57 |
g_pShareSys->AddNatives(myself, geoip_natives); |
|
dvander@1573
|
58 |
g_pShareSys->RegisterLibrary(myself, "GeoIP"); |
|
faluco@321
|
59 |
g_pSM->LogMessage(myself, "GeoIP database info: %s", GeoIP_database_info(gi)); |
|
faluco@321
|
60 |
|
|
faluco@316
|
61 |
return true; |
|
faluco@316
|
62 |
} |
|
faluco@316
|
63 |
|
|
faluco@316
|
64 |
void GeoIP_Extension::SDK_OnUnload() |
|
faluco@316
|
65 |
{ |
|
faluco@316
|
66 |
GeoIP_delete(gi); |
|
faluco@316
|
67 |
gi = NULL; |
|
faluco@316
|
68 |
} |
|
faluco@316
|
69 |
|
|
faluco@316
|
70 |
/******************************* |
|
faluco@316
|
71 |
* * |
|
faluco@316
|
72 |
* GEOIP NATIVE IMPLEMENTATIONS * |
|
faluco@316
|
73 |
* * |
|
faluco@316
|
74 |
*******************************/ |
|
faluco@316
|
75 |
|
|
faluco@316
|
76 |
inline void StripPort(char *ip) |
|
faluco@316
|
77 |
{ |
|
faluco@316
|
78 |
char *tmp = strchr(ip, ':'); |
|
faluco@316
|
79 |
if (!tmp) |
|
faluco@316
|
80 |
return; |
|
faluco@316
|
81 |
*tmp = '\0'; |
|
faluco@316
|
82 |
} |
|
faluco@316
|
83 |
|
|
faluco@316
|
84 |
static cell_t sm_Geoip_Code2(IPluginContext *pCtx, const cell_t *params) |
|
faluco@316
|
85 |
{ |
|
faluco@316
|
86 |
char *ip; |
|
faluco@316
|
87 |
const char *ccode; |
|
faluco@316
|
88 |
|
|
faluco@316
|
89 |
pCtx->LocalToString(params[1], &ip); |
|
faluco@316
|
90 |
StripPort(ip); |
|
faluco@316
|
91 |
|
|
faluco@316
|
92 |
ccode = GeoIP_country_code_by_addr(gi, ip); |
|
faluco@316
|
93 |
|
|
dvander@1293
|
94 |
pCtx->StringToLocal(params[2], 3, ccode ? ccode : ""); |
|
dvander@1293
|
95 |
|
|
dvander@1293
|
96 |
return ccode ? 1 : 0; |
|
faluco@316
|
97 |
} |
|
faluco@316
|
98 |
|
|
faluco@316
|
99 |
static cell_t sm_Geoip_Code3(IPluginContext *pCtx, const cell_t *params) |
|
faluco@316
|
100 |
{ |
|
faluco@316
|
101 |
char *ip; |
|
faluco@316
|
102 |
const char *ccode; |
|
faluco@316
|
103 |
|
|
faluco@316
|
104 |
pCtx->LocalToString(params[1], &ip); |
|
faluco@316
|
105 |
StripPort(ip); |
|
faluco@316
|
106 |
|
|
faluco@316
|
107 |
ccode = GeoIP_country_code3_by_addr(gi, ip); |
|
dvander@1293
|
108 |
pCtx->StringToLocal(params[2], 4, ccode ? ccode : ""); |
|
faluco@316
|
109 |
|
|
dvander@1293
|
110 |
return ccode ? 1 : 0; |
|
faluco@316
|
111 |
} |
|
faluco@316
|
112 |
|
|
faluco@316
|
113 |
static cell_t sm_Geoip_Country(IPluginContext *pCtx, const cell_t *params) |
|
faluco@316
|
114 |
{ |
|
faluco@316
|
115 |
char *ip; |
|
faluco@316
|
116 |
const char *ccode; |
|
faluco@316
|
117 |
|
|
faluco@316
|
118 |
pCtx->LocalToString(params[1], &ip); |
|
faluco@316
|
119 |
StripPort(ip); |
|
faluco@316
|
120 |
|
|
faluco@316
|
121 |
ccode = GeoIP_country_name_by_addr(gi, ip); |
|
dvander@1293
|
122 |
pCtx->StringToLocal(params[2], params[3], (ccode) ? ccode : ""); |
|
faluco@316
|
123 |
|
|
dvander@1293
|
124 |
return ccode ? 1 : 0; |
|
faluco@316
|
125 |
} |
|
faluco@321
|
126 |
|
|
faluco@321
|
127 |
const sp_nativeinfo_t geoip_natives[] = |
|
faluco@321
|
128 |
{ |
|
faluco@321
|
129 |
{"GeoipCode2", sm_Geoip_Code2}, |
|
faluco@321
|
130 |
{"GeoipCode3", sm_Geoip_Code3}, |
|
faluco@321
|
131 |
{"GeoipCountry", sm_Geoip_Country}, |
|
faluco@321
|
132 |
{NULL, NULL}, |
|
dvander@355
|
133 |
}; |