socket.c 11 KB
Newer Older
1
2
3
4
5
6
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
7
#include <sys/poll.h>
8
9
10
11
12
13
14
#include "../defines.h"
#include "packet.h"
#include "../util/dbg.h"
#include "../util/common.h"
#include "processes/feedback_receiver.h"
#include "processes/data_transmitter.h"
#include "processes/data_receiver.h"
15
#include "socket.h"
16
#include "block.h"
17
#include "receiver.h"
18
#include "clock.h"
19

20
21
PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
    PrrtSocket *sock_ptr = (PrrtSocket*) calloc(1, sizeof(PrrtSocket));
22
    check_mem(sock_ptr);
23

24
    sock_ptr->is_sender = is_sender;
25

26
27
    sock_ptr->sequenceNumberSource = 1;
    sock_ptr->sequenceNumberRedundancy = 1;
28

29
    PrrtChannelStateInformation_init(&sock_ptr->csi);
30

31
32
    sock_ptr->dataStore = NULL;

33
34
    check(sock_ptr->dataSocketFd = socket(AF_INET, SOCK_DGRAM, 0), "Cannot create data socket.")
    check(sock_ptr->feedbackSocketFd = socket(AF_INET, SOCK_DGRAM, 0), "Cannot create feedback socket.");
35

36
    pthread_mutexattr_t attr;
37
38
39
    check(pthread_mutexattr_init(&attr) == 0, "Mutex attr init failed.");
    check(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0, "Setting type failed.");
    check(pthread_mutex_init(&sock_ptr->closingMutex, &attr) == 0, "Mutex init failed.");
40

41
42
43
44
45
46
47
48
49
50
    // Bind Data Socket
    struct sockaddr_in address;
    memset((char *) &address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port = htons(port);

    check(bind(sock_ptr->dataSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
          "Cannot bind data socket.");

51
    if (is_sender) {
52
53
        // Bind Feedback Socket
        struct sockaddr_in address;
54
        memset((char *) &address, 0, sizeof(address));
55
56
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = htonl(INADDR_ANY);
57
58
        check(port <= 65534, "Port %d cannot be bound to.", port);
        address.sin_port = htons((uint16_t) (port + 1));
59

60
61
        check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
              "Cannot bind feedback socket.");
62

63
64
        check(pthread_mutex_init(&sock_ptr->outQueueFilledMutex, NULL) == 0, "Mutex init failed.");
        check(pthread_cond_init(&sock_ptr->outQueueFilledCv, NULL) == 0, "Cond init failed.");
65
        sock_ptr->outQueue = List_create();
66

67
68
69
70
        check(pthread_create(&sock_ptr->receiveFeedbackThread, NULL, receive_feedback_loop, (void *) sock_ptr) ==
              EXIT_SUCCESS, "Cannot create receive feedback thread.");
        check(pthread_create(&sock_ptr->sendThread, NULL, send_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
              "Cannot create send thread.");
71
72

        sock_ptr->receivers = List_create();
73
    } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
74
        sock_ptr->forwardPacketTable = calloc(1, sizeof(PrrtForwardPacketTable));
75
        check_mem(sock_ptr->forwardPacketTable)
76
        check(PrrtForwardPacketTable_create(sock_ptr->forwardPacketTable) == 0, "Creating table failed.");
77

78
79
        check(pthread_mutex_init(&sock_ptr->inQueueFilledMutex, NULL) == 0, "Mutex init failed.");
        check(pthread_cond_init(&sock_ptr->inQueueFilledCv, NULL) == 0, "Cond init failed.");
80
81
        sock_ptr->inQueue = List_create();

82
83
        check(pthread_create(&sock_ptr->receiveDataThread, NULL, receive_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
              "Cannot create data receiving thread.");
84
85
    }

86
    return sock_ptr;
87
88

    error:
89
        PrrtSocket_close(sock_ptr);
90
        return NULL;
91
92
}

93
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
94
    PrrtReceiver *recv = PrrtReceiver_create(host, port);
95
    List_push(sock_ptr->receivers, recv);
96
97
98
    return 0;
}

