Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file session_server.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libnetconf2 server session manipulation functions |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
| 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [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 |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 14 | */ |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 15 | #define _QNX_SOURCE /* getpeereid */ |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE /* signals, threads, SO_PEERCRED */ |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 17 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 18 | #include <arpa/inet.h> |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 20 | #include <errno.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 21 | #include <fcntl.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 22 | #include <netinet/in.h> |
| 23 | #include <netinet/tcp.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 24 | #include <poll.h> |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 25 | #include <pthread.h> |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 26 | #include <pwd.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 27 | #include <signal.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/un.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 36 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 37 | #include "compat.h" |
roman | 466719d | 2023-05-05 16:14:37 +0200 | [diff] [blame^] | 38 | #include "config_new_ssh.h" |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 39 | #include "libnetconf.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 40 | #include "session_server.h" |
Michal Vasko | 0bdf70b | 2019-06-24 19:20:20 +0200 | [diff] [blame] | 41 | #include "session_server_ch.h" |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 42 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 43 | struct nc_server_opts server_opts = { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 44 | #ifdef NC_ENABLED_SSH |
| 45 | .authkey_lock = PTHREAD_MUTEX_INITIALIZER, |
| 46 | #endif |
| 47 | .bind_lock = PTHREAD_MUTEX_INITIALIZER, |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 48 | .endpt_lock = PTHREAD_RWLOCK_INITIALIZER, |
| 49 | .ch_client_lock = PTHREAD_RWLOCK_INITIALIZER |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 50 | }; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 51 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 52 | static nc_rpc_clb global_rpc_clb = NULL; |
| 53 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 54 | struct nc_endpt * |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 55 | nc_server_endpt_lock_get(const char *name, NC_TRANSPORT_IMPL ti, uint16_t *idx) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 56 | { |
| 57 | uint16_t i; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 58 | struct nc_endpt *endpt = NULL; |
| 59 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 60 | if (!name) { |
| 61 | ERRARG("endpt_name"); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 65 | /* WRITE LOCK */ |
| 66 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 67 | |
| 68 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 69 | if (!strcmp(server_opts.endpts[i].name, name) && (!ti || (server_opts.endpts[i].ti == ti))) { |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 70 | endpt = &server_opts.endpts[i]; |
| 71 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 75 | if (!endpt) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 76 | ERR(NULL, "Endpoint \"%s\" was not found.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 77 | /* UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 78 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
| 81 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 82 | if (idx) { |
| 83 | *idx = i; |
| 84 | } |
| 85 | |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 86 | return endpt; |
| 87 | } |
| 88 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 89 | struct nc_ch_endpt * |
| 90 | nc_server_ch_client_lock(const char *name, const char *endpt_name, NC_TRANSPORT_IMPL ti, struct nc_ch_client **client_p) |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 91 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 92 | uint16_t i, j; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 93 | struct nc_ch_client *client = NULL; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 94 | struct nc_ch_endpt *endpt = NULL; |
| 95 | |
| 96 | *client_p = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 97 | |
Michal Vasko | ddce121 | 2019-05-24 09:58:49 +0200 | [diff] [blame] | 98 | if (!name) { |
| 99 | ERRARG("client_name"); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 103 | /* READ LOCK */ |
| 104 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 105 | |
| 106 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 107 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 108 | client = &server_opts.ch_clients[i]; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 109 | if (!endpt_name && !ti) { |
| 110 | /* return only client */ |
| 111 | break; |
| 112 | } |
| 113 | for (j = 0; j < client->ch_endpt_count; ++j) { |
Michal Vasko | 530d95c | 2021-05-28 13:32:02 +0200 | [diff] [blame] | 114 | if ((!endpt_name || !strcmp(client->ch_endpts[j].name, endpt_name)) && |
| 115 | (!ti || (ti == client->ch_endpts[j].ti))) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 116 | endpt = &client->ch_endpts[j]; |
| 117 | break; |
| 118 | } |
| 119 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (!client) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 125 | ERR(NULL, "Call Home client \"%s\" was not found.", name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 126 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 127 | /* READ UNLOCK */ |
| 128 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 129 | } else if (endpt_name && ti && !endpt) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 130 | ERR(NULL, "Call Home client \"%s\" endpoint \"%s\" was not found.", name, endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 131 | |
| 132 | /* READ UNLOCK */ |
| 133 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 134 | } else { |
| 135 | /* CH CLIENT LOCK */ |
| 136 | pthread_mutex_lock(&client->lock); |
| 137 | |
| 138 | *client_p = client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 141 | return endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void |
| 145 | nc_server_ch_client_unlock(struct nc_ch_client *client) |
| 146 | { |
| 147 | /* CH CLIENT UNLOCK */ |
| 148 | pthread_mutex_unlock(&client->lock); |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 149 | |
| 150 | /* READ UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 151 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 152 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 153 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 154 | API void |
| 155 | nc_session_set_term_reason(struct nc_session *session, NC_SESSION_TERM_REASON reason) |
| 156 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 157 | if (!session) { |
| 158 | ERRARG("session"); |
| 159 | return; |
| 160 | } else if (!reason) { |
| 161 | ERRARG("reason"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 165 | if ((reason != NC_SESSION_TERM_KILLED) && (session->term_reason == NC_SESSION_TERM_KILLED)) { |
| 166 | session->killed_by = 0; |
| 167 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 168 | session->term_reason = reason; |
| 169 | } |
| 170 | |
Michal Vasko | 142cfea | 2017-08-07 10:12:11 +0200 | [diff] [blame] | 171 | API void |
| 172 | nc_session_set_killed_by(struct nc_session *session, uint32_t sid) |
| 173 | { |
| 174 | if (!session || (session->term_reason != NC_SESSION_TERM_KILLED)) { |
| 175 | ERRARG("session"); |
| 176 | return; |
| 177 | } else if (!sid) { |
| 178 | ERRARG("sid"); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | session->killed_by = sid; |
| 183 | } |
| 184 | |
| 185 | API void |
| 186 | nc_session_set_status(struct nc_session *session, NC_STATUS status) |
| 187 | { |
| 188 | if (!session) { |
| 189 | ERRARG("session"); |
| 190 | return; |
| 191 | } else if (!status) { |
| 192 | ERRARG("status"); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | session->status = status; |
| 197 | } |
| 198 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 199 | int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 200 | nc_sock_listen_inet(const char *address, uint16_t port, struct nc_keepalives *ka) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 201 | { |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 202 | int opt; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 203 | int is_ipv4, sock; |
| 204 | struct sockaddr_storage saddr; |
| 205 | |
| 206 | struct sockaddr_in *saddr4; |
| 207 | struct sockaddr_in6 *saddr6; |
| 208 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 209 | if (!strchr(address, ':')) { |
| 210 | is_ipv4 = 1; |
| 211 | } else { |
| 212 | is_ipv4 = 0; |
| 213 | } |
| 214 | |
| 215 | sock = socket((is_ipv4 ? AF_INET : AF_INET6), SOCK_STREAM, 0); |
| 216 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 217 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 218 | goto fail; |
| 219 | } |
| 220 | |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 221 | /* these options will be inherited by accepted sockets */ |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 222 | opt = 1; |
| 223 | if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 224 | ERR(NULL, "Could not set SO_REUSEADDR socket option (%s).", strerror(errno)); |
Michal Vasko | 06c860d | 2018-07-09 16:08:52 +0200 | [diff] [blame] | 225 | goto fail; |
| 226 | } |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 227 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 228 | ERR(NULL, "Could not set TCP_NODELAY socket option (%s).", strerror(errno)); |
Michal Vasko | 83ad17e | 2019-01-30 10:11:37 +0100 | [diff] [blame] | 229 | goto fail; |
| 230 | } |
Michal Vasko | be52dc2 | 2018-10-17 09:28:17 +0200 | [diff] [blame] | 231 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 232 | if (nc_sock_enable_keepalive(sock, ka)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 233 | goto fail; |
| 234 | } |
| 235 | |
Michal Vasko | f22d5ff | 2020-04-15 11:10:27 +0200 | [diff] [blame] | 236 | memset(&saddr, 0, sizeof(struct sockaddr_storage)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 237 | if (is_ipv4) { |
| 238 | saddr4 = (struct sockaddr_in *)&saddr; |
| 239 | |
| 240 | saddr4->sin_family = AF_INET; |
| 241 | saddr4->sin_port = htons(port); |
| 242 | |
| 243 | if (inet_pton(AF_INET, address, &saddr4->sin_addr) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 244 | ERR(NULL, "Failed to convert IPv4 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 245 | goto fail; |
| 246 | } |
| 247 | |
| 248 | if (bind(sock, (struct sockaddr *)saddr4, sizeof(struct sockaddr_in)) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 249 | ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 250 | goto fail; |
| 251 | } |
| 252 | |
| 253 | } else { |
| 254 | saddr6 = (struct sockaddr_in6 *)&saddr; |
| 255 | |
| 256 | saddr6->sin6_family = AF_INET6; |
| 257 | saddr6->sin6_port = htons(port); |
| 258 | |
| 259 | if (inet_pton(AF_INET6, address, &saddr6->sin6_addr) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 260 | ERR(NULL, "Failed to convert IPv6 address \"%s\".", address); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 261 | goto fail; |
| 262 | } |
| 263 | |
| 264 | if (bind(sock, (struct sockaddr *)saddr6, sizeof(struct sockaddr_in6)) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 265 | ERR(NULL, "Could not bind \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 266 | goto fail; |
| 267 | } |
| 268 | } |
| 269 | |
Michal Vasko | fb89d77 | 2016-01-08 12:25:35 +0100 | [diff] [blame] | 270 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 271 | ERR(NULL, "Unable to start listening on \"%s\" port %d (%s).", address, port, strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 272 | goto fail; |
| 273 | } |
| 274 | |
| 275 | return sock; |
| 276 | |
| 277 | fail: |
| 278 | if (sock > -1) { |
| 279 | close(sock); |
| 280 | } |
| 281 | |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | int |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 286 | nc_sock_listen_unix(const struct nc_server_unix_opts *opts) |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 287 | { |
| 288 | struct sockaddr_un sun; |
| 289 | int sock = -1; |
| 290 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 291 | if (strlen(opts->address) > sizeof(sun.sun_path) - 1) { |
| 292 | ERR(NULL, "Socket path \"%s\" is longer than maximum length %d.", opts->address, (int)(sizeof(sun.sun_path) - 1)); |
Michal Vasko | 93e96f1 | 2021-09-30 10:02:09 +0200 | [diff] [blame] | 293 | goto fail; |
| 294 | } |
| 295 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 296 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 297 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 298 | ERR(NULL, "Failed to create socket (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 299 | goto fail; |
| 300 | } |
| 301 | |
| 302 | memset(&sun, 0, sizeof(sun)); |
| 303 | sun.sun_family = AF_UNIX; |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 304 | snprintf(sun.sun_path, sizeof(sun.sun_path) - 1, "%s", opts->address); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 305 | |
| 306 | unlink(sun.sun_path); |
| 307 | if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) { |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 308 | ERR(NULL, "Could not bind \"%s\" (%s).", opts->address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 309 | goto fail; |
| 310 | } |
| 311 | |
| 312 | if (opts->mode != (mode_t)-1) { |
| 313 | if (chmod(sun.sun_path, opts->mode) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 314 | ERR(NULL, "Failed to set unix socket permissions (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 315 | goto fail; |
| 316 | } |
| 317 | } |
| 318 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 319 | if ((opts->uid != (uid_t)-1) || (opts->gid != (gid_t)-1)) { |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 320 | if (chown(sun.sun_path, opts->uid, opts->gid) < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 321 | ERR(NULL, "Failed to set unix socket uid/gid (%s).", strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 322 | goto fail; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (listen(sock, NC_REVERSE_QUEUE) == -1) { |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 327 | ERR(NULL, "Unable to start listening on \"%s\" (%s).", opts->address, strerror(errno)); |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 328 | goto fail; |
| 329 | } |
| 330 | |
| 331 | return sock; |
| 332 | |
| 333 | fail: |
| 334 | if (sock > -1) { |
| 335 | close(sock); |
| 336 | } |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 337 | return -1; |
| 338 | } |
| 339 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 340 | /** |
| 341 | * @brief Evaluate socket name for AF_UNIX socket. |
| 342 | * @param[in] acc_sock_fd is file descriptor for the accepted socket (a nonnegative). |
| 343 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 344 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 345 | * @return 0 if the stream socket is unnamed. Parameter host is set to NULL. |
| 346 | * @return -1 in case of error. Parameter host is set to NULL. |
| 347 | */ |
| 348 | static int |
| 349 | sock_host_unix(int acc_sock_fd, char **host) |
| 350 | { |
| 351 | char *sun_path; |
| 352 | struct sockaddr_storage saddr; |
| 353 | socklen_t addr_len; |
| 354 | |
| 355 | *host = NULL; |
| 356 | saddr.ss_family = AF_UNIX; |
| 357 | addr_len = sizeof(saddr); |
| 358 | |
| 359 | if (getsockname(acc_sock_fd, (struct sockaddr *)&saddr, &addr_len)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 360 | ERR(NULL, "getsockname failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | sun_path = ((struct sockaddr_un *)&saddr)->sun_path; |
| 365 | if (!sun_path) { |
| 366 | /* stream socket is unnamed */ |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | if (!(*host = strdup(sun_path))) { |
| 371 | ERRMEM; |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * @brief Evaluate socket name and port number for AF_INET socket. |
| 380 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 381 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 382 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 383 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 384 | * @return -1 in case of error. Parameter host is set to NULL and port is unchanged. |
| 385 | */ |
| 386 | static int |
| 387 | sock_host_inet(const struct sockaddr_in *addr, char **host, uint16_t *port) |
| 388 | { |
| 389 | *host = malloc(INET_ADDRSTRLEN); |
| 390 | if (!(*host)) { |
| 391 | ERRMEM; |
| 392 | return -1; |
| 393 | } |
| 394 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 395 | if (!inet_ntop(AF_INET, &addr->sin_addr, *host, INET_ADDRSTRLEN)) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 396 | ERR(NULL, "inet_ntop failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 397 | free(*host); |
| 398 | *host = NULL; |
| 399 | return -1; |
| 400 | } |
| 401 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 402 | *port = ntohs(addr->sin_port); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @brief Evaluate socket name and port number for AF_INET6 socket. |
| 409 | * @param[in] addr is pointing to structure filled by accept function which was successful. |
| 410 | * @param[out] host is pointer to char* to which the socket name will be set. It must not be NULL. |
| 411 | * @param[out] port is pointer to uint16_t to which the port number will be set. It must not be NULL. |
| 412 | * @return 0 in case of success. Call free function for parameter host to avoid a memory leak. |
| 413 | * @return -1 in case of error. Parameter host is set to the NULL and port is unchanged. |
| 414 | */ |
| 415 | static int |
| 416 | sock_host_inet6(const struct sockaddr_in6 *addr, char **host, uint16_t *port) |
| 417 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 418 | *host = malloc(INET6_ADDRSTRLEN); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 419 | if (!(*host)) { |
| 420 | ERRMEM; |
| 421 | return -1; |
| 422 | } |
| 423 | |
aPiecek | 3da9b34 | 2021-02-18 15:00:03 +0100 | [diff] [blame] | 424 | if (!inet_ntop(AF_INET6, &addr->sin6_addr, *host, INET6_ADDRSTRLEN)) { |
Michal Vasko | 69e9875 | 2022-12-14 14:20:17 +0100 | [diff] [blame] | 425 | ERR(NULL, "inet_ntop failed (%s).", strerror(errno)); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 426 | free(*host); |
| 427 | *host = NULL; |
| 428 | return -1; |
| 429 | } |
| 430 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 431 | *port = ntohs(addr->sin6_port); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 436 | int |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 437 | nc_sock_accept_binds(struct nc_bind *binds, uint16_t bind_count, int timeout, char **host, uint16_t *port, uint16_t *idx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 438 | { |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 439 | sigset_t sigmask, origmask; |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 440 | uint16_t i, j, pfd_count, client_port; |
| 441 | char *client_address; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 442 | struct pollfd *pfd; |
| 443 | struct sockaddr_storage saddr; |
| 444 | socklen_t saddr_len = sizeof(saddr); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 445 | int ret, client_sock, sock = -1, flags; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 446 | |
| 447 | pfd = malloc(bind_count * sizeof *pfd); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 448 | if (!pfd) { |
| 449 | ERRMEM; |
| 450 | return -1; |
| 451 | } |
| 452 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 453 | for (i = 0, pfd_count = 0; i < bind_count; ++i) { |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 454 | if (binds[i].sock < 0) { |
| 455 | /* invalid socket */ |
Michal Vasko | 94acafc | 2016-09-23 13:40:10 +0200 | [diff] [blame] | 456 | continue; |
| 457 | } |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 458 | if (binds[i].pollin) { |
| 459 | binds[i].pollin = 0; |
| 460 | /* leftover pollin */ |
| 461 | sock = binds[i].sock; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 462 | break; |
| 463 | } |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 464 | pfd[pfd_count].fd = binds[i].sock; |
| 465 | pfd[pfd_count].events = POLLIN; |
| 466 | pfd[pfd_count].revents = 0; |
| 467 | |
| 468 | ++pfd_count; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 471 | if (sock == -1) { |
| 472 | /* poll for a new connection */ |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 473 | sigfillset(&sigmask); |
| 474 | pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 475 | ret = poll(pfd, pfd_count, timeout); |
Michal Vasko | f54cd35 | 2017-02-22 13:42:02 +0100 | [diff] [blame] | 476 | pthread_sigmask(SIG_SETMASK, &origmask, NULL); |
| 477 | |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 478 | if (!ret) { |
| 479 | /* we timeouted */ |
| 480 | free(pfd); |
| 481 | return 0; |
| 482 | } else if (ret == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 483 | ERR(NULL, "Poll failed (%s).", strerror(errno)); |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 484 | free(pfd); |
| 485 | return -1; |
| 486 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 487 | |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 488 | for (i = 0, j = 0; j < pfd_count; ++i, ++j) { |
| 489 | /* adjust i so that indices in binds and pfd always match */ |
| 490 | while (binds[i].sock != pfd[j].fd) { |
| 491 | ++i; |
| 492 | } |
| 493 | |
| 494 | if (pfd[j].revents & POLLIN) { |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 495 | --ret; |
| 496 | |
| 497 | if (!ret) { |
| 498 | /* the last socket with an event, use it */ |
Michal Vasko | ac2f618 | 2017-01-30 14:32:03 +0100 | [diff] [blame] | 499 | sock = pfd[j].fd; |
Michal Vasko | 0a3f375 | 2016-10-13 14:58:38 +0200 | [diff] [blame] | 500 | break; |
| 501 | } else { |
| 502 | /* just remember the event for next time */ |
| 503 | binds[i].pollin = 1; |
| 504 | } |
| 505 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | free(pfd); |
| 509 | |
| 510 | if (sock == -1) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 511 | ERRINT; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 512 | return -1; |
| 513 | } |
| 514 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 515 | /* accept connection */ |
| 516 | client_sock = accept(sock, (struct sockaddr *)&saddr, &saddr_len); |
| 517 | if (client_sock < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 518 | ERR(NULL, "Accept failed (%s).", strerror(errno)); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 519 | return -1; |
| 520 | } |
| 521 | |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 522 | /* make the socket non-blocking */ |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 523 | if (((flags = fcntl(client_sock, F_GETFL)) == -1) || (fcntl(client_sock, F_SETFL, flags | O_NONBLOCK) == -1)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 524 | ERR(NULL, "Fcntl failed (%s).", strerror(errno)); |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 525 | goto fail; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 528 | /* learn information about the client end */ |
| 529 | if (saddr.ss_family == AF_UNIX) { |
| 530 | if (sock_host_unix(client_sock, &client_address)) { |
| 531 | goto fail; |
| 532 | } |
| 533 | client_port = 0; |
| 534 | } else if (saddr.ss_family == AF_INET) { |
| 535 | if (sock_host_inet((struct sockaddr_in *)&saddr, &client_address, &client_port)) { |
| 536 | goto fail; |
| 537 | } |
| 538 | } else if (saddr.ss_family == AF_INET6) { |
| 539 | if (sock_host_inet6((struct sockaddr_in6 *)&saddr, &client_address, &client_port)) { |
| 540 | goto fail; |
| 541 | } |
| 542 | } else { |
| 543 | ERR(NULL, "Source host of an unknown protocol family."); |
| 544 | goto fail; |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 545 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 546 | |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 547 | if (saddr.ss_family == AF_UNIX) { |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 548 | VRB(NULL, "Accepted a connection on %s.", binds[i].address); |
aPiecek | 90ff024 | 2021-02-14 14:58:01 +0100 | [diff] [blame] | 549 | } else { |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 550 | VRB(NULL, "Accepted a connection on %s:%u from %s:%u.", binds[i].address, binds[i].port, client_address, client_port); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 551 | } |
| 552 | |
Michal Vasko | 89ffa8a | 2021-06-25 08:40:08 +0200 | [diff] [blame] | 553 | if (host) { |
| 554 | *host = client_address; |
| 555 | } else { |
| 556 | free(client_address); |
| 557 | } |
| 558 | if (port) { |
| 559 | *port = client_port; |
| 560 | } |
| 561 | if (idx) { |
| 562 | *idx = i; |
| 563 | } |
| 564 | return client_sock; |
| 565 | |
| 566 | fail: |
| 567 | close(client_sock); |
| 568 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 571 | API struct nc_server_reply * |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 572 | nc_clb_default_get_schema(struct lyd_node *rpc, struct nc_session *session) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 573 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 574 | const char *identifier = NULL, *revision = NULL, *format = NULL; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 575 | char *model_data = NULL; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 576 | struct ly_out *out; |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 577 | const struct lys_module *module = NULL, *mod; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 578 | const struct lysp_submodule *submodule = NULL; |
| 579 | struct lyd_node *child, *err, *data = NULL; |
| 580 | LYS_OUTFORMAT outformat = 0; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 581 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 582 | LY_LIST_FOR(lyd_child(rpc), child) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 583 | if (!strcmp(child->schema->name, "identifier")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 584 | identifier = lyd_get_value(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 585 | } else if (!strcmp(child->schema->name, "version")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 586 | revision = lyd_get_value(child); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 587 | if (revision && (revision[0] == '\0')) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 588 | revision = NULL; |
Radek Krejci | 1afa779 | 2017-03-26 11:24:16 -0500 | [diff] [blame] | 589 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 590 | } else if (!strcmp(child->schema->name, "format")) { |
Michal Vasko | e97b10a | 2021-04-28 08:52:52 +0200 | [diff] [blame] | 591 | format = lyd_get_value(child); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 592 | } |
| 593 | } |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 594 | VRB(session, "Module \"%s@%s\" was requested.", identifier, revision ? revision : "<any>"); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 595 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 596 | /* check revision */ |
| 597 | if (revision && (strlen(revision) != 10) && strcmp(revision, "1.0")) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 598 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 599 | nc_err_set_msg(err, "The requested version is not supported.", "en"); |
| 600 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 601 | } |
| 602 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 603 | if (revision) { |
| 604 | /* get specific module */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 605 | module = ly_ctx_get_module(session->ctx, identifier, revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 606 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 607 | submodule = ly_ctx_get_submodule(session->ctx, identifier, revision); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 608 | } |
| 609 | } else { |
| 610 | /* try to get implemented, then latest module */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 611 | module = ly_ctx_get_module_implemented(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 612 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 613 | module = ly_ctx_get_module_latest(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 614 | } |
| 615 | if (!module) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 616 | submodule = ly_ctx_get_submodule_latest(session->ctx, identifier); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 617 | } |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 618 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 619 | if (!module && !submodule) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 620 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 5ca5d97 | 2022-09-14 13:51:31 +0200 | [diff] [blame] | 621 | nc_err_set_msg(err, "The requested module was not found.", "en"); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 622 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* check format */ |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 626 | if (!format || !strcmp(format, "ietf-netconf-monitoring:yang")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 627 | outformat = LYS_OUT_YANG; |
Radek Krejci | 90fba64 | 2016-12-07 15:59:45 +0100 | [diff] [blame] | 628 | } else if (!strcmp(format, "ietf-netconf-monitoring:yin")) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 629 | outformat = LYS_OUT_YIN; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 630 | } else { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 631 | err = nc_err(session->ctx, NC_ERR_INVALID_VALUE, NC_ERR_TYPE_APP); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 632 | nc_err_set_msg(err, "The requested format is not supported.", "en"); |
| 633 | return nc_server_reply_err(err); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 634 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 635 | |
| 636 | /* print */ |
| 637 | ly_out_new_memory(&model_data, 0, &out); |
| 638 | if (module) { |
| 639 | lys_print_module(out, module, outformat, 0, 0); |
| 640 | } else { |
| 641 | lys_print_submodule(out, submodule, outformat, 0, 0); |
| 642 | } |
| 643 | ly_out_free(out, NULL, 0); |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 644 | if (!model_data) { |
| 645 | ERRINT; |
| 646 | return NULL; |
| 647 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 648 | |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 649 | /* create reply */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 650 | mod = ly_ctx_get_module_implemented(session->ctx, "ietf-netconf-monitoring"); |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 651 | if (!mod || lyd_new_inner(NULL, mod, "get-schema", 0, &data)) { |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 652 | ERRINT; |
Michal Vasko | d91f6e6 | 2016-04-05 11:34:22 +0200 | [diff] [blame] | 653 | free(model_data); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 654 | return NULL; |
| 655 | } |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 656 | if (lyd_new_any(data, NULL, "data", model_data, 1, LYD_ANYDATA_STRING, 1, NULL)) { |
| 657 | ERRINT; |
Michal Vasko | a50f68e | 2022-02-24 16:10:54 +0100 | [diff] [blame] | 658 | free(model_data); |
Michal Vasko | 9b1a952 | 2021-03-15 16:24:26 +0100 | [diff] [blame] | 659 | lyd_free_tree(data); |
| 660 | return NULL; |
| 661 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 662 | |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 663 | return nc_server_reply_data(data, NC_WD_EXPLICIT, NC_PARAMTYPE_FREE); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 666 | API struct nc_server_reply * |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 667 | nc_clb_default_close_session(struct lyd_node *UNUSED(rpc), struct nc_session *session) |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 668 | { |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 669 | session->term_reason = NC_SESSION_TERM_CLOSED; |
| 670 | return nc_server_reply_ok(); |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 673 | /** |
| 674 | * @brief Initialize a context with default RPC callbacks if none are set. |
| 675 | * |
| 676 | * @param[in] ctx Context to initialize. |
| 677 | */ |
| 678 | static void |
| 679 | nc_server_init_ctx(const struct ly_ctx *ctx) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 680 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 681 | struct lysc_node *rpc; |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 682 | |
Michal Vasko | 238b6c1 | 2021-12-14 15:14:09 +0100 | [diff] [blame] | 683 | if (global_rpc_clb) { |
| 684 | /* expect it to handle these RPCs as well */ |
| 685 | return; |
| 686 | } |
| 687 | |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 688 | /* set default <get-schema> callback if not specified */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 689 | rpc = NULL; |
| 690 | if (ly_ctx_get_module_implemented(ctx, "ietf-netconf-monitoring")) { |
| 691 | rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf-monitoring:get-schema", 0); |
| 692 | } |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 693 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 694 | rpc->priv = nc_clb_default_get_schema; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 695 | } |
| 696 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 697 | /* set default <close-session> callback if not specified */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 698 | rpc = (struct lysc_node *)lys_find_path(ctx, NULL, "/ietf-netconf:close-session", 0); |
Michal Vasko | 88639e9 | 2017-08-03 14:38:10 +0200 | [diff] [blame] | 699 | if (rpc && !rpc->priv) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 700 | rpc->priv = nc_clb_default_close_session; |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 701 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 702 | } |
Michal Vasko | 05ba9df | 2016-01-13 14:40:27 +0100 | [diff] [blame] | 703 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 704 | API int |
| 705 | nc_server_init(void) |
| 706 | { |
| 707 | pthread_rwlockattr_t attr, *attr_p = NULL; |
| 708 | int r; |
| 709 | |
| 710 | nc_init(); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 711 | |
| 712 | server_opts.new_session_id = 1; |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 713 | server_opts.new_client_id = 1; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 714 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 715 | #ifdef HAVE_PTHREAD_RWLOCKATTR_SETKIND_NP |
| 716 | if ((r = pthread_rwlockattr_init(&attr))) { |
| 717 | ERR(NULL, "%s: failed init attribute (%s).", __func__, strerror(r)); |
| 718 | goto error; |
| 719 | } |
| 720 | attr_p = &attr; |
| 721 | if ((r = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP))) { |
| 722 | ERR(NULL, "%s: failed set attribute (%s).", __func__, strerror(r)); |
| 723 | goto error; |
| 724 | } |
Rosen Penev | ef2f3ac | 2019-07-15 18:15:28 -0700 | [diff] [blame] | 725 | #endif |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 726 | |
| 727 | if ((r = pthread_rwlock_init(&server_opts.endpt_lock, attr_p))) { |
| 728 | ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r)); |
| 729 | goto error; |
| 730 | } |
| 731 | if ((r = pthread_rwlock_init(&server_opts.ch_client_lock, attr_p))) { |
| 732 | ERR(NULL, "%s: failed to init rwlock(%s).", __func__, strerror(r)); |
| 733 | goto error; |
| 734 | } |
| 735 | |
| 736 | if (attr_p) { |
| 737 | pthread_rwlockattr_destroy(attr_p); |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 738 | } |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 739 | return 0; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 740 | |
| 741 | error: |
| 742 | if (attr_p) { |
| 743 | pthread_rwlockattr_destroy(attr_p); |
| 744 | } |
| 745 | return -1; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 748 | API void |
| 749 | nc_server_destroy(void) |
| 750 | { |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 751 | uint32_t i; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 752 | |
| 753 | for (i = 0; i < server_opts.capabilities_count; i++) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 754 | free(server_opts.capabilities[i]); |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 755 | } |
| 756 | free(server_opts.capabilities); |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 757 | server_opts.capabilities = NULL; |
| 758 | server_opts.capabilities_count = 0; |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 759 | if (server_opts.content_id_data && server_opts.content_id_data_free) { |
| 760 | server_opts.content_id_data_free(server_opts.content_id_data); |
| 761 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 762 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 763 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 764 | nc_server_del_endpt(NULL, 0); |
Michal Vasko | 0bdf70b | 2019-06-24 19:20:20 +0200 | [diff] [blame] | 765 | nc_server_ch_del_client(NULL); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 766 | #endif |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 767 | #ifdef NC_ENABLED_SSH |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 768 | if (server_opts.passwd_auth_data && server_opts.passwd_auth_data_free) { |
| 769 | server_opts.passwd_auth_data_free(server_opts.passwd_auth_data); |
| 770 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 771 | server_opts.passwd_auth_data = NULL; |
| 772 | server_opts.passwd_auth_data_free = NULL; |
Michal Vasko | ebba760 | 2018-03-23 13:14:08 +0100 | [diff] [blame] | 773 | |
Michal Vasko | 1c2d265 | 2023-10-17 08:53:36 +0200 | [diff] [blame] | 774 | if (server_opts.pubkey_auth_data && server_opts.pubkey_auth_data_free) { |
| 775 | server_opts.pubkey_auth_data_free(server_opts.pubkey_auth_data); |
| 776 | } |
| 777 | server_opts.pubkey_auth_data = NULL; |
| 778 | server_opts.pubkey_auth_data_free = NULL; |
| 779 | |
| 780 | if (server_opts.interactive_auth_sess_data && server_opts.interactive_auth_sess_data_free) { |
| 781 | server_opts.interactive_auth_sess_data_free(server_opts.interactive_auth_sess_data); |
| 782 | } |
| 783 | server_opts.interactive_auth_sess_data = NULL; |
| 784 | server_opts.interactive_auth_sess_data_free = NULL; |
| 785 | |
| 786 | if (server_opts.interactive_auth_data && server_opts.interactive_auth_data_free) { |
| 787 | server_opts.interactive_auth_data_free(server_opts.interactive_auth_data); |
| 788 | } |
| 789 | server_opts.interactive_auth_data = NULL; |
| 790 | server_opts.interactive_auth_data_free = NULL; |
| 791 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 792 | nc_server_ssh_del_authkey(NULL, NULL, 0, NULL); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 793 | |
| 794 | if (server_opts.hostkey_data && server_opts.hostkey_data_free) { |
| 795 | server_opts.hostkey_data_free(server_opts.hostkey_data); |
| 796 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 797 | server_opts.hostkey_data = NULL; |
| 798 | server_opts.hostkey_data_free = NULL; |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 799 | |
| 800 | /* PAM */ |
| 801 | free(server_opts.conf_name); |
| 802 | free(server_opts.conf_dir); |
| 803 | server_opts.conf_name = NULL; |
| 804 | server_opts.conf_dir = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 805 | #endif |
| 806 | #ifdef NC_ENABLED_TLS |
| 807 | if (server_opts.server_cert_data && server_opts.server_cert_data_free) { |
| 808 | server_opts.server_cert_data_free(server_opts.server_cert_data); |
| 809 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 810 | server_opts.server_cert_data = NULL; |
| 811 | server_opts.server_cert_data_free = NULL; |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 812 | if (server_opts.trusted_cert_list_data && server_opts.trusted_cert_list_data_free) { |
| 813 | server_opts.trusted_cert_list_data_free(server_opts.trusted_cert_list_data); |
| 814 | } |
Michal Vasko | dd6e4f7 | 2018-06-01 10:21:27 +0200 | [diff] [blame] | 815 | server_opts.trusted_cert_list_data = NULL; |
| 816 | server_opts.trusted_cert_list_data_free = NULL; |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 817 | #endif |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 818 | nc_destroy(); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 821 | API int |
| 822 | nc_server_set_capab_withdefaults(NC_WD_MODE basic_mode, int also_supported) |
| 823 | { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 824 | if (!basic_mode || (basic_mode == NC_WD_ALL_TAG)) { |
| 825 | ERRARG("basic_mode"); |
| 826 | return -1; |
| 827 | } else if (also_supported && !(also_supported & (NC_WD_ALL | NC_WD_ALL_TAG | NC_WD_TRIM | NC_WD_EXPLICIT))) { |
| 828 | ERRARG("also_supported"); |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 829 | return -1; |
| 830 | } |
| 831 | |
| 832 | server_opts.wd_basic_mode = basic_mode; |
| 833 | server_opts.wd_also_supported = also_supported; |
| 834 | return 0; |
| 835 | } |
| 836 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 837 | API void |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 838 | nc_server_get_capab_withdefaults(NC_WD_MODE *basic_mode, int *also_supported) |
| 839 | { |
| 840 | if (!basic_mode && !also_supported) { |
| 841 | ERRARG("basic_mode and also_supported"); |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | if (basic_mode) { |
| 846 | *basic_mode = server_opts.wd_basic_mode; |
| 847 | } |
| 848 | if (also_supported) { |
| 849 | *also_supported = server_opts.wd_also_supported; |
| 850 | } |
| 851 | } |
| 852 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 853 | API int |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 854 | nc_server_set_capability(const char *value) |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 855 | { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 856 | void *mem; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 857 | |
| 858 | if (!value || !value[0]) { |
| 859 | ERRARG("value must not be empty"); |
| 860 | return EXIT_FAILURE; |
| 861 | } |
| 862 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 863 | mem = realloc(server_opts.capabilities, (server_opts.capabilities_count + 1) * sizeof *server_opts.capabilities); |
| 864 | if (!mem) { |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 865 | ERRMEM; |
| 866 | return EXIT_FAILURE; |
| 867 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 868 | server_opts.capabilities = mem; |
| 869 | |
| 870 | server_opts.capabilities[server_opts.capabilities_count] = strdup(value); |
| 871 | server_opts.capabilities_count++; |
Radek Krejci | 658782b | 2016-12-04 22:04:55 +0100 | [diff] [blame] | 872 | |
| 873 | return EXIT_SUCCESS; |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 874 | } |
| 875 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 876 | API void |
Michal Vasko | 1440a74 | 2021-03-31 11:11:03 +0200 | [diff] [blame] | 877 | nc_server_set_content_id_clb(char *(*content_id_clb)(void *user_data), void *user_data, |
| 878 | void (*free_user_data)(void *user_data)) |
| 879 | { |
| 880 | server_opts.content_id_clb = content_id_clb; |
| 881 | server_opts.content_id_data = user_data; |
| 882 | server_opts.content_id_data_free = free_user_data; |
| 883 | } |
| 884 | |
| 885 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 886 | nc_server_set_hello_timeout(uint16_t hello_timeout) |
| 887 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 888 | server_opts.hello_timeout = hello_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 889 | } |
| 890 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 891 | API uint16_t |
| 892 | nc_server_get_hello_timeout(void) |
| 893 | { |
| 894 | return server_opts.hello_timeout; |
| 895 | } |
| 896 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 897 | API void |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 898 | nc_server_set_idle_timeout(uint16_t idle_timeout) |
| 899 | { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 900 | server_opts.idle_timeout = idle_timeout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 901 | } |
| 902 | |
Michal Vasko | 55f0397 | 2016-04-13 08:56:01 +0200 | [diff] [blame] | 903 | API uint16_t |
| 904 | nc_server_get_idle_timeout(void) |
| 905 | { |
| 906 | return server_opts.idle_timeout; |
| 907 | } |
| 908 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 909 | API NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 910 | nc_accept_inout(int fdin, int fdout, const char *username, const struct ly_ctx *ctx, struct nc_session **session) |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 911 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 912 | NC_MSG_TYPE msgtype; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 913 | struct timespec ts_cur; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 914 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 915 | if (!ctx) { |
| 916 | ERRARG("ctx"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 917 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 918 | } else if (fdin < 0) { |
| 919 | ERRARG("fdin"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 920 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 921 | } else if (fdout < 0) { |
| 922 | ERRARG("fdout"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 923 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 924 | } else if (!username) { |
| 925 | ERRARG("username"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 926 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 927 | } else if (!session) { |
| 928 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 929 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 930 | } |
| 931 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 932 | /* init ctx as needed */ |
| 933 | nc_server_init_ctx(ctx); |
| 934 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 935 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 936 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 937 | if (!(*session)) { |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 938 | ERRMEM; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 939 | return NC_MSG_ERROR; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 940 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 941 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 942 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 943 | /* transport specific data */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 944 | (*session)->ti_type = NC_TI_FD; |
| 945 | (*session)->ti.fd.in = fdin; |
| 946 | (*session)->ti.fd.out = fdout; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 947 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 948 | /* assign context */ |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 949 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 950 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 951 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 952 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 953 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 954 | |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 955 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 956 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 957 | if (msgtype != NC_MSG_HELLO) { |
| 958 | nc_session_free(*session, NULL); |
| 959 | *session = NULL; |
| 960 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 961 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 962 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 963 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 964 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 965 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 966 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
| 967 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 968 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 969 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 970 | return msgtype; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 971 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 972 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 973 | static void |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 974 | nc_ps_queue_add_id(struct nc_pollsession *ps, uint8_t *id) |
| 975 | { |
| 976 | uint8_t q_last; |
| 977 | |
| 978 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
| 979 | ERRINT; |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | /* get a unique queue value (by adding 1 to the last added value, if any) */ |
| 984 | if (ps->queue_len) { |
| 985 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 986 | *id = ps->queue[q_last] + 1; |
| 987 | } else { |
| 988 | *id = 0; |
| 989 | } |
| 990 | |
| 991 | /* add the id into the queue */ |
| 992 | ++ps->queue_len; |
| 993 | q_last = (ps->queue_begin + ps->queue_len - 1) % NC_PS_QUEUE_SIZE; |
| 994 | ps->queue[q_last] = *id; |
| 995 | } |
| 996 | |
| 997 | static void |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 998 | nc_ps_queue_remove_id(struct nc_pollsession *ps, uint8_t id) |
| 999 | { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1000 | uint8_t i, q_idx, found = 0; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1001 | |
| 1002 | for (i = 0; i < ps->queue_len; ++i) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1003 | /* get the actual queue idx */ |
| 1004 | q_idx = (ps->queue_begin + i) % NC_PS_QUEUE_SIZE; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1005 | |
| 1006 | if (found) { |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1007 | if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1008 | /* another equal value, simply cannot be */ |
| 1009 | ERRINT; |
| 1010 | } |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1011 | if (found == 2) { |
| 1012 | /* move the following values */ |
| 1013 | ps->queue[q_idx ? q_idx - 1 : NC_PS_QUEUE_SIZE - 1] = ps->queue[q_idx]; |
| 1014 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1015 | } else if (ps->queue[q_idx] == id) { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1016 | /* found our id, there can be no more equal valid values */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1017 | if (i == 0) { |
| 1018 | found = 1; |
| 1019 | } else { |
| 1020 | /* this is not okay, our id is in the middle of the queue */ |
| 1021 | found = 2; |
| 1022 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1023 | } |
| 1024 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1025 | if (!found) { |
| 1026 | ERRINT; |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1027 | return; |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1028 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1029 | |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1030 | --ps->queue_len; |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1031 | if (found == 1) { |
Michal Vasko | 103fe63 | 2018-02-12 16:37:45 +0100 | [diff] [blame] | 1032 | /* remove the id by moving the queue, otherwise all the values in the queue were moved */ |
Michal Vasko | d834003 | 2018-02-12 14:41:00 +0100 | [diff] [blame] | 1033 | ps->queue_begin = (ps->queue_begin + 1) % NC_PS_QUEUE_SIZE; |
| 1034 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1037 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1038 | nc_ps_lock(struct nc_pollsession *ps, uint8_t *id, const char *func) |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1039 | { |
| 1040 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1041 | struct timespec ts; |
| 1042 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1043 | /* LOCK */ |
Michal Vasko | 8c7def5 | 2023-06-06 14:48:56 +0200 | [diff] [blame] | 1044 | ret = pthread_mutex_lock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1045 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1046 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1047 | return -1; |
| 1048 | } |
| 1049 | |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1050 | /* check that the queue is long enough */ |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 1051 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1052 | ERR(NULL, "%s: pollsession queue size (%d) too small.", func, NC_PS_QUEUE_SIZE); |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 1053 | pthread_mutex_unlock(&ps->lock); |
| 1054 | return -1; |
| 1055 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1056 | |
| 1057 | /* add ourselves into the queue */ |
| 1058 | nc_ps_queue_add_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1059 | DBL(NULL, "PS 0x%p TID %lu queue: added %u, head %u, length %u", ps, (long unsigned int)pthread_self(), *id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 1060 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1061 | |
| 1062 | /* is it our turn? */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1063 | while (ps->queue[ps->queue_begin] != *id) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1064 | nc_timeouttime_get(&ts, NC_PS_QUEUE_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1065 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1066 | ret = pthread_cond_clockwait(&ps->cond, &ps->lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1067 | if (ret) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1068 | /** |
| 1069 | * This may happen when another thread releases the lock and broadcasts the condition |
| 1070 | * and this thread had already timed out. When this thread is scheduled, it returns timed out error |
| 1071 | * but when actually this thread was ready for condition. |
| 1072 | */ |
preetbhansali | 629dfc4 | 2018-12-17 16:04:40 +0530 | [diff] [blame] | 1073 | if ((ETIMEDOUT == ret) && (ps->queue[ps->queue_begin] == *id)) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1074 | break; |
| 1075 | } |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1076 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1077 | ERR(NULL, "%s: failed to wait for a pollsession condition (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1078 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1079 | nc_ps_queue_remove_id(ps, *id); |
| 1080 | pthread_mutex_unlock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1081 | return -1; |
| 1082 | } |
| 1083 | } |
| 1084 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1085 | /* UNLOCK */ |
| 1086 | pthread_mutex_unlock(&ps->lock); |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1091 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1092 | nc_ps_unlock(struct nc_pollsession *ps, uint8_t id, const char *func) |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1093 | { |
| 1094 | int ret; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1095 | |
| 1096 | /* LOCK */ |
Michal Vasko | 8c7def5 | 2023-06-06 14:48:56 +0200 | [diff] [blame] | 1097 | ret = pthread_mutex_lock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1098 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1099 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1100 | ret = -1; |
| 1101 | } |
| 1102 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1103 | /* we must be the first, it was our turn after all, right? */ |
| 1104 | if (ps->queue[ps->queue_begin] != id) { |
| 1105 | ERRINT; |
Michal Vasko | b1a094b | 2016-10-05 14:04:52 +0200 | [diff] [blame] | 1106 | /* UNLOCK */ |
| 1107 | if (!ret) { |
| 1108 | pthread_mutex_unlock(&ps->lock); |
| 1109 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1110 | return -1; |
| 1111 | } |
| 1112 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1113 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1114 | nc_ps_queue_remove_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1115 | DBL(NULL, "PS 0x%p TID %lu queue: removed %u, head %u, length %u", ps, (long unsigned int)pthread_self(), id, |
Michal Vasko | 9129095 | 2019-09-27 11:30:55 +0200 | [diff] [blame] | 1116 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1117 | |
| 1118 | /* broadcast to all other threads that the queue moved */ |
| 1119 | pthread_cond_broadcast(&ps->cond); |
| 1120 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1121 | /* UNLOCK */ |
| 1122 | if (!ret) { |
| 1123 | pthread_mutex_unlock(&ps->lock); |
| 1124 | } |
| 1125 | |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1129 | API struct nc_pollsession * |
| 1130 | nc_ps_new(void) |
| 1131 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1132 | struct nc_pollsession *ps; |
| 1133 | |
| 1134 | ps = calloc(1, sizeof(struct nc_pollsession)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1135 | if (!ps) { |
| 1136 | ERRMEM; |
| 1137 | return NULL; |
| 1138 | } |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1139 | pthread_cond_init(&ps->cond, NULL); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1140 | pthread_mutex_init(&ps->lock, NULL); |
| 1141 | |
| 1142 | return ps; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | API void |
| 1146 | nc_ps_free(struct nc_pollsession *ps) |
| 1147 | { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1148 | uint16_t i; |
| 1149 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1150 | if (!ps) { |
| 1151 | return; |
| 1152 | } |
| 1153 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1154 | if (ps->queue_len) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1155 | ERR(NULL, "FATAL: Freeing a pollsession structure that is currently being worked with!"); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1158 | for (i = 0; i < ps->session_count; i++) { |
| 1159 | free(ps->sessions[i]); |
| 1160 | } |
| 1161 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1162 | free(ps->sessions); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1163 | pthread_mutex_destroy(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1164 | pthread_cond_destroy(&ps->cond); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1165 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1166 | free(ps); |
| 1167 | } |
| 1168 | |
| 1169 | API int |
| 1170 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1171 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1172 | uint8_t q_id; |
| 1173 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1174 | if (!ps) { |
| 1175 | ERRARG("ps"); |
| 1176 | return -1; |
| 1177 | } else if (!session) { |
| 1178 | ERRARG("session"); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1179 | return -1; |
| 1180 | } |
| 1181 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1182 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1183 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1184 | return -1; |
| 1185 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1186 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1187 | ++ps->session_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1188 | ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1189 | if (!ps->sessions) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1190 | ERRMEM; |
| 1191 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1192 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1193 | return -1; |
| 1194 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1195 | ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions); |
| 1196 | if (!ps->sessions[ps->session_count - 1]) { |
| 1197 | ERRMEM; |
| 1198 | --ps->session_count; |
| 1199 | /* UNLOCK */ |
| 1200 | nc_ps_unlock(ps, q_id, __func__); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | ps->sessions[ps->session_count - 1]->session = session; |
| 1204 | ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1205 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1206 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1207 | return nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1210 | static int |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1211 | _nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session, int index) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1212 | { |
| 1213 | uint16_t i; |
| 1214 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1215 | if (index >= 0) { |
| 1216 | i = (uint16_t)index; |
| 1217 | goto remove; |
| 1218 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1219 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1220 | if (ps->sessions[i]->session == session) { |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1221 | remove: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1222 | --ps->session_count; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1223 | if (i <= ps->session_count) { |
| 1224 | free(ps->sessions[i]); |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1225 | ps->sessions[i] = ps->sessions[ps->session_count]; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1226 | } |
| 1227 | if (!ps->session_count) { |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1228 | free(ps->sessions); |
| 1229 | ps->sessions = NULL; |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1230 | } |
Michal Vasko | 11d2f6a | 2017-02-02 11:15:06 +0100 | [diff] [blame] | 1231 | ps->last_event_session = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1232 | return 0; |
| 1233 | } |
| 1234 | } |
| 1235 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 1236 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1239 | API int |
| 1240 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1241 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1242 | uint8_t q_id; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1243 | int ret, ret2; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1244 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1245 | if (!ps) { |
| 1246 | ERRARG("ps"); |
| 1247 | return -1; |
| 1248 | } else if (!session) { |
| 1249 | ERRARG("session"); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1250 | return -1; |
| 1251 | } |
| 1252 | |
| 1253 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1254 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1255 | return -1; |
| 1256 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1257 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1258 | ret = _nc_ps_del_session(ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1259 | |
| 1260 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1261 | ret2 = nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1262 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1263 | return ret || ret2 ? -1 : 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1266 | API struct nc_session * |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1267 | nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx) |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1268 | { |
| 1269 | uint8_t q_id; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1270 | struct nc_session *ret = NULL; |
| 1271 | |
| 1272 | if (!ps) { |
| 1273 | ERRARG("ps"); |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | |
| 1277 | /* LOCK */ |
| 1278 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1282 | if (idx < ps->session_count) { |
| 1283 | ret = ps->sessions[idx]->session; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | /* UNLOCK */ |
| 1287 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1288 | |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
Michal Vasko | 3ec3b11 | 2022-07-21 12:32:33 +0200 | [diff] [blame] | 1292 | API struct nc_session * |
| 1293 | nc_ps_find_session(const struct nc_pollsession *ps, nc_ps_session_match_cb match_cb, void *cb_data) |
| 1294 | { |
| 1295 | uint8_t q_id; |
| 1296 | uint16_t i; |
| 1297 | struct nc_session *ret = NULL; |
| 1298 | |
| 1299 | if (!ps) { |
| 1300 | ERRARG("ps"); |
| 1301 | return NULL; |
| 1302 | } |
| 1303 | |
| 1304 | /* LOCK */ |
| 1305 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | |
| 1309 | for (i = 0; i < ps->session_count; ++i) { |
| 1310 | if (match_cb(ps->sessions[i]->session, cb_data)) { |
| 1311 | ret = ps->sessions[i]->session; |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* UNLOCK */ |
| 1317 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1318 | |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1322 | API uint16_t |
| 1323 | nc_ps_session_count(struct nc_pollsession *ps) |
| 1324 | { |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1325 | uint8_t q_id; |
| 1326 | uint16_t session_count; |
| 1327 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1328 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1329 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1330 | return 0; |
| 1331 | } |
| 1332 | |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1333 | /* LOCK (just for memory barrier so that we read the current value) */ |
| 1334 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | session_count = ps->session_count; |
| 1339 | |
| 1340 | /* UNLOCK */ |
| 1341 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1342 | |
| 1343 | return session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1344 | } |
| 1345 | |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1346 | static NC_MSG_TYPE |
| 1347 | recv_rpc_check_msgid(struct nc_session *session, const struct lyd_node *envp) |
| 1348 | { |
| 1349 | struct lyd_attr *attr; |
| 1350 | |
| 1351 | assert(envp && !envp->schema); |
| 1352 | |
| 1353 | /* find the message-id attribute */ |
| 1354 | LY_LIST_FOR(((struct lyd_node_opaq *)envp)->attr, attr) { |
| 1355 | if (!strcmp(attr->name.name, "message-id")) { |
| 1356 | break; |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | if (!attr) { |
| 1361 | ERR(session, "Received an <rpc> without a message-id."); |
| 1362 | return NC_MSG_REPLY_ERR_MSGID; |
| 1363 | } |
| 1364 | |
| 1365 | return NC_MSG_RPC; |
| 1366 | } |
| 1367 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1368 | /* should be called holding the session RPC lock! IO lock will be acquired as needed |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1369 | * returns: NC_PSPOLL_ERROR, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1370 | * NC_PSPOLL_TIMEOUT, |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1371 | * NC_PSPOLL_BAD_RPC (| NC_PSPOLL_REPLY_ERROR), |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1372 | * NC_PSPOLL_RPC |
| 1373 | */ |
| 1374 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1375 | nc_server_recv_rpc_io(struct nc_session *session, int io_timeout, struct nc_server_rpc **rpc) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1376 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1377 | struct ly_in *msg; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1378 | struct nc_server_reply *reply = NULL; |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1379 | struct lyd_node *e; |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1380 | int r, ret = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1381 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1382 | if (!session) { |
| 1383 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1384 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1385 | } else if (!rpc) { |
| 1386 | ERRARG("rpc"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1387 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1388 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1389 | ERR(session, "Invalid session to receive RPCs."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1390 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1391 | } |
| 1392 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1393 | *rpc = NULL; |
| 1394 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1395 | /* get a message */ |
| 1396 | r = nc_read_msg_io(session, io_timeout, &msg, 0); |
| 1397 | if (r == -2) { |
| 1398 | /* malformed message */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1399 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG)); |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1400 | goto cleanup; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1401 | } |
| 1402 | if (r == -1) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1403 | return NC_PSPOLL_ERROR; |
| 1404 | } else if (!r) { |
| 1405 | return NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1406 | } |
| 1407 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1408 | *rpc = calloc(1, sizeof **rpc); |
| 1409 | if (!*rpc) { |
| 1410 | ERRMEM; |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1411 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1412 | goto cleanup; |
| 1413 | } |
| 1414 | |
| 1415 | /* parse the RPC */ |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1416 | if (!lyd_parse_op(session->ctx, NULL, msg, LYD_XML, LYD_TYPE_RPC_NETCONF, &(*rpc)->envp, &(*rpc)->rpc)) { |
| 1417 | /* check message-id */ |
| 1418 | if (recv_rpc_check_msgid(session, (*rpc)->envp) == NC_MSG_RPC) { |
| 1419 | /* valid RPC */ |
| 1420 | ret = NC_PSPOLL_RPC; |
| 1421 | } else { |
| 1422 | /* no message-id */ |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1423 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MISSING_ATTR, NC_ERR_TYPE_RPC, "message-id", "rpc")); |
| 1424 | } |
| 1425 | } else { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1426 | /* bad RPC received */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1427 | if ((*rpc)->envp) { |
| 1428 | /* at least the envelopes were parsed */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1429 | e = nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 1430 | nc_err_set_msg(e, ly_errmsg(session->ctx), "en"); |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1431 | reply = nc_server_reply_err(e); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1432 | } else if (session->version == NC_VERSION_11) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1433 | /* completely malformed message, NETCONF version 1.1 defines sending error reply from |
| 1434 | * the server (RFC 6241 sec. 3) */ |
| 1435 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_MALFORMED_MSG)); |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1436 | } else { |
| 1437 | /* at least set the return value */ |
| 1438 | ret = NC_PSPOLL_BAD_RPC; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1439 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | cleanup: |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1443 | if (reply) { |
| 1444 | /* send error reply */ |
| 1445 | r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, *rpc ? (*rpc)->envp : NULL, reply); |
| 1446 | nc_server_reply_free(reply); |
| 1447 | if (r != NC_MSG_REPLY) { |
| 1448 | ERR(session, "Failed to write reply (%s), terminating session.", nc_msgtype2str[r]); |
| 1449 | if (session->status != NC_STATUS_INVALID) { |
| 1450 | session->status = NC_STATUS_INVALID; |
| 1451 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1452 | } |
| 1453 | } |
Michal Vasko | f8fba54 | 2023-10-23 12:03:50 +0200 | [diff] [blame] | 1454 | |
| 1455 | /* bad RPC and an error reply sent */ |
| 1456 | ret = NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR; |
Michal Vasko | 77e8357 | 2022-07-21 15:31:15 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1459 | ly_in_free(msg, 1); |
| 1460 | if (ret != NC_PSPOLL_RPC) { |
| 1461 | nc_server_rpc_free(*rpc); |
| 1462 | *rpc = NULL; |
| 1463 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1464 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1467 | API void |
| 1468 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1469 | { |
| 1470 | global_rpc_clb = clb; |
| 1471 | } |
| 1472 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1473 | API NC_MSG_TYPE |
| 1474 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1475 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1476 | NC_MSG_TYPE ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1477 | |
| 1478 | /* check parameters */ |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 1479 | if (!session || (session->side != NC_SERVER) || !nc_session_get_notif_status(session)) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1480 | ERRARG("session"); |
| 1481 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1482 | } else if (!notif || !notif->ntf || !notif->eventtime) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1483 | ERRARG("notif"); |
| 1484 | return NC_MSG_ERROR; |
| 1485 | } |
| 1486 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1487 | /* we do not need RPC lock for this, IO lock will be acquired properly */ |
| 1488 | ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif); |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 1489 | if (ret != NC_MSG_NOTIF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1490 | ERR(session, "Failed to write notification (%s).", nc_msgtype2str[ret]); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1491 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1492 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1493 | return ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Michal Vasko | f946776 | 2023-03-28 09:02:08 +0200 | [diff] [blame] | 1496 | /** |
| 1497 | * @brief Send a reply acquiring IO lock as needed. |
| 1498 | * Session RPC lock must be held! |
| 1499 | * |
| 1500 | * @param[in] session Session to use. |
| 1501 | * @param[in] io_timeout Timeout to use for acquiring IO lock. |
| 1502 | * @param[in] rpc RPC to sent. |
| 1503 | * @return 0 on success. |
| 1504 | * @return Bitmask of NC_PSPOLL_ERROR (any fatal error) and NC_PSPOLL_REPLY_ERROR (reply failed to be sent). |
| 1505 | * @return NC_PSPOLL_ERROR on other errors. |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1506 | */ |
| 1507 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1508 | nc_server_send_reply_io(struct nc_session *session, int io_timeout, const struct nc_server_rpc *rpc) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1509 | { |
| 1510 | nc_rpc_clb clb; |
| 1511 | struct nc_server_reply *reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1512 | const struct lysc_node *rpc_act = NULL; |
| 1513 | struct lyd_node *elem; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1514 | int ret = 0; |
| 1515 | NC_MSG_TYPE r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1516 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1517 | if (!rpc) { |
| 1518 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1519 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1520 | } |
| 1521 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1522 | if (rpc->rpc->schema->nodetype == LYS_RPC) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1523 | /* RPC */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1524 | rpc_act = rpc->rpc->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1525 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1526 | /* action */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1527 | LYD_TREE_DFS_BEGIN(rpc->rpc, elem) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1528 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1529 | rpc_act = elem->schema; |
| 1530 | break; |
| 1531 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1532 | LYD_TREE_DFS_END(rpc->rpc, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1533 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1534 | if (!rpc_act) { |
| 1535 | ERRINT; |
| 1536 | return NC_PSPOLL_ERROR; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | if (!rpc_act->priv) { |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1541 | if (!global_rpc_clb) { |
| 1542 | /* no callback, reply with a not-implemented error */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1543 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_NOT_SUPPORTED, NC_ERR_TYPE_PROT)); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1544 | } else { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1545 | reply = global_rpc_clb(rpc->rpc, session); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1546 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1547 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1548 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1549 | reply = clb(rpc->rpc, session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | if (!reply) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1553 | reply = nc_server_reply_err(nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP)); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1554 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1555 | r = nc_write_msg_io(session, io_timeout, NC_MSG_REPLY, rpc->envp, reply); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1556 | if (reply->type == NC_RPL_ERROR) { |
| 1557 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1558 | } |
| 1559 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1560 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1561 | if (r != NC_MSG_REPLY) { |
Michal Vasko | 1546949 | 2021-06-09 08:40:48 +0200 | [diff] [blame] | 1562 | ERR(session, "Failed to write reply (%s).", nc_msgtype2str[r]); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1563 | ret |= NC_PSPOLL_ERROR; |
| 1564 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1565 | |
| 1566 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1567 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1568 | session->status = NC_STATUS_INVALID; |
| 1569 | } |
| 1570 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1571 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
Michal Vasko | f946776 | 2023-03-28 09:02:08 +0200 | [diff] [blame] | 1574 | /** |
| 1575 | * @brief Poll a session from pspoll acquiring IO lock as needed. |
| 1576 | * Session must be running and session RPC lock held! |
| 1577 | * |
| 1578 | * @param[in] session Session to use. |
| 1579 | * @param[in] io_timeout Timeout to use for acquiring IO lock. |
| 1580 | * @param[in] now_mono Current monotonic timestamp. |
| 1581 | * @param[in,out] msg Message to fill in case of an error. |
| 1582 | * @return NC_PSPOLL_RPC if some application data are available. |
| 1583 | * @return NC_PSPOLL_TIMEOUT if a timeout elapsed. |
| 1584 | * @return NC_PSPOLL_SSH_CHANNEL if a new SSH channel has been created. |
| 1585 | * @return NC_PSPOLL_SSH_MSG if just an SSH message has been processed. |
| 1586 | * @return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR if session has been terminated (@p msg filled). |
| 1587 | * @return NC_PSPOLL_ERROR on other fatal errors (@p msg filled). |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1588 | */ |
| 1589 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1590 | nc_ps_poll_session_io(struct nc_session *session, int io_timeout, time_t now_mono, char *msg) |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1591 | { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1592 | struct pollfd pfd; |
Michal Vasko | 2260f55 | 2018-06-06 10:06:12 +0200 | [diff] [blame] | 1593 | int r, ret = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1594 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1595 | #ifdef NC_ENABLED_SSH |
roman | 456f92d | 2023-04-28 10:28:12 +0200 | [diff] [blame] | 1596 | ssh_message ssh_msg; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1597 | struct nc_session *new; |
| 1598 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1599 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1600 | /* check timeout first */ |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 1601 | if (!(session->flags & NC_SESSION_CALLHOME) && !nc_session_get_notif_status(session) && server_opts.idle_timeout && |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1602 | (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1603 | sprintf(msg, "session idle timeout elapsed"); |
| 1604 | session->status = NC_STATUS_INVALID; |
| 1605 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 1606 | return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1607 | } |
| 1608 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1609 | r = nc_session_io_lock(session, io_timeout, __func__); |
| 1610 | if (r < 0) { |
| 1611 | sprintf(msg, "session IO lock failed to be acquired"); |
| 1612 | return NC_PSPOLL_ERROR; |
| 1613 | } else if (!r) { |
| 1614 | return NC_PSPOLL_TIMEOUT; |
| 1615 | } |
| 1616 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1617 | switch (session->ti_type) { |
| 1618 | #ifdef NC_ENABLED_SSH |
| 1619 | case NC_TI_LIBSSH: |
| 1620 | r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1621 | if (r == SSH_EOF) { |
| 1622 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1623 | session->status = NC_STATUS_INVALID; |
| 1624 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1625 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1626 | } else if (r == SSH_ERROR) { |
| 1627 | sprintf(msg, "SSH channel poll error (%s)", ssh_get_error(session->ti.libssh.session)); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1628 | session->status = NC_STATUS_INVALID; |
| 1629 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1630 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1631 | } else if (!r) { |
| 1632 | if (session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1633 | /* new SSH message */ |
| 1634 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1635 | if (session->ti.libssh.next) { |
| 1636 | for (new = session->ti.libssh.next; new != session; new = new->ti.libssh.next) { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1637 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel && |
| 1638 | (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1639 | /* new NETCONF SSH channel */ |
| 1640 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1641 | break; |
| 1642 | } |
| 1643 | } |
| 1644 | if (new != session) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1645 | break; |
| 1646 | } |
| 1647 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1648 | |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1649 | /* just some SSH message */ |
| 1650 | ret = NC_PSPOLL_SSH_MSG; |
| 1651 | } else { |
| 1652 | ret = NC_PSPOLL_TIMEOUT; |
| 1653 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1654 | } else { |
| 1655 | /* we have some application data */ |
| 1656 | ret = NC_PSPOLL_RPC; |
| 1657 | } |
| 1658 | break; |
| 1659 | #endif |
| 1660 | #ifdef NC_ENABLED_TLS |
| 1661 | case NC_TI_OPENSSL: |
| 1662 | r = SSL_pending(session->ti.tls); |
| 1663 | if (!r) { |
| 1664 | /* no data pending in the SSL buffer, poll fd */ |
| 1665 | pfd.fd = SSL_get_rfd(session->ti.tls); |
| 1666 | if (pfd.fd < 0) { |
| 1667 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1668 | ret = NC_PSPOLL_ERROR; |
| 1669 | break; |
| 1670 | } |
| 1671 | pfd.events = POLLIN; |
| 1672 | pfd.revents = 0; |
| 1673 | r = poll(&pfd, 1, 0); |
| 1674 | |
| 1675 | if ((r < 0) && (errno != EINTR)) { |
| 1676 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1677 | session->status = NC_STATUS_INVALID; |
| 1678 | ret = NC_PSPOLL_ERROR; |
| 1679 | } else if (r > 0) { |
| 1680 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1681 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1682 | session->status = NC_STATUS_INVALID; |
| 1683 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1684 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1685 | } else if (pfd.revents & POLLERR) { |
| 1686 | sprintf(msg, "communication socket error"); |
| 1687 | session->status = NC_STATUS_INVALID; |
| 1688 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1689 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1690 | } else { |
| 1691 | ret = NC_PSPOLL_RPC; |
| 1692 | } |
| 1693 | } else { |
| 1694 | ret = NC_PSPOLL_TIMEOUT; |
| 1695 | } |
| 1696 | } else { |
| 1697 | ret = NC_PSPOLL_RPC; |
| 1698 | } |
| 1699 | break; |
| 1700 | #endif |
| 1701 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1702 | case NC_TI_UNIX: |
| 1703 | pfd.fd = (session->ti_type == NC_TI_FD) ? session->ti.fd.in : session->ti.unixsock.sock; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1704 | pfd.events = POLLIN; |
| 1705 | pfd.revents = 0; |
| 1706 | r = poll(&pfd, 1, 0); |
| 1707 | |
| 1708 | if ((r < 0) && (errno != EINTR)) { |
| 1709 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1710 | session->status = NC_STATUS_INVALID; |
| 1711 | ret = NC_PSPOLL_ERROR; |
| 1712 | } else if (r > 0) { |
| 1713 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1714 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1715 | session->status = NC_STATUS_INVALID; |
| 1716 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1717 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1718 | } else if (pfd.revents & POLLERR) { |
| 1719 | sprintf(msg, "communication socket error"); |
| 1720 | session->status = NC_STATUS_INVALID; |
| 1721 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1722 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1723 | } else { |
| 1724 | ret = NC_PSPOLL_RPC; |
| 1725 | } |
| 1726 | } else { |
| 1727 | ret = NC_PSPOLL_TIMEOUT; |
| 1728 | } |
| 1729 | break; |
| 1730 | case NC_TI_NONE: |
| 1731 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1732 | ret = NC_PSPOLL_ERROR; |
| 1733 | break; |
| 1734 | } |
| 1735 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1736 | nc_session_io_unlock(session, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1737 | return ret; |
| 1738 | } |
| 1739 | |
| 1740 | API int |
| 1741 | nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session) |
| 1742 | { |
Michal Vasko | 443faa0 | 2022-10-20 09:09:03 +0200 | [diff] [blame] | 1743 | int ret = NC_PSPOLL_ERROR, r; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1744 | uint8_t q_id; |
| 1745 | uint16_t i, j; |
| 1746 | char msg[256]; |
| 1747 | struct timespec ts_timeout, ts_cur; |
| 1748 | struct nc_session *cur_session; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1749 | struct nc_ps_session *cur_ps_session; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1750 | struct nc_server_rpc *rpc = NULL; |
| 1751 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 1752 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1753 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1754 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1755 | } |
| 1756 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1757 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1758 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1759 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1760 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1761 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1762 | if (!ps->session_count) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1763 | nc_ps_unlock(ps, q_id, __func__); |
| 1764 | return NC_PSPOLL_NOSESSIONS; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1765 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1766 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1767 | /* fill timespecs */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1768 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1769 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1770 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | /* poll all the sessions one-by-one */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1774 | do { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1775 | /* loop from i to j once (all sessions) */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1776 | if (ps->last_event_session == ps->session_count - 1) { |
| 1777 | i = j = 0; |
| 1778 | } else { |
| 1779 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1780 | } |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1781 | do { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1782 | cur_ps_session = ps->sessions[i]; |
| 1783 | cur_session = cur_ps_session->session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1784 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1785 | /* SESSION RPC LOCK */ |
| 1786 | r = nc_session_rpc_lock(cur_session, 0, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1787 | if (r == -1) { |
| 1788 | ret = NC_PSPOLL_ERROR; |
| 1789 | } else if (r == 1) { |
| 1790 | /* no one else is currently working with the session, so we can, otherwise skip it */ |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1791 | switch (cur_ps_session->state) { |
| 1792 | case NC_PS_STATE_NONE: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1793 | if (cur_session->status == NC_STATUS_RUNNING) { |
| 1794 | /* session is fine, work with it */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1795 | cur_ps_session->state = NC_PS_STATE_BUSY; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1796 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1797 | ret = nc_ps_poll_session_io(cur_session, NC_SESSION_LOCK_TIMEOUT, ts_cur.tv_sec, msg); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1798 | switch (ret) { |
| 1799 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1800 | ERR(cur_session, "%s.", msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1801 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1802 | break; |
| 1803 | case NC_PSPOLL_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1804 | ERR(cur_session, "%s.", msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1805 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1806 | break; |
| 1807 | case NC_PSPOLL_TIMEOUT: |
| 1808 | #ifdef NC_ENABLED_SSH |
| 1809 | case NC_PSPOLL_SSH_CHANNEL: |
| 1810 | case NC_PSPOLL_SSH_MSG: |
| 1811 | #endif |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1812 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1813 | break; |
| 1814 | case NC_PSPOLL_RPC: |
| 1815 | /* let's keep the state busy, we are not done with this session */ |
| 1816 | break; |
| 1817 | } |
| 1818 | } else { |
| 1819 | /* session is not fine, let the caller know */ |
| 1820 | ret = NC_PSPOLL_SESSION_TERM; |
| 1821 | if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) { |
| 1822 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1823 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1824 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1825 | } |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1826 | break; |
| 1827 | case NC_PS_STATE_BUSY: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1828 | /* it definitely should not be busy because we have the lock */ |
| 1829 | ERRINT; |
Michal Vasko | 4992d80 | 2017-08-09 12:20:01 +0200 | [diff] [blame] | 1830 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1831 | break; |
| 1832 | case NC_PS_STATE_INVALID: |
| 1833 | /* we got it locked, but it will be freed, let it be */ |
| 1834 | ret = NC_PSPOLL_TIMEOUT; |
| 1835 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1836 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1837 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1838 | /* keep RPC lock in this one case */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1839 | if (ret != NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1840 | /* SESSION RPC UNLOCK */ |
| 1841 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1842 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1843 | } else { |
| 1844 | /* timeout */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1845 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1846 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1847 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1848 | /* something happened */ |
| 1849 | if (ret != NC_PSPOLL_TIMEOUT) { |
| 1850 | break; |
| 1851 | } |
| 1852 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1853 | if (i == ps->session_count - 1) { |
| 1854 | i = 0; |
| 1855 | } else { |
| 1856 | ++i; |
| 1857 | } |
| 1858 | } while (i != j); |
| 1859 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1860 | /* no event, no session remains locked */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1861 | if (ret == NC_PSPOLL_TIMEOUT) { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1862 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1863 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 1864 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1865 | /* final timeout */ |
| 1866 | break; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1867 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1868 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1869 | } while (ret == NC_PSPOLL_TIMEOUT); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1870 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1871 | /* do we want to return the session? */ |
| 1872 | switch (ret) { |
| 1873 | case NC_PSPOLL_RPC: |
| 1874 | case NC_PSPOLL_SESSION_TERM: |
| 1875 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1876 | #ifdef NC_ENABLED_SSH |
| 1877 | case NC_PSPOLL_SSH_CHANNEL: |
| 1878 | case NC_PSPOLL_SSH_MSG: |
| 1879 | #endif |
| 1880 | if (session) { |
| 1881 | *session = cur_session; |
| 1882 | } |
| 1883 | ps->last_event_session = i; |
| 1884 | break; |
| 1885 | default: |
| 1886 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1887 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1888 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1889 | /* PS UNLOCK */ |
| 1890 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1891 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1892 | /* we have some data available and the session is RPC locked (but not IO locked) */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1893 | if (ret == NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1894 | ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1895 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1896 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1897 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1898 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1899 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1900 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1901 | } |
| 1902 | } else { |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1903 | cur_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1904 | |
Michal Vasko | 7f1ee93 | 2018-10-11 09:41:42 +0200 | [diff] [blame] | 1905 | /* process RPC */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1906 | ret |= nc_server_send_reply_io(cur_session, timeout, rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1907 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1908 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1909 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1910 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1911 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1912 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1913 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1914 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1915 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1916 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1917 | nc_server_rpc_free(rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1918 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1919 | /* SESSION RPC UNLOCK */ |
| 1920 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1921 | } |
| 1922 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1923 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1926 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1927 | nc_ps_clear(struct nc_pollsession *ps, int all, void (*data_free)(void *)) |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1928 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1929 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1930 | uint16_t i; |
| 1931 | struct nc_session *session; |
| 1932 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1933 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1934 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1935 | return; |
| 1936 | } |
| 1937 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1938 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1939 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1940 | return; |
| 1941 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1942 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1943 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1944 | for (i = 0; i < ps->session_count; i++) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1945 | nc_session_free(ps->sessions[i]->session, data_free); |
| 1946 | free(ps->sessions[i]); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1947 | } |
| 1948 | free(ps->sessions); |
| 1949 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1950 | ps->session_count = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1951 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1952 | } else { |
| 1953 | for (i = 0; i < ps->session_count; ) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1954 | if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) { |
| 1955 | session = ps->sessions[i]->session; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1956 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1957 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1958 | continue; |
| 1959 | } |
| 1960 | |
| 1961 | ++i; |
| 1962 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1963 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1964 | |
| 1965 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1966 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1967 | } |
| 1968 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1969 | static int |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1970 | nc_get_uid(int sock, uid_t *uid) |
| 1971 | { |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1972 | int ret; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1973 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1974 | #ifdef SO_PEERCRED |
| 1975 | struct ucred ucred; |
| 1976 | socklen_t len; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 1977 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1978 | len = sizeof(ucred); |
| 1979 | ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len); |
| 1980 | if (!ret) { |
| 1981 | *uid = ucred.uid; |
| 1982 | } |
| 1983 | #else |
| 1984 | ret = getpeereid(sock, uid, NULL); |
| 1985 | #endif |
| 1986 | |
| 1987 | if (ret < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1988 | ERR(NULL, "Failed to get credentials from unix socket (%s).", strerror(errno)); |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1989 | return -1; |
| 1990 | } |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1991 | return 0; |
| 1992 | } |
| 1993 | |
| 1994 | static int |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1995 | nc_accept_unix(struct nc_session *session, int sock) |
| 1996 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1997 | #if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID) |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1998 | struct passwd *pw, pw_buf; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1999 | char *username; |
Michal Vasko | 292c554 | 2023-02-01 14:33:17 +0100 | [diff] [blame] | 2000 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2001 | session->ti_type = NC_TI_UNIX; |
Michal Vasko | 143aa14 | 2021-10-01 15:31:48 +0200 | [diff] [blame] | 2002 | uid_t uid = 0; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 2003 | char *buf = NULL; |
| 2004 | size_t buf_len = 0; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2005 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 2006 | if (nc_get_uid(sock, &uid)) { |
| 2007 | close(sock); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2008 | return -1; |
| 2009 | } |
| 2010 | |
roman | f6e3201 | 2023-04-24 15:51:26 +0200 | [diff] [blame] | 2011 | pw = nc_getpw(uid, NULL, &pw_buf, &buf, &buf_len); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2012 | if (pw == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2013 | ERR(NULL, "Failed to find username for uid=%u (%s).\n", uid, strerror(errno)); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2014 | close(sock); |
| 2015 | return -1; |
| 2016 | } |
| 2017 | |
| 2018 | username = strdup(pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 2019 | free(buf); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2020 | if (username == NULL) { |
| 2021 | ERRMEM; |
| 2022 | close(sock); |
| 2023 | return -1; |
| 2024 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2025 | session->username = username; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2026 | |
| 2027 | session->ti.unixsock.sock = sock; |
| 2028 | |
| 2029 | return 1; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 2030 | #else |
| 2031 | return -1; |
| 2032 | #endif |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 2033 | } |
| 2034 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2035 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2036 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2037 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2038 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2039 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2040 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2041 | if (!name) { |
| 2042 | ERRARG("name"); |
| 2043 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2046 | /* BIND LOCK */ |
| 2047 | pthread_mutex_lock(&server_opts.bind_lock); |
| 2048 | |
| 2049 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2050 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2051 | |
| 2052 | /* check name uniqueness */ |
| 2053 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2054 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2055 | ERR(NULL, "Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2056 | ret = -1; |
| 2057 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2058 | } |
| 2059 | } |
| 2060 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2061 | server_opts.endpts = nc_realloc(server_opts.endpts, (server_opts.endpt_count + 1) * sizeof *server_opts.endpts); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2062 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2063 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2064 | ret = -1; |
| 2065 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2066 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2067 | memset(&server_opts.endpts[server_opts.endpt_count], 0, sizeof *server_opts.endpts); |
| 2068 | ++server_opts.endpt_count; |
| 2069 | |
| 2070 | server_opts.endpts[server_opts.endpt_count - 1].name = strdup(name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2071 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2072 | server_opts.endpts[server_opts.endpt_count - 1].ka.idle_time = 1; |
| 2073 | server_opts.endpts[server_opts.endpt_count - 1].ka.max_probes = 10; |
| 2074 | server_opts.endpts[server_opts.endpt_count - 1].ka.probe_interval = 5; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2075 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2076 | server_opts.binds = nc_realloc(server_opts.binds, server_opts.endpt_count * sizeof *server_opts.binds); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2077 | if (!server_opts.binds) { |
| 2078 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2079 | ret = -1; |
| 2080 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 2081 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2082 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2083 | memset(&server_opts.binds[server_opts.endpt_count - 1], 0, sizeof *server_opts.binds); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2084 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
| 2085 | |
| 2086 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2087 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2088 | case NC_TI_LIBSSH: |
| 2089 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2090 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 2091 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2092 | ret = -1; |
| 2093 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2094 | } |
| 2095 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 2096 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2097 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 2098 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_timeout = 30; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2099 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2100 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2101 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2102 | case NC_TI_OPENSSL: |
| 2103 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2104 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 2105 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2106 | ret = -1; |
| 2107 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2108 | } |
| 2109 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2110 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2111 | case NC_TI_UNIX: |
| 2112 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts)); |
| 2113 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) { |
| 2114 | ERRMEM; |
| 2115 | ret = -1; |
| 2116 | goto cleanup; |
| 2117 | } |
| 2118 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1; |
| 2119 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1; |
| 2120 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1; |
| 2121 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2122 | default: |
| 2123 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2124 | ret = -1; |
| 2125 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2126 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2127 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2128 | cleanup: |
| 2129 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2130 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2131 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2132 | /* BIND UNLOCK */ |
| 2133 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2134 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2135 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2136 | } |
| 2137 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2138 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2139 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2140 | { |
| 2141 | uint32_t i; |
| 2142 | int ret = -1; |
| 2143 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2144 | /* BIND LOCK */ |
| 2145 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2146 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2147 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2148 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2149 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2150 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2151 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2152 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2153 | free(server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2154 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2155 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2156 | case NC_TI_LIBSSH: |
| 2157 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2158 | free(server_opts.endpts[i].opts.ssh); |
| 2159 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2160 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2161 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2162 | case NC_TI_OPENSSL: |
| 2163 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2164 | free(server_opts.endpts[i].opts.tls); |
| 2165 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2166 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2167 | case NC_TI_UNIX: |
| 2168 | free(server_opts.endpts[i].opts.unixsock); |
| 2169 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2170 | default: |
| 2171 | ERRINT; |
| 2172 | /* won't get here ...*/ |
| 2173 | break; |
| 2174 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2175 | ret = 0; |
| 2176 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2177 | free(server_opts.endpts); |
| 2178 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2179 | |
| 2180 | /* remove all binds */ |
| 2181 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2182 | free(server_opts.binds[i].address); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2183 | if (server_opts.binds[i].sock > -1) { |
| 2184 | close(server_opts.binds[i].sock); |
| 2185 | } |
| 2186 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2187 | free(server_opts.binds); |
| 2188 | server_opts.binds = NULL; |
| 2189 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2190 | server_opts.endpt_count = 0; |
| 2191 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2192 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2193 | /* remove one endpoint with bind(s) or all endpoints using one transport protocol */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2194 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2195 | if ((name && !strcmp(server_opts.endpts[i].name, name)) || (!name && (server_opts.endpts[i].ti == ti))) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2196 | /* remove endpt */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2197 | free(server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2198 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2199 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2200 | case NC_TI_LIBSSH: |
| 2201 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2202 | free(server_opts.endpts[i].opts.ssh); |
| 2203 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2204 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2205 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2206 | case NC_TI_OPENSSL: |
| 2207 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2208 | free(server_opts.endpts[i].opts.tls); |
| 2209 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2210 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2211 | case NC_TI_UNIX: |
| 2212 | free(server_opts.endpts[i].opts.unixsock); |
| 2213 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2214 | default: |
| 2215 | ERRINT; |
| 2216 | break; |
| 2217 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2218 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2219 | /* remove bind(s) */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2220 | free(server_opts.binds[i].address); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2221 | if (server_opts.binds[i].sock > -1) { |
| 2222 | close(server_opts.binds[i].sock); |
| 2223 | } |
| 2224 | |
| 2225 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2226 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2227 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2228 | free(server_opts.binds); |
| 2229 | server_opts.binds = NULL; |
| 2230 | free(server_opts.endpts); |
| 2231 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2232 | } else if (i < server_opts.endpt_count) { |
| 2233 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 2234 | memcpy(&server_opts.endpts[i], &server_opts.endpts[server_opts.endpt_count], sizeof *server_opts.endpts); |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2235 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2236 | |
| 2237 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2238 | if (name) { |
| 2239 | break; |
| 2240 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2241 | } |
| 2242 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2245 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2246 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2247 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2248 | /* BIND UNLOCK */ |
| 2249 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2250 | |
| 2251 | return ret; |
| 2252 | } |
| 2253 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2254 | API int |
| 2255 | nc_server_endpt_count(void) |
| 2256 | { |
| 2257 | return server_opts.endpt_count; |
| 2258 | } |
| 2259 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2260 | API int |
| 2261 | nc_server_is_endpt(const char *name) |
| 2262 | { |
| 2263 | uint16_t i; |
| 2264 | int found = 0; |
| 2265 | |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2266 | if (!name) { |
| 2267 | return found; |
| 2268 | } |
| 2269 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2270 | /* ENDPT READ LOCK */ |
| 2271 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2272 | |
| 2273 | /* check name uniqueness */ |
| 2274 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2275 | if (!strcmp(server_opts.endpts[i].name, name)) { |
| 2276 | found = 1; |
| 2277 | break; |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | /* ENDPT UNLOCK */ |
| 2282 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2283 | |
| 2284 | return found; |
| 2285 | } |
| 2286 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2287 | int |
| 2288 | nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port) |
| 2289 | { |
| 2290 | struct nc_endpt *endpt; |
| 2291 | struct nc_bind *bind = NULL; |
| 2292 | uint16_t i; |
| 2293 | int sock = -1, set_addr, ret = 0; |
| 2294 | |
| 2295 | if (!endpt_name) { |
| 2296 | ERRARG("endpt_name"); |
| 2297 | return -1; |
| 2298 | } else if ((!address && !port) || (address && port)) { |
| 2299 | ERRARG("address and port"); |
| 2300 | return -1; |
| 2301 | } |
| 2302 | |
| 2303 | if (address) { |
| 2304 | set_addr = 1; |
| 2305 | } else { |
| 2306 | set_addr = 0; |
| 2307 | } |
| 2308 | |
| 2309 | /* BIND LOCK */ |
| 2310 | pthread_mutex_lock(&server_opts.bind_lock); |
| 2311 | |
| 2312 | /* ENDPT LOCK */ |
| 2313 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 2314 | if (!endpt) { |
| 2315 | /* BIND UNLOCK */ |
| 2316 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2317 | return -1; |
| 2318 | } |
| 2319 | |
| 2320 | bind = &server_opts.binds[i]; |
| 2321 | |
| 2322 | if (set_addr) { |
| 2323 | port = bind->port; |
| 2324 | } else { |
| 2325 | address = bind->address; |
| 2326 | } |
| 2327 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2328 | if (!set_addr && (endpt->ti == NC_TI_UNIX)) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2329 | ret = -1; |
| 2330 | goto cleanup; |
| 2331 | } |
| 2332 | |
| 2333 | /* we have all the information we need to create a listening socket */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2334 | if (address && (port || (endpt->ti == NC_TI_UNIX))) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2335 | /* create new socket, close the old one */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2336 | if (endpt->ti == NC_TI_UNIX) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2337 | sock = nc_sock_listen_unix(address, endpt->opts.unixsock); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2338 | } else { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2339 | sock = nc_sock_listen_inet(address, port, &endpt->ka); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2340 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2341 | if (sock == -1) { |
| 2342 | ret = -1; |
| 2343 | goto cleanup; |
| 2344 | } |
| 2345 | |
| 2346 | if (bind->sock > -1) { |
| 2347 | close(bind->sock); |
| 2348 | } |
| 2349 | bind->sock = sock; |
| 2350 | } /* else we are just setting address or port */ |
| 2351 | |
| 2352 | if (set_addr) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2353 | free(bind->address); |
| 2354 | bind->address = strdup(address); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2355 | } else { |
| 2356 | bind->port = port; |
| 2357 | } |
| 2358 | |
| 2359 | if (sock > -1) { |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2360 | switch (endpt->ti) { |
| 2361 | case NC_TI_UNIX: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2362 | VRB(NULL, "Listening on %s for UNIX connections.", address); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2363 | break; |
| 2364 | #ifdef NC_ENABLED_SSH |
| 2365 | case NC_TI_LIBSSH: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2366 | VRB(NULL, "Listening on %s:%u for SSH connections.", address, port); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2367 | break; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2368 | #endif |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2369 | #ifdef NC_ENABLED_TLS |
| 2370 | case NC_TI_OPENSSL: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2371 | VRB(NULL, "Listening on %s:%u for TLS connections.", address, port); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2372 | break; |
| 2373 | #endif |
| 2374 | default: |
| 2375 | ERRINT; |
| 2376 | break; |
| 2377 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | cleanup: |
| 2381 | /* ENDPT UNLOCK */ |
| 2382 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2383 | |
| 2384 | /* BIND UNLOCK */ |
| 2385 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2386 | |
| 2387 | return ret; |
| 2388 | } |
| 2389 | |
| 2390 | API int |
| 2391 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 2392 | { |
| 2393 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 2394 | } |
| 2395 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2396 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2397 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2398 | API int |
| 2399 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 2400 | { |
| 2401 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 2402 | } |
| 2403 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2404 | #endif |
| 2405 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2406 | API int |
| 2407 | nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid) |
| 2408 | { |
| 2409 | struct nc_endpt *endpt; |
| 2410 | uint16_t i; |
| 2411 | int ret = 0; |
| 2412 | |
| 2413 | if (!endpt_name) { |
| 2414 | ERRARG("endpt_name"); |
| 2415 | return -1; |
| 2416 | } else if (mode == 0) { |
| 2417 | ERRARG("mode"); |
| 2418 | return -1; |
| 2419 | } |
| 2420 | |
| 2421 | /* ENDPT LOCK */ |
| 2422 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2423 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2424 | return -1; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2425 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2426 | |
| 2427 | if (endpt->ti != NC_TI_UNIX) { |
| 2428 | ret = -1; |
| 2429 | goto cleanup; |
| 2430 | } |
| 2431 | |
| 2432 | endpt->opts.unixsock->mode = mode; |
| 2433 | endpt->opts.unixsock->uid = uid; |
| 2434 | endpt->opts.unixsock->gid = gid; |
| 2435 | |
| 2436 | cleanup: |
| 2437 | /* ENDPT UNLOCK */ |
| 2438 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2439 | |
| 2440 | return ret; |
| 2441 | } |
| 2442 | |
| 2443 | API int |
| 2444 | nc_server_endpt_enable_keepalives(const char *endpt_name, int enable) |
| 2445 | { |
| 2446 | struct nc_endpt *endpt; |
| 2447 | int ret = 0; |
| 2448 | |
| 2449 | if (!endpt_name) { |
| 2450 | ERRARG("endpt_name"); |
| 2451 | return -1; |
| 2452 | } |
| 2453 | |
| 2454 | /* ENDPT LOCK */ |
| 2455 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2456 | if (!endpt) { |
| 2457 | return -1; |
| 2458 | } |
| 2459 | |
| 2460 | endpt->ka.enabled = (enable ? 1 : 0); |
| 2461 | |
| 2462 | /* ENDPT UNLOCK */ |
| 2463 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2464 | |
| 2465 | return ret; |
| 2466 | } |
| 2467 | |
| 2468 | API int |
| 2469 | nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval) |
| 2470 | { |
| 2471 | struct nc_endpt *endpt; |
| 2472 | int ret = 0; |
| 2473 | |
| 2474 | if (!endpt_name) { |
| 2475 | ERRARG("endpt_name"); |
| 2476 | return -1; |
| 2477 | } |
| 2478 | |
| 2479 | /* ENDPT LOCK */ |
| 2480 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2481 | if (!endpt) { |
| 2482 | return -1; |
| 2483 | } |
| 2484 | |
| 2485 | if (idle_time > -1) { |
| 2486 | endpt->ka.idle_time = idle_time; |
| 2487 | } |
| 2488 | if (max_probes > -1) { |
| 2489 | endpt->ka.max_probes = max_probes; |
| 2490 | } |
| 2491 | if (probe_interval > -1) { |
| 2492 | endpt->ka.probe_interval = probe_interval; |
| 2493 | } |
| 2494 | |
| 2495 | /* ENDPT UNLOCK */ |
| 2496 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2497 | |
| 2498 | return ret; |
| 2499 | } |
| 2500 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2501 | API NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2502 | nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2503 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2504 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2505 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2506 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2507 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2508 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2509 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2510 | if (!ctx) { |
| 2511 | ERRARG("ctx"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2512 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2513 | } else if (!session) { |
| 2514 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2515 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2516 | } |
| 2517 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2518 | /* init ctx as needed */ |
| 2519 | nc_server_init_ctx(ctx); |
| 2520 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2521 | /* BIND LOCK */ |
| 2522 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2523 | |
| 2524 | if (!server_opts.endpt_count) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2525 | ERR(NULL, "No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2526 | /* BIND UNLOCK */ |
| 2527 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2528 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2529 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2530 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2531 | ret = nc_sock_accept_binds(server_opts.binds, server_opts.endpt_count, timeout, &host, &port, &bind_idx); |
Michal Vasko | 50456e8 | 2016-02-02 12:16:08 +0100 | [diff] [blame] | 2532 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2533 | /* BIND UNLOCK */ |
| 2534 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 2535 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 2536 | if (!ret) { |
| 2537 | return NC_MSG_WOULDBLOCK; |
| 2538 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2539 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2540 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2541 | |
| 2542 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 2543 | /* ENDPT READ LOCK */ |
| 2544 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2545 | |
| 2546 | /* BIND UNLOCK */ |
| 2547 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2548 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2549 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2550 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2551 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 2552 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2553 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2554 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2555 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2556 | msgtype = NC_MSG_ERROR; |
| 2557 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2558 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2559 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2560 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2561 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2562 | (*session)->host = host; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2563 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2564 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2565 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2566 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2567 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 2568 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2569 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2570 | if (ret < 0) { |
| 2571 | msgtype = NC_MSG_ERROR; |
| 2572 | goto cleanup; |
| 2573 | } else if (!ret) { |
| 2574 | msgtype = NC_MSG_WOULDBLOCK; |
| 2575 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2576 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2577 | } else |
| 2578 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2579 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2580 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 2581 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2582 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2583 | if (ret < 0) { |
| 2584 | msgtype = NC_MSG_ERROR; |
| 2585 | goto cleanup; |
| 2586 | } else if (!ret) { |
| 2587 | msgtype = NC_MSG_WOULDBLOCK; |
| 2588 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2589 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2590 | } else |
| 2591 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2592 | if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) { |
| 2593 | (*session)->data = server_opts.endpts[bind_idx].opts.unixsock; |
| 2594 | ret = nc_accept_unix(*session, sock); |
| 2595 | if (ret < 0) { |
| 2596 | msgtype = NC_MSG_ERROR; |
| 2597 | goto cleanup; |
| 2598 | } |
| 2599 | } else { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2600 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2601 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2602 | msgtype = NC_MSG_ERROR; |
| 2603 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2604 | } |
| 2605 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2606 | (*session)->data = NULL; |
| 2607 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2608 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2609 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2610 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2611 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2612 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2613 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2614 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2615 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2616 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2617 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2618 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2619 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2620 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2621 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2622 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2623 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 2624 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2625 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2626 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2627 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2628 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2629 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2630 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2631 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2632 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2633 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2634 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2635 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2636 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2637 | } |
| 2638 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2639 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2640 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2641 | /* client is expected to be locked */ |
| 2642 | static int |
| 2643 | _nc_server_ch_client_del_endpt(struct nc_ch_client *client, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2644 | { |
| 2645 | uint16_t i; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2646 | int ret = -1; |
| 2647 | |
| 2648 | if (!endpt_name) { |
| 2649 | /* remove all endpoints */ |
| 2650 | for (i = 0; i < client->ch_endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2651 | free(client->ch_endpts[i].name); |
| 2652 | free(client->ch_endpts[i].address); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2653 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2654 | close(client->ch_endpts[i].sock_pending); |
| 2655 | } |
| 2656 | switch (client->ch_endpts[i].ti) { |
| 2657 | #ifdef NC_ENABLED_SSH |
| 2658 | case NC_TI_LIBSSH: |
| 2659 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2660 | free(client->ch_endpts[i].opts.ssh); |
| 2661 | break; |
| 2662 | #endif |
| 2663 | #ifdef NC_ENABLED_TLS |
| 2664 | case NC_TI_OPENSSL: |
| 2665 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2666 | free(client->ch_endpts[i].opts.tls); |
| 2667 | break; |
| 2668 | #endif |
| 2669 | default: |
| 2670 | ERRINT; |
| 2671 | /* won't get here ...*/ |
| 2672 | break; |
| 2673 | } |
| 2674 | } |
| 2675 | free(client->ch_endpts); |
| 2676 | client->ch_endpts = NULL; |
| 2677 | client->ch_endpt_count = 0; |
| 2678 | |
| 2679 | ret = 0; |
| 2680 | } else { |
| 2681 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2682 | if (!strcmp(client->ch_endpts[i].name, endpt_name) && (!ti || (ti == client->ch_endpts[i].ti))) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2683 | free(client->ch_endpts[i].name); |
| 2684 | free(client->ch_endpts[i].address); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2685 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2686 | close(client->ch_endpts[i].sock_pending); |
| 2687 | } |
| 2688 | switch (client->ch_endpts[i].ti) { |
| 2689 | #ifdef NC_ENABLED_SSH |
| 2690 | case NC_TI_LIBSSH: |
| 2691 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2692 | free(client->ch_endpts[i].opts.ssh); |
| 2693 | break; |
| 2694 | #endif |
| 2695 | #ifdef NC_ENABLED_TLS |
| 2696 | case NC_TI_OPENSSL: |
| 2697 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2698 | free(client->ch_endpts[i].opts.tls); |
| 2699 | break; |
| 2700 | #endif |
| 2701 | default: |
| 2702 | ERRINT; |
| 2703 | /* won't get here ...*/ |
| 2704 | break; |
| 2705 | } |
| 2706 | |
| 2707 | /* move last endpoint to the empty space */ |
| 2708 | --client->ch_endpt_count; |
| 2709 | if (i < client->ch_endpt_count) { |
| 2710 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2711 | } else if (!server_opts.ch_client_count) { |
| 2712 | free(server_opts.ch_clients); |
| 2713 | server_opts.ch_clients = NULL; |
| 2714 | } |
| 2715 | |
| 2716 | ret = 0; |
| 2717 | break; |
| 2718 | } |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | return ret; |
| 2723 | } |
| 2724 | |
| 2725 | API int |
| 2726 | nc_server_ch_add_client(const char *name) |
| 2727 | { |
| 2728 | uint16_t i; |
| 2729 | struct nc_ch_client *client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2730 | |
| 2731 | if (!name) { |
| 2732 | ERRARG("name"); |
| 2733 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | /* WRITE LOCK */ |
| 2737 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2738 | |
| 2739 | /* check name uniqueness */ |
| 2740 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2741 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2742 | ERR(NULL, "Call Home client \"%s\" already exists.", name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2743 | /* WRITE UNLOCK */ |
| 2744 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2745 | return -1; |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | ++server_opts.ch_client_count; |
| 2750 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 2751 | if (!server_opts.ch_clients) { |
| 2752 | ERRMEM; |
| 2753 | /* WRITE UNLOCK */ |
| 2754 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2755 | return -1; |
| 2756 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2757 | client = &server_opts.ch_clients[server_opts.ch_client_count - 1]; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2758 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2759 | client->name = strdup(name); |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2760 | client->id = ATOMIC_INC_RELAXED(server_opts.new_client_id); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2761 | client->ch_endpts = NULL; |
| 2762 | client->ch_endpt_count = 0; |
| 2763 | client->conn_type = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2764 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2765 | /* set CH default options */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2766 | client->start_with = NC_CH_FIRST_LISTED; |
| 2767 | client->max_attempts = 3; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2768 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2769 | pthread_mutex_init(&client->lock, NULL); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2770 | |
| 2771 | /* WRITE UNLOCK */ |
| 2772 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2773 | |
| 2774 | return 0; |
| 2775 | } |
| 2776 | |
| 2777 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2778 | nc_server_ch_del_client(const char *name) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2779 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2780 | uint16_t i; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2781 | int ret = -1; |
| 2782 | |
| 2783 | /* WRITE LOCK */ |
| 2784 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2785 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2786 | if (!name) { |
| 2787 | /* remove all CH clients with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2788 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2789 | free(server_opts.ch_clients[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2790 | |
| 2791 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2792 | _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2793 | |
| 2794 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2795 | ret = 0; |
| 2796 | } |
| 2797 | free(server_opts.ch_clients); |
| 2798 | server_opts.ch_clients = NULL; |
| 2799 | |
| 2800 | server_opts.ch_client_count = 0; |
| 2801 | |
| 2802 | } else { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2803 | /* remove one client with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2804 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2805 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2806 | free(server_opts.ch_clients[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2807 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2808 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2809 | _nc_server_ch_client_del_endpt(&server_opts.ch_clients[i], NULL, 0); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2810 | |
| 2811 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2812 | |
| 2813 | /* move last client and endpoint(s) to the empty space */ |
| 2814 | --server_opts.ch_client_count; |
| 2815 | if (i < server_opts.ch_client_count) { |
| 2816 | memcpy(&server_opts.ch_clients[i], &server_opts.ch_clients[server_opts.ch_client_count], |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2817 | sizeof *server_opts.ch_clients); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2818 | } else if (!server_opts.ch_client_count) { |
| 2819 | free(server_opts.ch_clients); |
| 2820 | server_opts.ch_clients = NULL; |
| 2821 | } |
| 2822 | |
| 2823 | ret = 0; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2824 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | /* WRITE UNLOCK */ |
| 2830 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2831 | |
| 2832 | return ret; |
| 2833 | } |
| 2834 | |
| 2835 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2836 | nc_server_ch_is_client(const char *name) |
| 2837 | { |
| 2838 | uint16_t i; |
| 2839 | int found = 0; |
| 2840 | |
| 2841 | if (!name) { |
| 2842 | return found; |
| 2843 | } |
| 2844 | |
| 2845 | /* READ LOCK */ |
| 2846 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2847 | |
| 2848 | /* check name uniqueness */ |
| 2849 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2850 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2851 | found = 1; |
| 2852 | break; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | /* UNLOCK */ |
| 2857 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2858 | |
| 2859 | return found; |
| 2860 | } |
| 2861 | |
| 2862 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2863 | nc_server_ch_client_add_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2864 | { |
| 2865 | uint16_t i; |
| 2866 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2867 | struct nc_ch_endpt *endpt; |
| 2868 | int ret = -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2869 | |
| 2870 | if (!client_name) { |
| 2871 | ERRARG("client_name"); |
| 2872 | return -1; |
| 2873 | } else if (!endpt_name) { |
| 2874 | ERRARG("endpt_name"); |
| 2875 | return -1; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2876 | } else if (!ti) { |
| 2877 | ERRARG("ti"); |
| 2878 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2882 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2883 | if (!client) { |
| 2884 | return -1; |
| 2885 | } |
| 2886 | |
| 2887 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2888 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2889 | ERR(NULL, "Call Home client \"%s\" endpoint \"%s\" already exists.", client_name, endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2890 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | ++client->ch_endpt_count; |
| 2895 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2896 | if (!client->ch_endpts) { |
| 2897 | ERRMEM; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2898 | goto cleanup; |
| 2899 | } |
| 2900 | endpt = &client->ch_endpts[client->ch_endpt_count - 1]; |
| 2901 | |
| 2902 | memset(endpt, 0, sizeof *client->ch_endpts); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2903 | endpt->name = strdup(endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2904 | endpt->ti = ti; |
| 2905 | endpt->sock_pending = -1; |
| 2906 | endpt->ka.idle_time = 1; |
| 2907 | endpt->ka.max_probes = 10; |
| 2908 | endpt->ka.probe_interval = 5; |
| 2909 | |
| 2910 | switch (ti) { |
| 2911 | #ifdef NC_ENABLED_SSH |
| 2912 | case NC_TI_LIBSSH: |
| 2913 | endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2914 | if (!endpt->opts.ssh) { |
| 2915 | ERRMEM; |
| 2916 | goto cleanup; |
| 2917 | } |
roman | 41a11e4 | 2022-06-22 09:27:08 +0200 | [diff] [blame] | 2918 | endpt->opts.ssh->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2919 | endpt->opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 2920 | endpt->opts.ssh->auth_timeout = 30; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2921 | break; |
| 2922 | #endif |
| 2923 | #ifdef NC_ENABLED_TLS |
| 2924 | case NC_TI_OPENSSL: |
| 2925 | endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2926 | if (!endpt->opts.tls) { |
| 2927 | ERRMEM; |
| 2928 | goto cleanup; |
| 2929 | } |
| 2930 | break; |
| 2931 | #endif |
| 2932 | default: |
| 2933 | ERRINT; |
| 2934 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2935 | } |
| 2936 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2937 | /* success */ |
| 2938 | ret = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2939 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2940 | cleanup: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2941 | /* UNLOCK */ |
| 2942 | nc_server_ch_client_unlock(client); |
| 2943 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2944 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2948 | nc_server_ch_client_del_endpt(const char *client_name, const char *endpt_name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2949 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2950 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2951 | struct nc_ch_client *client; |
| 2952 | |
| 2953 | if (!client_name) { |
| 2954 | ERRARG("client_name"); |
| 2955 | return -1; |
| 2956 | } |
| 2957 | |
| 2958 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2959 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2960 | if (!client) { |
| 2961 | return -1; |
| 2962 | } |
| 2963 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2964 | ret = _nc_server_ch_client_del_endpt(client, endpt_name, ti); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2965 | |
| 2966 | /* UNLOCK */ |
| 2967 | nc_server_ch_client_unlock(client); |
| 2968 | |
| 2969 | return ret; |
| 2970 | } |
| 2971 | |
| 2972 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2973 | nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name) |
| 2974 | { |
| 2975 | uint16_t i; |
| 2976 | struct nc_ch_client *client = NULL; |
| 2977 | int found = 0; |
| 2978 | |
| 2979 | if (!client_name || !endpt_name) { |
| 2980 | return found; |
| 2981 | } |
| 2982 | |
| 2983 | /* READ LOCK */ |
| 2984 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2985 | |
| 2986 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2987 | if (!strcmp(server_opts.ch_clients[i].name, client_name)) { |
| 2988 | client = &server_opts.ch_clients[i]; |
| 2989 | break; |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | if (!client) { |
| 2994 | goto cleanup; |
| 2995 | } |
| 2996 | |
| 2997 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2998 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2999 | found = 1; |
| 3000 | break; |
| 3001 | } |
| 3002 | } |
| 3003 | |
| 3004 | cleanup: |
| 3005 | /* UNLOCK */ |
| 3006 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 3007 | return found; |
| 3008 | } |
| 3009 | |
| 3010 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3011 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 3012 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3013 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3014 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3015 | |
| 3016 | if (!client_name) { |
| 3017 | ERRARG("client_name"); |
| 3018 | return -1; |
| 3019 | } else if (!endpt_name) { |
| 3020 | ERRARG("endpt_name"); |
| 3021 | return -1; |
| 3022 | } else if (!address) { |
| 3023 | ERRARG("address"); |
| 3024 | return -1; |
| 3025 | } |
| 3026 | |
| 3027 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3028 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3029 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3030 | return -1; |
| 3031 | } |
| 3032 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3033 | free(endpt->address); |
| 3034 | endpt->address = strdup(address); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3035 | |
| 3036 | /* UNLOCK */ |
| 3037 | nc_server_ch_client_unlock(client); |
| 3038 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3039 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | API int |
| 3043 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 3044 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3045 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3046 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3047 | |
| 3048 | if (!client_name) { |
| 3049 | ERRARG("client_name"); |
| 3050 | return -1; |
| 3051 | } else if (!endpt_name) { |
| 3052 | ERRARG("endpt_name"); |
| 3053 | return -1; |
| 3054 | } else if (!port) { |
| 3055 | ERRARG("port"); |
| 3056 | return -1; |
| 3057 | } |
| 3058 | |
| 3059 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3060 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3061 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3062 | return -1; |
| 3063 | } |
| 3064 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3065 | endpt->port = port; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3066 | |
| 3067 | /* UNLOCK */ |
| 3068 | nc_server_ch_client_unlock(client); |
| 3069 | |
ravsz | 5c5a442 | 2020-03-31 15:53:21 +0200 | [diff] [blame] | 3070 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3074 | nc_server_ch_client_endpt_enable_keepalives(const char *client_name, const char *endpt_name, int enable) |
| 3075 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3076 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3077 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3078 | |
| 3079 | if (!client_name) { |
| 3080 | ERRARG("client_name"); |
| 3081 | return -1; |
| 3082 | } else if (!endpt_name) { |
| 3083 | ERRARG("endpt_name"); |
| 3084 | return -1; |
| 3085 | } |
| 3086 | |
| 3087 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3088 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3089 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3090 | return -1; |
| 3091 | } |
| 3092 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3093 | endpt->ka.enabled = (enable ? 1 : 0); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3094 | |
| 3095 | /* UNLOCK */ |
| 3096 | nc_server_ch_client_unlock(client); |
| 3097 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 3098 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | API int |
| 3102 | nc_server_ch_client_endpt_set_keepalives(const char *client_name, const char *endpt_name, int idle_time, int max_probes, |
| 3103 | int probe_interval) |
| 3104 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3105 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3106 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3107 | |
| 3108 | if (!client_name) { |
| 3109 | ERRARG("client_name"); |
| 3110 | return -1; |
| 3111 | } else if (!endpt_name) { |
| 3112 | ERRARG("endpt_name"); |
| 3113 | return -1; |
| 3114 | } |
| 3115 | |
| 3116 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3117 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3118 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3119 | return -1; |
| 3120 | } |
| 3121 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3122 | if (idle_time > -1) { |
| 3123 | endpt->ka.idle_time = idle_time; |
| 3124 | } |
| 3125 | if (max_probes > -1) { |
| 3126 | endpt->ka.max_probes = max_probes; |
| 3127 | } |
| 3128 | if (probe_interval > -1) { |
| 3129 | endpt->ka.probe_interval = probe_interval; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | /* UNLOCK */ |
| 3133 | nc_server_ch_client_unlock(client); |
| 3134 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 3135 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3139 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 3140 | { |
| 3141 | struct nc_ch_client *client; |
| 3142 | |
| 3143 | if (!client_name) { |
| 3144 | ERRARG("client_name"); |
| 3145 | return -1; |
| 3146 | } else if (!conn_type) { |
| 3147 | ERRARG("conn_type"); |
| 3148 | return -1; |
| 3149 | } |
| 3150 | |
| 3151 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3152 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3153 | if (!client) { |
| 3154 | return -1; |
| 3155 | } |
| 3156 | |
| 3157 | if (client->conn_type != conn_type) { |
| 3158 | client->conn_type = conn_type; |
| 3159 | |
| 3160 | /* set default options */ |
| 3161 | switch (conn_type) { |
| 3162 | case NC_CH_PERSIST: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3163 | /* no options */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3164 | break; |
| 3165 | case NC_CH_PERIOD: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3166 | client->conn.period.period = 60; |
| 3167 | client->conn.period.anchor_time = 0; |
| 3168 | client->conn.period.idle_timeout = 120; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3169 | break; |
| 3170 | default: |
| 3171 | ERRINT; |
| 3172 | break; |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | /* UNLOCK */ |
| 3177 | nc_server_ch_client_unlock(client); |
| 3178 | |
| 3179 | return 0; |
| 3180 | } |
| 3181 | |
| 3182 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3183 | nc_server_ch_client_periodic_set_period(const char *client_name, uint16_t period) |
| 3184 | { |
| 3185 | struct nc_ch_client *client; |
| 3186 | |
| 3187 | if (!client_name) { |
| 3188 | ERRARG("client_name"); |
| 3189 | return -1; |
| 3190 | } else if (!period) { |
| 3191 | ERRARG("period"); |
| 3192 | return -1; |
| 3193 | } |
| 3194 | |
| 3195 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3196 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3197 | if (!client) { |
| 3198 | return -1; |
| 3199 | } |
| 3200 | |
| 3201 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3202 | ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3203 | /* UNLOCK */ |
| 3204 | nc_server_ch_client_unlock(client); |
| 3205 | return -1; |
| 3206 | } |
| 3207 | |
| 3208 | client->conn.period.period = period; |
| 3209 | |
| 3210 | /* UNLOCK */ |
| 3211 | nc_server_ch_client_unlock(client); |
| 3212 | |
| 3213 | return 0; |
| 3214 | } |
| 3215 | |
| 3216 | API int |
| 3217 | nc_server_ch_client_periodic_set_anchor_time(const char *client_name, time_t anchor_time) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3218 | { |
| 3219 | struct nc_ch_client *client; |
| 3220 | |
| 3221 | if (!client_name) { |
| 3222 | ERRARG("client_name"); |
| 3223 | return -1; |
| 3224 | } |
| 3225 | |
| 3226 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3227 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3228 | if (!client) { |
| 3229 | return -1; |
| 3230 | } |
| 3231 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3232 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3233 | ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3234 | /* UNLOCK */ |
| 3235 | nc_server_ch_client_unlock(client); |
| 3236 | return -1; |
| 3237 | } |
| 3238 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3239 | client->conn.period.anchor_time = anchor_time; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3240 | |
| 3241 | /* UNLOCK */ |
| 3242 | nc_server_ch_client_unlock(client); |
| 3243 | |
| 3244 | return 0; |
| 3245 | } |
| 3246 | |
| 3247 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3248 | nc_server_ch_client_periodic_set_idle_timeout(const char *client_name, uint16_t idle_timeout) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3249 | { |
| 3250 | struct nc_ch_client *client; |
| 3251 | |
| 3252 | if (!client_name) { |
| 3253 | ERRARG("client_name"); |
| 3254 | return -1; |
| 3255 | } |
| 3256 | |
| 3257 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3258 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3259 | if (!client) { |
| 3260 | return -1; |
| 3261 | } |
| 3262 | |
| 3263 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3264 | ERR(NULL, "Call Home client \"%s\" is not of periodic connection type.", client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3265 | /* UNLOCK */ |
| 3266 | nc_server_ch_client_unlock(client); |
| 3267 | return -1; |
| 3268 | } |
| 3269 | |
| 3270 | client->conn.period.idle_timeout = idle_timeout; |
| 3271 | |
| 3272 | /* UNLOCK */ |
| 3273 | nc_server_ch_client_unlock(client); |
| 3274 | |
| 3275 | return 0; |
| 3276 | } |
| 3277 | |
| 3278 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3279 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 3280 | { |
| 3281 | struct nc_ch_client *client; |
| 3282 | |
| 3283 | if (!client_name) { |
| 3284 | ERRARG("client_name"); |
| 3285 | return -1; |
| 3286 | } |
| 3287 | |
| 3288 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3289 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3290 | if (!client) { |
| 3291 | return -1; |
| 3292 | } |
| 3293 | |
| 3294 | client->start_with = start_with; |
| 3295 | |
| 3296 | /* UNLOCK */ |
| 3297 | nc_server_ch_client_unlock(client); |
| 3298 | |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
| 3302 | API int |
| 3303 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 3304 | { |
| 3305 | struct nc_ch_client *client; |
| 3306 | |
| 3307 | if (!client_name) { |
| 3308 | ERRARG("client_name"); |
| 3309 | return -1; |
| 3310 | } else if (!max_attempts) { |
| 3311 | ERRARG("max_attempts"); |
| 3312 | return -1; |
| 3313 | } |
| 3314 | |
| 3315 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3316 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3317 | if (!client) { |
| 3318 | return -1; |
| 3319 | } |
| 3320 | |
| 3321 | client->max_attempts = max_attempts; |
| 3322 | |
| 3323 | /* UNLOCK */ |
| 3324 | nc_server_ch_client_unlock(client); |
| 3325 | |
| 3326 | return 0; |
| 3327 | } |
| 3328 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3329 | /** |
| 3330 | * @brief Create a connection for an endpoint. |
| 3331 | * |
| 3332 | * Client lock is expected to be held. |
| 3333 | * |
| 3334 | * @param[in] endpt Endpoint to use. |
| 3335 | * @param[in] acquire_ctx_cb Callback for acquiring the libyang context. |
| 3336 | * @param[in] release_ctx_cb Callback for releasing the libyang context. |
| 3337 | * @param[in] ctx_cb_data Context callbacks data. |
| 3338 | * @param[out] session Created NC session. |
| 3339 | * @return NC_MSG values. |
| 3340 | */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3341 | static NC_MSG_TYPE |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3342 | nc_connect_ch_endpt(struct nc_ch_endpt *endpt, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
| 3343 | nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, struct nc_session **session) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3344 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3345 | NC_MSG_TYPE msgtype; |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3346 | const struct ly_ctx *ctx = NULL; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3347 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3348 | struct timespec ts_cur; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3349 | char *ip_host; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3350 | |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3351 | sock = nc_sock_connect(endpt->address, endpt->port, NC_CH_CONNECT_TIMEOUT, &endpt->ka, &endpt->sock_pending, &ip_host); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 3352 | if (sock < 0) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3353 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3354 | } |
| 3355 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3356 | /* acquire context */ |
| 3357 | ctx = acquire_ctx_cb(ctx_cb_data); |
| 3358 | if (!ctx) { |
| 3359 | ERR(NULL, "Failed to acquire context for a new Call Home session."); |
| 3360 | close(sock); |
| 3361 | free(ip_host); |
| 3362 | return NC_MSG_ERROR; |
| 3363 | } |
| 3364 | |
| 3365 | /* create session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3366 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3367 | if (!(*session)) { |
| 3368 | ERRMEM; |
| 3369 | close(sock); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3370 | free(ip_host); |
Michal Vasko | d530d36 | 2022-09-07 10:07:57 +0200 | [diff] [blame] | 3371 | msgtype = NC_MSG_ERROR; |
| 3372 | goto fail; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3373 | } |
| 3374 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3375 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | dc96bb9 | 2023-03-28 08:52:48 +0200 | [diff] [blame] | 3376 | (*session)->flags = NC_SESSION_SHAREDCTX | NC_SESSION_CALLHOME; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3377 | (*session)->host = ip_host; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3378 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3379 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3380 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3381 | #ifdef NC_ENABLED_SSH |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3382 | if (endpt->ti == NC_TI_LIBSSH) { |
| 3383 | (*session)->data = endpt->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3384 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3385 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3386 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3387 | if (ret < 0) { |
| 3388 | msgtype = NC_MSG_ERROR; |
| 3389 | goto fail; |
| 3390 | } else if (!ret) { |
| 3391 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3392 | goto fail; |
| 3393 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3394 | } else |
| 3395 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3396 | #ifdef NC_ENABLED_TLS |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3397 | if (endpt->ti == NC_TI_OPENSSL) { |
| 3398 | (*session)->data = endpt->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3399 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3400 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3401 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3402 | if (ret < 0) { |
| 3403 | msgtype = NC_MSG_ERROR; |
| 3404 | goto fail; |
| 3405 | } else if (!ret) { |
| 3406 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3407 | goto fail; |
| 3408 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3409 | } else |
| 3410 | #endif |
| 3411 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3412 | ERRINT; |
| 3413 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3414 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3415 | goto fail; |
| 3416 | } |
| 3417 | |
| 3418 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 3419 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3420 | |
| 3421 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3422 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3423 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3424 | goto fail; |
| 3425 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3426 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3427 | nc_timeouttime_get(&ts_cur, 0); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3428 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3429 | nc_realtime_get(&ts_cur); |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3430 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3431 | (*session)->status = NC_STATUS_RUNNING; |
| 3432 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3433 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3434 | |
| 3435 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 3436 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3437 | *session = NULL; |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3438 | if (ctx) { |
| 3439 | release_ctx_cb(ctx_cb_data); |
| 3440 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3441 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3442 | } |
| 3443 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3444 | struct nc_ch_client_thread_arg { |
| 3445 | char *client_name; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3446 | nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb; |
| 3447 | nc_server_ch_session_release_ctx_cb release_ctx_cb; |
| 3448 | void *ctx_cb_data; |
| 3449 | nc_server_ch_new_session_cb new_session_cb; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3450 | }; |
| 3451 | |
| 3452 | static struct nc_ch_client * |
| 3453 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 3454 | { |
| 3455 | struct nc_ch_client *client; |
| 3456 | |
| 3457 | while (1) { |
| 3458 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3459 | nc_server_ch_client_lock(name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3460 | if (!client) { |
| 3461 | return NULL; |
| 3462 | } |
| 3463 | if (client->ch_endpt_count) { |
| 3464 | return client; |
| 3465 | } |
| 3466 | /* no endpoints defined yet */ |
| 3467 | |
| 3468 | /* UNLOCK */ |
| 3469 | nc_server_ch_client_unlock(client); |
| 3470 | |
| 3471 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 3472 | } |
| 3473 | |
| 3474 | return NULL; |
| 3475 | } |
| 3476 | |
| 3477 | static int |
| 3478 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 3479 | { |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3480 | int ret = 0, r; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3481 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3482 | struct timespec ts; |
| 3483 | struct nc_ch_client *client; |
| 3484 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3485 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3486 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3487 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 3488 | session->flags |= NC_SESSION_CH_THREAD; |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3489 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3490 | /* give the session to the user */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3491 | if (data->new_session_cb(data->client_name, session)) { |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3492 | /* something is wrong, free the session */ |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 3493 | session->flags &= ~NC_SESSION_CH_THREAD; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3494 | |
| 3495 | /* CH UNLOCK */ |
| 3496 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
| 3497 | |
Michal Vasko | 77d56d7 | 2022-09-07 10:30:48 +0200 | [diff] [blame] | 3498 | /* session terminated, free it and release its context */ |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3499 | nc_session_free(session, NULL); |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3500 | data->release_ctx_cb(data->ctx_cb_data); |
| 3501 | return ret; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3502 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3503 | |
| 3504 | do { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3505 | nc_timeouttime_get(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3506 | |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3507 | /* CH COND WAIT */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3508 | r = pthread_cond_clockwait(&session->opts.server.ch_cond, &session->opts.server.ch_lock, COMPAT_CLOCK_ID, &ts); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3509 | if (!r) { |
| 3510 | /* we were woken up, something probably happened */ |
| 3511 | if (session->status != NC_STATUS_RUNNING) { |
| 3512 | break; |
| 3513 | } |
| 3514 | } else if (r != ETIMEDOUT) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3515 | ERR(session, "Pthread condition timedwait failed (%s).", strerror(r)); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3516 | ret = -1; |
| 3517 | break; |
Michal Vasko | 2e39ed9 | 2018-03-12 13:51:44 +0100 | [diff] [blame] | 3518 | } |
| 3519 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3520 | /* check whether the client was not removed */ |
| 3521 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3522 | nc_server_ch_client_lock(data->client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3523 | if (!client) { |
| 3524 | /* client was removed, finish thread */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3525 | VRB(session, "Call Home client \"%s\" removed, but an established session will not be terminated.", |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3526 | data->client_name); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3527 | ret = 1; |
| 3528 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3529 | } |
| 3530 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3531 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3532 | idle_timeout = client->conn.period.idle_timeout; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3533 | } else { |
| 3534 | idle_timeout = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3535 | } |
| 3536 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 3537 | nc_timeouttime_get(&ts, 0); |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3538 | if (!nc_session_get_notif_status(session) && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3539 | VRB(session, "Call Home client \"%s\": session idle timeout elapsed.", client->name); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3540 | session->status = NC_STATUS_INVALID; |
| 3541 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 3542 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3543 | |
| 3544 | /* UNLOCK */ |
| 3545 | nc_server_ch_client_unlock(client); |
| 3546 | |
| 3547 | } while (session->status == NC_STATUS_RUNNING); |
| 3548 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame] | 3549 | /* signal to nc_session_free() that CH thread is terminating */ |
| 3550 | session->flags &= ~NC_SESSION_CH_THREAD; |
| 3551 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3552 | |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3553 | /* CH UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3554 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3555 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3556 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | static void * |
| 3560 | nc_ch_client_thread(void *arg) |
| 3561 | { |
| 3562 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 3563 | NC_MSG_TYPE msgtype; |
| 3564 | uint8_t cur_attempts = 0; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3565 | uint16_t next_endpt_index; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3566 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3567 | struct nc_ch_endpt *cur_endpt; |
| 3568 | struct nc_session *session; |
| 3569 | struct nc_ch_client *client; |
Michal Vasko | 5d0a1e3 | 2022-09-07 07:46:36 +0200 | [diff] [blame] | 3570 | uint32_t client_id, reconnect_in; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3571 | |
| 3572 | /* LOCK */ |
| 3573 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3574 | if (!client) { |
| 3575 | goto cleanup; |
| 3576 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3577 | client_id = client->id; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3578 | |
| 3579 | cur_endpt = &client->ch_endpts[0]; |
| 3580 | cur_endpt_name = strdup(cur_endpt->name); |
| 3581 | |
| 3582 | while (1) { |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3583 | if (!cur_attempts) { |
| 3584 | VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" connecting...", data->client_name, cur_endpt_name); |
| 3585 | } |
Michal Vasko | 58bac1c | 2022-03-24 15:25:26 +0100 | [diff] [blame] | 3586 | msgtype = nc_connect_ch_endpt(cur_endpt, data->acquire_ctx_cb, data->release_ctx_cb, data->ctx_cb_data, &session); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3587 | |
| 3588 | if (msgtype == NC_MSG_HELLO) { |
| 3589 | /* UNLOCK */ |
| 3590 | nc_server_ch_client_unlock(client); |
| 3591 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3592 | VRB(NULL, "Call Home client \"%s\" session %u established.", data->client_name, session->id); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3593 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 3594 | goto cleanup; |
| 3595 | } |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3596 | VRB(NULL, "Call Home client \"%s\" session terminated.", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3597 | |
| 3598 | /* LOCK */ |
| 3599 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3600 | if (!client) { |
| 3601 | goto cleanup; |
| 3602 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3603 | if (client->id != client_id) { |
| 3604 | nc_server_ch_client_unlock(client); |
| 3605 | goto cleanup; |
| 3606 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3607 | |
| 3608 | /* session changed status -> it was disconnected for whatever reason, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3609 | * persistent connection immediately tries to reconnect, periodic connects at specific times */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3610 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 18e1fa0 | 2021-11-29 09:02:05 +0100 | [diff] [blame] | 3611 | if (client->conn.period.anchor_time) { |
| 3612 | /* anchored */ |
| 3613 | reconnect_in = (time(NULL) - client->conn.period.anchor_time) % (client->conn.period.period * 60); |
| 3614 | } else { |
| 3615 | /* fixed timeout */ |
| 3616 | reconnect_in = client->conn.period.period * 60; |
| 3617 | } |
| 3618 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3619 | /* UNLOCK */ |
| 3620 | nc_server_ch_client_unlock(client); |
| 3621 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3622 | /* sleep until we should reconnect TODO wake up sometimes to check for new notifications */ |
Michal Vasko | 75b836e | 2022-09-07 07:51:45 +0200 | [diff] [blame] | 3623 | VRB(NULL, "Call Home client \"%s\" reconnecting in %" PRIu32 " seconds.", data->client_name, reconnect_in); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3624 | sleep(reconnect_in); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3625 | |
| 3626 | /* LOCK */ |
| 3627 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3628 | if (!client) { |
| 3629 | goto cleanup; |
| 3630 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3631 | if (client->id != client_id) { |
| 3632 | nc_server_ch_client_unlock(client); |
| 3633 | goto cleanup; |
| 3634 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | /* set next endpoint to try */ |
| 3638 | if (client->start_with == NC_CH_FIRST_LISTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3639 | next_endpt_index = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3640 | } else if (client->start_with == NC_CH_LAST_CONNECTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3641 | /* we keep the current one but due to unlock/lock we have to find it again */ |
| 3642 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3643 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
| 3644 | break; |
| 3645 | } |
| 3646 | } |
| 3647 | if (next_endpt_index >= client->ch_endpt_count) { |
| 3648 | /* endpoint was removed, start with the first one */ |
| 3649 | next_endpt_index = 0; |
| 3650 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3651 | } else { |
| 3652 | /* just get a random index */ |
| 3653 | next_endpt_index = rand() % client->ch_endpt_count; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3656 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3657 | /* UNLOCK */ |
| 3658 | nc_server_ch_client_unlock(client); |
| 3659 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3660 | /* session was not created */ |
Michal Vasko | d8951c7 | 2022-09-26 09:39:29 +0200 | [diff] [blame] | 3661 | sleep(NC_CH_ENDPT_BACKOFF_WAIT); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3662 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3663 | /* LOCK */ |
| 3664 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3665 | if (!client) { |
| 3666 | goto cleanup; |
| 3667 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3668 | if (client->id != client_id) { |
| 3669 | nc_server_ch_client_unlock(client); |
| 3670 | goto cleanup; |
| 3671 | } |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3672 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3673 | ++cur_attempts; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3674 | |
| 3675 | /* try to find our endpoint again */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3676 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3677 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3678 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3679 | } |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3680 | } |
| 3681 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3682 | if (next_endpt_index >= client->ch_endpt_count) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3683 | /* endpoint was removed, start with the first one */ |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3684 | VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" removed.", data->client_name, cur_endpt_name); |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3685 | next_endpt_index = 0; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3686 | cur_attempts = 0; |
| 3687 | } else if (cur_attempts == client->max_attempts) { |
| 3688 | /* we have tried to connect to this endpoint enough times */ |
Michal Vasko | 056f53c | 2022-10-21 13:38:15 +0200 | [diff] [blame] | 3689 | VRB(NULL, "Call Home client \"%s\" endpoint \"%s\" failed connection attempt limit %" PRIu8 " reached.", |
| 3690 | data->client_name, cur_endpt_name, client->max_attempts); |
| 3691 | |
| 3692 | /* clear a pending socket, if any */ |
| 3693 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3694 | if (cur_endpt->sock_pending > -1) { |
| 3695 | close(cur_endpt->sock_pending); |
| 3696 | cur_endpt->sock_pending = -1; |
| 3697 | } |
| 3698 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3699 | if (next_endpt_index < client->ch_endpt_count - 1) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3700 | /* just go to the next endpoint */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3701 | ++next_endpt_index; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3702 | } else { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3703 | /* cur_endpoint is the last, start with the first one */ |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 3704 | next_endpt_index = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3705 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3706 | cur_attempts = 0; |
| 3707 | } /* else we keep the current one */ |
| 3708 | } |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3709 | |
| 3710 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3711 | free(cur_endpt_name); |
| 3712 | cur_endpt_name = strdup(cur_endpt->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | cleanup: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3716 | VRB(NULL, "Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3717 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3718 | free(data->client_name); |
| 3719 | free(data); |
| 3720 | return NULL; |
| 3721 | } |
| 3722 | |
| 3723 | API int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3724 | nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
| 3725 | nc_server_ch_session_release_ctx_cb release_ctx_cb, void *ctx_cb_data, nc_server_ch_new_session_cb new_session_cb) |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3726 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3727 | int ret; |
| 3728 | pthread_t tid; |
| 3729 | struct nc_ch_client_thread_arg *arg; |
| 3730 | |
| 3731 | if (!client_name) { |
| 3732 | ERRARG("client_name"); |
| 3733 | return -1; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3734 | } else if (!acquire_ctx_cb) { |
| 3735 | ERRARG("acquire_ctx_cb"); |
| 3736 | return -1; |
| 3737 | } else if (!release_ctx_cb) { |
| 3738 | ERRARG("release_ctx_cb"); |
| 3739 | return -1; |
| 3740 | } else if (!new_session_cb) { |
| 3741 | ERRARG("new_session_cb"); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3742 | return -1; |
| 3743 | } |
| 3744 | |
| 3745 | arg = malloc(sizeof *arg); |
| 3746 | if (!arg) { |
| 3747 | ERRMEM; |
| 3748 | return -1; |
| 3749 | } |
| 3750 | arg->client_name = strdup(client_name); |
| 3751 | if (!arg->client_name) { |
| 3752 | ERRMEM; |
| 3753 | free(arg); |
| 3754 | return -1; |
| 3755 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3756 | arg->acquire_ctx_cb = acquire_ctx_cb; |
| 3757 | arg->release_ctx_cb = release_ctx_cb; |
| 3758 | arg->ctx_cb_data = ctx_cb_data; |
| 3759 | arg->new_session_cb = new_session_cb; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3760 | |
| 3761 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 3762 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3763 | ERR(NULL, "Creating a new thread failed (%s).", strerror(ret)); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3764 | free(arg->client_name); |
| 3765 | free(arg); |
| 3766 | return -1; |
| 3767 | } |
| 3768 | /* the thread now manages arg */ |
| 3769 | |
| 3770 | pthread_detach(tid); |
| 3771 | |
| 3772 | return 0; |
| 3773 | } |
| 3774 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3775 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3776 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3777 | API time_t |
| 3778 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3779 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3780 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3781 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3782 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3783 | } |
| 3784 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3785 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3786 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3787 | |
| 3788 | API void |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3789 | nc_session_inc_notif_status(struct nc_session *session) |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3790 | { |
| 3791 | if (!session || (session->side != NC_SERVER)) { |
| 3792 | ERRARG("session"); |
| 3793 | return; |
| 3794 | } |
| 3795 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3796 | /* NTF STATUS LOCK */ |
| 3797 | pthread_mutex_lock(&session->opts.server.ntf_status_lock); |
| 3798 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3799 | ++session->opts.server.ntf_status; |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3800 | |
| 3801 | /* NTF STATUS UNLOCK */ |
| 3802 | pthread_mutex_unlock(&session->opts.server.ntf_status_lock); |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | API void |
| 3806 | nc_session_dec_notif_status(struct nc_session *session) |
| 3807 | { |
| 3808 | if (!session || (session->side != NC_SERVER)) { |
| 3809 | ERRARG("session"); |
| 3810 | return; |
| 3811 | } |
| 3812 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3813 | /* NTF STATUS LOCK */ |
| 3814 | pthread_mutex_lock(&session->opts.server.ntf_status_lock); |
| 3815 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3816 | if (session->opts.server.ntf_status) { |
| 3817 | --session->opts.server.ntf_status; |
| 3818 | } |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3819 | |
| 3820 | /* NTF STATUS UNLOCK */ |
| 3821 | pthread_mutex_unlock(&session->opts.server.ntf_status_lock); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | API int |
| 3825 | nc_session_get_notif_status(const struct nc_session *session) |
| 3826 | { |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3827 | uint32_t ntf_status; |
| 3828 | |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3829 | if (!session || (session->side != NC_SERVER)) { |
| 3830 | ERRARG("session"); |
| 3831 | return 0; |
| 3832 | } |
| 3833 | |
Michal Vasko | df68e7e | 2022-04-21 11:04:00 +0200 | [diff] [blame] | 3834 | /* NTF STATUS LOCK */ |
| 3835 | pthread_mutex_lock(&((struct nc_session *)session)->opts.server.ntf_status_lock); |
| 3836 | |
| 3837 | ntf_status = session->opts.server.ntf_status; |
| 3838 | |
| 3839 | /* NTF STATUS UNLOCK */ |
| 3840 | pthread_mutex_unlock(&((struct nc_session *)session)->opts.server.ntf_status_lock); |
| 3841 | |
| 3842 | return ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3843 | } |