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