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