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 | a304abf | 2016-09-06 11:25:30 +0200 | [diff] [blame^] | 263 | sprintf(buf, "./%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 | 460dde5 | 2016-05-31 09:00:45 +0200 | [diff] [blame] | 412 | static int |
| 413 | 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] | 414 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 415 | struct nc_reply *reply = NULL; |
| 416 | char *data = NULL; |
| 417 | int ret = EXIT_SUCCESS; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 418 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 419 | /* send the request and get the reply */ |
| 420 | switch (netconf_send_recv_timed(session, rpc, 50000, 0, &reply)) { |
| 421 | case NC_MSG_ERROR: |
| 422 | if (nc_session_get_status(session) != NC_STATUS_RUNNING) { |
| 423 | ERROR("notifications: receiving rpc-reply failed."); |
| 424 | //cmd_disconnect(NULL); |
| 425 | ret = EXIT_FAILURE; |
| 426 | break; |
| 427 | } |
| 428 | ERROR("notifications: Unknown error occurred."); |
| 429 | ret = EXIT_FAILURE; |
| 430 | break; |
| 431 | case NC_MSG_NONE: |
| 432 | /* error occurred, but processed by callback */ |
| 433 | break; |
| 434 | case NC_MSG_REPLY: |
| 435 | switch (reply->type) { |
| 436 | case NC_RPL_OK: |
| 437 | break; |
| 438 | case NC_RPL_DATA: |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 439 | 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] | 440 | DEBUG("notifications: recv: %s.", data); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 441 | free(data); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 442 | break; |
| 443 | case NC_RPL_ERROR: |
| 444 | /* wtf, you shouldn't be here !?!? */ |
| 445 | DEBUG("notifications: operation failed, but rpc-error was not processed."); |
| 446 | ret = EXIT_FAILURE; |
| 447 | break; |
| 448 | default: |
| 449 | DEBUG("notifications: unexpected operation result."); |
| 450 | ret = EXIT_FAILURE; |
| 451 | break; |
| 452 | } |
| 453 | break; |
| 454 | default: |
| 455 | DEBUG("notifications: Unknown error occurred."); |
| 456 | ret = EXIT_FAILURE; |
| 457 | break; |
| 458 | } |
| 459 | nc_rpc_free(rpc); |
| 460 | nc_reply_free(reply); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 461 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 462 | return (ret); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
| 466 | * \brief Callback to store incoming notification |
| 467 | * \param [in] eventtime - time when notification occured |
| 468 | * \param [in] content - content of notification |
| 469 | */ |
Michal Vasko | 460dde5 | 2016-05-31 09:00:45 +0200 | [diff] [blame] | 470 | static void |
| 471 | notification_fileprint(struct nc_session *session, const struct nc_notif *notif) |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 472 | { |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 473 | time_t eventtime; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 474 | struct session_with_mutex *target_session = NULL; |
| 475 | notification_t *ntf = NULL; |
| 476 | const char *session_id = NULL; |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 477 | char *content; |
| 478 | (void)session; |
| 479 | |
| 480 | eventtime = nc_datetime2time(notif->datetime); |
| 481 | lyd_print_mem(&content, notif->tree, LYD_JSON, 0); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 482 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 483 | DEBUG("Accepted notif: %lu %s\n", (unsigned long int) eventtime, content); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 484 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 485 | session_id = pthread_getspecific(thread_key); |
| 486 | DEBUG("notification: fileprint getspecific (%s)", session_id); |
| 487 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
| 488 | ERROR("notifications: Error while locking rwlock"); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 489 | free(content); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | DEBUG("Get session with mutex from key %s.", session_id); |
| 493 | target_session = get_ncsession_from_sid(session_id); |
| 494 | if (target_session == NULL) { |
| 495 | ERROR("notifications: no session found last_session_key (%s)", session_id); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 496 | free(content); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 497 | goto unlock_glob; |
| 498 | } |
| 499 | if (pthread_mutex_lock(&target_session->lock) != 0) { |
| 500 | ERROR("notifications: Error while locking rwlock"); |
| 501 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 502 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 503 | DEBUG("notification: ready to push to notifications queue"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 504 | if (target_session->notif_count < NOTIFICATION_QUEUE_SIZE) { |
| 505 | ++target_session->notif_count; |
| 506 | target_session->notifications = realloc(target_session->notifications, |
| 507 | target_session->notif_count * sizeof *target_session->notifications); |
| 508 | if (target_session->notifications) { |
| 509 | ntf = target_session->notifications + target_session->notif_count - 1; |
| 510 | } |
| 511 | } |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 512 | if (ntf == NULL) { |
| 513 | ERROR("notifications: Failed to allocate element "); |
Michal Vasko | f35ea50 | 2016-02-24 10:44:54 +0100 | [diff] [blame] | 514 | free(content); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 515 | goto unlock_all; |
| 516 | } |
| 517 | ntf->eventtime = eventtime; |
| 518 | ntf->content = content; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 519 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 520 | DEBUG("added notif to queue %u (%s)", (unsigned int) ntf->eventtime, "notification"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 521 | |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 522 | unlock_all: |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 523 | if (pthread_mutex_unlock(&target_session->lock) != 0) { |
| 524 | ERROR("notifications: Error while unlocking rwlock"); |
| 525 | } |
Tomas Cejka | 885ec3e | 2014-06-22 18:01:34 +0200 | [diff] [blame] | 526 | unlock_glob: |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 527 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 528 | ERROR("notifications: Error while locking rwlock"); |
| 529 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Michal Vasko | 460dde5 | 2016-05-31 09:00:45 +0200 | [diff] [blame] | 532 | int |
| 533 | 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] | 534 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 535 | time_t start = -1; |
| 536 | time_t stop = -1; |
| 537 | char *stream = NULL; |
| 538 | struct nc_rpc *rpc = NULL; |
| 539 | struct nc_session *session; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 540 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 541 | DEBUG("notif_subscribe"); |
| 542 | if (locked_session == NULL) { |
| 543 | DEBUG("notifications: no locked_session was given."); |
| 544 | /* Close notification client */ |
| 545 | return -1; |
| 546 | } |
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 | pthread_mutex_lock(&locked_session->lock); |
| 549 | session = locked_session->session; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 550 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 551 | start = time(NULL) + start_time; |
| 552 | stop = time(NULL) + stop_time; |
| 553 | start = 0; |
| 554 | stop = 0; |
| 555 | DEBUG("notifications: history: %u %u", (unsigned int) start, (unsigned int) stop); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 556 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 557 | if (session == NULL) { |
| 558 | ERROR("notifications: NETCONF session not established."); |
| 559 | goto operation_failed; |
| 560 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 561 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 562 | /* check times */ |
| 563 | if (start != -1 && stop != -1 && start > stop) { |
| 564 | ERROR("notifications: Subscription start time must be lower than the end time."); |
| 565 | goto operation_failed; |
| 566 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 567 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 568 | DEBUG("Prepare to execute subscription."); |
| 569 | /* create requests */ |
Michal Vasko | 1cb9ebc | 2016-05-30 09:39:15 +0200 | [diff] [blame] | 570 | rpc = nc_rpc_subscribe(stream, NULL, (start_time == -1) ? NULL : nc_time2datetime(start, NULL, NULL), |
| 571 | (stop_time == 0) ? NULL : nc_time2datetime(stop, NULL, NULL), NC_PARAMTYPE_CONST); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 572 | if (rpc == NULL) { |
| 573 | ERROR("notifications: creating an rpc request failed."); |
| 574 | goto operation_failed; |
| 575 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 576 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 577 | DEBUG("Send NC subscribe."); |
| 578 | create_err_reply_p(); |
| 579 | if (send_recv_process(session, "subscribe", rpc) != 0) { |
| 580 | ERROR("Subscription RPC failed."); |
| 581 | goto operation_failed; |
| 582 | } |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 583 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 584 | GETSPEC_ERR_REPLY |
| 585 | if (err_reply != NULL) { |
Michal Vasko | 460dde5 | 2016-05-31 09:00:45 +0200 | [diff] [blame] | 586 | free_err_reply(); |
| 587 | ERROR("RPC-Error received and cleaned, because we can't send it anywhere."); |
| 588 | goto operation_failed; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 589 | } |
Tomas Cejka | 442258e | 2014-04-01 18:17:18 +0200 | [diff] [blame] | 590 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 591 | rpc = NULL; /* just note that rpc is already freed by send_recv_process() */ |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 592 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 593 | DEBUG("notifications: creating libnetconf notification thread (%s).", session_id); |
Tomas Cejka | 654f84e | 2013-04-19 11:55:01 +0200 | [diff] [blame] | 594 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 595 | pthread_mutex_unlock(&locked_session->lock); |
| 596 | |
| 597 | /* store hash identification of netconf session for notifications printing callback */ |
| 598 | if (pthread_setspecific(thread_key, session_id) != 0) { |
| 599 | ERROR("notifications: cannot set thread-specific hash value."); |
| 600 | } |
| 601 | |
| 602 | DEBUG("Create notification_thread."); |
| 603 | nc_recv_notif_dispatch(session, notification_fileprint); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 604 | return 0; |
Tomas Cejka | 5ce57b6 | 2013-08-29 17:47:39 +0200 | [diff] [blame] | 605 | |
| 606 | operation_failed: |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 607 | pthread_mutex_unlock(&locked_session->lock); |
| 608 | return -1; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 609 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 610 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 611 | static int |
| 612 | 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] | 613 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 614 | int n = 0, m = 0, i; |
| 615 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 40960 + LWS_SEND_BUFFER_POST_PADDING]; |
| 616 | unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING]; |
| 617 | 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] | 618 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 619 | debug_print_clb(__func__, reason); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 620 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 621 | switch (reason) { |
| 622 | case LWS_CALLBACK_ESTABLISHED: |
| 623 | DEBUG("notification client connected."); |
| 624 | break; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 625 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 626 | case LWS_CALLBACK_SERVER_WRITEABLE: |
| 627 | if (pss->session_id == NULL) { |
| 628 | return 0; |
| 629 | } |
| 630 | //DEBUG("Callback server writeable."); |
| 631 | //DEBUG("lock session lock."); |
| 632 | if (pthread_rwlock_wrlock(&session_lock) != 0) { |
| 633 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 634 | return -1; |
| 635 | } |
| 636 | //DEBUG("get session_with_mutex for %s.", pss->session_id); |
| 637 | struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id); |
| 638 | if (ls == NULL) { |
| 639 | DEBUG("notification: session not found"); |
| 640 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 641 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 642 | return -1; |
| 643 | } |
| 644 | return -1; |
| 645 | } |
| 646 | pthread_mutex_lock(&ls->lock); |
| 647 | if (pthread_rwlock_unlock(&session_lock) != 0) { |
| 648 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 649 | } |
| 650 | |
| 651 | //DEBUG("check for closed session."); |
| 652 | if (ls->closed == 1) { |
| 653 | DEBUG("unlock session key."); |
| 654 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 655 | DEBUG("Error while unlocking unlock: %d (%s)", errno, strerror(errno)); |
| 656 | return -1; |
| 657 | } |
| 658 | return -1; |
| 659 | } |
| 660 | //DEBUG("lock private lock."); |
| 661 | notification_t *notif = NULL; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 662 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 663 | if (ls->notif_count) { |
| 664 | DEBUG("notification: POP notifications for session"); |
Michal Vasko | 43d94a6 | 2015-11-05 10:59:32 +0100 | [diff] [blame] | 665 | for (i = ls->notif_count; i; --i) { |
| 666 | notif = ls->notifications + i - 1; |
Tomas Cejka | 7328693 | 2013-05-27 22:54:35 +0200 | [diff] [blame] | 667 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 668 | n = 0; |
| 669 | pthread_mutex_lock(&json_lock); |
| 670 | json_object *notif_json = json_object_new_object(); |
| 671 | json_object_object_add(notif_json, "eventtime", json_object_new_int64(notif->eventtime)); |
| 672 | json_object_object_add(notif_json, "content", json_object_new_string(notif->content)); |
| 673 | pthread_mutex_unlock(&json_lock); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 674 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 675 | const char *msgtext = json_object_to_json_string(notif_json); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 676 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 677 | //n = sprintf((char *)p, "{\"eventtime\": \"%s\", \"content\": \"notification\"}", t); |
| 678 | n = sprintf((char *)p, "%s", msgtext); |
| 679 | DEBUG("ws send %dB in %lu", n, sizeof(buf)); |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 680 | m = lws_write(wsi, p, n, LWS_WRITE_TEXT); |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 681 | if (lws_send_pipe_choked(wsi)) { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 682 | lws_callback_on_writable(wsi); |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 683 | break; |
| 684 | } |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 685 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 686 | pthread_mutex_lock(&json_lock); |
| 687 | json_object_put(notif_json); |
| 688 | pthread_mutex_unlock(&json_lock); |
| 689 | free(notif->content); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 690 | } |
Michal Vasko | 43d94a6 | 2015-11-05 10:59:32 +0100 | [diff] [blame] | 691 | ls->notif_count = 0; |
| 692 | free(ls->notifications); |
| 693 | ls->notifications = NULL; |
| 694 | |
Michal Vasko | 1ac1128 | 2015-11-05 10:28:07 +0100 | [diff] [blame] | 695 | DEBUG("notification: POP notifications done"); |
Michal Vasko | c314678 | 2015-11-04 14:46:41 +0100 | [diff] [blame] | 696 | } |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 697 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 698 | //DEBUG("unlock private lock"); |
| 699 | if (pthread_mutex_unlock(&ls->lock) != 0) { |
| 700 | DEBUG("notification: cannot unlock session"); |
| 701 | } |
| 702 | //DEBUG("unlock session lock"); |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 703 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 704 | if (m < n) { |
| 705 | DEBUG("ERROR %d writing to di socket.", n); |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 706 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 707 | return -1; |
| 708 | } |
| 709 | break; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 710 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 711 | case LWS_CALLBACK_RECEIVE: |
| 712 | DEBUG("Callback receive."); |
| 713 | DEBUG("received: (%s)", (char *)in); |
| 714 | if (pss->session_id == NULL) { |
| 715 | char *sid_end; |
| 716 | int start = -1; |
| 717 | time_t stop = time(NULL) + 30; |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 718 | |
Michal Vasko | 2bff5fb | 2015-11-05 10:23:02 +0100 | [diff] [blame] | 719 | sid_end = strchr(in, ' '); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 720 | pss->session_id = strndup(in, sid_end - (char *)in); |
Michal Vasko | 2bff5fb | 2015-11-05 10:23:02 +0100 | [diff] [blame] | 721 | |
| 722 | ++sid_end; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 723 | sscanf(sid_end, "%d %d", (int *) &start, (int *) &stop); |
| 724 | 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] | 725 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 726 | DEBUG("lock session lock"); |
| 727 | if (pthread_rwlock_rdlock (&session_lock) != 0) { |
| 728 | DEBUG("Error while locking rwlock: %d (%s)", errno, strerror(errno)); |
| 729 | return -1; |
| 730 | } |
| 731 | DEBUG("get session with ID (%s)", pss->session_id); |
| 732 | struct session_with_mutex *ls = get_ncsession_from_sid(pss->session_id); |
| 733 | if (ls == NULL) { |
| 734 | DEBUG("notification: session_id not found (%s)", pss->session_id); |
| 735 | DEBUG("unlock session lock"); |
| 736 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 737 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 738 | } |
| 739 | DEBUG("Close notification client"); |
| 740 | return -1; |
| 741 | } |
| 742 | DEBUG("lock private lock"); |
| 743 | pthread_mutex_lock(&ls->lock); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 744 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 745 | DEBUG("unlock session lock"); |
| 746 | if (pthread_rwlock_unlock (&session_lock) != 0) { |
| 747 | DEBUG("Error while unlocking rwlock: %d (%s)", errno, strerror(errno)); |
| 748 | } |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 749 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 750 | DEBUG("Found session to subscribe notif."); |
| 751 | if (ls->closed == 1) { |
| 752 | DEBUG("session already closed - handle no notification"); |
| 753 | DEBUG("unlock private lock"); |
| 754 | pthread_mutex_unlock(&ls->lock); |
| 755 | DEBUG("Close notification client"); |
| 756 | return -1; |
| 757 | } |
Michal Vasko | 460dde5 | 2016-05-31 09:00:45 +0200 | [diff] [blame] | 758 | if (nc_session_ntf_thread_running(ls->session)) { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 759 | DEBUG("notification: already subscribed"); |
| 760 | DEBUG("unlock private lock"); |
| 761 | pthread_mutex_unlock(&ls->lock); |
| 762 | /* do not close client, only do not subscribe again */ |
| 763 | return 0; |
| 764 | } |
| 765 | DEBUG("notification: prepare to subscribe stream"); |
| 766 | DEBUG("unlock session lock"); |
| 767 | pthread_mutex_unlock(&ls->lock); |
Tomas Cejka | 47387fd | 2013-06-10 20:37:46 +0200 | [diff] [blame] | 768 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 769 | /* notif_subscribe locks on its own */ |
| 770 | return notif_subscribe(ls, pss->session_id, (time_t) start, (time_t) stop); |
| 771 | } |
| 772 | if (len < 6) |
| 773 | break; |
| 774 | break; |
| 775 | /* |
| 776 | * this just demonstrates how to use the protocol filter. If you won't |
| 777 | * study and reject connections based on header content, you don't need |
| 778 | * to handle this callback |
| 779 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 780 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 781 | case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: |
| 782 | //dump_handshake_info(wsi); |
| 783 | /* you could return non-zero here and kill the connection */ |
| 784 | break; |
| 785 | //gives segfault :-( |
| 786 | //case LWS_CALLBACK_CLOSED: |
| 787 | // if (pss->session_key != NULL) { |
| 788 | // free(pss->session_key); |
| 789 | // } |
| 790 | // if (pss != NULL) { |
| 791 | // free(pss); |
| 792 | // } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 793 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 794 | default: |
| 795 | break; |
| 796 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 797 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 798 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 799 | } |
| 800 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 801 | static struct lws_protocols protocols[] = { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 802 | /* first protocol must always be HTTP handler */ |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 803 | { |
| 804 | "http-only", /* name */ |
| 805 | callback_http, /* callback */ |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 806 | sizeof(struct per_session_data__http), /* per_session_data_size */ |
| 807 | 0, /* max frame size / rx buffer */ |
| 808 | 1, |
| 809 | NULL |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 810 | }, |
| 811 | { |
| 812 | "notification-protocol", |
| 813 | callback_notification, |
| 814 | sizeof(struct per_session_data__notif_client), |
| 815 | 4000, |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 816 | 2, |
| 817 | NULL |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 818 | }, |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 819 | { NULL, NULL, 0, 0, 0, NULL } /* terminator */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 820 | }; |
| 821 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 822 | /** |
| 823 | * initialization of notification module |
| 824 | */ |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 825 | int |
| 826 | notification_init(void) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 827 | { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 828 | char cert_path[1024], key_path[1024]; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 829 | struct lws_context_creation_info info; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 830 | int debug_level = 7; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 831 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 832 | memset(&info, 0, sizeof info); |
| 833 | info.port = NOTIFICATION_SERVER_PORT; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 834 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 835 | /* tell the library what debug level to emit and to send it to syslog */ |
| 836 | lws_set_log_level(debug_level, lwsl_emit_syslog); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 837 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 838 | DEBUG("Initialization of libwebsocket"); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 839 | max_poll_elements = getdtablesize(); |
| 840 | pollfds = malloc(max_poll_elements * sizeof (struct pollfd)); |
| 841 | fd_lookup = malloc(max_poll_elements * sizeof (int)); |
| 842 | if (pollfds == NULL || fd_lookup == NULL) { |
| 843 | ERROR("notifications: Out of memory pollfds=%d\n", max_poll_elements); |
| 844 | return -1; |
| 845 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 846 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 847 | info.iface = NULL; |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 848 | info.protocols = protocols; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 849 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 850 | snprintf(cert_path, sizeof(cert_path), SERVER_CERT_DIR "/" SERVER_CERT); |
| 851 | snprintf(key_path, sizeof(key_path), SERVER_CERT_DIR "/" SERVER_KEY); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 852 | |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 853 | info.ssl_cert_filepath = cert_path; |
| 854 | info.ssl_private_key_filepath = key_path; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 855 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 856 | info.gid = -1; |
| 857 | info.uid = -1; |
Michal Vasko | a41bb80 | 2016-05-30 09:04:55 +0200 | [diff] [blame] | 858 | 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] | 859 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 860 | /* create server */ |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 861 | context = lws_create_context(&info); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 862 | if (context == NULL) { |
| 863 | DEBUG("libwebsocket init failed."); |
| 864 | return -1; |
| 865 | } |
Tomas Cejka | 15c5630 | 2013-05-30 01:11:30 +0200 | [diff] [blame] | 866 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 867 | if (pthread_key_create(&thread_key, NULL) != 0) { |
| 868 | ERROR("notifications: pthread_key_create failed"); |
| 869 | } |
| 870 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 871 | } |
| 872 | |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 873 | void |
| 874 | notification_close(void) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 875 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 876 | if (context) { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 877 | lws_context_destroy(context); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 878 | } |
| 879 | free(pollfds); |
| 880 | free(fd_lookup); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 881 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 882 | DEBUG("libwebsockets-test-server exited cleanly\n"); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Tomas Cejka | ba21b38 | 2013-04-13 02:37:32 +0200 | [diff] [blame] | 885 | |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 886 | /** |
| 887 | * \brief send notification if any |
| 888 | * \return < 0 on error |
| 889 | */ |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 890 | int |
| 891 | notification_handle() |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 892 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 893 | static struct timeval tv; |
| 894 | static unsigned int olds = 0; |
Michal Vasko | 60920a4 | 2016-05-30 10:02:02 +0200 | [diff] [blame] | 895 | int n = 0, fd_pos; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 896 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 897 | gettimeofday(&tv, NULL); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 898 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 899 | /* |
| 900 | * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every |
| 901 | * live websocket connection using the DUMB_INCREMENT protocol, |
| 902 | * as soon as it can take more packets (usually immediately) |
| 903 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 904 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 905 | if (((unsigned int)tv.tv_sec - olds) > 0) { |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 906 | lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_NOTIFICATION]); |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 907 | olds = tv.tv_sec; |
| 908 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 909 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 910 | /* |
| 911 | * this represents an existing server's single poll action |
| 912 | * which also includes libwebsocket sockets |
| 913 | */ |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 914 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 915 | n = poll(pollfds, count_pollfds, 50); |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 916 | if (n < 0) { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 917 | return n; |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 918 | } |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 919 | |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 920 | if (n) { |
| 921 | for (n = 0; n < count_pollfds; n++) { |
Michal Vasko | 3b2507e | 2016-05-30 09:33:04 +0200 | [diff] [blame] | 922 | if (pollfds[n].revents & POLLHUP) { |
| 923 | ERROR("notifications: poll pipe closed"); |
Michal Vasko | 0fdd821 | 2016-05-30 09:55:23 +0200 | [diff] [blame] | 924 | if (--count_pollfds) { |
Michal Vasko | 60920a4 | 2016-05-30 10:02:02 +0200 | [diff] [blame] | 925 | fd_pos = fd_lookup[pollfds[n].fd]; |
| 926 | pollfds[fd_pos] = pollfds[count_pollfds]; |
| 927 | fd_lookup[pollfds[count_pollfds].fd] = fd_pos; |
Michal Vasko | 0fdd821 | 2016-05-30 09:55:23 +0200 | [diff] [blame] | 928 | } |
Michal Vasko | 3b2507e | 2016-05-30 09:33:04 +0200 | [diff] [blame] | 929 | return -1; |
| 930 | } else if (pollfds[n].revents & POLLERR) { |
| 931 | ERROR("notifications: poll pipe error"); |
Michal Vasko | 0fdd821 | 2016-05-30 09:55:23 +0200 | [diff] [blame] | 932 | if (--count_pollfds) { |
Michal Vasko | 60920a4 | 2016-05-30 10:02:02 +0200 | [diff] [blame] | 933 | fd_pos = fd_lookup[pollfds[n].fd]; |
| 934 | pollfds[fd_pos] = pollfds[count_pollfds]; |
| 935 | fd_lookup[pollfds[count_pollfds].fd] = fd_pos; |
Michal Vasko | 0fdd821 | 2016-05-30 09:55:23 +0200 | [diff] [blame] | 936 | } |
Michal Vasko | 0fc2d4c | 2016-05-30 09:03:14 +0200 | [diff] [blame] | 937 | return -1; |
| 938 | } else if (pollfds[n].revents & (POLLIN | POLLOUT)) { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 939 | /* |
| 940 | * returns immediately if the fd does not |
| 941 | * match anything under libwebsockets |
| 942 | * control |
| 943 | */ |
Michal Vasko | 262d34b | 2016-05-06 13:54:37 +0200 | [diff] [blame] | 944 | if (lws_service_fd(context, &pollfds[n]) < 0) { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 945 | return 1; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | return 0; |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | #endif |
| 954 | |
| 955 | |
| 956 | #ifndef WITH_NOTIFICATIONS |
| 957 | #ifdef TEST_NOTIFICATION_SERVER |
Michal Vasko | 56d70bc | 2016-05-10 12:06:52 +0200 | [diff] [blame] | 958 | int |
| 959 | main(int argc, char **argv) |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 960 | { |
Michal Vasko | fcc9a9c | 2016-05-06 11:51:35 +0200 | [diff] [blame] | 961 | if (notification_init(NULL, NULL) == -1) { |
| 962 | fprintf(stderr, "Error during initialization\n"); |
| 963 | return 1; |
| 964 | } |
| 965 | while (!force_exit) { |
| 966 | notification_handle(); |
| 967 | } |
| 968 | notification_close(); |
Tomas Cejka | d340dbf | 2013-03-24 20:36:57 +0100 | [diff] [blame] | 969 | } |
| 970 | #endif |
| 971 | #endif |