1 /*
2 * lws-minimal-secure-streams-server
3 *
4 * Written in 2010-2020 by Andy Green <[email protected]>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 */
9
10 #include <libwebsockets.h>
11 #include <string.h>
12 #include <signal.h>
13
14 extern const lws_ss_info_t ssi_client, ssi_server;
15
16 static struct lws_context *context;
17 int interrupted, bad = 1;
18 static const char * const default_ss_policy =
19 "{"
20 "\"release\":" "\"01234567\","
21 "\"product\":" "\"myproduct\","
22 "\"schema-version\":" "1,"
23 "\"s\": ["
24
25 /*
26 * This streamtype represents a raw server listening on :7681,
27 * without tls
28 */
29
30 "{\"myrawserver\": {"
31 /* if given, "endpoint" is network if to bind to */
32 "\"server\":" "true,"
33 "\"port\":" "7681,"
34 "\"protocol\":" "\"raw\""
35 "}}"
36
37 "]"
38 "}"
39 ;
40
41 static int
smd_cb(void * opaque,lws_smd_class_t c,lws_usec_t ts,void * buf,size_t len)42 smd_cb(void *opaque, lws_smd_class_t c, lws_usec_t ts, void *buf, size_t len)
43 {
44 if ((c & LWSSMDCL_SYSTEM_STATE) &&
45 !lws_json_simple_strcmp(buf, len, "\"state\":", "OPERATIONAL")) {
46
47 /* create the secure streams */
48
49 lwsl_notice("%s: creating server stream\n", __func__);
50
51 if (lws_ss_create(context, 0, &ssi_server, NULL, NULL,
52 NULL, NULL)) {
53 lwsl_err("%s: failed to create secure stream\n",
54 __func__);
55 return -1;
56 }
57 }
58
59 return 0;
60 }
61
62 static void
sigint_handler(int sig)63 sigint_handler(int sig)
64 {
65 interrupted = 1;
66 }
67
main(int argc,const char ** argv)68 int main(int argc, const char **argv)
69 {
70 struct lws_context_creation_info info;
71 int n = 0;
72
73 signal(SIGINT, sigint_handler);
74
75 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
76 lws_cmdline_option_handle_builtin(argc, argv, &info);
77 lwsl_user("LWS Secure Streams Server Raw\n");
78
79 info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
80 LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
81 info.fd_limit_per_thread = 1 + 6 + 1;
82 info.pss_policies_json = default_ss_policy;
83 info.port = CONTEXT_PORT_NO_LISTEN;
84 info.early_smd_cb = smd_cb;
85 info.early_smd_class_filter = LWSSMDCL_SYSTEM_STATE;
86
87 context = lws_create_context(&info);
88 if (!context) {
89 lwsl_err("lws init failed\n");
90 return 1;
91 }
92
93 /* the event loop */
94
95 while (n >= 0 && !interrupted)
96 n = lws_service(context, 0);
97
98 bad = 0;
99
100 lws_context_destroy(context);
101 lwsl_user("Completed: %s\n", bad ? "failed" : "OK");
102
103 return bad;
104 }
105