packet.c 18.9 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");
43
        printf("| %61u |\n", payload->groupRTprop_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->groupRTT_us);
59
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
60
        printf("| %61u |\n", payload->forwardTripTimestamp_us);
61
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
62
        printf("| %29u | %29u |\n", payload->erasureCount, payload->packetCount);
63
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
64
        printf("| %29u | %29u |\n", payload->gapLength, payload->gapCount);
65
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
66
        printf("| %29u | %29u |\n", payload->burstLength, payload->burstCount);
67
        printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
68 69 70 71 72

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

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

    return 0;
}

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

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

    newPacket->payload = payload;
98 99
    newPacket->payloadLength = original->payloadLength;
    memcpy(newPacket->payload, original->payload, original->payloadLength);
100 101

    newPacket->index = original->index;
102
    newPacket->sequenceNumber = original->sequenceNumber;
103 104
    newPacket->type_priority = original->type_priority;

105 106
    newPacket->refcount = 1;

107 108 109

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

114 115
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
116 117 118 119 120 121
    PrrtPacket *packet = calloc(1, sizeof(PrrtPacket));
    check_mem(packet);

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

    return packet;

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

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

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

138 139
    buf_ptr = encode_general_header(buf_ptr, packet_ptr);

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

    error:
156
    return false;
157 158
}

159
#define identity(x) (x)
160

161 162
#define PrrtPacketField_encode(payload, buf_ptr, field_t, field, conversion) \
    do { \
163 164 165 166
        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)); \
167 168
        (buf_ptr) += sizeof(field_t); \
    } while (false)
169

170 171
void *encode_redundancy_header(void *buf_ptr, const void *payload) {
    const PrrtPacketRedundancyPayload *redundancyPayload = payload;
172

173 174
    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
175
    PrrtPacketField_encode(redundancyPayload, buf_ptr, prrtTimedelta_t , btl_pace, htonl);
176 177
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, n, identity);
    PrrtPacketField_encode(redundancyPayload, buf_ptr, uint8_t, k, identity);
178 179 180 181

    return buf_ptr;
}

182
void *encode_feedback_header(void *buf_ptr, const void *payload) {
183
    const PrrtPacketFeedbackPayload *feedbackPayload = payload;
184

185 186
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimedelta_t, groupRTT_us, htonl);
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimestamp_t, forwardTripTimestamp_us, htonl);
Andreas Schmidt's avatar
Andreas Schmidt committed
187
    PrrtPacketField_encode(feedbackPayload, buf_ptr, prrtTimedelta_t, btl_pace, htonl);
188 189 190 191 192 193 194 195
    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);
196

197 198 199
    return buf_ptr;
}

200
void *encode_data_header(void *buf_ptr, const void *payload) {
201
    const PrrtPacketDataPayload *data_payload = payload;
202

Andreas Schmidt's avatar
Andreas Schmidt committed
203
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, btl_pace, htonl);
204 205 206
    PrrtPacketField_encode(data_payload, buf_ptr, prrtPacketLength_t, dataLength, htonl);
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, timestamp, htonl);
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimedelta_t, groupRTprop_us, htonl);
rna's avatar
rna committed
207
    PrrtPacketField_encode(data_payload, buf_ptr, prrtTimestamp_t, packetTimeout_us, htonl);
Andreas Schmidt's avatar
Andreas Schmidt committed
208
    PrrtPacketField_encode(data_payload, buf_ptr, prrtDeliveryRate_t, btl_datarate, htonl);
209 210 211 212

    return buf_ptr;
}

213
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
214

215 216 217
    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);
218

219
    return buf_ptr;
220 221
}

222
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket) {
223 224 225 226

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

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

Andreas Schmidt's avatar
Andreas Schmidt committed
237
    void *payload_buffer = calloc(1, payload_len);
238 239
    check_mem(payload_buffer);

Andreas Schmidt's avatar
Andreas Schmidt committed
240
    targetPacket->payload = payload_buffer;
241
    targetPacket->payloadLength = payload_len;
Andreas Schmidt's avatar
Andreas Schmidt committed
242

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

    error:
258
    return false;
Andreas Schmidt's avatar
Andreas Schmidt committed
259
}
260

261 262
#define PrrtPacketField_decode(payload, buf_ptr, field_t, field, conversion) \
    do { \
263 264 265 266 267
        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); \
268 269 270
        (buf_ptr) += sizeof(field_t); \
    } while (false)

271
void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
272
    PrrtPacketRedundancyPayload *redundancyPayload = (PrrtPacketRedundancyPayload *) srcBuffer;
273

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

Andreas Schmidt's avatar
Andreas Schmidt committed
280
    return dstBuffer;
281 282
}

283
void *decode_feedback_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
284
    PrrtPacketFeedbackPayload *feedback_payload = (PrrtPacketFeedbackPayload *) srcBuffer;
285

286 287
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimedelta_t, groupRTT_us, ntohl);
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimestamp_t, forwardTripTimestamp_us, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
288
    PrrtPacketField_decode(feedback_payload, dstBuffer, prrtTimedelta_t, btl_pace, ntohl);
289 290 291 292 293 294 295 296
    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);
297

Andreas Schmidt's avatar
Andreas Schmidt committed
298
    return dstBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
299 300
}

