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

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

22
    sock_ptr->is_sender = is_sender;
23

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

27
28
    sock_ptr->dataStore = NULL;

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

32
33
34
35
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&sock_ptr->closingMutex, &attr);
36

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

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

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

59
60
61
        pthread_mutex_init(&sock_ptr->outQueueFilledMutex, NULL);
        pthread_cond_init(&sock_ptr->outQueueFilledCv, NULL);
        sock_ptr->outQueue = List_create();
62

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

        sock_ptr->receivers = List_create();
69
    } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
70
        sock_ptr->forwardPacketTable = calloc(1, sizeof(PrrtForwardPacketTable));
71
        check_mem(sock_ptr->forwardPacketTable)
72
        PrrtForwardPacketTable_create(sock_ptr->forwardPacketTable);
73
74

        pthread_mutex_init(&sock_ptr->inQueueFilledMutex, NULL);
75
        pthread_cond_init(&sock_ptr->inQueueFilledCv, NULL);
76
77
        sock_ptr->inQueue = List_create();

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

82
    return sock_ptr;
83
84

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

89
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
90
    PrrtReceiver *recv = calloc(1, sizeof(PrrtReceiver));
Andreas Schmidt's avatar
Andreas Schmidt committed
91
92
93
    uint32_t hostLength = (uint32_t) strlen(host);
    recv->host_name = calloc(1, hostLength + 1);
    memcpy((void *) recv->host_name, host, hostLength);
94
    recv->port = port;
95

96
    List_push(sock_ptr->receivers, recv);
97
98
99
    return 0;
}

Andreas Schmidt's avatar
Andreas Schmidt committed
100
int PrrtSocket_send(PrrtSocket *sock_ptr, uint8_t *data, const size_t data_len) {
101
    check(sock_ptr->is_sender, "Cannot send on receiver socket.")
102
    pthread_mutex_lock(&sock_ptr->outQueueFilledMutex);
103

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

106
107
108
    List_push(sock_ptr->outQueue, packet);
    pthread_cond_signal(&sock_ptr->outQueueFilledCv);
    pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex);
109

110
    return 0;
111
112
    error:
        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.")
Andreas Schmidt's avatar
Andreas Schmidt committed
117
118
    pthread_mutex_t filledMutex = sock_ptr->inQueueFilledMutex;
    pthread_mutex_t closingMutex = sock_ptr->closingMutex;
119
    while (1) {
Andreas Schmidt's avatar
Andreas Schmidt committed
120
        pthread_mutex_lock(&filledMutex);
121
        while (List_count(sock_ptr->inQueue) == 0) {
Andreas Schmidt's avatar
Andreas Schmidt committed
122
            pthread_mutex_lock(&closingMutex);
123
            if (sock_ptr->closing) {
Andreas Schmidt's avatar
Andreas Schmidt committed
124
125
                pthread_mutex_unlock(&closingMutex);
                pthread_mutex_unlock(&filledMutex);
126
127
                return -1;
            }
Andreas Schmidt's avatar
Andreas Schmidt committed
128
129
            pthread_mutex_unlock(&closingMutex);
            pthread_cond_wait(&sock_ptr->inQueueFilledCv, &filledMutex);
130
        }
131
        PrrtPacket *packet = List_shift(sock_ptr->inQueue);
132

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

Andreas Schmidt's avatar
Andreas Schmidt committed
137
        pthread_mutex_unlock(&filledMutex);
138
        return len;
139
    }
140
    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
141
        PERROR("Wrong socket type.%s", "");
142
        return -1;
143
144
}

145
int PrrtSocket_interrupt(PrrtSocket *sock_ptr) {
146
    pthread_mutex_lock(&sock_ptr->closingMutex);
147
    sock_ptr->closing = true;
148
149
    pthread_mutex_unlock(&sock_ptr->closingMutex);

150
    void **res = NULL;
151
    if (sock_ptr->sendThread != 0) {
152
153
154
155
156
157
158
159
        pthread_mutex_lock(&sock_ptr->outQueueFilledMutex);
        pthread_cond_signal(&sock_ptr->outQueueFilledCv);
        pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex);

        pthread_join(sock_ptr->sendThread, res);
        sock_ptr->sendThread = 0;
    }

160
    if (sock_ptr->receiveDataThread != 0) {
161
        pthread_mutex_lock(&sock_ptr->inQueueFilledMutex);
162
        pthread_cond_signal(&sock_ptr->inQueueFilledCv);
163
164
        pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex);

165
        pthread_cancel(sock_ptr->receiveDataThread);
166
167
168
169
170
        pthread_join(sock_ptr->receiveDataThread, res);

        sock_ptr->receiveDataThread = 0;
    }

171
    if (sock_ptr->receiveFeedbackThread != 0) {
172
173
174
175
176
        pthread_cancel(sock_ptr->receiveFeedbackThread);
        pthread_join(sock_ptr->receiveFeedbackThread, res);

        sock_ptr->receiveFeedbackThread = 0;
    }
177
    debug("Interrupted all threads.");
Andreas Schmidt's avatar
Andreas Schmidt committed
178
    return EXIT_SUCCESS;
179
180
181
}

int PrrtSocket_close(PrrtSocket *sock_ptr) {
182
    debug("Closing socket.");
183
    pthread_mutex_lock(&sock_ptr->closingMutex);
184
185
    if (!sock_ptr->closing) {
        pthread_mutex_unlock(&sock_ptr->closingMutex);
186
        PrrtSocket_interrupt(sock_ptr);
187
188
    } else {
        pthread_mutex_unlock(&sock_ptr->closingMutex);
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) {
219
220
221
            free(List_shift(sock_ptr->receivers));
        }
        List_destroy(sock_ptr->receivers);
222
        sock_ptr->receivers = NULL;
223
224
    }

225
    if (sock_ptr->outQueue != NULL) {
226
227
228
        pthread_mutex_destroy(&sock_ptr->outQueueFilledMutex);
        pthread_cond_destroy(&sock_ptr->outQueueFilledCv);
        List_destroy(sock_ptr->outQueue);
229
        sock_ptr->outQueue = NULL;
230
231
    }

232
    if (sock_ptr->inQueue != NULL) {
233
        pthread_mutex_destroy(&sock_ptr->inQueueFilledMutex);
234
        pthread_cond_destroy(&sock_ptr->inQueueFilledCv);
235
        List_destroy(sock_ptr->inQueue);
236
237
238
        sock_ptr->inQueue = NULL;
    }

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

244
245
    pthread_mutex_destroy(&sock_ptr->closingMutex);

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

252
PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, const size_t length) {
253
254
255
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
256
257
258
259
260
261
262
263
264
265
266
267

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

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

271
272
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
273
    debug("Received feedback %s:%d", remote_host, remote_port);
274

Andreas Schmidt's avatar
Andreas Schmidt committed
275
    PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
276
    check_mem(packet_ptr);
277
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
278
    return packet_ptr;
279
280

    error:
281
    return NULL;
282
}