2 * Copyright (C) 2018 NetDEF, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "lib_errors.h"
24 #include "northbound.h"
26 static const char *yang_get_default_value(const char *xpath
)
28 const struct lys_node
*snode
;
31 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
33 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
34 "%s: unknown data path: %s", __func__
, xpath
);
35 zlog_backtrace(LOG_ERR
);
39 value
= yang_snode_get_default(snode
);
45 #define YANG_DNODE_GET_ASSERT(dnode, xpath) \
47 if ((dnode) == NULL) { \
48 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND, \
49 "%s: couldn't find %s", __func__, (xpath)); \
50 zlog_backtrace(LOG_ERR); \
56 * Primitive type: bool.
58 bool yang_str2bool(const char *value
)
60 return strmatch(value
, "true");
63 struct yang_data
*yang_data_new_bool(const char *xpath
, bool value
)
65 return yang_data_new(xpath
, (value
== true) ? "true" : "false");
68 bool yang_dnode_get_bool(const struct lyd_node
*dnode
, const char *xpath_fmt
,
71 const struct lyd_node_leaf_list
*dleaf
;
76 char xpath
[XPATH_MAXLEN
];
78 va_start(ap
, xpath_fmt
);
79 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
81 dnode
= yang_dnode_get(dnode
, xpath
);
82 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
85 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
86 assert(dleaf
->value_type
== LY_TYPE_BOOL
);
87 return dleaf
->value
.bln
;
90 bool yang_get_default_bool(const char *xpath_fmt
, ...)
92 char xpath
[XPATH_MAXLEN
];
96 va_start(ap
, xpath_fmt
);
97 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
100 value
= yang_get_default_value(xpath
);
101 return yang_str2bool(value
);
105 * Primitive type: dec64.
107 double yang_str2dec64(const char *xpath
, const char *value
)
111 if (sscanf(value
, "%lf", &dbl
) != 1) {
112 flog_err(EC_LIB_YANG_DATA_CONVERT
,
113 "%s: couldn't convert string to decimal64 [xpath %s]",
115 zlog_backtrace(LOG_ERR
);
122 struct yang_data
*yang_data_new_dec64(const char *xpath
, double value
)
124 char value_str
[BUFSIZ
];
126 snprintf(value_str
, sizeof(value_str
), "%lf", value
);
127 return yang_data_new(xpath
, value_str
);
130 double yang_dnode_get_dec64(const struct lyd_node
*dnode
, const char *xpath_fmt
,
133 const struct lyd_node_leaf_list
*dleaf
;
138 char xpath
[XPATH_MAXLEN
];
140 va_start(ap
, xpath_fmt
);
141 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
143 dnode
= yang_dnode_get(dnode
, xpath
);
144 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
147 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
148 assert(dleaf
->value_type
== LY_TYPE_DEC64
);
150 return lyd_dec64_to_double(dnode
);
153 double yang_get_default_dec64(const char *xpath_fmt
, ...)
155 char xpath
[XPATH_MAXLEN
];
159 va_start(ap
, xpath_fmt
);
160 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
163 value
= yang_get_default_value(xpath
);
164 return yang_str2dec64(xpath
, value
);
168 * Primitive type: enum.
170 int yang_str2enum(const char *xpath
, const char *value
)
172 const struct lys_node
*snode
;
173 const struct lys_node_leaf
*sleaf
;
174 const struct lys_type_info_enums
*enums
;
176 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
178 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
179 "%s: unknown data path: %s", __func__
, xpath
);
180 zlog_backtrace(LOG_ERR
);
184 sleaf
= (const struct lys_node_leaf
*)snode
;
185 enums
= &sleaf
->type
.info
.enums
;
186 for (unsigned int i
= 0; i
< enums
->count
; i
++) {
187 const struct lys_type_enum
*enm
= &enums
->enm
[i
];
189 if (strmatch(value
, enm
->name
))
193 flog_err(EC_LIB_YANG_DATA_CONVERT
,
194 "%s: couldn't convert string to enum [xpath %s]", __func__
,
196 zlog_backtrace(LOG_ERR
);
200 struct yang_data
*yang_data_new_enum(const char *xpath
, int value
)
202 const struct lys_node
*snode
;
203 const struct lys_node_leaf
*sleaf
;
204 const struct lys_type
*type
;
205 const struct lys_type_info_enums
*enums
;
207 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
209 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
210 "%s: unknown data path: %s", __func__
, xpath
);
211 zlog_backtrace(LOG_ERR
);
215 sleaf
= (const struct lys_node_leaf
*)snode
;
217 enums
= &type
->info
.enums
;
218 while (enums
->count
== 0 && type
->der
) {
219 type
= &type
->der
->type
;
220 enums
= &type
->info
.enums
;
222 for (unsigned int i
= 0; i
< enums
->count
; i
++) {
223 const struct lys_type_enum
*enm
= &enums
->enm
[i
];
225 if (value
== enm
->value
)
226 return yang_data_new(xpath
, enm
->name
);
229 flog_err(EC_LIB_YANG_DATA_CONVERT
,
230 "%s: couldn't convert enum to string [xpath %s]", __func__
,
232 zlog_backtrace(LOG_ERR
);
236 int yang_dnode_get_enum(const struct lyd_node
*dnode
, const char *xpath_fmt
,
239 const struct lyd_node_leaf_list
*dleaf
;
244 char xpath
[XPATH_MAXLEN
];
246 va_start(ap
, xpath_fmt
);
247 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
249 dnode
= yang_dnode_get(dnode
, xpath
);
250 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
253 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
254 assert(dleaf
->value_type
== LY_TYPE_ENUM
);
255 return dleaf
->value
.enm
->value
;
258 int yang_get_default_enum(const char *xpath_fmt
, ...)
260 char xpath
[XPATH_MAXLEN
];
264 va_start(ap
, xpath_fmt
);
265 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
268 value
= yang_get_default_value(xpath
);
269 return yang_str2enum(xpath
, value
);
273 * Primitive type: int8.
275 int8_t yang_str2int8(const char *value
)
277 return strtol(value
, NULL
, 10);
280 struct yang_data
*yang_data_new_int8(const char *xpath
, int8_t value
)
282 char value_str
[BUFSIZ
];
284 snprintf(value_str
, sizeof(value_str
), "%d", value
);
285 return yang_data_new(xpath
, value_str
);
288 int8_t yang_dnode_get_int8(const struct lyd_node
*dnode
, const char *xpath_fmt
,
291 const struct lyd_node_leaf_list
*dleaf
;
296 char xpath
[XPATH_MAXLEN
];
298 va_start(ap
, xpath_fmt
);
299 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
301 dnode
= yang_dnode_get(dnode
, xpath
);
302 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
305 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
306 assert(dleaf
->value_type
== LY_TYPE_INT8
);
307 return dleaf
->value
.int8
;
310 int8_t yang_get_default_int8(const char *xpath_fmt
, ...)
312 char xpath
[XPATH_MAXLEN
];
316 va_start(ap
, xpath_fmt
);
317 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
320 value
= yang_get_default_value(xpath
);
321 return yang_str2int8(value
);
325 * Primitive type: int16.
327 int16_t yang_str2int16(const char *value
)
329 return strtol(value
, NULL
, 10);
332 struct yang_data
*yang_data_new_int16(const char *xpath
, int16_t value
)
334 char value_str
[BUFSIZ
];
336 snprintf(value_str
, sizeof(value_str
), "%d", value
);
337 return yang_data_new(xpath
, value_str
);
340 int16_t yang_dnode_get_int16(const struct lyd_node
*dnode
,
341 const char *xpath_fmt
, ...)
343 const struct lyd_node_leaf_list
*dleaf
;
348 char xpath
[XPATH_MAXLEN
];
350 va_start(ap
, xpath_fmt
);
351 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
353 dnode
= yang_dnode_get(dnode
, xpath
);
354 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
357 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
358 assert(dleaf
->value_type
== LY_TYPE_INT16
);
359 return dleaf
->value
.int16
;
362 int16_t yang_get_default_int16(const char *xpath_fmt
, ...)
364 char xpath
[XPATH_MAXLEN
];
368 va_start(ap
, xpath_fmt
);
369 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
372 value
= yang_get_default_value(xpath
);
373 return yang_str2int16(value
);
377 * Primitive type: int32.
379 int32_t yang_str2int32(const char *value
)
381 return strtol(value
, NULL
, 10);
384 struct yang_data
*yang_data_new_int32(const char *xpath
, int32_t value
)
386 char value_str
[BUFSIZ
];
388 snprintf(value_str
, sizeof(value_str
), "%d", value
);
389 return yang_data_new(xpath
, value_str
);
392 int32_t yang_dnode_get_int32(const struct lyd_node
*dnode
,
393 const char *xpath_fmt
, ...)
395 const struct lyd_node_leaf_list
*dleaf
;
400 char xpath
[XPATH_MAXLEN
];
402 va_start(ap
, xpath_fmt
);
403 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
405 dnode
= yang_dnode_get(dnode
, xpath
);
406 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
409 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
410 assert(dleaf
->value_type
== LY_TYPE_INT32
);
411 return dleaf
->value
.int32
;
414 int32_t yang_get_default_int32(const char *xpath_fmt
, ...)
416 char xpath
[XPATH_MAXLEN
];
420 va_start(ap
, xpath_fmt
);
421 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
424 value
= yang_get_default_value(xpath
);
425 return yang_str2int32(value
);
429 * Primitive type: int64.
431 int64_t yang_str2int64(const char *value
)
433 return strtoll(value
, NULL
, 10);
436 struct yang_data
*yang_data_new_int64(const char *xpath
, int64_t value
)
438 char value_str
[BUFSIZ
];
440 snprintf(value_str
, sizeof(value_str
), "%" PRId64
, value
);
441 return yang_data_new(xpath
, value_str
);
444 int64_t yang_dnode_get_int64(const struct lyd_node
*dnode
,
445 const char *xpath_fmt
, ...)
447 const struct lyd_node_leaf_list
*dleaf
;
452 char xpath
[XPATH_MAXLEN
];
454 va_start(ap
, xpath_fmt
);
455 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
457 dnode
= yang_dnode_get(dnode
, xpath
);
458 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
461 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
462 assert(dleaf
->value_type
== LY_TYPE_INT64
);
463 return dleaf
->value
.int64
;
466 int64_t yang_get_default_int64(const char *xpath_fmt
, ...)
468 char xpath
[XPATH_MAXLEN
];
472 va_start(ap
, xpath_fmt
);
473 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
476 value
= yang_get_default_value(xpath
);
477 return yang_str2int64(value
);
481 * Primitive type: uint8.
483 uint8_t yang_str2uint8(const char *value
)
485 return strtoul(value
, NULL
, 10);
488 struct yang_data
*yang_data_new_uint8(const char *xpath
, uint8_t value
)
490 char value_str
[BUFSIZ
];
492 snprintf(value_str
, sizeof(value_str
), "%u", value
);
493 return yang_data_new(xpath
, value_str
);
496 uint8_t yang_dnode_get_uint8(const struct lyd_node
*dnode
,
497 const char *xpath_fmt
, ...)
499 const struct lyd_node_leaf_list
*dleaf
;
504 char xpath
[XPATH_MAXLEN
];
506 va_start(ap
, xpath_fmt
);
507 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
509 dnode
= yang_dnode_get(dnode
, xpath
);
510 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
513 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
514 assert(dleaf
->value_type
== LY_TYPE_UINT8
);
515 return dleaf
->value
.uint8
;
518 uint8_t yang_get_default_uint8(const char *xpath_fmt
, ...)
520 char xpath
[XPATH_MAXLEN
];
524 va_start(ap
, xpath_fmt
);
525 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
528 value
= yang_get_default_value(xpath
);
529 return yang_str2uint8(value
);
533 * Primitive type: uint16.
535 uint16_t yang_str2uint16(const char *value
)
537 return strtoul(value
, NULL
, 10);
540 struct yang_data
*yang_data_new_uint16(const char *xpath
, uint16_t value
)
542 char value_str
[BUFSIZ
];
544 snprintf(value_str
, sizeof(value_str
), "%u", value
);
545 return yang_data_new(xpath
, value_str
);
548 uint16_t yang_dnode_get_uint16(const struct lyd_node
*dnode
,
549 const char *xpath_fmt
, ...)
551 const struct lyd_node_leaf_list
*dleaf
;
556 char xpath
[XPATH_MAXLEN
];
558 va_start(ap
, xpath_fmt
);
559 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
561 dnode
= yang_dnode_get(dnode
, xpath
);
562 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
565 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
566 assert(dleaf
->value_type
== LY_TYPE_UINT16
);
567 return dleaf
->value
.uint16
;
570 uint16_t yang_get_default_uint16(const char *xpath_fmt
, ...)
572 char xpath
[XPATH_MAXLEN
];
576 va_start(ap
, xpath_fmt
);
577 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
580 value
= yang_get_default_value(xpath
);
581 return yang_str2uint16(value
);
585 * Primitive type: uint32.
587 uint32_t yang_str2uint32(const char *value
)
589 return strtoul(value
, NULL
, 10);
592 struct yang_data
*yang_data_new_uint32(const char *xpath
, uint32_t value
)
594 char value_str
[BUFSIZ
];
596 snprintf(value_str
, sizeof(value_str
), "%u", value
);
597 return yang_data_new(xpath
, value_str
);
600 uint32_t yang_dnode_get_uint32(const struct lyd_node
*dnode
,
601 const char *xpath_fmt
, ...)
603 const struct lyd_node_leaf_list
*dleaf
;
608 char xpath
[XPATH_MAXLEN
];
610 va_start(ap
, xpath_fmt
);
611 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
613 dnode
= yang_dnode_get(dnode
, xpath
);
614 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
617 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
618 assert(dleaf
->value_type
== LY_TYPE_UINT32
);
619 return dleaf
->value
.uint32
;
622 uint32_t yang_get_default_uint32(const char *xpath_fmt
, ...)
624 char xpath
[XPATH_MAXLEN
];
628 va_start(ap
, xpath_fmt
);
629 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
632 value
= yang_get_default_value(xpath
);
633 return yang_str2uint32(value
);
637 * Primitive type: uint64.
639 uint64_t yang_str2uint64(const char *value
)
641 return strtoull(value
, NULL
, 10);
644 struct yang_data
*yang_data_new_uint64(const char *xpath
, uint64_t value
)
646 char value_str
[BUFSIZ
];
648 snprintf(value_str
, sizeof(value_str
), "%" PRIu64
, value
);
649 return yang_data_new(xpath
, value_str
);
652 uint64_t yang_dnode_get_uint64(const struct lyd_node
*dnode
,
653 const char *xpath_fmt
, ...)
655 const struct lyd_node_leaf_list
*dleaf
;
660 char xpath
[XPATH_MAXLEN
];
662 va_start(ap
, xpath_fmt
);
663 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
665 dnode
= yang_dnode_get(dnode
, xpath
);
666 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
669 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
670 assert(dleaf
->value_type
== LY_TYPE_UINT64
);
671 return dleaf
->value
.uint64
;
674 uint64_t yang_get_default_uint64(const char *xpath_fmt
, ...)
676 char xpath
[XPATH_MAXLEN
];
680 va_start(ap
, xpath_fmt
);
681 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
684 value
= yang_get_default_value(xpath
);
685 return yang_str2uint64(value
);
689 * Primitive type: string.
691 * All string wrappers can be used with non-string types.
693 struct yang_data
*yang_data_new_string(const char *xpath
, const char *value
)
695 return yang_data_new(xpath
, value
);
698 const char *yang_dnode_get_string(const struct lyd_node
*dnode
,
699 const char *xpath_fmt
, ...)
701 const struct lyd_node_leaf_list
*dleaf
;
706 char xpath
[XPATH_MAXLEN
];
708 va_start(ap
, xpath_fmt
);
709 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
711 dnode
= yang_dnode_get(dnode
, xpath
);
712 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
715 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
716 return dleaf
->value_str
;
719 void yang_dnode_get_string_buf(char *buf
, size_t size
,
720 const struct lyd_node
*dnode
,
721 const char *xpath_fmt
, ...)
723 const struct lyd_node_leaf_list
*dleaf
;
728 char xpath
[XPATH_MAXLEN
];
730 va_start(ap
, xpath_fmt
);
731 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
733 dnode
= yang_dnode_get(dnode
, xpath
);
734 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
737 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
738 if (strlcpy(buf
, dleaf
->value_str
, size
) >= size
) {
739 char xpath
[XPATH_MAXLEN
];
741 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
742 flog_warn(EC_LIB_YANG_DATA_TRUNCATED
,
743 "%s: value was truncated [xpath %s]", __func__
,
748 const char *yang_get_default_string(const char *xpath_fmt
, ...)
750 char xpath
[XPATH_MAXLEN
];
753 va_start(ap
, xpath_fmt
);
754 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
757 return yang_get_default_value(xpath
);
760 void yang_get_default_string_buf(char *buf
, size_t size
, const char *xpath_fmt
,
763 char xpath
[XPATH_MAXLEN
];
767 va_start(ap
, xpath_fmt
);
768 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
771 value
= yang_get_default_value(xpath
);
772 if (strlcpy(buf
, value
, size
) >= size
)
773 flog_warn(EC_LIB_YANG_DATA_TRUNCATED
,
774 "%s: value was truncated [xpath %s]", __func__
,
779 * Derived type: ipv4.
781 void yang_str2ipv4(const char *value
, struct in_addr
*addr
)
783 (void)inet_pton(AF_INET
, value
, addr
);
786 struct yang_data
*yang_data_new_ipv4(const char *xpath
,
787 const struct in_addr
*addr
)
789 char value_str
[INET_ADDRSTRLEN
];
791 (void)inet_ntop(AF_INET
, addr
, value_str
, sizeof(value_str
));
792 return yang_data_new(xpath
, value_str
);
795 void yang_dnode_get_ipv4(struct in_addr
*addr
, const struct lyd_node
*dnode
,
796 const char *xpath_fmt
, ...)
798 const struct lyd_node_leaf_list
*dleaf
;
803 char xpath
[XPATH_MAXLEN
];
805 va_start(ap
, xpath_fmt
);
806 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
808 dnode
= yang_dnode_get(dnode
, xpath
);
809 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
812 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
813 assert(dleaf
->value_type
== LY_TYPE_STRING
);
814 memcpy(addr
, dleaf
->value
.ptr
, sizeof(*addr
));
817 void yang_get_default_ipv4(struct in_addr
*var
, const char *xpath_fmt
, ...)
819 char xpath
[XPATH_MAXLEN
];
823 va_start(ap
, xpath_fmt
);
824 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
827 value
= yang_get_default_value(xpath
);
828 yang_str2ipv4(value
, var
);
832 * Derived type: ipv4p.
834 void yang_str2ipv4p(const char *value
, union prefixptr prefix
)
836 struct prefix_ipv4
*prefix4
= prefix
.p4
;
838 (void)str2prefix_ipv4(value
, prefix4
);
839 apply_mask_ipv4(prefix4
);
842 struct yang_data
*yang_data_new_ipv4p(const char *xpath
,
843 const union prefixptr prefix
)
845 char value_str
[PREFIX2STR_BUFFER
];
847 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
848 return yang_data_new(xpath
, value_str
);
851 void yang_dnode_get_ipv4p(union prefixptr prefix
, const struct lyd_node
*dnode
,
852 const char *xpath_fmt
, ...)
854 const struct lyd_node_leaf_list
*dleaf
;
855 struct prefix_ipv4
*prefix4
= prefix
.p4
;
860 char xpath
[XPATH_MAXLEN
];
862 va_start(ap
, xpath_fmt
);
863 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
865 dnode
= yang_dnode_get(dnode
, xpath
);
866 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
869 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
870 assert(dleaf
->value_type
== LY_TYPE_STRING
);
871 memcpy(prefix4
, dleaf
->value
.ptr
, sizeof(*prefix4
));
874 void yang_get_default_ipv4p(union prefixptr var
, const char *xpath_fmt
, ...)
876 char xpath
[XPATH_MAXLEN
];
880 va_start(ap
, xpath_fmt
);
881 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
884 value
= yang_get_default_value(xpath
);
885 yang_str2ipv4p(value
, var
);
889 * Derived type: ipv6.
891 void yang_str2ipv6(const char *value
, struct in6_addr
*addr
)
893 (void)inet_pton(AF_INET6
, value
, addr
);
896 struct yang_data
*yang_data_new_ipv6(const char *xpath
,
897 const struct in6_addr
*addr
)
899 char value_str
[INET6_ADDRSTRLEN
];
901 (void)inet_ntop(AF_INET6
, addr
, value_str
, sizeof(value_str
));
902 return yang_data_new(xpath
, value_str
);
905 void yang_dnode_get_ipv6(struct in6_addr
*addr
, const struct lyd_node
*dnode
,
906 const char *xpath_fmt
, ...)
908 const struct lyd_node_leaf_list
*dleaf
;
913 char xpath
[XPATH_MAXLEN
];
915 va_start(ap
, xpath_fmt
);
916 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
918 dnode
= yang_dnode_get(dnode
, xpath
);
919 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
922 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
923 assert(dleaf
->value_type
== LY_TYPE_STRING
);
924 memcpy(addr
, dleaf
->value
.ptr
, sizeof(*addr
));
927 void yang_get_default_ipv6(struct in6_addr
*var
, const char *xpath_fmt
, ...)
929 char xpath
[XPATH_MAXLEN
];
933 va_start(ap
, xpath_fmt
);
934 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
937 value
= yang_get_default_value(xpath
);
938 yang_str2ipv6(value
, var
);
942 * Derived type: ipv6p.
944 void yang_str2ipv6p(const char *value
, union prefixptr prefix
)
946 struct prefix_ipv6
*prefix6
= prefix
.p6
;
948 (void)str2prefix_ipv6(value
, prefix6
);
949 apply_mask_ipv6(prefix6
);
952 struct yang_data
*yang_data_new_ipv6p(const char *xpath
,
953 const union prefixptr prefix
)
955 char value_str
[PREFIX2STR_BUFFER
];
957 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
958 return yang_data_new(xpath
, value_str
);
961 void yang_dnode_get_ipv6p(union prefixptr prefix
, const struct lyd_node
*dnode
,
962 const char *xpath_fmt
, ...)
964 const struct lyd_node_leaf_list
*dleaf
;
965 struct prefix_ipv6
*prefix6
= prefix
.p6
;
970 char xpath
[XPATH_MAXLEN
];
972 va_start(ap
, xpath_fmt
);
973 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
975 dnode
= yang_dnode_get(dnode
, xpath
);
976 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
979 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
980 assert(dleaf
->value_type
== LY_TYPE_STRING
);
981 memcpy(prefix6
, dleaf
->value
.ptr
, sizeof(*prefix6
));
984 void yang_get_default_ipv6p(union prefixptr var
, const char *xpath_fmt
, ...)
986 char xpath
[XPATH_MAXLEN
];
990 va_start(ap
, xpath_fmt
);
991 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
994 value
= yang_get_default_value(xpath
);
995 yang_str2ipv6p(value
, var
);