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: IP prefix.
787 void yang_str2prefix(const char *value
, union prefixptr prefix
)
789 (void)str2prefix(value
, prefix
.p
);
790 apply_mask(prefix
.p
);
793 struct yang_data
*yang_data_new_prefix(const char *xpath
,
794 union prefixconstptr prefix
)
796 char value_str
[PREFIX2STR_BUFFER
];
798 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
799 return yang_data_new(xpath
, value_str
);
802 void yang_dnode_get_prefix(struct prefix
*prefix
, const struct lyd_node
*dnode
,
803 const char *xpath_fmt
, ...)
805 const struct lyd_node_leaf_list
*dleaf
;
810 char xpath
[XPATH_MAXLEN
];
812 va_start(ap
, xpath_fmt
);
813 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
815 dnode
= yang_dnode_get(dnode
, xpath
);
816 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
820 * Initialize prefix to avoid static analyzer complaints about
821 * uninitialized memory.
823 memset(prefix
, 0, sizeof(*prefix
));
825 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
826 assert(dleaf
->value_type
== LY_TYPE_STRING
);
827 (void)str2prefix(dleaf
->value_str
, prefix
);
830 void yang_get_default_prefix(union prefixptr var
, const char *xpath_fmt
, ...)
832 char xpath
[XPATH_MAXLEN
];
836 va_start(ap
, xpath_fmt
);
837 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
840 value
= yang_get_default_value(xpath
);
841 yang_str2prefix(value
, var
);
845 * Derived type: ipv4.
847 void yang_str2ipv4(const char *value
, struct in_addr
*addr
)
849 (void)inet_pton(AF_INET
, value
, addr
);
852 struct yang_data
*yang_data_new_ipv4(const char *xpath
,
853 const struct in_addr
*addr
)
855 char value_str
[INET_ADDRSTRLEN
];
857 (void)inet_ntop(AF_INET
, addr
, value_str
, sizeof(value_str
));
858 return yang_data_new(xpath
, value_str
);
861 void yang_dnode_get_ipv4(struct in_addr
*addr
, const struct lyd_node
*dnode
,
862 const char *xpath_fmt
, ...)
864 const struct lyd_node_leaf_list
*dleaf
;
869 char xpath
[XPATH_MAXLEN
];
871 va_start(ap
, xpath_fmt
);
872 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
874 dnode
= yang_dnode_get(dnode
, xpath
);
875 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
878 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
879 assert(dleaf
->value_type
== LY_TYPE_STRING
);
880 (void)inet_pton(AF_INET
, dleaf
->value_str
, addr
);
883 void yang_get_default_ipv4(struct in_addr
*var
, const char *xpath_fmt
, ...)
885 char xpath
[XPATH_MAXLEN
];
889 va_start(ap
, xpath_fmt
);
890 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
893 value
= yang_get_default_value(xpath
);
894 yang_str2ipv4(value
, var
);
898 * Derived type: ipv4p.
900 void yang_str2ipv4p(const char *value
, union prefixptr prefix
)
902 struct prefix_ipv4
*prefix4
= prefix
.p4
;
904 (void)str2prefix_ipv4(value
, prefix4
);
905 apply_mask_ipv4(prefix4
);
908 struct yang_data
*yang_data_new_ipv4p(const char *xpath
,
909 union prefixconstptr prefix
)
911 char value_str
[PREFIX2STR_BUFFER
];
913 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
914 return yang_data_new(xpath
, value_str
);
917 void yang_dnode_get_ipv4p(union prefixptr prefix
, const struct lyd_node
*dnode
,
918 const char *xpath_fmt
, ...)
920 const struct lyd_node_leaf_list
*dleaf
;
921 struct prefix_ipv4
*prefix4
= prefix
.p4
;
926 char xpath
[XPATH_MAXLEN
];
928 va_start(ap
, xpath_fmt
);
929 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
931 dnode
= yang_dnode_get(dnode
, xpath
);
932 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
935 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
936 assert(dleaf
->value_type
== LY_TYPE_STRING
);
937 (void)str2prefix_ipv4(dleaf
->value_str
, prefix4
);
940 void yang_get_default_ipv4p(union prefixptr var
, const char *xpath_fmt
, ...)
942 char xpath
[XPATH_MAXLEN
];
946 va_start(ap
, xpath_fmt
);
947 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
950 value
= yang_get_default_value(xpath
);
951 yang_str2ipv4p(value
, var
);
955 * Derived type: ipv6.
957 void yang_str2ipv6(const char *value
, struct in6_addr
*addr
)
959 (void)inet_pton(AF_INET6
, value
, addr
);
962 struct yang_data
*yang_data_new_ipv6(const char *xpath
,
963 const struct in6_addr
*addr
)
965 char value_str
[INET6_ADDRSTRLEN
];
967 (void)inet_ntop(AF_INET6
, addr
, value_str
, sizeof(value_str
));
968 return yang_data_new(xpath
, value_str
);
971 void yang_dnode_get_ipv6(struct in6_addr
*addr
, const struct lyd_node
*dnode
,
972 const char *xpath_fmt
, ...)
974 const struct lyd_node_leaf_list
*dleaf
;
979 char xpath
[XPATH_MAXLEN
];
981 va_start(ap
, xpath_fmt
);
982 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
984 dnode
= yang_dnode_get(dnode
, xpath
);
985 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
988 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
989 assert(dleaf
->value_type
== LY_TYPE_STRING
);
990 (void)inet_pton(AF_INET6
, dleaf
->value_str
, addr
);
993 void yang_get_default_ipv6(struct in6_addr
*var
, const char *xpath_fmt
, ...)
995 char xpath
[XPATH_MAXLEN
];
999 va_start(ap
, xpath_fmt
);
1000 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1003 value
= yang_get_default_value(xpath
);
1004 yang_str2ipv6(value
, var
);
1008 * Derived type: ipv6p.
1010 void yang_str2ipv6p(const char *value
, union prefixptr prefix
)
1012 struct prefix_ipv6
*prefix6
= prefix
.p6
;
1014 (void)str2prefix_ipv6(value
, prefix6
);
1015 apply_mask_ipv6(prefix6
);
1018 struct yang_data
*yang_data_new_ipv6p(const char *xpath
,
1019 union prefixconstptr prefix
)
1021 char value_str
[PREFIX2STR_BUFFER
];
1023 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
1024 return yang_data_new(xpath
, value_str
);
1027 void yang_dnode_get_ipv6p(union prefixptr prefix
, const struct lyd_node
*dnode
,
1028 const char *xpath_fmt
, ...)
1030 const struct lyd_node_leaf_list
*dleaf
;
1031 struct prefix_ipv6
*prefix6
= prefix
.p6
;
1036 char xpath
[XPATH_MAXLEN
];
1038 va_start(ap
, xpath_fmt
);
1039 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1041 dnode
= yang_dnode_get(dnode
, xpath
);
1042 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
1045 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
1046 assert(dleaf
->value_type
== LY_TYPE_STRING
);
1047 (void)str2prefix_ipv6(dleaf
->value_str
, prefix6
);
1050 void yang_get_default_ipv6p(union prefixptr var
, const char *xpath_fmt
, ...)
1052 char xpath
[XPATH_MAXLEN
];
1056 va_start(ap
, xpath_fmt
);
1057 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1060 value
= yang_get_default_value(xpath
);
1061 yang_str2ipv6p(value
, var
);
1067 void yang_str2ip(const char *value
, struct ipaddr
*ip
)
1069 (void)str2ipaddr(value
, ip
);
1072 struct yang_data
*yang_data_new_ip(const char *xpath
, const struct ipaddr
*addr
)
1074 size_t sz
= IS_IPADDR_V4(addr
) ? INET_ADDRSTRLEN
: INET6_ADDRSTRLEN
;
1077 ipaddr2str(addr
, value_str
, sizeof(value_str
));
1078 return yang_data_new(xpath
, value_str
);
1081 void yang_dnode_get_ip(struct ipaddr
*addr
, const struct lyd_node
*dnode
,
1082 const char *xpath_fmt
, ...)
1084 const struct lyd_node_leaf_list
*dleaf
;
1089 char xpath
[XPATH_MAXLEN
];
1091 va_start(ap
, xpath_fmt
);
1092 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1094 dnode
= yang_dnode_get(dnode
, xpath
);
1095 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
1098 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
1099 assert(dleaf
->value_type
== LY_TYPE_STRING
);
1100 (void)str2ipaddr(dleaf
->value_str
, addr
);
1103 void yang_get_default_ip(struct ipaddr
*var
, const char *xpath_fmt
, ...)
1105 char xpath
[XPATH_MAXLEN
];
1109 va_start(ap
, xpath_fmt
);
1110 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1113 value
= yang_get_default_value(xpath
);
1114 yang_str2ip(value
, var
);