socket.c 8.93 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 = calloc(1, sizeof(PrrtPacket));
99
    check_mem(packet);
100
    PrrtPacket_create_data_packet(packet, 5, data, (uint32_t) data_len, sock_ptr->sequenceNumberSource++);
101

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

106
    return 0;
107
108

    error:
109
    return -1;
110
111
}

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

127
        PrrtPacket *packet = List_shift(sock_ptr->inQueue);
128

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

132
133
        PrrtPacket_destroy(packet);

134
        pthread_mutex_unlock(&t);
135
        return len;
136
137
138
    }
}

139
int PrrtSocket_interrupt(PrrtSocket *sock_ptr) {
140
141
142
143
    pthread_mutex_lock(&sock_ptr->closingMutex);
    sock_ptr->closing = TRUE;
    pthread_mutex_unlock(&sock_ptr->closingMutex);

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

154
    if (sock_ptr->receiveDataThread != 0) {
155
        pthread_mutex_lock(&sock_ptr->inQueueFilledMutex);
156
        pthread_cond_signal(&sock_ptr->inQueueFilledCv);
157
158
        pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex);

159
        pthread_cancel(sock_ptr->receiveDataThread);
160
161
162
163
164
        pthread_join(sock_ptr->receiveDataThread, res);

        sock_ptr->receiveDataThread = 0;
    }

165
    if (sock_ptr->receiveFeedbackThread != 0) {
166
167
168
169
170
        pthread_cancel(sock_ptr->receiveFeedbackThread);
        pthread_join(sock_ptr->receiveFeedbackThread, res);

        sock_ptr->receiveFeedbackThread = 0;
    }
171
    debug("Interrupted all threads.");
172
173
174
}

int PrrtSocket_close(PrrtSocket *sock_ptr) {
175
    debug("Closing socket.");
176
    pthread_mutex_lock(&sock_ptr->closingMutex);
177
178
    if (!sock_ptr->closing) {
        pthread_mutex_unlock(&sock_ptr->closingMutex);
179
        PrrtSocket_interrupt(sock_ptr);
180
181
    } else {
        pthread_mutex_unlock(&sock_ptr->closingMutex);
182
183
    }

184
185
186
187
188
    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);
189
190
191
192
193
            PrrtPacket_destroy(packet);
        }

        List_destroy(dataList);

194
195
196
        sock_ptr->dataStore = BPTree_destroy(sock_ptr->dataStore);
    }

197
198
199
200
201
    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);
202
203
204
205
206
            PrrtBlock_free(block);
        }

        List_destroy(blockList);

207
        sock_ptr->blockStore = BPTree_destroy(sock_ptr->blockStore);
208
209
    }

210
    if (sock_ptr->receivers != NULL) {
211
        while (List_count(sock_ptr->receivers) > 0) {
212
213
214
            free(List_shift(sock_ptr->receivers));
        }
        List_destroy(sock_ptr->receivers);
215
        sock_ptr->receivers = NULL;
216
217
    }

218
    if (sock_ptr->outQueue != NULL) {
219
220
221
        pthread_mutex_destroy(&sock_ptr->outQueueFilledMutex);
        pthread_cond_destroy(&sock_ptr->outQueueFilledCv);
        List_destroy(sock_ptr->outQueue);
222
        sock_ptr->outQueue = NULL;
223
224
    }

225
    if (sock_ptr->inQueue != NULL) {
226
        pthread_mutex_destroy(&sock_ptr->inQueueFilledMutex);
227
        pthread_cond_destroy(&sock_ptr->inQueueFilledCv);
228
        List_destroy(sock_ptr->inQueue);
229
230
231
        sock_ptr->inQueue = NULL;
    }

232
    if (sock_ptr->forwardPacketTable != NULL) {
233
234
        free(sock_ptr->forwardPacketTable);
        sock_ptr->forwardPacketTable = NULL;
235
    }
236

237
238
    pthread_mutex_destroy(&sock_ptr->closingMutex);

239
240
    close(sock_ptr->dataSocketFd);
    close(sock_ptr->feedbackSocketFd);
241
    debug("Socket closed.");
242
243
244
    return 0;
}

245
PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, const size_t length) {
246
247
248
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
249
250
251
252
253
254
255
256
257
258
259
260

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

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

264
265
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
266
    debug("Received feedback %s:%d", remote_host, remote_port);
267

Andreas Schmidt's avatar
Andreas Schmidt committed
268
    PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
269
    check_mem(packet_ptr);
270
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
271
    return packet_ptr;
272
273

    error:
274
    return NULL;
275
}