99
int PrrtSocket_send(PrrtSocket *sock_ptr, const uint8_t *data, const size_t data_len) {
100
    check(sock_ptr->is_sender, "Cannot send on receiver socket.")
101
    check(pthread_mutex_lock(&sock_ptr->outQueueFilledMutex) == 0, "Lock failed.");
102

103
    PrrtPacket *packet = PrrtPacket_create_data_packet(5, data, (uint32_t) data_len, 0);
104

105
    List_push(sock_ptr->outQueue, packet);
106
107
    check(pthread_cond_signal(&sock_ptr->outQueueFilledCv) == 0, "Signal failed.");
    check(pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex) == 0, "Unlock failed");
108

109
    return 0;
110
    error:
111
        PERROR("There was a failure while sending from socket.%s", "");
112
        return -1;
113
114
}

115
int32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void *buf_ptr) {
116
    check(sock_ptr->is_sender == false, "Cannot receive on sender socket.")
117
    while (1) {
118
        check(pthread_mutex_lock(&sock_ptr->inQueueFilledMutex) == 0, "Lock failed.");
119
        while (List_count(sock_ptr->inQueue) == 0) {
120
            check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
121
            if (sock_ptr->closing) {
122
123
                check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
                check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed.");
124
125
                return -1;
            }
126
127
            check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
            check(pthread_cond_wait(&sock_ptr->inQueueFilledCv, &sock_ptr->inQueueFilledMutex) == 0, "Wait failed.");
128
        }
129
        PrrtPacket *packet = List_shift(sock_ptr->inQueue);
130

131
        uint32_t len = (uint32_t) (packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
132
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet, PRRT_PACKET_DATA_HEADER_SIZE);
133
134
        PrrtPacket_destroy(packet);

135
        check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed.");
136
        return len;
137
    }
138
    error:
139
        PERROR("There was a failure while receiving from socket.%s", "");
140
        return -1;
141
142
}

143
int PrrtSocket_interrupt(PrrtSocket *sock_ptr) {
144
    check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
145
    sock_ptr->closing = true;
146
    check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
147

148
    void **res = NULL;
149
    if (sock_ptr->sendThread != 0) {
150
151
152
        check(pthread_mutex_lock(&sock_ptr->outQueueFilledMutex) == 0, "Lock failed.");
        check(pthread_cond_broadcast(&sock_ptr->outQueueFilledCv) == 0, "Broadcast failed.");
        check(pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex) == 0, "Unlock failed.");
153

154
        check(pthread_join(sock_ptr->sendThread, res) == 0, "Join failed.");
155
156
157
        sock_ptr->sendThread = 0;
    }

158
    if (sock_ptr->receiveDataThread != 0) {
159
160
161
        check(pthread_mutex_lock(&sock_ptr->inQueueFilledMutex) == 0, "Lock failed.");
        check(pthread_cond_broadcast(&sock_ptr->inQueueFilledCv) == 0, "Broadcast failed");
        check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed");
162

163
164
        check(pthread_cancel(sock_ptr->receiveDataThread) == 0, "Cancel failed.");
        check(pthread_join(sock_ptr->receiveDataThread, res) == 0, "Join failed.");
165
166
167
168

        sock_ptr->receiveDataThread = 0;
    }

169
    if (sock_ptr->receiveFeedbackThread != 0) {
170
171
        check(pthread_cancel(sock_ptr->receiveFeedbackThread) == 0, "Cancel failed.");
        check(pthread_join(sock_ptr->receiveFeedbackThread, res) == 0, "Join failed.");
172
173
174

        sock_ptr->receiveFeedbackThread = 0;
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
175
    return EXIT_SUCCESS;
176
177
178
179

    error:
        PERROR("Interruping socket failed.%s","");
        return EXIT_FAILURE;
180
181
182
}

