1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* lws-minimal-secure-streams-threads
*
* Written in 2010-2021 by Andy Green <andy@warmcat.com>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
*
* This demonstrates how other threads can wake the lws event loop and ask it
* to do things via lws_cancel_service(), notifying Secure Streams using the
* LWSSSCS_EVENT_WAIT_CANCELLED state callback.
*
* Because of what we're testing, we don't actually connect the SS just create
* it and wait for the states we are testing for to come at 10Hz.
*
* We run the test for 3s and check we got an appropriate amount of wakes
* to call it a success.
*
* You can use the same pattern to have any amount of shared data protected by
* the mutex, containing whatever the other threads want the lws event loop
* thread to do for them.
*/
#include <libwebsockets.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
/*
* Define this to cause an ss api access from a foreign thread, it will
* assert. This is for testing lws, don't do this in your code.
*/
// #define DO_ILLEGAL_API_THREAD
static int interrupted, bad = 1, finished;
static lws_sorted_usec_list_t sul_timeout;
static struct lws_context *context;
static pthread_t pthread_spam;
static int wakes, started_thread;
#if defined(DO_ILLEGAL_API_THREAD)
static struct lws_ss_handle *ss; /* only needed for DO_ILLEGAL_API_THREAD */
#endif
/* the data shared between the spam thread and the lws event loop */
static pthread_mutex_t lock_shared;
static int shared_counter;
#if !defined(LWS_SS_USE_SSPC)
static const char * const default_ss_policy =
"{"
"\"schema-version\":1,"
"\"s\": ["
"{"
"\"mintest\": {"
"\"endpoint\": \"connectivitycheck.android.com\","
"\"http_url\": \"generate_204\","
"\"port\": 80,"
"\"protocol\": \"h1\","
"\"http_method\": \"GET\","
"\"opportunistic\": true,"
"\"http_expect\": 204,"
"\"http_fail_redirect\": true"
"}"
"}"
"]"
"}"
;
#endif
typedef struct myss {
struct lws_ss_handle *ss;
void *opaque_data;
/* ... application specific state ... */
} myss_t;
static void *
thread_spam(void *d)
{
do {
pthread_mutex_lock(&lock_shared); /* --------- shared lock { */
/*
* prepare the shared data area to indicate whatever it is that
* we want doing on the main event loop. In this case, we just
* bump a counter, but it can be any amount of data prepared,
* eg, whole info struct for a connection we want.
*/
shared_counter++;
lwsl_notice("%s: cancelling wait from spam thread: %d\n",
__func__, shared_counter);
lws_cancel_service(context);
#if defined(DO_ILLEGAL_API_THREAD)
/*
* ILLEGAL...
* We cannot call any other lws api from a foreign thread
*/
if (ss)
lws_ss_request_tx(ss);
#endif
pthread_mutex_unlock(&lock_shared); /* } shared lock ------- */
usleep(100000); /* wait 100ms and signal main thread again */
} while (!finished);
pthread_exit(NULL);
return NULL;
}
static lws_ss_state_return_t
myss_state(void *userobj, void *h_src, lws_ss_constate_t state,
lws_ss_tx_ordinal_t ack)
{
// myss_t *m = (myss_t *)userobj;
void *retval;
switch (state) {
case LWSSSCS_CREATING:
if (pthread_create(&pthread_spam, NULL, thread_spam, NULL)) {
lwsl_err("thread creation failed\n");
return LWSSSSRET_DESTROY_ME;
}
started_thread = 1;
break;
case LWSSSCS_DESTROYING:
finished = 1;
if (started_thread)
pthread_join(pthread_spam, &retval);
break;
case LWSSSCS_EVENT_WAIT_CANCELLED:
pthread_mutex_lock(&lock_shared); /* --------- shared lock { */
lwsl_notice("%s: LWSSSCS_EVENT_WAIT_CANCELLED: %d, shared: %d\n",
__func__, ++wakes, shared_counter);
pthread_mutex_unlock(&lock_shared); /* } shared lock ------- */
break;
default:
break;
}
return LWSSSSRET_OK;
}
static const lws_ss_info_t ssi_lws_threads = {
.handle_offset = offsetof(myss_t, ss),
.opaque_user_data_offset = offsetof(myss_t, opaque_data),
/* we don't actually do any rx or tx in this test */
.state = myss_state,
.user_alloc = sizeof(myss_t),
.streamtype = "mintest",
.manual_initial_tx_credit = 0,
};
static void
sul_timeout_cb(lws_sorted_usec_list_t *sul)
{
lwsl_notice("%s: test finishing\n", __func__);
interrupted = 1;
}
static void
sigint_handler(int sig)
{
interrupted = 1;
}
static int
system_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
int current, int target)
{
if (current != LWS_SYSTATE_OPERATIONAL || target != LWS_SYSTATE_OPERATIONAL)
return 0;
/* the test SS.. not going to connect it, just see if the cancel_service
* messages are coming
*/
if (lws_ss_create(context, 0, &ssi_lws_threads, NULL,
#if defined(DO_ILLEGAL_API_THREAD)
&ss,
#else
NULL,
#endif
NULL, NULL)) {
lwsl_err("%s: failed to create secure stream\n",
__func__);
return -1;
}
/* set up the test timeout */
lws_sul_schedule(context, 0, &sul_timeout, sul_timeout_cb,
3 * LWS_US_PER_SEC);
return 0;
}
int main(int argc, const char **argv)
{
lws_state_notify_link_t notifier = { { NULL, NULL, NULL},
system_notify_cb, "app" };
lws_state_notify_link_t *na[] = { ¬ifier, NULL };
struct lws_context_creation_info info;
signal(SIGINT, sigint_handler);
memset(&info, 0, sizeof info);
lws_cmdline_option_handle_builtin(argc, argv, &info);
lwsl_user("LWS Secure Streams threads test client [-d<verb>]\n");
info.fd_limit_per_thread = 1 + 6 + 1;
info.port = CONTEXT_PORT_NO_LISTEN;
#if !defined(LWS_SS_USE_SSPC)
info.pss_policies_json = default_ss_policy;
#else
info.protocols = lws_sspc_protocols;
{
const char *p;
/* connect to ssproxy via UDS by default, else via
* tcp connection to this port */
if ((p = lws_cmdline_option(argc, argv, "-p")))
info.ss_proxy_port = (uint16_t)atoi(p);
/* UDS "proxy.ss.lws" in abstract namespace, else this socket
* path; when -p given this can specify the network interface
* to bind to */
if ((p = lws_cmdline_option(argc, argv, "-i")))
info.ss_proxy_bind = p;
/* if -p given, -a specifies the proxy address to connect to */
if ((p = lws_cmdline_option(argc, argv, "-a")))
info.ss_proxy_address = p;
}
#endif
info.options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
info.register_notifier_list = na;
/* create the context */
context = lws_create_context(&info);
if (!context) {
lwsl_err("lws init failed\n");
return 1;
}
#if defined(LWS_SS_USE_SSPC)
if (!lws_create_vhost(context, &info)) {
lwsl_err("%s: failed to create default vhost\n", __func__);
goto bail;
}
#endif
/* the event loop */
while (lws_service(context, 0) >= 0 && !interrupted)
;
/* compare what happened with what we expect */
if (wakes > 10)
/* OSX can do the usleep thread slower than 100ms */
bad = 0;
lwsl_notice("wakes %d\n", wakes);
#if defined(LWS_SS_USE_SSPC)
bail:
#endif
lws_sul_cancel(&sul_timeout);
lws_context_destroy(context);
lwsl_user("Completed: %s\n", bad ? "failed" : "OK");
return bad;
}
|