Skip to content

Instantly share code, notes, and snippets.

@rwestphal
Created April 26, 2018 14:07
Show Gist options
  • Save rwestphal/09a638213705c0398e7561eb44d07fe5 to your computer and use it in GitHub Desktop.
Save rwestphal/09a638213705c0398e7561eb44d07fe5 to your computer and use it in GitHub Desktop.
#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 void validate(struct lyd_node **config)
{
if (lyd_validate(config, LYD_OPT_CONFIG, ly_ctx) != 0)
errx(1, "lyd_validate");
}
static void print_addr(struct lyd_node *config, const char *xpath)
{
struct ly_set *set;
struct lyd_node_leaf_list *node;
struct in_addr addr;
set = lyd_find_path(config, xpath);
assert(set);
node = (struct lyd_node_leaf_list *)set->set.d[0];
printf("address (from value_str): %s\n", node->value_str);
memcpy(&addr, node->value.ptr, sizeof(addr));
printf("address (from value.ptr): %s\n", inet_ntoa(addr));
}
int main(int argc, char **argv)
{
const struct lys_module *module;
struct lyd_node *config1 = NULL, *config2;
struct lyd_node *node;
const char *xpath_neighbor = "/frr-ripd:ripd/instance/explicit-neighbor[.='1.1.1.1']";
putenv("LIBYANG_USER_TYPES_PLUGINS_DIR=/usr/local/lib/frr/yang");
/* initialization */
ly_ctx = ly_ctx_new(NULL, 0);
if (!ly_ctx)
errx(1, "ly_ctx_new");
ly_ctx_set_searchdir(ly_ctx, "/usr/local/share/yang/");
module = lys_parse_path(ly_ctx, "/usr/local/share/yang/frr-ripd.yang",
LYS_IN_YANG);
if (!module)
errx(1, "lys_parse_path");
validate(&config1);
printf("===========================================\n");
printf("config1:\n");
printf("===========================================\n");
ly_errno = 0;
node = lyd_new_path(config1, ly_ctx, xpath_neighbor, NULL, 0,
LYD_PATH_OPT_UPDATE);
if (node == NULL && ly_errno)
errx(1, "lyd_new_path");
print_addr(config1, xpath_neighbor);
printf("===========================================\n");
printf("config2 (copy from config1):\n");
printf("===========================================\n");
config2 = lyd_dup_withsiblings(config1, 1);
if (config2 == NULL)
errx(1, "lyd_dup_withsiblings");
lyd_free_withsiblings(config1);
print_addr(config2, xpath_neighbor);
/* cleanup */
lyd_free_withsiblings(config2);
ly_ctx_remove_module(module, NULL);
ly_ctx_destroy(ly_ctx, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment