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