aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_RouteEntry.c
blob: 0de5c915e38e865c539c488fcf7020acc08a4f3d (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * 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 <stdlib.h>
#include <sys/time.h>

#include <ccnx/api/control/cpi_RouteEntry.h>
#include <ccnx/api/control/controlPlaneInterface.h>
#include <parc/algol/parc_Memory.h>

#include <limits.h>

#include <LongBow/runtime.h>

static const char *cpiPrefix = "PREFIX";
static const char *cpiInterface = "INTERFACE";
static const char *cpiFlags = "FLAGS";
static const char *cpiLifetime = "LIFETIME";
static const char *cpiNexthop = "NEXTHOP";
static const char *cpiProtocol = "PROTOCOL";
static const char *cpiRouteType = "ROUTETYPE";
static const char *cpiCost = "COST";
static const char *cpiSymbolic = "SYMBOLIC";

struct cpi_route_entry {
    bool hasInterfaceIndex;
    unsigned interfaceIndex;

    CCNxName *prefix;
    char *symbolic;
    CPIAddress *nexthop;
    CPINameRouteProtocolType routingProtocol;
    CPINameRouteType routeType;
    unsigned cost;

    bool hasLifetime;
    struct timeval lifetime;
};

void
cpiRouteEntry_Destroy(CPIRouteEntry **routeEntryPtr)
{
    assertNotNull(routeEntryPtr, "Parameter must be non-null double pointer");
    assertNotNull(*routeEntryPtr, "Parameter must dereference to non-null pointer");
    CPIRouteEntry *route = *routeEntryPtr;

    ccnxName_Release(&route->prefix);
    if (route->nexthop) {
        cpiAddress_Destroy(&route->nexthop);
    }

    if (route->symbolic) {
        parcMemory_Deallocate((void **) &route->symbolic);
    }

    parcMemory_Deallocate((void **) &route);
    *routeEntryPtr = NULL;
}

CPIRouteEntry *
cpiRouteEntry_CreateRouteToSelf(const CCNxName *prefix)
{
    void *optionalLifetime = NULL;
    void *nexthop = NULL;
    unsigned cost = 0;

    CPIRouteEntry *route = cpiRouteEntry_Create(ccnxName_Copy(prefix),
                                                CPI_CURRENT_INTERFACE,
                                                nexthop,
                                                cpiNameRouteProtocolType_LOCAL,
                                                cpiNameRouteType_LONGEST_MATCH,
                                                optionalLifetime,
                                                cost);
    return route;
}

char *
cpiRouteEntry_ToString(CPIRouteEntry *route)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();

    parcBufferComposer_Format(composer, "%6d %9.9s %7.7s %u ",
                              route->interfaceIndex,
                              cpiNameRouteProtocolType_ToString(route->routingProtocol),
                              cpiNameRouteType_ToString(route->routeType),
                              route->cost);

    if (route->symbolic != NULL) {
        parcBufferComposer_PutString(composer, route->symbolic);
    } else {
        parcBufferComposer_PutChar(composer, '-');
    }

    if (route->nexthop != NULL) {
        cpiAddress_BuildString(cpiRouteEntry_GetNexthop(route), composer);
    } else {
        parcBufferComposer_PutChar(composer, '-');
    }

    if (route->hasLifetime) {
#if __APPLE__
        parcBufferComposer_Format(composer, " %ld.%06d ", route->lifetime.tv_sec, route->lifetime.tv_usec);
#else
        parcBufferComposer_Format(composer, " %ld.%06ld ", route->lifetime.tv_sec, route->lifetime.tv_usec);
#endif
    } else {
        parcBufferComposer_Format(composer, " %8.8s ", "infinite");
    }

    char *ccnxName = ccnxName_ToString(cpiRouteEntry_GetPrefix(route));
    parcBufferComposer_PutString(composer, ccnxName);

    parcMemory_Deallocate((void **) &ccnxName);

    PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    char *result = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);

    parcBufferComposer_Release(&composer);
    return result;
}

CPIRouteEntry *
cpiRouteEntry_Create(CCNxName *prefix,
                     unsigned interfaceIndex,
                     const CPIAddress *optionalNexthop,
                     CPINameRouteProtocolType routingProtocol,
                     CPINameRouteType routeType,
                     const struct timeval *optionalLifetime,
                     unsigned cost)
{
    assertNotNull(prefix, "Parameter prefix must be non-null");

    CPIRouteEntry *route = parcMemory_AllocateAndClear(sizeof(CPIRouteEntry));
    assertNotNull(route, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIRouteEntry));
    route->prefix = prefix;
    route->symbolic = NULL;
    route->interfaceIndex = interfaceIndex;
    route->hasInterfaceIndex = true;
    route->nexthop = optionalNexthop != NULL ? cpiAddress_Copy(optionalNexthop) : NULL;
    route->routingProtocol = routingProtocol;
    route->routeType = routeType;
    route->cost = cost;

    if (optionalLifetime) {
        route->hasLifetime = true;
        route->lifetime = *optionalLifetime;
    } else {
        route->hasLifetime = false;
        route->lifetime = (struct timeval) { .tv_sec = INT_MAX, .tv_usec = 0 };
    }

    return route;
}

