current.c 20.5 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
H
haozi007 已提交
3 4 5 6
 * clibcni licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
H
haozi007 已提交
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
11 12 13 14 15 16 17 18 19
 * Author: tanyifeng
 * Create: 2019-04-25
 * Description: provide result functions
 ********************************************************************************/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "current.h"
#include <stdio.h>
O
openeuler-iSula 已提交
20
#include <stdlib.h>
O
overweight 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

#include "utils.h"
#include "log.h"

static struct result *get_result(const result_curr *curr_result, char **err);

static result_curr *new_curr_result_helper(const char *json_data, char **err)
{
    result_curr *result = NULL;
    parser_error errmsg = NULL;

    if (json_data == NULL) {
        ERROR("Json data is NULL");
        return NULL;
    }
    result = result_curr_parse_data(json_data, NULL, &errmsg);
    if (result == NULL) {
        if (asprintf(err, "parse json failed: %s", errmsg) < 0) {
H
haozi007 已提交
39
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        }
        ERROR("Parse failed: %s", errmsg);
        goto free_out;
    }
    return result;

free_out:
    free(errmsg);
    return NULL;
}

static void do_append_result_errmsg(const struct result *ret, const char *save_err, char **err)
{
    char *tmp_err = NULL;
    int nret = 0;

    if (ret != NULL) {
        return;
    }

    tmp_err = *err;
    *err = NULL;
    nret = asprintf(err, "parse err: %s, convert err: %s", save_err ? save_err : "", tmp_err ? tmp_err : "");
    if (nret < 0) {
H
haozi007 已提交
64
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
        ERROR("Out of memory");
    }
    free(tmp_err);
}

struct result *new_curr_result(const char *json_data, char **err)
{
    struct result *ret = NULL;
    result_curr *tmp_result = NULL;
    char *save_err = NULL;

    if (err == NULL) {
        ERROR("Invalid argument");
        return NULL;
    }
    tmp_result = new_curr_result_helper(json_data, err);
    if (tmp_result == NULL) {
        return NULL;
    }
    if (*err != NULL) {
        save_err = *err;
        *err = NULL;
    }
    ret = get_result(tmp_result, err);
    do_append_result_errmsg(ret, save_err, err);

    free_result_curr(tmp_result);
    free(save_err);
    return ret;
}

static struct interface *convert_curr_interface(const network_interface *curr_interface)
    {
        struct interface *result = NULL;

        if (curr_interface == NULL) {
            ERROR("Invalid argument");
            return NULL;
        }

H
haozi007 已提交
105
        result = clibcni_util_common_calloc_s(sizeof(struct interface));
O
overweight 已提交
106 107 108 109 110
        if (result == NULL) {
            ERROR("Out of memory");
            return NULL;
        }

H
haozi007 已提交
111 112 113
        result->name = clibcni_util_strdup_s(curr_interface->name);
        result->mac = clibcni_util_strdup_s(curr_interface->mac);
        result->sandbox = clibcni_util_strdup_s(curr_interface->sandbox);
O
overweight 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126
        return result;
    }

static int do_parse_ipnet(const char *cidr_str, const char *ip_str, uint8_t **ip, size_t *ip_len,
                          struct ipnet **ipnet_val, char **err)
{
    int ret = 0;

    ret = parse_cidr(cidr_str, ipnet_val, err);
    if (ret != 0) {
        ERROR("Parse cidr failed: %s", *err != NULL ? *err : "");
        return -1;
    }
L
LiuHao 已提交
127 128 129
    if (ip_str == NULL) {
        return 0;
    }
O
overweight 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    ret = parse_ip_from_str(ip_str, ip, ip_len, err);
    if (ret != 0) {
        ERROR("Parse ip failed: %s", *err != NULL ? *err : "");
        free_ipnet_type(*ipnet_val);
        *ipnet_val = NULL;
        return -1;
    }
    return 0;
}

