Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file io.c |
| 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * \brief libnetconf2 - input/output functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Miroslav Mareš | 9563b81 | 2017-08-19 17:45:36 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE /* asprintf, signals */ |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | #include <errno.h> |
Michal Vasko | 3512e40 | 2016-01-28 16:22:34 +0100 | [diff] [blame] | 18 | #include <inttypes.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 19 | #include <poll.h> |
| 20 | #include <signal.h> |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 21 | #include <stdarg.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 24 | #include <time.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 25 | #include <unistd.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 26 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 27 | #ifdef NC_ENABLED_TLS |
| 28 | # include <openssl/err.h> |
| 29 | #endif |
| 30 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 31 | #include <libyang/libyang.h> |
| 32 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 33 | #include "compat.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 34 | #include "libnetconf.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 35 | |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 36 | const 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 Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 48 | #define BUFFERSIZE 512 |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 49 | |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 50 | #ifdef NC_ENABLED_TLS |
| 51 | |
| 52 | static char * |
| 53 | nc_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 Penev | e38d7ef | 2019-07-15 18:18:03 -0700 | [diff] [blame] | 78 | reason_len += sprintf(reasons + reason_len, "%s", ERR_reason_error_string(e)); |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return reasons; |
| 82 | } |
| 83 | |
| 84 | #endif |
| 85 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 86 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 87 | nc_read(struct nc_session *session, char *buf, size_t count, uint32_t inact_timeout, struct timespec *ts_act_timeout) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 88 | { |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 89 | size_t readd = 0; |
Michal Vasko | 9d8bee6 | 2016-03-03 10:58:24 +0100 | [diff] [blame] | 90 | ssize_t r = -1; |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 91 | int fd, interrupted; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 92 | struct timespec ts_cur, ts_inact_timeout; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 93 | |
| 94 | assert(session); |
| 95 | assert(buf); |
| 96 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 97 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 98 | return -1; |
| 99 | } |
| 100 | |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 101 | if (!count) { |
| 102 | return 0; |
| 103 | } |
| 104 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 105 | nc_gettimespec_mono(&ts_inact_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 106 | nc_addtimespec(&ts_inact_timeout, inact_timeout); |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 107 | do { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 108 | interrupted = 0; |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 109 | switch (session->ti_type) { |
| 110 | case NC_TI_NONE: |
| 111 | return 0; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 112 | |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 113 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 114 | case NC_TI_UNIX: |
| 115 | fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock; |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 116 | /* read via standard file descriptor */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 117 | r = read(fd, buf + readd, count - readd); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 118 | if (r < 0) { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 119 | if (errno == EAGAIN) { |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 120 | r = 0; |
| 121 | break; |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 122 | } else if (errno == EINTR) { |
| 123 | r = 0; |
| 124 | interrupted = 1; |
| 125 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 126 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 127 | ERR(session, "Reading from file descriptor (%d) failed (%s).", fd, strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 128 | session->status = NC_STATUS_INVALID; |
| 129 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | } else if (r == 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 133 | ERR(session, "Communication file descriptor (%d) unexpectedly closed.", fd); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 134 | session->status = NC_STATUS_INVALID; |
| 135 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 136 | return -1; |
| 137 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 138 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 139 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 140 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 141 | case NC_TI_LIBSSH: |
| 142 | /* read via libssh */ |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 143 | r = ssh_channel_read(session->ti.libssh.channel, buf + readd, count - readd, 0); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 144 | if (r == SSH_AGAIN) { |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 145 | r = 0; |
| 146 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 147 | } else if (r == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 148 | ERR(session, "Reading from the SSH channel failed (%s).", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 149 | session->status = NC_STATUS_INVALID; |
| 150 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 151 | return -1; |
| 152 | } else if (r == 0) { |
| 153 | if (ssh_channel_is_eof(session->ti.libssh.channel)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 154 | ERR(session, "SSH channel unexpected EOF."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 155 | session->status = NC_STATUS_INVALID; |
| 156 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 157 | return -1; |
| 158 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 159 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 160 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 161 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 162 | #endif |
| 163 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 164 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 165 | case NC_TI_OPENSSL: |
| 166 | /* read via OpenSSL */ |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 167 | ERR_clear_error(); |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 168 | r = SSL_read(session->ti.tls, buf + readd, count - readd); |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 169 | if (r <= 0) { |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 170 | int e; |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 171 | char *reasons; |
| 172 | |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 173 | switch (e = SSL_get_error(session->ti.tls, r)) { |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 174 | case SSL_ERROR_WANT_READ: |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 175 | case SSL_ERROR_WANT_WRITE: |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 176 | r = 0; |
| 177 | break; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 178 | case SSL_ERROR_ZERO_RETURN: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 179 | ERR(session, "Communication socket unexpectedly closed (OpenSSL)."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 180 | session->status = NC_STATUS_INVALID; |
| 181 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 182 | return -1; |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 183 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 0272dc3 | 2021-09-03 13:02:20 +0200 | [diff] [blame^] | 184 | ERR(session, "SSL socket error (%s).", errno ? strerror(errno) : "unexpected EOF"); |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 185 | session->status = NC_STATUS_INVALID; |
| 186 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 187 | return -1; |
| 188 | case SSL_ERROR_SSL: |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 189 | reasons = nc_ssl_error_get_reasons(); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 190 | ERR(session, "SSL error (%s).", reasons); |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 191 | free(reasons); |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 192 | session->status = NC_STATUS_INVALID; |
| 193 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 194 | return -1; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 195 | default: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 196 | ERR(session, "Unknown SSL error occured (err code %d).", e); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 197 | session->status = NC_STATUS_INVALID; |
| 198 | session->term_reason = NC_SESSION_TERM_OTHER; |
Radek Krejci | d004659 | 2015-10-08 12:52:02 +0200 | [diff] [blame] | 199 | return -1; |
| 200 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 201 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 202 | break; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 203 | #endif |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 206 | if (r == 0) { |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 207 | /* nothing read */ |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 208 | if (!interrupted) { |
| 209 | usleep(NC_TIMEOUT_STEP); |
| 210 | } |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 211 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 212 | if ((nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) || (nc_difftimespec(&ts_cur, ts_act_timeout) < 1)) { |
| 213 | if (nc_difftimespec(&ts_cur, &ts_inact_timeout) < 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 214 | ERR(session, "Inactive read timeout elapsed."); |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 215 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 216 | ERR(session, "Active read timeout elapsed."); |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 217 | } |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 218 | session->status = NC_STATUS_INVALID; |
| 219 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 220 | return -1; |
| 221 | } |
Michal Vasko | f471fa0 | 2017-02-15 10:48:12 +0100 | [diff] [blame] | 222 | } else { |
| 223 | /* something read */ |
| 224 | readd += r; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 225 | |
| 226 | /* reset inactive timeout */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 227 | nc_gettimespec_mono(&ts_inact_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 228 | nc_addtimespec(&ts_inact_timeout, inact_timeout); |
Michal Vasko | 6b7c42e | 2016-03-02 15:46:41 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 231 | } while (readd < count); |
| 232 | buf[count] = '\0'; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 233 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 234 | return (ssize_t)readd; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 238 | nc_read_chunk(struct nc_session *session, size_t len, uint32_t inact_timeout, struct timespec *ts_act_timeout, char **chunk) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 239 | { |
| 240 | ssize_t r; |
| 241 | |
| 242 | assert(session); |
| 243 | assert(chunk); |
| 244 | |
| 245 | if (!len) { |
| 246 | return 0; |
| 247 | } |
| 248 | |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 249 | *chunk = malloc((len + 1) * sizeof **chunk); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 250 | if (!*chunk) { |
| 251 | ERRMEM; |
| 252 | return -1; |
| 253 | } |
| 254 | |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 255 | r = nc_read(session, *chunk, len, inact_timeout, ts_act_timeout); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 256 | if (r <= 0) { |
| 257 | free(*chunk); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | /* terminating null byte */ |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 262 | (*chunk)[r] = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 263 | |
| 264 | return r; |
| 265 | } |
| 266 | |
| 267 | static ssize_t |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 268 | nc_read_until(struct nc_session *session, const char *endtag, size_t limit, uint32_t inact_timeout, |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 269 | struct timespec *ts_act_timeout, char **result) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 270 | { |
| 271 | char *chunk = NULL; |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 272 | size_t size, count = 0, r, len, i, matched = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 273 | |
| 274 | assert(session); |
| 275 | assert(endtag); |
| 276 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 277 | if (limit && (limit < BUFFERSIZE)) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 278 | size = limit; |
| 279 | } else { |
| 280 | size = BUFFERSIZE; |
| 281 | } |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 282 | chunk = malloc((size + 1) * sizeof *chunk); |
Radek Krejci | b791b53 | 2015-10-08 15:29:34 +0200 | [diff] [blame] | 283 | if (!chunk) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 284 | ERRMEM; |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | len = strlen(endtag); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 289 | while (1) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 290 | if (limit && (count == limit)) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 291 | free(chunk); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 292 | WRN(session, "Reading limit (%d) reached.", limit); |
| 293 | ERR(session, "Invalid input data (missing \"%s\" sequence).", endtag); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | /* resize buffer if needed */ |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 298 | if ((count + (len - matched)) >= size) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 299 | /* get more memory */ |
| 300 | size = size + BUFFERSIZE; |
Radek Krejci | f6d9aef | 2018-08-17 11:50:53 +0200 | [diff] [blame] | 301 | chunk = nc_realloc(chunk, (size + 1) * sizeof *chunk); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 302 | if (!chunk) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 303 | ERRMEM; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 304 | return -1; |
| 305 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* get another character */ |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 309 | r = nc_read(session, &(chunk[count]), len - matched, inact_timeout, ts_act_timeout); |
| 310 | if (r != len - matched) { |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 311 | free(chunk); |
| 312 | return -1; |
| 313 | } |
| 314 | |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 315 | count += len - matched; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 316 | |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 317 | for (i = len - matched; i > 0; i--) { |
| 318 | if (!strncmp(&endtag[matched], &(chunk[count - i]), i)) { |
| 319 | /*part of endtag found */ |
| 320 | matched += i; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 321 | break; |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 322 | } else { |
| 323 | matched = 0; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 324 | } |
| 325 | } |
David Sedlák | fedbc79 | 2018-07-04 11:07:07 +0200 | [diff] [blame] | 326 | |
| 327 | /* whole endtag found */ |
| 328 | if (matched == len) { |
| 329 | break; |
| 330 | } |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* terminating null byte */ |
| 334 | chunk[count] = 0; |
| 335 | |
| 336 | if (result) { |
| 337 | *result = chunk; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 338 | } else { |
| 339 | free(chunk); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 340 | } |
| 341 | return count; |
| 342 | } |
| 343 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 344 | int |
| 345 | nc_read_msg_io(struct nc_session *session, int io_timeout, struct ly_in **msg, int passing_io_lock) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 346 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 347 | int ret = 1, r, io_locked = passing_io_lock; |
| 348 | char *data = NULL, *chunk; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 349 | uint64_t chunk_len, len = 0; |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 350 | /* use timeout in milliseconds instead seconds */ |
| 351 | uint32_t inact_timeout = NC_READ_INACT_TIMEOUT * 1000; |
| 352 | struct timespec ts_act_timeout; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 353 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 354 | assert(session && msg); |
| 355 | *msg = NULL; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 356 | |
| 357 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 358 | ERR(session, "Invalid session to read from."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 359 | ret = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 360 | goto cleanup; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 363 | nc_gettimespec_mono(&ts_act_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 364 | nc_addtimespec(&ts_act_timeout, NC_READ_ACT_TIMEOUT * 1000); |
| 365 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 366 | if (!io_locked) { |
| 367 | /* SESSION IO LOCK */ |
| 368 | ret = nc_session_io_lock(session, io_timeout, __func__); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 369 | if (ret < 1) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 370 | goto cleanup; |
| 371 | } |
| 372 | io_locked = 1; |
| 373 | } |
| 374 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 375 | /* read the message */ |
| 376 | switch (session->version) { |
| 377 | case NC_VERSION_10: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 378 | r = nc_read_until(session, NC_VERSION_10_ENDTAG, 0, inact_timeout, &ts_act_timeout, &data); |
| 379 | if (r == -1) { |
| 380 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 381 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* cut off the end tag */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 385 | data[r - NC_VERSION_10_ENDTAG_LEN] = '\0'; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 386 | break; |
| 387 | case NC_VERSION_11: |
| 388 | while (1) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 389 | r = nc_read_until(session, "\n#", 0, inact_timeout, &ts_act_timeout, NULL); |
| 390 | if (r == -1) { |
| 391 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 392 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 393 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 394 | r = nc_read_until(session, "\n", 0, inact_timeout, &ts_act_timeout, &chunk); |
| 395 | if (r == -1) { |
| 396 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 397 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | if (!strcmp(chunk, "#\n")) { |
| 401 | /* end of chunked framing message */ |
| 402 | free(chunk); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 403 | if (!data) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 404 | ERR(session, "Invalid frame chunk delimiters."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 405 | ret = -2; |
| 406 | goto cleanup; |
Michal Vasko | 79df326 | 2016-07-13 13:42:17 +0200 | [diff] [blame] | 407 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | |
| 411 | /* convert string to the size of the following chunk */ |
| 412 | chunk_len = strtoul(chunk, (char **)NULL, 10); |
| 413 | free(chunk); |
| 414 | if (!chunk_len) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 415 | ERR(session, "Invalid frame chunk size detected, fatal error."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 416 | ret = -2; |
| 417 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* now we have size of next chunk, so read the chunk */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 421 | r = nc_read_chunk(session, chunk_len, inact_timeout, &ts_act_timeout, &chunk); |
| 422 | if (r == -1) { |
| 423 | ret = r; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 424 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* realloc message buffer, remember to count terminating null byte */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 428 | data = nc_realloc(data, len + chunk_len + 1); |
| 429 | if (!data) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 430 | ERRMEM; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 431 | ret = -1; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 432 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 433 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 434 | memcpy(data + len, chunk, chunk_len); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 435 | len += chunk_len; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 436 | data[len] = '\0'; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 437 | free(chunk); |
| 438 | } |
| 439 | |
| 440 | break; |
| 441 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 442 | |
| 443 | /* SESSION IO UNLOCK */ |
| 444 | assert(io_locked); |
| 445 | nc_session_io_unlock(session, __func__); |
| 446 | io_locked = 0; |
| 447 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 448 | DBG(session, "Received message:\n%s\n", data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 449 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 450 | /* build an input structure, eats data */ |
| 451 | if (ly_in_new_memory(data, msg)) { |
| 452 | ret = -1; |
| 453 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 454 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 455 | data = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 456 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 457 | cleanup: |
| 458 | if (io_locked) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 459 | /* SESSION IO UNLOCK */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 460 | nc_session_io_unlock(session, __func__); |
| 461 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 462 | free(data); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 463 | return ret; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 466 | /* return -1 means either poll error or that session was invalidated (socket error), EINTR is handled inside */ |
| 467 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 468 | nc_read_poll(struct nc_session *session, int io_timeout) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 469 | { |
Radek Krejci | 5961c70 | 2016-07-15 09:15:18 +0200 | [diff] [blame] | 470 | sigset_t sigmask, origmask; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 471 | int ret = -2; |
| 472 | struct pollfd fds; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 473 | |
| 474 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 475 | ERR(session, "Invalid session to poll."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | switch (session->ti_type) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 480 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 481 | case NC_TI_LIBSSH: |
| 482 | /* EINTR is handled, it resumes waiting */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 483 | ret = ssh_channel_poll_timeout(session->ti.libssh.channel, io_timeout, 0); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 484 | if (ret == SSH_ERROR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 485 | ERR(session, "SSH channel poll error (%s).", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 486 | session->status = NC_STATUS_INVALID; |
| 487 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 488 | return -1; |
| 489 | } else if (ret == SSH_EOF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 490 | ERR(session, "SSH channel unexpected EOF."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 491 | session->status = NC_STATUS_INVALID; |
| 492 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 493 | return -1; |
| 494 | } else if (ret > 0) { |
| 495 | /* fake it */ |
| 496 | ret = 1; |
| 497 | fds.revents = POLLIN; |
Michal Vasko | 5550cda | 2016-02-03 15:28:57 +0100 | [diff] [blame] | 498 | } else { /* ret == 0 */ |
| 499 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 500 | } |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 501 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 502 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 503 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 504 | case NC_TI_OPENSSL: |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 505 | ret = SSL_pending(session->ti.tls); |
| 506 | if (ret) { |
| 507 | /* some buffered TLS data available */ |
| 508 | ret = 1; |
| 509 | fds.revents = POLLIN; |
| 510 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 511 | } |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 512 | |
| 513 | fds.fd = SSL_get_fd(session->ti.tls); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 514 | #endif |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 515 | /* fallthrough */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 516 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 517 | case NC_TI_UNIX: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 518 | if (session->ti_type == NC_TI_FD) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 519 | fds.fd = session->ti.fd.in; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 520 | } else if (session->ti_type == NC_TI_UNIX) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 521 | fds.fd = session->ti.unixsock.sock; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 522 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 523 | |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 524 | fds.events = POLLIN; |
| 525 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 526 | |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 527 | sigfillset(&sigmask); |
| 528 | pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 529 | ret = poll(&fds, 1, io_timeout); |
Michal Vasko | b5a58fa | 2017-01-31 09:47:50 +0100 | [diff] [blame] | 530 | pthread_sigmask(SIG_SETMASK, &origmask, NULL); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 531 | |
| 532 | break; |
| 533 | |
| 534 | default: |
| 535 | ERRINT; |
| 536 | return -1; |
| 537 | } |
| 538 | |
| 539 | /* process the poll result, unified ret meaning for poll and ssh_channel poll */ |
| 540 | if (ret < 0) { |
| 541 | /* poll failed - something really bad happened, close the session */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 542 | ERR(session, "poll error (%s).", strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 543 | session->status = NC_STATUS_INVALID; |
| 544 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 545 | return -1; |
| 546 | } else { /* status > 0 */ |
| 547 | /* in case of standard (non-libssh) poll, there still can be an error */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 548 | if (fds.revents & POLLERR) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 549 | ERR(session, "Communication channel error."); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 550 | session->status = NC_STATUS_INVALID; |
| 551 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 552 | return -1; |
| 553 | } |
Robin Jarry | f732adc | 2020-05-15 11:18:38 +0200 | [diff] [blame] | 554 | /* Some poll() implementations may return POLLHUP|POLLIN when the other |
| 555 | * side has closed but there is data left to read in the buffer. */ |
| 556 | if ((fds.revents & POLLHUP) && !(fds.revents & POLLIN)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 557 | ERR(session, "Communication channel unexpectedly closed."); |
Robin Jarry | f732adc | 2020-05-15 11:18:38 +0200 | [diff] [blame] | 558 | session->status = NC_STATUS_INVALID; |
| 559 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 560 | return -1; |
| 561 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | return ret; |
| 565 | } |
| 566 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 567 | int |
| 568 | nc_read_msg_poll_io(struct nc_session *session, int io_timeout, struct ly_in **msg) |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 569 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 570 | int ret; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 571 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 572 | assert(msg); |
| 573 | *msg = NULL; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 574 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 575 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 576 | ERR(session, "Invalid session to read from."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 577 | return -1; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 580 | /* SESSION IO LOCK */ |
| 581 | ret = nc_session_io_lock(session, io_timeout, __func__); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 582 | if (ret < 1) { |
| 583 | return ret; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | ret = nc_read_poll(session, io_timeout); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 587 | if (ret < 1) { |
| 588 | /* timed out or error */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 589 | |
| 590 | /* SESSION IO UNLOCK */ |
| 591 | nc_session_io_unlock(session, __func__); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 592 | return ret; |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 595 | /* SESSION IO LOCK passed down */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 596 | return nc_read_msg_io(session, io_timeout, msg, 1); |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 597 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 598 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 599 | /* does not really log, only fatal errors */ |
| 600 | int |
| 601 | nc_session_is_connected(struct nc_session *session) |
| 602 | { |
| 603 | int ret; |
| 604 | struct pollfd fds; |
| 605 | |
| 606 | switch (session->ti_type) { |
| 607 | case NC_TI_FD: |
| 608 | fds.fd = session->ti.fd.in; |
| 609 | break; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 610 | case NC_TI_UNIX: |
| 611 | fds.fd = session->ti.unixsock.sock; |
| 612 | break; |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 613 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 614 | case NC_TI_LIBSSH: |
Michal Vasko | 840a8a6 | 2017-02-07 10:56:34 +0100 | [diff] [blame] | 615 | return ssh_is_connected(session->ti.libssh.session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 616 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 617 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 618 | case NC_TI_OPENSSL: |
| 619 | fds.fd = SSL_get_fd(session->ti.tls); |
| 620 | break; |
| 621 | #endif |
Michal Vasko | f945da5 | 2018-02-15 08:45:13 +0100 | [diff] [blame] | 622 | default: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
Michal Vasko | 840a8a6 | 2017-02-07 10:56:34 +0100 | [diff] [blame] | 626 | if (fds.fd == -1) { |
| 627 | return 0; |
| 628 | } |
| 629 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 630 | fds.events = POLLIN; |
Michal Vasko | 3e9d168 | 2017-02-24 09:50:15 +0100 | [diff] [blame] | 631 | fds.revents = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 632 | |
| 633 | errno = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 634 | while (((ret = poll(&fds, 1, 0)) == -1) && (errno == EINTR)) {} |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 635 | |
| 636 | if (ret == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 637 | ERR(session, "poll failed (%s).", strerror(errno)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 638 | return 0; |
| 639 | } else if ((ret > 0) && (fds.revents & (POLLHUP | POLLERR))) { |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | return 1; |
| 644 | } |
| 645 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 646 | #define WRITE_BUFSIZE (2 * BUFFERSIZE) |
| 647 | struct wclb_arg { |
| 648 | struct nc_session *session; |
| 649 | char buf[WRITE_BUFSIZE]; |
| 650 | size_t len; |
| 651 | }; |
| 652 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 653 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 654 | nc_write(struct nc_session *session, const void *buf, size_t count) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 655 | { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 656 | int c, fd, interrupted; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 657 | size_t written = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 658 | |
Michal Vasko | e2357e9 | 2016-10-05 14:20:47 +0200 | [diff] [blame] | 659 | #ifdef NC_ENABLED_TLS |
| 660 | unsigned long e; |
| 661 | #endif |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 662 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 663 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 664 | return -1; |
| 665 | } |
| 666 | |
| 667 | /* prevent SIGPIPE this way */ |
| 668 | if (!nc_session_is_connected(session)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 669 | ERR(session, "Communication socket unexpectedly closed."); |
Michal Vasko | 2a7d473 | 2016-01-15 09:24:46 +0100 | [diff] [blame] | 670 | session->status = NC_STATUS_INVALID; |
| 671 | session->term_reason = NC_SESSION_TERM_DROPPED; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 672 | return -1; |
| 673 | } |
| 674 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 675 | DBG(session, "Sending message:\n%.*s\n", count, buf); |
Michal Vasko | 160b791 | 2016-06-20 10:00:53 +0200 | [diff] [blame] | 676 | |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 677 | do { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 678 | interrupted = 0; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 679 | switch (session->ti_type) { |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 680 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 681 | case NC_TI_UNIX: |
| 682 | fd = session->ti_type == NC_TI_FD ? session->ti.fd.out : session->ti.unixsock.sock; |
| 683 | c = write(fd, (char *)(buf + written), count - written); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 684 | if ((c < 0) && (errno == EAGAIN)) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 685 | c = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 686 | } else if ((c < 0) && (errno == EINTR)) { |
Robin Jarry | 7de4b8e | 2019-10-14 21:46:00 +0200 | [diff] [blame] | 687 | c = 0; |
| 688 | interrupted = 1; |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 689 | } else if (c < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 690 | ERR(session, "socket error (%s).", strerror(errno)); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 691 | return -1; |
| 692 | } |
| 693 | break; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 694 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 695 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 696 | case NC_TI_LIBSSH: |
| 697 | if (ssh_channel_is_closed(session->ti.libssh.channel) || ssh_channel_is_eof(session->ti.libssh.channel)) { |
| 698 | if (ssh_channel_is_closed(session->ti.libssh.channel)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 699 | ERR(session, "SSH channel unexpectedly closed."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 700 | } else { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 701 | ERR(session, "SSH channel unexpected EOF."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 702 | } |
| 703 | session->status = NC_STATUS_INVALID; |
| 704 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 705 | return -1; |
Michal Vasko | 454e22b | 2016-01-21 15:34:08 +0100 | [diff] [blame] | 706 | } |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 707 | c = ssh_channel_write(session->ti.libssh.channel, (char *)(buf + written), count - written); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 708 | if ((c == SSH_ERROR) || (c == -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 709 | ERR(session, "SSH channel write failed."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 710 | return -1; |
| 711 | } |
| 712 | break; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 713 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 714 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 715 | case NC_TI_OPENSSL: |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 716 | c = SSL_write(session->ti.tls, (char *)(buf + written), count - written); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 717 | if (c < 1) { |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 718 | char *reasons; |
| 719 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 720 | switch ((e = SSL_get_error(session->ti.tls, c))) { |
| 721 | case SSL_ERROR_ZERO_RETURN: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 722 | ERR(session, "SSL connection was properly closed."); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 723 | return -1; |
| 724 | case SSL_ERROR_WANT_WRITE: |
Michal Vasko | 0abba6d | 2018-12-10 14:09:39 +0100 | [diff] [blame] | 725 | case SSL_ERROR_WANT_READ: |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 726 | c = 0; |
| 727 | break; |
| 728 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 729 | ERR(session, "SSL socket error (%s).", strerror(errno)); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 730 | return -1; |
| 731 | case SSL_ERROR_SSL: |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 732 | reasons = nc_ssl_error_get_reasons(); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 733 | ERR(session, "SSL error (%s).", reasons); |
Michal Vasko | 90a87d9 | 2018-12-10 15:53:44 +0100 | [diff] [blame] | 734 | free(reasons); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 735 | return -1; |
| 736 | default: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 737 | ERR(session, "Unknown SSL error occured (err code %d).", e); |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 738 | return -1; |
| 739 | } |
| 740 | } |
| 741 | break; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 742 | #endif |
Michal Vasko | 339eea8 | 2016-09-29 11:42:36 +0200 | [diff] [blame] | 743 | default: |
| 744 | ERRINT; |
| 745 | return -1; |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 748 | if ((c == 0) && !interrupted) { |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 749 | /* we must wait */ |
| 750 | usleep(NC_TIMEOUT_STEP); |
| 751 | } |
| 752 | |
| 753 | written += c; |
Michal Vasko | 81b33fb | 2016-09-26 14:57:36 +0200 | [diff] [blame] | 754 | } while (written < count); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 755 | |
Michal Vasko | 964e173 | 2016-09-23 13:39:33 +0200 | [diff] [blame] | 756 | return written; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 759 | static int |
| 760 | nc_write_starttag_and_msg(struct nc_session *session, const void *buf, size_t count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 761 | { |
Michal Vasko | fd2db9b | 2016-01-14 16:15:16 +0100 | [diff] [blame] | 762 | int ret = 0, c; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 763 | char chunksize[24]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 764 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 765 | // warning: ‘%zu’ directive writing between 4 and 20 bytes into a region of size 18 [-Wformat-overflow=] |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 766 | if (session->version == NC_VERSION_11) { |
| 767 | sprintf(chunksize, "\n#%zu\n", count); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 768 | ret = nc_write(session, chunksize, strlen(chunksize)); |
| 769 | if (ret == -1) { |
| 770 | return -1; |
| 771 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 772 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 773 | |
| 774 | c = nc_write(session, buf, count); |
| 775 | if (c == -1) { |
| 776 | return -1; |
| 777 | } |
| 778 | ret += c; |
| 779 | |
| 780 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 783 | static int |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 784 | nc_write_endtag(struct nc_session *session) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 785 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 786 | int ret; |
Michal Vasko | 38a7c6c | 2015-12-04 12:29:20 +0100 | [diff] [blame] | 787 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 788 | if (session->version == NC_VERSION_11) { |
| 789 | ret = nc_write(session, "\n##\n", 4); |
| 790 | } else { |
| 791 | ret = nc_write(session, "]]>]]>", 6); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 792 | } |
| 793 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 794 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 797 | static int |
| 798 | nc_write_clb_flush(struct wclb_arg *warg) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 799 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 800 | int ret = 0; |
| 801 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 802 | /* flush current buffer */ |
| 803 | if (warg->len) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 804 | ret = nc_write_starttag_and_msg(warg->session, warg->buf, warg->len); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 805 | warg->len = 0; |
| 806 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 807 | |
| 808 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | static ssize_t |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 812 | nc_write_clb(void *arg, const void *buf, size_t count, int xmlcontent) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 813 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 814 | int ret = 0, c; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 815 | size_t l; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 816 | struct wclb_arg *warg = (struct wclb_arg *)arg; |
| 817 | |
| 818 | if (!buf) { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 819 | c = nc_write_clb_flush(warg); |
| 820 | if (c == -1) { |
| 821 | return -1; |
| 822 | } |
| 823 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 824 | |
| 825 | /* endtag */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 826 | c = nc_write_endtag(warg->session); |
| 827 | if (c == -1) { |
| 828 | return -1; |
| 829 | } |
| 830 | ret += c; |
| 831 | |
| 832 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if (warg->len && (warg->len + count > WRITE_BUFSIZE)) { |
| 836 | /* dump current buffer */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 837 | c = nc_write_clb_flush(warg); |
| 838 | if (c == -1) { |
| 839 | return -1; |
| 840 | } |
| 841 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 842 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 843 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 844 | if (!xmlcontent && (count > WRITE_BUFSIZE)) { |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 845 | /* write directly */ |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 846 | c = nc_write_starttag_and_msg(warg->session, buf, count); |
| 847 | if (c == -1) { |
| 848 | return -1; |
| 849 | } |
| 850 | ret += c; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 851 | } else { |
| 852 | /* keep in buffer and write later */ |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 853 | if (xmlcontent) { |
| 854 | for (l = 0; l < count; l++) { |
| 855 | if (warg->len + 5 >= WRITE_BUFSIZE) { |
| 856 | /* buffer is full */ |
| 857 | c = nc_write_clb_flush(warg); |
| 858 | if (c == -1) { |
| 859 | return -1; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | switch (((char *)buf)[l]) { |
| 864 | case '&': |
| 865 | ret += 5; |
| 866 | memcpy(&warg->buf[warg->len], "&", 5); |
| 867 | warg->len += 5; |
| 868 | break; |
| 869 | case '<': |
| 870 | ret += 4; |
| 871 | memcpy(&warg->buf[warg->len], "<", 4); |
| 872 | warg->len += 4; |
| 873 | break; |
| 874 | case '>': |
| 875 | /* not needed, just for readability */ |
| 876 | ret += 4; |
| 877 | memcpy(&warg->buf[warg->len], ">", 4); |
| 878 | warg->len += 4; |
| 879 | break; |
| 880 | default: |
| 881 | ret++; |
| 882 | memcpy(&warg->buf[warg->len], &((char *)buf)[l], 1); |
| 883 | warg->len++; |
| 884 | } |
| 885 | } |
| 886 | } else { |
| 887 | memcpy(&warg->buf[warg->len], buf, count); |
| 888 | warg->len += count; /* is <= WRITE_BUFSIZE */ |
| 889 | ret += count; |
| 890 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 891 | } |
| 892 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 893 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 894 | } |
| 895 | |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 896 | static ssize_t |
| 897 | nc_write_xmlclb(void *arg, const void *buf, size_t count) |
| 898 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 899 | ssize_t r; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 900 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 901 | r = nc_write_clb(arg, buf, count, 0); |
| 902 | if (r == -1) { |
| 903 | return -1; |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 904 | } |
| 905 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 906 | /* always return what libyang expects, simply that all the characters were printed */ |
| 907 | return count; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 908 | } |
| 909 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 910 | /* return NC_MSG_ERROR can change session status, acquires IO lock as needed */ |
| 911 | NC_MSG_TYPE |
| 912 | nc_write_msg_io(struct nc_session *session, int io_timeout, int type, ...) |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 913 | { |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 914 | va_list ap; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 915 | int count, ret; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 916 | const char *attrs; |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 917 | struct lyd_node *op, *reply_envp, *node, *next; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 918 | struct lyd_node_opaq *rpc_envp; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 919 | struct nc_server_notif *notif; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 920 | struct nc_server_reply *reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 921 | char *buf; |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 922 | struct wclb_arg arg; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 923 | const char **capabilities; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 924 | uint32_t *sid = NULL, i, wd = 0; |
| 925 | LY_ERR lyrc; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 926 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 927 | assert(session); |
| 928 | |
| 929 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 930 | ERR(session, "Invalid session to write to."); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 931 | return NC_MSG_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 932 | } |
| 933 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 934 | arg.session = session; |
| 935 | arg.len = 0; |
| 936 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 937 | /* SESSION IO LOCK */ |
| 938 | ret = nc_session_io_lock(session, io_timeout, __func__); |
| 939 | if (ret < 0) { |
| 940 | return NC_MSG_ERROR; |
| 941 | } else if (!ret) { |
| 942 | return NC_MSG_WOULDBLOCK; |
| 943 | } |
| 944 | |
| 945 | va_start(ap, type); |
Radek Krejci | 127f895 | 2016-10-12 14:57:16 +0200 | [diff] [blame] | 946 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 947 | switch (type) { |
| 948 | case NC_MSG_RPC: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 949 | op = va_arg(ap, struct lyd_node *); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 950 | attrs = va_arg(ap, const char *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 951 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 952 | count = asprintf(&buf, "<rpc xmlns=\"%s\" message-id=\"%" PRIu64 "\"%s>", |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 953 | NC_NS_BASE, session->opts.client.msgid + 1, attrs ? attrs : ""); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 954 | if (count == -1) { |
| 955 | ERRMEM; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 956 | ret = NC_MSG_ERROR; |
| 957 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 958 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 959 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 960 | free(buf); |
Michal Vasko | e170860 | 2016-10-18 12:17:22 +0200 | [diff] [blame] | 961 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 962 | if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, op, LYD_XML, LYD_PRINT_SHRINK)) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 963 | ret = NC_MSG_ERROR; |
| 964 | goto cleanup; |
Michal Vasko | 5a91ce7 | 2017-10-19 11:30:02 +0200 | [diff] [blame] | 965 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 966 | nc_write_clb((void *)&arg, "</rpc>", 6, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 967 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 968 | session->opts.client.msgid++; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 969 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 970 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 971 | case NC_MSG_REPLY: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 972 | rpc_envp = va_arg(ap, struct lyd_node_opaq *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 973 | reply = va_arg(ap, struct nc_server_reply *); |
| 974 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 975 | if (!rpc_envp) { |
| 976 | /* can be NULL if replying with a malformed-message error */ |
| 977 | nc_write_clb((void *)&arg, "<rpc-reply xmlns=\"" NC_NS_BASE "\">", 18 + strlen(NC_NS_BASE) + 2, 0); |
| 978 | |
| 979 | assert(reply->type == NC_RPL_ERROR); |
| 980 | if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, ((struct nc_server_reply_error *)reply)->err, LYD_XML, |
| 981 | LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS)) { |
| 982 | ret = NC_MSG_ERROR; |
| 983 | goto cleanup; |
| 984 | } |
| 985 | |
| 986 | nc_write_clb((void *)&arg, "</rpc-reply>", 12, 0); |
| 987 | break; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 990 | /* build a rpc-reply opaque node that can be simply printed */ |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 991 | if (lyd_new_opaq2(NULL, session->ctx, "rpc-reply", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, |
| 992 | &reply_envp)) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 993 | ERRINT; |
| 994 | ret = NC_MSG_ERROR; |
| 995 | goto cleanup; |
Michal Vasko | e7e534f | 2016-01-15 09:51:09 +0100 | [diff] [blame] | 996 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 997 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 998 | switch (reply->type) { |
| 999 | case NC_RPL_OK: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1000 | if (lyd_new_opaq2(reply_envp, NULL, "ok", NULL, rpc_envp->name.prefix, rpc_envp->name.module_ns, NULL)) { |
| 1001 | lyd_free_tree(reply_envp); |
| 1002 | |
| 1003 | ERRINT; |
| 1004 | ret = NC_MSG_ERROR; |
| 1005 | goto cleanup; |
Michal Vasko | 08611b3 | 2016-12-05 13:30:37 +0100 | [diff] [blame] | 1006 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1007 | break; |
| 1008 | case NC_RPL_DATA: |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1009 | switch (((struct nc_server_reply_data *)reply)->wd) { |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1010 | case NC_WD_UNKNOWN: |
| 1011 | case NC_WD_EXPLICIT: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1012 | wd = LYD_PRINT_WD_EXPLICIT; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1013 | break; |
| 1014 | case NC_WD_TRIM: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1015 | wd = LYD_PRINT_WD_TRIM; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1016 | break; |
| 1017 | case NC_WD_ALL: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1018 | wd = LYD_PRINT_WD_ALL; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1019 | break; |
| 1020 | case NC_WD_ALL_TAG: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1021 | wd = LYD_PRINT_WD_ALL_TAG; |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 1022 | break; |
| 1023 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1024 | |
| 1025 | node = ((struct nc_server_reply_data *)reply)->data; |
| 1026 | assert(node->schema->nodetype & (LYS_RPC | LYS_ACTION)); |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1027 | LY_LIST_FOR_SAFE(lyd_child(node), next, node) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1028 | /* temporary */ |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1029 | lyd_insert_child(reply_envp, node); |
Michal Vasko | 5a91ce7 | 2017-10-19 11:30:02 +0200 | [diff] [blame] | 1030 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1031 | break; |
| 1032 | case NC_RPL_ERROR: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1033 | /* temporary */ |
| 1034 | lyd_insert_child(reply_envp, ((struct nc_server_reply_error *)reply)->err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1035 | break; |
| 1036 | default: |
| 1037 | ERRINT; |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1038 | nc_write_clb((void *)&arg, NULL, 0, 0); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1039 | ret = NC_MSG_ERROR; |
| 1040 | goto cleanup; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1041 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1042 | |
| 1043 | /* temporary */ |
| 1044 | ((struct lyd_node_opaq *)reply_envp)->attr = rpc_envp->attr; |
| 1045 | |
| 1046 | /* print */ |
| 1047 | lyrc = lyd_print_clb(nc_write_xmlclb, (void *)&arg, reply_envp, LYD_XML, LYD_PRINT_SHRINK | wd); |
| 1048 | ((struct lyd_node_opaq *)reply_envp)->attr = NULL; |
| 1049 | |
| 1050 | /* cleanup */ |
| 1051 | switch (reply->type) { |
| 1052 | case NC_RPL_OK: |
| 1053 | /* just free everything */ |
| 1054 | lyd_free_tree(reply_envp); |
| 1055 | break; |
| 1056 | case NC_RPL_DATA: |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1057 | LY_LIST_FOR_SAFE(lyd_child(reply_envp), next, node) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1058 | /* connect back to the reply structure */ |
Michal Vasko | ff7286b | 2021-07-09 13:13:11 +0200 | [diff] [blame] | 1059 | lyd_insert_child(((struct nc_server_reply_data *)reply)->data, node); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1060 | } |
| 1061 | lyd_free_tree(reply_envp); |
| 1062 | break; |
| 1063 | case NC_RPL_ERROR: |
| 1064 | /* unlink from the data reply */ |
| 1065 | lyd_unlink_tree(lyd_child(reply_envp)); |
| 1066 | lyd_free_tree(reply_envp); |
| 1067 | break; |
| 1068 | default: |
| 1069 | break; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1070 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1071 | |
| 1072 | if (lyrc) { |
| 1073 | ret = NC_MSG_ERROR; |
| 1074 | goto cleanup; |
Michal Vasko | 7f0b0ff | 2016-11-15 11:02:28 +0100 | [diff] [blame] | 1075 | } |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1076 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1077 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1078 | case NC_MSG_NOTIF: |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1079 | notif = va_arg(ap, struct nc_server_notif *); |
| 1080 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1081 | nc_write_clb((void *)&arg, "<notification xmlns=\""NC_NS_NOTIF "\">", 21 + 47 + 2, 0); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1082 | nc_write_clb((void *)&arg, "<eventTime>", 11, 0); |
| 1083 | nc_write_clb((void *)&arg, notif->eventtime, strlen(notif->eventtime), 0); |
| 1084 | nc_write_clb((void *)&arg, "</eventTime>", 12, 0); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1085 | if (lyd_print_clb(nc_write_xmlclb, (void *)&arg, notif->ntf, LYD_XML, LYD_PRINT_SHRINK)) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1086 | ret = NC_MSG_ERROR; |
| 1087 | goto cleanup; |
Michal Vasko | 5a91ce7 | 2017-10-19 11:30:02 +0200 | [diff] [blame] | 1088 | } |
mohitarora24 | 878b296 | 2016-11-09 18:45:33 -0500 | [diff] [blame] | 1089 | nc_write_clb((void *)&arg, "</notification>", 15, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1090 | break; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1091 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1092 | case NC_MSG_HELLO: |
| 1093 | if (session->version != NC_VERSION_10) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1094 | ret = NC_MSG_ERROR; |
| 1095 | goto cleanup; |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1096 | } |
| 1097 | capabilities = va_arg(ap, const char **); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1098 | sid = va_arg(ap, uint32_t *); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1099 | |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1100 | count = asprintf(&buf, "<hello xmlns=\"%s\"><capabilities>", NC_NS_BASE); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1101 | if (count == -1) { |
| 1102 | ERRMEM; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1103 | ret = NC_MSG_ERROR; |
| 1104 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1105 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1106 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1107 | free(buf); |
| 1108 | for (i = 0; capabilities[i]; i++) { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1109 | nc_write_clb((void *)&arg, "<capability>", 12, 0); |
| 1110 | nc_write_clb((void *)&arg, capabilities[i], strlen(capabilities[i]), 1); |
| 1111 | nc_write_clb((void *)&arg, "</capability>", 13, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1112 | } |
| 1113 | if (sid) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1114 | count = asprintf(&buf, "</capabilities><session-id>%u</session-id></hello>", *sid); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1115 | if (count == -1) { |
| 1116 | ERRMEM; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1117 | ret = NC_MSG_ERROR; |
| 1118 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1119 | } |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1120 | nc_write_clb((void *)&arg, buf, count, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1121 | free(buf); |
| 1122 | } else { |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1123 | nc_write_clb((void *)&arg, "</capabilities></hello>", 23, 0); |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1124 | } |
Radek Krejci | d116db4 | 2016-01-08 15:36:30 +0100 | [diff] [blame] | 1125 | break; |
Michal Vasko | ed46234 | 2016-01-12 12:33:48 +0100 | [diff] [blame] | 1126 | |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1127 | default: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1128 | ret = NC_MSG_ERROR; |
| 1129 | goto cleanup; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | /* flush message */ |
Radek Krejci | 047300e | 2016-03-08 16:46:58 +0100 | [diff] [blame] | 1133 | nc_write_clb((void *)&arg, NULL, 0, 0); |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1134 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1135 | if ((session->status != NC_STATUS_RUNNING) && (session->status != NC_STATUS_STARTING)) { |
| 1136 | /* error was already written */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1137 | ret = NC_MSG_ERROR; |
| 1138 | } else { |
| 1139 | /* specific message successfully sent */ |
| 1140 | ret = type; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1143 | cleanup: |
| 1144 | va_end(ap); |
| 1145 | nc_session_io_unlock(session, __func__); |
| 1146 | return ret; |
Radek Krejci | fe0b347 | 2015-10-12 13:43:42 +0200 | [diff] [blame] | 1147 | } |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1148 | |
| 1149 | void * |
| 1150 | nc_realloc(void *ptr, size_t size) |
| 1151 | { |
| 1152 | void *ret; |
| 1153 | |
| 1154 | ret = realloc(ptr, size); |
| 1155 | if (!ret) { |
| 1156 | free(ptr); |
| 1157 | } |
| 1158 | |
| 1159 | return ret; |
| 1160 | } |