dataReceiver.c 12 KB
Newer Older
1
#include <netdb.h>
Andreas Schmidt's avatar
Andreas Schmidt committed
2
#include <stdio.h>
3
#include <arpa/inet.h>
4
5
6
#include "../../defines.h"
#include "../../util/dbg.h"
#include "../../util/common.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
7
#include "../types/lossStatistics.h"
8
#include "../block.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
9
10
#include "../clock.h"
#include "../socket.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
11
#include "dataReceiver.h"
12

Andreas Schmidt's avatar
Andreas Schmidt committed
13
14
static void
retrieve_data_blocks(PrrtSocket *sock_ptr, prrtSequenceNumber_t base_seqno, uint8_t k, const PrrtBlock *block) {
15
    List *res = List_create();
16

17
18
    PrrtDataPacketStore_remove_range(sock_ptr->dataPacketStore, res, base_seqno,
                                     (prrtSequenceNumber_t) (base_seqno + k - 1));
19

20
21
    LIST_FOREACH(res, first, next, cur) {
        PrrtPacket *packetPtr = cur->value;
22
        check(PrrtBlock_insert_data_packet((PrrtBlock *) block, packetPtr), "Insert failed!")
23
    }
24
25
    List_destroy(res);
    return;
26
    error:
27
    PERROR("Insert failed.");
28
    List_destroy(res);
29
30
}

Andreas Schmidt's avatar
Andreas Schmidt committed
31
32
static void decode_block(PrrtSocket *sock_ptr, PrrtBlock *block) {
    if (block != NULL && PrrtBlock_decode_ready(block)) {
33
        check(PrrtBlock_decode(block), "Decoding failed");
34

Andreas Schmidt's avatar
Andreas Schmidt committed
35
        while (List_count(block->dataPackets) > 0) {
36
            PrrtPacket *pkt = List_shift(block->dataPackets);
37
            if (PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable, pkt->sequenceNumber)) {
38
                PrrtPacketDeliveryStore_insert(sock_ptr->packetDeliveryStore, pkt);
39
40
            } else {
                PrrtPacket_destroy(pkt);
41
42
            }
        }
43

44
        PrrtRepairBlockStore_delete(sock_ptr->repairBlockStore, block->baseSequenceNumber);
Andreas Schmidt's avatar
Andreas Schmidt committed
45
        PrrtBlock_destroy(block);
46
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
47
48

    return;
49
50
    error:
    PERROR("Decoding failed.%s", "")
51
}
52

53
static bool send_feedback(PrrtSocket *sock_ptr, struct sockaddr_in remote) {
54
55
56
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);

57
58
59
60
61
62
63
64
65
    struct sockaddr_in targetaddr;
    memset((char *) &targetaddr, 0, sizeof(targetaddr));
    targetaddr.sin_family = AF_INET;
    targetaddr.sin_port = htons((uint16_t) (remote_port + 1));

    struct hostent *hp;
    hp = gethostbyname(remote_host);
    memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);

66
67
68
    prrtTimestamp_t forwardTripTime = (uint32_t) (((int64_t) PrrtClock_get_current_time_us()) +
                                                  (sock_ptr->lastSentTimestamp - sock_ptr->lastReceivedTimestamp));

Andreas Schmidt's avatar
Andreas Schmidt committed
69
70
    PrrtLossStatistics stats = sock_ptr->lossStatistics;

71
72
73
74
75
    int group_RTT = 0; // TODO: To be determined.
    PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, sock_ptr->sequenceNumberFeedback++, group_RTT,
                                                                     stats.gapLength, stats.gapCount, stats.burstLength,
                                                                     stats.burstCount, forwardTripTime,
                                                                     stats.erasureCount, stats.packetCount);
76
    prrtPacketLength_t length = PrrtPacket_size(feedback_pkt_ptr);
77
    void *buf = calloc(1, length);
78
    check_mem(buf);
79

80
    check(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr), "Buffer for encoding feedback is too small");
81

82
83
    check(sendto(sock_ptr->feedbackSocketFd, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) ==
          length, "Sending feedback failed.");
84
    free(buf);
85

86
87
    PrrtPacket_destroy(feedback_pkt_ptr);

88
    return true;
89
90

    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
91
92
    if (buf != NULL) { free(buf); }
    if (feedback_pkt_ptr != NULL) { PrrtPacket_destroy(feedback_pkt_ptr); }