301
void *decode_data_header(void *dstBuffer, const void *srcBuffer) {
Andreas Schmidt's avatar
Andreas Schmidt committed
302
    PrrtPacketDataPayload *data_payload = (PrrtPacketDataPayload *) srcBuffer;
Andreas Schmidt's avatar
Andreas Schmidt committed
303

Andreas Schmidt's avatar
Andreas Schmidt committed
304
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, btl_pace, ntohl);
305 306 307 308
    PrrtPacketField_decode(data_payload, dstBuffer, prrtPacketLength_t, dataLength, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimestamp_t, timestamp, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, groupRTprop_us, ntohl);
    PrrtPacketField_decode(data_payload, dstBuffer, prrtTimedelta_t, packetTimeout_us, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
309
    PrrtPacketField_decode(data_payload, dstBuffer, prrtDeliveryRate_t, btl_datarate, ntohl);
Andreas Schmidt's avatar
Andreas Schmidt committed
310

Andreas Schmidt's avatar
Andreas Schmidt committed
311
    return dstBuffer;
312 313
}

314
int PrrtPacket_destroy(PrrtPacket *packet) {
315 316 317 318 319 320 321 322 323 324

    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;
325
    }
326 327
}

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

335
    PrrtPacketDataPayload *dataPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
336 337
    check_mem(dataPayload);
    packet->payload = dataPayload;
338

rna's avatar
rna committed
339
    dataPayload->dataLength = dataLength;
340
    dataPayload->timestamp = PrrtClock_get_current_time_us();
341
    dataPayload->packetTimeout_us = dataPayload->timestamp + targetDelay;
342
    dataPayload->groupRTprop_us = 0;
Andreas Schmidt's avatar
Andreas Schmidt committed
343
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_DATA_HEADER_SIZE)
344

Andreas Schmidt's avatar
Andreas Schmidt committed
345
    return packet;
346

347
    error:
348
    PERROR("Could not create packet.%s", "");
Andreas Schmidt's avatar
Andreas Schmidt committed
349
    return NULL;
350 351
}

352 353
PrrtPacket *PrrtPacket_reconstruct_data_packet(PrrtPacketDataPayload *payload, prrtIndex_t index,
                                               prrtSequenceNumber_t sequenceNumber) {
rna's avatar
rna committed
354 355 356
    prrtPacketLength_t dataLength = payload->dataLength;

    PrrtPacket *packet = create_header(0, sequenceNumber,
357 358
                                       (prrtPacketLength_t) (dataLength + PRRT_PACKET_DATA_HEADER_SIZE),
                                       PACKET_TYPE_DATA, index);
rna's avatar
rna committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372

    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;

}

373 374 375
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadPointer,
                                                prrtPacketLength_t payloadLength,
                                                prrtSequenceNumber_t sequenceNumber, uint8_t index,
376
                                                prrtSequenceNumber_t baseSequenceNumber,
377
                                                PrrtCodingConfiguration *codingParams) {
Andreas Schmidt's avatar
Andreas Schmidt committed
378
    PrrtPacket *packet = create_header(priority, sequenceNumber,
379
                                       (prrtPacketLength_t) (payloadLength + PRRT_PACKET_REDUNDANCY_HEADER_SIZE),
Andreas Schmidt's avatar
Andreas Schmidt committed
380
                                       PACKET_TYPE_REDUNDANCY, index);
381

382
    PrrtPacketRedundancyPayload *redundancyPayload = calloc(1, packet->payloadLength);
Andreas Schmidt's avatar
Andreas Schmidt committed
383 384
    check_mem(redundancyPayload);
    packet->payload = redundancyPayload;
385

386
    redundancyPayload->baseSequenceNumber = baseSequenceNumber;
387
    redundancyPayload->timestamp = PrrtClock_get_current_time_us();
388 389
    redundancyPayload->k = codingParams->k;
    redundancyPayload->n = codingParams->n;
Andreas Schmidt's avatar
Andreas Schmidt committed
390
    PrrtPacket_copy_buffer_to_payload(packet, payloadPointer, PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
391

Andreas Schmidt's avatar
Andreas Schmidt committed
392
    return packet;
393 394

    error:
395
    PERROR("Out of memory%s.", "");
396
    return NULL;
397 398
}

399 400 401 402 403 404
PrrtPacket *
PrrtPacket_create_feedback_packet(uint8_t priority, prrtSequenceNumber_t sequenceNumber, prrtTimedelta_t groupRTT,
                                  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
405
                                  prrtPacketType_t ackPacketType, prrtTimedelta_t btl_pace) {
Andreas Schmidt's avatar
Andreas Schmidt committed
406
    PrrtPacket *packet = create_header(priority, sequenceNumber, PRRT_PACKET_FEEDBACK_HEADER_SIZE, PACKET_TYPE_FEEDBACK,
407
                                       0);
Andreas Schmidt's avatar
Andreas Schmidt committed
408

409
    PrrtPacketFeedbackPayload *payload = calloc(1, packet->payloadLength);
410
    check_mem(payload);
Andreas Schmidt's avatar
Andreas Schmidt committed
411
    packet->payload = payload;
Andreas Schmidt's avatar
Andreas Schmidt committed
412
    payload->btl_pace = btl_pace;
413 414
    payload->groupRTT_us = groupRTT;
    payload->forwardTripTimestamp_us = forwardTripTime;
Andreas Schmidt's avatar
Andreas Schmidt committed
415 416
    payload->erasureCount = erasureCount;
    payload->packetCount = packetCount;
417 418 419 420
    payload->gapLength = gapLength;
    payload->gapCount = gapCount;
    payload->burstLength = burstLength;
    payload->burstCount = burstCount;
421 422
    payload->ackSequenceNumber = ackSequenceNumber;
    payload->ackPacketType = ackPacketType;
423

Andreas Schmidt's avatar
Andreas Schmidt committed
424
    return packet;
425 426

    error:
427
    return NULL;
428
}