Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file messages_server.c |
| 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 - server NETCONF messages functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <ctype.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <stdarg.h> |
| 19 | |
| 20 | #include <libyang/libyang.h> |
| 21 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 22 | #include "session_server.h" |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 23 | #include "libnetconf.h" |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 24 | |
| 25 | extern struct nc_server_opts server_opts; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 26 | |
| 27 | API struct nc_server_reply * |
| 28 | nc_server_reply_ok(void) |
| 29 | { |
| 30 | struct nc_server_reply *ret; |
| 31 | |
| 32 | ret = malloc(sizeof *ret); |
| 33 | if (!ret) { |
| 34 | ERRMEM; |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | ret->type = NC_RPL_OK; |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | API struct nc_server_reply * |
| 43 | nc_server_reply_data(struct lyd_node *data, NC_PARAMTYPE paramtype) |
| 44 | { |
| 45 | struct nc_server_reply_data *ret; |
| 46 | |
| 47 | if (!data) { |
| 48 | ERRARG; |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | ret = malloc(sizeof *ret); |
| 53 | if (!ret) { |
| 54 | ERRMEM; |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | ret->type = NC_RPL_DATA; |
| 59 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 60 | ret->data = lyd_dup(data, 1); |
| 61 | } else { |
| 62 | ret->data = data; |
| 63 | } |
| 64 | if (paramtype != NC_PARAMTYPE_CONST) { |
| 65 | ret->free = 1; |
| 66 | } else { |
| 67 | ret->free = 0; |
| 68 | } |
| 69 | return (struct nc_server_reply *)ret; |
| 70 | } |
| 71 | |
| 72 | API struct nc_server_reply * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 73 | nc_server_reply_err(struct nc_server_error *err) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 74 | { |
| 75 | struct nc_server_reply_error *ret; |
| 76 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 77 | if (!err) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 78 | ERRARG; |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | ret = malloc(sizeof *ret); |
| 83 | if (!ret) { |
| 84 | ERRMEM; |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | ret->type = NC_RPL_ERROR; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 89 | ret->err = malloc(sizeof *ret->err); |
| 90 | ret->err[0] = err; |
| 91 | ret->count = 1; |
| 92 | return (struct nc_server_reply *)ret; |
| 93 | } |
| 94 | |
| 95 | API int |
| 96 | nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err) |
| 97 | { |
| 98 | struct nc_server_reply_error *err_rpl; |
| 99 | |
| 100 | if (!reply || (reply->type != NC_RPL_ERROR) || !err) { |
| 101 | ERRARG; |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | err_rpl = (struct nc_server_reply_error *)reply; |
| 106 | ++err_rpl->count; |
| 107 | err_rpl->err = realloc(err_rpl->err, err_rpl->count * sizeof *err_rpl->err); |
| 108 | err_rpl->err[err_rpl->count - 1] = err; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | API struct nc_server_error * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 113 | nc_err(NC_ERR tag, ...) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 114 | { |
| 115 | va_list ap; |
| 116 | struct nc_server_error *ret; |
| 117 | NC_ERR_TYPE type; |
| 118 | const char *arg1, *arg2; |
| 119 | uint32_t sid; |
| 120 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 121 | if (!tag) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 122 | ERRARG; |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | ret = calloc(1, sizeof *ret); |
| 127 | if (!ret) { |
| 128 | ERRMEM; |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | va_start(ap, tag); |
| 133 | |
| 134 | switch (tag) { |
| 135 | case NC_ERR_IN_USE: |
| 136 | case NC_ERR_INVALID_VALUE: |
| 137 | case NC_ERR_ACCESS_DENIED: |
| 138 | case NC_ERR_ROLLBACK_FAILED: |
| 139 | case NC_ERR_OP_NOT_SUPPORTED: |
| 140 | type = va_arg(ap, NC_ERR_TYPE); |
| 141 | if ((type != NC_ERR_TYPE_PROT) && (type == NC_ERR_TYPE_APP)) { |
| 142 | goto fail; |
| 143 | } |
| 144 | break; |
| 145 | |
| 146 | case NC_ERR_TOO_BIG: |
| 147 | case NC_ERR_RES_DENIED: |
| 148 | type = va_arg(ap, NC_ERR_TYPE); |
| 149 | /* nothing to check */ |
| 150 | break; |
| 151 | |
| 152 | case NC_ERR_MISSING_ATTR: |
| 153 | case NC_ERR_BAD_ATTR: |
| 154 | case NC_ERR_UNKNOWN_ATTR: |
| 155 | type = va_arg(ap, NC_ERR_TYPE); |
| 156 | arg1 = va_arg(ap, const char *); |
| 157 | arg2 = va_arg(ap, const char *); |
| 158 | |
| 159 | if (type == NC_ERR_TYPE_TRAN) { |
| 160 | goto fail; |
| 161 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 162 | nc_err_add_bad_attr(ret, arg1); |
| 163 | nc_err_add_bad_elem(ret, arg2); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 164 | break; |
| 165 | |
| 166 | case NC_ERR_MISSING_ELEM: |
| 167 | case NC_ERR_BAD_ELEM: |
| 168 | case NC_ERR_UNKNOWN_ELEM: |
| 169 | type = va_arg(ap, NC_ERR_TYPE); |
| 170 | arg1 = va_arg(ap, const char *); |
| 171 | |
| 172 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
| 173 | goto fail; |
| 174 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 175 | nc_err_add_bad_elem(ret, arg1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 176 | break; |
| 177 | |
| 178 | case NC_ERR_UNKNOWN_NS: |
| 179 | type = va_arg(ap, NC_ERR_TYPE); |
| 180 | arg1 = va_arg(ap, const char *); |
| 181 | arg2 = va_arg(ap, const char *); |
| 182 | |
| 183 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
| 184 | goto fail; |
| 185 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 186 | nc_err_add_bad_elem(ret, arg1); |
| 187 | nc_err_add_bad_ns(ret, arg2); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 188 | break; |
| 189 | |
| 190 | case NC_ERR_LOCK_DENIED: |
| 191 | sid = va_arg(ap, uint32_t); |
| 192 | |
| 193 | type = NC_ERR_TYPE_PROT; |
| 194 | nc_err_set_sid(ret, sid); |
| 195 | break; |
| 196 | |
| 197 | case NC_ERR_DATA_EXISTS: |
| 198 | case NC_ERR_DATA_MISSING: |
| 199 | type = NC_ERR_TYPE_APP; |
| 200 | break; |
| 201 | |
| 202 | case NC_ERR_OP_FAILED: |
| 203 | type = va_arg(ap, NC_ERR_TYPE); |
| 204 | |
| 205 | if (type == NC_ERR_TYPE_TRAN) { |
| 206 | goto fail; |
| 207 | } |
| 208 | break; |
| 209 | |
| 210 | case NC_ERR_MALFORMED_MSG: |
| 211 | type = NC_ERR_TYPE_RPC; |
| 212 | break; |
| 213 | |
| 214 | default: |
| 215 | goto fail; |
| 216 | } |
| 217 | |
| 218 | switch (tag) { |
| 219 | case NC_ERR_IN_USE: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 220 | nc_err_set_msg(ret, "The request requires a resource that already is in use.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 221 | break; |
| 222 | case NC_ERR_INVALID_VALUE: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 223 | nc_err_set_msg(ret, "The request specifies an unacceptable value for one or more parameters.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 224 | break; |
| 225 | case NC_ERR_TOO_BIG: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 226 | nc_err_set_msg(ret, "The request or response (that would be generated) is too large for the implementation to handle.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 227 | break; |
| 228 | case NC_ERR_MISSING_ATTR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 229 | nc_err_set_msg(ret, "An expected attribute is missing.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 230 | break; |
| 231 | case NC_ERR_BAD_ATTR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 232 | nc_err_set_msg(ret, "An attribute value is not correct.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 233 | break; |
| 234 | case NC_ERR_UNKNOWN_ATTR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 235 | nc_err_set_msg(ret, "An unexpected attribute is present.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 236 | break; |
| 237 | case NC_ERR_MISSING_ELEM: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 238 | nc_err_set_msg(ret, "An expected element is missing.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 239 | break; |
| 240 | case NC_ERR_BAD_ELEM: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 241 | nc_err_set_msg(ret, "An element value is not correct.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 242 | break; |
| 243 | case NC_ERR_UNKNOWN_ELEM: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 244 | nc_err_set_msg(ret, "An unexpected element is present.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 245 | break; |
| 246 | case NC_ERR_UNKNOWN_NS: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 247 | nc_err_set_msg(ret, "An unexpected namespace is present.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 248 | break; |
| 249 | case NC_ERR_ACCESS_DENIED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 250 | nc_err_set_msg(ret, "Access to the requested protocol operation or data model is denied because authorization failed.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 251 | break; |
| 252 | case NC_ERR_LOCK_DENIED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 253 | nc_err_set_msg(ret, "Access to the requested lock is denied because the lock is currently held by another entity.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 254 | break; |
| 255 | case NC_ERR_RES_DENIED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 256 | nc_err_set_msg(ret, "Request could not be completed because of insufficient resources.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 257 | break; |
| 258 | case NC_ERR_ROLLBACK_FAILED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 259 | nc_err_set_msg(ret, "Request to roll back some configuration change was not completed for some reason.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 260 | break; |
| 261 | case NC_ERR_DATA_EXISTS: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 262 | nc_err_set_msg(ret, "Request could not be completed because the relevant data model content already exists.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 263 | break; |
| 264 | case NC_ERR_DATA_MISSING: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 265 | nc_err_set_msg(ret, "Request could not be completed because the relevant data model content does not exist.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 266 | break; |
| 267 | case NC_ERR_OP_NOT_SUPPORTED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 268 | nc_err_set_msg(ret, "Request could not be completed because the requested operation is not supported by this implementation.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 269 | break; |
| 270 | case NC_ERR_OP_FAILED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 271 | nc_err_set_msg(ret, "Request could not be completed because the requested operation failed for a non-specific reason.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 272 | break; |
| 273 | case NC_ERR_MALFORMED_MSG: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 274 | nc_err_set_msg(ret, "A message could not be handled because it failed to be parsed correctly.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 275 | break; |
| 276 | default: |
| 277 | goto fail; |
| 278 | } |
| 279 | |
| 280 | va_end(ap); |
| 281 | |
| 282 | ret->type = type; |
| 283 | ret->tag = tag; |
| 284 | return ret; |
| 285 | |
| 286 | fail: |
| 287 | ERRARG; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 288 | va_end(ap); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 289 | free(ret); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 294 | nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 295 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 296 | if (!err || !error_app_tag) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 297 | ERRARG; |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | if (err->apptag) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 302 | lydict_remove(server_opts.ctx, err->apptag); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 303 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 304 | err->apptag = lydict_insert(server_opts.ctx, error_app_tag, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 305 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 310 | nc_err_set_path(struct nc_server_error *err, const char *error_path) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 311 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 312 | if (!err || !error_path) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 313 | ERRARG; |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | if (err->path) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 318 | lydict_remove(server_opts.ctx, err->path); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 319 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 320 | err->path = lydict_insert(server_opts.ctx, error_path, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 321 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 326 | nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 327 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 328 | if (!err || !error_message) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 329 | ERRARG; |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | if (err->message) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 334 | lydict_remove(server_opts.ctx, err->apptag); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 335 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 336 | err->message = lydict_insert(server_opts.ctx, error_message, 0); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 337 | |
| 338 | if (err->message_lang) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 339 | lydict_remove(server_opts.ctx, err->message_lang); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 340 | } |
| 341 | if (lang) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 342 | err->message_lang = lydict_insert(server_opts.ctx, lang, 0); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 343 | } else { |
| 344 | lang = NULL; |
| 345 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 346 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | API int |
| 351 | nc_err_set_sid(struct nc_server_error *err, uint32_t session_id) |
| 352 | { |
| 353 | if (!err || !session_id) { |
| 354 | ERRARG; |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | err->sid = session_id; |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 363 | nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 364 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 365 | if (!err || !attr_name) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 366 | ERRARG; |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | ++err->attr_count; |
| 371 | err->attr = realloc(err->attr, err->attr_count * sizeof *err->attr); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 372 | err->attr[err->attr_count - 1] = lydict_insert(server_opts.ctx, attr_name, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 373 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 378 | nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 379 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 380 | if (!err || !elem_name) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 381 | ERRARG; |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | ++err->elem_count; |
| 386 | err->elem = realloc(err->elem, err->elem_count * sizeof *err->elem); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 387 | err->elem[err->elem_count - 1] = lydict_insert(server_opts.ctx, elem_name, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 388 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 393 | nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 394 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 395 | if (!err || !ns_name) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 396 | ERRARG; |
| 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | ++err->ns_count; |
| 401 | err->ns = realloc(err->ns, err->ns_count * sizeof *err->ns); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 402 | err->ns[err->ns_count - 1] = lydict_insert(server_opts.ctx, ns_name, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 403 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | API int |
| 408 | nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other) |
| 409 | { |
| 410 | if (!err || !other) { |
| 411 | ERRARG; |
| 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | ++err->other_count; |
| 416 | err->other = realloc(err->other, err->other_count * sizeof *err->other); |
| 417 | err->other[err->other_count - 1] = other; |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | void |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 422 | nc_server_rpc_free(struct nc_server_rpc *rpc, struct ly_ctx *ctx) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 423 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 424 | if (!rpc) { |
| 425 | return; |
| 426 | } |
| 427 | |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 428 | lyxml_free(ctx, rpc->root); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 429 | lyd_free(rpc->tree); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 430 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 431 | free(rpc); |
| 432 | } |
| 433 | |
| 434 | API void |
| 435 | nc_server_reply_free(struct nc_server_reply *reply) |
| 436 | { |
| 437 | uint32_t i; |
| 438 | struct nc_server_reply_data *data_rpl; |
| 439 | struct nc_server_reply_error *error_rpl; |
| 440 | |
| 441 | if (!reply) { |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | switch (reply->type) { |
| 446 | case NC_RPL_DATA: |
| 447 | data_rpl = (struct nc_server_reply_data *)reply; |
| 448 | if (data_rpl->free) { |
| 449 | lyd_free_withsiblings(data_rpl->data); |
| 450 | } |
| 451 | break; |
| 452 | case NC_RPL_OK: |
| 453 | /* nothing to free */ |
| 454 | break; |
| 455 | case NC_RPL_ERROR: |
| 456 | error_rpl = (struct nc_server_reply_error *)reply; |
| 457 | for (i = 0; i < error_rpl->count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 458 | nc_err_free(error_rpl->err[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 459 | } |
| 460 | free(error_rpl->err); |
| 461 | break; |
| 462 | default: |
| 463 | break; |
| 464 | } |
| 465 | free(reply); |
| 466 | } |
| 467 | |
| 468 | API void |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 469 | nc_err_free(struct nc_server_error *err) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 470 | { |
| 471 | uint32_t i; |
| 472 | |
| 473 | if (!err) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 474 | return; |
| 475 | } |
| 476 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 477 | lydict_remove(server_opts.ctx, err->apptag); |
| 478 | lydict_remove(server_opts.ctx, err->path); |
| 479 | lydict_remove(server_opts.ctx, err->message); |
| 480 | lydict_remove(server_opts.ctx, err->message_lang); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 481 | for (i = 0; i < err->attr_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 482 | lydict_remove(server_opts.ctx, err->attr[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 483 | } |
| 484 | free(err->attr); |
| 485 | for (i = 0; i < err->elem_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 486 | lydict_remove(server_opts.ctx, err->elem[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 487 | } |
| 488 | free(err->elem); |
| 489 | for (i = 0; i < err->ns_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 490 | lydict_remove(server_opts.ctx, err->ns[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 491 | } |
| 492 | free(err->ns); |
| 493 | for (i = 0; i < err->other_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 494 | lyxml_free(server_opts.ctx, err->other[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 495 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 496 | free(err->other); |
| 497 | free(err); |
| 498 | } |