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 | |
| 517 | API void |
| 518 | nc_rpc_free(struct nc_rpc *rpc) |
| 519 | { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 520 | struct nc_rpc_act_generic *rpc_generic; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 521 | struct nc_rpc_getconfig *rpc_getconfig; |
| 522 | struct nc_rpc_edit *rpc_edit; |
| 523 | struct nc_rpc_copy *rpc_copy; |
| 524 | struct nc_rpc_delete *rpc_delete; |
| 525 | struct nc_rpc_get *rpc_get; |
| 526 | struct nc_rpc_commit *rpc_commit; |
| 527 | struct nc_rpc_cancel *rpc_cancel; |
| 528 | struct nc_rpc_validate *rpc_validate; |
| 529 | struct nc_rpc_getschema *rpc_getschema; |
| 530 | struct nc_rpc_subscribe *rpc_subscribe; |
| 531 | |
| 532 | if (!rpc) { |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 537 | case NC_RPC_ACT_GENERIC: |
| 538 | rpc_generic = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 539 | if (rpc_generic->free) { |
| 540 | if (rpc_generic->has_data) { |
| 541 | lyd_free(rpc_generic->content.data); |
| 542 | } else { |
| 543 | free(rpc_generic->content.xml_str); |
| 544 | } |
| 545 | } |
| 546 | break; |
| 547 | case NC_RPC_GETCONFIG: |
| 548 | rpc_getconfig = (struct nc_rpc_getconfig *)rpc; |
| 549 | if (rpc_getconfig->free) { |
| 550 | free(rpc_getconfig->filter); |
| 551 | } |
| 552 | break; |
| 553 | case NC_RPC_EDIT: |
| 554 | rpc_edit = (struct nc_rpc_edit *)rpc; |
| 555 | if (rpc_edit->free) { |
| 556 | free(rpc_edit->edit_cont); |
| 557 | } |
| 558 | break; |
| 559 | case NC_RPC_COPY: |
| 560 | rpc_copy = (struct nc_rpc_copy *)rpc; |
| 561 | if (rpc_copy->free) { |
| 562 | free(rpc_copy->url_config_src); |
David Sedlák | 35419a3 | 2018-10-07 23:23:33 +0200 | [diff] [blame] | 563 | free(rpc_copy->url_trg); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 564 | } |
| 565 | break; |
| 566 | case NC_RPC_DELETE: |
| 567 | rpc_delete = (struct nc_rpc_delete *)rpc; |
| 568 | if (rpc_delete->free) { |
| 569 | free(rpc_delete->url); |
| 570 | } |
| 571 | break; |
| 572 | case NC_RPC_GET: |
| 573 | rpc_get = (struct nc_rpc_get *)rpc; |
| 574 | if (rpc_get->free) { |
| 575 | free(rpc_get->filter); |
| 576 | } |
| 577 | break; |
| 578 | case NC_RPC_COMMIT: |
| 579 | rpc_commit = (struct nc_rpc_commit *)rpc; |
| 580 | if (rpc_commit->free) { |
| 581 | free(rpc_commit->persist); |
| 582 | free(rpc_commit->persist_id); |
| 583 | } |
| 584 | break; |
| 585 | case NC_RPC_CANCEL: |
| 586 | rpc_cancel = (struct nc_rpc_cancel *)rpc; |
| 587 | if (rpc_cancel->free) { |
| 588 | free(rpc_cancel->persist_id); |
| 589 | } |
| 590 | break; |
| 591 | case NC_RPC_VALIDATE: |
| 592 | rpc_validate = (struct nc_rpc_validate *)rpc; |
| 593 | if (rpc_validate->free) { |
| 594 | free(rpc_validate->url_config_src); |
| 595 | } |
| 596 | break; |
| 597 | case NC_RPC_GETSCHEMA: |
| 598 | rpc_getschema = (struct nc_rpc_getschema *)rpc; |
| 599 | if (rpc_getschema->free) { |
| 600 | free(rpc_getschema->identifier); |
| 601 | free(rpc_getschema->version); |
| 602 | free(rpc_getschema->format); |
| 603 | } |
| 604 | break; |
| 605 | case NC_RPC_SUBSCRIBE: |
| 606 | rpc_subscribe = (struct nc_rpc_subscribe *)rpc; |
| 607 | if (rpc_subscribe->free) { |
| 608 | free(rpc_subscribe->stream); |
| 609 | free(rpc_subscribe->filter); |
| 610 | free(rpc_subscribe->start); |
| 611 | free(rpc_subscribe->stop); |
| 612 | } |
| 613 | break; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 614 | default: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 615 | /* nothing special needed */ |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | free(rpc); |
| 620 | } |
| 621 | |
| 622 | API void |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 623 | nc_client_err_clean(struct nc_err *err, struct ly_ctx *ctx) |
| 624 | { |
| 625 | int i; |
| 626 | |
| 627 | assert(ctx); |
| 628 | |
| 629 | if (!err) { |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | lydict_remove(ctx, err->type); |
| 634 | lydict_remove(ctx, err->tag); |
| 635 | lydict_remove(ctx, err->severity); |
| 636 | lydict_remove(ctx, err->apptag); |
| 637 | lydict_remove(ctx, err->path); |
| 638 | lydict_remove(ctx, err->message); |
| 639 | lydict_remove(ctx, err->message_lang); |
| 640 | lydict_remove(ctx, err->sid); |
| 641 | for (i = 0; i < err->attr_count; ++i) { |
| 642 | lydict_remove(ctx, err->attr[i]); |
| 643 | } |
| 644 | free(err->attr); |
| 645 | for (i = 0; i < err->elem_count; ++i) { |
| 646 | lydict_remove(ctx, err->elem[i]); |
| 647 | } |
| 648 | free(err->elem); |
| 649 | for (i = 0; i < err->ns_count; ++i) { |
| 650 | lydict_remove(ctx, err->ns[i]); |
| 651 | } |
| 652 | free(err->ns); |
| 653 | for (i = 0; i < err->other_count; ++i) { |
| 654 | lyxml_free(ctx, err->other[i]); |
| 655 | } |
| 656 | free(err->other); |
| 657 | } |
| 658 | |
| 659 | API void |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 660 | nc_reply_free(struct nc_reply *reply) |
| 661 | { |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 662 | struct nc_client_reply_error *error; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 663 | struct nc_reply_data *data; |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 664 | uint32_t i; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 665 | |
| 666 | if (!reply) { |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | switch (reply->type) { |
| 671 | case NC_RPL_DATA: |
| 672 | data = (struct nc_reply_data *)reply; |
| 673 | lyd_free_withsiblings(data->data); |
| 674 | break; |
| 675 | |
| 676 | case NC_RPL_OK: |
| 677 | /* nothing to free */ |
| 678 | break; |
| 679 | |
| 680 | case NC_RPL_ERROR: |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 681 | error = (struct nc_client_reply_error *)reply; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 682 | for (i = 0; i < error->count; ++i) { |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 683 | nc_client_err_clean(&error->err[i], error->ctx); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 684 | } |
| 685 | free(error->err); |
| 686 | break; |
| 687 | |
| 688 | case NC_RPL_NOTIF: |
| 689 | nc_notif_free((struct nc_notif *)reply); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 690 | return; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | free(reply); |
| 694 | } |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 695 | |
| 696 | API void |
| 697 | nc_notif_free(struct nc_notif *notif) |
| 698 | { |
| 699 | if (!notif) { |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | lydict_remove(notif->tree->schema->module->ctx, notif->datetime); |
| 704 | lyd_free(notif->tree); |
| 705 | free(notif); |
| 706 | } |