summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2014-11-09 00:16:58 +0300
committerAndrei Karas <akaras@inbox.ru>2014-11-15 11:47:30 +0300
commit50cb8eeff4386e5546da35c40de93c891af47d0d (patch)
tree49f32efa5b5be350112f5ef1413f4db41d7d9d06
parente4f78d0a13233181cec890b24c77748e32b2fd03 (diff)
downloadhercules-50cb8eeff4386e5546da35c40de93c891af47d0d.tar.gz
hercules-50cb8eeff4386e5546da35c40de93c891af47d0d.tar.bz2
hercules-50cb8eeff4386e5546da35c40de93c891af47d0d.tar.xz
hercules-50cb8eeff4386e5546da35c40de93c891af47d0d.zip
Create geoip interface.
-rw-r--r--src/char/Makefile.in4
-rw-r--r--src/char/char.c2
-rw-r--r--src/char/geoip.c32
-rw-r--r--src/char/geoip.h30
-rw-r--r--src/char/inter.c51
5 files changed, 90 insertions, 29 deletions
diff --git a/src/char/Makefile.in b/src/char/Makefile.in
index 446af8d4f..20d19966e 100644
--- a/src/char/Makefile.in
+++ b/src/char/Makefile.in
@@ -22,11 +22,11 @@ MT19937AR_OBJ = $(MT19937AR_D)/mt19937ar.o
MT19937AR_H = $(MT19937AR_D)/mt19937ar.h
MT19937AR_INCLUDE = -I$(MT19937AR_D)
-CHAR_C = char.c HPMchar.c loginif.c mapif.c inter.c int_auction.c int_elemental.c int_guild.c \
+CHAR_C = char.c HPMchar.c loginif.c mapif.c geoip.c inter.c int_auction.c int_elemental.c int_guild.c \
int_homun.c int_mail.c int_mercenary.c int_party.c int_pet.c \
int_quest.c int_storage.c pincode.c
CHAR_OBJ = $(addprefix obj_sql/, $(patsubst %.c,%.o,$(CHAR_C)))
-CHAR_H = char.h HPMchar.h loginif.h mapif.h inter.h int_auction.h int_elemental.h int_guild.h \
+CHAR_H = char.h HPMchar.h loginif.h mapif.h geoip.h inter.h int_auction.h int_elemental.h int_guild.h \
int_homun.h int_mail.h int_mercenary.h int_party.h int_pet.h \
int_quest.h int_storage.h pincode.h
diff --git a/src/char/char.c b/src/char/char.c
index 24dcd6967..b73d40ee1 100644
--- a/src/char/char.c
+++ b/src/char/char.c
@@ -16,6 +16,7 @@
#include <time.h>
#include "HPMchar.h"
+#include "geoip.h"
#include "int_auction.h"
#include "int_elemental.h"
#include "int_guild.h"
@@ -6019,6 +6020,7 @@ void char_load_defaults(void)
inter_quest_defaults();
inter_storage_defaults();
inter_defaults();
+ geoip_defaults();
}
void char_defaults(void)
diff --git a/src/char/geoip.c b/src/char/geoip.c
new file mode 100644
index 000000000..9a78c1ebd
--- /dev/null
+++ b/src/char/geoip.c
@@ -0,0 +1,32 @@
+// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
+// See the LICENSE file
+// Portions Copyright (c) Athena Dev Teams
+
+#define HERCULES_CORE
+
+#include "geoip.h"
+
+#include <stdlib.h>
+
+#include "../common/cbasetypes.h"
+#include "../common/mmo.h"
+#include "../common/random.h"
+#include "../common/showmsg.h"
+#include "../common/socket.h"
+#include "../common/strlib.h"
+
+struct s_geoip geoip_data;
+
+const char* geoip_getcountry(uint32 ipnum);
+void geoip_final(bool shutdown);
+void geoip_init(void);
+
+void geoip_defaults(void) {
+ geoip = &geoip_s;
+
+ geoip->data = &geoip_data;
+
+ geoip->getcountry = geoip_getcountry;
+ geoip->final = geoip_final;
+ geoip->init = geoip_init;
+}
diff --git a/src/char/geoip.h b/src/char/geoip.h
new file mode 100644
index 000000000..4579554a1
--- /dev/null
+++ b/src/char/geoip.h
@@ -0,0 +1,30 @@
+// Copyright (c) Hercules Dev Team, licensed under GNU GPL.
+// See the LICENSE file
+// Portions Copyright (c) Athena Dev Teams
+
+#ifndef CHAR_GEOIP_H
+#define CHAR_GEOIP_H
+
+#include "../common/cbasetypes.h"
+
+/**
+ * GeoIP information
+ **/
+struct s_geoip {
+ unsigned char *cache; // GeoIP.dat information see geoip->init()
+ bool active;
+};
+
+/* geoip interface */
+struct geoip_interface {
+ struct s_geoip *data;
+ const char* (*getcountry) (uint32 ipnum);
+ void (*final) (bool shutdown);
+ void (*init) (void);
+} geoip_s;
+
+struct geoip_interface *geoip;
+
+void geoip_defaults(void);
+
+#endif /* CHAR_GEOIP_H */
diff --git a/src/char/inter.c b/src/char/inter.c
index 7f49dbf7f..be06bab35 100644
--- a/src/char/inter.c
+++ b/src/char/inter.c
@@ -13,6 +13,7 @@
#include <sys/stat.h> // for stat/lstat/fstat - [Dekamaster/Ultimate GM Tool]
#include "char.h"
+#include "geoip.h"
#include "int_auction.h"
#include "int_elemental.h"
#include "int_guild.h"
@@ -408,27 +409,21 @@ const char * geoip_countryname[GEOIP_MAX_COUNTRIES] = {"Unknown","Asia/Pacific R
"Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa",
"Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider","Other","Aland Islands","Guernsey","Isle of Man","Jersey",
"Saint Barthelemy", "Saint Martin", "Bonaire, Saint Eustatius and Saba", "South Sudan"};
-/**
- * GeoIP information
- **/
-struct s_geoip {
- unsigned char *cache; // GeoIP.dat information see geoip_init()
- bool active;
-} geoip;
/* [Dekamaster/Nightroad] */
/* WHY NOT A DBMAP: There are millions of entries in GeoIP and it has its own algorithm to go quickly through them, a DBMap wouldn't be efficient */
-const char* geoip_getcountry(uint32 ipnum){
+const char* geoip_getcountry(uint32 ipnum)
+{
int depth;
unsigned int x;
const unsigned char *buf;
unsigned int offset = 0;
- if( geoip.active == false )
+ if( geoip->data->active == false )
return geoip_countryname[0];
for (depth = 31; depth >= 0; depth--) {
- buf = geoip.cache + (long)6 *offset;
+ buf = geoip->data->cache + (long)6 *offset;
if (ipnum & (1 << depth)) {
/* Take the right-hand branch */
x = (buf[3*1 + 0] << (0*8))
@@ -460,16 +455,17 @@ const char* geoip_getcountry(uint32 ipnum){
* Disables GeoIP
* frees geoip.cache
**/
-void geoip_final(bool shutdown) {
- if (geoip.cache) {
- aFree(geoip.cache);
- geoip.cache = NULL;
+void geoip_final(bool shutdown)
+{
+ if (geoip->data->cache) {
+ aFree(geoip->data->cache);
+ geoip->data->cache = NULL;
}
- if (geoip.active) {
+ if (geoip->data->active) {
if (!shutdown)
ShowStatus("GeoIP "CL_RED"disabled"CL_RESET".\n");
- geoip.active = false;
+ geoip->data->active = false;
}
}
@@ -478,32 +474,33 @@ void geoip_final(bool shutdown) {
* geoip.cache should be freed after use!
* http://dev.maxmind.com/geoip/legacy/geolite/
**/
-void geoip_init(void) {
+void geoip_init(void)
+{
int i, fno;
char db_type = 1;
unsigned char delim[3];
struct stat bufa;
FILE *db;
- geoip.active = true;
+ geoip->data->active = true;
db = fopen("./db/GeoIP.dat","rb");
if (db == NULL) {
ShowError("geoip_readdb: Error reading GeoIP.dat!\n");
- geoip_final(false);
+ geoip->final(false);
return;
}
fno = fileno(db);
if (fstat(fno, &bufa) < 0) {
ShowError("geoip_readdb: Error stating GeoIP.dat! Error %d\n", errno);
- geoip_final(false);
+ geoip->final(false);
return;
}
- geoip.cache = aMalloc( (sizeof(geoip.cache) * bufa.st_size) );
- if (fread(geoip.cache, sizeof(unsigned char), bufa.st_size, db) != bufa.st_size) {
+ geoip->data->cache = aMalloc( (sizeof(geoip->data->cache) * bufa.st_size) );
+ if (fread(geoip->data->cache, sizeof(unsigned char), bufa.st_size, db) != bufa.st_size) {
ShowError("geoip_cache: Couldn't read all elements!\n");
fclose(db);
- geoip_final(false);
+ geoip->final(false);
return;
}
@@ -532,7 +529,7 @@ void geoip_init(void) {
else
ShowError("geoip_init(): GeoIP is corrupted!\n");
- geoip_final(false);
+ geoip->final(false);
return;
}
ShowStatus("Finished Reading "CL_GREEN"GeoIP"CL_RESET" Database.\n");
@@ -665,7 +662,7 @@ void mapif_parse_accinfo2(bool success, int map_fd, int u_fd, int u_aid, int acc
}
inter->msg_to_fd(map_fd, u_fd, u_aid, "Account e-mail: %s | Birthdate: %s", email, birthdate);
- inter->msg_to_fd(map_fd, u_fd, u_aid, "Last IP: %s (%s)", last_ip, geoip_getcountry(str2ip(last_ip)));
+ inter->msg_to_fd(map_fd, u_fd, u_aid, "Last IP: %s (%s)", last_ip, geoip->getcountry(str2ip(last_ip)));
inter->msg_to_fd(map_fd, u_fd, u_aid, "This user has logged %d times, the last time were at %s", logincount, lastlogin);
inter->msg_to_fd(map_fd, u_fd, u_aid, "-- Character Details --");
@@ -1048,7 +1045,7 @@ int inter_init_sql(const char *file)
inter_mail->sql_init();
inter_auction->sql_init();
- geoip_init();
+ geoip->init();
inter->msg_config_read("conf/messages.conf", false);
return 0;
}
@@ -1068,7 +1065,7 @@ void inter_final(void)
inter_mail->sql_final();
inter_auction->sql_final();
- geoip_final(true);
+ geoip->final(true);
inter->do_final_msg();
return;
}