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