Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets-test-server - libwebsockets test implementation |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <getopt.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/stat.h> |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 28 | #include <sys/queue.h> |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 29 | #include <fcntl.h> |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 30 | #include <pthread.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 31 | #include <errno.h> |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 32 | #include <nc_client.h> |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 33 | #include <libwebsockets.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 34 | |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 35 | #include "notification_server.h" |
| 36 | #include "netopeerguid.h" |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 37 | #include "../config.h" |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 38 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 39 | #ifdef TEST_NOTIFICATION_SERVER |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 40 | static int force_exit = 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #if defined(TEST_NOTIFICATION_SERVER) || defined(WITH_NOTIFICATIONS) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 44 | static int max_poll_elements; |
| 45 | |
| 46 | static struct pollfd *pollfds; |
| 47 | static int *fd_lookup; |
| 48 | static int count_pollfds; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 49 | static struct libwebsocket_context *context = NULL; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 50 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 51 | struct ntf_thread_config { |
| 52 | struct nc_session *session; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 53 | char *session_id; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 56 | extern struct session_with_mutex *netconf_sessions_list; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 57 | static pthread_key_t thread_key; |
| 58 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 59 | /* |
| 60 | * This demo server shows how to use libwebsockets for one or more |
| 61 | * websocket protocols in the same server |
| 62 | * |
| 63 | * It defines the following websocket protocols: |
| 64 | * |
| 65 | * dumb-increment-protocol: once the socket is opened, an incrementing |
| 66 | * ascii string is sent down it every 50ms. |
| 67 | * If you send "reset\n" on the websocket, then |
| 68 | * the incrementing number is reset to 0. |
| 69 | * |
| 70 | * lws-mirror-protocol: copies any received packet to every connection also |
| 71 | * using this protocol, including the sender |
| 72 | */ |
| 73 | |
| 74 | enum demo_protocols { |
| 75 | /* always first */ |
| 76 | PROTOCOL_HTTP = 0, |
| 77 | |
| 78 | PROTOCOL_NOTIFICATION, |
| 79 | |
| 80 | /* always last */ |
| 81 | DEMO_PROTOCOL_COUNT |
| 82 | }; |
| 83 | |
| 84 | |
| 85 | #define LOCAL_RESOURCE_PATH "." |
| 86 | char *resource_path = LOCAL_RESOURCE_PATH; |
| 87 | |
| 88 | /* |
| 89 | * We take a strict whitelist approach to stop ../ attacks |
| 90 | */ |
| 91 | |
| 92 | struct serveable { |
| 93 | const char *urlpath; |
| 94 | const char *mimetype; |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 95 | }; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 96 | |
| 97 | static const struct serveable whitelist[] = { |
| 98 | { "/favicon.ico", "image/x-icon" }, |
| 99 | { "/libwebsockets.org-logo.png", "image/png" }, |
| 100 | |
| 101 | /* last one is the default served if no match */ |
| 102 | { "/test.html", "text/html" }, |
| 103 | }; |
| 104 | |
| 105 | struct per_session_data__http { |
| 106 | int fd; |
| 107 | }; |
| 108 | |
| 109 | /* this protocol server (always the first one) just knows how to do HTTP */ |
| 110 | |
| 111 | static int callback_http(struct libwebsocket_context *context, |
| 112 | struct libwebsocket *wsi, |
| 113 | enum libwebsocket_callback_reasons reason, void *user, |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 114 | void *in, size_t UNUSED(len)) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 115 | { |
| 116 | char client_name[128]; |
| 117 | char client_ip[128]; |
| 118 | char buf[256]; |
| 119 | int n, m; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 120 | static unsigned char buffer[4096]; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 121 | struct per_session_data__http *pss = (struct per_session_data__http *)user; |
Tomas Cejka | 11d5d06 | 2014-07-15 13:13:52 +0200 | [diff] [blame] | 122 | struct libwebsocket_pollargs *pa = (struct libwebsocket_pollargs *) in; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 123 | |
| 124 | switch (reason) { |
| 125 | case LWS_CALLBACK_HTTP: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 126 | for (n = 0; n < (signed) (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 127 | if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0) |
| 128 | break; |
| 129 | |
| 130 | sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath); |
| 131 | |
Tomas Cejka | dc8f08d | 2015-04-29 11:08:24 +0200 | [diff] [blame] | 132 | if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype, NULL, 0)) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 133 | return -1; /* through completion or error, close the socket */ |
| 134 | |
| 135 | /* |
| 136 | * notice that the sending of the file completes asynchronously, |
| 137 | * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when |
| 138 | * it's done |
| 139 | */ |
| 140 | |
| 141 | break; |
| 142 | |
| 143 | case LWS_CALLBACK_HTTP_FILE_COMPLETION: |
| 144 | // lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n"); |
| 145 | /* kill the connection after we sent one file */ |
| 146 | return -1; |
| 147 | |
| 148 | case LWS_CALLBACK_HTTP_WRITEABLE: |
| 149 | /* |
| 150 | * we can send more of whatever it is we were sending |
| 151 | */ |
| 152 | |
| 153 | do { |
| 154 | n = read(pss->fd, buffer, sizeof buffer); |
| 155 | /* problem reading, close conn */ |
| 156 | if (n < 0) |
| 157 | goto bail; |
| 158 | /* sent it all, close conn */ |
| 159 | if (n == 0) |
| 160 | goto bail; |
| 161 | /* |
| 162 | * because it's HTTP and not websocket, don't need to take |
| 163 | * care about pre and postamble |
| 164 | */ |
| 165 | m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP); |
| 166 | if (m < 0) |
| 167 | /* write failed, close conn */ |
| 168 | goto bail; |
| 169 | if (m != n) |
| 170 | /* partial write, adjust */ |
| 171 | lseek(pss->fd, m - n, SEEK_CUR); |
| 172 | |
| 173 | } while (!lws_send_pipe_choked(wsi)); |
| 174 | libwebsocket_callback_on_writable(context, wsi); |
| 175 | break; |
| 176 | |
| 177 | bail: |
| 178 | close(pss->fd); |
| 179 | return -1; |
| 180 | |
Tomas Cejka | 11d5d06 | 2014-07-15 13:13:52 +0200 | [diff] [blame] | 181 | case LWS_CALLBACK_LOCK_POLL: |
| 182 | /* |
| 183 | * lock mutex to protect pollfd state |
| 184 | * called before any other POLL related callback |
| 185 | */ |
| 186 | break; |
| 187 | |
| 188 | case LWS_CALLBACK_UNLOCK_POLL: |
| 189 | /* |
| 190 | * unlock mutex to protect pollfd state when |
| 191 | * called after any other POLL related callback |
| 192 | */ |
| 193 | break; |
| 194 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 195 | /* |
| 196 | * callback for confirming to continue with client IP appear in |
| 197 | * protocol 0 callback since no websocket protocol has been agreed |
| 198 | * yet. You can just ignore this if you won't filter on client IP |
| 199 | * since the default uhandled callback return is 0 meaning let the |
| 200 | * connection continue. |
| 201 | */ |
| 202 | |
| 203 | case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: |
| 204 | libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name, |
| 205 | sizeof(client_name), client_ip, sizeof(client_ip)); |
| 206 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 207 | //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 208 | /* if we returned non-zero from here, we kill the connection */ |
| 209 | break; |
| 210 | |
| 211 | /* |
| 212 | * callbacks for managing the external poll() array appear in |
| 213 | * protocol 0 callback |
| 214 | */ |
| 215 | |
| 216 | case LWS_CALLBACK_ADD_POLL_FD: |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 217 | if (count_pollfds >= max_poll_elements) { |
| 218 | lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n"); |
| 219 | return 1; |
| 220 | } |
| 221 | |
Tomas Cejka | 11d5d06 | 2014-07-15 13:13:52 +0200 | [diff] [blame] | 222 | fd_lookup[pa->fd] = count_pollfds; |
| 223 | pollfds[count_pollfds].fd = pa->fd; |
| 224 | pollfds[count_pollfds].events = pa->events; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 225 | pollfds[count_pollfds++].revents = 0; |
| 226 | break; |
| 227 | |
| 228 | case LWS_CALLBACK_DEL_POLL_FD: |
| 229 | if (!--count_pollfds) |
| 230 | break; |
Tomas Cejka | 11d5d06 | 2014-07-15 13:13:52 +0200 | [diff] [blame] | 231 | m = fd_lookup[pa->fd]; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 232 | /* have the last guy take up the vacant slot */ |
| 233 | pollfds[m] = pollfds[count_pollfds]; |
| 234 | fd_lookup[pollfds[count_pollfds].fd] = m; |
| 235 | break; |
| 236 | |
Tomas Cejka | 11d5d06 | 2014-07-15 13:13:52 +0200 | [diff] [blame] | 237 | case LWS_CALLBACK_CHANGE_MODE_POLL_FD: |
| 238 | pollfds[fd_lookup[pa->fd]].events = pa->events; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 239 | break; |
| 240 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 241 | |
| 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 249 | |
| 250 | /* dumb_increment protocol */ |
| 251 | |
| 252 | /* |
| 253 | * one of these is auto-created for each connection and a pointer to the |
| 254 | * appropriate instance is passed to the callback in the user parameter |
| 255 | * |
| 256 | * for this example protocol we use it to individualize the count for each |
| 257 | * connection. |
| 258 | */ |
| 259 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 260 | struct per_session_data__notif_client { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 261 | int number; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 262 | char *session_id; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 263 | struct nc_session *session; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 264 | }; |
| 265 | |
Michal Vasko | 3d8681d | 2015-11-05 10:23:41 +0100 | [diff] [blame] | 266 | struct session_with_mutex *get_ncsession_from_sid(const char *session_id) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 267 | { |
| 268 | struct session_with_mutex *locked_session = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 269 | if (session_id == NULL) { |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 270 | return (NULL); |
| 271 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 272 | for (locked_session = netconf_sessions_list; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 273 | locked_session && (nc_session_get_id(locked_session->session) == (unsigned)atoi(session_id)); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 274 | locked_session = locked_session->next); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 275 | return locked_session; |
| 276 | } |
| 277 | |
| 278 | /* rpc parameter is freed after the function call */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 279 | static int send_recv_process(struct nc_session *session, const char* UNUSED(operation), struct nc_rpc* rpc) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 280 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 281 | struct nc_reply *reply = NULL; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 282 | char *data = NULL; |
| 283 | int ret = EXIT_SUCCESS; |
| 284 | |
| 285 | /* send the request and get the reply */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 286 | switch (netconf_send_recv_timed(session, rpc, 50000, 0, &reply)) { |
| 287 | case NC_MSG_ERROR: |
| 288 | if (nc_session_get_status(session) != NC_STATUS_RUNNING) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 289 | ERROR("notifications: receiving rpc-reply failed."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 290 | //cmd_disconnect(NULL); |
| 291 | ret = EXIT_FAILURE; |
| 292 | break; |
| 293 | } |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 294 | ERROR("notifications: Unknown error occurred."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 295 | ret = EXIT_FAILURE; |
| 296 | break; |
| 297 | case NC_MSG_NONE: |
| 298 | /* error occurred, but processed by callback */ |
| 299 | break; |
| 300 | case NC_MSG_REPLY: |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 301 | switch (reply->type) { |
| 302 | case NC_RPL_OK: |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 303 | break; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 304 | case NC_RPL_DATA: |
| 305 | lyd_print_mem(&data, ((struct nc_reply_data *)reply)->data, LYD_JSON, 0); |
| 306 | DEBUG("notifications: recv: %s.", data); |
| 307 | free(data); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 308 | break; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 309 | case NC_RPL_ERROR: |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 310 | /* wtf, you shouldn't be here !?!? */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 311 | DEBUG("notifications: operation failed, but rpc-error was not processed."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 312 | ret = EXIT_FAILURE; |
| 313 | break; |
| 314 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 315 | DEBUG("notifications: unexpected operation result."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 316 | ret = EXIT_FAILURE; |
| 317 | break; |
| 318 | } |
| 319 | break; |
| 320 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 321 | DEBUG("notifications: Unknown error occurred."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 322 | ret = EXIT_FAILURE; |
| 323 | break; |
| 324 | } |
| 325 | nc_rpc_free(rpc); |
| 326 | nc_reply_free(reply); |
| 327 | |
| 328 | return (ret); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * \brief Callback to store incoming notification |
| 333 | * \param [in] eventtime - time when notification occured |
| 334 | * \param [in] content - content of notification |
| 335 | */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 336 | static void notification_fileprint(struct nc_session *session, const struct nc_notif *notif) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 337 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 338 | time_t eventtime; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 339 | struct session_with_mutex *target_session = NULL; |
| 340 | notification_t *ntf = NULL; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 341 | const char *session_id = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 342 | char *content; |
| 343 | (void)session; |
| 344 | |
| 345 | eventtime = nc_datetime2time(notif->datetime); |
| 346 | lyd_print_mem(&content, notif->tree, LYD_JSON, 0); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 347 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 348 | DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 349 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 350 | session_id = pthread_getspecific(thread_key); |
| 351 | DEBUG("notification: fileprint getspecific (%s)", session_id); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 352 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 353 | ERROR("notifications: Error while locking rwlock"); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 354 | free(content); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 355 | return; |
| 356 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 357 | DEBUG("Get session with mutex from key %s.", session_id); |
Michal Vasko | 3d8681d | 2015-11-05 10:23:41 +0100 | [diff] [blame] | 358 | target_session = get_ncsession_from_sid(session_id); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 359 | if (target_session == NULL) { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 360 | ERROR("notifications: no session found last_session_key (%s)", session_id); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 361 | free(content); |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 362 | goto unlock_glob; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 363 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 364 | if (pthread_mutex_lock(&target_session->lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 365 | ERROR("notifications: Error while locking rwlock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 366 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 367 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 368 | DEBUG("notification: ready to push to notifications queue"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 369 | if (target_session->notif_count < NOTIFICATION_QUEUE_SIZE) { |
| 370 | ++target_session->notif_count; |
| 371 | target_session->notifications = realloc(target_session->notifications, |
| 372 | target_session->notif_count * sizeof *target_session->notifications); |
| 373 | if (target_session->notifications) { |
| 374 | ntf = target_session->notifications + target_session->notif_count - 1; |
| 375 | } |
| 376 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 377 | if (ntf == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 378 | ERROR("notifications: Failed to allocate element "); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 379 | free(content); |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 380 | goto unlock_all; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 381 | } |
Tomas Cejka | 7328693 | 2013-05-27 22:54:35 +0200 | [diff] [blame] | 382 | ntf->eventtime = eventtime; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 383 | ntf->content = content; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 384 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 385 | DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 386 | |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 387 | unlock_all: |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 388 | if (pthread_mutex_unlock(&target_session->lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 389 | ERROR("notifications: Error while unlocking rwlock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 390 | } |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 391 | unlock_glob: |
| 392 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 393 | ERROR("notifications: Error while locking rwlock"); |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 394 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * \brief Thread for libnetconf notifications dispatch |
| 399 | * \param [in] arg - struct ntf_thread_config * with nc_session |
| 400 | */ |
| 401 | void* notification_thread(void* arg) |
| 402 | { |
| 403 | struct ntf_thread_config *config = (struct ntf_thread_config*)arg; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 404 | DEBUG("notifications: in thread for libnetconf notifications"); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 405 | |
| 406 | /* store hash identification of netconf session for notifications printing callback */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 407 | if (pthread_setspecific(thread_key, config->session_id) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 408 | ERROR("notifications: cannot set thread-specific hash value."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 409 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 410 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 411 | DEBUG("notifications: dispatching"); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 412 | nc_recv_notif_dispatch(config->session, notification_fileprint); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 413 | DEBUG("notifications: ended thread for libnetconf notifications"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 414 | if (config->session_id != NULL) { |
| 415 | free(config->session_id); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 416 | } |
| 417 | if (config != NULL) { |
| 418 | free(config); |
| 419 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 420 | return (NULL); |
| 421 | } |
| 422 | |
| 423 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 424 | int notif_subscribe(struct session_with_mutex *locked_session, const char *session_id, time_t start_time, time_t stop_time) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 425 | { |
| 426 | time_t start = -1; |
| 427 | time_t stop = -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 428 | char *stream = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 429 | struct nc_rpc *rpc = NULL; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 430 | pthread_t thread; |
| 431 | struct ntf_thread_config *tconfig; |
| 432 | struct nc_session *session; |
| 433 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 434 | DEBUG("notif_subscribe"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 435 | if (locked_session == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 436 | DEBUG("notifications: no locked_session was given."); |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 437 | /* Close notification client */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 438 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 441 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 442 | session = locked_session->session; |
| 443 | |
| 444 | start = time(NULL) + start_time; |
| 445 | stop = time(NULL) + stop_time; |
Tomas Cejka | 5771414 | 2014-06-22 17:59:58 +0200 | [diff] [blame] | 446 | start = 0; |
| 447 | stop = 0; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 448 | DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 449 | |
| 450 | if (session == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 451 | ERROR("notifications: NETCONF session not established."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 452 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 455 | /* check times */ |
| 456 | if (start != -1 && stop != -1 && start > stop) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 457 | ERROR("notifications: Subscription start time must be lower than the end time."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 458 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 461 | DEBUG("Prepare to execute subscription."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 462 | /* create requests */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 463 | rpc = nc_rpc_subscribe(stream, NULL, (start_time == -1) ? NULL : nc_time2datetime(start, NULL), |
| 464 | (stop_time == 0) ? NULL : nc_time2datetime(stop, NULL), NC_PARAMTYPE_CONST); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 465 | if (rpc == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 466 | ERROR("notifications: creating an rpc request failed."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 467 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 470 | DEBUG("Send NC subscribe."); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 471 | create_err_reply_p(); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 472 | if (send_recv_process(session, "subscribe", rpc) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 473 | ERROR("Subscription RPC failed."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 474 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 475 | } |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 476 | |
| 477 | GETSPEC_ERR_REPLY |
| 478 | if (err_reply != NULL) { |
| 479 | free_err_reply(); |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 480 | ERROR("RPC-Error received and cleaned, because we can't send it anywhere."); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 481 | goto operation_failed; |
| 482 | } |
| 483 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 484 | rpc = NULL; /* just note that rpc is already freed by send_recv_process() */ |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 485 | locked_session->ntfc_subscribed = 1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 486 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 487 | DEBUG("Create config for notification_thread."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 488 | tconfig = malloc(sizeof(struct ntf_thread_config)); |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 489 | if (tconfig == NULL) { |
| 490 | ERROR("notifications: Allocation failed."); |
| 491 | goto operation_failed; |
| 492 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 493 | tconfig->session = session; |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 494 | tconfig->session_id = strdup(session_id); |
| 495 | DEBUG("notifications: creating libnetconf notification thread (%s).", tconfig->session_id); |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 496 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 497 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 498 | DEBUG("Create notification_thread."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 499 | if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 500 | ERROR("notifications: creating a thread for receiving notifications failed"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 501 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 502 | } |
| 503 | pthread_detach(thread); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 504 | DEBUG("Subscription finished."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 505 | return 0; |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 506 | |
| 507 | operation_failed: |
| 508 | pthread_mutex_unlock(&locked_session->lock); |
| 509 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 510 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 511 | |
| 512 | static int callback_notification(struct libwebsocket_context *context, |
| 513 | struct libwebsocket *wsi, |
| 514 | enum libwebsocket_callback_reasons reason, |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 515 | void *user, void *in, size_t len) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 516 | { |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 517 | int n = 0, m = 0, i; |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 518 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING]; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 519 | unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 520 | struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user; |
| 521 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 522 | switch (reason) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 523 | case LWS_CALLBACK_ESTABLISHED: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 524 | DEBUG("notification client connected."); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 525 | break; |
| 526 | |
| 527 | case LWS_CALLBACK_SERVER_WRITEABLE: |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 528 | if (pss->session_id == NULL) { |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 529 | return 0; |
| 530 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 531 | //DEBUG("Callback server writeable."); |
| 532 | //DEBUG("lock session lock."); |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 533 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 534 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 535 | return -1; |
| 536 | } |
Michal Vasko | 3d8681d | 2015-11-05 10:23:41 +0100 | [diff] [blame] | 537 | //DEBUG("get session_with_mutex for %s.", pss->session_id); |
| 538 | struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 539 | if (ls == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 540 | DEBUG("notification: session not found"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 541 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 542 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 543 | return -1; |
| 544 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 545 | return -1; |
| 546 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 547 | pthread_mutex_lock(&ls->lock); |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 548 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 549 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 550 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 551 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 552 | //DEBUG("check for closed session."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 553 | if (ls->closed == 1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 554 | DEBUG("unlock session key."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 555 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 556 | DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 557 | return -1; |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 558 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 559 | return -1; |
| 560 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 561 | //DEBUG("lock private lock."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 562 | notification_t *notif = NULL; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 563 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 564 | if (ls->notif_count) { |
| 565 | DEBUG("notification: POP notifications for session"); |
Michal Vasko | 43d94a6 | 2015-11-05 10:59:32 +0100 | [diff] [blame] | 566 | for (i = ls->notif_count; i; --i) { |
| 567 | notif = ls->notifications + i - 1; |
Tomas Cejka | 7328693 | 2013-05-27 22:54:35 +0200 | [diff] [blame] | 568 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 569 | n = 0; |
| 570 | pthread_mutex_lock(&json_lock); |
| 571 | json_object *notif_json = json_object_new_object(); |
| 572 | json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime)); |
| 573 | json_object_object_add(notif_json, "content", json_object_new_string(notif->content)); |
| 574 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 575 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 576 | const char *msgtext = json_object_to_json_string(notif_json); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 577 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 578 | //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t); |
| 579 | n = sprintf((char *)p, "%s", msgtext); |
| 580 | DEBUG("ws send %dB in %lu", n, sizeof(buf)); |
| 581 | m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT); |
| 582 | if (lws_send_pipe_choked(wsi)) { |
| 583 | libwebsocket_callback_on_writable(context, wsi); |
| 584 | break; |
| 585 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 586 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 587 | pthread_mutex_lock(&json_lock); |
| 588 | json_object_put(notif_json); |
| 589 | pthread_mutex_unlock(&json_lock); |
| 590 | free(notif->content); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 591 | } |
Michal Vasko | 43d94a6 | 2015-11-05 10:59:32 +0100 | [diff] [blame] | 592 | ls->notif_count = 0; |
| 593 | free(ls->notifications); |
| 594 | ls->notifications = NULL; |
| 595 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 596 | DEBUG("notification: POP notifications done"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 597 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 598 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 599 | //DEBUG("unlock private lock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 600 | if (pthread_mutex_unlock(&ls->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 601 | DEBUG("notification: cannot unlock session"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 602 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 603 | //DEBUG("unlock session lock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 604 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 605 | if (m < n) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 606 | DEBUG("ERROR %d writing to di socket.", n); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 607 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 608 | return -1; |
| 609 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 610 | break; |
| 611 | |
| 612 | case LWS_CALLBACK_RECEIVE: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 613 | DEBUG("Callback receive."); |
| 614 | DEBUG("received: (%s)", (char *)in); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 615 | if (pss->session_id == NULL) { |
Michal Vasko | 2bff5fb | 2015-11-05 10:23:02 +0100 | [diff] [blame] | 616 | char *sid_end; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 617 | int start = -1; |
| 618 | time_t stop = time(NULL) + 30; |
| 619 | |
Michal Vasko | 2bff5fb | 2015-11-05 10:23:02 +0100 | [diff] [blame] | 620 | sid_end = strchr(in, ' '); |
| 621 | pss->session_id = strndup(in, sid_end - (char *)in); |
| 622 | |
| 623 | ++sid_end; |
| 624 | sscanf(sid_end, "%d %d", (int *) &start, (int *) &stop); |
| 625 | DEBUG("notification: SID (%s) from (%s) (%i,%i)", pss->session_id, (char *) in, (int) start, (int) stop); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 626 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 627 | DEBUG("lock session lock"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 628 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 629 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 630 | return -1; |
| 631 | } |
Michal Vasko | 3d8681d | 2015-11-05 10:23:41 +0100 | [diff] [blame] | 632 | DEBUG("get session with ID (%s)", pss->session_id); |
| 633 | struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 634 | if (ls == NULL) { |
Michal Vasko | 3d8681d | 2015-11-05 10:23:41 +0100 | [diff] [blame] | 635 | DEBUG("notification: session_id not found (%s)", pss->session_id); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 636 | DEBUG("unlock session lock"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 637 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 638 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 639 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 640 | DEBUG("Close notification client"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 641 | return -1; |
| 642 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 643 | DEBUG("lock private lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 644 | pthread_mutex_lock(&ls->lock); |
| 645 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 646 | DEBUG("unlock session lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 647 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 648 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 651 | DEBUG("Found session to subscribe notif."); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 652 | if (ls->closed == 1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 653 | DEBUG("session already closed - handle no notification"); |
| 654 | DEBUG("unlock private lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 655 | pthread_mutex_unlock(&ls->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 656 | DEBUG("Close notification client"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 657 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 658 | } |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 659 | if (ls->ntfc_subscribed != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 660 | DEBUG("notification: already subscribed"); |
| 661 | DEBUG("unlock private lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 662 | pthread_mutex_unlock(&ls->lock); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 663 | /* do not close client, only do not subscribe again */ |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 664 | return 0; |
| 665 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 666 | DEBUG("notification: prepare to subscribe stream"); |
| 667 | DEBUG("unlock session lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 668 | pthread_mutex_unlock(&ls->lock); |
| 669 | |
| 670 | /* notif_subscribe locks on its own */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 671 | return notif_subscribe(ls, pss->session_id, (time_t) start, (time_t) stop); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 672 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 673 | if (len < 6) |
| 674 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 675 | break; |
| 676 | /* |
| 677 | * this just demonstrates how to use the protocol filter. If you won't |
| 678 | * study and reject connections based on header content, you don't need |
| 679 | * to handle this callback |
| 680 | */ |
| 681 | |
| 682 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
| 683 | //dump_handshake_info(wsi); |
| 684 | /* you could return non-zero here and kill the connection */ |
| 685 | break; |
Tomas Cejka | 866f228 | 2014-09-18 15:20:26 +0200 | [diff] [blame] | 686 | //gives segfault :-( |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 687 | //case LWS_CALLBACK_CLOSED: |
| 688 | // if (pss->session_key != NULL) { |
| 689 | // free(pss->session_key); |
| 690 | // } |
| 691 | // if (pss != NULL) { |
| 692 | // free(pss); |
| 693 | // } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 694 | |
| 695 | default: |
| 696 | break; |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | /* list of supported protocols and callbacks */ |
| 703 | |
| 704 | static struct libwebsocket_protocols protocols[] = { |
| 705 | /* first protocol must always be HTTP handler */ |
| 706 | |
| 707 | { |
| 708 | "http-only", /* name */ |
| 709 | callback_http, /* callback */ |
| 710 | sizeof (struct per_session_data__http), /* per_session_data_size */ |
| 711 | 0, /* max frame size / rx buffer */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 712 | 0, |
| 713 | NULL, |
| 714 | NULL, |
| 715 | 0 |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 716 | }, |
| 717 | { |
| 718 | "notification-protocol", |
| 719 | callback_notification, |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 720 | sizeof(struct per_session_data__notif_client), |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 721 | 4000, |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 722 | 0, |
| 723 | NULL, |
| 724 | NULL, |
| 725 | 0 |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 726 | }, |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 727 | { NULL, NULL, 0, 0, 0, NULL, NULL, 0 } /* terminator */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 728 | }; |
| 729 | |
| 730 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 731 | /** |
| 732 | * initialization of notification module |
| 733 | */ |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 734 | int notification_init(void) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 735 | { |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 736 | //char cert_path[1024]; |
| 737 | //char key_path[1024]; |
| 738 | //int use_ssl = 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 739 | struct lws_context_creation_info info; |
| 740 | int opts = 0; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 741 | //char interface_name[128] = ""; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 742 | const char *iface = NULL; |
| 743 | int debug_level = 7; |
| 744 | |
| 745 | memset(&info, 0, sizeof info); |
| 746 | info.port = NOTIFICATION_SERVER_PORT; |
| 747 | |
| 748 | /* tell the library what debug level to emit and to send it to syslog */ |
| 749 | lws_set_log_level(debug_level, lwsl_emit_syslog); |
| 750 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 751 | DEBUG("Initialization of libwebsocket"); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 752 | //lwsl_notice("libwebsockets test server - " |
| 753 | // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - " |
| 754 | // "licensed under LGPL2.1\n"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 755 | max_poll_elements = getdtablesize(); |
| 756 | pollfds = malloc(max_poll_elements * sizeof (struct pollfd)); |
| 757 | fd_lookup = malloc(max_poll_elements * sizeof (int)); |
| 758 | if (pollfds == NULL || fd_lookup == NULL) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 759 | ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 760 | return -1; |
| 761 | } |
| 762 | |
| 763 | info.iface = iface; |
| 764 | info.protocols = protocols; |
| 765 | |
| 766 | //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path); |
| 767 | //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path); |
| 768 | |
| 769 | //info.ssl_cert_filepath = cert_path; |
| 770 | //info.ssl_private_key_filepath = key_path; |
| 771 | |
| 772 | info.gid = -1; |
| 773 | info.uid = -1; |
| 774 | info.options = opts; |
| 775 | |
| 776 | /* create server */ |
| 777 | context = libwebsocket_create_context(&info); |
| 778 | if (context == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 779 | DEBUG("libwebsocket init failed."); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 780 | return -1; |
| 781 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 782 | |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 783 | if (pthread_key_create(&thread_key, NULL) != 0) { |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 784 | ERROR("notifications: pthread_key_create failed"); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 785 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 786 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 787 | } |
| 788 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 789 | void notification_close(void) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 790 | { |
Tomas Cejka | 98ed6cc | 2015-04-27 23:35:18 +0200 | [diff] [blame] | 791 | if (context) { |
| 792 | libwebsocket_context_destroy(context); |
| 793 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 794 | free(pollfds); |
| 795 | free(fd_lookup); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 796 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 797 | DEBUG("libwebsockets-test-server exited cleanly\n"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 800 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 801 | /** |
| 802 | * \brief send notification if any |
| 803 | * \return < 0 on error |
| 804 | */ |
| 805 | int notification_handle() |
| 806 | { |
| 807 | static struct timeval tv; |
| 808 | static unsigned int olds = 0; |
| 809 | int n = 0; |
| 810 | |
| 811 | gettimeofday(&tv, NULL); |
| 812 | |
| 813 | /* |
| 814 | * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every |
| 815 | * live websocket connection using the DUMB_INCREMENT protocol, |
| 816 | * as soon as it can take more packets (usually immediately) |
| 817 | */ |
| 818 | |
| 819 | if (((unsigned int)tv.tv_sec - olds) > 0) { |
| 820 | libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]); |
| 821 | olds = tv.tv_sec; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | /* |
| 826 | * this represents an existing server's single poll action |
| 827 | * which also includes libwebsocket sockets |
| 828 | */ |
| 829 | |
| 830 | n = poll(pollfds, count_pollfds, 50); |
| 831 | if (n < 0) |
| 832 | return n; |
| 833 | |
| 834 | |
| 835 | if (n) { |
| 836 | for (n = 0; n < count_pollfds; n++) { |
| 837 | if (pollfds[n].revents) { |
| 838 | /* |
| 839 | * returns immediately if the fd does not |
| 840 | * match anything under libwebsockets |
| 841 | * control |
| 842 | */ |
| 843 | if (libwebsocket_service_fd(context, &pollfds[n]) < 0) { |
| 844 | return 1; |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | #endif |
| 853 | |
| 854 | |
| 855 | #ifndef WITH_NOTIFICATIONS |
| 856 | #ifdef TEST_NOTIFICATION_SERVER |
| 857 | int main(int argc, char **argv) |
| 858 | { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 859 | if (notification_init(NULL, NULL) == -1) { |
| 860 | fprintf(stderr, "Error during initialization\n"); |
| 861 | return 1; |
| 862 | } |
| 863 | while (!force_exit) { |
| 864 | notification_handle(); |
| 865 | } |
| 866 | notification_close(); |
| 867 | } |
| 868 | #endif |
| 869 | #endif |