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