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