• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-minimal-secure-streams-server
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 
10 #include <libwebsockets.h>
11 
12 extern int interrupted, bad;
13 
14 typedef struct myss {
15 	struct lws_ss_handle 	*ss;
16 	void			*opaque_data;
17 	/* ... application specific state ... */
18 	lws_sorted_usec_list_t	sul;
19 
20 	int			count;
21 } myss_t;
22 
23 /* secure streams payload interface */
24 
25 static lws_ss_state_return_t
myss_rx(void * userobj,const uint8_t * buf,size_t len,int flags)26 myss_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
27 {
28 //	myss_t *m = (myss_t *)userobj;
29 
30 	lwsl_user("%s: len %d, flags: %d\n", __func__, (int)len, flags);
31 	lwsl_hexdump_info(buf, len);
32 
33 	/*
34 	 * If we received the whole message, for our example it means
35 	 * we are done.
36 	 */
37 	if (flags & LWSSS_FLAG_EOM) {
38 		bad = 0;
39 		interrupted = 1;
40 	}
41 
42 	return 0;
43 }
44 
45 static lws_ss_state_return_t
myss_tx(void * userobj,lws_ss_tx_ordinal_t ord,uint8_t * buf,size_t * len,int * flags)46 myss_tx(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
47 	int *flags)
48 {
49 	//myss_t *m = (myss_t *)userobj;
50 
51 	return LWSSSSRET_TX_DONT_SEND; /* don't want to write */
52 }
53 
54 static lws_ss_state_return_t
myss_state(void * userobj,void * sh,lws_ss_constate_t state,lws_ss_tx_ordinal_t ack)55 myss_state(void *userobj, void *sh, lws_ss_constate_t state,
56 	   lws_ss_tx_ordinal_t ack)
57 {
58 	myss_t *m = (myss_t *)userobj;
59 
60 	lwsl_user("%s: %p %s, ord 0x%x\n", __func__, m->ss,
61 		  lws_ss_state_name((int)state), (unsigned int)ack);
62 
63 	switch (state) {
64 	case LWSSSCS_CREATING:
65 		return lws_ss_request_tx(m->ss);
66 		break;
67 	case LWSSSCS_ALL_RETRIES_FAILED:
68 		/* if we're out of retries, we want to close the app and FAIL */
69 		interrupted = 1;
70 		break;
71 	default:
72 		break;
73 	}
74 
75 	return 0;
76 }
77 
78 const lws_ss_info_t ssi_client = {
79 	.handle_offset			= offsetof(myss_t, ss),
80 	.opaque_user_data_offset	= offsetof(myss_t, opaque_data),
81 	.streamtype			= "mintest",
82 	.rx				= myss_rx,
83 	.tx				= myss_tx,
84 	.state				= myss_state,
85 	.user_alloc			= sizeof(myss_t),
86 };
87