From 6ffde9bb4417a1546d31dc314c443f329b8573fe Mon Sep 17 00:00:00 2001 From: Gent Semaj Date: Mon, 28 Oct 2024 19:50:35 -0700 Subject: [PATCH] Replace most usages of CN_PACKET_BUFFER_SIZE with usable body size --- src/Abilities.cpp | 8 ++++---- src/Buddies.cpp | 4 ++-- src/Buffs.cpp | 6 +++--- src/Combat.cpp | 14 +++++++------- src/Eggs.cpp | 6 +++--- src/Groups.cpp | 12 ++++++------ src/Items.cpp | 12 ++++++------ src/Missions.cpp | 8 ++++---- src/MobAI.cpp | 4 ++-- src/core/CNProtocol.hpp | 8 ++++---- src/core/CNStructs.hpp | 4 ++-- src/core/Defines.hpp | 5 +++++ 12 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/Abilities.cpp b/src/Abilities.cpp index 2d90a43..fc5e615 100644 --- a/src/Abilities.cpp +++ b/src/Abilities.cpp @@ -334,8 +334,8 @@ void Abilities::useNanoSkill(CNSocket* sock, SkillData* skill, sNano& nano, std: size_t resplen = sizeof(sP_FE2CL_NANO_SKILL_USE_SUCC); for(SkillResult& sr : results) resplen += sr.size; - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, resplen); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_NANO_SKILL_USE_SUCC* pkt = (sP_FE2CL_NANO_SKILL_USE_SUCC*)respbuf; pkt->iPC_ID = plr->iID; @@ -379,8 +379,8 @@ void Abilities::useNPCSkill(EntityRef npc, int skillID, std::vector size_t resplen = sizeof(sP_FE2CL_NPC_SKILL_HIT); for(SkillResult& sr : results) resplen += sr.size; - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, resplen); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_NPC_SKILL_HIT* pkt = (sP_FE2CL_NPC_SKILL_HIT*)respbuf; pkt->iNPC_ID = npc.id; diff --git a/src/Buddies.cpp b/src/Buddies.cpp index fc53551..1fbf31d 100644 --- a/src/Buddies.cpp +++ b/src/Buddies.cpp @@ -41,9 +41,9 @@ void Buddies::sendBuddyList(CNSocket* sock) { // initialize response struct size_t resplen = sizeof(sP_FE2CL_REP_PC_BUDDYLIST_INFO_SUCC) + buddyCnt * sizeof(sBuddyBaseInfo); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; + uint8_t respbuf[CN_PACKET_BODY_SIZE]; - memset(respbuf, 0, resplen); + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_REP_PC_BUDDYLIST_INFO_SUCC* resp = (sP_FE2CL_REP_PC_BUDDYLIST_INFO_SUCC*)respbuf; sBuddyBaseInfo* respdata = (sBuddyBaseInfo*)(respbuf + sizeof(sP_FE2CL_REP_PC_BUDDYLIST_INFO_SUCC)); diff --git a/src/Buffs.cpp b/src/Buffs.cpp index 179f1c6..d225c94 100644 --- a/src/Buffs.cpp +++ b/src/Buffs.cpp @@ -178,9 +178,9 @@ void Buffs::tickDrain(EntityRef self, Buff* buff, int mult) { int dealt = combatant->takeDamage(buff->getLastSource(), damage); size_t resplen = sizeof(sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK) + sizeof(sSkillResult_Damage); - assert(resplen < CN_PACKET_BUFFER_SIZE - 8); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, resplen); + assert(resplen < CN_PACKET_BODY_SIZE); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK *pkt = (sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK*)respbuf; pkt->iID = self.id; diff --git a/src/Combat.cpp b/src/Combat.cpp index 37565ee..2820e8c 100644 --- a/src/Combat.cpp +++ b/src/Combat.cpp @@ -539,9 +539,9 @@ static void dealGooDamage(CNSocket *sock) { return; // ignore completely size_t resplen = sizeof(sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK) + sizeof(sSkillResult_DotDamage); - assert(resplen < CN_PACKET_BUFFER_SIZE - 8); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, resplen); + assert(resplen < CN_PACKET_BODY_SIZE); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK *pkt = (sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK*)respbuf; sSkillResult_DotDamage *dmg = (sSkillResult_DotDamage*)(respbuf + sizeof(sP_FE2CL_CHAR_TIME_BUFF_TIME_TICK)); @@ -633,9 +633,9 @@ static void pcAttackChars(CNSocket *sock, CNPacketData *data) { // initialize response struct size_t resplen = sizeof(sP_FE2CL_PC_ATTACK_CHARs_SUCC) + pkt->iTargetCnt * sizeof(sAttackResult); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; + uint8_t respbuf[CN_PACKET_BODY_SIZE]; - memset(respbuf, 0, resplen); + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_PC_ATTACK_CHARs_SUCC *resp = (sP_FE2CL_PC_ATTACK_CHARs_SUCC*)respbuf; sAttackResult *respdata = (sAttackResult*)(respbuf+sizeof(sP_FE2CL_PC_ATTACK_CHARs_SUCC)); @@ -847,9 +847,9 @@ static void projectileHit(CNSocket* sock, CNPacketData* data) { */ size_t resplen = sizeof(sP_FE2CL_PC_GRENADE_STYLE_HIT) + pkt->iTargetCnt * sizeof(sAttackResult); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; + uint8_t respbuf[CN_PACKET_BODY_SIZE]; - memset(respbuf, 0, resplen); + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_PC_GRENADE_STYLE_HIT* resp = (sP_FE2CL_PC_GRENADE_STYLE_HIT*)respbuf; sAttackResult* respdata = (sAttackResult*)(respbuf + sizeof(sP_FE2CL_PC_GRENADE_STYLE_HIT)); diff --git a/src/Eggs.cpp b/src/Eggs.cpp index 73f22f6..38cd97e 100644 --- a/src/Eggs.cpp +++ b/src/Eggs.cpp @@ -87,8 +87,8 @@ void Eggs::eggBuffPlayer(CNSocket* sock, int skillId, int eggId, int duration) { // initialize response struct size_t resplen = sizeof(sP_FE2CL_NPC_SKILL_HIT) + result.size; - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, resplen); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_NPC_SKILL_HIT* pkt = (sP_FE2CL_NPC_SKILL_HIT*)respbuf; pkt->iNPC_ID = eggId; @@ -192,7 +192,7 @@ static void eggPickup(CNSocket* sock, CNPacketData* data) { // drop if (type->dropCrateId != 0) { const size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + sizeof(sItemReward); - assert(resplen < CN_PACKET_BUFFER_SIZE - 8); + assert(resplen < CN_PACKET_BODY_SIZE); // we know it's only one trailing struct, so we can skip full validation uint8_t respbuf[resplen]; // not a variable length array, don't worry diff --git a/src/Groups.cpp b/src/Groups.cpp index 8d948b8..fc649ea 100644 --- a/src/Groups.cpp +++ b/src/Groups.cpp @@ -87,8 +87,8 @@ void Groups::addToGroup(Group* group, EntityRef member) { size_t pcCount = pcs.size(); size_t npcCount = npcs.size(); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, CN_PACKET_BUFFER_SIZE); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_PC_GROUP_JOIN* pkt = (sP_FE2CL_PC_GROUP_JOIN*)respbuf; pkt->iID_NewMember = PlayerManager::getPlayer(member.sock)->iID; @@ -143,8 +143,8 @@ bool Groups::removeFromGroup(Group* group, EntityRef member) { size_t pcCount = pcs.size(); size_t npcCount = npcs.size(); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, CN_PACKET_BUFFER_SIZE); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_PC_GROUP_LEAVE* pkt = (sP_FE2CL_PC_GROUP_LEAVE*)respbuf; pkt->iID_LeaveMember = PlayerManager::getPlayer(member.sock)->iID; @@ -288,8 +288,8 @@ void Groups::groupTickInfo(CNSocket* sock) { size_t pcCount = pcs.size(); size_t npcCount = npcs.size(); - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, CN_PACKET_BUFFER_SIZE); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_PC_GROUP_MEMBER_INFO* pkt = (sP_FE2CL_PC_GROUP_MEMBER_INFO*)respbuf; pkt->iID = plr->iID; diff --git a/src/Items.cpp b/src/Items.cpp index df7cb40..b11c086 100644 --- a/src/Items.cpp +++ b/src/Items.cpp @@ -46,7 +46,7 @@ static void nanoCapsuleHandler(CNSocket* sock, int slot, sItemBase *chest) { // in order to remove capsule form inventory, we have to send item reward packet with empty item const size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + sizeof(sItemReward); - assert(resplen < CN_PACKET_BUFFER_SIZE - 8); + assert(resplen < CN_PACKET_BODY_SIZE); // we know it's only one trailing struct, so we can skip full validation uint8_t respbuf[resplen]; // not a variable length array, don't worry @@ -475,8 +475,8 @@ static void itemUseHandler(CNSocket* sock, CNPacketData* data) { if (gumball.iOpt == 0) gumball = {}; - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, resplen); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_REP_PC_ITEM_USE_SUCC *resp = (sP_FE2CL_REP_PC_ITEM_USE_SUCC*)respbuf; sSkillResult_Buff *respdata = (sSkillResult_Buff*)(respbuf+sizeof(sP_FE2CL_NANO_SKILL_USE_SUCC)); @@ -556,7 +556,7 @@ static void chestOpenHandler(CNSocket *sock, CNPacketData *data) { // item giving packet const size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + sizeof(sItemReward); - assert(resplen < CN_PACKET_BUFFER_SIZE - 8); + assert(resplen < CN_PACKET_BODY_SIZE); // we know it's only one trailing struct, so we can skip full validation uint8_t respbuf[resplen]; // not a variable length array, don't worry @@ -645,7 +645,7 @@ void Items::checkItemExpire(CNSocket* sock, Player* player) { */ const size_t resplen = sizeof(sP_FE2CL_PC_DELETE_TIME_LIMIT_ITEM) + sizeof(sTimeLimitItemDeleteInfo2CL); - assert(resplen < CN_PACKET_BUFFER_SIZE - 8); + assert(resplen < CN_PACKET_BODY_SIZE); // we know it's only one trailing struct, so we can skip full validation uint8_t respbuf[resplen]; // not a variable length array, don't worry auto packet = (sP_FE2CL_PC_DELETE_TIME_LIMIT_ITEM*)respbuf; @@ -715,7 +715,7 @@ static void giveSingleDrop(CNSocket *sock, Mob* mob, int mobDropId, const DropRo Player *plr = PlayerManager::getPlayer(sock); const size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + sizeof(sItemReward); - assert(resplen < CN_PACKET_BUFFER_SIZE - 8); + assert(resplen < CN_PACKET_BODY_SIZE); // we know it's only one trailing struct, so we can skip full validation uint8_t respbuf[resplen]; // not a variable length array, don't worry diff --git a/src/Missions.cpp b/src/Missions.cpp index 7bbdba5..67c1161 100644 --- a/src/Missions.cpp +++ b/src/Missions.cpp @@ -64,7 +64,7 @@ static bool isQuestItemFull(CNSocket* sock, int itemId, int itemCount) { static void dropQuestItem(CNSocket *sock, int task, int count, int id, int mobid) { std::cout << "Altered item id " << id << " by " << count << " for task id " << task << std::endl; const size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + sizeof(sItemReward); - assert(resplen < CN_PACKET_BUFFER_SIZE); + assert(resplen < CN_PACKET_BODY_SIZE); // we know it's only one trailing struct, so we can skip full validation Player *plr = PlayerManager::getPlayer(sock); @@ -152,14 +152,14 @@ static int giveMissionReward(CNSocket *sock, int task, int choice=0) { plr->Inven[slots[i]] = { 999, 999, 999, 0 }; // temp item; overwritten later } - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; + uint8_t respbuf[CN_PACKET_BODY_SIZE]; size_t resplen = sizeof(sP_FE2CL_REP_REWARD_ITEM) + nrewards * sizeof(sItemReward); - assert(resplen < CN_PACKET_BUFFER_SIZE); + assert(resplen < CN_PACKET_BODY_SIZE); sP_FE2CL_REP_REWARD_ITEM *resp = (sP_FE2CL_REP_REWARD_ITEM *)respbuf; sItemReward *item = (sItemReward *)(respbuf + sizeof(sP_FE2CL_REP_REWARD_ITEM)); // don't forget to zero the buffer! - memset(respbuf, 0, resplen); + memset(respbuf, 0, CN_PACKET_BODY_SIZE); // update player plr->money += reward->money; diff --git a/src/MobAI.cpp b/src/MobAI.cpp index dbb700c..50c8742 100644 --- a/src/MobAI.cpp +++ b/src/MobAI.cpp @@ -238,8 +238,8 @@ static void dealCorruption(Mob *mob, std::vector targetData, int skillID, i return; } - uint8_t respbuf[CN_PACKET_BUFFER_SIZE]; - memset(respbuf, 0, resplen); + uint8_t respbuf[CN_PACKET_BODY_SIZE]; + memset(respbuf, 0, CN_PACKET_BODY_SIZE); sP_FE2CL_NPC_SKILL_CORRUPTION_HIT *resp = (sP_FE2CL_NPC_SKILL_CORRUPTION_HIT*)respbuf; sCAttackResult *respdata = (sCAttackResult*)(respbuf+sizeof(sP_FE2CL_NPC_SKILL_CORRUPTION_HIT)); diff --git a/src/core/CNProtocol.hpp b/src/core/CNProtocol.hpp index 9d1a6b2..1bf5dbc 100644 --- a/src/core/CNProtocol.hpp +++ b/src/core/CNProtocol.hpp @@ -95,14 +95,14 @@ inline constexpr bool isOutboundPacketID(uint32_t id) { // for outbound packets inline constexpr bool validOutVarPacket(size_t base, size_t npayloads, size_t plsize) { // check for multiplication overflow - if (npayloads > 0 && (CN_PACKET_BUFFER_SIZE - 8) / (size_t)npayloads < plsize) + if (npayloads > 0 && (CN_PACKET_BODY_SIZE) / (size_t)npayloads < plsize) return false; // it's safe to multiply size_t trailing = npayloads * plsize; // does it fit in a packet? - if (base + trailing > CN_PACKET_BUFFER_SIZE - 8) + if (base + trailing > CN_PACKET_BODY_SIZE) return false; // everything is a-ok! @@ -112,14 +112,14 @@ inline constexpr bool validOutVarPacket(size_t base, size_t npayloads, size_t pl // for inbound packets inline constexpr bool validInVarPacket(size_t base, size_t npayloads, size_t plsize, size_t datasize) { // check for multiplication overflow - if (npayloads > 0 && (CN_PACKET_BUFFER_SIZE - 8) / (size_t)npayloads < plsize) + if (npayloads > 0 && CN_PACKET_BODY_SIZE / (size_t)npayloads < plsize) return false; // it's safe to multiply size_t trailing = npayloads * plsize; // make sure size is exact - // datasize has already been validated against CN_PACKET_BUFFER_SIZE + // datasize has already been validated against CN_PACKET_BODY_SIZE if (datasize != base + trailing) return false; diff --git a/src/core/CNStructs.hpp b/src/core/CNStructs.hpp index 539c89f..f870f52 100644 --- a/src/core/CNStructs.hpp +++ b/src/core/CNStructs.hpp @@ -29,8 +29,8 @@ #define INITSTRUCT(T, x) T x; \ memset(&x, 0, sizeof(T)); -#define INITVARPACKET(_buf, _Pkt, _pkt, _Trailer, _trailer) uint8_t _buf[CN_PACKET_BUFFER_SIZE]; \ - memset(&_buf, 0, CN_PACKET_BUFFER_SIZE); \ +#define INITVARPACKET(_buf, _Pkt, _pkt, _Trailer, _trailer) uint8_t _buf[CN_PACKET_BODY_SIZE]; \ + memset(&_buf, 0, CN_PACKET_BODY_SIZE); \ auto _pkt = (_Pkt*)_buf; \ auto _trailer = (_Trailer*)(_pkt + 1); diff --git a/src/core/Defines.hpp b/src/core/Defines.hpp index 7939d14..c68f86d 100644 --- a/src/core/Defines.hpp +++ b/src/core/Defines.hpp @@ -934,3 +934,8 @@ enum { N_PACKETS = N_CL2LS + N_CL2FE + N_FE2CL + N_LS2CL }; + +/* + * Usable space in the packet buffer = CN_PACKET_BUFFER_SIZE - type - size + */ +constexpr size_t CN_PACKET_BODY_SIZE = CN_PACKET_BUFFER_SIZE - 2 * sizeof(int32_t);