Commit 1ba2975b authored by Andreas Schmidt's avatar Andreas Schmidt

Update packet print. Add additional functionality.

parent 1be7c2a0
......@@ -2,6 +2,7 @@
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <netinet/in.h>
#include "packet.h"
uint8_t packet_type(prrt_packet *packet_ptr) {
......@@ -17,30 +18,34 @@ uint16_t packet_size(prrt_packet *packet_ptr) {
}
int print_packet(prrt_packet *packet_ptr) {
printf("+------+------+----------+------------------+\n");
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");
uint8_t type = packet_type(packet_ptr);
printf("| %4u | %4u | %8u | %16u |\n", type, packet_priority(packet_ptr), packet_ptr->index, packet_ptr->seqno);
printf("+------------------------+------------------+\n");
printf("| %5u | %5u | %13u | %29u |\n", type, packet_priority(packet_ptr), packet_ptr->index, packet_ptr->seqno);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
if(type == PACKET_TYPE_SOURCE) {
if(type == PACKET_TYPE_DATA) {
prrt_packet_source_payload * payload = packet_ptr->payload;
printf("| %32u |\n", payload->timestamp);
printf("+---------------------+---------------------+\n");
printf("| %16u | %16u |\n", payload->round_trip_time, payload->packet_timeout);
printf("+---------------------+---------------------+\n");
printf("| %61u |\n", payload->timestamp);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %29u | %29u |\n", payload->group_round_trip_time, payload->packet_timeout);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
}
else if(type == PACKET_TYPE_FEEDBACK) {
prrt_packet_feedback_payload * payload = packet_ptr->payload;
printf("| %32u |\n", payload->round_trip_time);
printf("| %32u |\n", payload->packet_loss_rate);
printf("| %32u |\n", payload->burstiness_coefficient);
printf("| %32u |\n", payload->bandwidth);
printf("| %32u |\n", payload->feedback_block);
printf("+-------------------------------------------+\n");
printf("| %61u |\n", payload->group_round_trip_time);
printf("| %61u |\n", payload->forward_trip_time);
printf("| %61u |\n", payload->packet_loss_rate);
printf("| %29u | %29u |\n", payload->gap, payload->ngap);
printf("| %29u | %29u |\n", payload->burst, payload->nburst);
printf("| %61u |\n", payload->bandwidth);
printf("| %61u |\n", payload->buffer_feedback);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
} else {
printf("| Unhandeled Type |\n");
printf("+-------------------------------------------+\n");
printf("| Unhandeled Type |\n");
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
}
return 0;
......@@ -50,18 +55,25 @@ int encode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet) {
void * payload = packet->payload;
if(buf_size < packet->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE) {
perror("BUFER TO SMALL");
perror("BUFFER TO SMALL");
return -1;
}
memcpy(buf_ptr, packet, PRRT_PACKET_GENERAL_HEADER_SIZE);
buf_ptr += PRRT_PACKET_GENERAL_HEADER_SIZE;
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_SOURCE) {
memcpy(buf_ptr, payload, PRRT_PACKET_SOURCE_HEADER_SIZE);
buf_ptr += PRRT_PACKET_SOURCE_HEADER_SIZE;
memcpy(buf_ptr, payload + PRRT_PACKET_SOURCE_HEADER_SIZE, packet->payload_len - PRRT_PACKET_SOURCE_HEADER_SIZE);
if(type == PACKET_TYPE_DATA) {
memcpy(buf_ptr, payload, PRRT_PACKET_DATA_HEADER_SIZE);
buf_ptr += PRRT_PACKET_DATA_HEADER_SIZE;
memcpy(buf_ptr, payload + PRRT_PACKET_DATA_HEADER_SIZE, packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
} else if (type == PACKET_TYPE_FEEDBACK) {
memcpy(buf_ptr, payload, PRRT_PACKET_FEEDBACK_SIZE);
} else {
......@@ -77,11 +89,16 @@ int decode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet_ptr) {
uint8_t* index_ptr = (uint8_t*) (buf_ptr+1);
packet_ptr->index = *index_ptr;
uint16_t* seqno_prt = (uint16_t*) (buf_ptr+2);
packet_ptr->seqno = *seqno_prt;
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);
printf("%s\n", payload_buffer + PRRT_PACKET_DATA_HEADER_SIZE);
packet_ptr->payload = payload_buffer;
packet_ptr->payload_len = payload_len;
......@@ -97,11 +114,11 @@ int delete_packet(prrt_packet *packet_ptr) {
}
// PACKET SPECIFIC CODE
int create_packet_source(prrt_packet *packet_ptr, uint8_t priority, const void * data_ptr, unsigned long data_len) {
packet_ptr->type_priority = PACKET_TYPE_SOURCE << 4;
int create_packet_data(prrt_packet *packet_ptr, uint8_t priority, const void *data_ptr, unsigned long data_len) {
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
packet_ptr->seqno = 4711; // TODO: replace with sensible value
prrt_packet_source_payload * payload = malloc(sizeof(prrt_packet_source_payload));
......@@ -123,9 +140,10 @@ int create_packet_source(prrt_packet *packet_ptr, uint8_t priority, const void *
return 0;
}
prrt_packet *create_packet_feedback(uint8_t priority, uint8_t index, uint16_t seqno, uint32_t round_trip_time,
uint32_t bandwidth, uint32_t burstiness_coefficient, uint32_t packet_loss_rate,
uint32_t feedback_block) {
prrt_packet *create_packet_feedback(uint8_t priority, uint8_t index, uint16_t seqno, uint32_t receiver_addr,
uint32_t round_trip_time, 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) {
prrt_packet *packet_ptr = malloc(sizeof(prrt_packet));
packet_ptr->type_priority = PACKET_TYPE_FEEDBACK << 4;
packet_ptr->type_priority |= priority & 0x0F;
......@@ -134,11 +152,16 @@ prrt_packet *create_packet_feedback(uint8_t priority, uint8_t index, uint16_t se
prrt_packet_feedback_payload * payload = malloc(sizeof(prrt_packet_feedback_payload));
payload->round_trip_time = round_trip_time;
payload->bandwidth = bandwidth;
payload->burstiness_coefficient = burstiness_coefficient;
payload->receiver_addr = receiver_addr;
payload->group_round_trip_time = round_trip_time;
payload->forward_trip_time = forward_trip_time;
payload->packet_loss_rate = packet_loss_rate;
payload->feedback_block = feedback_block;
payload->gap = gap;
payload->ngap = ngap;
payload->burst = burst;
payload->nburst = nburst;
payload->bandwidth = bandwidth;
payload->buffer_feedback = buffer_feedback;
packet_ptr->payload = (void*) payload;
packet_ptr->payload_len = (uint32_t) (sizeof(prrt_packet_source_payload));
......
......@@ -3,7 +3,7 @@
#include <stdint.h>
#define PACKET_TYPE_SOURCE 0
#define PACKET_TYPE_DATA 0
#define PACKET_TYPE_REPETITION 1
#define PACKET_TYPE_PARITY 2
#define PACKET_TYPE_FEEDBACK 3
......@@ -17,22 +17,27 @@ typedef struct {
uint32_t payload_len;
} prrt_packet;
#define PRRT_PACKET_SOURCE_HEADER_SIZE 12
#define PRRT_PACKET_DATA_HEADER_SIZE 12
typedef struct {
uint32_t timestamp;
uint16_t round_trip_time;
uint16_t group_round_trip_time;
uint16_t packet_timeout;
uint16_t decoding_timeout;
uint16_t feedback_timer;
uint16_t feedback_timeout;
} prrt_packet_source_payload;
# define PRRT_PACKET_FEEDBACK_SIZE 20
# define PRRT_PACKET_FEEDBACK_SIZE 32
typedef struct {
uint32_t round_trip_time;
uint32_t receiver_addr;
uint32_t group_round_trip_time;
uint32_t forward_trip_time;
uint32_t packet_loss_rate;
uint32_t burstiness_coefficient;
uint16_t gap;
uint16_t ngap;
uint16_t burst;
uint16_t nburst;
uint32_t bandwidth;
uint32_t feedback_block;
uint32_t buffer_feedback;
} prrt_packet_feedback_payload;
uint8_t packet_type(prrt_packet *packet_ptr);
......@@ -48,7 +53,7 @@ int print_packet(prrt_packet *packet_ptr);
int decode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet_ptr);
int create_packet_source(prrt_packet *packet_ptr, uint8_t priority, const void * data_ptr, unsigned long data_len);
int create_packet_data(prrt_packet *packet_ptr, uint8_t priority, const void *data_ptr, unsigned long data_len);
int encode_packet(void *buf_ptr, uint16_t buf_size, prrt_packet *packet_ptr);
int delete_packet(prrt_packet *packet_ptr);
......@@ -56,9 +61,10 @@ int create_packet_repetition(prrt_packet packet, uint8_t priority);
int create_packet_parity(prrt_packet packet, uint8_t priority);
prrt_packet *create_packet_feedback(uint8_t priority, uint8_t index, uint16_t seqno, uint32_t round_trip_time,
uint32_t bandwidth, uint32_t burstiness_coefficient, uint32_t packet_loss_rate,
uint32_t feedback_block);
prrt_packet *create_packet_feedback(uint8_t priority, uint8_t index, uint16_t seqno, uint32_t receiver_addr,
uint32_t round_trip_time, 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);
int encode_packet_feedback(void *buf_ptr, uint16_t buf_size, prrt_packet *pkt_ptr);
int decode_packet_feedback(void* buf_ptr, uint16_t buf_size, prrt_packet *packet_ptr);
......
......@@ -6,6 +6,7 @@
#include <stdlib.h>
#include "../defines.h"
#include "socket.h"
#include "../util/common.h"
int prrt_create_socket(prrt_socket *sock_ptr, uint16_t port) {
// Create Data Socket
......@@ -61,7 +62,7 @@ int prrt_connect(prrt_socket *sock_ptr, char *host, uint16_t port) {
int prrt_send(prrt_socket *sock_ptr, const void *data, size_t data_len) {
prrt_packet packet;
create_packet_source(&packet, 5, data, data_len);
create_packet_data(&packet, 5, data, data_len);
// TODO: finally delete_packet(packet);
......@@ -71,6 +72,7 @@ int prrt_send(prrt_socket *sock_ptr, const void *data, size_t data_len) {
perror("BUF too small.");
return -1;
}
print_packet(&packet);
// SENDING TO ALL RECEIVERS
int i;
......@@ -109,6 +111,7 @@ 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;
......@@ -121,7 +124,10 @@ 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, 5, 47, 50, 1);
uint32_t addr = get_ip_from_socket(sock_ptr->fd_data);
printf("%d\n");
prrt_packet* feedback_pkt_ptr = create_packet_feedback(0, 19, 4715, addr, 5, 0, 50, 0, 0, 0, 0, 5, 1);
uint8_t buf[MAX_PAYLOAD_LENGTH];
uint32_t length = packet_size(feedback_pkt_ptr);
......
......@@ -29,9 +29,9 @@ int main(int argc, char* const argv[]) {
} else {
// TODO: case distinction over message type
char buffer[pkt->payload_len+1- PRRT_PACKET_SOURCE_HEADER_SIZE];
memcpy(buffer, pkt->payload + PRRT_PACKET_SOURCE_HEADER_SIZE, pkt->payload_len - PRRT_PACKET_SOURCE_HEADER_SIZE);
buffer[pkt->payload_len - PRRT_PACKET_SOURCE_HEADER_SIZE] = '\0';
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);
}
......
......@@ -24,7 +24,7 @@ int main(int argc, char* const argv) {
prrt_connect(&sock, remote_host, remote_port);
char *remote_host2 = "localhost";
uint16_t remote_port2 = 5003;
uint16_t remote_port2 = 5004;
prrt_connect(&sock, remote_host2, remote_port2);
printf("SENDING\n");
......
......@@ -9,7 +9,7 @@ int main() {
char *message = "this is a message";
prrt_packet * packet = malloc(sizeof(prrt_packet));
create_packet_source(packet, 0, message, strlen(message));
create_packet_data(packet, 0, message, strlen(message));
print_packet(packet);
......
#include <stdio.h>
#include <arpa/inet.h>
#include "common.h"
int print_buffer(char* buf, int length) {
......@@ -10,4 +11,11 @@ int print_buffer(char* buf, int length) {
printf("%2x", buf[i]);
}
printf("\n");
}
uint32_t get_ip_from_socket(int fd) {
struct sockaddr_in addr;
socklen_t addr_size = sizeof(struct sockaddr_in);
int res = getpeername(fd, (struct sockaddr *)&addr, &addr_size);
return addr.sin_addr.s_addr;
}
\ No newline at end of file
......@@ -2,5 +2,6 @@
#define PRRT_COMMON_H
int print_buffer(char* buf, int length);
uint32_t get_ip_from_socket(int fd);
#endif //PRRT_COMMON_H
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