aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/ccnx_WireFormatMessage.c
blob: 9fd82482849c3a0372213b959fbfa58771f98531 (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
/*
 * 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 <LongBow/runtime.h>

#include <ccnx/common/ccnx_WireFormatMessage.h>

void
ccnxWireFormatMessage_AssertValid(const CCNxWireFormatMessage *message)
{
    assertNotNull(message, "Must be a non-null pointer to a CCNxWireFormatMessage.");

    // Check for required fields in the underlying dictionary. Case 1036
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);
    assertNotNull(impl, "WireFormatMessage must have an valid implementation pointer.");
    if (impl->assertValid) {
        impl->assertValid(message);
    }
}

static CCNxWireFormatMessage *
_ccnxWireFormatMessage_CreateWithImpl(const CCNxWireFormatMessageInterface *impl, const PARCBuffer *wireFormatBuffer)
{
    CCNxWireFormatMessage *result = impl->create(wireFormatBuffer);
    return result;
}

CCNxWireFormatMessage *
ccnxWireFormatMessage_Create(const PARCBuffer *wireFormat)
{
    CCNxWireFormatMessage *result = NULL;

    uint8_t version = parcBuffer_GetAtIndex(wireFormat, 0);
    switch (version) {
        case CCNxTlvDictionary_SchemaVersion_V1:
            result = _ccnxWireFormatMessage_CreateWithImpl(&CCNxWireFormatFacadeV1_Implementation, wireFormat);
            break;
        default:
            // no action, will return NULL
            break;
    }

    return result;
}

static CCNxWireFormatMessageInterface *
_getImplForSchema(const CCNxTlvDictionary_SchemaVersion schemaVersion)
{
    CCNxWireFormatMessageInterface *result = NULL;
    switch (schemaVersion) {
        case CCNxTlvDictionary_SchemaVersion_V1:
            result = &CCNxWireFormatFacadeV1_Implementation;
            break;
        default:
            trapIllegalValue(data->fwd_state->nextMessage.version, "Unknown schema version: %d", schemaVersion);
    }
    return result;
}

CCNxWireFormatMessage *
ccnxWireFormatMessage_FromInterestPacketType(const CCNxTlvDictionary_SchemaVersion schemaVersion, const PARCBuffer *wireFormat)
{
    CCNxWireFormatMessageInterface *impl = _getImplForSchema(schemaVersion);
    CCNxWireFormatMessage *result = NULL;

    if (impl->fromInterestPacketType != NULL) {
        result = impl->fromInterestPacketType(wireFormat);
    }

    return result;
}

CCNxWireFormatMessage *
ccnxWireFormatMessage_FromInterestPacketTypeIoVec(const CCNxTlvDictionary_SchemaVersion schemaVersion, const CCNxCodecNetworkBufferIoVec *vec)
{
    CCNxWireFormatMessageInterface *impl = _getImplForSchema(schemaVersion);
    CCNxWireFormatMessage *result = NULL;

    if (impl->fromInterestPacketTypeIoVec != NULL) {
        result = impl->fromInterestPacketTypeIoVec(vec);
    }

    return result;
}

CCNxWireFormatMessage *
ccnxWireFormatMessage_FromContentObjectPacketType(const CCNxTlvDictionary_SchemaVersion schemaVersion, const PARCBuffer *wireFormat)
{
    CCNxWireFormatMessageInterface *impl = _getImplForSchema(schemaVersion);
    CCNxWireFormatMessage *result = NULL;

    if (impl->fromContentObjectPacketType != NULL) {
        result = impl->fromContentObjectPacketType(wireFormat);
    }

    return result;
}

CCNxWireFormatMessage *
ccnxWireFormatMessage_FromControlPacketType(const CCNxTlvDictionary_SchemaVersion schemaVersion, const PARCBuffer *wireFormat)
{
    CCNxWireFormatMessageInterface *impl = _getImplForSchema(schemaVersion);
    CCNxWireFormatMessage *result = NULL;

    if (impl->fromControlPacketType != NULL) {
        result = impl->fromControlPacketType(wireFormat);
    }

    return result;
}

CCNxTlvDictionary *
ccnxWireFormatMessage_GetDictionary(const CCNxWireFormatMessage *message)
{
    return (CCNxTlvDictionary *) message;
}


CCNxCodecNetworkBufferIoVec *
ccnxWireFormatMessage_GetIoVec(const CCNxWireFormatMessage *message)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    CCNxCodecNetworkBufferIoVec *result = NULL;

    if (impl->getIoVec != NULL) {
        result = impl->getIoVec(message);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_GetIoVec");
    }

    return result;
}

bool
ccnxWireFormatMessage_PutIoVec(CCNxWireFormatMessage *message, CCNxCodecNetworkBufferIoVec *vec)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    bool result = false;

    if (impl->putIoVec != NULL) {
        result = impl->putIoVec(message, vec);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_PutIoVec");
    }

    return result;
}

bool
ccnxWireFormatMessage_PutWireFormatBuffer(CCNxWireFormatMessage *message, PARCBuffer *buffer)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    bool result = false;

    if (impl->putWireFormatBuffer != NULL) {
        result = impl->putWireFormatBuffer(message, buffer);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_PutWireFormatBuffer");
    }

    return result;
}

PARCBuffer *
ccnxWireFormatMessage_GetWireFormatBuffer(const CCNxWireFormatMessage *message)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    PARCBuffer *result = NULL;

    if (impl->getWireFormatBuffer != NULL) {
        result = impl->getWireFormatBuffer(message);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_GetWireFormatBuffer");
    }

    return result;
}

void
ccnxWireFormatMessage_WriteToFile(const CCNxWireFormatMessage *message, const char *filename)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    if (impl->writeToFile != NULL) {
        impl->writeToFile(message, filename);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_WriteToFile");
    }
}

bool
ccnxWireFormatMessage_SetProtectedRegionStart(CCNxWireFormatMessage *message, size_t startPosition)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    bool result = false;

    if (impl->setProtectedRegionStart != NULL) {
        result = impl->setProtectedRegionStart(message, startPosition);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_SetProtectedRegionStart");
    }

    return result;
}

bool
ccnxWireFormatMessage_SetProtectedRegionLength(CCNxWireFormatMessage *message, size_t length)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    bool result = false;

    if (impl->setProtectedRegionLength != NULL) {
        result = impl->setProtectedRegionLength(message, length);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_SetProtectedRegionLength");
    }

    return result;
}


PARCCryptoHash *
ccnxWireFormatMessage_HashProtectedRegion(const CCNxWireFormatMessage *message, PARCCryptoHasher *hasher)
{
    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    PARCCryptoHash *result = NULL;

    if (impl->hashProtectedRegion != NULL) {
        result = impl->hashProtectedRegion(message, hasher);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_HashProtectedRegion");
    }

    return result;
}

PARCCryptoHash *
ccnxWireFormatMessage_CreateContentObjectHash(CCNxWireFormatMessage *message)
{
    ccnxWireFormatMessage_AssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    PARCCryptoHash *result = NULL;

    if (impl->computeContentObjectHash != NULL) {
        result = impl->computeContentObjectHash(message);
    } else {
        trapNotImplemented("ccnxWireFormatMessage_ComputeContentObjectHash");
    }

    return result;
}

CCNxWireFormatMessage *
ccnxWireFormatMessage_Acquire(const CCNxWireFormatMessage *message)
{
    return ccnxTlvDictionary_Acquire(message);
}

void
ccnxWireFormatMessage_Release(CCNxWireFormatMessage **message)
{
    ccnxTlvDictionary_Release(message);
}

bool
ccnxWireFormatMessage_SetHopLimit(CCNxWireFormatMessage *message, uint32_t hoplimit)
{
    bool result = false;

    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    if (impl != NULL) {
        result = impl->setHopLimit(message, hoplimit);
    }

    return result;
}

bool
ccnxWireFormatMessage_ConvertInterestToInterestReturn(CCNxWireFormatMessage *message, uint8_t returnCode)
{
    bool result = false;

    ccnxWireFormatMessage_OptionalAssertValid(message);
    CCNxWireFormatMessageInterface *impl = ccnxWireFormatMessageInterface_GetInterface(message);

    if (impl != NULL) {
        result = impl->convertInterestToInterestReturn(message, returnCode);
    }
    return result;
}