<commit_before>#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include "divert.h"
#include "tcpcryptd.h"

#define MAC_SIZE 14

static int	  _s;
static divert_cb _cb;
static unsigned char _mac[6];

struct packet {
	unsigned char p_buf[2048];
	int	      p_len;
	struct packet *p_next;
} _outbound;

struct arp {
	unsigned int  a_ip;
	unsigned char a_mac[6];
	struct arp    *a_next;
} _arp;

int divert_open(int port, divert_cb cb)
{
	char *m;
	int i;
	int mac[6];

	m = driver_param(0);
	if (!m)
		errx(1 ,"Supply interface's MAC address"
		        " e.g., -x 11:22:33:44:55:66");

	if (sscanf(m, "%x:%x:%x:%x:%x:%x",
		   &mac[0], &mac[1], &mac[2],
		   &mac[3], &mac[4], &mac[5]) != 6)
		errx(1, "Can't parse MAC %s\n", m);

	for (i = 0; i < 6; i++)
		_mac[i] = (unsigned char) mac[i];

	xprintf(XP_ALWAYS, "Mac %x:%x:%x:%x:%x:%x\n",
	       _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]);

	if ((_s = open("\\\\.\\PassThru", O_RDWR)) == -1)
		err(1, "open()");

	_cb = cb;

	return _s;
}

void divert_close(void)
{
	close(_s);
}

static void arp_cache(unsigned char *buf, int in)
{
	struct ip *iph = (struct ip*) &buf[MAC_SIZE];
	unsigned char *mac = buf;
	struct in_addr *i = &iph->ip_dst;
	struct arp *a = _arp.a_next;
	int num = 1;

	if (in) {
		mac = &buf[6];
		i   = &iph->ip_src;
	}

	while (a) {
		if (a->a_ip == i->s_addr) {
			memcpy(a->a_mac, mac, 6);
			return;
		}

		a = a->a_next;
		num++;
	}

	a = malloc(sizeof(*a));
	if (!a)
		err(1, "malloc()");

	memset(a, 0, sizeof(*a));

	a->a_ip = i->s_addr;
	memcpy(a->a_mac, mac, 6);

	a->a_next   = _arp.a_next;
	_arp.a_next = a;

	xprintf(XP_DEBUG,
		"Added ARP entry for %s [%x:%x:%x:%x:%x:%x] table size %d\n",
		inet_ntoa(*i),
		mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
		num);
}

static void do_divert_next_packet(unsigned char *buf, int rc)
{
	int verdict;
	int flags = 0;
	struct ip *iph = (struct ip*) &buf[MAC_SIZE];
	int len;

	if (rc < MAC_SIZE)
		errx(1, "short read %d", rc);

	if (memcmp(&buf[6], _mac, 6) != 0)
		flags |= DF_IN;

	arp_cache(buf, flags & DF_IN);

	// XXX ethernet padding on short packets?  (46 byte minimum)
	len = rc - MAC_SIZE;<commit_msg>pad hex in printf fmt strings<commit_after>#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include "divert.h"
#include "tcpcryptd.h"

#define MAC_SIZE 14

static int	  _s;
static divert_cb _cb;
static unsigned char _mac[6];

struct packet {
	unsigned char p_buf[2048];
	int	      p_len;
	struct packet *p_next;
} _outbound;

struct arp {
	unsigned int  a_ip;
	unsigned char a_mac[6];
	struct arp    *a_next;
} _arp;

int divert_open(int port, divert_cb cb)
{
	char *m;
	int i;
	int mac[6];

	m = driver_param(0);
	if (!m)
		errx(1 ,"Supply interface's MAC address"
		        " e.g., -x 11:22:33:44:55:66");

	if (sscanf(m, "%x:%x:%x:%x:%x:%x",
		   &mac[0], &mac[1], &mac[2],
		   &mac[3], &mac[4], &mac[5]) != 6)
		errx(1, "Can't parse MAC %s\n", m);

	for (i = 0; i < 6; i++)
		_mac[i] = (unsigned char) mac[i];

	xprintf(XP_ALWAYS, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
	       _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]);

	if ((_s = open("\\\\.\\PassThru", O_RDWR)) == -1)
		err(1, "open()");

	_cb = cb;

	return _s;
}

void divert_close(void)
{
	close(_s);
}

static void arp_cache(unsigned char *buf, int in)
{
	struct ip *iph = (struct ip*) &buf[MAC_SIZE];
	unsigned char *mac = buf;
	struct in_addr *i = &iph->ip_dst;
	struct arp *a = _arp.a_next;
	int num = 1;

	if (in) {
		mac = &buf[6];
		i   = &iph->ip_src;
	}

	while (a) {
		if (a->a_ip == i->s_addr) {
			memcpy(a->a_mac, mac, 6);
			return;
		}

		a = a->a_next;
		num++;
	}

	a = malloc(sizeof(*a));
	if (!a)
		err(1, "malloc()");

	memset(a, 0, sizeof(*a));

	a->a_ip = i->s_addr;
	memcpy(a->a_mac, mac, 6);

	a->a_next   = _arp.a_next;
	_arp.a_next = a;

	xprintf(XP_DEBUG,
		"Added ARP entry for %s [%02x:%02x:%02x:%02x:%02x:%02x]"
                " table size %d\n",
		inet_ntoa(*i),
		mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
		num);
}

static void do_divert_next_packet(unsigned char *buf, int rc)
{
	int verdict;
	int flags = 0;
	struct ip *iph = (struct ip*) &buf[MAC_SIZE];
	int len;

	if (rc < MAC_SIZE)
		errx(1, "short read %d", rc);

	if (memcmp(&buf[6], _mac, 6) != 0)
		flags |= DF_IN;

	arp_cache(buf, flags & DF_IN);

	// XXX ethernet padding on short packets?  (46 byte minimum)
	len = rc - MAC_SIZE;
{'fraction_non_alphanumeric': 0.205138917902653, 'fraction_numerical': 0.030081470649676208, 'mean_word_length': 3.4873477038425493, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 47, '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': '4517078', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2199, 'n_words': 540}