diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/GNUmakefile | 4 | ||||
-rw-r--r-- | src/common/core.cpp | 6 | ||||
-rw-r--r-- | src/common/db.hpp | 3 | ||||
-rw-r--r-- | src/common/grfio.cpp | 6 | ||||
-rw-r--r-- | src/common/md5calc.cpp | 23 | ||||
-rw-r--r-- | src/common/md5calc.hpp | 4 | ||||
-rw-r--r-- | src/common/mmo.hpp | 8 | ||||
-rw-r--r-- | src/common/mt_rand.cpp | 1 | ||||
-rw-r--r-- | src/common/nullpo.cpp | 17 | ||||
-rw-r--r-- | src/common/utils.cpp | 97 | ||||
-rw-r--r-- | src/common/utils.hpp | 8 |
11 files changed, 143 insertions, 34 deletions
diff --git a/src/common/GNUmakefile b/src/common/GNUmakefile index 555f81e..d8d841f 100644 --- a/src/common/GNUmakefile +++ b/src/common/GNUmakefile @@ -1,7 +1,7 @@ .SUFFIXES: all: - make -C ../.. common + ${MAKE} -C ../.. common clean: rm -r ../../obj/common/ %:: - make -C ../.. obj/common/$@ + ${MAKE} -C ../.. obj/common/$@ diff --git a/src/common/core.cpp b/src/common/core.cpp index 38b2260..db26e31 100644 --- a/src/common/core.cpp +++ b/src/common/core.cpp @@ -11,10 +11,6 @@ #include "mt_rand.hpp" #include "nullpo.hpp" -/// Defined by each server -extern int do_init (int, char **); -extern void term_func (void); - // Added by Gabuzomeu // // This is an implementation of signal() using sigaction() for portability. @@ -22,7 +18,7 @@ extern void term_func (void); // Programming in the UNIX Environment_. // typedef void (*sigfunc)(int); -sigfunc compat_signal (int signo, sigfunc func) +static sigfunc compat_signal (int signo, sigfunc func) { struct sigaction sact, oact; diff --git a/src/common/db.hpp b/src/common/db.hpp index 145ba13..62125d8 100644 --- a/src/common/db.hpp +++ b/src/common/db.hpp @@ -22,6 +22,9 @@ typedef union db_key_t char *ms __attribute__((deprecated)); const char* s; numdb_key_t i; + + db_key_t(numdb_key_t n) : i(n) {} + db_key_t(const char * z) : s(z) {} } db_key_t; typedef void* db_val_t; typedef uint32_t hash_t; diff --git a/src/common/grfio.cpp b/src/common/grfio.cpp index 1b89b18..dd1e707 100644 --- a/src/common/grfio.cpp +++ b/src/common/grfio.cpp @@ -29,7 +29,9 @@ static uint16_t filelist_entrys = 0; static uint16_t filelist_maxentry = 0; /// First index of the given hash, into the filelist[] array -static int16_t filelist_hash[256] = {[0 ... 255] = -1}; +#define l -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 +static int16_t filelist_hash[256] = {l,l,l,l,l,l,l,l,l,l,l,l,l,l,l,l}; +#undef l /// Hash a filename static uint8_t filehash (const char *fname) @@ -45,6 +47,7 @@ static uint8_t filehash (const char *fname) } /// Find the filelist entry for the given filename, or NULL if it is not +static FILELIST *filelist_find (const char *fname) { int16_t index = filelist_hash[filehash (fname)]; @@ -98,6 +101,7 @@ static FILELIST *filelist_modify (FILELIST * entry) /// Change fname data/*.gat to lfname data/*.wlk // TODO even if the file exists, don't keep reopening it every time one loads +static void grfio_resnametable (const char *fname, char *lfname) { char restable[] = "data/resnametable.txt"; diff --git a/src/common/md5calc.cpp b/src/common/md5calc.cpp index f00861d..b0f8e5f 100644 --- a/src/common/md5calc.cpp +++ b/src/common/md5calc.cpp @@ -309,31 +309,26 @@ bool pass_ok(const char *password, const char *crypted) { // [M|h]ashes up an IP address and a secret key // to return a hopefully unique masked IP. -in_addr_t MD5_ip(char *secret, in_addr_t ip) +struct in_addr MD5_ip(char *secret, struct in_addr ip) { char ipbuf[32]; uint8_t obuf[16]; union { - struct bytes { - uint8_t b1; - uint8_t b2; - uint8_t b3; - uint8_t b4; - } bytes; - in_addr_t ip; + uint8_t bytes[4]; + struct in_addr ip; } conv; // MD5sum a secret + the IP address memset(&ipbuf, 0, sizeof(ipbuf)); - snprintf(ipbuf, sizeof(ipbuf), "%lu%s", (unsigned long)ip, secret); + snprintf(ipbuf, sizeof(ipbuf), "%u%s", ip.s_addr, secret); /// TODO stop it from being a cstring MD5_to_bin(MD5_from_cstring(ipbuf), obuf); - // Fold the md5sum to 32 bits, pack the bytes to an in_addr_t - conv.bytes.b1 = obuf[0] ^ obuf[1] ^ obuf[8] ^ obuf[9]; - conv.bytes.b2 = obuf[2] ^ obuf[3] ^ obuf[10] ^ obuf[11]; - conv.bytes.b3 = obuf[4] ^ obuf[5] ^ obuf[12] ^ obuf[13]; - conv.bytes.b4 = obuf[6] ^ obuf[7] ^ obuf[14] ^ obuf[15]; + // Fold the md5sum to 32 bits, pack the bytes to an in_addr + conv.bytes[0] = obuf[0] ^ obuf[1] ^ obuf[8] ^ obuf[9]; + conv.bytes[1] = obuf[2] ^ obuf[3] ^ obuf[10] ^ obuf[11]; + conv.bytes[2] = obuf[4] ^ obuf[5] ^ obuf[12] ^ obuf[13]; + conv.bytes[3] = obuf[6] ^ obuf[7] ^ obuf[14] ^ obuf[15]; return conv.ip; } diff --git a/src/common/md5calc.hpp b/src/common/md5calc.hpp index 1dde2ed..2dfaecb 100644 --- a/src/common/md5calc.hpp +++ b/src/common/md5calc.hpp @@ -58,7 +58,7 @@ const char *make_salt(void); /// check plaintext password against saved saltcrypt bool pass_ok(const char *password, const char *crypted); -/// This returns an in_addr_t because it is configurable whether it gets called at all -in_addr_t MD5_ip(char *secret, in_addr_t ip); +/// This returns an in_addr because it is configurable whether it gets called at all +struct in_addr MD5_ip(char *secret, struct in_addr ip); #endif diff --git a/src/common/mmo.hpp b/src/common/mmo.hpp index 2ca93b0..151fa03 100644 --- a/src/common/mmo.hpp +++ b/src/common/mmo.hpp @@ -185,14 +185,14 @@ struct guild_position int exp_mode; }; -struct guild_alliance +struct GuildAlliance { int opposition; int guild_id; char name[24]; }; -struct guild_explusion +struct GuildExpulsion { char name[24]; char mes[40]; @@ -217,8 +217,8 @@ struct guild char mes1[60], mes2[120]; int emblem_len, emblem_id; char emblem_data[2048]; - struct guild_alliance alliance[MAX_GUILDALLIANCE]; - struct guild_explusion explusion[MAX_GUILDEXPLUSION]; + GuildAlliance alliance[MAX_GUILDALLIANCE]; + GuildExpulsion explusion[MAX_GUILDEXPLUSION]; struct guild_skill skill[MAX_GUILDSKILL]; }; diff --git a/src/common/mt_rand.cpp b/src/common/mt_rand.cpp index 89872ad..676eca1 100644 --- a/src/common/mt_rand.cpp +++ b/src/common/mt_rand.cpp @@ -70,6 +70,7 @@ void mt_seed (uint32_t seed) for (int j = N; *s++ = x, --j; x *= 69069U); } +static void mt_reload (void) { // if mt_seed has never been called diff --git a/src/common/nullpo.cpp b/src/common/nullpo.cpp index 6dd2736..67c839f 100644 --- a/src/common/nullpo.cpp +++ b/src/common/nullpo.cpp @@ -3,6 +3,8 @@ #include <string.h> #include "nullpo.hpp" +static void nullpo_info_core (const char *file, int line, const char *func); +__attribute__((format(printf, 4, 0))) static void nullpo_info_core (const char *file, int line, const char *func, const char *fmt, va_list ap); @@ -26,7 +28,7 @@ bool nullpo_chk (const char *file, int line, const char *func, if (target) return 0; - nullpo_info_core (file, line, func, NULL, NULL); + nullpo_info_core (file, line, func); return 1; } @@ -42,14 +44,11 @@ void nullpo_info_f (const char *file, int line, const char *func, } void nullpo_info (const char *file, int line, const char *func) { - nullpo_info_core (file, line, func, NULL, NULL); + nullpo_info_core (file, line, func); } /// Actual output function -static void nullpo_info_core (const char *file, int line, const char *func, - const char *fmt, va_list ap) __attribute__((format(printf, 4, 0))); -static void nullpo_info_core (const char *file, int line, const char *func, - const char *fmt, va_list ap) +static void nullpo_info_core (const char *file, int line, const char *func) { if (!file) file = "??"; @@ -57,6 +56,12 @@ static void nullpo_info_core (const char *file, int line, const char *func, func = "unknown"; fprintf (stderr, "%s:%d: in func `%s': NULL pointer\n", file, line, func); +} + +static void nullpo_info_core (const char *file, int line, const char *func, + const char *fmt, va_list ap) +{ + nullpo_info_core(file, line, func); if (fmt && *fmt) { vfprintf (stderr, fmt, ap); diff --git a/src/common/utils.cpp b/src/common/utils.cpp new file mode 100644 index 0000000..fbdd87e --- /dev/null +++ b/src/common/utils.cpp @@ -0,0 +1,97 @@ +#include "utils.hpp" + +#include <cstring> +#include <cstdio> +#include <cstdlib> + +#include <netinet/in.h> + +//----------------------------------------------------- +// Function to suppress control characters in a string. +//----------------------------------------------------- +int remove_control_chars (char *str) +{ + int i; + int change = 0; + + for (i = 0; str[i]; i++) + { + if (0 <= str[i] && str[i] < 32) + { + str[i] = '_'; + change = 1; + } + } + + return change; +} + +//--------------------------------------------------- +// E-mail check: return 0 (not correct) or 1 (valid). +//--------------------------------------------------- +int e_mail_check (const char *email) +{ + char ch; + const char *last_arobas; + + // athena limits + if (strlen (email) < 3 || strlen (email) > 39) + return 0; + + // part of RFC limits (official reference of e-mail description) + if (strchr (email, '@') == NULL || email[strlen (email) - 1] == '@') + return 0; + + if (email[strlen (email) - 1] == '.') + return 0; + + last_arobas = strrchr (email, '@'); + + if (strstr (last_arobas, "@.") != NULL || + strstr (last_arobas, "..") != NULL) + return 0; + + for (ch = 1; ch < 32; ch++) + { + if (strchr (last_arobas, ch) != NULL) + { + return 0; + break; + } + } + + if (strchr (last_arobas, ' ') != NULL || + strchr (last_arobas, ';') != NULL) + return 0; + + // all correct + return 1; +} + +//------------------------------------------------- +// Return numerical value of a switch configuration +// on/off, english, français, deutsch, español +//------------------------------------------------- +int config_switch (const char *str) +{ + if (strcasecmp (str, "on") == 0 || strcasecmp (str, "yes") == 0 + || strcasecmp (str, "oui") == 0 || strcasecmp (str, "ja") == 0 + || strcasecmp (str, "si") == 0) + return 1; + if (strcasecmp (str, "off") == 0 || strcasecmp (str, "no") == 0 + || strcasecmp (str, "non") == 0 || strcasecmp (str, "nein") == 0) + return 0; + + return atoi (str); +} + +const char *ip2str(struct in_addr ip, bool extra_dot) +{ + const uint8_t *p = (const uint8_t *)(&ip); + static char buf[17]; + if (extra_dot) + sprintf(buf, "%d.%d.%d.%d.", p[0], p[1], p[2], p[3]); + else + sprintf(buf, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + return buf; +} diff --git a/src/common/utils.hpp b/src/common/utils.hpp index 7c7da16..1097bf7 100644 --- a/src/common/utils.hpp +++ b/src/common/utils.hpp @@ -1,5 +1,8 @@ #ifndef UTILS_HPP #define UTILS_HPP + +#include "sanity.hpp" + /* Notes about memory allocation in tmwAthena: There used to be 3 sources of allocation: these macros, @@ -15,4 +18,9 @@ future calls should either use this or depend on the coming segfault. if (!((result) = (type *) realloc ((result), sizeof(type) * (number))))\ { perror("SYSERR: realloc failure"); abort(); } else (void)0 +int remove_control_chars (char *str); +int e_mail_check (const char *email); +int config_switch (const char *str); +const char *ip2str(struct in_addr ip, bool extra_dot = false); + #endif //UTILS_HPP |