1 /*
2  * lws-minimal-ws-client-ping
3  *
4  * Written in 2010-2020 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 keeping a ws connection validated by the lws validity
10  * timer stuff without having to do anything in the code.  Use debug logging
11  * -d1039 to see lws doing the pings / pongs in the background.
12  */
13 
14 #include <libwebsockets.h>
15 #include <string.h>
16 #include <signal.h>
17 #if defined(WIN32)
18 #define HAVE_STRUCT_TIMESPEC
19 #if defined(pid_t)
20 #undef pid_t
21 #endif
22 #endif
23 #include <pthread.h>
24 
25 static struct lws_context *context;
26 static struct lws *client_wsi;
27 static int interrupted, port = 443, ssl_connection = LCCSCF_USE_SSL;
28 static const char *server_address = "libwebsockets.org", *pro = "lws-mirror-protocol";
29 static lws_sorted_usec_list_t sul;
30 
31 static const lws_retry_bo_t retry = {
32 	.secs_since_valid_ping = 3,
33 	.secs_since_valid_hangup = 10,
34 };
35 
36 static void
connect_cb(lws_sorted_usec_list_t * _sul)37 connect_cb(lws_sorted_usec_list_t *_sul)
38 {
39 	struct lws_client_connect_info i;
40 
41 	lwsl_notice("%s: connecting\n", __func__);
42 
43 	memset(&i, 0, sizeof(i));
44 
45 	i.context = context;
46 	i.port = port;
47 	i.address = server_address;
48 	i.path = "/";
49 	i.host = i.address;
50 	i.origin = i.address;
51 	i.ssl_connection = ssl_connection;
52 	i.protocol = pro;
53 	i.alpn = "h2;http/1.1";
54 	i.local_protocol_name = "lws-ping-test";
55 	i.pwsi = &client_wsi;
56 	i.retry_and_idle_policy = &retry;
57 
58 	if (!lws_client_connect_via_info(&i))
59 		lws_sul_schedule(context, 0, _sul, connect_cb, 5 * LWS_USEC_PER_SEC);
60 }
61 
62 static int
callback_minimal_pingtest(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)63 callback_minimal_pingtest(struct lws *wsi, enum lws_callback_reasons reason,
64 			 void *user, void *in, size_t len)
65 {
66 
67 	switch (reason) {
68 
69 	case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
70 		lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
71 			 in ? (char *)in : "(null)");
72 		lws_sul_schedule(context, 0, &sul, connect_cb, 5 * LWS_USEC_PER_SEC);
73 		break;
74 
75 	case LWS_CALLBACK_CLIENT_ESTABLISHED:
76 		lwsl_user("%s: established\n", __func__);
77 		break;
78 
79 	default:
80 		break;
81 	}
82 
83 	return lws_callback_http_dummy(wsi, reason, user, in, len);
84 }
85 
86 static const struct lws_protocols protocols[] = {
87 	{
88 		"lws-ping-test",
89 		callback_minimal_pingtest,
90 		0,
91 		0,
92 	},
93 	{ NULL, NULL, 0, 0 }
94 };
95 
96 static void
sigint_handler(int sig)97 sigint_handler(int sig)
98 {
99 	interrupted = 1;
100 }
101 
main(int argc,const char ** argv)102 int main(int argc, const char **argv)
103 {
104 	struct lws_context_creation_info info;
105 	const char *p;
106 	int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
107 			/* for LLL_ verbosity above NOTICE to be built into lws,
108 			 * lws must have been configured and built with
109 			 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
110 			/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
111 			/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
112 			/* | LLL_DEBUG */;
113 
114 	signal(SIGINT, sigint_handler);
115 
116 	if ((p = lws_cmdline_option(argc, argv, "-d")))
117 		logs = atoi(p);
118 
119 	lws_set_log_level(logs, NULL);
120 	lwsl_user("LWS minimal ws client PING\n");
121 
122 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
123 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
124 	info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
125 	info.protocols = protocols;
126 #if defined(LWS_WITH_MBEDTLS) || defined(USE_WOLFSSL)
127 	/*
128 	 * OpenSSL uses the system trust store.  mbedTLS has to be told which
129 	 * CA to trust explicitly.
130 	 */
131 	info.client_ssl_ca_filepath = "./libwebsockets.org.cer";
132 #endif
133 
134 	if ((p = lws_cmdline_option(argc, argv, "--protocol")))
135 		pro = p;
136 
137 	if ((p = lws_cmdline_option(argc, argv, "--server"))) {
138 		server_address = p;
139 		pro = "lws-minimal";
140 		ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
141 	}
142 
143 	if ((p = lws_cmdline_option(argc, argv, "--port")))
144 		port = atoi(p);
145 
146 	info.fd_limit_per_thread = 1 + 1 + 1;
147 
148 	context = lws_create_context(&info);
149 	if (!context) {
150 		lwsl_err("lws init failed\n");
151 		return 1;
152 	}
153 
154 	lws_sul_schedule(context, 0, &sul, connect_cb, 100);
155 
156 	while (n >= 0 && !interrupted)
157 		n = lws_service(context, 0);
158 
159 	lws_context_destroy(context);
160 	lwsl_user("Completed\n");
161 
162 	return 0;
163 }
164