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