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