test server allow only best quality ciphers
[platform/upstream/libwebsockets.git] / test-server / test-server.c
1 /*
2  * libwebsockets-test-servet - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * The test apps are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20
21 #include "test-server.h"
22
23 int close_testing;
24 int max_poll_elements;
25 int debug_level = 7;
26
27 #ifdef EXTERNAL_POLL
28 struct lws_pollfd *pollfds;
29 int *fd_lookup;
30 int count_pollfds;
31 #endif
32 volatile int force_exit = 0;
33 struct lws_context *context;
34 struct lws_plat_file_ops fops_plat;
35
36 /* http server gets files from this path */
37 #define LOCAL_RESOURCE_PATH INSTALL_DATADIR"/libwebsockets-test-server"
38 char *resource_path = LOCAL_RESOURCE_PATH;
39
40 /* singlethreaded version --> no locks */
41
42 void test_server_lock(int care)
43 {
44 }
45 void test_server_unlock(int care)
46 {
47 }
48
49 /*
50  * This demo server shows how to use libwebsockets for one or more
51  * websocket protocols in the same server
52  *
53  * It defines the following websocket protocols:
54  *
55  *  dumb-increment-protocol:  once the socket is opened, an incrementing
56  *                              ascii string is sent down it every 50ms.
57  *                              If you send "reset\n" on the websocket, then
58  *                              the incrementing number is reset to 0.
59  *
60  *  lws-mirror-protocol: copies any received packet to every connection also
61  *                              using this protocol, including the sender
62  */
63
64 enum demo_protocols {
65         /* always first */
66         PROTOCOL_HTTP = 0,
67
68         PROTOCOL_DUMB_INCREMENT,
69         PROTOCOL_LWS_MIRROR,
70         PROTOCOL_LWS_ECHOGEN,
71
72         /* always last */
73         DEMO_PROTOCOL_COUNT
74 };
75
76 /* list of supported protocols and callbacks */
77
78 static struct lws_protocols protocols[] = {
79         /* first protocol must always be HTTP handler */
80
81         {
82                 "http-only",            /* name */
83                 callback_http,          /* callback */
84                 sizeof (struct per_session_data__http), /* per_session_data_size */
85                 0,                      /* max frame size / rx buffer */
86         },
87         {
88                 "dumb-increment-protocol",
89                 callback_dumb_increment,
90                 sizeof(struct per_session_data__dumb_increment),
91                 10,
92         },
93         {
94                 "lws-mirror-protocol",
95                 callback_lws_mirror,
96                 sizeof(struct per_session_data__lws_mirror),
97                 128,
98         },
99         {
100                 "lws-echogen",
101                 callback_lws_echogen,
102                 sizeof(struct per_session_data__echogen),
103                 128,
104         },
105         { NULL, NULL, 0, 0 } /* terminator */
106 };
107
108
109 /* this shows how to override the lws file operations.  You don't need
110  * to do any of this unless you have a reason (eg, want to serve
111  * compressed files without decompressing the whole archive)
112  */
113 static lws_filefd_type
114 test_server_fops_open(struct lws *wsi, const char *filename,
115                       unsigned long *filelen, int flags)
116 {
117         lws_filefd_type n;
118
119         /* call through to original platform implementation */
120         n = fops_plat.open(wsi, filename, filelen, flags);
121
122         lwsl_notice("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
123                         (long)n, *filelen);
124
125         return n;
126 }
127
128 void sighandler(int sig)
129 {
130         force_exit = 1;
131         lws_cancel_service(context);
132 }
133
134 static const struct lws_extension exts[] = {
135         {
136                 "permessage-deflate",
137                 lws_extension_callback_pm_deflate,
138                 "permessage-deflate"
139         },
140         {
141                 "deflate-frame",
142                 lws_extension_callback_pm_deflate,
143                 "deflate_frame"
144         },
145         { NULL, NULL, NULL /* terminator */ }
146 };
147
148
149
150 static struct option options[] = {
151         { "help",       no_argument,            NULL, 'h' },
152         { "debug",      required_argument,      NULL, 'd' },
153         { "port",       required_argument,      NULL, 'p' },
154         { "ssl",        no_argument,            NULL, 's' },
155         { "allow-non-ssl",      no_argument,    NULL, 'a' },
156         { "interface",  required_argument,      NULL, 'i' },
157         { "closetest",  no_argument,            NULL, 'c' },
158         { "ssl-cert",  required_argument,       NULL, 'C' },
159         { "ssl-key",  required_argument,        NULL, 'K' },
160         { "ssl-ca",  required_argument,         NULL, 'A' },
161         { "libev",  no_argument,                NULL, 'e' },
162 #ifndef LWS_NO_DAEMONIZE
163         { "daemonize",  no_argument,            NULL, 'D' },
164 #endif
165         { "resource_path", required_argument,   NULL, 'r' },
166         { NULL, 0, 0, 0 }
167 };
168
169 int main(int argc, char **argv)
170 {
171         struct lws_context_creation_info info;
172         char interface_name[128] = "";
173         unsigned int ms, oldms = 0;
174         const char *iface = NULL;
175         char cert_path[1024] = "";
176         char key_path[1024] = "";
177         char ca_path[1024] = "";
178         int use_ssl = 0;
179         int opts = 0;
180         int n = 0;
181 #ifndef _WIN32
182         int syslog_options = LOG_PID | LOG_PERROR;
183 #endif
184 #ifndef LWS_NO_DAEMONIZE
185         int daemonize = 0;
186 #endif
187
188         /*
189          * take care to zero down the info struct, he contains random garbaage
190          * from the stack otherwise
191          */
192         memset(&info, 0, sizeof info);
193         info.port = 7681;
194
195         while (n >= 0) {
196                 n = getopt_long(argc, argv, "eci:hsap:d:Dr:C:K:A:", options, NULL);
197                 if (n < 0)
198                         continue;
199                 switch (n) {
200                 case 'e':
201                         opts |= LWS_SERVER_OPTION_LIBEV;
202                         break;
203 #ifndef LWS_NO_DAEMONIZE
204                 case 'D':
205                         daemonize = 1;
206                         #ifndef _WIN32
207                         syslog_options &= ~LOG_PERROR;
208                         #endif
209                         break;
210 #endif
211                 case 'd':
212                         debug_level = atoi(optarg);
213                         break;
214                 case 's':
215                         use_ssl = 1;
216                         break;
217                 case 'a':
218                         opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
219                         break;
220                 case 'p':
221                         info.port = atoi(optarg);
222                         break;
223                 case 'i':
224                         strncpy(interface_name, optarg, sizeof interface_name);
225                         interface_name[(sizeof interface_name) - 1] = '\0';
226                         iface = interface_name;
227                         break;
228                 case 'c':
229                         close_testing = 1;
230                         fprintf(stderr, " Close testing mode -- closes on "
231                                            "client after 50 dumb increments"
232                                            "and suppresses lws_mirror spam\n");
233                         break;
234                 case 'r':
235                         resource_path = optarg;
236                         printf("Setting resource path to \"%s\"\n", resource_path);
237                         break;
238                 case 'C':
239                         strncpy(cert_path, optarg, sizeof cert_path);
240                         break;
241                 case 'K':
242                         strncpy(key_path, optarg, sizeof key_path);
243                         break;
244                 case 'A':
245                         strncpy(ca_path, optarg, sizeof ca_path);
246                         break;
247                 case 'h':
248                         fprintf(stderr, "Usage: test-server "
249                                         "[--port=<p>] [--ssl] "
250                                         "[-d <log bitfield>] "
251                                         "[--resource_path <path>]\n");
252                         exit(1);
253                 }
254         }
255
256 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
257         /*
258          * normally lock path would be /var/lock/lwsts or similar, to
259          * simplify getting started without having to take care about
260          * permissions or running as root, set to /tmp/.lwsts-lock
261          */
262         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
263                 fprintf(stderr, "Failed to daemonize\n");
264                 return 1;
265         }
266 #endif
267
268         signal(SIGINT, sighandler);
269
270 #ifndef _WIN32
271         /* we will only try to log things according to our debug_level */
272         setlogmask(LOG_UPTO (LOG_DEBUG));
273         openlog("lwsts", syslog_options, LOG_DAEMON);
274 #endif
275
276         /* tell the library what debug level to emit and to send it to syslog */
277         lws_set_log_level(debug_level, lwsl_emit_syslog);
278
279         lwsl_notice("libwebsockets test server - license LGPL2.1+SLE\n");
280         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
281
282         printf("Using resource path \"%s\"\n", resource_path);
283 #ifdef EXTERNAL_POLL
284         max_poll_elements = getdtablesize();
285         pollfds = malloc(max_poll_elements * sizeof (struct lws_pollfd));
286         fd_lookup = malloc(max_poll_elements * sizeof (int));
287         if (pollfds == NULL || fd_lookup == NULL) {
288                 lwsl_err("Out of memory pollfds=%d\n", max_poll_elements);
289                 return -1;
290         }
291 #endif
292
293         info.iface = iface;
294         info.protocols = protocols;
295         info.ssl_cert_filepath = NULL;
296         info.ssl_private_key_filepath = NULL;
297
298         if (use_ssl) {
299                 if (strlen(resource_path) > sizeof(cert_path) - 32) {
300                         lwsl_err("resource path too long\n");
301                         return -1;
302                 }
303                 if (!cert_path[0])
304                         sprintf(cert_path, "%s/libwebsockets-test-server.pem",
305                                                                 resource_path);
306                 if (strlen(resource_path) > sizeof(key_path) - 32) {
307                         lwsl_err("resource path too long\n");
308                         return -1;
309                 }
310                 if (!key_path[0])
311                         sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
312                                                                 resource_path);
313
314                 info.ssl_cert_filepath = cert_path;
315                 info.ssl_private_key_filepath = key_path;
316                 if (ca_path[0])
317                         info.ssl_ca_filepath = ca_path;
318         }
319         info.gid = -1;
320         info.uid = -1;
321         info.max_http_header_pool = 1;
322         info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8;
323         info.extensions = exts;
324         info.ssl_cipher_list = "ECDHE-ECDSA-AES256-GCM-SHA384:"
325                                "ECDHE-RSA-AES256-GCM-SHA384:"
326                                "DHE-RSA-AES256-GCM-SHA384:"
327                                "ECDHE-RSA-AES256-SHA384:"
328                                "HIGH:!aNULL:!eNULL:!EXPORT:"
329                                "!DES:!MD5:!PSK:!RC4:!HMAC_SHA1:"
330                                "!SHA1:!DHE-RSA-AES128-GCM-SHA256:"
331                                "!DHE-RSA-AES128-SHA256:"
332                                "!AES128-GCM-SHA256:"
333                                "!AES128-SHA256:"
334                                "!DHE-RSA-AES256-SHA256:"
335                                "!AES256-GCM-SHA384:"
336                                "!AES256-SHA256";
337         context = lws_create_context(&info);
338         if (context == NULL) {
339                 lwsl_err("libwebsocket init failed\n");
340                 return -1;
341         }
342
343         /* this shows how to override the lws file operations.  You don't need
344          * to do any of this unless you have a reason (eg, want to serve
345          * compressed files without decompressing the whole archive)
346          */
347         /* stash original platform fops */
348         fops_plat = *(lws_get_fops(context));
349         /* override the active fops */
350         lws_get_fops(context)->open = test_server_fops_open;
351
352         n = 0;
353         while (n >= 0 && !force_exit) {
354                 struct timeval tv;
355
356                 gettimeofday(&tv, NULL);
357
358                 /*
359                  * This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
360                  * live websocket connection using the DUMB_INCREMENT protocol,
361                  * as soon as it can take more packets (usually immediately)
362                  */
363
364                 ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
365                 if ((ms - oldms) > 50) {
366                         lws_callback_on_writable_all_protocol(context,
367                                 &protocols[PROTOCOL_DUMB_INCREMENT]);
368                         oldms = ms;
369                 }
370
371 #ifdef EXTERNAL_POLL
372                 /*
373                  * this represents an existing server's single poll action
374                  * which also includes libwebsocket sockets
375                  */
376
377                 n = poll(pollfds, count_pollfds, 50);
378                 if (n < 0)
379                         continue;
380
381                 if (n)
382                         for (n = 0; n < count_pollfds; n++)
383                                 if (pollfds[n].revents)
384                                         /*
385                                         * returns immediately if the fd does not
386                                         * match anything under libwebsockets
387                                         * control
388                                         */
389                                         if (lws_service_fd(context,
390                                                                   &pollfds[n]) < 0)
391                                                 goto done;
392 #else
393                 /*
394                  * If libwebsockets sockets are all we care about,
395                  * you can use this api which takes care of the poll()
396                  * and looping through finding who needed service.
397                  *
398                  * If no socket needs service, it'll return anyway after
399                  * the number of ms in the second argument.
400                  */
401
402                 n = lws_service(context, 50);
403 #endif
404         }
405
406 #ifdef EXTERNAL_POLL
407 done:
408 #endif
409
410         lws_context_destroy(context);
411
412         lwsl_notice("libwebsockets-test-server exited cleanly\n");
413
414 #ifndef _WIN32
415         closelog();
416 #endif
417
418         return 0;
419 }