aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_PacketEncoder.c
blob: bddd32d4e16cb5c46fe8b641fc317bfa80885e3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 */

#include <config.h>
#include <stdio.h>
#include <sys/time.h>
#include <inttypes.h>

#include <LongBow/runtime.h>
#include <parc/algol/parc_Memory.h>

#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_Types.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_PacketEncoder.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_FixedHeaderEncoder.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_OptionalHeadersEncoder.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_MessageEncoder.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_ValidationEncoder.h>

#include <ccnx/common/internal/ccnx_InterestDefault.h>
#include <ccnx/common/internal/ccnx_ValidationFacadeV1.h>
#include <ccnx/common/internal/ccnx_WireFormatMessageInterface.h>

// =====================================================
// Private API

static uint8_t
_getHopLimit(CCNxTlvDictionary *packetDictionary)
{
    uint8_t hoplimit = (uint8_t) CCNxInterestDefault_HopLimit;

    if (ccnxTlvDictionary_IsValueInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_HOPLIMIT)) {
        hoplimit = (uint8_t) ccnxTlvDictionary_GetInteger(packetDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_HOPLIMIT);
    }
    return hoplimit;
}

static uint8_t
_getInterestReturnCode(CCNxTlvDictionary *packetDictionary)
{
    uint8_t returnCode = (uint8_t) 0;

    if (ccnxTlvDictionary_IsValueInteger(packetDictionary,
                                         CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_InterestReturnCode)) {
        returnCode =
            (uint8_t) ccnxTlvDictionary_GetInteger(packetDictionary,
                                                   CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_InterestReturnCode);
    }
    return returnCode;
}

/**
 * Creates a fixed header from the given parameters and encodes in network byte order
 *
 * All parameters in host byte order.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return non-negative The total bytes appended to the encode buffer
 * @return -1 An error
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static ssize_t
_encodeFixedHeader(CCNxCodecTlvEncoder *fixedHeaderEncoder,
                   CCNxTlvDictionary *packetDictionary,
                   int packetType,
                   ssize_t headerLength,
                   ssize_t packetLength)
{
    CCNxCodecSchemaV1FixedHeader fixedHeader;
    memset(&fixedHeader, 0, sizeof(fixedHeader));

    fixedHeader.version = 1;
    fixedHeader.packetType = packetType;
    fixedHeader.packetLength = packetLength;
    fixedHeader.headerLength = headerLength;

    if ((packetType == CCNxCodecSchemaV1Types_PacketType_Interest) ||
        (packetType == CCNxCodecSchemaV1Types_PacketType_InterestReturn)) {
        CCNxCodecSchemaV1InterestHeader *interestHeader = (CCNxCodecSchemaV1InterestHeader *) &fixedHeader;
        interestHeader->hopLimit = _getHopLimit(packetDictionary);
        if (packetType == CCNxCodecSchemaV1Types_PacketType_InterestReturn) {
            interestHeader->returnCode = _getInterestReturnCode(packetDictionary);
        }
    }

    return ccnxCodecSchemaV1FixedHeaderEncoder_EncodeHeader(fixedHeaderEncoder, &fixedHeader);
}

static ssize_t
_encodeOptionalHeaders(CCNxCodecTlvEncoder *optionalHeaderEncoder, CCNxTlvDictionary *packetDictionary)
{
    // Optional Headers do not have a container, so just append them right to the buffer
    size_t optionalHeadersLength = 0;
    optionalHeadersLength = ccnxCodecSchemaV1OptionalHeadersEncoder_Encode(optionalHeaderEncoder, packetDictionary);
    return optionalHeadersLength;
}

/**
 * CPI payload is simply a dump of the PAYLOAD dictionary entry.
 *
 * There are no inner TLVs of this message, so it is not encoded like a normal message
 * with a call to ccnxCodecSchemaV1MessageEncoder_Encode().  Rather it is written here.
 *
 * @param [<#in#> | <#out#> | <#in,out#>] <#name#> <#description#>
 *
 * @return non-negative The number of bytes appended to the buffer
 * @return negative An error
 *
 * Example:
 * @code
 * {
 *     <#example#>
 * }
 * @endcode
 */
static ssize_t
_encodeCPI(CCNxCodecTlvEncoder *cpiEncoder, CCNxTlvDictionary *packetDictionary)
{
    // Optional Headers do not have a container, so just append them right to the buffer
    size_t payloadLength = 0;

    if (ccnxTlvDictionary_IsValueJson(packetDictionary,
                                      CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD)) {
        PARCJSON *json = ccnxTlvDictionary_GetJson(packetDictionary,
                                                   CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD);
        if (json) {
            char *jsonString = parcJSON_ToCompactString(json);

            payloadLength = strlen(jsonString);
            ccnxCodecTlvEncoder_AppendRawArray(cpiEncoder, payloadLength, (uint8_t * ) jsonString);
            parcMemory_Deallocate((void **) &jsonString);
        }
    } else {
        PARCBuffer *payload = ccnxTlvDictionary_GetBuffer(packetDictionary,
                                                          CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD);

        payloadLength = parcBuffer_Remaining(payload);
        uint8_t *overlay = parcBuffer_Overlay(payload, 0);
        ccnxCodecTlvEncoder_AppendRawArray(cpiEncoder, payloadLength, overlay);
    }
    return payloadLength;
}

