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