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