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, 0, 0, NULL, 0
91 },
92 LWS_PROTOCOL_LIST_TERM
93 };
94
95 static void
sigint_handler(int sig)96 sigint_handler(int sig)
97 {
98 interrupted = 1;
99 }
100
main(int argc,const char ** argv)101 int main(int argc, const char **argv)
102 {
103 struct lws_context_creation_info info;
104 const char *p;
105 int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
106 /* for LLL_ verbosity above NOTICE to be built into lws,
107 * lws must have been configured and built with
108 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
109 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
110 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
111 /* | LLL_DEBUG */;
112
113 signal(SIGINT, sigint_handler);
114
115 if ((p = lws_cmdline_option(argc, argv, "-d")))
116 logs = atoi(p);
117
118 lws_set_log_level(logs, NULL);
119 lwsl_user("LWS minimal ws client PING\n");
120
121 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
122 info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
123 info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
124 info.protocols = protocols;
125 #if defined(LWS_WITH_MBEDTLS) || defined(USE_WOLFSSL)
126 /*
127 * OpenSSL uses the system trust store. mbedTLS has to be told which
128 * CA to trust explicitly.
129 */
130 info.client_ssl_ca_filepath = "./libwebsockets.org.cer";
131 #endif
132
133 if ((p = lws_cmdline_option(argc, argv, "--protocol")))
134 pro = p;
135
136 if ((p = lws_cmdline_option(argc, argv, "--server"))) {
137 server_address = p;
138 pro = "lws-minimal";
139 ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
140 }
141
142 if ((p = lws_cmdline_option(argc, argv, "--port")))
143 port = atoi(p);
144
145 info.fd_limit_per_thread = 1 + 1 + 1;
146
147 context = lws_create_context(&info);
148 if (!context) {
149 lwsl_err("lws init failed\n");
150 return 1;
151 }
152
153 lws_sul_schedule(context, 0, &sul, connect_cb, 100);
154
155 while (n >= 0 && !interrupted)
156 n = lws_service(context, 0);
157
158 lws_context_destroy(context);
159 lwsl_user("Completed\n");
160
161 return 0;
162 }
163