CPIRouteEntry *
cpiRouteEntry_CreateSymbolic(CCNxName *prefix,
                             const char *symbolicName,
                             CPINameRouteProtocolType routingProtocol,
                             CPINameRouteType routeType,
                             const struct timeval *optionalLifetime,
                             unsigned cost)
{
    assertNotNull(prefix, "Parameter prefix must be non-null");

    CPIRouteEntry *route = parcMemory_AllocateAndClear(sizeof(CPIRouteEntry));
    assertNotNull(route, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIRouteEntry));
    route->prefix = prefix;
    route->symbolic = parcMemory_StringDuplicate(symbolicName, strlen(symbolicName));
    route->interfaceIndex = UINT32_MAX;
    route->hasInterfaceIndex = false;
    route->nexthop = NULL;
    route->routingProtocol = routingProtocol;
    route->routeType = routeType;
    route->cost = cost;

    if (optionalLifetime) {
        route->hasLifetime = true;
        route->lifetime = *optionalLifetime;
    } else {
        route->hasLifetime = false;
        route->lifetime = (struct timeval) { .tv_sec = INT_MAX, .tv_usec = 0 };
    }

    return route;
}


CPIRouteEntry *
cpiRouteEntry_Copy(const CPIRouteEntry *original)
{
    assertNotNull(original, "Parameter a must be non-null");
    CPIRouteEntry *copy;

    if (original->hasInterfaceIndex) {
        copy = cpiRouteEntry_Create(
            ccnxName_Copy(original->prefix),
            original->interfaceIndex,
            (original->nexthop ? original->nexthop : NULL),
            original->routingProtocol,
            original->routeType,
            (original->hasLifetime ? &original->lifetime : NULL),
            original->cost);

        if (original->symbolic) {
            copy->symbolic = parcMemory_StringDuplicate(original->symbolic, strlen(original->symbolic));
        }
    } else {
        copy = cpiRouteEntry_CreateSymbolic(
            ccnxName_Copy(original->prefix),
            original->symbolic,
            original->routingProtocol,
            original->routeType,
            (original->hasLifetime ? &original->lifetime : NULL),
            original->cost);
    }
    return copy;
}

void
cpiRouteEntry_SetInterfaceIndex(CPIRouteEntry *route, unsigned interfaceIndex)
{
    assertNotNull(route, "Parameter a must be non-null");
    route->interfaceIndex = interfaceIndex;
    route->hasInterfaceIndex = true;
}


