Skip to content

Commit

Permalink
Performance improvmements. Removed timestamps and some unused code. A…
Browse files Browse the repository at this point in the history
…dded lifetime info in update messages
  • Loading branch information
easymoney322 committed Jun 27, 2023
1 parent 1098180 commit 06ad40f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 44 deletions.
2 changes: 1 addition & 1 deletion GlobalVars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ extern struct ProtoPort
std::string proto{ "UDP" };
uint_fast16_t portnumin{ 0 };
uint_fast16_t portnumout{ 0 };
uint_fast32_t maptime{ 1 };
std::string UpdateText{ "" };
};
extern struct ProlongationStruct
{
pcpp::Packet Packet;
uint_fast64_t TimestampOfNextUpdate;
std::chrono::time_point <std::chrono::system_clock> chronotimepoint;
ProtoPort ProtocolAndPort;
uint_fast64_t PacketID{ 0 };
Expand Down
50 changes: 10 additions & 40 deletions NetFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,6 @@ uint_fast8_t PrintPayloadFromPacket(pcpp::Packet packet)
}


uint_fast64_t CreateTimestampOfNextUpdate(pcpp::PayloadLayer payload)
{
uint8_t* pdata = payload.getPayload();
uint_fast32_t* packetlifetime = (uint_fast32_t*)&pdata[8];
uint_fast32_t hostorder = ntohl(*packetlifetime);
hostorder /= 2; //Time till refresh in seconds
uint_fast64_t systime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); //Current time
uint_fast64_t timeofrefresh = systime + hostorder; //Time of required update

return timeofrefresh;
}


std::chrono::time_point <std::chrono::system_clock> CreateTimepointOfNextUpdate(pcpp::PayloadLayer payload)
{
uint8_t* pdata = payload.getPayload();
Expand All @@ -352,31 +339,14 @@ std::chrono::time_point <std::chrono::system_clock> CreateTimepointOfNextUpdate(
}



uint_fast64_t CreateTimestampOfNextUpdate(pcpp::Packet packet)
{
uint8_t* ppayload = packet.getLayerOfType<pcpp::PayloadLayer>()->getPayload();
uint_fast64_t retval = CreateTimestampOfNextUpdate(*ppayload);
return retval;
}

uint_fast64_t CreateTimestampOfNextUpdate(uint32_t lifetime)
{

uint_fast64_t secondstillupdate = lifetime / 2;
uint_fast64_t systime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); //Current time
uint_fast64_t timeofrefresh = systime + secondstillupdate; //Time of required update

return timeofrefresh;
}

ProtoPort GetProtoAndPortFromPayloadLayer(pcpp::PayloadLayer lspayload)
{
uint8_t* pdata = lspayload.getPayload();
uint8_t opmode = pdata[1];
uint_fast8_t opmode = pdata[1];
std::string retstr;
uint16_t retint = 0;
uint16_t retext = 0;
uint_fast16_t retint = 0;
uint_fast16_t retext = 0;
uint_fast32_t maptime = 1;
if (1 == opmode)
{
retstr = "UDP";
Expand All @@ -392,10 +362,12 @@ ProtoPort GetProtoAndPortFromPayloadLayer(pcpp::PayloadLayer lspayload)

uint_fast16_t* portnumint = (uint_fast16_t*)&pdata[4]; //4,5 = INT
retint = ntohs(*portnumint);
uint_fast16_t* portnumext = (uint_fast16_t*)&pdata[6]; //4,5 = INT
uint_fast16_t* portnumext = (uint_fast16_t*)&pdata[6]; //6,7 = EXT
retext = ntohs(*portnumext);
std::string Updstr = "Updated mapping for " + retstr + '-' + std::to_string(retint) + '-' + std::to_string(retext) + '.';
return { retstr,retint, retext, Updstr };
uint_fast32_t* maplifetime = (uint_fast32_t*)&pdata[8];
maptime = ntohl(*maplifetime);
std::string Updstr = "Updated mapping for " + retstr + '-' + std::to_string(retint) + '-' + std::to_string(retext) + '-' + std::to_string(maptime) + '.';
return { retstr,retint, retext, maptime, Updstr };
}

ProtoPort GetProtoAndPortFromPacket(pcpp::Packet packet)
Expand Down Expand Up @@ -478,7 +450,6 @@ uint_fast8_t SendPacketWrap(pcpp::Packet &lspacket, WinDev lsOut)
{
alreadyexists = true;
std::chrono::time_point <std::chrono::system_clock> UpdateTime = CreateTimepointOfNextUpdate(lspayload);
ProlongationList.at(sp).TimestampOfNextUpdate = CreateTimestampOfNextUpdate(lspacket);
ProlongationList.at(sp).chronotimepoint = UpdateTime;
std::time_t datetimerefr = std::chrono::system_clock::to_time_t(UpdateTime);
std::string str1 = ProlongationList.at(sp).ProtocolAndPort.UpdateText;
Expand All @@ -501,8 +472,7 @@ uint_fast8_t SendPacketWrap(pcpp::Packet &lspacket, WinDev lsOut)
if (false == alreadyexists)
{
ProtoPort lsportstruc = GetProtoAndPortFromPayloadLayer(lspayload);
std::string Updstr= "Updated mapping for " + lsportstruc.proto + '-' + std::to_string(lsportstruc.portnumin) + '-' + std::to_string(lsportstruc.portnumout) + '.';
ProlongationList.push_back({ lspacket, CreateTimestampOfNextUpdate(mappinglifetime),CreateTimepointOfNextUpdate(lspayload),lsportstruc});
ProlongationList.push_back({ lspacket ,CreateTimepointOfNextUpdate(lspayload),lsportstruc});
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions NetFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ uint_fast8_t PrintPayloadFromPacket(pcpp::Packet packet);
uint_fast8_t RemoveCreatedMappings(std::vector <pcpp::Packet>& packetvector, WinDev lsOut);
uint_fast8_t SendPacketWrap(pcpp::Packet& lspacket, WinDev lsOut);
uint_fast8_t WatchList();
uint_fast64_t CreateTimestampOfNextUpdate(pcpp::Packet packet);
uint_fast64_t CreateTimestampOfNextUpdate(pcpp::PayloadLayer payload);
uint_fast64_t CreateTimestampOfNextUpdate(uint32_t lifetime);
std::chrono::time_point <std::chrono::system_clock> CreateTimepointOfNextUpdate(pcpp::PayloadLayer payload);


Expand Down

0 comments on commit 06ad40f

Please sign in to comment.