aboutsummaryrefslogtreecommitdiffstats
path: root/metis/ccnx/forwarder/metis/tlv/metis_TlvSchemaV0.c
blob: e4bba4c86da93cc61a114a9a2d73b8e6f15e1cf8 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * 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.
 */

/**
 */

/**
 * THIS IS A DEPRECATED CLASS. V0 IS NO LONGER IN USE.
 */

#include <config.h>

#include <LongBow/runtime.h>

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>

#include <ccnx/forwarder/metis/tlv/metis_Tlv.h>
#include <ccnx/forwarder/metis/tlv/metis_TlvExtent.h>

#include <parc/algol/parc_Memory.h>
#include <parc/security/parc_CryptoHasher.h>

#include <ccnx/forwarder/metis/tlv/metis_TlvSchemaV0.h>

typedef struct __attribute__ ((__packed__)) metis_tlv_fixed_header {
    uint8_t version;
    uint8_t packetType;
    uint16_t payloadLength;
    uint16_t reserved;
    uint16_t headerLength;
} _MetisTlvFixedHeaderV0;

#define FIXED_HEADER_LEN 8


#define TotalPacketLength(fhPtr) (htons((fhPtr)->payloadLength) + htons((fhPtr)->headerLength) + FIXED_HEADER_LEN)


#define METIS_PACKET_TYPE_INTEREST 0x01
#define METIS_PACKET_TYPE_CONTENT  0x02

// The message type for a Metis control packet
#define METIS_PACKET_TYPE_CONTROL 0xA4

// -----------------------------
// in host byte order

#define T_NAME     0x0000

#define T_HOPLIMIT 0x0002
#define T_INTFRAG  0x0003
#define T_OBJFRAG  0x0004

// inside interest
#define T_KEYID    0x0001
#define T_OBJHASH  0x0002
#define T_SCOPE    0x0003
#define T_INTLIFE  0x0005

// inside an object
#define T_NAMEAUTH 0x0002
#define T_CONTENTS 0x0004
#define T_SIGBLOCK 0x0005
#define T_SIGBITS  0x000E

// inside a CPI
#define T_CPI      0xBEEF

// -----------------------------
// Internal API

static void
_parsePerHopV0(const uint8_t *packet, size_t offset, size_t endHeaders, MetisTlvSkeleton *skeleton)
{
    int foundCount = 0;
    const size_t tl_length = 4;

    // we only parse to the end of the per-hop headers or until we've found
    // the 1 header we want is hoplimit.  Others ignored.
    while (offset < endHeaders && foundCount < 1) {
        MetisTlvType *tlv = (MetisTlvType *) (packet + offset);
        uint16_t type = htons(tlv->type);
        uint16_t v_length = htons(tlv->length);

        // move past the TL header
        offset += tl_length;

        switch (type) {
            case T_HOPLIMIT:
                metisTlvSkeleton_SetHopLimit(skeleton, offset, v_length);
                foundCount++;
                break;

            default:
                break;
        }

        offset += v_length;
    }
}

static void
_parseNameAuth(const uint8_t *packet, size_t offset, size_t endSection, MetisTlvSkeleton *skeleton)
{
    const size_t tl_length = 4;

    while (offset < endSection) {
        MetisTlvType *tlv = (MetisTlvType *) (packet + offset);
        uint16_t type = htons(tlv->type);
        uint16_t v_length = htons(tlv->length);

        // move past the TL header
        offset += tl_length;

        switch (type) {
            case T_KEYID:
                metisTlvSkeleton_SetKeyId(skeleton, offset, v_length);
                return;

            default:
                break;
        }

        offset += v_length;
    }
}

static void
_parseObjectV0(const uint8_t *packet, size_t offset, size_t endMessage, MetisTlvSkeleton *skeleton)
{
    int foundCount = 0;
    const size_t tl_length = 4;

    // skip the opending content object TLV
    offset += 4;

    // parse to the end or until we find the two things we need (name, keyid)
    while (offset < endMessage && foundCount < 2) {
        MetisTlvType *tlv = (MetisTlvType *) (packet + offset);
        uint16_t type = htons(tlv->type);
        uint16_t v_length = htons(tlv->length);

        // move past the TL header
        offset += tl_length;

        switch (type) {
            case T_NAME:
                metisTlvSkeleton_SetName(skeleton, offset, v_length);
                foundCount++;
                break;

            case T_NAMEAUTH:
                _parseNameAuth(packet, offset, offset + v_length, skeleton);
                foundCount++;
                break;

            default:
                break;
        }

        offset += v_length;
    }
}

