Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets-test-server - libwebsockets test implementation |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <getopt.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/stat.h> |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 28 | #include <sys/queue.h> |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 29 | #include <fcntl.h> |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 30 | #include <pthread.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 31 | #include <errno.h> |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 32 | #include <nc_client.h> |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 33 | #include <libwebsockets.h> |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 34 | |
Michal Vasko | a53ef88 | 2015-11-24 11:02:01 +0100 | [diff] [blame] | 35 | #include "notification_server.h" |
| 36 | #include "netopeerguid.h" |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 37 | #include "../config.h" |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 38 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 39 | #ifdef TEST_NOTIFICATION_SERVER |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 40 | static int force_exit = 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #if defined(TEST_NOTIFICATION_SERVER) || defined(WITH_NOTIFICATIONS) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 44 | static int max_poll_elements; |
| 45 | |
| 46 | static struct pollfd *pollfds; |
| 47 | static int *fd_lookup; |
| 48 | static int count_pollfds; |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 49 | static struct lws_context *context = NULL; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 50 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 51 | extern struct session_with_mutex *netconf_sessions_list; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 52 | static pthread_key_t thread_key; |
| 53 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 54 | enum demo_protocols { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 55 | /* always first */ |
| 56 | PROTOCOL_HTTP = 0, |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 57 | PROTOCOL_NOTIFICATION, |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 58 | /* always last */ |
| 59 | DEMO_PROTOCOL_COUNT |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 62 | #define SERVER_CERT_DIR "." |
| 63 | #define SERVER_CERT "server.crt" |
| 64 | #define SERVER_KEY "server.key" |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * We take a strict whitelist approach to stop ../ attacks |
| 68 | */ |
| 69 | |
| 70 | struct serveable { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 71 | const char *urlpath; |
| 72 | const char *mimetype; |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 73 | }; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 74 | |
| 75 | static const struct serveable whitelist[] = { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 76 | { "/favicon.ico", "image/x-icon" }, |
| 77 | { "/libwebsockets.org-logo.png", "image/png" }, |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 78 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 79 | /* last one is the default served if no match */ |
| 80 | { "/test.html", "text/html" }, |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct per_session_data__http { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 84 | int fd; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 87 | static void |
| 88 | debug_print_clb(const char *func, enum lws_callback_reasons reason) |
| 89 | { |
| 90 | switch (reason) { |
| 91 | case LWS_CALLBACK_ESTABLISHED: |
| 92 | DEBUG("%s: LWS_CALLBACK_ESTABLISHED", func); |
| 93 | break; |
| 94 | case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: |
| 95 | DEBUG("%s: LWS_CALLBACK_CLIENT_CONNECTION_ERROR", func); |
| 96 | break; |
| 97 | case LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: |
| 98 | DEBUG("%s: LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH", func); |
| 99 | break; |
| 100 | case LWS_CALLBACK_CLIENT_ESTABLISHED: |
| 101 | DEBUG("%s: LWS_CALLBACK_CLIENT_ESTABLISHED", func); |
| 102 | break; |
| 103 | case LWS_CALLBACK_CLOSED: |
| 104 | DEBUG("%s: LWS_CALLBACK_CLOSED", func); |
| 105 | break; |
| 106 | case LWS_CALLBACK_RECEIVE: |
| 107 | DEBUG("%s: LWS_CALLBACK_RECEIVE", func); |
| 108 | break; |
| 109 | case LWS_CALLBACK_RECEIVE_PONG: |
| 110 | DEBUG("%s: LWS_CALLBACK_RECEIVE_PONG", func); |
| 111 | break; |
| 112 | case LWS_CALLBACK_CLIENT_RECEIVE_PONG: |
| 113 | DEBUG("%s: LWS_CALLBACK_CLIENT_RECEIVE_PONG", func); |
| 114 | break; |
| 115 | case LWS_CALLBACK_CLIENT_RECEIVE: |
| 116 | DEBUG("%s: LWS_CALLBACK_CLIENT_RECEIVE", func); |
| 117 | break; |
| 118 | case LWS_CALLBACK_CLIENT_WRITEABLE: |
| 119 | DEBUG("%s: LWS_CALLBACK_CLIENT_WRITEABLE", func); |
| 120 | break; |
| 121 | case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: |
| 122 | DEBUG("%s: LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP", func); |
| 123 | break; |
| 124 | case LWS_CALLBACK_CLOSED_CLIENT_HTTP: |
| 125 | DEBUG("%s: LWS_CALLBACK_CLOSED_CLIENT_HTTP", func); |
| 126 | break; |
| 127 | case LWS_CALLBACK_RECEIVE_CLIENT_HTTP: |
| 128 | DEBUG("%s: LWS_CALLBACK_RECEIVE_CLIENT_HTTP", func); |
| 129 | break; |
| 130 | case LWS_CALLBACK_COMPLETED_CLIENT_HTTP: |
| 131 | DEBUG("%s: LWS_CALLBACK_COMPLETED_CLIENT_HTTP", func); |
| 132 | break; |
| 133 | case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ: |
| 134 | DEBUG("%s: LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ", func); |
| 135 | break; |
| 136 | case LWS_CALLBACK_HTTP: |
| 137 | DEBUG("%s: LWS_CALLBACK_HTTP", func); |
| 138 | break; |
| 139 | case LWS_CALLBACK_HTTP_BODY: |
| 140 | DEBUG("%s: LWS_CALLBACK_HTTP_BODY", func); |
| 141 | break; |
| 142 | case LWS_CALLBACK_HTTP_BODY_COMPLETION: |
| 143 | DEBUG("%s: LWS_CALLBACK_HTTP_BODY_COMPLETION", func); |
| 144 | break; |
| 145 | case LWS_CALLBACK_FILTER_HTTP_CONNECTION: |
| 146 | DEBUG("%s: LWS_CALLBACK_FILTER_HTTP_CONNECTION", func); |
| 147 | break; |
| 148 | case LWS_CALLBACK_CLOSED_HTTP: |
| 149 | DEBUG("%s: LWS_CALLBACK_CLOSED_HTTP", func); |
| 150 | break; |
| 151 | case LWS_CALLBACK_HTTP_WRITEABLE: |
| 152 | DEBUG("%s: LWS_CALLBACK_HTTP_WRITEABLE", func); |
| 153 | break; |
| 154 | case LWS_CALLBACK_HTTP_FILE_COMPLETION: |
| 155 | DEBUG("%s: LWS_CALLBACK_HTTP_FILE_COMPLETION", func); |
| 156 | break; |
| 157 | case LWS_CALLBACK_SERVER_WRITEABLE: |
| 158 | DEBUG("%s: LWS_CALLBACK_SERVER_WRITEABLE", func); |
| 159 | break; |
| 160 | case LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED: |
| 161 | DEBUG("%s: LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED", func); |
| 162 | break; |
| 163 | case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: |
| 164 | DEBUG("%s: LWS_CALLBACK_FILTER_NETWORK_CONNECTION", func); |
| 165 | break; |
| 166 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
| 167 | DEBUG("%s: LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION", func); |
| 168 | break; |
| 169 | case LWS_CALLBACK_WSI_CREATE: |
| 170 | DEBUG("%s: LWS_CALLBACK_WSI_CREATE", func); |
| 171 | break; |
| 172 | case LWS_CALLBACK_WSI_DESTROY: |
| 173 | DEBUG("%s: LWS_CALLBACK_WSI_DESTROY", func); |
| 174 | break; |
| 175 | case LWS_CALLBACK_GET_THREAD_ID: |
| 176 | DEBUG("%s: LWS_CALLBACK_GET_THREAD_ID", func); |
| 177 | break; |
| 178 | case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: |
| 179 | DEBUG("%s: LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS", func); |
| 180 | break; |
| 181 | case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: |
| 182 | DEBUG("%s: LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS", func); |
| 183 | break; |
| 184 | case LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: |
| 185 | DEBUG("%s: LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION", func); |
| 186 | break; |
| 187 | case LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY: |
| 188 | DEBUG("%s: LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY", func); |
| 189 | break; |
| 190 | case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: |
| 191 | DEBUG("%s: LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER", func); |
| 192 | break; |
| 193 | case LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: |
| 194 | DEBUG("%s: LWS_CALLBACK_CONFIRM_EXTENSION_OKAY", func); |
| 195 | break; |
| 196 | case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: |
| 197 | DEBUG("%s: LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED", func); |
| 198 | break; |
| 199 | case LWS_CALLBACK_PROTOCOL_INIT: |
| 200 | DEBUG("%s: LWS_CALLBACK_PROTOCOL_INIT", func); |
| 201 | break; |
| 202 | case LWS_CALLBACK_PROTOCOL_DESTROY: |
| 203 | DEBUG("%s: LWS_CALLBACK_PROTOCOL_DESTROY", func); |
| 204 | break; |
| 205 | case LWS_CALLBACK_ADD_POLL_FD: |
| 206 | DEBUG("%s: LWS_CALLBACK_ADD_POLL_FD", func); |
| 207 | break; |
| 208 | case LWS_CALLBACK_DEL_POLL_FD: |
| 209 | DEBUG("%s: LWS_CALLBACK_DEL_POLL_FD", func); |
| 210 | break; |
| 211 | case LWS_CALLBACK_CHANGE_MODE_POLL_FD: |
| 212 | DEBUG("%s: LWS_CALLBACK_CHANGE_MODE_POLL_FD", func); |
| 213 | break; |
| 214 | case LWS_CALLBACK_LOCK_POLL: |
| 215 | DEBUG("%s: LWS_CALLBACK_LOCK_POLL", func); |
| 216 | break; |
| 217 | case LWS_CALLBACK_UNLOCK_POLL: |
| 218 | DEBUG("%s: LWS_CALLBACK_UNLOCK_POLL", func); |
| 219 | break; |
| 220 | case LWS_CALLBACK_USER: |
| 221 | DEBUG("%s: LWS_CALLBACK_USER", func); |
| 222 | break; |
| 223 | case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: |
| 224 | DEBUG("%s: LWS_CALLBACK_WS_PEER_INITIATED_CLOSE", func); |
| 225 | break; |
| 226 | case LWS_CALLBACK_WS_EXT_DEFAULTS: |
| 227 | DEBUG("%s: LWS_CALLBACK_WS_EXT_DEFAULTS", func); |
| 228 | break; |
| 229 | case LWS_CALLBACK_CGI: |
| 230 | DEBUG("%s: LWS_CALLBACK_CGI", func); |
| 231 | break; |
| 232 | case LWS_CALLBACK_CGI_TERMINATED: |
| 233 | DEBUG("%s: LWS_CALLBACK_CGI_TERMINATED", func); |
| 234 | break; |
| 235 | case LWS_CALLBACK_CGI_STDIN_DATA: |
| 236 | DEBUG("%s: LWS_CALLBACK_CGI_STDIN_DATA", func); |
| 237 | break; |
| 238 | case LWS_CALLBACK_CGI_STDIN_COMPLETED: |
| 239 | DEBUG("%s: LWS_CALLBACK_CGI_STDIN_COMPLETED", func); |
| 240 | break; |
| 241 | } |
| 242 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 243 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 244 | static int |
| 245 | callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t UNUSED(len)) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 246 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 247 | char client_name[128]; |
| 248 | char client_ip[128]; |
| 249 | char buf[256]; |
| 250 | int n, m; |
| 251 | static unsigned char buffer[4096]; |
| 252 | struct per_session_data__http *pss = (struct per_session_data__http *)user; |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 253 | struct lws_pollargs *pa = (struct lws_pollargs *)in; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 254 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 255 | debug_print_clb(__func__, reason); |
| 256 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 257 | switch (reason) { |
| 258 | case LWS_CALLBACK_HTTP: |
| 259 | for (n = 0; n < (signed) (sizeof(whitelist) / sizeof(whitelist[0]) - 1); n++) |
| 260 | if (in && strcmp((const char *)in, whitelist[n].urlpath) == 0) |
| 261 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 262 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 263 | sprintf(buf, SERVER_CERT_DIR "%s", whitelist[n].urlpath); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 264 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 265 | if (lws_serve_http_file(wsi, buf, whitelist[n].mimetype, NULL, 0)) |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 266 | return -1; /* through completion or error, close the socket */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 267 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 268 | /* |
| 269 | * notice that the sending of the file completes asynchronously, |
| 270 | * we'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION callback when |
| 271 | * it's done |
| 272 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 273 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 274 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 275 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 276 | case LWS_CALLBACK_HTTP_FILE_COMPLETION: |
| 277 | // lwsl_info("LWS_CALLBACK_HTTP_FILE_COMPLETION seen\n"); |
| 278 | /* kill the connection after we sent one file */ |
| 279 | return -1; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 280 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 281 | case LWS_CALLBACK_HTTP_WRITEABLE: |
| 282 | /* |
| 283 | * we can send more of whatever it is we were sending |
| 284 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 285 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 286 | do { |
| 287 | n = read(pss->fd, buffer, sizeof buffer); |
| 288 | /* problem reading, close conn */ |
| 289 | if (n < 0) |
| 290 | goto bail; |
| 291 | /* sent it all, close conn */ |
| 292 | if (n == 0) |
| 293 | goto bail; |
| 294 | /* |
| 295 | * because it's HTTP and not websocket, don't need to take |
| 296 | * care about pre and postamble |
| 297 | */ |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 298 | m = lws_write(wsi, buffer, n, LWS_WRITE_HTTP); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 299 | if (m < 0) |
| 300 | /* write failed, close conn */ |
| 301 | goto bail; |
| 302 | if (m != n) |
| 303 | /* partial write, adjust */ |
| 304 | lseek(pss->fd, m - n, SEEK_CUR); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 305 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 306 | } while (!lws_send_pipe_choked(wsi)); |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 307 | lws_callback_on_writable(wsi); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 308 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 309 | |
| 310 | bail: |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 311 | close(pss->fd); |
| 312 | return -1; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 313 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 314 | case LWS_CALLBACK_LOCK_POLL: |
| 315 | /* |
| 316 | * lock mutex to protect pollfd state |
| 317 | * called before any other POLL related callback |
| 318 | */ |
| 319 | break; |
Tomas Cejka | 11d5d06 | 2014-07-15 13:13:52 +0200 | [diff] [blame] | 320 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 321 | case LWS_CALLBACK_UNLOCK_POLL: |
| 322 | /* |
| 323 | * unlock mutex to protect pollfd state when |
| 324 | * called after any other POLL related callback |
| 325 | */ |
| 326 | break; |
Tomas Cejka | 11d5d06 | 2014-07-15 13:13:52 +0200 | [diff] [blame] | 327 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 328 | /* |
| 329 | * callback for confirming to continue with client IP appear in |
| 330 | * protocol 0 callback since no websocket protocol has been agreed |
| 331 | * yet. You can just ignore this if you won't filter on client IP |
| 332 | * since the default uhandled callback return is 0 meaning let the |
| 333 | * connection continue. |
| 334 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 335 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 336 | case LWS_CALLBACK_FILTER_NETWORK_CONNECTION: |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 337 | lws_get_peer_addresses(wsi, (int)(long)in, client_name, |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 338 | sizeof(client_name), client_ip, sizeof(client_ip)); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 339 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 340 | //fprintf(stderr, "Received network connect from %s (%s)\n", client_name, client_ip); |
| 341 | /* if we returned non-zero from here, we kill the connection */ |
| 342 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 343 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 344 | /* |
| 345 | * callbacks for managing the external poll() array appear in |
| 346 | * protocol 0 callback |
| 347 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 348 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 349 | case LWS_CALLBACK_ADD_POLL_FD: |
| 350 | if (count_pollfds >= max_poll_elements) { |
| 351 | lwsl_err("LWS_CALLBACK_ADD_POLL_FD: too many sockets to track\n"); |
| 352 | return 1; |
| 353 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 354 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 355 | fd_lookup[pa->fd] = count_pollfds; |
| 356 | pollfds[count_pollfds].fd = pa->fd; |
| 357 | pollfds[count_pollfds].events = pa->events; |
| 358 | pollfds[count_pollfds++].revents = 0; |
| 359 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 360 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 361 | case LWS_CALLBACK_DEL_POLL_FD: |
| 362 | if (!--count_pollfds) |
| 363 | break; |
| 364 | m = fd_lookup[pa->fd]; |
| 365 | /* have the last guy take up the vacant slot */ |
| 366 | pollfds[m] = pollfds[count_pollfds]; |
| 367 | fd_lookup[pollfds[count_pollfds].fd] = m; |
| 368 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 369 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 370 | case LWS_CALLBACK_CHANGE_MODE_POLL_FD: |
| 371 | pollfds[fd_lookup[pa->fd]].events = pa->events; |
| 372 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 373 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 374 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 375 | default: |
| 376 | break; |
| 377 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 378 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 379 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 382 | /* |
| 383 | * one of these is auto-created for each connection and a pointer to the |
| 384 | * appropriate instance is passed to the callback in the user parameter |
| 385 | * |
| 386 | * for this example protocol we use it to individualize the count for each |
| 387 | * connection. |
| 388 | */ |
| 389 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 390 | struct per_session_data__notif_client { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 391 | int number; |
| 392 | char *session_id; |
| 393 | struct nc_session *session; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 394 | }; |
| 395 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 396 | static struct session_with_mutex * |
| 397 | get_ncsession_from_sid(const char *session_id) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 398 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 399 | struct session_with_mutex *locked_session = NULL; |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 400 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 401 | if (session_id == NULL) { |
| 402 | return (NULL); |
| 403 | } |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 404 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 405 | for (locked_session = netconf_sessions_list; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 406 | locked_session && (nc_session_get_id(locked_session->session) == (unsigned)atoi(session_id)); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 407 | locked_session = locked_session->next); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 408 | return locked_session; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* rpc parameter is freed after the function call */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 412 | static int send_recv_process(struct nc_session *session, const char* UNUSED(operation), struct nc_rpc* rpc) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 413 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 414 | struct nc_reply *reply = NULL; |
| 415 | char *data = NULL; |
| 416 | int ret = EXIT_SUCCESS; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 417 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 418 | /* send the request and get the reply */ |
| 419 | switch (netconf_send_recv_timed(session, rpc, 50000, 0, &reply)) { |
| 420 | case NC_MSG_ERROR: |
| 421 | if (nc_session_get_status(session) != NC_STATUS_RUNNING) { |
| 422 | ERROR("notifications: receiving rpc-reply failed."); |
| 423 | //cmd_disconnect(NULL); |
| 424 | ret = EXIT_FAILURE; |
| 425 | break; |
| 426 | } |
| 427 | ERROR("notifications: Unknown error occurred."); |
| 428 | ret = EXIT_FAILURE; |
| 429 | break; |
| 430 | case NC_MSG_NONE: |
| 431 | /* error occurred, but processed by callback */ |
| 432 | break; |
| 433 | case NC_MSG_REPLY: |
| 434 | switch (reply->type) { |
| 435 | case NC_RPL_OK: |
| 436 | break; |
| 437 | case NC_RPL_DATA: |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 438 | lyd_print_mem(&data, ((struct nc_reply_data *)reply)->data, LYD_JSON, 0); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 439 | DEBUG("notifications: recv: %s.", data); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 440 | free(data); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 441 | break; |
| 442 | case NC_RPL_ERROR: |
| 443 | /* wtf, you shouldn't be here !?!? */ |
| 444 | DEBUG("notifications: operation failed, but rpc-error was not processed."); |
| 445 | ret = EXIT_FAILURE; |
| 446 | break; |
| 447 | default: |
| 448 | DEBUG("notifications: unexpected operation result."); |
| 449 | ret = EXIT_FAILURE; |
| 450 | break; |
| 451 | } |
| 452 | break; |
| 453 | default: |
| 454 | DEBUG("notifications: Unknown error occurred."); |
| 455 | ret = EXIT_FAILURE; |
| 456 | break; |
| 457 | } |
| 458 | nc_rpc_free(rpc); |
| 459 | nc_reply_free(reply); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 460 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 461 | return (ret); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /** |
| 465 | * \brief Callback to store incoming notification |
| 466 | * \param [in] eventtime - time when notification occured |
| 467 | * \param [in] content - content of notification |
| 468 | */ |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 469 | static void notification_fileprint(struct nc_session *session, const struct nc_notif *notif) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 470 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 471 | time_t eventtime; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 472 | struct session_with_mutex *target_session = NULL; |
| 473 | notification_t *ntf = NULL; |
| 474 | const char *session_id = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 475 | char *content; |
| 476 | (void)session; |
| 477 | |
| 478 | eventtime = nc_datetime2time(notif->datetime); |
| 479 | lyd_print_mem(&content, notif->tree, LYD_JSON, 0); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 480 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 481 | DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 482 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 483 | session_id = pthread_getspecific(thread_key); |
| 484 | DEBUG("notification: fileprint getspecific (%s)", session_id); |
| 485 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
| 486 | ERROR("notifications: Error while locking rwlock"); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 487 | free(content); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | DEBUG("Get session with mutex from key %s.", session_id); |
| 491 | target_session = get_ncsession_from_sid(session_id); |
| 492 | if (target_session == NULL) { |
| 493 | ERROR("notifications: no session found last_session_key (%s)", session_id); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 494 | free(content); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 495 | goto unlock_glob; |
| 496 | } |
| 497 | if (pthread_mutex_lock(&target_session->lock) != 0) { |
| 498 | ERROR("notifications: Error while locking rwlock"); |
| 499 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 500 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 501 | DEBUG("notification: ready to push to notifications queue"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 502 | if (target_session->notif_count < NOTIFICATION_QUEUE_SIZE) { |
| 503 | ++target_session->notif_count; |
| 504 | target_session->notifications = realloc(target_session->notifications, |
| 505 | target_session->notif_count * sizeof *target_session->notifications); |
| 506 | if (target_session->notifications) { |
| 507 | ntf = target_session->notifications + target_session->notif_count - 1; |
| 508 | } |
| 509 | } |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 510 | if (ntf == NULL) { |
| 511 | ERROR("notifications: Failed to allocate element "); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 512 | free(content); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 513 | goto unlock_all; |
| 514 | } |
| 515 | ntf->eventtime = eventtime; |
| 516 | ntf->content = content; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 517 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 518 | DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 519 | |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 520 | unlock_all: |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 521 | if (pthread_mutex_unlock(&target_session->lock) != 0) { |
| 522 | ERROR("notifications: Error while unlocking rwlock"); |
| 523 | } |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 524 | unlock_glob: |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 525 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 526 | ERROR("notifications: Error while locking rwlock"); |
| 527 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 528 | } |
| 529 | |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 530 | int notif_subscribe(struct session_with_mutex *locked_session, const char *session_id, time_t start_time, time_t stop_time) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 531 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 532 | time_t start = -1; |
| 533 | time_t stop = -1; |
| 534 | char *stream = NULL; |
| 535 | struct nc_rpc *rpc = NULL; |
| 536 | struct nc_session *session; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 537 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 538 | DEBUG("notif_subscribe"); |
| 539 | if (locked_session == NULL) { |
| 540 | DEBUG("notifications: no locked_session was given."); |
| 541 | /* Close notification client */ |
| 542 | return -1; |
| 543 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 544 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 545 | pthread_mutex_lock(&locked_session->lock); |
| 546 | session = locked_session->session; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 547 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 548 | start = time(NULL) + start_time; |
| 549 | stop = time(NULL) + stop_time; |
| 550 | start = 0; |
| 551 | stop = 0; |
| 552 | DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 553 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 554 | if (session == NULL) { |
| 555 | ERROR("notifications: NETCONF session not established."); |
| 556 | goto operation_failed; |
| 557 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 558 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 559 | /* check times */ |
| 560 | if (start != -1 && stop != -1 && start > stop) { |
| 561 | ERROR("notifications: Subscription start time must be lower than the end time."); |
| 562 | goto operation_failed; |
| 563 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 564 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 565 | DEBUG("Prepare to execute subscription."); |
| 566 | /* create requests */ |
| 567 | rpc = nc_rpc_subscribe(stream, NULL, (start_time == -1) ? NULL : nc_time2datetime(start, NULL), |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 568 | (stop_time == 0) ? NULL : nc_time2datetime(stop, NULL), NC_PARAMTYPE_CONST); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 569 | if (rpc == NULL) { |
| 570 | ERROR("notifications: creating an rpc request failed."); |
| 571 | goto operation_failed; |
| 572 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 573 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 574 | DEBUG("Send NC subscribe."); |
| 575 | create_err_reply_p(); |
| 576 | if (send_recv_process(session, "subscribe", rpc) != 0) { |
| 577 | ERROR("Subscription RPC failed."); |
| 578 | goto operation_failed; |
| 579 | } |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 580 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 581 | GETSPEC_ERR_REPLY |
| 582 | if (err_reply != NULL) { |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 583 | free_err_reply(); |
Tomas Cejka | cf44e52 | 2015-04-24 17:29:21 +0200 | [diff] [blame] | 584 | ERROR("RPC-Error received and cleaned, because we can't send it anywhere."); |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 585 | goto operation_failed; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 586 | } |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 587 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 588 | rpc = NULL; /* just note that rpc is already freed by send_recv_process() */ |
| 589 | locked_session->ntfc_subscribed = 1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 590 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 591 | DEBUG("notifications: creating libnetconf notification thread (%s).", session_id); |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 592 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 593 | pthread_mutex_unlock(&locked_session->lock); |
| 594 | |
| 595 | /* store hash identification of netconf session for notifications printing callback */ |
| 596 | if (pthread_setspecific(thread_key, session_id) != 0) { |
| 597 | ERROR("notifications: cannot set thread-specific hash value."); |
| 598 | } |
| 599 | |
| 600 | DEBUG("Create notification_thread."); |
| 601 | nc_recv_notif_dispatch(session, notification_fileprint); |
| 602 | DEBUG("Subscription finished."); |
| 603 | return 0; |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 604 | |
| 605 | operation_failed: |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 606 | pthread_mutex_unlock(&locked_session->lock); |
| 607 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 608 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 609 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 610 | static int |
| 611 | callback_notification(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 612 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 613 | int n = 0, m = 0, i; |
| 614 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING]; |
| 615 | unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; |
| 616 | struct per_session_data__notif_client *pss = (struct per_session_data__notif_client *)user; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 617 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 618 | debug_print_clb(__func__, reason); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 619 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 620 | switch (reason) { |
| 621 | case LWS_CALLBACK_ESTABLISHED: |
| 622 | DEBUG("notification client connected."); |
| 623 | break; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 624 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 625 | case LWS_CALLBACK_SERVER_WRITEABLE: |
| 626 | if (pss->session_id == NULL) { |
| 627 | return 0; |
| 628 | } |
| 629 | //DEBUG("Callback server writeable."); |
| 630 | //DEBUG("lock session lock."); |
| 631 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
| 632 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 633 | return -1; |
| 634 | } |
| 635 | //DEBUG("get session_with_mutex for %s.", pss->session_id); |
| 636 | struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id); |
| 637 | if (ls == NULL) { |
| 638 | DEBUG("notification: session not found"); |
| 639 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 640 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 641 | return -1; |
| 642 | } |
| 643 | return -1; |
| 644 | } |
| 645 | pthread_mutex_lock(&ls->lock); |
| 646 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 647 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 648 | } |
| 649 | |
| 650 | //DEBUG("check for closed session."); |
| 651 | if (ls->closed == 1) { |
| 652 | DEBUG("unlock session key."); |
| 653 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 654 | DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno)); |
| 655 | return -1; |
| 656 | } |
| 657 | return -1; |
| 658 | } |
| 659 | //DEBUG("lock private lock."); |
| 660 | notification_t *notif = NULL; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 661 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 662 | if (ls->notif_count) { |
| 663 | DEBUG("notification: POP notifications for session"); |
Michal Vasko | 43d94a6 | 2015-11-05 10:59:32 +0100 | [diff] [blame] | 664 | for (i = ls->notif_count; i; --i) { |
| 665 | notif = ls->notifications + i - 1; |
Tomas Cejka | 7328693 | 2013-05-27 22:54:35 +0200 | [diff] [blame] | 666 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 667 | n = 0; |
| 668 | pthread_mutex_lock(&json_lock); |
| 669 | json_object *notif_json = json_object_new_object(); |
| 670 | json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime)); |
| 671 | json_object_object_add(notif_json, "content", json_object_new_string(notif->content)); |
| 672 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 673 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 674 | const char *msgtext = json_object_to_json_string(notif_json); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 675 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 676 | //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t); |
| 677 | n = sprintf((char *)p, "%s", msgtext); |
| 678 | DEBUG("ws send %dB in %lu", n, sizeof(buf)); |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 679 | m = lws_write(wsi, p, n, LWS_WRITE_TEXT); |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 680 | if (lws_send_pipe_choked(wsi)) { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 681 | lws_callback_on_writable(wsi); |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 682 | break; |
| 683 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 684 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 685 | pthread_mutex_lock(&json_lock); |
| 686 | json_object_put(notif_json); |
| 687 | pthread_mutex_unlock(&json_lock); |
| 688 | free(notif->content); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 689 | } |
Michal Vasko | 43d94a6 | 2015-11-05 10:59:32 +0100 | [diff] [blame] | 690 | ls->notif_count = 0; |
| 691 | free(ls->notifications); |
| 692 | ls->notifications = NULL; |
| 693 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 694 | DEBUG("notification: POP notifications done"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 695 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 696 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 697 | //DEBUG("unlock private lock"); |
| 698 | if (pthread_mutex_unlock(&ls->lock) != 0) { |
| 699 | DEBUG("notification: cannot unlock session"); |
| 700 | } |
| 701 | //DEBUG("unlock session lock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 702 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 703 | if (m < n) { |
| 704 | DEBUG("ERROR %d writing to di socket.", n); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 705 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 706 | return -1; |
| 707 | } |
| 708 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 709 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 710 | case LWS_CALLBACK_RECEIVE: |
| 711 | DEBUG("Callback receive."); |
| 712 | DEBUG("received: (%s)", (char *)in); |
| 713 | if (pss->session_id == NULL) { |
| 714 | char *sid_end; |
| 715 | int start = -1; |
| 716 | time_t stop = time(NULL) + 30; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 717 | |
Michal Vasko | 2bff5fb | 2015-11-05 10:23:02 +0100 | [diff] [blame] | 718 | sid_end = strchr(in, ' '); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 719 | pss->session_id = strndup(in, sid_end - (char *)in); |
Michal Vasko | 2bff5fb | 2015-11-05 10:23:02 +0100 | [diff] [blame] | 720 | |
| 721 | ++sid_end; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 722 | sscanf(sid_end, "%d %d", (int *) &start, (int *) &stop); |
| 723 | DEBUG("notification: SID (%s) from (%s) (%i,%i)", pss->session_id, (char *) in, (int) start, (int) stop); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 724 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 725 | DEBUG("lock session lock"); |
| 726 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
| 727 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 728 | return -1; |
| 729 | } |
| 730 | DEBUG("get session with ID (%s)", pss->session_id); |
| 731 | struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id); |
| 732 | if (ls == NULL) { |
| 733 | DEBUG("notification: session_id not found (%s)", pss->session_id); |
| 734 | DEBUG("unlock session lock"); |
| 735 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 736 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 737 | } |
| 738 | DEBUG("Close notification client"); |
| 739 | return -1; |
| 740 | } |
| 741 | DEBUG("lock private lock"); |
| 742 | pthread_mutex_lock(&ls->lock); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 743 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 744 | DEBUG("unlock session lock"); |
| 745 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 746 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 747 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 748 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 749 | DEBUG("Found session to subscribe notif."); |
| 750 | if (ls->closed == 1) { |
| 751 | DEBUG("session already closed - handle no notification"); |
| 752 | DEBUG("unlock private lock"); |
| 753 | pthread_mutex_unlock(&ls->lock); |
| 754 | DEBUG("Close notification client"); |
| 755 | return -1; |
| 756 | } |
| 757 | if (ls->ntfc_subscribed != 0) { |
| 758 | DEBUG("notification: already subscribed"); |
| 759 | DEBUG("unlock private lock"); |
| 760 | pthread_mutex_unlock(&ls->lock); |
| 761 | /* do not close client, only do not subscribe again */ |
| 762 | return 0; |
| 763 | } |
| 764 | DEBUG("notification: prepare to subscribe stream"); |
| 765 | DEBUG("unlock session lock"); |
| 766 | pthread_mutex_unlock(&ls->lock); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 767 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 768 | /* notif_subscribe locks on its own */ |
| 769 | return notif_subscribe(ls, pss->session_id, (time_t) start, (time_t) stop); |
| 770 | } |
| 771 | if (len < 6) |
| 772 | break; |
| 773 | break; |
| 774 | /* |
| 775 | * this just demonstrates how to use the protocol filter. If you won't |
| 776 | * study and reject connections based on header content, you don't need |
| 777 | * to handle this callback |
| 778 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 779 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 780 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
| 781 | //dump_handshake_info(wsi); |
| 782 | /* you could return non-zero here and kill the connection */ |
| 783 | break; |
| 784 | //gives segfault :-( |
| 785 | //case LWS_CALLBACK_CLOSED: |
| 786 | // if (pss->session_key != NULL) { |
| 787 | // free(pss->session_key); |
| 788 | // } |
| 789 | // if (pss != NULL) { |
| 790 | // free(pss); |
| 791 | // } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 792 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 793 | default: |
| 794 | break; |
| 795 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 796 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 797 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 800 | static struct lws_protocols protocols[] = { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 801 | /* first protocol must always be HTTP handler */ |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 802 | { |
| 803 | "http-only", /* name */ |
| 804 | callback_http, /* callback */ |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 805 | sizeof(struct per_session_data__http), /* per_session_data_size */ |
| 806 | 0, /* max frame size / rx buffer */ |
| 807 | 1, |
| 808 | NULL |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 809 | }, |
| 810 | { |
| 811 | "notification-protocol", |
| 812 | callback_notification, |
| 813 | sizeof(struct per_session_data__notif_client), |
| 814 | 4000, |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 815 | 2, |
| 816 | NULL |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 817 | }, |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 818 | { NULL, NULL, 0, 0, 0, NULL } /* terminator */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 819 | }; |
| 820 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 821 | /** |
| 822 | * initialization of notification module |
| 823 | */ |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 824 | int |
| 825 | notification_init(void) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 826 | { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 827 | char cert_path[1024], key_path[1024]; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 828 | struct lws_context_creation_info info; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 829 | int debug_level = 7; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 830 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 831 | memset(&info, 0, sizeof info); |
| 832 | info.port = NOTIFICATION_SERVER_PORT; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 833 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 834 | /* tell the library what debug level to emit and to send it to syslog */ |
| 835 | lws_set_log_level(debug_level, lwsl_emit_syslog); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 836 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 837 | DEBUG("Initialization of libwebsocket"); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 838 | max_poll_elements = getdtablesize(); |
| 839 | pollfds = malloc(max_poll_elements * sizeof (struct pollfd)); |
| 840 | fd_lookup = malloc(max_poll_elements * sizeof (int)); |
| 841 | if (pollfds == NULL || fd_lookup == NULL) { |
| 842 | ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements); |
| 843 | return -1; |
| 844 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 845 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 846 | info.iface = NULL; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 847 | info.protocols = protocols; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 848 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 849 | snprintf(cert_path, sizeof(cert_path), SERVER_CERT_DIR "/" SERVER_CERT); |
| 850 | snprintf(key_path, sizeof(key_path), SERVER_CERT_DIR "/" SERVER_KEY); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 851 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 852 | info.ssl_cert_filepath = cert_path; |
| 853 | info.ssl_private_key_filepath = key_path; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 854 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 855 | info.gid = -1; |
| 856 | info.uid = -1; |
Michal Vasko | a41bb80 | 2016-05-30 09:04:55 +0200 | [diff] [blame^] | 857 | info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT | LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 858 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 859 | /* create server */ |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 860 | context = lws_create_context(&info); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 861 | if (context == NULL) { |
| 862 | DEBUG("libwebsocket init failed."); |
| 863 | return -1; |
| 864 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 865 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 866 | if (pthread_key_create(&thread_key, NULL) != 0) { |
| 867 | ERROR("notifications: pthread_key_create failed"); |
| 868 | } |
| 869 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 870 | } |
| 871 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 872 | void |
| 873 | notification_close(void) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 874 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 875 | if (context) { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 876 | lws_context_destroy(context); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 877 | } |
| 878 | free(pollfds); |
| 879 | free(fd_lookup); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 880 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 881 | DEBUG("libwebsockets-test-server exited cleanly\n"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 884 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 885 | /** |
| 886 | * \brief send notification if any |
| 887 | * \return < 0 on error |
| 888 | */ |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 889 | int |
| 890 | notification_handle() |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 891 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 892 | static struct timeval tv; |
| 893 | static unsigned int olds = 0; |
| 894 | int n = 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 895 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 896 | gettimeofday(&tv, NULL); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 897 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 898 | /* |
| 899 | * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every |
| 900 | * live websocket connection using the DUMB_INCREMENT protocol, |
| 901 | * as soon as it can take more packets (usually immediately) |
| 902 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 903 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 904 | if (((unsigned int)tv.tv_sec - olds) > 0) { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 905 | lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_NOTIFICATION]); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 906 | olds = tv.tv_sec; |
| 907 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 908 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 909 | /* |
| 910 | * this represents an existing server's single poll action |
| 911 | * which also includes libwebsocket sockets |
| 912 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 913 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 914 | n = poll(pollfds, count_pollfds, 50); |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 915 | if (n < 0) { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 916 | return n; |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 917 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 918 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 919 | if (n) { |
| 920 | for (n = 0; n < count_pollfds; n++) { |
Michal Vasko | 0fc2d4c | 2016-05-30 09:03:14 +0200 | [diff] [blame] | 921 | if (pollfds[n].revents & (POLLHUP | POLLERR)) { |
| 922 | ERROR("notifications: poll pipe closed or error"); |
| 923 | return -1; |
| 924 | } else if (pollfds[n].revents & (POLLIN | POLLOUT)) { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 925 | /* |
| 926 | * returns immediately if the fd does not |
| 927 | * match anything under libwebsockets |
| 928 | * control |
| 929 | */ |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 930 | if (lws_service_fd(context, &pollfds[n]) < 0) { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 931 | return 1; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | #endif |
| 940 | |
| 941 | |
| 942 | #ifndef WITH_NOTIFICATIONS |
| 943 | #ifdef TEST_NOTIFICATION_SERVER |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 944 | int |
| 945 | main(int argc, char **argv) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 946 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 947 | if (notification_init(NULL, NULL) == -1) { |
| 948 | fprintf(stderr, "Error during initialization\n"); |
| 949 | return 1; |
| 950 | } |
| 951 | while (!force_exit) { |
| 952 | notification_handle(); |
| 953 | } |
| 954 | notification_close(); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 955 | } |
| 956 | #endif |
| 957 | #endif |