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