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