blob: ba039b23b37328f1d605f888c56742cca2a6699a [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;
Tomas Cejkad340dbf2013-03-24 20:36:57 +010054
Tomas Cejkaba21b382013-04-13 02:37:32 +020055struct ntf_thread_config {
56 struct nc_session *session;
57 char *session_hash;
58};
59
Tomas Cejka47387fd2013-06-10 20:37:46 +020060extern apr_hash_t *netconf_sessions_list;
Tomas Cejkaba21b382013-04-13 02:37:32 +020061static pthread_key_t thread_key;
62
Tomas Cejkad340dbf2013-03-24 20:36:57 +010063/*
64 * This demo server shows how to use libwebsockets for one or more
65 * websocket protocols in the same server
66 *
67 * It defines the following websocket protocols:
68 *
69 * dumb-increment-protocol: once the socket is opened, an incrementing
70 * ascii string is sent down it every 50ms.
71 * If you send "reset\n" on the websocket, then
72 * the incrementing number is reset to 0.
73 *
74 * lws-mirror-protocol: copies any received packet to every connection also
75 * using this protocol, including the sender
76 */
77
78enum demo_protocols {
79 /* always first */
80 PROTOCOL_HTTP = 0,
81
82 PROTOCOL_NOTIFICATION,
83
84 /* always last */
85 DEMO_PROTOCOL_COUNT
86};
87
88
89#define LOCAL_RESOURCE_PATH "."
90char *resource_path = LOCAL_RESOURCE_PATH;
91
92/*
93 * We take a strict whitelist approach to stop ../ attacks
94 */
95
96struct serveable {
97 const char *urlpath;
98 const char *mimetype;
Tomas Cejka15c56302013-05-30 01:11:30 +020099};
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100100
101static const struct serveable whitelist[] = {
102 { "/favicon.ico", "image/x-icon" },
103 { "/libwebsockets.org-logo.png", "image/png" },
104
105 /* last one is the default served if no match */
106 { "/test.html", "text/html" },
107};
108
109struct per_session_data__http {
110 int fd;
111};
112
113/* this protocol server (always the first one) just knows how to do HTTP */
114
115static int callback_http(struct libwebsocket_context *context,
116 struct libwebsocket *wsi,
117 enum libwebsocket_callback_reasons reason, void *user,
118 void *in, size_t len)
119{
120 char client_name[128];
121 char client_ip[128];
122 char buf[256];
123 int n, m;
124 unsigned char *p;
125 static unsigned char buffer[4096];
126 struct stat stat_buf;
127 struct per_session_data__http *pss = (struct per_session_data__http *)user;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200128 struct libwebsocket_pollargs *pa = (struct libwebsocket_pollargs *) in;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100129
130 switch (reason) {
131 case LWS_CALLBACK_HTTP:
132
133 /* check for the "send a big file by hand" example case */
134
135 if (!strcmp((const char *)in, "/leaf.jpg")) {
136 char leaf_path[1024];
137 snprintf(leaf_path, sizeof(leaf_path), "%s/leaf.jpg", resource_path);
138
139 /* well, let's demonstrate how to send the hard way */
140
141 p = buffer;
142
143 pss->fd = open(leaf_path, O_RDONLY);
144
145 if (pss->fd < 0)
146 return -1;
147
148 fstat(pss->fd, &stat_buf);
149
150 /*
151 * we will send a big jpeg file, but it could be
152 * anything. Set the Content-Type: appropriately
153 * so the browser knows what to do with it.
154 */
155
156 p += sprintf((char *)p,
157 "HTTP/1.0 200 OK\x0d\x0a"
158 "Server: libwebsockets\x0d\x0a"
159 "Content-Type: image/jpeg\x0d\x0a"
160 "Content-Length: %u\x0d\x0a\x0d\x0a",
161 (unsigned int)stat_buf.st_size);
162
163 /*
164 * send the http headers...
165 * this won't block since it's the first payload sent
166 * on the connection since it was established
167 * (too small for partial)
168 */
169
170 n = libwebsocket_write(wsi, buffer,
171 p - buffer, LWS_WRITE_HTTP);
172
173 if (n < 0) {
174 close(pss->fd);
175 return -1;
176 }
177 /*
178 * book us a LWS_CALLBACK_HTTP_WRITEABLE callback
179 */
180 libwebsocket_callback_on_writable(context, wsi);
181 break;
182 }
183
184 /* if not, send a file the easy way */
185
186 for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
187 if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
188 break;
189
190 sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath);
191
Tomas Cejka11d5d062014-07-15 13:13:52 +0200192 if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype, NULL))
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100193 return -1; /* through completion or error, close the socket */
194
195 /*
196 * notice that the sending of the file completes asynchronously,
197 * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
198 * it's done
199 */
200
201 break;
202
203 case LWS_CALLBACK_HTTP_FILE_COMPLETION:
204// lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n");
205 /* kill the connection after we sent one file */
206 return -1;
207
208 case LWS_CALLBACK_HTTP_WRITEABLE:
209 /*
210 * we can send more of whatever it is we were sending
211 */
212
213 do {
214 n = read(pss->fd, buffer, sizeof buffer);
215 /* problem reading, close conn */
216 if (n < 0)
217 goto bail;
218 /* sent it all, close conn */
219 if (n == 0)
220 goto bail;
221 /*
222 * because it's HTTP and not websocket, don't need to take
223 * care about pre and postamble
224 */
225 m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP);
226 if (m < 0)
227 /* write failed, close conn */
228 goto bail;
229 if (m != n)
230 /* partial write, adjust */
231 lseek(pss->fd, m - n, SEEK_CUR);
232
233 } while (!lws_send_pipe_choked(wsi));
234 libwebsocket_callback_on_writable(context, wsi);
235 break;
236
237bail:
238 close(pss->fd);
239 return -1;
240
Tomas Cejka11d5d062014-07-15 13:13:52 +0200241 case LWS_CALLBACK_LOCK_POLL:
242 /*
243 * lock mutex to protect pollfd state
244 * called before any other POLL related callback
245 */
246 break;
247
248 case LWS_CALLBACK_UNLOCK_POLL:
249 /*
250 * unlock mutex to protect pollfd state when
251 * called after any other POLL related callback
252 */
253 break;
254
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100255 /*
256 * callback for confirming to continue with client IP appear in
257 * protocol 0 callback since no websocket protocol has been agreed
258 * yet. You can just ignore this if you won't filter on client IP
259 * since the default uhandled callback return is 0 meaning let the
260 * connection continue.
261 */
262
263 case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
264 libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name,
265 sizeof(client_name), client_ip, sizeof(client_ip));
266
Tomas Cejkaba21b382013-04-13 02:37:32 +0200267 //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100268 /* if we returned non-zero from here, we kill the connection */
269 break;
270
271 /*
272 * callbacks for managing the external poll() array appear in
273 * protocol 0 callback
274 */
275
276 case LWS_CALLBACK_ADD_POLL_FD:
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100277 if (count_pollfds >= max_poll_elements) {
278 lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
279 return 1;
280 }
281
Tomas Cejka11d5d062014-07-15 13:13:52 +0200282 fd_lookup[pa->fd] = count_pollfds;
283 pollfds[count_pollfds].fd = pa->fd;
284 pollfds[count_pollfds].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100285 pollfds[count_pollfds++].revents = 0;
286 break;
287
288 case LWS_CALLBACK_DEL_POLL_FD:
289 if (!--count_pollfds)
290 break;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200291 m = fd_lookup[pa->fd];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100292 /* have the last guy take up the vacant slot */
293 pollfds[m] = pollfds[count_pollfds];
294 fd_lookup[pollfds[count_pollfds].fd] = m;
295 break;
296
Tomas Cejka11d5d062014-07-15 13:13:52 +0200297 case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
298 pollfds[fd_lookup[pa->fd]].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100299 break;
300
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100301
302 default:
303 break;
304 }
305
306 return 0;
307}
308
Tomas Cejkaba21b382013-04-13 02:37:32 +0200309/**
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100310 * this is just an example of parsing handshake headers, you don't need this
311 * in your code unless you will filter allowing connections by the header
312 * content
313 */
Tomas Cejkaba21b382013-04-13 02:37:32 +0200314//static void dump_handshake_info(struct libwebsocket *wsi)
315//{
316// int n;
317// static const char *token_names[WSI_TOKEN_COUNT] = {
318// /*[WSI_TOKEN_GET_URI] =*/ "GET URI",
319// /*[WSI_TOKEN_HOST] =*/ "Host",
320// /*[WSI_TOKEN_CONNECTION] =*/ "Connection",
321// /*[WSI_TOKEN_KEY1] =*/ "key 1",
322// /*[WSI_TOKEN_KEY2] =*/ "key 2",
323// /*[WSI_TOKEN_PROTOCOL] =*/ "Protocol",
324// /*[WSI_TOKEN_UPGRADE] =*/ "Upgrade",
325// /*[WSI_TOKEN_ORIGIN] =*/ "Origin",
326// /*[WSI_TOKEN_DRAFT] =*/ "Draft",
327// /*[WSI_TOKEN_CHALLENGE] =*/ "Challenge",
328//
329// /* new for 04 */
330// /*[WSI_TOKEN_KEY] =*/ "Key",
331// /*[WSI_TOKEN_VERSION] =*/ "Version",
332// /*[WSI_TOKEN_SWORIGIN] =*/ "Sworigin",
333//
334// /* new for 05 */
335// /*[WSI_TOKEN_EXTENSIONS] =*/ "Extensions",
336//
337// /* client receives these */
338// /*[WSI_TOKEN_ACCEPT] =*/ "Accept",
339// /*[WSI_TOKEN_NONCE] =*/ "Nonce",
340// /*[WSI_TOKEN_HTTP] =*/ "Http",
341// /*[WSI_TOKEN_MUXURL] =*/ "MuxURL",
342// };
343// char buf[256];
344//
345// for (n = 0; n < WSI_TOKEN_COUNT; n++) {
346// if (!lws_hdr_total_length(wsi, n))
347// continue;
348//
349// //lws_hdr_copy(wsi, buf, sizeof buf, n);
350//
351// //fprintf(stderr, " %s = %s\n", token_names[n], buf);
352// }
353//}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100354
355/* dumb_increment protocol */
356
357/*
358 * one of these is auto-created for each connection and a pointer to the
359 * appropriate instance is passed to the callback in the user parameter
360 *
361 * for this example protocol we use it to individualize the count for each
362 * connection.
363 */
364
Tomas Cejkaba21b382013-04-13 02:37:32 +0200365struct per_session_data__notif_client {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100366 int number;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200367 char *session_key;
368 struct nc_session *session;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100369};
370
Tomas Cejkaba21b382013-04-13 02:37:32 +0200371struct session_with_mutex *get_ncsession_from_key(const char *session_key)
372{
373 struct session_with_mutex *locked_session = NULL;
374 if (session_key == NULL) {
375 return (NULL);
376 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200377 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 +0200378 return locked_session;
379}
380
381/* rpc parameter is freed after the function call */
382static int send_recv_process(struct nc_session *session, const char* operation, nc_rpc* rpc)
383{
384 nc_reply *reply = NULL;
385 char *data = NULL;
386 int ret = EXIT_SUCCESS;
387
388 /* send the request and get the reply */
389 switch (nc_session_send_recv(session, rpc, &reply)) {
390 case NC_MSG_UNKNOWN:
391 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100392 DEBUG("notifications: receiving rpc-reply failed.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200393 //cmd_disconnect(NULL);
394 ret = EXIT_FAILURE;
395 break;
396 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100397 DEBUG("notifications: Unknown error occurred.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200398 ret = EXIT_FAILURE;
399 break;
400 case NC_MSG_NONE:
401 /* error occurred, but processed by callback */
402 break;
403 case NC_MSG_REPLY:
404 switch (nc_reply_get_type(reply)) {
405 case NC_REPLY_OK:
406 break;
407 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100408 DEBUG("notifications: recv: %s.", data = nc_reply_get_data (reply));
409 free(data);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200410 break;
411 case NC_REPLY_ERROR:
412 /* wtf, you shouldn't be here !?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100413 DEBUG("notifications: operation failed, but rpc-error was not processed.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200414 ret = EXIT_FAILURE;
415 break;
416 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100417 DEBUG("notifications: unexpected operation result.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200418 ret = EXIT_FAILURE;
419 break;
420 }
421 break;
422 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100423 DEBUG("notifications: Unknown error occurred.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200424 ret = EXIT_FAILURE;
425 break;
426 }
427 nc_rpc_free(rpc);
428 nc_reply_free(reply);
429
430 return (ret);
431}
432
433/**
434 * \brief Callback to store incoming notification
435 * \param [in] eventtime - time when notification occured
436 * \param [in] content - content of notification
437 */
438static void notification_fileprint (time_t eventtime, const char* content)
439{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200440 struct session_with_mutex *target_session = NULL;
441 notification_t *ntf = NULL;
442 char *session_hash = NULL;
443
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100444 DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content);
Tomas Cejka15c56302013-05-30 01:11:30 +0200445
Tomas Cejkaba21b382013-04-13 02:37:32 +0200446 session_hash = pthread_getspecific(thread_key);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100447 DEBUG("notification: fileprint getspecific (%s)", session_hash);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200448 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100449 DEBUG("Error while locking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200450 return;
451 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100452 DEBUG("Get session with mutex from key %s.", session_hash);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200453 target_session = get_ncsession_from_key(session_hash);
454 if (target_session == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100455 DEBUG("no session found last_session_key (%s)", session_hash);
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200456 goto unlock_glob;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200457 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200458 if (pthread_mutex_lock(&target_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100459 DEBUG("Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200460 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200461
Tomas Cejkaba21b382013-04-13 02:37:32 +0200462 if (target_session->notifications == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100463 DEBUG("target_session->notifications is NULL");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200464 goto unlock_all;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200465 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100466 DEBUG("notification: ready to push to notifications queue");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200467 ntf = (notification_t *) apr_array_push(target_session->notifications);
468 if (ntf == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100469 DEBUG("Failed to allocate element ");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200470 goto unlock_all;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200471 }
Tomas Cejka73286932013-05-27 22:54:35 +0200472 ntf->eventtime = eventtime;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200473 ntf->content = strdup(content);
474
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100475 DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200476
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200477unlock_all:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200478 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100479 DEBUG("Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200480 }
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200481unlock_glob:
482 if (pthread_rwlock_unlock(&session_lock) != 0) {
483 DEBUG("Error while locking rwlock");
484 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200485}
486
487/**
488 * \brief Thread for libnetconf notifications dispatch
489 * \param [in] arg - struct ntf_thread_config * with nc_session
490 */
491void* notification_thread(void* arg)
492{
493 struct ntf_thread_config *config = (struct ntf_thread_config*)arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100494 DEBUG("notifications: in thread for libnetconf notifications");
Tomas Cejka15c56302013-05-30 01:11:30 +0200495
496 /* store hash identification of netconf session for notifications printing callback */
497 if (pthread_setspecific(thread_key, config->session_hash) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100498 DEBUG("notifications: cannot set thread-specific hash value.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200499 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200500
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100501 DEBUG("notifications: dispatching");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200502 ncntf_dispatch_receive(config->session, notification_fileprint);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100503 DEBUG("notifications: ended thread for libnetconf notifications");
504 if (config->session_hash != NULL) {
505 free(config->session_hash);
506 }
507 if (config != NULL) {
508 free(config);
509 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200510 return (NULL);
511}
512
513
514int notif_subscribe(struct session_with_mutex *locked_session, const char *session_hash, time_t start_time, time_t stop_time)
515{
516 time_t start = -1;
517 time_t stop = -1;
518 struct nc_filter *filter = NULL;
519 char *stream = NULL;
520 nc_rpc *rpc = NULL;
521 pthread_t thread;
522 struct ntf_thread_config *tconfig;
523 struct nc_session *session;
524
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100525 DEBUG("notif_subscribe");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200526 if (locked_session == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100527 DEBUG("notifications: no locked_session was given.");
528 DEBUG("Close notification client");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200529 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200530 }
531
Tomas Cejka47387fd2013-06-10 20:37:46 +0200532 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200533 session = locked_session->session;
534
535 start = time(NULL) + start_time;
536 stop = time(NULL) + stop_time;
Tomas Cejka57714142014-06-22 17:59:58 +0200537 start = 0;
538 stop = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100539 DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200540
541 if (session == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100542 DEBUG("notifications: NETCONF session not established.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200543 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200544 }
545
546 /* check if notifications are allowed on this session */
547 if (nc_session_notif_allowed(session) == 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100548 DEBUG("notifications: Notification subscription is not allowed on this session.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200549 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200550 }
551 /* check times */
552 if (start != -1 && stop != -1 && start > stop) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100553 DEBUG("notifications: Subscription start time must be lower than the end time.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200554 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200555 }
556
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100557 DEBUG("Prepare to execute subscription.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200558 /* create requests */
Tomas Cejka9ca00802014-07-08 16:03:26 +0200559 rpc = nc_rpc_subscribe(stream, filter, (start_time == -1)?NULL:&start, (stop_time == 0)?NULL:&stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200560 nc_filter_free(filter);
561 if (rpc == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100562 DEBUG("notifications: creating an rpc request failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200563 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200564 }
565
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100566 DEBUG("Send NC subscribe.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200567 create_err_reply_p();
Tomas Cejkaba21b382013-04-13 02:37:32 +0200568 if (send_recv_process(session, "subscribe", rpc) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100569 DEBUG("Subscription RPC failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200570 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200571 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200572
573 GETSPEC_ERR_REPLY
574 if (err_reply != NULL) {
575 free_err_reply();
576 DEBUG("RPC-Error received and cleaned, because we can't send it anywhere.");
577 goto operation_failed;
578 }
579
Tomas Cejkaba21b382013-04-13 02:37:32 +0200580 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200581 locked_session->ntfc_subscribed = 1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200582
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100583 DEBUG("Create config for notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200584 tconfig = malloc(sizeof(struct ntf_thread_config));
585 tconfig->session = session;
586 tconfig->session_hash = strdup(session_hash);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100587 DEBUG("notifications: creating libnetconf notification thread (%s).", tconfig->session_hash);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200588
Tomas Cejka47387fd2013-06-10 20:37:46 +0200589 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100590 DEBUG("Create notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200591 if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100592 DEBUG("notifications: creating a thread for receiving notifications failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200593 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200594 }
595 pthread_detach(thread);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100596 DEBUG("Subscription finished.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200597 return 0;
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200598
599operation_failed:
600 pthread_mutex_unlock(&locked_session->lock);
601 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200602}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100603
604static int callback_notification(struct libwebsocket_context *context,
605 struct libwebsocket *wsi,
606 enum libwebsocket_callback_reasons reason,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200607 void *user, void *in, size_t len)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100608{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200609 int n = 0;
610 int m = 0;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200611 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100612 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200613 struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user;
614
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100615 switch (reason) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100616 case LWS_CALLBACK_ESTABLISHED:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100617 DEBUG("notification client connected.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100618 break;
619
620 case LWS_CALLBACK_SERVER_WRITEABLE:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200621 if (pss->session_key == NULL) {
622 return 0;
623 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100624 //DEBUG("Callback server writeable.");
625 //DEBUG("lock session lock.");
Tomas Cejka866f2282014-09-18 15:20:26 +0200626 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100627 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200628 return -1;
629 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100630 //DEBUG("get session_with_mutex for %s.", pss->session_key);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200631 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
632 if (ls == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100633 DEBUG("notification: session not found");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200634 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100635 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200636 return -1;
637 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200638 return -1;
639 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200640 pthread_mutex_lock(&ls->lock);
Tomas Cejka866f2282014-09-18 15:20:26 +0200641 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100642 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkaba21b382013-04-13 02:37:32 +0200643 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200644
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100645 //DEBUG("check for closed session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200646 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100647 DEBUG("unlock session key.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200648 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100649 DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200650 return -1;
Tomas Cejka654f84e2013-04-19 11:55:01 +0200651 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200652 return -1;
653 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100654 //DEBUG("lock private lock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200655 notification_t *notif = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100656 //DEBUG("check for uninitialized notification list.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200657 if (ls->notifications == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100658 DEBUG("notification: no notifications array");
659 DEBUG("unlock private lock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200660 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100661 DEBUG("notification: cannot unlock session");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200662 }
663 return -1;
664 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100665 //DEBUG("check for empty notification list.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200666 if (!apr_is_empty_array(ls->notifications)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100667 DEBUG("notification: POP notifications for session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200668
Tomas Cejka654f84e2013-04-19 11:55:01 +0200669 while ((notif = (notification_t *) apr_array_pop(ls->notifications)) != NULL) {
Tomas Cejka654f84e2013-04-19 11:55:01 +0200670 n = 0;
Tomas Cejka442258e2014-04-01 18:17:18 +0200671 pthread_mutex_lock(&json_lock);
Tomas Cejka73286932013-05-27 22:54:35 +0200672 json_object *notif_json = json_object_new_object();
Tomas Cejka8800d652013-07-16 10:47:08 +0200673 json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime));
Tomas Cejka73286932013-05-27 22:54:35 +0200674 json_object_object_add(notif_json, "content", json_object_new_string(notif->content));
Tomas Cejka442258e2014-04-01 18:17:18 +0200675 pthread_mutex_unlock(&json_lock);
Tomas Cejka73286932013-05-27 22:54:35 +0200676
Tomas Cejka00635972013-06-03 15:10:52 +0200677 const char *msgtext = json_object_to_json_string(notif_json);
Tomas Cejka15c56302013-05-30 01:11:30 +0200678
Tomas Cejka47387fd2013-06-10 20:37:46 +0200679 //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t);
Tomas Cejka15c56302013-05-30 01:11:30 +0200680 n = sprintf((char *)p, "%s", msgtext);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100681 DEBUG("ws send %dB in %lu", n, sizeof(buf));
Tomas Cejka15c56302013-05-30 01:11:30 +0200682 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200683 if (lws_send_pipe_choked(wsi)) {
684 libwebsocket_callback_on_writable(context, wsi);
685 break;
686 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200687
Tomas Cejka442258e2014-04-01 18:17:18 +0200688 pthread_mutex_lock(&json_lock);
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200689 json_object_put(notif_json);
Tomas Cejka442258e2014-04-01 18:17:18 +0200690 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100691 free(notif->content);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200692 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100693 DEBUG("notification: POP notifications done");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200694 }
695
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100696 //DEBUG("unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200697 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100698 DEBUG("notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200699 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100700 //DEBUG("unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200701
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100702 if (m < n) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100703 DEBUG("ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200704
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100705 return -1;
706 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100707 break;
708
709 case LWS_CALLBACK_RECEIVE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100710 DEBUG("Callback receive.");
711 DEBUG("received: (%s)", (char *)in);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200712 if (pss->session_key == NULL) {
713 char session_key_buf[41];
714 int start = -1;
715 time_t stop = time(NULL) + 30;
716
717 strncpy((char *) session_key_buf, (const char *) in, 40);
718 session_key_buf[40] = '\0';
719 pss->session_key = strdup(session_key_buf);
720 sscanf(in+40, "%d %d", (int *) &start, (int *) &stop);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100721 DEBUG("notification: get key (%s) from (%s) (%i,%i)", pss->session_key, (char *) in, (int) start, (int) stop);
Tomas Cejka15c56302013-05-30 01:11:30 +0200722
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100723 DEBUG("lock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200724 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100725 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200726 return -1;
727 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100728 DEBUG("get session from key (%s)", pss->session_key);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200729 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
730 if (ls == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100731 DEBUG("notification: session_key not found (%s)", pss->session_key);
732 DEBUG("unlock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200733 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100734 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200735 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100736 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200737 return -1;
738 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100739 DEBUG("lock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200740 pthread_mutex_lock(&ls->lock);
741
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100742 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200743 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100744 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200745 }
746
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100747 DEBUG("Found session to subscribe notif.");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200748 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100749 DEBUG("session already closed - handle no notification");
750 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200751 pthread_mutex_unlock(&ls->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100752 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200753 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200754 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200755 if (ls->ntfc_subscribed != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100756 DEBUG("notification: already subscribed");
757 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200758 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200759 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200760 return 0;
761 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100762 DEBUG("notification: prepare to subscribe stream");
763 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200764 pthread_mutex_unlock(&ls->lock);
765
766 /* notif_subscribe locks on its own */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200767 return notif_subscribe(ls, pss->session_key, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200768 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100769 if (len < 6)
770 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100771 break;
772 /*
773 * this just demonstrates how to use the protocol filter. If you won't
774 * study and reject connections based on header content, you don't need
775 * to handle this callback
776 */
777
778 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
779 //dump_handshake_info(wsi);
780 /* you could return non-zero here and kill the connection */
781 break;
Tomas Cejka866f2282014-09-18 15:20:26 +0200782 //gives segfault :-(
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100783 //case LWS_CALLBACK_CLOSED:
784 // if (pss->session_key != NULL) {
785 // free(pss->session_key);
786 // }
787 // if (pss != NULL) {
788 // free(pss);
789 // }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100790
791 default:
792 break;
793 }
794
795 return 0;
796}
797
798/* list of supported protocols and callbacks */
799
800static struct libwebsocket_protocols protocols[] = {
801 /* first protocol must always be HTTP handler */
802
803 {
804 "http-only", /* name */
805 callback_http, /* callback */
806 sizeof (struct per_session_data__http), /* per_session_data_size */
807 0, /* max frame size / rx buffer */
808 },
809 {
810 "notification-protocol",
811 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200812 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200813 4000,
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100814 },
815 { NULL, NULL, 0, 0 } /* terminator */
816};
817
818
Tomas Cejkaba21b382013-04-13 02:37:32 +0200819/**
820 * initialization of notification module
821 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200822int notification_init(apr_pool_t * pool, server_rec * server)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100823{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200824 //char cert_path[1024];
825 //char key_path[1024];
826 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100827 struct lws_context_creation_info info;
828 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200829 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100830 const char *iface = NULL;
831 int debug_level = 7;
832
833 memset(&info, 0, sizeof info);
834 info.port = NOTIFICATION_SERVER_PORT;
835
836 /* tell the library what debug level to emit and to send it to syslog */
837 lws_set_log_level(debug_level, lwsl_emit_syslog);
838
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100839 DEBUG("Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200840 //lwsl_notice("libwebsockets test server - "
841 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
842 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100843 max_poll_elements = getdtablesize();
844 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
845 fd_lookup = malloc(max_poll_elements * sizeof (int));
846 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100847 DEBUG("Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100848 return -1;
849 }
850
851 info.iface = iface;
852 info.protocols = protocols;
853
854 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
855 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
856
857 //info.ssl_cert_filepath = cert_path;
858 //info.ssl_private_key_filepath = key_path;
859
860 info.gid = -1;
861 info.uid = -1;
862 info.options = opts;
863
864 /* create server */
865 context = libwebsocket_create_context(&info);
866 if (context == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100867 DEBUG("libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100868 return -1;
869 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200870
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100871 DEBUG("notifications: init of pthread_key_create.");
Tomas Cejka15c56302013-05-30 01:11:30 +0200872 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100873 DEBUG("notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200874 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200875 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100876}
877
878void notification_close()
879{
880 libwebsocket_context_destroy(context);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100881 free(pollfds);
882 free(fd_lookup);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100883
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100884 DEBUG("libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100885}
886
Tomas Cejkaba21b382013-04-13 02:37:32 +0200887
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100888/**
889 * \brief send notification if any
890 * \return < 0 on error
891 */
892int notification_handle()
893{
894 static struct timeval tv;
895 static unsigned int olds = 0;
896 int n = 0;
897
898 gettimeofday(&tv, NULL);
899
900 /*
901 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
902 * live websocket connection using the DUMB_INCREMENT protocol,
903 * as soon as it can take more packets (usually immediately)
904 */
905
906 if (((unsigned int)tv.tv_sec - olds) > 0) {
907 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
908 olds = tv.tv_sec;
909 }
910
911
912 /*
913 * this represents an existing server's single poll action
914 * which also includes libwebsocket sockets
915 */
916
917 n = poll(pollfds, count_pollfds, 50);
918 if (n < 0)
919 return n;
920
921
922 if (n) {
923 for (n = 0; n < count_pollfds; n++) {
924 if (pollfds[n].revents) {
925 /*
926 * returns immediately if the fd does not
927 * match anything under libwebsockets
928 * control
929 */
930 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
931 return 1;
932 }
933 }
934 }
935 }
936 return 0;
937}
938
939#endif
940
941
942#ifndef WITH_NOTIFICATIONS
943#ifdef TEST_NOTIFICATION_SERVER
944int main(int argc, char **argv)
945{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100946 if (notification_init(NULL, NULL) == -1) {
947 fprintf(stderr, "Error during initialization\n");
948 return 1;
949 }
950 while (!force_exit) {
951 notification_handle();
952 }
953 notification_close();
954}
955#endif
956#endif