Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Dherse/TRTP2
Browse files Browse the repository at this point in the history
  • Loading branch information
skjdbg committed Oct 30, 2019
2 parents 4e13ad3 + f2bd41c commit 96d9f47
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 85 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ BIN := $(wildcard $(BIN_DIR)/*)
LDFLAGS = -lpthread -lrt
FLAGS = -Werror -std=$(VERSION)
RELEASE_FLAGS = -O3
DEBUG_FLAGS = -O0 -ggdb -DDEBUG=true
DEBUG_FLAGS = -O0 -ggdb -DDEBUG

# does not need verification
.PHONY: clean report stat install_tectonic
Expand All @@ -65,7 +65,7 @@ release: build

# run
run:
$(OUT) -o $(BIN_DIR)/%d -n 12 -N 4 -W 124 -w 31 :: 64536
$(OUT) -o $(BIN_DIR)/%d -n 12 -N 4 -W 124 -w 31 -m 1 :: 64536

# Build and run tests
test: FLAGS += $(DEBUG_FLAGS)
Expand Down Expand Up @@ -105,17 +105,17 @@ report:
valgrind: FLAGS += $(DEBUG_FLAGS)
valgrind: build
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes --verbose \
$(OUT) -n 1 -N 1 -o $(BIN_DIR)/%d :: 64536 2> $(BIN_DIR)/valgrind.txt
$(OUT) -s -o $(BIN_DIR)/%d :: 64536 2> $(BIN_DIR)/valgrind.txt

helgrind: FLAGS += $(DEBUG_FLAGS)
helgrind: build
valgrind --tool=helgrind \
$(OUT) -s -n 4 -N 2 -o $(BIN_DIR)/%d :: 64536 2> $(BIN_DIR)/helgrind.txt
$(OUT) -s -n 12 -N 4 -o $(BIN_DIR)/%d :: 64536 2> $(BIN_DIR)/helgrind.txt

memcheck: FLAGS += $(DEBUG_FLAGS)
memcheck: build
valgrind --tool=memcheck --track-origins=yes \
$(OUT) -n 1 -N 1 -o $(BIN_DIR)/%d :: 64536 2> $(BIN_DIR)/memcheck.txt
$(OUT) -n 12 -N 4 -o $(BIN_DIR)/%d :: 64536 2> $(BIN_DIR)/memcheck.txt

callgrind: FLAGS += -O3 -ggdb
callgrind: build
Expand Down
2 changes: 1 addition & 1 deletion headers/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef struct config_receiver {
char *format;

/** Maximum number of concurrent connections */
uint8_t max_connections;
uint32_t max_connections;

/** The input IP */
struct addrinfo *addr_info;
Expand Down
3 changes: 3 additions & 0 deletions headers/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ typedef enum Errors {
/** The length of an incoming packet is too long (corruption) */
PACKET_TOO_LONG,

/** The length encoded in a packet is higher than 512 */
PACKET_INCORRECT_LENGTH,

/** The packet type is wrong (i.e packet->type = IGNORE) */
TYPE_IS_WRONG,

Expand Down
5 changes: 4 additions & 1 deletion headers/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@
/**
* Defines boolean semantic for ease of use
*/
#define true 1
#define true -1
#define false 0
typedef int bool;

/** Minimum size of a packet (in bytes) */
#define MIN_PACKET_SIZE 11

/** Maximum size of a packet (in bytes) */
#define MAX_PACKET_SIZE 528

Expand Down
6 changes: 0 additions & 6 deletions headers/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

#define PACKET_H

#ifndef true
#define true 1
#define false 0
typedef int bool;
#endif

#include "global.h"
#include "errors.h"
#include "lookup.h"
Expand Down
3 changes: 2 additions & 1 deletion src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ int parse_streams_file(config_rcv_t *config) {
config->stream_count = i;

if (i <= 0) {
fclose(file);
fclose(file);
LOG("CLI", "At least one stream must be defined\n");
return -1;
}
Expand All @@ -511,6 +511,7 @@ int parse_streams_file(config_rcv_t *config) {
}
}

fclose(file);
return 0;
}

Expand Down
32 changes: 17 additions & 15 deletions src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@


#define LOG_ERROR(message, id, port, ip_as_str, arg...) \
LOG("HANDLER][ERROR", "Error: " message " on client #%d [%s]:%u", id, ntohs(port), ip_as_str, ##arg)
LOG("HANDLER][ERROR", "Error on client #%d [%s]:%u: " message "\n", id, ip_as_str, ntohs(port), ##arg)


void print_unpack_error(uint32_t id, uint16_t port, char *ip_as_str) {
switch(errno) {
case TYPE_IS_WRONG:
LOG_ERROR("Type is wrong\n", id, port, ip_as_str);
LOG_ERROR("Type is wrong", id, port, ip_as_str);
break;
case NON_DATA_TRUNCATED:
LOG_ERROR("Non-PData packet truncated\n", id, port, ip_as_str);
LOG_ERROR("Non-PData packet truncated", id, port, ip_as_str);
break;
case CRC_VALIDATION_FAILED:
LOG_ERROR("Header could not be validated\n", id, port, ip_as_str);
LOG_ERROR("Header could not be validated", id, port, ip_as_str);
break;
case PAYLOAD_VALIDATION_FAILED:
LOG_ERROR("Payload could not be validated\n", id, port, ip_as_str);
LOG_ERROR("Payload could not be validated", id, port, ip_as_str);
break;
case PACKET_TOO_SHORT:
LOG_ERROR("Packet has incorrect size (too short)\n", id, port, ip_as_str);
LOG_ERROR("Packet has incorrect size (too short)", id, port, ip_as_str);
break;
case PACKET_TOO_LONG:
LOG_ERROR("Packet has incorrect size (too long)\n", id, port, ip_as_str);
LOG_ERROR("Packet has incorrect size (too long)", id, port, ip_as_str);
break;
case PAYLOAD_TOO_LONG:
LOG_ERROR("Payload has incorrect size (too long)\n", id, port, ip_as_str);
LOG_ERROR("Payload has incorrect size (too long)", id, port, ip_as_str);
break;
default:
LOG_ERROR("Unknown packet error on client #%d [%s]:%u\n", id, port, ip_as_str);
LOG_ERROR("Unknown packet error (errno = %d)", id, port, ip_as_str, errno);
break;
}
}
Expand Down Expand Up @@ -78,7 +78,8 @@ inline __attribute__((always_inline)) void hd_run_once(

hd_req_t *req = (hd_req_t *) node_rx->content;
if (req != NULL) {
if (req->stop) {
if (req->stop == true) {
LOG("HD", "Received STOP (%d)\n", cfg->id);
free(*decoded);
deallocate_node(node_rx);

Expand Down Expand Up @@ -306,6 +307,8 @@ inline __attribute__((always_inline)) void hd_run_once(
}

enqueue_or_free(cfg->tx, node_rx);
} else {
free(node_rx);
}
}

Expand Down Expand Up @@ -380,7 +383,7 @@ void *handle_thread(void *config) {

/**
* Refer to headers/receiver.h
*/
*/
s_node_t *pop_and_check_req(stream_t *stream, void *(*allocator)()) {
s_node_t *node = stream_pop(stream, false);

Expand All @@ -398,10 +401,9 @@ s_node_t *pop_and_check_req(stream_t *stream, void *(*allocator)()) {
* Refer to headers/handler.h
*/
inline void enqueue_or_free(stream_t *stream, s_node_t *node) {
if (!stream_enqueue(stream, node, false)) {
//TODO : replace with getter
free(node->content);
free(node);
if (stream_enqueue(stream, node, false) == false) {
TRACE("Failed to enqueue, freeing\n");
deallocate_node(node);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,11 @@ int main(int argc, char *argv[]) {
}
}

if (decoded != NULL) {
dealloc_packet(*decoded);
free(decoded);
}

} else {
while (true) {
pthread_mutex_lock(&stop_mutex);
Expand Down
98 changes: 52 additions & 46 deletions src/packet.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../headers/packet.h"

#define CRC32H(old, value, length) crc32_16bytes_prefetch(value, length, old, 11)
#define CRC32P(old, value, length) crc32_16bytes_prefetch(value, length, old, 512)
#define CRC32H(old, value, length) crc32_16bytes(value, length, old)
#define CRC32P(old, value, length) crc32_16bytes_prefetch(value, length, old, MAX_PAYLOAD_SIZE)

GETSET_IMPL(packet_t, ptype_t, type);

Expand Down Expand Up @@ -102,102 +102,108 @@ int dealloc_packet(packet_t* packet) {
* Refer to headers/packet.h
*/
int unpack(uint8_t *packet, int length, packet_t *out) {
uint8_t *raw = packet;
uint8_t *buffer = packet;
int length_rest = length;

if (--length <= 0) {
if ((length_rest -= 1) <= 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

uint8_t *header = packet++;

uint8_t ttrwin = *header;
out->type = (ttrwin & 0b11000000) >> 6;
out->truncated = (ttrwin & 0b00100000) >> 5;
out->window = ttrwin & 0b00011111;
uint8_t *header_pointer = buffer;
uint8_t header = *buffer++;
out->type = (header & 0b11000000) >> 6;
out->truncated = (header & 0b00100000) >> 5;
out->window = (header & 0b00011111);

if (--length <= 0) {
if ((length_rest -= 1) <= 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

uint8_t size = *packet++;
out->long_length = (size & 0b10000000) >> 7;
if (out->long_length) {
if (--length <= 0) {
uint8_t size = *buffer++;
uint8_t is_long = (size & 0b10000000) >> 7;
if (is_long) {
if ((length_rest -= 1) <= 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

out->length = (size & 0b01111111) | (*packet++ << 8);
out->length = ntohs(out->length);
out->long_length = is_long;
out->length = ntohs((size & 0b01111111) | (*buffer++ << 8));
} else {
out->length = size & 0b01111111;
out->length = (size & 0b01111111);
out->long_length = false;
}

if (--length <= 0) {
if ((length_rest -= 1) <= 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

out->seqnum = *packet++;
out->seqnum = *buffer++;

if ((length -= 4) <= 0) {
if ((length_rest -= 4) <= 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

out->timestamp = *packet++ | (*packet++ << 8) | (*packet++ << 16) | (*packet++ << 24);
out->timestamp = ntohl(out->timestamp);
out->timestamp = ntohl(*buffer++ | (*buffer++ << 8) | (*buffer++ << 16) | (*buffer++ << 24));

if ((length_rest -= 4) < 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

out->crc1 = ntohl(*buffer++ | (*buffer++ << 8) | (*buffer++ << 16) | (*buffer++ << 24));

size_t len = 7 + is_long;
*header_pointer &= 0b11011111;

uint32_t crc = CRC32H(0, (void*) packet, len);
if (out->crc1 != crc) {
errno = CRC_VALIDATION_FAILED;

return -1;
}

if ((length -= 4) < 0) {
if (length_rest == 0 && out->type == DATA && !out->truncated && out->length != 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

out->crc1 = *packet++ | (*packet++ << 8) | (*packet++ << 16) | (*packet++ << 24);
out->crc1 = ntohl(out->crc1);
if (out->length > MAX_PAYLOAD_SIZE) {
errno = PACKET_INCORRECT_LENGTH;
return -1;
}

if (out->type == DATA && !out->truncated && out->length != 0) {
if ((length -= out->length) <= 0) {
if ((length_rest -= out->length) < 4) {
errno = PACKET_TOO_SHORT;
return -1;
}

if (&out->payload != memcpy(&out->payload, packet, out->length)) {
if (&out->payload != memcpy(&out->payload, buffer, out->length)) {
errno = FAILED_TO_COPY;
return -1;
}

packet += out->length;
buffer += out->length;

if ((length -= 4) < 0) {
if ((length_rest -= 4) < 0) {
errno = PACKET_TOO_SHORT;
return -1;
}

out->crc2 = *packet++ | (*packet++ << 8) | (*packet++ << 16) | (*packet++ << 24);
out->crc2 = ntohl(out->crc2);
out->crc2 = ntohl(*buffer++ | (*buffer++ << 8) | (*buffer++ << 16) | (*buffer++ << 24));
}

if (length > 0 && !out->truncated) {
if (length_rest > 0) {
LOG("PKT", "%d\n", length_rest);
errno = PACKET_TOO_LONG;
return -1;
}

size_t len = 7;
if (out->long_length) {
len += 1;
}

*header = *header & 0b11011111;
uint32_t crc = CRC32H(0, (void*) raw, len);
if (out->crc1 != crc) {
errno = CRC_VALIDATION_FAILED;

return -1;
}

if (out->type == IGNORE) {
errno = TYPE_IS_WRONG;

Expand Down
Loading

0 comments on commit 96d9f47

Please sign in to comment.