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