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