dataReceiver.c 11.8 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

Andreas Schmidt's avatar
Andreas Schmidt committed
53
static bool send_feedback(const 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
    PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 0, 4, 6, 8, 9, 5,
                                                                     sock_ptr->address->sin_addr.s_addr,
Andreas Schmidt's avatar
Andreas Schmidt committed
73
                                                                     forwardTripTime, stats.erasureCount, stats.packetCount);
74
    prrtPacketLength_t length = PrrtPacket_size(feedback_pkt_ptr);
75
    void *buf = calloc(1, length);
76
    check_mem(buf);
77

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

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

84
85
    PrrtPacket_destroy(feedback_pkt_ptr);

86
    return true;
87
88

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

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

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

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

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

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

Stefan Reif's avatar
Stefan Reif committed
115
    prrtTimestamp_t now = PrrtClock_get_prrt_time_us(&sock_ptr->clock);
Andreas Schmidt's avatar
Andreas Schmidt committed
116
    if (is_timeout(now, payload->packetTimeout_us)) {
117
        PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable, packet->sequenceNumber);
118
119
        // TODO: note this as loss
        PrrtPacket_destroy(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
120
121
        debug(DEBUG_RECEIVER, "timeout data packet %u (%lu > %lu)", seqno, (unsigned long) now,
              (unsigned long) payload->packetTimeout_us);
122
123
    } else if (PrrtDeliveredPacketTable_test_set_is_number_relevant(sock_ptr->deliveredPacketTable,
                                                                    packet->sequenceNumber) ==
Andreas Schmidt's avatar
Andreas Schmidt committed
124
               false) {
125
126
127
128
129
130
131
        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
132
        if (block != NULL) {
133
134
            check(PrrtBlock_insert_data_packet(block, reference), "Inserting failed: %d, %d", baseSequenceNumber,
                  seqno);
135
136
            decode_block(sock_ptr, block);
        } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
137
            if (PrrtDataStore_insert(sock_ptr->dataPacketStore, reference) == false) {
138
139
140
141
142
                PrrtPacket_destroy(reference);
            }
        }

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

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

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

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

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

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

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

173
            PrrtRepairBlockStore_insert(socket->repairBlockStore, block);
174
175
        }

176
        retrieve_data_blocks(socket, redundancyPayload->baseSequenceNumber, block->codingParams->k, block);
177

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

187
188
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
189
    if (socket_ptr->isHardwareTimestamping) {
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
        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
211
212
        for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
            switch (cmsg->cmsg_type) {
213
                case SO_TIMESTAMPNS: {
Andreas Schmidt's avatar
Andreas Schmidt committed
214
                    memcpy(packet_stamp_ptr, (struct timespec *) CMSG_DATA(cmsg), sizeof(struct timespec));
215
216
217
218
219
220
221
222
223
224
225
226
227
228
                    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
229
void *receive_data_loop(void *ptr) {
230
231
232
233
234
235
    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
236
237
238
239
240
241
242
    while (1) {
        XlapTimestampPlaceholder tsph1;
        XlapTimestampPlaceholder tsph2;
        XlapTimestampPlaceholder tsph3;
        XlapTimestampPlaceholderInitialize(&tsph1);
        XlapTimestampPlaceholderInitialize(&tsph2);
        XlapTimestampPlaceholderInitialize(&tsph3);
Stefan Reif's avatar
Stefan Reif committed
243

244
245
246
247
248
249
        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
250
251
        XlapTimeStampClock(&tsph1, ts_any_packet, 0, LinkReceive);
        XlapTimeStampCycle(&tsph1, ts_any_packet, 0, LinkReceive);
252

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

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

Andreas Schmidt's avatar
Andreas Schmidt committed
260
        prrtSequenceNumber_t seqno = packet->sequenceNumber;
261

262
        prrtPacketType_t packetType = PrrtPacket_type(packet);
Andreas Schmidt's avatar
Andreas Schmidt committed
263
        debug(DEBUG_DATARECEIVER, "received packet %d:%u", (int) packetType, seqno);
Andreas Schmidt's avatar
Andreas Schmidt committed
264
        enum XlapTimestampPacketKind kind = ts_any_packet;
Andreas Schmidt's avatar
Andreas Schmidt committed
265
        if (packetType == PACKET_TYPE_DATA) {
Andreas Schmidt's avatar
Andreas Schmidt committed
266
            kind = ts_data_packet;
Andreas Schmidt's avatar
Andreas Schmidt committed
267
        } else if (packetType == PACKET_TYPE_REDUNDANCY) {
Andreas Schmidt's avatar
Andreas Schmidt committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
            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
286
287
288
        } else {
            PrrtPacket_print(packet);
            PrrtPacket_destroy(packet);
289
        }
290
291

        PrrtSocket_cleanup(sock_ptr);
292
293
294
    }

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