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