Program Listing for File dns_construct_helpers.h

Return to documentation for file (include/dns_construct_helpers.h)

#pragma once
#include "spdlog/spdlog.h"
#include <string.h>

#include <algorithm>

class DNSHelpers {
public:
    static inline char* ChangetoDnsNameFormat(char* dns, const char* host, int host_len);
};

char* DNSHelpers::ChangetoDnsNameFormat(char* dns, const char* host, int host_len) {
    int lock = 0, i;

    // Do not count room for an extra character when the last character is a .
    int final_len = host_len + (host[std::max(host_len - 1, 0)] != '.');

    // Cast to unsigned char to prevent overflows
    unsigned char* u_dns = (unsigned char*) dns;

    for (i = 0; i < final_len; i++) {
        if (host[i] == '.' || host[i] == '\0') {
            *u_dns++ = i - lock;
            memcpy(u_dns, host + sizeof(unsigned char) * lock,
                sizeof(unsigned char) * (i - lock));
            u_dns += i - lock;
            lock = i + 1;
        }
    }
    *u_dns++ = '\0';

    return (char*) u_dns;
}