static struct ipconfig *convert_curr_ipconfig(const network_ipconfig *curr_ipconfig, char **err)
{
    struct ipconfig *result = NULL;
    struct ipnet *ipnet_val = NULL;
    int ret = 0;
    uint8_t *gateway = NULL;
    size_t gateway_len = 0;

    if (curr_ipconfig == NULL) {
        ERROR("Invalid argument");
        return NULL;
    }

H
haozi007 已提交
153
    result = clibcni_util_common_calloc_s(sizeof(struct ipconfig));
O
overweight 已提交
154 155
    if (result == NULL) {
        ERROR("Out of memory");
H
haozi007 已提交
156
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
157 158 159 160 161 162 163 164 165 166
        return NULL;
    }
    /* parse address to ipnet */
    ret = do_parse_ipnet(curr_ipconfig->address, curr_ipconfig->gateway, &gateway, &gateway_len, &ipnet_val, err);
    if (ret != 0) {
        goto err_out;
    }
    result->address = ipnet_val;
    result->gateway = gateway;
    result->gateway_len = gateway_len;
H
haozi007 已提交
167
    result->version = clibcni_util_strdup_s(curr_ipconfig->version);
O
overweight 已提交
168 169

    if (curr_ipconfig->interface != NULL) {
H
haozi007 已提交
170
        result->interface = clibcni_util_common_calloc_s(sizeof(int32_t));
O
overweight 已提交
171 172
        if (result->interface == NULL) {
            ERROR("Out of memory");
H
haozi007 已提交
173
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
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
            goto err_out;
        }
        *(result->interface) = *(curr_ipconfig->interface);
    }

    return result;

err_out:
    free_ipconfig_type(result);
    return NULL;
}

static struct route *convert_curr_route(const network_route *curr_route, char **err)
{
    struct route *result = NULL;
    struct ipnet *dst = NULL;
    int ret = 0;
    uint8_t *gw = NULL;
    size_t gw_len = 0;

    if (curr_route == NULL) {
        ERROR("Invalid argument");
        return NULL;
    }
    ret = do_parse_ipnet(curr_route->dst, curr_route->gw, &gw, &gw_len, &dst, err);
    if (ret != 0) {
        return NULL;
    }

H
haozi007 已提交
203
    result = clibcni_util_common_calloc_s(sizeof(struct route));
O
overweight 已提交
204 205 206 207
    if (result == NULL) {
        ERROR("Out of memory");
        free(gw);
        free_ipnet_type(dst);
H
haozi007 已提交
208
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        return NULL;
    }

    result->dst = dst;
    result->gw = gw;
    result->gw_len = gw_len;

    return result;
}

static struct dns *convert_curr_dns(network_dns *curr_dns, char **err)
{
    struct dns *result = NULL;

    if (curr_dns == NULL) {
H
haozi007 已提交
224
        *err = clibcni_util_strdup_s("Empty dns argument");
O
overweight 已提交
225 226 227 228
        ERROR("Empty dns argument");
        return NULL;
    }

H
haozi007 已提交
229
    result = clibcni_util_common_calloc_s(sizeof(struct dns));
O
overweight 已提交
230 231
    if (result == NULL) {
        ERROR("Out of memory");
H
haozi007 已提交
232
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
233 234 235 236 237 238 239 240 241 242 243
        return NULL;
    }

    result->name_servers = curr_dns->nameservers;
    result->name_servers_len = curr_dns->nameservers_len;
    result->domain = curr_dns->domain;
    result->options = curr_dns->options;
    result->options_len = curr_dns->options_len;
    result->search = curr_dns->search;
    result->search_len = curr_dns->search_len;

O
openeuler-iSula 已提交
244
    (void)memset(curr_dns, 0, sizeof(network_dns));
O
overweight 已提交
245 246 247 248 249 250 251 252

    return result;
}

