updated minor memory management to avoid text corruption

This commit is contained in:
faraphel 2024-12-23 10:44:06 +01:00
parent 4e547635d8
commit 9c413c0edd
6 changed files with 17 additions and 15 deletions

View file

@ -9,14 +9,14 @@
namespace gns { namespace gns {
GnsNode::GnsNode(const GnsProject *project, std::string uuid, const std::vector<GnsPort>& ports) { GnsNode::GnsNode(const GnsProject *project, const std::string& uuid, const std::vector<GnsPort>& ports) {
this->project = project; this->project = project;
this->uuid = std::move(uuid); this->uuid = uuid;
this->ports = ports; this->ports = ports;
} }
std::string GnsNode::getApiBase() const { std::string GnsNode::getApiBase() const {
return this->project->getApiBase() + "nodes/" + this->uuid + "/"; return this->project->getApiBase() + "nodes/" + this->getUuid() + "/";
} }
std::string GnsNode::getUuid() const { std::string GnsNode::getUuid() const {
@ -30,7 +30,7 @@ std::vector<GnsPort> GnsNode::getPorts() const {
void GnsNode::start() const { void GnsNode::start() const {
// request the API endpoint // request the API endpoint
cpr::Url endpoint = this->getApiBase() + "start"; cpr::Url endpoint = this->getApiBase() + "start";
cpr::Response response = Post(endpoint); const cpr::Response response = Post(endpoint);
// check for a valid response // check for a valid response
if (response.error.code != cpr::ErrorCode::OK) if (response.error.code != cpr::ErrorCode::OK)

View file

@ -18,7 +18,7 @@ class GnsProject;
*/ */
class GnsNode { class GnsNode {
public: public:
GnsNode(const GnsProject* project, std::string uuid, const std::vector<GnsPort>& ports); GnsNode(const GnsProject* project, const std::string& uuid, const std::vector<GnsPort>& ports);
/** /**
* Get the base URL for the node API * Get the base URL for the node API

View file

@ -10,13 +10,13 @@ using json = nlohmann::json;
namespace gns { namespace gns {
GnsProject::GnsProject(const GnsServer* server, std::string uuid) { GnsProject::GnsProject(const GnsServer* server, const std::string& uuid) {
this->server = server; this->server = server;
this->uuid = std::move(uuid); this->uuid = uuid;
} }
std::string GnsProject::getApiBase() const { std::string GnsProject::getApiBase() const {
return this->server->getApiBase() + "projects/" + this->uuid + "/"; return this->server->getApiBase() + "projects/" + this->getUuid() + "/";
} }
std::string GnsProject::getUuid() const { std::string GnsProject::getUuid() const {

View file

@ -16,7 +16,7 @@ class GnsServer;
*/ */
class GnsProject { class GnsProject {
public: public:
GnsProject(const GnsServer* server, std::string uuid); GnsProject(const GnsServer* server, const std::string& uuid);
/** /**
* Get the base prefix for an API request * Get the base prefix for an API request

View file

@ -8,7 +8,7 @@ using json = nlohmann::json;
namespace gns { namespace gns {
GnsServer::GnsServer(const std::string &host, const std::uint16_t port) { GnsServer::GnsServer(const std::string& host, const std::uint16_t port) {
this->host = host; this->host = host;
this->port = port; this->port = port;
} }

View file

@ -14,6 +14,10 @@
#include "utils/address.hpp" #include "utils/address.hpp"
std::shared_ptr<gns::GnsServer> gnsServer;
/** /**
* Callback of a detected Wake-On-LAN packet * Callback of a detected Wake-On-LAN packet
*/ */
@ -22,8 +26,6 @@ void packet_wol_handler(
const pcap_pkthdr* header, const pcap_pkthdr* header,
const std::uint8_t* packet const std::uint8_t* packet
) { ) {
const auto server = reinterpret_cast<gns::GnsServer*>(user_data);
// get the special sll header (added by pcap when using the "any" device) // get the special sll header (added by pcap when using the "any" device)
std::uint16_t sll_header = *packet; std::uint16_t sll_header = *packet;
// get the ethernet header of the packet // get the ethernet header of the packet
@ -48,7 +50,7 @@ void packet_wol_handler(
// TODO(Faraphel): check the 16 repetitions of the source address ? // TODO(Faraphel): check the 16 repetitions of the source address ?
// find the machine with the mac address // find the machine with the mac address
for (const gns::GnsNode& node : server->getNodes()) for (const gns::GnsNode& node : gnsServer->getNodes())
for (const gns::GnsPort& port : node.getPorts()) for (const gns::GnsPort& port : node.getPorts())
if (port.mac_address == mac_address_target) { if (port.mac_address == mac_address_target) {
std::cout << "Matching node: " + node.getUuid() << std::endl; std::cout << "Matching node: " + node.getUuid() << std::endl;
@ -62,7 +64,7 @@ void packet_wol_handler(
int main() { int main() {
// get the GNS3 server // get the GNS3 server
auto server = gns::GnsServer("localhost", 80); gnsServer = std::make_shared<gns::GnsServer>("localhost", 80);
// capture any packet on the selected interface // capture any packet on the selected interface
char error_buffer[PCAP_ERRBUF_SIZE]; char error_buffer[PCAP_ERRBUF_SIZE];
@ -89,7 +91,7 @@ int main() {
handle.get(), handle.get(),
0, 0,
packet_wol_handler, packet_wol_handler,
reinterpret_cast<uint8_t*>(&server) nullptr
) == -1) ) == -1)
throw std::runtime_error("pcap_loop() failed: " + std::string(error_buffer)); throw std::runtime_error("pcap_loop() failed: " + std::string(error_buffer));