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 | |
| 22 | #include "libnetconf.h" |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 23 | #include "session_server.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 * |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 43 | 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] | 44 | { |
| 45 | struct nc_server_reply_data *ret; |
| 46 | |
Michal Vasko | b08743b | 2016-04-13 14:23:49 +0200 | [diff] [blame] | 47 | if (!data) { |
| 48 | ERRARG("data"); |
| 49 | return NULL; |
| 50 | } |
| 51 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 52 | ret = malloc(sizeof *ret); |
| 53 | if (!ret) { |
| 54 | ERRMEM; |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | ret->type = NC_RPL_DATA; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 59 | ret->wd = wd; |
Michal Vasko | b08743b | 2016-04-13 14:23:49 +0200 | [diff] [blame] | 60 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 61 | ret->data = lyd_dup(data, 1); |
| 62 | } else { |
| 63 | ret->data = data; |
| 64 | } |
| 65 | if (paramtype != NC_PARAMTYPE_CONST) { |
| 66 | ret->free = 1; |
| 67 | } else { |
| 68 | ret->free = 0; |
| 69 | } |
| 70 | return (struct nc_server_reply *)ret; |
| 71 | } |
| 72 | |
| 73 | API struct nc_server_reply * |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 74 | nc_server_reply_err(struct nc_server_error *err) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 75 | { |
| 76 | struct nc_server_reply_error *ret; |
| 77 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 78 | if (!err) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 79 | ERRARG("err"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | ret = malloc(sizeof *ret); |
| 84 | if (!ret) { |
| 85 | ERRMEM; |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | ret->type = NC_RPL_ERROR; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 90 | ret->err = malloc(sizeof *ret->err); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 91 | if (!ret->err) { |
| 92 | ERRMEM; |
| 93 | free(ret); |
| 94 | return NULL; |
| 95 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 96 | ret->err[0] = err; |
| 97 | ret->count = 1; |
| 98 | return (struct nc_server_reply *)ret; |
| 99 | } |
| 100 | |
| 101 | API int |
| 102 | nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err) |
| 103 | { |
| 104 | struct nc_server_reply_error *err_rpl; |
| 105 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 106 | if (!reply || (reply->type != NC_RPL_ERROR)) { |
| 107 | ERRARG("reply"); |
| 108 | return -1; |
| 109 | } else if (!err) { |
| 110 | ERRARG("err"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | err_rpl = (struct nc_server_reply_error *)reply; |
| 115 | ++err_rpl->count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 116 | err_rpl->err = nc_realloc(err_rpl->err, err_rpl->count * sizeof *err_rpl->err); |
| 117 | if (!err_rpl->err) { |
| 118 | ERRMEM; |
| 119 | return -1; |
| 120 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 121 | err_rpl->err[err_rpl->count - 1] = err; |
| 122 | return 0; |
| 123 | } |
| 124 | |
Michal Vasko | aa2e5a7 | 2017-03-16 09:48:44 +0100 | [diff] [blame] | 125 | API const struct nc_server_error * |
| 126 | nc_server_reply_get_last_err(const struct nc_server_reply *reply) |
| 127 | { |
| 128 | struct nc_server_reply_error *err_rpl; |
| 129 | |
| 130 | if (!reply || (reply->type != NC_RPL_ERROR)) { |
| 131 | ERRARG("reply"); |
| 132 | return NULL; |
| 133 | } |
| 134 | |
| 135 | err_rpl = (struct nc_server_reply_error *)reply; |
| 136 | if (!err_rpl->count) { |
| 137 | return NULL; |
| 138 | } |
| 139 | return err_rpl->err[err_rpl->count - 1]; |
| 140 | } |
| 141 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 142 | API struct nc_server_error * |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 143 | nc_err(int tag, ...) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 144 | { |
| 145 | va_list ap; |
| 146 | struct nc_server_error *ret; |
| 147 | NC_ERR_TYPE type; |
| 148 | const char *arg1, *arg2; |
| 149 | uint32_t sid; |
| 150 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 151 | if (!tag) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 152 | ERRARG("tag"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | ret = calloc(1, sizeof *ret); |
| 157 | if (!ret) { |
| 158 | ERRMEM; |
| 159 | return NULL; |
| 160 | } |
Michal Vasko | 90920b0 | 2016-05-20 14:07:00 +0200 | [diff] [blame] | 161 | ret->sid = -1; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 162 | |
| 163 | va_start(ap, tag); |
| 164 | |
| 165 | switch (tag) { |
| 166 | case NC_ERR_IN_USE: |
| 167 | case NC_ERR_INVALID_VALUE: |
| 168 | case NC_ERR_ACCESS_DENIED: |
| 169 | case NC_ERR_ROLLBACK_FAILED: |
| 170 | case NC_ERR_OP_NOT_SUPPORTED: |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 171 | 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] | 172 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 173 | ERRARG("type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 174 | goto fail; |
| 175 | } |
| 176 | break; |
| 177 | |
| 178 | case NC_ERR_TOO_BIG: |
| 179 | case NC_ERR_RES_DENIED: |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 180 | type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 181 | /* nothing to check */ |
| 182 | break; |
| 183 | |
| 184 | case NC_ERR_MISSING_ATTR: |
| 185 | case NC_ERR_BAD_ATTR: |
| 186 | case NC_ERR_UNKNOWN_ATTR: |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 187 | type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 188 | arg1 = va_arg(ap, const char *); |
| 189 | arg2 = va_arg(ap, const char *); |
| 190 | |
| 191 | if (type == NC_ERR_TYPE_TRAN) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 192 | ERRARG("type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 193 | goto fail; |
| 194 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 195 | nc_err_add_bad_attr(ret, arg1); |
| 196 | nc_err_add_bad_elem(ret, arg2); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 197 | break; |
| 198 | |
| 199 | case NC_ERR_MISSING_ELEM: |
| 200 | case NC_ERR_BAD_ELEM: |
| 201 | case NC_ERR_UNKNOWN_ELEM: |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 202 | type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 203 | arg1 = va_arg(ap, const char *); |
| 204 | |
| 205 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 206 | ERRARG("type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 207 | goto fail; |
| 208 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 209 | nc_err_add_bad_elem(ret, arg1); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 210 | break; |
| 211 | |
| 212 | case NC_ERR_UNKNOWN_NS: |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 213 | type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 214 | arg1 = va_arg(ap, const char *); |
| 215 | arg2 = va_arg(ap, const char *); |
| 216 | |
| 217 | if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 218 | ERRARG("type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 219 | goto fail; |
| 220 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 221 | nc_err_add_bad_elem(ret, arg1); |
| 222 | nc_err_add_bad_ns(ret, arg2); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 223 | break; |
| 224 | |
| 225 | case NC_ERR_LOCK_DENIED: |
| 226 | sid = va_arg(ap, uint32_t); |
| 227 | |
| 228 | type = NC_ERR_TYPE_PROT; |
| 229 | nc_err_set_sid(ret, sid); |
| 230 | break; |
| 231 | |
| 232 | case NC_ERR_DATA_EXISTS: |
| 233 | case NC_ERR_DATA_MISSING: |
| 234 | type = NC_ERR_TYPE_APP; |
| 235 | break; |
| 236 | |
| 237 | case NC_ERR_OP_FAILED: |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 238 | type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 239 | |
| 240 | if (type == NC_ERR_TYPE_TRAN) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 241 | ERRARG("type"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 242 | goto fail; |
| 243 | } |
| 244 | break; |
| 245 | |
| 246 | case NC_ERR_MALFORMED_MSG: |
| 247 | type = NC_ERR_TYPE_RPC; |
| 248 | break; |
| 249 | |
| 250 | default: |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 251 | ERRARG("tag"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 252 | goto fail; |
| 253 | } |
| 254 | |
| 255 | switch (tag) { |
| 256 | case NC_ERR_IN_USE: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 257 | 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] | 258 | break; |
| 259 | case NC_ERR_INVALID_VALUE: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 260 | 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] | 261 | break; |
| 262 | case NC_ERR_TOO_BIG: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 263 | 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] | 264 | break; |
| 265 | case NC_ERR_MISSING_ATTR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 266 | nc_err_set_msg(ret, "An expected attribute is missing.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 267 | break; |
| 268 | case NC_ERR_BAD_ATTR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 269 | nc_err_set_msg(ret, "An attribute value is not correct.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 270 | break; |
| 271 | case NC_ERR_UNKNOWN_ATTR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 272 | nc_err_set_msg(ret, "An unexpected attribute is present.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 273 | break; |
| 274 | case NC_ERR_MISSING_ELEM: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 275 | nc_err_set_msg(ret, "An expected element is missing.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 276 | break; |
| 277 | case NC_ERR_BAD_ELEM: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 278 | nc_err_set_msg(ret, "An element value is not correct.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 279 | break; |
| 280 | case NC_ERR_UNKNOWN_ELEM: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 281 | nc_err_set_msg(ret, "An unexpected element is present.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 282 | break; |
| 283 | case NC_ERR_UNKNOWN_NS: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 284 | nc_err_set_msg(ret, "An unexpected namespace is present.", "en"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 285 | break; |
| 286 | case NC_ERR_ACCESS_DENIED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 287 | 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] | 288 | break; |
| 289 | case NC_ERR_LOCK_DENIED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 290 | 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] | 291 | break; |
| 292 | case NC_ERR_RES_DENIED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 293 | 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] | 294 | break; |
| 295 | case NC_ERR_ROLLBACK_FAILED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 296 | 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] | 297 | break; |
| 298 | case NC_ERR_DATA_EXISTS: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 299 | 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] | 300 | break; |
| 301 | case NC_ERR_DATA_MISSING: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 302 | 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] | 303 | break; |
| 304 | case NC_ERR_OP_NOT_SUPPORTED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 305 | 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] | 306 | break; |
| 307 | case NC_ERR_OP_FAILED: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 308 | 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] | 309 | break; |
| 310 | case NC_ERR_MALFORMED_MSG: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 311 | 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] | 312 | break; |
| 313 | default: |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 314 | ERRARG("tag"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 315 | goto fail; |
| 316 | } |
| 317 | |
| 318 | va_end(ap); |
| 319 | |
| 320 | ret->type = type; |
| 321 | ret->tag = tag; |
| 322 | return ret; |
| 323 | |
| 324 | fail: |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 325 | va_end(ap); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 326 | free(ret); |
| 327 | return NULL; |
| 328 | } |
| 329 | |
Michal Vasko | a08e5c0 | 2016-04-12 15:12:04 +0200 | [diff] [blame] | 330 | static struct lyxml_elem * |
| 331 | nc_err_libyang_other_elem(const char *name, const char *content, int cont_len) |
| 332 | { |
| 333 | struct lyxml_elem *root = NULL; |
| 334 | struct lyxml_ns *ns; |
| 335 | |
| 336 | root = calloc(1, sizeof *root); |
| 337 | if (!root) { |
| 338 | ERRMEM; |
| 339 | goto error; |
| 340 | } |
| 341 | root->prev = root; |
| 342 | root->name = lydict_insert(server_opts.ctx, name, 0); |
| 343 | root->content = lydict_insert(server_opts.ctx, content, cont_len); |
| 344 | |
| 345 | ns = calloc(1, sizeof *root->ns); |
| 346 | if (!ns) { |
| 347 | ERRMEM; |
| 348 | goto error; |
| 349 | } |
| 350 | root->attr = (struct lyxml_attr *)ns; |
| 351 | ns->type = LYXML_ATTR_NS; |
| 352 | ns->parent = root; |
| 353 | ns->value = lydict_insert(server_opts.ctx, "urn:ietf:params:xml:ns:yang:1", 0); |
| 354 | root->ns = ns; |
| 355 | |
| 356 | return root; |
| 357 | |
| 358 | error: |
| 359 | lyxml_free(server_opts.ctx, root); |
| 360 | return NULL; |
| 361 | } |
| 362 | |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 363 | API struct nc_server_error * |
| 364 | nc_err_libyang(void) |
| 365 | { |
| 366 | struct nc_server_error *e; |
Michal Vasko | a08e5c0 | 2016-04-12 15:12:04 +0200 | [diff] [blame] | 367 | struct lyxml_elem *elem; |
| 368 | const char *str, *stri, *strj, *strk, *strl, *uniqi, *uniqj; |
| 369 | char *attr, *path; |
| 370 | int len; |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 371 | |
| 372 | if (!ly_errno) { |
| 373 | /* LY_SUCCESS */ |
| 374 | return NULL; |
| 375 | } else if (ly_errno == LY_EVALID) { |
| 376 | switch (ly_vecode) { |
Michal Vasko | a08e5c0 | 2016-04-12 15:12:04 +0200 | [diff] [blame] | 377 | /* RFC 6020 section 13 errors */ |
| 378 | case LYVE_NOUNIQ: |
| 379 | e = nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 380 | nc_err_set_app_tag(e, "data-not-unique"); |
| 381 | nc_err_set_path(e, ly_errpath()); |
| 382 | |
| 383 | /* parse the message and get all the information we need */ |
| 384 | str = ly_errmsg(); |
| 385 | uniqi = strchr(str, '"'); |
| 386 | uniqi++; |
| 387 | uniqj = strchr(uniqi, '"'); |
| 388 | |
| 389 | stri = strchr(uniqj + 1, '"'); |
| 390 | stri++; |
| 391 | strj = strchr(stri, '"'); |
| 392 | |
| 393 | strk = strchr(strj + 1, '"'); |
| 394 | ++strk; |
| 395 | strl = strchr(strk, '"'); |
| 396 | |
| 397 | /* maximum length is the whole unique string with the longer list instance identifier */ |
| 398 | len = (uniqj - uniqi) + (strj - stri > strl - strk ? strj - stri : strl - strk); |
| 399 | path = malloc(len + 1); |
| 400 | if (!path) { |
| 401 | ERRMEM; |
| 402 | return e; |
| 403 | } |
| 404 | |
| 405 | /* create non-unique elements, one in 1st list, one in 2nd list, for each unique list */ |
| 406 | while (1) { |
| 407 | uniqj = strpbrk(uniqi, " \""); |
| 408 | |
| 409 | len = sprintf(path, "%.*s/%.*s", (int)(strj - stri), stri, (int)(uniqj - uniqi), uniqi); |
| 410 | elem = nc_err_libyang_other_elem("non-unique", path, len); |
| 411 | if (!elem) { |
| 412 | free(path); |
| 413 | return e; |
| 414 | } |
| 415 | nc_err_add_info_other(e, elem); |
| 416 | |
| 417 | len = sprintf(path, "%.*s/%.*s", (int)(strl - strk), strk, (int)(uniqj - uniqi), uniqi); |
| 418 | elem = nc_err_libyang_other_elem("non-unique", path, len); |
| 419 | if (!elem) { |
| 420 | return e; |
| 421 | } |
| 422 | nc_err_add_info_other(e, elem); |
| 423 | |
| 424 | if (uniqj[0] == '"') { |
| 425 | break; |
| 426 | } |
| 427 | uniqi = uniqj + 1; |
| 428 | } |
| 429 | free(path); |
| 430 | break; |
| 431 | case LYVE_NOMAX: |
| 432 | e = nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 433 | nc_err_set_app_tag(e, "too-many-elements"); |
| 434 | nc_err_set_path(e, ly_errpath()); |
| 435 | break; |
| 436 | case LYVE_NOMIN: |
| 437 | e = nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 438 | nc_err_set_app_tag(e, "too-few-elements"); |
| 439 | nc_err_set_path(e, ly_errpath()); |
| 440 | break; |
| 441 | case LYVE_NOMUST: |
| 442 | e = nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 443 | if (ly_errapptag()[0]) { |
| 444 | nc_err_set_app_tag(e, ly_errapptag()); |
| 445 | } else { |
| 446 | nc_err_set_app_tag(e, "must-violation"); |
| 447 | } |
| 448 | break; |
| 449 | case LYVE_NOREQINS: |
| 450 | case LYVE_NOLEAFREF: |
| 451 | e = nc_err(NC_ERR_DATA_MISSING); |
| 452 | nc_err_set_app_tag(e, "instance-required"); |
| 453 | nc_err_set_path(e, ly_errpath()); |
| 454 | break; |
| 455 | case LYVE_NOMANDCHOICE: |
| 456 | e = nc_err(NC_ERR_DATA_MISSING); |
| 457 | nc_err_set_app_tag(e, "missing-choice"); |
| 458 | nc_err_set_path(e, ly_errpath()); |
| 459 | |
| 460 | str = ly_errmsg(); |
| 461 | stri = strchr(str, '"'); |
| 462 | stri++; |
| 463 | strj = strchr(stri, '"'); |
| 464 | elem = nc_err_libyang_other_elem("missing-choice", stri, strj - stri); |
| 465 | if (elem) { |
| 466 | nc_err_add_info_other(e, elem); |
| 467 | } |
| 468 | break; |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 469 | case LYVE_INELEM: |
| 470 | str = ly_errpath(); |
| 471 | if (!strcmp(str, "/")) { |
| 472 | e = nc_err(NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_APP); |
| 473 | /* keep default message */ |
| 474 | return e; |
| 475 | } else { |
| 476 | e = nc_err(NC_ERR_UNKNOWN_ELEM, NC_ERR_TYPE_PROT, ly_errpath()); |
| 477 | } |
| 478 | break; |
| 479 | case LYVE_MISSELEM: |
| 480 | case LYVE_INORDER: |
| 481 | e = nc_err(NC_ERR_MISSING_ELEM, NC_ERR_TYPE_PROT, ly_errpath()); |
| 482 | break; |
| 483 | case LYVE_INVAL: |
| 484 | e = nc_err(NC_ERR_BAD_ELEM, NC_ERR_TYPE_PROT, ly_errpath()); |
| 485 | break; |
| 486 | case LYVE_INATTR: |
| 487 | case LYVE_MISSATTR: |
Michal Vasko | 37fdad5 | 2017-03-02 11:40:22 +0100 | [diff] [blame] | 488 | case LYVE_INMETA: |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 489 | str = ly_errmsg(); |
| 490 | stri = strchr(str, '"'); |
| 491 | stri++; |
| 492 | strj = strchr(stri, '"'); |
| 493 | strj--; |
| 494 | attr = strndup(stri, strj - stri); |
Radek Krejci | 537fb52 | 2016-04-07 16:36:25 +0200 | [diff] [blame] | 495 | if (ly_vecode == LYVE_INATTR) { |
| 496 | e = nc_err(NC_ERR_UNKNOWN_ATTR, NC_ERR_TYPE_PROT, attr, ly_errpath()); |
| 497 | } else if (ly_vecode == LYVE_MISSATTR) { |
| 498 | e = nc_err(NC_ERR_MISSING_ATTR, NC_ERR_TYPE_PROT, attr, ly_errpath()); |
Michal Vasko | 37fdad5 | 2017-03-02 11:40:22 +0100 | [diff] [blame] | 499 | } else { /* LYVE_INMETA */ |
Radek Krejci | 537fb52 | 2016-04-07 16:36:25 +0200 | [diff] [blame] | 500 | e = nc_err(NC_ERR_BAD_ATTR, NC_ERR_TYPE_PROT, attr, ly_errpath()); |
| 501 | } |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 502 | free(attr); |
| 503 | break; |
Michal Vasko | a08e5c0 | 2016-04-12 15:12:04 +0200 | [diff] [blame] | 504 | case LYVE_NOCONSTR: |
| 505 | case LYVE_NOWHEN: |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 506 | e = nc_err(NC_ERR_INVALID_VALUE, NC_ERR_TYPE_PROT); |
Michal Vasko | a08e5c0 | 2016-04-12 15:12:04 +0200 | [diff] [blame] | 507 | /* LYVE_NOCONSTR (length, range, pattern) can have a specific error-app-tag */ |
| 508 | if (ly_errapptag()[0]) { |
| 509 | nc_err_set_app_tag(e, ly_errapptag()); |
| 510 | } |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 511 | break; |
| 512 | default: |
| 513 | e = nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 514 | break; |
| 515 | } |
| 516 | } else { |
| 517 | /* non-validation (internal) error */ |
| 518 | e = nc_err(NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 519 | } |
| 520 | nc_err_set_msg(e, ly_errmsg(), "en"); |
| 521 | return e; |
| 522 | } |
| 523 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 524 | API NC_ERR_TYPE |
Michal Vasko | ea20178 | 2017-03-16 09:49:54 +0100 | [diff] [blame] | 525 | nc_err_get_type(const struct nc_server_error *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 526 | { |
| 527 | if (!err) { |
| 528 | ERRARG("err"); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | return err->type; |
| 533 | } |
| 534 | |
| 535 | API NC_ERR |
Michal Vasko | ea20178 | 2017-03-16 09:49:54 +0100 | [diff] [blame] | 536 | nc_err_get_tag(const struct nc_server_error *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 537 | { |
| 538 | if (!err) { |
| 539 | ERRARG("err"); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | return err->tag; |
| 544 | } |
| 545 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 546 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 547 | 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] | 548 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 549 | if (!err) { |
| 550 | ERRARG("err"); |
| 551 | return -1; |
| 552 | } else if (!error_app_tag) { |
| 553 | ERRARG("error_app_tag"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | if (err->apptag) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 558 | lydict_remove(server_opts.ctx, err->apptag); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 559 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 560 | err->apptag = lydict_insert(server_opts.ctx, error_app_tag, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 561 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 565 | API const char * |
Michal Vasko | ea20178 | 2017-03-16 09:49:54 +0100 | [diff] [blame] | 566 | nc_err_get_app_tag(const struct nc_server_error *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 567 | { |
| 568 | if (!err) { |
| 569 | ERRARG("err"); |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | return err->apptag; |
| 574 | } |
| 575 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 576 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 577 | 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] | 578 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 579 | if (!err) { |
| 580 | ERRARG("err"); |
| 581 | return -1; |
| 582 | } else if (!error_path) { |
| 583 | ERRARG("error_path"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | if (err->path) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 588 | lydict_remove(server_opts.ctx, err->path); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 589 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 590 | err->path = lydict_insert(server_opts.ctx, error_path, 0); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 591 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 595 | API const char * |
Michal Vasko | ea20178 | 2017-03-16 09:49:54 +0100 | [diff] [blame] | 596 | nc_err_get_path(const struct nc_server_error *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 597 | { |
| 598 | if (!err) { |
| 599 | ERRARG("err"); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | return err->path; |
| 604 | } |
| 605 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 606 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 607 | 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] | 608 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 609 | if (!err) { |
| 610 | ERRARG("err"); |
| 611 | return -1; |
| 612 | } else if (!error_message) { |
| 613 | ERRARG("error_message"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 614 | return -1; |
| 615 | } |
| 616 | |
| 617 | if (err->message) { |
Michal Vasko | 092ea74 | 2016-04-12 15:08:59 +0200 | [diff] [blame] | 618 | lydict_remove(server_opts.ctx, err->message); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 619 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 620 | err->message = lydict_insert(server_opts.ctx, error_message, 0); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 621 | |
| 622 | if (err->message_lang) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 623 | lydict_remove(server_opts.ctx, err->message_lang); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 624 | } |
| 625 | if (lang) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 626 | err->message_lang = lydict_insert(server_opts.ctx, lang, 0); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 627 | } else { |
| 628 | lang = NULL; |
| 629 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 630 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 631 | return 0; |
| 632 | } |
| 633 | |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 634 | API const char * |
Michal Vasko | ea20178 | 2017-03-16 09:49:54 +0100 | [diff] [blame] | 635 | nc_err_get_msg(const struct nc_server_error *err) |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 636 | { |
| 637 | if (!err) { |
| 638 | ERRARG("err"); |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | return err->message; |
| 643 | } |
| 644 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 645 | API int |
| 646 | nc_err_set_sid(struct nc_server_error *err, uint32_t session_id) |
| 647 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 648 | if (!err) { |
| 649 | ERRARG("err"); |
| 650 | return -1; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | err->sid = session_id; |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 658 | 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] | 659 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 660 | if (!err) { |
| 661 | ERRARG("err"); |
| 662 | return -1; |
| 663 | } else if (!attr_name) { |
| 664 | ERRARG("attr_name"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | ++err->attr_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 669 | err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr); |
| 670 | if (!err->attr) { |
| 671 | ERRMEM; |
| 672 | return -1; |
| 673 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 674 | 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] | 675 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 680 | 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] | 681 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 682 | if (!err) { |
| 683 | ERRARG("err"); |
| 684 | return -1; |
| 685 | } else if (!elem_name) { |
| 686 | ERRARG("elem_name"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 687 | return -1; |
| 688 | } |
| 689 | |
| 690 | ++err->elem_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 691 | err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem); |
| 692 | if (!err->elem) { |
| 693 | ERRMEM; |
| 694 | return -1; |
| 695 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 696 | 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] | 697 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | API int |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 702 | 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] | 703 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 704 | if (!err) { |
| 705 | ERRARG("err"); |
| 706 | return -1; |
| 707 | } else if (!ns_name) { |
| 708 | ERRARG("ns_name"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | ++err->ns_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 713 | err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns); |
| 714 | if (!err->ns) { |
| 715 | ERRMEM; |
| 716 | return -1; |
| 717 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 718 | 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] | 719 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | API int |
| 724 | nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other) |
| 725 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 726 | if (!err) { |
| 727 | ERRARG("err"); |
| 728 | return -1; |
| 729 | } else if (!other) { |
| 730 | ERRARG("other"); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | ++err->other_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 735 | err->other = nc_realloc(err->other, err->other_count * sizeof *err->other); |
| 736 | if (!err->other) { |
| 737 | ERRMEM; |
| 738 | return -1; |
| 739 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 740 | err->other[err->other_count - 1] = other; |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | void |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 745 | 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] | 746 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 747 | if (!rpc) { |
| 748 | return; |
| 749 | } |
| 750 | |
Michal Vasko | ca4a242 | 2016-02-02 12:17:14 +0100 | [diff] [blame] | 751 | lyxml_free(ctx, rpc->root); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 752 | lyd_free(rpc->tree); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 753 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 754 | free(rpc); |
| 755 | } |
| 756 | |
| 757 | API void |
| 758 | nc_server_reply_free(struct nc_server_reply *reply) |
| 759 | { |
| 760 | uint32_t i; |
| 761 | struct nc_server_reply_data *data_rpl; |
| 762 | struct nc_server_reply_error *error_rpl; |
| 763 | |
| 764 | if (!reply) { |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | switch (reply->type) { |
| 769 | case NC_RPL_DATA: |
| 770 | data_rpl = (struct nc_server_reply_data *)reply; |
| 771 | if (data_rpl->free) { |
| 772 | lyd_free_withsiblings(data_rpl->data); |
| 773 | } |
| 774 | break; |
| 775 | case NC_RPL_OK: |
| 776 | /* nothing to free */ |
| 777 | break; |
| 778 | case NC_RPL_ERROR: |
| 779 | error_rpl = (struct nc_server_reply_error *)reply; |
| 780 | for (i = 0; i < error_rpl->count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 781 | nc_err_free(error_rpl->err[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 782 | } |
| 783 | free(error_rpl->err); |
| 784 | break; |
| 785 | default: |
| 786 | break; |
| 787 | } |
| 788 | free(reply); |
| 789 | } |
| 790 | |
| 791 | API void |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 792 | nc_err_free(struct nc_server_error *err) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 793 | { |
| 794 | uint32_t i; |
| 795 | |
| 796 | if (!err) { |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 800 | lydict_remove(server_opts.ctx, err->apptag); |
| 801 | lydict_remove(server_opts.ctx, err->path); |
| 802 | lydict_remove(server_opts.ctx, err->message); |
| 803 | lydict_remove(server_opts.ctx, err->message_lang); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 804 | for (i = 0; i < err->attr_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 805 | lydict_remove(server_opts.ctx, err->attr[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 806 | } |
| 807 | free(err->attr); |
| 808 | for (i = 0; i < err->elem_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 809 | lydict_remove(server_opts.ctx, err->elem[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 810 | } |
| 811 | free(err->elem); |
| 812 | for (i = 0; i < err->ns_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 813 | lydict_remove(server_opts.ctx, err->ns[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 814 | } |
| 815 | free(err->ns); |
| 816 | for (i = 0; i < err->other_count; ++i) { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 817 | lyxml_free(server_opts.ctx, err->other[i]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 818 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 819 | free(err->other); |
| 820 | free(err); |
| 821 | } |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 822 | |
| 823 | API struct nc_server_notif * |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 824 | 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] | 825 | { |
| 826 | struct nc_server_notif *ntf; |
| 827 | |
| 828 | if (!event || event->schema->nodetype != LYS_NOTIF) { |
| 829 | ERRARG("event"); |
| 830 | return NULL; |
| 831 | } else if (!eventtime) { |
| 832 | ERRARG("eventtime"); |
| 833 | return NULL; |
| 834 | } |
| 835 | |
| 836 | ntf = malloc(sizeof *ntf); |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 837 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 838 | ntf->eventtime = strdup(eventtime); |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 839 | ntf->tree = lyd_dup(event, 1); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 840 | } else { |
| 841 | ntf->eventtime = eventtime; |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 842 | ntf->tree = event; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 843 | } |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 844 | ntf->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 845 | |
| 846 | return ntf; |
| 847 | } |
| 848 | |
| 849 | API void |
| 850 | nc_server_notif_free(struct nc_server_notif *notif) |
| 851 | { |
| 852 | if (!notif) { |
| 853 | return; |
| 854 | } |
| 855 | |
Michal Vasko | fc9dbdd | 2017-03-17 09:27:57 +0100 | [diff] [blame] | 856 | if (notif->free) { |
| 857 | lyd_free(notif->tree); |
| 858 | free(notif->eventtime); |
| 859 | } |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 860 | free(notif); |
| 861 | } |
Michal Vasko | 9a2e4d2 | 2017-03-17 09:44:49 +0100 | [diff] [blame] | 862 | |
| 863 | API const char * |
| 864 | nc_server_notif_get_time(const struct nc_server_notif *notif) |
| 865 | { |
| 866 | if (!notif) { |
| 867 | ERRARG("notif"); |
| 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | return notif->eventtime; |
| 872 | } |