socket.c 10.7 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

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

23
    sock_ptr->is_sender = is_sender;
24

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

28
29
    sock_ptr->dataStore = NULL;

30
31
    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.");
32

33
    pthread_mutexattr_t attr;
34
35
36
    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.");
37

38
39
40
41
42
43
44
45
46
47
    // 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.");

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

57
58
        check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
              "Cannot bind feedback socket.");
59

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

64
65
66
67
        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.");
68
69

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

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

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

83
    return sock_ptr;
84
85

    error:
86
        PrrtSocket_close(sock_ptr);
87
        return NULL;
88
89
}

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

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

100
    PrrtPacket *packet = PrrtPacket_create_data_packet(5, data, (uint32_t) data_len, 0);
101

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

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

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

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

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

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

147
    void **res = NULL;
148
    if (sock_ptr->sendThread != 0) {
149
150
151
        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.");
152

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

157
    if (sock_ptr->receiveDataThread != 0) {
158
159
160
        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");
161

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

        sock_ptr->receiveDataThread = 0;
    }

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

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

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

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

191
192
193
194
195
    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);
196
197
198
199
200
            PrrtPacket_destroy(packet);
        }

        List_destroy(dataList);

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

204
205
206
207
208
    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
209
            PrrtBlock_destroy(block);
210
211
212
213
        }

        List_destroy(blockList);

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

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

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

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

240
    if (sock_ptr->forwardPacketTable != NULL) {
241
242
        free(sock_ptr->forwardPacketTable);
        sock_ptr->forwardPacketTable = NULL;
243
    }
244

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

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

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

257
PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, const size_t length) {
258
259
260
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
261
262
263
264
265
266
267
268
269
270
271
272

    struct pollfd fds;
    int timeout_msecs = 1000;
    fds.fd = sock_ptr->feedbackSocketFd;
    fds.events = POLLIN;

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

273
274
    n = recvfrom(sock_ptr->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
    check(n >= 0, "Receiving feedback failed.");
275

276
277
    //uint16_t remote_port = ntohs(remote.sin_port);
    //char *remote_host = inet_ntoa(remote.sin_addr);
Andreas Schmidt's avatar
Andreas Schmidt committed
278
    //debug("Received feedback %s:%d", remote_host, remote_port);
279

Andreas Schmidt's avatar
Andreas Schmidt committed
280
    PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
281
    check_mem(packet_ptr);
282
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
283
    return packet_ptr;
284
285

    error:
286
    return NULL;
287
}