From 6a2c567b85be275bb431c09952a88ea4cdf210aa Mon Sep 17 00:00:00 2001 From: sotech117 Date: Mon, 25 Sep 2023 22:21:42 -0400 Subject: massive restrcuting of code for readability --- protocol.c | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'protocol.c') diff --git a/protocol.c b/protocol.c index 9c8c65b..b70c70a 100644 --- a/protocol.c +++ b/protocol.c @@ -3,9 +3,11 @@ #include "protocol.h" -#define TCP_TIMEOUT 100000 // 100ms in microseconds -#define MAX_PACKET_SIZE 512 - +/* + ensures all bytes from the buffer are sent + note: applies a timeout during the send of bytes + note: modyfies the len variable to reflect the number of bytes send +*/ int send_all(int sock, char *buf, int *len) { struct timeval timeout; @@ -33,6 +35,11 @@ int send_all(int sock, char *buf, int *len) return n==-1?-1:0; // return -1 on failure, 0 on success } +/* + ensures all bytes that can be sent are loaded into the buffer + note: applies a timeout during the collection of bytes + note: modyfies the len variable to reflect the number of bytes read +*/ int recv_all(int sock, char *buf, int *len) { // setup the timeout on the socket @@ -68,6 +75,10 @@ int recv_all(int sock, char *buf, int *len) return n==-1?-1:0; // return -1 on failure, 0 on success } +/* + applies a timeout to the socket itself + note: should only be used with tcp connections, after connect() +*/ int apply_timeout(int fd) { // handle handshake struct timeval tv; @@ -81,6 +92,10 @@ int apply_timeout(int fd) { return 1; } +/* + removes the timeout on a socket + note: should only be used with tcp connections, after connect() +*/ int remove_timeout(int fd) { // handle handshake @@ -93,4 +108,14 @@ int remove_timeout(int fd) } return 1; +} + +/* + basic helper, not "really" used as we are only Ipv4 +*/ +void *get_in_addr(struct sockaddr *sa){ + if (sa->sa_family == AF_INET) { + return &(((struct sockaddr_in*)sa)->sin_addr); + } + return &(((struct sockaddr_in6*)sa)->sin6_addr); } \ No newline at end of file -- cgit v1.2.3-70-g09d2