Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ endif()

find_package(CBOR)
if(CBOR_FOUND)
set(LY_HAVE_CBOR 1)
list(APPEND libsrc src/parser_cbor.c src/lcbor.c src/printer_cbor.c)
list(APPEND headers src/lcbor.h)
list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c src/printer_cbor.c)
Expand Down Expand Up @@ -466,10 +467,8 @@ target_link_libraries(yang ${PCRE2_LIBRARIES})
# link libcbor if found
if(CBOR_FOUND)
if(TARGET yangobj)
target_compile_definitions(yangobj PRIVATE ENABLE_CBOR_SUPPORT)
target_include_directories(yangobj PRIVATE ${CBOR_INCLUDE_DIR})
endif()
target_compile_definitions(yang PRIVATE ENABLE_CBOR_SUPPORT)
target_include_directories(yang PRIVATE ${CBOR_INCLUDE_DIR})
target_link_libraries(yang ${CBOR_LIBRARY})
endif()
Expand Down
3 changes: 3 additions & 0 deletions src/ly_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@
/** regex.h BSD flag */
#cmakedefine HAVE_REG_STARTEND

/** libyang built with CBOR support (libcbor), ::LYD_CBOR is usable */
#cmakedefine LY_HAVE_CBOR

#endif /* LY_CONFIG_H_ */
193 changes: 183 additions & 10 deletions src/parser_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,50 @@ lydcbor_check_string(const struct ly_ctx *ctx, const char *str, size_t str_len)
return LY_SUCCESS;
}

/**
* @brief Concatenate the chunks of an indefinite-length CBOR text or byte string.
*
* @param[in] ctx Context for logging.
* @param[in] item Indefinite-length string item.
* @param[out] str_val String value (allocated, caller must free).
* @param[out] str_len String length.
* @return LY_ERR value.
*/
static LY_ERR
lydcbor_chunks_to_string(const struct ly_ctx *ctx, const cbor_item_t *item, char **str_val, size_t *str_len)
{
ly_bool text = cbor_isa_string(item);
cbor_item_t **chunks;
uint32_t i, count;
size_t len;

chunks = text ? cbor_string_chunks_handle(item) : cbor_bytestring_chunks_handle(item);
count = (uint32_t)(text ? cbor_string_chunk_count(item) : cbor_bytestring_chunk_count(item));

/* total length */
for (i = 0; i < count; ++i) {
*str_len += text ? cbor_string_length(chunks[i]) : cbor_bytestring_length(chunks[i]);
}

*str_val = malloc(*str_len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);

/* copy the chunks */
len = 0;
for (i = 0; i < count; ++i) {
if (text) {
memcpy(*str_val + len, cbor_string_handle(chunks[i]), cbor_string_length(chunks[i]));
len += cbor_string_length(chunks[i]);
} else {
memcpy(*str_val + len, cbor_bytestring_handle(chunks[i]), cbor_bytestring_length(chunks[i]));
len += cbor_bytestring_length(chunks[i]);
}
}
(*str_val)[len] = '\0';

return LY_SUCCESS;
}

/**
* @brief Convert a CBOR item to a string representation.
*
Expand Down Expand Up @@ -429,19 +473,27 @@ lydcbor_item_to_string(const struct ly_ctx *ctx, const cbor_item_t *item, char *
break;
}
case CBOR_TYPE_BYTESTRING:
*str_len = cbor_bytestring_length(item);
*str_val = malloc(*str_len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);
memcpy(*str_val, cbor_bytestring_handle(item), *str_len);
(*str_val)[*str_len] = '\0';
if (cbor_bytestring_is_indefinite(item)) {
LY_CHECK_RET(lydcbor_chunks_to_string(ctx, item, str_val, str_len));
} else {
*str_len = cbor_bytestring_length(item);
*str_val = malloc(*str_len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);
memcpy(*str_val, cbor_bytestring_handle(item), *str_len);
(*str_val)[*str_len] = '\0';
}
LY_CHECK_ERR_RET(ret = lydcbor_check_string(ctx, *str_val, *str_len), free(*str_val); *str_val = NULL, ret);
break;
case CBOR_TYPE_STRING:
*str_len = cbor_string_length(item);
*str_val = malloc(*str_len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);
memcpy(*str_val, cbor_string_handle(item), *str_len);
(*str_val)[*str_len] = '\0';
if (cbor_string_is_indefinite(item)) {
LY_CHECK_RET(lydcbor_chunks_to_string(ctx, item, str_val, str_len));
} else {
*str_len = cbor_string_length(item);
*str_val = malloc(*str_len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);
memcpy(*str_val, cbor_string_handle(item), *str_len);
(*str_val)[*str_len] = '\0';
}
LY_CHECK_ERR_RET(ret = lydcbor_check_string(ctx, *str_val, *str_len), free(*str_val); *str_val = NULL, ret);
break;
case CBOR_TYPE_FLOAT_CTRL:
Expand Down Expand Up @@ -1429,6 +1481,12 @@ lydcbor_subtree_r(struct lyd_cbor_ctx *lydctx, struct lyd_node *parent, struct l
/* process the node name */
lydcbor_parse_name(key_str, key_len, &name, &name_len, &prefix, &prefix_len, &is_meta);

