aboutsummaryrefslogtreecommitdiff
path: root/protocol.c
blob: b70c70a1b89dd388b3f8bacb5db4b88e7893cd09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <sys/types.h>
#include <sys/socket.h>

#include "protocol.h"

/*
    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;      
    timeout.tv_sec = 0;
    timeout.tv_usec = TCP_TIMEOUT;
    // if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
    //             sizeof timeout) < 0)
    //     perror("setsockopt failed\n");

    int total = 0;        // how many bytes we've sent
    int bytesleft = *len; // how many we have left to send
    int n;

    // ensure we don't send more than MAX_PACKET_SIZE bytes
    size_t max_send = bytesleft >= MAX_PACKET_SIZE ? MAX_PACKET_SIZE : bytesleft;
    while(total < *len) {
        n = send(sock, buf+total, max_send, 0);
        if (n == -1) { break; }
        total += n;
        bytesleft -= n;
    }

    *len = total; // return number actually sent here

    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
    struct timeval timeout;      
    timeout.tv_sec = 0;
    timeout.tv_usec = TCP_TIMEOUT;
    if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
                sizeof timeout) < 0)
        perror("setsockopt failed\n");

    // printf("start: %ld\n", start);
    int total = 0;        // how many bytes we've sent
    int bytesleft = *len; // how many we have left to send
    int n;

    while(total < *len) {
        n = recv(sock, buf+total, bytesleft, 0);
        // start = time(NULL);
        if (n == -1) { break; }
        total += n;
        bytesleft -= n;
    }

    timeout.tv_sec = 0;
    timeout.tv_usec = 0;
    if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
                sizeof timeout) < 0)
        perror("setsockopt failed\n");


    *len = total; // return number actually sent here

    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;
    tv.tv_sec = 0;
    tv.tv_usec = TCP_TIMEOUT;
    if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
        perror("setsockopt (in apply_timeout)");
        return -1;
    }

    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
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
        perror("setsockopt (in remove_timeout)");
        return -1;
    }

    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);
}