int PrrtSocket_close(PrrtSocket *sock_ptr) {
183
    debug("Closing socket.");
184
    check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
185
    if (!sock_ptr->closing) {
186
187
        check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
        check(PrrtSocket_interrupt(sock_ptr) == EXIT_SUCCESS, "Interrupt failed.");
188
    } else {
189
        check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
190
191
    }

192
193
194
195
196
    if (sock_ptr->dataStore != NULL) {
        List *dataList = List_create();
        BPTree_get_range(sock_ptr->dataStore, dataList, 0, SEQNO_SPACE - 1);
        while (List_count(dataList) > 0) {
            PrrtPacket *packet = List_shift(dataList);
197
198
199
200
201
            PrrtPacket_destroy(packet);
        }

        List_destroy(dataList);

202
203
204
        sock_ptr->dataStore = BPTree_destroy(sock_ptr->dataStore);
    }

205
206
207
208
209
    if (sock_ptr->blockStore != NULL) {
        List *blockList = List_create();
        BPTree_get_range(sock_ptr->blockStore, blockList, 0, SEQNO_SPACE - 1);
        while (List_count(blockList) > 0) {
            PrrtBlock *block = List_shift(blockList);
Andreas Schmidt's avatar
Andreas Schmidt committed
210
            PrrtBlock_destroy(block);
211
212
213
214
        }

        List_destroy(blockList);

215
        sock_ptr->blockStore = BPTree_destroy(sock_ptr->blockStore);
216
217
    }

218
    if (sock_ptr->receivers != NULL) {
219
        while (List_count(sock_ptr->receivers) > 0) {
Andreas Schmidt's avatar
Andreas Schmidt committed
220
            PrrtReceiver *recv = List_shift(sock_ptr->receivers);
221
            PrrtReceiver_destroy(recv);
222
223
        }
        List_destroy(sock_ptr->receivers);
224
        sock_ptr->receivers = NULL;
225
226
    }

227
    if (sock_ptr->outQueue != NULL) {
228
229
        check(pthread_mutex_destroy(&sock_ptr->outQueueFilledMutex) == 0, "Mutex destroy failed.");
        check(pthread_cond_destroy(&sock_ptr->outQueueFilledCv) == 0, "Cond destroy failed.");
230
        List_destroy(sock_ptr->outQueue);
231
        sock_ptr->outQueue = NULL;
232
233
    }

234
    if (sock_ptr->inQueue != NULL) {
235
236
        check(pthread_mutex_destroy(&sock_ptr->inQueueFilledMutex) == 0, "Mutex destroy failed.");
        check(pthread_cond_destroy(&sock_ptr->inQueueFilledCv) == 0, "Cond destroy failed.");
237
        List_destroy(sock_ptr->inQueue);
238
239
240
        sock_ptr->inQueue = NULL;
    }

241
    if (sock_ptr->forwardPacketTable != NULL) {
242
        check(PrrtForwardPacketTable_destroy(sock_ptr->forwardPacketTable), "Destroy failed.");
243
        sock_ptr->forwardPacketTable = NULL;
244
    }
245

246
    check(pthread_mutex_destroy(&sock_ptr->closingMutex) == 0, "Mutex destroy failed.");
247

248
249
    close(sock_ptr->dataSocketFd);
    close(sock_ptr->feedbackSocketFd);
250
    debug("Socket closed.");
251
    return 0;
252
253
254
255

    error:
        PERROR("Closing socket failed.%s", "");
        return -1;
256
257
}

258
PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *prrtSocket, const size_t length)
259
260
{
    char bufin[MAX_PAYLOAD_LENGTH];
261
262
263
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
264
265
266

    struct pollfd fds;
    int timeout_msecs = 1000;
267
    fds.fd = prrtSocket->feedbackSocketFd;
268
269
270
271
272
273
274
    fds.events = POLLIN;

    n = poll(&fds, 1, timeout_msecs);
    check(n >= 0, "Select failed.")
    if (n == 0) {
        return NULL;
    }
275
    uint32_t receiveTime = PrrtClock_get_current_time();
276

277
    n = recvfrom(prrtSocket->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
278
    check(n >= 0, "Receiving feedback failed.");
279

280
281
282
    PrrtPacket *prrtPacket = calloc(1, sizeof(PrrtPacket));
    check_mem(prrtPacket);
    PrrtPacket_decode(bufin, (uint16_t) n, prrtPacket);
283

284
285
    uint32_t forwardTripTimestamp = ((PrrtPacketFeedbackPayload*) prrtPacket->payload)->forward_trip_timestamp;
    PrrtChannelStateInformation_update_rtt(&prrtSocket->csi, receiveTime - forwardTripTimestamp);
286

287
    return prrtPacket;
288
289

    error:
290
    return NULL;
291
}