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