From 70265291d62280c525adc317158e9f531e0147ff Mon Sep 17 00:00:00 2001 From: Haru Date: Thu, 17 Sep 2015 01:21:28 +0200 Subject: Cleanup of the HPluginData implementation (First part) - Several explicit casts are removed, to have a slightly better type-checking at compile time. - A destructor function is provided, to remove code duplication. Signed-off-by: Haru --- src/common/socket.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/common/socket.h') diff --git a/src/common/socket.h b/src/common/socket.h index a995bffc8..510eac575 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -17,7 +17,7 @@ # include #endif -struct HPluginData; +struct hplugin_data_store; #define FIFOSIZE_SERVERLINK 256*1024 @@ -105,8 +105,7 @@ struct socket_data { void* session_data; // stores application-specific data related to the session - struct HPluginData **hdata; - unsigned int hdatac; + struct hplugin_data_store *hdata; }; struct hSockOpt { -- cgit v1.2.3-70-g09d2 From 657e7f9c07bc1445ce785cd11772664a1848ea5a Mon Sep 17 00:00:00 2001 From: Haru Date: Sat, 26 Sep 2015 04:11:41 +0200 Subject: Cleanup of the HPluginData implementation (second part) - Changed the hplugin_data_store's array into a VECTOR. Signed-off-by: Haru --- src/char/HPMchar.c | 3 +- src/char/HPMchar.h | 2 +- src/common/HPM.c | 219 ++++++++++++++++++++++++++++++------------------- src/common/HPM.h | 26 +++--- src/common/HPMi.h | 106 ++++++++++++------------ src/common/mmo.h | 4 +- src/common/socket.c | 3 +- src/common/socket.h | 3 +- src/login/HPMlogin.c | 3 +- src/login/HPMlogin.h | 2 +- src/map/HPMmap.c | 6 +- src/map/HPMmap.h | 2 +- src/map/battleground.c | 3 +- src/map/battleground.h | 3 +- src/map/guild.c | 6 +- src/map/instance.c | 3 +- src/map/instance.h | 4 +- src/map/itemdb.c | 3 +- src/map/itemdb.h | 4 +- src/map/map.c | 3 +- src/map/map.h | 4 +- src/map/mob.c | 3 +- src/map/mob.h | 8 +- src/map/npc.c | 3 +- src/map/npc.h | 3 +- src/map/party.c | 6 +- src/map/party.h | 4 +- src/map/pc.c | 6 +- src/map/pc.h | 7 +- src/map/unit.c | 9 +- 30 files changed, 242 insertions(+), 219 deletions(-) (limited to 'src/common/socket.h') diff --git a/src/char/HPMchar.c b/src/char/HPMchar.c index 14d7be1a3..d3150bc11 100644 --- a/src/char/HPMchar.c +++ b/src/char/HPMchar.c @@ -52,9 +52,10 @@ * * @see HPM_interface::data_store_validate */ -bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { switch (type) { + // No supported types at the moment. default: break; } diff --git a/src/char/HPMchar.h b/src/char/HPMchar.h index fd07f45ed..431017b7a 100644 --- a/src/char/HPMchar.h +++ b/src/char/HPMchar.h @@ -13,7 +13,7 @@ struct hplugin; -bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); +bool HPM_char_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); void HPM_char_plugin_load_sub(struct hplugin *plugin); diff --git a/src/common/HPM.c b/src/common/HPM.c index ce3d769ca..578e90bbc 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -168,120 +168,158 @@ bool hplugins_addpacket(unsigned short cmd, unsigned short length, void (*receiv return true; } -bool hplugin_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +/** + * Validates and if necessary initializes a plugin data store. + * + * @param type[in] The data store type. + * @param storeptr[in,out] A pointer to the store. + * @param initialize Whether the store should be initialized in case it isn't. + * @retval false if the store is an invalid or mismatching type. + * @retval true if the store is a valid type. + * + * @remark + * If \c storeptr is a pointer to a NULL pointer (\c storeptr itself can't + * be NULL), two things may happen, depending on the \c initialize value: + * if false, then \c storeptr isn't changed; if true, then \c storeptr is + * initialized through \c HPM->data_store_create() and ownership is passed + * to the caller. + */ +bool hplugin_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { - nullpo_retr(false, store); + struct hplugin_data_store *store; + nullpo_retr(false, storeptr); + store = *storeptr; + if (!initialize && store == NULL) + return true; switch (type) { /* core-handled */ case HPDT_SESSION: - if (!*store) { - *store = HPM->data_store_create(); - } - return true; - /* goes to sub */ + break; default: + if (HPM->data_store_validate_sub == NULL) { + ShowError("HPM:validateHPData failed, type %d needs sub-handler!\n",type); + return false; + } + if (!HPM->data_store_validate_sub(type, storeptr, initialize)) { + ShowError("HPM:HPM:validateHPData failed, unknown type %d!\n",type); + return false; + } break; } - if (HPM->data_store_validate_sub) { - if (HPM->data_store_validate_sub(type, store)) - return true; - ShowError("HPM:HPM:validateHPData failed, unknown type %d!\n",type); - } else { - ShowError("HPM:validateHPData failed, type %d needs sub-handler!\n",type); + if (initialize && (!store || store->type == HPDT_UNKNOWN)) { + HPM->data_store_create(storeptr, type); + store = *storeptr; } - return false; + if (store->type != type) { + ShowError("HPM:HPM:validateHPData failed, store type mismatch %d != %d.\n",store->type, type); + return false; + } + return true; } -void hplugins_addToHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, void *data, unsigned int index, bool autofree) +/** + * Adds an entry to a plugin data store. + * + * @param type[in] The store type. + * @param pluginID[in] The plugin identifier. + * @param storeptr[in,out] A pointer to the store. The store will be initialized if necessary. + * @param data[in] The data entry to add. + * @param classid[in] The entry class identifier. + * @param autofree[in] Whether the entry should be automatically freed when removed. + */ +void hplugins_addToHPData(enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store **storeptr, void *data, uint32 classid, bool autofree) { - struct hplugin_data_entry *HPData; - unsigned int i; struct hplugin_data_store *store; + struct hplugin_data_entry *entry; + int i; + nullpo_retv(storeptr); - if (!HPM->data_store_validate(type, storeptr)) { + if (!HPM->data_store_validate(type, storeptr, true)) { /* woo it failed! */ - ShowError("HPM:addToHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + ShowError("HPM:addToHPData:%s: failed, type %d (%u|%u)\n", HPM->pid2name(pluginID), type, pluginID, classid); return; } store = *storeptr; /* duplicate check */ - for (i = 0; i < store->count; i++) { - if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) { - ShowError("HPM:addToHPData:%s: error! attempting to insert duplicate struct of id %u and index %u\n",HPM->pid2name(pluginID),pluginID,index); - return; - } + ARR_FIND(0, VECTOR_LENGTH(store->entries), i, VECTOR_INDEX(store->entries, i)->pluginID == pluginID && VECTOR_INDEX(store->entries, i)->classid == classid); + if (i != VECTOR_LENGTH(store->entries)) { + ShowError("HPM:addToHPData:%s: error! attempting to insert duplicate struct of id %u and classid %u\n", HPM->pid2name(pluginID), pluginID, classid); + return; } /* hplugin_data_entry is always same size, probably better to use the ERS (with reasonable chunk size e.g. 10/25/50) */ - CREATE(HPData, struct hplugin_data_entry, 1); + CREATE(entry, struct hplugin_data_entry, 1); /* input */ - HPData->pluginID = pluginID; - HPData->type = index; - HPData->flag.free = autofree ? 1 : 0; - HPData->data = data; - - /* resize */ - RECREATE(store->array, struct hplugin_data_entry *, ++store->count); + entry->pluginID = pluginID; + entry->classid = classid; + entry->flag.free = autofree ? 1 : 0; + entry->data = data; - store->array[store->count - 1] = HPData; + VECTOR_ENSURE(store->entries, 1, 1); + VECTOR_PUSH(store->entries, entry); } -void *hplugins_getFromHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index) +/** + * Retrieves an entry from a plugin data store. + * + * @param type[in] The store type. + * @param pluginID[in] The plugin identifier. + * @param store[in] The store. + * @param classid[in] The entry class identifier. + * + * @return The retrieved entry, or NULL. + */ +void *hplugins_getFromHPData(enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid) { - unsigned int i; - struct hplugin_data_store *store; + int i; - if (!HPM->data_store_validate(type, storeptr)) { + if (!HPM->data_store_validate(type, &store, false)) { /* woo it failed! */ - ShowError("HPM:getFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + ShowError("HPM:getFromHPData:%s: failed, type %d (%u|%u)\n", HPM->pid2name(pluginID), type, pluginID, classid); return NULL; } - store = *storeptr; + if (!store) + return NULL; - for (i = 0; i < store->count; i++) { - if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) - return store->array[i]->data; - } + ARR_FIND(0, VECTOR_LENGTH(store->entries), i, VECTOR_INDEX(store->entries, i)->pluginID == pluginID && VECTOR_INDEX(store->entries, i)->classid == classid); + if (i != VECTOR_LENGTH(store->entries)) + return VECTOR_INDEX(store->entries, i)->data; return NULL; } -void hplugins_removeFromHPData(enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index) +/** + * Removes an entry from a plugin data store. + * + * @param type[in] The store type. + * @param pluginID[in] The plugin identifier. + * @param store[in] The store. + * @param classid[in] The entry class identifier. + */ +void hplugins_removeFromHPData(enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid) { - unsigned int i; - struct hplugin_data_store *store; + struct hplugin_data_entry *entry; + int i; - if (!HPM->data_store_validate(type, storeptr)) { + if (!HPM->data_store_validate(type, &store, false)) { /* woo it failed! */ - ShowError("HPM:removeFromHPData:%s: failed, type %d (%u|%u)\n",HPM->pid2name(pluginID),type,pluginID,index); + ShowError("HPM:removeFromHPData:%s: failed, type %d (%u|%u)\n", HPM->pid2name(pluginID), type, pluginID, classid); return; } - store = *storeptr; - - for (i = 0; i < store->count; i++) { - if (store->array[i]->pluginID == pluginID && store->array[i]->type == index) - break; - } - - if (i != store->count) { - unsigned int cursor; + if (!store) + return; - aFree(store->array[i]->data);/* when its removed we delete it regardless of autofree */ - aFree(store->array[i]); - store->array[i] = NULL; + ARR_FIND(0, VECTOR_LENGTH(store->entries), i, VECTOR_INDEX(store->entries, i)->pluginID == pluginID && VECTOR_INDEX(store->entries, i)->classid == classid); + if (i == VECTOR_LENGTH(store->entries)) + return; - for (i = 0, cursor = 0; i < store->count; i++) { - if (store->array[i] == NULL) - continue; - if (i != cursor) - store->array[cursor] = store->array[i]; - cursor++; - } - store->count = cursor; - } + entry = VECTOR_INDEX(store->entries, i); + VECTOR_ERASE(store->entries, i); // Erase and compact + aFree(entry->data); // when it's removed we delete it regardless of autofree + aFree(entry); } /* TODO: add ability for tracking using pID for the upcoming runtime load/unload support. */ @@ -765,25 +803,30 @@ bool hplugins_parse_conf(const char *w1, const char *w2, enum HPluginConfType po } /** - * Helper to destroy and release an interface's hplugin_data store. + * Helper to destroy an interface's hplugin_data store and release any owned memory. * - * @param hdata The hplugin_data store. The pointer will be freed. + * The pointer will be cleared. + * + * @param storeptr[in,out] A pointer to the plugin data store. */ -void hplugin_data_store_destroy(struct hplugin_data_store *store) +void hplugin_data_store_destroy(struct hplugin_data_store **storeptr) { - unsigned int i; - - if (!store) + struct hplugin_data_store *store; + nullpo_retv(storeptr); + store = *storeptr; + if (store == NULL) return; - for (i = 0; i < store->count; i++) { - if (store->array[i]->flag.free) { - aFree(store->array[i]->data); + while (VECTOR_LENGTH(store->entries) > 0) { + struct hplugin_data_entry *entry = VECTOR_POP(store->entries); + if (entry->flag.free) { + aFree(entry->data); } - aFree(store->array[i]); + aFree(entry); } - aFree(store->array); + VECTOR_CLEAR(store->entries); aFree(store); + *storeptr = NULL; } /** @@ -792,13 +835,21 @@ void hplugin_data_store_destroy(struct hplugin_data_store *store) * The store is owned by the caller, and it should be eventually destroyed by * \c hdata_destroy. * - * @return An initialized hplugin_data store. + * @param storeptr[in,out] A pointer to the data store to initialize. + * @param type[in] The store type. */ -struct hplugin_data_store *hplugin_data_store_create(void) +void hplugin_data_store_create(struct hplugin_data_store **storeptr, enum HPluginDataTypes type) { struct hplugin_data_store *store; - CREATE(store, struct hplugin_data_store, 1); - return store; + nullpo_retv(storeptr); + + if (*storeptr == NULL) { + CREATE(*storeptr, struct hplugin_data_store, 1); + } + store = *storeptr; + + store->type = type; + VECTOR_INIT(store->entries); } /** diff --git a/src/common/HPM.h b/src/common/HPM.h index 1d13a178f..5420e5300 100644 --- a/src/common/HPM.h +++ b/src/common/HPM.h @@ -65,18 +65,24 @@ struct hpm_symbol { void *ptr; ///< The symbol value }; +/** + * A plugin custom data, to be injected in various interfaces and objects. + */ struct hplugin_data_entry { - unsigned int pluginID; - unsigned int type; + uint32 pluginID; ///< The owner plugin identifier. + uint32 classid; ///< The entry's object type, managed by the plugin (for plugins that need more than one entry). struct { - unsigned int free : 1; + unsigned int free : 1; ///< Whether the entry data should be automatically cleared by the HPM. } flag; - void *data; + void *data; ///< The entry data. }; +/** + * A store for plugin custom data entries. + */ struct hplugin_data_store { - struct hplugin_data_entry **array; - unsigned int count; + enum HPluginDataTypes type; ///< The store type. + VECTOR_DECL(struct hplugin_data_entry *) entries; ///< The store entries. }; struct HPluginPacket { @@ -144,11 +150,11 @@ struct HPM_interface { void (*datacheck_init) (const struct s_HPMDataCheck *src, unsigned int length, int version); void (*datacheck_final) (void); - struct hplugin_data_store *(*data_store_create) (void); - void (*data_store_destroy) (struct hplugin_data_store *store); - bool (*data_store_validate) (enum HPluginDataTypes type, struct hplugin_data_store **store); + void (*data_store_create) (struct hplugin_data_store **storeptr, enum HPluginDataTypes type); + void (*data_store_destroy) (struct hplugin_data_store **storeptr); + bool (*data_store_validate) (enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); /* for server-specific HPData e.g. map_session_data */ - bool (*data_store_validate_sub) (enum HPluginDataTypes type, struct hplugin_data_store **store); + bool (*data_store_validate_sub) (enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); }; CMDLINEARG(loadplugin); diff --git a/src/common/HPMi.h b/src/common/HPMi.h index d92503a91..9a61dd256 100644 --- a/src/common/HPMi.h +++ b/src/common/HPMi.h @@ -60,19 +60,23 @@ enum HPluginHookType { HOOK_TYPE_POST, }; +/** + * Data types for plugin custom data. + */ enum HPluginDataTypes { - HPDT_SESSION, - HPDT_MSD, - HPDT_NPCD, - HPDT_MAP, - HPDT_INSTANCE, - HPDT_GUILD, - HPDT_PARTY, - HPDT_MOBDB, - HPDT_MOBDATA, - HPDT_ITEMDATA, - HPDT_BGDATA, - HPDT_AUTOTRADE_VEND, + HPDT_UNKNOWN, ///< Unknown type (such as an uninitialized store). + HPDT_SESSION, ///< For struct socket_data. + HPDT_MSD, ///< For struct map_session_data. + HPDT_NPCD, ///< For struct npc_data. + HPDT_MAP, ///< For struct map_data. + HPDT_INSTANCE, ///< For struct instance_data. + HPDT_GUILD, ///< For struct guild. + HPDT_PARTY, ///< For struct party_data. + HPDT_MOBDB, ///< For struct mob_db. + HPDT_MOBDATA, ///< For struct mob_data. + HPDT_ITEMDATA, ///< For struct item_data. + HPDT_BGDATA, ///< For struct battleground_data. + HPDT_AUTOTRADE_VEND, ///< For struct autotrade_vending. }; /* used in macros and conf storage */ @@ -97,53 +101,53 @@ enum HPluginConfType { #define addArg(name, param,func,help) (HPMi->addArg(HPMi->pid,(name),(param),(cmdline_arg_ ## func),(help))) /* HPData handy redirects */ /* session[] */ -#define addToSession(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromSession(ptr,index) (HPMi->getFromHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromSession(ptr,index) (HPMi->removeFromHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(index))) +#define addToSession(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_SESSION,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromSession(ptr,classid) (HPMi->getFromHPData(HPDT_SESSION,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromSession(ptr,classid) (HPMi->removeFromHPData(HPDT_SESSION,HPMi->pid,(ptr)->hdata,(classid))) /* map_session_data */ -#define addToMSD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMSD(ptr,index) (HPMi->getFromHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMSD(ptr,index) (HPMi->removeFromHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMSD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MSD,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMSD(ptr,classid) (HPMi->getFromHPData(HPDT_MSD,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMSD(ptr,classid) (HPMi->removeFromHPData(HPDT_MSD,HPMi->pid,(ptr)->hdata,(classid))) /* npc_data */ -#define addToNPCD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromNPCD(ptr,index) (HPMi->getFromHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromNPCD(ptr,index) (HPMi->removeFromHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(index))) +#define addToNPCD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_NPCD,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromNPCD(ptr,classid) (HPMi->getFromHPData(HPDT_NPCD,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromNPCD(ptr,classid) (HPMi->removeFromHPData(HPDT_NPCD,HPMi->pid,(ptr)->hdata,(classid))) /* map_data */ -#define addToMAPD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMAPD(ptr,index) (HPMi->getFromHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMAPD(ptr,index) (HPMi->removeFromHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMAPD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MAP,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMAPD(ptr,classid) (HPMi->getFromHPData(HPDT_MAP,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMAPD(ptr,classid) (HPMi->removeFromHPData(HPDT_MAP,HPMi->pid,(ptr)->hdata,(classid))) /* party_data */ -#define addToPAD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromPAD(ptr,index) (HPMi->getFromHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromPAD(ptr,index) (HPMi->removeFromHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(index))) +#define addToPAD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_PARTY,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromPAD(ptr,classid) (HPMi->getFromHPData(HPDT_PARTY,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromPAD(ptr,classid) (HPMi->removeFromHPData(HPDT_PARTY,HPMi->pid,(ptr)->hdata,(classid))) /* guild */ -#define addToGLD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromGLD(ptr,index) (HPMi->getFromHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromGLD(ptr,index) (HPMi->removeFromHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(index))) +#define addToGLD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_GUILD,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromGLD(ptr,classid) (HPMi->getFromHPData(HPDT_GUILD,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromGLD(ptr,classid) (HPMi->removeFromHPData(HPDT_GUILD,HPMi->pid,(ptr)->hdata,(classid))) /* instance_data */ -#define addToINSTD(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromINSTD(ptr,index) (HPMi->getFromHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromINSTD(ptr,index) (HPMi->removeFromHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(index))) +#define addToINSTD(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_INSTANCE,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromINSTD(ptr,classid) (HPMi->getFromHPData(HPDT_INSTANCE,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromINSTD(ptr,classid) (HPMi->removeFromHPData(HPDT_INSTANCE,HPMi->pid,(ptr)->hdata,(classid))) /* mob_db */ -#define addToMOBDB(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMOBDB(ptr,index) (HPMi->getFromHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMOBDB(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMOBDB(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MOBDB,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMOBDB(ptr,classid) (HPMi->getFromHPData(HPDT_MOBDB,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMOBDB(ptr,classid) (HPMi->removeFromHPData(HPDT_MOBDB,HPMi->pid,(ptr)->hdata,(classid))) /* mob_data */ -#define addToMOBDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromMOBDATA(ptr,index) (HPMi->getFromHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromMOBDATA(ptr,index) (HPMi->removeFromHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define addToMOBDATA(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_MOBDATA,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromMOBDATA(ptr,classid) (HPMi->getFromHPData(HPDT_MOBDATA,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromMOBDATA(ptr,classid) (HPMi->removeFromHPData(HPDT_MOBDATA,HPMi->pid,(ptr)->hdata,(classid))) /* item_data */ -#define addToITEMDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromITEMDATA(ptr,index) (HPMi->getFromHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromITEMDATA(ptr,index) (HPMi->removeFromHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define addToITEMDATA(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_ITEMDATA,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromITEMDATA(ptr,classid) (HPMi->getFromHPData(HPDT_ITEMDATA,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromITEMDATA(ptr,classid) (HPMi->removeFromHPData(HPDT_ITEMDATA,HPMi->pid,(ptr)->hdata,(classid))) /* battleground_data */ -#define addToBGDATA(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromBGDATA(ptr,index) (HPMi->getFromHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromBGDATA(ptr,index) (HPMi->removeFromHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(index))) +#define addToBGDATA(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_BGDATA,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromBGDATA(ptr,classid) (HPMi->getFromHPData(HPDT_BGDATA,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromBGDATA(ptr,classid) (HPMi->removeFromHPData(HPDT_BGDATA,HPMi->pid,(ptr)->hdata,(classid))) /* autotrade_vending */ -#define addToATVEND(ptr,data,index,autofree) (HPMi->addToHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(data),(index),(autofree))) -#define getFromATVEND(ptr,index) (HPMi->getFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(index))) -#define removeFromATVEND(ptr,index) (HPMi->removeFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(index))) +#define addToATVEND(ptr,data,classid,autofree) (HPMi->addToHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,&(ptr)->hdata,(data),(classid),(autofree))) +#define getFromATVEND(ptr,classid) (HPMi->getFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,(ptr)->hdata,(classid))) +#define removeFromATVEND(ptr,classid) (HPMi->removeFromHPData(HPDT_AUTOTRADE_VEND,HPMi->pid,(ptr)->hdata,(classid))) /// HPMi->addCommand #define addAtcommand(cname,funcname) do { \ @@ -206,9 +210,9 @@ struct HPMi_interface { bool (*addScript) (char *name, char *args, bool (*func)(struct script_state *st), bool isDeprecated); void (*addCPCommand) (char *name, CParseFunc func); /* HPM Custom Data */ - void (*addToHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, void *data, unsigned int index, bool autofree); - void *(*getFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index); - void (*removeFromHPData) (enum HPluginDataTypes type, unsigned int pluginID, struct hplugin_data_store **storeptr, unsigned int index); + void (*addToHPData) (enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store **storeptr, void *data, uint32 classid, bool autofree); + void *(*getFromHPData) (enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid); + void (*removeFromHPData) (enum HPluginDataTypes type, uint32 pluginID, struct hplugin_data_store *store, uint32 classid); /* packet */ bool (*addPacket) (unsigned short cmd, unsigned short length, void (*receive)(int fd), unsigned int point, unsigned int pluginID); /* Hooking */ diff --git a/src/common/mmo.h b/src/common/mmo.h index cbda0e91b..73d4510a1 100644 --- a/src/common/mmo.h +++ b/src/common/mmo.h @@ -661,9 +661,7 @@ struct guild { unsigned short instances; struct channel_data *channel; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct guild_castle { diff --git a/src/common/socket.c b/src/common/socket.c index 842f9ba87..a26873b67 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -637,8 +637,7 @@ static void delete_session(int fd) aFree(sockt->session[fd]->wdata); if( sockt->session[fd]->session_data ) aFree(sockt->session[fd]->session_data); - HPM->data_store_destroy(sockt->session[fd]->hdata); - sockt->session[fd]->hdata = NULL; + HPM->data_store_destroy(&sockt->session[fd]->hdata); aFree(sockt->session[fd]); sockt->session[fd] = NULL; } diff --git a/src/common/socket.h b/src/common/socket.h index 510eac575..a2dea8b74 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -104,8 +104,7 @@ struct socket_data { ParseFunc func_parse; void* session_data; // stores application-specific data related to the session - - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store. }; struct hSockOpt { diff --git a/src/login/HPMlogin.c b/src/login/HPMlogin.c index ce88728fe..b900f28fe 100644 --- a/src/login/HPMlogin.c +++ b/src/login/HPMlogin.c @@ -37,9 +37,10 @@ * * @see HPM_interface::data_store_validate */ -bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { switch (type) { + // No supported types at the moment default: break; } diff --git a/src/login/HPMlogin.h b/src/login/HPMlogin.h index 1f08a8666..35d4bd01c 100644 --- a/src/login/HPMlogin.h +++ b/src/login/HPMlogin.h @@ -13,7 +13,7 @@ struct hplugin; -bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); +bool HPM_login_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); void HPM_login_plugin_load_sub(struct hplugin *plugin); diff --git a/src/map/HPMmap.c b/src/map/HPMmap.c index ddffb5519..edbe74039 100644 --- a/src/map/HPMmap.c +++ b/src/map/HPMmap.c @@ -90,7 +90,7 @@ unsigned int atcommand_list_items = 0; * * @see HPM_interface::data_store_validate */ -bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store) +bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize) { switch (type) { case HPDT_MSD: @@ -104,9 +104,7 @@ bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data case HPDT_ITEMDATA: case HPDT_BGDATA: case HPDT_AUTOTRADE_VEND: - if (!*store) { - *store = HPM->data_store_create(); - } + // Initialized by the caller. return true; default: break; diff --git a/src/map/HPMmap.h b/src/map/HPMmap.h index 10bdf0ba7..b1957b139 100644 --- a/src/map/HPMmap.h +++ b/src/map/HPMmap.h @@ -15,7 +15,7 @@ struct hplugin; struct map_session_data; -bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **store); +bool HPM_map_data_store_validate(enum HPluginDataTypes type, struct hplugin_data_store **storeptr, bool initialize); bool HPM_map_add_atcommand(char *name, AtCommandFunc func); void HPM_map_atcommands(void); diff --git a/src/map/battleground.c b/src/map/battleground.c index 46d695272..be6b7a863 100644 --- a/src/map/battleground.c +++ b/src/map/battleground.c @@ -881,8 +881,7 @@ void do_init_battleground(bool minimal) { int bg_team_db_final(DBKey key, DBData *data, va_list ap) { struct battleground_data* bgd = DB->data2ptr(data); - HPM->data_store_destroy(bgd->hdata); - bgd->hdata = NULL; + HPM->data_store_destroy(&bgd->hdata); return 0; } diff --git a/src/map/battleground.h b/src/map/battleground.h index 0280407a2..dcf92d6d8 100644 --- a/src/map/battleground.h +++ b/src/map/battleground.h @@ -53,8 +53,7 @@ struct battleground_data { // Logout Event char logout_event[EVENT_NAME_LENGTH]; char die_event[EVENT_NAME_LENGTH]; - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct bg_arena { diff --git a/src/map/guild.c b/src/map/guild.c index 2dca97502..f8798f821 100644 --- a/src/map/guild.c +++ b/src/map/guild.c @@ -1763,8 +1763,7 @@ int guild_broken(int guild_id,int flag) if( g->instance ) aFree(g->instance); - HPM->data_store_destroy(g->hdata); - g->hdata = NULL; + HPM->data_store_destroy(&g->hdata); idb_remove(guild->db,guild_id); return 0; @@ -2250,8 +2249,7 @@ void do_final_guild(void) { aFree(g->instance); g->instance = NULL; } - HPM->data_store_destroy(g->hdata); - g->hdata = NULL; + HPM->data_store_destroy(&g->hdata); } dbi_destroy(iter); diff --git a/src/map/instance.c b/src/map/instance.c index 7213c43a1..0936a5ace 100644 --- a/src/map/instance.c +++ b/src/map/instance.c @@ -588,8 +588,7 @@ void instance_destroy(int instance_id) { instance->list[instance_id].state = INSTANCE_FREE; instance->list[instance_id].num_map = 0; - HPM->data_store_destroy(instance->list[instance_id].hdata); - instance->list[instance_id].hdata = NULL; + HPM->data_store_destroy(&instance->list[instance_id].hdata); } /*-------------------------------------- diff --git a/src/map/instance.h b/src/map/instance.h index f879195ef..058cd2c3d 100644 --- a/src/map/instance.h +++ b/src/map/instance.h @@ -52,9 +52,7 @@ struct instance_data { unsigned int original_progress_timeout; struct point respawn; ///< reload spawn - - /** HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct instance_interface { diff --git a/src/map/itemdb.c b/src/map/itemdb.c index c531a4d7a..ccfff2246 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -2100,8 +2100,7 @@ void destroy_item_data(struct item_data* self, int free_self) script->free_code(self->unequip_script); if( self->combos ) aFree(self->combos); - HPM->data_store_destroy(self->hdata); - self->hdata = NULL; + HPM->data_store_destroy(&self->hdata); #if defined(DEBUG) // trash item memset(self, 0xDD, sizeof(struct item_data)); diff --git a/src/map/itemdb.h b/src/map/itemdb.h index de9770aab..64b800b87 100644 --- a/src/map/itemdb.h +++ b/src/map/itemdb.h @@ -489,9 +489,7 @@ struct item_data { /* TODO add a pointer to some sort of (struct extra) and gather all the not-common vals into it to save memory */ struct item_group *group; struct item_package *package; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; #define itemdb_name(n) (itemdb->search(n)->name) diff --git a/src/map/map.c b/src/map/map.c index 0ed7072d2..b142ed1f3 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -3239,8 +3239,7 @@ void do_final_maps(void) { if( map->list[i].qi_data ) aFree(map->list[i].qi_data); - HPM->data_store_destroy(map->list[i].hdata); - map->list[i].hdata = NULL; + HPM->data_store_destroy(&map->list[i].hdata); } map->zone_db_clear(); diff --git a/src/map/map.h b/src/map/map.h index c3e426bb6..961a45793 100644 --- a/src/map/map.h +++ b/src/map/map.h @@ -737,9 +737,7 @@ struct map_data { /* speeds up clif_updatestatus processing by causing hpmeter to run only when someone with the permission can view it */ unsigned short hpmeter_visible; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; /// Stores information about a remote map (for multi-mapserver setups). diff --git a/src/map/mob.c b/src/map/mob.c index 174d2e49f..01f585b6f 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -4708,8 +4708,7 @@ int do_init_mob(bool minimal) { void mob_destroy_mob_db(int index) { struct mob_db *data = mob->db_data[index]; - HPM->data_store_destroy(data->hdata); - data->hdata = NULL; + HPM->data_store_destroy(&data->hdata); aFree(data); mob->db_data[index] = NULL; } diff --git a/src/map/mob.h b/src/map/mob.h index 1142502f4..bb26258c6 100644 --- a/src/map/mob.h +++ b/src/map/mob.h @@ -140,9 +140,7 @@ struct mob_db { int maxskill; struct mob_skill skill[MAX_MOBSKILL]; struct spawn_info spawn[10]; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; struct mob_data { @@ -209,9 +207,7 @@ struct mob_data { * MvP Tombstone NPC ID **/ int tomb_nid; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; diff --git a/src/map/npc.c b/src/map/npc.c index 85de5301b..24b22beb3 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -2321,8 +2321,7 @@ int npc_unload(struct npc_data* nd, bool single) nd->ud = NULL; } - HPM->data_store_destroy(nd->hdata); - nd->hdata = NULL; + HPM->data_store_destroy(&nd->hdata); aFree(nd); diff --git a/src/map/npc.h b/src/map/npc.h index 06aee3f93..bf3d1494d 100644 --- a/src/map/npc.h +++ b/src/map/npc.h @@ -102,8 +102,7 @@ struct npc_data { char killer_name[NAME_LENGTH]; } tomb; } u; - /* HPData Support for npc_data */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; diff --git a/src/map/party.c b/src/map/party.c index 4735dfc42..f7e7d431c 100644 --- a/src/map/party.c +++ b/src/map/party.c @@ -103,8 +103,7 @@ int party_db_final(DBKey key, DBData *data, va_list ap) { if (p->instance) aFree(p->instance); - HPM->data_store_destroy(p->hdata); - p->hdata = NULL; + HPM->data_store_destroy(&p->hdata); } return 0; } @@ -600,8 +599,7 @@ int party_broken(int party_id) if( p->instance ) aFree(p->instance); - HPM->data_store_destroy(p->hdata); - p->hdata = NULL; + HPM->data_store_destroy(&p->hdata); idb_remove(party->db,party_id); return 0; diff --git a/src/map/party.h b/src/map/party.h index 960e6da08..df7c03f05 100644 --- a/src/map/party.h +++ b/src/map/party.h @@ -35,9 +35,7 @@ struct party_data { unsigned snovice :1; ///< There's a Super Novice unsigned tk : 1; ///< There's a taekwon } state; - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; #define PB_NOTICE_LENGTH (36 + 1) diff --git a/src/map/pc.c b/src/map/pc.c index fee41deff..99f5b867e 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -11332,8 +11332,7 @@ void pc_autotrade_populate(struct map_session_data *sd) { pc->autotrade_update(sd,PAUC_START); - HPM->data_store_destroy(data->hdata); - data->hdata = NULL; + HPM->data_store_destroy(&data->hdata); idb_remove(pc->at_db, sd->status.char_id); } @@ -11343,8 +11342,7 @@ void pc_autotrade_populate(struct map_session_data *sd) { */ int pc_autotrade_final(DBKey key, DBData *data, va_list ap) { struct autotrade_vending* at_v = DB->data2ptr(data); - HPM->data_store_destroy(at_v->hdata); - at_v->hdata = NULL; + HPM->data_store_destroy(&at_v->hdata); return 0; } diff --git a/src/map/pc.h b/src/map/pc.h index a74bc6c80..2c8b24acf 100644 --- a/src/map/pc.h +++ b/src/map/pc.h @@ -539,9 +539,7 @@ END_ZEROED_BLOCK; unsigned short (*parse_cmd_func)(int fd, struct map_session_data *sd); ///< parse_cmd_func used by this player unsigned char delayed_damage;//ref. counter bugreport:7307 [Ind/Hercules] - - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store /* expiration_time timer id */ int expiration_tid; @@ -756,8 +754,7 @@ struct autotrade_vending { struct item list[MAX_VENDING]; struct s_vending vending[MAX_VENDING]; unsigned char vend_num; - /* HPM Custom Struct */ - struct hplugin_data_store *hdata; + struct hplugin_data_store *hdata; ///< HPM Plugin Data Store }; /*===================================== diff --git a/src/map/unit.c b/src/map/unit.c index 0f8a9d7e6..dcb5ec900 100644 --- a/src/map/unit.c +++ b/src/map/unit.c @@ -2654,10 +2654,7 @@ int unit_free(struct block_list *bl, clr_type clrtype) { sd->quest_log = NULL; sd->num_quests = sd->avail_quests = 0; } - - HPM->data_store_destroy(sd->hdata); - sd->hdata = NULL; - + HPM->data_store_destroy(&sd->hdata); break; } case BL_PET: @@ -2768,9 +2765,7 @@ int unit_free(struct block_list *bl, clr_type clrtype) { if( md->tomb_nid ) mob->mvptomb_destroy(md); - HPM->data_store_destroy(md->hdata); - md->hdata = NULL; - + HPM->data_store_destroy(&md->hdata); break; } case BL_HOM: -- cgit v1.2.3-70-g09d2 From ec725a7c59ca35a254984439585ad2be74f33d83 Mon Sep 17 00:00:00 2001 From: Haru Date: Sat, 26 Sep 2015 04:57:26 +0200 Subject: Changed various s_subnet arrays to VECTORs - sockt->lan_subnet was renamed to sockt->lan_subnets. - sockt->trusted_ip was renamed to sockt->trusted_ips. - sockt->allowed_ip was renamed to sockt->allowed_ips. - Convenience macros for checking IP ranges and subnets are provided (SUBNET_MATCH, APPLY_MASK). Signed-off-by: Haru --- src/common/socket.c | 121 +++++++++++++++++++--------------------------------- src/common/socket.h | 20 ++++++--- 2 files changed, 58 insertions(+), 83 deletions(-) (limited to 'src/common/socket.h') diff --git a/src/common/socket.c b/src/common/socket.c index a26873b67..f1318ab47 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -967,7 +967,7 @@ static int connect_check_(uint32 ip) // Search the allow list for( i=0; i < access_allownum; ++i ){ - if( (ip & access_allow[i].mask) == (access_allow[i].ip & access_allow[i].mask) ){ + if (SUBNET_MATCH(ip, access_allow[i].ip, access_allow[i].mask)) { if( access_debug ){ ShowInfo("connect_check: Found match from allow list:%d.%d.%d.%d IP:%d.%d.%d.%d Mask:%d.%d.%d.%d\n", CONVIP(ip), @@ -980,7 +980,7 @@ static int connect_check_(uint32 ip) } // Search the deny list for( i=0; i < access_denynum; ++i ){ - if( (ip & access_deny[i].mask) == (access_deny[i].ip & access_deny[i].mask) ){ + if (SUBNET_MATCH(ip, access_deny[i].ip, access_deny[i].mask)) { if( access_debug ){ ShowInfo("connect_check: Found match from deny list:%d.%d.%d.%d IP:%d.%d.%d.%d Mask:%d.%d.%d.%d\n", CONVIP(ip), @@ -1216,20 +1216,9 @@ void socket_final(void) aFree(sockt->session); - if (sockt->lan_subnet) - aFree(sockt->lan_subnet); - sockt->lan_subnet = NULL; - sockt->lan_subnet_count = 0; - - if (sockt->allowed_ip) - aFree(sockt->allowed_ip); - sockt->allowed_ip = NULL; - sockt->allowed_ip_count = 0; - - if (sockt->trusted_ip) - aFree(sockt->trusted_ip); - sockt->trusted_ip = NULL; - sockt->trusted_ip_count = 0; + VECTOR_CLEAR(sockt->lan_subnets); + VECTOR_CLEAR(sockt->allowed_ips); + VECTOR_CLEAR(sockt->trusted_ips); } /// Closes a socket. @@ -1605,13 +1594,13 @@ void send_shortlist_do_sends() uint32 socket_lan_subnet_check(uint32 ip, struct s_subnet *info) { int i; - ARR_FIND(0, sockt->lan_subnet_count, i, (sockt->lan_subnet[i].ip & sockt->lan_subnet[i].mask) == (ip & sockt->lan_subnet[i].mask)); - if (i < sockt->lan_subnet_count) { + ARR_FIND(0, VECTOR_LENGTH(sockt->lan_subnets), i, SUBNET_MATCH(ip, VECTOR_INDEX(sockt->lan_subnets, i).ip, VECTOR_INDEX(sockt->lan_subnets, i).mask)); + if (i != VECTOR_LENGTH(sockt->lan_subnets)) { if (info) { - info->ip = sockt->lan_subnet[i].ip; - info->mask = sockt->lan_subnet[i].mask; + info->ip = VECTOR_INDEX(sockt->lan_subnets, i).ip; + info->mask = VECTOR_INDEX(sockt->lan_subnets, i).mask; } - return sockt->lan_subnet[i].ip; + return VECTOR_INDEX(sockt->lan_subnets, i).ip; } if (info) { info->ip = info->mask = 0; @@ -1629,8 +1618,8 @@ uint32 socket_lan_subnet_check(uint32 ip, struct s_subnet *info) bool socket_allowed_ip_check(uint32 ip) { int i; - ARR_FIND(0, sockt->allowed_ip_count, i, (sockt->allowed_ip[i].ip & sockt->allowed_ip[i].mask) == (ip & sockt->allowed_ip[i].mask) ); - if (i < sockt->allowed_ip_count) + ARR_FIND(0, VECTOR_LENGTH(sockt->allowed_ips), i, SUBNET_MATCH(ip, VECTOR_INDEX(sockt->allowed_ips, i).ip, VECTOR_INDEX(sockt->allowed_ips, i).mask)); + if (i != VECTOR_LENGTH(sockt->allowed_ips)) return true; return sockt->trusted_ip_check(ip); // If an address is trusted, it's automatically also allowed. } @@ -1645,8 +1634,8 @@ bool socket_allowed_ip_check(uint32 ip) bool socket_trusted_ip_check(uint32 ip) { int i; - ARR_FIND(0, sockt->trusted_ip_count, i, (sockt->trusted_ip[i].ip & sockt->trusted_ip[i].mask) == (ip & sockt->trusted_ip[i].mask)); - if (i < sockt->trusted_ip_count) + ARR_FIND(0, VECTOR_LENGTH(sockt->trusted_ips), i, SUBNET_MATCH(ip, VECTOR_INDEX(sockt->trusted_ips, i).ip, VECTOR_INDEX(sockt->trusted_ips, i).mask)); + if (i != VECTOR_LENGTH(sockt->trusted_ips)) return true; return false; } @@ -1657,39 +1646,38 @@ bool socket_trusted_ip_check(uint32 ip) * Entries will be appended to the variable-size array pointed to by list/count. * * @param[in] t The list to parse. - * @param[in,out] list Pointer to the head of the output array to append to. Must not be NULL (but the array may be empty). - * @param[in,out] count Pointer to the counter of the output array to append to. Must not be NULL (but it may contain zero). + * @param[in,out] list Vector to append to. Must not be NULL (but the vector may be empty). * @param[in] filename Current filename, for output/logging reasons. * @param[in] groupname Current group name, for output/logging reasons. * @return The amount of entries read, zero in case of errors. */ -int socket_net_config_read_sub(config_setting_t *t, struct s_subnet **list, int *count, const char *filename, const char *groupname) +int socket_net_config_read_sub(config_setting_t *t, struct s_subnet_vector *list, const char *filename, const char *groupname) { int i, len; char ipbuf[64], maskbuf[64]; nullpo_retr(0, list); - nullpo_retr(0, count); if (t == NULL) return 0; len = libconfig->setting_length(t); + VECTOR_ENSURE(*list, len, 1); for (i = 0; i < len; ++i) { const char *subnet = libconfig->setting_get_string_elem(t, i); - struct s_subnet *l = NULL; + struct s_subnet *entry = NULL; if (sscanf(subnet, "%63[^:]:%63[^:]", ipbuf, maskbuf) != 2) { ShowWarning("Invalid IP:Subnet entry in configuration file %s: '%s' (%s)\n", filename, subnet, groupname); + continue; } - RECREATE(*list, struct s_subnet, *count + 1); - l = *list; - l[*count].ip = sockt->str2ip(ipbuf); - l[*count].mask = sockt->str2ip(maskbuf); - ++*count; + VECTOR_PUSHZEROED(*list); + entry = &VECTOR_LAST(*list); + entry->ip = sockt->str2ip(ipbuf); + entry->mask = sockt->str2ip(maskbuf); } - return *count; + return (int)VECTOR_LENGTH(*list); } /** @@ -1708,45 +1696,29 @@ void socket_net_config_read(const char *filename) return; } - if (sockt->lan_subnet) { - aFree(sockt->lan_subnet); - sockt->lan_subnet = NULL; - } - sockt->lan_subnet_count = 0; - if (sockt->net_config_read_sub(libconfig->lookup(&network_config, "lan_subnets"), &sockt->lan_subnet, &sockt->lan_subnet_count, filename, "lan_subnets") > 0) - ShowStatus("Read information about %d LAN subnets.\n", sockt->lan_subnet_count); + VECTOR_CLEAR(sockt->lan_subnets); + if (sockt->net_config_read_sub(libconfig->lookup(&network_config, "lan_subnets"), &sockt->lan_subnets, filename, "lan_subnets") > 0) + ShowStatus("Read information about %d LAN subnets.\n", (int)VECTOR_LENGTH(sockt->lan_subnets)); - if (sockt->trusted_ip) { - aFree(sockt->trusted_ip); - sockt->trusted_ip = NULL; - } - sockt->trusted_ip_count = 0; - if (sockt->net_config_read_sub(libconfig->lookup(&network_config, "trusted"), &sockt->trusted_ip, &sockt->trusted_ip_count, filename, "trusted") > 0) - ShowStatus("Read information about %d trusted IP ranges.\n", sockt->trusted_ip_count); - for (i = 0; i < sockt->allowed_ip_count; ++i) { - if ((sockt->allowed_ip[i].ip & sockt->allowed_ip[i].mask) == 0) { - ShowError("Using a wildcard IP range in the trusted server IPs is NOT RECOMMENDED.\n"); - ShowNotice("Please edit your '%s' trusted list to fit your network configuration.\n", filename); - break; - } + VECTOR_CLEAR(sockt->trusted_ips); + if (sockt->net_config_read_sub(libconfig->lookup(&network_config, "trusted"), &sockt->trusted_ips, filename, "trusted") > 0) + ShowStatus("Read information about %d trusted IP ranges.\n", (int)VECTOR_LENGTH(sockt->trusted_ips)); + ARR_FIND(0, VECTOR_LENGTH(sockt->trusted_ips), i, SUBNET_MATCH(0, VECTOR_INDEX(sockt->trusted_ips, i).ip, VECTOR_INDEX(sockt->trusted_ips, i).mask)); + if (i != VECTOR_LENGTH(sockt->trusted_ips)) { + ShowError("Using a wildcard IP range in the trusted server IPs is NOT RECOMMENDED.\n"); + ShowNotice("Please edit your '%s' trusted list to fit your network configuration.\n", filename); } - if (sockt->allowed_ip) { - aFree(sockt->allowed_ip); - sockt->allowed_ip = NULL; - } - sockt->allowed_ip_count = 0; - if (sockt->net_config_read_sub(libconfig->lookup(&network_config, "allowed"), &sockt->allowed_ip, &sockt->allowed_ip_count, filename, "allowed") > 0) - ShowStatus("Read information about %d allowed server IP ranges.\n", sockt->allowed_ip_count); - if (sockt->allowed_ip_count == 0) { + VECTOR_CLEAR(sockt->allowed_ips); + if (sockt->net_config_read_sub(libconfig->lookup(&network_config, "allowed"), &sockt->allowed_ips, filename, "allowed") > 0) + ShowStatus("Read information about %d allowed server IP ranges.\n", (int)VECTOR_LENGTH(sockt->allowed_ips)); + if (VECTOR_LENGTH(sockt->allowed_ips) + VECTOR_LENGTH(sockt->trusted_ips) == 0) { ShowError("No allowed server IP ranges configured. This server won't be able to accept connections from any char servers.\n"); } - for (i = 0; i < sockt->allowed_ip_count; ++i) { - if ((sockt->allowed_ip[i].ip & sockt->allowed_ip[i].mask) == 0) { - ShowWarning("Using a wildcard IP range in the allowed server IPs is NOT RECOMMENDED.\n"); - ShowNotice("Please edit your '%s' allowed list to fit your network configuration.\n", filename); - break; - } + ARR_FIND(0, VECTOR_LENGTH(sockt->allowed_ips), i, SUBNET_MATCH(0, VECTOR_INDEX(sockt->allowed_ips, i).ip, VECTOR_INDEX(sockt->allowed_ips, i).mask)); + if (i != VECTOR_LENGTH(sockt->allowed_ips)) { + ShowWarning("Using a wildcard IP range in the allowed server IPs is NOT RECOMMENDED.\n"); + ShowNotice("Please edit your '%s' allowed list to fit your network configuration.\n", filename); } libconfig->destroy(&network_config); return; @@ -1763,12 +1735,9 @@ void socket_defaults(void) { memset(&sockt->addr_, 0, sizeof(sockt->addr_)); sockt->naddr_ = 0; /* */ - sockt->lan_subnet_count = 0; - sockt->lan_subnet = NULL; - sockt->allowed_ip_count = 0; - sockt->allowed_ip = NULL; - sockt->trusted_ip_count = 0; - sockt->trusted_ip = NULL; + VECTOR_INIT(sockt->lan_subnets); + VECTOR_INIT(sockt->allowed_ips); + VECTOR_INIT(sockt->trusted_ips); sockt->init = socket_init; sockt->final = socket_final; diff --git a/src/common/socket.h b/src/common/socket.h index a2dea8b74..426f8e4bb 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -7,6 +7,7 @@ #include "common/hercules.h" #include "common/conf.h" +#include "common/db.h" #ifdef WIN32 # include "common/winapi.h" @@ -118,6 +119,9 @@ struct s_subnet { uint32 mask; }; +/// A vector of subnets/IP ranges. +VECTOR_STRUCT_DECL(s_subnet_vector, struct s_subnet); + /// Use a shortlist of sockets instead of iterating all sessions for sockets /// that have data to send or need eof handling. /// Adapted to use a static array instead of a linked list. @@ -129,6 +133,11 @@ struct s_subnet { #define CONVIP(ip) ((ip)>>24)&0xFF,((ip)>>16)&0xFF,((ip)>>8)&0xFF,((ip)>>0)&0xFF #define MAKEIP(a,b,c,d) ((uint32)( ( ( (a)&0xFF ) << 24 ) | ( ( (b)&0xFF ) << 16 ) | ( ( (c)&0xFF ) << 8 ) | ( ( (d)&0xFF ) << 0 ) )) +/// Applies a subnet mask to an IP +#define APPLY_MASK(ip, mask) ((ip)&(mask)) +/// Verifies the match between two IPs, with a subnet mask applied +#define SUBNET_MATCH(ip1, ip2, mask) (APPLY_MASK((ip1), (mask)) == APPLY_MASK((ip2), (mask))) + /** * Socket.c interface, mostly for reading however. **/ @@ -143,12 +152,9 @@ struct socket_interface { struct socket_data **session; - struct s_subnet *lan_subnet; ///< LAN subnets array - int lan_subnet_count; ///< LAN subnets count - struct s_subnet *trusted_ip; ///< Trusted IP ranges array - int trusted_ip_count; ///< Trusted IP ranges count - struct s_subnet *allowed_ip; ///< Allowed server IP ranges array - int allowed_ip_count; ///< Allowed server IP ranges count + struct s_subnet_vector lan_subnets; ///< LAN subnets. + struct s_subnet_vector trusted_ips; ///< Trusted IP ranges + struct s_subnet_vector allowed_ips; ///< Allowed server IP ranges /* */ void (*init) (void); @@ -187,7 +193,7 @@ struct socket_interface { uint32 (*lan_subnet_check) (uint32 ip, struct s_subnet *info); bool (*allowed_ip_check) (uint32 ip); bool (*trusted_ip_check) (uint32 ip); - int (*net_config_read_sub) (config_setting_t *t, struct s_subnet **list, int *count, const char *filename, const char *groupname); + int (*net_config_read_sub) (config_setting_t *t, struct s_subnet_vector *list, const char *filename, const char *groupname); void (*net_config_read) (const char *filename); }; -- cgit v1.2.3-70-g09d2