93
    return false;
94
95
}

Andreas Schmidt's avatar
Andreas Schmidt committed
96
97
98
static bool is_timeout(prrtTimestamp_t now, prrtTimestamp_t to) {
    /* TODO: implement */
    return false;
Stefan Reif's avatar
Stefan Reif committed
99
}
100

Andreas Schmidt's avatar
Andreas Schmidt committed
101
static void handle_data_packet(PrrtSocket *sock_ptr, PrrtPacket *packet, struct sockaddr_in remote) {
102
103
104
    PrrtPacketDataPayload *payload = packet->payload;
    prrtTimestamp_t dataTimestamp = payload->timestamp;
    sock_ptr->lastSentTimestamp = dataTimestamp;
105
    PrrtClock_update(&sock_ptr->clock, dataTimestamp, payload->groupRTprop_us);
106

107
    PrrtPacketTimeout *packetTimeout = PrrtPacketTimeout_create(packet);
108
    check(PrrtPacketTimeoutTable_insert(sock_ptr->packetTimeoutTable, packetTimeout), "Could not insert data packet.");
109

Andreas Schmidt's avatar
Andreas Schmidt committed
110
    prrtSequenceNumber_t seqno = packet->sequenceNumber;
Andreas Schmidt's avatar
Andreas Schmidt committed
111
    PrrtReceptionTable_mark_received(sock_ptr->dataReceptionTable, seqno);
Stefan Reif's avatar
Stefan Reif committed
112

Andreas Schmidt's avatar
Andreas Schmidt committed
113
    XlapTimeStampCycle(sock_ptr, ts_data_packet, seqno, SendFeedbackStart);
114
    check(send_feedback(sock_ptr, remote), "Sending feedback failed.");
Andreas Schmidt's avatar
Andreas Schmidt committed
115
    XlapTimeStampCycle(sock_ptr, ts_data_packet, seqno, SendFeedbackEnd);
116

Stefan Reif's avatar
Stefan Reif committed
117
    prrtTimestamp_t now = PrrtClock_get_prrt_time_us(&sock_ptr->clock);
Andreas Schmidt's avatar
Andreas Schmidt committed
118
    if (is_timeout(now, payload->packetTimeout_us)) {
119
        PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable, packet->sequenceNumber);
120
121
        // TODO: note this as loss
        PrrtPacket_destroy(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
122
123
        debug(DEBUG_RECEIVER, "timeout data packet %u (%lu > %lu)", seqno, (unsigned long) now,
              (unsigned long) payload->packetTimeout_us);
124
125
    } else if (PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable,
                                                                    packet->sequenceNumber) ==
Andreas Schmidt's avatar
Andreas Schmidt committed
126
               false) {
127
128
129
130
131
132
133
        PrrtPacket_destroy(packet);
    } else {
        prrtSequenceNumber_t baseSequenceNumber = packet->sequenceNumber - packet->index;

        PrrtPacket *reference = PrrtPacket_copy(packet);

        PrrtBlock *block = PrrtRepairBlockStore_get_block(sock_ptr->repairBlockStore, baseSequenceNumber);
Andreas Schmidt's avatar
Andreas Schmidt committed
134
        if (block != NULL) {
135
136
            check(PrrtBlock_insert_data_packet(block, reference), "Inserting failed: %d, %d", baseSequenceNumber,
                  seqno);
137
138
            decode_block(sock_ptr, block);
        } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
139
            if (PrrtDataStore_insert(sock_ptr->dataPacketStore, reference) == false) {
140
141
142
143
144
                PrrtPacket_destroy(reference);
            }
        }

        // forward to application layer
Andreas Schmidt's avatar
Andreas Schmidt committed
145
        debug(DEBUG_DATARECEIVER, "forward %u", seqno);
146

147
        XlapTimeStampClock(sock_ptr, ts_data_packet, seqno, PrrtReturnPackage);
Andreas Schmidt's avatar
Andreas Schmidt committed
148
        XlapTimeStampCycle(sock_ptr, ts_data_packet, seqno, PrrtReturnPackage);
149
        PrrtPacketDeliveryStore_insert(sock_ptr->packetDeliveryStore, packet);
150
151
152
153
154
155
156
157
    }
    return;

    error:
    PERROR("Handling data packet failed%s.", "");
    return;
}

