aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/internal/ccnx_InterestFacadeV1.c
blob: 5fca3cea2c398ebe91cc30c85c515c5bba60df9e (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
/*
 * 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 <stdlib.h>
#include <LongBow/runtime.h>

#include <ccnx/common/internal/ccnx_InterestFacadeV1.h>
#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_TlvDictionary.h>
#include <ccnx/common/internal/ccnx_InterestDefault.h>


// =====================

static void
_assertInvariants(const CCNxTlvDictionary *interestDictionary)
{
    assertNotNull(interestDictionary, "Dictionary is null");
    assertTrue((ccnxTlvDictionary_IsInterest(interestDictionary) ||
                ccnxTlvDictionary_IsInterestReturn(interestDictionary)), "Dictionary is not an interest");
    assertTrue(ccnxTlvDictionary_GetSchemaVersion(interestDictionary) == CCNxTlvDictionary_SchemaVersion_V1,
               "Dictionary is wrong schema Interest, got %d expected %d",
               ccnxTlvDictionary_GetSchemaVersion(interestDictionary), CCNxTlvDictionary_SchemaVersion_V1);
}

static uint32_t
_fetchUint32(const CCNxTlvDictionary *interestDictionary, uint32_t key, uint32_t defaultValue)
{
    if (ccnxTlvDictionary_IsValueInteger(interestDictionary, key)) {
        return (uint32_t) ccnxTlvDictionary_GetInteger(interestDictionary, key);
    }
    return defaultValue;
}

static bool
_ccnxInterestFacadeV1_SetContentObjectHashRestriction(CCNxTlvDictionary *interestDictionary, const PARCBuffer *contentObjectHash)
{
    _assertInvariants(interestDictionary);
    PARCCryptoHash *hash = parcCryptoHash_Create(PARCCryptoHashType_SHA256, contentObjectHash);
    bool result = ccnxTlvDictionary_PutObject(interestDictionary,
                                              CCNxCodecSchemaV1TlvDictionary_MessageFastArray_OBJHASH_RESTRICTION,
                                              hash);
    parcCryptoHash_Release(&hash);
    return result;
}

// forward declaration. Body below, in Getters.
static CCNxName *
_ccnxInterestFacadeV1_GetName(const CCNxTlvDictionary *interestDictionary);

static bool
_ccnxInterestFacadeV1_SetPayloadWithId(CCNxTlvDictionary *interestDictionary, const PARCBuffer *payload, const CCNxInterestPayloadId *payloadId)
{
    bool result = false;

    if (payload) {
        result = ccnxTlvDictionary_PutBuffer(interestDictionary,
                                             CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD, payload);
        if (payloadId != NULL) {
            CCNxName *name = _ccnxInterestFacadeV1_GetName(interestDictionary);
            ccnxName_Append(name, ccnxInterestPayloadId_GetNameSegment(payloadId));
        }
    }
    return result;
}

static bool
_ccnxInterestFacadeV1_SetPayloadAndId(CCNxTlvDictionary *interestDictionary, const PARCBuffer *payload)
{
    bool result = false;

    if (payload) {
        CCNxInterestPayloadId *pid = ccnxInterestPayloadId_CreateAsSHA256Hash(payload);
        _ccnxInterestFacadeV1_SetPayloadWithId(interestDictionary, payload, pid);
        ccnxInterestPayloadId_Release(&pid);
    }
    return result;
}

static bool
_ccnxInterestFacadeV1_SetPayload(CCNxTlvDictionary *interestDictionary, const PARCBuffer *payload)
{
    bool result = false;

    if (payload) {
        _ccnxInterestFacadeV1_SetPayloadWithId(interestDictionary, payload, NULL);
    }
    return result;
}

static bool
_ccnxInterestFacadeV1_SetLifetime(CCNxTlvDictionary *interestDictionary, uint32_t lifetimeInMillis)
{
    return ccnxTlvDictionary_PutInteger(interestDictionary,
                                        CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_InterestLifetime, lifetimeInMillis);
}

static bool
_ccnxInterestFacadeV1_SetKeyIdRestriction(CCNxTlvDictionary *interestDictionary, const PARCBuffer *keyId)
{
    _assertInvariants(interestDictionary);
    PARCCryptoHash *hash = parcCryptoHash_Create(PARCCryptoHashType_SHA256, keyId);
    bool result = ccnxTlvDictionary_PutObject(interestDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_KEYID_RESTRICTION, hash);
    parcCryptoHash_Release(&hash);
    return result;
}

static bool
_ccnxInterestFacadeV1_SetHopLimit(CCNxTlvDictionary *interestDictionary, uint32_t hopLimit)
{
    _assertInvariants(interestDictionary);
    return ccnxTlvDictionary_PutInteger(interestDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_HOPLIMIT, hopLimit);
}

// =====================
// Getters

static CCNxName *
_ccnxInterestFacadeV1_GetName(const CCNxTlvDictionary *interestDictionary)
{
    int key = CCNxCodecSchemaV1TlvDictionary_MessageFastArray_NAME;
    _assertInvariants(interestDictionary);
    if (ccnxTlvDictionary_IsValueName(interestDictionary, key)) {
        return ccnxTlvDictionary_GetName(interestDictionary, key);
    }
    return NULL;
}

static uint32_t
_ccnxInterestFacadeV1_GetLifetime(const CCNxTlvDictionary *interestDictionary)
{
    _assertInvariants(interestDictionary);
    return _fetchUint32(interestDictionary, CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_InterestLifetime, CCNxInterestDefault_LifetimeMilliseconds);
}

static PARCBuffer *
_ccnxInterestFacadeV1_GetKeyIdRestriction(const CCNxTlvDictionary *interestDictionary)
{
    _assertInvariants(interestDictionary);
    PARCCryptoHash *hash = ccnxTlvDictionary_GetObject(interestDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_KEYID_RESTRICTION);
    if (hash != NULL) {
        return parcCryptoHash_GetDigest(hash);
    } else {
        return NULL;
    }
}

static PARCBuffer *
_ccnxInterestFacadeV1_GetContentObjectHashRestriction(const CCNxTlvDictionary *interestDictionary)
{
    _assertInvariants(interestDictionary);
    PARCCryptoHash *hash = ccnxTlvDictionary_GetObject(interestDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_OBJHASH_RESTRICTION);
    if (hash != NULL) {
        return parcCryptoHash_GetDigest(hash);
    } else {
        return NULL;
    }
}

static PARCBuffer *
_ccnxInterestFacadeV1_GetPayload(const CCNxTlvDictionary *interestDictionary)
{
    _assertInvariants(interestDictionary);
    return ccnxTlvDictionary_GetBuffer(interestDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_PAYLOAD);
}

static uint32_t
_ccnxInterestFacadeV1_GetHopLimit(const CCNxTlvDictionary *interestDictionary)
{
    _assertInvariants(interestDictionary);
    return _fetchUint32(interestDictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_HOPLIMIT, CCNxInterestDefault_HopLimit);
}

// =====================
// Miscellaneous

static void
_ccnxInterestFacadeV1_AssertValid(const CCNxTlvDictionary *interestDictionary)
{
    _assertInvariants(interestDictionary);
    assertTrue(ccnxTlvDictionary_IsValueName(interestDictionary,
                                             CCNxCodecSchemaV1TlvDictionary_MessageFastArray_NAME), "Name field is not a name");
}

static bool
_ccnxInterestFacadeV1_Equals(const CCNxTlvDictionary *a, const CCNxTlvDictionary *b)
{
    return ccnxTlvDictionary_Equals(a, b);
}

static void
_ccnxInterestFacadeV1_Display(const CCNxTlvDictionary *interestDictionary, size_t indentation)
{
    _assertInvariants(interestDictionary);
    ccnxTlvDictionary_Display(interestDictionary, (unsigned) indentation);
}


// =====================
// Creation

static CCNxTlvDictionary *
_ccnxInterestFacadeV1_Create(const CCNxName   *name,                       // required
                             const uint32_t lifetimeMilliseconds,          // may use DefaultLimetimeMilliseconds
                             const PARCBuffer *keyId,                      // may be NULL
                             const PARCBuffer *contentObjectHash,          // may be NULL
                             const uint32_t hopLimit)                      // may be DefaultHopLimit
{
    assertNotNull(name, "Parameter name must be non-null");

    CCNxTlvDictionary *dictionary = ccnxCodecSchemaV1TlvDictionary_CreateInterest();

    if (dictionary) {
        ccnxTlvDictionary_PutName(dictionary, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_NAME, name);

        if (lifetimeMilliseconds != CCNxInterestDefault_LifetimeMilliseconds) {
            _ccnxInterestFacadeV1_SetLifetime(dictionary, lifetimeMilliseconds);
        }

        if (keyId) {
            _ccnxInterestFacadeV1_SetKeyIdRestriction(dictionary, keyId);
        }

        if (contentObjectHash) {
            _ccnxInterestFacadeV1_SetContentObjectHashRestriction(dictionary, contentObjectHash);
        }

        if (hopLimit != CCNxInterestDefault_HopLimit) {
            _ccnxInterestFacadeV1_SetHopLimit(dictionary, hopLimit);
        }
    } else {
        trapOutOfMemory("Could not allocate an Interest");
    }

    return dictionary;
}

static CCNxTlvDictionary *
_ccnxInterestFacadeV1_CreateSimple(const CCNxName *name)
{
    return _ccnxInterestFacadeV1_Create(name,
                                        CCNxInterestDefault_LifetimeMilliseconds,
                                        NULL,                         // keyid
                                        NULL,                         // content object hash
                                        CCNxInterestDefault_HopLimit);
}

CCNxInterestInterface CCNxInterestFacadeV1_Implementation = {
    .description                     = "CCNxInterestFacadeV1_Implementation",

    .createSimple                    = &_ccnxInterestFacadeV1_CreateSimple,
    .create                          = &_ccnxInterestFacadeV1_Create,

    .getName                         = &_ccnxInterestFacadeV1_GetName,

    .setContentObjectHashRestriction = &_ccnxInterestFacadeV1_SetContentObjectHashRestriction,
    .getContentObjectHashRestriction = &_ccnxInterestFacadeV1_GetContentObjectHashRestriction,

    .setLifetime                     = &_ccnxInterestFacadeV1_SetLifetime,
    .getLifetime                     = &_ccnxInterestFacadeV1_GetLifetime,

    .setKeyIdRestriction             = &_ccnxInterestFacadeV1_SetKeyIdRestriction,
    .getKeyIdRestriction             = &_ccnxInterestFacadeV1_GetKeyIdRestriction,

    .getHopLimit                     = &_ccnxInterestFacadeV1_GetHopLimit,
    .setHopLimit                     = &_ccnxInterestFacadeV1_SetHopLimit,

    .getPayload                      = &_ccnxInterestFacadeV1_GetPayload,

    .setPayload                      = &_ccnxInterestFacadeV1_SetPayload,
    .setPayloadAndId                 = &_ccnxInterestFacadeV1_SetPayloadAndId,
    .setPayloadWithId                = &_ccnxInterestFacadeV1_SetPayloadWithId,

    .toString                        = NULL,
    .equals                          = &_ccnxInterestFacadeV1_Equals,
    .display                         = &_ccnxInterestFacadeV1_Display,

    .assertValid                     = &_ccnxInterestFacadeV1_AssertValid,
};