static void
_parseInterestV0(const uint8_t *packet, size_t offset, size_t endMessage, MetisTlvSkeleton *skeleton)
{
    int foundCount = 0;
    const size_t tl_length = sizeof(MetisTlvType);

    // skip the Interest wrapper
    offset += 4;

    // parse to the end or until we find all 5 things (name, keyid, objecthash, scope, interest lifetime)
    while (offset < endMessage && foundCount < 5) {
        MetisTlvType *tlv = (MetisTlvType *) (packet + offset);
        uint16_t type = htons(tlv->type);
        uint16_t v_length = htons(tlv->length);

        // skip past the TLV header
        offset += tl_length;

        switch (type) {
            case T_NAME:
                metisTlvSkeleton_SetName(skeleton, offset, v_length);
                foundCount++;
                break;

            case T_KEYID:
                metisTlvSkeleton_SetKeyId(skeleton, offset, v_length);
                foundCount++;
                break;

            case T_OBJHASH:
                metisTlvSkeleton_SetObjectHash(skeleton, offset, v_length);
                foundCount++;
                break;

            case T_INTLIFE:
                metisTlvSkeleton_SetInterestLifetime(skeleton, offset, v_length);
                foundCount++;
                break;

            default:
                break;
        }

        offset += v_length;
    }
}


static void
_parseControlPlaneInterface(const uint8_t *packet, size_t offset, size_t endMessage, MetisTlvSkeleton *skeleton)
{
    int foundCount = 0;
    const size_t tl_length = 4;

    // parse to the end or until we find all 5 things (name, keyid, objecthash, scope, interest lifetime)
    while (offset < endMessage && foundCount < 1) {
        MetisTlvType *tlv = (MetisTlvType *) (packet + offset);
        uint16_t type = htons(tlv->type);
        uint16_t v_length = htons(tlv->length);

        // skip past the TLV header
        offset += tl_length;

        switch (type) {
            case T_CPI:
                metisTlvSkeleton_SetCPI(skeleton, offset, v_length);
                foundCount++;
                break;

            default:
                break;
        }

        offset += v_length;
    }
}

static PARCCryptoHash *
_computeHash(const uint8_t *packet, size_t offset, size_t endMessage)
{
    PARCCryptoHasher *hasher = parcCryptoHasher_Create(PARCCryptoHashType_SHA256);
    parcCryptoHasher_Init(hasher);
    parcCryptoHasher_UpdateBytes(hasher, packet + offset, endMessage - offset);
    PARCCryptoHash *hash = parcCryptoHasher_Finalize(hasher);
    parcCryptoHasher_Release(&hasher);
    return hash;
}

// ==================
// TlvOps functions

static PARCBuffer *
_encodeControlPlaneInformation(const CCNxControl *cpiControlMessage)
{
    PARCJSON *json = ccnxControl_GetJson(cpiControlMessage);
    char *str = parcJSON_ToCompactString(json);

    // include +1 because we need the NULL byte
    size_t len = strlen(str) + 1;

    size_t packetLength = sizeof(_MetisTlvFixedHeaderV0) + sizeof(MetisTlvType) + len;
    PARCBuffer *packet = parcBuffer_Allocate(packetLength);

    _MetisTlvFixedHeaderV0 hdr;
    memset(&hdr, 0, sizeof(hdr));
    hdr.version = 0;
    hdr.packetType = METIS_PACKET_TYPE_CONTROL;
    hdr.payloadLength = htons(len + sizeof(MetisTlvType));

    parcBuffer_PutArray(packet, sizeof(hdr), (uint8_t *) &hdr);

    MetisTlvType tlv = { .type = htons(T_CPI), .length = htons(len) };
    parcBuffer_PutArray(packet, sizeof(tlv), (uint8_t *) &tlv);

    parcBuffer_PutArray(packet, len, (uint8_t *) str);

    parcMemory_Deallocate((void **) &str);
    return parcBuffer_Flip(packet);
}


static PARCCryptoHash *
_computeContentObjectHash(const uint8_t *packet)
{
    assertNotNull(packet, "Parameter packet must be non-null");

    _MetisTlvFixedHeaderV0 *hdr = (_MetisTlvFixedHeaderV0 *) packet;
    if (hdr->packetType == METIS_PACKET_TYPE_CONTENT) {
        size_t headerLength = htons(hdr->headerLength);
        size_t endHeaders = FIXED_HEADER_LEN + headerLength;
        size_t endPacket = TotalPacketLength(hdr);

        return _computeHash(packet, endHeaders, endPacket);
    }

    return NULL;
}

static bool
_isPacketTypeInterest(const uint8_t *packet)
{
    _MetisTlvFixedHeaderV0 *hdr = (_MetisTlvFixedHeaderV0 *) packet;
    return (hdr->packetType == METIS_PACKET_TYPE_INTEREST);
}

static bool
_isPacketTypeInterestReturn(const uint8_t *packet)
{
    return false;
}

static bool
_isPacketTypeContentObject(const uint8_t *packet)
{
    _MetisTlvFixedHeaderV0 *hdr = (_MetisTlvFixedHeaderV0 *) packet;
    return (hdr->packetType == METIS_PACKET_TYPE_CONTENT);
}

static bool
_isPacketTypeControl(const uint8_t *packet)
{
    _MetisTlvFixedHeaderV0 *hdr = (_MetisTlvFixedHeaderV0 *) packet;
    return (hdr->packetType == METIS_PACKET_TYPE_CONTROL);
}

static bool
_isPacketTypeHopByHopFragment(const uint8_t *packet)
{
    // does not exist for version 0 packets
    return false;
}

static size_t
_fixedHeaderLength(const uint8_t *packet)
{
    return sizeof(_MetisTlvFixedHeaderV0);
}

static size_t
_totalHeaderLength(const uint8_t *packet)
{
    _MetisTlvFixedHeaderV0 *hdr = (_MetisTlvFixedHeaderV0 *) packet;

    return htons(hdr->headerLength) + sizeof(_MetisTlvFixedHeaderV0);
}

static size_t
_totalPacketLength(const uint8_t *packet)
{
    _MetisTlvFixedHeaderV0 *hdr = (_MetisTlvFixedHeaderV0 *) packet;

    return TotalPacketLength(hdr);
}

static bool
_parse(MetisTlvSkeleton *skeleton)
{
    _MetisTlvFixedHeaderV0 *hdr = (_MetisTlvFixedHeaderV0 *) metisTlvSkeleton_GetPacket(skeleton);
    size_t headerLength = htons(hdr->headerLength);

    size_t endHeaders = FIXED_HEADER_LEN + headerLength;
    size_t endPacket = TotalPacketLength(hdr);

    trapUnexpectedStateIf(hdr->version != 0, "Version not 0");

    switch (hdr->packetType) {
        case METIS_PACKET_TYPE_INTEREST:
            _parsePerHopV0(metisTlvSkeleton_GetPacket(skeleton), FIXED_HEADER_LEN, endHeaders, skeleton);
            _parseInterestV0(metisTlvSkeleton_GetPacket(skeleton), endHeaders, endPacket, skeleton);
            break;

        case METIS_PACKET_TYPE_CONTENT:
            _parsePerHopV0(metisTlvSkeleton_GetPacket(skeleton), FIXED_HEADER_LEN, endHeaders, skeleton);
            _parseObjectV0(metisTlvSkeleton_GetPacket(skeleton), endHeaders, endPacket, skeleton);
            break;

        case METIS_PACKET_TYPE_CONTROL:
            _parseControlPlaneInterface(metisTlvSkeleton_GetPacket(skeleton), endHeaders, endPacket, skeleton);
            break;

        default:
            break;
    }

    return true;
}

const MetisTlvOps MetisTlvSchemaV0_Ops = {
    .parse                         = _parse,
    .computeContentObjectHash      = _computeContentObjectHash,
    .encodeControlPlaneInformation = _encodeControlPlaneInformation,
    .fixedHeaderLength             = _fixedHeaderLength,
    .totalHeaderLength             = _totalHeaderLength,
    .totalPacketLength             = _totalPacketLength,
    .isPacketTypeInterest          = _isPacketTypeInterest,
    .isPacketTypeContentObject     = _isPacketTypeContentObject,
    .isPacketTypeInterestReturn    = _isPacketTypeInterestReturn,
    .isPacketTypeControl           = _isPacketTypeControl,
    .isPacketTypeHopByHopFragment  = _isPacketTypeHopByHopFragment,
};