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"
27 static const char *yang_get_default_value(const char *xpath
)
29 const struct lys_node
*snode
;
32 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
34 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
35 "%s: unknown data path: %s", __func__
, xpath
);
36 zlog_backtrace(LOG_ERR
);
40 value
= yang_snode_get_default(snode
);
46 #define YANG_DNODE_GET_ASSERT(dnode, xpath) \
48 if ((dnode) == NULL) { \
49 flog_err(EC_LIB_YANG_DNODE_NOT_FOUND, \
50 "%s: couldn't find %s", __func__, (xpath)); \
51 zlog_backtrace(LOG_ERR); \
57 * Primitive type: bool.
59 bool yang_str2bool(const char *value
)
61 return strmatch(value
, "true");
64 struct yang_data
*yang_data_new_bool(const char *xpath
, bool value
)
66 return yang_data_new(xpath
, (value
) ? "true" : "false");
69 bool yang_dnode_get_bool(const struct lyd_node
*dnode
, const char *xpath_fmt
,
72 const struct lyd_node_leaf_list
*dleaf
;
77 char xpath
[XPATH_MAXLEN
];
79 va_start(ap
, xpath_fmt
);
80 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
82 dnode
= yang_dnode_get(dnode
, xpath
);
83 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
86 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
87 assert(dleaf
->value_type
== LY_TYPE_BOOL
);
88 return dleaf
->value
.bln
;
91 bool yang_get_default_bool(const char *xpath_fmt
, ...)
93 char xpath
[XPATH_MAXLEN
];
97 va_start(ap
, xpath_fmt
);
98 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
101 value
= yang_get_default_value(xpath
);
102 return yang_str2bool(value
);
106 * Primitive type: dec64.
108 double yang_str2dec64(const char *xpath
, const char *value
)
112 if (sscanf(value
, "%lf", &dbl
) != 1) {
113 flog_err(EC_LIB_YANG_DATA_CONVERT
,
114 "%s: couldn't convert string to decimal64 [xpath %s]",
116 zlog_backtrace(LOG_ERR
);
123 struct yang_data
*yang_data_new_dec64(const char *xpath
, double value
)
125 char value_str
[BUFSIZ
];
127 snprintf(value_str
, sizeof(value_str
), "%lf", value
);
128 return yang_data_new(xpath
, value_str
);
131 double yang_dnode_get_dec64(const struct lyd_node
*dnode
, const char *xpath_fmt
,
134 const struct lyd_node_leaf_list
*dleaf
;
139 char xpath
[XPATH_MAXLEN
];
141 va_start(ap
, xpath_fmt
);
142 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
144 dnode
= yang_dnode_get(dnode
, xpath
);
145 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
148 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
149 assert(dleaf
->value_type
== LY_TYPE_DEC64
);
151 return lyd_dec64_to_double(dnode
);
154 double yang_get_default_dec64(const char *xpath_fmt
, ...)
156 char xpath
[XPATH_MAXLEN
];
160 va_start(ap
, xpath_fmt
);
161 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
164 value
= yang_get_default_value(xpath
);
165 return yang_str2dec64(xpath
, value
);
169 * Primitive type: enum.
171 int yang_str2enum(const char *xpath
, const char *value
)
173 const struct lys_node
*snode
;
174 const struct lys_node_leaf
*sleaf
;
175 const struct lys_type
*type
;
176 const struct lys_type_info_enums
*enums
;
178 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
180 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
181 "%s: unknown data path: %s", __func__
, xpath
);
182 zlog_backtrace(LOG_ERR
);
186 sleaf
= (const struct lys_node_leaf
*)snode
;
188 enums
= &type
->info
.enums
;
189 while (enums
->count
== 0 && type
->der
) {
190 type
= &type
->der
->type
;
191 enums
= &type
->info
.enums
;
193 for (unsigned int i
= 0; i
< enums
->count
; i
++) {
194 const struct lys_type_enum
*enm
= &enums
->enm
[i
];
196 if (strmatch(value
, enm
->name
))
200 flog_err(EC_LIB_YANG_DATA_CONVERT
,
201 "%s: couldn't convert string to enum [xpath %s]", __func__
,
203 zlog_backtrace(LOG_ERR
);
207 struct yang_data
*yang_data_new_enum(const char *xpath
, int value
)
209 const struct lys_node
*snode
;
210 const struct lys_node_leaf
*sleaf
;
211 const struct lys_type
*type
;
212 const struct lys_type_info_enums
*enums
;
214 snode
= ly_ctx_get_node(ly_native_ctx
, NULL
, xpath
, 0);
216 flog_err(EC_LIB_YANG_UNKNOWN_DATA_PATH
,
217 "%s: unknown data path: %s", __func__
, xpath
);
218 zlog_backtrace(LOG_ERR
);
222 sleaf
= (const struct lys_node_leaf
*)snode
;
224 enums
= &type
->info
.enums
;
225 while (enums
->count
== 0 && type
->der
) {
226 type
= &type
->der
->type
;
227 enums
= &type
->info
.enums
;
229 for (unsigned int i
= 0; i
< enums
->count
; i
++) {
230 const struct lys_type_enum
*enm
= &enums
->enm
[i
];
232 if (value
== enm
->value
)
233 return yang_data_new(xpath
, enm
->name
);
236 flog_err(EC_LIB_YANG_DATA_CONVERT
,
237 "%s: couldn't convert enum to string [xpath %s]", __func__
,
239 zlog_backtrace(LOG_ERR
);
243 int yang_dnode_get_enum(const struct lyd_node
*dnode
, const char *xpath_fmt
,
246 const struct lyd_node_leaf_list
*dleaf
;
251 char xpath
[XPATH_MAXLEN
];
253 va_start(ap
, xpath_fmt
);
254 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
256 dnode
= yang_dnode_get(dnode
, xpath
);
257 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
260 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
261 assert(dleaf
->value_type
== LY_TYPE_ENUM
);
262 return dleaf
->value
.enm
->value
;
265 int yang_get_default_enum(const char *xpath_fmt
, ...)
267 char xpath
[XPATH_MAXLEN
];
271 va_start(ap
, xpath_fmt
);
272 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
275 value
= yang_get_default_value(xpath
);
276 return yang_str2enum(xpath
, value
);
280 * Primitive type: int8.
282 int8_t yang_str2int8(const char *value
)
284 return strtol(value
, NULL
, 10);
287 struct yang_data
*yang_data_new_int8(const char *xpath
, int8_t value
)
289 char value_str
[BUFSIZ
];
291 snprintf(value_str
, sizeof(value_str
), "%d", value
);
292 return yang_data_new(xpath
, value_str
);
295 int8_t yang_dnode_get_int8(const struct lyd_node
*dnode
, const char *xpath_fmt
,
298 const struct lyd_node_leaf_list
*dleaf
;
303 char xpath
[XPATH_MAXLEN
];
305 va_start(ap
, xpath_fmt
);
306 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
308 dnode
= yang_dnode_get(dnode
, xpath
);
309 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
312 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
313 assert(dleaf
->value_type
== LY_TYPE_INT8
);
314 return dleaf
->value
.int8
;
317 int8_t yang_get_default_int8(const char *xpath_fmt
, ...)
319 char xpath
[XPATH_MAXLEN
];
323 va_start(ap
, xpath_fmt
);
324 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
327 value
= yang_get_default_value(xpath
);
328 return yang_str2int8(value
);
332 * Primitive type: int16.
334 int16_t yang_str2int16(const char *value
)
336 return strtol(value
, NULL
, 10);
339 struct yang_data
*yang_data_new_int16(const char *xpath
, int16_t value
)
341 char value_str
[BUFSIZ
];
343 snprintf(value_str
, sizeof(value_str
), "%d", value
);
344 return yang_data_new(xpath
, value_str
);
347 int16_t yang_dnode_get_int16(const struct lyd_node
*dnode
,
348 const char *xpath_fmt
, ...)
350 const struct lyd_node_leaf_list
*dleaf
;
355 char xpath
[XPATH_MAXLEN
];
357 va_start(ap
, xpath_fmt
);
358 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
360 dnode
= yang_dnode_get(dnode
, xpath
);
361 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
364 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
365 assert(dleaf
->value_type
== LY_TYPE_INT16
);
366 return dleaf
->value
.int16
;
369 int16_t yang_get_default_int16(const char *xpath_fmt
, ...)
371 char xpath
[XPATH_MAXLEN
];
375 va_start(ap
, xpath_fmt
);
376 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
379 value
= yang_get_default_value(xpath
);
380 return yang_str2int16(value
);
384 * Primitive type: int32.
386 int32_t yang_str2int32(const char *value
)
388 return strtol(value
, NULL
, 10);
391 struct yang_data
*yang_data_new_int32(const char *xpath
, int32_t value
)
393 char value_str
[BUFSIZ
];
395 snprintf(value_str
, sizeof(value_str
), "%d", value
);
396 return yang_data_new(xpath
, value_str
);
399 int32_t yang_dnode_get_int32(const struct lyd_node
*dnode
,
400 const char *xpath_fmt
, ...)
402 const struct lyd_node_leaf_list
*dleaf
;
407 char xpath
[XPATH_MAXLEN
];
409 va_start(ap
, xpath_fmt
);
410 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
412 dnode
= yang_dnode_get(dnode
, xpath
);
413 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
416 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
417 assert(dleaf
->value_type
== LY_TYPE_INT32
);
418 return dleaf
->value
.int32
;
421 int32_t yang_get_default_int32(const char *xpath_fmt
, ...)
423 char xpath
[XPATH_MAXLEN
];
427 va_start(ap
, xpath_fmt
);
428 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
431 value
= yang_get_default_value(xpath
);
432 return yang_str2int32(value
);
436 * Primitive type: int64.
438 int64_t yang_str2int64(const char *value
)
440 return strtoll(value
, NULL
, 10);
443 struct yang_data
*yang_data_new_int64(const char *xpath
, int64_t value
)
445 char value_str
[BUFSIZ
];
447 snprintfrr(value_str
, sizeof(value_str
), "%" PRId64
, value
);
448 return yang_data_new(xpath
, value_str
);
451 int64_t yang_dnode_get_int64(const struct lyd_node
*dnode
,
452 const char *xpath_fmt
, ...)
454 const struct lyd_node_leaf_list
*dleaf
;
459 char xpath
[XPATH_MAXLEN
];
461 va_start(ap
, xpath_fmt
);
462 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
464 dnode
= yang_dnode_get(dnode
, xpath
);
465 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
468 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
469 assert(dleaf
->value_type
== LY_TYPE_INT64
);
470 return dleaf
->value
.int64
;
473 int64_t yang_get_default_int64(const char *xpath_fmt
, ...)
475 char xpath
[XPATH_MAXLEN
];
479 va_start(ap
, xpath_fmt
);
480 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
483 value
= yang_get_default_value(xpath
);
484 return yang_str2int64(value
);
488 * Primitive type: uint8.
490 uint8_t yang_str2uint8(const char *value
)
492 return strtoul(value
, NULL
, 10);
495 struct yang_data
*yang_data_new_uint8(const char *xpath
, uint8_t value
)
497 char value_str
[BUFSIZ
];
499 snprintf(value_str
, sizeof(value_str
), "%u", value
);
500 return yang_data_new(xpath
, value_str
);
503 uint8_t yang_dnode_get_uint8(const struct lyd_node
*dnode
,
504 const char *xpath_fmt
, ...)
506 const struct lyd_node_leaf_list
*dleaf
;
511 char xpath
[XPATH_MAXLEN
];
513 va_start(ap
, xpath_fmt
);
514 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
516 dnode
= yang_dnode_get(dnode
, xpath
);
517 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
520 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
521 assert(dleaf
->value_type
== LY_TYPE_UINT8
);
522 return dleaf
->value
.uint8
;
525 uint8_t yang_get_default_uint8(const char *xpath_fmt
, ...)
527 char xpath
[XPATH_MAXLEN
];
531 va_start(ap
, xpath_fmt
);
532 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
535 value
= yang_get_default_value(xpath
);
536 return yang_str2uint8(value
);
540 * Primitive type: uint16.
542 uint16_t yang_str2uint16(const char *value
)
544 return strtoul(value
, NULL
, 10);
547 struct yang_data
*yang_data_new_uint16(const char *xpath
, uint16_t value
)
549 char value_str
[BUFSIZ
];
551 snprintf(value_str
, sizeof(value_str
), "%u", value
);
552 return yang_data_new(xpath
, value_str
);
555 uint16_t yang_dnode_get_uint16(const struct lyd_node
*dnode
,
556 const char *xpath_fmt
, ...)
558 const struct lyd_node_leaf_list
*dleaf
;
563 char xpath
[XPATH_MAXLEN
];
565 va_start(ap
, xpath_fmt
);
566 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
568 dnode
= yang_dnode_get(dnode
, xpath
);
569 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
572 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
573 assert(dleaf
->value_type
== LY_TYPE_UINT16
);
574 return dleaf
->value
.uint16
;
577 uint16_t yang_get_default_uint16(const char *xpath_fmt
, ...)
579 char xpath
[XPATH_MAXLEN
];
583 va_start(ap
, xpath_fmt
);
584 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
587 value
= yang_get_default_value(xpath
);
588 return yang_str2uint16(value
);
592 * Primitive type: uint32.
594 uint32_t yang_str2uint32(const char *value
)
596 return strtoul(value
, NULL
, 10);
599 struct yang_data
*yang_data_new_uint32(const char *xpath
, uint32_t value
)
601 char value_str
[BUFSIZ
];
603 snprintf(value_str
, sizeof(value_str
), "%u", value
);
604 return yang_data_new(xpath
, value_str
);
607 uint32_t yang_dnode_get_uint32(const struct lyd_node
*dnode
,
608 const char *xpath_fmt
, ...)
610 const struct lyd_node_leaf_list
*dleaf
;
615 char xpath
[XPATH_MAXLEN
];
617 va_start(ap
, xpath_fmt
);
618 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
620 dnode
= yang_dnode_get(dnode
, xpath
);
621 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
624 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
625 assert(dleaf
->value_type
== LY_TYPE_UINT32
);
626 return dleaf
->value
.uint32
;
629 uint32_t yang_get_default_uint32(const char *xpath_fmt
, ...)
631 char xpath
[XPATH_MAXLEN
];
635 va_start(ap
, xpath_fmt
);
636 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
639 value
= yang_get_default_value(xpath
);
640 return yang_str2uint32(value
);
644 * Primitive type: uint64.
646 uint64_t yang_str2uint64(const char *value
)
648 return strtoull(value
, NULL
, 10);
651 struct yang_data
*yang_data_new_uint64(const char *xpath
, uint64_t value
)
653 char value_str
[BUFSIZ
];
655 snprintfrr(value_str
, sizeof(value_str
), "%" PRIu64
, value
);
656 return yang_data_new(xpath
, value_str
);
659 uint64_t yang_dnode_get_uint64(const struct lyd_node
*dnode
,
660 const char *xpath_fmt
, ...)
662 const struct lyd_node_leaf_list
*dleaf
;
667 char xpath
[XPATH_MAXLEN
];
669 va_start(ap
, xpath_fmt
);
670 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
672 dnode
= yang_dnode_get(dnode
, xpath
);
673 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
676 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
677 assert(dleaf
->value_type
== LY_TYPE_UINT64
);
678 return dleaf
->value
.uint64
;
681 uint64_t yang_get_default_uint64(const char *xpath_fmt
, ...)
683 char xpath
[XPATH_MAXLEN
];
687 va_start(ap
, xpath_fmt
);
688 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
691 value
= yang_get_default_value(xpath
);
692 return yang_str2uint64(value
);
696 * Primitive type: string.
698 * All string wrappers can be used with non-string types.
700 struct yang_data
*yang_data_new_string(const char *xpath
, const char *value
)
702 return yang_data_new(xpath
, value
);
705 const char *yang_dnode_get_string(const struct lyd_node
*dnode
,
706 const char *xpath_fmt
, ...)
708 const struct lyd_node_leaf_list
*dleaf
;
713 char xpath
[XPATH_MAXLEN
];
715 va_start(ap
, xpath_fmt
);
716 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
718 dnode
= yang_dnode_get(dnode
, xpath
);
719 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
722 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
723 return dleaf
->value_str
;
726 void yang_dnode_get_string_buf(char *buf
, size_t size
,
727 const struct lyd_node
*dnode
,
728 const char *xpath_fmt
, ...)
730 const struct lyd_node_leaf_list
*dleaf
;
735 char xpath
[XPATH_MAXLEN
];
737 va_start(ap
, xpath_fmt
);
738 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
740 dnode
= yang_dnode_get(dnode
, xpath
);
741 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
744 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
745 if (strlcpy(buf
, dleaf
->value_str
, size
) >= size
) {
746 char xpath
[XPATH_MAXLEN
];
748 yang_dnode_get_path(dnode
, xpath
, sizeof(xpath
));
749 flog_warn(EC_LIB_YANG_DATA_TRUNCATED
,
750 "%s: value was truncated [xpath %s]", __func__
,
755 const char *yang_get_default_string(const char *xpath_fmt
, ...)
757 char xpath
[XPATH_MAXLEN
];
760 va_start(ap
, xpath_fmt
);
761 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
764 return yang_get_default_value(xpath
);
767 void yang_get_default_string_buf(char *buf
, size_t size
, const char *xpath_fmt
,
770 char xpath
[XPATH_MAXLEN
];
774 va_start(ap
, xpath_fmt
);
775 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
778 value
= yang_get_default_value(xpath
);
779 if (strlcpy(buf
, value
, size
) >= size
)
780 flog_warn(EC_LIB_YANG_DATA_TRUNCATED
,
781 "%s: value was truncated [xpath %s]", __func__
,
786 * Derived type: IP prefix.
788 void yang_str2prefix(const char *value
, union prefixptr prefix
)
790 (void)str2prefix(value
, prefix
.p
);
791 apply_mask(prefix
.p
);
794 struct yang_data
*yang_data_new_prefix(const char *xpath
,
795 union prefixconstptr prefix
)
797 char value_str
[PREFIX2STR_BUFFER
];
799 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
800 return yang_data_new(xpath
, value_str
);
803 void yang_dnode_get_prefix(struct prefix
*prefix
, const struct lyd_node
*dnode
,
804 const char *xpath_fmt
, ...)
806 const struct lyd_node_leaf_list
*dleaf
;
811 char xpath
[XPATH_MAXLEN
];
813 va_start(ap
, xpath_fmt
);
814 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
816 dnode
= yang_dnode_get(dnode
, xpath
);
817 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
821 * Initialize prefix to avoid static analyzer complaints about
822 * uninitialized memory.
824 memset(prefix
, 0, sizeof(*prefix
));
826 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
827 assert(dleaf
->value_type
== LY_TYPE_STRING
);
828 (void)str2prefix(dleaf
->value_str
, prefix
);
831 void yang_get_default_prefix(union prefixptr var
, const char *xpath_fmt
, ...)
833 char xpath
[XPATH_MAXLEN
];
837 va_start(ap
, xpath_fmt
);
838 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
841 value
= yang_get_default_value(xpath
);
842 yang_str2prefix(value
, var
);
846 * Derived type: ipv4.
848 void yang_str2ipv4(const char *value
, struct in_addr
*addr
)
850 (void)inet_pton(AF_INET
, value
, addr
);
853 struct yang_data
*yang_data_new_ipv4(const char *xpath
,
854 const struct in_addr
*addr
)
856 char value_str
[INET_ADDRSTRLEN
];
858 (void)inet_ntop(AF_INET
, addr
, value_str
, sizeof(value_str
));
859 return yang_data_new(xpath
, value_str
);
862 void yang_dnode_get_ipv4(struct in_addr
*addr
, const struct lyd_node
*dnode
,
863 const char *xpath_fmt
, ...)
865 const struct lyd_node_leaf_list
*dleaf
;
870 char xpath
[XPATH_MAXLEN
];
872 va_start(ap
, xpath_fmt
);
873 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
875 dnode
= yang_dnode_get(dnode
, xpath
);
876 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
879 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
880 assert(dleaf
->value_type
== LY_TYPE_STRING
);
881 (void)inet_pton(AF_INET
, dleaf
->value_str
, addr
);
884 void yang_get_default_ipv4(struct in_addr
*var
, const char *xpath_fmt
, ...)
886 char xpath
[XPATH_MAXLEN
];
890 va_start(ap
, xpath_fmt
);
891 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
894 value
= yang_get_default_value(xpath
);
895 yang_str2ipv4(value
, var
);
899 * Derived type: ipv4p.
901 void yang_str2ipv4p(const char *value
, union prefixptr prefix
)
903 struct prefix_ipv4
*prefix4
= prefix
.p4
;
905 (void)str2prefix_ipv4(value
, prefix4
);
906 apply_mask_ipv4(prefix4
);
909 struct yang_data
*yang_data_new_ipv4p(const char *xpath
,
910 union prefixconstptr prefix
)
912 char value_str
[PREFIX2STR_BUFFER
];
914 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
915 return yang_data_new(xpath
, value_str
);
918 void yang_dnode_get_ipv4p(union prefixptr prefix
, const struct lyd_node
*dnode
,
919 const char *xpath_fmt
, ...)
921 const struct lyd_node_leaf_list
*dleaf
;
922 struct prefix_ipv4
*prefix4
= prefix
.p4
;
927 char xpath
[XPATH_MAXLEN
];
929 va_start(ap
, xpath_fmt
);
930 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
932 dnode
= yang_dnode_get(dnode
, xpath
);
933 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
936 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
937 assert(dleaf
->value_type
== LY_TYPE_STRING
);
938 (void)str2prefix_ipv4(dleaf
->value_str
, prefix4
);
941 void yang_get_default_ipv4p(union prefixptr var
, const char *xpath_fmt
, ...)
943 char xpath
[XPATH_MAXLEN
];
947 va_start(ap
, xpath_fmt
);
948 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
951 value
= yang_get_default_value(xpath
);
952 yang_str2ipv4p(value
, var
);
956 * Derived type: ipv6.
958 void yang_str2ipv6(const char *value
, struct in6_addr
*addr
)
960 (void)inet_pton(AF_INET6
, value
, addr
);
963 struct yang_data
*yang_data_new_ipv6(const char *xpath
,
964 const struct in6_addr
*addr
)
966 char value_str
[INET6_ADDRSTRLEN
];
968 (void)inet_ntop(AF_INET6
, addr
, value_str
, sizeof(value_str
));
969 return yang_data_new(xpath
, value_str
);
972 void yang_dnode_get_ipv6(struct in6_addr
*addr
, const struct lyd_node
*dnode
,
973 const char *xpath_fmt
, ...)
975 const struct lyd_node_leaf_list
*dleaf
;
980 char xpath
[XPATH_MAXLEN
];
982 va_start(ap
, xpath_fmt
);
983 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
985 dnode
= yang_dnode_get(dnode
, xpath
);
986 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
989 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
990 assert(dleaf
->value_type
== LY_TYPE_STRING
);
991 (void)inet_pton(AF_INET6
, dleaf
->value_str
, addr
);
994 void yang_get_default_ipv6(struct in6_addr
*var
, const char *xpath_fmt
, ...)
996 char xpath
[XPATH_MAXLEN
];
1000 va_start(ap
, xpath_fmt
);
1001 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1004 value
= yang_get_default_value(xpath
);
1005 yang_str2ipv6(value
, var
);
1009 * Derived type: ipv6p.
1011 void yang_str2ipv6p(const char *value
, union prefixptr prefix
)
1013 struct prefix_ipv6
*prefix6
= prefix
.p6
;
1015 (void)str2prefix_ipv6(value
, prefix6
);
1016 apply_mask_ipv6(prefix6
);
1019 struct yang_data
*yang_data_new_ipv6p(const char *xpath
,
1020 union prefixconstptr prefix
)
1022 char value_str
[PREFIX2STR_BUFFER
];
1024 (void)prefix2str(prefix
.p
, value_str
, sizeof(value_str
));
1025 return yang_data_new(xpath
, value_str
);
1028 void yang_dnode_get_ipv6p(union prefixptr prefix
, const struct lyd_node
*dnode
,
1029 const char *xpath_fmt
, ...)
1031 const struct lyd_node_leaf_list
*dleaf
;
1032 struct prefix_ipv6
*prefix6
= prefix
.p6
;
1037 char xpath
[XPATH_MAXLEN
];
1039 va_start(ap
, xpath_fmt
);
1040 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1042 dnode
= yang_dnode_get(dnode
, xpath
);
1043 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
1046 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
1047 assert(dleaf
->value_type
== LY_TYPE_STRING
);
1048 (void)str2prefix_ipv6(dleaf
->value_str
, prefix6
);
1051 void yang_get_default_ipv6p(union prefixptr var
, const char *xpath_fmt
, ...)
1053 char xpath
[XPATH_MAXLEN
];
1057 va_start(ap
, xpath_fmt
);
1058 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1061 value
= yang_get_default_value(xpath
);
1062 yang_str2ipv6p(value
, var
);
1068 void yang_str2ip(const char *value
, struct ipaddr
*ip
)
1070 (void)str2ipaddr(value
, ip
);
1073 struct yang_data
*yang_data_new_ip(const char *xpath
, const struct ipaddr
*addr
)
1075 size_t sz
= IS_IPADDR_V4(addr
) ? INET_ADDRSTRLEN
: INET6_ADDRSTRLEN
;
1078 ipaddr2str(addr
, value_str
, sizeof(value_str
));
1079 return yang_data_new(xpath
, value_str
);
1082 void yang_dnode_get_ip(struct ipaddr
*addr
, const struct lyd_node
*dnode
,
1083 const char *xpath_fmt
, ...)
1085 const struct lyd_node_leaf_list
*dleaf
;
1090 char xpath
[XPATH_MAXLEN
];
1092 va_start(ap
, xpath_fmt
);
1093 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1095 dnode
= yang_dnode_get(dnode
, xpath
);
1096 YANG_DNODE_GET_ASSERT(dnode
, xpath
);
1099 dleaf
= (const struct lyd_node_leaf_list
*)dnode
;
1100 assert(dleaf
->value_type
== LY_TYPE_STRING
);
1101 (void)str2ipaddr(dleaf
->value_str
, addr
);
1104 void yang_get_default_ip(struct ipaddr
*var
, const char *xpath_fmt
, ...)
1106 char xpath
[XPATH_MAXLEN
];
1110 va_start(ap
, xpath_fmt
);
1111 vsnprintf(xpath
, sizeof(xpath
), xpath_fmt
, ap
);
1114 value
= yang_get_default_value(xpath
);
1115 yang_str2ip(value
, var
);