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" : "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
*type
;
175 const struct lys_type_info_enums
*enums
;
177 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
179 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
180 "%s: unknown data path: %s", __func__
, xpath
);
181 zlog_backtrace(LOG_ERR
);
185 sleaf
= (const struct lys_node_leaf
*)snode
;
187 enums
= &type
->info
.enums
;
188 while (enums
->count
== 0 && type
->der
) {
189 type
= &type
->der
->type
;
190 enums
= &type
->info
.enums
;
192 for (unsigned int i
= 0; i
< enums
->count
; i
++) {
193 const struct lys_type_enum
*enm
= &enums
->enm
[i
];
195 if (strmatch(value
, enm
->name
))
199 flog_err(EC_LIB_YANG_DATA_CONVERT
,
200 "%s: couldn't convert string to enum [xpath %s]", __func__
,
202 zlog_backtrace(LOG_ERR
);
206 struct yang_data
*yang_data_new_enum(const char *xpath
, int value
)
208 const struct lys_node
*snode
;
209 const struct lys_node_leaf
*sleaf
;
210 const struct lys_type
*type
;
211 const struct lys_type_info_enums
*enums
;
213 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
215 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
216 "%s: unknown data path: %s", __func__
, xpath
);
217 zlog_backtrace(LOG_ERR
);
221 sleaf
= (const struct lys_node_leaf
*)snode
;
223 enums
= &type
->info
.enums
;
224 while (enums
->count
== 0 && type
->der
) {
225 type
= &type
->der
->type
;
226 enums
= &type
->info
.enums
;
228 for (unsigned int i
= 0; i
< enums
->count
; i
++) {
229 const struct lys_type_enum
*enm
= &enums
->enm
[i
];
231 if (value
== enm
->value
)
232 return yang_data_new(xpath
, enm
->name
);
235 flog_err(EC_LIB_YANG_DATA_CONVERT
,
236 "%s: couldn't convert enum to string [xpath %s]", __func__
,
238 zlog_backtrace(LOG_ERR
);
242 int yang_dnode_get_enum(const struct lyd_node
*dnode
, const char *xpath_fmt
,
245 const struct lyd_node_leaf_list
*dleaf
;
250 char xpath
[XPATH_MAXLEN
];
252 va_start(ap
, xpath_fmt
);
253 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
255 dnode
= yang_dnode_get(dnode
, xpath
);
256 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
259 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
260 assert(dleaf
->value_type
== LY_TYPE_ENUM
);
261 return dleaf
->value
.enm
->value
;
264 int yang_get_default_enum(const char *xpath_fmt
, ...)
266 char xpath
[XPATH_MAXLEN
];
270 va_start(ap
, xpath_fmt
);
271 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
274 value
= yang_get_default_value(xpath
);
275 return yang_str2enum(xpath
, value
);
279 * Primitive type: int8.
281 int8_t yang_str2int8(const char *value
)
283 return strtol(value
, NULL
, 10);
286 struct yang_data
*yang_data_new_int8(const char *xpath
, int8_t value
)
288 char value_str
[BUFSIZ
];
290 snprintf(value_str
, sizeof(value_str
), "%d", value
);
291 return yang_data_new(xpath
, value_str
);
294 int8_t yang_dnode_get_int8(const struct lyd_node
*dnode
, const char *xpath_fmt
,
297 const struct lyd_node_leaf_list
*dleaf
;
302 char xpath
[XPATH_MAXLEN
];
304 va_start(ap
, xpath_fmt
);
305 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
307 dnode
= yang_dnode_get(dnode
, xpath
);
308 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
311 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
312 assert(dleaf
->value_type
== LY_TYPE_INT8
);
313 return dleaf
->value
.int8
;
316 int8_t yang_get_default_int8(const char *xpath_fmt
, ...)
318 char xpath
[XPATH_MAXLEN
];
322 va_start(ap
, xpath_fmt
);
323 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
326 value
= yang_get_default_value(xpath
);
327 return yang_str2int8(value
);
331 * Primitive type: int16.
333 int16_t yang_str2int16(const char *value
)
335 return strtol(value
, NULL
, 10);
338 struct yang_data
*yang_data_new_int16(const char *xpath
, int16_t value
)
340 char value_str
[BUFSIZ
];
342 snprintf(value_str
, sizeof(value_str
), "%d", value
);
343 return yang_data_new(xpath
, value_str
);
346 int16_t yang_dnode_get_int16(const struct lyd_node
*dnode
,
347 const char *xpath_fmt
, ...)
349 const struct lyd_node_leaf_list
*dleaf
;
354 char xpath
[XPATH_MAXLEN
];
356 va_start(ap
, xpath_fmt
);
357 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
359 dnode
= yang_dnode_get(dnode
, xpath
);
360 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
363 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
364 assert(dleaf
->value_type
== LY_TYPE_INT16
);
365 return dleaf
->value
.int16
;
368 int16_t yang_get_default_int16(const char *xpath_fmt
, ...)
370 char xpath
[XPATH_MAXLEN
];
374 va_start(ap
, xpath_fmt
);
375 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
378 value
= yang_get_default_value(xpath
);
379 return yang_str2int16(value
);
383 * Primitive type: int32.
385 int32_t yang_str2int32(const char *value
)
387 return strtol(value
, NULL
, 10);
390 struct yang_data
*yang_data_new_int32(const char *xpath
, int32_t value
)
392 char value_str
[BUFSIZ
];
394 snprintf(value_str
, sizeof(value_str
), "%d", value
);
395 return yang_data_new(xpath
, value_str
);
398 int32_t yang_dnode_get_int32(const struct lyd_node
*dnode
,
399 const char *xpath_fmt
, ...)
401 const struct lyd_node_leaf_list
*dleaf
;
406 char xpath
[XPATH_MAXLEN
];
408 va_start(ap
, xpath_fmt
);
409 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
411 dnode
= yang_dnode_get(dnode
, xpath
);
412 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
415 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
416 assert(dleaf
->value_type
== LY_TYPE_INT32
);
417 return dleaf
->value
.int32
;
420 int32_t yang_get_default_int32(const char *xpath_fmt
, ...)
422 char xpath
[XPATH_MAXLEN
];
426 va_start(ap
, xpath_fmt
);
427 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
430 value
= yang_get_default_value(xpath
);
431 return yang_str2int32(value
);
435 * Primitive type: int64.
437 int64_t yang_str2int64(const char *value
)
439 return strtoll(value
, NULL
, 10);
442 struct yang_data
*yang_data_new_int64(const char *xpath
, int64_t value
)
444 char value_str
[BUFSIZ
];
446 snprintf(value_str
, sizeof(value_str
), "%" PRId64
, value
);
447 return yang_data_new(xpath
, value_str
);
450 int64_t yang_dnode_get_int64(const struct lyd_node
*dnode
,
451 const char *xpath_fmt
, ...)
453 const struct lyd_node_leaf_list
*dleaf
;
458 char xpath
[XPATH_MAXLEN
];
460 va_start(ap
, xpath_fmt
);
461 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
463 dnode
= yang_dnode_get(dnode
, xpath
);
464 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
467 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
468 assert(dleaf
->value_type
== LY_TYPE_INT64
);
469 return dleaf
->value
.int64
;
472 int64_t yang_get_default_int64(const char *xpath_fmt
, ...)
474 char xpath
[XPATH_MAXLEN
];
478 va_start(ap
, xpath_fmt
);
479 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
482 value
= yang_get_default_value(xpath
);
483 return yang_str2int64(value
);
487 * Primitive type: uint8.
489 uint8_t yang_str2uint8(const char *value
)
491 return strtoul(value
, NULL
, 10);
494 struct yang_data
*yang_data_new_uint8(const char *xpath
, uint8_t value
)
496 char value_str
[BUFSIZ
];
498 snprintf(value_str
, sizeof(value_str
), "%u", value
);
499 return yang_data_new(xpath
, value_str
);
502 uint8_t yang_dnode_get_uint8(const struct lyd_node
*dnode
,
503 const char *xpath_fmt
, ...)
505 const struct lyd_node_leaf_list
*dleaf
;
510 char xpath
[XPATH_MAXLEN
];
512 va_start(ap
, xpath_fmt
);
513 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
515 dnode
= yang_dnode_get(dnode
, xpath
);
516 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
519 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
520 assert(dleaf
->value_type
== LY_TYPE_UINT8
);
521 return dleaf
->value
.uint8
;
524 uint8_t yang_get_default_uint8(const char *xpath_fmt
, ...)
526 char xpath
[XPATH_MAXLEN
];
530 va_start(ap
, xpath_fmt
);
531 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
534 value
= yang_get_default_value(xpath
);
535 return yang_str2uint8(value
);
539 * Primitive type: uint16.
541 uint16_t yang_str2uint16(const char *value
)
543 return strtoul(value
, NULL
, 10);
546 struct yang_data
*yang_data_new_uint16(const char *xpath
, uint16_t value
)
548 char value_str
[BUFSIZ
];
550 snprintf(value_str
, sizeof(value_str
), "%u", value
);
551 return yang_data_new(xpath
, value_str
);
554 uint16_t yang_dnode_get_uint16(const struct lyd_node
*dnode
,
555 const char *xpath_fmt
, ...)
557 const struct lyd_node_leaf_list
*dleaf
;
562 char xpath
[XPATH_MAXLEN
];
564 va_start(ap
, xpath_fmt
);
565 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
567 dnode
= yang_dnode_get(dnode
, xpath
);
568 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
571 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
572 assert(dleaf
->value_type
== LY_TYPE_UINT16
);
573 return dleaf
->value
.uint16
;
576 uint16_t yang_get_default_uint16(const char *xpath_fmt
, ...)
578 char xpath
[XPATH_MAXLEN
];
582 va_start(ap
, xpath_fmt
);
583 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
586 value
= yang_get_default_value(xpath
);
587 return yang_str2uint16(value
);
591 * Primitive type: uint32.
593 uint32_t yang_str2uint32(const char *value
)
595 return strtoul(value
, NULL
, 10);
598 struct yang_data
*yang_data_new_uint32(const char *xpath
, uint32_t value
)
600 char value_str
[BUFSIZ
];
602 snprintf(value_str
, sizeof(value_str
), "%u", value
);
603 return yang_data_new(xpath
, value_str
);
606 uint32_t yang_dnode_get_uint32(const struct lyd_node
*dnode
,
607 const char *xpath_fmt
, ...)
609 const struct lyd_node_leaf_list
*dleaf
;
614 char xpath
[XPATH_MAXLEN
];
616 va_start(ap
, xpath_fmt
);
617 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
619 dnode
= yang_dnode_get(dnode
, xpath
);
620 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
623 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
624 assert(dleaf
->value_type
== LY_TYPE_UINT32
);
625 return dleaf
->value
.uint32
;
628 uint32_t yang_get_default_uint32(const char *xpath_fmt
, ...)
630 char xpath
[XPATH_MAXLEN
];
634 va_start(ap
, xpath_fmt
);
635 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
638 value
= yang_get_default_value(xpath
);
639 return yang_str2uint32(value
);
643 * Primitive type: uint64.
645 uint64_t yang_str2uint64(const char *value
)
647 return strtoull(value
, NULL
, 10);
650 struct yang_data
*yang_data_new_uint64(const char *xpath
, uint64_t value
)
652 char value_str
[BUFSIZ
];
654 snprintf(value_str
, sizeof(value_str
), "%" PRIu64
, value
);
655 return yang_data_new(xpath
, value_str
);
658 uint64_t yang_dnode_get_uint64(const struct lyd_node
*dnode
,
659 const char *xpath_fmt
, ...)
661 const struct lyd_node_leaf_list
*dleaf
;
666 char xpath
[XPATH_MAXLEN
];
668 va_start(ap
, xpath_fmt
);
669 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
671 dnode
= yang_dnode_get(dnode
, xpath
);
672 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
675 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
676 assert(dleaf
->value_type
== LY_TYPE_UINT64
);
677 return dleaf
->value
.uint64
;
680 uint64_t yang_get_default_uint64(const char *xpath_fmt
, ...)
682 char xpath
[XPATH_MAXLEN
];
686 va_start(ap
, xpath_fmt
);
687 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
690 value
= yang_get_default_value(xpath
);
691 return yang_str2uint64(value
);
695 * Primitive type: string.
697 * All string wrappers can be used with non-string types.
699 struct yang_data
*yang_data_new_string(const char *xpath
, const char *value
)
701 return yang_data_new(xpath
, value
);
704 const char *yang_dnode_get_string(const struct lyd_node
*dnode
,
705 const char *xpath_fmt
, ...)
707 const struct lyd_node_leaf_list
*dleaf
;
712 char xpath
[XPATH_MAXLEN
];
714 va_start(ap
, xpath_fmt
);
715 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
717 dnode
= yang_dnode_get(dnode
, xpath
);
718 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
721 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
722 return dleaf
->value_str
;
725 void yang_dnode_get_string_buf(char *buf
, size_t size
,
726 const struct lyd_node
*dnode
,
727 const char *xpath_fmt
, ...)
729 const struct lyd_node_leaf_list
*dleaf
;
734 char xpath
[XPATH_MAXLEN
];
736 va_start(ap
, xpath_fmt
);
737 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
739 dnode
= yang_dnode_get(dnode
, xpath
);
740 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
743 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
744 if (strlcpy(buf
, dleaf
->value_str
, size
) >= size
) {
745 char xpath
[XPATH_MAXLEN
];
747 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
748 flog_warn(EC_LIB_YANG_DATA_TRUNCATED
,
749 "%s: value was truncated [xpath %s]", __func__
,
754 const char *yang_get_default_string(const char *xpath_fmt
, ...)
756 char xpath
[XPATH_MAXLEN
];
759 va_start(ap
, xpath_fmt
);
760 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
763 return yang_get_default_value(xpath
);
766 void yang_get_default_string_buf(char *buf
, size_t size
, const char *xpath_fmt
,
769 char xpath
[XPATH_MAXLEN
];
773 va_start(ap
, xpath_fmt
);
774 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
777 value
= yang_get_default_value(xpath
);
778 if (strlcpy(buf
, value
, size
) >= size
)
779 flog_warn(EC_LIB_YANG_DATA_TRUNCATED
,
780 "%s: value was truncated [xpath %s]", __func__
,
785 * Derived type: ipv4.
787 void yang_str2ipv4(const char *value
, struct in_addr
*addr
)
789 (void)inet_pton(AF_INET
, value
, addr
);
792 struct yang_data
*yang_data_new_ipv4(const char *xpath
,
793 const struct in_addr
*addr
)
795 char value_str
[INET_ADDRSTRLEN
];
797 (void)inet_ntop(AF_INET
, addr
, value_str
, sizeof(value_str
));
798 return yang_data_new(xpath
, value_str
);
801 void yang_dnode_get_ipv4(struct in_addr
*addr
, const struct lyd_node
*dnode
,
802 const char *xpath_fmt
, ...)
804 const struct lyd_node_leaf_list
*dleaf
;
809 char xpath
[XPATH_MAXLEN
];
811 va_start(ap
, xpath_fmt
);
812 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
814 dnode
= yang_dnode_get(dnode
, xpath
);
815 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
818 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
819 assert(dleaf
->value_type
== LY_TYPE_STRING
);
820 (void)inet_pton(AF_INET
, dleaf
->value_str
, addr
);
823 void yang_get_default_ipv4(struct in_addr
*var
, const char *xpath_fmt
, ...)
825 char xpath
[XPATH_MAXLEN
];
829 va_start(ap
, xpath_fmt
);
830 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
833 value
= yang_get_default_value(xpath
);
834 yang_str2ipv4(value
, var
);
838 * Derived type: ipv4p.
840 void yang_str2ipv4p(const char *value
, union prefixptr prefix
)
842 struct prefix_ipv4
*prefix4
= prefix
.p4
;
844 (void)str2prefix_ipv4(value
, prefix4
);
845 apply_mask_ipv4(prefix4
);
848 struct yang_data
*yang_data_new_ipv4p(const char *xpath
,
849 union prefixconstptr prefix
)
851 char value_str
[PREFIX2STR_BUFFER
];
853 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
854 return yang_data_new(xpath
, value_str
);
857 void yang_dnode_get_ipv4p(union prefixptr prefix
, const struct lyd_node
*dnode
,
858 const char *xpath_fmt
, ...)
860 const struct lyd_node_leaf_list
*dleaf
;
861 struct prefix_ipv4
*prefix4
= prefix
.p4
;
866 char xpath
[XPATH_MAXLEN
];
868 va_start(ap
, xpath_fmt
);
869 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
871 dnode
= yang_dnode_get(dnode
, xpath
);
872 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
875 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
876 assert(dleaf
->value_type
== LY_TYPE_STRING
);
877 (void)str2prefix_ipv4(dleaf
->value_str
, prefix4
);
880 void yang_get_default_ipv4p(union prefixptr var
, const char *xpath_fmt
, ...)
882 char xpath
[XPATH_MAXLEN
];
886 va_start(ap
, xpath_fmt
);
887 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
890 value
= yang_get_default_value(xpath
);
891 yang_str2ipv4p(value
, var
);
895 * Derived type: ipv6.
897 void yang_str2ipv6(const char *value
, struct in6_addr
*addr
)
899 (void)inet_pton(AF_INET6
, value
, addr
);
902 struct yang_data
*yang_data_new_ipv6(const char *xpath
,
903 const struct in6_addr
*addr
)
905 char value_str
[INET6_ADDRSTRLEN
];
907 (void)inet_ntop(AF_INET6
, addr
, value_str
, sizeof(value_str
));
908 return yang_data_new(xpath
, value_str
);
911 void yang_dnode_get_ipv6(struct in6_addr
*addr
, const struct lyd_node
*dnode
,
912 const char *xpath_fmt
, ...)
914 const struct lyd_node_leaf_list
*dleaf
;
919 char xpath
[XPATH_MAXLEN
];
921 va_start(ap
, xpath_fmt
);
922 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
924 dnode
= yang_dnode_get(dnode
, xpath
);
925 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
928 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
929 assert(dleaf
->value_type
== LY_TYPE_STRING
);
930 (void)inet_pton(AF_INET6
, dleaf
->value_str
, addr
);
933 void yang_get_default_ipv6(struct in6_addr
*var
, const char *xpath_fmt
, ...)
935 char xpath
[XPATH_MAXLEN
];
939 va_start(ap
, xpath_fmt
);
940 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
943 value
= yang_get_default_value(xpath
);
944 yang_str2ipv6(value
, var
);
948 * Derived type: ipv6p.
950 void yang_str2ipv6p(const char *value
, union prefixptr prefix
)
952 struct prefix_ipv6
*prefix6
= prefix
.p6
;
954 (void)str2prefix_ipv6(value
, prefix6
);
955 apply_mask_ipv6(prefix6
);
958 struct yang_data
*yang_data_new_ipv6p(const char *xpath
,
959 union prefixconstptr prefix
)
961 char value_str
[PREFIX2STR_BUFFER
];
963 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
964 return yang_data_new(xpath
, value_str
);
967 void yang_dnode_get_ipv6p(union prefixptr prefix
, const struct lyd_node
*dnode
,
968 const char *xpath_fmt
, ...)
970 const struct lyd_node_leaf_list
*dleaf
;
971 struct prefix_ipv6
*prefix6
= prefix
.p6
;
976 char xpath
[XPATH_MAXLEN
];
978 va_start(ap
, xpath_fmt
);
979 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
981 dnode
= yang_dnode_get(dnode
, xpath
);
982 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
985 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
986 assert(dleaf
->value_type
== LY_TYPE_STRING
);
987 (void)str2prefix_ipv6(dleaf
->value_str
, prefix6
);
990 void yang_get_default_ipv6p(union prefixptr var
, const char *xpath_fmt
, ...)
992 char xpath
[XPATH_MAXLEN
];
996 va_start(ap
, xpath_fmt
);
997 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1000 value
= yang_get_default_value(xpath
);
1001 yang_str2ipv6p(value
, var
);