aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_Acks.c
blob: 07bc934a0e1d3fbeb7f417f98e46ae0878bcf12c (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
/*
 * 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.
 */

/**
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
/*
 *  {
 *      "CPI_ACK" : {
 *          "SEQUENCE" : <sequence number>,
 *          "RETURN"   : "ACK" or "NACK",
 *          "REQUEST"  : <original request JSON>
 *          [, "MESSAGE" : <optional message> ]
 *        }
 *     ["AUTHENTICATOR" : <TBD proof based on request/response, e.g. a crypto signature>]
 *  }
 */

#include <config.h>
#include <stdio.h>
#include <strings.h>

#include <LongBow/runtime.h>

#include "controlPlaneInterface.h"
#include "cpi_private.h"
#include "cpi_Acks.h"

struct control_plane_ack {
    ControlPlaneInformation cpi_ack;
    CpiAckType ack_type;
    ControlPlaneInformation cpi_original;
};

static const char *cpiReturn = "RETURN";
static const char *cpiReturnAck = "ACK";
static const char *cpiReturnNack = "NACK";
static const char *cpiOriginal = "REQUEST";
static const char *cpiRequest = "CPI_REQUEST";

PARCJSON *
cpiAcks_CreateAck(const PARCJSON *originalRequest)
{
    uint64_t seqnum = cpi_GetNextSequenceNumber();
    PARCJSON *body = parcJSON_Create();

    parcJSON_AddInteger(body, cpiSeqnum, (int) seqnum);
    parcJSON_AddString(body, cpiReturn, cpiReturnAck);

    PARCJSON *copy = parcJSON_Copy(originalRequest);
    parcJSON_AddObject(body, cpiOriginal, copy);
    parcJSON_Release(&copy);

    PARCJSON *json = parcJSON_Create();

    parcJSON_AddObject(json, cpiAck, body);
    parcJSON_Release(&body);

    return json;
}

PARCJSON *
cpiAcks_CreateNack(const PARCJSON *request)
{
    uint64_t seqnum = cpi_GetNextSequenceNumber();
    PARCJSON *body = parcJSON_Create();
    parcJSON_AddInteger(body, cpiSeqnum, (int) seqnum);
    parcJSON_AddString(body, cpiReturn, cpiReturnNack);

    PARCJSON *copy = parcJSON_Copy(request);
    parcJSON_AddObject(body, cpiOriginal, copy);
    parcJSON_Release(&copy);

    PARCJSON *json = parcJSON_Create();
    parcJSON_AddObject(json, cpiAck, body);
    parcJSON_Release(&body);

    return json;
}

bool
cpiAcks_IsAck(const PARCJSON *json)
{
    PARCJSONValue *ack_value = parcJSON_GetValueByName(json, cpiAck);
    if (ack_value != NULL) {
        PARCJSON *ack_json = parcJSONValue_GetJSON(ack_value);
        PARCJSONValue *return_value = parcJSON_GetValueByName(ack_json, cpiReturn);
        PARCBuffer *sBuf = parcJSONValue_GetString(return_value);
        const char *returnStr = parcBuffer_Overlay(sBuf, 0);
        return strcasecmp(returnStr, cpiReturnAck) == 0;
    }
    return false;
}

uint64_t
cpiAcks_GetAckOriginalSequenceNumber(const PARCJSON *json)
{
    PARCJSONValue *value = parcJSON_GetValueByName(json, cpiAck);
    assertNotNull(value, "got null ack json: %s", parcJSON_ToString(json));

    PARCJSON *tempJson = parcJSONValue_GetJSON(value);

    value = parcJSON_GetValueByName(tempJson, cpiOriginal);
    assertNotNull(value, "got null original json from the ack: %s", parcJSON_ToString(tempJson));

    tempJson = parcJSONValue_GetJSON(value);

    value = parcJSON_GetValueByName(tempJson, cpiRequest);
    assertNotNull(value, "got null request json from the ack: %s", parcJSON_ToString(tempJson));

    tempJson = parcJSONValue_GetJSON(value);

    value = parcJSON_GetValueByName(tempJson, cpiSeqnum);
    assertNotNull(value, "got null seqnum inside the request: %s", parcJSON_ToString(tempJson));

    return parcJSONValue_GetInteger(value);
}