blob: d7ae67d246b79509279d945e31311fa731602c3c [file] [log] [blame]
Radek Krejci206fcd62015-10-07 15:42:48 +02001/**
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 Krejci9b81f5b2016-02-24 13:14:49 +01008 * 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 Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci206fcd62015-10-07 15:42:48 +020013 */
14
Miroslav Mareš9563b812017-08-19 17:45:36 +020015#define _GNU_SOURCE /* asprintf, signals */
Radek Krejci206fcd62015-10-07 15:42:48 +020016#include <assert.h>
17#include <errno.h>
Michal Vasko3512e402016-01-28 16:22:34 +010018#include <inttypes.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020019#include <poll.h>
20#include <signal.h>
Radek Krejcife0b3472015-10-12 13:43:42 +020021#include <stdarg.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020022#include <stdlib.h>
23#include <string.h>
Michal Vasko36c7be82017-02-22 13:37:59 +010024#include <time.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020025#include <unistd.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020026
Michal Vasko964e1732016-09-23 13:39:33 +020027#ifdef NC_ENABLED_TLS
28# include <openssl/err.h>
29#endif
30
Radek Krejci206fcd62015-10-07 15:42:48 +020031#include <libyang/libyang.h>
32
Michal Vasko9e8ac262020-04-07 13:06:45 +020033#include "compat.h"
Radek Krejci206fcd62015-10-07 15:42:48 +020034#include "libnetconf.h"
Radek Krejci206fcd62015-10-07 15:42:48 +020035
Michal Vasko8fe604c2020-02-10 15:25:04 +010036const char *nc_msgtype2str[] = {
37 "error",
38 "would block",
39 "no message",
40 "hello message",
41 "bad hello message",
42 "RPC message",
43 "rpc-reply message",
44 "rpc-reply message with wrong ID",
45 "notification message",
46};
47
Radek Krejcife0b3472015-10-12 13:43:42 +020048#define BUFFERSIZE 512
Radek Krejci206fcd62015-10-07 15:42:48 +020049
Michal Vasko90a87d92018-12-10 15:53:44 +010050#ifdef NC_ENABLED_TLS
51
52static char *
53nc_ssl_error_get_reasons(void)
54{
55 unsigned int e;
56 int reason_size, reason_len;
57 char *reasons = NULL;
58
59 reason_size = 1;
60 reason_len = 0;
61 while ((e = ERR_get_error())) {
62 if (reason_len) {
63 /* add "; " */
64 reason_size += 2;
65 reasons = nc_realloc(reasons, reason_size);
66 if (!reasons) {
67 ERRMEM;
68 return NULL;
69 }
70 reason_len += sprintf(reasons + reason_len, "; ");
71 }
72 reason_size += strlen(ERR_reason_error_string(e));
73 reasons = nc_realloc(reasons, reason_size);
74 if (!reasons) {
75 ERRMEM;
76 return NULL;
77 }
Rosen Peneve38d7ef2019-07-15 18:18:03 -070078 reason_len += sprintf(reasons + reason_len, "%s", ERR_reason_error_string(e));
Michal Vasko90a87d92018-12-10 15:53:44 +010079 }
80
81 return reasons;
82}
83
84#endif
85
Radek Krejci206fcd62015-10-07 15:42:48 +020086static ssize_t
Michal Vasko36c7be82017-02-22 13:37:59 +010087nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_timeout, struct timespec *ts_act_timeout)
Radek Krejci206fcd62015-10-07 15:42:48 +020088{
Michal Vasko81b33fb2016-09-26 14:57:36 +020089 size_t readd = 0;
Michal Vasko9d8bee62016-03-03 10:58:24 +010090 ssize_t r = -1;
Robin Jarry7de4b8e2019-10-14 21:46:00 +020091 int fd, interrupted;
Michal Vasko36c7be82017-02-22 13:37:59 +010092 struct timespec ts_cur, ts_inact_timeout;
Radek Krejci206fcd62015-10-07 15:42:48 +020093
94 assert(session);
95 assert(buf);
96
Michal Vasko428087d2016-01-14 16:04:28 +010097 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
98 return -1;
99 }
100
Radek Krejci206fcd62015-10-07 15:42:48 +0200101 if (!count) {
102 return 0;
103 }
104
Michal Vasko77a6abe2017-10-05 10:02:20 +0200105 nc_gettimespec_mono(&ts_inact_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100106 nc_addtimespec(&ts_inact_timeout, inact_timeout);
Michal Vasko81b33fb2016-09-26 14:57:36 +0200107 do {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200108 interrupted = 0;
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100109 switch (session->ti_type) {
110 case NC_TI_NONE:
111 return 0;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100112
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100113 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200114 case NC_TI_UNIX:
115 fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock;
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100116 /* read via standard file descriptor */
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200117 r = read(fd, buf + readd, count - readd);
Radek Krejci206fcd62015-10-07 15:42:48 +0200118 if (r < 0) {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200119 if (errno == EAGAIN) {
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100120 r = 0;
121 break;
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200122 } else if (errno == EINTR) {
123 r = 0;
124 interrupted = 1;
125 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200126 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100127 ERR("Session %u: reading from file descriptor (%d) failed (%s).",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200128 session->id, fd, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100129 session->status = NC_STATUS_INVALID;
130 session->term_reason = NC_SESSION_TERM_OTHER;
Radek Krejci206fcd62015-10-07 15:42:48 +0200131 return -1;
132 }
133 } else if (r == 0) {
Michal Vaskod083db62016-01-19 10:31:29 +0100134 ERR("Session %u: communication file descriptor (%d) unexpectedly closed.",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200135 session->id, fd);
Michal Vasko428087d2016-01-14 16:04:28 +0100136 session->status = NC_STATUS_INVALID;
137 session->term_reason = NC_SESSION_TERM_DROPPED;
Radek Krejci206fcd62015-10-07 15:42:48 +0200138 return -1;
139 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100140 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200141
Radek Krejci53691be2016-02-22 13:58:37 +0100142#ifdef NC_ENABLED_SSH
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100143 case NC_TI_LIBSSH:
144 /* read via libssh */
Michal Vasko81b33fb2016-09-26 14:57:36 +0200145 r = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0);
Radek Krejci206fcd62015-10-07 15:42:48 +0200146 if (r == SSH_AGAIN) {
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100147 r = 0;
148 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200149 } else if (r == SSH_ERROR) {
Michal Vasko051d35b2016-02-03 15:28:37 +0100150 ERR("Session %u: reading from the SSH channel failed (%s).", session->id,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200151 ssh_get_error(session->ti.libssh.session));
Michal Vasko428087d2016-01-14 16:04:28 +0100152 session->status = NC_STATUS_INVALID;
153 session->term_reason = NC_SESSION_TERM_OTHER;
Radek Krejci206fcd62015-10-07 15:42:48 +0200154 return -1;
155 } else if (r == 0) {
156 if (ssh_channel_is_eof(session->ti.libssh.channel)) {
Michal Vasko454e22b2016-01-21 15:34:08 +0100157 ERR("Session %u: SSH channel unexpected EOF.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100158 session->status = NC_STATUS_INVALID;
159 session->term_reason = NC_SESSION_TERM_DROPPED;
Radek Krejci206fcd62015-10-07 15:42:48 +0200160 return -1;
161 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100162 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200163 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100164 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200165#endif
166
Radek Krejci53691be2016-02-22 13:58:37 +0100167#ifdef NC_ENABLED_TLS
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100168 case NC_TI_OPENSSL:
169 /* read via OpenSSL */
Michal Vasko90a87d92018-12-10 15:53:44 +0100170 ERR_clear_error();
Michal Vasko81b33fb2016-09-26 14:57:36 +0200171 r = SSL_read(session->ti.tls, buf + readd, count - readd);
Radek Krejcid0046592015-10-08 12:52:02 +0200172 if (r <= 0) {
Michal Vasko0abba6d2018-12-10 14:09:39 +0100173 int e;
Michal Vasko90a87d92018-12-10 15:53:44 +0100174 char *reasons;
175
Michal Vasko0abba6d2018-12-10 14:09:39 +0100176 switch (e = SSL_get_error(session->ti.tls, r)) {
Radek Krejcid0046592015-10-08 12:52:02 +0200177 case SSL_ERROR_WANT_READ:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100178 case SSL_ERROR_WANT_WRITE:
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100179 r = 0;
180 break;
Radek Krejcid0046592015-10-08 12:52:02 +0200181 case SSL_ERROR_ZERO_RETURN:
Michal Vaskod083db62016-01-19 10:31:29 +0100182 ERR("Session %u: communication socket unexpectedly closed (OpenSSL).", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100183 session->status = NC_STATUS_INVALID;
184 session->term_reason = NC_SESSION_TERM_DROPPED;
Radek Krejcid0046592015-10-08 12:52:02 +0200185 return -1;
Michal Vasko0abba6d2018-12-10 14:09:39 +0100186 case SSL_ERROR_SYSCALL:
187 ERR("Session %u: SSL socket error (%s).", session->id, strerror(errno));
188 session->status = NC_STATUS_INVALID;
189 session->term_reason = NC_SESSION_TERM_OTHER;
190 return -1;
191 case SSL_ERROR_SSL:
Michal Vasko90a87d92018-12-10 15:53:44 +0100192 reasons = nc_ssl_error_get_reasons();
193 ERR("Session %u: SSL error (%s).", session->id, reasons);
194 free(reasons);
Michal Vasko0abba6d2018-12-10 14:09:39 +0100195 session->status = NC_STATUS_INVALID;
196 session->term_reason = NC_SESSION_TERM_OTHER;
197 return -1;
Radek Krejcid0046592015-10-08 12:52:02 +0200198 default:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100199 ERR("Session %u: unknown SSL error occured (err code %d).", session->id, e);
Michal Vasko428087d2016-01-14 16:04:28 +0100200 session->status = NC_STATUS_INVALID;
201 session->term_reason = NC_SESSION_TERM_OTHER;
Radek Krejcid0046592015-10-08 12:52:02 +0200202 return -1;
203 }
Radek Krejci206fcd62015-10-07 15:42:48 +0200204 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100205 break;
Radek Krejci206fcd62015-10-07 15:42:48 +0200206#endif
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100207 }
208
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100209 if (r == 0) {
Michal Vaskof471fa02017-02-15 10:48:12 +0100210 /* nothing read */
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200211 if (!interrupted) {
212 usleep(NC_TIMEOUT_STEP);
213 }
Michal Vasko77a6abe2017-10-05 10:02:20 +0200214 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +0100215 if ((nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) || (nc_difftimespec(&ts_cur, ts_act_timeout) < 1)) {
216 if (nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) {
Michal Vaskof471fa02017-02-15 10:48:12 +0100217 ERR("Session %u: inactive read timeout elapsed.", session->id);
218 } else {
219 ERR("Session %u: active read timeout elapsed.", session->id);
220 }
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100221 session->status = NC_STATUS_INVALID;
222 session->term_reason = NC_SESSION_TERM_OTHER;
223 return -1;
224 }
Michal Vaskof471fa02017-02-15 10:48:12 +0100225 } else {
226 /* something read */
227 readd += r;
Michal Vasko36c7be82017-02-22 13:37:59 +0100228
229 /* reset inactive timeout */
Michal Vasko77a6abe2017-10-05 10:02:20 +0200230 nc_gettimespec_mono(&ts_inact_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100231 nc_addtimespec(&ts_inact_timeout, inact_timeout);
Michal Vasko6b7c42e2016-03-02 15:46:41 +0100232 }
233
Michal Vasko81b33fb2016-09-26 14:57:36 +0200234 } while (readd < count);
235 buf[count] = '\0';
Radek Krejci206fcd62015-10-07 15:42:48 +0200236
Michal Vasko81b33fb2016-09-26 14:57:36 +0200237 return (ssize_t)readd;
Radek Krejci206fcd62015-10-07 15:42:48 +0200238}
239
240static ssize_t
Michal Vasko36c7be82017-02-22 13:37:59 +0100241nc_read_chunk(struct nc_session *session, size_t len, uint32_t inact_timeout, struct timespec *ts_act_timeout, char **chunk)
Radek Krejci206fcd62015-10-07 15:42:48 +0200242{
243 ssize_t r;
244
245 assert(session);
246 assert(chunk);
247
248 if (!len) {
249 return 0;
250 }
251
Michal Vasko4eb3c312016-03-01 14:09:37 +0100252 *chunk = malloc((len + 1) * sizeof **chunk);
Radek Krejci206fcd62015-10-07 15:42:48 +0200253 if (!*chunk) {
254 ERRMEM;
255 return -1;
256 }
257
Michal Vasko36c7be82017-02-22 13:37:59 +0100258 r = nc_read(session, *chunk, len, inact_timeout, ts_act_timeout);
Radek Krejci206fcd62015-10-07 15:42:48 +0200259 if (r <= 0) {
260 free(*chunk);
261 return -1;
262 }
263
264 /* terminating null byte */
Radek Krejcife0b3472015-10-12 13:43:42 +0200265 (*chunk)[r] = 0;
Radek Krejci206fcd62015-10-07 15:42:48 +0200266
267 return r;
268}
269
270static ssize_t
Michal Vasko36c7be82017-02-22 13:37:59 +0100271nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint32_t inact_timeout,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200272 struct timespec *ts_act_timeout, char **result)
Radek Krejci206fcd62015-10-07 15:42:48 +0200273{
274 char *chunk = NULL;
David Sedlákfedbc792018-07-04 11:07:07 +0200275 size_t size, count = 0, r, len, i, matched = 0;
Radek Krejci206fcd62015-10-07 15:42:48 +0200276
277 assert(session);
278 assert(endtag);
279
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200280 if (limit && (limit < BUFFERSIZE)) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200281 size = limit;
282 } else {
283 size = BUFFERSIZE;
284 }
Michal Vasko4eb3c312016-03-01 14:09:37 +0100285 chunk = malloc((size + 1) * sizeof *chunk);
Radek Krejcib791b532015-10-08 15:29:34 +0200286 if (!chunk) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200287 ERRMEM;
288 return -1;
289 }
290
291 len = strlen(endtag);
Michal Vasko428087d2016-01-14 16:04:28 +0100292 while (1) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200293 if (limit && (count == limit)) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200294 free(chunk);
Michal Vaskod083db62016-01-19 10:31:29 +0100295 WRN("Session %u: reading limit (%d) reached.", session->id, limit);
296 ERR("Session %u: invalid input data (missing \"%s\" sequence).", session->id, endtag);
Radek Krejci206fcd62015-10-07 15:42:48 +0200297 return -1;
298 }
299
300 /* resize buffer if needed */
David Sedlákfedbc792018-07-04 11:07:07 +0200301 if ((count + (len - matched)) >= size) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200302 /* get more memory */
303 size = size + BUFFERSIZE;
Radek Krejcif6d9aef2018-08-17 11:50:53 +0200304 chunk = nc_realloc(chunk, (size + 1) * sizeof *chunk);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100305 if (!chunk) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200306 ERRMEM;
Radek Krejci206fcd62015-10-07 15:42:48 +0200307 return -1;
308 }
Radek Krejci206fcd62015-10-07 15:42:48 +0200309 }
310
311 /* get another character */
David Sedlákfedbc792018-07-04 11:07:07 +0200312 r = nc_read(session, &(chunk[count]), len - matched, inact_timeout, ts_act_timeout);
313 if (r != len - matched) {
Radek Krejci206fcd62015-10-07 15:42:48 +0200314 free(chunk);
315 return -1;
316 }
317
David Sedlákfedbc792018-07-04 11:07:07 +0200318 count += len - matched;
Radek Krejci206fcd62015-10-07 15:42:48 +0200319
David Sedlákfedbc792018-07-04 11:07:07 +0200320 for (i = len - matched; i > 0; i--) {
321 if (!strncmp(&endtag[matched], &(chunk[count - i]), i)) {
322 /*part of endtag found */
323 matched += i;
Radek Krejci206fcd62015-10-07 15:42:48 +0200324 break;
David Sedlákfedbc792018-07-04 11:07:07 +0200325 } else {
326 matched = 0;
Radek Krejci206fcd62015-10-07 15:42:48 +0200327 }
328 }
David Sedlákfedbc792018-07-04 11:07:07 +0200329
330 /* whole endtag found */
331 if (matched == len) {
332 break;
333 }
Radek Krejci206fcd62015-10-07 15:42:48 +0200334 }
335
336 /* terminating null byte */
337 chunk[count] = 0;
338
339 if (result) {
340 *result = chunk;
Radek Krejcife0b3472015-10-12 13:43:42 +0200341 } else {
342 free(chunk);
Radek Krejci206fcd62015-10-07 15:42:48 +0200343 }
344 return count;
345}
346
Michal Vasko77367452021-02-16 16:32:18 +0100347int
348nc_read_msg_io(struct nc_session *session, int io_timeout, struct ly_in **msg, int passing_io_lock)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100349{
Michal Vasko77367452021-02-16 16:32:18 +0100350 int ret = 1, r, io_locked = passing_io_lock;
351 char *data = NULL, *chunk;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100352 uint64_t chunk_len, len = 0;
Michal Vasko36c7be82017-02-22 13:37:59 +0100353 /* use timeout in milliseconds instead seconds */
354 uint32_t inact_timeout = NC_READ_INACT_TIMEOUT * 1000;
355 struct timespec ts_act_timeout;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100356
Michal Vasko77367452021-02-16 16:32:18 +0100357 assert(session && msg);
358 *msg = NULL;
Michal Vasko428087d2016-01-14 16:04:28 +0100359
360 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100361 ERR("Session %u: invalid session to read from.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100362 ret = -1;
Michal Vasko131120a2018-05-29 15:44:02 +0200363 goto cleanup;
Michal Vasko428087d2016-01-14 16:04:28 +0100364 }
365
Michal Vasko77a6abe2017-10-05 10:02:20 +0200366 nc_gettimespec_mono(&ts_act_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100367 nc_addtimespec(&ts_act_timeout, NC_READ_ACT_TIMEOUT * 1000);
368
Michal Vasko131120a2018-05-29 15:44:02 +0200369 if (!io_locked) {
370 /* SESSION IO LOCK */
371 ret = nc_session_io_lock(session, io_timeout, __func__);
Michal Vasko77367452021-02-16 16:32:18 +0100372 if (ret < 1) {
Michal Vasko131120a2018-05-29 15:44:02 +0200373 goto cleanup;
374 }
375 io_locked = 1;
376 }
377
Michal Vasko05ba9df2016-01-13 14:40:27 +0100378 /* read the message */
379 switch (session->version) {
380 case NC_VERSION_10:
Michal Vasko77367452021-02-16 16:32:18 +0100381 r = nc_read_until(session, NC_VERSION_10_ENDTAG, 0, inact_timeout, &ts_act_timeout, &data);
382 if (r == -1) {
383 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200384 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100385 }
386
387 /* cut off the end tag */
Michal Vasko77367452021-02-16 16:32:18 +0100388 data[r - NC_VERSION_10_ENDTAG_LEN] = '\0';
Michal Vasko05ba9df2016-01-13 14:40:27 +0100389 break;
390 case NC_VERSION_11:
391 while (1) {
Michal Vasko77367452021-02-16 16:32:18 +0100392 r = nc_read_until(session, "\n#", 0, inact_timeout, &ts_act_timeout, NULL);
393 if (r == -1) {
394 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200395 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100396 }
Michal Vasko77367452021-02-16 16:32:18 +0100397 r = nc_read_until(session, "\n", 0, inact_timeout, &ts_act_timeout, &chunk);
398 if (r == -1) {
399 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200400 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100401 }
402
403 if (!strcmp(chunk, "#\n")) {
404 /* end of chunked framing message */
405 free(chunk);
Michal Vasko77367452021-02-16 16:32:18 +0100406 if (!data) {
Michal Vasko79df3262016-07-13 13:42:17 +0200407 ERR("Session %u: invalid frame chunk delimiters.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100408 ret = -2;
409 goto cleanup;
Michal Vasko79df3262016-07-13 13:42:17 +0200410 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100411 break;
412 }
413
414 /* convert string to the size of the following chunk */
415 chunk_len = strtoul(chunk, (char **)NULL, 10);
416 free(chunk);
417 if (!chunk_len) {
Michal Vaskod083db62016-01-19 10:31:29 +0100418 ERR("Session %u: invalid frame chunk size detected, fatal error.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100419 ret = -2;
420 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100421 }
422
423 /* now we have size of next chunk, so read the chunk */
Michal Vasko77367452021-02-16 16:32:18 +0100424 r = nc_read_chunk(session, chunk_len, inact_timeout, &ts_act_timeout, &chunk);
425 if (r == -1) {
426 ret = r;
Michal Vasko131120a2018-05-29 15:44:02 +0200427 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100428 }
429
430 /* realloc message buffer, remember to count terminating null byte */
Michal Vasko77367452021-02-16 16:32:18 +0100431 data = nc_realloc(data, len + chunk_len + 1);
432 if (!data) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100433 ERRMEM;
Michal Vasko77367452021-02-16 16:32:18 +0100434 ret = -1;
Michal Vasko131120a2018-05-29 15:44:02 +0200435 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100436 }
Michal Vasko77367452021-02-16 16:32:18 +0100437 memcpy(data + len, chunk, chunk_len);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100438 len += chunk_len;
Michal Vasko77367452021-02-16 16:32:18 +0100439 data[len] = '\0';
Michal Vasko05ba9df2016-01-13 14:40:27 +0100440 free(chunk);
441 }
442
443 break;
444 }
Michal Vasko131120a2018-05-29 15:44:02 +0200445
446 /* SESSION IO UNLOCK */
447 assert(io_locked);
448 nc_session_io_unlock(session, __func__);
449 io_locked = 0;
450
Michal Vasko77367452021-02-16 16:32:18 +0100451 DBG("Session %u: received message:\n%s\n", session->id, data);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100452
Michal Vasko77367452021-02-16 16:32:18 +0100453 /* build an input structure, eats data */
454 if (ly_in_new_memory(data, msg)) {
455 ret = -1;
456 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100457 }
Michal Vasko77367452021-02-16 16:32:18 +0100458 data = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100459
Michal Vasko131120a2018-05-29 15:44:02 +0200460cleanup:
461 if (io_locked) {
Michal Vasko77367452021-02-16 16:32:18 +0100462 /* SESSION IO UNLOCK */
Michal Vasko131120a2018-05-29 15:44:02 +0200463 nc_session_io_unlock(session, __func__);
464 }
Michal Vasko77367452021-02-16 16:32:18 +0100465 free(data);
Michal Vasko131120a2018-05-29 15:44:02 +0200466 return ret;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100467}
468
Michal Vasko428087d2016-01-14 16:04:28 +0100469/* return -1 means either poll error or that session was invalidated (socket error), EINTR is handled inside */
470static int
Michal Vasko131120a2018-05-29 15:44:02 +0200471nc_read_poll(struct nc_session *session, int io_timeout)
Michal Vasko428087d2016-01-14 16:04:28 +0100472{
Radek Krejci5961c702016-07-15 09:15:18 +0200473 sigset_t sigmask, origmask;
Michal Vasko428087d2016-01-14 16:04:28 +0100474 int ret = -2;
475 struct pollfd fds;
Michal Vasko428087d2016-01-14 16:04:28 +0100476
477 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100478 ERR("Session %u: invalid session to poll.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100479 return -1;
480 }
481
482 switch (session->ti_type) {
Radek Krejci53691be2016-02-22 13:58:37 +0100483#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100484 case NC_TI_LIBSSH:
485 /* EINTR is handled, it resumes waiting */
Michal Vasko131120a2018-05-29 15:44:02 +0200486 ret = ssh_channel_poll_timeout(session->ti.libssh.channel, io_timeout, 0);
Michal Vasko428087d2016-01-14 16:04:28 +0100487 if (ret == SSH_ERROR) {
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100488 ERR("Session %u: SSH channel poll error (%s).", session->id,
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200489 ssh_get_error(session->ti.libssh.session));
Michal Vasko428087d2016-01-14 16:04:28 +0100490 session->status = NC_STATUS_INVALID;
491 session->term_reason = NC_SESSION_TERM_OTHER;
492 return -1;
493 } else if (ret == SSH_EOF) {
Michal Vasko051d35b2016-02-03 15:28:37 +0100494 ERR("Session %u: SSH channel unexpected EOF.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100495 session->status = NC_STATUS_INVALID;
496 session->term_reason = NC_SESSION_TERM_DROPPED;
497 return -1;
498 } else if (ret > 0) {
499 /* fake it */
500 ret = 1;
501 fds.revents = POLLIN;
Michal Vasko5550cda2016-02-03 15:28:57 +0100502 } else { /* ret == 0 */
503 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100504 }
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100505 break;
Michal Vasko428087d2016-01-14 16:04:28 +0100506#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100507#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100508 case NC_TI_OPENSSL:
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100509 ret = SSL_pending(session->ti.tls);
510 if (ret) {
511 /* some buffered TLS data available */
512 ret = 1;
513 fds.revents = POLLIN;
514 break;
Michal Vasko428087d2016-01-14 16:04:28 +0100515 }
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100516
517 fds.fd = SSL_get_fd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100518#endif
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200519 /* fallthrough */
Michal Vasko428087d2016-01-14 16:04:28 +0100520 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200521 case NC_TI_UNIX:
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200522 if (session->ti_type == NC_TI_FD) {
Michal Vasko428087d2016-01-14 16:04:28 +0100523 fds.fd = session->ti.fd.in;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200524 } else if (session->ti_type == NC_TI_UNIX) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200525 fds.fd = session->ti.unixsock.sock;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200526 }
Michal Vasko428087d2016-01-14 16:04:28 +0100527
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100528 fds.events = POLLIN;
529 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100530
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100531 sigfillset(&sigmask);
532 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Michal Vasko131120a2018-05-29 15:44:02 +0200533 ret = poll(&fds, 1, io_timeout);
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100534 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
Michal Vasko428087d2016-01-14 16:04:28 +0100535
536 break;
537
538 default:
539 ERRINT;
540 return -1;
541 }
542
543 /* process the poll result, unified ret meaning for poll and ssh_channel poll */
544 if (ret < 0) {
545 /* poll failed - something really bad happened, close the session */
Radek Krejci5961c702016-07-15 09:15:18 +0200546 ERR("Session %u: poll error (%s).", session->id, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100547 session->status = NC_STATUS_INVALID;
548 session->term_reason = NC_SESSION_TERM_OTHER;
549 return -1;
550 } else { /* status > 0 */
551 /* in case of standard (non-libssh) poll, there still can be an error */
Michal Vasko428087d2016-01-14 16:04:28 +0100552 if (fds.revents & POLLERR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100553 ERR("Session %u: communication channel error.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100554 session->status = NC_STATUS_INVALID;
555 session->term_reason = NC_SESSION_TERM_OTHER;
556 return -1;
557 }
Robin Jarryf732adc2020-05-15 11:18:38 +0200558 /* Some poll() implementations may return POLLHUP|POLLIN when the other
559 * side has closed but there is data left to read in the buffer. */
560 if ((fds.revents & POLLHUP) && !(fds.revents & POLLIN)) {
561 ERR("Session %u: communication channel unexpectedly closed.", session->id);
562 session->status = NC_STATUS_INVALID;
563 session->term_reason = NC_SESSION_TERM_DROPPED;
564 return -1;
565 }
Michal Vasko428087d2016-01-14 16:04:28 +0100566 }
567
568 return ret;
569}
570
Michal Vasko77367452021-02-16 16:32:18 +0100571int
572nc_read_msg_poll_io(struct nc_session *session, int io_timeout, struct ly_in **msg)
Radek Krejci206fcd62015-10-07 15:42:48 +0200573{
Michal Vasko428087d2016-01-14 16:04:28 +0100574 int ret;
Radek Krejci206fcd62015-10-07 15:42:48 +0200575
Michal Vasko77367452021-02-16 16:32:18 +0100576 assert(msg);
577 *msg = NULL;
Radek Krejci206fcd62015-10-07 15:42:48 +0200578
Michal Vasko428087d2016-01-14 16:04:28 +0100579 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100580 ERR("Session %u: invalid session to read from.", session->id);
Michal Vasko77367452021-02-16 16:32:18 +0100581 return -1;
Radek Krejci206fcd62015-10-07 15:42:48 +0200582 }
583
Michal Vasko131120a2018-05-29 15:44:02 +0200584 /* SESSION IO LOCK */
585 ret = nc_session_io_lock(session, io_timeout, __func__);
Michal Vasko77367452021-02-16 16:32:18 +0100586 if (ret < 1) {
587 return ret;
Michal Vasko131120a2018-05-29 15:44:02 +0200588 }
589
590 ret = nc_read_poll(session, io_timeout);
Michal Vasko77367452021-02-16 16:32:18 +0100591 if (ret < 1) {
592 /* timed out or error */
Michal Vasko131120a2018-05-29 15:44:02 +0200593
594 /* SESSION IO UNLOCK */
595 nc_session_io_unlock(session, __func__);
Michal Vasko77367452021-02-16 16:32:18 +0100596 return ret;
Radek Krejci206fcd62015-10-07 15:42:48 +0200597 }
598
Michal Vasko131120a2018-05-29 15:44:02 +0200599 /* SESSION IO LOCK passed down */
Michal Vasko77367452021-02-16 16:32:18 +0100600 return nc_read_msg_io(session, io_timeout, msg, 1);
Radek Krejci206fcd62015-10-07 15:42:48 +0200601}
Radek Krejcife0b3472015-10-12 13:43:42 +0200602
Michal Vasko428087d2016-01-14 16:04:28 +0100603/* does not really log, only fatal errors */
604int
605nc_session_is_connected(struct nc_session *session)
606{
607 int ret;
608 struct pollfd fds;
609
610 switch (session->ti_type) {
611 case NC_TI_FD:
612 fds.fd = session->ti.fd.in;
613 break;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200614 case NC_TI_UNIX:
615 fds.fd = session->ti.unixsock.sock;
616 break;
Radek Krejci53691be2016-02-22 13:58:37 +0100617#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100618 case NC_TI_LIBSSH:
Michal Vasko840a8a62017-02-07 10:56:34 +0100619 return ssh_is_connected(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100620#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100621#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100622 case NC_TI_OPENSSL:
623 fds.fd = SSL_get_fd(session->ti.tls);
624 break;
625#endif
Michal Vaskof945da52018-02-15 08:45:13 +0100626 default:
Michal Vasko428087d2016-01-14 16:04:28 +0100627 return 0;
628 }
629
Michal Vasko840a8a62017-02-07 10:56:34 +0100630 if (fds.fd == -1) {
631 return 0;
632 }
633
Michal Vasko428087d2016-01-14 16:04:28 +0100634 fds.events = POLLIN;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100635 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100636
637 errno = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200638 while (((ret = poll(&fds, 1, 0)) == -1) && (errno == EINTR)) {}
Michal Vasko428087d2016-01-14 16:04:28 +0100639
640 if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100641 ERR("Session %u: poll failed (%s).", session->id, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100642 return 0;
643 } else if ((ret > 0) && (fds.revents & (POLLHUP | POLLERR))) {
644 return 0;
645 }
646
647 return 1;
648}
649
Radek Krejcife0b3472015-10-12 13:43:42 +0200650#define WRITE_BUFSIZE (2 * BUFFERSIZE)
651struct wclb_arg {
652 struct nc_session *session;
653 char buf[WRITE_BUFSIZE];
654 size_t len;
655};
656
Michal Vasko964e1732016-09-23 13:39:33 +0200657static int
Michal Vasko428087d2016-01-14 16:04:28 +0100658nc_write(struct nc_session *session, const void *buf, size_t count)
Radek Krejcife0b3472015-10-12 13:43:42 +0200659{
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200660 int c, fd, interrupted;
Michal Vasko964e1732016-09-23 13:39:33 +0200661 size_t written = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200662
Michal Vaskoe2357e92016-10-05 14:20:47 +0200663#ifdef NC_ENABLED_TLS
664 unsigned long e;
665#endif
Michal Vasko964e1732016-09-23 13:39:33 +0200666
Michal Vasko428087d2016-01-14 16:04:28 +0100667 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
668 return -1;
669 }
670
671 /* prevent SIGPIPE this way */
672 if (!nc_session_is_connected(session)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100673 ERR("Session %u: communication socket unexpectedly closed.", session->id);
Michal Vasko2a7d4732016-01-15 09:24:46 +0100674 session->status = NC_STATUS_INVALID;
675 session->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko428087d2016-01-14 16:04:28 +0100676 return -1;
677 }
678
Michal Vasko81b33fb2016-09-26 14:57:36 +0200679 DBG("Session %u: sending message:\n%.*s\n", session->id, count, buf);
Michal Vasko160b7912016-06-20 10:00:53 +0200680
Michal Vasko81b33fb2016-09-26 14:57:36 +0200681 do {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200682 interrupted = 0;
Michal Vasko964e1732016-09-23 13:39:33 +0200683 switch (session->ti_type) {
Michal Vasko964e1732016-09-23 13:39:33 +0200684 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200685 case NC_TI_UNIX:
686 fd = session->ti_type == NC_TI_FD ? session->ti.fd.out : session->ti.unixsock.sock;
687 c = write(fd, (char *)(buf + written), count - written);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200688 if ((c < 0) && (errno == EAGAIN)) {
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200689 c = 0;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200690 } else if ((c < 0) && (errno == EINTR)) {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200691 c = 0;
692 interrupted = 1;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200693 } else if (c < 0) {
Michal Vaskoe2146a32016-09-23 14:20:36 +0200694 ERR("Session %u: socket error (%s).", session->id, strerror(errno));
Michal Vasko964e1732016-09-23 13:39:33 +0200695 return -1;
696 }
697 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200698
Radek Krejci53691be2016-02-22 13:58:37 +0100699#ifdef NC_ENABLED_SSH
Michal Vasko964e1732016-09-23 13:39:33 +0200700 case NC_TI_LIBSSH:
701 if (ssh_channel_is_closed(session->ti.libssh.channel) || ssh_channel_is_eof(session->ti.libssh.channel)) {
702 if (ssh_channel_is_closed(session->ti.libssh.channel)) {
703 ERR("Session %u: SSH channel unexpectedly closed.", session->id);
704 } else {
705 ERR("Session %u: SSH channel unexpected EOF.", session->id);
706 }
707 session->status = NC_STATUS_INVALID;
708 session->term_reason = NC_SESSION_TERM_DROPPED;
709 return -1;
Michal Vasko454e22b2016-01-21 15:34:08 +0100710 }
Michal Vasko81b33fb2016-09-26 14:57:36 +0200711 c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written);
Michal Vasko964e1732016-09-23 13:39:33 +0200712 if ((c == SSH_ERROR) || (c == -1)) {
713 ERR("Session %u: SSH channel write failed.", session->id);
714 return -1;
715 }
716 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200717#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100718#ifdef NC_ENABLED_TLS
Michal Vasko964e1732016-09-23 13:39:33 +0200719 case NC_TI_OPENSSL:
Michal Vasko81b33fb2016-09-26 14:57:36 +0200720 c = SSL_write(session->ti.tls, (char *)(buf + written), count - written);
Michal Vasko964e1732016-09-23 13:39:33 +0200721 if (c < 1) {
Michal Vasko90a87d92018-12-10 15:53:44 +0100722 char *reasons;
723
Michal Vasko964e1732016-09-23 13:39:33 +0200724 switch ((e = SSL_get_error(session->ti.tls, c))) {
725 case SSL_ERROR_ZERO_RETURN:
726 ERR("Session %u: SSL connection was properly closed.", session->id);
727 return -1;
728 case SSL_ERROR_WANT_WRITE:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100729 case SSL_ERROR_WANT_READ:
Michal Vasko964e1732016-09-23 13:39:33 +0200730 c = 0;
731 break;
732 case SSL_ERROR_SYSCALL:
733 ERR("Session %u: SSL socket error (%s).", session->id, strerror(errno));
734 return -1;
735 case SSL_ERROR_SSL:
Michal Vasko90a87d92018-12-10 15:53:44 +0100736 reasons = nc_ssl_error_get_reasons();
737 ERR("Session %u: SSL error (%s).", session->id, reasons);
738 free(reasons);
Michal Vasko964e1732016-09-23 13:39:33 +0200739 return -1;
740 default:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100741 ERR("Session %u: unknown SSL error occured (err code %d).", session->id, e);
Michal Vasko964e1732016-09-23 13:39:33 +0200742 return -1;
743 }
744 }
745 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200746#endif
Michal Vasko339eea82016-09-29 11:42:36 +0200747 default:
748 ERRINT;
749 return -1;
Michal Vasko964e1732016-09-23 13:39:33 +0200750 }
751
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200752 if ((c == 0) && !interrupted) {
Michal Vasko964e1732016-09-23 13:39:33 +0200753 /* we must wait */
754 usleep(NC_TIMEOUT_STEP);
755 }
756
757 written += c;
Michal Vasko81b33fb2016-09-26 14:57:36 +0200758 } while (written < count);
Radek Krejcife0b3472015-10-12 13:43:42 +0200759
Michal Vasko964e1732016-09-23 13:39:33 +0200760 return written;
Radek Krejcife0b3472015-10-12 13:43:42 +0200761}
762
Michal Vasko428087d2016-01-14 16:04:28 +0100763static int
764nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count)
Michal Vasko086311b2016-01-08 09:53:11 +0100765{
Michal Vaskofd2db9b2016-01-14 16:15:16 +0100766 int ret = 0, c;
Claus Klein22091912020-01-20 13:45:47 +0100767 char chunksize[24];
Michal Vasko086311b2016-01-08 09:53:11 +0100768
Claus Klein22091912020-01-20 13:45:47 +0100769 // warning: ‘%zu’ directive writing between 4 and 20 bytes into a region of size 18 [-Wformat-overflow=]
Michal Vasko086311b2016-01-08 09:53:11 +0100770 if (session->version == NC_VERSION_11) {
771 sprintf(chunksize, "\n#%zu\n", count);
Michal Vasko428087d2016-01-14 16:04:28 +0100772 ret = nc_write(session, chunksize, strlen(chunksize));
773 if (ret == -1) {
774 return -1;
775 }
Michal Vasko086311b2016-01-08 09:53:11 +0100776 }
Michal Vasko428087d2016-01-14 16:04:28 +0100777
778 c = nc_write(session, buf, count);
779 if (c == -1) {
780 return -1;
781 }
782 ret += c;
783
784 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100785}
786
Radek Krejcife0b3472015-10-12 13:43:42 +0200787static int
Michal Vasko428087d2016-01-14 16:04:28 +0100788nc_write_endtag(struct nc_session *session)
Radek Krejcife0b3472015-10-12 13:43:42 +0200789{
Michal Vasko428087d2016-01-14 16:04:28 +0100790 int ret;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100791
Michal Vasko428087d2016-01-14 16:04:28 +0100792 if (session->version == NC_VERSION_11) {
793 ret = nc_write(session, "\n##\n", 4);
794 } else {
795 ret = nc_write(session, "]]>]]>", 6);
Radek Krejcife0b3472015-10-12 13:43:42 +0200796 }
797
Michal Vasko428087d2016-01-14 16:04:28 +0100798 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200799}
800
Michal Vasko428087d2016-01-14 16:04:28 +0100801static int
802nc_write_clb_flush(struct wclb_arg *warg)
Radek Krejcife0b3472015-10-12 13:43:42 +0200803{
Michal Vasko428087d2016-01-14 16:04:28 +0100804 int ret = 0;
805
Radek Krejcife0b3472015-10-12 13:43:42 +0200806 /* flush current buffer */
807 if (warg->len) {
Michal Vasko428087d2016-01-14 16:04:28 +0100808 ret = nc_write_starttag_and_msg(warg->session, warg->buf, warg->len);
Radek Krejcife0b3472015-10-12 13:43:42 +0200809 warg->len = 0;
810 }
Michal Vasko428087d2016-01-14 16:04:28 +0100811
812 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200813}
814
815static ssize_t
Radek Krejci047300e2016-03-08 16:46:58 +0100816nc_write_clb(void *arg, const void *buf, size_t count, int xmlcontent)
Radek Krejcife0b3472015-10-12 13:43:42 +0200817{
Michal Vasko428087d2016-01-14 16:04:28 +0100818 int ret = 0, c;
Radek Krejci047300e2016-03-08 16:46:58 +0100819 size_t l;
Radek Krejcife0b3472015-10-12 13:43:42 +0200820 struct wclb_arg *warg = (struct wclb_arg *)arg;
821
822 if (!buf) {
Michal Vasko428087d2016-01-14 16:04:28 +0100823 c = nc_write_clb_flush(warg);
824 if (c == -1) {
825 return -1;
826 }
827 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200828
829 /* endtag */
Michal Vasko428087d2016-01-14 16:04:28 +0100830 c = nc_write_endtag(warg->session);
831 if (c == -1) {
832 return -1;
833 }
834 ret += c;
835
836 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200837 }
838
839 if (warg->len && (warg->len + count > WRITE_BUFSIZE)) {
840 /* dump current buffer */
Michal Vasko428087d2016-01-14 16:04:28 +0100841 c = nc_write_clb_flush(warg);
842 if (c == -1) {
843 return -1;
844 }
845 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200846 }
Michal Vasko428087d2016-01-14 16:04:28 +0100847
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200848 if (!xmlcontent && (count > WRITE_BUFSIZE)) {
Radek Krejcife0b3472015-10-12 13:43:42 +0200849 /* write directly */
Michal Vasko428087d2016-01-14 16:04:28 +0100850 c = nc_write_starttag_and_msg(warg->session, buf, count);
851 if (c == -1) {
852 return -1;
853 }
854 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200855 } else {
856 /* keep in buffer and write later */
Radek Krejci047300e2016-03-08 16:46:58 +0100857 if (xmlcontent) {
858 for (l = 0; l < count; l++) {
859 if (warg->len + 5 >= WRITE_BUFSIZE) {
860 /* buffer is full */
861 c = nc_write_clb_flush(warg);
862 if (c == -1) {
863 return -1;
864 }
865 }
866
867 switch (((char *)buf)[l]) {
868 case '&':
869 ret += 5;
870 memcpy(&warg->buf[warg->len], "&amp;", 5);
871 warg->len += 5;
872 break;
873 case '<':
874 ret += 4;
875 memcpy(&warg->buf[warg->len], "&lt;", 4);
876 warg->len += 4;
877 break;
878 case '>':
879 /* not needed, just for readability */
880 ret += 4;
881 memcpy(&warg->buf[warg->len], "&gt;", 4);
882 warg->len += 4;
883 break;
884 default:
885 ret++;
886 memcpy(&warg->buf[warg->len], &((char *)buf)[l], 1);
887 warg->len++;
888 }
889 }
890 } else {
891 memcpy(&warg->buf[warg->len], buf, count);
892 warg->len += count; /* is <= WRITE_BUFSIZE */
893 ret += count;
894 }
Radek Krejcife0b3472015-10-12 13:43:42 +0200895 }
896
Michal Vasko428087d2016-01-14 16:04:28 +0100897 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200898}
899
Radek Krejci047300e2016-03-08 16:46:58 +0100900static ssize_t
901nc_write_xmlclb(void *arg, const void *buf, size_t count)
902{
Michal Vasko77367452021-02-16 16:32:18 +0100903 ssize_t r;
Radek Krejci047300e2016-03-08 16:46:58 +0100904
Michal Vasko77367452021-02-16 16:32:18 +0100905 r = nc_write_clb(arg, buf, count, 0);
906 if (r == -1) {
907 return -1;
Michal Vasko08611b32016-12-05 13:30:37 +0100908 }
909
Michal Vasko77367452021-02-16 16:32:18 +0100910 /* always return what libyang expects, simply that all the characters were printed */
911 return count;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100912}
913
Michal Vasko131120a2018-05-29 15:44:02 +0200914/* return NC_MSG_ERROR can change session status, acquires IO lock as needed */
915NC_MSG_TYPE
916nc_write_msg_io(struct nc_session *session, int io_timeout, int type, ...)
Radek Krejcife0b3472015-10-12 13:43:42 +0200917{
Radek Krejcid116db42016-01-08 15:36:30 +0100918 va_list ap;
Michal Vasko131120a2018-05-29 15:44:02 +0200919 int count, ret;
Michal Vasko77367452021-02-16 16:32:18 +0100920 const char *attrs;
921 struct lyd_node *op, *reply_envp, *node;
922 struct lyd_node_opaq *rpc_envp;
Radek Krejci93e80222016-10-03 13:34:25 +0200923 struct nc_server_notif *notif;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100924 struct nc_server_reply *reply;
Michal Vasko77367452021-02-16 16:32:18 +0100925 char *buf;
Radek Krejcid116db42016-01-08 15:36:30 +0100926 struct wclb_arg arg;
Radek Krejci695d4fa2015-10-22 13:23:54 +0200927 const char **capabilities;
Michal Vasko77367452021-02-16 16:32:18 +0100928 uint32_t *sid = NULL, i, wd = 0;
929 LY_ERR lyrc;
Radek Krejcife0b3472015-10-12 13:43:42 +0200930
Michal Vasko428087d2016-01-14 16:04:28 +0100931 assert(session);
932
933 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100934 ERR("Session %u: invalid session to write to.", session->id);
Michal Vasko131120a2018-05-29 15:44:02 +0200935 return NC_MSG_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +0100936 }
937
Radek Krejcife0b3472015-10-12 13:43:42 +0200938 arg.session = session;
939 arg.len = 0;
940
Michal Vasko131120a2018-05-29 15:44:02 +0200941 /* SESSION IO LOCK */
942 ret = nc_session_io_lock(session, io_timeout, __func__);
943 if (ret < 0) {
944 return NC_MSG_ERROR;
945 } else if (!ret) {
946 return NC_MSG_WOULDBLOCK;
947 }
948
949 va_start(ap, type);
Radek Krejci127f8952016-10-12 14:57:16 +0200950
Radek Krejcife0b3472015-10-12 13:43:42 +0200951 switch (type) {
952 case NC_MSG_RPC:
Michal Vasko77367452021-02-16 16:32:18 +0100953 op = va_arg(ap, struct lyd_node *);
Radek Krejcife0b3472015-10-12 13:43:42 +0200954 attrs = va_arg(ap, const char *);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100955
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200956 count = asprintf(&buf, "<rpc xmlns=\"%s\" message-id=\"%" PRIu64 "\"%s>",
Michal Vasko77367452021-02-16 16:32:18 +0100957 NC_NS_BASE, session->opts.client.msgid + 1, attrs ? attrs : "");
Michal Vasko4eb3c312016-03-01 14:09:37 +0100958 if (count == -1) {
959 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +0200960 ret = NC_MSG_ERROR;
961 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100962 }
Radek Krejci047300e2016-03-08 16:46:58 +0100963 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +0200964 free(buf);
Michal Vaskoe1708602016-10-18 12:17:22 +0200965
Michal Vasko77367452021-02-16 16:32:18 +0100966 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, op, LYD_XML, LYD_PRINT_SHRINK)) {
Michal Vasko131120a2018-05-29 15:44:02 +0200967 ret = NC_MSG_ERROR;
968 goto cleanup;
Michal Vasko5a91ce72017-10-19 11:30:02 +0200969 }
Radek Krejci047300e2016-03-08 16:46:58 +0100970 nc_write_clb((void *)&arg, "</rpc>", 6, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +0200971
Michal Vasko2e6defd2016-10-07 15:48:15 +0200972 session->opts.client.msgid++;
Radek Krejcife0b3472015-10-12 13:43:42 +0200973 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100974
Radek Krejcife0b3472015-10-12 13:43:42 +0200975 case NC_MSG_REPLY:
Michal Vasko77367452021-02-16 16:32:18 +0100976 rpc_envp = va_arg(ap, struct lyd_node_opaq *);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100977 reply = va_arg(ap, struct nc_server_reply *);
978
Michal Vasko77367452021-02-16 16:32:18 +0100979 if (!rpc_envp) {
980 /* can be NULL if replying with a malformed-message error */
981 nc_write_clb((void *)&arg, "<rpc-reply xmlns=\"" NC_NS_BASE "\">", 18 + strlen(NC_NS_BASE) + 2, 0);
982
983 assert(reply->type == NC_RPL_ERROR);
984 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, ((struct nc_server_reply_error *)reply)->err, LYD_XML,
985 LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS)) {
986 ret = NC_MSG_ERROR;
987 goto cleanup;
988 }
989
990 nc_write_clb((void *)&arg, "</rpc-reply>", 12, 0);
991 break;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +0100992 }
993
Michal Vasko77367452021-02-16 16:32:18 +0100994 /* build a rpc-reply opaque node that can be simply printed */
995 if (lyd_new_opaq2(NULL, session->ctx, "rpc-reply", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, &reply_envp)) {
996 ERRINT;
997 ret = NC_MSG_ERROR;
998 goto cleanup;
Michal Vaskoe7e534f2016-01-15 09:51:09 +0100999 }
Michal Vasko77367452021-02-16 16:32:18 +01001000
Michal Vasko05ba9df2016-01-13 14:40:27 +01001001 switch (reply->type) {
1002 case NC_RPL_OK:
Michal Vasko77367452021-02-16 16:32:18 +01001003 if (lyd_new_opaq2(reply_envp, NULL, "ok", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, NULL)) {
1004 lyd_free_tree(reply_envp);
1005
1006 ERRINT;
1007 ret = NC_MSG_ERROR;
1008 goto cleanup;
Michal Vasko08611b32016-12-05 13:30:37 +01001009 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001010 break;
1011 case NC_RPL_DATA:
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001012 switch (((struct nc_server_reply_data *)reply)->wd) {
Radek Krejci36dfdb32016-09-01 16:56:35 +02001013 case NC_WD_UNKNOWN:
1014 case NC_WD_EXPLICIT:
Michal Vasko77367452021-02-16 16:32:18 +01001015 wd = LYD_PRINT_WD_EXPLICIT;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001016 break;
1017 case NC_WD_TRIM:
Michal Vasko77367452021-02-16 16:32:18 +01001018 wd = LYD_PRINT_WD_TRIM;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001019 break;
1020 case NC_WD_ALL:
Michal Vasko77367452021-02-16 16:32:18 +01001021 wd = LYD_PRINT_WD_ALL;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001022 break;
1023 case NC_WD_ALL_TAG:
Michal Vasko77367452021-02-16 16:32:18 +01001024 wd = LYD_PRINT_WD_ALL_TAG;
Radek Krejci36dfdb32016-09-01 16:56:35 +02001025 break;
1026 }
Michal Vasko77367452021-02-16 16:32:18 +01001027
1028 node = ((struct nc_server_reply_data *)reply)->data;
1029 assert(node->schema->nodetype & (LYS_RPC | LYS_ACTION));
1030 if (lyd_child(node)) {
1031 /* temporary */
1032 lyd_child(node)->parent = NULL;
1033 lyd_insert_child(reply_envp, lyd_child(node));
1034 ((struct lyd_node_inner *)node)->child = NULL;
Michal Vasko5a91ce72017-10-19 11:30:02 +02001035 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001036 break;
1037 case NC_RPL_ERROR:
Michal Vasko77367452021-02-16 16:32:18 +01001038 /* temporary */
1039 lyd_insert_child(reply_envp, ((struct nc_server_reply_error *)reply)->err);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001040 break;
1041 default:
1042 ERRINT;
Radek Krejci047300e2016-03-08 16:46:58 +01001043 nc_write_clb((void *)&arg, NULL, 0, 0);
Michal Vasko131120a2018-05-29 15:44:02 +02001044 ret = NC_MSG_ERROR;
1045 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001046 }
Michal Vasko77367452021-02-16 16:32:18 +01001047
1048 /* temporary */
1049 ((struct lyd_node_opaq *)reply_envp)->attr = rpc_envp->attr;
1050
1051 /* print */
1052 lyrc = lyd_print_clb(nc_write_xmlclb, (void *)&arg, reply_envp, LYD_XML, LYD_PRINT_SHRINK | wd);
1053 ((struct lyd_node_opaq *)reply_envp)->attr = NULL;
1054
1055 /* cleanup */
1056 switch (reply->type) {
1057 case NC_RPL_OK:
1058 /* just free everything */
1059 lyd_free_tree(reply_envp);
1060 break;
1061 case NC_RPL_DATA:
1062 if (lyd_child(reply_envp)) {
1063 /* connect back to the reply structure */
1064 lyd_child(reply_envp)->parent = NULL;
1065 lyd_insert_child(((struct nc_server_reply_data *)reply)->data, lyd_child(reply_envp));
1066 ((struct lyd_node_opaq *)reply_envp)->child = NULL;
1067 }
1068 lyd_free_tree(reply_envp);
1069 break;
1070 case NC_RPL_ERROR:
1071 /* unlink from the data reply */
1072 lyd_unlink_tree(lyd_child(reply_envp));
1073 lyd_free_tree(reply_envp);
1074 break;
1075 default:
1076 break;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001077 }
Michal Vasko77367452021-02-16 16:32:18 +01001078
1079 if (lyrc) {
1080 ret = NC_MSG_ERROR;
1081 goto cleanup;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001082 }
Radek Krejcife0b3472015-10-12 13:43:42 +02001083 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001084
Radek Krejcife0b3472015-10-12 13:43:42 +02001085 case NC_MSG_NOTIF:
Radek Krejci93e80222016-10-03 13:34:25 +02001086 notif = va_arg(ap, struct nc_server_notif *);
1087
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001088 nc_write_clb((void *)&arg, "<notification xmlns=\""NC_NS_NOTIF "\">", 21 + 47 + 2, 0);
Radek Krejci93e80222016-10-03 13:34:25 +02001089 nc_write_clb((void *)&arg, "<eventTime>", 11, 0);
1090 nc_write_clb((void *)&arg, notif->eventtime, strlen(notif->eventtime), 0);
1091 nc_write_clb((void *)&arg, "</eventTime>", 12, 0);
Michal Vasko77367452021-02-16 16:32:18 +01001092 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, notif->ntf, LYD_XML, LYD_PRINT_SHRINK)) {
Michal Vasko131120a2018-05-29 15:44:02 +02001093 ret = NC_MSG_ERROR;
1094 goto cleanup;
Michal Vasko5a91ce72017-10-19 11:30:02 +02001095 }
mohitarora24878b2962016-11-09 18:45:33 -05001096 nc_write_clb((void *)&arg, "</notification>", 15, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001097 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001098
Radek Krejcid116db42016-01-08 15:36:30 +01001099 case NC_MSG_HELLO:
1100 if (session->version != NC_VERSION_10) {
Michal Vasko131120a2018-05-29 15:44:02 +02001101 ret = NC_MSG_ERROR;
1102 goto cleanup;
Radek Krejcid116db42016-01-08 15:36:30 +01001103 }
1104 capabilities = va_arg(ap, const char **);
Michal Vaskob83a3fa2021-05-26 09:53:42 +02001105 sid = va_arg(ap, uint32_t *);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001106
Radek Krejcid116db42016-01-08 15:36:30 +01001107 count = asprintf(&buf, "<hello xmlns=\"%s\"><capabilities>", NC_NS_BASE);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001108 if (count == -1) {
1109 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +02001110 ret = NC_MSG_ERROR;
1111 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001112 }
Radek Krejci047300e2016-03-08 16:46:58 +01001113 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001114 free(buf);
1115 for (i = 0; capabilities[i]; i++) {
Radek Krejci047300e2016-03-08 16:46:58 +01001116 nc_write_clb((void *)&arg, "<capability>", 12, 0);
1117 nc_write_clb((void *)&arg, capabilities[i], strlen(capabilities[i]), 1);
1118 nc_write_clb((void *)&arg, "</capability>", 13, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001119 }
1120 if (sid) {
Michal Vasko05ba9df2016-01-13 14:40:27 +01001121 count = asprintf(&buf, "</capabilities><session-id>%u</session-id></hello>", *sid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001122 if (count == -1) {
1123 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +02001124 ret = NC_MSG_ERROR;
1125 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001126 }
Radek Krejci047300e2016-03-08 16:46:58 +01001127 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001128 free(buf);
1129 } else {
Radek Krejci047300e2016-03-08 16:46:58 +01001130 nc_write_clb((void *)&arg, "</capabilities></hello>", 23, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001131 }
Radek Krejcid116db42016-01-08 15:36:30 +01001132 break;
Michal Vaskoed462342016-01-12 12:33:48 +01001133
Radek Krejcife0b3472015-10-12 13:43:42 +02001134 default:
Michal Vasko131120a2018-05-29 15:44:02 +02001135 ret = NC_MSG_ERROR;
1136 goto cleanup;
Radek Krejcife0b3472015-10-12 13:43:42 +02001137 }
1138
1139 /* flush message */
Radek Krejci047300e2016-03-08 16:46:58 +01001140 nc_write_clb((void *)&arg, NULL, 0, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001141
Michal Vasko428087d2016-01-14 16:04:28 +01001142 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
1143 /* error was already written */
Michal Vasko131120a2018-05-29 15:44:02 +02001144 ret = NC_MSG_ERROR;
1145 } else {
1146 /* specific message successfully sent */
1147 ret = type;
Michal Vasko428087d2016-01-14 16:04:28 +01001148 }
1149
Michal Vasko131120a2018-05-29 15:44:02 +02001150cleanup:
1151 va_end(ap);
1152 nc_session_io_unlock(session, __func__);
1153 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +02001154}
Michal Vasko4eb3c312016-03-01 14:09:37 +01001155
1156void *
1157nc_realloc(void *ptr, size_t size)
1158{
1159 void *ret;
1160
1161 ret = realloc(ptr, size);
1162 if (!ret) {
1163 free(ptr);
1164 }
1165
1166 return ret;
1167}