Commit eec89693 authored by Andreas Schmidt's avatar Andreas Schmidt

Fix encoding for data and feedback packets.

parent 6e175dd1
......@@ -5,6 +5,12 @@
#include <netinet/in.h>
#include "packet.h"
void *encode_general_header(void *buf_ptr, const prrt_packet *packet);
void *encode_data_header(void *buf_ptr, void *payload);
void *encode_feedback_header(void *buf_ptr, void *payload);
uint8_t packet_type(prrt_packet *packet_ptr) {
return (uint8_t) ((packet_ptr->type_priority >> 4) & 0x0F);
}
......@@ -27,7 +33,7 @@ int print_packet(prrt_packet *packet_ptr) {
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
if(type == PACKET_TYPE_DATA) {
prrt_packet_source_payload * payload = packet_ptr->payload;
prrt_packet_data_payload * payload = packet_ptr->payload;
printf("| %61u |\n", payload->timestamp);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %29u | %29u |\n", payload->group_round_trip_time, payload->packet_timeout);
......@@ -45,7 +51,7 @@ int print_packet(prrt_packet *packet_ptr) {
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %29u | %29u |\n", payload->burst, payload->nburst);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %61u |\n", payload->bandwidth);
printf("| %61u |\n", payload->bandwidth_estimate);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %61u |\n", payload->buffer_feedback);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
......@@ -57,36 +63,117 @@ int print_packet(prrt_packet *packet_ptr) {
return 0;
}
int encode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet) {
void * payload = packet->payload;
int encode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet_ptr) {
void * payload = packet_ptr->payload;
if(buf_size < packet->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE) {
if(buf_size < packet_ptr->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE) {
perror("BUFFER TO SMALL");
return -1;
}
uint8_t *type_priority =(uint8_t *) buf_ptr;
buf_ptr = encode_general_header(buf_ptr, packet_ptr);
uint8_t type = packet_type(packet_ptr);
if(type == PACKET_TYPE_DATA) {
buf_ptr = encode_data_header(buf_ptr, payload);
memcpy(buf_ptr, payload + PRRT_PACKET_DATA_HEADER_SIZE, packet_ptr->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
} else if (type == PACKET_TYPE_FEEDBACK) {
buf_ptr = encode_feedback_header(buf_ptr, payload);
memcpy(buf_ptr, payload + PRRT_PACKET_FEEDBACK_SIZE, packet_ptr->payload_len - PRRT_PACKET_FEEDBACK_SIZE);
} else {
perror("NOT IMPLEMENTED");
return -1;
}
return 0;
}
void *encode_feedback_header(void *buf_ptr, void *payload) {
prrt_packet_feedback_payload * feedback_payload = payload;
uint32_t *receiver_ip = (uint32_t *) buf_ptr;
*receiver_ip = htonl(feedback_payload->receiver_addr);
buf_ptr += 4;
uint32_t *group_round_trip_time = (uint32_t *) buf_ptr;
*group_round_trip_time = htonl(feedback_payload->group_round_trip_time);
buf_ptr+=4;
uint32_t *forward_trip_time = (uint32_t *) buf_ptr;
*forward_trip_time = htonl(feedback_payload->forward_trip_time);
buf_ptr+=4;
uint32_t *packet_loss_rate = (uint32_t *) buf_ptr;
*packet_loss_rate = htonl(feedback_payload->packet_loss_rate);
buf_ptr+=4;
uint16_t *gap = (uint16_t *) buf_ptr;
*gap = htons(feedback_payload->gap);
buf_ptr+=2;
uint16_t *ngap = (uint16_t *) buf_ptr;
*ngap = htons(feedback_payload->ngap);
buf_ptr+=2;
uint16_t *burst = (uint16_t *) buf_ptr;
*burst = htons(feedback_payload->burst);
buf_ptr+=2;
uint16_t *nburst = (uint16_t *) buf_ptr;
*nburst = htons(feedback_payload->nburst);
buf_ptr+=2;
uint32_t *bandwidth_estimate = (uint32_t *) buf_ptr;
*bandwidth_estimate = htonl(feedback_payload->bandwidth_estimate);
buf_ptr+=4;
uint32_t *buffer_feedback = (uint32_t *) buf_ptr;
*buffer_feedback = htonl(feedback_payload->buffer_feedback);
buf_ptr+=4;
return buf_ptr;
}
void *encode_data_header(void *buf_ptr, void *payload) {
prrt_packet_data_payload * data_payload = payload;
uint32_t *timestamp = (uint32_t *) buf_ptr;
*timestamp = htonl(data_payload->timestamp);
buf_ptr += 4;
uint16_t *group_round_trip_time = (uint16_t *) buf_ptr;
*group_round_trip_time = htons(data_payload->group_round_trip_time);
buf_ptr += 2;
uint16_t *packet_timeout = (uint16_t *) buf_ptr;
*packet_timeout = htons(data_payload->packet_timeout);
buf_ptr += 2;
uint16_t *decoding_timeout = (uint16_t *) buf_ptr;
*decoding_timeout = htons(data_payload->decoding_timeout);
buf_ptr += 2;
uint16_t *feedback_timeout = (uint16_t *) buf_ptr;
*feedback_timeout = htons(data_payload->feedback_timeout);
buf_ptr += 2;
return buf_ptr;
}
void *encode_general_header(void *buf_ptr, const prrt_packet *packet) {
uint8_t *type_priority = (uint8_t *) buf_ptr;
*type_priority = packet->type_priority;
buf_ptr += 1;
uint8_t *index =(uint8_t *) buf_ptr;
*index = packet->index;
buf_ptr += 1;
uint16_t *seqno =(uint16_t *) buf_ptr;
*seqno = htons(packet->seqno);
buf_ptr += 2;
uint8_t type = packet_type(packet);
if(type == PACKET_TYPE_DATA) {
// TODO: use network order
memcpy(buf_ptr, payload, packet->payload_len);
} else if (type == PACKET_TYPE_FEEDBACK) {
// TODO: use network order
memcpy(buf_ptr, payload, PRRT_PACKET_FEEDBACK_SIZE);
} else {
perror("NOT IMPLEMENTED");
return -1;
}
return 0;
return buf_ptr;
}
int decode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet_ptr) {
......@@ -97,9 +184,36 @@ int decode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet_ptr) {
uint16_t* seqno_prt = (uint16_t*) (buf_ptr+2);
packet_ptr->seqno = ntohs(*seqno_prt);
// TODO: packet specific transformation
unsigned char* payload_buffer = malloc(payload_len);
memcpy(payload_buffer, buf_ptr + PRRT_PACKET_GENERAL_HEADER_SIZE, payload_len);
void* payload_buffer = malloc(payload_len);
if(packet_type(packet_ptr) == PACKET_TYPE_DATA+100) {
prrt_packet_data_payload * data_payload = (prrt_packet_data_payload *) payload_buffer;
uint32_t *timestamp = (uint32_t *) buf_ptr;
data_payload->timestamp = *timestamp;
buf_ptr += 4;
uint16_t *group_round_trip_time = (uint16_t *) buf_ptr;
data_payload->group_round_trip_time = *group_round_trip_time;
buf_ptr += 2;
uint16_t *packet_timeout = (uint16_t *) buf_ptr;
data_payload->packet_timeout = *packet_timeout;
buf_ptr += 2;
uint16_t *decoding_timeout = (uint16_t *) buf_ptr;
data_payload->decoding_timeout = *decoding_timeout;
buf_ptr += 2;
uint16_t *feedback_timeout = (uint16_t *) buf_ptr;
data_payload->feedback_timeout = *feedback_timeout;
buf_ptr += 2;
memcpy(payload_buffer, buf_ptr, packet_ptr->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
} else {
// TODO: packet specific transformation
memcpy(payload_buffer, buf_ptr, payload_len);
}
packet_ptr->payload = payload_buffer;
packet_ptr->payload_len = payload_len;
......@@ -117,33 +231,31 @@ int delete_packet(prrt_packet *packet_ptr) {
// PACKET SPECIFIC CODE
int create_packet_data(prrt_packet *packet_ptr, uint8_t priority, const void *data_ptr, unsigned long data_len) {
uint32_t payload_length = (uint32_t) (data_len + sizeof(prrt_packet_source_payload));
uint32_t payload_length = (uint32_t) (data_len + sizeof(prrt_packet_data_payload));
packet_ptr->type_priority = PACKET_TYPE_DATA << 4;
packet_ptr->type_priority |= priority & 0x0F;
packet_ptr->index = 17; // TODO: replace with sensible value
packet_ptr->seqno = 4711; // TODO: replace with sensible value
void *content_buf = malloc(sizeof(prrt_packet_source_payload) + data_len);
void *content_buf = malloc(sizeof(prrt_packet_data_payload) + data_len);
prrt_packet_source_payload * source_payload = content_buf;
prrt_packet_data_payload * source_payload = content_buf;
struct timeval tv;
gettimeofday(&tv,NULL);
unsigned long time_in_micros = (unsigned long) (1000000 * tv.tv_sec + tv.tv_usec);
source_payload->timestamp = (uint32_t) time_in_micros;
// TODO: payload->rtt = CURRENT ESTIMATE
// TODO: payload->packet_timeout = NOW + maximum delay
// TODO: payload->decoding_timeout
// TODO: payload->feedback_timer
source_payload->group_round_trip_time = 95; // TODO: payload->rtt = CURRENT ESTIMATE
source_payload->packet_timeout = 110; // TODO: payload->packet_timeout = NOW + maximum delay
source_payload->decoding_timeout = 150; // TODO: payload->decoding_timeout
source_payload->feedback_timeout = 170; // TODO: payload->feedback_timer
packet_ptr->payload = content_buf;
memcpy(content_buf + sizeof(prrt_packet_source_payload), data_ptr, data_len);
memcpy(content_buf + sizeof(prrt_packet_data_payload), data_ptr, data_len);
packet_ptr->payload_len = payload_length;
return 0;
}
......@@ -165,7 +277,7 @@ prrt_packet *create_packet_feedback(uint8_t priority, uint8_t index, uint16_t se
payload->ngap = ngap;
payload->burst = burst;
payload->nburst = nburst;
payload->bandwidth = bandwidth;
payload->bandwidth_estimate = bandwidth;
payload->buffer_feedback = buffer_feedback;
packet_ptr->payload = (void*) payload;
......
......@@ -24,7 +24,7 @@ typedef struct {
uint16_t packet_timeout;
uint16_t decoding_timeout;
uint16_t feedback_timeout;
} prrt_packet_source_payload;
} prrt_packet_data_payload;
# define PRRT_PACKET_FEEDBACK_SIZE 32
typedef struct {
......@@ -36,7 +36,7 @@ typedef struct {
uint16_t ngap;
uint16_t burst;
uint16_t nburst;
uint32_t bandwidth;
uint32_t bandwidth_estimate;
uint32_t buffer_feedback;
} prrt_packet_feedback_payload;
......
......@@ -14,7 +14,6 @@ void *receive_loop(void *ptr) {
prrt_socket *sock_ptr = ptr;
printf("RECEIVING:\n");
while(1) {
memset(bufin, 0, MAX_PAYLOAD_LENGTH);
prrt_packet *t = prrt_recv_feedback(sock_ptr, bufin, MAX_PAYLOAD_LENGTH);
......@@ -140,7 +139,6 @@ prrt_packet *prrt_recv(prrt_socket *sock_ptr) {
prrt_packet *packet = malloc(sizeof(prrt_packet));
decode_packet(buffer, (uint16_t) n, packet);
print_packet(packet);
// REPLY
struct sockaddr_in targetaddr;
......@@ -152,7 +150,7 @@ prrt_packet *prrt_recv(prrt_socket *sock_ptr) {
hp = gethostbyname(remote_host);
memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
prrt_packet* feedback_pkt_ptr = create_packet_feedback(0, 19, 4715, 5, 0, 50, 0, 0, 0, 0, 5, 1);
prrt_packet* feedback_pkt_ptr = create_packet_feedback(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
uint8_t buf[MAX_PAYLOAD_LENGTH];
uint32_t length = packet_size(feedback_pkt_ptr);
......@@ -184,9 +182,8 @@ prrt_packet * prrt_recv_feedback(prrt_socket *sock_ptr, void *bufin, size_t leng
socklen_t addrlen = sizeof(remote);
n = recvfrom(sock_ptr->fd_feedback, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
printf("SIZE: %d\n", n);
if(n < 0) {
perror("Test");
perror("RECVFROM FAIL");
return NULL;
}
uint16_t remote_port = ntohs(remote.sin_port);
......
......@@ -28,12 +28,10 @@ int main(int argc, char* const argv[]) {
break;
} else {
// TODO: case distinction over message type
char buffer[pkt->payload_len + 1 - PRRT_PACKET_DATA_HEADER_SIZE];
memcpy(buffer, pkt->payload + PRRT_PACKET_DATA_HEADER_SIZE, pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
buffer[pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE] = '\0';
printf("%s\n", buffer);
}
usleep(1000*1000);
}
......
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