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