<commit_before>#include "udpping.h"
#include "../../log/logger.h"

#include <time.h>
#include <Windows.h>
#include <mswsock.h>
#include <Mstcpip.h>
#include <pcap.h>

#include <QCryptographicHash>
#include <QThread>

LOGGER("UdpPing");

namespace
{
    union ipAddress
    {
        quint32 v4;
        quint8 v6[16];
    };

    union inAddress
    {
        struct in_addr v4;
        struct in6_addr v6;
    };

    bool getAddress(const QString &address, sockaddr_any *addr)
    {
        struct addrinfo hints;
        struct addrinfo *rp = NULL, *result = NULL;

        memset(&hints, 0, sizeof(hints));
        hints.ai_family = AF_UNSPEC;

        if (getaddrinfo(address.toStdString().c_str(), NULL, &hints, &result))
        {
            return false;
        }

        for (rp = result; rp && rp->ai_family != AF_INET &&
             rp->ai_family != AF_INET6; rp = rp->ai_next)
        {
        }

        if (!rp)
        {
            rp = result;
        }

        memcpy(addr, rp->ai_addr, rp->ai_addrlen);

        freeaddrinfo(result);

        return true;
    }

    PingProbe handleIpv4Response(const u_char *data,
                                 const pcap_pkthdr *header,
                                 const sockaddr_any &destination,
                                 int payloadSize)
    {
        char sourceAddress[INET_ADDRSTRLEN] = "";
        char destinationAddress[INET_ADDRSTRLEN] = "";
        ipAddress *source;
        quint8 *icmpType;
        quint8 *icmpCode;
        quint8 *ipProto;
        PingProbe probe = {0};
        QByteArray payload(0);

        if (data == NULL || header == NULL)
        {
            return probe;
        }

        ipProto = const_cast<quint8 *>(data + 23);
        source = reinterpret_cast<ipAddress *>(const_cast<u_char *>(data + 26));
        icmpType = const_cast<quint8 *>(data + 34);
        icmpCode = const_cast<quint8 *>(data + 35);

        // source address of the response packet
        RtlIpv4AddressToStringA(&(reinterpret_cast<inAddress *>(source))->v4,
                                sourceAddress);
        // destination of request packet
        RtlIpv4AddressToStringA(&destination.sin.sin_addr, destinationAddress);

        switch (*ipProto)
        {
        case 17:
            // UDP request
            probe.type = udpping::UDP;
            probe.sendTime = header->ts.tv_sec * 1e6 + header->ts.tv_usec;
            payload = QByteArray::fromRawData(reinterpret_cast<char *>(const_cast<u_char *>(data + 42)),
                                              payloadSize);
            probe.hash = QCryptographicHash::hash(payload, QCryptographicHash::Sha256).toHex();
            break;

        case 1:
            // ICMP response
            probe.type = udpping::ICMP;
            getAddress(sourceAddress, &probe.source);

            if ((*icmpType == 3 && *icmpCode == 3) ||
                (*icmpType == 11 && *icmpCode == 0))
            {
                payload = QByteArray::fromRawData(reinterpret_cast<char *>(const_cast<u_char *>(data + 70)),
                                                  payloadSize);
                probe.hash = QCryptographicHash::hash(payload, QCryptographicHash::Sha256).toHex();
                /*
                 * 'source' points to the destination field of the original
                 * packet, which is sent as the ICMP payload.
                 */
                source = reinterpret_cast<ipAddress *>(const_cast<u_char *>(data + 58));
                RtlIpv4AddressToStringA(&(reinterpret_cast<inAddress *>(source))->v4,
                                        sourceAddress);

                if (strncmp(sourceAddress, destinationAddress,
                            INET_ADDRSTRLEN) == 0)
                {
                    probe.recvTime = header->ts.tv_sec * 1e6 +
                                     header->ts.tv_usec;
                    probe.response = *icmpType == 3
                                     ? udpping::DESTINATION_UNREACHABLE
                                     : udpping::TTL_EXCEEDED;
                }
            }
            else
            {
                /*
                 * An unhandled ICMP packet has been captured. We need to check
                 * this and handle it if needed.
                 */
                probe.icmpType = *icmpType;
                probe.icmpCode = *icmpCode;
                probe.response = udpping::UNHANDLED_ICMP;
            }

            break;

        default:
            /*
             * This else-branch exists because of paranoia only. The WinPCAP
             * filter is set to capture certain ICMP and UDP packets only and
             * therefore should never end up here.
             */
            probe.icmpType = udpping::UNKNOWN;
            probe.sendTime = -1;
            probe.recvTime = -1;
        }

        return probe;
    }

