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