blob: 5701403760514d8181eb4d023259d9c6ec7a9a57 [file] [log] [blame]
Tomas Cejkad340dbf2013-03-24 20:36:57 +01001/*
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 Cejkaba21b382013-04-13 02:37:32 +020028#include <sys/queue.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010029#include <fcntl.h>
30#include <assert.h>
Tomas Cejkaba21b382013-04-13 02:37:32 +020031#include <pthread.h>
32#include <libnetconf.h>
33#include <libwebsockets.h>
Tomas Cejkad340dbf2013-03-24 20:36:57 +010034#include "notification_module.h"
Tomas Cejkaba21b382013-04-13 02:37:32 +020035#include "mod_netconf.h"
Tomas Cejkad340dbf2013-03-24 20:36:57 +010036
37#ifndef TEST_NOTIFICATION_SERVER
38#include <httpd.h>
39#include <http_log.h>
Tomas Cejkaba21b382013-04-13 02:37:32 +020040#include <apr_hash.h>
41#include <apr_tables.h>
42
43#else
44static int force_exit = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010045#endif
46
47#if defined(TEST_NOTIFICATION_SERVER) || defined(WITH_NOTIFICATIONS)
Tomas Cejkad340dbf2013-03-24 20:36:57 +010048static int max_poll_elements;
49
50static struct pollfd *pollfds;
51static int *fd_lookup;
52static int count_pollfds;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010053static struct libwebsocket_context *context = NULL;
54static server_rec *http_server = NULL;
55
Tomas Cejkaba21b382013-04-13 02:37:32 +020056struct ntf_thread_config {
57 struct nc_session *session;
58 char *session_hash;
59};
60
Tomas Cejka47387fd2013-06-10 20:37:46 +020061extern apr_hash_t *netconf_sessions_list;
Tomas Cejkaba21b382013-04-13 02:37:32 +020062static pthread_key_t thread_key;
63
Tomas Cejkad340dbf2013-03-24 20:36:57 +010064/*
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
79enum 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 "."
91char *resource_path = LOCAL_RESOURCE_PATH;
92
93/*
94 * We take a strict whitelist approach to stop ../ attacks
95 */
96
97struct serveable {
98 const char *urlpath;
99 const char *mimetype;
Tomas Cejka15c56302013-05-30 01:11:30 +0200100};
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100101
102static 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
110struct per_session_data__http {
111 int fd;
112};
113
114/* this protocol server (always the first one) just knows how to do HTTP */
115
116static 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
238bail:
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 Cejkaba21b382013-04-13 02:37:32 +0200254 //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100255 /* 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 Cejkaba21b382013-04-13 02:37:32 +0200300/**
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100301 * 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 Cejkaba21b382013-04-13 02:37:32 +0200305//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 Cejkad340dbf2013-03-24 20:36:57 +0100345
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 Cejkaba21b382013-04-13 02:37:32 +0200356struct per_session_data__notif_client {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100357 int number;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200358 char *session_key;
359 struct nc_session *session;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100360};
361
Tomas Cejkaba21b382013-04-13 02:37:32 +0200362struct 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 Cejka47387fd2013-06-10 20:37:46 +0200368 locked_session = (struct session_with_mutex *)apr_hash_get(netconf_sessions_list, session_key, APR_HASH_KEY_STRING);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200369 return locked_session;
370}
371
372/* rpc parameter is freed after the function call */
373static 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 */
453static void notification_fileprint (time_t eventtime, const char* content)
454{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200455 struct session_with_mutex *target_session = NULL;
456 notification_t *ntf = NULL;
457 char *session_hash = NULL;
458
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200459 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Accepted notif: %lu %s\n", (unsigned long int) eventtime, content);
Tomas Cejka15c56302013-05-30 01:11:30 +0200460
Tomas Cejkaba21b382013-04-13 02:37:32 +0200461 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 Cejkabdedcd32013-06-09 11:54:53 +0200465 if (pthread_rwlock_wrlock(&session_lock) != 0) {
466 #ifndef TEST_NOTIFICATION_SERVER
Tomas Cejka47387fd2013-06-10 20:37:46 +0200467 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while locking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200468 #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 Cejkaba21b382013-04-13 02:37:32 +0200472 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 Cejkabdedcd32013-06-09 11:54:53 +0200475 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200476 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200477 return;
478 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200479 return;
480 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200481 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 Cejka15c56302013-05-30 01:11:30 +0200487
Tomas Cejkaba21b382013-04-13 02:37:32 +0200488 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 Cejka47387fd2013-06-10 20:37:46 +0200491 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkaba21b382013-04-13 02:37:32 +0200492 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 Cejka47387fd2013-06-10 20:37:46 +0200503 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200504 return;
505 }
506 return;
507 }
Tomas Cejka73286932013-05-27 22:54:35 +0200508 ntf->eventtime = eventtime;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200509 ntf->content = strdup(content);
510
Tomas Cejka47387fd2013-06-10 20:37:46 +0200511 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notifikace");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200512
513 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200514 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200515 }
516}
517
518/**
519 * \brief Thread for libnetconf notifications dispatch
520 * \param [in] arg - struct ntf_thread_config * with nc_session
521 */
522void* notification_thread(void* arg)
523{
524 struct ntf_thread_config *config = (struct ntf_thread_config*)arg;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200525 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "notifications: in thread for libnetconf notifications");
Tomas Cejka15c56302013-05-30 01:11:30 +0200526
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 Cejkaba21b382013-04-13 02:37:32 +0200530 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200531
Tomas Cejkaba21b382013-04-13 02:37:32 +0200532 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 Cejka47387fd2013-06-10 20:37:46 +0200536 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "notifications: ended thread for libnetconf notifications");
537 //free(config);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200538 return (NULL);
539}
540
541
542int 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 Cejka47387fd2013-06-10 20:37:46 +0200553 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notif_subscribe");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200554 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 Cejka47387fd2013-06-10 20:37:46 +0200558 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
559 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200560 }
561
Tomas Cejka47387fd2013-06-10 20:37:46 +0200562 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200563 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 Cejka47387fd2013-06-10 20:37:46 +0200571 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: NETCONF session not established.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200572 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200573 }
574
575 /* check if notifications are allowed on this session */
576 if (nc_session_notif_allowed(session) == 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200577 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Notification subscription is not allowed on this session.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200578 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200579 }
580 /* check times */
581 if (start != -1 && stop != -1 && start > stop) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200582 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: Subscription start time must be lower than the end time.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200583 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200584 }
585
Tomas Cejka47387fd2013-06-10 20:37:46 +0200586 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Prepare to execute subscription.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200587 /* 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 Cejka47387fd2013-06-10 20:37:46 +0200591 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating an rpc request failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200592 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200593 }
594
Tomas Cejka47387fd2013-06-10 20:37:46 +0200595 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 Cejkaba21b382013-04-13 02:37:32 +0200597 if (send_recv_process(session, "subscribe", rpc) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200598 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Subscription RPC failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200599 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200600 }
601 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200602 locked_session->ntfc_subscribed = 1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200603
Tomas Cejka47387fd2013-06-10 20:37:46 +0200604 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Create config for notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200605 tconfig = malloc(sizeof(struct ntf_thread_config));
606 tconfig->session = session;
607 tconfig->session_hash = strdup(session_hash);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200608 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating libnetconf notification thread (%s).", tconfig->session_hash);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200609
Tomas Cejka47387fd2013-06-10 20:37:46 +0200610 pthread_mutex_unlock(&locked_session->lock);
611 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Create notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200612 if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200613 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: creating a thread for receiving notifications failed");
614 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200615 }
616 pthread_detach(thread);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200617 ap_log_error (APLOG_MARK, APLOG_DEBUG, 0, http_server, "Subscription finished.");
618 return 0;
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200619
620operation_failed:
621 pthread_mutex_unlock(&locked_session->lock);
622 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200623}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100624
625static int callback_notification(struct libwebsocket_context *context,
626 struct libwebsocket *wsi,
627 enum libwebsocket_callback_reasons reason,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200628 void *user, void *in, size_t len)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100629{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200630 int n = 0;
631 int m = 0;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200632 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100633 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200634 struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user;
635
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100636 switch (reason) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100637 case LWS_CALLBACK_ESTABLISHED:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200638 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification client connected.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100639 break;
640
641 case LWS_CALLBACK_SERVER_WRITEABLE:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200642 if (pss->session_key == NULL) {
643 return 0;
644 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200645 //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 Cejkaba21b382013-04-13 02:37:32 +0200652 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 Cejka47387fd2013-06-10 20:37:46 +0200657 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 Cejkaba21b382013-04-13 02:37:32 +0200661 return -1;
662 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200663 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 Cejkaba21b382013-04-13 02:37:32 +0200666 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200667
Tomas Cejka47387fd2013-06-10 20:37:46 +0200668 //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 Cejka654f84e2013-04-19 11:55:01 +0200674 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200675 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 Cejkaba21b382013-04-13 02:37:32 +0200691
Tomas Cejka654f84e2013-04-19 11:55:01 +0200692 while ((notif = (notification_t *) apr_array_pop(ls->notifications)) != NULL) {
Tomas Cejka654f84e2013-04-19 11:55:01 +0200693 n = 0;
Tomas Cejka73286932013-05-27 22:54:35 +0200694 json_object *notif_json = json_object_new_object();
Tomas Cejka8800d652013-07-16 10:47:08 +0200695 json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime));
Tomas Cejka73286932013-05-27 22:54:35 +0200696 json_object_object_add(notif_json, "content", json_object_new_string(notif->content));
697
Tomas Cejka00635972013-06-03 15:10:52 +0200698 const char *msgtext = json_object_to_json_string(notif_json);
Tomas Cejka15c56302013-05-30 01:11:30 +0200699
Tomas Cejka47387fd2013-06-10 20:37:46 +0200700 //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t);
Tomas Cejka15c56302013-05-30 01:11:30 +0200701 n = sprintf((char *)p, "%s", msgtext);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200702 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "ws send %dB in %lu", n, sizeof(buf));
Tomas Cejka15c56302013-05-30 01:11:30 +0200703 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200704 if (lws_send_pipe_choked(wsi)) {
705 libwebsocket_callback_on_writable(context, wsi);
706 break;
707 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200708
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200709 json_object_put(notif_json);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200710 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200711 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: POP notifications done");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200712 }
713
Tomas Cejka47387fd2013-06-10 20:37:46 +0200714 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200715 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200716 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200717 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200718 //ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200719
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100720 if (m < n) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200721 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200722
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100723 return -1;
724 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100725 break;
726
727 case LWS_CALLBACK_RECEIVE:
Tomas Cejka47387fd2013-06-10 20:37:46 +0200728 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 Cejkaba21b382013-04-13 02:37:32 +0200730 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 Cejkabdedcd32013-06-09 11:54:53 +0200739 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 Cejka15c56302013-05-30 01:11:30 +0200740
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200741 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 Cejkaba21b382013-04-13 02:37:32 +0200747 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
748 if (ls == NULL) {
Tomas Cejka47387fd2013-06-10 20:37:46 +0200749 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notification: session_key not found (%s)", pss->session_key);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200750 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 Cejkabdedcd32013-06-09 11:54:53 +0200753 }
754 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
755 return -1;
756 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200757 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 Cejkabdedcd32013-06-09 11:54:53 +0200765 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 Cejka47387fd2013-06-10 20:37:46 +0200768 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
769 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200770 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "Close notification client");
771 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200772 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200773 if (ls->ntfc_subscribed != 0) {
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200774 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "notification: already subscribed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200775 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "unlock private lock");
776 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200777 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200778 return 0;
779 }
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200780 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 Cejka47387fd2013-06-10 20:37:46 +0200782 pthread_mutex_unlock(&ls->lock);
783
784 /* notif_subscribe locks on its own */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200785 return notif_subscribe(ls, pss->session_key, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200786 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100787 if (len < 6)
788 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100789 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
810static 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 Cejkaba21b382013-04-13 02:37:32 +0200822 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200823 4000,
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100824 },
825 { NULL, NULL, 0, 0 } /* terminator */
826};
827
828
Tomas Cejkaba21b382013-04-13 02:37:32 +0200829/**
830 * initialization of notification module
831 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200832int notification_init(apr_pool_t * pool, server_rec * server)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100833{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200834 //char cert_path[1024];
835 //char key_path[1024];
836 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100837 struct lws_context_creation_info info;
838 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200839 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100840 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 Cejka47387fd2013-06-10 20:37:46 +0200849 http_server = server;
850 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200851 //lwsl_notice("libwebsockets test server - "
852 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
853 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100854 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 Cejka15c56302013-05-30 01:11:30 +0200858 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100859 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 Cejka15c56302013-05-30 01:11:30 +0200878 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, http_server, "libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100879 return -1;
880 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200881
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 Cejka47387fd2013-06-10 20:37:46 +0200884 ap_log_error(APLOG_MARK, APLOG_ERR, 0, http_server, "notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200885 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200886 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100887}
888
889void notification_close()
890{
891 libwebsocket_context_destroy(context);
892
Tomas Cejka15c56302013-05-30 01:11:30 +0200893 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, http_server, "libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100894}
895
Tomas Cejkaba21b382013-04-13 02:37:32 +0200896
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100897/**
898 * \brief send notification if any
899 * \return < 0 on error
900 */
901int 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
953int main(int argc, char **argv)
954{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100955 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