static int copy_result_interface(const result_curr *curr_result, struct result *value, char **err)
{
    value->interfaces_len = curr_result->interfaces_len;
    if (value->interfaces_len > 0) {
H
haozi007 已提交
253
        value->interfaces = clibcni_util_smart_calloc_s(value->interfaces_len, sizeof(struct interface *));
O
overweight 已提交
254
        if (value->interfaces == NULL) {
H
haozi007 已提交
255
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
256 257 258 259 260 261 262 263
            value->interfaces_len = 0;
            ERROR("Out of memory");
            return -1;
        }
        size_t i;
        for (i = 0; i < curr_result->interfaces_len; i++) {
            value->interfaces[i] = convert_curr_interface(curr_result->interfaces[i]);
            if (value->interfaces[i] == NULL) {
H
haozi007 已提交
264
                *err = clibcni_util_strdup_s("Convert interfaces failed");
O
overweight 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
                value->interfaces_len = i;
                ERROR("Convert interfaces failed");
                return -1;
            }
        }
    }
    return 0;
}

static int copy_result_ips(const result_curr *curr_result, struct result *value, char **err)
{
    size_t i = 0;
    value->ips_len = curr_result->ips_len;

    if (value->ips_len == 0) {
        return 0;
    }

H
haozi007 已提交
283
    value->ips = clibcni_util_smart_calloc_s(value->ips_len, sizeof(struct ipconfig *));
O
overweight 已提交
284
    if (value->ips == NULL) {
H
haozi007 已提交
285
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
286 287 288 289
        ERROR("Out of memory");
        value->ips_len = 0;
        return -1;
    }
O
openeuler-iSula 已提交
290

O
overweight 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    for (i = 0; i < value->ips_len; i++) {
        value->ips[i] = convert_curr_ipconfig(curr_result->ips[i], err);
        if (value->ips[i] == NULL) {
            ERROR("Convert ips failed: %s", *err != NULL ? *err : "");
            value->ips_len = i;
            return -1;
        }
    }
    return 0;
}

static int copy_result_routes(const result_curr *curr_result, struct result *value, char **err)
{
    size_t i = 0;

    value->routes_len = curr_result->routes_len;
    if (value->routes_len == 0) {
        return 0;
    }

H
haozi007 已提交
311
    value->routes = clibcni_util_smart_calloc_s(value->routes_len, sizeof(struct route *));
O
overweight 已提交
312
    if (value->routes == NULL) {
H
haozi007 已提交
313
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
314 315 316 317
        ERROR("Out of memory");
        value->routes_len = 0;
        return -1;
    }
O
openeuler-iSula 已提交
318

O
overweight 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    for (i = 0; i < value->routes_len; i++) {
        value->routes[i] = convert_curr_route(curr_result->routes[i], err);
        if (value->routes[i] == NULL) {
            ERROR("Convert routes failed: %s", *err != NULL ? *err : "");
            value->routes_len = i;
            return -1;
        }
    }
    return 0;
}

static struct result *get_result(const result_curr *curr_result, char **err)
{
    struct result *value = NULL;
    bool invalid_arg = (curr_result == NULL || err == NULL);

    if (invalid_arg) {
        return NULL;
    }
H
haozi007 已提交
338
    value = clibcni_util_common_calloc_s(sizeof(struct result));
O
overweight 已提交
339
    if (value == NULL) {
H
haozi007 已提交
340
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
341 342 343 344 345
        ERROR("Out of memory");
        return NULL;
    }

    /* copy cni version */
H
haozi007 已提交
346
    value->cniversion = clibcni_util_strdup_s(curr_result->cni_version);
O
overweight 已提交
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

    /* copy interfaces */
    if (copy_result_interface(curr_result, value, err) != 0) {
        goto free_out;
    }

    /* copy ips */
    if (copy_result_ips(curr_result, value, err) != 0) {
        goto free_out;
    }

    /* copy routes */
    if (copy_result_routes(curr_result, value, err) != 0) {
        goto free_out;
    }

    /* copy dns */
    value->my_dns = convert_curr_dns(curr_result->dns, err);
    if (value->my_dns == NULL) {
        goto free_out;
    }

    return value;
free_out:
    free_result(value);
    return NULL;
}

