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 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 300 | assert(session->opts.client.cpblts && session->ctx); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 301 | |
| 302 | /* check if get-schema is supported */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 303 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
| 304 | if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring", 51)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 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 | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 322 | if (ctx_check_and_load_ietf_netconf(session->ctx, session->opts.client.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 */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 330 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
| 331 | if (!strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:capability", 34) |
| 332 | || !strncmp(session->opts.client.cpblts[i], "urn:ietf:params:netconf:base", 28)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 333 | continue; |
| 334 | } |
| 335 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 336 | r = ctx_check_and_load_model(session, session->opts.client.cpblts[i]); |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 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); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 349 | r = ctx_check_and_load_model(session, session->opts.client.cpblts[i]); |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 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 | |
Michal Vasko | 1f0b2ac | 2016-10-13 10:33:50 +0200 | [diff] [blame] | 448 | for (res = res_list; res != NULL; res = res->ai_next) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 449 | sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 450 | if (sock == -1) { |
| 451 | /* socket was not created, try another resource */ |
Michal Vasko | 1f0b2ac | 2016-10-13 10:33:50 +0200 | [diff] [blame] | 452 | continue; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) { |
| 456 | /* network connection failed, try another resource */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 457 | close(sock); |
| 458 | sock = -1; |
Michal Vasko | 1f0b2ac | 2016-10-13 10:33:50 +0200 | [diff] [blame] | 459 | continue; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 462 | /* make the socket non-blocking */ |
| 463 | if (((flags = fcntl(sock, F_GETFL)) == -1) || (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)) { |
| 464 | ERR("Fcntl failed (%s).", strerror(errno)); |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 465 | close(sock); |
Michal Vasko | a8a66b6 | 2016-10-05 14:14:19 +0200 | [diff] [blame] | 466 | freeaddrinfo(res_list); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 467 | return -1; |
| 468 | } |
| 469 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 470 | /* we're done, network connection established */ |
| 471 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Michal Vasko | 29af44b | 2016-10-13 10:59:55 +0200 | [diff] [blame] | 474 | if (sock != -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 475 | 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] | 476 | } |
| 477 | freeaddrinfo(res_list); |
| 478 | |
| 479 | return sock; |
| 480 | } |
| 481 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 482 | static NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 483 | 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] | 484 | { |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 485 | int r; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 486 | char *ptr; |
| 487 | const char *str_msgid; |
| 488 | uint64_t cur_msgid; |
| 489 | struct lyxml_elem *xml; |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 490 | struct nc_msg_cont *cont, **cont_ptr; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 491 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 492 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 493 | r = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 494 | if (r == -1) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 495 | /* error */ |
| 496 | return NC_MSG_ERROR; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 497 | } else if (!r) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 498 | /* timeout */ |
| 499 | return NC_MSG_WOULDBLOCK; |
| 500 | } |
| 501 | |
| 502 | /* try to get notification from the session's queue */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 503 | if (!msgid && session->opts.client.notifs) { |
| 504 | cont = session->opts.client.notifs; |
| 505 | session->opts.client.notifs = cont->next; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 506 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 507 | xml = cont->msg; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 508 | free(cont); |
| 509 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 510 | msgtype = NC_MSG_NOTIF; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /* try to get rpc-reply from the session's queue */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 514 | if (msgid && session->opts.client.replies) { |
| 515 | cont = session->opts.client.replies; |
| 516 | session->opts.client.replies = cont->next; |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 517 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 518 | xml = cont->msg; |
| 519 | free(cont); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 520 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 521 | msgtype = NC_MSG_REPLY; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 524 | if (!msgtype) { |
| 525 | /* read message from wire */ |
| 526 | msgtype = nc_read_msg_poll(session, timeout, &xml); |
| 527 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 528 | |
| 529 | /* we read rpc-reply, want a notif */ |
| 530 | if (!msgid && (msgtype == NC_MSG_REPLY)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 531 | cont_ptr = &session->opts.client.replies; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 532 | while (*cont_ptr) { |
| 533 | cont_ptr = &((*cont_ptr)->next); |
| 534 | } |
| 535 | *cont_ptr = malloc(sizeof **cont_ptr); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 536 | if (!*cont_ptr) { |
| 537 | ERRMEM; |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 538 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 539 | lyxml_free(session->ctx, xml); |
| 540 | return NC_MSG_ERROR; |
| 541 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 542 | (*cont_ptr)->msg = xml; |
| 543 | (*cont_ptr)->next = NULL; |
| 544 | } |
| 545 | |
| 546 | /* we read notif, want a rpc-reply */ |
| 547 | if (msgid && (msgtype == NC_MSG_NOTIF)) { |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 548 | /* TODO check whether the session is even subscribed */ |
| 549 | /*if (!session->notif) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 550 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 551 | 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] | 552 | lyxml_free(session->ctx, xml); |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 553 | return NC_MSG_ERROR; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 554 | }*/ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 555 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 556 | cont_ptr = &session->opts.client.notifs; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 557 | while (*cont_ptr) { |
| 558 | cont_ptr = &((*cont_ptr)->next); |
| 559 | } |
| 560 | *cont_ptr = malloc(sizeof **cont_ptr); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 561 | if (!cont_ptr) { |
| 562 | ERRMEM; |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 563 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 564 | lyxml_free(session->ctx, xml); |
| 565 | return NC_MSG_ERROR; |
| 566 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 567 | (*cont_ptr)->msg = xml; |
| 568 | (*cont_ptr)->next = NULL; |
| 569 | } |
| 570 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 571 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 572 | |
| 573 | switch (msgtype) { |
| 574 | case NC_MSG_NOTIF: |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 575 | if (!msgid) { |
| 576 | *msg = xml; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 577 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 578 | break; |
| 579 | |
| 580 | case NC_MSG_REPLY: |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 581 | if (msgid) { |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 582 | /* check message-id */ |
| 583 | str_msgid = lyxml_get_attr(xml, "message-id", NULL); |
| 584 | if (!str_msgid) { |
| 585 | ERR("Session %u: received a <rpc-reply> without a message-id.", session->id); |
| 586 | msgtype = NC_MSG_REPLY_ERR_MSGID; |
| 587 | } else { |
| 588 | cur_msgid = strtoul(str_msgid, &ptr, 10); |
| 589 | if (cur_msgid != msgid) { |
| 590 | ERR("Session %u: received a <rpc-reply> with an unexpected message-id \"%s\".", |
| 591 | session->id, str_msgid); |
| 592 | msgtype = NC_MSG_REPLY_ERR_MSGID; |
| 593 | } |
| 594 | } |
Michal Vasko | 2518b6b | 2016-01-28 13:24:53 +0100 | [diff] [blame] | 595 | *msg = xml; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 596 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 597 | break; |
| 598 | |
| 599 | case NC_MSG_HELLO: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 600 | ERR("Session %u: received another <hello> message.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 601 | lyxml_free(session->ctx, xml); |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 602 | msgtype = NC_MSG_ERROR; |
| 603 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 604 | |
| 605 | case NC_MSG_RPC: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 606 | ERR("Session %u: received <rpc> from a NETCONF server.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 607 | lyxml_free(session->ctx, xml); |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 608 | msgtype = NC_MSG_ERROR; |
| 609 | break; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 610 | |
| 611 | default: |
| 612 | /* NC_MSG_WOULDBLOCK and NC_MSG_ERROR - pass it out; |
| 613 | * NC_MSG_NONE is not returned by nc_read_msg() |
| 614 | */ |
| 615 | break; |
| 616 | } |
| 617 | |
| 618 | return msgtype; |
| 619 | } |
| 620 | |
| 621 | /* cannot strictly fail, but does not need to fill any error parameter at all */ |
| 622 | static void |
| 623 | parse_rpc_error(struct ly_ctx *ctx, struct lyxml_elem *xml, struct nc_err *err) |
| 624 | { |
| 625 | struct lyxml_elem *iter, *next, *info; |
| 626 | |
| 627 | LY_TREE_FOR(xml->child, iter) { |
| 628 | if (!iter->ns) { |
| 629 | if (iter->content) { |
| 630 | WRN("<rpc-error> child \"%s\" with value \"%s\" without namespace.", iter->name, iter->content); |
| 631 | } else { |
| 632 | WRN("<rpc-error> child \"%s\" without namespace.", iter->name); |
| 633 | } |
| 634 | continue; |
| 635 | } else if (strcmp(iter->ns->value, NC_NS_BASE)) { |
| 636 | if (iter->content) { |
| 637 | WRN("<rpc-error> child \"%s\" with value \"%s\" in an unknown namespace \"%s\".", |
| 638 | iter->name, iter->content, iter->ns->value); |
| 639 | } else { |
| 640 | WRN("<rpc-error> child \"%s\" in an unknown namespace \"%s\".", iter->name, iter->ns->value); |
| 641 | } |
| 642 | continue; |
| 643 | } |
| 644 | |
| 645 | if (!strcmp(iter->name, "error-type")) { |
| 646 | if (!iter->content || (strcmp(iter->content, "transport") && strcmp(iter->content, "rpc") |
| 647 | && strcmp(iter->content, "protocol") && strcmp(iter->content, "application"))) { |
| 648 | WRN("<rpc-error> <error-type> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 649 | } else if (err->type) { |
| 650 | WRN("<rpc-error> <error-type> duplicated."); |
| 651 | } else { |
| 652 | err->type = lydict_insert(ctx, iter->content, 0); |
| 653 | } |
| 654 | } else if (!strcmp(iter->name, "error-tag")) { |
| 655 | if (!iter->content || (strcmp(iter->content, "in-use") && strcmp(iter->content, "invalid-value") |
| 656 | && strcmp(iter->content, "too-big") && strcmp(iter->content, "missing-attribute") |
| 657 | && strcmp(iter->content, "bad-attribute") && strcmp(iter->content, "unknown-attribute") |
| 658 | && strcmp(iter->content, "missing-element") && strcmp(iter->content, "bad-element") |
| 659 | && strcmp(iter->content, "unknown-element") && strcmp(iter->content, "unknown-namespace") |
| 660 | && strcmp(iter->content, "access-denied") && strcmp(iter->content, "lock-denied") |
| 661 | && strcmp(iter->content, "resource-denied") && strcmp(iter->content, "rollback-failed") |
| 662 | && strcmp(iter->content, "data-exists") && strcmp(iter->content, "data-missing") |
| 663 | && strcmp(iter->content, "operation-not-supported") && strcmp(iter->content, "operation-failed") |
| 664 | && strcmp(iter->content, "malformed-message"))) { |
| 665 | WRN("<rpc-error> <error-tag> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 666 | } else if (err->tag) { |
| 667 | WRN("<rpc-error> <error-tag> duplicated."); |
| 668 | } else { |
| 669 | err->tag = lydict_insert(ctx, iter->content, 0); |
| 670 | } |
| 671 | } else if (!strcmp(iter->name, "error-severity")) { |
| 672 | if (!iter->content || (strcmp(iter->content, "error") && strcmp(iter->content, "warning"))) { |
| 673 | WRN("<rpc-error> <error-severity> unknown value \"%s\".", (iter->content ? iter->content : "")); |
| 674 | } else if (err->severity) { |
| 675 | WRN("<rpc-error> <error-severity> duplicated."); |
| 676 | } else { |
| 677 | err->severity = lydict_insert(ctx, iter->content, 0); |
| 678 | } |
| 679 | } else if (!strcmp(iter->name, "error-app-tag")) { |
| 680 | if (err->apptag) { |
| 681 | WRN("<rpc-error> <error-app-tag> duplicated."); |
| 682 | } else { |
| 683 | err->apptag = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 684 | } |
| 685 | } else if (!strcmp(iter->name, "error-path")) { |
| 686 | if (err->path) { |
| 687 | WRN("<rpc-error> <error-path> duplicated."); |
| 688 | } else { |
| 689 | err->path = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 690 | } |
| 691 | } else if (!strcmp(iter->name, "error-message")) { |
| 692 | if (err->message) { |
| 693 | WRN("<rpc-error> <error-message> duplicated."); |
| 694 | } else { |
| 695 | err->message_lang = lyxml_get_attr(iter, "xml:lang", NULL); |
| 696 | if (!err->message_lang) { |
| 697 | VRB("<rpc-error> <error-message> without the recommended \"xml:lang\" attribute."); |
| 698 | } |
| 699 | err->message = lydict_insert(ctx, (iter->content ? iter->content : ""), 0); |
| 700 | } |
| 701 | } else if (!strcmp(iter->name, "error-info")) { |
| 702 | LY_TREE_FOR_SAFE(iter->child, next, info) { |
| 703 | if (info->ns && !strcmp(info->ns->value, NC_NS_BASE)) { |
| 704 | if (!strcmp(info->name, "session-id")) { |
| 705 | if (err->sid) { |
| 706 | WRN("<rpc-error> <error-info> <session-id> duplicated."); |
| 707 | } else { |
| 708 | err->sid = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 709 | } |
| 710 | } else if (!strcmp(info->name, "bad-attr")) { |
| 711 | ++err->attr_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 712 | err->attr = nc_realloc(err->attr, err->attr_count * sizeof *err->attr); |
| 713 | if (!err->attr) { |
| 714 | ERRMEM; |
| 715 | return; |
| 716 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 717 | err->attr[err->attr_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 718 | } else if (!strcmp(info->name, "bad-element")) { |
| 719 | ++err->elem_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 720 | err->elem = nc_realloc(err->elem, err->elem_count * sizeof *err->elem); |
| 721 | if (!err->elem) { |
| 722 | ERRMEM; |
| 723 | return; |
| 724 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 725 | err->elem[err->elem_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 726 | } else if (!strcmp(info->name, "bad-namespace")) { |
| 727 | ++err->ns_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 728 | err->ns = nc_realloc(err->ns, err->ns_count * sizeof *err->ns); |
| 729 | if (!err->ns) { |
| 730 | ERRMEM; |
| 731 | return; |
| 732 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 733 | err->ns[err->ns_count - 1] = lydict_insert(ctx, (info->content ? info->content : ""), 0); |
| 734 | } else { |
| 735 | if (info->content) { |
| 736 | WRN("<rpc-error> <error-info> unknown child \"%s\" with value \"%s\".", |
| 737 | info->name, info->content); |
| 738 | } else { |
| 739 | WRN("<rpc-error> <error-info> unknown child \"%s\".", info->name); |
| 740 | } |
| 741 | } |
| 742 | } else { |
| 743 | lyxml_unlink(ctx, info); |
| 744 | ++err->other_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 745 | err->other = nc_realloc(err->other, err->other_count * sizeof *err->other); |
| 746 | if (!err->other) { |
| 747 | ERRMEM; |
| 748 | return; |
| 749 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 750 | err->other[err->other_count - 1] = info; |
| 751 | } |
| 752 | } |
| 753 | } else { |
| 754 | if (iter->content) { |
| 755 | WRN("<rpc-error> unknown child \"%s\" with value \"%s\".", iter->name, iter->content); |
| 756 | } else { |
| 757 | WRN("<rpc-error> unknown child \"%s\".", iter->name); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | static struct nc_reply * |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 764 | 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] | 765 | { |
| 766 | struct lyxml_elem *iter; |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 767 | struct lyd_node *data = NULL, *rpc_act = NULL; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 768 | struct nc_client_reply_error *error_rpl; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 769 | struct nc_reply_data *data_rpl; |
| 770 | struct nc_reply *reply = NULL; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 771 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 772 | int i; |
| 773 | |
| 774 | if (!xml->child) { |
| 775 | ERR("An empty <rpc-reply>."); |
| 776 | return NULL; |
| 777 | } |
| 778 | |
| 779 | /* rpc-error */ |
| 780 | if (!strcmp(xml->child->name, "rpc-error") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) { |
| 781 | /* count and check elements */ |
| 782 | i = 0; |
| 783 | LY_TREE_FOR(xml->child, iter) { |
| 784 | if (strcmp(iter->name, "rpc-error")) { |
| 785 | ERR("<rpc-reply> content mismatch (<rpc-error> and <%s>).", iter->name); |
| 786 | return NULL; |
| 787 | } else if (!iter->ns) { |
| 788 | ERR("<rpc-reply> content mismatch (<rpc-error> without namespace)."); |
| 789 | return NULL; |
| 790 | } else if (strcmp(iter->ns->value, NC_NS_BASE)) { |
| 791 | ERR("<rpc-reply> content mismatch (<rpc-error> with NS \"%s\").", iter->ns->value); |
| 792 | return NULL; |
| 793 | } |
| 794 | ++i; |
| 795 | } |
| 796 | |
| 797 | error_rpl = malloc(sizeof *error_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 798 | if (!error_rpl) { |
| 799 | ERRMEM; |
| 800 | return NULL; |
| 801 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 802 | error_rpl->type = NC_RPL_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 803 | error_rpl->err = calloc(i, sizeof *error_rpl->err); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 804 | if (!error_rpl->err) { |
| 805 | ERRMEM; |
| 806 | free(error_rpl); |
| 807 | return NULL; |
| 808 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 809 | error_rpl->count = i; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 810 | error_rpl->ctx = ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 811 | reply = (struct nc_reply *)error_rpl; |
| 812 | |
| 813 | i = 0; |
| 814 | LY_TREE_FOR(xml->child, iter) { |
| 815 | parse_rpc_error(ctx, iter, error_rpl->err + i); |
| 816 | ++i; |
| 817 | } |
| 818 | |
| 819 | /* ok */ |
| 820 | } else if (!strcmp(xml->child->name, "ok") && xml->child->ns && !strcmp(xml->child->ns->value, NC_NS_BASE)) { |
| 821 | if (xml->child->next) { |
| 822 | ERR("<rpc-reply> content mismatch (<ok> and <%s>).", xml->child->next->name); |
| 823 | return NULL; |
| 824 | } |
| 825 | reply = malloc(sizeof *reply); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 826 | if (!reply) { |
| 827 | ERRMEM; |
| 828 | return NULL; |
| 829 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 830 | reply->type = NC_RPL_OK; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 831 | |
| 832 | /* some RPC output */ |
| 833 | } else { |
| 834 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 835 | case NC_RPC_ACT_GENERIC: |
| 836 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 837 | |
| 838 | if (rpc_gen->has_data) { |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 839 | rpc_act = rpc_gen->content.data; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 840 | } else { |
Michal Vasko | 0a5ae9a | 2016-11-15 11:54:47 +0100 | [diff] [blame] | 841 | rpc_act = lyd_parse_mem(ctx, rpc_gen->content.xml_str, |
| 842 | LYD_XML, LYD_OPT_RPC | LYD_OPT_STRICT | parseroptions, NULL); |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 843 | if (!rpc_act) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 844 | ERR("Failed to parse a generic RPC/action XML."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 845 | return NULL; |
| 846 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 847 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 848 | break; |
| 849 | |
| 850 | case NC_RPC_GETCONFIG: |
| 851 | case NC_RPC_GET: |
Michal Vasko | 13ed294 | 2016-02-29 16:21:00 +0100 | [diff] [blame] | 852 | if (!xml->child->child) { |
| 853 | /* we did not receive any data */ |
| 854 | data_rpl = malloc(sizeof *data_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 855 | if (!data_rpl) { |
| 856 | ERRMEM; |
| 857 | return NULL; |
| 858 | } |
Michal Vasko | 13ed294 | 2016-02-29 16:21:00 +0100 | [diff] [blame] | 859 | data_rpl->type = NC_RPL_DATA; |
| 860 | data_rpl->data = NULL; |
| 861 | return (struct nc_reply *)data_rpl; |
| 862 | } |
| 863 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 864 | /* special treatment */ |
Michal Vasko | 0a5ae9a | 2016-11-15 11:54:47 +0100 | [diff] [blame] | 865 | data = lyd_parse_xml(ctx, &xml->child->child, |
| 866 | LYD_OPT_DESTRUCT | (rpc->type == NC_RPC_GETCONFIG ? LYD_OPT_GETCONFIG : LYD_OPT_GET) |
| 867 | | LYD_OPT_STRICT | parseroptions); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 868 | if (!data) { |
| 869 | ERR("Failed to parse <%s> reply.", (rpc->type == NC_RPC_GETCONFIG ? "get-config" : "get")); |
| 870 | return NULL; |
| 871 | } |
| 872 | break; |
| 873 | |
| 874 | case NC_RPC_GETSCHEMA: |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 875 | rpc_act = lyd_new(NULL, ly_ctx_get_module(ctx, "ietf-netconf-monitoring", NULL), "get-schema"); |
| 876 | if (!rpc_act) { |
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_EDIT: |
| 883 | case NC_RPC_COPY: |
| 884 | case NC_RPC_DELETE: |
| 885 | case NC_RPC_LOCK: |
| 886 | case NC_RPC_UNLOCK: |
| 887 | case NC_RPC_KILL: |
| 888 | case NC_RPC_COMMIT: |
| 889 | case NC_RPC_DISCARD: |
| 890 | case NC_RPC_CANCEL: |
| 891 | case NC_RPC_VALIDATE: |
| 892 | case NC_RPC_SUBSCRIBE: |
| 893 | /* there is no output defined */ |
| 894 | ERR("Unexpected data reply (root elem \"%s\").", xml->child->name); |
| 895 | return NULL; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 896 | default: |
| 897 | ERRINT; |
| 898 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | data_rpl = malloc(sizeof *data_rpl); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 902 | if (!data_rpl) { |
| 903 | ERRMEM; |
| 904 | return NULL; |
| 905 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 906 | data_rpl->type = NC_RPL_DATA; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 907 | if (!data) { |
Michal Vasko | 0a5ae9a | 2016-11-15 11:54:47 +0100 | [diff] [blame] | 908 | data_rpl->data = lyd_parse_xml(ctx, &xml->child, |
| 909 | LYD_OPT_RPCREPLY | LYD_OPT_DESTRUCT | LYD_OPT_STRICT | parseroptions, |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 910 | rpc_act, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 911 | } else { |
| 912 | /* <get>, <get-config> */ |
| 913 | data_rpl->data = data; |
| 914 | } |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 915 | lyd_free_withsiblings(rpc_act); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 916 | if (!data_rpl->data) { |
| 917 | ERR("Failed to parse <rpc-reply>."); |
| 918 | free(data_rpl); |
| 919 | return NULL; |
| 920 | } |
| 921 | reply = (struct nc_reply *)data_rpl; |
| 922 | } |
| 923 | |
| 924 | return reply; |
| 925 | } |
| 926 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 927 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 928 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 929 | int |
| 930 | nc_client_ch_add_bind_listen(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 931 | { |
| 932 | int sock; |
| 933 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 934 | if (!address) { |
| 935 | ERRARG("address"); |
| 936 | return -1; |
| 937 | } else if (!port) { |
| 938 | ERRARG("port"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 939 | return -1; |
| 940 | } |
| 941 | |
| 942 | sock = nc_sock_listen(address, port); |
| 943 | if (sock == -1) { |
| 944 | return -1; |
| 945 | } |
| 946 | |
| 947 | ++client_opts.ch_bind_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 948 | client_opts.ch_binds = nc_realloc(client_opts.ch_binds, client_opts.ch_bind_count * sizeof *client_opts.ch_binds); |
| 949 | if (!client_opts.ch_binds) { |
| 950 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 951 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 952 | return -1; |
| 953 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 954 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 955 | client_opts.ch_bind_ti = nc_realloc(client_opts.ch_bind_ti, client_opts.ch_bind_count * sizeof *client_opts.ch_bind_ti); |
| 956 | if (!client_opts.ch_bind_ti) { |
| 957 | ERRMEM; |
| 958 | close(sock); |
| 959 | return -1; |
| 960 | } |
| 961 | client_opts.ch_bind_ti[client_opts.ch_bind_count - 1] = ti; |
| 962 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 963 | 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] | 964 | if (!client_opts.ch_binds[client_opts.ch_bind_count - 1].address) { |
| 965 | ERRMEM; |
Michal Vasko | 0f74da5 | 2016-03-03 08:52:52 +0100 | [diff] [blame] | 966 | close(sock); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 967 | return -1; |
| 968 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 969 | client_opts.ch_binds[client_opts.ch_bind_count - 1].port = port; |
| 970 | client_opts.ch_binds[client_opts.ch_bind_count - 1].sock = sock; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 971 | client_opts.ch_binds[client_opts.ch_bind_count - 1].pollin = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | int |
| 977 | nc_client_ch_del_bind(const char *address, uint16_t port, NC_TRANSPORT_IMPL ti) |
| 978 | { |
| 979 | uint32_t i; |
| 980 | int ret = -1; |
| 981 | |
| 982 | if (!address && !port && !ti) { |
| 983 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 984 | close(client_opts.ch_binds[i].sock); |
| 985 | free((char *)client_opts.ch_binds[i].address); |
| 986 | |
| 987 | ret = 0; |
| 988 | } |
| 989 | free(client_opts.ch_binds); |
| 990 | client_opts.ch_binds = NULL; |
| 991 | client_opts.ch_bind_count = 0; |
| 992 | } else { |
| 993 | for (i = 0; i < client_opts.ch_bind_count; ++i) { |
| 994 | if ((!address || !strcmp(client_opts.ch_binds[i].address, address)) |
| 995 | && (!port || (client_opts.ch_binds[i].port == port)) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 996 | && (!ti || (client_opts.ch_bind_ti[i] == ti))) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 997 | close(client_opts.ch_binds[i].sock); |
| 998 | free((char *)client_opts.ch_binds[i].address); |
| 999 | |
| 1000 | --client_opts.ch_bind_count; |
Michal Vasko | 66c762a | 2016-10-13 10:34:14 +0200 | [diff] [blame] | 1001 | if (!client_opts.ch_bind_count) { |
| 1002 | free(client_opts.ch_binds); |
| 1003 | client_opts.ch_binds = NULL; |
| 1004 | } else if (i < client_opts.ch_bind_count) { |
| 1005 | memcpy(&client_opts.ch_binds[i], &client_opts.ch_binds[client_opts.ch_bind_count], sizeof *client_opts.ch_binds); |
| 1006 | client_opts.ch_bind_ti[i] = client_opts.ch_bind_ti[client_opts.ch_bind_count]; |
| 1007 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1008 | |
| 1009 | ret = 0; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | API int |
| 1018 | nc_accept_callhome(int timeout, struct ly_ctx *ctx, struct nc_session **session) |
| 1019 | { |
| 1020 | int sock; |
| 1021 | char *host = NULL; |
| 1022 | uint16_t port, idx; |
| 1023 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1024 | if (!client_opts.ch_binds) { |
| 1025 | ERRINIT; |
| 1026 | return -1; |
| 1027 | } else if (!session) { |
| 1028 | ERRARG("session"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1029 | return -1; |
| 1030 | } |
| 1031 | |
| 1032 | sock = nc_sock_accept_binds(client_opts.ch_binds, client_opts.ch_bind_count, timeout, &host, &port, &idx); |
| 1033 | |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 1034 | if (sock < 1) { |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 1035 | free(host); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1036 | return sock; |
| 1037 | } |
| 1038 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1039 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1040 | if (client_opts.ch_bind_ti[idx] == NC_TI_LIBSSH) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1041 | *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] | 1042 | } else |
| 1043 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1044 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1045 | if (client_opts.ch_bind_ti[idx] == NC_TI_OPENSSL) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 1046 | *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] | 1047 | } else |
| 1048 | #endif |
| 1049 | { |
Michal Vasko | fee717c | 2016-02-01 13:25:43 +0100 | [diff] [blame] | 1050 | close(sock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1051 | *session = NULL; |
| 1052 | } |
| 1053 | |
| 1054 | free(host); |
| 1055 | |
| 1056 | if (!(*session)) { |
| 1057 | return -1; |
| 1058 | } |
| 1059 | |
| 1060 | return 1; |
| 1061 | } |
| 1062 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1063 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 1064 | |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1065 | API const char ** |
| 1066 | nc_session_get_cpblts(const struct nc_session *session) |
| 1067 | { |
| 1068 | if (!session) { |
| 1069 | ERRARG("session"); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1073 | return session->opts.client.cpblts; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | API const char * |
| 1077 | nc_session_cpblt(const struct nc_session *session, const char *capab) |
| 1078 | { |
| 1079 | int i, len; |
| 1080 | |
| 1081 | if (!session) { |
| 1082 | ERRARG("session"); |
| 1083 | return NULL; |
| 1084 | } else if (!capab) { |
| 1085 | ERRARG("capab"); |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | len = strlen(capab); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1090 | for (i = 0; session->opts.client.cpblts[i]; ++i) { |
| 1091 | if (!strncmp(session->opts.client.cpblts[i], capab, len)) { |
| 1092 | return session->opts.client.cpblts[i]; |
Michal Vasko | bdfb524 | 2016-05-24 09:11:01 +0200 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | return NULL; |
| 1097 | } |
| 1098 | |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1099 | API int |
| 1100 | nc_session_ntf_thread_running(const struct nc_session *session) |
| 1101 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1102 | if (!session || (session->side != NC_CLIENT)) { |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1103 | ERRARG("session"); |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1107 | return session->opts.client.ntf_tid ? 1 : 0; |
Michal Vasko | 9cd26a8 | 2016-05-31 08:58:48 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1110 | API void |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1111 | nc_client_init(void) |
| 1112 | { |
| 1113 | nc_init(); |
| 1114 | } |
| 1115 | |
| 1116 | API void |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1117 | nc_client_destroy(void) |
| 1118 | { |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 1119 | nc_client_set_schema_searchpath(NULL); |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1120 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
| 1121 | nc_client_ch_del_bind(NULL, 0, 0); |
| 1122 | #endif |
| 1123 | #ifdef NC_ENABLED_SSH |
| 1124 | nc_client_ssh_destroy_opts(); |
| 1125 | #endif |
Michal Vasko | c979d3a | 2016-02-26 15:26:21 +0100 | [diff] [blame] | 1126 | #ifdef NC_ENABLED_TLS |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1127 | nc_client_tls_destroy_opts(); |
| 1128 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1129 | nc_destroy(); |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1132 | API NC_MSG_TYPE |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 1133 | 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] | 1134 | { |
| 1135 | struct lyxml_elem *xml; |
| 1136 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 1137 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1138 | if (!session) { |
| 1139 | ERRARG("session"); |
| 1140 | return NC_MSG_ERROR; |
| 1141 | } else if (!rpc) { |
| 1142 | ERRARG("rpc"); |
| 1143 | return NC_MSG_ERROR; |
| 1144 | } else if (!reply) { |
| 1145 | ERRARG("reply"); |
| 1146 | return NC_MSG_ERROR; |
| 1147 | } else if (parseroptions & LYD_OPT_TYPEMASK) { |
| 1148 | ERRARG("parseroptions"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1149 | return NC_MSG_ERROR; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1150 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1151 | ERR("Session %u: invalid session to receive RPC replies.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1152 | return NC_MSG_ERROR; |
| 1153 | } |
Michal Vasko | eb7080e | 2016-02-18 13:27:05 +0100 | [diff] [blame] | 1154 | parseroptions &= ~(LYD_OPT_DESTRUCT | LYD_OPT_NOSIBLINGS); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1155 | *reply = NULL; |
| 1156 | |
| 1157 | msgtype = get_msg(session, timeout, msgid, &xml); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1158 | |
Michal Vasko | 71ba2da | 2016-05-04 10:53:16 +0200 | [diff] [blame] | 1159 | if ((msgtype == NC_MSG_REPLY) || (msgtype == NC_MSG_REPLY_ERR_MSGID)) { |
Michal Vasko | 0a5ae9a | 2016-11-15 11:54:47 +0100 | [diff] [blame] | 1160 | *reply = parse_reply(session->ctx, xml, rpc, LYD_OPT_STRICT | parseroptions); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1161 | lyxml_free(session->ctx, xml); |
| 1162 | if (!(*reply)) { |
| 1163 | return NC_MSG_ERROR; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return msgtype; |
| 1168 | } |
| 1169 | |
| 1170 | API NC_MSG_TYPE |
| 1171 | nc_recv_notif(struct nc_session *session, int timeout, struct nc_notif **notif) |
| 1172 | { |
| 1173 | struct lyxml_elem *xml, *ev_time; |
| 1174 | NC_MSG_TYPE msgtype = 0; /* NC_MSG_ERROR */ |
| 1175 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1176 | if (!session) { |
| 1177 | ERRARG("session"); |
| 1178 | return NC_MSG_ERROR; |
| 1179 | } else if (!notif) { |
| 1180 | ERRARG("notif"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1181 | return NC_MSG_ERROR; |
| 1182 | } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1183 | ERR("Session %u: invalid session to receive Notifications.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1184 | return NC_MSG_ERROR; |
| 1185 | } |
| 1186 | |
| 1187 | msgtype = get_msg(session, timeout, 0, &xml); |
| 1188 | |
| 1189 | if (msgtype == NC_MSG_NOTIF) { |
| 1190 | *notif = calloc(1, sizeof **notif); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1191 | if (!*notif) { |
| 1192 | ERRMEM; |
| 1193 | lyxml_free(session->ctx, xml); |
| 1194 | return NC_MSG_ERROR; |
| 1195 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1196 | |
| 1197 | /* eventTime */ |
| 1198 | LY_TREE_FOR(xml->child, ev_time) { |
| 1199 | if (!strcmp(ev_time->name, "eventTime")) { |
| 1200 | (*notif)->datetime = lydict_insert(session->ctx, ev_time->content, 0); |
| 1201 | /* lyd_parse does not know this element */ |
| 1202 | lyxml_free(session->ctx, ev_time); |
| 1203 | break; |
| 1204 | } |
| 1205 | } |
| 1206 | if (!(*notif)->datetime) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1207 | ERR("Session %u: notification is missing the \"eventTime\" element.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1208 | goto fail; |
| 1209 | } |
| 1210 | |
| 1211 | /* notification body */ |
Michal Vasko | 0a5ae9a | 2016-11-15 11:54:47 +0100 | [diff] [blame] | 1212 | (*notif)->tree = lyd_parse_xml(session->ctx, &xml->child, LYD_OPT_NOTIF | LYD_OPT_STRICT | LYD_OPT_DESTRUCT, NULL); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1213 | lyxml_free(session->ctx, xml); |
| 1214 | xml = NULL; |
| 1215 | if (!(*notif)->tree) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1216 | ERR("Session %u: failed to parse a new notification.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1217 | goto fail; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | return msgtype; |
| 1222 | |
| 1223 | fail: |
| 1224 | lydict_remove(session->ctx, (*notif)->datetime); |
| 1225 | lyd_free((*notif)->tree); |
| 1226 | free(*notif); |
| 1227 | *notif = NULL; |
| 1228 | lyxml_free(session->ctx, xml); |
| 1229 | |
| 1230 | return NC_MSG_ERROR; |
| 1231 | } |
| 1232 | |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1233 | static void * |
| 1234 | nc_recv_notif_thread(void *arg) |
| 1235 | { |
| 1236 | struct nc_ntf_thread_arg *ntarg; |
| 1237 | struct nc_session *session; |
| 1238 | void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif); |
| 1239 | struct nc_notif *notif; |
| 1240 | NC_MSG_TYPE msgtype; |
| 1241 | |
| 1242 | ntarg = (struct nc_ntf_thread_arg *)arg; |
| 1243 | session = ntarg->session; |
| 1244 | notif_clb = ntarg->notif_clb; |
| 1245 | free(ntarg); |
| 1246 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1247 | while (session->opts.client.ntf_tid) { |
Michal vasko | 4e1a36c | 2016-10-05 13:04:37 +0200 | [diff] [blame] | 1248 | msgtype = nc_recv_notif(session, NC_CLIENT_NOTIF_THREAD_SLEEP / 1000, ¬if); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1249 | if (msgtype == NC_MSG_NOTIF) { |
| 1250 | notif_clb(session, notif); |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 1251 | if (!strcmp(notif->tree->schema->name, "notificationComplete") |
| 1252 | && !strcmp(notif->tree->schema->module->name, "nc-notifications")) { |
| 1253 | nc_notif_free(notif); |
| 1254 | break; |
| 1255 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1256 | nc_notif_free(notif); |
Michal Vasko | 0651c90 | 2016-05-19 15:55:42 +0200 | [diff] [blame] | 1257 | } else if (msgtype == NC_MSG_ERROR) { |
| 1258 | break; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | usleep(NC_CLIENT_NOTIF_THREAD_SLEEP); |
| 1262 | } |
| 1263 | |
Michal Vasko | 0651c90 | 2016-05-19 15:55:42 +0200 | [diff] [blame] | 1264 | VRB("Session %u: notification thread exit.", session->id); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1265 | session->opts.client.ntf_tid = NULL; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1266 | return NULL; |
| 1267 | } |
| 1268 | |
| 1269 | API int |
| 1270 | nc_recv_notif_dispatch(struct nc_session *session, void (*notif_clb)(struct nc_session *session, const struct nc_notif *notif)) |
| 1271 | { |
| 1272 | struct nc_ntf_thread_arg *ntarg; |
| 1273 | int ret; |
| 1274 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1275 | if (!session) { |
| 1276 | ERRARG("session"); |
| 1277 | return -1; |
| 1278 | } else if (!notif_clb) { |
| 1279 | ERRARG("notif_clb"); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1280 | return -1; |
| 1281 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_CLIENT)) { |
| 1282 | ERR("Session %u: invalid session to receive Notifications.", session->id); |
| 1283 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1284 | } else if (session->opts.client.ntf_tid) { |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1285 | ERR("Session %u: separate notification thread is already running.", session->id); |
| 1286 | return -1; |
| 1287 | } |
| 1288 | |
| 1289 | ntarg = malloc(sizeof *ntarg); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1290 | if (!ntarg) { |
| 1291 | ERRMEM; |
| 1292 | return -1; |
| 1293 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1294 | ntarg->session = session; |
| 1295 | ntarg->notif_clb = notif_clb; |
| 1296 | |
| 1297 | /* just so that nc_recv_notif_thread() does not immediately exit, the value does not matter */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1298 | session->opts.client.ntf_tid = malloc(sizeof *session->opts.client.ntf_tid); |
| 1299 | if (!session->opts.client.ntf_tid) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1300 | ERRMEM; |
| 1301 | free(ntarg); |
| 1302 | return -1; |
| 1303 | } |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1304 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1305 | ret = pthread_create((pthread_t *)session->opts.client.ntf_tid, NULL, nc_recv_notif_thread, ntarg); |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1306 | if (ret) { |
| 1307 | ERR("Session %u: failed to create a new thread (%s).", strerror(errno)); |
| 1308 | free(ntarg); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1309 | free((pthread_t *)session->opts.client.ntf_tid); |
| 1310 | session->opts.client.ntf_tid = NULL; |
Michal Vasko | a8ad448 | 2016-01-28 14:25:54 +0100 | [diff] [blame] | 1311 | return -1; |
| 1312 | } |
| 1313 | |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1317 | API NC_MSG_TYPE |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1318 | 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] | 1319 | { |
| 1320 | NC_MSG_TYPE r; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1321 | int ret; |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1322 | struct nc_rpc_act_generic *rpc_gen; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1323 | struct nc_rpc_getconfig *rpc_gc; |
| 1324 | struct nc_rpc_edit *rpc_e; |
| 1325 | struct nc_rpc_copy *rpc_cp; |
| 1326 | struct nc_rpc_delete *rpc_del; |
| 1327 | struct nc_rpc_lock *rpc_lock; |
| 1328 | struct nc_rpc_get *rpc_g; |
| 1329 | struct nc_rpc_kill *rpc_k; |
| 1330 | struct nc_rpc_commit *rpc_com; |
| 1331 | struct nc_rpc_cancel *rpc_can; |
| 1332 | struct nc_rpc_validate *rpc_val; |
| 1333 | struct nc_rpc_getschema *rpc_gs; |
| 1334 | struct nc_rpc_subscribe *rpc_sub; |
| 1335 | struct lyd_node *data, *node; |
Michal Vasko | 9d8bee6 | 2016-03-03 10:58:24 +0100 | [diff] [blame] | 1336 | const struct lys_module *ietfnc = NULL, *ietfncmon, *notifs, *ietfncwd = NULL; |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1337 | char str[11]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1338 | uint64_t cur_msgid; |
| 1339 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1340 | if (!session) { |
| 1341 | ERRARG("session"); |
| 1342 | return NC_MSG_ERROR; |
| 1343 | } else if (!rpc) { |
| 1344 | ERRARG("rpc"); |
| 1345 | return NC_MSG_ERROR; |
| 1346 | } else if (!msgid) { |
| 1347 | ERRARG("msgid"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1348 | return NC_MSG_ERROR; |
| 1349 | } else if (session->status != NC_STATUS_RUNNING || session->side != NC_CLIENT) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1350 | ERR("Session %u: invalid session to send RPCs.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1351 | return NC_MSG_ERROR; |
| 1352 | } |
| 1353 | |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1354 | 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] | 1355 | ietfnc = ly_ctx_get_module(session->ctx, "ietf-netconf", NULL); |
| 1356 | if (!ietfnc) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1357 | ERR("Session %u: missing ietf-netconf schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1358 | return NC_MSG_ERROR; |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | switch (rpc->type) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1363 | case NC_RPC_ACT_GENERIC: |
| 1364 | rpc_gen = (struct nc_rpc_act_generic *)rpc; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1365 | |
| 1366 | if (rpc_gen->has_data) { |
| 1367 | data = rpc_gen->content.data; |
| 1368 | } else { |
Michal Vasko | 68b3f29 | 2016-09-16 12:00:32 +0200 | [diff] [blame] | 1369 | 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] | 1370 | } |
| 1371 | break; |
| 1372 | |
| 1373 | case NC_RPC_GETCONFIG: |
| 1374 | rpc_gc = (struct nc_rpc_getconfig *)rpc; |
| 1375 | |
| 1376 | data = lyd_new(NULL, ietfnc, "get-config"); |
| 1377 | node = lyd_new(data, ietfnc, "source"); |
| 1378 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_gc->source], NULL); |
| 1379 | if (!node) { |
| 1380 | lyd_free(data); |
| 1381 | return NC_MSG_ERROR; |
| 1382 | } |
| 1383 | if (rpc_gc->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 1384 | if (!rpc_gc->filter[0] || (rpc_gc->filter[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame] | 1385 | 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] | 1386 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1387 | } else { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1388 | node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1389 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 1390 | lyd_insert_attr(node, NULL, "select", rpc_gc->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1391 | } |
| 1392 | if (!node) { |
| 1393 | lyd_free(data); |
| 1394 | return NC_MSG_ERROR; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | if (rpc_gc->wd_mode) { |
| 1399 | if (!ietfncwd) { |
| 1400 | ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL); |
| 1401 | if (!ietfncwd) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1402 | 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] | 1403 | return NC_MSG_ERROR; |
| 1404 | } |
| 1405 | } |
| 1406 | switch (rpc_gc->wd_mode) { |
| 1407 | case NC_WD_UNKNOWN: |
| 1408 | /* cannot get here */ |
| 1409 | break; |
| 1410 | case NC_WD_ALL: |
| 1411 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 1412 | break; |
| 1413 | case NC_WD_ALL_TAG: |
| 1414 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 1415 | break; |
| 1416 | case NC_WD_TRIM: |
| 1417 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 1418 | break; |
| 1419 | case NC_WD_EXPLICIT: |
| 1420 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 1421 | break; |
| 1422 | } |
| 1423 | if (!node) { |
| 1424 | lyd_free(data); |
| 1425 | return NC_MSG_ERROR; |
| 1426 | } |
| 1427 | } |
| 1428 | break; |
| 1429 | |
| 1430 | case NC_RPC_EDIT: |
| 1431 | rpc_e = (struct nc_rpc_edit *)rpc; |
| 1432 | |
| 1433 | data = lyd_new(NULL, ietfnc, "edit-config"); |
| 1434 | node = lyd_new(data, ietfnc, "target"); |
| 1435 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_e->target], NULL); |
| 1436 | if (!node) { |
| 1437 | lyd_free(data); |
| 1438 | return NC_MSG_ERROR; |
| 1439 | } |
| 1440 | |
| 1441 | if (rpc_e->default_op) { |
| 1442 | node = lyd_new_leaf(data, ietfnc, "default-operation", rpcedit_dfltop2str[rpc_e->default_op]); |
| 1443 | if (!node) { |
| 1444 | lyd_free(data); |
| 1445 | return NC_MSG_ERROR; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | if (rpc_e->test_opt) { |
| 1450 | node = lyd_new_leaf(data, ietfnc, "test-option", rpcedit_testopt2str[rpc_e->test_opt]); |
| 1451 | if (!node) { |
| 1452 | lyd_free(data); |
| 1453 | return NC_MSG_ERROR; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | if (rpc_e->error_opt) { |
| 1458 | node = lyd_new_leaf(data, ietfnc, "error-option", rpcedit_erropt2str[rpc_e->error_opt]); |
| 1459 | if (!node) { |
| 1460 | lyd_free(data); |
| 1461 | return NC_MSG_ERROR; |
| 1462 | } |
| 1463 | } |
| 1464 | |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 1465 | if (!rpc_e->edit_cont[0] || (rpc_e->edit_cont[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame] | 1466 | 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] | 1467 | } else { |
| 1468 | node = lyd_new_leaf(data, ietfnc, "url", rpc_e->edit_cont); |
| 1469 | } |
| 1470 | if (!node) { |
| 1471 | lyd_free(data); |
| 1472 | return NC_MSG_ERROR; |
| 1473 | } |
| 1474 | break; |
| 1475 | |
| 1476 | case NC_RPC_COPY: |
| 1477 | rpc_cp = (struct nc_rpc_copy *)rpc; |
| 1478 | |
| 1479 | data = lyd_new(NULL, ietfnc, "copy-config"); |
| 1480 | node = lyd_new(data, ietfnc, "target"); |
| 1481 | if (rpc_cp->url_trg) { |
| 1482 | node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_trg); |
| 1483 | } else { |
| 1484 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->target], NULL); |
| 1485 | } |
| 1486 | if (!node) { |
| 1487 | lyd_free(data); |
| 1488 | return NC_MSG_ERROR; |
| 1489 | } |
| 1490 | |
| 1491 | node = lyd_new(data, ietfnc, "source"); |
| 1492 | if (rpc_cp->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 1493 | 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] | 1494 | 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] | 1495 | } else { |
| 1496 | node = lyd_new_leaf(node, ietfnc, "url", rpc_cp->url_config_src); |
| 1497 | } |
| 1498 | } else { |
| 1499 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_cp->source], NULL); |
| 1500 | } |
| 1501 | if (!node) { |
| 1502 | lyd_free(data); |
| 1503 | return NC_MSG_ERROR; |
| 1504 | } |
| 1505 | |
| 1506 | if (rpc_cp->wd_mode) { |
| 1507 | if (!ietfncwd) { |
| 1508 | ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL); |
| 1509 | if (!ietfncwd) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1510 | 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] | 1511 | return NC_MSG_ERROR; |
| 1512 | } |
| 1513 | } |
| 1514 | switch (rpc_cp->wd_mode) { |
| 1515 | case NC_WD_UNKNOWN: |
| 1516 | /* cannot get here */ |
| 1517 | break; |
| 1518 | case NC_WD_ALL: |
| 1519 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 1520 | break; |
| 1521 | case NC_WD_ALL_TAG: |
| 1522 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 1523 | break; |
| 1524 | case NC_WD_TRIM: |
| 1525 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 1526 | break; |
| 1527 | case NC_WD_EXPLICIT: |
| 1528 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 1529 | break; |
| 1530 | } |
| 1531 | if (!node) { |
| 1532 | lyd_free(data); |
| 1533 | return NC_MSG_ERROR; |
| 1534 | } |
| 1535 | } |
| 1536 | break; |
| 1537 | |
| 1538 | case NC_RPC_DELETE: |
| 1539 | rpc_del = (struct nc_rpc_delete *)rpc; |
| 1540 | |
| 1541 | data = lyd_new(NULL, ietfnc, "delete-config"); |
| 1542 | node = lyd_new(data, ietfnc, "target"); |
| 1543 | if (rpc_del->url) { |
| 1544 | node = lyd_new_leaf(node, ietfnc, "url", rpc_del->url); |
| 1545 | } else { |
| 1546 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_del->target], NULL); |
| 1547 | } |
| 1548 | if (!node) { |
| 1549 | lyd_free(data); |
| 1550 | return NC_MSG_ERROR; |
| 1551 | } |
| 1552 | break; |
| 1553 | |
| 1554 | case NC_RPC_LOCK: |
| 1555 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 1556 | |
| 1557 | data = lyd_new(NULL, ietfnc, "lock"); |
| 1558 | node = lyd_new(data, ietfnc, "target"); |
| 1559 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL); |
| 1560 | if (!node) { |
| 1561 | lyd_free(data); |
| 1562 | return NC_MSG_ERROR; |
| 1563 | } |
| 1564 | break; |
| 1565 | |
| 1566 | case NC_RPC_UNLOCK: |
| 1567 | rpc_lock = (struct nc_rpc_lock *)rpc; |
| 1568 | |
| 1569 | data = lyd_new(NULL, ietfnc, "unlock"); |
| 1570 | node = lyd_new(data, ietfnc, "target"); |
| 1571 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_lock->target], NULL); |
| 1572 | if (!node) { |
| 1573 | lyd_free(data); |
| 1574 | return NC_MSG_ERROR; |
| 1575 | } |
| 1576 | break; |
| 1577 | |
| 1578 | case NC_RPC_GET: |
| 1579 | rpc_g = (struct nc_rpc_get *)rpc; |
| 1580 | |
| 1581 | data = lyd_new(NULL, ietfnc, "get"); |
| 1582 | if (rpc_g->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 1583 | if (!rpc_g->filter[0] || (rpc_g->filter[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame] | 1584 | 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] | 1585 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1586 | } else { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1587 | node = lyd_new_anydata(data, ietfnc, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1588 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 1589 | lyd_insert_attr(node, NULL, "select", rpc_g->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1590 | } |
| 1591 | if (!node) { |
| 1592 | lyd_free(data); |
| 1593 | return NC_MSG_ERROR; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | if (rpc_g->wd_mode) { |
| 1598 | if (!ietfncwd) { |
| 1599 | ietfncwd = ly_ctx_get_module(session->ctx, "ietf-netconf-with-defaults", NULL); |
| 1600 | if (!ietfncwd) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1601 | 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] | 1602 | return NC_MSG_ERROR; |
| 1603 | } |
| 1604 | } |
| 1605 | switch (rpc_g->wd_mode) { |
| 1606 | case NC_WD_UNKNOWN: |
| 1607 | /* cannot get here */ |
| 1608 | break; |
| 1609 | case NC_WD_ALL: |
| 1610 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all"); |
| 1611 | break; |
| 1612 | case NC_WD_ALL_TAG: |
| 1613 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "report-all-tagged"); |
| 1614 | break; |
| 1615 | case NC_WD_TRIM: |
| 1616 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "trim"); |
| 1617 | break; |
| 1618 | case NC_WD_EXPLICIT: |
| 1619 | node = lyd_new_leaf(data, ietfncwd, "with-defaults", "explicit"); |
| 1620 | break; |
| 1621 | } |
| 1622 | if (!node) { |
| 1623 | lyd_free(data); |
| 1624 | return NC_MSG_ERROR; |
| 1625 | } |
| 1626 | } |
| 1627 | break; |
| 1628 | |
| 1629 | case NC_RPC_KILL: |
| 1630 | rpc_k = (struct nc_rpc_kill *)rpc; |
| 1631 | |
| 1632 | data = lyd_new(NULL, ietfnc, "kill-session"); |
| 1633 | sprintf(str, "%u", rpc_k->sid); |
| 1634 | lyd_new_leaf(data, ietfnc, "session-id", str); |
| 1635 | break; |
| 1636 | |
| 1637 | case NC_RPC_COMMIT: |
| 1638 | rpc_com = (struct nc_rpc_commit *)rpc; |
| 1639 | |
| 1640 | data = lyd_new(NULL, ietfnc, "commit"); |
| 1641 | if (rpc_com->confirmed) { |
| 1642 | lyd_new_leaf(data, ietfnc, "confirmed", NULL); |
| 1643 | } |
| 1644 | |
| 1645 | if (rpc_com->confirm_timeout) { |
| 1646 | sprintf(str, "%u", rpc_com->confirm_timeout); |
| 1647 | lyd_new_leaf(data, ietfnc, "confirm-timeout", str); |
| 1648 | } |
| 1649 | |
| 1650 | if (rpc_com->persist) { |
| 1651 | node = lyd_new_leaf(data, ietfnc, "persist", rpc_com->persist); |
| 1652 | if (!node) { |
| 1653 | lyd_free(data); |
| 1654 | return NC_MSG_ERROR; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | if (rpc_com->persist_id) { |
| 1659 | node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_com->persist_id); |
| 1660 | if (!node) { |
| 1661 | lyd_free(data); |
| 1662 | return NC_MSG_ERROR; |
| 1663 | } |
| 1664 | } |
| 1665 | break; |
| 1666 | |
| 1667 | case NC_RPC_DISCARD: |
| 1668 | data = lyd_new(NULL, ietfnc, "discard-changes"); |
| 1669 | break; |
| 1670 | |
| 1671 | case NC_RPC_CANCEL: |
| 1672 | rpc_can = (struct nc_rpc_cancel *)rpc; |
| 1673 | |
| 1674 | data = lyd_new(NULL, ietfnc, "cancel-commit"); |
| 1675 | if (rpc_can->persist_id) { |
| 1676 | node = lyd_new_leaf(data, ietfnc, "persist-id", rpc_can->persist_id); |
| 1677 | if (!node) { |
| 1678 | lyd_free(data); |
| 1679 | return NC_MSG_ERROR; |
| 1680 | } |
| 1681 | } |
| 1682 | break; |
| 1683 | |
| 1684 | case NC_RPC_VALIDATE: |
| 1685 | rpc_val = (struct nc_rpc_validate *)rpc; |
| 1686 | |
| 1687 | data = lyd_new(NULL, ietfnc, "validate"); |
| 1688 | node = lyd_new(data, ietfnc, "source"); |
| 1689 | if (rpc_val->url_config_src) { |
Michal Vasko | 7793bc6 | 2016-09-16 11:58:41 +0200 | [diff] [blame] | 1690 | 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] | 1691 | 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] | 1692 | } else { |
| 1693 | node = lyd_new_leaf(node, ietfnc, "url", rpc_val->url_config_src); |
| 1694 | } |
| 1695 | } else { |
| 1696 | node = lyd_new_leaf(node, ietfnc, ncds2str[rpc_val->source], NULL); |
| 1697 | } |
| 1698 | if (!node) { |
| 1699 | lyd_free(data); |
| 1700 | return NC_MSG_ERROR; |
| 1701 | } |
| 1702 | break; |
| 1703 | |
| 1704 | case NC_RPC_GETSCHEMA: |
| 1705 | ietfncmon = ly_ctx_get_module(session->ctx, "ietf-netconf-monitoring", NULL); |
| 1706 | if (!ietfncmon) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1707 | 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] | 1708 | return NC_MSG_ERROR; |
| 1709 | } |
| 1710 | |
| 1711 | rpc_gs = (struct nc_rpc_getschema *)rpc; |
| 1712 | |
| 1713 | data = lyd_new(NULL, ietfncmon, "get-schema"); |
| 1714 | node = lyd_new_leaf(data, ietfncmon, "identifier", rpc_gs->identifier); |
| 1715 | if (!node) { |
| 1716 | lyd_free(data); |
| 1717 | return NC_MSG_ERROR; |
| 1718 | } |
| 1719 | if (rpc_gs->version) { |
| 1720 | node = lyd_new_leaf(data, ietfncmon, "version", rpc_gs->version); |
| 1721 | if (!node) { |
| 1722 | lyd_free(data); |
| 1723 | return NC_MSG_ERROR; |
| 1724 | } |
| 1725 | } |
| 1726 | if (rpc_gs->format) { |
| 1727 | node = lyd_new_leaf(data, ietfncmon, "format", rpc_gs->format); |
| 1728 | if (!node) { |
| 1729 | lyd_free(data); |
| 1730 | return NC_MSG_ERROR; |
| 1731 | } |
| 1732 | } |
| 1733 | break; |
| 1734 | |
| 1735 | case NC_RPC_SUBSCRIBE: |
| 1736 | notifs = ly_ctx_get_module(session->ctx, "notifications", NULL); |
| 1737 | if (!notifs) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 1738 | ERR("Session %u: missing notifications schema in the context.", session->id); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1739 | return NC_MSG_ERROR; |
| 1740 | } |
| 1741 | |
| 1742 | rpc_sub = (struct nc_rpc_subscribe *)rpc; |
| 1743 | |
| 1744 | data = lyd_new(NULL, notifs, "create-subscription"); |
| 1745 | if (rpc_sub->stream) { |
| 1746 | node = lyd_new_leaf(data, notifs, "stream", rpc_sub->stream); |
| 1747 | if (!node) { |
| 1748 | lyd_free(data); |
| 1749 | return NC_MSG_ERROR; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | if (rpc_sub->filter) { |
Michal Vasko | f3c647b | 2016-03-08 12:17:33 +0100 | [diff] [blame] | 1754 | if (!rpc_sub->filter[0] || (rpc_sub->filter[0] == '<')) { |
Michal Vasko | c741ddb | 2016-10-07 13:34:57 +0200 | [diff] [blame] | 1755 | 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] | 1756 | lyd_insert_attr(node, NULL, "type", "subtree"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1757 | } else { |
Radek Krejci | 539efb6 | 2016-08-24 15:05:16 +0200 | [diff] [blame] | 1758 | node = lyd_new_anydata(data, notifs, "filter", NULL, LYD_ANYDATA_CONSTSTRING); |
Michal Vasko | 303245c | 2016-03-24 15:20:03 +0100 | [diff] [blame] | 1759 | lyd_insert_attr(node, NULL, "type", "xpath"); |
| 1760 | lyd_insert_attr(node, NULL, "select", rpc_sub->filter); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1761 | } |
| 1762 | if (!node) { |
| 1763 | lyd_free(data); |
| 1764 | return NC_MSG_ERROR; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | if (rpc_sub->start) { |
| 1769 | node = lyd_new_leaf(data, notifs, "startTime", rpc_sub->start); |
| 1770 | if (!node) { |
| 1771 | lyd_free(data); |
| 1772 | return NC_MSG_ERROR; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | if (rpc_sub->stop) { |
| 1777 | node = lyd_new_leaf(data, notifs, "stopTime", rpc_sub->stop); |
| 1778 | if (!node) { |
| 1779 | lyd_free(data); |
| 1780 | return NC_MSG_ERROR; |
| 1781 | } |
| 1782 | } |
| 1783 | break; |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1784 | default: |
| 1785 | ERRINT; |
| 1786 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1787 | } |
| 1788 | |
Michal Vasko | 49a0cf9 | 2016-09-14 09:15:32 +0200 | [diff] [blame] | 1789 | if (lyd_validate(&data, LYD_OPT_RPC | LYD_OPT_STRICT, NULL)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1790 | lyd_free(data); |
| 1791 | return NC_MSG_ERROR; |
| 1792 | } |
| 1793 | |
Michal vasko | 50cc94f | 2016-10-04 13:46:20 +0200 | [diff] [blame] | 1794 | ret = nc_timedlock(session->ti_lock, timeout, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1795 | if (ret == -1) { |
| 1796 | /* error */ |
| 1797 | r = NC_MSG_ERROR; |
| 1798 | } else if (!ret) { |
| 1799 | /* blocking */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1800 | r = NC_MSG_WOULDBLOCK; |
| 1801 | } else { |
| 1802 | /* send RPC, store its message ID */ |
| 1803 | r = nc_send_msg(session, data); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1804 | cur_msgid = session->opts.client.msgid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1805 | } |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1806 | pthread_mutex_unlock(session->ti_lock); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1807 | |
| 1808 | lyd_free(data); |
| 1809 | |
| 1810 | if (r != NC_MSG_RPC) { |
| 1811 | return r; |
| 1812 | } |
| 1813 | |
| 1814 | *msgid = cur_msgid; |
| 1815 | return NC_MSG_RPC; |
| 1816 | } |