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

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