static network_interface *interface_to_json_interface(const struct interface *src)
{
    network_interface *result = NULL;

    if (src == NULL) {
        ERROR("Invalid arguments");
        return NULL;
    }

H
haozi007 已提交
384
    result = clibcni_util_common_calloc_s(sizeof(network_interface));
O
overweight 已提交
385 386 387 388 389
    if (result == NULL) {
        ERROR("Out of memory");
        return NULL;
    }

H
haozi007 已提交
390 391 392
    result->name = clibcni_util_strdup_s(src->name);
    result->mac = clibcni_util_strdup_s(src->mac);
    result->sandbox = clibcni_util_strdup_s(src->sandbox);
O
overweight 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

    return result;
}

static int parse_ip_and_gateway(const struct ipconfig *src, network_ipconfig *result, char **err)
{
    if (src->address != NULL) {
        result->address = ipnet_to_string(src->address, err);
        if (result->address == NULL) {
            ERROR("Covert ipnet failed: %s", *err != NULL ? *err : "");
            return -1;
        }
    }

    if (src->gateway && src->gateway_len > 0) {
        result->gateway = ip_to_string(src->gateway, src->gateway_len);
        if (result->gateway == NULL) {
            if (asprintf(err, "ip: %s to string failed", src->gateway) < 0) {
H
haozi007 已提交
411
                *err = clibcni_util_strdup_s("ip to string failed");
O
overweight 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
            }
            ERROR("IP: %s to string failed", src->gateway);
            return -1;
        }
    }
    return 0;
}

static network_ipconfig *ipconfig_to_json_ipconfig(const struct ipconfig *src, char **err)
{
    network_ipconfig *result = NULL;
    int ret = -1;

    if (src == NULL) {
        ERROR("Invalid arguments");
        return result;
    }

H
haozi007 已提交
430
    result = clibcni_util_common_calloc_s(sizeof(network_ipconfig));
O
overweight 已提交
431
    if (result == NULL) {
H
haozi007 已提交
432
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
433 434 435 436 437 438 439 440 441 442
        ERROR("Out of memory");
        goto out;
    }

    /* parse address and ip */
    if (parse_ip_and_gateway(src, result, err) != 0) {
        goto out;
    }

    if (src->version != NULL) {
H
haozi007 已提交
443
        result->version = clibcni_util_strdup_s(src->version);
O
overweight 已提交
444 445 446
    }

    if (src->interface != NULL) {
H
haozi007 已提交
447
        result->interface = clibcni_util_common_calloc_s(sizeof(int32_t));
O
overweight 已提交
448
        if (result->interface == NULL) {
H
haozi007 已提交
449
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
            ERROR("Out of memory");
            goto out;
        }
        *(result->interface) = *(src->interface);
    }

    ret = 0;
out:
    if (ret != 0) {
        free_network_ipconfig(result);
        result = NULL;
    }
    return result;
}

static network_route *route_to_json_route(const struct route *src, char **err)
{
    network_route *result = NULL;
    int ret = -1;

    if (src == NULL) {
        ERROR("Invalid arguments");
        return NULL;
    }

H
haozi007 已提交
475
    result = (network_route *)clibcni_util_common_calloc_s(sizeof(network_route));
O
overweight 已提交
476
    if (result == NULL) {
H
haozi007 已提交
477
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491
        ERROR("Out of memory");
        goto out;
    }

    if (src->dst != NULL) {
        result->dst = ipnet_to_string(src->dst, err);
        if (result->dst == NULL) {
            goto out;
        }
    }

    if (src->gw != NULL && src->gw_len > 0) {
        result->gw = ip_to_string(src->gw, src->gw_len);
        if (result->gw == NULL) {
H
haozi007 已提交
492
            *err = clibcni_util_strdup_s("ip to string failed");
O
overweight 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
            ERROR("ip to string failed");
            goto out;
        }
    }

    ret = 0;
out:
    if (ret != 0) {
        free_network_route(result);
        result = NULL;
    }
    return result;
}

