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