if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && !is_meta && !prefix_len &&
!ly_strncmp("eventTime", name, name_len)) {
/* already parsed into the envelope by lyd_parse_cbor_restconf() */
continue;
}

if (!is_meta || name_len || prefix_len) {
/* get the schema node */
r = lydcbor_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
Expand Down Expand Up @@ -1679,3 +1737,118 @@ lyd_parse_cbor(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, st
}
return rc;
}

LY_ERR
lyd_parse_cbor_restconf(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
struct lyd_node **first_p, struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyd_cbor_ctx *lydctx = NULL;
const cbor_item_t *root, *notif, *event_time = NULL;
struct cbor_pair *pairs;
struct lyd_node *node;
char *str = NULL;
size_t str_len;
uint32_t i;

assert(ctx && in && first_p && envp && lydctx_p);

*envp = NULL;

rc = lyd_parse_cbor_init(ctx, in, parse_opts, val_opts, &lydctx);
LY_CHECK_GOTO(rc, cleanup);
lydctx->int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;

/* parse "notification" */
root = lydctx->cborctx->cbor_data;
pairs = cbor_map_handle(root);
if ((cbor_map_size(root) != 1) || !cbor_isa_string(pairs[0].key)) {
LOGVAL(ctx, NULL, LYVE_DATA, "Expected a single CBOR map member \"ietf-restconf:notification\".");
rc = LY_EVALID;
goto cleanup;
}
LY_CHECK_GOTO(rc = lydcbor_item_to_string(ctx, pairs[0].key, &str, &str_len), cleanup);
if (ly_strncmp("ietf-restconf:notification", str, str_len)) {
LOGVAL(ctx, NULL, LYVE_DATA, "Unexpected object \"%.*s\" instead of \"ietf-restconf:notification\".",
(int)str_len, str);
rc = LY_EVALID;
goto cleanup;
} else if (!cbor_isa_map(pairs[0].value)) {
LOGVAL(ctx, NULL, LYVE_SYNTAX, "Expecting CBOR %s but %s found.", lycbor_token2str(CBOR_TYPE_MAP),
lycbor_token2str(cbor_typeof(pairs[0].value)));
rc = LY_EVALID;
goto cleanup;
}
notif = pairs[0].value;

rc = lyd_create_opaq(ctx, "notification", 12, "ietf-restconf", 13, "ietf-restconf", 13, NULL, 0, NULL,
LY_VALUE_CBOR, NULL, LYD_VALHINT_STRING, envp);
LY_CHECK_GOTO(rc, cleanup);

/* find eventTime, it can be anywhere in the map */
pairs = cbor_map_handle(notif);
for (i = 0; i < cbor_map_size(notif); ++i) {
if (!cbor_isa_string(pairs[i].key)) {
continue;
}

free(str);
LY_CHECK_GOTO(rc = lydcbor_item_to_string(ctx, pairs[i].key, &str, &str_len), cleanup);
if (ly_strncmp("eventTime", str, str_len)) {
continue;
}

if (event_time) {
LOGVAL(ctx, NULL, LYVE_DATA, "Duplicate notification \"eventTime\" node.");
rc = LY_EVALID;
goto cleanup;
}
event_time = pairs[i].value;
}
if (!event_time) {
LOGVAL(ctx, NULL, LYVE_DATA, "Missing notification \"eventTime\" node.");
rc = LY_EVALID;
goto cleanup;
} else if (!cbor_isa_string(event_time)) {
LOGVAL(ctx, NULL, LYVE_SYNTAX, "Expecting CBOR %s but %s found.", lycbor_token2str(CBOR_TYPE_STRING),
lycbor_token2str(cbor_typeof(event_time)));
rc = LY_EVALID;
goto cleanup;
}

/* parse eventTime into the envelope */
free(str);
LY_CHECK_GOTO(rc = lydcbor_item_to_string(ctx, event_time, &str, &str_len), cleanup);
rc = lyd_create_opaq(ctx, "eventTime", 9, NULL, 0, NULL, 0, str, str_len, NULL, LY_VALUE_CBOR, NULL,
LYD_VALHINT_STRING, &node);
LY_CHECK_GOTO(rc, cleanup);
lyd_insert_child(*envp, node);
LY_CHECK_GOTO(rc = lyd_parser_notif_eventtime_validate(node), cleanup);

/* parse the notification */
r = lydcbor_subtree_r(lydctx, NULL, first_p, parsed, notif);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);

