summaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/src/interfaces/hicn_light/hicn_light.c
blob: 85694573dcd6288925e4200b06fbed919dbafb87 (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
/*
 * Copyright (c) 2017-2019 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.
 */

/**
 * \file interfaces/hicn_light/hicn_light.c
 * \brief hICN light interface
 */
#include <stdbool.h>
#include <stdlib.h> // arc4random [random, rand]
#include <stdio.h> // snprintf
#include <time.h> // time

#include <hicn/ctrl.h>

#include "../../facemgr.h"
#include "../../interface.h"
#include "../../util/ip_address.h"
#include "../../util/log.h"
#include "../../util/map.h"
#include "../../event.h"

#define DEFAULT_ROUTE_COST 0

typedef struct {
    hc_sock_t * s;
    bool busy;
} hl_data_t;

int hl_initialize(interface_t * interface, face_rules_t * rules, void ** pdata)
{
    hl_data_t * data = malloc(sizeof(hl_data_t));
    if (!data) {
        ERROR("[hicn_light] Out of memory!");
        goto ERR_MALLOC;
    }

    data->s = hc_sock_create();
    if (data->s <= 0) {
        ERROR("[hicn_light] Could not create control socket");
        goto ERR_SOCK;
    }

    if (hc_sock_connect(data->s) < 0) {
        ERROR("[hicn_light] Could not connect control socket");
        goto ERR_CONNECT;
    }

    data->busy = false;

    *pdata = data;

    return FACEMGR_SUCCESS;

ERR_CONNECT:
    hc_sock_free(data->s);
ERR_SOCK:
    free(data);
ERR_MALLOC:
    return FACEMGR_FAILURE;
}

int hl_finalize(interface_t * interface)
{
    //hc_data_t * data = interface->data;
    //hc_sock_close(data->s);
    return FACEMGR_SUCCESS;
}

int hl_on_event(interface_t * interface, const event_t * event)
{
    hc_face_t face;
    hc_route_t route;
    int rc;
    hl_data_t * data = (hl_data_t *)interface->data;

    /* XXX We need  a queue or a socket pool to process concurrent events */
    if (data->busy) {
        ERROR("[hicn_light] Busy !");
        return FACEMGR_FAILURE;
    }

    switch(event->type) {
        case EVENT_TYPE_CREATE:

            /* Create face */
            face.face = *event->face;
            rc = hc_face_create(data->s, &face);
            if (rc < 0) {
                ERROR("Failed to create face\n");
                goto ERR;
            }
            DEBUG("Created face id=%d\n", face.id);

#if 0
            /* Add default route v4 */
            route = (hc_route_t) {
                .face_id = face.id,
                .family = AF_INET,
                .remote_addr = IPV4_ANY,
                .len = 0,
                .cost = DEFAULT_ROUTE_COST,

            };
            if (hc_route_create(data->s, &route) < 0) {
                ERROR("Failed to create default hICN/IPv4 route");
                goto ERR;
            }
            INFO("Successfully created default hICN/IPv4 route.");
#endif

#if 0
            route = (hc_route_t) {
                .face_id = face.id,
                .family = AF_INET6,
                .remote_addr = IPV6_ANY,
                .len = 0,
                .cost = DEFAULT_ROUTE_COST,
            };
            if (hc_route_create(data->s, &route) < 0) {
                ERROR("Failed to create default hICN/IPv6 route");
                goto ERR;
            }
#endif

#if 1
            /* We add routes based on face tags */

            if (policy_tags_has(event->face->tags, POLICY_TAG_TRUSTED)) {
                route = (hc_route_t) {
                    .face_id = face.id,
                    .family = AF_INET6,
                    .len = 16,
                    .cost = DEFAULT_ROUTE_COST,
                };
                if (ip_address_pton("b001::", &route.remote_addr) < 0) {
                    ERROR("Failed to convert prefix");
                    goto ERR;
                }
                if (hc_route_create(data->s, &route) < 0) {
                    ERROR("Failed to create hICN/IPv6 route");
                    goto ERR;
                }

                route = (hc_route_t) {
                    .face_id = face.id,
                    .family = AF_INET6,
                    .len = 16,
                    .cost = DEFAULT_ROUTE_COST,
                };
                if (ip_address_pton("d001::", &route.remote_addr) < 0) {
                    ERROR("Failed to convert prefix");
                    goto ERR;
                }
                if (hc_route_create(data->s, &route) < 0) {
                    ERROR("Failed to create hICN/IPv6 route");
                    goto ERR;
                }

            } else {

                route = (hc_route_t) {
                    .face_id = face.id,
                    .family = AF_INET6,
                    .len = 16,
                    .cost = DEFAULT_ROUTE_COST,
                };
                if (ip_address_pton("c001::", &route.remote_addr) < 0) {
                    ERROR("Failed to convert prefix");
                    goto ERR;
                }
                if (hc_route_create(data->s, &route) < 0) {
                    ERROR("Failed to create hICN/IPv6 route");
                    goto ERR;
                }
            }
#endif

            break;

        case EVENT_TYPE_DELETE:
            /* Removing a face should also remove associated routes */
            /* Create face */
            face.face = *event->face;
            rc = hc_face_delete(data->s, &face);
            if (rc < 0) {
                ERROR("Failed to delete face\n");
                goto ERR;
            }
            INFO("Deleted face id=%d\n", face.id);
            break;

        default:
            ERROR("Unknown event %s\n", event_type_str[event->type]);
            /* Unsupported events */
            goto ERR;
    }

    return FACEMGR_SUCCESS;

ERR:
    return FACEMGR_FAILURE;
}

const interface_ops_t hicn_light_ops = {
    .type = "hicn_light",
    .is_singleton = false,
    .initialize = hl_initialize,
    .finalize = hl_finalize,
    .on_event = hl_on_event,
};