<commit_before>#include <thread>
#include <vector>

#include "a_devdiag.h"
#include "netstructures.h"
#include "tracepacket.h"

void a_devdiag() {
	pcap_if_t* alldevs;
	char errbuf[PCAP_ERRBUF_SIZE];

	if (pcap_findalldevs(&alldevs, errbuf) == -1) {
		printf("Could not find all network devices: %s\n", errbuf);
		exit(1);
	}

	int i = 0;
	pcap_if_t* d;
	for (d = alldevs; d; d = d->next) {
		printf("%d. %s", ++i, d->name);
		if (d->description) {
			printf(" - %s\n\n", d->description);
		}
	}

	int inum;
	printf("Enter the interface number (1-%d): ", i);
	scanf_s("%d", &inum);
	if (inum < 0 || inum > i) {
		printf("Number out of range!\n");
		exit(1);
	}

	for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++);

	pcap_t* adhandle;
	if ((adhandle = pcap_open_live(d->name, 65536, 1, 500, errbuf)) == NULL) {
		printf("Unable to open the network adapter! %s is not supported by WinPcap\n", d->name);
		pcap_freealldevs(alldevs);
		exit(1);
	}

	if (pcap_datalink(adhandle) != DLT_EN10MB) {
		printf("Not using an ethernet device: output may not always be correct.\n");
	}

	u_int netmask;
	if (d->addresses != NULL) {
		netmask = ((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
	}
	else {
		netmask = 0xffffff;
	}

	struct bpf_program fcode;
	char a_packetfilter[] = "ip and udp";
	if (pcap_compile(adhandle, &fcode, a_packetfilter, 1, netmask) < 0) {
		printf("Unable to compile packet filter!\n");
		pcap_freealldevs(alldevs);
		exit(1);
	}

	if (pcap_setfilter(adhandle, &fcode) < 0) {
		printf("Unable to set packet filter!\n");
		pcap_freealldevs(alldevs);
		exit(1);
	}

	pcap_freealldevs(alldevs);
	printf("\nStarting active diagnostics...\n\n");

	std::thread concurrent(cprocess);

	printf("Started PCAP processing...\n\n");
	currentpacket.count = 0;
	pcap_loop(adhandle, INFINITE, a_packethandler, NULL);
}

std::vector<tracepacket> pbuffer;

void cprocess() {
	printf("Started concurrent processing - interval: %d\n\n", TRACE_PRINT_DELAY);
	bool activity_flag = false;
	while (true) {
		if (!pbuffer.empty()) {
			activity_flag = true;
			system("cls");

			tracepacket packet = pbuffer.front();
			printf("Packet %i:\n", packet.count);
			printf("\t%s\n", packet.directionstring);
			printf("\tTimestamp: %s\n", packet.timestr);
			printf("\tDatagram length: %f\n", packet.d_len);
			printf("\tPayload length: %f\n", packet.size_payload);
			printf("\tTotal length: %f\n", packet.t_len);
			printf("\tPayload:\n");
			printf("\t-----------------\n");
			printf("\t%s\n", packet.payload);
			printf("\t-----------------\n");
			printf("\tPackets Buffered: %i\n", pbuffer.size());
			pbuffer.erase(pbuffer.begin());
			std::this_thread::sleep_for(std::chrono::milliseconds(TRACE_PRINT_DELAY));
		}
		else {
			if (activity_flag) {
				activity_flag = false;
				system("cls");

				printf("No more packets! PCAP probably dropped some packets or device lost connection.\n");<commit_msg>Working on binary data check<commit_after>#include <thread>
#include <vector>

#include "a_devdiag.h"
#include "netstructures.h"
#include "tracepacket.h"

void a_devdiag() {
	pcap_if_t* alldevs;
	char errbuf[PCAP_ERRBUF_SIZE];

	if (pcap_findalldevs(&alldevs, errbuf) == -1) {
		printf("Could not find all network devices: %s\n", errbuf);
		exit(1);
	}

	int i = 0;
	pcap_if_t* d;
	for (d = alldevs; d; d = d->next) {
		printf("%d. %s", ++i, d->name);
		if (d->description) {
			printf(" - %s\n\n", d->description);
		}
	}

	int inum;
	printf("Enter the interface number (1-%d): ", i);
	scanf_s("%d", &inum);
	if (inum < 0 || inum > i) {
		printf("Number out of range!\n");
		exit(1);
	}

	for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++);

	pcap_t* adhandle;
	if ((adhandle = pcap_open_live(d->name, 65536, 1, 500, errbuf)) == NULL) {
		printf("Unable to open the network adapter! %s is not supported by WinPcap\n", d->name);
		pcap_freealldevs(alldevs);
		exit(1);
	}

	if (pcap_datalink(adhandle) != DLT_EN10MB) {
		printf("Not using an ethernet device: output may not always be correct.\n");
	}

	u_int netmask;
	if (d->addresses != NULL) {
		netmask = ((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
	}
	else {
		netmask = 0xffffff;
	}

	struct bpf_program fcode;
	char a_packetfilter[] = "ip and udp";
	if (pcap_compile(adhandle, &fcode, a_packetfilter, 1, netmask) < 0) {
		printf("Unable to compile packet filter!\n");
		pcap_freealldevs(alldevs);
		exit(1);
	}

	if (pcap_setfilter(adhandle, &fcode) < 0) {
		printf("Unable to set packet filter!\n");
		pcap_freealldevs(alldevs);
		exit(1);
	}

	pcap_freealldevs(alldevs);
	printf("\nStarting active diagnostics...\n\n");

	std::thread concurrent(cprocess);

	printf("Started PCAP processing...\n\n");
	currentpacket.count = 0;
	pcap_loop(adhandle, INFINITE, a_packethandler, NULL);
}

std::vector<tracepacket> pbuffer;

bool strbinary(const u_char* p) {//WORK IN PROGRESS
	char dump[160] = {'\0'};
	sprintf_s(dump, 160, "%s", p);
	for (int i = 0; i < sizeof(dump); i++) {
		if (dump[i] < 0) {
			return true;
		}
	}
	return false;
}

void cprocess() {
	printf("Started concurrent processing - interval: %d\n\n", TRACE_PRINT_DELAY);
	bool activity_flag = false;
	while (true) {
		if (!pbuffer.empty()) {
			activity_flag = true;
			system("cls");

			tracepacket packet = pbuffer.front();
			printf("Packet %i:\n", packet.count);
			printf("\t%s\n", packet.directionstring);
			printf("\tTimestamp: %s\n", packet.timestr);
			printf("\tDatagram length: %f\n", packet.d_len);
			printf("\tPayload length: %f\n", packet.size_payload);
			printf("\tTotal length: %f\n", packet.t_len);
			printf("\tPayload:\n");
			printf("\t-----------------\n");
			if (strbinary(packet.payload)) {
				printf("\t%s\n", "(BINARY DATA)");
			}
			else {
				printf("\t%s\n", packet.payload);
			}
			printf("\t-----------------\n");
			printf("\tPackets Buffered: %i\n", pbuffer.size());
			pbuffer.erase(pbuffer.begin());
			std::this_thread::sleep_for(std::chrono::milliseconds(TRACE_PRINT_DELAY));
		}
		else {
			if (activity_flag) {
				activity_flag = false;
				system("cls");

				printf("No more packets! PCAP probably dropped some packets or device lost connection.\n");
{'fraction_non_alphanumeric': 0.19005989962765096, 'fraction_numerical': 0.010199125789218067, 'mean_word_length': 3.8876582278481013, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '881939', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2405, 'n_words': 539}