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