/**
 * Encode the CCNx Message
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [out] packetTypePtr The type to use for the PacketType based on the message type
 *
 * @retval non-negative the bytes appended to the encoder
 * @retval negative An error
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static ssize_t
_encodeMessage(CCNxCodecTlvEncoder *packetEncoder, CCNxTlvDictionary *packetDictionary, CCNxCodecSchemaV1Types_PacketType *packetTypePtr)
{
    ssize_t startPosition = ccnxCodecTlvEncoder_Position(packetEncoder);
    ssize_t innerLength = -1;

    // what kind of message is it?  need this to set the packetTypePtr

    if (ccnxTlvDictionary_IsInterest(packetDictionary)) {
        *packetTypePtr = CCNxCodecSchemaV1Types_PacketType_Interest;
        ccnxCodecTlvEncoder_AppendContainer(packetEncoder, CCNxCodecSchemaV1Types_MessageType_Interest, 0);
        innerLength = ccnxCodecSchemaV1MessageEncoder_Encode(packetEncoder, packetDictionary);
    } else if (ccnxTlvDictionary_IsInterestReturn(packetDictionary)) {
        *packetTypePtr = CCNxCodecSchemaV1Types_PacketType_InterestReturn;
        ccnxCodecTlvEncoder_AppendContainer(packetEncoder, CCNxCodecSchemaV1Types_MessageType_Interest, 0);
        innerLength = ccnxCodecSchemaV1MessageEncoder_Encode(packetEncoder, packetDictionary);
    } else if (ccnxTlvDictionary_IsContentObject(packetDictionary)) {
        *packetTypePtr = CCNxCodecSchemaV1Types_PacketType_ContentObject;
        ccnxCodecTlvEncoder_AppendContainer(packetEncoder, CCNxCodecSchemaV1Types_MessageType_ContentObject, 0);
        innerLength = ccnxCodecSchemaV1MessageEncoder_Encode(packetEncoder, packetDictionary);
    } else if (ccnxTlvDictionary_IsControl(packetDictionary)) {
        *packetTypePtr = CCNxCodecSchemaV1Types_PacketType_Control;
        ccnxCodecTlvEncoder_AppendContainer(packetEncoder, CCNxCodecSchemaV1Types_MessageType_Control, 0);
        innerLength = _encodeCPI(packetEncoder, packetDictionary);
    } else if (ccnxTlvDictionary_IsManifest(packetDictionary)) {
        *packetTypePtr = CCNxCodecSchemaV1Types_PacketType_ContentObject;
        ccnxCodecTlvEncoder_AppendContainer(packetEncoder, CCNxCodecSchemaV1Types_MessageType_Manifest, 0);
        innerLength = ccnxCodecSchemaV1MessageEncoder_Encode(packetEncoder, packetDictionary);
    }

    if (innerLength >= 0) {
        // For a 0 length message, we do not backup and erase the TLV container.
        ccnxCodecTlvEncoder_SetContainerLength(packetEncoder, startPosition, innerLength);
        ssize_t endPosition = ccnxCodecTlvEncoder_Position(packetEncoder);
        innerLength = endPosition - startPosition;
    } else {
        CCNxCodecError *error = ccnxCodecError_Create(TLV_MISSING_MANDATORY, __func__, __LINE__, ccnxCodecTlvEncoder_Position(packetEncoder));
        ccnxCodecTlvEncoder_SetError(packetEncoder, error);
        ccnxCodecError_Release(&error);
    }

    return innerLength;
}

static ssize_t
_encodeValidationAlg(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t innerLength = 0;

    // There must be a CryptoSuite in the packet to sign it.
    // Temporary exception for Content Objects, which are all signed if the codec has a signer.
    if (ccnxValidationFacadeV1_HasCryptoSuite(packetDictionary) || ccnxTlvDictionary_IsContentObject(packetDictionary)) {
        ssize_t startPosition = ccnxCodecTlvEncoder_Position(encoder);

        ccnxCodecTlvEncoder_AppendContainer(encoder, CCNxCodecSchemaV1Types_MessageType_ValidationAlg, 0);
        innerLength = ccnxCodecSchemaV1ValidationEncoder_EncodeAlg(encoder, packetDictionary);

        if (innerLength == 0) {
            // backup and erase the container
            ccnxCodecTlvEncoder_SetPosition(encoder, startPosition);
        } else if (innerLength >= 0) {
            ccnxCodecTlvEncoder_SetContainerLength(encoder, startPosition, innerLength);
            ssize_t endPosition = ccnxCodecTlvEncoder_Position(encoder);
            return endPosition - startPosition;
        }
    }

    return innerLength;
}

static ssize_t
_encodeValidationPayload(CCNxCodecTlvEncoder *encoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t startPosition = ccnxCodecTlvEncoder_Position(encoder);

    ccnxCodecTlvEncoder_AppendContainer(encoder, CCNxCodecSchemaV1Types_MessageType_ValidationPayload, 0);
    ssize_t innerLength = ccnxCodecSchemaV1ValidationEncoder_EncodePayload(encoder, packetDictionary);

    if (innerLength == 0) {
        // backup and erase the container
        ccnxCodecTlvEncoder_SetPosition(encoder, startPosition);
    } else if (innerLength > 0) {
        ccnxCodecTlvEncoder_SetContainerLength(encoder, startPosition, innerLength);
        ssize_t endPosition = ccnxCodecTlvEncoder_Position(encoder);
        return endPosition - startPosition;
    }

    return innerLength;
}

// =====================================================
// Public API

CCNxCodecNetworkBufferIoVec *
ccnxCodecSchemaV1PacketEncoder_DictionaryEncode(CCNxTlvDictionary *packetDictionary, PARCSigner *signer)
{
    CCNxCodecNetworkBufferIoVec *outputBuffer = NULL;

    CCNxCodecTlvEncoder *packetEncoder = ccnxCodecTlvEncoder_Create();

    if (signer) {
//        ccnxCodecTlvEncoder_SetSigner(packetEncoder, signer);
    }

    ssize_t encodedLength = ccnxCodecSchemaV1PacketEncoder_Encode(packetEncoder, packetDictionary);
    if (encodedLength > 0) {
        ccnxCodecTlvEncoder_Finalize(packetEncoder);
        outputBuffer = ccnxCodecTlvEncoder_CreateIoVec(packetEncoder);
    }

    trapUnexpectedStateIf(encodedLength < 0 && !ccnxCodecTlvEncoder_HasError(packetEncoder),
                          "Got error length but no error set");

    assertFalse(ccnxCodecTlvEncoder_HasError(packetEncoder), "ENCODING ERROR")
    {
        printf("ERROR: %s\n", ccnxCodecError_ToString(ccnxCodecTlvEncoder_GetError(packetEncoder)));
        ccnxTlvDictionary_Display(packetDictionary, 3);
    }

    ccnxCodecTlvEncoder_Destroy(&packetEncoder);

    // return a reference counted copy so it won't be destroyed by ccnxCodecTlvEncoder_Destroy
    return outputBuffer;
}

ssize_t
ccnxCodecSchemaV1PacketEncoder_Encode(CCNxCodecTlvEncoder *packetEncoder, CCNxTlvDictionary *packetDictionary)
{
    ssize_t length = -1;

    // We will need to go back and fixedup the headers
    ssize_t fixedHeaderPosition = ccnxCodecTlvEncoder_Position(packetEncoder);
    ssize_t fixedHeaderLength = _encodeFixedHeader(packetEncoder, packetDictionary, -1, 0, 0);

    ssize_t optionalHeadersLength = _encodeOptionalHeaders(packetEncoder, packetDictionary);

    if (optionalHeadersLength >= 0) {
        ccnxCodecTlvEncoder_MarkSignatureStart(packetEncoder);

        CCNxCodecSchemaV1Types_PacketType messageType = -1;

        ssize_t messageLength = _encodeMessage(packetEncoder, packetDictionary, &messageType);

        if (messageLength >= 0) {
            // validation is optional, so it's ok if its 0 length
            ssize_t validationAlgLength = _encodeValidationAlg(packetEncoder, packetDictionary);
            ssize_t validationPayloadLength = 0;
            if (validationAlgLength > 0) {
                ccnxCodecTlvEncoder_MarkSignatureEnd(packetEncoder);

                validationPayloadLength = _encodeValidationPayload(packetEncoder, packetDictionary);
            }

            if (validationAlgLength >= 0 && validationPayloadLength >= 0) {
                // now fix up the fixed header
                size_t endPosition = ccnxCodecTlvEncoder_Position(packetEncoder);

                size_t headerLength = fixedHeaderLength + optionalHeadersLength;
                size_t packetLength = headerLength + messageLength + validationAlgLength + validationPayloadLength;

                // Will this work for InterestReturn?  As long as _encodeMessage returns InterestReturn it
                // will be ok.
                int packetType = messageType;

                ccnxCodecTlvEncoder_SetPosition(packetEncoder, fixedHeaderPosition);
                _encodeFixedHeader(packetEncoder, packetDictionary, packetType, headerLength, packetLength);
                ccnxCodecTlvEncoder_SetPosition(packetEncoder, endPosition);
                length = endPosition - fixedHeaderPosition;

                trapUnexpectedStateIf(packetLength != length, "packet length %zu not equal to measured length %zd", packetLength, length);
            }
        }
    }

    return length;
}