<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "filestream.h"
#include "netconfig.h"
#include "protocol.h"
#include "util.h"

#define SOCK_BUFFER_SIZE 2000
#define ADDR_SIZE        200
#define TIMEOUT          500
#define MAXIMUM_TIMEOUTS 5
#define MAXIMUM_RESETS   5
#define WINDOW_SIZE      10

/* Stat tracking */
int stat_sent_total_bytes             = 0;
int stat_sent_unique_bytes            = 0;
int stat_sent_dat_packets             = 0;
int stat_sent_dat_unique_packets      = 0;
int stat_sent_syn_packets             = 0;
int stat_sent_ack_packets             = 0;
int stat_sent_fin_packets             = 0;
int stat_sent_rst_packets             = 0;
int stat_recieved_total_bytes         = 0;
int stat_recieved_unique_bytes        = 0;
int stat_recieved_dat_packets         = 0;
int stat_recieved_dat_unique_packets  = 0;
int stat_recieved_syn_packets         = 0;
int stat_recieved_ack_packets         = 0;
int stat_recieved_fin_packets         = 0;
int stat_recieved_rst_packets         = 0;
int stat_start_time                   = 0;
int stat_end_time                     = 0;

/* listen_rdp variables */

enum { event_recieved, event_timeout, event_bad_packet };

char recieve_buffer[SOCK_BUFFER_SIZE];
char send_buffer[SOCK_BUFFER_SIZE];

int source_socket;
struct sockaddr_in source_address;
int destination_socket;
struct sockaddr_in destination_address;

unint16_t sequence_number;

char source_ip[ADDR_SIZE];
char source_port[ADDR_SIZE];
char destination_ip[ADDR_SIZE];
char destination_port[ADDR_SIZE];

void open_source_socket() {
    rdp_log("source_ip: %s\nsource_port: %s\n", source_ip, source_port);
    source_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (source_socket == -1) {
        rdp_exit(EXIT_FAILURE, "Unable to create sender socket");
    }
    // Let go of socket on exit/crash
    int option = 1;
    setsockopt(
        source_socket,
        SOL_SOCKET,
        SO_REUSEADDR,
        (const void*) &option,
        sizeof(int)
    );
    memset(&source_address, 0, sizeof(source_address));
    source_address.sin_family      = AF_INET;
    source_address.sin_addr.s_addr = inet_addr(source_ip);
    source_address.sin_port        = htons(atoi(source_port));
    if (-1 == bind(
        source_socket,
        (struct sockaddr*) &source_address,
        sizeof(source_address)
    )) {
        close(source_socket);
        rdp_exit(EXIT_FAILURE, "Failed to bind socket: %s\n", strerror(errno));
    }
}

void open_destination_socket() {
    rdp_log("destination_ip: %s\ndestination_port %s\n", destination_ip, destination_port);
    destination_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (destination_socket == -1) {
        rdp_exit(EXIT_FAILURE, "Unable to create reciever socket");
    }
    memset(&destination_address, 0, sizeof(destination_address));
    destination_address.sin_family      = AF_INET;
    destination_address.sin_addr.s_addr = inet_addr(destination_ip);
    destination_address.sin_port        = htons(atoi(destination_port));
}

