Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file io.c |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 - input/output 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 |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 15 | #define _GNU_SOURCE /* asprintf, ppoll */ |
| 16 | #define _POSIX_SOUCE /* signals */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <errno.h> |
| 19 | #include <poll.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 20 | #include <inttypes.h> |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 21 | #include <stdarg.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 25 | #include <signal.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 26 | |
| 27 | #include <libyang/libyang.h> |
| 28 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 29 | #include "libnetconf.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 30 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 31 | #define BUFFERSIZE 512 |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 32 | |
| 33 | static ssize_t |
| 34 | nc_read(struct nc_session *session, char *buf, size_t count) |
| 35 | { |
| 36 | size_t size = 0; |
| 37 | ssize_t r; |
| 38 | |
| 39 | assert(session); |
| 40 | assert(buf); |
| 41 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 42 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 43 | return -1; |
| 44 | } |
| 45 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 46 | if (!count) { |
| 47 | return 0; |
| 48 | } |
| 49 | |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 50 | switch (session->ti_type) { |
| 51 | case NC_TI_NONE: |
| 52 | return 0; |
| 53 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 54 | case NC_TI_FD: |
| 55 | /* read via standard file descriptor */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 56 | while (count) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 57 | r = read(session->ti.fd.in, &(buf[size]), count); |
| 58 | if (r < 0) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 59 | if ((errno == EAGAIN) || (errno == EINTR)) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 60 | usleep(NC_READ_SLEEP); |
| 61 | continue; |
| 62 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 63 | ERR("Session %u: reading from file descriptor (%d) failed (%s).", |
| 64 | session->id, session->ti.fd.in, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 65 | session->status = NC_STATUS_INVALID; |
| 66 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | } else if (r == 0) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 70 | ERR("Session %u: communication file descriptor (%d) unexpectedly closed.", |
| 71 | session->id, session->ti.fd.in); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 72 | session->status = NC_STATUS_INVALID; |
| 73 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | size = size + r; |
| 78 | count = count - r; |
| 79 | } |
| 80 | break; |
| 81 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 82 | #ifdef NC_ENABLED_SSH |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 83 | case NC_TI_LIBSSH: |
| 84 | /* read via libssh */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 85 | while (count) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 86 | r = ssh_channel_read(session->ti.libssh.channel, &(buf[size]), count, 0); |
| 87 | if (r == SSH_AGAIN) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 88 | usleep(NC_READ_SLEEP); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 89 | continue; |
| 90 | } else if (r == SSH_ERROR) { |
Michal Vasko | 051d35b | 2016-02-03 15:28:37 +0100 | [diff] [blame] | 91 | ERR("Session %u: reading from the SSH channel failed (%s).", session->id, |
| 92 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 93 | session->status = NC_STATUS_INVALID; |
| 94 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 95 | return -1; |
| 96 | } else if (r == 0) { |
| 97 | if (ssh_channel_is_eof(session->ti.libssh.channel)) { |
Michal Vasko | 454e22b | 2016-01-21 15:34:08 +0100 | [diff] [blame] | 98 | ERR("Session %u: SSH channel unexpected EOF.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 99 | session->status = NC_STATUS_INVALID; |
| 100 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 101 | return -1; |
| 102 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 103 | usleep(NC_READ_SLEEP); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 104 | continue; |
| 105 | } |
| 106 | |
| 107 | size = size + r; |
| 108 | count = count - r; |
| 109 | } |
| 110 | break; |
| 111 | #endif |
| 112 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 113 | #ifdef NC_ENABLED_TLS |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 114 | case NC_TI_OPENSSL: |
| 115 | /* read via OpenSSL */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 116 | while (count) { |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 117 | r = SSL_read(session->ti.tls, &(buf[size]), count); |
| 118 | if (r <= 0) { |
| 119 | int x; |
| 120 | switch (x = SSL_get_error(session->ti.tls, r)) { |
| 121 | case SSL_ERROR_WANT_READ: |
| 122 | usleep(NC_READ_SLEEP); |
| 123 | continue; |
| 124 | case SSL_ERROR_ZERO_RETURN: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 125 | ERR("Session %u: communication socket unexpectedly closed (OpenSSL).", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 126 | session->status = NC_STATUS_INVALID; |
| 127 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 128 | return -1; |
| 129 | default: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 130 | ERR("Session %u: reading from the TLS session failed (SSL code %d).", session->id, x); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 131 | session->status = NC_STATUS_INVALID; |
| 132 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 133 | return -1; |
| 134 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 135 | } |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 136 | size = size + r; |
| 137 | count = count - r; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 138 | } |
| 139 | break; |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | return (ssize_t)size; |
| 144 | } |
| 145 | |
| 146 | static ssize_t |
| 147 | nc_read_chunk(struct nc_session *session, size_t len, char **chunk) |
| 148 | { |
| 149 | ssize_t r; |
| 150 | |
| 151 | assert(session); |
| 152 | assert(chunk); |
| 153 | |
| 154 | if (!len) { |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | *chunk = malloc ((len + 1) * sizeof **chunk); |
| 159 | if (!*chunk) { |
| 160 | ERRMEM; |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | r = nc_read(session, *chunk, len); |
| 165 | if (r <= 0) { |
| 166 | free(*chunk); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | /* terminating null byte */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 171 | (*chunk)[r] = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 172 | |
| 173 | return r; |
| 174 | } |
| 175 | |
| 176 | static ssize_t |
| 177 | nc_read_until(struct nc_session *session, const char *endtag, size_t limit, char **result) |
| 178 | { |
| 179 | char *chunk = NULL; |
| 180 | size_t size, count = 0, r, len; |
| 181 | |
| 182 | assert(session); |
| 183 | assert(endtag); |
| 184 | |
| 185 | if (limit && limit < BUFFERSIZE) { |
| 186 | size = limit; |
| 187 | } else { |
| 188 | size = BUFFERSIZE; |
| 189 | } |
| 190 | chunk = malloc ((size + 1) * sizeof *chunk); |
Radek Krejci | b791b53 | 2015-10-08 15:29:34 +0200 | [diff] [blame] | 191 | if (!chunk) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 192 | ERRMEM; |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | len = strlen(endtag); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 197 | while (1) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 198 | if (limit && count == limit) { |
| 199 | free(chunk); |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 200 | WRN("Session %u: reading limit (%d) reached.", session->id, limit); |
| 201 | ERR("Session %u: invalid input data (missing \"%s\" sequence).", session->id, endtag); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | /* resize buffer if needed */ |
| 206 | if (count == size) { |
| 207 | /* get more memory */ |
| 208 | size = size + BUFFERSIZE; |
| 209 | char *tmp = realloc (chunk, (size + 1) * sizeof *tmp); |
| 210 | if (!tmp) { |
| 211 | ERRMEM; |
| 212 | free(chunk); |
| 213 | return -1; |
| 214 | } |
| 215 | chunk = tmp; |
| 216 | } |
| 217 | |
| 218 | /* get another character */ |
| 219 | r = nc_read(session, &(chunk[count]), 1); |
| 220 | if (r != 1) { |
| 221 | free(chunk); |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | count++; |
| 226 | |
| 227 | /* check endtag */ |
| 228 | if (count >= len) { |
| 229 | if (!strncmp(endtag, &(chunk[count - len]), len)) { |
| 230 | /* endtag found */ |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* terminating null byte */ |
| 237 | chunk[count] = 0; |
| 238 | |
| 239 | if (result) { |
| 240 | *result = chunk; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 241 | } else { |
| 242 | free(chunk); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 243 | } |
| 244 | return count; |
| 245 | } |
| 246 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 247 | /* return NC_MSG_ERROR can change session status */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 248 | NC_MSG_TYPE |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 249 | nc_read_msg(struct nc_session *session, struct lyxml_elem **data) |
| 250 | { |
| 251 | int ret; |
| 252 | char *msg = NULL, *chunk, *aux; |
| 253 | uint64_t chunk_len, len = 0; |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 254 | struct nc_server_reply *reply; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 255 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 256 | assert(session && data); |
| 257 | *data = NULL; |
| 258 | |
| 259 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 260 | ERR("Session %u: invalid session to read from.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 261 | return NC_MSG_ERROR; |
| 262 | } |
| 263 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 264 | /* read the message */ |
| 265 | switch (session->version) { |
| 266 | case NC_VERSION_10: |
| 267 | ret = nc_read_until(session, NC_VERSION_10_ENDTAG, 0, &msg); |
| 268 | if (ret == -1) { |
| 269 | goto error; |
| 270 | } |
| 271 | |
| 272 | /* cut off the end tag */ |
| 273 | msg[ret - NC_VERSION_10_ENDTAG_LEN] = '\0'; |
| 274 | break; |
| 275 | case NC_VERSION_11: |
| 276 | while (1) { |
| 277 | ret = nc_read_until(session, "\n#", 0, NULL); |
| 278 | if (ret == -1) { |
| 279 | goto error; |
| 280 | } |
| 281 | ret = nc_read_until(session, "\n", 0, &chunk); |
| 282 | if (ret == -1) { |
| 283 | goto error; |
| 284 | } |
| 285 | |
| 286 | if (!strcmp(chunk, "#\n")) { |
| 287 | /* end of chunked framing message */ |
| 288 | free(chunk); |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | /* convert string to the size of the following chunk */ |
| 293 | chunk_len = strtoul(chunk, (char **)NULL, 10); |
| 294 | free(chunk); |
| 295 | if (!chunk_len) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 296 | ERR("Session %u: invalid frame chunk size detected, fatal error.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 297 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* now we have size of next chunk, so read the chunk */ |
| 301 | ret = nc_read_chunk(session, chunk_len, &chunk); |
| 302 | if (ret == -1) { |
| 303 | goto error; |
| 304 | } |
| 305 | |
| 306 | /* realloc message buffer, remember to count terminating null byte */ |
| 307 | aux = realloc(msg, len + chunk_len + 1); |
| 308 | if (!aux) { |
| 309 | ERRMEM; |
| 310 | goto error; |
| 311 | } |
| 312 | msg = aux; |
| 313 | memcpy(msg + len, chunk, chunk_len); |
| 314 | len += chunk_len; |
| 315 | msg[len] = '\0'; |
| 316 | free(chunk); |
| 317 | } |
| 318 | |
| 319 | break; |
| 320 | } |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 321 | DBG("Session %u: received message:\n%s", session->id, msg); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 322 | |
| 323 | /* build XML tree */ |
Michal Vasko | a4c23d8 | 2016-02-03 15:48:09 +0100 | [diff] [blame] | 324 | *data = lyxml_parse_mem(session->ctx, msg, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 325 | if (!*data) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 326 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 327 | } else if (!(*data)->ns) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 328 | ERR("Session %u: invalid message root element (invalid namespace).", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 329 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 330 | } |
| 331 | free(msg); |
| 332 | msg = NULL; |
| 333 | |
| 334 | /* get and return message type */ |
| 335 | if (!strcmp((*data)->ns->value, NC_NS_BASE)) { |
| 336 | if (!strcmp((*data)->name, "rpc")) { |
| 337 | return NC_MSG_RPC; |
| 338 | } else if (!strcmp((*data)->name, "rpc-reply")) { |
| 339 | return NC_MSG_REPLY; |
| 340 | } else if (!strcmp((*data)->name, "hello")) { |
| 341 | return NC_MSG_HELLO; |
| 342 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 343 | ERR("Session %u: invalid message root element (invalid name \"%s\").", session->id, (*data)->name); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 344 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 345 | } |
| 346 | } else if (!strcmp((*data)->ns->value, NC_NS_NOTIF)) { |
| 347 | if (!strcmp((*data)->name, "notification")) { |
| 348 | return NC_MSG_NOTIF; |
| 349 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 350 | ERR("Session %u: invalid message root element (invalid name \"%s\").", session->id, (*data)->name); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 351 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 352 | } |
| 353 | } else { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 354 | ERR("Session %u: invalid message root element (invalid namespace \"%s\").", session->id, (*data)->ns->value); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 355 | goto malformed_msg; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 358 | malformed_msg: |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 359 | ERR("Session %u: malformed message received.", session->id); |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 360 | if ((session->side == NC_SERVER) && (session->version == NC_VERSION_11)) { |
| 361 | /* NETCONF version 1.1 defines sending error reply from the server (RFC 6241 sec. 3) */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 362 | reply = nc_server_reply_err(nc_err(NC_ERR_MALFORMED_MSG)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 363 | |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 364 | if (nc_write_msg(session, NC_MSG_REPLY, NULL, reply) == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 365 | ERR("Session %u: unable to send a \"Malformed message\" error reply, terminating session.", session->id); |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 366 | if (session->status != NC_STATUS_INVALID) { |
| 367 | session->status = NC_STATUS_INVALID; |
| 368 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 369 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 370 | } |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 371 | nc_server_reply_free(reply); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 372 | } |
| 373 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 374 | error: |
| 375 | /* cleanup */ |
| 376 | free(msg); |
| 377 | free(*data); |
| 378 | *data = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 379 | |
| 380 | return NC_MSG_ERROR; |
| 381 | } |
| 382 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 383 | /* return -1 means either poll error or that session was invalidated (socket error), EINTR is handled inside */ |
| 384 | static int |
| 385 | nc_read_poll(struct nc_session *session, int timeout) |
| 386 | { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 387 | sigset_t sigmask; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 388 | int ret = -2; |
| 389 | struct pollfd fds; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 390 | struct timespec ts_timeout; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 391 | |
| 392 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 393 | ERR("Session %u: invalid session to poll.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | switch (session->ti_type) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 398 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 399 | case NC_TI_LIBSSH: |
| 400 | /* EINTR is handled, it resumes waiting */ |
| 401 | ret = ssh_channel_poll_timeout(session->ti.libssh.channel, timeout, 0); |
| 402 | if (ret == SSH_ERROR) { |
Michal Vasko | 051d35b | 2016-02-03 15:28:37 +0100 | [diff] [blame] | 403 | ERR("Session %u: polling on the SSH channel failed (%s).", session->id, |
| 404 | ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 405 | session->status = NC_STATUS_INVALID; |
| 406 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 407 | return -1; |
| 408 | } else if (ret == SSH_EOF) { |
Michal Vasko | 051d35b | 2016-02-03 15:28:37 +0100 | [diff] [blame] | 409 | ERR("Session %u: SSH channel unexpected EOF.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 410 | session->status = NC_STATUS_INVALID; |
| 411 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 412 | return -1; |
| 413 | } else if (ret > 0) { |
| 414 | /* fake it */ |
| 415 | ret = 1; |
| 416 | fds.revents = POLLIN; |
Michal Vasko | 5550cda | 2016-02-03 15:28:57 +0100 | [diff] [blame] | 417 | } else { /* ret == 0 */ |
| 418 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 419 | } |
| 420 | /* fallthrough */ |
| 421 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 422 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 423 | case NC_TI_OPENSSL: |
| 424 | if (session->ti_type == NC_TI_OPENSSL) { |
| 425 | fds.fd = SSL_get_fd(session->ti.tls); |
| 426 | } |
| 427 | /* fallthrough */ |
| 428 | #endif |
| 429 | case NC_TI_FD: |
| 430 | if (session->ti_type == NC_TI_FD) { |
| 431 | fds.fd = session->ti.fd.in; |
| 432 | } |
| 433 | |
| 434 | /* poll only if it is not an SSH session */ |
| 435 | if (ret == -2) { |
| 436 | fds.events = POLLIN; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 437 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 438 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 439 | if (timeout > -1) { |
Michal Vasko | 085ff56 | 2016-01-20 10:07:30 +0100 | [diff] [blame] | 440 | if (!timeout) { |
| 441 | ts_timeout.tv_sec = 0; |
| 442 | ts_timeout.tv_nsec = 0; |
| 443 | } else if (timeout > 0) { |
| 444 | ts_timeout.tv_sec = timeout / 1000; |
| 445 | ts_timeout.tv_nsec = (timeout % 1000) * 1000000; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 446 | } |
| 447 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 448 | sigfillset(&sigmask); |
| 449 | ret = ppoll(&fds, 1, (timeout == -1 ? NULL : &ts_timeout), &sigmask); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | break; |
| 453 | |
| 454 | default: |
| 455 | ERRINT; |
| 456 | return -1; |
| 457 | } |
| 458 | |
| 459 | /* process the poll result, unified ret meaning for poll and ssh_channel poll */ |
| 460 | if (ret < 0) { |
| 461 | /* poll failed - something really bad happened, close the session */ |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 462 | ERR("Session %u: ppoll error (%s).", session->id, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 463 | session->status = NC_STATUS_INVALID; |
| 464 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 465 | return -1; |
| 466 | } else { /* status > 0 */ |
| 467 | /* in case of standard (non-libssh) poll, there still can be an error */ |
| 468 | if (fds.revents & POLLHUP) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 469 | ERR("Session %u: communication channel unexpectedly closed.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 470 | session->status = NC_STATUS_INVALID; |
| 471 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 472 | return -1; |
| 473 | } |
| 474 | if (fds.revents & POLLERR) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 475 | ERR("Session %u: communication channel error.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 476 | session->status = NC_STATUS_INVALID; |
| 477 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 478 | return -1; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | /* return NC_MSG_ERROR can change session status */ |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 486 | NC_MSG_TYPE |
| 487 | nc_read_msg_poll(struct nc_session *session, int timeout, struct lyxml_elem **data) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 488 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 489 | int ret; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 490 | |
| 491 | assert(data); |
| 492 | *data = NULL; |
| 493 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 494 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 495 | ERR("Session %u: invalid session to read from.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 496 | return NC_MSG_ERROR; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 499 | ret = nc_read_poll(session, timeout); |
| 500 | if (ret == 0) { |
| 501 | /* timed out */ |
| 502 | return NC_MSG_WOULDBLOCK; |
| 503 | } else if (ret < 0) { |
| 504 | /* poll error, error written */ |
| 505 | return NC_MSG_ERROR; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 508 | return nc_read_msg(session, data); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 509 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 510 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 511 | /* does not really log, only fatal errors */ |
| 512 | int |
| 513 | nc_session_is_connected(struct nc_session *session) |
| 514 | { |
| 515 | int ret; |
| 516 | struct pollfd fds; |
| 517 | |
| 518 | switch (session->ti_type) { |
| 519 | case NC_TI_FD: |
| 520 | fds.fd = session->ti.fd.in; |
| 521 | break; |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 522 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 523 | case NC_TI_LIBSSH: |
| 524 | fds.fd = ssh_get_fd(session->ti.libssh.session); |
| 525 | break; |
| 526 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 527 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 528 | case NC_TI_OPENSSL: |
| 529 | fds.fd = SSL_get_fd(session->ti.tls); |
| 530 | break; |
| 531 | #endif |
| 532 | case NC_TI_NONE: |
| 533 | ERRINT; |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | fds.events = POLLIN; |
| 538 | |
| 539 | errno = 0; |
| 540 | while (((ret = poll(&fds, 1, 0)) == -1) && (errno == EINTR)); |
| 541 | |
| 542 | if (ret == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 543 | ERR("Session %u: poll failed (%s).", session->id, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 544 | return 0; |
| 545 | } else if ((ret > 0) && (fds.revents & (POLLHUP | POLLERR))) { |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | return 1; |
| 550 | } |
| 551 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 552 | #define WRITE_BUFSIZE (2 * BUFFERSIZE) |
| 553 | struct wclb_arg { |
| 554 | struct nc_session *session; |
| 555 | char buf[WRITE_BUFSIZE]; |
| 556 | size_t len; |
| 557 | }; |
| 558 | |
| 559 | static ssize_t |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 560 | nc_write(struct nc_session *session, const void *buf, size_t count) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 561 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 562 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 563 | return -1; |
| 564 | } |
| 565 | |
| 566 | /* prevent SIGPIPE this way */ |
| 567 | if (!nc_session_is_connected(session)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 568 | ERR("Session %u: communication socket unexpectedly closed.", session->id); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 569 | session->status = NC_STATUS_INVALID; |
| 570 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 571 | return -1; |
| 572 | } |
| 573 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 574 | switch (session->ti_type) { |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 575 | case NC_TI_NONE: |
| 576 | return -1; |
| 577 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 578 | case NC_TI_FD: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 579 | return write(session->ti.fd.out, buf, count); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 580 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 581 | #ifdef NC_ENABLED_SSH |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 582 | case NC_TI_LIBSSH: |
Michal Vasko | 454e22b | 2016-01-21 15:34:08 +0100 | [diff] [blame] | 583 | if (ssh_channel_is_closed(session->ti.libssh.channel) || ssh_channel_is_eof(session->ti.libssh.channel)) { |
| 584 | if (ssh_channel_is_closed(session->ti.libssh.channel)) { |
| 585 | ERR("Session %u: SSH channel unexpectedly closed.", session->id); |
| 586 | } else { |
| 587 | ERR("Session %u: SSH channel unexpected EOF.", session->id); |
| 588 | } |
| 589 | session->status = NC_STATUS_INVALID; |
| 590 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 591 | return -1; |
| 592 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 593 | return ssh_channel_write(session->ti.libssh.channel, buf, count); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 594 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 595 | #ifdef NC_ENABLED_TLS |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 596 | case NC_TI_OPENSSL: |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 597 | return SSL_write(session->ti.tls, buf, count); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 598 | #endif |
| 599 | } |
| 600 | |
| 601 | return -1; |
| 602 | } |
| 603 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 604 | static int |
| 605 | nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 606 | { |
Michal Vasko | fd2db9b | 2016-01-14 16:15:16 +0100 | [diff] [blame] | 607 | int ret = 0, c; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 608 | char chunksize[20]; |
| 609 | |
| 610 | if (session->version == NC_VERSION_11) { |
| 611 | sprintf(chunksize, "\n#%zu\n", count); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 612 | ret = nc_write(session, chunksize, strlen(chunksize)); |
| 613 | if (ret == -1) { |
| 614 | return -1; |
| 615 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 616 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 617 | |
| 618 | c = nc_write(session, buf, count); |
| 619 | if (c == -1) { |
| 620 | return -1; |
| 621 | } |
| 622 | ret += c; |
| 623 | |
| 624 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 627 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 628 | nc_write_endtag(struct nc_session *session) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 629 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 630 | int ret; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 631 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 632 | if (session->version == NC_VERSION_11) { |
| 633 | ret = nc_write(session, "\n##\n", 4); |
| 634 | } else { |
| 635 | ret = nc_write(session, "]]>]]>", 6); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 638 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 641 | static int |
| 642 | nc_write_clb_flush(struct wclb_arg *warg) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 643 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 644 | int ret = 0; |
| 645 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 646 | /* flush current buffer */ |
| 647 | if (warg->len) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 648 | ret = nc_write_starttag_and_msg(warg->session, warg->buf, warg->len); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 649 | warg->len = 0; |
| 650 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 651 | |
| 652 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static ssize_t |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 656 | nc_write_clb(void *arg, const void *buf, size_t count) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 657 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 658 | int ret = 0, c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 659 | struct wclb_arg *warg = (struct wclb_arg *)arg; |
| 660 | |
| 661 | if (!buf) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 662 | c = nc_write_clb_flush(warg); |
| 663 | if (c == -1) { |
| 664 | return -1; |
| 665 | } |
| 666 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 667 | |
| 668 | /* endtag */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 669 | c = nc_write_endtag(warg->session); |
| 670 | if (c == -1) { |
| 671 | return -1; |
| 672 | } |
| 673 | ret += c; |
| 674 | |
| 675 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (warg->len && (warg->len + count > WRITE_BUFSIZE)) { |
| 679 | /* dump current buffer */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 680 | c = nc_write_clb_flush(warg); |
| 681 | if (c == -1) { |
| 682 | return -1; |
| 683 | } |
| 684 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 685 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 686 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 687 | if (count > WRITE_BUFSIZE) { |
| 688 | /* write directly */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 689 | c = nc_write_starttag_and_msg(warg->session, buf, count); |
| 690 | if (c == -1) { |
| 691 | return -1; |
| 692 | } |
| 693 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 694 | } else { |
| 695 | /* keep in buffer and write later */ |
| 696 | memcpy(&warg->buf[warg->len], buf, count); |
| 697 | warg->len += count; /* is <= WRITE_BUFSIZE */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 698 | ret += count; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 701 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 704 | static void |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 705 | nc_write_error(struct wclb_arg *arg, struct nc_server_error *err) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 706 | { |
| 707 | uint16_t i; |
| 708 | char str_sid[11]; |
| 709 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 710 | nc_write_clb((void *)arg, "<rpc-error>", 11); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 711 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 712 | nc_write_clb((void *)arg, "<error-type>", 12); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 713 | switch (err->type) { |
| 714 | case NC_ERR_TYPE_TRAN: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 715 | nc_write_clb((void *)arg, "transport", 9); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 716 | break; |
| 717 | case NC_ERR_TYPE_RPC: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 718 | nc_write_clb((void *)arg, "rpc", 3); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 719 | break; |
| 720 | case NC_ERR_TYPE_PROT: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 721 | nc_write_clb((void *)arg, "protocol", 8); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 722 | break; |
| 723 | case NC_ERR_TYPE_APP: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 724 | nc_write_clb((void *)arg, "application", 11); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 725 | break; |
| 726 | default: |
| 727 | ERRINT; |
| 728 | return; |
| 729 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 730 | nc_write_clb((void *)arg, "</error-type>", 13); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 731 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 732 | nc_write_clb((void *)arg, "<error-tag>", 11); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 733 | switch (err->tag) { |
| 734 | case NC_ERR_IN_USE: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 735 | nc_write_clb((void *)arg, "in-use", 6); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 736 | break; |
| 737 | case NC_ERR_INVALID_VALUE: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 738 | nc_write_clb((void *)arg, "invalid-value", 13); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 739 | break; |
| 740 | case NC_ERR_TOO_BIG: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 741 | nc_write_clb((void *)arg, "too-big", 7); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 742 | break; |
| 743 | case NC_ERR_MISSING_ATTR: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 744 | nc_write_clb((void *)arg, "missing-attribute", 17); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 745 | break; |
| 746 | case NC_ERR_BAD_ATTR: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 747 | nc_write_clb((void *)arg, "bad-attribute", 13); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 748 | break; |
| 749 | case NC_ERR_UNKNOWN_ATTR: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 750 | nc_write_clb((void *)arg, "unknown-attribute", 17); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 751 | break; |
| 752 | case NC_ERR_MISSING_ELEM: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 753 | nc_write_clb((void *)arg, "missing-element", 15); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 754 | break; |
| 755 | case NC_ERR_BAD_ELEM: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 756 | nc_write_clb((void *)arg, "bad-element", 11); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 757 | break; |
| 758 | case NC_ERR_UNKNOWN_ELEM: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 759 | nc_write_clb((void *)arg, "unknown-element", 15); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 760 | break; |
| 761 | case NC_ERR_UNKNOWN_NS: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 762 | nc_write_clb((void *)arg, "unknown-namespace", 17); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 763 | break; |
| 764 | case NC_ERR_ACCESS_DENIED: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 765 | nc_write_clb((void *)arg, "access-denied", 13); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 766 | break; |
| 767 | case NC_ERR_LOCK_DENIED: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 768 | nc_write_clb((void *)arg, "lock-denied", 11); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 769 | break; |
| 770 | case NC_ERR_RES_DENIED: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 771 | nc_write_clb((void *)arg, "resource-denied", 15); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 772 | break; |
| 773 | case NC_ERR_ROLLBACK_FAILED: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 774 | nc_write_clb((void *)arg, "rollback-failed", 15); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 775 | break; |
| 776 | case NC_ERR_DATA_EXISTS: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 777 | nc_write_clb((void *)arg, "data-exists", 11); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 778 | break; |
| 779 | case NC_ERR_DATA_MISSING: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 780 | nc_write_clb((void *)arg, "data-missing", 12); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 781 | break; |
| 782 | case NC_ERR_OP_NOT_SUPPORTED: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 783 | nc_write_clb((void *)arg, "operation-not-supported", 23); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 784 | break; |
| 785 | case NC_ERR_OP_FAILED: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 786 | nc_write_clb((void *)arg, "operation-failed", 16); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 787 | break; |
| 788 | case NC_ERR_MALFORMED_MSG: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 789 | nc_write_clb((void *)arg, "malformed-message", 17); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 790 | break; |
| 791 | default: |
| 792 | ERRINT; |
| 793 | return; |
| 794 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 795 | nc_write_clb((void *)arg, "</error-tag>", 12); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 796 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 797 | nc_write_clb((void *)arg, "<error-severity>error</error-severity>", 38); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 798 | |
| 799 | if (err->apptag) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 800 | nc_write_clb((void *)arg, "<error-app-tag>", 15); |
| 801 | nc_write_clb((void *)arg, err->apptag, strlen(err->apptag)); |
| 802 | nc_write_clb((void *)arg, "</error-app-tag>", 16); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | if (err->path) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 806 | nc_write_clb((void *)arg, "<error-path>", 12); |
| 807 | nc_write_clb((void *)arg, err->path, strlen(err->path)); |
| 808 | nc_write_clb((void *)arg, "</error-path>", 13); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | if (err->message) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 812 | nc_write_clb((void *)arg, "<error-message", 14); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 813 | if (err->message_lang) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 814 | nc_write_clb((void *)arg, " xml:lang=\"", 11); |
| 815 | nc_write_clb((void *)arg, err->message_lang, strlen(err->message_lang)); |
| 816 | nc_write_clb((void *)arg, "\"", 1); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 817 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 818 | nc_write_clb((void *)arg, ">", 1); |
| 819 | nc_write_clb((void *)arg, err->message, strlen(err->message)); |
| 820 | nc_write_clb((void *)arg, "</error-message>", 16); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 823 | if (err->sid || err->attr_count || err->elem_count || err->ns_count || err->other_count) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 824 | nc_write_clb((void *)arg, "<error-info>", 12); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 825 | |
| 826 | if (err->sid) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 827 | nc_write_clb((void *)arg, "<session-id>", 12); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 828 | sprintf(str_sid, "%u", err->sid); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 829 | nc_write_clb((void *)arg, str_sid, strlen(str_sid)); |
| 830 | nc_write_clb((void *)arg, "</session-id>", 13); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | for (i = 0; i < err->attr_count; ++i) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 834 | nc_write_clb((void *)arg, "<bad-attribute>", 15); |
| 835 | nc_write_clb((void *)arg, err->attr[i], strlen(err->attr[i])); |
| 836 | nc_write_clb((void *)arg, "</bad-attribute>", 16); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | for (i = 0; i < err->elem_count; ++i) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 840 | nc_write_clb((void *)arg, "<bad-element>", 13); |
| 841 | nc_write_clb((void *)arg, err->elem[i], strlen(err->elem[i])); |
| 842 | nc_write_clb((void *)arg, "</bad-element>", 14); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | for (i = 0; i < err->ns_count; ++i) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 846 | nc_write_clb((void *)arg, "<bad-namespace>", 15); |
| 847 | nc_write_clb((void *)arg, err->ns[i], strlen(err->ns[i])); |
| 848 | nc_write_clb((void *)arg, "</bad-namespace>", 16); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | for (i = 0; i < err->other_count; ++i) { |
Michal Vasko | a4c23d8 | 2016-02-03 15:48:09 +0100 | [diff] [blame] | 852 | lyxml_print_clb(nc_write_clb, (void *)arg, err->other[i], 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 853 | } |
| 854 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 855 | nc_write_clb((void *)arg, "</error-info>", 13); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 856 | } |
| 857 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 858 | nc_write_clb((void *)arg, "</rpc-error>", 12); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 859 | } |
| 860 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 861 | /* return -1 can change session status */ |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 862 | int |
| 863 | nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 864 | { |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 865 | va_list ap; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 866 | int count; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 867 | const char *attrs; |
| 868 | struct lyd_node *content; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 869 | struct lyxml_elem *rpc_elem; |
| 870 | struct nc_server_reply *reply; |
| 871 | struct nc_server_reply_error *error_rpl; |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 872 | char *buf = NULL; |
| 873 | struct wclb_arg arg; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 874 | const char **capabilities; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 875 | uint32_t *sid = NULL, i; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 876 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 877 | assert(session); |
| 878 | |
| 879 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 880 | ERR("Session %u: invalid session to write to.", session->id); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 881 | return -1; |
| 882 | } |
| 883 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 884 | va_start(ap, type); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 885 | |
| 886 | arg.session = session; |
| 887 | arg.len = 0; |
| 888 | |
| 889 | switch (type) { |
| 890 | case NC_MSG_RPC: |
| 891 | content = va_arg(ap, struct lyd_node *); |
| 892 | attrs = va_arg(ap, const char *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 893 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 894 | count = asprintf(&buf, "<rpc xmlns=\"%s\" message-id=\"%"PRIu64"\"%s>", |
| 895 | NC_NS_BASE, session->msgid + 1, attrs ? attrs : ""); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 896 | nc_write_clb((void *)&arg, buf, count); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 897 | free(buf); |
Radek Krejci | 2927112 | 2016-02-29 16:41:43 +0100 | [diff] [blame] | 898 | lyd_print_clb(nc_write_clb, (void *)&arg, content, LYD_XML, LYP_WITHSIBLINGS); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 899 | nc_write_clb((void *)&arg, "</rpc>", 6); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 900 | |
| 901 | session->msgid++; |
| 902 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 903 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 904 | case NC_MSG_REPLY: |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 905 | rpc_elem = va_arg(ap, struct lyxml_elem *); |
| 906 | reply = va_arg(ap, struct nc_server_reply *); |
| 907 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 908 | nc_write_clb((void *)&arg, "<rpc-reply", 10); |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 909 | /* can be NULL if replying with a malformed-message error */ |
| 910 | if (rpc_elem) { |
Michal Vasko | a4c23d8 | 2016-02-03 15:48:09 +0100 | [diff] [blame] | 911 | lyxml_print_clb(nc_write_clb, (void *)&arg, rpc_elem, LYXML_PRINT_ATTRS); |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 912 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 913 | nc_write_clb((void *)&arg, ">", 1); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 914 | switch (reply->type) { |
| 915 | case NC_RPL_OK: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 916 | nc_write_clb((void *)&arg, "<ok/>", 5); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 917 | break; |
| 918 | case NC_RPL_DATA: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 919 | nc_write_clb((void *)&arg, "<data>", 6); |
Radek Krejci | 2927112 | 2016-02-29 16:41:43 +0100 | [diff] [blame] | 920 | lyd_print_clb(nc_write_clb, (void *)&arg, ((struct nc_reply_data *)reply)->data, LYD_XML, LYP_WITHSIBLINGS); |
Michal Vasko | 82b0629 | 2016-02-01 10:07:40 +0100 | [diff] [blame] | 921 | nc_write_clb((void *)&arg, "</data>", 7); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 922 | break; |
| 923 | case NC_RPL_ERROR: |
| 924 | error_rpl = (struct nc_server_reply_error *)reply; |
| 925 | for (i = 0; i < error_rpl->count; ++i) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 926 | nc_write_error(&arg, error_rpl->err[i]); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 927 | } |
| 928 | break; |
| 929 | default: |
| 930 | ERRINT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 931 | nc_write_clb((void *)&arg, NULL, 0); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 932 | va_end(ap); |
| 933 | return -1; |
| 934 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 935 | nc_write_clb((void *)&arg, "</rpc-reply>", 12); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 936 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 937 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 938 | case NC_MSG_NOTIF: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 939 | nc_write_clb((void *)&arg, "<notification xmlns=\""NC_NS_NOTIF"\"/>", 21 + 47 + 3); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 940 | /* TODO content */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 941 | nc_write_clb((void *)&arg, "</notification>", 12); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 942 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 943 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 944 | case NC_MSG_HELLO: |
| 945 | if (session->version != NC_VERSION_10) { |
| 946 | va_end(ap); |
| 947 | return -1; |
| 948 | } |
| 949 | capabilities = va_arg(ap, const char **); |
| 950 | sid = va_arg(ap, uint32_t*); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 951 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 952 | count = asprintf(&buf, "<hello xmlns=\"%s\"><capabilities>", NC_NS_BASE); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 953 | nc_write_clb((void *)&arg, buf, count); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 954 | free(buf); |
| 955 | for (i = 0; capabilities[i]; i++) { |
| 956 | count = asprintf(&buf, "<capability>%s</capability>", capabilities[i]); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 957 | nc_write_clb((void *)&arg, buf, count); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 958 | free(buf); |
| 959 | } |
| 960 | if (sid) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 961 | count = asprintf(&buf, "</capabilities><session-id>%u</session-id></hello>", *sid); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 962 | nc_write_clb((void *)&arg, buf, count); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 963 | free(buf); |
| 964 | } else { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 965 | nc_write_clb((void *)&arg, "</capabilities></hello>", 23); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 966 | } |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 967 | break; |
Michal Vasko | ed46234 | 2016-01-12 12:33:48 +0100 | [diff] [blame] | 968 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 969 | default: |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 970 | va_end(ap); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 971 | return -1; |
| 972 | } |
| 973 | |
| 974 | /* flush message */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 975 | nc_write_clb((void *)&arg, NULL, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 976 | |
| 977 | va_end(ap); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 978 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 979 | /* error was already written */ |
| 980 | return -1; |
| 981 | } |
| 982 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 983 | return 0; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 984 | } |