• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-dbus-ws-proxy
3  *
4  * Written in 2010-2019 by 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  * This demonstrates a minimal session dbus server that uses the lws event loop,
10  * and allows proxying ws client connections via DBUS.
11  */
12 
13 #include <stdbool.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <signal.h>
19 
20 #include <libwebsockets.h>
21 #include <libwebsockets/lws-dbus.h>
22 
23 #define LWS_PLUGIN_STATIC
24 #include "protocol_lws_minimal_dbus_ws_proxy.c"
25 
26 static int interrupted;
27 static struct lws_protocols protocols[] = {
28 	LWS_PLUGIN_PROTOCOL_MINIMAL_DBUS_WSPROXY,
29 	LWS_PROTOCOL_LIST_TERM
30 };
31 
32 /*
33  * we pass the dbus address to connect to proxy with from outside the
34  * protocol plugin... eg if built as a plugin for lwsws, you would instead
35  * set this pvo in the lwsws JSON config.
36  */
37 
38 static const struct lws_protocol_vhost_options pvo_ads = {
39 	NULL,
40 	NULL,
41 	"ads",				/* pvo name */
42 	(void *)"unix:abstract=org.libwebsockets.wsclientproxy"	/* pvo value */
43 };
44 
45 static const struct lws_protocol_vhost_options pvo = {
46 	NULL,				/* "next" pvo linked-list */
47 	&pvo_ads,			/* "child" pvo linked-list */
48 	"lws-minimal-dbus-wsproxy",	/* protocol name we belong to on this vhost */
49 	""				/* ignored */
50 };
51 
sigint_handler(int sig)52 void sigint_handler(int sig)
53 {
54 	interrupted = 1;
55 }
56 
main(int argc,const char ** argv)57 int main(int argc, const char **argv)
58 {
59 	static struct lws_context *context;
60 	struct lws_context_creation_info info;
61 	const char *p;
62 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
63 			/* for LLL_ verbosity above NOTICE to be built into lws,
64 			 * lws must have been configured and built with
65 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
66 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
67 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
68 			/* | LLL_DEBUG */ /* | LLL_THREAD */;
69 
70 	signal(SIGINT, sigint_handler);
71 
72 	if ((p = lws_cmdline_option(argc, argv, "-d")))
73 		logs = atoi(p);
74 
75 	lws_set_log_level(logs, NULL);
76 	lwsl_user("LWS DBUS ws client proxy\n");
77 
78 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
79 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
80 		LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
81 	info.port = CONTEXT_PORT_NO_LISTEN;
82 	info.protocols = protocols;
83 	info.pvo = &pvo;
84 
85 	context = lws_create_context(&info);
86 	if (!context) {
87 		lwsl_err("lws init failed\n");
88 		return 1;
89 	}
90 
91 	/* lws event loop (default poll one) */
92 
93 	while (n >= 0 && !interrupted)
94 		n = lws_service(context, 0);
95 
96 	lws_context_destroy(context);
97 
98 	lwsl_notice("Exiting cleanly\n");
99 
100 	return 0;
101 }
102