Andreas Schmidt's avatar
Andreas Schmidt committed
158
static void handle_redundancy_packet(PrrtSocket *socket, PrrtPacket *packet) {
159
160
    PrrtPacketRedundancyPayload *redundancyPayload = packet->payload;

Andreas Schmidt's avatar
Andreas Schmidt committed
161
162
    PrrtReceptionTable_mark_received(socket->redundancyReceptionTable, packet->sequenceNumber);

163
164
165
    if (!PrrtDeliveredPacketTable_test_is_block_relevant(socket->deliveredPacketTable,
                                                         redundancyPayload->baseSequenceNumber,
                                                         redundancyPayload->n)) {
166
167
168
169
        PrrtPacket_destroy(packet);
    } else {
        PrrtBlock *block = PrrtRepairBlockStore_get_block(socket->repairBlockStore,
                                                          redundancyPayload->baseSequenceNumber);
Andreas Schmidt's avatar
Andreas Schmidt committed
170
        if (block == NULL) {
Andreas Schmidt's avatar
Andreas Schmidt committed
171
            PrrtCodingParams_update(socket->codingParameters, redundancyPayload->k, redundancyPayload->n);
172

Andreas Schmidt's avatar
Andreas Schmidt committed
173
            block = PrrtBlock_create(socket->codingParameters, redundancyPayload->baseSequenceNumber);
174

175
            PrrtRepairBlockStore_insert(socket->repairBlockStore, block);
176
177
        }

178
        retrieve_data_blocks(socket, redundancyPayload->baseSequenceNumber, block->codingParams->k, block);
179

Andreas Schmidt's avatar
Andreas Schmidt committed
180
        if (PrrtBlock_insert_redundancy_packet(block, packet)) {
181
182
183
184
185
186
187
188
            decode_block(socket, block);
        } else {
            PrrtPacket_destroy(packet);
        }
    }
    return;
}

189
190
void receive_from_socket(const PrrtSocket *socket_ptr, unsigned char *buffer_ptr, ssize_t *received_size,
                         struct sockaddr_in *remote_ptr, socklen_t *remote_len_ptr, struct timespec *packet_stamp_ptr) {
Andreas Schmidt's avatar
Andreas Schmidt committed
191
    if (socket_ptr->isHardwareTimestamping) {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        struct cmsghdr *cmsg;
        struct msghdr msg;
        struct iovec entry;

        struct {
            struct cmsghdr cm;
            char control[512];
        } control;

        memset(&msg, 0, sizeof(msg));
        msg.msg_iov = &entry;
        msg.msg_iovlen = 1;
        entry.iov_base = buffer_ptr;
        entry.iov_len = MAX_PAYLOAD_LENGTH;
        msg.msg_name = (caddr_t) remote_ptr;
        msg.msg_namelen = *remote_len_ptr;
        msg.msg_control = &control;
        msg.msg_controllen = sizeof(control);

        *received_size = recvmsg(socket_ptr->dataSocketFd, &msg, 0);

Andreas Schmidt's avatar
Andreas Schmidt committed
213
214
        for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
            switch (cmsg->cmsg_type) {
215
                case SO_TIMESTAMPNS: {
Andreas Schmidt's avatar
Andreas Schmidt committed
216
                    memcpy(packet_stamp_ptr, (struct timespec *) CMSG_DATA(cmsg), sizeof(struct timespec));
217
218
219
220
221
222
223
224
225
226
227
228
229
230
                    break;
                }
                default:
                    PERROR("Unknown Control Msg Type: %d", cmsg->cmsg_type);
                    break;
            }
        }
    } else {
        *received_size = recvfrom(socket_ptr->dataSocketFd, buffer_ptr, MAX_PAYLOAD_LENGTH, 0,
                                  (struct sockaddr *) remote_ptr, remote_len_ptr);
        clock_gettime(CLOCK_REALTIME, packet_stamp_ptr);
    }
}

