• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-secure-streams-avs
3  *
4  * This file is made available under the Creative Commons CC0 1.0
5  * Universal Public Domain Dedication.
6  */
7 
8 #include <libwebsockets.h>
9 #include <string.h>
10 #include <signal.h>
11 
12 extern int
13 avs_example_start(struct lws_context *context);
14 
15 int interrupted, bad = 1;
16 static lws_state_notify_link_t nl;
17 
18 static const char *canned_root_token_payload =
19 	"grant_type=refresh_token"
20 	"&refresh_token=Atzr|IwEBIJedGXjDqsU_vMxykqOMg"
21 	"SHfYe3CPcedueWEMWSDMaDnEmiW8RlR1Kns7Cb4B-TOSnqp7ifVsY4BMY2B8tpHfO39XP"
22 	"zfu9HapGjTR458IyHX44FE71pWJkGZ79uVBpljP4sazJuk8XS3Oe_yLnm_DIO6fU1nU3Y"
23 	"0flYmsOiOAQE_gRk_pdlmEtHnpMA-9rLw3mkY5L89Ty9kUygBsiFaYatouROhbsTn8-jW"
24 	"k1zZLUDpT6ICtBXSnrCIg0pUbZevPFhTwdXd6eX-u4rq0W-XaDvPWFO7au-iPb4Zk5eZE"
25 	"iX6sissYrtNmuEXc2uHu7MnQO1hHCaTdIO2CANVumf-PHSD8xseamyh04sLV5JgFzY45S"
26 	"KvKMajiUZuLkMokOx86rjC2Hdkx5DO7G-dbG1ufBDG-N79pFMSs7Ck5pc283IdLoJkCQc"
27 	"AGvTX8o8I29QqkcGou-9TKhOJmpX8As94T61ok0UqqEKPJ7RhfQHHYdCtsdwxgvfVr9qI"
28 	"xL_hDCcTho8opCVX-6QhJHl6SQFlTw13"
29 	"&client_id="
30 		"amzn1.application-oa2-client.4823334c434b4190a2b5a42c07938a2d";
31 
32 static int
app_system_state_nf(lws_state_manager_t * mgr,lws_state_notify_link_t * link,int current,int target)33 app_system_state_nf(lws_state_manager_t *mgr, lws_state_notify_link_t *link,
34 		    int current, int target)
35 {
36 	struct lws_context *context = lws_system_context_from_system_mgr(mgr);
37 	lws_system_blob_t *ab = lws_system_get_blob(context,
38 				LWS_SYSBLOB_TYPE_AUTH, 1 /* AUTH_IDX_ROOT */);
39 	size_t size;
40 
41 	/*
42 	 * For the things we care about, let's notice if we are trying to get
43 	 * past them when we haven't solved them yet, and make the system
44 	 * state wait while we trigger the dependent action.
45 	 */
46 	switch (target) {
47 	case LWS_SYSTATE_REGISTERED:
48 		size = lws_system_blob_get_size(ab);
49 		if (size)
50 			break;
51 
52 		/* let's register our canned root token so auth can use it */
53 		lws_system_blob_direct_set(ab,
54 				(const uint8_t *)canned_root_token_payload,
55 				strlen(canned_root_token_payload));
56 		break;
57 	case LWS_SYSTATE_OPERATIONAL:
58 		if (current == LWS_SYSTATE_OPERATIONAL)
59 			avs_example_start(context);
60 		break;
61 	case LWS_SYSTATE_POLICY_INVALID:
62 		/*
63 		 * This is a NOP since we used direct set... but in a real
64 		 * system this could easily change to be done on the heap, then
65 		 * this would be important
66 		 */
67 		lws_system_blob_destroy(lws_system_get_blob(context,
68 					LWS_SYSBLOB_TYPE_AUTH,
69 					1 /* AUTH_IDX_ROOT */));
70 		break;
71 	}
72 
73 	return 0;
74 }
75 
76 static void
sigint_handler(int sig)77 sigint_handler(int sig)
78 {
79 	interrupted = 1;
80 }
81 
82 static lws_state_notify_link_t * const app_notifier_list[] = {
83 	&nl, NULL
84 };
85 
main(int argc,const char ** argv)86 int main(int argc, const char **argv)
87 {
88 	struct lws_context_creation_info info;
89 	struct lws_context *context;
90 	int n = 0;
91 
92 	signal(SIGINT, sigint_handler);
93 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
94 	lws_cmdline_option_handle_builtin(argc, argv, &info);
95 
96 	lwsl_user("LWS secure streams - AVS test client [-d<verb>]\n");
97 
98 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
99 	info.fd_limit_per_thread = 1 + 6 + 1;
100 	info.protocols = lws_sspc_protocols;
101 	info.port = CONTEXT_PORT_NO_LISTEN;
102 
103 	/* integrate us with lws system state management when context created */
104 	nl.name = "app";
105 	nl.notify_cb = app_system_state_nf;
106 	info.register_notifier_list = app_notifier_list;
107 
108 	context = lws_create_context(&info);
109 	if (!context) {
110 		lwsl_err("lws init failed\n");
111 		return 1;
112 	}
113 
114 	/* the event loop */
115 
116 	while (n >= 0 && !interrupted)
117 		n = lws_service(context, 0);
118 
119 	lws_context_destroy(context);
120 	lwsl_user("Completed: %s\n", bad ? "failed" : "OK");
121 
122 	return bad;
123 }
124