Created
April 10, 2019 15:14
-
-
Save rwestphal/822bd19e79d49003f9d5381c7815f1b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <err.h> | |
#include <assert.h> | |
#include <arpa/inet.h> | |
#include <stdbool.h> | |
#include <libyang/libyang.h> | |
static struct ly_ctx *ly_ctx; | |
static struct lyd_node *running, *candidate; | |
static void validate_config(struct lyd_node **config) | |
{ | |
if (lyd_validate(config, LYD_OPT_CONFIG, ly_ctx) != 0) | |
errx(1, "lyd_validate"); | |
} | |
static void edit_candidate(const char *xpath, const char *value) | |
{ | |
struct lyd_node *node; | |
ly_errno = 0; | |
node = lyd_new_path(candidate, ly_ctx, xpath, (void *)value, 0, | |
LYD_PATH_OPT_UPDATE); | |
if (node == NULL && ly_errno) | |
errx(1, "lyd_new_path"); | |
} | |
static void commit_candidate(void) | |
{ | |
lyd_free_withsiblings(running); | |
running = candidate; | |
candidate = lyd_dup_withsiblings(running, 1); | |
if (candidate == NULL) | |
errx(1, "lyd_dup_withsiblings"); | |
} | |
int main(int argc, char **argv) | |
{ | |
/* initialization */ | |
ly_ctx = ly_ctx_new(NULL, 0); | |
ly_ctx_set_searchdir(ly_ctx, "/usr/local/share/yang/"); | |
ly_ctx_load_module(ly_ctx, "frr-ripd", NULL); | |
validate_config(&running); | |
validate_config(&candidate); | |
// Edit config (1) | |
edit_candidate("/frr-ripd:ripd/instance", NULL); | |
validate_config(&candidate); | |
commit_candidate(); | |
// Edit config (2) | |
edit_candidate("/frr-ripd:ripd/instance/allow-ecmp", "true"); | |
validate_config(&candidate); // <-- Crashes here! | |
commit_candidate(); | |
/* cleanup */ | |
lyd_free_withsiblings(candidate); | |
lyd_free_withsiblings(running); | |
ly_ctx_destroy(ly_ctx, NULL); | |
printf("Exiting...\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment