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 | |
| 937 | nc_gettimespec_mono(&ts_cur); |
| 938 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 939 | nc_gettimespec_real(&ts_cur); |
| 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 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1017 | nc_gettimespec_real(&ts); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 1018 | nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1019 | |
| 1020 | /* LOCK */ |
| 1021 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 1022 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1023 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1024 | return -1; |
| 1025 | } |
| 1026 | |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1027 | /* check that the queue is long enough */ |
Michal Vasko | 8bc747c | 2018-02-09 16:37:04 +0100 | [diff] [blame] | 1028 | if (ps->queue_len == NC_PS_QUEUE_SIZE) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1029 | 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] | 1030 | pthread_mutex_unlock(&ps->lock); |
| 1031 | return -1; |
| 1032 | } |
Michal Vasko | 74c345f | 2018-02-07 10:37:11 +0100 | [diff] [blame] | 1033 | |
| 1034 | /* add ourselves into the queue */ |
| 1035 | nc_ps_queue_add_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1036 | 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] | 1037 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1038 | |
| 1039 | /* is it our turn? */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1040 | while (ps->queue[ps->queue_begin] != *id) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1041 | nc_gettimespec_real(&ts); |
Michal Vasko | 2b76809 | 2018-02-12 16:37:12 +0100 | [diff] [blame] | 1042 | nc_addtimespec(&ts, NC_PS_QUEUE_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1043 | |
| 1044 | ret = pthread_cond_timedwait(&ps->cond, &ps->lock, &ts); |
| 1045 | if (ret) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1046 | /** |
| 1047 | * This may happen when another thread releases the lock and broadcasts the condition |
| 1048 | * and this thread had already timed out. When this thread is scheduled, it returns timed out error |
| 1049 | * but when actually this thread was ready for condition. |
| 1050 | */ |
preetbhansali | 629dfc4 | 2018-12-17 16:04:40 +0530 | [diff] [blame] | 1051 | if ((ETIMEDOUT == ret) && (ps->queue[ps->queue_begin] == *id)) { |
preetbhansali | f3edf49 | 2018-12-14 17:53:32 +0530 | [diff] [blame] | 1052 | break; |
| 1053 | } |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 1054 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1055 | 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] | 1056 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1057 | nc_ps_queue_remove_id(ps, *id); |
| 1058 | pthread_mutex_unlock(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1059 | return -1; |
| 1060 | } |
| 1061 | } |
| 1062 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1063 | /* UNLOCK */ |
| 1064 | pthread_mutex_unlock(&ps->lock); |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Michal Vasko | f04a52a | 2016-04-07 10:52:10 +0200 | [diff] [blame] | 1069 | int |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1070 | 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] | 1071 | { |
| 1072 | int ret; |
| 1073 | struct timespec ts; |
| 1074 | |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1075 | nc_gettimespec_real(&ts); |
Michal Vasko | 81c5b30 | 2017-03-15 12:10:40 +0100 | [diff] [blame] | 1076 | nc_addtimespec(&ts, NC_PS_LOCK_TIMEOUT); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1077 | |
| 1078 | /* LOCK */ |
| 1079 | ret = pthread_mutex_timedlock(&ps->lock, &ts); |
| 1080 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1081 | ERR(NULL, "%s: failed to lock a pollsession (%s).", func, strerror(ret)); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1082 | ret = -1; |
| 1083 | } |
| 1084 | |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1085 | /* we must be the first, it was our turn after all, right? */ |
| 1086 | if (ps->queue[ps->queue_begin] != id) { |
| 1087 | ERRINT; |
Michal Vasko | b1a094b | 2016-10-05 14:04:52 +0200 | [diff] [blame] | 1088 | /* UNLOCK */ |
| 1089 | if (!ret) { |
| 1090 | pthread_mutex_unlock(&ps->lock); |
| 1091 | } |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1092 | return -1; |
| 1093 | } |
| 1094 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1095 | /* remove ourselves from the queue */ |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1096 | nc_ps_queue_remove_id(ps, id); |
Michal Vasko | fdba4a3 | 2022-01-05 12:13:53 +0100 | [diff] [blame] | 1097 | 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] | 1098 | ps->queue[ps->queue_begin], ps->queue_len); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1099 | |
| 1100 | /* broadcast to all other threads that the queue moved */ |
| 1101 | pthread_cond_broadcast(&ps->cond); |
| 1102 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1103 | /* UNLOCK */ |
| 1104 | if (!ret) { |
| 1105 | pthread_mutex_unlock(&ps->lock); |
| 1106 | } |
| 1107 | |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1111 | API struct nc_pollsession * |
| 1112 | nc_ps_new(void) |
| 1113 | { |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1114 | struct nc_pollsession *ps; |
| 1115 | |
| 1116 | ps = calloc(1, sizeof(struct nc_pollsession)); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1117 | if (!ps) { |
| 1118 | ERRMEM; |
| 1119 | return NULL; |
| 1120 | } |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1121 | pthread_cond_init(&ps->cond, NULL); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1122 | pthread_mutex_init(&ps->lock, NULL); |
| 1123 | |
| 1124 | return ps; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | API void |
| 1128 | nc_ps_free(struct nc_pollsession *ps) |
| 1129 | { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1130 | uint16_t i; |
| 1131 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 1132 | if (!ps) { |
| 1133 | return; |
| 1134 | } |
| 1135 | |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1136 | if (ps->queue_len) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1137 | 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] | 1138 | } |
| 1139 | |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1140 | for (i = 0; i < ps->session_count; i++) { |
| 1141 | free(ps->sessions[i]); |
| 1142 | } |
| 1143 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1144 | free(ps->sessions); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1145 | pthread_mutex_destroy(&ps->lock); |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1146 | pthread_cond_destroy(&ps->cond); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1147 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1148 | free(ps); |
| 1149 | } |
| 1150 | |
| 1151 | API int |
| 1152 | nc_ps_add_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1153 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1154 | uint8_t q_id; |
| 1155 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1156 | if (!ps) { |
| 1157 | ERRARG("ps"); |
| 1158 | return -1; |
| 1159 | } else if (!session) { |
| 1160 | ERRARG("session"); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1161 | return -1; |
| 1162 | } |
| 1163 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1164 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1165 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1166 | return -1; |
| 1167 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1168 | |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1169 | ++ps->session_count; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1170 | ps->sessions = nc_realloc(ps->sessions, ps->session_count * sizeof *ps->sessions); |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1171 | if (!ps->sessions) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1172 | ERRMEM; |
| 1173 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1174 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1175 | return -1; |
| 1176 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1177 | ps->sessions[ps->session_count - 1] = calloc(1, sizeof **ps->sessions); |
| 1178 | if (!ps->sessions[ps->session_count - 1]) { |
| 1179 | ERRMEM; |
| 1180 | --ps->session_count; |
| 1181 | /* UNLOCK */ |
| 1182 | nc_ps_unlock(ps, q_id, __func__); |
| 1183 | return -1; |
| 1184 | } |
| 1185 | ps->sessions[ps->session_count - 1]->session = session; |
| 1186 | ps->sessions[ps->session_count - 1]->state = NC_PS_STATE_NONE; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1187 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1188 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1189 | return nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1192 | static int |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1193 | _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] | 1194 | { |
| 1195 | uint16_t i; |
| 1196 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1197 | if (index >= 0) { |
| 1198 | i = (uint16_t)index; |
| 1199 | goto remove; |
| 1200 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1201 | for (i = 0; i < ps->session_count; ++i) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1202 | if (ps->sessions[i]->session == session) { |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1203 | remove: |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1204 | --ps->session_count; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1205 | if (i <= ps->session_count) { |
| 1206 | free(ps->sessions[i]); |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1207 | ps->sessions[i] = ps->sessions[ps->session_count]; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1208 | } |
| 1209 | if (!ps->session_count) { |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1210 | free(ps->sessions); |
| 1211 | ps->sessions = NULL; |
Michal Vasko | 5800573 | 2016-02-02 15:50:52 +0100 | [diff] [blame] | 1212 | } |
Michal Vasko | 11d2f6a | 2017-02-02 11:15:06 +0100 | [diff] [blame] | 1213 | ps->last_event_session = 0; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | } |
| 1217 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 1218 | return -1; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1221 | API int |
| 1222 | nc_ps_del_session(struct nc_pollsession *ps, struct nc_session *session) |
| 1223 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1224 | uint8_t q_id; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1225 | int ret, ret2; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1226 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1227 | if (!ps) { |
| 1228 | ERRARG("ps"); |
| 1229 | return -1; |
| 1230 | } else if (!session) { |
| 1231 | ERRARG("session"); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1232 | return -1; |
| 1233 | } |
| 1234 | |
| 1235 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1236 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1237 | return -1; |
| 1238 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1239 | |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1240 | ret = _nc_ps_del_session(ps, session, -1); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1241 | |
| 1242 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1243 | ret2 = nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1244 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1245 | return ret || ret2 ? -1 : 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1246 | } |
| 1247 | |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1248 | API struct nc_session * |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1249 | nc_ps_get_session(const struct nc_pollsession *ps, uint16_t idx) |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1250 | { |
| 1251 | uint8_t q_id; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1252 | struct nc_session *ret = NULL; |
| 1253 | |
| 1254 | if (!ps) { |
| 1255 | ERRARG("ps"); |
| 1256 | return NULL; |
| 1257 | } |
| 1258 | |
| 1259 | /* LOCK */ |
| 1260 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | |
Michal Vasko | 4871c9d | 2017-10-09 14:48:39 +0200 | [diff] [blame] | 1264 | if (idx < ps->session_count) { |
| 1265 | ret = ps->sessions[idx]->session; |
Michal Vasko | e1ee05b | 2017-03-21 10:10:18 +0100 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /* UNLOCK */ |
| 1269 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1270 | |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1274 | API uint16_t |
| 1275 | nc_ps_session_count(struct nc_pollsession *ps) |
| 1276 | { |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1277 | uint8_t q_id; |
| 1278 | uint16_t session_count; |
| 1279 | |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1280 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1281 | ERRARG("ps"); |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
Michal Vasko | 4700394 | 2019-03-14 12:25:23 +0100 | [diff] [blame] | 1285 | /* LOCK (just for memory barrier so that we read the current value) */ |
| 1286 | if (nc_ps_lock((struct nc_pollsession *)ps, &q_id, __func__)) { |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | session_count = ps->session_count; |
| 1291 | |
| 1292 | /* UNLOCK */ |
| 1293 | nc_ps_unlock((struct nc_pollsession *)ps, q_id, __func__); |
| 1294 | |
| 1295 | return session_count; |
Michal Vasko | 0fdb7ac | 2016-03-01 09:03:12 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1298 | /* 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] | 1299 | * returns: NC_PSPOLL_ERROR, |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1300 | * NC_PSPOLL_TIMEOUT, |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1301 | * NC_PSPOLL_BAD_RPC | NC_PSPOLL_REPLY_ERROR, |
| 1302 | * NC_PSPOLL_RPC |
| 1303 | */ |
| 1304 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1305 | 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] | 1306 | { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1307 | struct ly_in *msg; |
Radek Krejci | f93c7d4 | 2016-04-06 13:41:15 +0200 | [diff] [blame] | 1308 | struct nc_server_reply *reply = NULL; |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1309 | struct lyd_node *e; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1310 | int r, ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1311 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1312 | if (!session) { |
| 1313 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1314 | return NC_PSPOLL_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1315 | } else if (!rpc) { |
| 1316 | ERRARG("rpc"); |
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 | } else if ((session->status != NC_STATUS_RUNNING) || (session->side != NC_SERVER)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1319 | ERR(session, "Invalid session to receive RPCs."); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1320 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1323 | *rpc = NULL; |
| 1324 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1325 | /* get a message */ |
| 1326 | r = nc_read_msg_io(session, io_timeout, &msg, 0); |
| 1327 | if (r == -2) { |
| 1328 | /* malformed message */ |
| 1329 | ret = NC_PSPOLL_REPLY_ERROR; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1330 | 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] | 1331 | goto send_reply; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1332 | } |
| 1333 | if (r == -1) { |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1334 | return NC_PSPOLL_ERROR; |
| 1335 | } else if (!r) { |
| 1336 | return NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1337 | } |
| 1338 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1339 | *rpc = calloc(1, sizeof **rpc); |
| 1340 | if (!*rpc) { |
| 1341 | ERRMEM; |
| 1342 | ret = NC_PSPOLL_REPLY_ERROR; |
| 1343 | goto cleanup; |
| 1344 | } |
| 1345 | |
| 1346 | /* parse the RPC */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1347 | 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] | 1348 | /* bad RPC received */ |
| 1349 | ret = NC_PSPOLL_REPLY_ERROR | NC_PSPOLL_BAD_RPC; |
| 1350 | |
| 1351 | if ((*rpc)->envp) { |
| 1352 | /* at least the envelopes were parsed */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1353 | e = nc_err(session->ctx, NC_ERR_OP_FAILED, NC_ERR_TYPE_APP); |
| 1354 | nc_err_set_msg(e, ly_errmsg(session->ctx), "en"); |
Michal Vasko | 939ffce | 2021-04-12 13:02:01 +0200 | [diff] [blame] | 1355 | reply = nc_server_reply_err(e); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1356 | } else if (session->version == NC_VERSION_11) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1357 | /* completely malformed message, NETCONF version 1.1 defines sending error reply from |
| 1358 | * the server (RFC 6241 sec. 3) */ |
| 1359 | 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] | 1360 | } |
| 1361 | |
| 1362 | send_reply: |
| 1363 | if (reply) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1364 | 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] | 1365 | nc_server_reply_free(reply); |
| 1366 | if (r != NC_MSG_REPLY) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1367 | ERR(session, "Failed to write reply (%s), terminating session.", nc_msgtype2str[r]); |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1368 | if (session->status != NC_STATUS_INVALID) { |
| 1369 | session->status = NC_STATUS_INVALID; |
| 1370 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | } else { |
| 1375 | ret = NC_PSPOLL_RPC; |
| 1376 | } |
| 1377 | |
| 1378 | cleanup: |
| 1379 | ly_in_free(msg, 1); |
| 1380 | if (ret != NC_PSPOLL_RPC) { |
| 1381 | nc_server_rpc_free(*rpc); |
| 1382 | *rpc = NULL; |
| 1383 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1384 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1387 | API void |
| 1388 | nc_set_global_rpc_clb(nc_rpc_clb clb) |
| 1389 | { |
| 1390 | global_rpc_clb = clb; |
| 1391 | } |
| 1392 | |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1393 | API NC_MSG_TYPE |
| 1394 | nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout) |
| 1395 | { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1396 | NC_MSG_TYPE ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1397 | |
| 1398 | /* check parameters */ |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 1399 | if (!session || (session->side != NC_SERVER) || !session->opts.server.ntf_status) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1400 | ERRARG("session"); |
| 1401 | return NC_MSG_ERROR; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1402 | } else if (!notif || !notif->ntf || !notif->eventtime) { |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1403 | ERRARG("notif"); |
| 1404 | return NC_MSG_ERROR; |
| 1405 | } |
| 1406 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1407 | /* we do not need RPC lock for this, IO lock will be acquired properly */ |
| 1408 | ret = nc_write_msg_io(session, timeout, NC_MSG_NOTIF, notif); |
Michal Vasko | 8fe604c | 2020-02-10 15:25:04 +0100 | [diff] [blame] | 1409 | if (ret != NC_MSG_NOTIF) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1410 | ERR(session, "Failed to write notification (%s).", nc_msgtype2str[ret]); |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1411 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1412 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1413 | return ret; |
Radek Krejci | 93e8022 | 2016-10-03 13:34:25 +0200 | [diff] [blame] | 1414 | } |
| 1415 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1416 | /* 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] | 1417 | * returns: NC_PSPOLL_ERROR, |
| 1418 | * NC_PSPOLL_ERROR | NC_PSPOLL_REPLY_ERROR, |
| 1419 | * NC_PSPOLL_REPLY_ERROR, |
| 1420 | * 0 |
| 1421 | */ |
| 1422 | static int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1423 | 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] | 1424 | { |
| 1425 | nc_rpc_clb clb; |
| 1426 | struct nc_server_reply *reply; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1427 | const struct lysc_node *rpc_act = NULL; |
| 1428 | struct lyd_node *elem; |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1429 | int ret = 0; |
| 1430 | NC_MSG_TYPE r; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1431 | |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1432 | if (!rpc) { |
| 1433 | ERRINT; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1434 | return NC_PSPOLL_ERROR; |
Michal Vasko | 4a827e5 | 2016-03-03 10:59:00 +0100 | [diff] [blame] | 1435 | } |
| 1436 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1437 | if (rpc->rpc->schema->nodetype == LYS_RPC) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1438 | /* RPC */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1439 | rpc_act = rpc->rpc->schema; |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1440 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1441 | /* action */ |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1442 | LYD_TREE_DFS_BEGIN(rpc->rpc, elem) { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1443 | if (elem->schema->nodetype == LYS_ACTION) { |
| 1444 | rpc_act = elem->schema; |
| 1445 | break; |
| 1446 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1447 | LYD_TREE_DFS_END(rpc->rpc, elem); |
fanchanghu | 966f2de | 2016-07-21 02:28:57 -0400 | [diff] [blame] | 1448 | } |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1449 | if (!rpc_act) { |
| 1450 | ERRINT; |
| 1451 | return NC_PSPOLL_ERROR; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | if (!rpc_act->priv) { |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1456 | if (!global_rpc_clb) { |
| 1457 | /* no callback, reply with a not-implemented error */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1458 | 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] | 1459 | } else { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1460 | reply = global_rpc_clb(rpc->rpc, session); |
Petr A. Sokolov | 16284ca | 2019-02-04 09:02:40 +0300 | [diff] [blame] | 1461 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1462 | } else { |
Michal Vasko | 90e8e69 | 2016-07-13 12:27:57 +0200 | [diff] [blame] | 1463 | clb = (nc_rpc_clb)rpc_act->priv; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1464 | reply = clb(rpc->rpc, session); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | if (!reply) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1468 | 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] | 1469 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1470 | 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] | 1471 | if (reply->type == NC_RPL_ERROR) { |
| 1472 | ret |= NC_PSPOLL_REPLY_ERROR; |
| 1473 | } |
| 1474 | nc_server_reply_free(reply); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1475 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1476 | if (r != NC_MSG_REPLY) { |
Michal Vasko | 1546949 | 2021-06-09 08:40:48 +0200 | [diff] [blame] | 1477 | ERR(session, "Failed to write reply (%s).", nc_msgtype2str[r]); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1478 | ret |= NC_PSPOLL_ERROR; |
| 1479 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1480 | |
| 1481 | /* special case if term_reason was set in callback, last reply was sent (needed for <close-session> if nothing else) */ |
| 1482 | if ((session->status == NC_STATUS_RUNNING) && (session->term_reason != NC_SESSION_TERM_NONE)) { |
| 1483 | session->status = NC_STATUS_INVALID; |
| 1484 | } |
| 1485 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1486 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1487 | } |
| 1488 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1489 | /* session must be running and session RPC lock held! |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1490 | * returns: NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR, (msg filled) |
| 1491 | * NC_PSPOLL_ERROR, (msg filled) |
| 1492 | * NC_PSPOLL_TIMEOUT, |
| 1493 | * NC_PSPOLL_RPC (some application data available), |
| 1494 | * NC_PSPOLL_SSH_CHANNEL, |
| 1495 | * NC_PSPOLL_SSH_MSG |
| 1496 | */ |
| 1497 | static int |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1498 | 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] | 1499 | { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1500 | struct pollfd pfd; |
Michal Vasko | 2260f55 | 2018-06-06 10:06:12 +0200 | [diff] [blame] | 1501 | int r, ret = 0; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1502 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1503 | #ifdef NC_ENABLED_SSH |
| 1504 | struct nc_session *new; |
| 1505 | #endif |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1506 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1507 | /* check timeout first */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1508 | if (!(session->flags & NC_SESSION_CALLHOME) && !session->opts.server.ntf_status && server_opts.idle_timeout && |
| 1509 | (now_mono >= session->opts.server.last_rpc + server_opts.idle_timeout)) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1510 | sprintf(msg, "session idle timeout elapsed"); |
| 1511 | session->status = NC_STATUS_INVALID; |
| 1512 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 1513 | return NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1514 | } |
| 1515 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1516 | r = nc_session_io_lock(session, io_timeout, __func__); |
| 1517 | if (r < 0) { |
| 1518 | sprintf(msg, "session IO lock failed to be acquired"); |
| 1519 | return NC_PSPOLL_ERROR; |
| 1520 | } else if (!r) { |
| 1521 | return NC_PSPOLL_TIMEOUT; |
| 1522 | } |
| 1523 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1524 | switch (session->ti_type) { |
| 1525 | #ifdef NC_ENABLED_SSH |
| 1526 | case NC_TI_LIBSSH: |
| 1527 | r = ssh_channel_poll_timeout(session->ti.libssh.channel, 0, 0); |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1528 | if (r == SSH_EOF) { |
| 1529 | sprintf(msg, "SSH channel unexpected EOF"); |
| 1530 | session->status = NC_STATUS_INVALID; |
| 1531 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1532 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1533 | } else if (r == SSH_ERROR) { |
| 1534 | 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] | 1535 | session->status = NC_STATUS_INVALID; |
| 1536 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1537 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1538 | } else if (!r) { |
| 1539 | if (session->flags & NC_SESSION_SSH_NEW_MSG) { |
| 1540 | /* new SSH message */ |
| 1541 | session->flags &= ~NC_SESSION_SSH_NEW_MSG; |
| 1542 | if (session->ti.libssh.next) { |
| 1543 | 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] | 1544 | if ((new->status == NC_STATUS_STARTING) && new->ti.libssh.channel && |
| 1545 | (new->flags & NC_SESSION_SSH_SUBSYS_NETCONF)) { |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1546 | /* new NETCONF SSH channel */ |
| 1547 | ret = NC_PSPOLL_SSH_CHANNEL; |
| 1548 | break; |
| 1549 | } |
| 1550 | } |
| 1551 | if (new != session) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1552 | break; |
| 1553 | } |
| 1554 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1555 | |
Michal Vasko | 8dcaa88 | 2017-10-19 14:28:42 +0200 | [diff] [blame] | 1556 | /* just some SSH message */ |
| 1557 | ret = NC_PSPOLL_SSH_MSG; |
| 1558 | } else { |
| 1559 | ret = NC_PSPOLL_TIMEOUT; |
| 1560 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1561 | } else { |
| 1562 | /* we have some application data */ |
| 1563 | ret = NC_PSPOLL_RPC; |
| 1564 | } |
| 1565 | break; |
| 1566 | #endif |
| 1567 | #ifdef NC_ENABLED_TLS |
| 1568 | case NC_TI_OPENSSL: |
| 1569 | r = SSL_pending(session->ti.tls); |
| 1570 | if (!r) { |
| 1571 | /* no data pending in the SSL buffer, poll fd */ |
| 1572 | pfd.fd = SSL_get_rfd(session->ti.tls); |
| 1573 | if (pfd.fd < 0) { |
| 1574 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1575 | ret = NC_PSPOLL_ERROR; |
| 1576 | break; |
| 1577 | } |
| 1578 | pfd.events = POLLIN; |
| 1579 | pfd.revents = 0; |
| 1580 | r = poll(&pfd, 1, 0); |
| 1581 | |
| 1582 | if ((r < 0) && (errno != EINTR)) { |
| 1583 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1584 | session->status = NC_STATUS_INVALID; |
| 1585 | ret = NC_PSPOLL_ERROR; |
| 1586 | } else if (r > 0) { |
| 1587 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1588 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1589 | session->status = NC_STATUS_INVALID; |
| 1590 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1591 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1592 | } else if (pfd.revents & POLLERR) { |
| 1593 | sprintf(msg, "communication socket error"); |
| 1594 | session->status = NC_STATUS_INVALID; |
| 1595 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1596 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1597 | } else { |
| 1598 | ret = NC_PSPOLL_RPC; |
| 1599 | } |
| 1600 | } else { |
| 1601 | ret = NC_PSPOLL_TIMEOUT; |
| 1602 | } |
| 1603 | } else { |
| 1604 | ret = NC_PSPOLL_RPC; |
| 1605 | } |
| 1606 | break; |
| 1607 | #endif |
| 1608 | case NC_TI_FD: |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 1609 | case NC_TI_UNIX: |
| 1610 | 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] | 1611 | pfd.events = POLLIN; |
| 1612 | pfd.revents = 0; |
| 1613 | r = poll(&pfd, 1, 0); |
| 1614 | |
| 1615 | if ((r < 0) && (errno != EINTR)) { |
| 1616 | sprintf(msg, "poll failed (%s)", strerror(errno)); |
| 1617 | session->status = NC_STATUS_INVALID; |
| 1618 | ret = NC_PSPOLL_ERROR; |
| 1619 | } else if (r > 0) { |
| 1620 | if (pfd.revents & (POLLHUP | POLLNVAL)) { |
| 1621 | sprintf(msg, "communication socket unexpectedly closed"); |
| 1622 | session->status = NC_STATUS_INVALID; |
| 1623 | session->term_reason = NC_SESSION_TERM_DROPPED; |
| 1624 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1625 | } else if (pfd.revents & POLLERR) { |
| 1626 | sprintf(msg, "communication socket error"); |
| 1627 | session->status = NC_STATUS_INVALID; |
| 1628 | session->term_reason = NC_SESSION_TERM_OTHER; |
| 1629 | ret = NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
| 1630 | } else { |
| 1631 | ret = NC_PSPOLL_RPC; |
| 1632 | } |
| 1633 | } else { |
| 1634 | ret = NC_PSPOLL_TIMEOUT; |
| 1635 | } |
| 1636 | break; |
| 1637 | case NC_TI_NONE: |
| 1638 | sprintf(msg, "internal error (%s:%d)", __FILE__, __LINE__); |
| 1639 | ret = NC_PSPOLL_ERROR; |
| 1640 | break; |
| 1641 | } |
| 1642 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1643 | nc_session_io_unlock(session, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1644 | return ret; |
| 1645 | } |
| 1646 | |
| 1647 | API int |
| 1648 | nc_ps_poll(struct nc_pollsession *ps, int timeout, struct nc_session **session) |
| 1649 | { |
| 1650 | int ret, r; |
| 1651 | uint8_t q_id; |
| 1652 | uint16_t i, j; |
| 1653 | char msg[256]; |
| 1654 | struct timespec ts_timeout, ts_cur; |
| 1655 | struct nc_session *cur_session; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1656 | struct nc_ps_session *cur_ps_session; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1657 | struct nc_server_rpc *rpc = NULL; |
| 1658 | |
Michal Vasko | 30a5d6b | 2017-02-15 14:29:39 +0100 | [diff] [blame] | 1659 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1660 | ERRARG("ps"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1661 | return NC_PSPOLL_ERROR; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1664 | /* PS LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1665 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1666 | return NC_PSPOLL_ERROR; |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1667 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1668 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1669 | if (!ps->session_count) { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1670 | nc_ps_unlock(ps, q_id, __func__); |
| 1671 | return NC_PSPOLL_NOSESSIONS; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1672 | } |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1673 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1674 | /* fill timespecs */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1675 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1676 | if (timeout > -1) { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1677 | nc_gettimespec_mono(&ts_timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 1678 | nc_addtimespec(&ts_timeout, timeout); |
| 1679 | } |
| 1680 | |
| 1681 | /* poll all the sessions one-by-one */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1682 | do { |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1683 | /* loop from i to j once (all sessions) */ |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1684 | if (ps->last_event_session == ps->session_count - 1) { |
| 1685 | i = j = 0; |
| 1686 | } else { |
| 1687 | i = j = ps->last_event_session + 1; |
Michal Vasko | bd8ef26 | 2016-01-20 11:09:27 +0100 | [diff] [blame] | 1688 | } |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1689 | do { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1690 | cur_ps_session = ps->sessions[i]; |
| 1691 | cur_session = cur_ps_session->session; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1692 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1693 | /* SESSION RPC LOCK */ |
| 1694 | r = nc_session_rpc_lock(cur_session, 0, __func__); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1695 | if (r == -1) { |
| 1696 | ret = NC_PSPOLL_ERROR; |
| 1697 | } else if (r == 1) { |
| 1698 | /* 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] | 1699 | switch (cur_ps_session->state) { |
| 1700 | case NC_PS_STATE_NONE: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1701 | if (cur_session->status == NC_STATUS_RUNNING) { |
| 1702 | /* session is fine, work with it */ |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1703 | cur_ps_session->state = NC_PS_STATE_BUSY; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1704 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1705 | 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] | 1706 | switch (ret) { |
| 1707 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_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_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1710 | break; |
| 1711 | case NC_PSPOLL_ERROR: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1712 | ERR(cur_session, "%s.", msg); |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1713 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1714 | break; |
| 1715 | case NC_PSPOLL_TIMEOUT: |
| 1716 | #ifdef NC_ENABLED_SSH |
| 1717 | case NC_PSPOLL_SSH_CHANNEL: |
| 1718 | case NC_PSPOLL_SSH_MSG: |
| 1719 | #endif |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1720 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1721 | break; |
| 1722 | case NC_PSPOLL_RPC: |
| 1723 | /* let's keep the state busy, we are not done with this session */ |
| 1724 | break; |
| 1725 | } |
| 1726 | } else { |
| 1727 | /* session is not fine, let the caller know */ |
| 1728 | ret = NC_PSPOLL_SESSION_TERM; |
| 1729 | if (cur_session->term_reason != NC_SESSION_TERM_CLOSED) { |
| 1730 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1731 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1732 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1733 | } |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1734 | break; |
| 1735 | case NC_PS_STATE_BUSY: |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1736 | /* it definitely should not be busy because we have the lock */ |
| 1737 | ERRINT; |
Michal Vasko | 4992d80 | 2017-08-09 12:20:01 +0200 | [diff] [blame] | 1738 | ret = NC_PSPOLL_ERROR; |
Michal Vasko | c97cb16 | 2017-10-16 12:10:23 +0200 | [diff] [blame] | 1739 | break; |
| 1740 | case NC_PS_STATE_INVALID: |
| 1741 | /* we got it locked, but it will be freed, let it be */ |
| 1742 | ret = NC_PSPOLL_TIMEOUT; |
| 1743 | break; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1744 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1745 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1746 | /* keep RPC lock in this one case */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1747 | if (ret != NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1748 | /* SESSION RPC UNLOCK */ |
| 1749 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1750 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1751 | } else { |
| 1752 | /* timeout */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1753 | ret = NC_PSPOLL_TIMEOUT; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1754 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1755 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1756 | /* something happened */ |
| 1757 | if (ret != NC_PSPOLL_TIMEOUT) { |
| 1758 | break; |
| 1759 | } |
| 1760 | |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1761 | if (i == ps->session_count - 1) { |
| 1762 | i = 0; |
| 1763 | } else { |
| 1764 | ++i; |
| 1765 | } |
| 1766 | } while (i != j); |
| 1767 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1768 | /* no event, no session remains locked */ |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1769 | if (ret == NC_PSPOLL_TIMEOUT) { |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1770 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1771 | /* update current time */ |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 1772 | nc_gettimespec_mono(&ts_cur); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1773 | |
| 1774 | if ((timeout > -1) && (nc_difftimespec(&ts_cur, &ts_timeout) < 1)) { |
| 1775 | /* final timeout */ |
| 1776 | break; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1777 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1778 | } |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1779 | } while (ret == NC_PSPOLL_TIMEOUT); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1780 | |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1781 | /* do we want to return the session? */ |
| 1782 | switch (ret) { |
| 1783 | case NC_PSPOLL_RPC: |
| 1784 | case NC_PSPOLL_SESSION_TERM: |
| 1785 | case NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR: |
| 1786 | #ifdef NC_ENABLED_SSH |
| 1787 | case NC_PSPOLL_SSH_CHANNEL: |
| 1788 | case NC_PSPOLL_SSH_MSG: |
| 1789 | #endif |
| 1790 | if (session) { |
| 1791 | *session = cur_session; |
| 1792 | } |
| 1793 | ps->last_event_session = i; |
| 1794 | break; |
| 1795 | default: |
| 1796 | break; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 1797 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1798 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1799 | /* PS UNLOCK */ |
| 1800 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1801 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1802 | /* 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] | 1803 | if (ret == NC_PSPOLL_RPC) { |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1804 | ret = nc_server_recv_rpc_io(cur_session, timeout, &rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1805 | if (ret & (NC_PSPOLL_ERROR | NC_PSPOLL_BAD_RPC)) { |
| 1806 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1807 | ret |= NC_PSPOLL_SESSION_TERM | NC_PSPOLL_SESSION_ERROR; |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1808 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1809 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1810 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1811 | } |
| 1812 | } else { |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 1813 | cur_session->opts.server.last_rpc = ts_cur.tv_sec; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1814 | |
Michal Vasko | 7f1ee93 | 2018-10-11 09:41:42 +0200 | [diff] [blame] | 1815 | /* process RPC */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1816 | ret |= nc_server_send_reply_io(cur_session, timeout, rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1817 | if (cur_session->status != NC_STATUS_RUNNING) { |
| 1818 | ret |= NC_PSPOLL_SESSION_TERM; |
| 1819 | if (!(cur_session->term_reason & (NC_SESSION_TERM_CLOSED | NC_SESSION_TERM_KILLED))) { |
| 1820 | ret |= NC_PSPOLL_SESSION_ERROR; |
| 1821 | } |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1822 | cur_ps_session->state = NC_PS_STATE_INVALID; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1823 | } else { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1824 | cur_ps_session->state = NC_PS_STATE_NONE; |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1825 | } |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1826 | } |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 1827 | nc_server_rpc_free(rpc); |
Michal Vasko | efbc5e3 | 2017-05-26 14:02:16 +0200 | [diff] [blame] | 1828 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 1829 | /* SESSION RPC UNLOCK */ |
| 1830 | nc_session_rpc_unlock(cur_session, NC_SESSION_LOCK_TIMEOUT, __func__); |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1831 | } |
| 1832 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1833 | return ret; |
Michal Vasko | 428087d | 2016-01-14 16:04:28 +0100 | [diff] [blame] | 1834 | } |
| 1835 | |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1836 | API void |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1837 | 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] | 1838 | { |
Michal Vasko | b30b99c | 2016-07-26 11:35:43 +0200 | [diff] [blame] | 1839 | uint8_t q_id; |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1840 | uint16_t i; |
| 1841 | struct nc_session *session; |
| 1842 | |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1843 | if (!ps) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1844 | ERRARG("ps"); |
Michal Vasko | 9a25e93 | 2016-02-01 10:36:42 +0100 | [diff] [blame] | 1845 | return; |
| 1846 | } |
| 1847 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1848 | /* LOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1849 | if (nc_ps_lock(ps, &q_id, __func__)) { |
Michal Vasko | be86fe3 | 2016-04-07 10:43:03 +0200 | [diff] [blame] | 1850 | return; |
| 1851 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1852 | |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1853 | if (all) { |
Radek Krejci | 4f8042c | 2016-03-03 13:11:26 +0100 | [diff] [blame] | 1854 | for (i = 0; i < ps->session_count; i++) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1855 | nc_session_free(ps->sessions[i]->session, data_free); |
| 1856 | free(ps->sessions[i]); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1857 | } |
| 1858 | free(ps->sessions); |
| 1859 | ps->sessions = NULL; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1860 | ps->session_count = 0; |
Michal Vasko | 9a32736 | 2017-01-11 11:31:46 +0100 | [diff] [blame] | 1861 | ps->last_event_session = 0; |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1862 | } else { |
| 1863 | for (i = 0; i < ps->session_count; ) { |
fanchanghu | 3d4e721 | 2017-08-09 09:42:30 +0800 | [diff] [blame] | 1864 | if (ps->sessions[i]->session->status != NC_STATUS_RUNNING) { |
| 1865 | session = ps->sessions[i]->session; |
Radek Krejci | d5f978f | 2016-03-03 13:14:45 +0100 | [diff] [blame] | 1866 | _nc_ps_del_session(ps, NULL, i); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 1867 | nc_session_free(session, data_free); |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1868 | continue; |
| 1869 | } |
| 1870 | |
| 1871 | ++i; |
| 1872 | } |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1873 | } |
Michal Vasko | 48a63ed | 2016-03-01 09:48:21 +0100 | [diff] [blame] | 1874 | |
| 1875 | /* UNLOCK */ |
Michal Vasko | 2604317 | 2016-07-26 14:08:59 +0200 | [diff] [blame] | 1876 | nc_ps_unlock(ps, q_id, __func__); |
Michal Vasko | d09eae6 | 2016-02-01 10:32:52 +0100 | [diff] [blame] | 1877 | } |
| 1878 | |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1879 | static int |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1880 | nc_get_uid(int sock, uid_t *uid) |
| 1881 | { |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1882 | int ret; |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1883 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1884 | #ifdef SO_PEERCRED |
| 1885 | struct ucred ucred; |
| 1886 | socklen_t len; |
| 1887 | len = sizeof(ucred); |
| 1888 | ret = getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len); |
| 1889 | if (!ret) { |
| 1890 | *uid = ucred.uid; |
| 1891 | } |
| 1892 | #else |
| 1893 | ret = getpeereid(sock, uid, NULL); |
| 1894 | #endif |
| 1895 | |
| 1896 | if (ret < 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1897 | ERR(NULL, "Failed to get credentials from unix socket (%s).", strerror(errno)); |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1898 | return -1; |
| 1899 | } |
apropp-molex | 4e903c3 | 2020-04-20 03:06:58 -0400 | [diff] [blame] | 1900 | return 0; |
| 1901 | } |
| 1902 | |
| 1903 | static int |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1904 | nc_accept_unix(struct nc_session *session, int sock) |
| 1905 | { |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 1906 | #if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID) |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1907 | struct passwd *pw, pw_buf; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1908 | char *username; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1909 | session->ti_type = NC_TI_UNIX; |
Michal Vasko | 143aa14 | 2021-10-01 15:31:48 +0200 | [diff] [blame] | 1910 | uid_t uid = 0; |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1911 | char *buf = NULL; |
| 1912 | size_t buf_len = 0; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1913 | |
Michal Vasko | d391091 | 2020-04-20 09:12:49 +0200 | [diff] [blame] | 1914 | if (nc_get_uid(sock, &uid)) { |
| 1915 | close(sock); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1916 | return -1; |
| 1917 | } |
| 1918 | |
Jan Kundrát | 6aa0eeb | 2021-10-08 21:10:05 +0200 | [diff] [blame] | 1919 | pw = nc_getpwuid(uid, &pw_buf, &buf, &buf_len); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1920 | if (pw == NULL) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1921 | 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] | 1922 | close(sock); |
| 1923 | return -1; |
| 1924 | } |
| 1925 | |
| 1926 | username = strdup(pw->pw_name); |
Michal Vasko | ccd2dd0 | 2021-10-11 09:13:01 +0200 | [diff] [blame] | 1927 | free(buf); |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1928 | if (username == NULL) { |
| 1929 | ERRMEM; |
| 1930 | close(sock); |
| 1931 | return -1; |
| 1932 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1933 | session->username = username; |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1934 | |
| 1935 | session->ti.unixsock.sock = sock; |
| 1936 | |
| 1937 | return 1; |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 1938 | #else |
| 1939 | return -1; |
| 1940 | #endif |
Michal Vasko | 5f352c5 | 2019-07-10 16:12:06 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1943 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1944 | nc_server_add_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1945 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1946 | uint16_t i; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1947 | int ret = 0; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1948 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 1949 | if (!name) { |
| 1950 | ERRARG("name"); |
| 1951 | return -1; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1952 | } |
| 1953 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1954 | /* BIND LOCK */ |
| 1955 | pthread_mutex_lock(&server_opts.bind_lock); |
| 1956 | |
| 1957 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1958 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1959 | |
| 1960 | /* check name uniqueness */ |
| 1961 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1962 | if (!strcmp(server_opts.endpts[i].name, name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 1963 | ERR(NULL, "Endpoint \"%s\" already exists.", name); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1964 | ret = -1; |
| 1965 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1969 | 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] | 1970 | if (!server_opts.endpts) { |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1971 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1972 | ret = -1; |
| 1973 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1974 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 1975 | memset(&server_opts.endpts[server_opts.endpt_count], 0, sizeof *server_opts.endpts); |
| 1976 | ++server_opts.endpt_count; |
| 1977 | |
| 1978 | server_opts.endpts[server_opts.endpt_count - 1].name = strdup(name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1979 | server_opts.endpts[server_opts.endpt_count - 1].ti = ti; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1980 | server_opts.endpts[server_opts.endpt_count - 1].ka.idle_time = 1; |
| 1981 | server_opts.endpts[server_opts.endpt_count - 1].ka.max_probes = 10; |
| 1982 | server_opts.endpts[server_opts.endpt_count - 1].ka.probe_interval = 5; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1983 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 1984 | 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] | 1985 | if (!server_opts.binds) { |
| 1986 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 1987 | ret = -1; |
| 1988 | goto cleanup; |
Michal Vasko | 4eb3c31 | 2016-03-01 14:09:37 +0100 | [diff] [blame] | 1989 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 1990 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 1991 | 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] | 1992 | server_opts.binds[server_opts.endpt_count - 1].sock = -1; |
| 1993 | |
| 1994 | switch (ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 1995 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 1996 | case NC_TI_LIBSSH: |
| 1997 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 1998 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.ssh) { |
| 1999 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2000 | ret = -1; |
| 2001 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2002 | } |
| 2003 | server_opts.endpts[server_opts.endpt_count - 1].opts.ssh->auth_methods = |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 2004 | NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2005 | 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] | 2006 | 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] | 2007 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2008 | #endif |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2009 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2010 | case NC_TI_OPENSSL: |
| 2011 | server_opts.endpts[server_opts.endpt_count - 1].opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2012 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.tls) { |
| 2013 | ERRMEM; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2014 | ret = -1; |
| 2015 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2016 | } |
| 2017 | break; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2018 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2019 | case NC_TI_UNIX: |
| 2020 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock = calloc(1, sizeof(struct nc_server_unix_opts)); |
| 2021 | if (!server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock) { |
| 2022 | ERRMEM; |
| 2023 | ret = -1; |
| 2024 | goto cleanup; |
| 2025 | } |
| 2026 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->mode = (mode_t)-1; |
| 2027 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->uid = (uid_t)-1; |
| 2028 | server_opts.endpts[server_opts.endpt_count - 1].opts.unixsock->gid = (gid_t)-1; |
| 2029 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2030 | default: |
| 2031 | ERRINT; |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2032 | ret = -1; |
| 2033 | goto cleanup; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2034 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2035 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2036 | cleanup: |
| 2037 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2038 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2039 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2040 | /* BIND UNLOCK */ |
| 2041 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2042 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2043 | return ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2046 | API int |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2047 | nc_server_del_endpt(const char *name, NC_TRANSPORT_IMPL ti) |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2048 | { |
| 2049 | uint32_t i; |
| 2050 | int ret = -1; |
| 2051 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2052 | /* BIND LOCK */ |
| 2053 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2054 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2055 | /* ENDPT WRITE LOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2056 | pthread_rwlock_wrlock(&server_opts.endpt_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2057 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2058 | if (!name && !ti) { |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2059 | /* remove all endpoints */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2060 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2061 | free(server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2062 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2063 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2064 | case NC_TI_LIBSSH: |
| 2065 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2066 | free(server_opts.endpts[i].opts.ssh); |
| 2067 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2068 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2069 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2070 | case NC_TI_OPENSSL: |
| 2071 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2072 | free(server_opts.endpts[i].opts.tls); |
| 2073 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2074 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2075 | case NC_TI_UNIX: |
| 2076 | free(server_opts.endpts[i].opts.unixsock); |
| 2077 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2078 | default: |
| 2079 | ERRINT; |
| 2080 | /* won't get here ...*/ |
| 2081 | break; |
| 2082 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2083 | ret = 0; |
| 2084 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2085 | free(server_opts.endpts); |
| 2086 | server_opts.endpts = NULL; |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2087 | |
| 2088 | /* remove all binds */ |
| 2089 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2090 | free(server_opts.binds[i].address); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2091 | if (server_opts.binds[i].sock > -1) { |
| 2092 | close(server_opts.binds[i].sock); |
| 2093 | } |
| 2094 | } |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2095 | free(server_opts.binds); |
| 2096 | server_opts.binds = NULL; |
| 2097 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2098 | server_opts.endpt_count = 0; |
| 2099 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2100 | } else { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2101 | /* 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] | 2102 | for (i = 0; i < server_opts.endpt_count; ++i) { |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2103 | 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] | 2104 | /* remove endpt */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2105 | free(server_opts.endpts[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2106 | switch (server_opts.endpts[i].ti) { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2107 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2108 | case NC_TI_LIBSSH: |
| 2109 | nc_server_ssh_clear_opts(server_opts.endpts[i].opts.ssh); |
| 2110 | free(server_opts.endpts[i].opts.ssh); |
| 2111 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2112 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2113 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2114 | case NC_TI_OPENSSL: |
| 2115 | nc_server_tls_clear_opts(server_opts.endpts[i].opts.tls); |
| 2116 | free(server_opts.endpts[i].opts.tls); |
| 2117 | break; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2118 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2119 | case NC_TI_UNIX: |
| 2120 | free(server_opts.endpts[i].opts.unixsock); |
| 2121 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2122 | default: |
| 2123 | ERRINT; |
| 2124 | break; |
| 2125 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2126 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2127 | /* remove bind(s) */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2128 | free(server_opts.binds[i].address); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2129 | if (server_opts.binds[i].sock > -1) { |
| 2130 | close(server_opts.binds[i].sock); |
| 2131 | } |
| 2132 | |
| 2133 | /* move last endpt and bind(s) to the empty space */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2134 | --server_opts.endpt_count; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2135 | if (!server_opts.endpt_count) { |
Michal Vasko | c025649 | 2016-02-02 12:19:06 +0100 | [diff] [blame] | 2136 | free(server_opts.binds); |
| 2137 | server_opts.binds = NULL; |
| 2138 | free(server_opts.endpts); |
| 2139 | server_opts.endpts = NULL; |
Michal Vasko | 9ed67a7 | 2016-10-13 15:00:51 +0200 | [diff] [blame] | 2140 | } else if (i < server_opts.endpt_count) { |
| 2141 | memcpy(&server_opts.binds[i], &server_opts.binds[server_opts.endpt_count], sizeof *server_opts.binds); |
| 2142 | 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] | 2143 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2144 | |
| 2145 | ret = 0; |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 2146 | if (name) { |
| 2147 | break; |
| 2148 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2149 | } |
| 2150 | } |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2151 | } |
| 2152 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2153 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2154 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2155 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2156 | /* BIND UNLOCK */ |
| 2157 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2158 | |
| 2159 | return ret; |
| 2160 | } |
| 2161 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2162 | API int |
| 2163 | nc_server_endpt_count(void) |
| 2164 | { |
| 2165 | return server_opts.endpt_count; |
| 2166 | } |
| 2167 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2168 | API int |
| 2169 | nc_server_is_endpt(const char *name) |
| 2170 | { |
| 2171 | uint16_t i; |
| 2172 | int found = 0; |
| 2173 | |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2174 | if (!name) { |
| 2175 | return found; |
| 2176 | } |
| 2177 | |
Michal Vasko | 1b5973e | 2020-01-30 16:05:46 +0100 | [diff] [blame] | 2178 | /* ENDPT READ LOCK */ |
| 2179 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2180 | |
| 2181 | /* check name uniqueness */ |
| 2182 | for (i = 0; i < server_opts.endpt_count; ++i) { |
| 2183 | if (!strcmp(server_opts.endpts[i].name, name)) { |
| 2184 | found = 1; |
| 2185 | break; |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | /* ENDPT UNLOCK */ |
| 2190 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2191 | |
| 2192 | return found; |
| 2193 | } |
| 2194 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2195 | int |
| 2196 | nc_server_endpt_set_address_port(const char *endpt_name, const char *address, uint16_t port) |
| 2197 | { |
| 2198 | struct nc_endpt *endpt; |
| 2199 | struct nc_bind *bind = NULL; |
| 2200 | uint16_t i; |
| 2201 | int sock = -1, set_addr, ret = 0; |
| 2202 | |
| 2203 | if (!endpt_name) { |
| 2204 | ERRARG("endpt_name"); |
| 2205 | return -1; |
| 2206 | } else if ((!address && !port) || (address && port)) { |
| 2207 | ERRARG("address and port"); |
| 2208 | return -1; |
| 2209 | } |
| 2210 | |
| 2211 | if (address) { |
| 2212 | set_addr = 1; |
| 2213 | } else { |
| 2214 | set_addr = 0; |
| 2215 | } |
| 2216 | |
| 2217 | /* BIND LOCK */ |
| 2218 | pthread_mutex_lock(&server_opts.bind_lock); |
| 2219 | |
| 2220 | /* ENDPT LOCK */ |
| 2221 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
| 2222 | if (!endpt) { |
| 2223 | /* BIND UNLOCK */ |
| 2224 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2225 | return -1; |
| 2226 | } |
| 2227 | |
| 2228 | bind = &server_opts.binds[i]; |
| 2229 | |
| 2230 | if (set_addr) { |
| 2231 | port = bind->port; |
| 2232 | } else { |
| 2233 | address = bind->address; |
| 2234 | } |
| 2235 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2236 | if (!set_addr && (endpt->ti == NC_TI_UNIX)) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2237 | ret = -1; |
| 2238 | goto cleanup; |
| 2239 | } |
| 2240 | |
| 2241 | /* we have all the information we need to create a listening socket */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2242 | if (address && (port || (endpt->ti == NC_TI_UNIX))) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2243 | /* create new socket, close the old one */ |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2244 | if (endpt->ti == NC_TI_UNIX) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2245 | sock = nc_sock_listen_unix(address, endpt->opts.unixsock); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2246 | } else { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2247 | sock = nc_sock_listen_inet(address, port, &endpt->ka); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2248 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2249 | if (sock == -1) { |
| 2250 | ret = -1; |
| 2251 | goto cleanup; |
| 2252 | } |
| 2253 | |
| 2254 | if (bind->sock > -1) { |
| 2255 | close(bind->sock); |
| 2256 | } |
| 2257 | bind->sock = sock; |
| 2258 | } /* else we are just setting address or port */ |
| 2259 | |
| 2260 | if (set_addr) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2261 | free(bind->address); |
| 2262 | bind->address = strdup(address); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2263 | } else { |
| 2264 | bind->port = port; |
| 2265 | } |
| 2266 | |
| 2267 | if (sock > -1) { |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2268 | switch (endpt->ti) { |
| 2269 | case NC_TI_UNIX: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2270 | VRB(NULL, "Listening on %s for UNIX connections.", address); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2271 | break; |
| 2272 | #ifdef NC_ENABLED_SSH |
| 2273 | case NC_TI_LIBSSH: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2274 | VRB(NULL, "Listening on %s:%u for SSH connections.", address, port); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2275 | break; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2276 | #endif |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2277 | #ifdef NC_ENABLED_TLS |
| 2278 | case NC_TI_OPENSSL: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2279 | VRB(NULL, "Listening on %s:%u for TLS connections.", address, port); |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2280 | break; |
| 2281 | #endif |
| 2282 | default: |
| 2283 | ERRINT; |
| 2284 | break; |
| 2285 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | cleanup: |
| 2289 | /* ENDPT UNLOCK */ |
| 2290 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2291 | |
| 2292 | /* BIND UNLOCK */ |
| 2293 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2294 | |
| 2295 | return ret; |
| 2296 | } |
| 2297 | |
| 2298 | API int |
| 2299 | nc_server_endpt_set_address(const char *endpt_name, const char *address) |
| 2300 | { |
| 2301 | return nc_server_endpt_set_address_port(endpt_name, address, 0); |
| 2302 | } |
| 2303 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2304 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2305 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2306 | API int |
| 2307 | nc_server_endpt_set_port(const char *endpt_name, uint16_t port) |
| 2308 | { |
| 2309 | return nc_server_endpt_set_address_port(endpt_name, NULL, port); |
| 2310 | } |
| 2311 | |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2312 | #endif |
| 2313 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2314 | API int |
| 2315 | nc_server_endpt_set_perms(const char *endpt_name, mode_t mode, uid_t uid, gid_t gid) |
| 2316 | { |
| 2317 | struct nc_endpt *endpt; |
| 2318 | uint16_t i; |
| 2319 | int ret = 0; |
| 2320 | |
| 2321 | if (!endpt_name) { |
| 2322 | ERRARG("endpt_name"); |
| 2323 | return -1; |
| 2324 | } else if (mode == 0) { |
| 2325 | ERRARG("mode"); |
| 2326 | return -1; |
| 2327 | } |
| 2328 | |
| 2329 | /* ENDPT LOCK */ |
| 2330 | endpt = nc_server_endpt_lock_get(endpt_name, 0, &i); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2331 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2332 | return -1; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2333 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2334 | |
| 2335 | if (endpt->ti != NC_TI_UNIX) { |
| 2336 | ret = -1; |
| 2337 | goto cleanup; |
| 2338 | } |
| 2339 | |
| 2340 | endpt->opts.unixsock->mode = mode; |
| 2341 | endpt->opts.unixsock->uid = uid; |
| 2342 | endpt->opts.unixsock->gid = gid; |
| 2343 | |
| 2344 | cleanup: |
| 2345 | /* ENDPT UNLOCK */ |
| 2346 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2347 | |
| 2348 | return ret; |
| 2349 | } |
| 2350 | |
| 2351 | API int |
| 2352 | nc_server_endpt_enable_keepalives(const char *endpt_name, int enable) |
| 2353 | { |
| 2354 | struct nc_endpt *endpt; |
| 2355 | int ret = 0; |
| 2356 | |
| 2357 | if (!endpt_name) { |
| 2358 | ERRARG("endpt_name"); |
| 2359 | return -1; |
| 2360 | } |
| 2361 | |
| 2362 | /* ENDPT LOCK */ |
| 2363 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2364 | if (!endpt) { |
| 2365 | return -1; |
| 2366 | } |
| 2367 | |
| 2368 | endpt->ka.enabled = (enable ? 1 : 0); |
| 2369 | |
| 2370 | /* ENDPT UNLOCK */ |
| 2371 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2372 | |
| 2373 | return ret; |
| 2374 | } |
| 2375 | |
| 2376 | API int |
| 2377 | nc_server_endpt_set_keepalives(const char *endpt_name, int idle_time, int max_probes, int probe_interval) |
| 2378 | { |
| 2379 | struct nc_endpt *endpt; |
| 2380 | int ret = 0; |
| 2381 | |
| 2382 | if (!endpt_name) { |
| 2383 | ERRARG("endpt_name"); |
| 2384 | return -1; |
| 2385 | } |
| 2386 | |
| 2387 | /* ENDPT LOCK */ |
| 2388 | endpt = nc_server_endpt_lock_get(endpt_name, 0, NULL); |
| 2389 | if (!endpt) { |
| 2390 | return -1; |
| 2391 | } |
| 2392 | |
| 2393 | if (idle_time > -1) { |
| 2394 | endpt->ka.idle_time = idle_time; |
| 2395 | } |
| 2396 | if (max_probes > -1) { |
| 2397 | endpt->ka.max_probes = max_probes; |
| 2398 | } |
| 2399 | if (probe_interval > -1) { |
| 2400 | endpt->ka.probe_interval = probe_interval; |
| 2401 | } |
| 2402 | |
| 2403 | /* ENDPT UNLOCK */ |
| 2404 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
| 2405 | |
| 2406 | return ret; |
| 2407 | } |
| 2408 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2409 | API NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2410 | 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] | 2411 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2412 | NC_MSG_TYPE msgtype; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2413 | int sock, ret; |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2414 | char *host = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2415 | uint16_t port, bind_idx; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2416 | struct timespec ts_cur; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2417 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2418 | if (!ctx) { |
| 2419 | ERRARG("ctx"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2420 | return NC_MSG_ERROR; |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 2421 | } else if (!session) { |
| 2422 | ERRARG("session"); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2423 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2424 | } |
| 2425 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2426 | /* init ctx as needed */ |
| 2427 | nc_server_init_ctx(ctx); |
| 2428 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2429 | /* BIND LOCK */ |
| 2430 | pthread_mutex_lock(&server_opts.bind_lock); |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2431 | |
| 2432 | if (!server_opts.endpt_count) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2433 | ERR(NULL, "No endpoints to accept sessions on."); |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2434 | /* BIND UNLOCK */ |
| 2435 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2436 | return NC_MSG_ERROR; |
Michal Vasko | 51e514d | 2016-02-02 15:51:52 +0100 | [diff] [blame] | 2437 | } |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2438 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 2439 | 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] | 2440 | if (ret < 1) { |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2441 | /* BIND UNLOCK */ |
| 2442 | pthread_mutex_unlock(&server_opts.bind_lock); |
Michal Vasko | b737d75 | 2016-02-09 09:01:27 +0100 | [diff] [blame] | 2443 | free(host); |
Michal Vasko | 5e20347 | 2016-05-30 15:27:58 +0200 | [diff] [blame] | 2444 | if (!ret) { |
| 2445 | return NC_MSG_WOULDBLOCK; |
| 2446 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2447 | return NC_MSG_ERROR; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2448 | } |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2449 | |
| 2450 | /* switch bind_lock for endpt_lock, so that another thread can accept another session */ |
| 2451 | /* ENDPT READ LOCK */ |
| 2452 | pthread_rwlock_rdlock(&server_opts.endpt_lock); |
| 2453 | |
| 2454 | /* BIND UNLOCK */ |
| 2455 | pthread_mutex_unlock(&server_opts.bind_lock); |
| 2456 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2457 | sock = ret; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2458 | |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2459 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | 686aa31 | 2016-01-21 15:58:18 +0100 | [diff] [blame] | 2460 | if (!(*session)) { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2461 | ERRMEM; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2462 | close(sock); |
Michal Vasko | 5c2f795 | 2016-01-22 13:16:31 +0100 | [diff] [blame] | 2463 | free(host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2464 | msgtype = NC_MSG_ERROR; |
| 2465 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2466 | } |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2467 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2468 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2469 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2470 | (*session)->host = host; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2471 | (*session)->port = port; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2472 | |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2473 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2474 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2475 | if (server_opts.endpts[bind_idx].ti == NC_TI_LIBSSH) { |
| 2476 | (*session)->data = server_opts.endpts[bind_idx].opts.ssh; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2477 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2478 | if (ret < 0) { |
| 2479 | msgtype = NC_MSG_ERROR; |
| 2480 | goto cleanup; |
| 2481 | } else if (!ret) { |
| 2482 | msgtype = NC_MSG_WOULDBLOCK; |
| 2483 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2484 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2485 | } else |
| 2486 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 2487 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2488 | if (server_opts.endpts[bind_idx].ti == NC_TI_OPENSSL) { |
| 2489 | (*session)->data = server_opts.endpts[bind_idx].opts.tls; |
Michal Vasko | b70c8b8 | 2017-03-17 09:09:29 +0100 | [diff] [blame] | 2490 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2491 | if (ret < 0) { |
| 2492 | msgtype = NC_MSG_ERROR; |
| 2493 | goto cleanup; |
| 2494 | } else if (!ret) { |
| 2495 | msgtype = NC_MSG_WOULDBLOCK; |
| 2496 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2497 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 2498 | } else |
| 2499 | #endif |
Olivier Matz | ac7fa2f | 2018-10-11 10:02:04 +0200 | [diff] [blame] | 2500 | if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) { |
| 2501 | (*session)->data = server_opts.endpts[bind_idx].opts.unixsock; |
| 2502 | ret = nc_accept_unix(*session, sock); |
| 2503 | if (ret < 0) { |
| 2504 | msgtype = NC_MSG_ERROR; |
| 2505 | goto cleanup; |
| 2506 | } |
| 2507 | } else { |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2508 | ERRINT; |
Michal Vasko | c14e3c8 | 2016-01-11 16:14:30 +0100 | [diff] [blame] | 2509 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2510 | msgtype = NC_MSG_ERROR; |
| 2511 | goto cleanup; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2512 | } |
| 2513 | |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 2514 | (*session)->data = NULL; |
| 2515 | |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2516 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2517 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2518 | |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2519 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2520 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 2521 | |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2522 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 2523 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2524 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2525 | nc_session_free(*session, NULL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2526 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2527 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2528 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 2529 | |
| 2530 | nc_gettimespec_mono(&ts_cur); |
| 2531 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 2532 | nc_gettimespec_real(&ts_cur); |
| 2533 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2534 | (*session)->status = NC_STATUS_RUNNING; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2535 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2536 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2537 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2538 | cleanup: |
Michal Vasko | ade892d | 2017-02-22 13:40:35 +0100 | [diff] [blame] | 2539 | /* ENDPT UNLOCK */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2540 | pthread_rwlock_unlock(&server_opts.endpt_lock); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 2541 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 2542 | nc_session_free(*session, NULL); |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 2543 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 2544 | return msgtype; |
Michal Vasko | 9e036d5 | 2016-01-08 10:49:26 +0100 | [diff] [blame] | 2545 | } |
| 2546 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 2547 | #if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS) |
Michal Vasko | 946cacb | 2020-08-12 11:18:08 +0200 | [diff] [blame] | 2548 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2549 | /* client is expected to be locked */ |
| 2550 | static int |
| 2551 | _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] | 2552 | { |
| 2553 | uint16_t i; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2554 | int ret = -1; |
| 2555 | |
| 2556 | if (!endpt_name) { |
| 2557 | /* remove all endpoints */ |
| 2558 | for (i = 0; i < client->ch_endpt_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2559 | free(client->ch_endpts[i].name); |
| 2560 | free(client->ch_endpts[i].address); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2561 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2562 | close(client->ch_endpts[i].sock_pending); |
| 2563 | } |
| 2564 | switch (client->ch_endpts[i].ti) { |
| 2565 | #ifdef NC_ENABLED_SSH |
| 2566 | case NC_TI_LIBSSH: |
| 2567 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2568 | free(client->ch_endpts[i].opts.ssh); |
| 2569 | break; |
| 2570 | #endif |
| 2571 | #ifdef NC_ENABLED_TLS |
| 2572 | case NC_TI_OPENSSL: |
| 2573 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2574 | free(client->ch_endpts[i].opts.tls); |
| 2575 | break; |
| 2576 | #endif |
| 2577 | default: |
| 2578 | ERRINT; |
| 2579 | /* won't get here ...*/ |
| 2580 | break; |
| 2581 | } |
| 2582 | } |
| 2583 | free(client->ch_endpts); |
| 2584 | client->ch_endpts = NULL; |
| 2585 | client->ch_endpt_count = 0; |
| 2586 | |
| 2587 | ret = 0; |
| 2588 | } else { |
| 2589 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2590 | 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] | 2591 | free(client->ch_endpts[i].name); |
| 2592 | free(client->ch_endpts[i].address); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2593 | if (client->ch_endpts[i].sock_pending != -1) { |
| 2594 | close(client->ch_endpts[i].sock_pending); |
| 2595 | } |
| 2596 | switch (client->ch_endpts[i].ti) { |
| 2597 | #ifdef NC_ENABLED_SSH |
| 2598 | case NC_TI_LIBSSH: |
| 2599 | nc_server_ssh_clear_opts(client->ch_endpts[i].opts.ssh); |
| 2600 | free(client->ch_endpts[i].opts.ssh); |
| 2601 | break; |
| 2602 | #endif |
| 2603 | #ifdef NC_ENABLED_TLS |
| 2604 | case NC_TI_OPENSSL: |
| 2605 | nc_server_tls_clear_opts(client->ch_endpts[i].opts.tls); |
| 2606 | free(client->ch_endpts[i].opts.tls); |
| 2607 | break; |
| 2608 | #endif |
| 2609 | default: |
| 2610 | ERRINT; |
| 2611 | /* won't get here ...*/ |
| 2612 | break; |
| 2613 | } |
| 2614 | |
| 2615 | /* move last endpoint to the empty space */ |
| 2616 | --client->ch_endpt_count; |
| 2617 | if (i < client->ch_endpt_count) { |
| 2618 | memcpy(&client->ch_endpts[i], &client->ch_endpts[client->ch_endpt_count], sizeof *client->ch_endpts); |
| 2619 | } else if (!server_opts.ch_client_count) { |
| 2620 | free(server_opts.ch_clients); |
| 2621 | server_opts.ch_clients = NULL; |
| 2622 | } |
| 2623 | |
| 2624 | ret = 0; |
| 2625 | break; |
| 2626 | } |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | return ret; |
| 2631 | } |
| 2632 | |
| 2633 | API int |
| 2634 | nc_server_ch_add_client(const char *name) |
| 2635 | { |
| 2636 | uint16_t i; |
| 2637 | struct nc_ch_client *client; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2638 | |
| 2639 | if (!name) { |
| 2640 | ERRARG("name"); |
| 2641 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | /* WRITE LOCK */ |
| 2645 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2646 | |
| 2647 | /* check name uniqueness */ |
| 2648 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2649 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2650 | ERR(NULL, "Call Home client \"%s\" already exists.", name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2651 | /* WRITE UNLOCK */ |
| 2652 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2653 | return -1; |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | ++server_opts.ch_client_count; |
| 2658 | server_opts.ch_clients = nc_realloc(server_opts.ch_clients, server_opts.ch_client_count * sizeof *server_opts.ch_clients); |
| 2659 | if (!server_opts.ch_clients) { |
| 2660 | ERRMEM; |
| 2661 | /* WRITE UNLOCK */ |
| 2662 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2663 | return -1; |
| 2664 | } |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2665 | client = &server_opts.ch_clients[server_opts.ch_client_count - 1]; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2666 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2667 | client->name = strdup(name); |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 2668 | client->id = ATOMIC_INC_RELAXED(server_opts.new_client_id); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2669 | client->ch_endpts = NULL; |
| 2670 | client->ch_endpt_count = 0; |
| 2671 | client->conn_type = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 2672 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2673 | /* set CH default options */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2674 | client->start_with = NC_CH_FIRST_LISTED; |
| 2675 | client->max_attempts = 3; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2676 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2677 | pthread_mutex_init(&client->lock, NULL); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2678 | |
| 2679 | /* WRITE UNLOCK */ |
| 2680 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2681 | |
| 2682 | return 0; |
| 2683 | } |
| 2684 | |
| 2685 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2686 | nc_server_ch_del_client(const char *name) |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2687 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2688 | uint16_t i; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2689 | int ret = -1; |
| 2690 | |
| 2691 | /* WRITE LOCK */ |
| 2692 | pthread_rwlock_wrlock(&server_opts.ch_client_lock); |
| 2693 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2694 | if (!name) { |
| 2695 | /* remove all CH clients with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2696 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2697 | free(server_opts.ch_clients[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2698 | |
| 2699 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2700 | _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] | 2701 | |
| 2702 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2703 | ret = 0; |
| 2704 | } |
| 2705 | free(server_opts.ch_clients); |
| 2706 | server_opts.ch_clients = NULL; |
| 2707 | |
| 2708 | server_opts.ch_client_count = 0; |
| 2709 | |
| 2710 | } else { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2711 | /* remove one client with endpoints */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2712 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2713 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2714 | free(server_opts.ch_clients[i].name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2715 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2716 | /* remove all endpoints */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2717 | _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] | 2718 | |
| 2719 | pthread_mutex_destroy(&server_opts.ch_clients[i].lock); |
| 2720 | |
| 2721 | /* move last client and endpoint(s) to the empty space */ |
| 2722 | --server_opts.ch_client_count; |
| 2723 | if (i < server_opts.ch_client_count) { |
| 2724 | 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] | 2725 | sizeof *server_opts.ch_clients); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2726 | } else if (!server_opts.ch_client_count) { |
| 2727 | free(server_opts.ch_clients); |
| 2728 | server_opts.ch_clients = NULL; |
| 2729 | } |
| 2730 | |
| 2731 | ret = 0; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2732 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | /* WRITE UNLOCK */ |
| 2738 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2739 | |
| 2740 | return ret; |
| 2741 | } |
| 2742 | |
| 2743 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2744 | nc_server_ch_is_client(const char *name) |
| 2745 | { |
| 2746 | uint16_t i; |
| 2747 | int found = 0; |
| 2748 | |
| 2749 | if (!name) { |
| 2750 | return found; |
| 2751 | } |
| 2752 | |
| 2753 | /* READ LOCK */ |
| 2754 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2755 | |
| 2756 | /* check name uniqueness */ |
| 2757 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2758 | if (!strcmp(server_opts.ch_clients[i].name, name)) { |
| 2759 | found = 1; |
| 2760 | break; |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | /* UNLOCK */ |
| 2765 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2766 | |
| 2767 | return found; |
| 2768 | } |
| 2769 | |
| 2770 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2771 | 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] | 2772 | { |
| 2773 | uint16_t i; |
| 2774 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2775 | struct nc_ch_endpt *endpt; |
| 2776 | int ret = -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2777 | |
| 2778 | if (!client_name) { |
| 2779 | ERRARG("client_name"); |
| 2780 | return -1; |
| 2781 | } else if (!endpt_name) { |
| 2782 | ERRARG("endpt_name"); |
| 2783 | return -1; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2784 | } else if (!ti) { |
| 2785 | ERRARG("ti"); |
| 2786 | return -1; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2790 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2791 | if (!client) { |
| 2792 | return -1; |
| 2793 | } |
| 2794 | |
| 2795 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2796 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 2797 | 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] | 2798 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | ++client->ch_endpt_count; |
| 2803 | client->ch_endpts = realloc(client->ch_endpts, client->ch_endpt_count * sizeof *client->ch_endpts); |
| 2804 | if (!client->ch_endpts) { |
| 2805 | ERRMEM; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2806 | goto cleanup; |
| 2807 | } |
| 2808 | endpt = &client->ch_endpts[client->ch_endpt_count - 1]; |
| 2809 | |
| 2810 | memset(endpt, 0, sizeof *client->ch_endpts); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2811 | endpt->name = strdup(endpt_name); |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2812 | endpt->ti = ti; |
| 2813 | endpt->sock_pending = -1; |
| 2814 | endpt->ka.idle_time = 1; |
| 2815 | endpt->ka.max_probes = 10; |
| 2816 | endpt->ka.probe_interval = 5; |
| 2817 | |
| 2818 | switch (ti) { |
| 2819 | #ifdef NC_ENABLED_SSH |
| 2820 | case NC_TI_LIBSSH: |
| 2821 | endpt->opts.ssh = calloc(1, sizeof(struct nc_server_ssh_opts)); |
| 2822 | if (!endpt->opts.ssh) { |
| 2823 | ERRMEM; |
| 2824 | goto cleanup; |
| 2825 | } |
| 2826 | endpt->opts.ssh->auth_methods = NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE; |
| 2827 | endpt->opts.ssh->auth_attempts = 3; |
Michal Vasko | cbad4c5 | 2019-06-27 16:30:35 +0200 | [diff] [blame] | 2828 | endpt->opts.ssh->auth_timeout = 30; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2829 | break; |
| 2830 | #endif |
| 2831 | #ifdef NC_ENABLED_TLS |
| 2832 | case NC_TI_OPENSSL: |
| 2833 | endpt->opts.tls = calloc(1, sizeof(struct nc_server_tls_opts)); |
| 2834 | if (!endpt->opts.tls) { |
| 2835 | ERRMEM; |
| 2836 | goto cleanup; |
| 2837 | } |
| 2838 | break; |
| 2839 | #endif |
| 2840 | default: |
| 2841 | ERRINT; |
| 2842 | goto cleanup; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2843 | } |
| 2844 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2845 | /* success */ |
| 2846 | ret = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2847 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2848 | cleanup: |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2849 | /* UNLOCK */ |
| 2850 | nc_server_ch_client_unlock(client); |
| 2851 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2852 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | API int |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2856 | 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] | 2857 | { |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2858 | int ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2859 | struct nc_ch_client *client; |
| 2860 | |
| 2861 | if (!client_name) { |
| 2862 | ERRARG("client_name"); |
| 2863 | return -1; |
| 2864 | } |
| 2865 | |
| 2866 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2867 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2868 | if (!client) { |
| 2869 | return -1; |
| 2870 | } |
| 2871 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2872 | ret = _nc_server_ch_client_del_endpt(client, endpt_name, ti); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2873 | |
| 2874 | /* UNLOCK */ |
| 2875 | nc_server_ch_client_unlock(client); |
| 2876 | |
| 2877 | return ret; |
| 2878 | } |
| 2879 | |
| 2880 | API int |
Michal Vasko | fb1724b | 2020-01-31 11:02:00 +0100 | [diff] [blame] | 2881 | nc_server_ch_client_is_endpt(const char *client_name, const char *endpt_name) |
| 2882 | { |
| 2883 | uint16_t i; |
| 2884 | struct nc_ch_client *client = NULL; |
| 2885 | int found = 0; |
| 2886 | |
| 2887 | if (!client_name || !endpt_name) { |
| 2888 | return found; |
| 2889 | } |
| 2890 | |
| 2891 | /* READ LOCK */ |
| 2892 | pthread_rwlock_rdlock(&server_opts.ch_client_lock); |
| 2893 | |
| 2894 | for (i = 0; i < server_opts.ch_client_count; ++i) { |
| 2895 | if (!strcmp(server_opts.ch_clients[i].name, client_name)) { |
| 2896 | client = &server_opts.ch_clients[i]; |
| 2897 | break; |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | if (!client) { |
| 2902 | goto cleanup; |
| 2903 | } |
| 2904 | |
| 2905 | for (i = 0; i < client->ch_endpt_count; ++i) { |
| 2906 | if (!strcmp(client->ch_endpts[i].name, endpt_name)) { |
| 2907 | found = 1; |
| 2908 | break; |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | cleanup: |
| 2913 | /* UNLOCK */ |
| 2914 | pthread_rwlock_unlock(&server_opts.ch_client_lock); |
| 2915 | return found; |
| 2916 | } |
| 2917 | |
| 2918 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2919 | nc_server_ch_client_endpt_set_address(const char *client_name, const char *endpt_name, const char *address) |
| 2920 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2921 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2922 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2923 | |
| 2924 | if (!client_name) { |
| 2925 | ERRARG("client_name"); |
| 2926 | return -1; |
| 2927 | } else if (!endpt_name) { |
| 2928 | ERRARG("endpt_name"); |
| 2929 | return -1; |
| 2930 | } else if (!address) { |
| 2931 | ERRARG("address"); |
| 2932 | return -1; |
| 2933 | } |
| 2934 | |
| 2935 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2936 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2937 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2938 | return -1; |
| 2939 | } |
| 2940 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 2941 | free(endpt->address); |
| 2942 | endpt->address = strdup(address); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2943 | |
| 2944 | /* UNLOCK */ |
| 2945 | nc_server_ch_client_unlock(client); |
| 2946 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2947 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | API int |
| 2951 | nc_server_ch_client_endpt_set_port(const char *client_name, const char *endpt_name, uint16_t port) |
| 2952 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2953 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2954 | struct nc_ch_endpt *endpt; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2955 | |
| 2956 | if (!client_name) { |
| 2957 | ERRARG("client_name"); |
| 2958 | return -1; |
| 2959 | } else if (!endpt_name) { |
| 2960 | ERRARG("endpt_name"); |
| 2961 | return -1; |
| 2962 | } else if (!port) { |
| 2963 | ERRARG("port"); |
| 2964 | return -1; |
| 2965 | } |
| 2966 | |
| 2967 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2968 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2969 | if (!endpt) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2970 | return -1; |
| 2971 | } |
| 2972 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2973 | endpt->port = port; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2974 | |
| 2975 | /* UNLOCK */ |
| 2976 | nc_server_ch_client_unlock(client); |
| 2977 | |
ravsz | 5c5a442 | 2020-03-31 15:53:21 +0200 | [diff] [blame] | 2978 | return 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2982 | nc_server_ch_client_endpt_enable_keepalives(const char *client_name, const char *endpt_name, int enable) |
| 2983 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2984 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2985 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2986 | |
| 2987 | if (!client_name) { |
| 2988 | ERRARG("client_name"); |
| 2989 | return -1; |
| 2990 | } else if (!endpt_name) { |
| 2991 | ERRARG("endpt_name"); |
| 2992 | return -1; |
| 2993 | } |
| 2994 | |
| 2995 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 2996 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 2997 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 2998 | return -1; |
| 2999 | } |
| 3000 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3001 | endpt->ka.enabled = (enable ? 1 : 0); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3002 | |
| 3003 | /* UNLOCK */ |
| 3004 | nc_server_ch_client_unlock(client); |
| 3005 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 3006 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | API int |
| 3010 | nc_server_ch_client_endpt_set_keepalives(const char *client_name, const char *endpt_name, int idle_time, int max_probes, |
| 3011 | int probe_interval) |
| 3012 | { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3013 | struct nc_ch_client *client; |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3014 | struct nc_ch_endpt *endpt; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3015 | |
| 3016 | if (!client_name) { |
| 3017 | ERRARG("client_name"); |
| 3018 | return -1; |
| 3019 | } else if (!endpt_name) { |
| 3020 | ERRARG("endpt_name"); |
| 3021 | return -1; |
| 3022 | } |
| 3023 | |
| 3024 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3025 | endpt = nc_server_ch_client_lock(client_name, endpt_name, 0, &client); |
| 3026 | if (!endpt) { |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3027 | return -1; |
| 3028 | } |
| 3029 | |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3030 | if (idle_time > -1) { |
| 3031 | endpt->ka.idle_time = idle_time; |
| 3032 | } |
| 3033 | if (max_probes > -1) { |
| 3034 | endpt->ka.max_probes = max_probes; |
| 3035 | } |
| 3036 | if (probe_interval > -1) { |
| 3037 | endpt->ka.probe_interval = probe_interval; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | /* UNLOCK */ |
| 3041 | nc_server_ch_client_unlock(client); |
| 3042 | |
Michal Vasko | 9af829a | 2019-09-12 13:50:00 +0200 | [diff] [blame] | 3043 | return 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3047 | nc_server_ch_client_set_conn_type(const char *client_name, NC_CH_CONN_TYPE conn_type) |
| 3048 | { |
| 3049 | struct nc_ch_client *client; |
| 3050 | |
| 3051 | if (!client_name) { |
| 3052 | ERRARG("client_name"); |
| 3053 | return -1; |
| 3054 | } else if (!conn_type) { |
| 3055 | ERRARG("conn_type"); |
| 3056 | return -1; |
| 3057 | } |
| 3058 | |
| 3059 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3060 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3061 | if (!client) { |
| 3062 | return -1; |
| 3063 | } |
| 3064 | |
| 3065 | if (client->conn_type != conn_type) { |
| 3066 | client->conn_type = conn_type; |
| 3067 | |
| 3068 | /* set default options */ |
| 3069 | switch (conn_type) { |
| 3070 | case NC_CH_PERSIST: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3071 | /* no options */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3072 | break; |
| 3073 | case NC_CH_PERIOD: |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3074 | client->conn.period.period = 60; |
| 3075 | client->conn.period.anchor_time = 0; |
| 3076 | client->conn.period.idle_timeout = 120; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3077 | break; |
| 3078 | default: |
| 3079 | ERRINT; |
| 3080 | break; |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | /* UNLOCK */ |
| 3085 | nc_server_ch_client_unlock(client); |
| 3086 | |
| 3087 | return 0; |
| 3088 | } |
| 3089 | |
| 3090 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3091 | nc_server_ch_client_periodic_set_period(const char *client_name, uint16_t period) |
| 3092 | { |
| 3093 | struct nc_ch_client *client; |
| 3094 | |
| 3095 | if (!client_name) { |
| 3096 | ERRARG("client_name"); |
| 3097 | return -1; |
| 3098 | } else if (!period) { |
| 3099 | ERRARG("period"); |
| 3100 | return -1; |
| 3101 | } |
| 3102 | |
| 3103 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3104 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3105 | if (!client) { |
| 3106 | return -1; |
| 3107 | } |
| 3108 | |
| 3109 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3110 | 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] | 3111 | /* UNLOCK */ |
| 3112 | nc_server_ch_client_unlock(client); |
| 3113 | return -1; |
| 3114 | } |
| 3115 | |
| 3116 | client->conn.period.period = period; |
| 3117 | |
| 3118 | /* UNLOCK */ |
| 3119 | nc_server_ch_client_unlock(client); |
| 3120 | |
| 3121 | return 0; |
| 3122 | } |
| 3123 | |
| 3124 | API int |
| 3125 | 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] | 3126 | { |
| 3127 | struct nc_ch_client *client; |
| 3128 | |
| 3129 | if (!client_name) { |
| 3130 | ERRARG("client_name"); |
| 3131 | return -1; |
| 3132 | } |
| 3133 | |
| 3134 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3135 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3136 | if (!client) { |
| 3137 | return -1; |
| 3138 | } |
| 3139 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3140 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3141 | 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] | 3142 | /* UNLOCK */ |
| 3143 | nc_server_ch_client_unlock(client); |
| 3144 | return -1; |
| 3145 | } |
| 3146 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3147 | client->conn.period.anchor_time = anchor_time; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3148 | |
| 3149 | /* UNLOCK */ |
| 3150 | nc_server_ch_client_unlock(client); |
| 3151 | |
| 3152 | return 0; |
| 3153 | } |
| 3154 | |
| 3155 | API int |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3156 | 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] | 3157 | { |
| 3158 | struct nc_ch_client *client; |
| 3159 | |
| 3160 | if (!client_name) { |
| 3161 | ERRARG("client_name"); |
| 3162 | return -1; |
| 3163 | } |
| 3164 | |
| 3165 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3166 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3167 | if (!client) { |
| 3168 | return -1; |
| 3169 | } |
| 3170 | |
| 3171 | if (client->conn_type != NC_CH_PERIOD) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3172 | 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] | 3173 | /* UNLOCK */ |
| 3174 | nc_server_ch_client_unlock(client); |
| 3175 | return -1; |
| 3176 | } |
| 3177 | |
| 3178 | client->conn.period.idle_timeout = idle_timeout; |
| 3179 | |
| 3180 | /* UNLOCK */ |
| 3181 | nc_server_ch_client_unlock(client); |
| 3182 | |
| 3183 | return 0; |
| 3184 | } |
| 3185 | |
| 3186 | API int |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3187 | nc_server_ch_client_set_start_with(const char *client_name, NC_CH_START_WITH start_with) |
| 3188 | { |
| 3189 | struct nc_ch_client *client; |
| 3190 | |
| 3191 | if (!client_name) { |
| 3192 | ERRARG("client_name"); |
| 3193 | return -1; |
| 3194 | } |
| 3195 | |
| 3196 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3197 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3198 | if (!client) { |
| 3199 | return -1; |
| 3200 | } |
| 3201 | |
| 3202 | client->start_with = start_with; |
| 3203 | |
| 3204 | /* UNLOCK */ |
| 3205 | nc_server_ch_client_unlock(client); |
| 3206 | |
| 3207 | return 0; |
| 3208 | } |
| 3209 | |
| 3210 | API int |
| 3211 | nc_server_ch_client_set_max_attempts(const char *client_name, uint8_t max_attempts) |
| 3212 | { |
| 3213 | struct nc_ch_client *client; |
| 3214 | |
| 3215 | if (!client_name) { |
| 3216 | ERRARG("client_name"); |
| 3217 | return -1; |
| 3218 | } else if (!max_attempts) { |
| 3219 | ERRARG("max_attempts"); |
| 3220 | return -1; |
| 3221 | } |
| 3222 | |
| 3223 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3224 | nc_server_ch_client_lock(client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3225 | if (!client) { |
| 3226 | return -1; |
| 3227 | } |
| 3228 | |
| 3229 | client->max_attempts = max_attempts; |
| 3230 | |
| 3231 | /* UNLOCK */ |
| 3232 | nc_server_ch_client_unlock(client); |
| 3233 | |
| 3234 | return 0; |
| 3235 | } |
| 3236 | |
| 3237 | /* client lock is expected to be held */ |
| 3238 | static NC_MSG_TYPE |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3239 | nc_connect_ch_endpt(struct nc_ch_endpt *endpt, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, void *ctx_cb_data, |
| 3240 | struct nc_session **session) |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3241 | { |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3242 | NC_MSG_TYPE msgtype; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3243 | const struct ly_ctx *ctx; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3244 | int sock, ret; |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3245 | struct timespec ts_cur; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3246 | char *ip_host; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3247 | |
Michal Vasko | 4c612cd | 2021-02-05 08:53:42 +0100 | [diff] [blame] | 3248 | 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] | 3249 | if (sock < 0) { |
Michal Vasko | 0b30e45 | 2021-03-03 10:30:15 +0100 | [diff] [blame] | 3250 | if (endpt->sock_pending > -1) { |
| 3251 | ++endpt->sock_retries; |
| 3252 | if (endpt->sock_retries == NC_SOCKET_CH_RETRIES) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3253 | 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] | 3254 | close(endpt->sock_pending); |
| 3255 | endpt->sock_pending = -1; |
| 3256 | endpt->sock_retries = 0; |
| 3257 | } |
Michal Vasko | 4c612cd | 2021-02-05 08:53:42 +0100 | [diff] [blame] | 3258 | } |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3259 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3260 | } |
Frank Rimpler | 9f838b0 | 2018-07-25 06:44:03 +0000 | [diff] [blame] | 3261 | /* no need to store the socket as pending any longer */ |
| 3262 | endpt->sock_pending = -1; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3263 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3264 | /* acquire context */ |
| 3265 | ctx = acquire_ctx_cb(ctx_cb_data); |
| 3266 | if (!ctx) { |
| 3267 | ERR(NULL, "Failed to acquire context for a new Call Home session."); |
| 3268 | close(sock); |
| 3269 | free(ip_host); |
| 3270 | return NC_MSG_ERROR; |
| 3271 | } |
| 3272 | |
| 3273 | /* create session */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3274 | *session = nc_new_session(NC_SERVER, 0); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3275 | if (!(*session)) { |
| 3276 | ERRMEM; |
| 3277 | close(sock); |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 3278 | free(ip_host); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3279 | return NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3280 | } |
| 3281 | (*session)->status = NC_STATUS_STARTING; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3282 | (*session)->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3283 | (*session)->flags = NC_SESSION_SHAREDCTX; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3284 | (*session)->host = ip_host; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3285 | (*session)->port = endpt->port; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3286 | |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3287 | /* sock gets assigned to session or closed */ |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3288 | #ifdef NC_ENABLED_SSH |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3289 | if (endpt->ti == NC_TI_LIBSSH) { |
| 3290 | (*session)->data = endpt->opts.ssh; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3291 | ret = nc_accept_ssh_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3292 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3293 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3294 | if (ret < 0) { |
| 3295 | msgtype = NC_MSG_ERROR; |
| 3296 | goto fail; |
| 3297 | } else if (!ret) { |
| 3298 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3299 | goto fail; |
| 3300 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3301 | } else |
| 3302 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3303 | #ifdef NC_ENABLED_TLS |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3304 | if (endpt->ti == NC_TI_OPENSSL) { |
| 3305 | (*session)->data = endpt->opts.tls; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 3306 | ret = nc_accept_tls_session(*session, sock, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 2cc4c68 | 2016-03-01 09:16:48 +0100 | [diff] [blame] | 3307 | (*session)->data = NULL; |
Michal Vasko | c6b9c7b | 2016-01-28 11:10:08 +0100 | [diff] [blame] | 3308 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3309 | if (ret < 0) { |
| 3310 | msgtype = NC_MSG_ERROR; |
| 3311 | goto fail; |
| 3312 | } else if (!ret) { |
| 3313 | msgtype = NC_MSG_WOULDBLOCK; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3314 | goto fail; |
| 3315 | } |
Michal Vasko | 3d865d2 | 2016-01-28 16:00:53 +0100 | [diff] [blame] | 3316 | } else |
| 3317 | #endif |
| 3318 | { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3319 | ERRINT; |
| 3320 | close(sock); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3321 | msgtype = NC_MSG_ERROR; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3322 | goto fail; |
| 3323 | } |
| 3324 | |
| 3325 | /* assign new SID atomically */ |
Michal Vasko | 5bd4a3f | 2021-06-17 16:40:10 +0200 | [diff] [blame] | 3326 | (*session)->id = ATOMIC_INC_RELAXED(server_opts.new_session_id); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3327 | |
| 3328 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 3329 | msgtype = nc_handshake_io(*session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3330 | if (msgtype != NC_MSG_HELLO) { |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3331 | goto fail; |
| 3332 | } |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3333 | |
| 3334 | nc_gettimespec_mono(&ts_cur); |
| 3335 | (*session)->opts.server.last_rpc = ts_cur.tv_sec; |
| 3336 | nc_gettimespec_real(&ts_cur); |
| 3337 | (*session)->opts.server.session_start = ts_cur.tv_sec; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3338 | (*session)->status = NC_STATUS_RUNNING; |
| 3339 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3340 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3341 | |
| 3342 | fail: |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 3343 | nc_session_free(*session, NULL); |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3344 | *session = NULL; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 3345 | return msgtype; |
Michal Vasko | b05053d | 2016-01-22 16:12:06 +0100 | [diff] [blame] | 3346 | } |
| 3347 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3348 | struct nc_ch_client_thread_arg { |
| 3349 | char *client_name; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3350 | nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb; |
| 3351 | nc_server_ch_session_release_ctx_cb release_ctx_cb; |
| 3352 | void *ctx_cb_data; |
| 3353 | nc_server_ch_new_session_cb new_session_cb; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3354 | }; |
| 3355 | |
| 3356 | static struct nc_ch_client * |
| 3357 | nc_server_ch_client_with_endpt_lock(const char *name) |
| 3358 | { |
| 3359 | struct nc_ch_client *client; |
| 3360 | |
| 3361 | while (1) { |
| 3362 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3363 | nc_server_ch_client_lock(name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3364 | if (!client) { |
| 3365 | return NULL; |
| 3366 | } |
| 3367 | if (client->ch_endpt_count) { |
| 3368 | return client; |
| 3369 | } |
| 3370 | /* no endpoints defined yet */ |
| 3371 | |
| 3372 | /* UNLOCK */ |
| 3373 | nc_server_ch_client_unlock(client); |
| 3374 | |
| 3375 | usleep(NC_CH_NO_ENDPT_WAIT * 1000); |
| 3376 | } |
| 3377 | |
| 3378 | return NULL; |
| 3379 | } |
| 3380 | |
| 3381 | static int |
| 3382 | nc_server_ch_client_thread_session_cond_wait(struct nc_session *session, struct nc_ch_client_thread_arg *data) |
| 3383 | { |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3384 | int ret = 0, r; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3385 | uint32_t idle_timeout; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3386 | struct timespec ts; |
| 3387 | struct nc_ch_client *client; |
| 3388 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3389 | /* CH LOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3390 | pthread_mutex_lock(&session->opts.server.ch_lock); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3391 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame^] | 3392 | session->flags |= NC_SESSION_CH_THREAD; |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3393 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3394 | /* give the session to the user */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3395 | if (data->new_session_cb(data->client_name, session)) { |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3396 | /* something is wrong, free the session */ |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame^] | 3397 | session->flags &= ~NC_SESSION_CH_THREAD; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3398 | |
| 3399 | /* CH UNLOCK */ |
| 3400 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
| 3401 | |
| 3402 | nc_session_free(session, NULL); |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3403 | goto release_ctx; |
Michal Vasko | f1c26c2 | 2021-04-12 16:34:33 +0200 | [diff] [blame] | 3404 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3405 | |
| 3406 | do { |
Michal Vasko | 77a6abe | 2017-10-05 10:02:20 +0200 | [diff] [blame] | 3407 | nc_gettimespec_real(&ts); |
Michal Vasko | e39ae6b | 2017-03-14 09:03:46 +0100 | [diff] [blame] | 3408 | nc_addtimespec(&ts, NC_CH_NO_ENDPT_WAIT); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3409 | |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3410 | /* CH COND WAIT */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3411 | 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] | 3412 | if (!r) { |
| 3413 | /* we were woken up, something probably happened */ |
| 3414 | if (session->status != NC_STATUS_RUNNING) { |
| 3415 | break; |
| 3416 | } |
| 3417 | } else if (r != ETIMEDOUT) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3418 | ERR(session, "Pthread condition timedwait failed (%s).", strerror(r)); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3419 | ret = -1; |
| 3420 | break; |
Michal Vasko | 2e39ed9 | 2018-03-12 13:51:44 +0100 | [diff] [blame] | 3421 | } |
| 3422 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3423 | /* check whether the client was not removed */ |
| 3424 | /* LOCK */ |
Michal Vasko | adf30f0 | 2019-06-24 09:34:47 +0200 | [diff] [blame] | 3425 | nc_server_ch_client_lock(data->client_name, NULL, 0, &client); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3426 | if (!client) { |
| 3427 | /* client was removed, finish thread */ |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3428 | 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] | 3429 | data->client_name); |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3430 | ret = 1; |
| 3431 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3432 | } |
| 3433 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3434 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3435 | idle_timeout = client->conn.period.idle_timeout; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3436 | } else { |
| 3437 | idle_timeout = 0; |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3438 | } |
| 3439 | |
Michal Vasko | 9fb4227 | 2017-10-05 13:50:05 +0200 | [diff] [blame] | 3440 | nc_gettimespec_mono(&ts); |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3441 | if (!session->opts.server.ntf_status && idle_timeout && (ts.tv_sec >= session->opts.server.last_rpc + idle_timeout)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3442 | VRB(session, "Call Home client \"%s\": session idle timeout elapsed.", client->name); |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3443 | session->status = NC_STATUS_INVALID; |
| 3444 | session->term_reason = NC_SESSION_TERM_TIMEOUT; |
| 3445 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3446 | |
| 3447 | /* UNLOCK */ |
| 3448 | nc_server_ch_client_unlock(client); |
| 3449 | |
| 3450 | } while (session->status == NC_STATUS_RUNNING); |
| 3451 | |
Michal Vasko | feccb31 | 2022-03-24 15:24:59 +0100 | [diff] [blame^] | 3452 | /* signal to nc_session_free() that CH thread is terminating */ |
| 3453 | session->flags &= ~NC_SESSION_CH_THREAD; |
| 3454 | pthread_cond_signal(&session->opts.server.ch_cond); |
Michal Vasko | 0db3db5 | 2021-03-03 10:45:42 +0100 | [diff] [blame] | 3455 | |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3456 | /* CH UNLOCK */ |
Michal Vasko | acf9847 | 2021-02-04 15:33:57 +0100 | [diff] [blame] | 3457 | pthread_mutex_unlock(&session->opts.server.ch_lock); |
Michal Vasko | 2737742 | 2018-03-15 08:59:35 +0100 | [diff] [blame] | 3458 | |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3459 | release_ctx: |
| 3460 | /* session terminated, release its context */ |
| 3461 | data->release_ctx_cb(data->ctx_cb_data); |
| 3462 | |
Michal Vasko | 3f05a09 | 2018-03-13 10:39:49 +0100 | [diff] [blame] | 3463 | return ret; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | static void * |
| 3467 | nc_ch_client_thread(void *arg) |
| 3468 | { |
| 3469 | struct nc_ch_client_thread_arg *data = (struct nc_ch_client_thread_arg *)arg; |
| 3470 | NC_MSG_TYPE msgtype; |
| 3471 | uint8_t cur_attempts = 0; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3472 | uint16_t next_endpt_index; |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3473 | char *cur_endpt_name = NULL; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3474 | struct nc_ch_endpt *cur_endpt; |
| 3475 | struct nc_session *session; |
| 3476 | struct nc_ch_client *client; |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3477 | uint32_t client_id; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3478 | time_t reconnect_in; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3479 | |
| 3480 | /* LOCK */ |
| 3481 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3482 | if (!client) { |
| 3483 | goto cleanup; |
| 3484 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3485 | client_id = client->id; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3486 | |
| 3487 | cur_endpt = &client->ch_endpts[0]; |
| 3488 | cur_endpt_name = strdup(cur_endpt->name); |
| 3489 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3490 | VRB(NULL, "Call Home client \"%s\" connecting...", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3491 | while (1) { |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3492 | msgtype = nc_connect_ch_endpt(cur_endpt, data->acquire_ctx_cb, data->ctx_cb_data, &session); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3493 | |
| 3494 | if (msgtype == NC_MSG_HELLO) { |
| 3495 | /* UNLOCK */ |
| 3496 | nc_server_ch_client_unlock(client); |
| 3497 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3498 | 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] | 3499 | if (nc_server_ch_client_thread_session_cond_wait(session, data)) { |
| 3500 | goto cleanup; |
| 3501 | } |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3502 | VRB(NULL, "Call Home client \"%s\" session terminated.", data->client_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3503 | |
| 3504 | /* LOCK */ |
| 3505 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3506 | if (!client) { |
| 3507 | goto cleanup; |
| 3508 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3509 | if (client->id != client_id) { |
| 3510 | nc_server_ch_client_unlock(client); |
| 3511 | goto cleanup; |
| 3512 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3513 | |
| 3514 | /* session changed status -> it was disconnected for whatever reason, |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3515 | * persistent connection immediately tries to reconnect, periodic connects at specific times */ |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3516 | if (client->conn_type == NC_CH_PERIOD) { |
Michal Vasko | 18e1fa0 | 2021-11-29 09:02:05 +0100 | [diff] [blame] | 3517 | if (client->conn.period.anchor_time) { |
| 3518 | /* anchored */ |
| 3519 | reconnect_in = (time(NULL) - client->conn.period.anchor_time) % (client->conn.period.period * 60); |
| 3520 | } else { |
| 3521 | /* fixed timeout */ |
| 3522 | reconnect_in = client->conn.period.period * 60; |
| 3523 | } |
| 3524 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3525 | /* UNLOCK */ |
| 3526 | nc_server_ch_client_unlock(client); |
| 3527 | |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3528 | /* 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] | 3529 | 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] | 3530 | sleep(reconnect_in); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3531 | |
| 3532 | /* LOCK */ |
| 3533 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3534 | if (!client) { |
| 3535 | goto cleanup; |
| 3536 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3537 | if (client->id != client_id) { |
| 3538 | nc_server_ch_client_unlock(client); |
| 3539 | goto cleanup; |
| 3540 | } |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | /* set next endpoint to try */ |
| 3544 | if (client->start_with == NC_CH_FIRST_LISTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3545 | next_endpt_index = 0; |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3546 | } else if (client->start_with == NC_CH_LAST_CONNECTED) { |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3547 | /* we keep the current one but due to unlock/lock we have to find it again */ |
| 3548 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3549 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
| 3550 | break; |
| 3551 | } |
| 3552 | } |
| 3553 | if (next_endpt_index >= client->ch_endpt_count) { |
| 3554 | /* endpoint was removed, start with the first one */ |
| 3555 | next_endpt_index = 0; |
| 3556 | } |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 3557 | } else { |
| 3558 | /* just get a random index */ |
| 3559 | next_endpt_index = rand() % client->ch_endpt_count; |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3562 | } else { |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3563 | /* UNLOCK */ |
| 3564 | nc_server_ch_client_unlock(client); |
| 3565 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3566 | /* session was not created */ |
Michal Vasko | c4bc581 | 2016-10-13 10:59:36 +0200 | [diff] [blame] | 3567 | usleep(NC_CH_ENDPT_FAIL_WAIT * 1000); |
| 3568 | |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3569 | /* LOCK */ |
| 3570 | client = nc_server_ch_client_with_endpt_lock(data->client_name); |
| 3571 | if (!client) { |
| 3572 | goto cleanup; |
| 3573 | } |
Andrew Langefeld | 6ed922d | 2018-09-12 14:08:32 -0500 | [diff] [blame] | 3574 | if (client->id != client_id) { |
| 3575 | nc_server_ch_client_unlock(client); |
| 3576 | goto cleanup; |
| 3577 | } |
Michal Vasko | 6bb116b | 2016-10-26 13:53:46 +0200 | [diff] [blame] | 3578 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3579 | ++cur_attempts; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3580 | |
| 3581 | /* try to find our endpoint again */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3582 | for (next_endpt_index = 0; next_endpt_index < client->ch_endpt_count; ++next_endpt_index) { |
| 3583 | if (!strcmp(client->ch_endpts[next_endpt_index].name, cur_endpt_name)) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3584 | break; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3585 | } |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3586 | } |
| 3587 | |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3588 | if (next_endpt_index >= client->ch_endpt_count) { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3589 | /* endpoint was removed, start with the first one */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3590 | next_endpt_index = 0; |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3591 | cur_attempts = 0; |
| 3592 | } else if (cur_attempts == client->max_attempts) { |
| 3593 | /* we have tried to connect to this endpoint enough times */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3594 | if (next_endpt_index < client->ch_endpt_count - 1) { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3595 | /* just go to the next endpoint */ |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3596 | ++next_endpt_index; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3597 | } else { |
Michal Vasko | 4cb8de5 | 2018-04-23 14:38:07 +0200 | [diff] [blame] | 3598 | /* cur_endpoint is the last, start with the first one */ |
Michal Vasko | 2a22534 | 2018-09-05 08:38:34 +0200 | [diff] [blame] | 3599 | next_endpt_index = 0; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | cur_attempts = 0; |
| 3603 | } /* else we keep the current one */ |
| 3604 | } |
Peter Feige | d05f225 | 2018-09-03 08:09:47 +0000 | [diff] [blame] | 3605 | |
| 3606 | cur_endpt = &client->ch_endpts[next_endpt_index]; |
| 3607 | free(cur_endpt_name); |
| 3608 | cur_endpt_name = strdup(cur_endpt->name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | cleanup: |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3612 | VRB(NULL, "Call Home client \"%s\" thread exit.", data->client_name); |
Michal Vasko | 9550cf1 | 2017-03-21 15:33:58 +0100 | [diff] [blame] | 3613 | free(cur_endpt_name); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3614 | free(data->client_name); |
| 3615 | free(data); |
| 3616 | return NULL; |
| 3617 | } |
| 3618 | |
| 3619 | API int |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3620 | nc_connect_ch_client_dispatch(const char *client_name, nc_server_ch_session_acquire_ctx_cb acquire_ctx_cb, |
| 3621 | 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] | 3622 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3623 | int ret; |
| 3624 | pthread_t tid; |
| 3625 | struct nc_ch_client_thread_arg *arg; |
| 3626 | |
| 3627 | if (!client_name) { |
| 3628 | ERRARG("client_name"); |
| 3629 | return -1; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3630 | } else if (!acquire_ctx_cb) { |
| 3631 | ERRARG("acquire_ctx_cb"); |
| 3632 | return -1; |
| 3633 | } else if (!release_ctx_cb) { |
| 3634 | ERRARG("release_ctx_cb"); |
| 3635 | return -1; |
| 3636 | } else if (!new_session_cb) { |
| 3637 | ERRARG("new_session_cb"); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3638 | return -1; |
| 3639 | } |
| 3640 | |
| 3641 | arg = malloc(sizeof *arg); |
| 3642 | if (!arg) { |
| 3643 | ERRMEM; |
| 3644 | return -1; |
| 3645 | } |
| 3646 | arg->client_name = strdup(client_name); |
| 3647 | if (!arg->client_name) { |
| 3648 | ERRMEM; |
| 3649 | free(arg); |
| 3650 | return -1; |
| 3651 | } |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 3652 | arg->acquire_ctx_cb = acquire_ctx_cb; |
| 3653 | arg->release_ctx_cb = release_ctx_cb; |
| 3654 | arg->ctx_cb_data = ctx_cb_data; |
| 3655 | arg->new_session_cb = new_session_cb; |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3656 | |
| 3657 | ret = pthread_create(&tid, NULL, nc_ch_client_thread, arg); |
| 3658 | if (ret) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 3659 | ERR(NULL, "Creating a new thread failed (%s).", strerror(ret)); |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3660 | free(arg->client_name); |
| 3661 | free(arg); |
| 3662 | return -1; |
| 3663 | } |
| 3664 | /* the thread now manages arg */ |
| 3665 | |
| 3666 | pthread_detach(tid); |
| 3667 | |
| 3668 | return 0; |
| 3669 | } |
| 3670 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 3671 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3672 | |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3673 | API time_t |
| 3674 | nc_session_get_start_time(const struct nc_session *session) |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3675 | { |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3676 | if (!session || (session->side != NC_SERVER)) { |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3677 | ERRARG("session"); |
Michal Vasko | c45ebd3 | 2016-05-25 11:17:36 +0200 | [diff] [blame] | 3678 | return 0; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3679 | } |
| 3680 | |
Michal Vasko | 2e6defd | 2016-10-07 15:48:15 +0200 | [diff] [blame] | 3681 | return session->opts.server.session_start; |
Michal Vasko | f835235 | 2016-05-24 09:11:36 +0200 | [diff] [blame] | 3682 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3683 | |
| 3684 | API void |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3685 | nc_session_inc_notif_status(struct nc_session *session) |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3686 | { |
| 3687 | if (!session || (session->side != NC_SERVER)) { |
| 3688 | ERRARG("session"); |
| 3689 | return; |
| 3690 | } |
| 3691 | |
Michal Vasko | 71dbd77 | 2021-03-23 14:08:37 +0100 | [diff] [blame] | 3692 | ++session->opts.server.ntf_status; |
| 3693 | } |
| 3694 | |
| 3695 | API void |
| 3696 | nc_session_dec_notif_status(struct nc_session *session) |
| 3697 | { |
| 3698 | if (!session || (session->side != NC_SERVER)) { |
| 3699 | ERRARG("session"); |
| 3700 | return; |
| 3701 | } |
| 3702 | |
| 3703 | if (session->opts.server.ntf_status) { |
| 3704 | --session->opts.server.ntf_status; |
| 3705 | } |
Michal Vasko | 3486a7c | 2017-03-03 13:28:07 +0100 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | API int |
| 3709 | nc_session_get_notif_status(const struct nc_session *session) |
| 3710 | { |
| 3711 | if (!session || (session->side != NC_SERVER)) { |
| 3712 | ERRARG("session"); |
| 3713 | return 0; |
| 3714 | } |
| 3715 | |
| 3716 | return session->opts.server.ntf_status; |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 3717 | } |
Michal Vasko | 8f43059 | 2019-02-26 08:32:54 +0100 | [diff] [blame] | 3718 | |
| 3719 | API int |
| 3720 | nc_session_is_callhome(const struct nc_session *session) |
| 3721 | { |
| 3722 | if (!session || (session->side != NC_SERVER)) { |
| 3723 | ERRARG("session"); |
| 3724 | return 0; |
| 3725 | } |
| 3726 | |
| 3727 | if (session->flags & NC_SESSION_CALLHOME) { |
| 3728 | return 1; |
| 3729 | } |
| 3730 | |
| 3731 | return 0; |
| 3732 | } |