<commit_before>#include <dhcp/pkt4.h>
#include <log/logger.h>
#include <log/macros.h>
#include <log/message_initializer.h>
#include "library_common.h"
#include <curl/curl.h>
#include <sstream>

using namespace isc::dhcp;
using namespace isc::hooks;
using namespace std;
using namespace isc::log;
namespace pt = boost::property_tree;


isc::log::Logger logger("pxe-replace-logger");
const char* log_messages[] = {
    "PRL_BASE", "message: %1",
    "PRL_PKT_SEND", "Outgoing packet: \n%1",
    "PRL_CURL_RECEIVED", "Received: \n%1",
    "PRL_CURL_FAILED", "Unable to receive data from %1",
    "PRL_REPLACE_FIELD", "Replacing: %1 with %2",
    "PRL_NO_SIADDR", "siaddr field not being replaced, this is probably an error.",
    NULL
};

/// Initializer for log messages.
const MessageInitializer message_initializer(log_messages);

extern "C" {
int pkt4_send(CalloutHandle& handle) {

    CURL *curl;
    CURLcode res;
    struct curl_slist *list=NULL;
    bool perform_updates = 0;
    int curl_opt_res = 0;

    char *bp;
    size_t size;
    FILE *response_memfile;

    pt::ptree root;
    std::stringstream ss;
    string hwaddr;
    string final_url;
    //OptionCollection 	options_collection;
    //options_collection = response4_ptr->options_;
    OptionPtr tftp_server_name_opt_ptr;
    OptionPtr bootfile_name_opt_ptr;
    boost::optional<std::string> siaddr_json_field;
    boost::optional<std::string> tftp_server_json_field;
    boost::optional<std::string> bootfile_json_field;

    Pkt4Ptr response4_ptr;
    handle.getArgument("response4", response4_ptr);
    HWAddrPtr hwaddr_ptr = response4_ptr->getHWAddr();
    isc::asiolink::IOAddress orig_siaddr(response4_ptr->getSiaddr());
    hwaddr = hwaddr_ptr->toText(false);
    //json_params[0].append(hwaddr);
    final_url = json_params[0] + hwaddr;
    LOG_DEBUG(logger, 0, "PRL_BASE").arg(final_url);

    tftp_server_name_opt_ptr = response4_ptr->getOption((uint16_t)66);
    bootfile_name_opt_ptr = response4_ptr->getOption((uint16_t)67);

    if (tftp_server_name_opt_ptr != NULL || bootfile_name_opt_ptr != NULL
        || orig_siaddr.toText() != "0.0.0.0")
        perform_updates = 1;

    if (!perform_updates) {
        LOG_WARN(logger, "PRL_BASE").arg("Nothing to update.");
        return(0);
    }

    curl_global_init(CURL_GLOBAL_DEFAULT);<commit_msg>variable cleanup<commit_after>#include <dhcp/pkt4.h>
#include <log/logger.h>
#include <log/macros.h>
#include <log/message_initializer.h>
#include "library_common.h"
#include <curl/curl.h>
#include <sstream>

using namespace isc::dhcp;
using namespace isc::hooks;
using namespace std;
using namespace isc::log;
namespace pt = boost::property_tree;


isc::log::Logger logger("pxe-replace-logger");
const char* log_messages[] = {
    "PRL_BASE", "message: %1",
    "PRL_PKT_SEND", "Outgoing packet: \n%1",
    "PRL_CURL_RECEIVED", "Received: \n%1",
    "PRL_CURL_FAILED", "Unable to receive data from %1",
    "PRL_REPLACE_FIELD", "Replacing: %1 with %2",
    "PRL_NO_SIADDR", "siaddr field not being replaced, this is probably an error.",
    NULL
};

/// Initializer for log messages.
const MessageInitializer message_initializer(log_messages);

extern "C" {
int pkt4_send(CalloutHandle& handle) {

    // Variables related to curl (there are "C" variables)
    CURL *curl;
    CURLcode res;
    struct curl_slist *list=NULL;
    bool perform_updates = 0;
    int curl_opt_res = 0;

    // "C" varaibles.
    char *bp;
    size_t size;
    FILE *response_memfile;

    // string variables
    string hwaddr;
    string final_url;

    // pkt4 related variables.
    Pkt4Ptr response4_ptr;
    HWAddrPtr hwaddr_ptr;
    OptionPtr tftp_server_name_opt_ptr;
    OptionPtr bootfile_name_opt_ptr;
    // isc::asiolink::IOAddress orig_siaddr;
    // isc::asiolink::IOAddress new_siaddr;

    // Boost / json related variables.
    pt::ptree root;
    std::stringstream ss;
    boost::optional<std::string> siaddr_json_field;
    boost::optional<std::string> tftp_server_json_field;
    boost::optional<std::string> bootfile_json_field;

    /* Begin Code */
    handle.getArgument("response4", response4_ptr);
    hwaddr_ptr = response4_ptr->getHWAddr();
    isc::asiolink::IOAddress orig_siaddr(response4_ptr->getSiaddr());
    hwaddr = hwaddr_ptr->toText(false);

    final_url = json_params[0] + hwaddr;
    LOG_DEBUG(logger, 0, "PRL_BASE").arg(final_url);

    tftp_server_name_opt_ptr = response4_ptr->getOption((uint16_t)66);
    bootfile_name_opt_ptr = response4_ptr->getOption((uint16_t)67);

    if (tftp_server_name_opt_ptr != NULL || bootfile_name_opt_ptr != NULL
        || orig_siaddr.toText() != "0.0.0.0")
        perform_updates = 1;

    if (!perform_updates) {
        LOG_WARN(logger, "PRL_BASE").arg("Nothing to update.");
        return(0);
    }

    curl_global_init(CURL_GLOBAL_DEFAULT);
{'fraction_non_alphanumeric': 0.12903225806451613, 'fraction_numerical': 0.014681555004135649, 'mean_word_length': 4.201075268817204, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3575957', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1702, 'n_words': 341}