    PingProbe handleIpv6Response(const u_char *data,
                                 const pcap_pkthdr *header,
                                 const sockaddr_any &destination,
                                 quint32 payloadSize)
    {
        char sourceAddress[INET6_ADDRSTRLEN] = "";
        char destinationAddress[INET6_ADDRSTRLEN] = "";
        ipAddress *source;
        quint8 *icmpType;
        quint8 *icmpCode;
        quint8 *ipProto;
        PingProbe probe = {0};
        QByteArray payload(0);

        if (data == NULL || header == NULL)
        {
            return probe;
        }

        ipProto = const_cast<quint8 *>(data + 20);<commit_msg>remove explicit variable initialization<commit_after>#include "udpping.h"
#include "../../log/logger.h"

#include <time.h>
#include <Windows.h>
#include <mswsock.h>
#include <Mstcpip.h>
#include <pcap.h>

#include <QCryptographicHash>
#include <QThread>

LOGGER("UdpPing");

namespace
{
    union ipAddress
    {
        quint32 v4;
        quint8 v6[16];
    };

    union inAddress
    {
        struct in_addr v4;
        struct in6_addr v6;
    };

    bool getAddress(const QString &address, sockaddr_any *addr)
    {
        struct addrinfo hints;
        struct addrinfo *rp = NULL, *result = NULL;

        memset(&hints, 0, sizeof(hints));
        hints.ai_family = AF_UNSPEC;

        if (getaddrinfo(address.toStdString().c_str(), NULL, &hints, &result))
        {
            return false;
        }

        for (rp = result; rp && rp->ai_family != AF_INET &&
             rp->ai_family != AF_INET6; rp = rp->ai_next)
        {
        }

        if (!rp)
        {
            rp = result;
        }

        memcpy(addr, rp->ai_addr, rp->ai_addrlen);

        freeaddrinfo(result);

        return true;
    }

    PingProbe handleIpv4Response(const u_char *data,
                                 const pcap_pkthdr *header,
                                 const sockaddr_any &destination,
                                 int payloadSize)
    {
        char sourceAddress[INET_ADDRSTRLEN] = "";
        char destinationAddress[INET_ADDRSTRLEN] = "";
        ipAddress *source;
        quint8 *icmpType;
        quint8 *icmpCode;
        quint8 *ipProto;
        PingProbe probe = {0};
        QByteArray payload;

        if (data == NULL || header == NULL)
        {
            return probe;
        }

        ipProto = const_cast<quint8 *>(data + 23);
        source = reinterpret_cast<ipAddress *>(const_cast<u_char *>(data + 26));
        icmpType = const_cast<quint8 *>(data + 34);
        icmpCode = const_cast<quint8 *>(data + 35);

        // source address of the response packet
        RtlIpv4AddressToStringA(&(reinterpret_cast<inAddress *>(source))->v4,
                                sourceAddress);
        // destination of request packet
        RtlIpv4AddressToStringA(&destination.sin.sin_addr, destinationAddress);

        switch (*ipProto)
        {
        case 17:
            // UDP request
            probe.type = udpping::UDP;
            probe.sendTime = header->ts.tv_sec * 1e6 + header->ts.tv_usec;
            payload = QByteArray::fromRawData(reinterpret_cast<char *>(const_cast<u_char *>(data + 42)),
                                              payloadSize);
            probe.hash = QCryptographicHash::hash(payload, QCryptographicHash::Sha256).toHex();
            break;

        case 1:
            // ICMP response
            probe.type = udpping::ICMP;
            getAddress(sourceAddress, &probe.source);

            if ((*icmpType == 3 && *icmpCode == 3) ||
                (*icmpType == 11 && *icmpCode == 0))
            {
                payload = QByteArray::fromRawData(reinterpret_cast<char *>(const_cast<u_char *>(data + 70)),
                                                  payloadSize);
                probe.hash = QCryptographicHash::hash(payload, QCryptographicHash::Sha256).toHex();
                /*
                 * 'source' points to the destination field of the original
                 * packet, which is sent as the ICMP payload.
                 */
                source = reinterpret_cast<ipAddress *>(const_cast<u_char *>(data + 58));
                RtlIpv4AddressToStringA(&(reinterpret_cast<inAddress *>(source))->v4,
                                        sourceAddress);

                if (strncmp(sourceAddress, destinationAddress,
                            INET_ADDRSTRLEN) == 0)
                {
                    probe.recvTime = header->ts.tv_sec * 1e6 +
                                     header->ts.tv_usec;
                    probe.response = *icmpType == 3
                                     ? udpping::DESTINATION_UNREACHABLE
                                     : udpping::TTL_EXCEEDED;
                }
            }
            else
            {
                /*
                 * An unhandled ICMP packet has been captured. We need to check
                 * this and handle it if needed.
                 */
                probe.icmpType = *icmpType;
                probe.icmpCode = *icmpCode;
                probe.response = udpping::UNHANDLED_ICMP;
            }

            break;

        default:
            /*
             * This else-branch exists because of paranoia only. The WinPCAP
             * filter is set to capture certain ICMP and UDP packets only and
             * therefore should never end up here.
             */
            probe.icmpType = udpping::UNKNOWN;
            probe.sendTime = -1;
            probe.recvTime = -1;
        }

        return probe;
    }

    PingProbe handleIpv6Response(const u_char *data,
                                 const pcap_pkthdr *header,
                                 const sockaddr_any &destination,
                                 quint32 payloadSize)
    {
        char sourceAddress[INET6_ADDRSTRLEN] = "";
        char destinationAddress[INET6_ADDRSTRLEN] = "";
        ipAddress *source;
        quint8 *icmpType;
        quint8 *icmpCode;
        quint8 *ipProto;
        PingProbe probe = {0};
        QByteArray payload;

        if (data == NULL || header == NULL)
        {
            return probe;
        }

        ipProto = const_cast<quint8 *>(data + 20);
{'fraction_non_alphanumeric': 0.09980254891401903, 'fraction_numerical': 0.013283073056901813, 'mean_word_length': 1.443640350877193, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6534371', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3087, 'n_words': 676}