bool
cpiRouteEntry_Equals(const CPIRouteEntry *a, const CPIRouteEntry *b)
{
    assertNotNull(a, "Parameter a must be non-null");
    assertNotNull(b, "Parameter b must be non-null");
    if (a == b) {
        return true;
    }

    if (a->interfaceIndex == b->interfaceIndex) {
        if (a->routeType == b->routeType) {
            if (a->routingProtocol == b->routingProtocol) {
                if (a->cost == b->cost) {
                    if (ccnxName_Equals(a->prefix, b->prefix)) {
                        if (cpiAddress_Equals(a->nexthop, b->nexthop)) {
                            if (a->hasLifetime == b->hasLifetime) {
                                if (timercmp(&a->lifetime, &b->lifetime, ==)) {
                                    if (a->symbolic == b->symbolic || (a->symbolic != NULL && b->symbolic != NULL && strcasecmp(a->symbolic, b->symbolic) == 0)) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

const CCNxName *
cpiRouteEntry_GetPrefix(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->prefix;
}

unsigned
cpiRouteEntry_GetInterfaceIndex(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->interfaceIndex;
}

const CPIAddress *
cpiRouteEntry_GetNexthop(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->nexthop;
}

bool
cpiRouteEntry_HasLifetime(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->hasLifetime;
}

struct timeval
cpiRouteEntry_GetLifetime(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->lifetime;
}

CPINameRouteProtocolType
cpiRouteEntry_GetRouteProtocolType(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->routingProtocol;
}

CPINameRouteType
cpiRouteEntry_GetRouteType(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->routeType;
}

unsigned
cpiRouteEntry_GetCost(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->cost;
}

const char *
cpiRouteEntry_GetSymbolicName(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");
    return route->symbolic;
}

PARCJSON *
cpiRouteEntry_ToJson(const CPIRouteEntry *route)
{
    assertNotNull(route, "Parameter must be non-null");

    PARCJSON *routeJson = parcJSON_Create();
    char *uri = ccnxName_ToString(route->prefix);
    parcJSON_AddString(routeJson, cpiPrefix, uri);
    parcMemory_Deallocate((void **) &uri);

    if (route->symbolic) {
        parcJSON_AddString(routeJson, cpiSymbolic, route->symbolic);
    }

    if (route->hasInterfaceIndex) {
        parcJSON_AddInteger(routeJson, cpiInterface, route->interfaceIndex);
    }

    parcJSON_AddInteger(routeJson, cpiFlags, 0);

    if (route->nexthop) {
        // some registrations can have NULL nexthop, its ok
        PARCJSON *json = cpiAddress_ToJson(route->nexthop);
        parcJSON_AddObject(routeJson, cpiNexthop, json);
        parcJSON_Release(&json);
    }

    parcJSON_AddString(routeJson, cpiProtocol, cpiNameRouteProtocolType_ToString(route->routingProtocol));
    parcJSON_AddString(routeJson, cpiRouteType, cpiNameRouteType_ToString(route->routeType));
    parcJSON_AddInteger(routeJson, cpiCost, route->cost);

    if (route->hasLifetime) {
        PARCJSONArray *lifetimeJson = parcJSONArray_Create();
        PARCJSONValue *value = parcJSONValue_CreateFromInteger(route->lifetime.tv_sec);
        parcJSONArray_AddValue(lifetimeJson, value);
        parcJSONValue_Release(&value);
        value = parcJSONValue_CreateFromInteger(route->lifetime.tv_usec);
        parcJSONArray_AddValue(lifetimeJson, value);
        parcJSONValue_Release(&value);
        parcJSON_AddArray(routeJson, cpiLifetime, lifetimeJson);
        parcJSONArray_Release(&lifetimeJson);
    }

    return routeJson;
}

CPIRouteEntry *
cpiRouteEntry_FromJson(PARCJSON *json)
{
    assertNotNull(json, "Parameter json must be non-null");
    PARCJSON *routeJson = json;

    PARCJSONValue *value = parcJSON_GetValueByName(routeJson, cpiPrefix);
    assertNotNull(value, "Couldn't locate tag %s in: %s", cpiPrefix, parcJSON_ToString(json));
    PARCBuffer *sBuf = parcJSONValue_GetString(value);
    CCNxName *prefix = ccnxName_CreateFromCString(parcBuffer_Overlay(sBuf, 0));

    const char *symbolicName = NULL;
    value = parcJSON_GetValueByName(routeJson, cpiSymbolic);
    if (value) {
        sBuf = parcJSONValue_GetString(value);
        symbolicName = parcBuffer_Overlay(sBuf, 0);
    }

    bool hasInterfaceIndex = false;
    unsigned interfaceIndex = UINT32_MAX;
    value = parcJSON_GetValueByName(routeJson, cpiInterface);
    if (value) {
        hasInterfaceIndex = true;
        interfaceIndex = (unsigned) parcJSONValue_GetInteger(value);
    }

    CPIAddress *nexthop = NULL;
    value = parcJSON_GetValueByName(routeJson, cpiNexthop);
    if (value != NULL) {
        assertTrue(parcJSONValue_IsJSON(value),
                   "Json key %s wrong type in json %s",
                   cpiNexthop,
                   parcJSON_ToString(json));
        PARCJSON *nexthopJson = parcJSONValue_GetJSON(value);
        nexthop = cpiAddress_CreateFromJson(nexthopJson);
    }

    value = parcJSON_GetValueByName(routeJson, cpiProtocol);
    sBuf = parcJSONValue_GetString(value);
    char *valueString = parcBuffer_Overlay(sBuf, 0);
    CPINameRouteProtocolType routingProtocol = cpiNameRouteProtocolType_FromString(valueString);

    value = parcJSON_GetValueByName(routeJson, cpiRouteType);
    sBuf = parcJSONValue_GetString(value);
    valueString = parcBuffer_Overlay(sBuf, 0);
    CPINameRouteType routingType = cpiNameRouteType_FromString(valueString);

    value = parcJSON_GetValueByName(routeJson, cpiCost);
    unsigned cost = (unsigned) parcJSONValue_GetInteger(value);

    value = parcJSON_GetValueByName(routeJson, cpiLifetime);
    struct timeval *lifetime = NULL;

    struct timeval actual_time = { 0, 0 };
    if (value != NULL) {
        assertTrue(parcJSONValue_IsArray(value),
                   "Json key %s wrong typein json %s",
                   cpiNexthop,
                   parcJSON_ToString(json));
        PARCJSONArray *lifetimeJson = parcJSONValue_GetArray(value);

        actual_time.tv_sec = parcJSONValue_GetInteger(parcJSONArray_GetValue(lifetimeJson, 0));
        actual_time.tv_usec = (int) parcJSONValue_GetInteger(parcJSONArray_GetValue(lifetimeJson, 1));

        lifetime = &actual_time;
    }


    // == we're now ready to create the object
    CPIRouteEntry *route;
    if (symbolicName) {
        route = cpiRouteEntry_CreateSymbolic(prefix, symbolicName, routingProtocol, routingType, lifetime, cost);
        if (hasInterfaceIndex) {
            cpiRouteEntry_SetInterfaceIndex(route, interfaceIndex);
        }
    } else {
        route = cpiRouteEntry_Create(prefix, interfaceIndex, nexthop, routingProtocol, routingType, lifetime, cost);
    }

    if (nexthop) {
        cpiAddress_Destroy(&nexthop);
    }

    return route;
}