• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-ws-client
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 a ws client that connects by default to libwebsockets.org
10  * dumb increment ws server.
11  */
12 
13 #include <libwebsockets.h>
14 #include <string.h>
15 #include <signal.h>
16 #include <pthread.h>
17 
18 /*
19  * This represents your object that "contains" the client connection and has
20  * the client connection bound to it
21  */
22 
23 static struct my_conn {
24 	lws_sorted_usec_list_t	sul;	     /* schedule connection retry */
25 	struct lws		*wsi;	     /* related wsi if any */
26 	uint16_t		retry_count; /* count of consequetive retries */
27 } mco;
28 
29 static struct lws_context *context;
30 static int interrupted, port = 443, ssl_connection = LCCSCF_USE_SSL;
31 static const char *server_address = "libwebsockets.org",
32 		  *pro = "dumb-increment-protocol";
33 
34 /*
35  * The retry and backoff policy we want to use for our client connections
36  */
37 
38 static const uint32_t backoff_ms[] = { 1000, 2000, 3000, 4000, 5000 };
39 
40 static const lws_retry_bo_t retry = {
41 	.retry_ms_table			= backoff_ms,
42 	.retry_ms_table_count		= LWS_ARRAY_SIZE(backoff_ms),
43 	.conceal_count			= LWS_ARRAY_SIZE(backoff_ms),
44 
45 	.secs_since_valid_ping		= 3,  /* force PINGs after secs idle */
46 	.secs_since_valid_hangup	= 10, /* hangup after secs idle */
47 
48 	.jitter_percent			= 20,
49 };
50 
51 /*
52  * Scheduled sul callback that starts the connection attempt
53  */
54 
55 static void
connect_client(lws_sorted_usec_list_t * sul)56 connect_client(lws_sorted_usec_list_t *sul)
57 {
58 	struct my_conn *mco = lws_container_of(sul, struct my_conn, sul);
59 	struct lws_client_connect_info i;
60 
61 	memset(&i, 0, sizeof(i));
62 
63 	i.context = context;
64 	i.port = port;
65 	i.address = server_address;
66 	i.path = "/";
67 	i.host = i.address;
68 	i.origin = i.address;
69 	i.ssl_connection = ssl_connection;
70 	i.protocol = pro;
71 	i.local_protocol_name = "lws-minimal-client";
72 	i.pwsi = &mco->wsi;
73 	i.retry_and_idle_policy = &retry;
74 	i.userdata = mco;
75 
76 	if (!lws_client_connect_via_info(&i))
77 		/*
78 		 * Failed... schedule a retry... we can't use the _retry_wsi()
79 		 * convenience wrapper api here because no valid wsi at this
80 		 * point.
81 		 */
82 		if (lws_retry_sul_schedule(context, 0, sul, &retry,
83 					   connect_client, &mco->retry_count)) {
84 			lwsl_err("%s: connection attempts exhausted\n", __func__);
85 			interrupted = 1;
86 		}
87 }
88 
89 static int
callback_minimal(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)90 callback_minimal(struct lws *wsi, enum lws_callback_reasons reason,
91 		 void *user, void *in, size_t len)
92 {
93 	struct my_conn *mco = (struct my_conn *)user;
94 
95 	switch (reason) {
96 
97 	case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
98 		lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
99 			 in ? (char *)in : "(null)");
100 		goto do_retry;
101 		break;
102 
103 	case LWS_CALLBACK_CLIENT_RECEIVE:
104 		lwsl_hexdump_notice(in, len);
105 		break;
106 
107 	case LWS_CALLBACK_CLIENT_ESTABLISHED:
108 		lwsl_user("%s: established\n", __func__);
109 		break;
110 
111 	case LWS_CALLBACK_CLIENT_CLOSED:
112 		goto do_retry;
113 
114 	default:
115 		break;
116 	}
117 
118 	return lws_callback_http_dummy(wsi, reason, user, in, len);
119 
120 do_retry:
121 	/*
122 	 * retry the connection to keep it nailed up
123 	 *
124 	 * For this example, we try to conceal any problem for one set of
125 	 * backoff retries and then exit the app.
126 	 *
127 	 * If you set retry.conceal_count to be larger than the number of
128 	 * elements in the backoff table, it will never give up and keep
129 	 * retrying at the last backoff delay plus the random jitter amount.
130 	 */
131 	if (lws_retry_sul_schedule_retry_wsi(wsi, &mco->sul, connect_client,
132 					     &mco->retry_count)) {
133 		lwsl_err("%s: connection attempts exhausted\n", __func__);
134 		interrupted = 1;
135 	}
136 
137 	return 0;
138 }
139 
140 static const struct lws_protocols protocols[] = {
141 	{ "lws-minimal-client", callback_minimal, 0, 0, },
142 	{ NULL, NULL, 0, 0 }
143 };
144 
145 static void
sigint_handler(int sig)146 sigint_handler(int sig)
147 {
148 	interrupted = 1;
149 }
150 
main(int argc,const char ** argv)151 int main(int argc, const char **argv)
152 {
153 	struct lws_context_creation_info info;
154 	const char *p;
155 	int n = 0;
156 
157 	signal(SIGINT, sigint_handler);
158 	memset(&info, 0, sizeof info);
159 	lws_cmdline_option_handle_builtin(argc, argv, &info);
160 
161 	lwsl_user("LWS minimal ws client\n");
162 
163 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
164 	info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */
165 	info.protocols = protocols;
166 
167 #if defined(LWS_WITH_MBEDTLS)
168 	/*
169 	 * OpenSSL uses the system trust store.  mbedTLS has to be told which
170 	 * CA to trust explicitly.
171 	 */
172 	info.client_ssl_ca_filepath = "./libwebsockets.org.cer";
173 #endif
174 
175 	if ((p = lws_cmdline_option(argc, argv, "--protocol")))
176 		pro = p;
177 
178 	if ((p = lws_cmdline_option(argc, argv, "-s")))
179 		server_address = p;
180 
181 	if ((p = lws_cmdline_option(argc, argv, "-p")))
182 		port = atoi(p);
183 
184 	if (lws_cmdline_option(argc, argv, "-j"))
185 		ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
186 
187 	if (lws_cmdline_option(argc, argv, "-k"))
188 		ssl_connection |= LCCSCF_ALLOW_INSECURE;
189 
190 	if (lws_cmdline_option(argc, argv, "-m"))
191 		ssl_connection |= LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
192 
193 	if (lws_cmdline_option(argc, argv, "-e"))
194 		ssl_connection |= LCCSCF_ALLOW_EXPIRED;
195 
196 	info.fd_limit_per_thread = 1 + 1 + 1;
197 
198 	context = lws_create_context(&info);
199 	if (!context) {
200 		lwsl_err("lws init failed\n");
201 		return 1;
202 	}
203 
204 	/* schedule the first client connection attempt to happen immediately */
205 	lws_sul_schedule(context, 0, &mco.sul, connect_client, 1);
206 
207 	while (n >= 0 && !interrupted)
208 		n = lws_service(context, 0);
209 
210 	lws_context_destroy(context);
211 	lwsl_user("Completed\n");
212 
213 	return 0;
214 }
215