Commit 650af3c2 authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Started with ForwardPacketTable.

parent a0976594
......@@ -10,32 +10,33 @@
#include "socket.h"
#include "processes/feedback_receiver.h"
#include "processes/data_transmitter.h"
#include "packet.h"
int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender) {
sock_ptr->seqno_source = 1;
sock_ptr->seqnoRedundancy = 1;
// Create Data Socket
if((sock_ptr->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
if ((sock_ptr->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket");
return -1;
}
// Create Feedback Socket
if((sock_ptr->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
if ((sock_ptr->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket");
return -1;
}
if(is_sender) {
if (is_sender) {
// Bind Feedback Socket
struct sockaddr_in address;
memset((char*) &address, 0, sizeof(address));
memset((char *) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons((uint16_t) (port + 1)); // TODO: fail if port is 65535
if(bind(sock_ptr->fd_feedback, (struct sockaddr *) &address, sizeof(address)) < 0) {
if (bind(sock_ptr->fd_feedback, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("cannot bind socket");
return -1;
}
......@@ -46,29 +47,30 @@ int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t i
sock_ptr->out_queue = List_create();
int rc = pthread_create(&sock_ptr->receive_thread, NULL, receive_feedback_loop, (void *) sock_ptr);
if(rc) {
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
rc = pthread_create(&sock_ptr->send_thread, NULL, send_data_loop, (void *) sock_ptr);
if(rc) {
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
} else {
// Bind Data Socket
struct sockaddr_in address;
memset((char*) &address, 0, sizeof(address));
memset((char *) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(port);
if(bind(sock_ptr->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
if (bind(sock_ptr->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("cannot bind socket");
return -1;
}
sock_ptr->forwardPacketTable = malloc(sizeof(PrrtForwardPacketTable));
PrrtForwardPacketTable_create(sock_ptr->forwardPacketTable);
}
......@@ -76,10 +78,10 @@ int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t i
}
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
PrrtReceiver recv = {host , port};
PrrtReceiver recv = {host, port};
if(sock_ptr->receiver_len < PRRT_MAX_RECEIVER_COUNT) {
sock_ptr->receivers[sock_ptr->receiver_len] =recv;
if (sock_ptr->receiver_len < PRRT_MAX_RECEIVER_COUNT) {
sock_ptr->receivers[sock_ptr->receiver_len] = recv;
sock_ptr->receiver_len++;
} else {
return -1;
......@@ -91,7 +93,7 @@ int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t po
int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, const size_t data_len) {
pthread_mutex_lock(&sock_ptr->out_queue_filled_mutex);
PrrtPacket * packet = malloc(sizeof(PrrtPacket));
PrrtPacket *packet = malloc(sizeof(PrrtPacket));
assert(packet != NULL);
PrrtPacket_create_data_packet(packet, 5, data, data_len, sock_ptr->seqno_source++);
......@@ -105,8 +107,7 @@ int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, const size_t data_le
uint32_t PrrtSocket_recv(const PrrtSocket *sock_ptr, void *buf_ptr) {
unsigned char buffer[MAX_PAYLOAD_LENGTH];
while(1)
{
while (1) {
// RECEIVE DATA
ssize_t n;
struct sockaddr_in remote;
......@@ -122,42 +123,47 @@ uint32_t PrrtSocket_recv(const PrrtSocket *sock_ptr, void *buf_ptr) {
// REPLY
struct sockaddr_in targetaddr;
memset((char*) &targetaddr, 0, sizeof(targetaddr));
memset((char *) &targetaddr, 0, sizeof(targetaddr));
targetaddr.sin_family = AF_INET;
targetaddr.sin_port = htons((uint16_t) (remote_port + 1));
struct hostent *hp;
hp = gethostbyname(remote_host);
memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
PrrtPacket * feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
uint8_t buf[MAX_PAYLOAD_LENGTH];
uint32_t length = PrrtPacket_size(feedback_pkt_ptr);
if(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
if (PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
perror("BUF too small.");
} else {
if((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
if ((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) <
0)) {
perror("sendto failed");
return NULL;
}
}
switch(PrrtPacket_type(packet)) {
switch (PrrtPacket_type(packet)) {
case PACKET_TYPE_DATA:
// packet.timestamp + packet.timeout < now: break
// if (FTP_is_irrelevant(packet)): break;
if (PrrtForwardPacketTable_test_set_is_number_relevant(sock_ptr->forwardPacketTable, packet->seqno) ==
FALSE) {
break;
}
// check incomplete_prrt_blocks for this seqno: insert if found
// else: insert in data_packet_store
// forward to application layer
memcpy(buf_ptr, packet->payload + PRRT_PACKET_DATA_HEADER_SIZE, packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
memcpy(buf_ptr, packet->payload + PRRT_PACKET_DATA_HEADER_SIZE,
packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
return packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE;
default:
PrrtPacket_print(packet);
//PrrtPacket_print(packet);
break;
}
......@@ -186,7 +192,7 @@ PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, co
socklen_t addrlen = sizeof(remote);
n = recvfrom(sock_ptr->fd_feedback, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
if(n < 0) {
if (n < 0) {
perror("RECVFROM FAIL");
return NULL;
}
......@@ -194,7 +200,7 @@ PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, co
char *remote_host = inet_ntoa(remote.sin_addr);
printf("RECV FEEDBACK: %s:%d\n", remote_host, remote_port);
PrrtPacket * packet_ptr = malloc(sizeof(PrrtPacket));
PrrtPacket *packet_ptr = malloc(sizeof(PrrtPacket));
assert(packet_ptr != NULL);
PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
return packet_ptr;
......
#include <string.h>
#include <stdio.h>
#include "forward_packet_table.h"
#include "../../defines.h"
#include "../packet.h"
int PrrtForwardPacketTable_create(PrrtForwardPacketTable* fpt_prt) {
void move_start(PrrtForwardPacketTable *fpt_ptr) {
uint16_t seqno = fpt_ptr->start;
uint16_t which_byte;
uint16_t which_bit;
while(1) {
which_byte = (uint16_t) (seqno / 32);
which_bit = (uint16_t) (seqno % 32);
if ((fpt_ptr->data[which_byte] >> which_bit) == 0) {
break;
}
fpt_ptr->data[which_byte] &= ~(1 << which_bit);
seqno++;
}
}
\ No newline at end of file
fpt_ptr->start = seqno;
}
int PrrtForwardPacketTable_create(PrrtForwardPacketTable *fpt_prt) {
fpt_prt->start = 1;
memset(fpt_prt->data, 0, sizeof(fpt_prt->data));
}
int PrrtForwardPacketTable_test_set_is_number_relevant(PrrtForwardPacketTable *fpt_ptr, uint16_t seqno) {
uint16_t stop = (uint16_t) (SEQNO_SPACE / 2 + fpt_ptr->start);
if (fpt_ptr->start < stop && !(fpt_ptr->start <= seqno && seqno <= stop)) {
return FALSE;
} else if (!(seqno < stop)) {
return FALSE;
} else {
printf("SEQ: %d, START:%d\n", seqno, fpt_ptr->start);
uint16_t which_byte = (uint16_t) (seqno / 32);
uint16_t which_bit = (uint16_t) (seqno % 32);
int res = ((fpt_ptr->data[which_byte] >> which_bit) & 0x01) == 0;
fpt_ptr->data[which_byte] |= 1 << which_bit;
printf("D: %d\n", fpt_ptr->data[which_byte]);
if(seqno == fpt_ptr->start) {
move_start(fpt_ptr);
}
printf("START:%d\n", fpt_ptr->start);
return res;
}
}
......@@ -3,15 +3,18 @@
#include <stdint.h>
#define FORWARD_PACKET_TABLE_LENGTH 2048 // sequence number space is 2**16 and we have 32 bit so: 2048
#define MAX_FORWARD_ENTRY_VALUE 4294967296 // 2**32
typedef struct {
uint32_t data[2048]; // sequence number space is 2**16 and we have 32 bit so: 2048
uint32_t data[FORWARD_PACKET_TABLE_LENGTH];
uint16_t start;
} PrrtForwardPacketTable;
int PrrtForwardPacketTable_create(PrrtForwardPacketTable* fpt_prt);
int PrrtForwardPacketTable_test_set_number(PrrtForwardPacketTable* fpt_ptr, uint16_t seqno);
int PrrtForwardPacketTable_test_set_is_number_relevant(PrrtForwardPacketTable *fpt_ptr, uint16_t seqno);
int PrrtForwardPacketTable_destroy(PrrtForwardPacketTable* fpt_prt);
......
Supports Markdown
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