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 | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 373 | WRN(session, "%s: session RPC lock timeout, should not happen."); |
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) { |
| 666 | WRN(session, "Missing ietf-netconf schema in context, unable to send <close-session>."); |
| 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 */ |
| 716 | int sock = -1, r; |
| 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 |
| 741 | case NC_TI_LIBSSH: |
| 742 | if (connected) { |
| 743 | ssh_channel_free(session->ti.libssh.channel); |
| 744 | } |
| 745 | /* There can be multiple NETCONF sessions on the same SSH session (NETCONF session maps to |
| 746 | * SSH channel). So destroy the SSH session only if there is no other NETCONF session using |
| 747 | * it. Also, avoid concurrent free by multiple threads of sessions that share the SSH session. |
| 748 | */ |
| 749 | /* SESSION IO LOCK */ |
| 750 | r = nc_session_io_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
| 751 | |
| 752 | if (session->ti.libssh.next) { |
| 753 | for (siter = session->ti.libssh.next; siter != session; siter = siter->ti.libssh.next) { |
| 754 | if (siter->status != NC_STATUS_STARTING) { |
| 755 | *multisession = 1; |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (!*multisession) { |
| 762 | /* it's not multisession yet, but we still need to free the starting sessions */ |
| 763 | if (session->ti.libssh.next) { |
| 764 | do { |
| 765 | siter = session->ti.libssh.next; |
| 766 | session->ti.libssh.next = siter->ti.libssh.next; |
| 767 | |
| 768 | /* free starting SSH NETCONF session (channel will be freed in ssh_free()) */ |
| 769 | free(siter->username); |
| 770 | free(siter->host); |
| 771 | if (!(siter->flags & NC_SESSION_SHAREDCTX)) { |
| 772 | ly_ctx_destroy((struct ly_ctx *)siter->ctx); |
| 773 | } |
| 774 | |
| 775 | free(siter); |
| 776 | } while (session->ti.libssh.next != session); |
| 777 | } |
| 778 | /* remember sock so we can close it */ |
| 779 | sock = ssh_get_fd(session->ti.libssh.session); |
| 780 | if (connected) { |
| 781 | ssh_disconnect(session->ti.libssh.session); |
| 782 | sock = -1; |
| 783 | } |
| 784 | ssh_free(session->ti.libssh.session); |
| 785 | } else { |
| 786 | /* remove the session from the list */ |
| 787 | for (siter = session->ti.libssh.next; siter->ti.libssh.next != session; siter = siter->ti.libssh.next) {} |
| 788 | if (session->ti.libssh.next == siter) { |
| 789 | /* there will be only one session */ |
| 790 | siter->ti.libssh.next = NULL; |
| 791 | } else { |
| 792 | /* there are still multiple sessions, keep the ring list */ |
| 793 | siter->ti.libssh.next = session->ti.libssh.next; |
| 794 | } |
| 795 | /* change nc_sshcb_msg() argument, we need a RUNNING session and this one will be freed */ |
| 796 | if (session->flags & NC_SESSION_SSH_MSG_CB) { |
| 797 | siter = session->ti.libssh.next; |
| 798 | while (siter && (siter->status != NC_STATUS_RUNNING)) { |
| 799 | if (siter->ti.libssh.next == session) { |
| 800 | ERRINT; |
| 801 | break; |
| 802 | } |
| 803 | siter = siter->ti.libssh.next; |
| 804 | } |
| 805 | /* siter may be NULL in case all the sessions terminated at the same time (socket was disconnected), |
| 806 | * we set session to NULL because we do not expect any new message to arrive */ |
| 807 | ssh_set_message_callback(session->ti.libssh.session, nc_sshcb_msg, siter); |
| 808 | if (siter) { |
| 809 | siter->flags |= NC_SESSION_SSH_MSG_CB; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* SESSION IO UNLOCK */ |
| 815 | if (r == 1) { |
| 816 | nc_session_io_unlock(session, __func__); |
| 817 | } |
| 818 | break; |
| 819 | #endif |
| 820 | |
| 821 | #ifdef NC_ENABLED_TLS |
| 822 | case NC_TI_OPENSSL: |
| 823 | /* remember sock so we can close it */ |
| 824 | sock = SSL_get_fd(session->ti.tls); |
| 825 | |
| 826 | if (connected) { |
| 827 | SSL_shutdown(session->ti.tls); |
| 828 | } |
| 829 | SSL_free(session->ti.tls); |
| 830 | |
| 831 | if (session->side == NC_SERVER) { |
| 832 | X509_free(session->opts.server.client_cert); |
| 833 | } |
| 834 | break; |
| 835 | #endif |
| 836 | case NC_TI_NONE: |
| 837 | break; |
| 838 | } |
| 839 | |
| 840 | /* close socket separately */ |
| 841 | if (sock > -1) { |
| 842 | close(sock); |
| 843 | } |
| 844 | } |
| 845 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 846 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 847 | nc_session_free(struct nc_session *session, void (*data_free)(void *)) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 848 | { |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame^] | 849 | int r, i, rpc_locked = 0, msgs_locked = 0, timeout; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 850 | 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] | 851 | struct nc_msg_cont *contiter; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 852 | struct ly_in *msg; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 853 | struct timespec ts; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 854 | void *p; |
| 855 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 856 | if (!session || (session->status == NC_STATUS_CLOSING)) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 857 | return; |
| 858 | } |
| 859 | |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 860 | /* stop notifications thread if any */ |
| 861 | if ((session->side == NC_CLIENT) && ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) { |
| 862 | /* let the thread know it should quit */ |
| 863 | ATOMIC_STORE_RELAXED(session->opts.client.ntf_thread, 2); |
| 864 | |
| 865 | /* wait for it */ |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 866 | nc_gettimespec_mono_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 867 | while (ATOMIC_LOAD_RELAXED(session->opts.client.ntf_thread)) { |
| 868 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | 60d8ffb | 2022-07-21 11:08:34 +0200 | [diff] [blame] | 869 | if (nc_difftimespec_mono_cur(&ts) < 1) { |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 870 | ERR(session, "Waiting for notification thread exit failed (timed out)."); |
| 871 | break; |
| 872 | } |
| 873 | } |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 876 | if (session->side == NC_SERVER) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 877 | r = nc_session_rpc_lock(session, NC_SESSION_FREE_LOCK_TIMEOUT, __func__); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 878 | if (r == -1) { |
Michal Vasko | add4c79 | 2015-10-26 15:36:58 +0100 | [diff] [blame] | 879 | return; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 880 | } else if (r) { |
| 881 | rpc_locked = 1; |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 882 | } else { |
| 883 | /* else failed to lock it, too bad */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 884 | ERR(session, "Freeing a session while an RPC is being processed."); |
Michal Vasko | 96a28a3 | 2021-02-04 15:35:20 +0100 | [diff] [blame] | 885 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 888 | if (session->side == NC_CLIENT) { |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 889 | timeout = NC_SESSION_FREE_LOCK_TIMEOUT; |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 890 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 891 | /* MSGS LOCK */ |
| 892 | r = nc_session_client_msgs_lock(session, &timeout, __func__); |
| 893 | if (r == -1) { |
| 894 | return; |
| 895 | } else if (r) { |
| 896 | msgs_locked = 1; |
| 897 | } else { |
| 898 | /* else failed to lock it, too bad */ |
| 899 | ERR(session, "Freeing a session while messages are being received."); |
| 900 | } |
| 901 | |
| 902 | /* cleanup message queue */ |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 903 | for (contiter = session->opts.client.msgs; contiter; ) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 904 | ly_in_free(contiter->msg, 1); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 905 | |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 906 | p = contiter; |
| 907 | contiter = contiter->next; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 908 | free(p); |
| 909 | } |
| 910 | |
Michal Vasko | 01130bd | 2021-08-26 11:47:38 +0200 | [diff] [blame] | 911 | if (msgs_locked) { |
| 912 | /* MSGS UNLOCK */ |
| 913 | nc_session_client_msgs_unlock(session, __func__); |
| 914 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 915 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 916 | if (session->status == NC_STATUS_RUNNING) { |
Michal Vasko | 9c6d38c | 2021-09-03 13:02:53 +0200 | [diff] [blame] | 917 | /* receive any leftover messages */ |
| 918 | while (nc_read_msg_poll_io(session, 0, &msg) == 1) { |
| 919 | ly_in_free(msg, 1); |
| 920 | } |
| 921 | |
Michal Vasko | 8c24782 | 2020-09-07 13:23:23 +0200 | [diff] [blame] | 922 | /* send closing info to the other side */ |
Michal Vasko | d4da363 | 2022-05-25 11:49:10 +0200 | [diff] [blame] | 923 | nc_session_free_close_session(session); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | /* list of server's capabilities */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 927 | if (session->opts.client.cpblts) { |
| 928 | for (i = 0; session->opts.client.cpblts[i]; i++) { |
Michal Vasko | 96fc4bb | 2017-05-23 14:58:34 +0200 | [diff] [blame] | 929 | free(session->opts.client.cpblts[i]); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 930 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 931 | free(session->opts.client.cpblts); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 935 | if (session->data && data_free) { |
| 936 | data_free(session->data); |
| 937 | } |
| 938 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 939 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 940 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 941 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Michal Vasko | 86d357c | 2016-03-11 13:46:38 +0100 | [diff] [blame] | 944 | /* mark session for closing */ |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 945 | session->status = NC_STATUS_CLOSING; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 946 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 947 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 948 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 949 | |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 950 | nc_gettimespec_real_add(&ts, NC_SESSION_FREE_LOCK_TIMEOUT); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 951 | |
| 952 | /* wait for CH thread to actually wake up and terminate */ |
| 953 | r = 0; |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 954 | while (!r && (session->flags & NC_SESSION_CH_THREAD)) { |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 955 | r = pthread_cond_timedwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, &ts); |
| 956 | } |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 957 | if (r) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 958 | ERR(session, "Waiting for Call Home thread failed (%s).", strerror(r)); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 959 | } |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 960 | } |
| 961 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 962 | if ((session->side == NC_SERVER) && (session->flags & NC_SESSION_CALLHOME)) { |
| 963 | /* CH UNLOCK */ |
| 964 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
| 965 | } |
| 966 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 967 | /* transport implementation cleanup */ |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame^] | 968 | nc_session_free_transport(session, &multisession); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 969 | |
Michal Vasko | 33476c3 | 2022-09-09 11:21:40 +0200 | [diff] [blame^] | 970 | /* final cleanup */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 971 | free(session->username); |
| 972 | free(session->host); |
| 973 | free(session->path); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 974 | |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 975 | if (session->side == NC_SERVER) { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 976 | pthread_mutex_destroy(&session->opts.server.ntf_status_lock); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 977 | if (rpc_locked) { |
| 978 | nc_session_rpc_unlock(session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 9e99f01 | 2016-03-03 13:25:20 +0100 | [diff] [blame] | 979 | } |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 980 | pthread_mutex_destroy(&session->opts.server.rpc_lock); |
| 981 | pthread_cond_destroy(&session->opts.server.rpc_cond); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | if (session->io_lock && !multisession) { |
| 985 | pthread_mutex_destroy(session->io_lock); |
| 986 | free(session->io_lock); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | if (!(session->flags & NC_SESSION_SHAREDCTX)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 990 | ly_ctx_destroy((struct ly_ctx *)session->ctx); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 991 | } |
| 992 | |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 993 | if (session->side == NC_SERVER) { |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 994 | /* free CH synchronization structures */ |
| 995 | pthread_cond_destroy(&session->opts.server.ch_cond); |
| 996 | pthread_mutex_destroy(&session->opts.server.ch_lock); |
tadeas-vintrlik | 54f142a | 2021-08-23 10:36:18 +0200 | [diff] [blame] | 997 | } else { |
| 998 | pthread_mutex_destroy(&session->opts.client.msgs_lock); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1001 | free(session); |
| 1002 | } |
| 1003 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1004 | static void |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1005 | add_cpblt(const char *capab, char ***cpblts, int *size, int *count) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1006 | { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1007 | size_t len; |
| 1008 | int i; |
| 1009 | char *p; |
| 1010 | |
| 1011 | if (capab) { |
| 1012 | /* check if already present */ |
| 1013 | p = strchr(capab, '?'); |
| 1014 | if (p) { |
| 1015 | len = p - capab; |
| 1016 | } else { |
| 1017 | len = strlen(capab); |
| 1018 | } |
| 1019 | for (i = 0; i < *count; i++) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1020 | if (!strncmp((*cpblts)[i], capab, len) && (((*cpblts)[i][len] == '\0') || ((*cpblts)[i][len] == '?'))) { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1021 | /* already present, do not duplicate it */ |
| 1022 | return; |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | /* add another capability */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1028 | if (*count == *size) { |
| 1029 | *size += 5; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1030 | *cpblts = nc_realloc(*cpblts, *size * sizeof **cpblts); |
| 1031 | if (!(*cpblts)) { |
| 1032 | ERRMEM; |
| 1033 | return; |
| 1034 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1037 | (*cpblts)[*count] = capab ? strdup(capab) : NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1038 | ++(*count); |
| 1039 | } |
| 1040 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1041 | API char ** |
| 1042 | nc_server_get_cpblts_version(const struct ly_ctx *ctx, LYS_VERSION version) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1043 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1044 | char **cpblts; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1045 | const struct lys_module *mod; |
| 1046 | struct lysp_feature *feat; |
| 1047 | int size = 10, count, features_count = 0, dev_count = 0, str_len, len; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1048 | uint32_t i, u; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1049 | LY_ARRAY_COUNT_TYPE v; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1050 | char *yl_content_id; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1051 | |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1052 | #define NC_CPBLT_BUF_LEN 4096 |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1053 | char str[NC_CPBLT_BUF_LEN]; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1054 | |
Michal Vasko | 4ffa3b2 | 2016-05-24 16:36:25 +0200 | [diff] [blame] | 1055 | if (!ctx) { |
| 1056 | ERRARG("ctx"); |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1060 | cpblts = malloc(size * sizeof *cpblts); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1061 | if (!cpblts) { |
| 1062 | ERRMEM; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1063 | goto error; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1064 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1065 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1066 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1067 | count = 2; |
| 1068 | |
| 1069 | /* capabilities */ |
| 1070 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1071 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1072 | if (mod) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1073 | if (lys_feature_value(mod, "writable-running") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1074 | 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] | 1075 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1076 | if (lys_feature_value(mod, "candidate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1077 | 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] | 1078 | if (lys_feature_value(mod, "confirmed-commit") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1079 | 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] | 1080 | } |
| 1081 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1082 | if (lys_feature_value(mod, "rollback-on-error") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1083 | 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] | 1084 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1085 | if (lys_feature_value(mod, "validate") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1086 | 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] | 1087 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1088 | if (lys_feature_value(mod, "startup") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1089 | 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] | 1090 | } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1091 | |
| 1092 | /* The URL capability must be set manually using nc_server_set_capability() |
| 1093 | * because of the need for supported protocols to be included. |
| 1094 | * https://tools.ietf.org/html/rfc6241#section-8.8.3 |
| 1095 | */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1096 | // if (lys_feature_value(mod, "url") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1097 | // add_cpblt("urn:ietf:params:netconf:capability:url:1.0", &cpblts, &size, &count); |
mekleo | a8de5e9 | 2020-02-13 09:05:56 +0100 | [diff] [blame] | 1098 | // } |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1099 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1100 | if (lys_feature_value(mod, "xpath") == LY_SUCCESS) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1101 | 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] | 1102 | } |
| 1103 | } |
| 1104 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1105 | mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf-with-defaults"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1106 | if (mod) { |
| 1107 | if (!server_opts.wd_basic_mode) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1108 | 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] | 1109 | } else { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1110 | strcpy(str, "urn:ietf:params:netconf:capability:with-defaults:1.0"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1111 | switch (server_opts.wd_basic_mode) { |
| 1112 | case NC_WD_ALL: |
| 1113 | strcat(str, "?basic-mode=report-all"); |
| 1114 | break; |
| 1115 | case NC_WD_TRIM: |
| 1116 | strcat(str, "?basic-mode=trim"); |
| 1117 | break; |
| 1118 | case NC_WD_EXPLICIT: |
| 1119 | strcat(str, "?basic-mode=explicit"); |
| 1120 | break; |
| 1121 | default: |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1122 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1123 | break; |
| 1124 | } |
| 1125 | |
| 1126 | if (server_opts.wd_also_supported) { |
Michal Vasko | 2e47ef9 | 2016-06-20 10:03:24 +0200 | [diff] [blame] | 1127 | strcat(str, "&also-supported="); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1128 | if (server_opts.wd_also_supported & NC_WD_ALL) { |
| 1129 | strcat(str, "report-all,"); |
| 1130 | } |
| 1131 | if (server_opts.wd_also_supported & NC_WD_ALL_TAG) { |
| 1132 | strcat(str, "report-all-tagged,"); |
| 1133 | } |
| 1134 | if (server_opts.wd_also_supported & NC_WD_TRIM) { |
| 1135 | strcat(str, "trim,"); |
| 1136 | } |
| 1137 | if (server_opts.wd_also_supported & NC_WD_EXPLICIT) { |
| 1138 | strcat(str, "explicit,"); |
| 1139 | } |
| 1140 | str[strlen(str) - 1] = '\0'; |
| 1141 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1142 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 1147 | /* other capabilities */ |
| 1148 | for (u = 0; u < server_opts.capabilities_count; u++) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1149 | add_cpblt(server_opts.capabilities[u], &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* models */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1153 | u = 0; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1154 | while ((mod = ly_ctx_get_module_iter(ctx, &u))) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1155 | if (!strcmp(mod->name, "ietf-yang-library")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1156 | 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] | 1157 | 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] | 1158 | goto error; |
Michal Vasko | f0fba4e | 2020-02-14 17:15:31 +0100 | [diff] [blame] | 1159 | } |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1160 | |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1161 | /* get content-id */ |
| 1162 | if (server_opts.content_id_clb) { |
| 1163 | yl_content_id = server_opts.content_id_clb(server_opts.content_id_data); |
| 1164 | if (!yl_content_id) { |
| 1165 | ERRMEM; |
| 1166 | goto error; |
| 1167 | } |
| 1168 | } else { |
| 1169 | yl_content_id = malloc(11); |
| 1170 | if (!yl_content_id) { |
| 1171 | ERRMEM; |
| 1172 | goto error; |
| 1173 | } |
| 1174 | sprintf(yl_content_id, "%u", ly_ctx_get_change_count(ctx)); |
| 1175 | } |
| 1176 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1177 | if (!strcmp(mod->revision, "2019-01-04")) { |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1178 | /* new one (capab defined in RFC 8526 section 2) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1179 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.1?revision=%s&content-id=%s", |
| 1180 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1181 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | 7b5e3d9 | 2020-04-08 14:40:31 +0200 | [diff] [blame] | 1182 | } else { |
| 1183 | /* old one (capab defined in RFC 7950 section 5.6.4) */ |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1184 | sprintf(str, "urn:ietf:params:netconf:capability:yang-library:1.0?revision=%s&module-set-id=%s", |
| 1185 | mod->revision, yl_content_id); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1186 | add_cpblt(str, &cpblts, &size, &count); |
Michal Vasko | d5ada12 | 2020-03-19 18:28:06 +0100 | [diff] [blame] | 1187 | } |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 1188 | free(yl_content_id); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1189 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1190 | } else if ((version == LYS_VERSION_1_0) && (mod->parsed->version > version)) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1191 | /* skip YANG 1.1 schemas */ |
| 1192 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1193 | } else if ((version == LYS_VERSION_1_1) && (mod->parsed->version != version)) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1194 | /* skip YANG 1.0 schemas */ |
| 1195 | continue; |
| 1196 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1197 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1198 | str_len = sprintf(str, "%s?module=%s%s%s", mod->ns, mod->name, mod->revision ? "&revision=" : "", |
| 1199 | mod->revision ? mod->revision : ""); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1200 | |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1201 | features_count = 0; |
| 1202 | i = 0; |
| 1203 | feat = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1204 | while ((feat = lysp_feature_next(feat, mod->parsed, &i))) { |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1205 | if (!(feat->flags & LYS_FENABLED)) { |
| 1206 | continue; |
Michal Vasko | e90e4d1 | 2016-06-20 10:05:01 +0200 | [diff] [blame] | 1207 | } |
Michal Vasko | dafdc74 | 2020-03-11 16:15:59 +0100 | [diff] [blame] | 1208 | if (!features_count) { |
| 1209 | strcat(str, "&features="); |
| 1210 | str_len += 10; |
| 1211 | } |
| 1212 | len = strlen(feat->name); |
| 1213 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1214 | ERRINT; |
| 1215 | break; |
| 1216 | } |
| 1217 | if (features_count) { |
| 1218 | strcat(str, ","); |
| 1219 | ++str_len; |
| 1220 | } |
| 1221 | strcat(str, feat->name); |
| 1222 | str_len += len; |
| 1223 | features_count++; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1224 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1225 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1226 | if (mod->deviated_by) { |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1227 | strcat(str, "&deviations="); |
| 1228 | str_len += 12; |
| 1229 | dev_count = 0; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1230 | LY_ARRAY_FOR(mod->deviated_by, v) { |
| 1231 | len = strlen(mod->deviated_by[v]->name); |
| 1232 | if (str_len + 1 + len >= NC_CPBLT_BUF_LEN) { |
| 1233 | ERRINT; |
| 1234 | break; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1235 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1236 | if (dev_count) { |
| 1237 | strcat(str, ","); |
| 1238 | ++str_len; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1239 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1240 | strcat(str, mod->deviated_by[v]->name); |
| 1241 | str_len += len; |
| 1242 | dev_count++; |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1246 | add_cpblt(str, &cpblts, &size, &count); |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1247 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1248 | |
| 1249 | /* ending NULL capability */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1250 | add_cpblt(NULL, &cpblts, &size, &count); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1251 | |
| 1252 | return cpblts; |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1253 | |
| 1254 | error: |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1255 | free(cpblts); |
Radek Krejci | f906e41 | 2017-09-22 14:44:45 +0200 | [diff] [blame] | 1256 | return NULL; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1257 | } |
| 1258 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1259 | API char ** |
| 1260 | nc_server_get_cpblts(const struct ly_ctx *ctx) |
Radek Krejci | 24a1841 | 2018-05-16 15:09:10 +0200 | [diff] [blame] | 1261 | { |
| 1262 | return nc_server_get_cpblts_version(ctx, LYS_VERSION_UNDEF); |
| 1263 | } |
| 1264 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1265 | static int |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1266 | parse_cpblts(struct lyd_node *capabilities, char ***list) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1267 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1268 | struct lyd_node *iter; |
| 1269 | struct lyd_node_opaq *cpblt; |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1270 | int ver = -1, i = 0; |
| 1271 | const char *cpb_start, *cpb_end; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1272 | |
| 1273 | if (list) { |
| 1274 | /* get the storage for server's capabilities */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1275 | LY_LIST_FOR(lyd_child(capabilities), iter) { |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1276 | i++; |
| 1277 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 1278 | /* last item remains NULL */ |
| 1279 | *list = calloc(i + 1, sizeof **list); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1280 | if (!*list) { |
| 1281 | ERRMEM; |
| 1282 | return -1; |
| 1283 | } |
| 1284 | i = 0; |
| 1285 | } |
| 1286 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1287 | LY_LIST_FOR(lyd_child(capabilities), iter) { |
| 1288 | cpblt = (struct lyd_node_opaq *)iter; |
| 1289 | |
| 1290 | 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] | 1291 | ERR(NULL, "Unexpected <%s> element in client's <hello>.", cpblt->name.name); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1292 | return -1; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1295 | /* skip leading/trailing whitespaces */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1296 | for (cpb_start = cpblt->value; isspace(cpb_start[0]); ++cpb_start) {} |
| 1297 | for (cpb_end = cpblt->value + strlen(cpblt->value); (cpb_end > cpblt->value) && isspace(cpb_end[-1]); --cpb_end) {} |
| 1298 | if (!cpb_start[0] || (cpb_end == cpblt->value)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1299 | ERR(NULL, "Empty capability \"%s\" received.", cpblt->value); |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1300 | return -1; |
| 1301 | } |
| 1302 | |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1303 | /* detect NETCONF version */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1304 | 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] | 1305 | ver = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1306 | } 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] | 1307 | ver = 1; |
| 1308 | } |
| 1309 | |
| 1310 | /* store capabilities */ |
| 1311 | if (list) { |
Michal Vasko | 156d327 | 2017-04-11 11:46:49 +0200 | [diff] [blame] | 1312 | (*list)[i] = strndup(cpb_start, cpb_end - cpb_start); |
| 1313 | if (!(*list)[i]) { |
| 1314 | ERRMEM; |
| 1315 | return -1; |
| 1316 | } |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1317 | i++; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | if (ver == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1322 | ERR(NULL, "Peer does not support a compatible NETCONF version."); |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | return ver; |
| 1326 | } |
| 1327 | |
| 1328 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1329 | nc_send_hello_io(struct nc_session *session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1330 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1331 | NC_MSG_TYPE ret; |
| 1332 | int i, io_timeout; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1333 | char **cpblts; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1334 | uint32_t *sid; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1335 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1336 | if (session->side == NC_CLIENT) { |
| 1337 | /* client side hello - send only NETCONF base capabilities */ |
| 1338 | cpblts = malloc(3 * sizeof *cpblts); |
| 1339 | if (!cpblts) { |
| 1340 | ERRMEM; |
| 1341 | return NC_MSG_ERROR; |
| 1342 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1343 | cpblts[0] = strdup("urn:ietf:params:netconf:base:1.0"); |
| 1344 | cpblts[1] = strdup("urn:ietf:params:netconf:base:1.1"); |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1345 | cpblts[2] = NULL; |
| 1346 | |
| 1347 | io_timeout = NC_CLIENT_HELLO_TIMEOUT * 1000; |
| 1348 | sid = NULL; |
| 1349 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1350 | cpblts = nc_server_get_cpblts_version(session->ctx, LYS_VERSION_1_0); |
Michal Vasko | 5b24b6b | 2020-12-04 09:29:47 +0100 | [diff] [blame] | 1351 | if (!cpblts) { |
| 1352 | return NC_MSG_ERROR; |
| 1353 | } |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1354 | |
| 1355 | io_timeout = NC_SERVER_HELLO_TIMEOUT * 1000; |
| 1356 | sid = &session->id; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1357 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 1358 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1359 | 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] | 1360 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1361 | for (i = 0; cpblts[i]; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1362 | free(cpblts[i]); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1363 | } |
| 1364 | free(cpblts); |
| 1365 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1366 | return ret; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1370 | nc_recv_client_hello_io(struct nc_session *session) |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1371 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1372 | struct ly_in *msg; |
| 1373 | struct lyd_node *hello = NULL, *iter; |
| 1374 | struct lyd_node_opaq *node; |
| 1375 | int r, ver = -1, flag = 0; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1376 | char *str; |
| 1377 | long long int id; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1378 | NC_MSG_TYPE rc = NC_MSG_HELLO; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1379 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1380 | r = nc_read_msg_poll_io(session, NC_CLIENT_HELLO_TIMEOUT * 1000, &msg); |
| 1381 | switch (r) { |
| 1382 | case 1: |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1383 | /* parse <hello> data */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1384 | 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] | 1385 | ERR(session, "Failed to parse server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1386 | rc = NC_MSG_ERROR; |
| 1387 | goto cleanup; |
| 1388 | } |
| 1389 | |
| 1390 | LY_LIST_FOR(lyd_child(hello), iter) { |
| 1391 | node = (struct lyd_node_opaq *)iter; |
| 1392 | |
| 1393 | 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] | 1394 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1395 | } else if (!strcmp(node->name.name, "session-id")) { |
| 1396 | if (!node->value || !strlen(node->value)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1397 | ERR(session, "No value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1398 | rc = NC_MSG_ERROR; |
| 1399 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1400 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1401 | str = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1402 | id = strtoll(node->value, &str, 10); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1403 | if (*str || (id < 1) || (id > UINT32_MAX)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1404 | ERR(session, "Invalid value of <session-id> element in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1405 | rc = NC_MSG_ERROR; |
| 1406 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1407 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1408 | session->id = (uint32_t)id; |
| 1409 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1410 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1411 | ERR(session, "Unexpected <%s> element in server <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1412 | rc = NC_MSG_ERROR; |
| 1413 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1414 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1415 | |
| 1416 | if (flag) { |
| 1417 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1418 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1419 | rc = NC_MSG_ERROR; |
| 1420 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1421 | } |
| 1422 | flag = 1; |
| 1423 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1424 | if ((ver = parse_cpblts(&node->node, &session->opts.client.cpblts)) < 0) { |
| 1425 | rc = NC_MSG_ERROR; |
| 1426 | goto cleanup; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1427 | } |
| 1428 | session->version = ver; |
| 1429 | } |
| 1430 | |
| 1431 | if (!session->id) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1432 | ERR(session, "Missing <session-id> in server <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1433 | rc = NC_MSG_ERROR; |
| 1434 | goto cleanup; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1435 | } |
| 1436 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1437 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1438 | ERR(session, "Server <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1439 | rc = NC_MSG_WOULDBLOCK; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1440 | break; |
| 1441 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1442 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1443 | break; |
Radek Krejci | 695d4fa | 2015-10-22 13:23:54 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1446 | cleanup: |
| 1447 | ly_in_free(msg, 1); |
| 1448 | lyd_free_tree(hello); |
| 1449 | return rc; |
Radek Krejci | 5686ff7 | 2015-10-09 13:33:56 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1452 | static NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1453 | nc_recv_server_hello_io(struct nc_session *session) |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1454 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1455 | struct ly_in *msg; |
| 1456 | struct lyd_node *hello = NULL, *iter; |
| 1457 | struct lyd_node_opaq *node; |
| 1458 | NC_MSG_TYPE rc = NC_MSG_HELLO; |
| 1459 | int r, ver = -1, flag = 0, timeout_io; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1460 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1461 | timeout_io = server_opts.hello_timeout ? server_opts.hello_timeout * 1000 : NC_SERVER_HELLO_TIMEOUT * 1000; |
| 1462 | r = nc_read_msg_poll_io(session, timeout_io, &msg); |
| 1463 | switch (r) { |
| 1464 | case 1: |
| 1465 | /* parse <hello> data */ |
| 1466 | 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] | 1467 | ERR(session, "Failed to parse client <hello>."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1468 | rc = NC_MSG_ERROR; |
| 1469 | goto cleanup; |
| 1470 | } |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1471 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1472 | /* learn NETCONF version */ |
| 1473 | LY_LIST_FOR(lyd_child(hello), iter) { |
| 1474 | node = (struct lyd_node_opaq *)iter; |
| 1475 | |
| 1476 | 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] | 1477 | continue; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1478 | } else if (strcmp(node->name.name, "capabilities")) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1479 | ERR(session, "Unexpected <%s> element in client <hello>.", node->name.name); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1480 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1481 | goto cleanup; |
| 1482 | } |
| 1483 | |
| 1484 | if (flag) { |
| 1485 | /* multiple capabilities elements */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1486 | ERR(session, "Invalid <hello> message (multiple <capabilities> elements)."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1487 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1488 | goto cleanup; |
| 1489 | } |
| 1490 | flag = 1; |
| 1491 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1492 | if ((ver = parse_cpblts(&node->node, NULL)) < 0) { |
| 1493 | rc = NC_MSG_BAD_HELLO; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1494 | goto cleanup; |
| 1495 | } |
| 1496 | session->version = ver; |
| 1497 | } |
| 1498 | break; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1499 | case 0: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1500 | ERR(session, "Client <hello> timeout elapsed."); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1501 | rc = NC_MSG_WOULDBLOCK; |
Michal Vasko | 5e6f4cc | 2016-01-20 13:27:44 +0100 | [diff] [blame] | 1502 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1503 | default: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1504 | rc = NC_MSG_ERROR; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1505 | break; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | cleanup: |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1509 | ly_in_free(msg, 1); |
| 1510 | lyd_free_tree(hello); |
| 1511 | return rc; |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1512 | } |
| 1513 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1514 | NC_MSG_TYPE |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1515 | nc_handshake_io(struct nc_session *session) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1516 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1517 | NC_MSG_TYPE type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1518 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1519 | type = nc_send_hello_io(session); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1520 | if (type != NC_MSG_HELLO) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1521 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1522 | } |
| 1523 | |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1524 | if (session->side == NC_CLIENT) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1525 | type = nc_recv_client_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1526 | } else { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1527 | type = nc_recv_server_hello_io(session); |
Michal Vasko | 11d142a | 2016-01-19 15:58:24 +0100 | [diff] [blame] | 1528 | } |
| 1529 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1530 | return type; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 1531 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1532 | |
Michal Vasko | 889162e | 2019-09-06 14:43:37 +0200 | [diff] [blame] | 1533 | #ifdef NC_ENABLED_SSH |
| 1534 | |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1535 | static void |
| 1536 | nc_ssh_init(void) |
| 1537 | { |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1538 | #if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0)) |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1539 | ssh_threads_set_callbacks(ssh_threads_get_pthread()); |
| 1540 | ssh_init(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1541 | #endif |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static void |
| 1545 | nc_ssh_destroy(void) |
| 1546 | { |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1547 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1548 | FIPS_mode_set(0); |
| 1549 | CONF_modules_unload(1); |
| 1550 | nc_thread_destroy(); |
Michal Vasko | 889162e | 2019-09-06 14:43:37 +0200 | [diff] [blame] | 1551 | #endif |
| 1552 | |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1553 | #if (LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 8, 0)) |
| 1554 | ssh_finalize(); |
| 1555 | #endif |
| 1556 | } |
| 1557 | |
Michal Vasko | 889162e | 2019-09-06 14:43:37 +0200 | [diff] [blame] | 1558 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 999b64a | 2019-07-10 16:06:15 +0200 | [diff] [blame] | 1559 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1560 | #ifdef NC_ENABLED_TLS |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1561 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1562 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1563 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1564 | struct CRYPTO_dynlock_value { |
| 1565 | pthread_mutex_t lock; |
| 1566 | }; |
| 1567 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1568 | static struct CRYPTO_dynlock_value * |
| 1569 | tls_dyn_create_func(const char *UNUSED(file), int UNUSED(line)) |
| 1570 | { |
| 1571 | struct CRYPTO_dynlock_value *value; |
| 1572 | |
| 1573 | value = malloc(sizeof *value); |
| 1574 | if (!value) { |
| 1575 | ERRMEM; |
| 1576 | return NULL; |
| 1577 | } |
| 1578 | pthread_mutex_init(&value->lock, NULL); |
| 1579 | |
| 1580 | return value; |
| 1581 | } |
| 1582 | |
| 1583 | static void |
| 1584 | tls_dyn_lock_func(int mode, struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1585 | { |
| 1586 | /* mode can also be CRYPTO_READ or CRYPTO_WRITE, but all the examples |
| 1587 | * I found ignored this fact, what do I know... */ |
| 1588 | if (mode & CRYPTO_LOCK) { |
| 1589 | pthread_mutex_lock(&l->lock); |
| 1590 | } else { |
| 1591 | pthread_mutex_unlock(&l->lock); |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | static void |
| 1596 | tls_dyn_destroy_func(struct CRYPTO_dynlock_value *l, const char *UNUSED(file), int UNUSED(line)) |
| 1597 | { |
| 1598 | pthread_mutex_destroy(&l->lock); |
| 1599 | free(l); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1600 | } |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1601 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1602 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1603 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1604 | #endif /* NC_ENABLED_TLS */ |
| 1605 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1606 | #if defined (NC_ENABLED_TLS) && !defined (NC_ENABLED_SSH) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1607 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1608 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1609 | static pthread_mutex_t *tls_locks; |
| 1610 | |
| 1611 | static void |
| 1612 | tls_thread_locking_func(int mode, int n, const char *UNUSED(file), int UNUSED(line)) |
| 1613 | { |
| 1614 | if (mode & CRYPTO_LOCK) { |
| 1615 | pthread_mutex_lock(tls_locks + n); |
| 1616 | } else { |
| 1617 | pthread_mutex_unlock(tls_locks + n); |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | static void |
| 1622 | tls_thread_id_func(CRYPTO_THREADID *tid) |
| 1623 | { |
| 1624 | CRYPTO_THREADID_set_numeric(tid, (unsigned long)pthread_self()); |
| 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 | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1628 | |
| 1629 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1630 | nc_tls_init(void) |
| 1631 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1632 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1633 | SSL_load_error_strings(); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1634 | ERR_load_BIO_strings(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1635 | SSL_library_init(); |
| 1636 | |
Michal Vasko | f89948c | 2018-01-04 09:19:46 +0100 | [diff] [blame] | 1637 | int i; |
| 1638 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1639 | tls_locks = malloc(CRYPTO_num_locks() * sizeof *tls_locks); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1640 | if (!tls_locks) { |
| 1641 | ERRMEM; |
| 1642 | return; |
| 1643 | } |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1644 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1645 | pthread_mutex_init(tls_locks + i, NULL); |
| 1646 | } |
| 1647 | |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1648 | CRYPTO_THREADID_set_callback(tls_thread_id_func); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1649 | CRYPTO_set_locking_callback(tls_thread_locking_func); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1650 | |
| 1651 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1652 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1653 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1654 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1655 | } |
| 1656 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1657 | static void |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1658 | nc_tls_destroy(void) |
| 1659 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1660 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1661 | FIPS_mode_set(0); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1662 | CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1663 | nc_thread_destroy(); |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1664 | EVP_cleanup(); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1665 | ERR_free_strings(); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1666 | #if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1667 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1668 | #elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1669 | SSL_COMP_free_compression_methods(); |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1670 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1671 | |
Michal Vasko | f89948c | 2018-01-04 09:19:46 +0100 | [diff] [blame] | 1672 | int i; |
| 1673 | |
Michal Vasko | b6e3726 | 2016-02-25 14:49:00 +0100 | [diff] [blame] | 1674 | CRYPTO_THREADID_set_callback(NULL); |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1675 | CRYPTO_set_locking_callback(NULL); |
| 1676 | for (i = 0; i < CRYPTO_num_locks(); ++i) { |
| 1677 | pthread_mutex_destroy(tls_locks + i); |
| 1678 | } |
| 1679 | free(tls_locks); |
Michal Vasko | f0c92c0 | 2016-01-29 09:41:45 +0100 | [diff] [blame] | 1680 | |
| 1681 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1682 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1683 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1684 | #endif |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1687 | #endif /* NC_ENABLED_TLS && !NC_ENABLED_SSH */ |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1688 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1689 | #if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS) |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1690 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1691 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1692 | nc_ssh_tls_init(void) |
| 1693 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1694 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1695 | SSL_load_error_strings(); |
| 1696 | ERR_load_BIO_strings(); |
| 1697 | SSL_library_init(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1698 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1699 | |
| 1700 | nc_ssh_init(); |
| 1701 | |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1702 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1703 | CRYPTO_set_dynlock_create_callback(tls_dyn_create_func); |
| 1704 | CRYPTO_set_dynlock_lock_callback(tls_dyn_lock_func); |
| 1705 | CRYPTO_set_dynlock_destroy_callback(tls_dyn_destroy_func); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1706 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1707 | } |
| 1708 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1709 | static void |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1710 | nc_ssh_tls_destroy(void) |
| 1711 | { |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 1712 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1713 | ERR_free_strings(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1714 | # if OPENSSL_VERSION_NUMBER < 0x10002000L // < 1.0.2 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1715 | sk_SSL_COMP_free(SSL_COMP_get_compression_methods()); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1716 | # elif OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1717 | SSL_COMP_free_compression_methods(); |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1718 | # endif |
Jan Kundrát | 47e9e1a | 2016-11-21 09:41:59 +0100 | [diff] [blame] | 1719 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1720 | |
| 1721 | nc_ssh_destroy(); |
| 1722 | |
Michal Vasko | e1e8263 | 2019-09-09 09:18:03 +0200 | [diff] [blame] | 1723 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1724 | CRYPTO_set_dynlock_create_callback(NULL); |
| 1725 | CRYPTO_set_dynlock_lock_callback(NULL); |
| 1726 | CRYPTO_set_dynlock_destroy_callback(NULL); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1727 | #endif |
Michal Vasko | 5e22879 | 2016-02-03 15:30:24 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1730 | #endif /* NC_ENABLED_SSH && NC_ENABLED_TLS */ |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1731 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1732 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1733 | |
| 1734 | API void |
| 1735 | nc_thread_destroy(void) |
| 1736 | { |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1737 | /* caused data-races and seems not neccessary for avoiding valgrind reachable memory */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1738 | // CRYPTO_cleanup_all_ex_data(); |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1739 | |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1740 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 1741 | CRYPTO_THREADID crypto_tid; |
| 1742 | |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1743 | CRYPTO_THREADID_current(&crypto_tid); |
| 1744 | ERR_remove_thread_state(&crypto_tid); |
Michal Vasko | 770b436 | 2017-02-17 14:44:18 +0100 | [diff] [blame] | 1745 | #endif |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1748 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 1749 | |
| 1750 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1751 | nc_init(void) |
| 1752 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1753 | #if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1754 | nc_ssh_tls_init(); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1755 | #elif defined (NC_ENABLED_SSH) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1756 | nc_ssh_init(); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1757 | #elif defined (NC_ENABLED_TLS) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1758 | nc_tls_init(); |
| 1759 | #endif |
| 1760 | } |
| 1761 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 1762 | void |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1763 | nc_destroy(void) |
| 1764 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1765 | #if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1766 | nc_ssh_tls_destroy(); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1767 | #elif defined (NC_ENABLED_SSH) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1768 | nc_ssh_destroy(); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1769 | #elif defined (NC_ENABLED_TLS) |
Michal Vasko | 8f0c028 | 2016-02-29 10:17:14 +0100 | [diff] [blame] | 1770 | nc_tls_destroy(); |
| 1771 | #endif |
| 1772 | } |