Commit 8160f1d1 authored by Andreas Schmidt's avatar Andreas Schmidt

Add checks for proper port number. Increase error resilience.

parent 7276e1b8
......@@ -46,7 +46,8 @@ int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t i
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
check(port <= 65534, "Port %d cannot be bound to.", port);
address.sin_port = htons((uint16_t) (port + 1));
check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
"Cannot bind feedback socket.");
......@@ -63,6 +64,7 @@ int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t i
sock_ptr->receivers = List_create();
} else {
sock_ptr->forwardPacketTable = calloc(1, sizeof(PrrtForwardPacketTable));
check_mem(sock_ptr->forwardPacketTable)
PrrtForwardPacketTable_create(sock_ptr->forwardPacketTable);
pthread_mutex_init(&sock_ptr->inQueueFilledMutex, NULL);
......@@ -76,10 +78,8 @@ int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t i
return EXIT_SUCCESS;
error:
// TODO: cancel threads
close(sock_ptr->dataSocketFd);
close(sock_ptr->feedbackSocketFd);
return EXIT_FAILURE;
PrrtSocket_close(sock_ptr);
return EXIT_FAILURE;
}
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
......@@ -115,7 +115,6 @@ int32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void *buf_ptr) {
while (List_count(sock_ptr->inQueue) == 0) {
pthread_mutex_lock(&sock_ptr->closingMutex);
if (sock_ptr->closing) {
// TODO: proper close
pthread_mutex_unlock(&sock_ptr->closingMutex);
pthread_mutex_unlock(&t);
return -1;
......
......@@ -15,10 +15,8 @@ int main(int argc, char *const argv) {
PrrtSocket sock = {};
debug("PRRT - SENDER\n");
if (PrrtSocket_create(&sock, local_port, TRUE) < 0) {
perror("socket failed");
return 0;
}
int res = PrrtSocket_create(&sock, local_port, TRUE);
check(res == EXIT_SUCCESS, "Socket creation failed.");
char *remote_host = "127.0.0.1";
uint16_t remote_port = 5000;
......@@ -53,4 +51,7 @@ int main(int argc, char *const argv) {
PrrtSocket_close(&sock);
debug("COMPLETELY CLOSED\n");
return 0;
error:
return EXIT_FAILURE;
}
......@@ -7,7 +7,7 @@ int print_buffer(const char *buf, const int length);
void print_gf(const gf *start, const int len);
#define PERROR(fmt, args...) \
printf("PRRT ERROR: " fmt, ## args);
printf("PRRT ERROR: \n" fmt, ## args);
#define PNOTIMPLEMENTED(args) \
printf("NOT IMPLEMENTED: %s\n", args); \
......
#include "list.h"
#include "dbg.h"
#include "common.h"
// Taken from: http://c.learncodethehardway.org/book/ex32.html
List *List_create()
{
return calloc(1, sizeof(List));
void *p = calloc(1, sizeof(List));
check_mem(p);
return p;
error:
PERROR("Could not create necessary list.", "");
}
void List_destroy(const List *list)
......@@ -37,6 +43,7 @@ void List_clear_destroy(const List *list)
void List_push(List *list, const void *value)
{
ListNode *node = calloc(1, sizeof(ListNode));
check_mem(node);
node->value = (void *) value;
......@@ -50,9 +57,10 @@ void List_push(List *list, const void *value)
}
list->count++;
return;
error:
return;
PERROR("Cannot add necessary list item: %p.", value);
}
void *List_pop(const List *list)
......@@ -64,6 +72,7 @@ void *List_pop(const List *list)
void List_unshift(List *list, const void *value)
{
ListNode *node = calloc(1, sizeof(ListNode));
check_mem(node);
node->value = (void *) value;
......@@ -79,7 +88,7 @@ void List_unshift(List *list, const void *value)
list->count++;
error:
return;
PERROR("Cannot add necessary list item: %p.", value);
}
void *List_shift(const List *list)
......@@ -92,15 +101,20 @@ void *List_remove(List *list, const ListNode *node)
{
void *result = NULL;
check(list->first && list->last, "List is empty.");
check(node, "node can't be NULL");
if(node == list->first && node == list->last) {
list->first = NULL;
list->first = NULL;
list->last = NULL;
} else if(node == list->first) {
list->first = node->next;
list->first->prev = NULL;
check(list->first != NULL, "Invalid list, somehow got a first that is NULL.");
} else if (node == list->last) {
list->last = node->prev;
list->last->next = NULL;
check(list->last != NULL, "Invalid list, somehow got a next that is NULL.");
} else {
ListNode *after = node->next;
ListNode *before = node->prev;
......@@ -112,6 +126,7 @@ void *List_remove(List *list, const ListNode *node)
result = node->value;
free((void *) node);
error:
return result;
error:
PERROR("Cannot remove list item: %p.", node);
}
\ No newline at end of file
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