if (!lydctx->op_node) {
LOGVAL(ctx, NULL, LYVE_DATA, "Missing the operation node.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}

/* finish linking metadata */
r = lydcbor_metadata_finish(lydctx, first_p);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);

cleanup:
free(str);
if (rc) {
lyd_cbor_ctx_free((struct lyd_ctx *)lydctx);
} else {
*lydctx_p = (struct lyd_ctx *)lydctx;

/* the CBOR context is no more needed */
lycbor_ctx_free(lydctx->cborctx);
lydctx->cborctx = NULL;
}
return rc;
}
7 changes: 4 additions & 3 deletions src/parser_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ enum lyd_type {

LYD_TYPE_RPC_RESTCONF, /**< message-body of a RESTCONF operation input parameters
([ref](https://www.rfc-editor.org/rfc/rfc8040.html#section-3.6.1)) */
LYD_TYPE_NOTIF_RESTCONF, /**< RESTCONF JSON notification data
LYD_TYPE_NOTIF_RESTCONF, /**< RESTCONF JSON or CBOR notification data
([ref](https://www.rfc-editor.org/rfc/rfc8040.html#section-6.4)), to parse
a notification in XML, use ::LYD_TYPE_NOTIF_NETCONF */
LYD_TYPE_REPLY_RESTCONF /**< message-body of a RESTCONF operation output parameters
Expand Down Expand Up @@ -413,8 +413,9 @@ enum lyd_type {
*
* - ::LYD_TYPE_NOTIF_RESTCONF:
* - @p parent - must be NULL, the whole notification is expected;
* - @p format - must be ::LYD_JSON, XML-formatted notifications are parsed using ::LYD_TYPE_NOTIF_NETCONF;
* - @p tree - must be provided, all the RESTCONF-specific JSON objects will be returned here as
* - @p format - can be both ::LYD_JSON and ::LYD_CBOR, XML-formatted notifications are parsed using
* ::LYD_TYPE_NOTIF_NETCONF;
* - @p tree - must be provided, all the RESTCONF-specific JSON/CBOR objects will be returned here as
* a separate opaque data tree, even if the function fails, this may be returned;
* - @p op - must be provided, the notification data tree itself will be returned here, pointing to the operation;
*
Expand Down
16 changes: 16 additions & 0 deletions src/parser_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,22 @@ LY_ERR lyd_parse_cbor(const struct ly_ctx *ctx, const struct lysc_ext_instance *
struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p);

/**
* @brief Parse CBOR data as a RESTCONF notification.
*
* @param[in] ctx libyang context.
* @param[in] in Input structure.
* @param[in] parse_opts Options for parser, see @ref dataparseroptions.
* @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
* @param[in,out] first_p Pointer to the first top-level parsed node.
* @param[out] envp Parsed envelope with eventTime, may be returned possibly even on an error.
* @param[out] parsed Set to add all the parsed siblings into.
* @param[out] lydctx_p Data parser context to finish validation.
* @return LY_ERR value.
*/
LY_ERR lyd_parse_cbor_restconf(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
struct lyd_node **first_p, struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p);

/**
* @brief Validate eventTime date-and-time value.
*
Expand Down
4 changes: 2 additions & 2 deletions src/printer_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, u
case LYD_LYB:
ret = lyb_print_data(out, root, options);
break;
#ifdef ENABLE_CBOR_SUPPORT
#ifdef LY_HAVE_CBOR
case LYD_CBOR:
ret = cbor_print_data(out, root, options);
break;
Expand All @@ -47,7 +47,7 @@ lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, u
LOGERR(root ? LYD_CTX(root) : NULL, LY_ENOT, "CBOR format not supported, libcbor not found.");
ret = LY_ENOT;
break;
#endif /* ENABLE_CBOR_SUPPORT */
#endif /* LY_HAVE_CBOR */
case LYD_UNKNOWN:
LOGERR(root ? LYD_CTX(root) : NULL, LY_EINVAL, "Unknown data output format.");
ret = LY_EINVAL;
Expand Down
23 changes: 16 additions & 7 deletions src/tree_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ lyd_parse(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **f
case LYD_LYB:
r = lyd_parse_lyb(ctx, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed, &lydctx);
break;
#ifdef ENABLE_CBOR_SUPPORT
#ifdef LY_HAVE_CBOR
case LYD_CBOR:
r = lyd_parse_cbor(ctx, NULL, parent, first_p, in, parse_opts, val_opts, int_opts, &parsed, NULL, &lydctx);
break;
Expand All @@ -143,7 +143,7 @@ lyd_parse(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **f
LOGARG(ctx, format);
r = LY_EINVAL;
break;
#endif /* ENABLE_CBOR_SUPPORT */
#endif /* LY_HAVE_CBOR */
case LYD_UNKNOWN:
LOGARG(ctx, format);
r = LY_EINVAL;
Expand Down Expand Up @@ -422,11 +422,12 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in
break;
case LYD_TYPE_RPC_RESTCONF:
case LYD_TYPE_REPLY_RESTCONF:
LY_CHECK_ARG_RET(ctx, parent, parent->schema, parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
LY_CHECK_ARG_RET(ctx, (format == LYD_XML) || (format == LYD_JSON), parent, parent->schema,
parent->schema->nodetype & (LYS_RPC | LYS_ACTION), tree, !op, LY_EINVAL);
proto_msg = 1;
break;
case LYD_TYPE_NOTIF_RESTCONF:
LY_CHECK_ARG_RET(ctx, format == LYD_JSON, !parent, tree, op, LY_EINVAL);
LY_CHECK_ARG_RET(ctx, (format == LYD_JSON) || (format == LYD_CBOR), !parent, tree, op, LY_EINVAL);
proto_msg = 1;
break;

Expand All @@ -451,9 +452,17 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in
if (format == LYD_XML) {
/* parse the NETCONF (or RESTCONF XML) message */
rc = lyd_parse_xml_netconf(ctx, parent, &first, in, parse_options, val_opts, data_type, &envp, &parsed, &lydctx);
} else {
} else if (format == LYD_JSON) {
/* parse the RESTCONF message */
rc = lyd_parse_json_restconf(ctx, parent, &first, in, parse_options, val_opts, data_type, &envp, &parsed, &lydctx);
} else {
#ifdef LY_HAVE_CBOR
/* parse the RESTCONF CBOR notification */
rc = lyd_parse_cbor_restconf(ctx, in, parse_options, val_opts, &first, &envp, &parsed, &lydctx);
#else
LOGARG(ctx, format);
rc = LY_EINVAL;
#endif /* LY_HAVE_CBOR */
}
if (rc) {
if (envp) {
Expand Down Expand Up @@ -487,7 +496,7 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in
case LYD_LYB:
rc = lyd_parse_lyb(ctx, parent, &first, in, parse_options, val_opts, int_opts, &parsed, &lydctx);
break;
#ifdef ENABLE_CBOR_SUPPORT
#ifdef LY_HAVE_CBOR
case LYD_CBOR:
rc = lyd_parse_cbor(ctx, NULL, parent, &first, in, parse_options, val_opts, int_opts, &parsed, NULL, &lydctx);
break;
Expand All @@ -496,7 +505,7 @@ lyd_parse_op(const struct ly_ctx *ctx, struct lyd_node *parent, struct ly_in *in
LOGARG(ctx, format);
rc = LY_EINVAL;
break;
#endif /* ENABLE_CBOR_SUPPORT */
#endif /* LY_HAVE_CBOR */
case LYD_UNKNOWN:
LOGARG(ctx, format);
rc = LY_EINVAL;
Expand Down
2 changes: 1 addition & 1 deletion src/tree_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ typedef enum {
LYD_XML, /**< XML instance data format */
LYD_JSON, /**< JSON instance data format */
LYD_LYB, /**< LYB instance data format */
LYD_CBOR /**< CBOR instance data format */
LYD_CBOR /**< CBOR instance data format, available only if LY_HAVE_CBOR is defined */
} LYD_FORMAT;

/** @} */
Expand Down
Loading
Loading