Commit 69f35f8a authored by Andreas Schmidt's avatar Andreas Schmidt

Create copy method for packets.

parent ba191ccb
......@@ -4,6 +4,7 @@
#include <string.h>
#include <netinet/in.h>
#include <src/util/dbg.h>
#include <src/util/common.h>
#include "packet.h"
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
......@@ -20,19 +21,23 @@ void *decode_redundancy_header(void *buf_ptr, const void *payload_ptr);
void *decode_feedback_header(void *buf_ptr, const void *payload_ptr);
uint8_t PrrtPacket_type(PrrtPacket *packet_ptr) {
uint8_t PrrtPacket_type(PrrtPacket *packet_ptr)
{
return (uint8_t) ((packet_ptr->type_priority >> 4) & 0x0F);
}
uint8_t PrrtPacket_priority(PrrtPacket *packet_ptr) {
uint8_t PrrtPacket_priority(PrrtPacket *packet_ptr)
{
return (uint8_t) (packet_ptr->type_priority & 0x0F);
}
uint16_t PrrtPacket_size(PrrtPacket *packet_ptr) {
uint16_t PrrtPacket_size(PrrtPacket *packet_ptr)
{
return (uint16_t) (packet_ptr->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE);
}
int PrrtPacket_print(PrrtPacket *packet_ptr) {
int PrrtPacket_print(PrrtPacket *packet_ptr)
{
printf(" 0 1 2 3\n"
" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n"
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
......@@ -42,7 +47,7 @@ int PrrtPacket_print(PrrtPacket *packet_ptr) {
packet_ptr->seqno);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
if (type == PACKET_TYPE_DATA) {
if(type == PACKET_TYPE_DATA) {
PrrtPacketDataPayload *payload = packet_ptr->payload;
printf("| %61u |\n", payload->timestamp);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
......@@ -52,11 +57,11 @@ int PrrtPacket_print(PrrtPacket *packet_ptr) {
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %61s |\n", (char *) (packet_ptr->payload + PRRT_PACKET_DATA_HEADER_SIZE));
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
} else if (type == PACKET_TYPE_REDUNDANCY) {
} else if(type == PACKET_TYPE_REDUNDANCY) {
PrrtPacketRedundancyPayload *payload = packet_ptr->payload;
printf("| %29u | %13u | %13u |\n", payload->base_seqno, payload->n, payload->k);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
} else if (type == PACKET_TYPE_FEEDBACK) {
} else if(type == PACKET_TYPE_FEEDBACK) {
PrrtPacketFeedbackPayload *payload = packet_ptr->payload;
printf("| %61u |\n", payload->group_round_trip_time);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
......@@ -80,7 +85,29 @@ int PrrtPacket_print(PrrtPacket *packet_ptr) {
return 0;
}
int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
PrrtPacket *PrrtPacket_copy(PrrtPacket *original)
{
PrrtPacket *newPacket = calloc(1, sizeof(PrrtPacket));
check_mem(newPacket);
void *payload = calloc(1, original->payload_len);
check_mem(payload);
newPacket->payload = payload;
newPacket->payload_len = original->payload_len;
memcpy(newPacket->payload, original->payload, original->payload_len);
newPacket->index = original->index;
newPacket->seqno = original->seqno;
newPacket->type_priority = original->type_priority;
return newPacket;
error:
PERROR("Not enough memory for packet copies.");
}
int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
{
void *payload = packet_ptr->payload;
check(packet_ptr->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE <= buf_size, "Buffer too small.");
......@@ -88,13 +115,13 @@ int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
buf_ptr = encode_general_header(buf_ptr, packet_ptr);
uint8_t type = PrrtPacket_type(packet_ptr);
if (type == PACKET_TYPE_DATA) {
if(type == PACKET_TYPE_DATA) {
buf_ptr = encode_data_header(buf_ptr, payload);
PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_DATA_HEADER_SIZE);
} else if (type == PACKET_TYPE_REDUNDANCY) {
} else if(type == PACKET_TYPE_REDUNDANCY) {
buf_ptr = encode_redundancy_header(buf_ptr, payload);
PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
} else if (type == PACKET_TYPE_FEEDBACK) {
} else if(type == PACKET_TYPE_FEEDBACK) {
buf_ptr = encode_feedback_header(buf_ptr, payload);
PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_FEEDBACK_HEADER_SIZE);
} else {
......@@ -107,7 +134,8 @@ int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
return -1;
}
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
void *encode_redundancy_header(void *buf_ptr, const void *payload)
{
const PrrtPacketRedundancyPayload *redundancyPayload = payload;
uint16_t *baseSeqNo = (uint16_t *) buf_ptr;
......@@ -125,7 +153,8 @@ void *encode_redundancy_header(void *buf_ptr, const void *payload) {
return buf_ptr;
}
void *encode_feedback_header(void *buf_ptr, const void *payload) {
void *encode_feedback_header(void *buf_ptr, const void *payload)
{
const PrrtPacketFeedbackPayload *feedbackPayload = payload;
uint32_t *receiver_ip = (uint32_t *) buf_ptr;
......@@ -171,7 +200,8 @@ void *encode_feedback_header(void *buf_ptr, const void *payload) {
return buf_ptr;
}
void *encode_data_header(void *buf_ptr, const void *payload) {
void *encode_data_header(void *buf_ptr, const void *payload)
{
const PrrtPacketDataPayload *data_payload = payload;
uint32_t *timestamp = (uint32_t *) buf_ptr;
......@@ -196,7 +226,8 @@ void *encode_data_header(void *buf_ptr, const void *payload) {
return buf_ptr;
}
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet)
{
uint8_t *type_priority = (uint8_t *) buf_ptr;
*type_priority = packet->type_priority;
buf_ptr += 1;
......@@ -212,7 +243,8 @@ void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
return buf_ptr;
}
int PrrtPacket_decode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
int PrrtPacket_decode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
{
uint32_t payload_len = (uint32_t) (buf_size - PRRT_PACKET_GENERAL_HEADER_SIZE);
packet_ptr->type_priority = *(uint8_t *) buf_ptr;
buf_ptr += 1;
......@@ -229,13 +261,13 @@ int PrrtPacket_decode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
packet_ptr->payload = payload_buffer;
packet_ptr->payload_len = payload_len;
if (PrrtPacket_type(packet_ptr) == PACKET_TYPE_DATA) {
if(PrrtPacket_type(packet_ptr) == PACKET_TYPE_DATA) {
buf_ptr = decode_data_header(buf_ptr, payload_buffer);
PrrtPacket_copy_buffer_to_payload(packet_ptr, buf_ptr, PRRT_PACKET_DATA_HEADER_SIZE);
} else if (PrrtPacket_type(packet_ptr)) {
} else if(PrrtPacket_type(packet_ptr)) {
buf_ptr = decode_redundancy_header(buf_ptr, payload_buffer);
PrrtPacket_copy_buffer_to_payload(packet_ptr, buf_ptr, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
} else if (PrrtPacket_type(packet_ptr) == PACKET_TYPE_FEEDBACK) {
} else if(PrrtPacket_type(packet_ptr) == PACKET_TYPE_FEEDBACK) {
buf_ptr = decode_feedback_header(buf_ptr, payload_buffer);
PrrtPacket_copy_buffer_to_payload(packet_ptr, buf_ptr, PRRT_PACKET_FEEDBACK_HEADER_SIZE);
} else {
......@@ -247,7 +279,8 @@ int PrrtPacket_decode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
return EXIT_FAILURE;
}
void *decode_redundancy_header(void *buf_ptr, const void *payload_ptr) {
void *decode_redundancy_header(void *buf_ptr, const void *payload_ptr)
{
PrrtPacketRedundancyPayload *redundancyPayload = (PrrtPacketRedundancyPayload *) payload_ptr;
uint16_t *baseSeqNo = (uint16_t *) buf_ptr;
......@@ -265,7 +298,8 @@ void *decode_redundancy_header(void *buf_ptr, const void *payload_ptr) {
return buf_ptr;
}
void *decode_feedback_header(void *buf_ptr, const void *payload_ptr) {
void *decode_feedback_header(void *buf_ptr, const void *payload_ptr)
{
PrrtPacketFeedbackPayload *feedback_payload = (PrrtPacketFeedbackPayload *) payload_ptr;
uint32_t *receiver_addr = (uint32_t *) buf_ptr;
......@@ -311,7 +345,8 @@ void *decode_feedback_header(void *buf_ptr, const void *payload_ptr) {
return buf_ptr;
}
void *decode_data_header(void *buf_ptr, const void *payload_ptr) {
void *decode_data_header(void *buf_ptr, const void *payload_ptr)
{
PrrtPacketDataPayload *data_payload = (PrrtPacketDataPayload *) payload_ptr;
uint32_t *timestamp = (uint32_t *) buf_ptr;
......@@ -336,8 +371,9 @@ void *decode_data_header(void *buf_ptr, const void *payload_ptr) {
return buf_ptr;
}
int PrrtPacket_destroy(PrrtPacket *packet_ptr) {
if (packet_ptr->payload != NULL) {
int PrrtPacket_destroy(PrrtPacket *packet_ptr)
{
if(packet_ptr->payload != NULL) {
free(packet_ptr->payload);
}
free(packet_ptr);
......@@ -345,7 +381,8 @@ int PrrtPacket_destroy(PrrtPacket *packet_ptr) {
}
int PrrtPacket_create_data_packet(PrrtPacket *packet_ptr, uint8_t priority, const void *data_ptr,
uint32_t data_len, uint16_t seqno) {
uint32_t data_len, uint16_t seqno)
{
uint32_t payload_length = (uint32_t) (data_len + PRRT_PACKET_DATA_HEADER_SIZE);
packet_ptr->type_priority = PACKET_TYPE_DATA << 4;
......@@ -378,7 +415,8 @@ int PrrtPacket_create_data_packet(PrrtPacket *packet_ptr, uint8_t priority, cons
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *data_ptr, uint32_t data_len,
uint16_t seqno, uint8_t index, uint16_t base_seqno,
PrrtCodingParams codingParams) {
PrrtCodingParams codingParams)
{
uint32_t payload_length = (uint32_t) (data_len + PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
......@@ -412,7 +450,8 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
uint32_t forward_trip_time, uint32_t packet_loss_rate, uint16_t gap,
uint16_t ngap,
uint16_t burst, uint16_t nburst, uint32_t bandwidth,
uint32_t buffer_feedback) {
uint32_t buffer_feedback)
{
PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
check_mem(packet_ptr);
packet_ptr->type_priority = PACKET_TYPE_FEEDBACK << 4;
......
......@@ -61,7 +61,7 @@ uint8_t PrrtPacket_type(PrrtPacket *packet_ptr);
uint8_t PrrtPacket_priority(PrrtPacket *packet_ptr);
uint16_t PrrtPacket_size(PrrtPacket *packet_ptr);
int PrrtPacket_print(PrrtPacket *packet_ptr);
PrrtPacket *PrrtPacket_copy(PrrtPacket *original);
int PrrtPacket_create_data_packet(PrrtPacket *packet_ptr, uint8_t priority, const void *data_ptr,
uint32_t data_len, uint16_t seqno);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment