packet.c 18.5 KB
Newer Older
1 2 3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
4
#include <netinet/in.h>
5
#include <stdbool.h>
6 7 8
#include "../../util/common.h"
#include "../../util/dbg.h"
#include "../clock.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
9
#include "packet.h"
10

11
static void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
12

13
static void *encode_data_header(void *buf_ptr, const void *payload);
14

15
static void *encode_redundancy_header(void *buf_ptr, const void *payload);
16

17
static void *encode_feedback_header(void *buf_ptr, const void *payload);
18

19
static void *decode_data_header(void *dstBuffer, const void *srcBuffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
20

21
static void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer);
22

23
static void *decode_feedback_header(void *dstBuffer, const void *srcBuffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
24

25
prrtPacketLength_t PrrtPacket_size(PrrtPacket *packet_ptr) {
26
    return (prrtPacketLength_t) (packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
27 28
}

29
int PrrtPacket_print(PrrtPacket *packet_ptr) {
30 31
    printf(" 0                   1                   2                   3\n"
                   " 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n"
32
                   "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
33
    prrtPacketType_t type = PrrtPacket_type(packet_ptr);
34

35
    printf("| %5u | %5u | %13u | %29u |\n", type, PrrtPacket_priority(packet_ptr), packet_ptr->index,
36
           packet_ptr->sequenceNumber);
37
    printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
38

39
    if (type == PACKET_TYPE_DATA) {
40
        PrrtPacketDataPayload *payload = packet_ptr->payload;
41 42
        printf("| %61u |\n", payload->timestamp);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
Andreas Schmidt's avatar
Andreas Schmidt committed
43
        printf("| %61u |\n", payload->RTprop_us);
44
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
45 46
        printf("| %61u |\n", payload->packetTimeout_us);
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
47 48
        printf("| %61s |\n", (char *) (packet_ptr->payload + PRRT_PACKET_DATA_HEADER_SIZE));
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
49
    } else if (type == PACKET_TYPE_REDUNDANCY) {
50
        PrrtPacketRedundancyPayload *payload = packet_ptr->payload;
51
        printf("| %29u | %13u | %13u |\n", payload->baseSequenceNumber, payload->n, payload->k);
52
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
53
    } else if (type == PACKET_TYPE_FEEDBACK) {
54 55
        prrtPacketLength_t i = 0;

56
        PrrtPacketFeedbackPayload *payload = packet_ptr->payload;
57

58
        printf("| %61u |\n", payload->forwardTripTimestamp_us);
59
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
60
        printf("| %29u | %29u |\n", payload->erasureCount, payload->packetCount);
61
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
62
        printf("| %29u | %29u |\n", payload->gapLength, payload->gapCount);
63
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
64
        printf("| %29u | %29u |\n", payload->burstLength, payload->burstCount);
65
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
66 67 68 69 70

        prrtPacketLength_t remainingSpace = ((prrtPacketLength_t) (packet_ptr->payloadLength -
                                                                   PRRT_PACKET_FEEDBACK_HEADER_SIZE));
        prrtPacketLength_t blockCount = (prrtPacketLength_t) (remainingSpace / sizeof(PrrtIncompleteBlock));

71
        for (i = 0; i < blockCount; ++i) {
72 73 74 75
            PrrtIncompleteBlock block = payload->incompleteBlocks[i];
            printf("| %29u | %29u |\n", block.sequenceNumberBase, block.repairCycleIndex);
            printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
        }
76
    } else {
77 78
        printf("| Unhandeled Type                                               |\n");
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
79 80 81 82 83
    }

    return 0;
}

84
PrrtPacket *PrrtPacket_copy(PrrtPacket *original) {
85 86 87 88 89
    __sync_fetch_and_add(&original->refcount, 1);
    return original;
}

PrrtPacket *PrrtPacket_deepcopy(PrrtPacket *original) {
90 91
    PrrtPacket *newPacket = calloc(1, sizeof(PrrtPacket));
    check_mem(newPacket);
92
    void *payload = calloc(1, original->payloadLength);
93 94 95
    check_mem(payload);

    newPacket->payload = payload;
96 97
    newPacket->payloadLength = original->payloadLength;
    memcpy(newPacket->payload, original->payload, original->payloadLength);
98 99

    newPacket->index = original->index;
100
    newPacket->sequenceNumber = original->sequenceNumber;
101 102
    newPacket->type_priority = original->type_priority;

103 104
    newPacket->refcount = 1;

105 106 107

    return newPacket;
    error:
108
    PERROR("Not enough memory for packet copies.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
109
    return NULL;
110 111
}

112 113
static PrrtPacket *
create_header(uint8_t priority, prrtSequenceNumber_t seqno, prrtPacketLength_t size, uint8_t type, uint8_t index) {
Andreas Schmidt's avatar
Andreas Schmidt committed
114 115 116 117 118 119
    PrrtPacket *packet = calloc(1, sizeof(PrrtPacket));
    check_mem(packet);

    packet->type_priority = type << 4;
    packet->type_priority |= priority & 0x0F;
    packet->index = index;
120 121
    packet->sequenceNumber = seqno;
    packet->payloadLength = size;
122
    packet->refcount = 1;
Andreas Schmidt's avatar
Andreas Schmidt committed
123 124 125 126 127

    return packet;

    error:
    PERROR("Could not create packet.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
128
    return NULL;
Andreas Schmidt's avatar
Andreas Schmidt committed
129 130
}

131
bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
132
    void *payload = packet_ptr->payload;
133

134
    check(packet_ptr->payloadLength + PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH <= buf_size, "Buffer too small.");
135

136 137
    buf_ptr = encode_general_header(buf_ptr, packet_ptr);

138
    prrtPacketType_t type = PrrtPacket_type(packet_ptr);
139
    if (type == PACKET_TYPE_DATA) {
140
        buf_ptr = encode_data_header(buf_ptr, payload);
141
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_DATA_HEADER_SIZE);
142
    } else if (type == PACKET_TYPE_REDUNDANCY) {
143
        buf_ptr = encode_redundancy_header(buf_ptr, payload);
144
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
145
    } else if (type == PACKET_TYPE_FEEDBACK) {
146
        encode_feedback_header(buf_ptr, payload);
147 148
    } else {
        perror("NOT IMPLEMENTED");
149
        return false;
150
    }
151
    return true;
152 153

    error:
154
    return false;
155 156
}

157
#define identity(x) (x)
158

159 160
#define PrrtPacketField_encode(payload, buf_ptr, field_t, field, conversion) \
    do { \
161 162 163 164
        field_t PrrtPacketField_encode_value = conversion((payload)->field); \
        /* memcpy to avoid misaligned access.
         * The compiler will optimize this into appropriate mov instructions */ \
        memcpy(buf_ptr, &PrrtPacketField_encode_value, sizeof(field_t)); \
165 166
        (buf_ptr) += sizeof(field_t); \
    } while (false)
167

168 169
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
    const PrrtPacketRedundancyPayload *redundancyPayload = payload;
170

171 172
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtSequenceNumber_t, baseSequenceNumber, htons);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
Andreas Schmidt's avatar
Andreas Schmidt committed
173
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtTimedelta_t , btl_pace, htonl);
174 175
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, n, identity);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, k, identity);
176 177 178 179

    return buf_ptr;
}

180
void *encode_feedback_header(void *buf_ptr, const void *payload) {
181
    const PrrtPacketFeedbackPayload *feedbackPayload = payload;
182

183
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimestamp_t, forwardTripTimestamp_us, htonl);
Andreas Schmidt's avatar
Andreas Schmidt committed
184
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimedelta_t, btl_pace, htonl);
185 186 187 188 189 190 191 192
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, erasureCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, packetCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapLength, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, gapCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstLength, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, burstCount, htons);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtPacketType_t, ackPacketType, identity);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtSequenceNumber_t, ackSequenceNumber, htons);
193

194 195 196
    return buf_ptr;
}

197
void *encode_data_header(void *buf_ptr, const void *payload) {
198
    const PrrtPacketDataPayload *data_payload = payload;
199

Andreas Schmidt's avatar
Andreas Schmidt committed
200
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, btl_pace, htonl);
201 202
    PrrtPacketField_encode(data_payload, buf_ptr, prrtPacketLength_t, dataLength, htonl);
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
Andreas Schmidt's avatar
Andreas Schmidt committed
203
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, RTprop_us, htonl);
rna's avatar
rna committed
204
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, packetTimeout_us, htonl);
Andreas Schmidt's avatar
Andreas Schmidt committed
205
    PrrtPacketField_encode(data_payload, buf_ptr, prrtDeliveryRate_t, btl_datarate, htonl);
206 207 208 209

    return buf_ptr;
}

210
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
211

212 213 214
    PrrtPacketField_encode(packet, buf_ptr, uint8_t, type_priority, identity);
    PrrtPacketField_encode(packet, buf_ptr, uint8_t, index, identity);
    PrrtPacketField_encode(packet, buf_ptr, prrtSequenceNumber_t, sequenceNumber, htons);
215

216
    return buf_ptr;
217 218
}

219
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket) {
220 221 222 223

    // targetPacket is uninitialized, so we need to set the reference count
    targetPacket->refcount = 1;

224
    prrtPacketLength_t payload_len = (prrtPacketLength_t) (srcBufferSize - PRRT_PACKET_ENCODED_GENERAL_HEADER_LENGTH);
Andreas Schmidt's avatar
Andreas Schmidt committed
225 226 227 228 229
    targetPacket->type_priority = *(uint8_t *) srcBuffer;
    srcBuffer += 1;
    uint8_t *index_ptr = (uint8_t *) srcBuffer;
    targetPacket->index = *index_ptr;
    srcBuffer += 1;
230 231 232
    prrtSequenceNumber_t *seqno_prt = (prrtSequenceNumber_t *) srcBuffer;
    targetPacket->sequenceNumber = ntohs(*seqno_prt);
    srcBuffer += sizeof(prrtSequenceNumber_t);
233

Andreas Schmidt's avatar
Andreas Schmidt committed
234
    void *payload_buffer = calloc(1, payload_len);
235 236
    check_mem(payload_buffer);

Andreas Schmidt's avatar
Andreas Schmidt committed
237
    targetPacket->payload = payload_buffer;
238
    targetPacket->payloadLength = payload_len;
Andreas Schmidt's avatar
Andreas Schmidt committed
239

240
    prrtPacketType_t packetType = PrrtPacket_type(targetPacket);
241
    if (packetType == PACKET_TYPE_DATA) {
Andreas Schmidt's avatar
Andreas Schmidt committed
242 243
        srcBuffer = decode_data_header(srcBuffer, payload_buffer);
        PrrtPacket_copy_buffer_to_payload(targetPacket, srcBuffer, PRRT_PACKET_DATA_HEADER_SIZE);
244
    } else if (packetType == PACKET_TYPE_REDUNDANCY) {
Andreas Schmidt's avatar
Andreas Schmidt committed
245 246
        srcBuffer = decode_redundancy_header(srcBuffer, payload_buffer);
        PrrtPacket_copy_buffer_to_payload(targetPacket, srcBuffer, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
247
    } else if (packetType == PACKET_TYPE_FEEDBACK) {
248
        decode_feedback_header(srcBuffer, payload_buffer);
Andreas Schmidt's avatar
Andreas Schmidt committed
249
    } else {
250
        printf("NOT IMPLEMENTED\n");
Andreas Schmidt's avatar
Andreas Schmidt committed
251
    }
252
    return true;
253 254

    error:
255
    return false;
Andreas Schmidt's avatar
Andreas Schmidt committed
256
}
257

258 259
#define PrrtPacketField_decode(payload, buf_ptr, field_t, field, conversion) \
    do { \
260 261 262 263 264
        field_t PrrtPacketField_decode_value; \
        /* memcpy to avoid misaligned access.
         * The compiler will optimize this into appropriate mov instructions */ \
        memcpy(&PrrtPacketField_decode_value, buf_ptr, sizeof(field_t)); \
        (payload)->field = conversion(PrrtPacketField_decode_value); \
265 266 267
        (buf_ptr) += sizeof(field_t); \
    } while (false)

268
void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
269
    PrrtPacketRedundancyPayload *redundancyPayload = (PrrtPacketRedundancyPayload *) srcBuffer;
270

271 272
    PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtSequenceNumber_t, baseSequenceNumber, ntohs);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
273
    PrrtPacketField_decode(redundancyPayload, dstBuffer, prrtTimedelta_t , btl_pace, ntohl);
274 275
    PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, n, identity);
    PrrtPacketField_decode(redundancyPayload, dstBuffer, uint8_t, k, identity);
276

Andreas Schmidt's avatar
Andreas Schmidt committed
277
    return dstBuffer;
278 279
}

280
void *decode_feedback_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
281
    PrrtPacketFeedbackPayload *feedback_payload = (PrrtPacketFeedbackPayload *) srcBuffer;
282

283
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimestamp_t, forwardTripTimestamp_us, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
284
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimedelta_t, btl_pace, ntohl);
285 286 287 288 289 290 291 292
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, erasureCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, packetCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapLength, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, gapCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstLength, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, burstCount, ntohs);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtPacketType_t, ackPacketType, identity);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtSequenceNumber_t, ackSequenceNumber, ntohs);
293

Andreas Schmidt's avatar
Andreas Schmidt committed
294
    return dstBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
295 296
}

297
void *decode_data_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
298
    PrrtPacketDataPayload *data_payload = (PrrtPacketDataPayload *) srcBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
299

Andreas Schmidt's avatar
Andreas Schmidt committed
300
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, btl_pace, ntohl);
301 302
    PrrtPacketField_decode(data_payload, dstBuffer, prrtPacketLength_t, dataLength, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
303
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, RTprop_us, ntohl);
304
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, packetTimeout_us, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
305
    PrrtPacketField_decode(data_payload, dstBuffer, prrtDeliveryRate_t, btl_datarate, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
306

Andreas Schmidt's avatar
Andreas Schmidt committed
307
    return dstBuffer;
308 309
}

310
int PrrtPacket_destroy(PrrtPacket *packet) {
311 312 313 314 315 316 317 318 319 320

    prrtRefcount_t refcount = __sync_fetch_and_sub(&packet->refcount, 1);
    if (refcount > 1) {
        return 0;
    } else {
        if (packet->payload != NULL) {
            free(packet->payload);
        }
        free(packet);
        return 0;
321
    }
322 323
}

324
PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadPointer,
rna's avatar
rna committed
325
                                          prrtPacketLength_t dataLength, prrtSequenceNumber_t sequenceNumber,
326
                                          prrtTimedelta_t targetDelay) {
Andreas Schmidt's avatar
Andreas Schmidt committed
327
    PrrtPacket *packet = create_header(priority, sequenceNumber,
328 329
                                       (prrtPacketLength_t) (dataLength + PRRT_PACKET_DATA_HEADER_SIZE),
                                       PACKET_TYPE_DATA, 0);
330

331
    PrrtPacketDataPayload *dataPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
332 333
    check_mem(dataPayload);
    packet->payload = dataPayload;
334

rna's avatar
rna committed
335
    dataPayload->dataLength = dataLength;
336
    dataPayload->timestamp = PrrtClock_get_current_time_us();
337
    dataPayload->packetTimeout_us = dataPayload->timestamp + targetDelay;
Andreas Schmidt's avatar
Andreas Schmidt committed
338
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_DATA_HEADER_SIZE)
339

Andreas Schmidt's avatar
Andreas Schmidt committed
340
    return packet;
341

342
    error:
343
    PERROR("Could not create packet.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
344
    return NULL;
345 346
}

347 348
PrrtPacket *PrrtPacket_reconstruct_data_packet(PrrtPacketDataPayload *payload, prrtIndex_t index,
                                               prrtSequenceNumber_t sequenceNumber) {
rna's avatar
rna committed
349 350 351
    prrtPacketLength_t dataLength = payload->dataLength;

    PrrtPacket *packet = create_header(0, sequenceNumber,
352 353
                                       (prrtPacketLength_t) (dataLength + PRRT_PACKET_DATA_HEADER_SIZE),
                                       PACKET_TYPE_DATA, index);
rna's avatar
rna committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367

    PrrtPacketDataPayload *dataPayload = calloc(1, packet->payloadLength);
    check_mem(dataPayload);
    packet->payload = dataPayload;

    PrrtPacket_copy_buffer_to_payload(packet, payload, 0)

    return packet;
    error:
    PERROR("Could not reconstruct packet.%s", "");
    return NULL;

}

368 369 370
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadPointer,
                                                prrtPacketLength_t payloadLength,
                                                prrtSequenceNumber_t sequenceNumber, uint8_t index,
371
                                                prrtSequenceNumber_t baseSequenceNumber,
372
                                                PrrtCodingConfiguration *codingParams) {
Andreas Schmidt's avatar
Andreas Schmidt committed
373
    PrrtPacket *packet = create_header(priority, sequenceNumber,
374
                                       (prrtPacketLength_t) (payloadLength + PRRT_PACKET_REDUNDANCY_HEADER_SIZE),
Andreas Schmidt's avatar
Andreas Schmidt committed
375
                                       PACKET_TYPE_REDUNDANCY, index);
376

377
    PrrtPacketRedundancyPayload *redundancyPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
378 379
    check_mem(redundancyPayload);
    packet->payload = redundancyPayload;
380

381
    redundancyPayload->baseSequenceNumber = baseSequenceNumber;
382
    redundancyPayload->timestamp = PrrtClock_get_current_time_us();
383 384
    redundancyPayload->k = codingParams->k;
    redundancyPayload->n = codingParams->n;
Andreas Schmidt's avatar
Andreas Schmidt committed
385
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
386

Andreas Schmidt's avatar
Andreas Schmidt committed
387
    return packet;
388 389

    error:
390
    PERROR("Out of memory%s.", "");
391
    return NULL;
392 393
}

394
PrrtPacket *
Andreas Schmidt's avatar
Andreas Schmidt committed
395
PrrtPacket_create_feedback_packet(uint8_t priority, prrtSequenceNumber_t sequenceNumber,
396 397 398 399
                                  prrtSequenceNumber_t gapLength, prrtSequenceNumber_t gapCount,
                                  prrtSequenceNumber_t burstLength, prrtSequenceNumber_t burstCount,
                                  prrtTimestamp_t forwardTripTime, prrtSequenceNumber_t erasureCount,
                                  prrtSequenceNumber_t packetCount, prrtSequenceNumber_t ackSequenceNumber,
Andreas Schmidt's avatar
Andreas Schmidt committed
400
                                  prrtPacketType_t ackPacketType, prrtTimedelta_t btl_pace) {
Andreas Schmidt's avatar
Andreas Schmidt committed
401
    PrrtPacket *packet = create_header(priority, sequenceNumber, PRRT_PACKET_FEEDBACK_HEADER_SIZE, PACKET_TYPE_FEEDBACK,
402
                                       0);
Andreas Schmidt's avatar
Andreas Schmidt committed
403

404
    PrrtPacketFeedbackPayload *payload = calloc(1, packet->payloadLength);
405
    check_mem(payload);
Andreas Schmidt's avatar
Andreas Schmidt committed
406
    packet->payload = payload;
Andreas Schmidt's avatar
Andreas Schmidt committed
407
    payload->btl_pace = btl_pace;
408
    payload->forwardTripTimestamp_us = forwardTripTime;
Andreas Schmidt's avatar
Andreas Schmidt committed
409 410
    payload->erasureCount = erasureCount;
    payload->packetCount = packetCount;
411 412 413 414
    payload->gapLength = gapLength;
    payload->gapCount = gapCount;
    payload->burstLength = burstLength;
    payload->burstCount = burstCount;
415 416
    payload->ackSequenceNumber = ackSequenceNumber;
    payload->ackPacketType = ackPacketType;
417

Andreas Schmidt's avatar
Andreas Schmidt committed
418
    return packet;
419 420

    error:
421
    return NULL;
422
}