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