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

View file

@ -18,7 +18,7 @@ class GnsProject;
*/
class GnsNode {
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

View file

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

View file

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

View file

@ -14,6 +14,10 @@
#include "utils/address.hpp"
std::shared_ptr<gns::GnsServer> gnsServer;
/**
* Callback of a detected Wake-On-LAN packet
*/
@ -22,8 +26,6 @@ void packet_wol_handler(
const pcap_pkthdr* header,
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)
std::uint16_t sll_header = *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 ?
// 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())
if (port.mac_address == mac_address_target) {
std::cout << "Matching node: " + node.getUuid() << std::endl;
@ -62,7 +64,7 @@ void packet_wol_handler(
int main() {
// 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
char error_buffer[PCAP_ERRBUF_SIZE];
@ -89,7 +91,7 @@ int main() {
handle.get(),
0,
packet_wol_handler,
reinterpret_cast<uint8_t*>(&server)
nullptr
) == -1)
throw std::runtime_error("pcap_loop() failed: " + std::string(error_buffer));