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; |
| 124 | unsigned char *p; |
| 125 | static unsigned char buffer[4096]; |
| 126 | struct stat stat_buf; |
| 127 | struct per_session_data__http *pss = (struct per_session_data__http *)user; |
| 128 | int fd = (int)(long)in; |
| 129 | |
| 130 | switch (reason) { |
| 131 | case LWS_CALLBACK_HTTP: |
| 132 | |
| 133 | /* check for the "send a big file by hand" example case */ |
| 134 | |
| 135 | if (!strcmp((const char *)in, "/leaf.jpg")) { |
| 136 | char leaf_path[1024]; |
| 137 | snprintf(leaf_path, sizeof(leaf_path), "%s/leaf.jpg", resource_path); |
| 138 | |
| 139 | /* well, let's demonstrate how to send the hard way */ |
| 140 | |
| 141 | p = buffer; |
| 142 | |
| 143 | pss->fd = open(leaf_path, O_RDONLY); |
| 144 | |
| 145 | if (pss->fd < 0) |
| 146 | return -1; |
| 147 | |
| 148 | fstat(pss->fd, &stat_buf); |
| 149 | |
| 150 | /* |
| 151 | * we will send a big jpeg file, but it could be |
| 152 | * anything. Set the Content-Type: appropriately |
| 153 | * so the browser knows what to do with it. |
| 154 | */ |
| 155 | |
| 156 | p += sprintf((char *)p, |
| 157 | "HTTP/1.0 200 OK\x0d\x0a" |
| 158 | "Server: libwebsockets\x0d\x0a" |
| 159 | "Content-Type: image/jpeg\x0d\x0a" |
| 160 | "Content-Length: %u\x0d\x0a\x0d\x0a", |
| 161 | (unsigned int)stat_buf.st_size); |
| 162 | |
| 163 | /* |
| 164 | * send the http headers... |
| 165 | * this won't block since it's the first payload sent |
| 166 | * on the connection since it was established |
| 167 | * (too small for partial) |
| 168 | */ |
| 169 | |
| 170 | n = libwebsocket_write(wsi, buffer, |
| 171 | p - buffer, LWS_WRITE_HTTP); |
| 172 | |
| 173 | if (n < 0) { |
| 174 | close(pss->fd); |
| 175 | return -1; |
| 176 | } |
| 177 | /* |
| 178 | * book us a LWS_CALLBACK_HTTP_WRITEABLE callback |
| 179 | */ |
| 180 | libwebsocket_callback_on_writable(context, wsi); |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | /* if not, send a file the easy way */ |
| 185 | |
| 186 | for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++) |
| 187 | if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0) |
| 188 | break; |
| 189 | |
| 190 | sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath); |
| 191 | |
| 192 | if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype)) |
| 193 | return -1; /* through completion or error, close the socket */ |
| 194 | |
| 195 | /* |
| 196 | * notice that the sending of the file completes asynchronously, |
| 197 | * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when |
| 198 | * it's done |
| 199 | */ |
| 200 | |
| 201 | break; |
| 202 | |
| 203 | case LWS_CALLBACK_HTTP_FILE_COMPLETION: |
| 204 | // lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n"); |
| 205 | /* kill the connection after we sent one file */ |
| 206 | return -1; |
| 207 | |
| 208 | case LWS_CALLBACK_HTTP_WRITEABLE: |
| 209 | /* |
| 210 | * we can send more of whatever it is we were sending |
| 211 | */ |
| 212 | |
| 213 | do { |
| 214 | n = read(pss->fd, buffer, sizeof buffer); |
| 215 | /* problem reading, close conn */ |
| 216 | if (n < 0) |
| 217 | goto bail; |
| 218 | /* sent it all, close conn */ |
| 219 | if (n == 0) |
| 220 | goto bail; |
| 221 | /* |
| 222 | * because it's HTTP and not websocket, don't need to take |
| 223 | * care about pre and postamble |
| 224 | */ |
| 225 | m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP); |
| 226 | if (m < 0) |
| 227 | /* write failed, close conn */ |
| 228 | goto bail; |
| 229 | if (m != n) |
| 230 | /* partial write, adjust */ |
| 231 | lseek(pss->fd, m - n, SEEK_CUR); |
| 232 | |
| 233 | } while (!lws_send_pipe_choked(wsi)); |
| 234 | libwebsocket_callback_on_writable(context, wsi); |
| 235 | break; |
| 236 | |
| 237 | bail: |
| 238 | close(pss->fd); |
| 239 | return -1; |
| 240 | |
| 241 | /* |
| 242 | * callback for confirming to continue with client IP appear in |
| 243 | * protocol 0 callback since no websocket protocol has been agreed |
| 244 | * yet. You can just ignore this if you won't filter on client IP |
| 245 | * since the default uhandled callback return is 0 meaning let the |
| 246 | * connection continue. |
| 247 | */ |
| 248 | |
| 249 | case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: |
| 250 | libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name, |
| 251 | sizeof(client_name), client_ip, sizeof(client_ip)); |
| 252 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 253 | //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] | 254 | /* if we returned non-zero from here, we kill the connection */ |
| 255 | break; |
| 256 | |
| 257 | /* |
| 258 | * callbacks for managing the external poll() array appear in |
| 259 | * protocol 0 callback |
| 260 | */ |
| 261 | |
| 262 | case LWS_CALLBACK_ADD_POLL_FD: |
| 263 | |
| 264 | if (count_pollfds >= max_poll_elements) { |
| 265 | lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n"); |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | fd_lookup[fd] = count_pollfds; |
| 270 | pollfds[count_pollfds].fd = fd; |
| 271 | pollfds[count_pollfds].events = (int)(long)len; |
| 272 | pollfds[count_pollfds++].revents = 0; |
| 273 | break; |
| 274 | |
| 275 | case LWS_CALLBACK_DEL_POLL_FD: |
| 276 | if (!--count_pollfds) |
| 277 | break; |
| 278 | m = fd_lookup[fd]; |
| 279 | /* have the last guy take up the vacant slot */ |
| 280 | pollfds[m] = pollfds[count_pollfds]; |
| 281 | fd_lookup[pollfds[count_pollfds].fd] = m; |
| 282 | break; |
| 283 | |
| 284 | case LWS_CALLBACK_SET_MODE_POLL_FD: |
| 285 | pollfds[fd_lookup[fd]].events |= (int)(long)len; |
| 286 | break; |
| 287 | |
| 288 | case LWS_CALLBACK_CLEAR_MODE_POLL_FD: |
| 289 | pollfds[fd_lookup[fd]].events &= ~(int)(long)len; |
| 290 | break; |
| 291 | |
| 292 | default: |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 299 | /** |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 300 | * this is just an example of parsing handshake headers, you don't need this |
| 301 | * in your code unless you will filter allowing connections by the header |
| 302 | * content |
| 303 | */ |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 304 | //static void dump_handshake_info(struct libwebsocket *wsi) |
| 305 | //{ |
| 306 | // int n; |
| 307 | // static const char *token_names[WSI_TOKEN_COUNT] = { |
| 308 | // /*[WSI_TOKEN_GET_URI] =*/ "GET URI", |
| 309 | // /*[WSI_TOKEN_HOST] =*/ "Host", |
| 310 | // /*[WSI_TOKEN_CONNECTION] =*/ "Connection", |
| 311 | // /*[WSI_TOKEN_KEY1] =*/ "key 1", |
| 312 | // /*[WSI_TOKEN_KEY2] =*/ "key 2", |
| 313 | // /*[WSI_TOKEN_PROTOCOL] =*/ "Protocol", |
| 314 | // /*[WSI_TOKEN_UPGRADE] =*/ "Upgrade", |
| 315 | // /*[WSI_TOKEN_ORIGIN] =*/ "Origin", |
| 316 | // /*[WSI_TOKEN_DRAFT] =*/ "Draft", |
| 317 | // /*[WSI_TOKEN_CHALLENGE] =*/ "Challenge", |
| 318 | // |
| 319 | // /* new for 04 */ |
| 320 | // /*[WSI_TOKEN_KEY] =*/ "Key", |
| 321 | // /*[WSI_TOKEN_VERSION] =*/ "Version", |
| 322 | // /*[WSI_TOKEN_SWORIGIN] =*/ "Sworigin", |
| 323 | // |
| 324 | // /* new for 05 */ |
| 325 | // /*[WSI_TOKEN_EXTENSIONS] =*/ "Extensions", |
| 326 | // |
| 327 | // /* client receives these */ |
| 328 | // /*[WSI_TOKEN_ACCEPT] =*/ "Accept", |
| 329 | // /*[WSI_TOKEN_NONCE] =*/ "Nonce", |
| 330 | // /*[WSI_TOKEN_HTTP] =*/ "Http", |
| 331 | // /*[WSI_TOKEN_MUXURL] =*/ "MuxURL", |
| 332 | // }; |
| 333 | // char buf[256]; |
| 334 | // |
| 335 | // for (n = 0; n < WSI_TOKEN_COUNT; n++) { |
| 336 | // if (!lws_hdr_total_length(wsi, n)) |
| 337 | // continue; |
| 338 | // |
| 339 | // //lws_hdr_copy(wsi, buf, sizeof buf, n); |
| 340 | // |
| 341 | // //fprintf(stderr, " %s = %s\n", token_names[n], buf); |
| 342 | // } |
| 343 | //} |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 344 | |
| 345 | /* dumb_increment protocol */ |
| 346 | |
| 347 | /* |
| 348 | * one of these is auto-created for each connection and a pointer to the |
| 349 | * appropriate instance is passed to the callback in the user parameter |
| 350 | * |
| 351 | * for this example protocol we use it to individualize the count for each |
| 352 | * connection. |
| 353 | */ |
| 354 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 355 | struct per_session_data__notif_client { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 356 | int number; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 357 | char *session_key; |
| 358 | struct nc_session *session; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 359 | }; |
| 360 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 361 | struct session_with_mutex *get_ncsession_from_key(const char *session_key) |
| 362 | { |
| 363 | struct session_with_mutex *locked_session = NULL; |
| 364 | if (session_key == NULL) { |
| 365 | return (NULL); |
| 366 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 367 | 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] | 368 | return locked_session; |
| 369 | } |
| 370 | |
| 371 | /* rpc parameter is freed after the function call */ |
| 372 | static int send_recv_process(struct nc_session *session, const char* operation, nc_rpc* rpc) |
| 373 | { |
| 374 | nc_reply *reply = NULL; |
| 375 | char *data = NULL; |
| 376 | int ret = EXIT_SUCCESS; |
| 377 | |
| 378 | /* send the request and get the reply */ |
| 379 | switch (nc_session_send_recv(session, rpc, &reply)) { |
| 380 | case NC_MSG_UNKNOWN: |
| 381 | if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 382 | DEBUG("notifications: receiving rpc-reply failed."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 383 | //cmd_disconnect(NULL); |
| 384 | ret = EXIT_FAILURE; |
| 385 | break; |
| 386 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 387 | DEBUG("notifications: Unknown error occurred."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 388 | ret = EXIT_FAILURE; |
| 389 | break; |
| 390 | case NC_MSG_NONE: |
| 391 | /* error occurred, but processed by callback */ |
| 392 | break; |
| 393 | case NC_MSG_REPLY: |
| 394 | switch (nc_reply_get_type(reply)) { |
| 395 | case NC_REPLY_OK: |
| 396 | break; |
| 397 | case NC_REPLY_DATA: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 398 | DEBUG("notifications: recv: %s.", data = nc_reply_get_data (reply)); |
| 399 | free(data); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 400 | break; |
| 401 | case NC_REPLY_ERROR: |
| 402 | /* wtf, you shouldn't be here !?!? */ |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 403 | DEBUG("notifications: operation failed, but rpc-error was not processed."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 404 | ret = EXIT_FAILURE; |
| 405 | break; |
| 406 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 407 | DEBUG("notifications: unexpected operation result."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 408 | ret = EXIT_FAILURE; |
| 409 | break; |
| 410 | } |
| 411 | break; |
| 412 | default: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 413 | DEBUG("notifications: Unknown error occurred."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 414 | ret = EXIT_FAILURE; |
| 415 | break; |
| 416 | } |
| 417 | nc_rpc_free(rpc); |
| 418 | nc_reply_free(reply); |
| 419 | |
| 420 | return (ret); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * \brief Callback to store incoming notification |
| 425 | * \param [in] eventtime - time when notification occured |
| 426 | * \param [in] content - content of notification |
| 427 | */ |
| 428 | static void notification_fileprint (time_t eventtime, const char* content) |
| 429 | { |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 430 | struct session_with_mutex *target_session = NULL; |
| 431 | notification_t *ntf = NULL; |
| 432 | char *session_hash = NULL; |
| 433 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 434 | DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 435 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 436 | session_hash = pthread_getspecific(thread_key); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 437 | DEBUG("notification: fileprint getspecific (%s)", session_hash); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 438 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 439 | DEBUG("Error while locking rwlock"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 440 | return; |
| 441 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 442 | DEBUG("Get session with mutex from key %s.", session_hash); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 443 | target_session = get_ncsession_from_key(session_hash); |
| 444 | if (target_session == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 445 | DEBUG("no session found last_session_key (%s)", session_hash); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 446 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 447 | DEBUG("Error while unlocking rwlock"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 448 | return; |
| 449 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 450 | return; |
| 451 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 452 | if (pthread_mutex_lock(&target_session->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 453 | DEBUG("Error while locking rwlock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 454 | } |
| 455 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 456 | DEBUG("Error while locking rwlock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 457 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 458 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 459 | if (target_session->notifications == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 460 | DEBUG("target_session->notifications is NULL"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 461 | if (pthread_mutex_unlock(&target_session->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 462 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | return; |
| 466 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 467 | DEBUG("notification: ready to push to notifications queue"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 468 | ntf = (notification_t *) apr_array_push(target_session->notifications); |
| 469 | if (ntf == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 470 | DEBUG("Failed to allocate element "); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 471 | if (pthread_mutex_unlock(&target_session->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 472 | DEBUG("Error while unlocking rwlock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | return; |
| 476 | } |
Tomas Cejka | 7328693 | 2013-05-27 22:54:35 +0200 | [diff] [blame] | 477 | ntf->eventtime = eventtime; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 478 | ntf->content = strdup(content); |
| 479 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 480 | DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 481 | |
| 482 | if (pthread_mutex_unlock(&target_session->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 483 | DEBUG("Error while unlocking rwlock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * \brief Thread for libnetconf notifications dispatch |
| 489 | * \param [in] arg - struct ntf_thread_config * with nc_session |
| 490 | */ |
| 491 | void* notification_thread(void* arg) |
| 492 | { |
| 493 | struct ntf_thread_config *config = (struct ntf_thread_config*)arg; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 494 | DEBUG("notifications: in thread for libnetconf notifications"); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 495 | |
| 496 | /* store hash identification of netconf session for notifications printing callback */ |
| 497 | if (pthread_setspecific(thread_key, config->session_hash) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 498 | DEBUG("notifications: cannot set thread-specific hash value."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 499 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 500 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 501 | DEBUG("notifications: dispatching"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 502 | ncntf_dispatch_receive(config->session, notification_fileprint); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 503 | DEBUG("notifications: ended thread for libnetconf notifications"); |
| 504 | if (config->session_hash != NULL) { |
| 505 | free(config->session_hash); |
| 506 | } |
| 507 | if (config != NULL) { |
| 508 | free(config); |
| 509 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 510 | return (NULL); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | int notif_subscribe(struct session_with_mutex *locked_session, const char *session_hash, time_t start_time, time_t stop_time) |
| 515 | { |
| 516 | time_t start = -1; |
| 517 | time_t stop = -1; |
| 518 | struct nc_filter *filter = NULL; |
| 519 | char *stream = NULL; |
| 520 | nc_rpc *rpc = NULL; |
| 521 | pthread_t thread; |
| 522 | struct ntf_thread_config *tconfig; |
| 523 | struct nc_session *session; |
| 524 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 525 | DEBUG("notif_subscribe"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 526 | if (locked_session == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 527 | DEBUG("notifications: no locked_session was given."); |
| 528 | DEBUG("Close notification client"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 529 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 532 | pthread_mutex_lock(&locked_session->lock); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 533 | session = locked_session->session; |
| 534 | |
| 535 | start = time(NULL) + start_time; |
| 536 | stop = time(NULL) + stop_time; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 537 | DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 538 | |
| 539 | if (session == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 540 | DEBUG("notifications: NETCONF session not established."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 541 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* check if notifications are allowed on this session */ |
| 545 | if (nc_session_notif_allowed(session) == 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 546 | DEBUG("notifications: Notification subscription is not allowed on this session."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 547 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 548 | } |
| 549 | /* check times */ |
| 550 | if (start != -1 && stop != -1 && start > stop) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 551 | DEBUG("notifications: Subscription start time must be lower than the end time."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 552 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 555 | DEBUG("Prepare to execute subscription."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 556 | /* create requests */ |
| 557 | rpc = nc_rpc_subscribe(stream, filter, (start_time == 0)?NULL:&start, (stop_time == 0)?NULL:&stop); |
| 558 | nc_filter_free(filter); |
| 559 | if (rpc == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 560 | DEBUG("notifications: creating an rpc request failed."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 561 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 564 | DEBUG("Send NC subscribe."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 565 | /** \todo replace with sth like netconf_op(http_server, session_hash, rpc) */ |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 566 | if (send_recv_process(session, "subscribe", rpc) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 567 | DEBUG("Subscription RPC failed."); |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 568 | goto operation_failed; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 569 | } |
| 570 | 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] | 571 | locked_session->ntfc_subscribed = 1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 572 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 573 | DEBUG("Create config for notification_thread."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 574 | tconfig = malloc(sizeof(struct ntf_thread_config)); |
| 575 | tconfig->session = session; |
| 576 | tconfig->session_hash = strdup(session_hash); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 577 | DEBUG("notifications: creating libnetconf notification thread (%s).", tconfig->session_hash); |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 578 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 579 | pthread_mutex_unlock(&locked_session->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 580 | DEBUG("Create notification_thread."); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 581 | if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 582 | DEBUG("notifications: creating a thread for receiving notifications failed"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 583 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 584 | } |
| 585 | pthread_detach(thread); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 586 | DEBUG("Subscription finished."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 587 | return 0; |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 588 | |
| 589 | operation_failed: |
| 590 | pthread_mutex_unlock(&locked_session->lock); |
| 591 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 592 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 593 | |
| 594 | static int callback_notification(struct libwebsocket_context *context, |
| 595 | struct libwebsocket *wsi, |
| 596 | enum libwebsocket_callback_reasons reason, |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 597 | void *user, void *in, size_t len) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 598 | { |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 599 | int n = 0; |
| 600 | int m = 0; |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 601 | 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] | 602 | unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 603 | struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user; |
| 604 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 605 | switch (reason) { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 606 | case LWS_CALLBACK_ESTABLISHED: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 607 | DEBUG("notification client connected."); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 608 | break; |
| 609 | |
| 610 | case LWS_CALLBACK_SERVER_WRITEABLE: |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 611 | if (pss->session_key == NULL) { |
| 612 | return 0; |
| 613 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 614 | //DEBUG("Callback server writeable."); |
| 615 | //DEBUG("lock session lock."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 616 | if (pthread_rwlock_wrlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 617 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 618 | return -1; |
| 619 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 620 | //DEBUG("get session_with_mutex for %s.", pss->session_key); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 621 | struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key); |
| 622 | if (ls == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 623 | DEBUG("notification: session not found"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 624 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 625 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 626 | return -1; |
| 627 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 628 | return -1; |
| 629 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 630 | pthread_mutex_lock(&ls->lock); |
| 631 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 632 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 633 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 634 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 635 | //DEBUG("check for closed session."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 636 | if (ls->closed == 1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 637 | DEBUG("unlock session key."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 638 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 639 | DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 640 | return -1; |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 641 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 642 | return -1; |
| 643 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 644 | //DEBUG("lock private lock."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 645 | notification_t *notif = NULL; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 646 | //DEBUG("check for uninitialized notification list."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 647 | if (ls->notifications == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 648 | DEBUG("notification: no notifications array"); |
| 649 | DEBUG("unlock private lock."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 650 | if (pthread_mutex_unlock(&ls->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 651 | DEBUG("notification: cannot unlock session"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 652 | } |
| 653 | return -1; |
| 654 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 655 | //DEBUG("check for empty notification list."); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 656 | if (!apr_is_empty_array(ls->notifications)) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 657 | DEBUG("notification: POP notifications for session"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 658 | |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 659 | while ((notif = (notification_t *) apr_array_pop(ls->notifications)) != NULL) { |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 660 | n = 0; |
Tomas Cejka | 7328693 | 2013-05-27 22:54:35 +0200 | [diff] [blame] | 661 | json_object *notif_json = json_object_new_object(); |
Tomas Cejka | 8800d65 | 2013-07-16 10:47:08 +0200 | [diff] [blame] | 662 | 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] | 663 | json_object_object_add(notif_json, "content", json_object_new_string(notif->content)); |
| 664 | |
Tomas Cejka | 0063597 | 2013-06-03 15:10:52 +0200 | [diff] [blame] | 665 | const char *msgtext = json_object_to_json_string(notif_json); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 666 | |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 667 | //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 668 | n = sprintf((char *)p, "%s", msgtext); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 669 | DEBUG("ws send %dB in %lu", n, sizeof(buf)); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 670 | m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 671 | if (lws_send_pipe_choked(wsi)) { |
| 672 | libwebsocket_callback_on_writable(context, wsi); |
| 673 | break; |
| 674 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 675 | |
Tomas Cejka | 8a82dab | 2013-05-30 23:37:23 +0200 | [diff] [blame] | 676 | json_object_put(notif_json); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 677 | free(notif->content); |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 678 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 679 | DEBUG("notification: POP notifications done"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 682 | //DEBUG("unlock private lock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 683 | if (pthread_mutex_unlock(&ls->lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 684 | DEBUG("notification: cannot unlock session"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 685 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 686 | //DEBUG("unlock session lock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 687 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 688 | if (m < n) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 689 | DEBUG("ERROR %d writing to di socket.", n); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 690 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 691 | return -1; |
| 692 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 693 | break; |
| 694 | |
| 695 | case LWS_CALLBACK_RECEIVE: |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 696 | DEBUG("Callback receive."); |
| 697 | DEBUG("received: (%s)", (char *)in); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 698 | if (pss->session_key == NULL) { |
| 699 | char session_key_buf[41]; |
| 700 | int start = -1; |
| 701 | time_t stop = time(NULL) + 30; |
| 702 | |
| 703 | strncpy((char *) session_key_buf, (const char *) in, 40); |
| 704 | session_key_buf[40] = '\0'; |
| 705 | pss->session_key = strdup(session_key_buf); |
| 706 | sscanf(in+40, "%d %d", (int *) &start, (int *) &stop); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 707 | 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] | 708 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 709 | DEBUG("lock session lock"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 710 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 711 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 712 | return -1; |
| 713 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 714 | DEBUG("get session from key (%s)", pss->session_key); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 715 | struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key); |
| 716 | if (ls == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 717 | DEBUG("notification: session_key not found (%s)", pss->session_key); |
| 718 | DEBUG("unlock session lock"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 719 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 720 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 721 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 722 | DEBUG("Close notification client"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 723 | return -1; |
| 724 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 725 | DEBUG("lock private lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 726 | pthread_mutex_lock(&ls->lock); |
| 727 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 728 | DEBUG("unlock session lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 729 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 730 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 733 | DEBUG("Found session to subscribe notif."); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 734 | if (ls->closed == 1) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 735 | DEBUG("session already closed - handle no notification"); |
| 736 | DEBUG("unlock private lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 737 | pthread_mutex_unlock(&ls->lock); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 738 | DEBUG("Close notification client"); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 739 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 740 | } |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 741 | if (ls->ntfc_subscribed != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 742 | DEBUG("notification: already subscribed"); |
| 743 | DEBUG("unlock private lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 744 | pthread_mutex_unlock(&ls->lock); |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 745 | /* do not close client, only do not subscribe again */ |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 746 | return 0; |
| 747 | } |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 748 | DEBUG("notification: prepare to subscribe stream"); |
| 749 | DEBUG("unlock session lock"); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 750 | pthread_mutex_unlock(&ls->lock); |
| 751 | |
| 752 | /* notif_subscribe locks on its own */ |
Tomas Cejka | bdedcd3 | 2013-06-09 11:54:53 +0200 | [diff] [blame] | 753 | 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] | 754 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 755 | if (len < 6) |
| 756 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 757 | break; |
| 758 | /* |
| 759 | * this just demonstrates how to use the protocol filter. If you won't |
| 760 | * study and reject connections based on header content, you don't need |
| 761 | * to handle this callback |
| 762 | */ |
| 763 | |
| 764 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
| 765 | //dump_handshake_info(wsi); |
| 766 | /* you could return non-zero here and kill the connection */ |
| 767 | break; |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 768 | //gives segfailt :-( |
| 769 | //case LWS_CALLBACK_CLOSED: |
| 770 | // if (pss->session_key != NULL) { |
| 771 | // free(pss->session_key); |
| 772 | // } |
| 773 | // if (pss != NULL) { |
| 774 | // free(pss); |
| 775 | // } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 776 | |
| 777 | default: |
| 778 | break; |
| 779 | } |
| 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /* list of supported protocols and callbacks */ |
| 785 | |
| 786 | static struct libwebsocket_protocols protocols[] = { |
| 787 | /* first protocol must always be HTTP handler */ |
| 788 | |
| 789 | { |
| 790 | "http-only", /* name */ |
| 791 | callback_http, /* callback */ |
| 792 | sizeof (struct per_session_data__http), /* per_session_data_size */ |
| 793 | 0, /* max frame size / rx buffer */ |
| 794 | }, |
| 795 | { |
| 796 | "notification-protocol", |
| 797 | callback_notification, |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 798 | sizeof(struct per_session_data__notif_client), |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 799 | 4000, |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 800 | }, |
| 801 | { NULL, NULL, 0, 0 } /* terminator */ |
| 802 | }; |
| 803 | |
| 804 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 805 | /** |
| 806 | * initialization of notification module |
| 807 | */ |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 808 | int notification_init(apr_pool_t * pool, server_rec * server) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 809 | { |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 810 | //char cert_path[1024]; |
| 811 | //char key_path[1024]; |
| 812 | //int use_ssl = 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 813 | struct lws_context_creation_info info; |
| 814 | int opts = 0; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 815 | //char interface_name[128] = ""; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 816 | const char *iface = NULL; |
| 817 | int debug_level = 7; |
| 818 | |
| 819 | memset(&info, 0, sizeof info); |
| 820 | info.port = NOTIFICATION_SERVER_PORT; |
| 821 | |
| 822 | /* tell the library what debug level to emit and to send it to syslog */ |
| 823 | lws_set_log_level(debug_level, lwsl_emit_syslog); |
| 824 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 825 | DEBUG("Initialization of libwebsocket"); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 826 | //lwsl_notice("libwebsockets test server - " |
| 827 | // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - " |
| 828 | // "licensed under LGPL2.1\n"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 829 | max_poll_elements = getdtablesize(); |
| 830 | pollfds = malloc(max_poll_elements * sizeof (struct pollfd)); |
| 831 | fd_lookup = malloc(max_poll_elements * sizeof (int)); |
| 832 | if (pollfds == NULL || fd_lookup == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 833 | DEBUG("Out of memory pollfds=%d\n", max_poll_elements); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 834 | return -1; |
| 835 | } |
| 836 | |
| 837 | info.iface = iface; |
| 838 | info.protocols = protocols; |
| 839 | |
| 840 | //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path); |
| 841 | //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path); |
| 842 | |
| 843 | //info.ssl_cert_filepath = cert_path; |
| 844 | //info.ssl_private_key_filepath = key_path; |
| 845 | |
| 846 | info.gid = -1; |
| 847 | info.uid = -1; |
| 848 | info.options = opts; |
| 849 | |
| 850 | /* create server */ |
| 851 | context = libwebsocket_create_context(&info); |
| 852 | if (context == NULL) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 853 | DEBUG("libwebsocket init failed."); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 854 | return -1; |
| 855 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 856 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 857 | DEBUG("notifications: init of pthread_key_create."); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 858 | if (pthread_key_create(&thread_key, NULL) != 0) { |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 859 | DEBUG("notifications: pthread_key_create failed"); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 860 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 861 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | void notification_close() |
| 865 | { |
| 866 | libwebsocket_context_destroy(context); |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 867 | free(pollfds); |
| 868 | free(fd_lookup); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 869 | |
Tomas Cejka | ef531ee | 2013-11-12 16:07:00 +0100 | [diff] [blame] | 870 | DEBUG("libwebsockets-test-server exited cleanly\n"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 871 | } |
| 872 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 873 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 874 | /** |
| 875 | * \brief send notification if any |
| 876 | * \return < 0 on error |
| 877 | */ |
| 878 | int notification_handle() |
| 879 | { |
| 880 | static struct timeval tv; |
| 881 | static unsigned int olds = 0; |
| 882 | int n = 0; |
| 883 | |
| 884 | gettimeofday(&tv, NULL); |
| 885 | |
| 886 | /* |
| 887 | * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every |
| 888 | * live websocket connection using the DUMB_INCREMENT protocol, |
| 889 | * as soon as it can take more packets (usually immediately) |
| 890 | */ |
| 891 | |
| 892 | if (((unsigned int)tv.tv_sec - olds) > 0) { |
| 893 | libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]); |
| 894 | olds = tv.tv_sec; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | /* |
| 899 | * this represents an existing server's single poll action |
| 900 | * which also includes libwebsocket sockets |
| 901 | */ |
| 902 | |
| 903 | n = poll(pollfds, count_pollfds, 50); |
| 904 | if (n < 0) |
| 905 | return n; |
| 906 | |
| 907 | |
| 908 | if (n) { |
| 909 | for (n = 0; n < count_pollfds; n++) { |
| 910 | if (pollfds[n].revents) { |
| 911 | /* |
| 912 | * returns immediately if the fd does not |
| 913 | * match anything under libwebsockets |
| 914 | * control |
| 915 | */ |
| 916 | if (libwebsocket_service_fd(context, &pollfds[n]) < 0) { |
| 917 | return 1; |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | #endif |
| 926 | |
| 927 | |
| 928 | #ifndef WITH_NOTIFICATIONS |
| 929 | #ifdef TEST_NOTIFICATION_SERVER |
| 930 | int main(int argc, char **argv) |
| 931 | { |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 932 | if (notification_init(NULL, NULL) == -1) { |
| 933 | fprintf(stderr, "Error during initialization\n"); |
| 934 | return 1; |
| 935 | } |
| 936 | while (!force_exit) { |
| 937 | notification_handle(); |
| 938 | } |
| 939 | notification_close(); |
| 940 | } |
| 941 | #endif |
| 942 | #endif |