diff options
Diffstat (limited to 'protocol.c')
-rw-r--r-- | protocol.c | 31 |
1 files changed, 28 insertions, 3 deletions
@@ -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 |