aboutsummaryrefslogtreecommitdiff
path: root/protocol.c
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2023-09-25 22:21:42 -0400
committersotech117 <michael_foiani@brown.edu>2023-09-25 22:21:42 -0400
commit6a2c567b85be275bb431c09952a88ea4cdf210aa (patch)
treeff649ae093b3b8eb9931e3a2e4cf7bb3e5ce0bb7 /protocol.c
parent8c6ae1ecde9faa0af5dacaf7ecf0f9cf47b69159 (diff)
massive restrcuting of code for readability
Diffstat (limited to 'protocol.c')
-rw-r--r--protocol.c31
1 files changed, 28 insertions, 3 deletions
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