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