Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file messages_server.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 - server NETCONF messages functions |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 7 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 14 | */ |
| 15 | |
Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 16 | #define _GNU_SOURCE /* pthread_rwlock_t, strdup */ |
| 17 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 19 | #include <inttypes.h> |
| 20 | #include <stdarg.h> |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 23 | |
| 24 | #include <libyang/libyang.h> |
| 25 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 26 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 27 | #include "config.h" |
| 28 | #include "log_p.h" |
| 29 | #include "messages_p.h" |
| 30 | #include "messages_server.h" |
| 31 | #include "netconf.h" |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 32 | |
| 33 | extern struct nc_server_opts server_opts; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 34 | |
| 35 | API struct nc_server_reply * |
| 36 | nc_server_reply_ok(void) |
| 37 | { |
| 38 | struct nc_server_reply *ret; |
| 39 | |
| 40 | ret = malloc(sizeof *ret); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 41 | NC_CHECK_ERRMEM_RET(!ret, NULL); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 42 | |
| 43 | ret->type = NC_RPL_OK; |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | API struct nc_server_reply * |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 48 | nc_server_reply_data(struct lyd_node *data, NC_WD_MODE wd, NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 49 | { |
| 50 | struct nc_server_reply_data *ret; |
| 51 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 52 | NC_CHECK_ARG_RET(NULL, data, NULL); |
| 53 | |
| 54 | if (!(data->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 55 | ERR(NULL, "nc_server_reply_data bad data"); |
Michal Vasko | b08743b | 2016-04-13 14:23:49 +0200 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
| 58 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 59 | ret = malloc(sizeof *ret); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 60 | NC_CHECK_ERRMEM_RET(!ret, NULL); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 61 | |
| 62 | ret->type = NC_RPL_DATA; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 63 | ret->wd = wd; |
Michal Vasko | b08743b | 2016-04-13 14:23:49 +0200 | [diff] [blame] | 64 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 65 | if (lyd_dup_single(data, NULL, LYD_DUP_RECURSIVE, &ret->data)) { |
| 66 | free(ret); |
| 67 | return NULL; |
| 68 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 69 | } else { |
| 70 | ret->data = data; |
| 71 | } |
| 72 | if (paramtype != NC_PARAMTYPE_CONST) { |
| 73 | ret->free = 1; |
| 74 | } else { |
| 75 | ret->free = 0; |
| 76 | } |
| 77 | return (struct nc_server_reply *)ret; |
| 78 | } |
| 79 | |
| 80 | API struct nc_server_reply * |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 81 | nc_server_reply_err(struct lyd_node *err) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 82 | { |
| 83 | struct nc_server_reply_error *ret; |
| 84 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 85 | NC_CHECK_ARG_RET(NULL, err, NULL); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 86 | |
| 87 | ret = malloc(sizeof *ret); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 88 | NC_CHECK_ERRMEM_RET(!ret, NULL); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 89 | |
| 90 | ret->type = NC_RPL_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 91 | ret->err = err; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 92 | return (struct nc_server_reply *)ret; |
| 93 | } |
| 94 | |
| 95 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 96 | nc_server_reply_add_err(struct nc_server_reply *reply, struct lyd_node *err) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 97 | { |
| 98 | struct nc_server_reply_error *err_rpl; |
| 99 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 100 | NC_CHECK_ARG_RET(NULL, reply, err, -1); |
| 101 | |
| 102 | if (reply->type != NC_RPL_ERROR) { |
roman | 3adc7c0 | 2023-10-25 11:10:58 +0200 | [diff] [blame] | 103 | ERR(NULL, "nc_server_reply_add_err() bad reply type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | err_rpl = (struct nc_server_reply_error *)reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 108 | lyd_insert_sibling(err_rpl->err, err, &err_rpl->err); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 112 | API const struct lyd_node * |
Michal Vasko | aa2e5a7 | 2017-03-16 09:48:44 +0100 | [diff] [blame] | 113 | nc_server_reply_get_last_err(const struct nc_server_reply *reply) |
| 114 | { |
| 115 | struct nc_server_reply_error *err_rpl; |
| 116 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 117 | NC_CHECK_ARG_RET(NULL, reply, NULL); |
| 118 | |
| 119 | if (reply->type != NC_RPL_ERROR) { |
roman | 3adc7c0 | 2023-10-25 11:10:58 +0200 | [diff] [blame] | 120 | ERR(NULL, "nc_server_reply_get_last_err() bad reply type"); |
Michal Vasko | aa2e5a7 | 2017-03-16 09:48:44 +0100 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | err_rpl = (struct nc_server_reply_error *)reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 125 | if (!err_rpl->err) { |
Michal Vasko | aa2e5a7 | 2017-03-16 09:48:44 +0100 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 128 | return err_rpl->err->prev; |
Michal Vasko | aa2e5a7 | 2017-03-16 09:48:44 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 131 | static const char * |
| 132 | nc_err_tag2str(NC_ERR tag) |
| 133 | { |
| 134 | switch (tag) { |
| 135 | case NC_ERR_IN_USE: |
| 136 | return "in-use"; |
| 137 | case NC_ERR_INVALID_VALUE: |
| 138 | return "invalid-value"; |
| 139 | case NC_ERR_ACCESS_DENIED: |
| 140 | return "access-denied"; |
| 141 | case NC_ERR_ROLLBACK_FAILED: |
| 142 | return "rollback-failed"; |
| 143 | case NC_ERR_OP_NOT_SUPPORTED: |
| 144 | return "operation-not-supported"; |
| 145 | case NC_ERR_TOO_BIG: |
| 146 | return "too-big"; |
| 147 | case NC_ERR_RES_DENIED: |
| 148 | return "resource-denied"; |
| 149 | case NC_ERR_MISSING_ATTR: |
| 150 | return "missing-attribute"; |
| 151 | case NC_ERR_BAD_ATTR: |
| 152 | return "bad-attribute"; |
| 153 | case NC_ERR_UNKNOWN_ATTR: |
| 154 | return "unknown-attribute"; |
| 155 | case NC_ERR_MISSING_ELEM: |
| 156 | return "missing-element"; |
| 157 | case NC_ERR_BAD_ELEM: |
| 158 | return "bad-element"; |
| 159 | case NC_ERR_UNKNOWN_ELEM: |
| 160 | return "unknown-element"; |
| 161 | case NC_ERR_UNKNOWN_NS: |
| 162 | return "unknown-namespace"; |
| 163 | case NC_ERR_LOCK_DENIED: |
| 164 | return "lock-denied"; |
| 165 | case NC_ERR_DATA_EXISTS: |
| 166 | return "data-exists"; |
| 167 | case NC_ERR_DATA_MISSING: |
| 168 | return "data-missing"; |
| 169 | case NC_ERR_OP_FAILED: |
| 170 | return "operation-failed"; |
| 171 | case NC_ERR_MALFORMED_MSG: |
| 172 | return "malformed-message"; |
| 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | static NC_ERR |
| 181 | nc_err_str2tag(const char *str) |
| 182 | { |
| 183 | if (!strcmp(str, "in-use")) { |
| 184 | return NC_ERR_IN_USE; |
| 185 | } else if (!strcmp(str, "invalid-value")) { |
| 186 | return NC_ERR_INVALID_VALUE; |
| 187 | } else if (!strcmp(str, "access-denied")) { |
| 188 | return NC_ERR_ACCESS_DENIED; |
| 189 | } else if (!strcmp(str, "rollback-failed")) { |
| 190 | return NC_ERR_ROLLBACK_FAILED; |
| 191 | } else if (!strcmp(str, "operation-not-supported")) { |
| 192 | return NC_ERR_OP_NOT_SUPPORTED; |
| 193 | } else if (!strcmp(str, "too-big")) { |
| 194 | return NC_ERR_TOO_BIG; |
| 195 | } else if (!strcmp(str, "resource-denied")) { |
| 196 | return NC_ERR_RES_DENIED; |
| 197 | } else if (!strcmp(str, "missing-attribute")) { |
| 198 | return NC_ERR_MISSING_ATTR; |
| 199 | } else if (!strcmp(str, "bad-attribute")) { |
| 200 | return NC_ERR_BAD_ATTR; |
| 201 | } else if (!strcmp(str, "unknown-attribute")) { |
| 202 | return NC_ERR_UNKNOWN_ATTR; |
| 203 | } else if (!strcmp(str, "missing-element")) { |
| 204 | return NC_ERR_MISSING_ELEM; |
| 205 | } else if (!strcmp(str, "bad-element")) { |
| 206 | return NC_ERR_BAD_ELEM; |
| 207 | } else if (!strcmp(str, "unknown-element")) { |
| 208 | return NC_ERR_UNKNOWN_ELEM; |
| 209 | } else if (!strcmp(str, "unknown-namespace")) { |
| 210 | return NC_ERR_UNKNOWN_NS; |
| 211 | } else if (!strcmp(str, "lock-denied")) { |
| 212 | return NC_ERR_LOCK_DENIED; |
| 213 | } else if (!strcmp(str, "data-exists")) { |
| 214 | return NC_ERR_DATA_EXISTS; |
| 215 | } else if (!strcmp(str, "data-missing")) { |
| 216 | return NC_ERR_DATA_MISSING; |
| 217 | } else if (!strcmp(str, "operation-failed")) { |
| 218 | return NC_ERR_OP_FAILED; |
| 219 | } else if (!strcmp(str, "malformed-message")) { |
| 220 | return NC_ERR_MALFORMED_MSG; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static const char * |
| 227 | nc_err_type2str(NC_ERR_TYPE type) |
| 228 | { |
| 229 | switch (type) { |
| 230 | case NC_ERR_TYPE_TRAN: |
| 231 | return "transport"; |
| 232 | case NC_ERR_TYPE_RPC: |
| 233 | return "rpc"; |
| 234 | case NC_ERR_TYPE_PROT: |
| 235 | return "protocol"; |
| 236 | case NC_ERR_TYPE_APP: |
| 237 | return "application"; |
| 238 | default: |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static NC_ERR_TYPE |
| 246 | nc_err_str2type(const char *str) |
| 247 | { |
| 248 | if (!strcmp(str, "transport")) { |
| 249 | return NC_ERR_TYPE_TRAN; |
| 250 | } else if (!strcmp(str, "rpc")) { |
| 251 | return NC_ERR_TYPE_RPC; |
| 252 | } else if (!strcmp(str, "protocol")) { |
| 253 | return NC_ERR_TYPE_PROT; |
| 254 | } else if (!strcmp(str, "application")) { |
| 255 | return NC_ERR_TYPE_APP; |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | API struct lyd_node * |
| 262 | nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 263 | { |
| 264 | va_list ap; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 265 | struct lyd_node *err = NULL; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 266 | NC_ERR_TYPE type; |
| 267 | const char *arg1, *arg2; |
| 268 | uint32_t sid; |
| 269 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 270 | NC_CHECK_ARG_RET(NULL, tag, NULL); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 271 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 272 | /* rpc-error */ |
| 273 | if (lyd_new_opaq2(NULL, ctx, "rpc-error", NULL, NULL, NC_NS_BASE, &err)) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | va_start(ap, tag); |
| 278 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 279 | /* error-type */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 280 | switch (tag) { |
| 281 | case NC_ERR_IN_USE: |
| 282 | case NC_ERR_INVALID_VALUE: |
| 283 | case NC_ERR_ACCESS_DENIED: |
| 284 | case NC_ERR_ROLLBACK_FAILED: |
| 285 | case NC_ERR_OP_NOT_SUPPORTED: |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 286 | type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */ |
Michal Vasko | 2f923d1 | 2016-04-05 11:33:02 +0200 | [diff] [blame] | 287 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 288 | ERRARG(NULL, "type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 289 | goto fail; |
| 290 | } |
| 291 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 292 | case NC_ERR_TOO_BIG: |
| 293 | case NC_ERR_RES_DENIED: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 294 | type = (NC_ERR_TYPE)va_arg(ap, int); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 295 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 296 | case NC_ERR_MISSING_ATTR: |
| 297 | case NC_ERR_BAD_ATTR: |
| 298 | case NC_ERR_UNKNOWN_ATTR: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 299 | type = (NC_ERR_TYPE)va_arg(ap, int); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 300 | if (type == NC_ERR_TYPE_TRAN) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 301 | ERRARG(NULL, "type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 302 | goto fail; |
| 303 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 304 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 305 | case NC_ERR_MISSING_ELEM: |
| 306 | case NC_ERR_BAD_ELEM: |
| 307 | case NC_ERR_UNKNOWN_ELEM: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 308 | type = (NC_ERR_TYPE)va_arg(ap, int); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 309 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 310 | ERRARG(NULL, "type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 311 | goto fail; |
| 312 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 313 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 314 | case NC_ERR_UNKNOWN_NS: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 315 | type = (NC_ERR_TYPE)va_arg(ap, int); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 316 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 317 | ERRARG(NULL, "type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 318 | goto fail; |
| 319 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 320 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 321 | case NC_ERR_LOCK_DENIED: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 322 | type = NC_ERR_TYPE_PROT; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 323 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 324 | case NC_ERR_DATA_EXISTS: |
| 325 | case NC_ERR_DATA_MISSING: |
| 326 | type = NC_ERR_TYPE_APP; |
| 327 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 328 | case NC_ERR_OP_FAILED: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 329 | type = (NC_ERR_TYPE)va_arg(ap, int); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 330 | if (type == NC_ERR_TYPE_TRAN) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 331 | ERRARG(NULL, "type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 332 | goto fail; |
| 333 | } |
| 334 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 335 | case NC_ERR_MALFORMED_MSG: |
| 336 | type = NC_ERR_TYPE_RPC; |
| 337 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 338 | default: |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 339 | ERRARG(NULL, "tag"); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 340 | goto fail; |
| 341 | } |
| 342 | if (lyd_new_opaq2(err, NULL, "error-type", nc_err_type2str(type), NULL, NC_NS_BASE, NULL)) { |
| 343 | goto fail; |
| 344 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 345 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 346 | /* error-tag */ |
| 347 | if (lyd_new_opaq2(err, NULL, "error-tag", nc_err_tag2str(tag), NULL, NC_NS_BASE, NULL)) { |
| 348 | goto fail; |
| 349 | } |
| 350 | |
| 351 | /* error-severity */ |
| 352 | if (lyd_new_opaq2(err, NULL, "error-severity", "error", NULL, NC_NS_BASE, NULL)) { |
| 353 | goto fail; |
| 354 | } |
| 355 | |
| 356 | /* error-message */ |
| 357 | switch (tag) { |
| 358 | case NC_ERR_IN_USE: |
| 359 | nc_err_set_msg(err, "The request requires a resource that already is in use.", "en"); |
| 360 | break; |
| 361 | case NC_ERR_INVALID_VALUE: |
| 362 | nc_err_set_msg(err, "The request specifies an unacceptable value for one or more parameters.", "en"); |
| 363 | break; |
| 364 | case NC_ERR_TOO_BIG: |
| 365 | nc_err_set_msg(err, "The request or response (that would be generated) is too large for the implementation to handle.", "en"); |
| 366 | break; |
| 367 | case NC_ERR_MISSING_ATTR: |
| 368 | nc_err_set_msg(err, "An expected attribute is missing.", "en"); |
| 369 | break; |
| 370 | case NC_ERR_BAD_ATTR: |
| 371 | nc_err_set_msg(err, "An attribute value is not correct.", "en"); |
| 372 | break; |
| 373 | case NC_ERR_UNKNOWN_ATTR: |
| 374 | nc_err_set_msg(err, "An unexpected attribute is present.", "en"); |
| 375 | break; |
| 376 | case NC_ERR_MISSING_ELEM: |
| 377 | nc_err_set_msg(err, "An expected element is missing.", "en"); |
| 378 | break; |
| 379 | case NC_ERR_BAD_ELEM: |
| 380 | nc_err_set_msg(err, "An element value is not correct.", "en"); |
| 381 | break; |
| 382 | case NC_ERR_UNKNOWN_ELEM: |
| 383 | nc_err_set_msg(err, "An unexpected element is present.", "en"); |
| 384 | break; |
| 385 | case NC_ERR_UNKNOWN_NS: |
| 386 | nc_err_set_msg(err, "An unexpected namespace is present.", "en"); |
| 387 | break; |
| 388 | case NC_ERR_ACCESS_DENIED: |
| 389 | nc_err_set_msg(err, "Access to the requested protocol operation or data model is denied because authorization failed.", "en"); |
| 390 | break; |
| 391 | case NC_ERR_LOCK_DENIED: |
| 392 | nc_err_set_msg(err, "Access to the requested lock is denied because the lock is currently held by another entity.", "en"); |
| 393 | break; |
| 394 | case NC_ERR_RES_DENIED: |
| 395 | nc_err_set_msg(err, "Request could not be completed because of insufficient resources.", "en"); |
| 396 | break; |
| 397 | case NC_ERR_ROLLBACK_FAILED: |
| 398 | nc_err_set_msg(err, "Request to roll back some configuration change was not completed for some reason.", "en"); |
| 399 | break; |
| 400 | case NC_ERR_DATA_EXISTS: |
| 401 | nc_err_set_msg(err, "Request could not be completed because the relevant data model content already exists.", "en"); |
| 402 | break; |
| 403 | case NC_ERR_DATA_MISSING: |
| 404 | nc_err_set_msg(err, "Request could not be completed because the relevant data model content does not exist.", "en"); |
| 405 | break; |
| 406 | case NC_ERR_OP_NOT_SUPPORTED: |
| 407 | nc_err_set_msg(err, "Request could not be completed because the requested operation is not supported by this implementation.", "en"); |
| 408 | break; |
| 409 | case NC_ERR_OP_FAILED: |
| 410 | nc_err_set_msg(err, "Request could not be completed because the requested operation failed for a non-specific reason.", "en"); |
| 411 | break; |
| 412 | case NC_ERR_MALFORMED_MSG: |
| 413 | nc_err_set_msg(err, "A message could not be handled because it failed to be parsed correctly.", "en"); |
| 414 | break; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 415 | default: |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 416 | ERRARG(NULL, "tag"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 417 | goto fail; |
| 418 | } |
| 419 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 420 | /* error-info */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 421 | switch (tag) { |
| 422 | case NC_ERR_IN_USE: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 423 | case NC_ERR_INVALID_VALUE: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 424 | case NC_ERR_ACCESS_DENIED: |
| 425 | case NC_ERR_ROLLBACK_FAILED: |
| 426 | case NC_ERR_OP_NOT_SUPPORTED: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 427 | case NC_ERR_TOO_BIG: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 428 | case NC_ERR_RES_DENIED: |
| 429 | case NC_ERR_DATA_EXISTS: |
| 430 | case NC_ERR_DATA_MISSING: |
| 431 | case NC_ERR_OP_FAILED: |
| 432 | case NC_ERR_MALFORMED_MSG: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 433 | break; |
| 434 | case NC_ERR_MISSING_ATTR: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 435 | case NC_ERR_BAD_ATTR: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 436 | case NC_ERR_UNKNOWN_ATTR: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 437 | arg1 = va_arg(ap, const char *); |
| 438 | arg2 = va_arg(ap, const char *); |
| 439 | |
| 440 | nc_err_add_bad_attr(err, arg1); |
| 441 | nc_err_add_bad_elem(err, arg2); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 442 | break; |
| 443 | case NC_ERR_MISSING_ELEM: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 444 | case NC_ERR_BAD_ELEM: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 445 | case NC_ERR_UNKNOWN_ELEM: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 446 | arg1 = va_arg(ap, const char *); |
| 447 | |
| 448 | nc_err_add_bad_elem(err, arg1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 449 | break; |
| 450 | case NC_ERR_UNKNOWN_NS: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 451 | arg1 = va_arg(ap, const char *); |
| 452 | arg2 = va_arg(ap, const char *); |
| 453 | |
| 454 | nc_err_add_bad_elem(err, arg1); |
| 455 | nc_err_add_bad_ns(err, arg2); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 456 | break; |
| 457 | case NC_ERR_LOCK_DENIED: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 458 | sid = va_arg(ap, uint32_t); |
| 459 | |
| 460 | nc_err_set_sid(err, sid); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 461 | break; |
| 462 | default: |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 463 | ERRARG(NULL, "tag"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 464 | goto fail; |
| 465 | } |
| 466 | |
| 467 | va_end(ap); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 468 | return err; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 469 | |
| 470 | fail: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 471 | va_end(ap); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 472 | lyd_free_siblings(err); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 473 | return NULL; |
| 474 | } |
| 475 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 476 | API NC_ERR_TYPE |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 477 | nc_err_get_type(const struct lyd_node *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 478 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 479 | struct lyd_node *match; |
| 480 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 481 | NC_CHECK_ARG_RET(NULL, err, 0); |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 482 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 483 | lyd_find_sibling_opaq_next(lyd_child(err), "error-type", &match); |
| 484 | if (match) { |
| 485 | return nc_err_str2type(((struct lyd_node_opaq *)match)->value); |
| 486 | } |
| 487 | |
| 488 | return 0; |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | API NC_ERR |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 492 | nc_err_get_tag(const struct lyd_node *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 493 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 494 | struct lyd_node *match; |
| 495 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 496 | NC_CHECK_ARG_RET(NULL, err, 0); |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 497 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 498 | lyd_find_sibling_opaq_next(lyd_child(err), "error-tag", &match); |
| 499 | if (match) { |
| 500 | return nc_err_str2tag(((struct lyd_node_opaq *)match)->value); |
| 501 | } |
| 502 | |
| 503 | return 0; |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 506 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 507 | nc_err_set_app_tag(struct lyd_node *err, const char *error_app_tag) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 508 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 509 | struct lyd_node *match; |
| 510 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 511 | NC_CHECK_ARG_RET(NULL, err, error_app_tag, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 512 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 513 | /* remove previous node */ |
| 514 | lyd_find_sibling_opaq_next(lyd_child(err), "error-app-tag", &match); |
| 515 | if (match) { |
| 516 | lyd_free_tree(match); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 517 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 518 | |
| 519 | if (lyd_new_opaq2(err, NULL, "error-app-tag", error_app_tag, NULL, NC_NS_BASE, NULL)) { |
| 520 | return -1; |
| 521 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 522 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 523 | return 0; |
| 524 | } |
| 525 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 526 | API const char * |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 527 | nc_err_get_app_tag(const struct lyd_node *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 528 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 529 | struct lyd_node *match; |
| 530 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 531 | NC_CHECK_ARG_RET(NULL, err, NULL); |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 532 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 533 | lyd_find_sibling_opaq_next(lyd_child(err), "error-app-tag", &match); |
| 534 | if (match) { |
| 535 | return ((struct lyd_node_opaq *)match)->value; |
| 536 | } |
| 537 | |
| 538 | return NULL; |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 539 | } |
| 540 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 541 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 542 | nc_err_set_path(struct lyd_node *err, const char *error_path) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 543 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 544 | struct lyd_node *match; |
| 545 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 546 | NC_CHECK_ARG_RET(NULL, err, error_path, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 547 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 548 | /* remove previous node */ |
| 549 | lyd_find_sibling_opaq_next(lyd_child(err), "error-path", &match); |
| 550 | if (match) { |
| 551 | lyd_free_tree(match); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 552 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 553 | |
| 554 | if (lyd_new_opaq2(err, NULL, "error-path", error_path, NULL, NC_NS_BASE, NULL)) { |
| 555 | return -1; |
| 556 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 557 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 561 | API const char * |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 562 | nc_err_get_path(const struct lyd_node *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 563 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 564 | struct lyd_node *match; |
| 565 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 566 | NC_CHECK_ARG_RET(NULL, err, NULL); |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 567 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 568 | lyd_find_sibling_opaq_next(lyd_child(err), "error-path", &match); |
| 569 | if (match) { |
| 570 | return ((struct lyd_node_opaq *)match)->value; |
| 571 | } |
| 572 | |
| 573 | return NULL; |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 576 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 577 | nc_err_set_msg(struct lyd_node *err, const char *error_message, const char *lang) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 578 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 579 | struct lyd_node *match; |
| 580 | struct lyd_attr *attr; |
| 581 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 582 | NC_CHECK_ARG_RET(NULL, err, error_message, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 583 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 584 | /* remove previous message */ |
| 585 | lyd_find_sibling_opaq_next(lyd_child(err), "error-message", &match); |
| 586 | if (match) { |
| 587 | lyd_free_tree(match); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 588 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 589 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 590 | if (lyd_new_opaq2(err, NULL, "error-message", error_message, NULL, NC_NS_BASE, &match)) { |
| 591 | return -1; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 592 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 593 | if (lang && lyd_new_attr(match, NULL, "xml:lang", lang, &attr)) { |
| 594 | lyd_free_tree(match); |
| 595 | return -1; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 596 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 597 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 601 | API const char * |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 602 | nc_err_get_msg(const struct lyd_node *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 603 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 604 | struct lyd_node *match; |
| 605 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 606 | NC_CHECK_ARG_RET(NULL, err, NULL); |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 607 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 608 | lyd_find_sibling_opaq_next(lyd_child(err), "error-message", &match); |
| 609 | if (match) { |
| 610 | return ((struct lyd_node_opaq *)match)->value; |
| 611 | } |
| 612 | |
| 613 | return NULL; |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 616 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 617 | nc_err_set_sid(struct lyd_node *err, uint32_t session_id) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 618 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 619 | struct lyd_node *match, *info; |
| 620 | char buf[22]; |
| 621 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 622 | NC_CHECK_ARG_RET(NULL, err, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 623 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 624 | /* find error-info */ |
| 625 | lyd_find_sibling_opaq_next(lyd_child(err), "error-info", &info); |
| 626 | if (!info && lyd_new_opaq2(err, NULL, "error-info", NULL, NULL, NC_NS_BASE, &info)) { |
| 627 | return -1; |
| 628 | } |
| 629 | |
| 630 | /* remove previous node */ |
| 631 | lyd_find_sibling_opaq_next(lyd_child(info), "session-id", &match); |
| 632 | if (match) { |
| 633 | lyd_free_tree(match); |
| 634 | } |
| 635 | |
| 636 | sprintf(buf, "%" PRIu32, session_id); |
| 637 | if (lyd_new_opaq2(info, NULL, "session-id", buf, NULL, NC_NS_BASE, NULL)) { |
| 638 | return -1; |
| 639 | } |
| 640 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 645 | nc_err_add_bad_attr(struct lyd_node *err, const char *attr_name) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 646 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 647 | struct lyd_node *info; |
| 648 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 649 | NC_CHECK_ARG_RET(NULL, err, attr_name, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 650 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 651 | /* find error-info */ |
| 652 | lyd_find_sibling_opaq_next(lyd_child(err), "error-info", &info); |
| 653 | if (!info && lyd_new_opaq2(err, NULL, "error-info", NULL, NULL, NC_NS_BASE, &info)) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 654 | return -1; |
| 655 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 656 | |
| 657 | if (lyd_new_opaq2(info, NULL, "bad-attribute", attr_name, NULL, NC_NS_BASE, NULL)) { |
| 658 | return -1; |
| 659 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 660 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 665 | nc_err_add_bad_elem(struct lyd_node *err, const char *elem_name) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 666 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 667 | struct lyd_node *info; |
| 668 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 669 | NC_CHECK_ARG_RET(NULL, err, elem_name, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 670 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 671 | /* find error-info */ |
| 672 | lyd_find_sibling_opaq_next(lyd_child(err), "error-info", &info); |
| 673 | if (!info && lyd_new_opaq2(err, NULL, "error-info", NULL, NULL, NC_NS_BASE, &info)) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 674 | return -1; |
| 675 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 676 | |
| 677 | if (lyd_new_opaq2(info, NULL, "bad-element", elem_name, NULL, NC_NS_BASE, NULL)) { |
| 678 | return -1; |
| 679 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 680 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 685 | nc_err_add_bad_ns(struct lyd_node *err, const char *ns_name) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 686 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 687 | struct lyd_node *info; |
| 688 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 689 | NC_CHECK_ARG_RET(NULL, err, ns_name, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 690 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 691 | /* find error-info */ |
| 692 | lyd_find_sibling_opaq_next(lyd_child(err), "error-info", &info); |
| 693 | if (!info && lyd_new_opaq2(err, NULL, "error-info", NULL, NULL, NC_NS_BASE, &info)) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 694 | return -1; |
| 695 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 696 | |
| 697 | if (lyd_new_opaq2(info, NULL, "bad-namespace", ns_name, NULL, NC_NS_BASE, NULL)) { |
| 698 | return -1; |
| 699 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 700 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | API int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 705 | nc_err_add_info_other(struct lyd_node *err, struct lyd_node *other) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 706 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 707 | struct lyd_node *info; |
| 708 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 709 | NC_CHECK_ARG_RET(NULL, err, other, -1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 710 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 711 | /* find error-info */ |
| 712 | lyd_find_sibling_opaq_next(lyd_child(err), "error-info", &info); |
| 713 | if (!info && lyd_new_opaq2(err, NULL, "error-info", NULL, NULL, NC_NS_BASE, &info)) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 714 | return -1; |
| 715 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 716 | |
| 717 | lyd_insert_child(info, other); |
| 718 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | void |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 723 | nc_server_rpc_free(struct nc_server_rpc *rpc) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 724 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 725 | if (!rpc) { |
| 726 | return; |
| 727 | } |
| 728 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 729 | lyd_free_tree(rpc->envp); |
Michal Vasko | d5bfc48 | 2021-10-26 10:47:54 +0200 | [diff] [blame] | 730 | |
| 731 | /* may be action */ |
| 732 | lyd_free_all(rpc->rpc); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 733 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 734 | free(rpc); |
| 735 | } |
| 736 | |
| 737 | API void |
| 738 | nc_server_reply_free(struct nc_server_reply *reply) |
| 739 | { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 740 | struct nc_server_reply_data *data_rpl; |
| 741 | struct nc_server_reply_error *error_rpl; |
| 742 | |
| 743 | if (!reply) { |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | switch (reply->type) { |
| 748 | case NC_RPL_DATA: |
| 749 | data_rpl = (struct nc_server_reply_data *)reply; |
| 750 | if (data_rpl->free) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 751 | lyd_free_siblings(data_rpl->data); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 752 | } |
| 753 | break; |
| 754 | case NC_RPL_OK: |
| 755 | /* nothing to free */ |
| 756 | break; |
| 757 | case NC_RPL_ERROR: |
| 758 | error_rpl = (struct nc_server_reply_error *)reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 759 | lyd_free_siblings(error_rpl->err); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 760 | break; |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | free(reply); |
| 765 | } |
| 766 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 767 | API struct nc_server_notif * |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 768 | nc_server_notif_new(struct lyd_node *event, char *eventtime, NC_PARAMTYPE paramtype) |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 769 | { |
| 770 | struct nc_server_notif *ntf; |
Michal Vasko | 0b89f3d | 2018-01-04 10:57:03 +0100 | [diff] [blame] | 771 | struct lyd_node *elem; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 772 | int found; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 773 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 774 | NC_CHECK_ARG_RET(NULL, event, eventtime, NULL); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 775 | |
Michal Vasko | 0b89f3d | 2018-01-04 10:57:03 +0100 | [diff] [blame] | 776 | /* check that there is a notification */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 777 | found = 0; |
| 778 | LYD_TREE_DFS_BEGIN(event, elem) { |
| 779 | if (elem->schema->nodetype == LYS_NOTIF) { |
| 780 | found = 1; |
Michal Vasko | 0b89f3d | 2018-01-04 10:57:03 +0100 | [diff] [blame] | 781 | break; |
Michal Vasko | 0b89f3d | 2018-01-04 10:57:03 +0100 | [diff] [blame] | 782 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 783 | LYD_TREE_DFS_END(event, elem); |
Michal Vasko | 0b89f3d | 2018-01-04 10:57:03 +0100 | [diff] [blame] | 784 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 785 | if (!found) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 786 | ERRARG(NULL, "event"); |
Michal Vasko | 0b89f3d | 2018-01-04 10:57:03 +0100 | [diff] [blame] | 787 | return NULL; |
| 788 | } |
| 789 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 790 | ntf = malloc(sizeof *ntf); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 791 | NC_CHECK_ERRMEM_RET(!ntf, NULL); |
Michal Vasko | 25e244b | 2022-12-14 14:19:59 +0100 | [diff] [blame] | 792 | |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 793 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 794 | ntf->eventtime = strdup(eventtime); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 795 | if (lyd_dup_single(event, NULL, LYD_DUP_RECURSIVE, &ntf->ntf)) { |
| 796 | free(ntf); |
| 797 | return NULL; |
| 798 | } |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 799 | } else { |
| 800 | ntf->eventtime = eventtime; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 801 | ntf->ntf = event; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 802 | } |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 803 | ntf->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 804 | |
| 805 | return ntf; |
| 806 | } |
| 807 | |
| 808 | API void |
| 809 | nc_server_notif_free(struct nc_server_notif *notif) |
| 810 | { |
| 811 | if (!notif) { |
| 812 | return; |
| 813 | } |
| 814 | |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 815 | if (notif->free) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 816 | lyd_free_tree(notif->ntf); |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 817 | free(notif->eventtime); |
| 818 | } |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 819 | free(notif); |
| 820 | } |
Michal Vasko | 9a2e4d2 | 2017-03-17 09:44:49 +0100 | [diff] [blame] | 821 | |
| 822 | API const char * |
| 823 | nc_server_notif_get_time(const struct nc_server_notif *notif) |
| 824 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 825 | NC_CHECK_ARG_RET(NULL, notif, NULL); |
Michal Vasko | 9a2e4d2 | 2017-03-17 09:44:49 +0100 | [diff] [blame] | 826 | |
| 827 | return notif->eventtime; |
| 828 | } |