Commit aec080cf authored by Andreas Schmidt's avatar Andreas Schmidt

Clean-up usings. Add more checks.

parent 63374a82
add_library(PRRT ../defines.h socket.c block.c block.h packet.c packet.h processes/feedback_receiver.c processes/feedback_receiver.h processes/data_transmitter.c processes/data_transmitter.h coding_params.c coding_params.h vdmcode/block_code.c vdmcode/block_code.h coding_params.c coding_params.h collections/in_buffer.c collections/in_buffer.h stores/forward_packet_table.c stores/forward_packet_table.h processes/data_receiver.c processes/data_receiver.h)
\ No newline at end of file
add_library(PRRT ../defines.h socket.c block.c block.h packet.c packet.h processes/feedback_receiver.c processes/feedback_receiver.h processes/data_transmitter.c processes/data_transmitter.h coding_params.c coding_params.h vdmcode/block_code.c vdmcode/block_code.h coding_params.c coding_params.h stores/forward_packet_table.c stores/forward_packet_table.h processes/data_receiver.c processes/data_receiver.h)
\ No newline at end of file
#include <string.h>
#include <defines.h>
#include <prrt/vdmcode/block_code.h>
#include "block.h"
#include "../defines.h"
#include "vdmcode/block_code.h"
void clear_list(gf *const *src, uint8_t k) {
int j = 0;
......
#ifndef PRRT_BLOCK_H
#define PRRT_BLOCK_H
#include "../util/list.h"
#include "coding_params.h"
#include "packet.h"
#include <prrt/coding_params.h>
#include <prrt/packet.h>
#include <util/list.h>
typedef struct {
uint32_t data_count;
......
#include "coding_params.h"
#include "../defines.h"
#include <defines.h>
#include <prrt/coding_params.h>
void PrrtCodingParams_init(PrrtCodingParams *cpar) {
pthread_mutex_init(&cpar->lock, NULL);
......
//
// Created by andreas on 11.02.16.
//
#ifndef PRRT_IN_BUFFER_H
#define PRRT_IN_BUFFER_H
typedef struct {
} PrrtInBuffer;
#endif //PRRT_IN_BUFFER_H
......@@ -3,8 +3,8 @@
#include <sys/time.h>
#include <string.h>
#include <netinet/in.h>
#include <assert.h>
#include "packet.h"
#include <prrt/packet.h>
#include <util/dbg.h>
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
......@@ -81,10 +81,7 @@ int PrrtPacket_print(PrrtPacket *packet_ptr) {
int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
void *payload = packet_ptr->payload;
if (buf_size < packet_ptr->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE) {
perror("BUFFER TO SMALL");
return -1;
}
check(packet_ptr->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE <= buf_size, "Buffer too small.");
buf_ptr = encode_general_header(buf_ptr, packet_ptr);
......@@ -105,10 +102,13 @@ int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
return -1;
}
return 0;
error:
return -1;
}
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
PrrtPacketRedundancyPayload *redundancyPayload = payload;
const PrrtPacketRedundancyPayload *redundancyPayload = payload;
uint16_t *baseSeqNo = (uint16_t *) buf_ptr;
*baseSeqNo = htons(redundancyPayload->base_seqno);
......@@ -126,7 +126,7 @@ void *encode_redundancy_header(void *buf_ptr, const void *payload) {
}
void *encode_feedback_header(void *buf_ptr, const void *payload) {
PrrtPacketFeedbackPayload *feedbackPayload = payload;
const PrrtPacketFeedbackPayload *feedbackPayload = payload;
uint32_t *receiver_ip = (uint32_t *) buf_ptr;
*receiver_ip = htonl(feedbackPayload->receiver_addr);
......@@ -172,7 +172,7 @@ void *encode_feedback_header(void *buf_ptr, const void *payload) {
}
void *encode_data_header(void *buf_ptr, const void *payload) {
PrrtPacketDataPayload *data_payload = payload;
const PrrtPacketDataPayload *data_payload = payload;
uint32_t *timestamp = (uint32_t *) buf_ptr;
*timestamp = htonl(data_payload->timestamp);
......@@ -224,7 +224,8 @@ int PrrtPacket_decode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
buf_ptr += 2;
void *payload_buffer = malloc(payload_len);
assert(payload_buffer != NULL);
check_mem(payload_buffer);
packet_ptr->payload = payload_buffer;
packet_ptr->payload_len = payload_len;
......@@ -242,6 +243,9 @@ int PrrtPacket_decode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
printf("NOT IMPLEMENTED\n");
}
return 0;
error:
return EXIT_FAILURE;
}
void *decode_redundancy_header(void *buf_ptr, const void *payload_ptr) {
......@@ -349,10 +353,10 @@ int PrrtPacket_create_data_packet(PrrtPacket *packet_ptr, uint8_t priority, cons
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 = seqno; // TODO: replace with sensible value
packet_ptr->seqno = seqno;
void *content_buf = malloc(sizeof(PrrtPacketDataPayload) + data_len);
assert(content_buf != NULL);
check_mem(content_buf);
PrrtPacketDataPayload *source_payload = content_buf;
......@@ -370,7 +374,8 @@ int PrrtPacket_create_data_packet(PrrtPacket *packet_ptr, uint8_t priority, cons
memcpy(content_buf + sizeof(PrrtPacketDataPayload), data_ptr, data_len);
packet_ptr->payload_len = payload_length;
return 0;
error:
return -1;
}
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *data_ptr, uint32_t data_len,
......@@ -379,14 +384,15 @@ PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *data_ptr
uint32_t payload_length = (uint32_t) (data_len + sizeof(PrrtPacketRedundancyPayload));
PrrtPacket *packet_ptr = malloc(sizeof(PrrtPacket));
assert(packet_ptr != NULL);
check_mem(packet_ptr);
packet_ptr->type_priority = PACKET_TYPE_REDUNDANCY << 4;
packet_ptr->type_priority |= priority & 0x0F;
packet_ptr->index = index;
packet_ptr->seqno = seqno;
void *content_buf = malloc(sizeof(PrrtPacketRedundancyPayload) + data_len);
assert(content_buf != NULL);
check_mem(content_buf);
PrrtPacketRedundancyPayload *redundancy_payload = content_buf;
......@@ -399,6 +405,9 @@ PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *data_ptr
packet_ptr->payload_len = payload_length;
return packet_ptr;
error:
return NULL;
}
PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, uint16_t seqno, uint32_t round_trip_time,
......@@ -407,14 +416,14 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
uint16_t burst, uint16_t nburst, uint32_t bandwidth,
uint32_t buffer_feedback) {
PrrtPacket *packet_ptr = malloc(sizeof(PrrtPacket));
assert(packet_ptr != NULL);
check_mem(packet_ptr);
packet_ptr->type_priority = PACKET_TYPE_FEEDBACK << 4;
packet_ptr->type_priority |= priority & 0x0F;
packet_ptr->index = index;
packet_ptr->seqno = seqno;
PrrtPacketFeedbackPayload *payload = malloc(sizeof(PrrtPacketFeedbackPayload));
assert(payload != NULL);
check_mem(payload);
payload->group_round_trip_time = round_trip_time;
payload->forward_trip_time = forward_trip_time;
......@@ -430,4 +439,7 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
packet_ptr->payload_len = (uint32_t) (sizeof(PrrtPacketFeedbackPayload));
return packet_ptr;
error:
return NULL;
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
#define PRRT_FRAME_H
#include <stdint.h>
#include "coding_params.h"
#include <prrt/coding_params.h>
#define PACKET_TYPE_DATA 0
#define PACKET_TYPE_REPETITION 1
......
#include <defines.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <util/dbg.h>
#include <prrt/socket.h>
#include <arpa/inet.h>
#include "data_receiver.h"
int send_feedback(const PrrtSocket *sock_ptr, const char *remote_host, const int remote_port) {
......@@ -20,7 +24,8 @@ int send_feedback(const PrrtSocket *sock_ptr, const char *remote_host, const int
check(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) == EXIT_SUCCESS,
"Buffer for encoding feedback is too small");
check(sendto(sock_ptr->feedbackSocketFd, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) == length, "Sending feedback failed.");
check(sendto(sock_ptr->feedbackSocketFd, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) ==
length, "Sending feedback failed.");
return EXIT_SUCCESS;
......
#ifndef PRRT_DATA_RECEIVER_H
#define PRRT_DATA_RECEIVER_H
#include "../../util/dbg.h"
void *receive_data_loop(void *ptr);
#endif //PRRT_DATA_RECEIVER_H
......@@ -2,9 +2,11 @@
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <prrt/socket.h>
#include <prrt/block.h>
#include <defines.h>
#include <stdio.h>
#include "data_transmitter.h"
#include "../socket.h"
#include "../block.h"
int send_packet(PrrtSocket *sock_ptr, PrrtPacket *data_pkt) {
......
#include <string.h>
#include <unistd.h>
#include <defines.h>
#include <prrt/packet.h>
#include <prrt/socket.h>
#include "feedback_receiver.h"
#include "../../defines.h"
#include "../socket.h"
void *receive_feedback_loop(void *ptr) {
char bufin[MAX_PAYLOAD_LENGTH];
......
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <defines.h>
#include <prrt/packet.h>
#include <prrt/socket.h>
#include <prrt/processes/feedback_receiver.h>
#include <prrt/processes/data_transmitter.h>
#include <prrt/processes/data_receiver.h>
#include "socket.h"
#include <util/dbg.h>
int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender) {
......@@ -70,7 +81,7 @@ int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, const size_t data_le
pthread_mutex_lock(&sock_ptr->outQueueFilledMutex);
PrrtPacket *packet = malloc(sizeof(PrrtPacket));
assert(packet != NULL);
check_mem(packet);
PrrtPacket_create_data_packet(packet, 5, data, data_len, sock_ptr->sequenceNumberSource++);
List_push(sock_ptr->outQueue, packet);
......@@ -78,13 +89,17 @@ int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, const size_t data_le
pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex);
return 0;
error:
return -1;
}
uint32_t PrrtSocket_recv(const PrrtSocket *sock_ptr, void *buf_ptr) {
pthread_mutex_t t = sock_ptr->inQueueFilledMutex;
while (1) {
pthread_mutex_lock(&sock_ptr->inQueueFilledMutex);
pthread_mutex_lock(&t);
while (List_count(sock_ptr->inQueue) == 0) {
pthread_cond_wait(&sock_ptr->inQueueFilledMutexCv, &sock_ptr->inQueueFilledMutex);
pthread_cond_wait(&sock_ptr->inQueueFilledMutexCv, &t);
}
PrrtPacket *packet = List_shift(sock_ptr->inQueue);
......@@ -92,7 +107,7 @@ uint32_t PrrtSocket_recv(const PrrtSocket *sock_ptr, void *buf_ptr) {
uint32_t len = packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE;
memcpy(buf_ptr, packet->payload + PRRT_PACKET_DATA_HEADER_SIZE, len);
pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex);
pthread_mutex_unlock(&t);
return len;
}
}
......@@ -116,17 +131,17 @@ PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, co
struct sockaddr_in remote;
socklen_t addrlen = sizeof(remote);
n = recvfrom(sock_ptr->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
if (n < 0) {
perror("RECVFROM FAIL");
return NULL;
}
check(n = recvfrom(sock_ptr->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen) >= 0, "Receiving feedback failed.");
uint16_t remote_port = ntohs(remote.sin_port);
char *remote_host = inet_ntoa(remote.sin_addr);
printf("RECV FEEDBACK: %s:%d\n", remote_host, remote_port);
debug("Received feedback %s:%d", remote_host, remote_port);
PrrtPacket *packet_ptr = malloc(sizeof(PrrtPacket));
assert(packet_ptr != NULL);
check_mem(packet_ptr);
PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
return packet_ptr;
error:
return NULL;
}
#ifndef PRRT_SOCKET_H
#define PRRT_SOCKET_H
#include <arpa/inet.h>
#include <assert.h>
#include <netdb.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "packet.h"
#include "processes/feedback_receiver.h"
#include "processes/data_transmitter.h"
#include "stores/forward_packet_table.h"
#include "../defines.h"
#include "../util/list.h"
#include <defines.h>
#include <util/list.h>
#include <prrt/packet.h>
#include <prrt/stores/forward_packet_table.h>
typedef struct {
const char* host_name;
......
#include <stdint.h>
#include <string.h>
#include <defines.h>
#include <prrt/packet.h>
#include "forward_packet_table.h"
void move_start(PrrtForwardPacketTable *fpt_ptr) {
......
......@@ -2,11 +2,6 @@
#define PRRT_FORWARD_PACKET_TABLE_H
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "../../defines.h"
#include "../packet.h"
#define FORWARD_PACKET_TABLE_LENGTH 2048 // sequence number space is 2**16 and we have 32 bit so: 2048
......
......@@ -49,7 +49,7 @@ int main(int argc, char* const argv) {
free(line);
usleep(1000*1000*1000);
usleep(1000*1000);
PrrtSocket_close(&sock);
printf("COMPLETELY CLOSED\n");
return 0;
......
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