Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file messages.c |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 - 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 | |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 16 | #include <ctype.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <stdarg.h> |
| 20 | |
| 21 | #include <libyang/libyang.h> |
| 22 | |
| 23 | #include "libnetconf.h" |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 24 | |
| 25 | const char *rpcedit_dfltop2str[] = {NULL, "merge", "replace", "none"}; |
| 26 | const char *rpcedit_testopt2str[] = {NULL, "test-then-set", "set", "test-only"}; |
| 27 | const char *rpcedit_erropt2str[] = {NULL, "stop-on-error", "continue-on-error", "rollback-on-error"}; |
| 28 | |
| 29 | API NC_RPC_TYPE |
| 30 | nc_rpc_get_type(const struct nc_rpc *rpc) |
| 31 | { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 32 | if (!rpc) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 33 | ERRARG("rpc"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 34 | return 0; |
| 35 | } |
| 36 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 37 | return rpc->type; |
| 38 | } |
| 39 | |
| 40 | API struct nc_rpc * |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 41 | nc_rpc_act_generic(const struct lyd_node *data, NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 42 | { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 43 | struct nc_rpc_act_generic *rpc; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 44 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 45 | if (!data || data->next || (data->prev != data)) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 46 | ERRARG("data"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 47 | return NULL; |
| 48 | } |
| 49 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 50 | rpc = malloc(sizeof *rpc); |
| 51 | if (!rpc) { |
| 52 | ERRMEM; |
| 53 | return NULL; |
| 54 | } |
| 55 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 56 | rpc->type = NC_RPC_ACT_GENERIC; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 57 | rpc->has_data = 1; |
| 58 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 59 | rpc->content.data = lyd_dup(data, 1); |
| 60 | } else { |
| 61 | rpc->content.data = (struct lyd_node *)data; |
| 62 | } |
| 63 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 64 | |
| 65 | return (struct nc_rpc *)rpc; |
| 66 | } |
| 67 | |
| 68 | API struct nc_rpc * |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 69 | nc_rpc_act_generic_xml(const char *xml_str, NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 70 | { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 71 | struct nc_rpc_act_generic *rpc; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 72 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 73 | if (!xml_str) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 74 | ERRARG("xml_str"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 78 | rpc = malloc(sizeof *rpc); |
| 79 | if (!rpc) { |
| 80 | ERRMEM; |
| 81 | return NULL; |
| 82 | } |
| 83 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 84 | rpc->type = NC_RPC_ACT_GENERIC; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 85 | rpc->has_data = 0; |
| 86 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 87 | rpc->content.xml_str = strdup(xml_str); |
| 88 | } else { |
| 89 | rpc->content.xml_str = (char *)xml_str; |
| 90 | } |
| 91 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 92 | |
| 93 | return (struct nc_rpc *)rpc; |
| 94 | } |
| 95 | |
| 96 | API struct nc_rpc * |
| 97 | nc_rpc_getconfig(NC_DATASTORE source, const char *filter, NC_WD_MODE wd_mode, NC_PARAMTYPE paramtype) |
| 98 | { |
| 99 | struct nc_rpc_getconfig *rpc; |
| 100 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 101 | if (!source) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 102 | ERRARG("source"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
| 105 | |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 106 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 107 | ERR("Filter is neither an XML subtree nor an XPath expression (invalid first char '%c').", filter[0]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | rpc = malloc(sizeof *rpc); |
| 112 | if (!rpc) { |
| 113 | ERRMEM; |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | rpc->type = NC_RPC_GETCONFIG; |
| 118 | rpc->source = source; |
| 119 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 120 | rpc->filter = strdup(filter); |
| 121 | } else { |
| 122 | rpc->filter = (char *)filter; |
| 123 | } |
| 124 | rpc->wd_mode = wd_mode; |
| 125 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 126 | |
| 127 | return (struct nc_rpc *)rpc; |
| 128 | } |
| 129 | |
| 130 | API struct nc_rpc * |
| 131 | nc_rpc_edit(NC_DATASTORE target, NC_RPC_EDIT_DFLTOP default_op, NC_RPC_EDIT_TESTOPT test_opt, |
| 132 | NC_RPC_EDIT_ERROPT error_opt, const char *edit_content, NC_PARAMTYPE paramtype) |
| 133 | { |
| 134 | struct nc_rpc_edit *rpc; |
| 135 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 136 | if (!target) { |
| 137 | ERRARG("target"); |
| 138 | return NULL; |
| 139 | } else if (!edit_content) { |
| 140 | ERRARG("edit_content"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| 143 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 144 | if (edit_content[0] && (edit_content[0] != '<') && !isalpha(edit_content[0])) { |
| 145 | ERR("<edit-config> content is neither a URL nor an XML config (invalid first char '%c').", edit_content[0]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | rpc = malloc(sizeof *rpc); |
| 150 | if (!rpc) { |
| 151 | ERRMEM; |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | rpc->type = NC_RPC_EDIT; |
| 156 | rpc->target = target; |
| 157 | rpc->default_op = default_op; |
| 158 | rpc->test_opt = test_opt; |
| 159 | rpc->error_opt = error_opt; |
| 160 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 161 | rpc->edit_cont = strdup(edit_content); |
| 162 | } else { |
| 163 | rpc->edit_cont = (char *)edit_content; |
| 164 | } |
| 165 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 166 | |
| 167 | return (struct nc_rpc *)rpc; |
| 168 | } |
| 169 | |
| 170 | API struct nc_rpc * |
| 171 | nc_rpc_copy(NC_DATASTORE target, const char *url_trg, NC_DATASTORE source, const char *url_or_config_src, |
| 172 | NC_WD_MODE wd_mode, NC_PARAMTYPE paramtype) |
| 173 | { |
| 174 | struct nc_rpc_copy *rpc; |
| 175 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 176 | if (!target) { |
| 177 | ERRARG("target"); |
| 178 | return NULL; |
| 179 | } else if (!source) { |
| 180 | ERRARG("source"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
| 183 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 184 | if (url_or_config_src && url_or_config_src[0] && (url_or_config_src[0] != '<') && !isalpha(url_or_config_src[0])) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 185 | ERR("<copy-config> source is neither a URL nor an XML config (invalid first char '%c').", url_or_config_src[0]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | rpc = malloc(sizeof *rpc); |
| 190 | if (!rpc) { |
| 191 | ERRMEM; |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | rpc->type = NC_RPC_COPY; |
| 196 | rpc->target = target; |
| 197 | if (url_trg && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 198 | rpc->url_trg = strdup(url_trg); |
| 199 | } else { |
| 200 | rpc->url_trg = (char *)url_trg; |
| 201 | } |
| 202 | rpc->source = source; |
| 203 | if (url_or_config_src && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 204 | rpc->url_config_src = strdup(url_or_config_src); |
| 205 | } else { |
| 206 | rpc->url_config_src = (char *)url_or_config_src; |
| 207 | } |
| 208 | rpc->wd_mode = wd_mode; |
| 209 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 210 | |
| 211 | return (struct nc_rpc *)rpc; |
| 212 | } |
| 213 | |
| 214 | API struct nc_rpc * |
| 215 | nc_rpc_delete(NC_DATASTORE target, const char *url, NC_PARAMTYPE paramtype) |
| 216 | { |
| 217 | struct nc_rpc_delete *rpc; |
| 218 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 219 | if (!target) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 220 | ERRARG("target"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 224 | rpc = malloc(sizeof *rpc); |
| 225 | if (!rpc) { |
| 226 | ERRMEM; |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | rpc->type = NC_RPC_DELETE; |
| 231 | rpc->target = target; |
| 232 | if (url && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 233 | rpc->url = strdup(url); |
| 234 | } else { |
| 235 | rpc->url = (char *)url; |
| 236 | } |
| 237 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 238 | |
| 239 | return (struct nc_rpc *)rpc; |
| 240 | } |
| 241 | |
| 242 | API struct nc_rpc * |
| 243 | nc_rpc_lock(NC_DATASTORE target) |
| 244 | { |
| 245 | struct nc_rpc_lock *rpc; |
| 246 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 247 | if (!target) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 248 | ERRARG("target"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 249 | return NULL; |
| 250 | } |
| 251 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 252 | rpc = malloc(sizeof *rpc); |
| 253 | if (!rpc) { |
| 254 | ERRMEM; |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | rpc->type = NC_RPC_LOCK; |
| 259 | rpc->target = target; |
| 260 | |
| 261 | return (struct nc_rpc *)rpc; |
| 262 | } |
| 263 | |
| 264 | API struct nc_rpc * |
| 265 | nc_rpc_unlock(NC_DATASTORE target) |
| 266 | { |
| 267 | struct nc_rpc_lock *rpc; |
| 268 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 269 | if (!target) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 270 | ERRARG("target"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 271 | return NULL; |
| 272 | } |
| 273 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 274 | rpc = malloc(sizeof *rpc); |
| 275 | if (!rpc) { |
| 276 | ERRMEM; |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | rpc->type = NC_RPC_UNLOCK; |
| 281 | rpc->target = target; |
| 282 | |
| 283 | return (struct nc_rpc *)rpc; |
| 284 | } |
| 285 | |
| 286 | API struct nc_rpc * |
| 287 | nc_rpc_get(const char *filter, NC_WD_MODE wd_mode, NC_PARAMTYPE paramtype) |
| 288 | { |
| 289 | struct nc_rpc_get *rpc; |
| 290 | |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 291 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 292 | ERR("Filter is neither an XML subtree nor an XPath expression (invalid first char '%c').", filter[0]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | rpc = malloc(sizeof *rpc); |
| 297 | if (!rpc) { |
| 298 | ERRMEM; |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | rpc->type = NC_RPC_GET; |
| 303 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 304 | rpc->filter = strdup(filter); |
| 305 | } else { |
| 306 | rpc->filter = (char *)filter; |
| 307 | } |
| 308 | rpc->wd_mode = wd_mode; |
| 309 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 310 | |
| 311 | return (struct nc_rpc *)rpc; |
| 312 | } |
| 313 | |
| 314 | API struct nc_rpc * |
| 315 | nc_rpc_kill(uint32_t session_id) |
| 316 | { |
| 317 | struct nc_rpc_kill *rpc; |
| 318 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 319 | if (!session_id) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 320 | ERRARG("session_id"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 321 | return NULL; |
| 322 | } |
| 323 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 324 | rpc = malloc(sizeof *rpc); |
| 325 | if (!rpc) { |
| 326 | ERRMEM; |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | rpc->type = NC_RPC_KILL; |
| 331 | rpc->sid = session_id; |
| 332 | |
| 333 | return (struct nc_rpc *)rpc; |
| 334 | } |
| 335 | |
| 336 | API struct nc_rpc * |
| 337 | nc_rpc_commit(int confirmed, uint32_t confirm_timeout, const char *persist, const char *persist_id, |
| 338 | NC_PARAMTYPE paramtype) |
| 339 | { |
| 340 | struct nc_rpc_commit *rpc; |
| 341 | |
| 342 | rpc = malloc(sizeof *rpc); |
| 343 | if (!rpc) { |
| 344 | ERRMEM; |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | rpc->type = NC_RPC_COMMIT; |
| 349 | rpc->confirmed = confirmed; |
| 350 | rpc->confirm_timeout = confirm_timeout; |
| 351 | if (persist && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 352 | rpc->persist = strdup(persist); |
| 353 | } else { |
| 354 | rpc->persist = (char *)persist; |
| 355 | } |
| 356 | if (persist_id && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 357 | rpc->persist_id = strdup(persist_id); |
| 358 | } else { |
| 359 | rpc->persist_id = (char *)persist_id; |
| 360 | } |
| 361 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 362 | |
| 363 | return (struct nc_rpc *)rpc; |
| 364 | } |
| 365 | |
| 366 | API struct nc_rpc * |
| 367 | nc_rpc_discard(void) |
| 368 | { |
| 369 | struct nc_rpc *rpc; |
| 370 | |
| 371 | rpc = malloc(sizeof *rpc); |
| 372 | if (!rpc) { |
| 373 | ERRMEM; |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | rpc->type = NC_RPC_DISCARD; |
| 378 | |
| 379 | return rpc; |
| 380 | } |
| 381 | |
| 382 | API struct nc_rpc * |
| 383 | nc_rpc_cancel(const char *persist_id, NC_PARAMTYPE paramtype) |
| 384 | { |
| 385 | struct nc_rpc_cancel *rpc; |
| 386 | |
| 387 | rpc = malloc(sizeof *rpc); |
| 388 | if (!rpc) { |
| 389 | ERRMEM; |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | rpc->type = NC_RPC_CANCEL; |
| 394 | if (persist_id && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 395 | rpc->persist_id = strdup(persist_id); |
| 396 | } else { |
| 397 | rpc->persist_id = (char *)persist_id; |
| 398 | } |
| 399 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 400 | |
| 401 | return (struct nc_rpc *)rpc; |
| 402 | } |
| 403 | |
| 404 | API struct nc_rpc * |
| 405 | nc_rpc_validate(NC_DATASTORE source, const char *url_or_config, NC_PARAMTYPE paramtype) |
| 406 | { |
| 407 | struct nc_rpc_validate *rpc; |
| 408 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 409 | if (!source) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 410 | ERRARG("source"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 411 | return NULL; |
| 412 | } |
| 413 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 414 | if (url_or_config && url_or_config[0] && (url_or_config[0] != '<') && !isalpha(url_or_config[0])) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 415 | ERR("<validate> source is neither a URL nor an XML config (invalid first char '%c').", url_or_config[0]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | rpc = malloc(sizeof *rpc); |
| 420 | if (!rpc) { |
| 421 | ERRMEM; |
| 422 | return NULL; |
| 423 | } |
| 424 | |
| 425 | rpc->type = NC_RPC_VALIDATE; |
| 426 | rpc->source = source; |
| 427 | if (url_or_config && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 428 | rpc->url_config_src = strdup(url_or_config); |
| 429 | } else { |
| 430 | rpc->url_config_src = (char *)url_or_config; |
| 431 | } |
| 432 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 433 | |
| 434 | return (struct nc_rpc *)rpc; |
| 435 | } |
| 436 | |
| 437 | API struct nc_rpc * |
| 438 | nc_rpc_getschema(const char *identifier, const char *version, const char *format, NC_PARAMTYPE paramtype) |
| 439 | { |
| 440 | struct nc_rpc_getschema *rpc; |
| 441 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 442 | if (!identifier) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 443 | ERRARG("identifier"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 444 | return NULL; |
| 445 | } |
| 446 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 447 | rpc = malloc(sizeof *rpc); |
| 448 | if (!rpc) { |
| 449 | ERRMEM; |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | rpc->type = NC_RPC_GETSCHEMA; |
| 454 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 455 | rpc->identifier = strdup(identifier); |
| 456 | } else { |
| 457 | rpc->identifier = (char *)identifier; |
| 458 | } |
| 459 | if (version && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 460 | rpc->version = strdup(version); |
| 461 | } else { |
| 462 | rpc->version = (char *)version; |
| 463 | } |
| 464 | if (format && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 465 | rpc->format = strdup(format); |
| 466 | } else { |
| 467 | rpc->format = (char *)format; |
| 468 | } |
| 469 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 470 | |
| 471 | return (struct nc_rpc *)rpc; |
| 472 | } |
| 473 | |
| 474 | API struct nc_rpc * |
| 475 | nc_rpc_subscribe(const char *stream_name, const char *filter, const char *start_time, const char *stop_time, |
| 476 | NC_PARAMTYPE paramtype) |
| 477 | { |
| 478 | struct nc_rpc_subscribe *rpc; |
| 479 | |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 480 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 481 | ERR("Filter is neither an XML subtree nor an XPath expression (invalid first char '%c').", filter[0]); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | rpc = malloc(sizeof *rpc); |
| 486 | if (!rpc) { |
| 487 | ERRMEM; |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | rpc->type = NC_RPC_SUBSCRIBE; |
| 492 | if (stream_name && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 493 | rpc->stream = strdup(stream_name); |
| 494 | } else { |
| 495 | rpc->stream = (char *)stream_name; |
| 496 | } |
| 497 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 498 | rpc->filter = strdup(filter); |
| 499 | } else { |
| 500 | rpc->filter = (char *)filter; |
| 501 | } |
| 502 | if (start_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 503 | rpc->start = strdup(start_time); |
| 504 | } else { |
| 505 | rpc->start = (char *)start_time; |
| 506 | } |
| 507 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 508 | rpc->stop = strdup(stop_time); |
| 509 | } else { |
| 510 | rpc->stop = (char *)stop_time; |
| 511 | } |
| 512 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 513 | |
| 514 | return (struct nc_rpc *)rpc; |
| 515 | } |
| 516 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 517 | API struct nc_rpc * |
| 518 | nc_rpc_getdata(const char *datastore, const char *filter, const char *config_filter, char **origin_filter, |
| 519 | int origin_filter_count, int negated_origin_filter, uint16_t max_depth, int with_origin, |
| 520 | NC_WD_MODE wd_mode, NC_PARAMTYPE paramtype) |
| 521 | { |
| 522 | struct nc_rpc_getdata *rpc = NULL; |
| 523 | int i; |
| 524 | |
| 525 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 526 | ERR("Filter is neither an XML subtree nor an XPath expression (invalid first char '%c').", filter[0]); |
| 527 | return NULL; |
| 528 | } else if (!datastore) { |
| 529 | ERRARG("datastore"); |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | rpc = calloc(1, sizeof *rpc); |
| 534 | if (!rpc) { |
| 535 | ERRMEM; |
| 536 | return NULL; |
| 537 | } |
| 538 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 539 | |
| 540 | rpc->type = NC_RPC_GETDATA; |
| 541 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 542 | rpc->datastore = strdup(datastore); |
| 543 | } else { |
| 544 | rpc->datastore = (char *)datastore; |
| 545 | } |
| 546 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 547 | rpc->filter = strdup(filter); |
| 548 | } else { |
| 549 | rpc->filter = (char *)filter; |
| 550 | } |
| 551 | if (config_filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 552 | rpc->config_filter = strdup(config_filter); |
| 553 | } else { |
| 554 | rpc->config_filter = (char *)config_filter; |
| 555 | } |
| 556 | if (origin_filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 557 | rpc->origin_filter = malloc(origin_filter_count * sizeof *rpc->origin_filter); |
| 558 | if (!rpc->origin_filter) { |
| 559 | ERRMEM; |
| 560 | goto error; |
| 561 | } |
| 562 | for (i = 0; i < origin_filter_count; ++i) { |
| 563 | rpc->origin_filter[i] = strdup(origin_filter[i]); |
| 564 | if (!rpc->origin_filter[i]) { |
| 565 | ERRMEM; |
| 566 | goto error; |
| 567 | } |
| 568 | ++rpc->origin_filter_count; |
| 569 | } |
| 570 | } else { |
| 571 | rpc->origin_filter = origin_filter; |
| 572 | rpc->origin_filter_count = origin_filter_count; |
| 573 | } |
| 574 | rpc->negated_origin_filter = negated_origin_filter; |
| 575 | rpc->max_depth = max_depth; |
| 576 | rpc->with_origin = with_origin; |
| 577 | rpc->wd_mode = wd_mode; |
| 578 | |
| 579 | return (struct nc_rpc *)rpc; |
| 580 | |
| 581 | error: |
| 582 | nc_rpc_free((struct nc_rpc *)rpc); |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | API struct nc_rpc * |
| 587 | nc_rpc_editdata(const char *datastore, NC_RPC_EDIT_DFLTOP default_op, const char *edit_content, NC_PARAMTYPE paramtype) |
| 588 | { |
| 589 | struct nc_rpc_editdata *rpc; |
| 590 | |
| 591 | if (!datastore) { |
| 592 | ERRARG("datastore"); |
| 593 | return NULL; |
| 594 | } else if (!edit_content) { |
| 595 | ERRARG("edit_content"); |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | if (edit_content[0] && (edit_content[0] != '<') && !isalpha(edit_content[0])) { |
| 600 | ERR("<edit-data> content is neither a URL nor an XML config (invalid first char '%c').", edit_content[0]); |
| 601 | return NULL; |
| 602 | } |
| 603 | |
| 604 | rpc = malloc(sizeof *rpc); |
| 605 | if (!rpc) { |
| 606 | ERRMEM; |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | rpc->type = NC_RPC_EDITDATA; |
| 611 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 612 | rpc->datastore = strdup(datastore); |
| 613 | } else { |
| 614 | rpc->datastore = (char *)datastore; |
| 615 | } |
| 616 | rpc->default_op = default_op; |
| 617 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 618 | rpc->edit_cont = strdup(edit_content); |
| 619 | } else { |
| 620 | rpc->edit_cont = (char *)edit_content; |
| 621 | } |
| 622 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 623 | |
| 624 | return (struct nc_rpc *)rpc; |
| 625 | } |
| 626 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 627 | API void |
| 628 | nc_rpc_free(struct nc_rpc *rpc) |
| 629 | { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 630 | struct nc_rpc_act_generic *rpc_generic; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 631 | struct nc_rpc_getconfig *rpc_getconfig; |
| 632 | struct nc_rpc_edit *rpc_edit; |
| 633 | struct nc_rpc_copy *rpc_copy; |
| 634 | struct nc_rpc_delete *rpc_delete; |
| 635 | struct nc_rpc_get *rpc_get; |
| 636 | struct nc_rpc_commit *rpc_commit; |
| 637 | struct nc_rpc_cancel *rpc_cancel; |
| 638 | struct nc_rpc_validate *rpc_validate; |
| 639 | struct nc_rpc_getschema *rpc_getschema; |
| 640 | struct nc_rpc_subscribe *rpc_subscribe; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 641 | struct nc_rpc_getdata *rpc_getdata; |
| 642 | struct nc_rpc_editdata *rpc_editdata; |
| 643 | int i; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 644 | |
| 645 | if (!rpc) { |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 650 | case NC_RPC_ACT_GENERIC: |
| 651 | rpc_generic = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 652 | if (rpc_generic->free) { |
| 653 | if (rpc_generic->has_data) { |
| 654 | lyd_free(rpc_generic->content.data); |
| 655 | } else { |
| 656 | free(rpc_generic->content.xml_str); |
| 657 | } |
| 658 | } |
| 659 | break; |
| 660 | case NC_RPC_GETCONFIG: |
| 661 | rpc_getconfig = (struct nc_rpc_getconfig *)rpc; |
| 662 | if (rpc_getconfig->free) { |
| 663 | free(rpc_getconfig->filter); |
| 664 | } |
| 665 | break; |
| 666 | case NC_RPC_EDIT: |
| 667 | rpc_edit = (struct nc_rpc_edit *)rpc; |
| 668 | if (rpc_edit->free) { |
| 669 | free(rpc_edit->edit_cont); |
| 670 | } |
| 671 | break; |
| 672 | case NC_RPC_COPY: |
| 673 | rpc_copy = (struct nc_rpc_copy *)rpc; |
| 674 | if (rpc_copy->free) { |
| 675 | free(rpc_copy->url_config_src); |
David Sedlák | 35419a3 | 2018-10-07 23:23:33 +0200 | [diff] [blame] | 676 | free(rpc_copy->url_trg); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 677 | } |
| 678 | break; |
| 679 | case NC_RPC_DELETE: |
| 680 | rpc_delete = (struct nc_rpc_delete *)rpc; |
| 681 | if (rpc_delete->free) { |
| 682 | free(rpc_delete->url); |
| 683 | } |
| 684 | break; |
| 685 | case NC_RPC_GET: |
| 686 | rpc_get = (struct nc_rpc_get *)rpc; |
| 687 | if (rpc_get->free) { |
| 688 | free(rpc_get->filter); |
| 689 | } |
| 690 | break; |
| 691 | case NC_RPC_COMMIT: |
| 692 | rpc_commit = (struct nc_rpc_commit *)rpc; |
| 693 | if (rpc_commit->free) { |
| 694 | free(rpc_commit->persist); |
| 695 | free(rpc_commit->persist_id); |
| 696 | } |
| 697 | break; |
| 698 | case NC_RPC_CANCEL: |
| 699 | rpc_cancel = (struct nc_rpc_cancel *)rpc; |
| 700 | if (rpc_cancel->free) { |
| 701 | free(rpc_cancel->persist_id); |
| 702 | } |
| 703 | break; |
| 704 | case NC_RPC_VALIDATE: |
| 705 | rpc_validate = (struct nc_rpc_validate *)rpc; |
| 706 | if (rpc_validate->free) { |
| 707 | free(rpc_validate->url_config_src); |
| 708 | } |
| 709 | break; |
| 710 | case NC_RPC_GETSCHEMA: |
| 711 | rpc_getschema = (struct nc_rpc_getschema *)rpc; |
| 712 | if (rpc_getschema->free) { |
| 713 | free(rpc_getschema->identifier); |
| 714 | free(rpc_getschema->version); |
| 715 | free(rpc_getschema->format); |
| 716 | } |
| 717 | break; |
| 718 | case NC_RPC_SUBSCRIBE: |
| 719 | rpc_subscribe = (struct nc_rpc_subscribe *)rpc; |
| 720 | if (rpc_subscribe->free) { |
| 721 | free(rpc_subscribe->stream); |
| 722 | free(rpc_subscribe->filter); |
| 723 | free(rpc_subscribe->start); |
| 724 | free(rpc_subscribe->stop); |
| 725 | } |
| 726 | break; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 727 | case NC_RPC_GETDATA: |
| 728 | rpc_getdata = (struct nc_rpc_getdata *)rpc; |
| 729 | if (rpc_getdata->free) { |
| 730 | free(rpc_getdata->datastore); |
| 731 | free(rpc_getdata->filter); |
| 732 | free(rpc_getdata->config_filter); |
| 733 | for (i = 0; i < rpc_getdata->origin_filter_count; ++i) { |
| 734 | free(rpc_getdata->origin_filter[i]); |
| 735 | } |
| 736 | free(rpc_getdata->origin_filter); |
| 737 | } |
| 738 | break; |
| 739 | case NC_RPC_EDITDATA: |
| 740 | rpc_editdata = (struct nc_rpc_editdata *)rpc; |
| 741 | if (rpc_editdata->free) { |
| 742 | free(rpc_editdata->datastore); |
| 743 | free(rpc_editdata->edit_cont); |
| 744 | } |
| 745 | break; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 746 | default: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 747 | /* nothing special needed */ |
| 748 | break; |
| 749 | } |
| 750 | |
| 751 | free(rpc); |
| 752 | } |
| 753 | |
| 754 | API void |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 755 | nc_client_err_clean(struct nc_err *err, struct ly_ctx *ctx) |
| 756 | { |
| 757 | int i; |
| 758 | |
| 759 | assert(ctx); |
| 760 | |
| 761 | if (!err) { |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | lydict_remove(ctx, err->type); |
| 766 | lydict_remove(ctx, err->tag); |
| 767 | lydict_remove(ctx, err->severity); |
| 768 | lydict_remove(ctx, err->apptag); |
| 769 | lydict_remove(ctx, err->path); |
| 770 | lydict_remove(ctx, err->message); |
| 771 | lydict_remove(ctx, err->message_lang); |
| 772 | lydict_remove(ctx, err->sid); |
| 773 | for (i = 0; i < err->attr_count; ++i) { |
| 774 | lydict_remove(ctx, err->attr[i]); |
| 775 | } |
| 776 | free(err->attr); |
| 777 | for (i = 0; i < err->elem_count; ++i) { |
| 778 | lydict_remove(ctx, err->elem[i]); |
| 779 | } |
| 780 | free(err->elem); |
| 781 | for (i = 0; i < err->ns_count; ++i) { |
| 782 | lydict_remove(ctx, err->ns[i]); |
| 783 | } |
| 784 | free(err->ns); |
| 785 | for (i = 0; i < err->other_count; ++i) { |
| 786 | lyxml_free(ctx, err->other[i]); |
| 787 | } |
| 788 | free(err->other); |
| 789 | } |
| 790 | |
| 791 | API void |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 792 | nc_reply_free(struct nc_reply *reply) |
| 793 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 794 | struct nc_client_reply_error *error; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 795 | struct nc_reply_data *data; |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 796 | uint32_t i; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 797 | |
| 798 | if (!reply) { |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | switch (reply->type) { |
| 803 | case NC_RPL_DATA: |
| 804 | data = (struct nc_reply_data *)reply; |
| 805 | lyd_free_withsiblings(data->data); |
| 806 | break; |
| 807 | |
| 808 | case NC_RPL_OK: |
| 809 | /* nothing to free */ |
| 810 | break; |
| 811 | |
| 812 | case NC_RPL_ERROR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 813 | error = (struct nc_client_reply_error *)reply; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 814 | for (i = 0; i < error->count; ++i) { |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 815 | nc_client_err_clean(&error->err[i], error->ctx); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 816 | } |
| 817 | free(error->err); |
| 818 | break; |
| 819 | |
| 820 | case NC_RPL_NOTIF: |
| 821 | nc_notif_free((struct nc_notif *)reply); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 822 | return; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | free(reply); |
| 826 | } |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 827 | |
| 828 | API void |
| 829 | nc_notif_free(struct nc_notif *notif) |
| 830 | { |
| 831 | if (!notif) { |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | lydict_remove(notif->tree->schema->module->ctx, notif->datetime); |
| 836 | lyd_free(notif->tree); |
| 837 | free(notif); |
| 838 | } |