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