blob: e469bd3a32c01e70ff01aa81669a9e910255a0e7 [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>
18#include <poll.h>
Michal Vasko3512e402016-01-28 16:22:34 +010019#include <inttypes.h>
Radek Krejcife0b3472015-10-12 13:43:42 +020020#include <stdarg.h>
Radek Krejci206fcd62015-10-07 15:42:48 +020021#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
Michal Vasko11d142a2016-01-19 15:58:24 +010024#include <signal.h>
Michal Vasko36c7be82017-02-22 13:37:59 +010025#include <time.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).",
Olivier Matzac7fa2f2018-10-11 10:02:04 +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.",
Olivier Matzac7fa2f2018-10-11 10:02:04 +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,
151 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,
272 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
280 if (limit && limit < BUFFERSIZE) {
281 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) {
David Sedlák15dad862018-07-04 11:25:55 +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 Vasko131120a2018-05-29 15:44:02 +0200347/* return NC_MSG_ERROR can change session status, acquires IO lock as needed */
Radek Krejci206fcd62015-10-07 15:42:48 +0200348NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200349nc_read_msg_io(struct nc_session *session, int io_timeout, struct lyxml_elem **data, int passing_io_lock)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100350{
Michal Vasko131120a2018-05-29 15:44:02 +0200351 int ret, io_locked = passing_io_lock;
Michal Vasko4eb3c312016-03-01 14:09:37 +0100352 char *msg = NULL, *chunk;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100353 uint64_t chunk_len, len = 0;
Michal Vasko36c7be82017-02-22 13:37:59 +0100354 /* use timeout in milliseconds instead seconds */
355 uint32_t inact_timeout = NC_READ_INACT_TIMEOUT * 1000;
356 struct timespec ts_act_timeout;
Michal Vaskoe7e534f2016-01-15 09:51:09 +0100357 struct nc_server_reply *reply;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100358
Michal Vasko428087d2016-01-14 16:04:28 +0100359 assert(session && data);
360 *data = NULL;
361
362 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100363 ERR("Session %u: invalid session to read from.", session->id);
Michal Vasko131120a2018-05-29 15:44:02 +0200364 ret = NC_MSG_ERROR;
365 goto cleanup;
Michal Vasko428087d2016-01-14 16:04:28 +0100366 }
367
Michal Vasko77a6abe2017-10-05 10:02:20 +0200368 nc_gettimespec_mono(&ts_act_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100369 nc_addtimespec(&ts_act_timeout, NC_READ_ACT_TIMEOUT * 1000);
370
Michal Vasko131120a2018-05-29 15:44:02 +0200371 if (!io_locked) {
372 /* SESSION IO LOCK */
373 ret = nc_session_io_lock(session, io_timeout, __func__);
374 if (ret < 0) {
375 ret = NC_MSG_ERROR;
376 goto cleanup;
377 } else if (!ret) {
378 ret = NC_MSG_WOULDBLOCK;
379 goto cleanup;
380 }
381 io_locked = 1;
382 }
383
Michal Vasko05ba9df2016-01-13 14:40:27 +0100384 /* read the message */
385 switch (session->version) {
386 case NC_VERSION_10:
Michal Vasko36c7be82017-02-22 13:37:59 +0100387 ret = nc_read_until(session, NC_VERSION_10_ENDTAG, 0, inact_timeout, &ts_act_timeout, &msg);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100388 if (ret == -1) {
Michal Vasko131120a2018-05-29 15:44:02 +0200389 ret = NC_MSG_ERROR;
390 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100391 }
392
393 /* cut off the end tag */
394 msg[ret - NC_VERSION_10_ENDTAG_LEN] = '\0';
395 break;
396 case NC_VERSION_11:
397 while (1) {
Michal Vasko36c7be82017-02-22 13:37:59 +0100398 ret = nc_read_until(session, "\n#", 0, inact_timeout, &ts_act_timeout, NULL);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100399 if (ret == -1) {
Michal Vasko131120a2018-05-29 15:44:02 +0200400 ret = NC_MSG_ERROR;
401 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100402 }
Michal Vasko36c7be82017-02-22 13:37:59 +0100403 ret = nc_read_until(session, "\n", 0, inact_timeout, &ts_act_timeout, &chunk);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100404 if (ret == -1) {
Michal Vasko131120a2018-05-29 15:44:02 +0200405 ret = NC_MSG_ERROR;
406 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100407 }
408
409 if (!strcmp(chunk, "#\n")) {
410 /* end of chunked framing message */
411 free(chunk);
Michal Vasko79df3262016-07-13 13:42:17 +0200412 if (!msg) {
413 ERR("Session %u: invalid frame chunk delimiters.", session->id);
414 goto malformed_msg;
415 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100416 break;
417 }
418
419 /* convert string to the size of the following chunk */
420 chunk_len = strtoul(chunk, (char **)NULL, 10);
421 free(chunk);
422 if (!chunk_len) {
Michal Vaskod083db62016-01-19 10:31:29 +0100423 ERR("Session %u: invalid frame chunk size detected, fatal error.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100424 goto malformed_msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100425 }
426
427 /* now we have size of next chunk, so read the chunk */
Michal Vasko36c7be82017-02-22 13:37:59 +0100428 ret = nc_read_chunk(session, chunk_len, inact_timeout, &ts_act_timeout, &chunk);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100429 if (ret == -1) {
Michal Vasko131120a2018-05-29 15:44:02 +0200430 ret = NC_MSG_ERROR;
431 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100432 }
433
434 /* realloc message buffer, remember to count terminating null byte */
Radek Krejcif6d9aef2018-08-17 11:50:53 +0200435 msg = nc_realloc(msg, len + chunk_len + 1);
Michal Vasko4eb3c312016-03-01 14:09:37 +0100436 if (!msg) {
Michal Vasko05ba9df2016-01-13 14:40:27 +0100437 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +0200438 ret = NC_MSG_ERROR;
439 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100440 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100441 memcpy(msg + len, chunk, chunk_len);
442 len += chunk_len;
443 msg[len] = '\0';
444 free(chunk);
445 }
446
447 break;
448 }
Michal Vasko131120a2018-05-29 15:44:02 +0200449
450 /* SESSION IO UNLOCK */
451 assert(io_locked);
452 nc_session_io_unlock(session, __func__);
453 io_locked = 0;
454
Michal Vasko81b33fb2016-09-26 14:57:36 +0200455 DBG("Session %u: received message:\n%s\n", session->id, msg);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100456
457 /* build XML tree */
Michal Vaskod484a392019-11-11 09:48:17 +0100458 *data = lyxml_parse_mem(session->ctx, msg, LYXML_PARSE_NOMIXEDCONTENT);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100459 if (!*data) {
Michal Vasko428087d2016-01-14 16:04:28 +0100460 goto malformed_msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100461 } else if (!(*data)->ns) {
Michal Vaskod083db62016-01-19 10:31:29 +0100462 ERR("Session %u: invalid message root element (invalid namespace).", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100463 goto malformed_msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100464 }
465 free(msg);
466 msg = NULL;
467
468 /* get and return message type */
469 if (!strcmp((*data)->ns->value, NC_NS_BASE)) {
470 if (!strcmp((*data)->name, "rpc")) {
471 return NC_MSG_RPC;
472 } else if (!strcmp((*data)->name, "rpc-reply")) {
473 return NC_MSG_REPLY;
474 } else if (!strcmp((*data)->name, "hello")) {
475 return NC_MSG_HELLO;
476 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100477 ERR("Session %u: invalid message root element (invalid name \"%s\").", session->id, (*data)->name);
Michal Vasko428087d2016-01-14 16:04:28 +0100478 goto malformed_msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100479 }
480 } else if (!strcmp((*data)->ns->value, NC_NS_NOTIF)) {
481 if (!strcmp((*data)->name, "notification")) {
482 return NC_MSG_NOTIF;
483 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100484 ERR("Session %u: invalid message root element (invalid name \"%s\").", session->id, (*data)->name);
Michal Vasko428087d2016-01-14 16:04:28 +0100485 goto malformed_msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100486 }
487 } else {
Michal Vaskod083db62016-01-19 10:31:29 +0100488 ERR("Session %u: invalid message root element (invalid namespace \"%s\").", session->id, (*data)->ns->value);
Michal Vasko428087d2016-01-14 16:04:28 +0100489 goto malformed_msg;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100490 }
491
Michal Vasko428087d2016-01-14 16:04:28 +0100492malformed_msg:
Michal Vaskod083db62016-01-19 10:31:29 +0100493 ERR("Session %u: malformed message received.", session->id);
Michal Vaskoe7e534f2016-01-15 09:51:09 +0100494 if ((session->side == NC_SERVER) && (session->version == NC_VERSION_11)) {
495 /* NETCONF version 1.1 defines sending error reply from the server (RFC 6241 sec. 3) */
Michal Vasko1a38c862016-01-15 15:50:07 +0100496 reply = nc_server_reply_err(nc_err(NC_ERR_MALFORMED_MSG));
Michal Vasko428087d2016-01-14 16:04:28 +0100497
Michal Vaskofc5d07e2018-08-17 10:23:48 +0200498 if (io_locked) {
499 /* nc_write_msg_io locks and unlocks the lock by itself */
500 nc_session_io_unlock(session, __func__);
501 io_locked = 0;
502 }
503
Michal Vasko131120a2018-05-29 15:44:02 +0200504 if (nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, NULL, reply) != NC_MSG_REPLY) {
Michal Vaskod083db62016-01-19 10:31:29 +0100505 ERR("Session %u: unable to send a \"Malformed message\" error reply, terminating session.", session->id);
Michal Vaskoe7e534f2016-01-15 09:51:09 +0100506 if (session->status != NC_STATUS_INVALID) {
507 session->status = NC_STATUS_INVALID;
508 session->term_reason = NC_SESSION_TERM_OTHER;
509 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100510 }
Michal Vaskoe7e534f2016-01-15 09:51:09 +0100511 nc_server_reply_free(reply);
Michal Vasko05ba9df2016-01-13 14:40:27 +0100512 }
Michal Vasko131120a2018-05-29 15:44:02 +0200513 ret = NC_MSG_ERROR;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100514
Michal Vasko131120a2018-05-29 15:44:02 +0200515cleanup:
516 if (io_locked) {
517 nc_session_io_unlock(session, __func__);
518 }
Michal Vasko428087d2016-01-14 16:04:28 +0100519 free(msg);
520 free(*data);
521 *data = NULL;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100522
Michal Vasko131120a2018-05-29 15:44:02 +0200523 return ret;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100524}
525
Michal Vasko428087d2016-01-14 16:04:28 +0100526/* return -1 means either poll error or that session was invalidated (socket error), EINTR is handled inside */
527static int
Michal Vasko131120a2018-05-29 15:44:02 +0200528nc_read_poll(struct nc_session *session, int io_timeout)
Michal Vasko428087d2016-01-14 16:04:28 +0100529{
Radek Krejci5961c702016-07-15 09:15:18 +0200530 sigset_t sigmask, origmask;
Michal Vasko428087d2016-01-14 16:04:28 +0100531 int ret = -2;
532 struct pollfd fds;
Michal Vasko428087d2016-01-14 16:04:28 +0100533
534 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100535 ERR("Session %u: invalid session to poll.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100536 return -1;
537 }
538
539 switch (session->ti_type) {
Radek Krejci53691be2016-02-22 13:58:37 +0100540#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100541 case NC_TI_LIBSSH:
542 /* EINTR is handled, it resumes waiting */
Michal Vasko131120a2018-05-29 15:44:02 +0200543 ret = ssh_channel_poll_timeout(session->ti.libssh.channel, io_timeout, 0);
Michal Vasko428087d2016-01-14 16:04:28 +0100544 if (ret == SSH_ERROR) {
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100545 ERR("Session %u: SSH channel poll error (%s).", session->id,
Michal Vasko051d35b2016-02-03 15:28:37 +0100546 ssh_get_error(session->ti.libssh.session));
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 if (ret == SSH_EOF) {
Michal Vasko051d35b2016-02-03 15:28:37 +0100551 ERR("Session %u: SSH channel unexpected EOF.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100552 session->status = NC_STATUS_INVALID;
553 session->term_reason = NC_SESSION_TERM_DROPPED;
554 return -1;
555 } else if (ret > 0) {
556 /* fake it */
557 ret = 1;
558 fds.revents = POLLIN;
Michal Vasko5550cda2016-02-03 15:28:57 +0100559 } else { /* ret == 0 */
560 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100561 }
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100562 break;
Michal Vasko428087d2016-01-14 16:04:28 +0100563#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100564#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100565 case NC_TI_OPENSSL:
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100566 ret = SSL_pending(session->ti.tls);
567 if (ret) {
568 /* some buffered TLS data available */
569 ret = 1;
570 fds.revents = POLLIN;
571 break;
Michal Vasko428087d2016-01-14 16:04:28 +0100572 }
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100573
574 fds.fd = SSL_get_fd(session->ti.tls);
Michal Vasko428087d2016-01-14 16:04:28 +0100575#endif
Michal Vaskob983c002017-11-02 13:10:57 +0100576 /* fallthrough */
Michal Vasko428087d2016-01-14 16:04:28 +0100577 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200578 case NC_TI_UNIX:
579 if (session->ti_type == NC_TI_FD)
Michal Vasko428087d2016-01-14 16:04:28 +0100580 fds.fd = session->ti.fd.in;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200581 else if (session->ti_type == NC_TI_UNIX)
582 fds.fd = session->ti.unixsock.sock;
Michal Vasko428087d2016-01-14 16:04:28 +0100583
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100584 fds.events = POLLIN;
585 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100586
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100587 sigfillset(&sigmask);
588 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
Michal Vasko131120a2018-05-29 15:44:02 +0200589 ret = poll(&fds, 1, io_timeout);
Michal Vaskob5a58fa2017-01-31 09:47:50 +0100590 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
Michal Vasko428087d2016-01-14 16:04:28 +0100591
592 break;
593
594 default:
595 ERRINT;
596 return -1;
597 }
598
599 /* process the poll result, unified ret meaning for poll and ssh_channel poll */
600 if (ret < 0) {
601 /* poll failed - something really bad happened, close the session */
Radek Krejci5961c702016-07-15 09:15:18 +0200602 ERR("Session %u: poll error (%s).", session->id, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100603 session->status = NC_STATUS_INVALID;
604 session->term_reason = NC_SESSION_TERM_OTHER;
605 return -1;
606 } else { /* status > 0 */
607 /* in case of standard (non-libssh) poll, there still can be an error */
608 if (fds.revents & POLLHUP) {
Michal Vaskod083db62016-01-19 10:31:29 +0100609 ERR("Session %u: communication channel unexpectedly closed.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100610 session->status = NC_STATUS_INVALID;
611 session->term_reason = NC_SESSION_TERM_DROPPED;
612 return -1;
613 }
614 if (fds.revents & POLLERR) {
Michal Vaskod083db62016-01-19 10:31:29 +0100615 ERR("Session %u: communication channel error.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100616 session->status = NC_STATUS_INVALID;
617 session->term_reason = NC_SESSION_TERM_OTHER;
618 return -1;
619 }
620 }
621
622 return ret;
623}
624
Michal Vasko131120a2018-05-29 15:44:02 +0200625/* return NC_MSG_ERROR can change session status, acquires IO lock as needed */
Michal Vasko05ba9df2016-01-13 14:40:27 +0100626NC_MSG_TYPE
Michal Vasko131120a2018-05-29 15:44:02 +0200627nc_read_msg_poll_io(struct nc_session *session, int io_timeout, struct lyxml_elem **data)
Radek Krejci206fcd62015-10-07 15:42:48 +0200628{
Michal Vasko428087d2016-01-14 16:04:28 +0100629 int ret;
Radek Krejci206fcd62015-10-07 15:42:48 +0200630
631 assert(data);
632 *data = NULL;
633
Michal Vasko428087d2016-01-14 16:04:28 +0100634 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100635 ERR("Session %u: invalid session to read from.", session->id);
Michal Vasko428087d2016-01-14 16:04:28 +0100636 return NC_MSG_ERROR;
Radek Krejci206fcd62015-10-07 15:42:48 +0200637 }
638
Michal Vasko131120a2018-05-29 15:44:02 +0200639 /* SESSION IO LOCK */
640 ret = nc_session_io_lock(session, io_timeout, __func__);
641 if (ret < 0) {
642 return NC_MSG_ERROR;
643 } else if (!ret) {
644 return NC_MSG_WOULDBLOCK;
645 }
646
647 ret = nc_read_poll(session, io_timeout);
Michal Vasko428087d2016-01-14 16:04:28 +0100648 if (ret == 0) {
649 /* timed out */
Michal Vasko131120a2018-05-29 15:44:02 +0200650
651 /* SESSION IO UNLOCK */
652 nc_session_io_unlock(session, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100653 return NC_MSG_WOULDBLOCK;
654 } else if (ret < 0) {
655 /* poll error, error written */
Michal Vasko131120a2018-05-29 15:44:02 +0200656
657 /* SESSION IO UNLOCK */
658 nc_session_io_unlock(session, __func__);
Michal Vasko428087d2016-01-14 16:04:28 +0100659 return NC_MSG_ERROR;
Radek Krejci206fcd62015-10-07 15:42:48 +0200660 }
661
Michal Vasko131120a2018-05-29 15:44:02 +0200662 /* SESSION IO LOCK passed down */
663 return nc_read_msg_io(session, io_timeout, data, 1);
Radek Krejci206fcd62015-10-07 15:42:48 +0200664}
Radek Krejcife0b3472015-10-12 13:43:42 +0200665
Michal Vasko428087d2016-01-14 16:04:28 +0100666/* does not really log, only fatal errors */
667int
668nc_session_is_connected(struct nc_session *session)
669{
670 int ret;
671 struct pollfd fds;
672
673 switch (session->ti_type) {
674 case NC_TI_FD:
675 fds.fd = session->ti.fd.in;
676 break;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200677 case NC_TI_UNIX:
678 fds.fd = session->ti.unixsock.sock;
679 break;
Radek Krejci53691be2016-02-22 13:58:37 +0100680#ifdef NC_ENABLED_SSH
Michal Vasko428087d2016-01-14 16:04:28 +0100681 case NC_TI_LIBSSH:
Michal Vasko840a8a62017-02-07 10:56:34 +0100682 return ssh_is_connected(session->ti.libssh.session);
Michal Vasko428087d2016-01-14 16:04:28 +0100683#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100684#ifdef NC_ENABLED_TLS
Michal Vasko428087d2016-01-14 16:04:28 +0100685 case NC_TI_OPENSSL:
686 fds.fd = SSL_get_fd(session->ti.tls);
687 break;
688#endif
Michal Vaskof945da52018-02-15 08:45:13 +0100689 default:
Michal Vasko428087d2016-01-14 16:04:28 +0100690 return 0;
691 }
692
Michal Vasko840a8a62017-02-07 10:56:34 +0100693 if (fds.fd == -1) {
694 return 0;
695 }
696
Michal Vasko428087d2016-01-14 16:04:28 +0100697 fds.events = POLLIN;
Michal Vasko3e9d1682017-02-24 09:50:15 +0100698 fds.revents = 0;
Michal Vasko428087d2016-01-14 16:04:28 +0100699
700 errno = 0;
701 while (((ret = poll(&fds, 1, 0)) == -1) && (errno == EINTR));
702
703 if (ret == -1) {
Michal Vaskod083db62016-01-19 10:31:29 +0100704 ERR("Session %u: poll failed (%s).", session->id, strerror(errno));
Michal Vasko428087d2016-01-14 16:04:28 +0100705 return 0;
706 } else if ((ret > 0) && (fds.revents & (POLLHUP | POLLERR))) {
707 return 0;
708 }
709
710 return 1;
711}
712
Radek Krejcife0b3472015-10-12 13:43:42 +0200713#define WRITE_BUFSIZE (2 * BUFFERSIZE)
714struct wclb_arg {
715 struct nc_session *session;
716 char buf[WRITE_BUFSIZE];
717 size_t len;
718};
719
Michal Vasko964e1732016-09-23 13:39:33 +0200720static int
Michal Vasko428087d2016-01-14 16:04:28 +0100721nc_write(struct nc_session *session, const void *buf, size_t count)
Radek Krejcife0b3472015-10-12 13:43:42 +0200722{
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200723 int c, fd, interrupted;
Michal Vasko964e1732016-09-23 13:39:33 +0200724 size_t written = 0;
Michal Vaskoe2357e92016-10-05 14:20:47 +0200725#ifdef NC_ENABLED_TLS
726 unsigned long e;
727#endif
Michal Vasko964e1732016-09-23 13:39:33 +0200728
Michal Vasko428087d2016-01-14 16:04:28 +0100729 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
730 return -1;
731 }
732
733 /* prevent SIGPIPE this way */
734 if (!nc_session_is_connected(session)) {
Michal Vaskod083db62016-01-19 10:31:29 +0100735 ERR("Session %u: communication socket unexpectedly closed.", session->id);
Michal Vasko2a7d4732016-01-15 09:24:46 +0100736 session->status = NC_STATUS_INVALID;
737 session->term_reason = NC_SESSION_TERM_DROPPED;
Michal Vasko428087d2016-01-14 16:04:28 +0100738 return -1;
739 }
740
Michal Vasko81b33fb2016-09-26 14:57:36 +0200741 DBG("Session %u: sending message:\n%.*s\n", session->id, count, buf);
Michal Vasko160b7912016-06-20 10:00:53 +0200742
Michal Vasko81b33fb2016-09-26 14:57:36 +0200743 do {
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200744 interrupted = 0;
Michal Vasko964e1732016-09-23 13:39:33 +0200745 switch (session->ti_type) {
Michal Vasko964e1732016-09-23 13:39:33 +0200746 case NC_TI_FD:
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200747 case NC_TI_UNIX:
748 fd = session->ti_type == NC_TI_FD ? session->ti.fd.out : session->ti.unixsock.sock;
749 c = write(fd, (char *)(buf + written), count - written);
750 if (c < 0 && errno == EAGAIN) {
751 c = 0;
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200752 } else if (c < 0 && errno == EINTR) {
753 c = 0;
754 interrupted = 1;
Olivier Matzac7fa2f2018-10-11 10:02:04 +0200755 } else if (c < 0) {
Michal Vaskoe2146a32016-09-23 14:20:36 +0200756 ERR("Session %u: socket error (%s).", session->id, strerror(errno));
Michal Vasko964e1732016-09-23 13:39:33 +0200757 return -1;
758 }
759 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200760
Radek Krejci53691be2016-02-22 13:58:37 +0100761#ifdef NC_ENABLED_SSH
Michal Vasko964e1732016-09-23 13:39:33 +0200762 case NC_TI_LIBSSH:
763 if (ssh_channel_is_closed(session->ti.libssh.channel) || ssh_channel_is_eof(session->ti.libssh.channel)) {
764 if (ssh_channel_is_closed(session->ti.libssh.channel)) {
765 ERR("Session %u: SSH channel unexpectedly closed.", session->id);
766 } else {
767 ERR("Session %u: SSH channel unexpected EOF.", session->id);
768 }
769 session->status = NC_STATUS_INVALID;
770 session->term_reason = NC_SESSION_TERM_DROPPED;
771 return -1;
Michal Vasko454e22b2016-01-21 15:34:08 +0100772 }
Michal Vasko81b33fb2016-09-26 14:57:36 +0200773 c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written);
Michal Vasko964e1732016-09-23 13:39:33 +0200774 if ((c == SSH_ERROR) || (c == -1)) {
775 ERR("Session %u: SSH channel write failed.", session->id);
776 return -1;
777 }
778 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200779#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100780#ifdef NC_ENABLED_TLS
Michal Vasko964e1732016-09-23 13:39:33 +0200781 case NC_TI_OPENSSL:
Michal Vasko81b33fb2016-09-26 14:57:36 +0200782 c = SSL_write(session->ti.tls, (char *)(buf + written), count - written);
Michal Vasko964e1732016-09-23 13:39:33 +0200783 if (c < 1) {
Michal Vasko90a87d92018-12-10 15:53:44 +0100784 char *reasons;
785
Michal Vasko964e1732016-09-23 13:39:33 +0200786 switch ((e = SSL_get_error(session->ti.tls, c))) {
787 case SSL_ERROR_ZERO_RETURN:
788 ERR("Session %u: SSL connection was properly closed.", session->id);
789 return -1;
790 case SSL_ERROR_WANT_WRITE:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100791 case SSL_ERROR_WANT_READ:
Michal Vasko964e1732016-09-23 13:39:33 +0200792 c = 0;
793 break;
794 case SSL_ERROR_SYSCALL:
795 ERR("Session %u: SSL socket error (%s).", session->id, strerror(errno));
796 return -1;
797 case SSL_ERROR_SSL:
Michal Vasko90a87d92018-12-10 15:53:44 +0100798 reasons = nc_ssl_error_get_reasons();
799 ERR("Session %u: SSL error (%s).", session->id, reasons);
800 free(reasons);
Michal Vasko964e1732016-09-23 13:39:33 +0200801 return -1;
802 default:
Michal Vasko0abba6d2018-12-10 14:09:39 +0100803 ERR("Session %u: unknown SSL error occured (err code %d).", session->id, e);
Michal Vasko964e1732016-09-23 13:39:33 +0200804 return -1;
805 }
806 }
807 break;
Radek Krejcife0b3472015-10-12 13:43:42 +0200808#endif
Michal Vasko339eea82016-09-29 11:42:36 +0200809 default:
810 ERRINT;
811 return -1;
Michal Vasko964e1732016-09-23 13:39:33 +0200812 }
813
Robin Jarry7de4b8e2019-10-14 21:46:00 +0200814 if (c == 0 && !interrupted) {
Michal Vasko964e1732016-09-23 13:39:33 +0200815 /* we must wait */
816 usleep(NC_TIMEOUT_STEP);
817 }
818
819 written += c;
Michal Vasko81b33fb2016-09-26 14:57:36 +0200820 } while (written < count);
Radek Krejcife0b3472015-10-12 13:43:42 +0200821
Michal Vasko964e1732016-09-23 13:39:33 +0200822 return written;
Radek Krejcife0b3472015-10-12 13:43:42 +0200823}
824
Michal Vasko428087d2016-01-14 16:04:28 +0100825static int
826nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count)
Michal Vasko086311b2016-01-08 09:53:11 +0100827{
Michal Vaskofd2db9b2016-01-14 16:15:16 +0100828 int ret = 0, c;
Claus Klein22091912020-01-20 13:45:47 +0100829 char chunksize[24];
Michal Vasko086311b2016-01-08 09:53:11 +0100830
Claus Klein22091912020-01-20 13:45:47 +0100831 // warning: ‘%zu’ directive writing between 4 and 20 bytes into a region of size 18 [-Wformat-overflow=]
Michal Vasko086311b2016-01-08 09:53:11 +0100832 if (session->version == NC_VERSION_11) {
833 sprintf(chunksize, "\n#%zu\n", count);
Michal Vasko428087d2016-01-14 16:04:28 +0100834 ret = nc_write(session, chunksize, strlen(chunksize));
835 if (ret == -1) {
836 return -1;
837 }
Michal Vasko086311b2016-01-08 09:53:11 +0100838 }
Michal Vasko428087d2016-01-14 16:04:28 +0100839
840 c = nc_write(session, buf, count);
841 if (c == -1) {
842 return -1;
843 }
844 ret += c;
845
846 return ret;
Michal Vasko086311b2016-01-08 09:53:11 +0100847}
848
Radek Krejcife0b3472015-10-12 13:43:42 +0200849static int
Michal Vasko428087d2016-01-14 16:04:28 +0100850nc_write_endtag(struct nc_session *session)
Radek Krejcife0b3472015-10-12 13:43:42 +0200851{
Michal Vasko428087d2016-01-14 16:04:28 +0100852 int ret;
Michal Vasko38a7c6c2015-12-04 12:29:20 +0100853
Michal Vasko428087d2016-01-14 16:04:28 +0100854 if (session->version == NC_VERSION_11) {
855 ret = nc_write(session, "\n##\n", 4);
856 } else {
857 ret = nc_write(session, "]]>]]>", 6);
Radek Krejcife0b3472015-10-12 13:43:42 +0200858 }
859
Michal Vasko428087d2016-01-14 16:04:28 +0100860 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200861}
862
Michal Vasko428087d2016-01-14 16:04:28 +0100863static int
864nc_write_clb_flush(struct wclb_arg *warg)
Radek Krejcife0b3472015-10-12 13:43:42 +0200865{
Michal Vasko428087d2016-01-14 16:04:28 +0100866 int ret = 0;
867
Radek Krejcife0b3472015-10-12 13:43:42 +0200868 /* flush current buffer */
869 if (warg->len) {
Michal Vasko428087d2016-01-14 16:04:28 +0100870 ret = nc_write_starttag_and_msg(warg->session, warg->buf, warg->len);
Radek Krejcife0b3472015-10-12 13:43:42 +0200871 warg->len = 0;
872 }
Michal Vasko428087d2016-01-14 16:04:28 +0100873
874 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200875}
876
877static ssize_t
Radek Krejci047300e2016-03-08 16:46:58 +0100878nc_write_clb(void *arg, const void *buf, size_t count, int xmlcontent)
Radek Krejcife0b3472015-10-12 13:43:42 +0200879{
Michal Vasko428087d2016-01-14 16:04:28 +0100880 int ret = 0, c;
Radek Krejci047300e2016-03-08 16:46:58 +0100881 size_t l;
Radek Krejcife0b3472015-10-12 13:43:42 +0200882 struct wclb_arg *warg = (struct wclb_arg *)arg;
883
884 if (!buf) {
Michal Vasko428087d2016-01-14 16:04:28 +0100885 c = nc_write_clb_flush(warg);
886 if (c == -1) {
887 return -1;
888 }
889 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200890
891 /* endtag */
Michal Vasko428087d2016-01-14 16:04:28 +0100892 c = nc_write_endtag(warg->session);
893 if (c == -1) {
894 return -1;
895 }
896 ret += c;
897
898 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200899 }
900
901 if (warg->len && (warg->len + count > WRITE_BUFSIZE)) {
902 /* dump current buffer */
Michal Vasko428087d2016-01-14 16:04:28 +0100903 c = nc_write_clb_flush(warg);
904 if (c == -1) {
905 return -1;
906 }
907 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200908 }
Michal Vasko428087d2016-01-14 16:04:28 +0100909
Radek Krejci047300e2016-03-08 16:46:58 +0100910 if (!xmlcontent && count > WRITE_BUFSIZE) {
Radek Krejcife0b3472015-10-12 13:43:42 +0200911 /* write directly */
Michal Vasko428087d2016-01-14 16:04:28 +0100912 c = nc_write_starttag_and_msg(warg->session, buf, count);
913 if (c == -1) {
914 return -1;
915 }
916 ret += c;
Radek Krejcife0b3472015-10-12 13:43:42 +0200917 } else {
918 /* keep in buffer and write later */
Radek Krejci047300e2016-03-08 16:46:58 +0100919 if (xmlcontent) {
920 for (l = 0; l < count; l++) {
921 if (warg->len + 5 >= WRITE_BUFSIZE) {
922 /* buffer is full */
923 c = nc_write_clb_flush(warg);
924 if (c == -1) {
925 return -1;
926 }
927 }
928
929 switch (((char *)buf)[l]) {
930 case '&':
931 ret += 5;
932 memcpy(&warg->buf[warg->len], "&amp;", 5);
933 warg->len += 5;
934 break;
935 case '<':
936 ret += 4;
937 memcpy(&warg->buf[warg->len], "&lt;", 4);
938 warg->len += 4;
939 break;
940 case '>':
941 /* not needed, just for readability */
942 ret += 4;
943 memcpy(&warg->buf[warg->len], "&gt;", 4);
944 warg->len += 4;
945 break;
946 default:
947 ret++;
948 memcpy(&warg->buf[warg->len], &((char *)buf)[l], 1);
949 warg->len++;
950 }
951 }
952 } else {
953 memcpy(&warg->buf[warg->len], buf, count);
954 warg->len += count; /* is <= WRITE_BUFSIZE */
955 ret += count;
956 }
Radek Krejcife0b3472015-10-12 13:43:42 +0200957 }
958
Michal Vasko428087d2016-01-14 16:04:28 +0100959 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +0200960}
961
Radek Krejci047300e2016-03-08 16:46:58 +0100962static ssize_t
963nc_write_xmlclb(void *arg, const void *buf, size_t count)
964{
965 return nc_write_clb(arg, buf, count, 0);
966}
967
Michal Vasko05ba9df2016-01-13 14:40:27 +0100968static void
Michal Vasko52bd9492016-12-08 09:37:43 +0100969nc_write_error_elem(struct wclb_arg *arg, const char *name, uint16_t nam_len, const char *prefix, uint16_t pref_len,
970 int open, int no_attr)
Michal Vasko05ba9df2016-01-13 14:40:27 +0100971{
Michal Vasko08611b32016-12-05 13:30:37 +0100972 if (open) {
973 nc_write_clb((void *)arg, "<", 1, 0);
974 } else {
975 nc_write_clb((void *)arg, "</", 2, 0);
976 }
977
978 if (prefix) {
979 nc_write_clb((void *)arg, prefix, pref_len, 0);
980 nc_write_clb((void *)arg, ":", 1, 0);
981 }
982
983 nc_write_clb((void *)arg, name, nam_len, 0);
Michal Vasko52bd9492016-12-08 09:37:43 +0100984 if (!open || !no_attr) {
985 nc_write_clb((void *)arg, ">", 1, 0);
986 }
Michal Vasko08611b32016-12-05 13:30:37 +0100987}
988
989static void
990nc_write_error(struct wclb_arg *arg, struct nc_server_error *err, const char *prefix)
991{
Michal Vasko3e9d1682017-02-24 09:50:15 +0100992 uint16_t i, pref_len = 0;
Michal Vasko05ba9df2016-01-13 14:40:27 +0100993 char str_sid[11];
994
Michal Vasko08611b32016-12-05 13:30:37 +0100995 if (prefix) {
996 pref_len = strlen(prefix);
997 }
Michal Vasko05ba9df2016-01-13 14:40:27 +0100998
Michal Vasko52bd9492016-12-08 09:37:43 +0100999 nc_write_error_elem(arg, "rpc-error", 9, prefix, pref_len, 1, 0);
Michal Vasko08611b32016-12-05 13:30:37 +01001000
Michal Vasko52bd9492016-12-08 09:37:43 +01001001 nc_write_error_elem(arg, "error-type", 10, prefix, pref_len, 1, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001002 switch (err->type) {
1003 case NC_ERR_TYPE_TRAN:
Radek Krejci047300e2016-03-08 16:46:58 +01001004 nc_write_clb((void *)arg, "transport", 9, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001005 break;
1006 case NC_ERR_TYPE_RPC:
Radek Krejci047300e2016-03-08 16:46:58 +01001007 nc_write_clb((void *)arg, "rpc", 3, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001008 break;
1009 case NC_ERR_TYPE_PROT:
Radek Krejci047300e2016-03-08 16:46:58 +01001010 nc_write_clb((void *)arg, "protocol", 8, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001011 break;
1012 case NC_ERR_TYPE_APP:
Radek Krejci047300e2016-03-08 16:46:58 +01001013 nc_write_clb((void *)arg, "application", 11, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001014 break;
1015 default:
1016 ERRINT;
1017 return;
1018 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001019
Michal Vasko52bd9492016-12-08 09:37:43 +01001020 nc_write_error_elem(arg, "error-type", 10, prefix, pref_len, 0, 0);
Michal Vasko08611b32016-12-05 13:30:37 +01001021
Michal Vasko52bd9492016-12-08 09:37:43 +01001022 nc_write_error_elem(arg, "error-tag", 9, prefix, pref_len, 1, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001023 switch (err->tag) {
1024 case NC_ERR_IN_USE:
Radek Krejci047300e2016-03-08 16:46:58 +01001025 nc_write_clb((void *)arg, "in-use", 6, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001026 break;
1027 case NC_ERR_INVALID_VALUE:
Radek Krejci047300e2016-03-08 16:46:58 +01001028 nc_write_clb((void *)arg, "invalid-value", 13, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001029 break;
1030 case NC_ERR_TOO_BIG:
Radek Krejci047300e2016-03-08 16:46:58 +01001031 nc_write_clb((void *)arg, "too-big", 7, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001032 break;
1033 case NC_ERR_MISSING_ATTR:
Radek Krejci047300e2016-03-08 16:46:58 +01001034 nc_write_clb((void *)arg, "missing-attribute", 17, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001035 break;
1036 case NC_ERR_BAD_ATTR:
Radek Krejci047300e2016-03-08 16:46:58 +01001037 nc_write_clb((void *)arg, "bad-attribute", 13, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001038 break;
1039 case NC_ERR_UNKNOWN_ATTR:
Radek Krejci047300e2016-03-08 16:46:58 +01001040 nc_write_clb((void *)arg, "unknown-attribute", 17, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001041 break;
1042 case NC_ERR_MISSING_ELEM:
Radek Krejci047300e2016-03-08 16:46:58 +01001043 nc_write_clb((void *)arg, "missing-element", 15, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001044 break;
1045 case NC_ERR_BAD_ELEM:
Radek Krejci047300e2016-03-08 16:46:58 +01001046 nc_write_clb((void *)arg, "bad-element", 11, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001047 break;
1048 case NC_ERR_UNKNOWN_ELEM:
Radek Krejci047300e2016-03-08 16:46:58 +01001049 nc_write_clb((void *)arg, "unknown-element", 15, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001050 break;
1051 case NC_ERR_UNKNOWN_NS:
Radek Krejci047300e2016-03-08 16:46:58 +01001052 nc_write_clb((void *)arg, "unknown-namespace", 17, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001053 break;
1054 case NC_ERR_ACCESS_DENIED:
Radek Krejci047300e2016-03-08 16:46:58 +01001055 nc_write_clb((void *)arg, "access-denied", 13, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001056 break;
1057 case NC_ERR_LOCK_DENIED:
Radek Krejci047300e2016-03-08 16:46:58 +01001058 nc_write_clb((void *)arg, "lock-denied", 11, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001059 break;
1060 case NC_ERR_RES_DENIED:
Radek Krejci047300e2016-03-08 16:46:58 +01001061 nc_write_clb((void *)arg, "resource-denied", 15, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001062 break;
1063 case NC_ERR_ROLLBACK_FAILED:
Radek Krejci047300e2016-03-08 16:46:58 +01001064 nc_write_clb((void *)arg, "rollback-failed", 15, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001065 break;
1066 case NC_ERR_DATA_EXISTS:
Radek Krejci047300e2016-03-08 16:46:58 +01001067 nc_write_clb((void *)arg, "data-exists", 11, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001068 break;
1069 case NC_ERR_DATA_MISSING:
Radek Krejci047300e2016-03-08 16:46:58 +01001070 nc_write_clb((void *)arg, "data-missing", 12, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001071 break;
1072 case NC_ERR_OP_NOT_SUPPORTED:
Radek Krejci047300e2016-03-08 16:46:58 +01001073 nc_write_clb((void *)arg, "operation-not-supported", 23, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001074 break;
1075 case NC_ERR_OP_FAILED:
Radek Krejci047300e2016-03-08 16:46:58 +01001076 nc_write_clb((void *)arg, "operation-failed", 16, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001077 break;
1078 case NC_ERR_MALFORMED_MSG:
Radek Krejci047300e2016-03-08 16:46:58 +01001079 nc_write_clb((void *)arg, "malformed-message", 17, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001080 break;
1081 default:
1082 ERRINT;
1083 return;
1084 }
Michal Vasko52bd9492016-12-08 09:37:43 +01001085 nc_write_error_elem(arg, "error-tag", 9, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001086
Michal Vasko52bd9492016-12-08 09:37:43 +01001087 nc_write_error_elem(arg, "error-severity", 14, prefix, pref_len, 1, 0);
Michal Vasko08611b32016-12-05 13:30:37 +01001088 nc_write_clb((void *)arg, "error", 5, 0);
Michal Vasko52bd9492016-12-08 09:37:43 +01001089 nc_write_error_elem(arg, "error-severity", 14, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001090
1091 if (err->apptag) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001092 nc_write_error_elem(arg, "error-app-tag", 13, prefix, pref_len, 1, 0);
Radek Krejci047300e2016-03-08 16:46:58 +01001093 nc_write_clb((void *)arg, err->apptag, strlen(err->apptag), 1);
Michal Vasko52bd9492016-12-08 09:37:43 +01001094 nc_write_error_elem(arg, "error-app-tag", 13, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001095 }
1096
1097 if (err->path) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001098 nc_write_error_elem(arg, "error-path", 10, prefix, pref_len, 1, 0);
Radek Krejci047300e2016-03-08 16:46:58 +01001099 nc_write_clb((void *)arg, err->path, strlen(err->path), 1);
Michal Vasko52bd9492016-12-08 09:37:43 +01001100 nc_write_error_elem(arg, "error-path", 10, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001101 }
1102
1103 if (err->message) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001104 nc_write_error_elem(arg, "error-message", 13, prefix, pref_len, 1, 1);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001105 if (err->message_lang) {
Radek Krejci047300e2016-03-08 16:46:58 +01001106 nc_write_clb((void *)arg, " xml:lang=\"", 11, 0);
1107 nc_write_clb((void *)arg, err->message_lang, strlen(err->message_lang), 1);
1108 nc_write_clb((void *)arg, "\"", 1, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001109 }
Radek Krejci047300e2016-03-08 16:46:58 +01001110 nc_write_clb((void *)arg, ">", 1, 0);
1111 nc_write_clb((void *)arg, err->message, strlen(err->message), 1);
Michal Vasko52bd9492016-12-08 09:37:43 +01001112 nc_write_error_elem(arg, "error-message", 13, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001113 }
1114
Michal Vasko90920b02016-05-20 14:07:00 +02001115 if ((err->sid > -1) || err->attr_count || err->elem_count || err->ns_count || err->other_count) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001116 nc_write_error_elem(arg, "error-info", 10, prefix, pref_len, 1, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001117
Michal Vasko90920b02016-05-20 14:07:00 +02001118 if (err->sid > -1) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001119 nc_write_error_elem(arg, "session-id", 10, prefix, pref_len, 1, 0);
Michal Vasko90920b02016-05-20 14:07:00 +02001120 sprintf(str_sid, "%u", (uint32_t)err->sid);
Radek Krejci047300e2016-03-08 16:46:58 +01001121 nc_write_clb((void *)arg, str_sid, strlen(str_sid), 0);
Michal Vasko52bd9492016-12-08 09:37:43 +01001122 nc_write_error_elem(arg, "session-id", 10, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001123 }
1124
1125 for (i = 0; i < err->attr_count; ++i) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001126 nc_write_error_elem(arg, "bad-attribute", 13, prefix, pref_len, 1, 0);
Radek Krejci047300e2016-03-08 16:46:58 +01001127 nc_write_clb((void *)arg, err->attr[i], strlen(err->attr[i]), 1);
Michal Vasko52bd9492016-12-08 09:37:43 +01001128 nc_write_error_elem(arg, "bad-attribute", 13, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001129 }
1130
1131 for (i = 0; i < err->elem_count; ++i) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001132 nc_write_error_elem(arg, "bad-element", 11, prefix, pref_len, 1, 0);
Radek Krejci047300e2016-03-08 16:46:58 +01001133 nc_write_clb((void *)arg, err->elem[i], strlen(err->elem[i]), 1);
Michal Vasko52bd9492016-12-08 09:37:43 +01001134 nc_write_error_elem(arg, "bad-element", 11, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001135 }
1136
1137 for (i = 0; i < err->ns_count; ++i) {
Michal Vasko52bd9492016-12-08 09:37:43 +01001138 nc_write_error_elem(arg, "bad-namespace", 13, prefix, pref_len, 1, 0);
Radek Krejci047300e2016-03-08 16:46:58 +01001139 nc_write_clb((void *)arg, err->ns[i], strlen(err->ns[i]), 1);
Michal Vasko52bd9492016-12-08 09:37:43 +01001140 nc_write_error_elem(arg, "bad-namespace", 13, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001141 }
1142
1143 for (i = 0; i < err->other_count; ++i) {
Radek Krejci047300e2016-03-08 16:46:58 +01001144 lyxml_print_clb(nc_write_xmlclb, (void *)arg, err->other[i], 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001145 }
1146
Michal Vasko52bd9492016-12-08 09:37:43 +01001147 nc_write_error_elem(arg, "error-info", 10, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001148 }
1149
Michal Vasko52bd9492016-12-08 09:37:43 +01001150 nc_write_error_elem(arg, "rpc-error", 9, prefix, pref_len, 0, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001151}
1152
Michal Vasko131120a2018-05-29 15:44:02 +02001153/* return NC_MSG_ERROR can change session status, acquires IO lock as needed */
1154NC_MSG_TYPE
1155nc_write_msg_io(struct nc_session *session, int io_timeout, int type, ...)
Radek Krejcife0b3472015-10-12 13:43:42 +02001156{
Radek Krejcid116db42016-01-08 15:36:30 +01001157 va_list ap;
Michal Vasko131120a2018-05-29 15:44:02 +02001158 int count, ret;
Michal Vasko08611b32016-12-05 13:30:37 +01001159 const char *attrs, *base_prefix;
Radek Krejcife0b3472015-10-12 13:43:42 +02001160 struct lyd_node *content;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001161 struct lyxml_elem *rpc_elem;
Radek Krejci93e80222016-10-03 13:34:25 +02001162 struct nc_server_notif *notif;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001163 struct nc_server_reply *reply;
1164 struct nc_server_reply_error *error_rpl;
Radek Krejcid116db42016-01-08 15:36:30 +01001165 char *buf = NULL;
1166 struct wclb_arg arg;
Radek Krejci695d4fa2015-10-22 13:23:54 +02001167 const char **capabilities;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001168 uint32_t *sid = NULL, i;
Radek Krejcif9f93482016-09-21 14:11:15 +02001169 int wd = 0;
Radek Krejcife0b3472015-10-12 13:43:42 +02001170
Michal Vasko428087d2016-01-14 16:04:28 +01001171 assert(session);
1172
1173 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
Michal Vaskod083db62016-01-19 10:31:29 +01001174 ERR("Session %u: invalid session to write to.", session->id);
Michal Vasko131120a2018-05-29 15:44:02 +02001175 return NC_MSG_ERROR;
Michal Vasko428087d2016-01-14 16:04:28 +01001176 }
1177
Radek Krejcife0b3472015-10-12 13:43:42 +02001178 arg.session = session;
1179 arg.len = 0;
1180
Michal Vasko131120a2018-05-29 15:44:02 +02001181 /* SESSION IO LOCK */
1182 ret = nc_session_io_lock(session, io_timeout, __func__);
1183 if (ret < 0) {
1184 return NC_MSG_ERROR;
1185 } else if (!ret) {
1186 return NC_MSG_WOULDBLOCK;
1187 }
1188
1189 va_start(ap, type);
Radek Krejci127f8952016-10-12 14:57:16 +02001190
Radek Krejcife0b3472015-10-12 13:43:42 +02001191 switch (type) {
1192 case NC_MSG_RPC:
1193 content = va_arg(ap, struct lyd_node *);
1194 attrs = va_arg(ap, const char *);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001195
Radek Krejcife0b3472015-10-12 13:43:42 +02001196 count = asprintf(&buf, "<rpc xmlns=\"%s\" message-id=\"%"PRIu64"\"%s>",
Michal Vasko2e6defd2016-10-07 15:48:15 +02001197 NC_NS_BASE, session->opts.client.msgid + 1, attrs ? attrs : "");
Michal Vasko4eb3c312016-03-01 14:09:37 +01001198 if (count == -1) {
1199 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +02001200 ret = NC_MSG_ERROR;
1201 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001202 }
Radek Krejci047300e2016-03-08 16:46:58 +01001203 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001204 free(buf);
Michal Vaskoe1708602016-10-18 12:17:22 +02001205
Michal Vasko5a91ce72017-10-19 11:30:02 +02001206 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, content, LYD_XML, LYP_WITHSIBLINGS | LYP_NETCONF)) {
Michal Vasko131120a2018-05-29 15:44:02 +02001207 ret = NC_MSG_ERROR;
1208 goto cleanup;
Michal Vasko5a91ce72017-10-19 11:30:02 +02001209 }
Radek Krejci047300e2016-03-08 16:46:58 +01001210 nc_write_clb((void *)&arg, "</rpc>", 6, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001211
Michal Vasko2e6defd2016-10-07 15:48:15 +02001212 session->opts.client.msgid++;
Radek Krejcife0b3472015-10-12 13:43:42 +02001213 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001214
Radek Krejcife0b3472015-10-12 13:43:42 +02001215 case NC_MSG_REPLY:
Michal Vasko05ba9df2016-01-13 14:40:27 +01001216 rpc_elem = va_arg(ap, struct lyxml_elem *);
1217 reply = va_arg(ap, struct nc_server_reply *);
1218
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001219 if (rpc_elem && rpc_elem->ns && rpc_elem->ns->prefix) {
1220 nc_write_clb((void *)&arg, "<", 1, 0);
1221 nc_write_clb((void *)&arg, rpc_elem->ns->prefix, strlen(rpc_elem->ns->prefix), 0);
1222 nc_write_clb((void *)&arg, ":rpc-reply", 10, 0);
Michal Vasko08611b32016-12-05 13:30:37 +01001223 base_prefix = rpc_elem->ns->prefix;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001224 }
1225 else {
1226 nc_write_clb((void *)&arg, "<rpc-reply", 10, 0);
Michal Vasko08611b32016-12-05 13:30:37 +01001227 base_prefix = NULL;
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001228 }
1229
Michal Vaskoe7e534f2016-01-15 09:51:09 +01001230 /* can be NULL if replying with a malformed-message error */
1231 if (rpc_elem) {
Radek Krejci047300e2016-03-08 16:46:58 +01001232 lyxml_print_clb(nc_write_xmlclb, (void *)&arg, rpc_elem, LYXML_PRINT_ATTRS);
Radek Krejci844662e2016-04-13 16:54:43 +02001233 nc_write_clb((void *)&arg, ">", 1, 0);
1234 } else {
1235 /* but put there at least the correct namespace */
Michal Vaskobaaa9f02018-01-18 09:12:58 +01001236 nc_write_clb((void *)&arg, " xmlns=\""NC_NS_BASE"\">", 49, 0);
Michal Vaskoe7e534f2016-01-15 09:51:09 +01001237 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001238 switch (reply->type) {
1239 case NC_RPL_OK:
Michal Vasko08611b32016-12-05 13:30:37 +01001240 nc_write_clb((void *)&arg, "<", 1, 0);
1241 if (base_prefix) {
1242 nc_write_clb((void *)&arg, base_prefix, strlen(base_prefix), 0);
1243 nc_write_clb((void *)&arg, ":", 1, 0);
1244 }
1245 nc_write_clb((void *)&arg, "ok/>", 4, 0);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001246 break;
1247 case NC_RPL_DATA:
Radek Krejci36dfdb32016-09-01 16:56:35 +02001248 switch(((struct nc_server_reply_data *)reply)->wd) {
1249 case NC_WD_UNKNOWN:
1250 case NC_WD_EXPLICIT:
1251 wd = LYP_WD_EXPLICIT;
1252 break;
1253 case NC_WD_TRIM:
1254 wd = LYP_WD_TRIM;
1255 break;
1256 case NC_WD_ALL:
1257 wd = LYP_WD_ALL;
1258 break;
1259 case NC_WD_ALL_TAG:
1260 wd = LYP_WD_ALL_TAG;
1261 break;
1262 }
Michal Vasko5a91ce72017-10-19 11:30:02 +02001263 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, ((struct nc_reply_data *)reply)->data, LYD_XML,
1264 LYP_WITHSIBLINGS | LYP_NETCONF | wd)) {
Michal Vasko131120a2018-05-29 15:44:02 +02001265 ret = NC_MSG_ERROR;
1266 goto cleanup;
Michal Vasko5a91ce72017-10-19 11:30:02 +02001267 }
Michal Vasko05ba9df2016-01-13 14:40:27 +01001268 break;
1269 case NC_RPL_ERROR:
1270 error_rpl = (struct nc_server_reply_error *)reply;
1271 for (i = 0; i < error_rpl->count; ++i) {
Michal Vasko08611b32016-12-05 13:30:37 +01001272 nc_write_error(&arg, error_rpl->err[i], base_prefix);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001273 }
1274 break;
1275 default:
1276 ERRINT;
Radek Krejci047300e2016-03-08 16:46:58 +01001277 nc_write_clb((void *)&arg, NULL, 0, 0);
Michal Vasko131120a2018-05-29 15:44:02 +02001278 ret = NC_MSG_ERROR;
1279 goto cleanup;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001280 }
Michal Vasko7f0b0ff2016-11-15 11:02:28 +01001281 if (rpc_elem && rpc_elem->ns && rpc_elem->ns->prefix) {
1282 nc_write_clb((void *)&arg, "</", 2, 0);
1283 nc_write_clb((void *)&arg, rpc_elem->ns->prefix, strlen(rpc_elem->ns->prefix), 0);
1284 nc_write_clb((void *)&arg, ":rpc-reply>", 11, 0);
1285 }
1286 else {
1287 nc_write_clb((void *)&arg, "</rpc-reply>", 12, 0);
1288 }
Radek Krejcife0b3472015-10-12 13:43:42 +02001289 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001290
Radek Krejcife0b3472015-10-12 13:43:42 +02001291 case NC_MSG_NOTIF:
Radek Krejci93e80222016-10-03 13:34:25 +02001292 notif = va_arg(ap, struct nc_server_notif *);
1293
1294 nc_write_clb((void *)&arg, "<notification xmlns=\""NC_NS_NOTIF"\">", 21 + 47 + 2, 0);
1295 nc_write_clb((void *)&arg, "<eventTime>", 11, 0);
1296 nc_write_clb((void *)&arg, notif->eventtime, strlen(notif->eventtime), 0);
1297 nc_write_clb((void *)&arg, "</eventTime>", 12, 0);
Michal Vasko5a91ce72017-10-19 11:30:02 +02001298 if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, notif->tree, LYD_XML, 0)) {
Michal Vasko131120a2018-05-29 15:44:02 +02001299 ret = NC_MSG_ERROR;
1300 goto cleanup;
Michal Vasko5a91ce72017-10-19 11:30:02 +02001301 }
mohitarora24878b2962016-11-09 18:45:33 -05001302 nc_write_clb((void *)&arg, "</notification>", 15, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001303 break;
Michal Vasko05ba9df2016-01-13 14:40:27 +01001304
Radek Krejcid116db42016-01-08 15:36:30 +01001305 case NC_MSG_HELLO:
1306 if (session->version != NC_VERSION_10) {
Michal Vasko131120a2018-05-29 15:44:02 +02001307 ret = NC_MSG_ERROR;
1308 goto cleanup;
Radek Krejcid116db42016-01-08 15:36:30 +01001309 }
1310 capabilities = va_arg(ap, const char **);
1311 sid = va_arg(ap, uint32_t*);
Michal Vasko05ba9df2016-01-13 14:40:27 +01001312
Radek Krejcid116db42016-01-08 15:36:30 +01001313 count = asprintf(&buf, "<hello xmlns=\"%s\"><capabilities>", NC_NS_BASE);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001314 if (count == -1) {
1315 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +02001316 ret = NC_MSG_ERROR;
1317 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001318 }
Radek Krejci047300e2016-03-08 16:46:58 +01001319 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001320 free(buf);
1321 for (i = 0; capabilities[i]; i++) {
Radek Krejci047300e2016-03-08 16:46:58 +01001322 nc_write_clb((void *)&arg, "<capability>", 12, 0);
1323 nc_write_clb((void *)&arg, capabilities[i], strlen(capabilities[i]), 1);
1324 nc_write_clb((void *)&arg, "</capability>", 13, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001325 }
1326 if (sid) {
Michal Vasko05ba9df2016-01-13 14:40:27 +01001327 count = asprintf(&buf, "</capabilities><session-id>%u</session-id></hello>", *sid);
Michal Vasko4eb3c312016-03-01 14:09:37 +01001328 if (count == -1) {
1329 ERRMEM;
Michal Vasko131120a2018-05-29 15:44:02 +02001330 ret = NC_MSG_ERROR;
1331 goto cleanup;
Michal Vasko4eb3c312016-03-01 14:09:37 +01001332 }
Radek Krejci047300e2016-03-08 16:46:58 +01001333 nc_write_clb((void *)&arg, buf, count, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001334 free(buf);
1335 } else {
Radek Krejci047300e2016-03-08 16:46:58 +01001336 nc_write_clb((void *)&arg, "</capabilities></hello>", 23, 0);
Radek Krejcid116db42016-01-08 15:36:30 +01001337 }
Radek Krejcid116db42016-01-08 15:36:30 +01001338 break;
Michal Vaskoed462342016-01-12 12:33:48 +01001339
Radek Krejcife0b3472015-10-12 13:43:42 +02001340 default:
Michal Vasko131120a2018-05-29 15:44:02 +02001341 ret = NC_MSG_ERROR;
1342 goto cleanup;
Radek Krejcife0b3472015-10-12 13:43:42 +02001343 }
1344
1345 /* flush message */
Radek Krejci047300e2016-03-08 16:46:58 +01001346 nc_write_clb((void *)&arg, NULL, 0, 0);
Radek Krejcife0b3472015-10-12 13:43:42 +02001347
Michal Vasko428087d2016-01-14 16:04:28 +01001348 if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) {
1349 /* error was already written */
Michal Vasko131120a2018-05-29 15:44:02 +02001350 ret = NC_MSG_ERROR;
1351 } else {
1352 /* specific message successfully sent */
1353 ret = type;
Michal Vasko428087d2016-01-14 16:04:28 +01001354 }
1355
Michal Vasko131120a2018-05-29 15:44:02 +02001356cleanup:
1357 va_end(ap);
1358 nc_session_io_unlock(session, __func__);
1359 return ret;
Radek Krejcife0b3472015-10-12 13:43:42 +02001360}
Michal Vasko4eb3c312016-03-01 14:09:37 +01001361
1362void *
1363nc_realloc(void *ptr, size_t size)
1364{
1365 void *ret;
1366
1367 ret = realloc(ptr, size);
1368 if (!ret) {
1369 free(ptr);
1370 }
1371
1372 return ret;
1373}
David Sedlákfedbc792018-07-04 11:07:07 +02001374