static int dns_to_json_copy_servers(const struct dns *src, network_dns *result, char **err)
{
    bool need_copy = (src->name_servers != NULL && src->name_servers_len > 0);

    if (need_copy) {
H
haozi007 已提交
512
        result->nameservers = (char **)clibcni_util_smart_calloc_s(src->name_servers_len, sizeof(char *));
O
overweight 已提交
513
        if (result->nameservers == NULL) {
H
haozi007 已提交
514
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
515 516 517 518
            ERROR("Out of memory");
            return -1;
        }
        result->nameservers_len = src->name_servers_len;
O
openeuler-iSula 已提交
519
        (void)memcpy(result->nameservers, src->name_servers, src->name_servers_len);
O
overweight 已提交
520 521 522 523 524 525 526 527 528
    }
    return 0;
}

static int dns_to_json_copy_options(const struct dns *src, network_dns *result, char **err)
{
    bool need_copy = (src->options != NULL && src->options_len > 0);

    if (need_copy) {
H
haozi007 已提交
529
        result->options = (char **)clibcni_util_smart_calloc_s(src->options_len, sizeof(char *));
O
overweight 已提交
530
        if (result->options == NULL) {
H
haozi007 已提交
531
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
532 533 534 535
            ERROR("Out of memory");
            return -1;
        }
        result->options_len = src->options_len;
O
openeuler-iSula 已提交
536
        (void)memcpy(result->options, src->options, src->options_len);
O
overweight 已提交
537 538 539 540 541 542 543 544 545
    }
    return 0;
}

static int dns_to_json_copy_searchs(const struct dns *src, network_dns *result, char **err)
{
    bool need_copy = (src->search != NULL && src->search_len > 0);

    if (need_copy) {
H
haozi007 已提交
546
        result->search = (char **)clibcni_util_smart_calloc_s(src->search_len, sizeof(char *));
O
overweight 已提交
547
        if (result->search == NULL) {
H
haozi007 已提交
548
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
549 550 551 552
            ERROR("Out of memory");
            return -1;
        }
        result->search_len = src->search_len;
O
openeuler-iSula 已提交
553
        (void)memcpy(result->search, src->search, src->search_len);
O
overweight 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    }
    return 0;
}

static int do_copy_dns_configs_to_json(const struct dns *src, network_dns *result, char **err)
{
    if (dns_to_json_copy_servers(src, result, err) != 0) {
        return -1;
    }

    if (dns_to_json_copy_options(src, result, err) != 0) {
        return -1;
    }

    if (dns_to_json_copy_searchs(src, result, err) != 0) {
        return -1;
    }
    return 0;
}

static network_dns *dns_to_json_dns(const struct dns *src, char **err)
{
    network_dns *result = NULL;
    int ret = -1;

    if (src == NULL) {
        return NULL;
    }

H
haozi007 已提交
583
    result = (network_dns *)clibcni_util_common_calloc_s(sizeof(network_dns));
O
overweight 已提交
584
    if (result == NULL) {
H
haozi007 已提交
585
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
586 587 588 589 590
        ERROR("Out of memory");
        goto out;
    }

    if (src->domain != NULL) {
H
haozi007 已提交
591
        result->domain = clibcni_util_strdup_s(src->domain);
O
overweight 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    }

    ret = do_copy_dns_configs_to_json(src, result, err);
out:
    if (ret != 0) {
        free_network_dns(result);
        result = NULL;
    }
    return result;
}

static bool copy_interfaces_from_result_to_json(const struct result *src, result_curr *res, char **err)
{
    size_t i = 0;
    bool empty_src = (src->interfaces == NULL || src->interfaces_len == 0);

    if (empty_src) {
        return true;
    }

    res->interfaces_len = 0;

H
haozi007 已提交
614
    res->interfaces = (network_interface **)clibcni_util_smart_calloc_s(src->interfaces_len, sizeof(network_interface *));
O
overweight 已提交
615
    if (res->interfaces == NULL) {
H
haozi007 已提交
616
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
617 618 619 620 621 622 623 624 625
        ERROR("Out of memory");
        return false;
    }
    for (i = 0; i < src->interfaces_len; i++) {
        if (src->interfaces[i] == NULL) {
            continue;
        }
        res->interfaces[i] = interface_to_json_interface(src->interfaces[i]);
        if (res->interfaces[i] == NULL) {
H
haozi007 已提交
626
            *err = clibcni_util_strdup_s("interface to json struct failed");
O
overweight 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640
            ERROR("interface to json struct failed");
            return false;
        }
        res->interfaces_len++;
    }
    return true;
}

