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