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