Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file session_client.c |
| 3 | * \author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * \brief libnetconf2 session client 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 | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <assert.h> |
| 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <netdb.h> |
Radek Krejci | 4cf58ec | 2016-02-26 15:04:52 +0100 | [diff] [blame] | 19 | #include <netinet/in.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | #include <arpa/inet.h> |
| 28 | #include <poll.h> |
| 29 | |
| 30 | #include <libyang/libyang.h> |
| 31 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 32 | #include "libnetconf.h" |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 33 | #include "session_client.h" |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 34 | #include "messages_client.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 35 | |
Michal Vasko | 80ef5d2 | 2016-01-18 09:21:02 +0100 | [diff] [blame] | 36 | static const char *ncds2str[] = {NULL, "config", "url", "running", "startup", "candidate"}; |
| 37 | |
Michal Vasko | daf9a09 | 2016-02-09 10:42:05 +0100 | [diff] [blame] | 38 | struct nc_client_opts client_opts; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 39 | |
| 40 | API int |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 41 | nc_client_set_schema_searchpath(const char *path) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 42 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 43 | if (client_opts.schema_searchpath) { |
| 44 | free(client_opts.schema_searchpath); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 45 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 46 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 47 | if (path) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 48 | client_opts.schema_searchpath = strdup(path); |
| 49 | if (!client_opts.schema_searchpath) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 50 | ERRMEM; |
| 51 | return 1; |
| 52 | } |
| 53 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 54 | client_opts.schema_searchpath = NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return 0; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 60 | API const char * |
| 61 | nc_client_get_schema_searchpath(void) |
| 62 | { |
| 63 | return client_opts.schema_searchpath; |
| 64 | } |
| 65 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 66 | /* SCHEMAS_DIR not used (implicitly) */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 67 | static int |
| 68 | ctx_check_and_load_model(struct nc_session *session, const char *cpblt) |
| 69 | { |
| 70 | const struct lys_module *module; |
| 71 | char *ptr, *ptr2; |
| 72 | char *model_name, *revision = NULL, *features = NULL; |
| 73 | |
| 74 | /* parse module */ |
| 75 | ptr = strstr(cpblt, "module="); |
| 76 | if (!ptr) { |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 77 | ERR("Unknown capability \"%s\" could not be parsed.", cpblt); |
| 78 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 79 | } |
| 80 | ptr += 7; |
| 81 | ptr2 = strchr(ptr, '&'); |
| 82 | if (!ptr2) { |
| 83 | ptr2 = ptr + strlen(ptr); |
| 84 | } |
| 85 | model_name = strndup(ptr, ptr2 - ptr); |
| 86 | |
| 87 | /* parse revision */ |
| 88 | ptr = strstr(cpblt, "revision="); |
| 89 | if (ptr) { |
| 90 | ptr += 9; |
| 91 | ptr2 = strchr(ptr, '&'); |
| 92 | if (!ptr2) { |
| 93 | ptr2 = ptr + strlen(ptr); |
| 94 | } |
| 95 | revision = strndup(ptr, ptr2 - ptr); |
| 96 | } |
| 97 | |
| 98 | /* load module if needed */ |
| 99 | module = ly_ctx_get_module(session->ctx, model_name, revision); |
| 100 | if (!module) { |
| 101 | module = ly_ctx_load_module(session->ctx, model_name, revision); |
| 102 | } |
| 103 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 104 | free(revision); |
| 105 | if (!module) { |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 106 | WRN("Failed to load model \"%s\".", model_name); |
| 107 | free(model_name); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 108 | return 1; |
| 109 | } |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 110 | free(model_name); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 111 | |
| 112 | /* parse features */ |
| 113 | ptr = strstr(cpblt, "features="); |
| 114 | if (ptr) { |
| 115 | ptr += 9; |
| 116 | ptr2 = strchr(ptr, '&'); |
| 117 | if (!ptr2) { |
| 118 | ptr2 = ptr + strlen(ptr); |
| 119 | } |
| 120 | features = strndup(ptr, ptr2 - ptr); |
| 121 | } |
| 122 | |
| 123 | /* enable features */ |
| 124 | if (features) { |
| 125 | /* basically manual strtok_r (to avoid macro) */ |
| 126 | ptr2 = features; |
| 127 | for (ptr = features; *ptr; ++ptr) { |
| 128 | if (*ptr == ',') { |
| 129 | *ptr = '\0'; |
| 130 | /* remember last feature */ |
| 131 | ptr2 = ptr + 1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | ptr = features; |
| 136 | lys_features_enable(module, ptr); |
| 137 | while (ptr != ptr2) { |
| 138 | ptr += strlen(ptr) + 1; |
| 139 | lys_features_enable(module, ptr); |
| 140 | } |
| 141 | |
| 142 | free(features); |
| 143 | } |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Michal Vasko | 1aaa660 | 2016-02-09 11:04:33 +0100 | [diff] [blame] | 148 | /* SCHEMAS_DIR used as the last resort */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 149 | static int |
Michal Vasko | 1aaa660 | 2016-02-09 11:04:33 +0100 | [diff] [blame] | 150 | ctx_check_and_load_ietf_netconf(struct ly_ctx *ctx, const char **cpblts) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 151 | { |
| 152 | int i; |
| 153 | const struct lys_module *ietfnc; |
| 154 | |
| 155 | ietfnc = ly_ctx_get_module(ctx, "ietf-netconf", NULL); |
| 156 | if (!ietfnc) { |
Michal Vasko | 1aaa660 | 2016-02-09 11:04:33 +0100 | [diff] [blame] | 157 | ietfnc = ly_ctx_load_module(ctx, "ietf-netconf", NULL); |
| 158 | if (!ietfnc) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 159 | ietfnc = lys_parse_path(ctx, SCHEMAS_DIR"/ietf-netconf.yin", LYS_IN_YIN); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | if (!ietfnc) { |
| 163 | ERR("Loading base NETCONF schema failed."); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | /* set supported capabilities from ietf-netconf */ |
| 168 | for (i = 0; cpblts[i]; ++i) { |
| 169 | if (!strncmp(cpblts[i], "urn:ietf:params:netconf:capability:", 35)) { |
| 170 | if (!strncmp(cpblts[i] + 35, "writable-running", 16)) { |
| 171 | lys_features_enable(ietfnc, "writable-running"); |
| 172 | } else if (!strncmp(cpblts[i] + 35, "candidate", 9)) { |
| 173 | lys_features_enable(ietfnc, "candidate"); |
| 174 | } else if (!strcmp(cpblts[i] + 35, "confirmed-commit:1.1")) { |
| 175 | lys_features_enable(ietfnc, "confirmed-commit"); |
| 176 | } else if (!strncmp(cpblts[i] + 35, "rollback-on-error", 17)) { |
| 177 | lys_features_enable(ietfnc, "rollback-on-error"); |
| 178 | } else if (!strcmp(cpblts[i] + 35, "validate:1.1")) { |
| 179 | lys_features_enable(ietfnc, "validate"); |
| 180 | } else if (!strncmp(cpblts[i] + 35, "startup", 7)) { |
| 181 | lys_features_enable(ietfnc, "startup"); |
| 182 | } else if (!strncmp(cpblts[i] + 35, "url", 3)) { |
| 183 | lys_features_enable(ietfnc, "url"); |
| 184 | } else if (!strncmp(cpblts[i] + 35, "xpath", 5)) { |
| 185 | lys_features_enable(ietfnc, "xpath"); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static char * |
Michal Vasko | d5ad5f7 | 2016-07-25 16:17:46 +0200 | [diff] [blame] | 194 | libyang_module_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev, |
| 195 | void *user_data, LYS_INFORMAT *format, void (**free_model_data)(void *model_data)) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 196 | { |
| 197 | struct nc_session *session = (struct nc_session *)user_data; |
| 198 | struct nc_rpc *rpc; |
| 199 | struct nc_reply *reply; |
| 200 | struct nc_reply_data *data_rpl; |
Michal Vasko | 998ba41 | 2016-09-16 12:00:07 +0200 | [diff] [blame] | 201 | struct nc_reply_error *error_rpl; |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 202 | struct lyd_node_anydata *get_schema_data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 203 | NC_MSG_TYPE msg; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 204 | char *model_data = NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 205 | uint64_t msgid; |
| 206 | |
Michal Vasko | d5ad5f7 | 2016-07-25 16:17:46 +0200 | [diff] [blame] | 207 | if (submod_name) { |
Michal Vasko | 3e7eaf4 | 2016-09-20 10:51:23 +0200 | [diff] [blame] | 208 | rpc = nc_rpc_getschema(submod_name, submod_rev, "yang", NC_PARAMTYPE_CONST); |
Michal Vasko | d5ad5f7 | 2016-07-25 16:17:46 +0200 | [diff] [blame] | 209 | } else { |
Michal Vasko | 3e7eaf4 | 2016-09-20 10:51:23 +0200 | [diff] [blame] | 210 | rpc = nc_rpc_getschema(mod_name, mod_rev, "yang", NC_PARAMTYPE_CONST); |
Michal Vasko | d5ad5f7 | 2016-07-25 16:17:46 +0200 | [diff] [blame] | 211 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 212 | *format = LYS_IN_YIN; |
| 213 | |
| 214 | while ((msg = nc_send_rpc(session, rpc, 0, &msgid)) == NC_MSG_WOULDBLOCK) { |
| 215 | usleep(1000); |
| 216 | } |
| 217 | if (msg == NC_MSG_ERROR) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 218 | ERR("Session %u: failed to send the <get-schema> RPC.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 219 | nc_rpc_free(rpc); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
Michal Vasko | ff8ddc0 | 2016-10-03 14:13:29 +0200 | [diff] [blame] | 223 | do { |
| 224 | msg = nc_recv_reply(session, rpc, msgid, 1000, 0, &reply); |
| 225 | } while (msg == NC_MSG_NOTIF); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 226 | nc_rpc_free(rpc); |
| 227 | if (msg == NC_MSG_WOULDBLOCK) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 228 | ERR("Session %u: timeout for receiving reply to a <get-schema> expired.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 229 | return NULL; |
| 230 | } else if (msg == NC_MSG_ERROR) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 231 | ERR("Session %u: failed to receive a reply to <get-schema>.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | |
Michal Vasko | 998ba41 | 2016-09-16 12:00:07 +0200 | [diff] [blame] | 235 | switch (reply->type) { |
| 236 | case NC_RPL_OK: |
| 237 | ERR("Session %u: unexpected reply OK to a <get-schema> RPC.", session->id); |
| 238 | nc_reply_free(reply); |
| 239 | return NULL; |
| 240 | case NC_RPL_DATA: |
| 241 | /* fine */ |
| 242 | break; |
| 243 | case NC_RPL_ERROR: |
| 244 | error_rpl = (struct nc_reply_error *)reply; |
| 245 | if (error_rpl->count) { |
| 246 | ERR("Session %u: error reply to a <get-schema> RPC (tag \"%s\", message \"%s\").", |
| 247 | session->id, error_rpl->err[0].tag, error_rpl->err[0].message); |
| 248 | } else { |
| 249 | ERR("Session %u: unexpected reply error to a <get-schema> RPC.", session->id); |
| 250 | } |
| 251 | nc_reply_free(reply); |
| 252 | return NULL; |
| 253 | case NC_RPL_NOTIF: |
| 254 | ERR("Session %u: unexpected reply notification to a <get-schema> RPC.", session->id); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 255 | nc_reply_free(reply); |
| 256 | return NULL; |
| 257 | } |
| 258 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 259 | data_rpl = (struct nc_reply_data *)reply; |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 260 | if ((data_rpl->data->schema->nodetype != LYS_RPC) || strcmp(data_rpl->data->schema->name, "get-schema") |
| 261 | || !data_rpl->data->child || (data_rpl->data->child->schema->nodetype != LYS_ANYXML)) { |
| 262 | ERR("Session %u: unexpected data in reply to a <get-schema> RPC.", session->id); |
| 263 | nc_reply_free(reply); |
| 264 | return NULL; |
| 265 | } |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 266 | get_schema_data = (struct lyd_node_anydata *)data_rpl->data->child; |
| 267 | switch (get_schema_data->value_type) { |
| 268 | case LYD_ANYDATA_CONSTSTRING: |
| 269 | case LYD_ANYDATA_STRING: |
Michal Vasko | b2583f1 | 2016-05-12 11:40:23 +0200 | [diff] [blame] | 270 | model_data = strdup(get_schema_data->value.str); |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 271 | break; |
| 272 | case LYD_ANYDATA_DATATREE: |
| 273 | lyd_print_mem(&model_data, get_schema_data->value.tree, LYD_XML, LYP_WITHSIBLINGS); |
| 274 | break; |
| 275 | case LYD_ANYDATA_XML: |
| 276 | lyxml_print_mem(&model_data, get_schema_data->value.xml, LYXML_PRINT_SIBLINGS); |
| 277 | break; |
Radek Krejci | 03438ec | 2016-09-21 14:12:26 +0200 | [diff] [blame] | 278 | case LYD_ANYDATA_JSON: |
| 279 | case LYD_ANYDATA_JSOND: |
Michal Vasko | 94868ed | 2016-09-29 10:33:38 +0200 | [diff] [blame] | 280 | case LYD_ANYDATA_SXML: |
| 281 | case LYD_ANYDATA_SXMLD: |
Radek Krejci | 03438ec | 2016-09-21 14:12:26 +0200 | [diff] [blame] | 282 | ERRINT; |
| 283 | break; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 284 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 285 | nc_reply_free(reply); |
Michal Vasko | ca4ad15 | 2016-03-03 15:50:45 +0100 | [diff] [blame] | 286 | *free_model_data = free; |
Michal Vasko | 3e7eaf4 | 2016-09-20 10:51:23 +0200 | [diff] [blame] | 287 | *format = LYS_IN_YANG; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 288 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 289 | return model_data; |
| 290 | } |
| 291 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 292 | /* return 0 - ok, 1 - some models failed to load, -1 - error */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 293 | int |
| 294 | nc_ctx_check_and_fill(struct nc_session *session) |
| 295 | { |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 296 | int i, get_schema_support = 0, ret = 0, r; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 297 | ly_module_clb old_clb = NULL; |
| 298 | void *old_data = NULL; |
| 299 | |
| 300 | assert(session->cpblts && session->ctx); |
| 301 | |
| 302 | /* check if get-schema is supported */ |
| 303 | for (i = 0; session->cpblts[i]; ++i) { |
| 304 | if (!strncmp(session->cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) { |
| 305 | get_schema_support = 1; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* get-schema is supported, load local ietf-netconf-monitoring so we can create <get-schema> RPCs */ |
| 311 | if (get_schema_support && !ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL)) { |
| 312 | if (lys_parse_path(session->ctx, SCHEMAS_DIR"/ietf-netconf-monitoring.yin", LYS_IN_YIN)) { |
| 313 | /* set module retrieval using <get-schema> */ |
| 314 | old_clb = ly_ctx_get_module_clb(session->ctx, &old_data); |
Michal Vasko | ca4ad15 | 2016-03-03 15:50:45 +0100 | [diff] [blame] | 315 | ly_ctx_set_module_clb(session->ctx, libyang_module_clb, session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 316 | } else { |
| 317 | WRN("Loading NETCONF monitoring schema failed, cannot use <get-schema>."); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* load base model disregarding whether it's in capabilities (but NETCONF capabilities are used to enable features) */ |
Michal Vasko | 1aaa660 | 2016-02-09 11:04:33 +0100 | [diff] [blame] | 322 | if (ctx_check_and_load_ietf_netconf(session->ctx, session->cpblts)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 323 | if (old_clb) { |
| 324 | ly_ctx_set_module_clb(session->ctx, old_clb, old_data); |
| 325 | } |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 326 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* load all other models */ |
| 330 | for (i = 0; session->cpblts[i]; ++i) { |
| 331 | if (!strncmp(session->cpblts[i], "urn:ietf:params:netconf:capability", 34) |
| 332 | || !strncmp(session->cpblts[i], "urn:ietf:params:netconf:base", 28)) { |
| 333 | continue; |
| 334 | } |
| 335 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 336 | r = ctx_check_and_load_model(session, session->cpblts[i]); |
| 337 | if (r == -1) { |
| 338 | ret = -1; |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | /* failed to load schema, but let's try to find it using user callback (or locally, if not set), |
| 343 | * if it was using get-schema */ |
| 344 | if (r == 1) { |
| 345 | if (get_schema_support) { |
| 346 | VRB("Trying to load the schema from a different source."); |
| 347 | /* works even if old_clb is NULL */ |
| 348 | ly_ctx_set_module_clb(session->ctx, old_clb, old_data); |
| 349 | r = ctx_check_and_load_model(session, session->cpblts[i]); |
| 350 | } |
| 351 | |
| 352 | /* fail again (or no other way to try), too bad */ |
| 353 | if (r) { |
| 354 | ret = 1; |
| 355 | } |
| 356 | |
| 357 | /* set get-schema callback back */ |
| 358 | ly_ctx_set_module_clb(session->ctx, &libyang_module_clb, session); |
| 359 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (old_clb) { |
| 363 | ly_ctx_set_module_clb(session->ctx, old_clb, old_data); |
| 364 | } |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 365 | if (ret == 1) { |
| 366 | WRN("Some models failed to be loaded, any data from these models will be ignored."); |
| 367 | } |
| 368 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | API struct nc_session * |
| 372 | nc_connect_inout(int fdin, int fdout, struct ly_ctx *ctx) |
| 373 | { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 374 | struct nc_session *session; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 375 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 376 | if (fdin < 0) { |
| 377 | ERRARG("fdin"); |
| 378 | return NULL; |
| 379 | } else if (fdout < 0) { |
| 380 | ERRARG("fdout"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | /* prepare session structure */ |
| 385 | session = calloc(1, sizeof *session); |
| 386 | if (!session) { |
| 387 | ERRMEM; |
| 388 | return NULL; |
| 389 | } |
| 390 | session->status = NC_STATUS_STARTING; |
| 391 | session->side = NC_CLIENT; |
| 392 | |
| 393 | /* transport specific data */ |
| 394 | session->ti_type = NC_TI_FD; |
| 395 | session->ti.fd.in = fdin; |
| 396 | session->ti.fd.out = fdout; |
| 397 | |
| 398 | /* assign context (dicionary needed for handshake) */ |
| 399 | if (!ctx) { |
| 400 | ctx = ly_ctx_new(SCHEMAS_DIR); |
Michal Vasko | e035b8e | 2016-03-11 10:10:03 +0100 | [diff] [blame] | 401 | /* definitely should not happen, but be ready */ |
| 402 | if (!ctx && !(ctx = ly_ctx_new(NULL))) { |
| 403 | /* that's just it */ |
| 404 | goto fail; |
| 405 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 406 | } else { |
| 407 | session->flags |= NC_SESSION_SHAREDCTX; |
| 408 | } |
| 409 | session->ctx = ctx; |
| 410 | |
| 411 | /* NETCONF handshake */ |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 412 | if (nc_handshake(session) != NC_MSG_HELLO) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 413 | goto fail; |
| 414 | } |
| 415 | session->status = NC_STATUS_RUNNING; |
| 416 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 417 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 418 | goto fail; |
| 419 | } |
| 420 | |
| 421 | return session; |
| 422 | |
| 423 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 424 | nc_session_free(session, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | int |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 429 | nc_sock_connect(const char* host, uint16_t port) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 430 | { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 431 | int i, sock = -1, flags; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 432 | struct addrinfo hints, *res_list, *res; |
| 433 | char port_s[6]; /* length of string representation of short int */ |
| 434 | |
| 435 | snprintf(port_s, 6, "%u", port); |
| 436 | |
| 437 | /* Connect to a server */ |
| 438 | memset(&hints, 0, sizeof hints); |
| 439 | hints.ai_family = AF_UNSPEC; |
| 440 | hints.ai_socktype = SOCK_STREAM; |
| 441 | hints.ai_protocol = IPPROTO_TCP; |
| 442 | i = getaddrinfo(host, port_s, &hints, &res_list); |
| 443 | if (i != 0) { |
| 444 | ERR("Unable to translate the host address (%s).", gai_strerror(i)); |
| 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | for (i = 0, res = res_list; res != NULL; res = res->ai_next) { |
| 449 | sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 450 | if (sock == -1) { |
| 451 | /* socket was not created, try another resource */ |
| 452 | i = errno; |
| 453 | goto errloop; |
| 454 | } |
| 455 | |
| 456 | if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) { |
| 457 | /* network connection failed, try another resource */ |
| 458 | i = errno; |
| 459 | close(sock); |
| 460 | sock = -1; |
| 461 | goto errloop; |
| 462 | } |
| 463 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 464 | /* make the socket non-blocking */ |
| 465 | if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) { |
| 466 | ERR("Fcntl failed (%s).", strerror(errno)); |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 467 | close(sock); |
Michal Vasko | a8a66b6 | 2016-10-05 14:14:19 +0200 | [diff] [blame] | 468 | freeaddrinfo(res_list); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 469 | return -1; |
| 470 | } |
| 471 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 472 | /* we're done, network connection established */ |
| 473 | break; |
| 474 | errloop: |
| 475 | VRB("Unable to connect to %s:%s over %s (%s).", host, port_s, |
| 476 | (res->ai_family == AF_INET6) ? "IPv6" : "IPv4", strerror(i)); |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | if (sock == -1) { |
| 481 | ERR("Unable to connect to %s:%s.", host, port_s); |
| 482 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 483 | VRB("Successfully connected to %s:%s over %s.", host, port_s, (res->ai_family == AF_INET6) ? "IPv6" : "IPv4"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 484 | } |
| 485 | freeaddrinfo(res_list); |
| 486 | |
| 487 | return sock; |
| 488 | } |
| 489 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 490 | static NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 491 | get_msg(struct nc_session *session, int timeout, uint64_t msgid, struct lyxml_elem **msg) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 492 | { |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 493 | int r; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 494 | char *ptr; |
| 495 | const char *str_msgid; |
| 496 | uint64_t cur_msgid; |
| 497 | struct lyxml_elem *xml; |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 498 | struct nc_msg_cont *cont, **cont_ptr; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 499 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 500 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 501 | r = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 502 | if (r == -1) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 503 | /* error */ |
| 504 | return NC_MSG_ERROR; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 505 | } else if (!r) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 506 | /* timeout */ |
| 507 | return NC_MSG_WOULDBLOCK; |
| 508 | } |
| 509 | |
| 510 | /* try to get notification from the session's queue */ |
| 511 | if (!msgid && session->notifs) { |
| 512 | cont = session->notifs; |
| 513 | session->notifs = cont->next; |
| 514 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 515 | xml = cont->msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 516 | free(cont); |
| 517 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 518 | msgtype = NC_MSG_NOTIF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* try to get rpc-reply from the session's queue */ |
| 522 | if (msgid && session->replies) { |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 523 | cont = session->replies; |
| 524 | session->replies = cont->next; |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 525 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 526 | xml = cont->msg; |
| 527 | free(cont); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 528 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 529 | msgtype = NC_MSG_REPLY; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 532 | if (!msgtype) { |
| 533 | /* read message from wire */ |
| 534 | msgtype = nc_read_msg_poll(session, timeout, &xml); |
| 535 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 536 | |
| 537 | /* we read rpc-reply, want a notif */ |
| 538 | if (!msgid && (msgtype == NC_MSG_REPLY)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 539 | cont_ptr = &session->replies; |
| 540 | while (*cont_ptr) { |
| 541 | cont_ptr = &((*cont_ptr)->next); |
| 542 | } |
| 543 | *cont_ptr = malloc(sizeof **cont_ptr); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 544 | if (!*cont_ptr) { |
| 545 | ERRMEM; |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 546 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 547 | lyxml_free(session->ctx, xml); |
| 548 | return NC_MSG_ERROR; |
| 549 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 550 | (*cont_ptr)->msg = xml; |
| 551 | (*cont_ptr)->next = NULL; |
| 552 | } |
| 553 | |
| 554 | /* we read notif, want a rpc-reply */ |
| 555 | if (msgid && (msgtype == NC_MSG_NOTIF)) { |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 556 | /* TODO check whether the session is even subscribed */ |
| 557 | /*if (!session->notif) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 558 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 559 | ERR("Session %u: received a <notification> but session is not subscribed.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 560 | lyxml_free(session->ctx, xml); |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 561 | return NC_MSG_ERROR; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 562 | }*/ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 563 | |
| 564 | cont_ptr = &session->notifs; |
| 565 | while (*cont_ptr) { |
| 566 | cont_ptr = &((*cont_ptr)->next); |
| 567 | } |
| 568 | *cont_ptr = malloc(sizeof **cont_ptr); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 569 | if (!cont_ptr) { |
| 570 | ERRMEM; |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 571 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 572 | lyxml_free(session->ctx, xml); |
| 573 | return NC_MSG_ERROR; |
| 574 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 575 | (*cont_ptr)->msg = xml; |
| 576 | (*cont_ptr)->next = NULL; |
| 577 | } |
| 578 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 579 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 580 | |
| 581 | switch (msgtype) { |
| 582 | case NC_MSG_NOTIF: |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 583 | if (!msgid) { |
| 584 | *msg = xml; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 585 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 586 | break; |
| 587 | |
| 588 | case NC_MSG_REPLY: |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 589 | if (msgid) { |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 590 | /* check message-id */ |
| 591 | str_msgid = lyxml_get_attr(xml, "message-id", NULL); |
| 592 | if (!str_msgid) { |
| 593 | ERR("Session %u: received a <rpc-reply> without a message-id.", session->id); |
| 594 | msgtype = NC_MSG_REPLY_ERR_MSGID; |
| 595 | } else { |
| 596 | cur_msgid = strtoul(str_msgid, &ptr, 10); |
| 597 | if (cur_msgid != msgid) { |
| 598 | ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".", |
| 599 | session->id, str_msgid); |
| 600 | msgtype = NC_MSG_REPLY_ERR_MSGID; |
| 601 | } |
| 602 | } |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 603 | *msg = xml; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 604 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 605 | break; |
| 606 | |
| 607 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 608 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 609 | lyxml_free(session->ctx, xml); |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 610 | msgtype = NC_MSG_ERROR; |
| 611 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 612 | |
| 613 | case NC_MSG_RPC: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 614 | ERR("Session %u: received <rpc> from a NETCONF server.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 615 | lyxml_free(session->ctx, xml); |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 616 | msgtype = NC_MSG_ERROR; |
| 617 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 618 | |
| 619 | default: |
| 620 | /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out; |
| 621 | * NC_MSG_NONE is not returned by nc_read_msg() |
| 622 | */ |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | return msgtype; |
| 627 | } |
| 628 | |
| 629 | /* cannot strictly fail, but does not need to fill any error parameter at all */ |
| 630 | static void |
| 631 | parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err) |
| 632 | { |
| 633 | struct lyxml_elem *iter, *next, *info; |
| 634 | |
| 635 | LY_TREE_FOR(xml->child, iter) { |
| 636 | if (!iter->ns) { |
| 637 | if (iter->content) { |
| 638 | WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content); |
| 639 | } else { |
| 640 | WRN("<rpc-error> child \"%s\" without namespace.", iter->name); |
| 641 | } |
| 642 | continue; |
| 643 | } else if (strcmp(iter->ns->value, NC_NS_BASE)) { |
| 644 | if (iter->content) { |
| 645 | WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".", |
| 646 | iter->name, iter->content, iter->ns->value); |
| 647 | } else { |
| 648 | WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value); |
| 649 | } |
| 650 | continue; |
| 651 | } |
| 652 | |
| 653 | if (!strcmp(iter->name, "error-type")) { |
| 654 | if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc") |
| 655 | && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) { |
| 656 | WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 657 | } else if (err->type) { |
| 658 | WRN("<rpc-error> <error-type> duplicated."); |
| 659 | } else { |
| 660 | err->type = lydict_insert(ctx, iter->content, 0); |
| 661 | } |
| 662 | } else if (!strcmp(iter->name, "error-tag")) { |
| 663 | if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value") |
| 664 | && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute") |
| 665 | && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute") |
| 666 | && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element") |
| 667 | && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace") |
| 668 | && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied") |
| 669 | && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed") |
| 670 | && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing") |
| 671 | && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed") |
| 672 | && strcmp(iter->content, "malformed-message"))) { |
| 673 | WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 674 | } else if (err->tag) { |
| 675 | WRN("<rpc-error> <error-tag> duplicated."); |
| 676 | } else { |
| 677 | err->tag = lydict_insert(ctx, iter->content, 0); |
| 678 | } |
| 679 | } else if (!strcmp(iter->name, "error-severity")) { |
| 680 | if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) { |
| 681 | WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 682 | } else if (err->severity) { |
| 683 | WRN("<rpc-error> <error-severity> duplicated."); |
| 684 | } else { |
| 685 | err->severity = lydict_insert(ctx, iter->content, 0); |
| 686 | } |
| 687 | } else if (!strcmp(iter->name, "error-app-tag")) { |
| 688 | if (err->apptag) { |
| 689 | WRN("<rpc-error> <error-app-tag> duplicated."); |
| 690 | } else { |
| 691 | err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 692 | } |
| 693 | } else if (!strcmp(iter->name, "error-path")) { |
| 694 | if (err->path) { |
| 695 | WRN("<rpc-error> <error-path> duplicated."); |
| 696 | } else { |
| 697 | err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 698 | } |
| 699 | } else if (!strcmp(iter->name, "error-message")) { |
| 700 | if (err->message) { |
| 701 | WRN("<rpc-error> <error-message> duplicated."); |
| 702 | } else { |
| 703 | err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL); |
| 704 | if (!err->message_lang) { |
| 705 | VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute."); |
| 706 | } |
| 707 | err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 708 | } |
| 709 | } else if (!strcmp(iter->name, "error-info")) { |
| 710 | LY_TREE_FOR_SAFE(iter->child, next, info) { |
| 711 | if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) { |
| 712 | if (!strcmp(info->name, "session-id")) { |
| 713 | if (err->sid) { |
| 714 | WRN("<rpc-error> <error-info> <session-id> duplicated."); |
| 715 | } else { |
| 716 | err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 717 | } |
| 718 | } else if (!strcmp(info->name, "bad-attr")) { |
| 719 | ++err->attr_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 720 | err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr); |
| 721 | if (!err->attr) { |
| 722 | ERRMEM; |
| 723 | return; |
| 724 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 725 | err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 726 | } else if (!strcmp(info->name, "bad-element")) { |
| 727 | ++err->elem_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 728 | err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem); |
| 729 | if (!err->elem) { |
| 730 | ERRMEM; |
| 731 | return; |
| 732 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 733 | err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 734 | } else if (!strcmp(info->name, "bad-namespace")) { |
| 735 | ++err->ns_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 736 | err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns); |
| 737 | if (!err->ns) { |
| 738 | ERRMEM; |
| 739 | return; |
| 740 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 741 | err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 742 | } else { |
| 743 | if (info->content) { |
| 744 | WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".", |
| 745 | info->name, info->content); |
| 746 | } else { |
| 747 | WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name); |
| 748 | } |
| 749 | } |
| 750 | } else { |
| 751 | lyxml_unlink(ctx, info); |
| 752 | ++err->other_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 753 | err->other = nc_realloc(err->other, err->other_count * sizeof *err->other); |
| 754 | if (!err->other) { |
| 755 | ERRMEM; |
| 756 | return; |
| 757 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 758 | err->other[err->other_count - 1] = info; |
| 759 | } |
| 760 | } |
| 761 | } else { |
| 762 | if (iter->content) { |
| 763 | WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content); |
| 764 | } else { |
| 765 | WRN("<rpc-error> unknown child \"%s\".", iter->name); |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | static struct nc_reply * |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 772 | parse_reply(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_rpc *rpc, int parseroptions) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 773 | { |
| 774 | struct lyxml_elem *iter; |
Michal Vasko | 0473c4c | 2016-01-19 10:40:06 +0100 | [diff] [blame] | 775 | const struct lys_node *schema = NULL; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 776 | struct lyd_node *data = NULL, *next, *elem; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 777 | struct nc_client_reply_error *error_rpl; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 778 | struct nc_reply_data *data_rpl; |
| 779 | struct nc_reply *reply = NULL; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 780 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 781 | int i; |
| 782 | |
| 783 | if (!xml->child) { |
| 784 | ERR("An empty <rpc-reply>."); |
| 785 | return NULL; |
| 786 | } |
| 787 | |
| 788 | /* rpc-error */ |
| 789 | if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) { |
| 790 | /* count and check elements */ |
| 791 | i = 0; |
| 792 | LY_TREE_FOR(xml->child, iter) { |
| 793 | if (strcmp(iter->name, "rpc-error")) { |
| 794 | ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name); |
| 795 | return NULL; |
| 796 | } else if (!iter->ns) { |
| 797 | ERR("<rpc-reply> content mismatch (<rpc-error> without namespace)."); |
| 798 | return NULL; |
| 799 | } else if (strcmp(iter->ns->value, NC_NS_BASE)) { |
| 800 | ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value); |
| 801 | return NULL; |
| 802 | } |
| 803 | ++i; |
| 804 | } |
| 805 | |
| 806 | error_rpl = malloc(sizeof *error_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 807 | if (!error_rpl) { |
| 808 | ERRMEM; |
| 809 | return NULL; |
| 810 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 811 | error_rpl->type = NC_RPL_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 812 | error_rpl->err = calloc(i, sizeof *error_rpl->err); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 813 | if (!error_rpl->err) { |
| 814 | ERRMEM; |
| 815 | free(error_rpl); |
| 816 | return NULL; |
| 817 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 818 | error_rpl->count = i; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 819 | error_rpl->ctx = ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 820 | reply = (struct nc_reply *)error_rpl; |
| 821 | |
| 822 | i = 0; |
| 823 | LY_TREE_FOR(xml->child, iter) { |
| 824 | parse_rpc_error(ctx, iter, error_rpl->err + i); |
| 825 | ++i; |
| 826 | } |
| 827 | |
| 828 | /* ok */ |
| 829 | } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) { |
| 830 | if (xml->child->next) { |
| 831 | ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name); |
| 832 | return NULL; |
| 833 | } |
| 834 | reply = malloc(sizeof *reply); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 835 | if (!reply) { |
| 836 | ERRMEM; |
| 837 | return NULL; |
| 838 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 839 | reply->type = NC_RPL_OK; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 840 | |
| 841 | /* some RPC output */ |
| 842 | } else { |
| 843 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 844 | case NC_RPC_ACT_GENERIC: |
| 845 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 846 | |
| 847 | if (rpc_gen->has_data) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 848 | data = rpc_gen->content.data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 849 | } else { |
Michal Vasko | 68b3f29 | 2016-09-16 12:00:32 +0200 | [diff] [blame] | 850 | data = lyd_parse_mem(ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | parseroptions, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 851 | if (!data) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 852 | ERR("Failed to parse a generic RPC/action XML."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 853 | return NULL; |
| 854 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 855 | } |
| 856 | if (data->schema->nodetype == LYS_RPC) { |
| 857 | /* RPC */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 858 | schema = data->schema; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 859 | } else { |
| 860 | /* action */ |
| 861 | LY_TREE_DFS_BEGIN(data, next, elem) { |
| 862 | if (elem->schema->nodetype == LYS_ACTION) { |
| 863 | schema = elem->schema; |
| 864 | break; |
| 865 | } |
| 866 | LY_TREE_DFS_END(data, next, elem); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* cleanup */ |
| 871 | if (data != rpc_gen->content.data) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 872 | lyd_free(data); |
| 873 | data = NULL; |
| 874 | } |
| 875 | if (!schema) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 876 | /* only with action, if there is no action, it should not have gotten this far */ |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 877 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 878 | return NULL; |
| 879 | } |
| 880 | break; |
| 881 | |
| 882 | case NC_RPC_GETCONFIG: |
| 883 | case NC_RPC_GET: |
Michal Vasko | 13ed294 | 2016-02-29 16:21:00 +0100 | [diff] [blame] | 884 | if (!xml->child->child) { |
| 885 | /* we did not receive any data */ |
| 886 | data_rpl = malloc(sizeof *data_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 887 | if (!data_rpl) { |
| 888 | ERRMEM; |
| 889 | return NULL; |
| 890 | } |
Michal Vasko | 13ed294 | 2016-02-29 16:21:00 +0100 | [diff] [blame] | 891 | data_rpl->type = NC_RPL_DATA; |
| 892 | data_rpl->data = NULL; |
| 893 | return (struct nc_reply *)data_rpl; |
| 894 | } |
| 895 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 896 | /* special treatment */ |
| 897 | data = lyd_parse_xml(ctx, &xml->child->child, LYD_OPT_DESTRUCT |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 898 | | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) | parseroptions); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 899 | if (!data) { |
| 900 | ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get")); |
| 901 | return NULL; |
| 902 | } |
| 903 | break; |
| 904 | |
| 905 | case NC_RPC_GETSCHEMA: |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 906 | schema = ly_ctx_get_node(ctx, NULL, "/ietf-netconf-monitoring:get-schema"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 907 | if (!schema) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 908 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 909 | return NULL; |
| 910 | } |
| 911 | break; |
| 912 | |
| 913 | case NC_RPC_EDIT: |
| 914 | case NC_RPC_COPY: |
| 915 | case NC_RPC_DELETE: |
| 916 | case NC_RPC_LOCK: |
| 917 | case NC_RPC_UNLOCK: |
| 918 | case NC_RPC_KILL: |
| 919 | case NC_RPC_COMMIT: |
| 920 | case NC_RPC_DISCARD: |
| 921 | case NC_RPC_CANCEL: |
| 922 | case NC_RPC_VALIDATE: |
| 923 | case NC_RPC_SUBSCRIBE: |
| 924 | /* there is no output defined */ |
| 925 | ERR("Unexpected data reply (root elem \"%s\").", xml->child->name); |
| 926 | return NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 927 | default: |
| 928 | ERRINT; |
| 929 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | data_rpl = malloc(sizeof *data_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 933 | if (!data_rpl) { |
| 934 | ERRMEM; |
| 935 | return NULL; |
| 936 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 937 | data_rpl->type = NC_RPL_DATA; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 938 | if (!data) { |
Michal Vasko | 68b3f29 | 2016-09-16 12:00:32 +0200 | [diff] [blame] | 939 | data_rpl->data = lyd_parse_xml(ctx, &xml->child, LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | parseroptions, |
| 940 | schema, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 941 | } else { |
| 942 | /* <get>, <get-config> */ |
| 943 | data_rpl->data = data; |
| 944 | } |
| 945 | if (!data_rpl->data) { |
| 946 | ERR("Failed to parse <rpc-reply>."); |
| 947 | free(data_rpl); |
| 948 | return NULL; |
| 949 | } |
| 950 | reply = (struct nc_reply *)data_rpl; |
| 951 | } |
| 952 | |
| 953 | return reply; |
| 954 | } |
| 955 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 956 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 957 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 958 | int |
| 959 | nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 960 | { |
| 961 | int sock; |
| 962 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 963 | if (!address) { |
| 964 | ERRARG("address"); |
| 965 | return -1; |
| 966 | } else if (!port) { |
| 967 | ERRARG("port"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 968 | return -1; |
| 969 | } |
| 970 | |
| 971 | sock = nc_sock_listen(address, port); |
| 972 | if (sock == -1) { |
| 973 | return -1; |
| 974 | } |
| 975 | |
| 976 | ++client_opts.ch_bind_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 977 | client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds); |
| 978 | if (!client_opts.ch_binds) { |
| 979 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 980 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 981 | return -1; |
| 982 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 983 | |
| 984 | client_opts.ch_binds[client_opts.ch_bind_count - 1].address = strdup(address); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 985 | if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) { |
| 986 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 987 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 988 | return -1; |
| 989 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 990 | client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port; |
| 991 | client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock; |
| 992 | client_opts.ch_binds[client_opts.ch_bind_count - 1].ti = ti; |
| 993 | |
| 994 | return 0; |
| 995 | } |
| 996 | |
| 997 | int |
| 998 | nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 999 | { |
| 1000 | uint32_t i; |
| 1001 | int ret = -1; |
| 1002 | |
| 1003 | if (!address && !port && !ti) { |
| 1004 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 1005 | close(client_opts.ch_binds[i].sock); |
| 1006 | free((char *)client_opts.ch_binds[i].address); |
| 1007 | |
| 1008 | ret = 0; |
| 1009 | } |
| 1010 | free(client_opts.ch_binds); |
| 1011 | client_opts.ch_binds = NULL; |
| 1012 | client_opts.ch_bind_count = 0; |
| 1013 | } else { |
| 1014 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 1015 | if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) |
| 1016 | && (!port || (client_opts.ch_binds[i].port == port)) |
| 1017 | && (!ti || (client_opts.ch_binds[i].ti == ti))) { |
| 1018 | close(client_opts.ch_binds[i].sock); |
| 1019 | free((char *)client_opts.ch_binds[i].address); |
| 1020 | |
| 1021 | --client_opts.ch_bind_count; |
| 1022 | memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds); |
| 1023 | |
| 1024 | ret = 0; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
| 1032 | API int |
| 1033 | nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session) |
| 1034 | { |
| 1035 | int sock; |
| 1036 | char *host = NULL; |
| 1037 | uint16_t port, idx; |
| 1038 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1039 | if (!client_opts.ch_binds) { |
| 1040 | ERRINIT; |
| 1041 | return -1; |
| 1042 | } else if (!session) { |
| 1043 | ERRARG("session"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx); |
| 1048 | |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 1049 | if (sock < 1) { |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1050 | free(host); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1051 | return sock; |
| 1052 | } |
| 1053 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1054 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1055 | if (client_opts.ch_binds[idx].ti == NC_TI_LIBSSH) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1056 | *session = nc_accept_callhome_ssh_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1057 | } else |
| 1058 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1059 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1060 | if (client_opts.ch_binds[idx].ti == NC_TI_OPENSSL) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1061 | *session = nc_accept_callhome_tls_sock(sock, host, port, ctx, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1062 | } else |
| 1063 | #endif |
| 1064 | { |
Michal Vasko | fee717c | 2016-02-01 13:25:43 +0100 | [diff] [blame] | 1065 | close(sock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1066 | *session = NULL; |
| 1067 | } |
| 1068 | |
| 1069 | free(host); |
| 1070 | |
| 1071 | if (!(*session)) { |
| 1072 | return -1; |
| 1073 | } |
| 1074 | |
| 1075 | return 1; |
| 1076 | } |
| 1077 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1078 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1079 | |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1080 | API const char ** |
| 1081 | nc_session_get_cpblts(const struct nc_session *session) |
| 1082 | { |
| 1083 | if (!session) { |
| 1084 | ERRARG("session"); |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | |
| 1088 | return session->cpblts; |
| 1089 | } |
| 1090 | |
| 1091 | API const char * |
| 1092 | nc_session_cpblt(const struct nc_session *session, const char *capab) |
| 1093 | { |
| 1094 | int i, len; |
| 1095 | |
| 1096 | if (!session) { |
| 1097 | ERRARG("session"); |
| 1098 | return NULL; |
| 1099 | } else if (!capab) { |
| 1100 | ERRARG("capab"); |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | |
| 1104 | len = strlen(capab); |
| 1105 | for (i = 0; session->cpblts[i]; ++i) { |
| 1106 | if (!strncmp(session->cpblts[i], capab, len)) { |
| 1107 | return session->cpblts[i]; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | return NULL; |
| 1112 | } |
| 1113 | |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1114 | API int |
| 1115 | nc_session_ntf_thread_running(const struct nc_session *session) |
| 1116 | { |
| 1117 | if (!session) { |
| 1118 | ERRARG("session"); |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | return session->ntf_tid ? 1 : 0; |
| 1123 | } |
| 1124 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1125 | API void |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1126 | nc_client_init(void) |
| 1127 | { |
| 1128 | nc_init(); |
| 1129 | } |
| 1130 | |
| 1131 | API void |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1132 | nc_client_destroy(void) |
| 1133 | { |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 1134 | nc_client_set_schema_searchpath(NULL); |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1135 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1136 | nc_client_ch_del_bind(NULL, 0, 0); |
| 1137 | #endif |
| 1138 | #ifdef NC_ENABLED_SSH |
| 1139 | nc_client_ssh_destroy_opts(); |
| 1140 | #endif |
Michal Vasko | c979d3a | 2016-02-26 15:26:21 +0100 | [diff] [blame] | 1141 | #ifdef NC_ENABLED_TLS |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1142 | nc_client_tls_destroy_opts(); |
| 1143 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1144 | nc_destroy(); |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1145 | } |
| 1146 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1147 | API NC_MSG_TYPE |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 1148 | nc_recv_reply(struct nc_session *session, struct nc_rpc *rpc, uint64_t msgid, int timeout, int parseroptions, struct nc_reply **reply) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1149 | { |
| 1150 | struct lyxml_elem *xml; |
| 1151 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 1152 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1153 | if (!session) { |
| 1154 | ERRARG("session"); |
| 1155 | return NC_MSG_ERROR; |
| 1156 | } else if (!rpc) { |
| 1157 | ERRARG("rpc"); |
| 1158 | return NC_MSG_ERROR; |
| 1159 | } else if (!reply) { |
| 1160 | ERRARG("reply"); |
| 1161 | return NC_MSG_ERROR; |
| 1162 | } else if (parseroptions & LYD_OPT_TYPEMASK) { |
| 1163 | ERRARG("parseroptions"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1164 | return NC_MSG_ERROR; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1165 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1166 | ERR("Session %u: invalid session to receive RPC replies.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1167 | return NC_MSG_ERROR; |
| 1168 | } |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 1169 | parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1170 | *reply = NULL; |
| 1171 | |
| 1172 | msgtype = get_msg(session, timeout, msgid, &xml); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1173 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1174 | if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) { |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 1175 | *reply = parse_reply(session->ctx, xml, rpc, parseroptions); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1176 | lyxml_free(session->ctx, xml); |
| 1177 | if (!(*reply)) { |
| 1178 | return NC_MSG_ERROR; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | return msgtype; |
| 1183 | } |
| 1184 | |
| 1185 | API NC_MSG_TYPE |
| 1186 | nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif) |
| 1187 | { |
| 1188 | struct lyxml_elem *xml, *ev_time; |
| 1189 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 1190 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1191 | if (!session) { |
| 1192 | ERRARG("session"); |
| 1193 | return NC_MSG_ERROR; |
| 1194 | } else if (!notif) { |
| 1195 | ERRARG("notif"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1196 | return NC_MSG_ERROR; |
| 1197 | } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1198 | ERR("Session %u: invalid session to receive Notifications.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1199 | return NC_MSG_ERROR; |
| 1200 | } |
| 1201 | |
| 1202 | msgtype = get_msg(session, timeout, 0, &xml); |
| 1203 | |
| 1204 | if (msgtype == NC_MSG_NOTIF) { |
| 1205 | *notif = calloc(1, sizeof **notif); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1206 | if (!*notif) { |
| 1207 | ERRMEM; |
| 1208 | lyxml_free(session->ctx, xml); |
| 1209 | return NC_MSG_ERROR; |
| 1210 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1211 | |
| 1212 | /* eventTime */ |
| 1213 | LY_TREE_FOR(xml->child, ev_time) { |
| 1214 | if (!strcmp(ev_time->name, "eventTime")) { |
| 1215 | (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0); |
| 1216 | /* lyd_parse does not know this element */ |
| 1217 | lyxml_free(session->ctx, ev_time); |
| 1218 | break; |
| 1219 | } |
| 1220 | } |
| 1221 | if (!(*notif)->datetime) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1222 | ERR("Session %u: notification is missing the \"eventTime\" element.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1223 | goto fail; |
| 1224 | } |
| 1225 | |
| 1226 | /* notification body */ |
Michal Vasko | 68b3f29 | 2016-09-16 12:00:32 +0200 | [diff] [blame] | 1227 | (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_DESTRUCT, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1228 | lyxml_free(session->ctx, xml); |
| 1229 | xml = NULL; |
| 1230 | if (!(*notif)->tree) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1231 | ERR("Session %u: failed to parse a new notification.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1232 | goto fail; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | return msgtype; |
| 1237 | |
| 1238 | fail: |
| 1239 | lydict_remove(session->ctx, (*notif)->datetime); |
| 1240 | lyd_free((*notif)->tree); |
| 1241 | free(*notif); |
| 1242 | *notif = NULL; |
| 1243 | lyxml_free(session->ctx, xml); |
| 1244 | |
| 1245 | return NC_MSG_ERROR; |
| 1246 | } |
| 1247 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1248 | static void * |
| 1249 | nc_recv_notif_thread(void *arg) |
| 1250 | { |
| 1251 | struct nc_ntf_thread_arg *ntarg; |
| 1252 | struct nc_session *session; |
| 1253 | void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif); |
| 1254 | struct nc_notif *notif; |
| 1255 | NC_MSG_TYPE msgtype; |
| 1256 | |
| 1257 | ntarg = (struct nc_ntf_thread_arg *)arg; |
| 1258 | session = ntarg->session; |
| 1259 | notif_clb = ntarg->notif_clb; |
| 1260 | free(ntarg); |
| 1261 | |
| 1262 | while (session->ntf_tid) { |
Michal vasko | 4e1a36c | 2016-10-05 13:04:37 +0200 | [diff] [blame] | 1263 | msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, ¬if); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1264 | if (msgtype == NC_MSG_NOTIF) { |
| 1265 | notif_clb(session, notif); |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 1266 | if (!strcmp(notif->tree->schema->name, "notificationComplete") |
| 1267 | && !strcmp(notif->tree->schema->module->name, "nc-notifications")) { |
| 1268 | nc_notif_free(notif); |
| 1269 | break; |
| 1270 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1271 | nc_notif_free(notif); |
Michal Vasko | 0651c90 | 2016-05-19 15:55:42 +0200 | [diff] [blame] | 1272 | } else if (msgtype == NC_MSG_ERROR) { |
| 1273 | break; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | usleep(NC_CLIENT_NOTIF_THREAD_SLEEP); |
| 1277 | } |
| 1278 | |
Michal Vasko | 0651c90 | 2016-05-19 15:55:42 +0200 | [diff] [blame] | 1279 | VRB("Session %u: notification thread exit.", session->id); |
| 1280 | session->ntf_tid = NULL; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1281 | return NULL; |
| 1282 | } |
| 1283 | |
| 1284 | API int |
| 1285 | nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif)) |
| 1286 | { |
| 1287 | struct nc_ntf_thread_arg *ntarg; |
| 1288 | int ret; |
| 1289 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1290 | if (!session) { |
| 1291 | ERRARG("session"); |
| 1292 | return -1; |
| 1293 | } else if (!notif_clb) { |
| 1294 | ERRARG("notif_clb"); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1295 | return -1; |
| 1296 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
| 1297 | ERR("Session %u: invalid session to receive Notifications.", session->id); |
| 1298 | return -1; |
| 1299 | } else if (session->ntf_tid) { |
| 1300 | ERR("Session %u: separate notification thread is already running.", session->id); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
| 1304 | ntarg = malloc(sizeof *ntarg); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1305 | if (!ntarg) { |
| 1306 | ERRMEM; |
| 1307 | return -1; |
| 1308 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1309 | ntarg->session = session; |
| 1310 | ntarg->notif_clb = notif_clb; |
| 1311 | |
| 1312 | /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */ |
| 1313 | session->ntf_tid = malloc(sizeof *session->ntf_tid); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1314 | if (!session->ntf_tid) { |
| 1315 | ERRMEM; |
| 1316 | free(ntarg); |
| 1317 | return -1; |
| 1318 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1319 | |
| 1320 | ret = pthread_create((pthread_t *)session->ntf_tid, NULL, nc_recv_notif_thread, ntarg); |
| 1321 | if (ret) { |
| 1322 | ERR("Session %u: failed to create a new thread (%s).", strerror(errno)); |
| 1323 | free(ntarg); |
| 1324 | free((pthread_t *)session->ntf_tid); |
| 1325 | session->ntf_tid = NULL; |
| 1326 | return -1; |
| 1327 | } |
| 1328 | |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1332 | API NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1333 | nc_send_rpc(struct nc_session *session, struct nc_rpc *rpc, int timeout, uint64_t *msgid) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1334 | { |
| 1335 | NC_MSG_TYPE r; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1336 | int ret; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1337 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1338 | struct nc_rpc_getconfig *rpc_gc; |
| 1339 | struct nc_rpc_edit *rpc_e; |
| 1340 | struct nc_rpc_copy *rpc_cp; |
| 1341 | struct nc_rpc_delete *rpc_del; |
| 1342 | struct nc_rpc_lock *rpc_lock; |
| 1343 | struct nc_rpc_get *rpc_g; |
| 1344 | struct nc_rpc_kill *rpc_k; |
| 1345 | struct nc_rpc_commit *rpc_com; |
| 1346 | struct nc_rpc_cancel *rpc_can; |
| 1347 | struct nc_rpc_validate *rpc_val; |
| 1348 | struct nc_rpc_getschema *rpc_gs; |
| 1349 | struct nc_rpc_subscribe *rpc_sub; |
| 1350 | struct lyd_node *data, *node; |
Michal Vasko | 9d8bee6 | 2016-03-03 10:58:24 +0100 | [diff] [blame] | 1351 | const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL; |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1352 | char str[11]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1353 | uint64_t cur_msgid; |
| 1354 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1355 | if (!session) { |
| 1356 | ERRARG("session"); |
| 1357 | return NC_MSG_ERROR; |
| 1358 | } else if (!rpc) { |
| 1359 | ERRARG("rpc"); |
| 1360 | return NC_MSG_ERROR; |
| 1361 | } else if (!msgid) { |
| 1362 | ERRARG("msgid"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1363 | return NC_MSG_ERROR; |
| 1364 | } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1365 | ERR("Session %u: invalid session to send RPCs.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1366 | return NC_MSG_ERROR; |
| 1367 | } |
| 1368 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1369 | if ((rpc->type != NC_RPC_GETSCHEMA) && (rpc->type != NC_RPC_ACT_GENERIC) && (rpc->type != NC_RPC_SUBSCRIBE)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1370 | ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL); |
| 1371 | if (!ietfnc) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1372 | ERR("Session %u: missing ietf-netconf schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1373 | return NC_MSG_ERROR; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1378 | case NC_RPC_ACT_GENERIC: |
| 1379 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1380 | |
| 1381 | if (rpc_gen->has_data) { |
| 1382 | data = rpc_gen->content.data; |
| 1383 | } else { |
Michal Vasko | 68b3f29 | 2016-09-16 12:00:32 +0200 | [diff] [blame] | 1384 | data = lyd_parse_mem(session->ctx, rpc_gen->content.xml_str, LYD_XML, LYD_OPT_RPC | LYD_OPT_STRICT, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1385 | } |
| 1386 | break; |
| 1387 | |
| 1388 | case NC_RPC_GETCONFIG: |
| 1389 | rpc_gc = (struct nc_rpc_getconfig *)rpc; |
| 1390 | |
| 1391 | data = lyd_new(NULL, ietfnc, "get-config"); |
| 1392 | node = lyd_new(data, ietfnc, "source"); |
| 1393 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL); |
| 1394 | if (!node) { |
| 1395 | lyd_free(data); |
| 1396 | return NC_MSG_ERROR; |
| 1397 | } |
| 1398 | if (rpc_gc->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 1399 | if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame^] | 1400 | node = lyd_new_anydata(data, ietfnc, "filter", rpc_gc->filter, LYD_ANYDATA_SXML); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1401 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1402 | } else { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1403 | node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1404 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 1405 | lyd_insert_attr(node, NULL, "select", rpc_gc->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1406 | } |
| 1407 | if (!node) { |
| 1408 | lyd_free(data); |
| 1409 | return NC_MSG_ERROR; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | if (rpc_gc->wd_mode) { |
| 1414 | if (!ietfncwd) { |
| 1415 | ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL); |
| 1416 | if (!ietfncwd) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1417 | ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1418 | return NC_MSG_ERROR; |
| 1419 | } |
| 1420 | } |
| 1421 | switch (rpc_gc->wd_mode) { |
| 1422 | case NC_WD_UNKNOWN: |
| 1423 | /* cannot get here */ |
| 1424 | break; |
| 1425 | case NC_WD_ALL: |
| 1426 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 1427 | break; |
| 1428 | case NC_WD_ALL_TAG: |
| 1429 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 1430 | break; |
| 1431 | case NC_WD_TRIM: |
| 1432 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 1433 | break; |
| 1434 | case NC_WD_EXPLICIT: |
| 1435 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 1436 | break; |
| 1437 | } |
| 1438 | if (!node) { |
| 1439 | lyd_free(data); |
| 1440 | return NC_MSG_ERROR; |
| 1441 | } |
| 1442 | } |
| 1443 | break; |
| 1444 | |
| 1445 | case NC_RPC_EDIT: |
| 1446 | rpc_e = (struct nc_rpc_edit *)rpc; |
| 1447 | |
| 1448 | data = lyd_new(NULL, ietfnc, "edit-config"); |
| 1449 | node = lyd_new(data, ietfnc, "target"); |
| 1450 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL); |
| 1451 | if (!node) { |
| 1452 | lyd_free(data); |
| 1453 | return NC_MSG_ERROR; |
| 1454 | } |
| 1455 | |
| 1456 | if (rpc_e->default_op) { |
| 1457 | node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]); |
| 1458 | if (!node) { |
| 1459 | lyd_free(data); |
| 1460 | return NC_MSG_ERROR; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | if (rpc_e->test_opt) { |
| 1465 | node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]); |
| 1466 | if (!node) { |
| 1467 | lyd_free(data); |
| 1468 | return NC_MSG_ERROR; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | if (rpc_e->error_opt) { |
| 1473 | node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]); |
| 1474 | if (!node) { |
| 1475 | lyd_free(data); |
| 1476 | return NC_MSG_ERROR; |
| 1477 | } |
| 1478 | } |
| 1479 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 1480 | if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame^] | 1481 | node = lyd_new_anydata(data, ietfnc, "config", rpc_e->edit_cont, LYD_ANYDATA_SXML); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1482 | } else { |
| 1483 | node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont); |
| 1484 | } |
| 1485 | if (!node) { |
| 1486 | lyd_free(data); |
| 1487 | return NC_MSG_ERROR; |
| 1488 | } |
| 1489 | break; |
| 1490 | |
| 1491 | case NC_RPC_COPY: |
| 1492 | rpc_cp = (struct nc_rpc_copy *)rpc; |
| 1493 | |
| 1494 | data = lyd_new(NULL, ietfnc, "copy-config"); |
| 1495 | node = lyd_new(data, ietfnc, "target"); |
| 1496 | if (rpc_cp->url_trg) { |
| 1497 | node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg); |
| 1498 | } else { |
| 1499 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL); |
| 1500 | } |
| 1501 | if (!node) { |
| 1502 | lyd_free(data); |
| 1503 | return NC_MSG_ERROR; |
| 1504 | } |
| 1505 | |
| 1506 | node = lyd_new(data, ietfnc, "source"); |
| 1507 | if (rpc_cp->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 1508 | if (!rpc_cp->url_config_src[0] || (rpc_cp->url_config_src[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame^] | 1509 | node = lyd_new_anydata(node, ietfnc, "config", rpc_cp->url_config_src, LYD_ANYDATA_SXML); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1510 | } else { |
| 1511 | node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src); |
| 1512 | } |
| 1513 | } else { |
| 1514 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL); |
| 1515 | } |
| 1516 | if (!node) { |
| 1517 | lyd_free(data); |
| 1518 | return NC_MSG_ERROR; |
| 1519 | } |
| 1520 | |
| 1521 | if (rpc_cp->wd_mode) { |
| 1522 | if (!ietfncwd) { |
| 1523 | ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL); |
| 1524 | if (!ietfncwd) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1525 | ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1526 | return NC_MSG_ERROR; |
| 1527 | } |
| 1528 | } |
| 1529 | switch (rpc_cp->wd_mode) { |
| 1530 | case NC_WD_UNKNOWN: |
| 1531 | /* cannot get here */ |
| 1532 | break; |
| 1533 | case NC_WD_ALL: |
| 1534 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 1535 | break; |
| 1536 | case NC_WD_ALL_TAG: |
| 1537 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 1538 | break; |
| 1539 | case NC_WD_TRIM: |
| 1540 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 1541 | break; |
| 1542 | case NC_WD_EXPLICIT: |
| 1543 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 1544 | break; |
| 1545 | } |
| 1546 | if (!node) { |
| 1547 | lyd_free(data); |
| 1548 | return NC_MSG_ERROR; |
| 1549 | } |
| 1550 | } |
| 1551 | break; |
| 1552 | |
| 1553 | case NC_RPC_DELETE: |
| 1554 | rpc_del = (struct nc_rpc_delete *)rpc; |
| 1555 | |
| 1556 | data = lyd_new(NULL, ietfnc, "delete-config"); |
| 1557 | node = lyd_new(data, ietfnc, "target"); |
| 1558 | if (rpc_del->url) { |
| 1559 | node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url); |
| 1560 | } else { |
| 1561 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL); |
| 1562 | } |
| 1563 | if (!node) { |
| 1564 | lyd_free(data); |
| 1565 | return NC_MSG_ERROR; |
| 1566 | } |
| 1567 | break; |
| 1568 | |
| 1569 | case NC_RPC_LOCK: |
| 1570 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 1571 | |
| 1572 | data = lyd_new(NULL, ietfnc, "lock"); |
| 1573 | node = lyd_new(data, ietfnc, "target"); |
| 1574 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL); |
| 1575 | if (!node) { |
| 1576 | lyd_free(data); |
| 1577 | return NC_MSG_ERROR; |
| 1578 | } |
| 1579 | break; |
| 1580 | |
| 1581 | case NC_RPC_UNLOCK: |
| 1582 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 1583 | |
| 1584 | data = lyd_new(NULL, ietfnc, "unlock"); |
| 1585 | node = lyd_new(data, ietfnc, "target"); |
| 1586 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL); |
| 1587 | if (!node) { |
| 1588 | lyd_free(data); |
| 1589 | return NC_MSG_ERROR; |
| 1590 | } |
| 1591 | break; |
| 1592 | |
| 1593 | case NC_RPC_GET: |
| 1594 | rpc_g = (struct nc_rpc_get *)rpc; |
| 1595 | |
| 1596 | data = lyd_new(NULL, ietfnc, "get"); |
| 1597 | if (rpc_g->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 1598 | if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame^] | 1599 | node = lyd_new_anydata(data, ietfnc, "filter", rpc_g->filter, LYD_ANYDATA_SXML); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1600 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1601 | } else { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1602 | node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1603 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 1604 | lyd_insert_attr(node, NULL, "select", rpc_g->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1605 | } |
| 1606 | if (!node) { |
| 1607 | lyd_free(data); |
| 1608 | return NC_MSG_ERROR; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (rpc_g->wd_mode) { |
| 1613 | if (!ietfncwd) { |
| 1614 | ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL); |
| 1615 | if (!ietfncwd) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1616 | ERR("Session %u: missing ietf-netconf-with-defaults schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1617 | return NC_MSG_ERROR; |
| 1618 | } |
| 1619 | } |
| 1620 | switch (rpc_g->wd_mode) { |
| 1621 | case NC_WD_UNKNOWN: |
| 1622 | /* cannot get here */ |
| 1623 | break; |
| 1624 | case NC_WD_ALL: |
| 1625 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 1626 | break; |
| 1627 | case NC_WD_ALL_TAG: |
| 1628 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 1629 | break; |
| 1630 | case NC_WD_TRIM: |
| 1631 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 1632 | break; |
| 1633 | case NC_WD_EXPLICIT: |
| 1634 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 1635 | break; |
| 1636 | } |
| 1637 | if (!node) { |
| 1638 | lyd_free(data); |
| 1639 | return NC_MSG_ERROR; |
| 1640 | } |
| 1641 | } |
| 1642 | break; |
| 1643 | |
| 1644 | case NC_RPC_KILL: |
| 1645 | rpc_k = (struct nc_rpc_kill *)rpc; |
| 1646 | |
| 1647 | data = lyd_new(NULL, ietfnc, "kill-session"); |
| 1648 | sprintf(str, "%u", rpc_k->sid); |
| 1649 | lyd_new_leaf(data, ietfnc, "session-id", str); |
| 1650 | break; |
| 1651 | |
| 1652 | case NC_RPC_COMMIT: |
| 1653 | rpc_com = (struct nc_rpc_commit *)rpc; |
| 1654 | |
| 1655 | data = lyd_new(NULL, ietfnc, "commit"); |
| 1656 | if (rpc_com->confirmed) { |
| 1657 | lyd_new_leaf(data, ietfnc, "confirmed", NULL); |
| 1658 | } |
| 1659 | |
| 1660 | if (rpc_com->confirm_timeout) { |
| 1661 | sprintf(str, "%u", rpc_com->confirm_timeout); |
| 1662 | lyd_new_leaf(data, ietfnc, "confirm-timeout", str); |
| 1663 | } |
| 1664 | |
| 1665 | if (rpc_com->persist) { |
| 1666 | node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist); |
| 1667 | if (!node) { |
| 1668 | lyd_free(data); |
| 1669 | return NC_MSG_ERROR; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | if (rpc_com->persist_id) { |
| 1674 | node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id); |
| 1675 | if (!node) { |
| 1676 | lyd_free(data); |
| 1677 | return NC_MSG_ERROR; |
| 1678 | } |
| 1679 | } |
| 1680 | break; |
| 1681 | |
| 1682 | case NC_RPC_DISCARD: |
| 1683 | data = lyd_new(NULL, ietfnc, "discard-changes"); |
| 1684 | break; |
| 1685 | |
| 1686 | case NC_RPC_CANCEL: |
| 1687 | rpc_can = (struct nc_rpc_cancel *)rpc; |
| 1688 | |
| 1689 | data = lyd_new(NULL, ietfnc, "cancel-commit"); |
| 1690 | if (rpc_can->persist_id) { |
| 1691 | node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id); |
| 1692 | if (!node) { |
| 1693 | lyd_free(data); |
| 1694 | return NC_MSG_ERROR; |
| 1695 | } |
| 1696 | } |
| 1697 | break; |
| 1698 | |
| 1699 | case NC_RPC_VALIDATE: |
| 1700 | rpc_val = (struct nc_rpc_validate *)rpc; |
| 1701 | |
| 1702 | data = lyd_new(NULL, ietfnc, "validate"); |
| 1703 | node = lyd_new(data, ietfnc, "source"); |
| 1704 | if (rpc_val->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 1705 | if (!rpc_val->url_config_src[0] || (rpc_val->url_config_src[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame^] | 1706 | node = lyd_new_anydata(node, ietfnc, "config", rpc_val->url_config_src, LYD_ANYDATA_SXML); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1707 | } else { |
| 1708 | node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src); |
| 1709 | } |
| 1710 | } else { |
| 1711 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL); |
| 1712 | } |
| 1713 | if (!node) { |
| 1714 | lyd_free(data); |
| 1715 | return NC_MSG_ERROR; |
| 1716 | } |
| 1717 | break; |
| 1718 | |
| 1719 | case NC_RPC_GETSCHEMA: |
| 1720 | ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL); |
| 1721 | if (!ietfncmon) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1722 | ERR("Session %u: missing ietf-netconf-monitoring schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1723 | return NC_MSG_ERROR; |
| 1724 | } |
| 1725 | |
| 1726 | rpc_gs = (struct nc_rpc_getschema *)rpc; |
| 1727 | |
| 1728 | data = lyd_new(NULL, ietfncmon, "get-schema"); |
| 1729 | node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier); |
| 1730 | if (!node) { |
| 1731 | lyd_free(data); |
| 1732 | return NC_MSG_ERROR; |
| 1733 | } |
| 1734 | if (rpc_gs->version) { |
| 1735 | node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version); |
| 1736 | if (!node) { |
| 1737 | lyd_free(data); |
| 1738 | return NC_MSG_ERROR; |
| 1739 | } |
| 1740 | } |
| 1741 | if (rpc_gs->format) { |
| 1742 | node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format); |
| 1743 | if (!node) { |
| 1744 | lyd_free(data); |
| 1745 | return NC_MSG_ERROR; |
| 1746 | } |
| 1747 | } |
| 1748 | break; |
| 1749 | |
| 1750 | case NC_RPC_SUBSCRIBE: |
| 1751 | notifs = ly_ctx_get_module(session->ctx, "notifications", NULL); |
| 1752 | if (!notifs) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1753 | ERR("Session %u: missing notifications schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1754 | return NC_MSG_ERROR; |
| 1755 | } |
| 1756 | |
| 1757 | rpc_sub = (struct nc_rpc_subscribe *)rpc; |
| 1758 | |
| 1759 | data = lyd_new(NULL, notifs, "create-subscription"); |
| 1760 | if (rpc_sub->stream) { |
| 1761 | node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream); |
| 1762 | if (!node) { |
| 1763 | lyd_free(data); |
| 1764 | return NC_MSG_ERROR; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | if (rpc_sub->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 1769 | if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame^] | 1770 | node = lyd_new_anydata(data, notifs, "filter", rpc_sub->filter, LYD_ANYDATA_SXML); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1771 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1772 | } else { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1773 | node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1774 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 1775 | lyd_insert_attr(node, NULL, "select", rpc_sub->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1776 | } |
| 1777 | if (!node) { |
| 1778 | lyd_free(data); |
| 1779 | return NC_MSG_ERROR; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | if (rpc_sub->start) { |
| 1784 | node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start); |
| 1785 | if (!node) { |
| 1786 | lyd_free(data); |
| 1787 | return NC_MSG_ERROR; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | if (rpc_sub->stop) { |
| 1792 | node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop); |
| 1793 | if (!node) { |
| 1794 | lyd_free(data); |
| 1795 | return NC_MSG_ERROR; |
| 1796 | } |
| 1797 | } |
| 1798 | break; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1799 | default: |
| 1800 | ERRINT; |
| 1801 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1802 | } |
| 1803 | |
Michal Vasko | 49a0cf9 | 2016-09-14 09:15:32 +0200 | [diff] [blame] | 1804 | if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT, NULL)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1805 | lyd_free(data); |
| 1806 | return NC_MSG_ERROR; |
| 1807 | } |
| 1808 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 1809 | ret = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1810 | if (ret == -1) { |
| 1811 | /* error */ |
| 1812 | r = NC_MSG_ERROR; |
| 1813 | } else if (!ret) { |
| 1814 | /* blocking */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1815 | r = NC_MSG_WOULDBLOCK; |
| 1816 | } else { |
| 1817 | /* send RPC, store its message ID */ |
| 1818 | r = nc_send_msg(session, data); |
| 1819 | cur_msgid = session->msgid; |
| 1820 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1821 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1822 | |
| 1823 | lyd_free(data); |
| 1824 | |
| 1825 | if (r != NC_MSG_RPC) { |
| 1826 | return r; |
| 1827 | } |
| 1828 | |
| 1829 | *msgid = cur_msgid; |
| 1830 | return NC_MSG_RPC; |
| 1831 | } |