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