Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 - general session functions |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2023 CESNET, z.s.p.o. |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 14 | */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 16 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 17 | #include <assert.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 20 | #include <libyang/libyang.h> |
roman | 3c5b75d | 2024-04-18 14:56:53 +0200 | [diff] [blame] | 21 | #include <netinet/in.h> |
| 22 | #include <netinet/tcp.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 23 | #include <pthread.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/types.h> |
Michal Vasko | 58f3155 | 2016-01-19 12:39:15 +0100 | [diff] [blame] | 29 | #include <time.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 31 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 32 | #include "compat.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 33 | #include "config.h" |
| 34 | #include "log_p.h" |
| 35 | #include "netconf.h" |
| 36 | #include "session_p.h" |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 37 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 38 | #ifdef NC_ENABLED_SSH_TLS |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 39 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 40 | #include <libssh/libssh.h> |
roman | 008cfe7 | 2024-04-05 12:36:18 +0200 | [diff] [blame] | 41 | #include "session_wrapper.h" |
Radek Krejci | 206fcd6 | 2015-10-07 15:42:48 +0200 | [diff] [blame] | 42 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 43 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 44 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 45 | /* in seconds */ |
| 46 | #define NC_CLIENT_HELLO_TIMEOUT 60 |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 47 | #define NC_SERVER_CH_HELLO_TIMEOUT 180 |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 48 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 49 | /* in milliseconds */ |
| 50 | #define NC_CLOSE_REPLY_TIMEOUT 200 |
| 51 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 52 | extern struct nc_server_opts server_opts; |
| 53 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 54 | void |
| 55 | nc_timeouttime_get(struct timespec *ts, uint32_t add_ms) |
Radek Krejci | 7ac1605 | 2016-07-15 11:48:18 +0200 | [diff] [blame] | 56 | { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 57 | if (clock_gettime(COMPAT_CLOCK_ID, ts) == -1) { |
| 58 | ERR(NULL, "clock_gettime() failed (%s).", strerror(errno)); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (!add_ms) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L)); |
| 67 | |
| 68 | ts->tv_sec += add_ms / 1000; |
| 69 | ts->tv_nsec += (add_ms % 1000) * 1000000L; |
| 70 | |
| 71 | if (ts->tv_nsec >= 1000000000L) { |
| 72 | ++ts->tv_sec; |
| 73 | ts->tv_nsec -= 1000000000L; |
| 74 | } else if (ts->tv_nsec < 0) { |
| 75 | --ts->tv_sec; |
| 76 | ts->tv_nsec += 1000000000L; |
| 77 | } |
| 78 | |
| 79 | assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L)); |
| 80 | } |
| 81 | |
| 82 | int32_t |
| 83 | nc_timeouttime_cur_diff(const struct timespec *ts) |
| 84 | { |
| 85 | struct timespec cur; |
| 86 | int64_t nsec_diff = 0; |
| 87 | |
| 88 | nc_timeouttime_get(&cur, 0); |
| 89 | |
| 90 | nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L; |
| 91 | nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec); |
| 92 | |
| 93 | return nsec_diff / 1000000L; |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | nc_realtime_get(struct timespec *ts) |
| 98 | { |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 99 | if (clock_gettime(CLOCK_REALTIME, ts)) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 100 | ERR(NULL, "clock_gettime() failed (%s).", strerror(errno)); |
| 101 | return; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 102 | } |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Michal Vasko | 63b92d6 | 2024-04-29 10:04:56 +0200 | [diff] [blame] | 105 | int |
| 106 | nc_poll(struct pollfd *pfd, uint16_t pfd_count, int timeout_ms) |
| 107 | { |
| 108 | int rc; |
| 109 | struct timespec start_ts; |
| 110 | |
| 111 | if (timeout_ms > 0) { |
| 112 | /* get current time */ |
| 113 | nc_timeouttime_get(&start_ts, 0); |
| 114 | } |
| 115 | |
| 116 | do { |
| 117 | /* poll */ |
| 118 | rc = poll(pfd, pfd_count, timeout_ms); |
| 119 | |
| 120 | if (timeout_ms > 0) { |
| 121 | /* adjust the timeout by subtracting the elapsed time (relevant in case of EINTR) */ |
| 122 | timeout_ms += nc_timeouttime_cur_diff(&start_ts); |
| 123 | if (timeout_ms < 0) { |
| 124 | /* manual timeout */ |
| 125 | rc = 0; |
| 126 | errno = 0; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } while ((rc == -1) && (errno == EINTR)); |
| 131 | |
| 132 | if (rc == -1) { |
| 133 | ERR(NULL, "Poll failed (%s).", strerror(errno)); |
| 134 | } |
| 135 | return rc; |
| 136 | } |
| 137 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 138 | #ifdef NC_ENABLED_SSH_TLS |
| 139 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 140 | const char * |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 141 | nc_privkey_format_to_str(NC_PRIVKEY_FORMAT format) |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 142 | { |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 143 | switch (format) { |
| 144 | case NC_PRIVKEY_FORMAT_RSA: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 145 | return " RSA "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 146 | case NC_PRIVKEY_FORMAT_EC: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 147 | return " EC "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 148 | case NC_PRIVKEY_FORMAT_X509: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 149 | return " "; |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 150 | case NC_PRIVKEY_FORMAT_OPENSSH: |
roman | d0fe595 | 2024-03-21 15:59:33 +0100 | [diff] [blame] | 151 | return " OPENSSH "; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 152 | default: |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 153 | return NULL; |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 154 | } |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 155 | } |
| 156 | |
roman | 1314591 | 2023-08-17 15:36:54 +0200 | [diff] [blame] | 157 | int |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 158 | nc_is_pk_subject_public_key_info(const char *b64) |
| 159 | { |
| 160 | int ret = 0; |
| 161 | long len; |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 162 | unsigned char *bin = NULL, *tmp; |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 163 | |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 164 | /* decode base64 */ |
| 165 | len = nc_base64_decode_wrap(b64, &bin); |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 166 | if (len == -1) { |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 167 | ret = -1; |
| 168 | goto cleanup; |
| 169 | } |
| 170 | |
| 171 | /* for deallocation later */ |
| 172 | tmp = bin; |
| 173 | |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 174 | /* try to parse the supposed SubjectPublicKeyInfo binary data */ |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 175 | if (nc_tls_is_der_subpubkey_wrap(tmp, len)) { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 176 | /* success, it's most likely SubjectPublicKeyInfo */ |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 177 | ret = 1; |
| 178 | } else { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 179 | /* it's most likely not SubjectPublicKeyInfo */ |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 180 | ret = 0; |
| 181 | } |
| 182 | |
| 183 | cleanup: |
roman | 51a1e19 | 2023-09-14 10:13:45 +0200 | [diff] [blame] | 184 | free(bin); |
| 185 | return ret; |
| 186 | } |
| 187 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 188 | #endif /* NC_ENABLED_SSH_TLS */ |
| 189 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 190 | int |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 191 | nc_sock_configure_ka(int sock, const struct nc_keepalives *ka) |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 192 | { |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 193 | int opt; |
| 194 | |
| 195 | opt = ka->enabled; |
| 196 | if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 197 | ERR(NULL, "Failed to set SO_KEEPALIVE (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 198 | return -1; |
| 199 | } |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 200 | |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 201 | if (!ka->enabled) { |
| 202 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 203 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 204 | |
| 205 | #ifdef TCP_KEEPIDLE |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 206 | opt = ka->idle_time; |
| 207 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 208 | ERR(NULL, "Failed to set TCP_KEEPIDLE (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 209 | return -1; |
| 210 | } |
| 211 | #endif |
| 212 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 213 | #ifdef TCP_KEEPCNT |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 214 | opt = ka->max_probes; |
| 215 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 216 | ERR(NULL, "Failed to set TCP_KEEPCNT (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | #endif |
| 220 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 221 | #ifdef TCP_KEEPINTVL |
Michal Vasko | c4cdc9e | 2024-05-03 12:03:07 +0200 | [diff] [blame] | 222 | opt = ka->probe_interval; |
| 223 | if (setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof opt) == -1) { |
roman | 56acd55 | 2024-04-18 16:00:37 +0200 | [diff] [blame] | 224 | ERR(NULL, "Failed to set TCP_KEEPINTVL (%s).", strerror(errno)); |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 232 | struct nc_session * |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 233 | nc_new_session(NC_SIDE side, int shared_ti) |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 234 | { |
| 235 | struct nc_session *sess; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 236 | struct timespec ts_cur; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 237 | |
| 238 | sess = calloc(1, sizeof *sess); |
| 239 | if (!sess) { |
| 240 | return NULL; |
| 241 | } |
| 242 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 243 | sess->side = side; |
| 244 | |
| 245 | if (side == NC_SERVER) { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 246 | pthread_mutex_init(&sess->opts.server.ntf_status_lock, NULL); |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 247 | pthread_mutex_init(&sess->opts.server.rpc_lock, NULL); |
| 248 | pthread_cond_init(&sess->opts.server.rpc_cond, NULL); |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 249 | |
| 250 | pthread_mutex_init(&sess->opts.server.ch_lock, NULL); |
| 251 | pthread_cond_init(&sess->opts.server.ch_cond, NULL); |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 252 | |
| 253 | /* initialize last_rpc for idle_timeout */ |
| 254 | nc_timeouttime_get(&ts_cur, 0); |
| 255 | sess->opts.server.last_rpc = ts_cur.tv_sec; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 256 | } else { |
| 257 | pthread_mutex_init(&sess->opts.client.msgs_lock, NULL); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (!shared_ti) { |
| 261 | sess->io_lock = malloc(sizeof *sess->io_lock); |
| 262 | if (!sess->io_lock) { |
| 263 | goto error; |
| 264 | } |
| 265 | pthread_mutex_init(sess->io_lock, NULL); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | return sess; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 269 | |
| 270 | error: |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 271 | free(sess); |
| 272 | return NULL; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 275 | /* |
| 276 | * @return 1 - success |
| 277 | * 0 - timeout |
| 278 | * -1 - error |
| 279 | */ |
| 280 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 281 | nc_session_rpc_lock(struct nc_session *session, int timeout, const char *func) |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 282 | { |
| 283 | int ret; |
Michal Vasko | 62be1ce | 2016-03-03 13:24:52 +0100 | [diff] [blame] | 284 | struct timespec ts_timeout; |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 285 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 286 | if (session->side != NC_SERVER) { |
| 287 | ERRINT; |
| 288 | return -1; |
| 289 | } |
| 290 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 291 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 292 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 293 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 294 | /* LOCK */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 295 | ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 296 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 297 | while (session->opts.server.rpc_inuse) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 298 | ret = pthread_cond_clockwait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock, |
| 299 | COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 300 | if (ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 301 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | } |
| 305 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 306 | } else if (!timeout) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 307 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 308 | ret = pthread_mutex_trylock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 309 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 310 | if (session->opts.server.rpc_inuse) { |
| 311 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 312 | return 0; |
| 313 | } |
Michal vasko | 2f8e4b5 | 2016-10-05 13:04:11 +0200 | [diff] [blame] | 314 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 315 | } else { /* timeout == -1 */ |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 316 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 317 | ret = pthread_mutex_lock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 318 | if (!ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 319 | while (session->opts.server.rpc_inuse) { |
| 320 | ret = pthread_cond_wait(&session->opts.server.rpc_cond, &session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 321 | if (ret) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 322 | pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 329 | if (ret) { |
| 330 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 331 | /* timeout */ |
| 332 | return 0; |
| 333 | } |
| 334 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 335 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 336 | ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret)); |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | /* ok */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 341 | assert(session->opts.server.rpc_inuse == 0); |
| 342 | session->opts.server.rpc_inuse = 1; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 343 | |
| 344 | /* UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 345 | ret = pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 346 | if (ret) { |
| 347 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 348 | ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 356 | nc_session_rpc_unlock(struct nc_session *session, int timeout, const char *func) |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 357 | { |
| 358 | int ret; |
| 359 | struct timespec ts_timeout; |
| 360 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 361 | if (session->side != NC_SERVER) { |
| 362 | ERRINT; |
| 363 | return -1; |
| 364 | } |
| 365 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 366 | assert(session->opts.server.rpc_inuse); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 367 | |
| 368 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 369 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 370 | |
| 371 | /* LOCK */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 372 | ret = pthread_mutex_clocklock(&session->opts.server.rpc_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 373 | } else if (!timeout) { |
| 374 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 375 | ret = pthread_mutex_trylock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 376 | } else { /* timeout == -1 */ |
| 377 | /* LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 378 | ret = pthread_mutex_lock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | if (ret && (ret != EBUSY) && (ret != ETIMEDOUT)) { |
| 382 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 383 | ERR(session, "%s: failed to RPC lock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 384 | return -1; |
| 385 | } else if (ret) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 386 | WRN(session, "%s: session RPC lock timeout, should not happen.", func); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 389 | session->opts.server.rpc_inuse = 0; |
| 390 | pthread_cond_signal(&session->opts.server.rpc_cond); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 391 | |
| 392 | if (!ret) { |
| 393 | /* UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 394 | ret = pthread_mutex_unlock(&session->opts.server.rpc_lock); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 395 | if (ret) { |
| 396 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 397 | ERR(session, "%s: failed to RPC unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 398 | return -1; |
| 399 | } |
| 400 | } |
| 401 | |
Michal Vasko | 96164bf | 2016-01-21 15:41:58 +0100 | [diff] [blame] | 402 | return 1; |
| 403 | } |
| 404 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 405 | int |
| 406 | nc_session_io_lock(struct nc_session *session, int timeout, const char *func) |
| 407 | { |
| 408 | int ret; |
| 409 | struct timespec ts_timeout; |
| 410 | |
| 411 | if (timeout > 0) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 412 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 413 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 414 | ret = pthread_mutex_clocklock(session->io_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 415 | } else if (!timeout) { |
| 416 | ret = pthread_mutex_trylock(session->io_lock); |
| 417 | } else { /* timeout == -1 */ |
Robin Jarry | 54ea296 | 2018-10-10 10:33:40 +0200 | [diff] [blame] | 418 | ret = pthread_mutex_lock(session->io_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if (ret) { |
| 422 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 423 | /* timeout */ |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 428 | ERR(session, "%s: failed to IO lock a session (%s).", func, strerror(ret)); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | return 1; |
| 433 | } |
| 434 | |
| 435 | int |
| 436 | nc_session_io_unlock(struct nc_session *session, const char *func) |
| 437 | { |
| 438 | int ret; |
| 439 | |
| 440 | ret = pthread_mutex_unlock(session->io_lock); |
| 441 | if (ret) { |
| 442 | /* error */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 443 | ERR(session, "%s: failed to IO unlock a session (%s).", func, strerror(ret)); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | return 1; |
| 448 | } |
| 449 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 450 | int |
| 451 | nc_session_client_msgs_lock(struct nc_session *session, int *timeout, const char *func) |
| 452 | { |
| 453 | int ret; |
| 454 | int32_t diff_msec; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 455 | struct timespec ts_timeout, ts_start; |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 456 | |
| 457 | assert(session->side == NC_CLIENT); |
| 458 | |
| 459 | if (*timeout > 0) { |
| 460 | /* get current time */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 461 | nc_timeouttime_get(&ts_start, 0); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 462 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 463 | nc_timeouttime_get(&ts_timeout, *timeout); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 464 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 465 | ret = pthread_mutex_clocklock(&session->opts.client.msgs_lock, COMPAT_CLOCK_ID, &ts_timeout); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 466 | if (!ret) { |
| 467 | /* update timeout based on what was elapsed */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 468 | diff_msec = nc_timeouttime_cur_diff(&ts_start); |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 469 | *timeout -= diff_msec; |
| 470 | } |
| 471 | } else if (!*timeout) { |
| 472 | ret = pthread_mutex_trylock(&session->opts.client.msgs_lock); |
| 473 | } else { /* timeout == -1 */ |
| 474 | ret = pthread_mutex_lock(&session->opts.client.msgs_lock); |
| 475 | } |
| 476 | |
| 477 | if (ret) { |
| 478 | if ((ret == EBUSY) || (ret == ETIMEDOUT)) { |
| 479 | /* timeout */ |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* error */ |
| 484 | ERR(session, "%s: failed to MSGS lock a session (%s).", func, strerror(ret)); |
| 485 | return -1; |
| 486 | } |
| 487 | |
| 488 | return 1; |
| 489 | } |
| 490 | |
| 491 | int |
| 492 | nc_session_client_msgs_unlock(struct nc_session *session, const char *func) |
| 493 | { |
| 494 | int ret; |
| 495 | |
| 496 | assert(session->side == NC_CLIENT); |
| 497 | |
| 498 | ret = pthread_mutex_unlock(&session->opts.client.msgs_lock); |
| 499 | if (ret) { |
| 500 | /* error */ |
| 501 | ERR(session, "%s: failed to MSGS unlock a session (%s).", func, strerror(ret)); |
| 502 | return -1; |
| 503 | } |
| 504 | |
| 505 | return 1; |
| 506 | } |
| 507 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 508 | API NC_STATUS |
| 509 | nc_session_get_status(const struct nc_session *session) |
| 510 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 511 | NC_CHECK_ARG_RET(session, session, NC_STATUS_ERR); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 512 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 513 | return session->status; |
| 514 | } |
| 515 | |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 516 | API NC_SESSION_TERM_REASON |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 517 | nc_session_get_term_reason(const struct nc_session *session) |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 518 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 519 | NC_CHECK_ARG_RET(session, session, NC_SESSION_TERM_ERR); |
Radek Krejci | 6e36bfb | 2016-12-01 21:40:16 +0100 | [diff] [blame] | 520 | |
| 521 | return session->term_reason; |
| 522 | } |
| 523 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 524 | API uint32_t |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 525 | nc_session_get_killed_by(const struct nc_session *session) |
| 526 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 527 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 528 | |
| 529 | return session->killed_by; |
| 530 | } |
| 531 | |
| 532 | API uint32_t |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 533 | nc_session_get_id(const struct nc_session *session) |
| 534 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 535 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 536 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 537 | return session->id; |
| 538 | } |
| 539 | |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 540 | API int |
| 541 | nc_session_get_version(const struct nc_session *session) |
| 542 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 543 | NC_CHECK_ARG_RET(session, session, -1); |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 544 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 545 | return session->version == NC_VERSION_10 ? 0 : 1; |
Michal Vasko | 174fe8e | 2016-02-17 15:38:09 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 548 | API NC_TRANSPORT_IMPL |
| 549 | nc_session_get_ti(const struct nc_session *session) |
| 550 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 551 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 552 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 553 | return session->ti_type; |
| 554 | } |
| 555 | |
| 556 | API const char * |
| 557 | nc_session_get_username(const struct nc_session *session) |
| 558 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 559 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 560 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 561 | return session->username; |
| 562 | } |
| 563 | |
| 564 | API const char * |
| 565 | nc_session_get_host(const struct nc_session *session) |
| 566 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 567 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 568 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 569 | return session->host; |
| 570 | } |
| 571 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 572 | API const char * |
| 573 | nc_session_get_path(const struct nc_session *session) |
| 574 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 575 | NC_CHECK_ARG_RET(session, session, NULL); |
| 576 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 577 | if (session->ti_type != NC_TI_UNIX) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 578 | return NULL; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 579 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 580 | |
| 581 | return session->path; |
| 582 | } |
| 583 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 584 | API uint16_t |
| 585 | nc_session_get_port(const struct nc_session *session) |
| 586 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 587 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 588 | |
Michal Vasko | 8dadf78 | 2016-01-15 10:29:36 +0100 | [diff] [blame] | 589 | return session->port; |
| 590 | } |
| 591 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 592 | API const struct ly_ctx * |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 593 | nc_session_get_ctx(const struct nc_session *session) |
| 594 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 595 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 596 | |
| 597 | return session->ctx; |
| 598 | } |
| 599 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 600 | API void |
| 601 | nc_session_set_data(struct nc_session *session, void *data) |
| 602 | { |
| 603 | if (!session) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 604 | ERRARG(NULL, "session"); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | |
| 608 | session->data = data; |
| 609 | } |
| 610 | |
| 611 | API void * |
| 612 | nc_session_get_data(const struct nc_session *session) |
| 613 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 614 | NC_CHECK_ARG_RET(session, session, NULL); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 615 | |
| 616 | return session->data; |
| 617 | } |
| 618 | |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 619 | API int |
| 620 | nc_session_is_callhome(const struct nc_session *session) |
| 621 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 622 | NC_CHECK_ARG_RET(session, session, 0); |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 623 | |
| 624 | if (session->flags & NC_SESSION_CALLHOME) { |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |
| 630 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 631 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 632 | nc_send_msg_io(struct nc_session *session, int io_timeout, struct lyd_node *op) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 633 | { |
Michal Vasko | 7e1f5fb | 2021-11-10 10:14:45 +0100 | [diff] [blame] | 634 | if (session->ctx != LYD_CTX(op)) { |
| 635 | ERR(session, "RPC \"%s\" was created in different context than that of the session.", LYD_NAME(op)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 636 | return NC_MSG_ERROR; |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 637 | } |
| 638 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 639 | return nc_write_msg_io(session, io_timeout, NC_MSG_RPC, op, NULL); |
Michal Vasko | 7df39ec | 2015-12-09 15:26:24 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 642 | /** |
| 643 | * @brief Send \<close-session\> and read the reply on a session. |
| 644 | * |
| 645 | * @param[in] session Closing NETCONF session. |
| 646 | */ |
| 647 | static void |
| 648 | nc_session_free_close_session(struct nc_session *session) |
| 649 | { |
| 650 | struct ly_in *msg; |
| 651 | struct lyd_node *close_rpc, *envp; |
| 652 | const struct lys_module *ietfnc; |
| 653 | |
| 654 | ietfnc = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf"); |
| 655 | if (!ietfnc) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 656 | WRN(session, "Missing ietf-netconf module in context, unable to send <close-session>."); |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 657 | return; |
| 658 | } |
| 659 | if (lyd_new_inner(NULL, ietfnc, "close-session", 0, &close_rpc)) { |
| 660 | WRN(session, "Failed to create <close-session> RPC."); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | /* send the RPC */ |
| 665 | nc_send_msg_io(session, NC_SESSION_FREE_LOCK_TIMEOUT, close_rpc); |
| 666 | |
| 667 | read_msg: |
| 668 | switch (nc_read_msg_poll_io(session, NC_CLOSE_REPLY_TIMEOUT, &msg)) { |
| 669 | case 1: |
| 670 | if (!strncmp(ly_in_memory(msg, NULL), "<notification", 13)) { |
| 671 | /* ignore */ |
| 672 | ly_in_free(msg, 1); |
| 673 | goto read_msg; |
| 674 | } |
| 675 | if (lyd_parse_op(session->ctx, close_rpc, msg, LYD_XML, LYD_TYPE_REPLY_NETCONF, &envp, NULL)) { |
| 676 | WRN(session, "Failed to parse <close-session> reply."); |
| 677 | } else if (!lyd_child(envp) || strcmp(LYD_NAME(lyd_child(envp)), "ok")) { |
| 678 | WRN(session, "Reply to <close-session> was not <ok> as expected."); |
| 679 | } |
| 680 | lyd_free_tree(envp); |
| 681 | ly_in_free(msg, 1); |
| 682 | break; |
| 683 | case 0: |
| 684 | WRN(session, "Timeout for receiving a reply to <close-session> elapsed."); |
| 685 | break; |
| 686 | case -1: |
| 687 | ERR(session, "Failed to receive a reply to <close-session>."); |
| 688 | break; |
| 689 | default: |
| 690 | /* cannot happen */ |
| 691 | break; |
| 692 | } |
| 693 | lyd_free_tree(close_rpc); |
| 694 | } |
| 695 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 696 | /** |
| 697 | * @brief Free transport implementation members of a session. |
| 698 | * |
| 699 | * @param[in] session Session to free. |
| 700 | * @param[out] multisession Whether there are other NC sessions on the same SSH sessions. |
| 701 | */ |
| 702 | static void |
| 703 | nc_session_free_transport(struct nc_session *session, int *multisession) |
| 704 | { |
| 705 | int connected; /* flag to indicate whether the transport socket is still connected */ |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 706 | int sock = -1; |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 707 | struct nc_session *siter; |
| 708 | |
| 709 | *multisession = 0; |
| 710 | connected = nc_session_is_connected(session); |
| 711 | |
| 712 | /* transport implementation cleanup */ |
| 713 | switch (session->ti_type) { |
| 714 | case NC_TI_FD: |
| 715 | /* nothing needed - file descriptors were provided by caller, |
| 716 | * so it is up to the caller to close them correctly |
| 717 | * TODO use callbacks |
| 718 | */ |
| 719 | /* just to avoid compiler warning */ |
| 720 | (void)connected; |
| 721 | (void)siter; |
| 722 | break; |
| 723 | |
| 724 | case NC_TI_UNIX: |
| 725 | sock = session->ti.unixsock.sock; |
| 726 | (void)connected; |
| 727 | (void)siter; |
| 728 | break; |
| 729 | |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 730 | #ifdef NC_ENABLED_SSH_TLS |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 731 | case NC_TI_SSH: { |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 732 | int r; |
| 733 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 734 | if (connected) { |
Michal Vasko | 6473440 | 2022-09-09 11:22:00 +0200 | [diff] [blame] | 735 | ssh_channel_send_eof(session->ti.libssh.channel); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 736 | ssh_channel_free(session->ti.libssh.channel); |
| 737 | } |
| 738 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 739 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 740 | * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session. |
| 741 | */ |
| 742 | /* SESSION IO LOCK */ |
| 743 | r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
| 744 | |
| 745 | if (session->ti.libssh.next) { |
| 746 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 747 | if (siter->status != NC_STATUS_STARTING) { |
| 748 | *multisession = 1; |
| 749 | break; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if (!*multisession) { |
| 755 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 756 | if (session->ti.libssh.next) { |
| 757 | do { |
| 758 | siter = session->ti.libssh.next; |
| 759 | session->ti.libssh.next = siter->ti.libssh.next; |
| 760 | |
| 761 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
| 762 | free(siter->username); |
| 763 | free(siter->host); |
| 764 | if (!(siter->flags & NC_SESSION_SHAREDCTX)) { |
| 765 | ly_ctx_destroy((struct ly_ctx *)siter->ctx); |
| 766 | } |
| 767 | |
| 768 | free(siter); |
| 769 | } while (session->ti.libssh.next != session); |
| 770 | } |
| 771 | /* remember sock so we can close it */ |
| 772 | sock = ssh_get_fd(session->ti.libssh.session); |
| 773 | if (connected) { |
Michal Vasko | 70e9062 | 2023-12-07 08:43:14 +0100 | [diff] [blame] | 774 | /* does not close sock */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 775 | ssh_disconnect(session->ti.libssh.session); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 776 | } |
| 777 | ssh_free(session->ti.libssh.session); |
| 778 | } else { |
| 779 | /* remove the session from the list */ |
| 780 | for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {} |
| 781 | if (session->ti.libssh.next == siter) { |
| 782 | /* there will be only one session */ |
| 783 | siter->ti.libssh.next = NULL; |
| 784 | } else { |
| 785 | /* there are still multiple sessions, keep the ring list */ |
| 786 | siter->ti.libssh.next = session->ti.libssh.next; |
| 787 | } |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* SESSION IO UNLOCK */ |
| 791 | if (r == 1) { |
| 792 | nc_session_io_unlock(session, __func__); |
| 793 | } |
| 794 | break; |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 795 | } |
roman | 506354a | 2024-04-11 09:37:22 +0200 | [diff] [blame] | 796 | case NC_TI_TLS: |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 797 | sock = nc_tls_get_fd_wrap(session); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 798 | |
| 799 | if (connected) { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 800 | /* notify the peer that we're shutting down */ |
| 801 | nc_tls_close_notify_wrap(session->ti.tls.session); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 802 | } |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 803 | |
| 804 | nc_tls_ctx_destroy_wrap(&session->ti.tls.ctx); |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 805 | memset(&session->ti.tls.ctx, 0, sizeof session->ti.tls.ctx); |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 806 | nc_tls_session_destroy_wrap(session->ti.tls.session); |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 807 | session->ti.tls.session = NULL; |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 808 | nc_tls_config_destroy_wrap(session->ti.tls.config); |
roman | c95a14f | 2024-04-23 15:13:00 +0200 | [diff] [blame] | 809 | session->ti.tls.config = NULL; |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 810 | |
| 811 | if (session->side == NC_SERVER) { |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 812 | nc_tls_cert_destroy_wrap(session->opts.server.client_cert); |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 813 | } |
roman | 9ba26aa | 2024-04-05 12:31:54 +0200 | [diff] [blame] | 814 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 815 | break; |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 816 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 817 | case NC_TI_NONE: |
| 818 | break; |
| 819 | } |
| 820 | |
| 821 | /* close socket separately */ |
| 822 | if (sock > -1) { |
| 823 | close(sock); |
| 824 | } |
| 825 | } |
| 826 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 827 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 828 | nc_session_free(struct nc_session *session, void (*data_free)(void *)) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 829 | { |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 830 | int r, i, rpc_locked = 0, msgs_locked = 0, timeout; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 831 | int multisession = 0; /* flag for more NETCONF sessions on a single SSH session */ |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 832 | struct nc_msg_cont *contiter; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 833 | struct ly_in *msg; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 834 | struct timespec ts; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 835 | void *p; |
| 836 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 837 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 838 | return; |
| 839 | } |
| 840 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 841 | /* stop notification threads if any */ |
| 842 | if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_running)) { |
| 843 | /* let the threads know they should quit */ |
| 844 | ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread_running, 0); |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 845 | |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 846 | /* wait for them */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 847 | nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | a8ec54b | 2022-10-20 09:59:07 +0200 | [diff] [blame] | 848 | while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread_count)) { |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 849 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 850 | if (nc_timeouttime_cur_diff(&ts) < 1) { |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 851 | ERR(session, "Waiting for notification thread exit failed (timed out)."); |
| 852 | break; |
| 853 | } |
| 854 | } |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 857 | if (session->side == NC_SERVER) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 858 | r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 859 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 860 | return; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 861 | } else if (r) { |
| 862 | rpc_locked = 1; |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 863 | } else { |
| 864 | /* else failed to lock it, too bad */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 865 | ERR(session, "Freeing a session while an RPC is being processed."); |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 866 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 867 | } |
| 868 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 869 | if (session->side == NC_CLIENT) { |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 870 | timeout = NC_SESSION_FREE_LOCK_TIMEOUT; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 871 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 872 | /* MSGS LOCK */ |
| 873 | r = nc_session_client_msgs_lock(session, &timeout, __func__); |
| 874 | if (r == -1) { |
| 875 | return; |
| 876 | } else if (r) { |
| 877 | msgs_locked = 1; |
| 878 | } else { |
| 879 | /* else failed to lock it, too bad */ |
| 880 | ERR(session, "Freeing a session while messages are being received."); |
| 881 | } |
| 882 | |
| 883 | /* cleanup message queue */ |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 884 | for (contiter = session->opts.client.msgs; contiter; ) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 885 | ly_in_free(contiter->msg, 1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 886 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 887 | p = contiter; |
| 888 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 889 | free(p); |
| 890 | } |
| 891 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 892 | if (msgs_locked) { |
| 893 | /* MSGS UNLOCK */ |
| 894 | nc_session_client_msgs_unlock(session, __func__); |
| 895 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 896 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 897 | if (session->status == NC_STATUS_RUNNING) { |
Michal Vasko | 9c6d38c | 2021-09-03 13:02:53 +0200 | [diff] [blame] | 898 | /* receive any leftover messages */ |
| 899 | while (nc_read_msg_poll_io(session, 0, &msg) == 1) { |
| 900 | ly_in_free(msg, 1); |
| 901 | } |
| 902 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 903 | /* send closing info to the other side */ |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 904 | nc_session_free_close_session(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* list of server's capabilities */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 908 | if (session->opts.client.cpblts) { |
| 909 | for (i = 0; session->opts.client.cpblts[i]; i++) { |
Michal Vasko | 96fc4bb | 2017-05-23 14:58:34 +0200 | [diff] [blame] | 910 | free(session->opts.client.cpblts[i]); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 911 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 912 | free(session->opts.client.cpblts); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 913 | } |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 914 | |
| 915 | /* LY ext data */ |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 916 | #ifdef NC_ENABLED_SSH_TLS |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 917 | struct nc_session *siter; |
| 918 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 919 | if ((session->flags & NC_SESSION_SHAREDCTX) && session->ti.libssh.next) { |
| 920 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 921 | if (siter->status != NC_STATUS_STARTING) { |
| 922 | /* move LY ext data to this session */ |
| 923 | assert(!siter->opts.client.ext_data); |
| 924 | siter->opts.client.ext_data = session->opts.client.ext_data; |
| 925 | session->opts.client.ext_data = NULL; |
| 926 | break; |
| 927 | } |
| 928 | } |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 929 | } else |
roman | 2eab474 | 2023-06-06 10:00:26 +0200 | [diff] [blame] | 930 | #endif /* NC_ENABLED_SSH_TLS */ |
Michal Vasko | e44f270 | 2022-12-12 07:58:06 +0100 | [diff] [blame] | 931 | { |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 932 | lyd_free_siblings(session->opts.client.ext_data); |
| 933 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 934 | } |
| 935 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 936 | if (session->data && data_free) { |
| 937 | data_free(session->data); |
| 938 | } |
| 939 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 940 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 941 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 942 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 945 | /* mark session for closing */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 946 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 947 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 948 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 949 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 950 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 951 | nc_timeouttime_get(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 952 | |
| 953 | /* wait for CH thread to actually wake up and terminate */ |
| 954 | r = 0; |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 955 | while (!r && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 956 | r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 957 | } |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 958 | if (r) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 959 | ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r)); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 960 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 963 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 964 | /* CH UNLOCK */ |
| 965 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
| 966 | } |
| 967 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 968 | /* transport implementation cleanup */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 969 | nc_session_free_transport(session, &multisession); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 970 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame] | 971 | /* final cleanup */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 972 | free(session->username); |
| 973 | free(session->host); |
| 974 | free(session->path); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 975 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 976 | if (session->side == NC_SERVER) { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 977 | pthread_mutex_destroy(&session->opts.server.ntf_status_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 978 | if (rpc_locked) { |
| 979 | nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 980 | } |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 981 | pthread_mutex_destroy(&session->opts.server.rpc_lock); |
| 982 | pthread_cond_destroy(&session->opts.server.rpc_cond); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | if (session->io_lock && !multisession) { |
| 986 | pthread_mutex_destroy(session->io_lock); |
| 987 | free(session->io_lock); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 991 | ly_ctx_destroy((struct ly_ctx *)session->ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 994 | if (session->side == NC_SERVER) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 995 | /* free CH synchronization structures */ |
| 996 | pthread_cond_destroy(&session->opts.server.ch_cond); |
| 997 | pthread_mutex_destroy(&session->opts.server.ch_lock); |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 998 | } else { |
| 999 | pthread_mutex_destroy(&session->opts.client.msgs_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1002 | free(session); |
| 1003 | } |
| 1004 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1005 | static void |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1006 | add_cpblt(const char *capab, char ***cpblts, int *size, int *count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1007 | { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1008 | size_t len; |
| 1009 | int i; |
| 1010 | char *p; |
| 1011 | |
| 1012 | if (capab) { |
| 1013 | /* check if already present */ |
| 1014 | p = strchr(capab, '?'); |
| 1015 | if (p) { |
| 1016 | len = p - capab; |
| 1017 | } else { |
| 1018 | len = strlen(capab); |
| 1019 | } |
| 1020 | for (i = 0; i < *count; i++) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1021 | if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1022 | /* already present, do not duplicate it */ |
| 1023 | return; |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /* add another capability */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1029 | if (*count == *size) { |
| 1030 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1031 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 1032 | if (!(*cpblts)) { |
| 1033 | ERRMEM; |
| 1034 | return; |
| 1035 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1038 | (*cpblts)[*count] = capab ? strdup(capab) : NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1039 | ++(*count); |
| 1040 | } |
| 1041 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1042 | API char ** |
| 1043 | nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1044 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1045 | char **cpblts; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1046 | const struct lys_module *mod; |
| 1047 | struct lysp_feature *feat; |
| 1048 | int size = 10, count, features_count = 0, dev_count = 0, str_len, len; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1049 | uint32_t i, u; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1050 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1051 | char *yl_content_id; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1052 | uint32_t wd_also_supported; |
| 1053 | uint32_t wd_basic_mode; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1054 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1055 | #define NC_CPBLT_BUF_LEN 4096 |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1056 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1057 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 1058 | NC_CHECK_ARG_RET(NULL, ctx, NULL); |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 1059 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1060 | cpblts = malloc(size * sizeof *cpblts); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1061 | NC_CHECK_ERRMEM_GOTO(!cpblts, , error); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1062 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1063 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1064 | count = 2; |
| 1065 | |
| 1066 | /* capabilities */ |
| 1067 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1068 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1069 | if (mod) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1070 | if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1071 | add_cpblt("urn:ietf:params:netconf:capability:writable-running:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1072 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1073 | if (lys_feature_value(mod, "candidate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1074 | add_cpblt("urn:ietf:params:netconf:capability:candidate:1.0", &cpblts, &size, &count); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1075 | if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1076 | add_cpblt("urn:ietf:params:netconf:capability:confirmed-commit:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1077 | } |
| 1078 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1079 | if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1080 | add_cpblt("urn:ietf:params:netconf:capability:rollback-on-error:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1081 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1082 | if (lys_feature_value(mod, "validate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1083 | add_cpblt("urn:ietf:params:netconf:capability:validate:1.1", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1084 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1085 | if (lys_feature_value(mod, "startup") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1086 | add_cpblt("urn:ietf:params:netconf:capability:startup:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1087 | } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1088 | |
| 1089 | /* The URL capability must be set manually using nc_server_set_capability() |
| 1090 | * because of the need for supported protocols to be included. |
| 1091 | * https://tools.ietf.org/html/rfc6241#section-8.8.3 |
| 1092 | */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1093 | // if (lys_feature_value(mod, "url") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1094 | // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count); |
mekleo | a8de5e9 | 2020-02-13 09:05:56 +0100 | [diff] [blame] | 1095 | // } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1096 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1097 | if (lys_feature_value(mod, "xpath") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1098 | add_cpblt("urn:ietf:params:netconf:capability:xpath:1.0", &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1102 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1103 | if (mod) { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1104 | wd_basic_mode = ATOMIC_LOAD_RELAXED(server_opts.wd_basic_mode); |
| 1105 | if (!wd_basic_mode) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1106 | VRB(NULL, "with-defaults capability will not be advertised even though \"ietf-netconf-with-defaults\" model is present, unknown basic-mode."); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1107 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1108 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1109 | switch (wd_basic_mode) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1110 | case NC_WD_ALL: |
| 1111 | strcat(str, "?basic-mode=report-all"); |
| 1112 | break; |
| 1113 | case NC_WD_TRIM: |
| 1114 | strcat(str, "?basic-mode=trim"); |
| 1115 | break; |
| 1116 | case NC_WD_EXPLICIT: |
| 1117 | strcat(str, "?basic-mode=explicit"); |
| 1118 | break; |
| 1119 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1120 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1121 | break; |
| 1122 | } |
| 1123 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1124 | wd_also_supported = ATOMIC_LOAD_RELAXED(server_opts.wd_also_supported); |
| 1125 | if (wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1126 | strcat(str, "&also-supported="); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1127 | if (wd_also_supported & NC_WD_ALL) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1128 | strcat(str, "report-all,"); |
| 1129 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1130 | if (wd_also_supported & NC_WD_ALL_TAG) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1131 | strcat(str, "report-all-tagged,"); |
| 1132 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1133 | if (wd_also_supported & NC_WD_TRIM) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1134 | strcat(str, "trim,"); |
| 1135 | } |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1136 | if (wd_also_supported & NC_WD_EXPLICIT) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1137 | strcat(str, "explicit,"); |
| 1138 | } |
| 1139 | str[strlen(str) - 1] = '\0'; |
| 1140 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1141 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1146 | /* other capabilities */ |
| 1147 | for (u = 0; u < server_opts.capabilities_count; u++) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1148 | add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /* models */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1152 | u = 0; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1153 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1154 | if (!strcmp(mod->name, "ietf-yang-library")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1155 | if (!mod->revision || (strcmp(mod->revision, "2016-06-21") && strcmp(mod->revision, "2019-01-04"))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1156 | ERR(NULL, "Unknown \"ietf-yang-library\" revision, only 2016-06-21 and 2019-01-04 are supported."); |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1157 | goto error; |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1158 | } |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1159 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1160 | /* get content-id */ |
| 1161 | if (server_opts.content_id_clb) { |
| 1162 | yl_content_id = server_opts.content_id_clb(server_opts.content_id_data); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1163 | NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error); |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1164 | } else { |
| 1165 | yl_content_id = malloc(11); |
roman | 124a436 | 2023-10-26 15:36:22 +0200 | [diff] [blame] | 1166 | NC_CHECK_ERRMEM_GOTO(!yl_content_id, , error); |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1167 | sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx)); |
| 1168 | } |
| 1169 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1170 | if (!strcmp(mod->revision, "2019-01-04")) { |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1171 | /* new one (capab defined in RFC 8526 section 2) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1172 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s", |
| 1173 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1174 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1175 | } else { |
| 1176 | /* old one (capab defined in RFC 7950 section 5.6.4) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1177 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s", |
| 1178 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1179 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1180 | } |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1181 | free(yl_content_id); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1182 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1183 | } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1184 | /* skip YANG 1.1 modules */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1185 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1186 | } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) { |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 1187 | /* skip YANG 1.0 modules */ |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1188 | continue; |
| 1189 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1190 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1191 | str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "", |
| 1192 | mod->revision ? mod->revision : ""); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1193 | |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1194 | features_count = 0; |
| 1195 | i = 0; |
| 1196 | feat = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1197 | while ((feat = lysp_feature_next(feat, mod->parsed, &i))) { |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1198 | if (!(feat->flags & LYS_FENABLED)) { |
| 1199 | continue; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 1200 | } |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1201 | if (!features_count) { |
| 1202 | strcat(str, "&features="); |
| 1203 | str_len += 10; |
| 1204 | } |
| 1205 | len = strlen(feat->name); |
| 1206 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1207 | ERRINT; |
| 1208 | break; |
| 1209 | } |
| 1210 | if (features_count) { |
| 1211 | strcat(str, ","); |
| 1212 | ++str_len; |
| 1213 | } |
| 1214 | strcat(str, feat->name); |
| 1215 | str_len += len; |
| 1216 | features_count++; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1217 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1218 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1219 | if (mod->deviated_by) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1220 | strcat(str, "&deviations="); |
| 1221 | str_len += 12; |
| 1222 | dev_count = 0; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1223 | LY_ARRAY_FOR(mod->deviated_by, v) { |
| 1224 | len = strlen(mod->deviated_by[v]->name); |
| 1225 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1226 | ERRINT; |
| 1227 | break; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1228 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1229 | if (dev_count) { |
| 1230 | strcat(str, ","); |
| 1231 | ++str_len; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1232 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1233 | strcat(str, mod->deviated_by[v]->name); |
| 1234 | str_len += len; |
| 1235 | dev_count++; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1239 | add_cpblt(str, &cpblts, &size, &count); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1240 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1241 | |
| 1242 | /* ending NULL capability */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1243 | add_cpblt(NULL, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1244 | |
| 1245 | return cpblts; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1246 | |
| 1247 | error: |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1248 | free(cpblts); |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1249 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1250 | } |
| 1251 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1252 | API char ** |
| 1253 | nc_server_get_cpblts(const struct ly_ctx *ctx) |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1254 | { |
| 1255 | return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF); |
| 1256 | } |
| 1257 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1258 | static int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1259 | parse_cpblts(struct lyd_node *capabilities, char ***list) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1260 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1261 | struct lyd_node *iter; |
| 1262 | struct lyd_node_opaq *cpblt; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1263 | int ver = -1, i = 0; |
| 1264 | const char *cpb_start, *cpb_end; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1265 | |
| 1266 | if (list) { |
| 1267 | /* get the storage for server's capabilities */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1268 | LY_LIST_FOR(lyd_child(capabilities), iter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1269 | i++; |
| 1270 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1271 | /* last item remains NULL */ |
| 1272 | *list = calloc(i + 1, sizeof **list); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1273 | NC_CHECK_ERRMEM_RET(!*list, -1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1274 | i = 0; |
| 1275 | } |
| 1276 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1277 | LY_LIST_FOR(lyd_child(capabilities), iter) { |
| 1278 | cpblt = (struct lyd_node_opaq *)iter; |
| 1279 | |
| 1280 | if (strcmp(cpblt->name.name, "capability") || !cpblt->name.module_ns || strcmp(cpblt->name.module_ns, NC_NS_BASE)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1281 | ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1282 | return -1; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1285 | /* skip leading/trailing whitespaces */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1286 | for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {} |
| 1287 | for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {} |
| 1288 | if (!cpb_start[0] || (cpb_end == cpblt->value)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1289 | ERR(NULL, "Empty capability \"%s\" received.", cpblt->value); |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1290 | return -1; |
| 1291 | } |
| 1292 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1293 | /* detect NETCONF version */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1294 | if ((ver < 0) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.0", cpb_end - cpb_start)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1295 | ver = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1296 | } else if ((ver < 1) && !strncmp(cpb_start, "urn:ietf:params:netconf:base:1.1", cpb_end - cpb_start)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1297 | ver = 1; |
| 1298 | } |
| 1299 | |
| 1300 | /* store capabilities */ |
| 1301 | if (list) { |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1302 | (*list)[i] = strndup(cpb_start, cpb_end - cpb_start); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1303 | NC_CHECK_ERRMEM_RET(!(*list)[i], -1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1304 | i++; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | if (ver == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1309 | ERR(NULL, "Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | return ver; |
| 1313 | } |
| 1314 | |
| 1315 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1316 | nc_send_hello_io(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1317 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1318 | NC_MSG_TYPE ret; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1319 | int i, timeout_io; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1320 | char **cpblts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1321 | uint32_t *sid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1322 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1323 | if (session->side == NC_CLIENT) { |
| 1324 | /* client side hello - send only NETCONF base capabilities */ |
| 1325 | cpblts = malloc(3 * sizeof *cpblts); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame] | 1326 | NC_CHECK_ERRMEM_RET(!cpblts, NC_MSG_ERROR); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1327 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1328 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1329 | cpblts[2] = NULL; |
| 1330 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1331 | timeout_io = NC_CLIENT_HELLO_TIMEOUT * 1000; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1332 | sid = NULL; |
| 1333 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1334 | cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0); |
Michal Vasko | 5b24b6b | 2020-12-04 09:29:47 +0100 | [diff] [blame] | 1335 | if (!cpblts) { |
| 1336 | return NC_MSG_ERROR; |
| 1337 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1338 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1339 | if (session->flags & NC_SESSION_CALLHOME) { |
| 1340 | timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000; |
| 1341 | } else { |
Michal Vasko | 5c1c9b3 | 2024-02-02 09:02:42 +0100 | [diff] [blame] | 1342 | timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1343 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1344 | sid = &session->id; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1345 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1346 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1347 | ret = nc_write_msg_io(session, timeout_io, NC_MSG_HELLO, cpblts, sid); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1348 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1349 | for (i = 0; cpblts[i]; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1350 | free(cpblts[i]); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1351 | } |
| 1352 | free(cpblts); |
| 1353 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1354 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1358 | nc_recv_client_hello_io(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1359 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1360 | struct ly_in *msg; |
| 1361 | struct lyd_node *hello = NULL, *iter; |
| 1362 | struct lyd_node_opaq *node; |
| 1363 | int r, ver = -1, flag = 0; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1364 | char *str; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 1365 | long long id; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1366 | NC_MSG_TYPE rc = NC_MSG_HELLO; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1367 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1368 | r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg); |
| 1369 | switch (r) { |
| 1370 | case 1: |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1371 | /* parse <hello> data */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1372 | if (lyd_parse_data(session->ctx, NULL, msg, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &hello)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1373 | ERR(session, "Failed to parse server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1374 | rc = NC_MSG_ERROR; |
| 1375 | goto cleanup; |
| 1376 | } |
| 1377 | |
| 1378 | LY_LIST_FOR(lyd_child(hello), iter) { |
| 1379 | node = (struct lyd_node_opaq *)iter; |
| 1380 | |
| 1381 | if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1382 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1383 | } else if (!strcmp(node->name.name, "session-id")) { |
| 1384 | if (!node->value || !strlen(node->value)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1385 | ERR(session, "No value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1386 | rc = NC_MSG_ERROR; |
| 1387 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1388 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1389 | str = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1390 | id = strtoll(node->value, &str, 10); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1391 | if (*str || (id < 1) || (id > UINT32_MAX)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1392 | ERR(session, "Invalid value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1393 | rc = NC_MSG_ERROR; |
| 1394 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1395 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1396 | session->id = (uint32_t)id; |
| 1397 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1398 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1399 | ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1400 | rc = NC_MSG_ERROR; |
| 1401 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1402 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1403 | |
| 1404 | if (flag) { |
| 1405 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1406 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1407 | rc = NC_MSG_ERROR; |
| 1408 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1409 | } |
| 1410 | flag = 1; |
| 1411 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1412 | if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) { |
| 1413 | rc = NC_MSG_ERROR; |
| 1414 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1415 | } |
| 1416 | session->version = ver; |
| 1417 | } |
| 1418 | |
| 1419 | if (!session->id) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1420 | ERR(session, "Missing <session-id> in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1421 | rc = NC_MSG_ERROR; |
| 1422 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1423 | } |
| 1424 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1425 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1426 | ERR(session, "Server <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1427 | rc = NC_MSG_WOULDBLOCK; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1428 | break; |
| 1429 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1430 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1431 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1434 | cleanup: |
| 1435 | ly_in_free(msg, 1); |
| 1436 | lyd_free_tree(hello); |
| 1437 | return rc; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1440 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1441 | nc_recv_server_hello_io(struct nc_session *session) |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1442 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1443 | struct ly_in *msg; |
| 1444 | struct lyd_node *hello = NULL, *iter; |
| 1445 | struct lyd_node_opaq *node; |
| 1446 | NC_MSG_TYPE rc = NC_MSG_HELLO; |
| 1447 | int r, ver = -1, flag = 0, timeout_io; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1448 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1449 | if (session->flags & NC_SESSION_CALLHOME) { |
| 1450 | timeout_io = NC_SERVER_CH_HELLO_TIMEOUT * 1000; |
| 1451 | } else { |
Michal Vasko | 5c1c9b3 | 2024-02-02 09:02:42 +0100 | [diff] [blame] | 1452 | timeout_io = server_opts.idle_timeout ? server_opts.idle_timeout * 1000 : -1; |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1455 | r = nc_read_msg_poll_io(session, timeout_io, &msg); |
| 1456 | switch (r) { |
| 1457 | case 1: |
| 1458 | /* parse <hello> data */ |
| 1459 | if (lyd_parse_data(session->ctx, NULL, msg, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &hello)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1460 | ERR(session, "Failed to parse client <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1461 | rc = NC_MSG_ERROR; |
| 1462 | goto cleanup; |
| 1463 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1464 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1465 | /* learn NETCONF version */ |
| 1466 | LY_LIST_FOR(lyd_child(hello), iter) { |
| 1467 | node = (struct lyd_node_opaq *)iter; |
| 1468 | |
| 1469 | if (!node->name.module_ns || strcmp(node->name.module_ns, NC_NS_BASE)) { |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1470 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1471 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1472 | ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1473 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1474 | goto cleanup; |
| 1475 | } |
| 1476 | |
| 1477 | if (flag) { |
| 1478 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1479 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1480 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1481 | goto cleanup; |
| 1482 | } |
| 1483 | flag = 1; |
| 1484 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1485 | if ((ver = parse_cpblts(&node->node, NULL)) < 0) { |
| 1486 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1487 | goto cleanup; |
| 1488 | } |
| 1489 | session->version = ver; |
| 1490 | } |
| 1491 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1492 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1493 | ERR(session, "Client <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1494 | rc = NC_MSG_WOULDBLOCK; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1495 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1496 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1497 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1498 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | cleanup: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1502 | ly_in_free(msg, 1); |
| 1503 | lyd_free_tree(hello); |
| 1504 | return rc; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1505 | } |
| 1506 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1507 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1508 | nc_handshake_io(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1509 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1510 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1511 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1512 | type = nc_send_hello_io(session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1513 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1514 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1517 | if (session->side == NC_CLIENT) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1518 | type = nc_recv_client_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1519 | } else { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1520 | type = nc_recv_server_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1521 | } |
| 1522 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1523 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1524 | } |