Andreas Schmidt's avatar
Andreas Schmidt committed
231
void *receive_data_loop(void *ptr) {
232
233
234
235
236
237
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
    unsigned char buffer[MAX_PAYLOAD_LENGTH];
    PrrtSocket *sock_ptr = ptr;

Andreas Schmidt's avatar
Andreas Schmidt committed
238
239
240
241
242
243
244
    while (1) {
        XlapTimestampPlaceholder tsph1;
        XlapTimestampPlaceholder tsph2;
        XlapTimestampPlaceholder tsph3;
        XlapTimestampPlaceholderInitialize(&tsph1);
        XlapTimestampPlaceholderInitialize(&tsph2);
        XlapTimestampPlaceholderInitialize(&tsph3);
Stefan Reif's avatar
Stefan Reif committed
245

246
247
248
249
250
251
        struct timespec packet_recv_stamp;
        receive_from_socket(sock_ptr, buffer, &n, &remote, &addrlen, &packet_recv_stamp);
        debug(DEBUG_HARDSTAMPING, "Packet TS:\t%ld.%09ld; Who? %s", (long) packet_recv_stamp.tv_sec,
              packet_recv_stamp.tv_nsec, inet_ntoa(remote.sin_addr));
        sock_ptr->lastReceivedTimestamp = PrrtClock_TimespecToPrrtTimestamp(packet_recv_stamp);

Andreas Schmidt's avatar
Andreas Schmidt committed
252
253
        XlapTimeStampClock(&tsph1, ts_any_packet, 0, LinkReceive);
        XlapTimeStampCycle(&tsph1, ts_any_packet, 0, LinkReceive);
254

255
        PrrtPacket *packet = (PrrtPacket *) calloc(1, sizeof(PrrtPacket));
256
        check_mem(packet);
Stefan Reif's avatar
Stefan Reif committed
257

Andreas Schmidt's avatar
Andreas Schmidt committed
258
        XlapTimeStampCycle(&tsph2, ts_any_packet, 0, DecodeStart);
259
        check(PrrtPacket_decode(buffer, (uint16_t) n, packet), "Decode failed.");
Andreas Schmidt's avatar
Andreas Schmidt committed
260
        XlapTimeStampCycle(&tsph3, ts_any_packet, 0, DecodeEnd);
Stefan Reif's avatar
Stefan Reif committed
261

Andreas Schmidt's avatar
Andreas Schmidt committed
262
        prrtSequenceNumber_t seqno = packet->sequenceNumber;
263

264
        prrtPacketType_t packetType = PrrtPacket_type(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
265
        debug(DEBUG_DATARECEIVER, "received packet %d:%u", (int) packetType, seqno);
Andreas Schmidt's avatar
Andreas Schmidt committed
266
        enum XlapTimestampPacketKind kind = ts_any_packet;
Andreas Schmidt's avatar
Andreas Schmidt committed
267
        if (packetType == PACKET_TYPE_DATA) {
Andreas Schmidt's avatar
Andreas Schmidt committed
268
            kind = ts_data_packet;
Andreas Schmidt's avatar
Andreas Schmidt committed
269
        } else if (packetType == PACKET_TYPE_REDUNDANCY) {
Andreas Schmidt's avatar
Andreas Schmidt committed
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
            kind = ts_redundancy_packet;
        }
        if (packetType == PACKET_TYPE_DATA || packetType == PACKET_TYPE_REDUNDANCY) {
            XlapTimeStampValue(sock_ptr, kind, seqno, ChannelReceive, packet_recv_stamp);

            XlapTimestampPlaceholderUse(sock_ptr, kind, seqno, &tsph1);
            XlapTimestampPlaceholderUse(sock_ptr, kind, seqno, &tsph2);
            XlapTimestampPlaceholderUse(sock_ptr, kind, seqno, &tsph3);

            XlapTimeStampCycle(sock_ptr, kind, seqno, HandlePacketStart);
            if (packetType == PACKET_TYPE_DATA) {
                handle_data_packet(sock_ptr, packet, remote);
            } else if (packetType == PACKET_TYPE_REDUNDANCY) {
                handle_redundancy_packet(sock_ptr, packet);
            } else {
                goto error;
            }
            XlapTimeStampCycle(sock_ptr, kind, seqno, HandlePacketEnd);
Andreas Schmidt's avatar
Andreas Schmidt committed
288
289
290
        } else {
            PrrtPacket_print(packet);
            PrrtPacket_destroy(packet);
291
        }
292
293

        PrrtSocket_cleanup(sock_ptr);
294
295
296
    }

    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
297
PNOTIMPLEMENTED("SHOULD IMPLEMENT ERROR HANDLER HERE");
Andreas Schmidt's avatar
Andreas Schmidt committed
298
}