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 | * |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 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> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 17 | #include <stdarg.h> |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 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) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 59 | if (lyd_dup_single(data, NULL, LYD_DUP_RECURSIVE, &rpc->content.data)) { |
| 60 | free(rpc); |
| 61 | return NULL; |
| 62 | } |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 63 | } else { |
| 64 | rpc->content.data = (struct lyd_node *)data; |
| 65 | } |
| 66 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 67 | |
| 68 | return (struct nc_rpc *)rpc; |
| 69 | } |
| 70 | |
| 71 | API struct nc_rpc * |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 72 | nc_rpc_act_generic_xml(const char *xml_str, NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 73 | { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 74 | struct nc_rpc_act_generic *rpc; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 75 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 76 | if (!xml_str) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 77 | ERRARG("xml_str"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 81 | rpc = malloc(sizeof *rpc); |
| 82 | if (!rpc) { |
| 83 | ERRMEM; |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 87 | rpc->type = NC_RPC_ACT_GENERIC; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 88 | rpc->has_data = 0; |
| 89 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 90 | rpc->content.xml_str = strdup(xml_str); |
| 91 | } else { |
| 92 | rpc->content.xml_str = (char *)xml_str; |
| 93 | } |
| 94 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 95 | |
| 96 | return (struct nc_rpc *)rpc; |
| 97 | } |
| 98 | |
| 99 | API struct nc_rpc * |
| 100 | nc_rpc_getconfig(NC_DATASTORE source, const char *filter, NC_WD_MODE wd_mode, NC_PARAMTYPE paramtype) |
| 101 | { |
| 102 | struct nc_rpc_getconfig *rpc; |
| 103 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 104 | if (!source) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 105 | ERRARG("source"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
| 108 | |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 109 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 110 | 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] | 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | rpc = malloc(sizeof *rpc); |
| 115 | if (!rpc) { |
| 116 | ERRMEM; |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | rpc->type = NC_RPC_GETCONFIG; |
| 121 | rpc->source = source; |
| 122 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 123 | rpc->filter = strdup(filter); |
| 124 | } else { |
| 125 | rpc->filter = (char *)filter; |
| 126 | } |
| 127 | rpc->wd_mode = wd_mode; |
| 128 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 129 | |
| 130 | return (struct nc_rpc *)rpc; |
| 131 | } |
| 132 | |
| 133 | API struct nc_rpc * |
| 134 | nc_rpc_edit(NC_DATASTORE target, NC_RPC_EDIT_DFLTOP default_op, NC_RPC_EDIT_TESTOPT test_opt, |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 135 | NC_RPC_EDIT_ERROPT error_opt, const char *edit_content, NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 136 | { |
| 137 | struct nc_rpc_edit *rpc; |
| 138 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 139 | if (!target) { |
| 140 | ERRARG("target"); |
| 141 | return NULL; |
| 142 | } else if (!edit_content) { |
| 143 | ERRARG("edit_content"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 147 | if (edit_content[0] && (edit_content[0] != '<') && !isalpha(edit_content[0])) { |
| 148 | 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] | 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | rpc = malloc(sizeof *rpc); |
| 153 | if (!rpc) { |
| 154 | ERRMEM; |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | rpc->type = NC_RPC_EDIT; |
| 159 | rpc->target = target; |
| 160 | rpc->default_op = default_op; |
| 161 | rpc->test_opt = test_opt; |
| 162 | rpc->error_opt = error_opt; |
| 163 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 164 | rpc->edit_cont = strdup(edit_content); |
| 165 | } else { |
| 166 | rpc->edit_cont = (char *)edit_content; |
| 167 | } |
| 168 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 169 | |
| 170 | return (struct nc_rpc *)rpc; |
| 171 | } |
| 172 | |
| 173 | API struct nc_rpc * |
| 174 | nc_rpc_copy(NC_DATASTORE target, const char *url_trg, NC_DATASTORE source, const char *url_or_config_src, |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 175 | NC_WD_MODE wd_mode, NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 176 | { |
| 177 | struct nc_rpc_copy *rpc; |
| 178 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 179 | if (!target) { |
| 180 | ERRARG("target"); |
| 181 | return NULL; |
| 182 | } else if (!source) { |
| 183 | ERRARG("source"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 184 | return NULL; |
| 185 | } |
| 186 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 187 | 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] | 188 | 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] | 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | rpc = malloc(sizeof *rpc); |
| 193 | if (!rpc) { |
| 194 | ERRMEM; |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | rpc->type = NC_RPC_COPY; |
| 199 | rpc->target = target; |
| 200 | if (url_trg && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 201 | rpc->url_trg = strdup(url_trg); |
| 202 | } else { |
| 203 | rpc->url_trg = (char *)url_trg; |
| 204 | } |
| 205 | rpc->source = source; |
| 206 | if (url_or_config_src && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 207 | rpc->url_config_src = strdup(url_or_config_src); |
| 208 | } else { |
| 209 | rpc->url_config_src = (char *)url_or_config_src; |
| 210 | } |
| 211 | rpc->wd_mode = wd_mode; |
| 212 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 213 | |
| 214 | return (struct nc_rpc *)rpc; |
| 215 | } |
| 216 | |
| 217 | API struct nc_rpc * |
| 218 | nc_rpc_delete(NC_DATASTORE target, const char *url, NC_PARAMTYPE paramtype) |
| 219 | { |
| 220 | struct nc_rpc_delete *rpc; |
| 221 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 222 | if (!target) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 223 | ERRARG("target"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 224 | return NULL; |
| 225 | } |
| 226 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 227 | rpc = malloc(sizeof *rpc); |
| 228 | if (!rpc) { |
| 229 | ERRMEM; |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | rpc->type = NC_RPC_DELETE; |
| 234 | rpc->target = target; |
| 235 | if (url && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 236 | rpc->url = strdup(url); |
| 237 | } else { |
| 238 | rpc->url = (char *)url; |
| 239 | } |
| 240 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 241 | |
| 242 | return (struct nc_rpc *)rpc; |
| 243 | } |
| 244 | |
| 245 | API struct nc_rpc * |
| 246 | nc_rpc_lock(NC_DATASTORE target) |
| 247 | { |
| 248 | struct nc_rpc_lock *rpc; |
| 249 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 250 | if (!target) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 251 | ERRARG("target"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 252 | return NULL; |
| 253 | } |
| 254 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 255 | rpc = malloc(sizeof *rpc); |
| 256 | if (!rpc) { |
| 257 | ERRMEM; |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | rpc->type = NC_RPC_LOCK; |
| 262 | rpc->target = target; |
| 263 | |
| 264 | return (struct nc_rpc *)rpc; |
| 265 | } |
| 266 | |
| 267 | API struct nc_rpc * |
| 268 | nc_rpc_unlock(NC_DATASTORE target) |
| 269 | { |
| 270 | struct nc_rpc_lock *rpc; |
| 271 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 272 | if (!target) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 273 | ERRARG("target"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 274 | return NULL; |
| 275 | } |
| 276 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 277 | rpc = malloc(sizeof *rpc); |
| 278 | if (!rpc) { |
| 279 | ERRMEM; |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | rpc->type = NC_RPC_UNLOCK; |
| 284 | rpc->target = target; |
| 285 | |
| 286 | return (struct nc_rpc *)rpc; |
| 287 | } |
| 288 | |
| 289 | API struct nc_rpc * |
| 290 | nc_rpc_get(const char *filter, NC_WD_MODE wd_mode, NC_PARAMTYPE paramtype) |
| 291 | { |
| 292 | struct nc_rpc_get *rpc; |
| 293 | |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 294 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 295 | 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] | 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | rpc = malloc(sizeof *rpc); |
| 300 | if (!rpc) { |
| 301 | ERRMEM; |
| 302 | return NULL; |
| 303 | } |
| 304 | |
| 305 | rpc->type = NC_RPC_GET; |
| 306 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 307 | rpc->filter = strdup(filter); |
| 308 | } else { |
| 309 | rpc->filter = (char *)filter; |
| 310 | } |
| 311 | rpc->wd_mode = wd_mode; |
| 312 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 313 | |
| 314 | return (struct nc_rpc *)rpc; |
| 315 | } |
| 316 | |
| 317 | API struct nc_rpc * |
| 318 | nc_rpc_kill(uint32_t session_id) |
| 319 | { |
| 320 | struct nc_rpc_kill *rpc; |
| 321 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 322 | if (!session_id) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 323 | ERRARG("session_id"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 324 | return NULL; |
| 325 | } |
| 326 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 327 | rpc = malloc(sizeof *rpc); |
| 328 | if (!rpc) { |
| 329 | ERRMEM; |
| 330 | return NULL; |
| 331 | } |
| 332 | |
| 333 | rpc->type = NC_RPC_KILL; |
| 334 | rpc->sid = session_id; |
| 335 | |
| 336 | return (struct nc_rpc *)rpc; |
| 337 | } |
| 338 | |
| 339 | API struct nc_rpc * |
| 340 | nc_rpc_commit(int confirmed, uint32_t confirm_timeout, const char *persist, const char *persist_id, |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 341 | NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 342 | { |
| 343 | struct nc_rpc_commit *rpc; |
| 344 | |
| 345 | rpc = malloc(sizeof *rpc); |
| 346 | if (!rpc) { |
| 347 | ERRMEM; |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | rpc->type = NC_RPC_COMMIT; |
| 352 | rpc->confirmed = confirmed; |
| 353 | rpc->confirm_timeout = confirm_timeout; |
| 354 | if (persist && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 355 | rpc->persist = strdup(persist); |
| 356 | } else { |
| 357 | rpc->persist = (char *)persist; |
| 358 | } |
| 359 | if (persist_id && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 360 | rpc->persist_id = strdup(persist_id); |
| 361 | } else { |
| 362 | rpc->persist_id = (char *)persist_id; |
| 363 | } |
| 364 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 365 | |
| 366 | return (struct nc_rpc *)rpc; |
| 367 | } |
| 368 | |
| 369 | API struct nc_rpc * |
| 370 | nc_rpc_discard(void) |
| 371 | { |
| 372 | struct nc_rpc *rpc; |
| 373 | |
| 374 | rpc = malloc(sizeof *rpc); |
| 375 | if (!rpc) { |
| 376 | ERRMEM; |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | rpc->type = NC_RPC_DISCARD; |
| 381 | |
| 382 | return rpc; |
| 383 | } |
| 384 | |
| 385 | API struct nc_rpc * |
| 386 | nc_rpc_cancel(const char *persist_id, NC_PARAMTYPE paramtype) |
| 387 | { |
| 388 | struct nc_rpc_cancel *rpc; |
| 389 | |
| 390 | rpc = malloc(sizeof *rpc); |
| 391 | if (!rpc) { |
| 392 | ERRMEM; |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | rpc->type = NC_RPC_CANCEL; |
| 397 | if (persist_id && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 398 | rpc->persist_id = strdup(persist_id); |
| 399 | } else { |
| 400 | rpc->persist_id = (char *)persist_id; |
| 401 | } |
| 402 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 403 | |
| 404 | return (struct nc_rpc *)rpc; |
| 405 | } |
| 406 | |
| 407 | API struct nc_rpc * |
| 408 | nc_rpc_validate(NC_DATASTORE source, const char *url_or_config, NC_PARAMTYPE paramtype) |
| 409 | { |
| 410 | struct nc_rpc_validate *rpc; |
| 411 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 412 | if (!source) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 413 | ERRARG("source"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 414 | return NULL; |
| 415 | } |
| 416 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 417 | 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] | 418 | 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] | 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | rpc = malloc(sizeof *rpc); |
| 423 | if (!rpc) { |
| 424 | ERRMEM; |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | rpc->type = NC_RPC_VALIDATE; |
| 429 | rpc->source = source; |
| 430 | if (url_or_config && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 431 | rpc->url_config_src = strdup(url_or_config); |
| 432 | } else { |
| 433 | rpc->url_config_src = (char *)url_or_config; |
| 434 | } |
| 435 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 436 | |
| 437 | return (struct nc_rpc *)rpc; |
| 438 | } |
| 439 | |
| 440 | API struct nc_rpc * |
| 441 | nc_rpc_getschema(const char *identifier, const char *version, const char *format, NC_PARAMTYPE paramtype) |
| 442 | { |
| 443 | struct nc_rpc_getschema *rpc; |
| 444 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 445 | if (!identifier) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 446 | ERRARG("identifier"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
| 449 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 450 | rpc = malloc(sizeof *rpc); |
| 451 | if (!rpc) { |
| 452 | ERRMEM; |
| 453 | return NULL; |
| 454 | } |
| 455 | |
| 456 | rpc->type = NC_RPC_GETSCHEMA; |
| 457 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 458 | rpc->identifier = strdup(identifier); |
| 459 | } else { |
| 460 | rpc->identifier = (char *)identifier; |
| 461 | } |
| 462 | if (version && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 463 | rpc->version = strdup(version); |
| 464 | } else { |
| 465 | rpc->version = (char *)version; |
| 466 | } |
| 467 | if (format && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 468 | rpc->format = strdup(format); |
| 469 | } else { |
| 470 | rpc->format = (char *)format; |
| 471 | } |
| 472 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 473 | |
| 474 | return (struct nc_rpc *)rpc; |
| 475 | } |
| 476 | |
| 477 | API struct nc_rpc * |
| 478 | nc_rpc_subscribe(const char *stream_name, const char *filter, const char *start_time, const char *stop_time, |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 479 | NC_PARAMTYPE paramtype) |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 480 | { |
| 481 | struct nc_rpc_subscribe *rpc; |
| 482 | |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 483 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 484 | 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] | 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | rpc = malloc(sizeof *rpc); |
| 489 | if (!rpc) { |
| 490 | ERRMEM; |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | rpc->type = NC_RPC_SUBSCRIBE; |
| 495 | if (stream_name && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 496 | rpc->stream = strdup(stream_name); |
| 497 | } else { |
| 498 | rpc->stream = (char *)stream_name; |
| 499 | } |
| 500 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 501 | rpc->filter = strdup(filter); |
| 502 | } else { |
| 503 | rpc->filter = (char *)filter; |
| 504 | } |
| 505 | if (start_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 506 | rpc->start = strdup(start_time); |
| 507 | } else { |
| 508 | rpc->start = (char *)start_time; |
| 509 | } |
| 510 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 511 | rpc->stop = strdup(stop_time); |
| 512 | } else { |
| 513 | rpc->stop = (char *)stop_time; |
| 514 | } |
| 515 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 516 | |
| 517 | return (struct nc_rpc *)rpc; |
| 518 | } |
| 519 | |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 520 | API struct nc_rpc * |
| 521 | nc_rpc_getdata(const char *datastore, const char *filter, const char *config_filter, char **origin_filter, |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 522 | int origin_filter_count, int negated_origin_filter, uint16_t max_depth, int with_origin, NC_WD_MODE wd_mode, |
| 523 | NC_PARAMTYPE paramtype) |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 524 | { |
| 525 | struct nc_rpc_getdata *rpc = NULL; |
| 526 | int i; |
| 527 | |
| 528 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 529 | ERR("Filter is neither an XML subtree nor an XPath expression (invalid first char '%c').", filter[0]); |
| 530 | return NULL; |
| 531 | } else if (!datastore) { |
| 532 | ERRARG("datastore"); |
| 533 | return NULL; |
| 534 | } |
| 535 | |
| 536 | rpc = calloc(1, sizeof *rpc); |
| 537 | if (!rpc) { |
| 538 | ERRMEM; |
| 539 | return NULL; |
| 540 | } |
| 541 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 542 | |
| 543 | rpc->type = NC_RPC_GETDATA; |
| 544 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 545 | rpc->datastore = strdup(datastore); |
| 546 | } else { |
| 547 | rpc->datastore = (char *)datastore; |
| 548 | } |
| 549 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 550 | rpc->filter = strdup(filter); |
| 551 | } else { |
| 552 | rpc->filter = (char *)filter; |
| 553 | } |
| 554 | if (config_filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 555 | rpc->config_filter = strdup(config_filter); |
| 556 | } else { |
| 557 | rpc->config_filter = (char *)config_filter; |
| 558 | } |
| 559 | if (origin_filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 560 | rpc->origin_filter = malloc(origin_filter_count * sizeof *rpc->origin_filter); |
| 561 | if (!rpc->origin_filter) { |
| 562 | ERRMEM; |
| 563 | goto error; |
| 564 | } |
| 565 | for (i = 0; i < origin_filter_count; ++i) { |
| 566 | rpc->origin_filter[i] = strdup(origin_filter[i]); |
| 567 | if (!rpc->origin_filter[i]) { |
| 568 | ERRMEM; |
| 569 | goto error; |
| 570 | } |
| 571 | ++rpc->origin_filter_count; |
| 572 | } |
| 573 | } else { |
| 574 | rpc->origin_filter = origin_filter; |
| 575 | rpc->origin_filter_count = origin_filter_count; |
| 576 | } |
| 577 | rpc->negated_origin_filter = negated_origin_filter; |
| 578 | rpc->max_depth = max_depth; |
| 579 | rpc->with_origin = with_origin; |
| 580 | rpc->wd_mode = wd_mode; |
| 581 | |
| 582 | return (struct nc_rpc *)rpc; |
| 583 | |
| 584 | error: |
| 585 | nc_rpc_free((struct nc_rpc *)rpc); |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | API struct nc_rpc * |
| 590 | nc_rpc_editdata(const char *datastore, NC_RPC_EDIT_DFLTOP default_op, const char *edit_content, NC_PARAMTYPE paramtype) |
| 591 | { |
| 592 | struct nc_rpc_editdata *rpc; |
| 593 | |
| 594 | if (!datastore) { |
| 595 | ERRARG("datastore"); |
| 596 | return NULL; |
| 597 | } else if (!edit_content) { |
| 598 | ERRARG("edit_content"); |
| 599 | return NULL; |
| 600 | } |
| 601 | |
| 602 | if (edit_content[0] && (edit_content[0] != '<') && !isalpha(edit_content[0])) { |
| 603 | ERR("<edit-data> content is neither a URL nor an XML config (invalid first char '%c').", edit_content[0]); |
| 604 | return NULL; |
| 605 | } |
| 606 | |
| 607 | rpc = malloc(sizeof *rpc); |
| 608 | if (!rpc) { |
| 609 | ERRMEM; |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | rpc->type = NC_RPC_EDITDATA; |
| 614 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 615 | rpc->datastore = strdup(datastore); |
| 616 | } else { |
| 617 | rpc->datastore = (char *)datastore; |
| 618 | } |
| 619 | rpc->default_op = default_op; |
| 620 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 621 | rpc->edit_cont = strdup(edit_content); |
| 622 | } else { |
| 623 | rpc->edit_cont = (char *)edit_content; |
| 624 | } |
| 625 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 626 | |
| 627 | return (struct nc_rpc *)rpc; |
| 628 | } |
| 629 | |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 630 | API struct nc_rpc * |
| 631 | nc_rpc_establishsub(const char *filter, const char *stream_name, const char *start_time, |
| 632 | const char *stop_time, const char *encoding, NC_PARAMTYPE paramtype) |
| 633 | { |
| 634 | struct nc_rpc_establishsub *rpc; |
| 635 | |
| 636 | if (!stream_name) { |
| 637 | ERRARG("stream_name"); |
| 638 | return NULL; |
| 639 | } |
| 640 | |
| 641 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 642 | ERR("Filter is not an XML subtree, an XPath expression, not a filter reference (invalid first char '%c').", filter[0]); |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | rpc = malloc(sizeof *rpc); |
| 647 | if (!rpc) { |
| 648 | ERRMEM; |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | rpc->type = NC_RPC_ESTABLISHSUB; |
| 653 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 654 | rpc->filter = strdup(filter); |
| 655 | } else { |
| 656 | rpc->filter = (char *)filter; |
| 657 | } |
| 658 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 659 | rpc->stream = strdup(stream_name); |
| 660 | } else { |
| 661 | rpc->stream = (char *)stream_name; |
| 662 | } |
| 663 | if (start_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 664 | rpc->start = strdup(start_time); |
| 665 | } else { |
| 666 | rpc->start = (char *)start_time; |
| 667 | } |
| 668 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 669 | rpc->stop = strdup(stop_time); |
| 670 | } else { |
| 671 | rpc->stop = (char *)stop_time; |
| 672 | } |
| 673 | if (encoding && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 674 | rpc->encoding = strdup(encoding); |
| 675 | } else { |
| 676 | rpc->encoding = (char *)encoding; |
| 677 | } |
| 678 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 679 | |
| 680 | return (struct nc_rpc *)rpc; |
| 681 | } |
| 682 | |
| 683 | API struct nc_rpc * |
| 684 | nc_rpc_modifysub(uint32_t id, const char *filter, const char *stop_time, NC_PARAMTYPE paramtype) |
| 685 | { |
| 686 | struct nc_rpc_modifysub *rpc; |
| 687 | |
| 688 | if (!id) { |
| 689 | ERRARG("id"); |
| 690 | return NULL; |
| 691 | } |
| 692 | |
| 693 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 694 | ERR("Filter is not an XML subtree, an XPath expression, not a filter reference (invalid first char '%c').", filter[0]); |
| 695 | return NULL; |
| 696 | } |
| 697 | |
| 698 | rpc = malloc(sizeof *rpc); |
| 699 | if (!rpc) { |
| 700 | ERRMEM; |
| 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | rpc->type = NC_RPC_MODIFYSUB; |
| 705 | rpc->id = id; |
| 706 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 707 | rpc->filter = strdup(filter); |
| 708 | } else { |
| 709 | rpc->filter = (char *)filter; |
| 710 | } |
| 711 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 712 | rpc->stop = strdup(stop_time); |
| 713 | } else { |
| 714 | rpc->stop = (char *)stop_time; |
| 715 | } |
| 716 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 717 | |
| 718 | return (struct nc_rpc *)rpc; |
| 719 | } |
| 720 | |
| 721 | API struct nc_rpc * |
| 722 | nc_rpc_deletesub(uint32_t id) |
| 723 | { |
| 724 | struct nc_rpc_deletesub *rpc; |
| 725 | |
| 726 | if (!id) { |
| 727 | ERRARG("id"); |
| 728 | return NULL; |
| 729 | } |
| 730 | |
| 731 | rpc = malloc(sizeof *rpc); |
| 732 | if (!rpc) { |
| 733 | ERRMEM; |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | rpc->type = NC_RPC_DELETESUB; |
| 738 | rpc->id = id; |
| 739 | |
| 740 | return (struct nc_rpc *)rpc; |
| 741 | } |
| 742 | |
| 743 | API struct nc_rpc * |
| 744 | nc_rpc_killsub(uint32_t id) |
| 745 | { |
| 746 | struct nc_rpc_killsub *rpc; |
| 747 | |
| 748 | if (!id) { |
| 749 | ERRARG("id"); |
| 750 | return NULL; |
| 751 | } |
| 752 | |
| 753 | rpc = malloc(sizeof *rpc); |
| 754 | if (!rpc) { |
| 755 | ERRMEM; |
| 756 | return NULL; |
| 757 | } |
| 758 | |
| 759 | rpc->type = NC_RPC_KILLSUB; |
| 760 | rpc->id = id; |
| 761 | |
| 762 | return (struct nc_rpc *)rpc; |
| 763 | } |
| 764 | |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 765 | API struct nc_rpc * |
| 766 | nc_rpc_establishpush_periodic(const char *datastore, const char *filter, const char *stop_time, const char *encoding, |
| 767 | uint32_t period, const char *anchor_time, NC_PARAMTYPE paramtype) |
| 768 | { |
| 769 | struct nc_rpc_establishpush *rpc; |
| 770 | |
| 771 | if (!datastore) { |
| 772 | ERRARG("datastore"); |
| 773 | return NULL; |
| 774 | } else if (!period) { |
| 775 | ERRARG("period"); |
| 776 | return NULL; |
| 777 | } |
| 778 | |
| 779 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 780 | ERR("Filter is not an XML subtree, an XPath expression, not a filter reference (invalid first char '%c').", filter[0]); |
| 781 | return NULL; |
| 782 | } |
| 783 | |
| 784 | rpc = malloc(sizeof *rpc); |
| 785 | if (!rpc) { |
| 786 | ERRMEM; |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | rpc->type = NC_RPC_ESTABLISHPUSH; |
| 791 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 792 | rpc->datastore = strdup(datastore); |
| 793 | } else { |
| 794 | rpc->datastore = (char *)datastore; |
| 795 | } |
| 796 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 797 | rpc->filter = strdup(filter); |
| 798 | } else { |
| 799 | rpc->filter = (char *)filter; |
| 800 | } |
| 801 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 802 | rpc->stop = strdup(stop_time); |
| 803 | } else { |
| 804 | rpc->stop = (char *)stop_time; |
| 805 | } |
| 806 | if (encoding && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 807 | rpc->encoding = strdup(encoding); |
| 808 | } else { |
| 809 | rpc->encoding = (char *)encoding; |
| 810 | } |
| 811 | rpc->periodic = 1; |
| 812 | rpc->period = period; |
| 813 | if (anchor_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 814 | rpc->anchor_time = strdup(anchor_time); |
| 815 | } else { |
| 816 | rpc->anchor_time = (char *)anchor_time; |
| 817 | } |
| 818 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 819 | |
| 820 | return (struct nc_rpc *)rpc; |
| 821 | } |
| 822 | |
| 823 | API struct nc_rpc * |
| 824 | nc_rpc_establishpush_onchange(const char *datastore, const char *filter, const char *stop_time, const char *encoding, |
| 825 | uint32_t dampening_period, int sync_on_start, const char **excluded_change, NC_PARAMTYPE paramtype) |
| 826 | { |
| 827 | struct nc_rpc_establishpush *rpc; |
| 828 | uint32_t i; |
| 829 | |
| 830 | if (!datastore) { |
| 831 | ERRARG("datastore"); |
| 832 | return NULL; |
| 833 | } |
| 834 | |
| 835 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 836 | ERR("Filter is not an XML subtree, an XPath expression, not a filter reference (invalid first char '%c').", filter[0]); |
| 837 | return NULL; |
| 838 | } |
| 839 | |
| 840 | rpc = malloc(sizeof *rpc); |
| 841 | if (!rpc) { |
| 842 | ERRMEM; |
| 843 | return NULL; |
| 844 | } |
| 845 | |
| 846 | rpc->type = NC_RPC_ESTABLISHPUSH; |
| 847 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 848 | rpc->datastore = strdup(datastore); |
| 849 | } else { |
| 850 | rpc->datastore = (char *)datastore; |
| 851 | } |
| 852 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 853 | rpc->filter = strdup(filter); |
| 854 | } else { |
| 855 | rpc->filter = (char *)filter; |
| 856 | } |
| 857 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 858 | rpc->stop = strdup(stop_time); |
| 859 | } else { |
| 860 | rpc->stop = (char *)stop_time; |
| 861 | } |
| 862 | if (encoding && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 863 | rpc->encoding = strdup(encoding); |
| 864 | } else { |
| 865 | rpc->encoding = (char *)encoding; |
| 866 | } |
| 867 | rpc->periodic = 0; |
| 868 | rpc->dampening_period = dampening_period; |
| 869 | rpc->sync_on_start = sync_on_start; |
| 870 | if (excluded_change && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 871 | rpc->excluded_change = NULL; |
| 872 | for (i = 0; excluded_change[i]; ++i) { |
| 873 | rpc->excluded_change = realloc(rpc->excluded_change, (i + 2) * sizeof *rpc->excluded_change); |
| 874 | rpc->excluded_change[i] = strdup(excluded_change[i]); |
| 875 | rpc->excluded_change[i + 1] = NULL; |
| 876 | } |
| 877 | } else { |
| 878 | rpc->excluded_change = (char **)excluded_change; |
| 879 | } |
| 880 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 881 | |
| 882 | return (struct nc_rpc *)rpc; |
| 883 | } |
| 884 | |
| 885 | API struct nc_rpc * |
| 886 | nc_rpc_modifypush_periodic(uint32_t id, const char *datastore, const char *filter, const char *stop_time, uint32_t period, |
| 887 | const char *anchor_time, NC_PARAMTYPE paramtype) |
| 888 | { |
| 889 | struct nc_rpc_modifypush *rpc; |
| 890 | |
| 891 | if (!id) { |
| 892 | ERRARG("id"); |
| 893 | return NULL; |
| 894 | } else if (!datastore) { |
| 895 | ERRARG("datastore"); |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 900 | ERR("Filter is not an XML subtree, an XPath expression, not a filter reference (invalid first char '%c').", filter[0]); |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | rpc = malloc(sizeof *rpc); |
| 905 | if (!rpc) { |
| 906 | ERRMEM; |
| 907 | return NULL; |
| 908 | } |
| 909 | |
| 910 | rpc->type = NC_RPC_MODIFYPUSH; |
| 911 | rpc->id = id; |
| 912 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 913 | rpc->datastore = strdup(datastore); |
| 914 | } else { |
| 915 | rpc->datastore = (char *)datastore; |
| 916 | } |
| 917 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 918 | rpc->filter = strdup(filter); |
| 919 | } else { |
| 920 | rpc->filter = (char *)filter; |
| 921 | } |
| 922 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 923 | rpc->stop = strdup(stop_time); |
| 924 | } else { |
| 925 | rpc->stop = (char *)stop_time; |
| 926 | } |
| 927 | rpc->periodic = 1; |
| 928 | rpc->period = period; |
| 929 | if (anchor_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 930 | rpc->anchor_time = strdup(anchor_time); |
| 931 | } else { |
| 932 | rpc->anchor_time = (char *)anchor_time; |
| 933 | } |
| 934 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 935 | |
| 936 | return (struct nc_rpc *)rpc; |
| 937 | } |
| 938 | |
| 939 | API struct nc_rpc * |
| 940 | nc_rpc_modifypush_onchange(uint32_t id, const char *datastore, const char *filter, const char *stop_time, |
| 941 | uint32_t dampening_period, NC_PARAMTYPE paramtype) |
| 942 | { |
| 943 | struct nc_rpc_modifypush *rpc; |
| 944 | |
| 945 | if (!id) { |
| 946 | ERRARG("id"); |
| 947 | return NULL; |
| 948 | } else if (!datastore) { |
| 949 | ERRARG("datastore"); |
| 950 | return NULL; |
| 951 | } |
| 952 | |
| 953 | if (filter && filter[0] && (filter[0] != '<') && (filter[0] != '/') && !isalpha(filter[0])) { |
| 954 | ERR("Filter is not an XML subtree, an XPath expression, not a filter reference (invalid first char '%c').", filter[0]); |
| 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | rpc = malloc(sizeof *rpc); |
| 959 | if (!rpc) { |
| 960 | ERRMEM; |
| 961 | return NULL; |
| 962 | } |
| 963 | |
| 964 | rpc->type = NC_RPC_MODIFYPUSH; |
| 965 | rpc->id = id; |
| 966 | if (paramtype == NC_PARAMTYPE_DUP_AND_FREE) { |
| 967 | rpc->datastore = strdup(datastore); |
| 968 | } else { |
| 969 | rpc->datastore = (char *)datastore; |
| 970 | } |
| 971 | if (filter && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 972 | rpc->filter = strdup(filter); |
| 973 | } else { |
| 974 | rpc->filter = (char *)filter; |
| 975 | } |
| 976 | if (stop_time && (paramtype == NC_PARAMTYPE_DUP_AND_FREE)) { |
| 977 | rpc->stop = strdup(stop_time); |
| 978 | } else { |
| 979 | rpc->stop = (char *)stop_time; |
| 980 | } |
| 981 | rpc->periodic = 0; |
| 982 | rpc->dampening_period = dampening_period; |
| 983 | rpc->free = (paramtype == NC_PARAMTYPE_CONST ? 0 : 1); |
| 984 | |
| 985 | return (struct nc_rpc *)rpc; |
| 986 | } |
| 987 | |
| 988 | API struct nc_rpc * |
| 989 | nc_rpc_resyncsub(uint32_t id) |
| 990 | { |
| 991 | struct nc_rpc_resyncsub *rpc; |
| 992 | |
| 993 | if (!id) { |
| 994 | ERRARG("id"); |
| 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | rpc = malloc(sizeof *rpc); |
| 999 | if (!rpc) { |
| 1000 | ERRMEM; |
| 1001 | return NULL; |
| 1002 | } |
| 1003 | |
| 1004 | rpc->type = NC_RPC_RESYNCSUB; |
| 1005 | rpc->id = id; |
| 1006 | |
| 1007 | return (struct nc_rpc *)rpc; |
| 1008 | } |
| 1009 | |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1010 | API void |
| 1011 | nc_rpc_free(struct nc_rpc *rpc) |
| 1012 | { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1013 | struct nc_rpc_act_generic *rpc_generic; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1014 | struct nc_rpc_getconfig *rpc_getconfig; |
| 1015 | struct nc_rpc_edit *rpc_edit; |
| 1016 | struct nc_rpc_copy *rpc_copy; |
| 1017 | struct nc_rpc_delete *rpc_delete; |
| 1018 | struct nc_rpc_get *rpc_get; |
| 1019 | struct nc_rpc_commit *rpc_commit; |
| 1020 | struct nc_rpc_cancel *rpc_cancel; |
| 1021 | struct nc_rpc_validate *rpc_validate; |
| 1022 | struct nc_rpc_getschema *rpc_getschema; |
| 1023 | struct nc_rpc_subscribe *rpc_subscribe; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 1024 | struct nc_rpc_getdata *rpc_getdata; |
| 1025 | struct nc_rpc_editdata *rpc_editdata; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 1026 | struct nc_rpc_establishsub *rpc_establishsub; |
| 1027 | struct nc_rpc_modifysub *rpc_modifysub; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 1028 | struct nc_rpc_establishpush *rpc_establishpush; |
| 1029 | struct nc_rpc_modifypush *rpc_modifypush; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 1030 | int i; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1031 | |
| 1032 | if (!rpc) { |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1037 | case NC_RPC_ACT_GENERIC: |
| 1038 | rpc_generic = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1039 | if (rpc_generic->free) { |
| 1040 | if (rpc_generic->has_data) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1041 | lyd_free_tree(rpc_generic->content.data); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1042 | } else { |
| 1043 | free(rpc_generic->content.xml_str); |
| 1044 | } |
| 1045 | } |
| 1046 | break; |
| 1047 | case NC_RPC_GETCONFIG: |
| 1048 | rpc_getconfig = (struct nc_rpc_getconfig *)rpc; |
| 1049 | if (rpc_getconfig->free) { |
| 1050 | free(rpc_getconfig->filter); |
| 1051 | } |
| 1052 | break; |
| 1053 | case NC_RPC_EDIT: |
| 1054 | rpc_edit = (struct nc_rpc_edit *)rpc; |
| 1055 | if (rpc_edit->free) { |
| 1056 | free(rpc_edit->edit_cont); |
| 1057 | } |
| 1058 | break; |
| 1059 | case NC_RPC_COPY: |
| 1060 | rpc_copy = (struct nc_rpc_copy *)rpc; |
| 1061 | if (rpc_copy->free) { |
| 1062 | free(rpc_copy->url_config_src); |
David Sedlák | 35419a3 | 2018-10-07 23:23:33 +0200 | [diff] [blame] | 1063 | free(rpc_copy->url_trg); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1064 | } |
| 1065 | break; |
| 1066 | case NC_RPC_DELETE: |
| 1067 | rpc_delete = (struct nc_rpc_delete *)rpc; |
| 1068 | if (rpc_delete->free) { |
| 1069 | free(rpc_delete->url); |
| 1070 | } |
| 1071 | break; |
| 1072 | case NC_RPC_GET: |
| 1073 | rpc_get = (struct nc_rpc_get *)rpc; |
| 1074 | if (rpc_get->free) { |
| 1075 | free(rpc_get->filter); |
| 1076 | } |
| 1077 | break; |
| 1078 | case NC_RPC_COMMIT: |
| 1079 | rpc_commit = (struct nc_rpc_commit *)rpc; |
| 1080 | if (rpc_commit->free) { |
| 1081 | free(rpc_commit->persist); |
| 1082 | free(rpc_commit->persist_id); |
| 1083 | } |
| 1084 | break; |
| 1085 | case NC_RPC_CANCEL: |
| 1086 | rpc_cancel = (struct nc_rpc_cancel *)rpc; |
| 1087 | if (rpc_cancel->free) { |
| 1088 | free(rpc_cancel->persist_id); |
| 1089 | } |
| 1090 | break; |
| 1091 | case NC_RPC_VALIDATE: |
| 1092 | rpc_validate = (struct nc_rpc_validate *)rpc; |
| 1093 | if (rpc_validate->free) { |
| 1094 | free(rpc_validate->url_config_src); |
| 1095 | } |
| 1096 | break; |
| 1097 | case NC_RPC_GETSCHEMA: |
| 1098 | rpc_getschema = (struct nc_rpc_getschema *)rpc; |
| 1099 | if (rpc_getschema->free) { |
| 1100 | free(rpc_getschema->identifier); |
| 1101 | free(rpc_getschema->version); |
| 1102 | free(rpc_getschema->format); |
| 1103 | } |
| 1104 | break; |
| 1105 | case NC_RPC_SUBSCRIBE: |
| 1106 | rpc_subscribe = (struct nc_rpc_subscribe *)rpc; |
| 1107 | if (rpc_subscribe->free) { |
| 1108 | free(rpc_subscribe->stream); |
| 1109 | free(rpc_subscribe->filter); |
| 1110 | free(rpc_subscribe->start); |
| 1111 | free(rpc_subscribe->stop); |
| 1112 | } |
| 1113 | break; |
Michal Vasko | c1171a4 | 2019-11-05 12:06:46 +0100 | [diff] [blame] | 1114 | case NC_RPC_GETDATA: |
| 1115 | rpc_getdata = (struct nc_rpc_getdata *)rpc; |
| 1116 | if (rpc_getdata->free) { |
| 1117 | free(rpc_getdata->datastore); |
| 1118 | free(rpc_getdata->filter); |
| 1119 | free(rpc_getdata->config_filter); |
| 1120 | for (i = 0; i < rpc_getdata->origin_filter_count; ++i) { |
| 1121 | free(rpc_getdata->origin_filter[i]); |
| 1122 | } |
| 1123 | free(rpc_getdata->origin_filter); |
| 1124 | } |
| 1125 | break; |
| 1126 | case NC_RPC_EDITDATA: |
| 1127 | rpc_editdata = (struct nc_rpc_editdata *)rpc; |
| 1128 | if (rpc_editdata->free) { |
| 1129 | free(rpc_editdata->datastore); |
| 1130 | free(rpc_editdata->edit_cont); |
| 1131 | } |
| 1132 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 1133 | case NC_RPC_ESTABLISHSUB: |
| 1134 | rpc_establishsub = (struct nc_rpc_establishsub *)rpc; |
| 1135 | if (rpc_establishsub->free) { |
| 1136 | free(rpc_establishsub->filter); |
| 1137 | free(rpc_establishsub->stream); |
| 1138 | free(rpc_establishsub->start); |
| 1139 | free(rpc_establishsub->stop); |
| 1140 | free(rpc_establishsub->encoding); |
| 1141 | } |
| 1142 | break; |
| 1143 | case NC_RPC_MODIFYSUB: |
| 1144 | rpc_modifysub = (struct nc_rpc_modifysub *)rpc; |
| 1145 | if (rpc_modifysub->free) { |
| 1146 | free(rpc_modifysub->filter); |
| 1147 | free(rpc_modifysub->stop); |
| 1148 | } |
| 1149 | break; |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 1150 | case NC_RPC_ESTABLISHPUSH: |
| 1151 | rpc_establishpush = (struct nc_rpc_establishpush *)rpc; |
| 1152 | if (rpc_establishpush->free) { |
| 1153 | free(rpc_establishpush->datastore); |
| 1154 | free(rpc_establishpush->filter); |
| 1155 | free(rpc_establishpush->stop); |
| 1156 | free(rpc_establishpush->encoding); |
| 1157 | if (rpc_establishpush->periodic) { |
| 1158 | free(rpc_establishpush->anchor_time); |
| 1159 | } else { |
| 1160 | if (rpc_establishpush->excluded_change) { |
| 1161 | for (i = 0; rpc_establishpush->excluded_change[i]; ++i) { |
| 1162 | free(rpc_establishpush->excluded_change[i]); |
| 1163 | } |
| 1164 | free(rpc_establishpush->excluded_change); |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | break; |
| 1169 | case NC_RPC_MODIFYPUSH: |
| 1170 | rpc_modifypush = (struct nc_rpc_modifypush *)rpc; |
| 1171 | if (rpc_modifypush->free) { |
| 1172 | free(rpc_modifypush->datastore); |
| 1173 | free(rpc_modifypush->filter); |
| 1174 | free(rpc_modifypush->stop); |
| 1175 | if (rpc_modifypush->periodic) { |
| 1176 | free(rpc_modifypush->anchor_time); |
| 1177 | } |
| 1178 | } |
| 1179 | break; |
Michal Vasko | 96f247a | 2021-03-15 13:32:10 +0100 | [diff] [blame] | 1180 | case NC_RPC_UNKNOWN: |
| 1181 | case NC_RPC_LOCK: |
| 1182 | case NC_RPC_UNLOCK: |
| 1183 | case NC_RPC_KILL: |
| 1184 | case NC_RPC_DISCARD: |
| 1185 | case NC_RPC_DELETESUB: |
| 1186 | case NC_RPC_KILLSUB: |
Michal Vasko | 305faca | 2021-03-25 09:16:02 +0100 | [diff] [blame] | 1187 | case NC_RPC_RESYNCSUB: |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1188 | /* nothing special needed */ |
| 1189 | break; |
| 1190 | } |
| 1191 | |
| 1192 | free(rpc); |
| 1193 | } |
| 1194 | |
| 1195 | API void |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 1196 | nc_client_err_clean(struct nc_err *err, struct ly_ctx *ctx) |
| 1197 | { |
| 1198 | int i; |
| 1199 | |
| 1200 | assert(ctx); |
| 1201 | |
| 1202 | if (!err) { |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | lydict_remove(ctx, err->type); |
| 1207 | lydict_remove(ctx, err->tag); |
| 1208 | lydict_remove(ctx, err->severity); |
| 1209 | lydict_remove(ctx, err->apptag); |
| 1210 | lydict_remove(ctx, err->path); |
| 1211 | lydict_remove(ctx, err->message); |
| 1212 | lydict_remove(ctx, err->message_lang); |
| 1213 | lydict_remove(ctx, err->sid); |
| 1214 | for (i = 0; i < err->attr_count; ++i) { |
| 1215 | lydict_remove(ctx, err->attr[i]); |
| 1216 | } |
| 1217 | free(err->attr); |
| 1218 | for (i = 0; i < err->elem_count; ++i) { |
| 1219 | lydict_remove(ctx, err->elem[i]); |
| 1220 | } |
| 1221 | free(err->elem); |
| 1222 | for (i = 0; i < err->ns_count; ++i) { |
| 1223 | lydict_remove(ctx, err->ns[i]); |
| 1224 | } |
| 1225 | free(err->ns); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1226 | lyd_free_siblings(err->other); |
Radek Krejci | 36d2ee4 | 2017-10-13 13:55:59 +0200 | [diff] [blame] | 1227 | free(err->other); |
| 1228 | } |