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