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