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