socket.c 8.84 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>
Andreas Schmidt's avatar
Andreas Schmidt committed
7
8
9
10
11
12
13
#include <src/defines.h>
#include <src/prrt/packet.h>
#include <src/prrt/socket.h>
#include <src/prrt/processes/feedback_receiver.h>
#include <src/prrt/processes/data_transmitter.h>
#include <src/prrt/processes/data_receiver.h>
#include <src/util/dbg.h>
14
#include <sys/poll.h>
15
#include "socket.h"
16
#include "block.h"
17

18

19

20
int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender) {
21
22
    sock_ptr->sequenceNumberSource = 1;
    sock_ptr->sequenceNumberRedundancy = 1;
23

24
25
    sock_ptr->dataStore = NULL;

26
27
    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.");
28

29
30
31
32
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&sock_ptr->closingMutex, &attr);
33

34
35
36
37
38
39
40
41
42
43
    // 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.");

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

53
54
        check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
              "Cannot bind feedback socket.");
55

56
57
58
        pthread_mutex_init(&sock_ptr->outQueueFilledMutex, NULL);
        pthread_cond_init(&sock_ptr->outQueueFilledCv, NULL);
        sock_ptr->outQueue = List_create();
59

60
61
62
63
        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.");
64
65

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

        pthread_mutex_init(&sock_ptr->inQueueFilledMutex, NULL);
72
        pthread_cond_init(&sock_ptr->inQueueFilledCv, NULL);
73
74
        sock_ptr->inQueue = List_create();

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

79
80
81
    return EXIT_SUCCESS;

    error:
82
83
        PrrtSocket_close(sock_ptr);
        return EXIT_FAILURE;
84
85
}

86
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
87
    PrrtReceiver *recv = calloc(1, sizeof(PrrtReceiver));
88
89
    recv->host_name = host;
    recv->port = port;
90

91
    List_push(sock_ptr->receivers, recv);
92
93
94
    return 0;
}

95
int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, const size_t data_len) {
96
    pthread_mutex_lock(&sock_ptr->outQueueFilledMutex);
97

Andreas Schmidt's avatar
Andreas Schmidt committed
98
    PrrtPacket *packet = PrrtPacket_create_data_packet(5, data, (uint32_t) data_len, sock_ptr->sequenceNumberSource++);
99

100
101
102
    List_push(sock_ptr->outQueue, packet);
    pthread_cond_signal(&sock_ptr->outQueueFilledCv);
    pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex);
103

104
    return 0;
105
106
}

107
int32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void *buf_ptr) {
108
    pthread_mutex_t t = sock_ptr->inQueueFilledMutex;
109
    while (1) {
110
        pthread_mutex_lock(&t);
111
        while (List_count(sock_ptr->inQueue) == 0) {
112
            pthread_mutex_lock(&sock_ptr->closingMutex);
113
            if (sock_ptr->closing) {
114
115
116
117
118
                pthread_mutex_unlock(&sock_ptr->closingMutex);
                pthread_mutex_unlock(&t);
                return -1;
            }
            pthread_mutex_unlock(&sock_ptr->closingMutex);
119
            pthread_cond_wait(&sock_ptr->inQueueFilledCv, &t);
120
        }
121

122
        PrrtPacket *packet = List_shift(sock_ptr->inQueue);
123

124
        uint32_t len = packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE;
125
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet, PRRT_PACKET_DATA_HEADER_SIZE);
126

127
128
        PrrtPacket_destroy(packet);

129
        pthread_mutex_unlock(&t);
130
        return len;
131
132
133
    }
}

134
int PrrtSocket_interrupt(PrrtSocket *sock_ptr) {
135
136
137
138
    pthread_mutex_lock(&sock_ptr->closingMutex);
    sock_ptr->closing = TRUE;
    pthread_mutex_unlock(&sock_ptr->closingMutex);

139
    void **res = NULL;
140
    if (sock_ptr->sendThread != 0) {
141
142
143
144
145
146
147
148
        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;
    }

149
    if (sock_ptr->receiveDataThread != 0) {
150
        pthread_mutex_lock(&sock_ptr->inQueueFilledMutex);
151
        pthread_cond_signal(&sock_ptr->inQueueFilledCv);
152
153
        pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex);

154
        pthread_cancel(sock_ptr->receiveDataThread);
155
156
157
158
159
        pthread_join(sock_ptr->receiveDataThread, res);

        sock_ptr->receiveDataThread = 0;
    }

160
    if (sock_ptr->receiveFeedbackThread != 0) {
161
162
163
164
165
        pthread_cancel(sock_ptr->receiveFeedbackThread);
        pthread_join(sock_ptr->receiveFeedbackThread, res);

        sock_ptr->receiveFeedbackThread = 0;
    }
166
    debug("Interrupted all threads.");
167
168
169
}

int PrrtSocket_close(PrrtSocket *sock_ptr) {
170
    debug("Closing socket.");
171
    pthread_mutex_lock(&sock_ptr->closingMutex);
172
173
    if (!sock_ptr->closing) {
        pthread_mutex_unlock(&sock_ptr->closingMutex);
174
        PrrtSocket_interrupt(sock_ptr);
175
176
    } else {
        pthread_mutex_unlock(&sock_ptr->closingMutex);
177
178
    }

179
180
181
182
183
    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);
184
185
186
187
188
            PrrtPacket_destroy(packet);
        }

        List_destroy(dataList);

189
190
191
        sock_ptr->dataStore = BPTree_destroy(sock_ptr->dataStore);
    }

192
193
194
195
196
    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
197
            PrrtBlock_destroy(block);
198
199
200
201
        }

        List_destroy(blockList);

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

205
    if (sock_ptr->receivers != NULL) {
206
        while (List_count(sock_ptr->receivers) > 0) {
207
208
209
            free(List_shift(sock_ptr->receivers));
        }
        List_destroy(sock_ptr->receivers);
210
        sock_ptr->receivers = NULL;
211
212
    }

213
    if (sock_ptr->outQueue != NULL) {
214
215
216
        pthread_mutex_destroy(&sock_ptr->outQueueFilledMutex);
        pthread_cond_destroy(&sock_ptr->outQueueFilledCv);
        List_destroy(sock_ptr->outQueue);
217
        sock_ptr->outQueue = NULL;
218
219
    }

220
    if (sock_ptr->inQueue != NULL) {
221
        pthread_mutex_destroy(&sock_ptr->inQueueFilledMutex);
222
        pthread_cond_destroy(&sock_ptr->inQueueFilledCv);
223
        List_destroy(sock_ptr->inQueue);
224
225
226
        sock_ptr->inQueue = NULL;
    }

227
    if (sock_ptr->forwardPacketTable != NULL) {
228
229
        free(sock_ptr->forwardPacketTable);
        sock_ptr->forwardPacketTable = NULL;
230
    }
231

232
233
    pthread_mutex_destroy(&sock_ptr->closingMutex);

234
235
    close(sock_ptr->dataSocketFd);
    close(sock_ptr->feedbackSocketFd);
236
    debug("Socket closed.");
237
238
239
    return 0;
}

240
PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, const size_t length) {
241
242
243
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
244
245
246
247
248
249
250
251
252
253
254
255

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

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

259
260
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
261
    debug("Received feedback %s:%d", remote_host, remote_port);
262

Andreas Schmidt's avatar
Andreas Schmidt committed
263
    PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
264
    check_mem(packet_ptr);
265
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
266
    return packet_ptr;
267
268

    error:
269
    return NULL;
270
}