static bool copy_ips_from_result_to_json(const struct result *src, result_curr *res, char **err)
{
    bool need_copy = (src->ips && src->ips_len > 0);

    res->ips_len = 0;
    if (need_copy) {
H
haozi007 已提交
641
        res->ips = (network_ipconfig **)clibcni_util_smart_calloc_s(src->ips_len, sizeof(network_ipconfig *));
O
overweight 已提交
642
        if (res->ips == NULL) {
H
haozi007 已提交
643
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
            ERROR("Out of memory");
            return false;
        }
        size_t i = 0;
        for (i = 0; i < src->ips_len; i++) {
            res->ips[i] = ipconfig_to_json_ipconfig(src->ips[i], err);
            if (res->ips[i] == NULL) {
                ERROR("parse ip failed: %s", *err != NULL ? *err : "");
                return false;
            }
            res->ips_len++;
        }
    }
    return true;
}

static bool copy_routes_from_result_to_json(const struct result *src, result_curr *res, char **err)
{
    bool need_copy = (src->routes && src->routes_len > 0);

    res->routes_len = 0;
    if (need_copy) {
H
haozi007 已提交
666
        res->routes = (network_route **)clibcni_util_smart_calloc_s(src->routes_len, sizeof(network_route *));
O
overweight 已提交
667
        if (res->routes == NULL) {
H
haozi007 已提交
668
            *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
            ERROR("Out of memory");
            return false;
        }
        size_t i = 0;
        for (i = 0; i < src->routes_len; i++) {
            res->routes[i] = route_to_json_route(src->routes[i], err);
            if (res->routes[i] == NULL) {
                ERROR("Parse route failed: %s", *err != NULL ? *err : "");
                return false;
            }
            res->routes_len++;
        }
    }
    return true;
}

static int do_result_copy_configs_to_json(const struct result *src, result_curr *res, char **err)
{
    /* copy interfaces */
    if (!copy_interfaces_from_result_to_json(src, res, err)) {
        return -1;
    }

    /* copy ips */
    if (!copy_ips_from_result_to_json(src, res, err)) {
        return -1;
    }

    /* copy routes */
    if (!copy_routes_from_result_to_json(src, res, err)) {
        return -1;
    }

    /* copy dns */
    if (src->my_dns != NULL) {
        res->dns = dns_to_json_dns(src->my_dns, err);
        if (res->dns == NULL) {
            return -1;
        }
    }

    return 0;
}

result_curr *result_curr_to_json_result(const struct result *src, char **err)
{
    result_curr *res = NULL;
    int ret = -1;
    bool invalid_arg = (src == NULL || err == NULL);

    if (invalid_arg) {
        ERROR("Invalid arguments");
        return res;
    }

H
haozi007 已提交
724
    res = (result_curr *)clibcni_util_common_calloc_s(sizeof(result_curr));
O
overweight 已提交
725 726
    if (res == NULL) {
        ERROR("Out of memory");
H
haozi007 已提交
727
        *err = clibcni_util_strdup_s("Out of memory");
O
overweight 已提交
728 729 730 731 732
        goto out;
    }

    /* copy cni version */
    if (src->cniversion != NULL) {
H
haozi007 已提交
733
        res->cni_version = clibcni_util_strdup_s(src->cniversion);
O
overweight 已提交
734 735 736 737 738 739 740 741 742 743 744
    }

    ret = do_result_copy_configs_to_json(src, res, err);
out:
    if (ret != 0) {
        free_result_curr(res);
        res = NULL;
    }
    return res;
}