blob: 151d1b523893fd810444b4cf3ceee84bed78a180 [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;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100124 static unsigned char buffer[4096];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100125 struct per_session_data__http *pss = (struct per_session_data__http *)user;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200126 struct libwebsocket_pollargs *pa = (struct libwebsocket_pollargs *) in;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100127
128 switch (reason) {
129 case LWS_CALLBACK_HTTP:
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100130 for (n = 0; n < (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++)
131 if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0)
132 break;
133
134 sprintf(buf, "%s%s", resource_path, whitelist[n].urlpath);
135
Tomas Cejka11d5d062014-07-15 13:13:52 +0200136 if (libwebsockets_serve_http_file(context, wsi, buf, whitelist[n].mimetype, NULL))
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100137 return -1; /* through completion or error, close the socket */
138
139 /*
140 * notice that the sending of the file completes asynchronously,
141 * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when
142 * it's done
143 */
144
145 break;
146
147 case LWS_CALLBACK_HTTP_FILE_COMPLETION:
148// lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n");
149 /* kill the connection after we sent one file */
150 return -1;
151
152 case LWS_CALLBACK_HTTP_WRITEABLE:
153 /*
154 * we can send more of whatever it is we were sending
155 */
156
157 do {
158 n = read(pss->fd, buffer, sizeof buffer);
159 /* problem reading, close conn */
160 if (n < 0)
161 goto bail;
162 /* sent it all, close conn */
163 if (n == 0)
164 goto bail;
165 /*
166 * because it's HTTP and not websocket, don't need to take
167 * care about pre and postamble
168 */
169 m = libwebsocket_write(wsi, buffer, n, LWS_WRITE_HTTP);
170 if (m < 0)
171 /* write failed, close conn */
172 goto bail;
173 if (m != n)
174 /* partial write, adjust */
175 lseek(pss->fd, m - n, SEEK_CUR);
176
177 } while (!lws_send_pipe_choked(wsi));
178 libwebsocket_callback_on_writable(context, wsi);
179 break;
180
181bail:
182 close(pss->fd);
183 return -1;
184
Tomas Cejka11d5d062014-07-15 13:13:52 +0200185 case LWS_CALLBACK_LOCK_POLL:
186 /*
187 * lock mutex to protect pollfd state
188 * called before any other POLL related callback
189 */
190 break;
191
192 case LWS_CALLBACK_UNLOCK_POLL:
193 /*
194 * unlock mutex to protect pollfd state when
195 * called after any other POLL related callback
196 */
197 break;
198
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100199 /*
200 * callback for confirming to continue with client IP appear in
201 * protocol 0 callback since no websocket protocol has been agreed
202 * yet. You can just ignore this if you won't filter on client IP
203 * since the default uhandled callback return is 0 meaning let the
204 * connection continue.
205 */
206
207 case LWS_CALLBACK_FILTER_NETWORK_CONNECTION:
208 libwebsockets_get_peer_addresses(context, wsi, (int)(long)in, client_name,
209 sizeof(client_name), client_ip, sizeof(client_ip));
210
Tomas Cejkaba21b382013-04-13 02:37:32 +0200211 //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100212 /* if we returned non-zero from here, we kill the connection */
213 break;
214
215 /*
216 * callbacks for managing the external poll() array appear in
217 * protocol 0 callback
218 */
219
220 case LWS_CALLBACK_ADD_POLL_FD:
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100221 if (count_pollfds >= max_poll_elements) {
222 lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n");
223 return 1;
224 }
225
Tomas Cejka11d5d062014-07-15 13:13:52 +0200226 fd_lookup[pa->fd] = count_pollfds;
227 pollfds[count_pollfds].fd = pa->fd;
228 pollfds[count_pollfds].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100229 pollfds[count_pollfds++].revents = 0;
230 break;
231
232 case LWS_CALLBACK_DEL_POLL_FD:
233 if (!--count_pollfds)
234 break;
Tomas Cejka11d5d062014-07-15 13:13:52 +0200235 m = fd_lookup[pa->fd];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100236 /* have the last guy take up the vacant slot */
237 pollfds[m] = pollfds[count_pollfds];
238 fd_lookup[pollfds[count_pollfds].fd] = m;
239 break;
240
Tomas Cejka11d5d062014-07-15 13:13:52 +0200241 case LWS_CALLBACK_CHANGE_MODE_POLL_FD:
242 pollfds[fd_lookup[pa->fd]].events = pa->events;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100243 break;
244
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100245
246 default:
247 break;
248 }
249
250 return 0;
251}
252
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100253
254/* dumb_increment protocol */
255
256/*
257 * one of these is auto-created for each connection and a pointer to the
258 * appropriate instance is passed to the callback in the user parameter
259 *
260 * for this example protocol we use it to individualize the count for each
261 * connection.
262 */
263
Tomas Cejkaba21b382013-04-13 02:37:32 +0200264struct per_session_data__notif_client {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100265 int number;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200266 char *session_key;
267 struct nc_session *session;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100268};
269
Tomas Cejkaba21b382013-04-13 02:37:32 +0200270struct session_with_mutex *get_ncsession_from_key(const char *session_key)
271{
272 struct session_with_mutex *locked_session = NULL;
273 if (session_key == NULL) {
274 return (NULL);
275 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200276 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 +0200277 return locked_session;
278}
279
280/* rpc parameter is freed after the function call */
281static int send_recv_process(struct nc_session *session, const char* operation, nc_rpc* rpc)
282{
283 nc_reply *reply = NULL;
284 char *data = NULL;
285 int ret = EXIT_SUCCESS;
286
287 /* send the request and get the reply */
288 switch (nc_session_send_recv(session, rpc, &reply)) {
289 case NC_MSG_UNKNOWN:
290 if (nc_session_get_status(session) != NC_SESSION_STATUS_WORKING) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200291 ERROR("notifications: receiving rpc-reply failed.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200292 //cmd_disconnect(NULL);
293 ret = EXIT_FAILURE;
294 break;
295 }
Tomas Cejkacf44e522015-04-24 17:29:21 +0200296 ERROR("notifications: Unknown error occurred.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200297 ret = EXIT_FAILURE;
298 break;
299 case NC_MSG_NONE:
300 /* error occurred, but processed by callback */
301 break;
302 case NC_MSG_REPLY:
303 switch (nc_reply_get_type(reply)) {
304 case NC_REPLY_OK:
305 break;
306 case NC_REPLY_DATA:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100307 DEBUG("notifications: recv: %s.", data = nc_reply_get_data (reply));
308 free(data);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200309 break;
310 case NC_REPLY_ERROR:
311 /* wtf, you shouldn't be here !?!? */
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100312 DEBUG("notifications: operation failed, but rpc-error was not processed.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200313 ret = EXIT_FAILURE;
314 break;
315 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100316 DEBUG("notifications: unexpected operation result.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200317 ret = EXIT_FAILURE;
318 break;
319 }
320 break;
321 default:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100322 DEBUG("notifications: Unknown error occurred.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200323 ret = EXIT_FAILURE;
324 break;
325 }
326 nc_rpc_free(rpc);
327 nc_reply_free(reply);
328
329 return (ret);
330}
331
332/**
333 * \brief Callback to store incoming notification
334 * \param [in] eventtime - time when notification occured
335 * \param [in] content - content of notification
336 */
337static void notification_fileprint (time_t eventtime, const char* content)
338{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200339 struct session_with_mutex *target_session = NULL;
340 notification_t *ntf = NULL;
341 char *session_hash = NULL;
342
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100343 DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content);
Tomas Cejka15c56302013-05-30 01:11:30 +0200344
Tomas Cejkaba21b382013-04-13 02:37:32 +0200345 session_hash = pthread_getspecific(thread_key);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100346 DEBUG("notification: fileprint getspecific (%s)", session_hash);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200347 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200348 ERROR("notifications: Error while locking rwlock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200349 return;
350 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100351 DEBUG("Get session with mutex from key %s.", session_hash);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200352 target_session = get_ncsession_from_key(session_hash);
353 if (target_session == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200354 ERROR("notifications: no session found last_session_key (%s)", session_hash);
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200355 goto unlock_glob;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200356 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200357 if (pthread_mutex_lock(&target_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200358 ERROR("notifications: Error while locking rwlock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200359 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200360
Tomas Cejkaba21b382013-04-13 02:37:32 +0200361 if (target_session->notifications == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200362 ERROR("notifications: target_session->notifications is NULL");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200363 goto unlock_all;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200364 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100365 DEBUG("notification: ready to push to notifications queue");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200366 ntf = (notification_t *) apr_array_push(target_session->notifications);
367 if (ntf == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200368 ERROR("notifications: Failed to allocate element ");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200369 goto unlock_all;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200370 }
Tomas Cejka73286932013-05-27 22:54:35 +0200371 ntf->eventtime = eventtime;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200372 ntf->content = strdup(content);
373
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100374 DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200375
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200376unlock_all:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200377 if (pthread_mutex_unlock(&target_session->lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200378 ERROR("notifications: Error while unlocking rwlock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200379 }
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200380unlock_glob:
381 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200382 ERROR("notifications: Error while locking rwlock");
Tomas Cejka885ec3e2014-06-22 18:01:34 +0200383 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200384}
385
386/**
387 * \brief Thread for libnetconf notifications dispatch
388 * \param [in] arg - struct ntf_thread_config * with nc_session
389 */
390void* notification_thread(void* arg)
391{
392 struct ntf_thread_config *config = (struct ntf_thread_config*)arg;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100393 DEBUG("notifications: in thread for libnetconf notifications");
Tomas Cejka15c56302013-05-30 01:11:30 +0200394
395 /* store hash identification of netconf session for notifications printing callback */
396 if (pthread_setspecific(thread_key, config->session_hash) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200397 ERROR("notifications: cannot set thread-specific hash value.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200398 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200399
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100400 DEBUG("notifications: dispatching");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200401 ncntf_dispatch_receive(config->session, notification_fileprint);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100402 DEBUG("notifications: ended thread for libnetconf notifications");
403 if (config->session_hash != NULL) {
404 free(config->session_hash);
405 }
406 if (config != NULL) {
407 free(config);
408 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200409 return (NULL);
410}
411
412
413int notif_subscribe(struct session_with_mutex *locked_session, const char *session_hash, time_t start_time, time_t stop_time)
414{
415 time_t start = -1;
416 time_t stop = -1;
417 struct nc_filter *filter = NULL;
418 char *stream = NULL;
419 nc_rpc *rpc = NULL;
420 pthread_t thread;
421 struct ntf_thread_config *tconfig;
422 struct nc_session *session;
423
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100424 DEBUG("notif_subscribe");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200425 if (locked_session == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100426 DEBUG("notifications: no locked_session was given.");
Tomas Cejkacf44e522015-04-24 17:29:21 +0200427 /* Close notification client */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200428 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200429 }
430
Tomas Cejka47387fd2013-06-10 20:37:46 +0200431 pthread_mutex_lock(&locked_session->lock);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200432 session = locked_session->session;
433
434 start = time(NULL) + start_time;
435 stop = time(NULL) + stop_time;
Tomas Cejka57714142014-06-22 17:59:58 +0200436 start = 0;
437 stop = 0;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100438 DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200439
440 if (session == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200441 ERROR("notifications: NETCONF session not established.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200442 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200443 }
444
445 /* check if notifications are allowed on this session */
446 if (nc_session_notif_allowed(session) == 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200447 ERROR("notifications: Notification subscription is not allowed on this session.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200448 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200449 }
450 /* check times */
451 if (start != -1 && stop != -1 && start > stop) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200452 ERROR("notifications: Subscription start time must be lower than the end time.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200453 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200454 }
455
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100456 DEBUG("Prepare to execute subscription.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200457 /* create requests */
Tomas Cejka9ca00802014-07-08 16:03:26 +0200458 rpc = nc_rpc_subscribe(stream, filter, (start_time == -1)?NULL:&start, (stop_time == 0)?NULL:&stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200459 nc_filter_free(filter);
460 if (rpc == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200461 ERROR("notifications: creating an rpc request failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200462 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200463 }
464
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100465 DEBUG("Send NC subscribe.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200466 create_err_reply_p();
Tomas Cejkaba21b382013-04-13 02:37:32 +0200467 if (send_recv_process(session, "subscribe", rpc) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200468 ERROR("Subscription RPC failed.");
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200469 goto operation_failed;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200470 }
Tomas Cejka442258e2014-04-01 18:17:18 +0200471
472 GETSPEC_ERR_REPLY
473 if (err_reply != NULL) {
474 free_err_reply();
Tomas Cejkacf44e522015-04-24 17:29:21 +0200475 ERROR("RPC-Error received and cleaned, because we can't send it anywhere.");
Tomas Cejka442258e2014-04-01 18:17:18 +0200476 goto operation_failed;
477 }
478
Tomas Cejkaba21b382013-04-13 02:37:32 +0200479 rpc = NULL; /* just note that rpc is already freed by send_recv_process() */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200480 locked_session->ntfc_subscribed = 1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200481
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100482 DEBUG("Create config for notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200483 tconfig = malloc(sizeof(struct ntf_thread_config));
Tomas Cejkacf44e522015-04-24 17:29:21 +0200484 if (tconfig == NULL) {
485 ERROR("notifications: Allocation failed.");
486 goto operation_failed;
487 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200488 tconfig->session = session;
489 tconfig->session_hash = strdup(session_hash);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100490 DEBUG("notifications: creating libnetconf notification thread (%s).", tconfig->session_hash);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200491
Tomas Cejka47387fd2013-06-10 20:37:46 +0200492 pthread_mutex_unlock(&locked_session->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100493 DEBUG("Create notification_thread.");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200494 if (pthread_create(&thread, NULL, notification_thread, tconfig) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200495 ERROR("notifications: creating a thread for receiving notifications failed");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200496 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200497 }
498 pthread_detach(thread);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100499 DEBUG("Subscription finished.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200500 return 0;
Tomas Cejka5ce57b62013-08-29 17:47:39 +0200501
502operation_failed:
503 pthread_mutex_unlock(&locked_session->lock);
504 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200505}
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100506
507static int callback_notification(struct libwebsocket_context *context,
508 struct libwebsocket *wsi,
509 enum libwebsocket_callback_reasons reason,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200510 void *user, void *in, size_t len)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100511{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200512 int n = 0;
513 int m = 0;
Tomas Cejka47387fd2013-06-10 20:37:46 +0200514 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING];
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100515 unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
Tomas Cejkaba21b382013-04-13 02:37:32 +0200516 struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user;
517
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100518 switch (reason) {
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100519 case LWS_CALLBACK_ESTABLISHED:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100520 DEBUG("notification client connected.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100521 break;
522
523 case LWS_CALLBACK_SERVER_WRITEABLE:
Tomas Cejkaba21b382013-04-13 02:37:32 +0200524 if (pss->session_key == NULL) {
525 return 0;
526 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100527 //DEBUG("Callback server writeable.");
528 //DEBUG("lock session lock.");
Tomas Cejka866f2282014-09-18 15:20:26 +0200529 if (pthread_rwlock_wrlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100530 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200531 return -1;
532 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100533 //DEBUG("get session_with_mutex for %s.", pss->session_key);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200534 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
535 if (ls == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100536 DEBUG("notification: session not found");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200537 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100538 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200539 return -1;
540 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200541 return -1;
542 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200543 pthread_mutex_lock(&ls->lock);
Tomas Cejka866f2282014-09-18 15:20:26 +0200544 if (pthread_rwlock_unlock(&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100545 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkaba21b382013-04-13 02:37:32 +0200546 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200547
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100548 //DEBUG("check for closed session.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200549 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100550 DEBUG("unlock session key.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200551 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100552 DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200553 return -1;
Tomas Cejka654f84e2013-04-19 11:55:01 +0200554 }
Tomas Cejka47387fd2013-06-10 20:37:46 +0200555 return -1;
556 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100557 //DEBUG("lock private lock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200558 notification_t *notif = NULL;
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100559 //DEBUG("check for uninitialized notification list.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200560 if (ls->notifications == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100561 DEBUG("notification: no notifications array");
562 DEBUG("unlock private lock.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200563 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100564 DEBUG("notification: cannot unlock session");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200565 }
566 return -1;
567 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100568 //DEBUG("check for empty notification list.");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200569 if (!apr_is_empty_array(ls->notifications)) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100570 DEBUG("notification: POP notifications for session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200571
Tomas Cejka654f84e2013-04-19 11:55:01 +0200572 while ((notif = (notification_t *) apr_array_pop(ls->notifications)) != NULL) {
Tomas Cejka654f84e2013-04-19 11:55:01 +0200573 n = 0;
Tomas Cejka442258e2014-04-01 18:17:18 +0200574 pthread_mutex_lock(&json_lock);
Tomas Cejka73286932013-05-27 22:54:35 +0200575 json_object *notif_json = json_object_new_object();
Tomas Cejka8800d652013-07-16 10:47:08 +0200576 json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime));
Tomas Cejka73286932013-05-27 22:54:35 +0200577 json_object_object_add(notif_json, "content", json_object_new_string(notif->content));
Tomas Cejka442258e2014-04-01 18:17:18 +0200578 pthread_mutex_unlock(&json_lock);
Tomas Cejka73286932013-05-27 22:54:35 +0200579
Tomas Cejka00635972013-06-03 15:10:52 +0200580 const char *msgtext = json_object_to_json_string(notif_json);
Tomas Cejka15c56302013-05-30 01:11:30 +0200581
Tomas Cejka47387fd2013-06-10 20:37:46 +0200582 //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t);
Tomas Cejka15c56302013-05-30 01:11:30 +0200583 n = sprintf((char *)p, "%s", msgtext);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100584 DEBUG("ws send %dB in %lu", n, sizeof(buf));
Tomas Cejka15c56302013-05-30 01:11:30 +0200585 m = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
Tomas Cejka47387fd2013-06-10 20:37:46 +0200586 if (lws_send_pipe_choked(wsi)) {
587 libwebsocket_callback_on_writable(context, wsi);
588 break;
589 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200590
Tomas Cejka442258e2014-04-01 18:17:18 +0200591 pthread_mutex_lock(&json_lock);
Tomas Cejka8a82dab2013-05-30 23:37:23 +0200592 json_object_put(notif_json);
Tomas Cejka442258e2014-04-01 18:17:18 +0200593 pthread_mutex_unlock(&json_lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100594 free(notif->content);
Tomas Cejka654f84e2013-04-19 11:55:01 +0200595 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100596 DEBUG("notification: POP notifications done");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200597 }
598
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100599 //DEBUG("unlock private lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200600 if (pthread_mutex_unlock(&ls->lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100601 DEBUG("notification: cannot unlock session");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200602 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100603 //DEBUG("unlock session lock");
Tomas Cejkaba21b382013-04-13 02:37:32 +0200604
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100605 if (m < n) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100606 DEBUG("ERROR %d writing to di socket.", n);
Tomas Cejka15c56302013-05-30 01:11:30 +0200607
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100608 return -1;
609 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100610 break;
611
612 case LWS_CALLBACK_RECEIVE:
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100613 DEBUG("Callback receive.");
614 DEBUG("received: (%s)", (char *)in);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200615 if (pss->session_key == NULL) {
616 char session_key_buf[41];
617 int start = -1;
618 time_t stop = time(NULL) + 30;
619
620 strncpy((char *) session_key_buf, (const char *) in, 40);
621 session_key_buf[40] = '\0';
622 pss->session_key = strdup(session_key_buf);
623 sscanf(in+40, "%d %d", (int *) &start, (int *) &stop);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100624 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 +0200625
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100626 DEBUG("lock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200627 if (pthread_rwlock_rdlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100628 DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200629 return -1;
630 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100631 DEBUG("get session from key (%s)", pss->session_key);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200632 struct session_with_mutex *ls = get_ncsession_from_key(pss->session_key);
633 if (ls == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100634 DEBUG("notification: session_key not found (%s)", pss->session_key);
635 DEBUG("unlock session lock");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200636 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100637 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200638 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100639 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200640 return -1;
641 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100642 DEBUG("lock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200643 pthread_mutex_lock(&ls->lock);
644
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100645 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200646 if (pthread_rwlock_unlock (&session_lock) != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100647 DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno));
Tomas Cejka47387fd2013-06-10 20:37:46 +0200648 }
649
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100650 DEBUG("Found session to subscribe notif.");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200651 if (ls->closed == 1) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100652 DEBUG("session already closed - handle no notification");
653 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200654 pthread_mutex_unlock(&ls->lock);
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100655 DEBUG("Close notification client");
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200656 return -1;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200657 }
Tomas Cejka654f84e2013-04-19 11:55:01 +0200658 if (ls->ntfc_subscribed != 0) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100659 DEBUG("notification: already subscribed");
660 DEBUG("unlock private lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200661 pthread_mutex_unlock(&ls->lock);
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200662 /* do not close client, only do not subscribe again */
Tomas Cejka654f84e2013-04-19 11:55:01 +0200663 return 0;
664 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100665 DEBUG("notification: prepare to subscribe stream");
666 DEBUG("unlock session lock");
Tomas Cejka47387fd2013-06-10 20:37:46 +0200667 pthread_mutex_unlock(&ls->lock);
668
669 /* notif_subscribe locks on its own */
Tomas Cejkabdedcd32013-06-09 11:54:53 +0200670 return notif_subscribe(ls, pss->session_key, (time_t) start, (time_t) stop);
Tomas Cejkaba21b382013-04-13 02:37:32 +0200671 }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100672 if (len < 6)
673 break;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100674 break;
675 /*
676 * this just demonstrates how to use the protocol filter. If you won't
677 * study and reject connections based on header content, you don't need
678 * to handle this callback
679 */
680
681 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
682 //dump_handshake_info(wsi);
683 /* you could return non-zero here and kill the connection */
684 break;
Tomas Cejka866f2282014-09-18 15:20:26 +0200685 //gives segfault :-(
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100686 //case LWS_CALLBACK_CLOSED:
687 // if (pss->session_key != NULL) {
688 // free(pss->session_key);
689 // }
690 // if (pss != NULL) {
691 // free(pss);
692 // }
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100693
694 default:
695 break;
696 }
697
698 return 0;
699}
700
701/* list of supported protocols and callbacks */
702
703static struct libwebsocket_protocols protocols[] = {
704 /* first protocol must always be HTTP handler */
705
706 {
707 "http-only", /* name */
708 callback_http, /* callback */
709 sizeof (struct per_session_data__http), /* per_session_data_size */
710 0, /* max frame size / rx buffer */
711 },
712 {
713 "notification-protocol",
714 callback_notification,
Tomas Cejkaba21b382013-04-13 02:37:32 +0200715 sizeof(struct per_session_data__notif_client),
Tomas Cejka47387fd2013-06-10 20:37:46 +0200716 4000,
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100717 },
718 { NULL, NULL, 0, 0 } /* terminator */
719};
720
721
Tomas Cejkaba21b382013-04-13 02:37:32 +0200722/**
723 * initialization of notification module
724 */
Tomas Cejka47387fd2013-06-10 20:37:46 +0200725int notification_init(apr_pool_t * pool, server_rec * server)
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100726{
Tomas Cejkaba21b382013-04-13 02:37:32 +0200727 //char cert_path[1024];
728 //char key_path[1024];
729 //int use_ssl = 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100730 struct lws_context_creation_info info;
731 int opts = 0;
Tomas Cejkaba21b382013-04-13 02:37:32 +0200732 //char interface_name[128] = "";
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100733 const char *iface = NULL;
734 int debug_level = 7;
735
736 memset(&info, 0, sizeof info);
737 info.port = NOTIFICATION_SERVER_PORT;
738
739 /* tell the library what debug level to emit and to send it to syslog */
740 lws_set_log_level(debug_level, lwsl_emit_syslog);
741
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100742 DEBUG("Initialization of libwebsocket");
Tomas Cejka15c56302013-05-30 01:11:30 +0200743 //lwsl_notice("libwebsockets test server - "
744 // "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> - "
745 // "licensed under LGPL2.1\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100746 max_poll_elements = getdtablesize();
747 pollfds = malloc(max_poll_elements * sizeof (struct pollfd));
748 fd_lookup = malloc(max_poll_elements * sizeof (int));
749 if (pollfds == NULL || fd_lookup == NULL) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200750 ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100751 return -1;
752 }
753
754 info.iface = iface;
755 info.protocols = protocols;
756
757 //snprintf(cert_path, sizeof(cert_path), "%s/libwebsockets-test-server.pem", resource_path);
758 //snprintf(key_path, sizeof(cert_path), "%s/libwebsockets-test-server.key.pem", resource_path);
759
760 //info.ssl_cert_filepath = cert_path;
761 //info.ssl_private_key_filepath = key_path;
762
763 info.gid = -1;
764 info.uid = -1;
765 info.options = opts;
766
767 /* create server */
768 context = libwebsocket_create_context(&info);
769 if (context == NULL) {
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100770 DEBUG("libwebsocket init failed.");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100771 return -1;
772 }
Tomas Cejka15c56302013-05-30 01:11:30 +0200773
Tomas Cejka15c56302013-05-30 01:11:30 +0200774 if (pthread_key_create(&thread_key, NULL) != 0) {
Tomas Cejkacf44e522015-04-24 17:29:21 +0200775 ERROR("notifications: pthread_key_create failed");
Tomas Cejka15c56302013-05-30 01:11:30 +0200776 }
Tomas Cejkaba21b382013-04-13 02:37:32 +0200777 return 0;
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100778}
779
780void notification_close()
781{
Tomas Cejka98ed6cc2015-04-27 23:35:18 +0200782 if (context) {
783 libwebsocket_context_destroy(context);
784 }
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100785 free(pollfds);
786 free(fd_lookup);
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100787
Tomas Cejkaef531ee2013-11-12 16:07:00 +0100788 DEBUG("libwebsockets-test-server exited cleanly\n");
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100789}
790
Tomas Cejkaba21b382013-04-13 02:37:32 +0200791
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100792/**
793 * \brief send notification if any
794 * \return < 0 on error
795 */
796int notification_handle()
797{
798 static struct timeval tv;
799 static unsigned int olds = 0;
800 int n = 0;
801
802 gettimeofday(&tv, NULL);
803
804 /*
805 * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
806 * live websocket connection using the DUMB_INCREMENT protocol,
807 * as soon as it can take more packets (usually immediately)
808 */
809
810 if (((unsigned int)tv.tv_sec - olds) > 0) {
811 libwebsocket_callback_on_writable_all_protocol(&protocols[PROTOCOL_NOTIFICATION]);
812 olds = tv.tv_sec;
813 }
814
815
816 /*
817 * this represents an existing server's single poll action
818 * which also includes libwebsocket sockets
819 */
820
821 n = poll(pollfds, count_pollfds, 50);
822 if (n < 0)
823 return n;
824
825
826 if (n) {
827 for (n = 0; n < count_pollfds; n++) {
828 if (pollfds[n].revents) {
829 /*
830 * returns immediately if the fd does not
831 * match anything under libwebsockets
832 * control
833 */
834 if (libwebsocket_service_fd(context, &pollfds[n]) < 0) {
835 return 1;
836 }
837 }
838 }
839 }
840 return 0;
841}
842
843#endif
844
845
846#ifndef WITH_NOTIFICATIONS
847#ifdef TEST_NOTIFICATION_SERVER
848int main(int argc, char **argv)
849{
Tomas Cejkad340dbf2013-03-24 20:36:57 +0100850 if (notification_init(NULL, NULL) == -1) {
851 fprintf(stderr, "Error during initialization\n");
852 return 1;
853 }
854 while (!force_exit) {
855 notification_handle();
856 }
857 notification_close();
858}
859#endif
860#endif