int listen_rdp(int timeout_milli) {

    int select_result;
    ssize_t recsize;
    struct timeval timeout;
    fd_set read_fds;
    socklen_t socket_length = sizeof(destination_address);

    rdp_log("listening...");

    FD_ZERO(&read_fds);
    FD_SET(source_socket, &read_fds);

    timeout.tv_sec  = 0;
    timeout.tv_usec = timeout_milli * 1000;
    // Wait on select
    select_result = select(source_socket + 1, &read_fds, NULL, NULL, &timeout);

    // Check result
    switch(select_result) {
        case -1:
            close(source_socket);
            rdp_exit(EXIT_FAILURE, "Error in select: %s\n", strerror(errno));
        case 0: return event_timeout;
        default: break;
    }
    // Recieve and parse the packet
    rdp_zero(recieve_buffer, rdp_MAX_PACKET_SIZE + 1);
    recsize = recvfrom(
        source_socket,
        (void*) recieve_buffer,
        SOCK_BUFFER_SIZE,
        0,
        (struct sockaddr*) &destination_address,
        &socket_length
    );

    if(!rdp_parse(recieve_buffer)) {
        return event_bad_packet;
    }

    // We don't yet know our destination so read it in
    if(destination_ip[0] == 0 || destination_port[0] == 0) {
        strcpy(destination_ip, inet_ntoa(destination_address.sin_addr));
        sprintf(destination_port, "%d", ntohs(destination_address.sin_port));
        if (destination_ip == NULL) {
            close(source_socket);
            rdp_exit(EXIT_FAILURE, "Failed to read destination IP:port from recieved packet.");
        }
        open_destination_socket();
    }

    // Log the packet
    rdp_log_packet(
        "r",
        destination_ip,
        destination_port,
        source_ip,
        source_port,
        rdp_flag_names[rdp_flags()],
        rdp_seq_ack_number(),
        rdp_payload_size() | rdp_window_size()
    );
    rdp_log_hex(recieve_buffer, rdp_size());

    // Compute statistics
    stat_recieved_syn_packets += !!(rdp_flags() & rdp_SYN);
    stat_recieved_fin_packets += !!(rdp_flags() & rdp_FIN);<commit_msg>autogenerated commit<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "filestream.h"
#include "netconfig.h"
#include "protocol.h"
#include "util.h"

#define SOCK_BUFFER_SIZE 2000
#define ADDR_SIZE        200
#define TIMEOUT          500
#define MAXIMUM_TIMEOUTS 5
#define MAXIMUM_RESETS   5
#define WINDOW_SIZE      10

/* Stat tracking */
int stat_sent_total_bytes             = 0;
int stat_sent_unique_bytes            = 0;
int stat_sent_dat_packets             = 0;
int stat_sent_dat_unique_packets      = 0;
int stat_sent_syn_packets             = 0;
int stat_sent_ack_packets             = 0;
int stat_sent_fin_packets             = 0;
int stat_sent_rst_packets             = 0;
int stat_recieved_total_bytes         = 0;
int stat_recieved_unique_bytes        = 0;
int stat_recieved_dat_packets         = 0;
int stat_recieved_dat_unique_packets  = 0;
int stat_recieved_syn_packets         = 0;
int stat_recieved_ack_packets         = 0;
int stat_recieved_fin_packets         = 0;
int stat_recieved_rst_packets         = 0;
int stat_start_time                   = 0;
int stat_end_time                     = 0;

/* listen_rdp variables */

enum { event_recieved, event_timeout, event_bad_packet };

char recieve_buffer[SOCK_BUFFER_SIZE];
char send_buffer[SOCK_BUFFER_SIZE];

int source_socket;
struct sockaddr_in source_address;
int destination_socket;
struct sockaddr_in destination_address;

unint16_t sequence_number;

char source_ip[ADDR_SIZE];
char source_port[ADDR_SIZE];
char destination_ip[ADDR_SIZE];
char destination_port[ADDR_SIZE];

void open_source_socket() {
    rdp_log("source_ip: %s\nsource_port: %s\n", source_ip, source_port);
    source_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (source_socket == -1) {
        rdp_exit(EXIT_FAILURE, "Unable to create sender socket");
    }
    // Let go of socket on exit/crash
    int option = 1;
    setsockopt(
        source_socket,
        SOL_SOCKET,
        SO_REUSEADDR,
        (const void*) &option,
        sizeof(int)
    );
    memset(&source_address, 0, sizeof(source_address));
    source_address.sin_family      = AF_INET;
    source_address.sin_addr.s_addr = inet_addr(source_ip);
    source_address.sin_port        = htons(atoi(source_port));
    if (-1 == bind(
        source_socket,
        (struct sockaddr*) &source_address,
        sizeof(source_address)
    )) {
        close(source_socket);
        rdp_exit(EXIT_FAILURE, "Failed to bind socket: %s\n", strerror(errno));
    }
}

void open_destination_socket() {
    rdp_log("destination_ip: %s\ndestination_port %s\n", destination_ip, destination_port);
    destination_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (destination_socket == -1) {
        rdp_exit(EXIT_FAILURE, "Unable to create reciever socket");
    }
    memset(&destination_address, 0, sizeof(destination_address));
    destination_address.sin_family      = AF_INET;
    destination_address.sin_addr.s_addr = inet_addr(destination_ip);
    destination_address.sin_port        = htons(atoi(destination_port));
}

int listen_rdp(int timeout_milli) {

    int select_result;
    ssize_t recsize;
    struct timeval timeout;
    fd_set read_fds;
    socklen_t socket_length = sizeof(destination_address);

    rdp_log("listening...");

    FD_ZERO(&read_fds);
    FD_SET(source_socket, &read_fds);

    timeout.tv_sec  = 0;
    timeout.tv_usec = timeout_milli * 1000;
    // Wait on select
    select_result = select(source_socket + 1, &read_fds, NULL, NULL, &timeout);

    // Check result
    switch(select_result) {
        case -1:
            close(source_socket);
            rdp_exit(EXIT_FAILURE, "Error in select: %s\n", strerror(errno));
        case 0: return event_timeout;
        default: break;
    }
    // Recieve and parse the packet
    rdp_zero(recieve_buffer, rdp_MAX_PACKET_SIZE + 1);
    recsize = recvfrom(
        source_socket,
        (void*) recieve_buffer,
        SOCK_BUFFER_SIZE,
        0,
        (struct sockaddr*) &destination_address,
        &socket_length
    );

    if(!rdp_parse(recieve_buffer)) {
        rdp_log("Bad packet!");
        return event_bad_packet;
    }

    // We don't yet know our destination so read it in
    if(destination_ip[0] == 0 || destination_port[0] == 0) {
        strcpy(destination_ip, inet_ntoa(destination_address.sin_addr));
        sprintf(destination_port, "%d", ntohs(destination_address.sin_port));
        if (destination_ip == NULL) {
            close(source_socket);
            rdp_exit(EXIT_FAILURE, "Failed to read destination IP:port from recieved packet.");
        }
        open_destination_socket();
    }

    // Log the packet
    rdp_log_packet(
        "r",
        destination_ip,
        destination_port,
        source_ip,
        source_port,
        rdp_flag_names[rdp_flags()],
        rdp_seq_ack_number(),
        rdp_payload_size() | rdp_window_size()
    );
    rdp_log_hex(recieve_buffer, rdp_size());

    // Compute statistics
    stat_recieved_syn_packets += !!(rdp_flags() & rdp_SYN);
    stat_recieved_fin_packets += !!(rdp_flags() & rdp_FIN);
{'fraction_non_alphanumeric': 0.10018162699550712, 'fraction_numerical': 0.010324060797246917, 'mean_word_length': 3.039382239382239, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '865392', 'n_tokens_mistral': 3655, 'n_tokens_neox': 3547, 'n_words': 704}