1d4afb5ceSopenharmony_ci# Secure Streams
2d4afb5ceSopenharmony_ci
3d4afb5ceSopenharmony_ciSecure Streams is a networking api that strictly separates payload from any
4d4afb5ceSopenharmony_cimetadata.  That includes the client endpoint address for the connection, the tls
5d4afb5ceSopenharmony_citrust chain and even the protocol used to connect to the endpoint.
6d4afb5ceSopenharmony_ci
7d4afb5ceSopenharmony_ciThe user api just receives and transmits payload, and receives advisory
8d4afb5ceSopenharmony_ciconnection state information.
9d4afb5ceSopenharmony_ci
10d4afb5ceSopenharmony_ciThe details about how the connections for different types of secure stream should
11d4afb5ceSopenharmony_cibe made are held in JSON "policy database" initially passed in to the context
12d4afb5ceSopenharmony_cicreation, but able to be updated from a remote copy.
13d4afb5ceSopenharmony_ci
14d4afb5ceSopenharmony_ciBoth client and server networking can be handled using Secure Streams APIS.
15d4afb5ceSopenharmony_ci
16d4afb5ceSopenharmony_ci![overview](/doc-assets/ss-operation-modes.png)
17d4afb5ceSopenharmony_ci
18d4afb5ceSopenharmony_ci## Secure Streams CLIENT State lifecycle
19d4afb5ceSopenharmony_ci
20d4afb5ceSopenharmony_ci![overview](/doc-assets/ss-state-flow.png)
21d4afb5ceSopenharmony_ci
22d4afb5ceSopenharmony_ciSecure Streams are created using `lws_ss_create()`, after that they may acquire
23d4afb5ceSopenharmony_ciunderlying connections, and lose them, but the lifecycle of the Secure Stream
24d4afb5ceSopenharmony_ciitself is not directly related to any underlying connection.
25d4afb5ceSopenharmony_ci
26d4afb5ceSopenharmony_ciOnce created, Secure Streams may attempt connections, these may fail and once
27d4afb5ceSopenharmony_cithe number of failures exceeds the count of attempts to conceal in the retry /
28d4afb5ceSopenharmony_cibackoff policy, the stream reaches `LWSSSCS_ALL_RETRIES_FAILED`.  The stream becomes
29d4afb5ceSopenharmony_ciidle again until another explicit connection attempt is given.
30d4afb5ceSopenharmony_ci
31d4afb5ceSopenharmony_ciOnce connected, the user code can use `lws_ss_request_tx()` to ask for a slot
32d4afb5ceSopenharmony_cito write to the peer, when this if forthcoming the tx handler can send a message.
33d4afb5ceSopenharmony_ciIf the underlying protocol gives indications of transaction success, such as,
34d4afb5ceSopenharmony_cieg, a 200 for http, or an ACK from MQTT, the stream state is called back with
35d4afb5ceSopenharmony_cian `LWSSSCS_QOS_ACK_REMOTE` or `LWSSSCS_QOS_NACK_REMOTE`.
36d4afb5ceSopenharmony_ci
37d4afb5ceSopenharmony_ci## SS Callback return handling
38d4afb5ceSopenharmony_ci
39d4afb5ceSopenharmony_ciSS state(), rx() and tx() can indicate with their return code some common
40d4afb5ceSopenharmony_cisituations that should be handled by the caller.
41d4afb5ceSopenharmony_ci
42d4afb5ceSopenharmony_ciConstant|Scope|Meaning
43d4afb5ceSopenharmony_ci---|---|---
44d4afb5ceSopenharmony_ciLWSSSSRET_TX_DONT_SEND|tx|This opportunity to send something was passed on
45d4afb5ceSopenharmony_ciLWSSSSRET_OK|state, rx, tx|No error, continue doing what we're doing
46d4afb5ceSopenharmony_ciLWSSSSRET_DISCONNECT_ME|state, rx|assertively disconnect from peer
47d4afb5ceSopenharmony_ciLWSSSSRET_DESTROY_ME|state, rx|Caller should now destroy the stream itself
48d4afb5ceSopenharmony_ciLWSSSSRET_SS_HANDLE_DESTROYED|state|Something handled a request to destroy the stream
49d4afb5ceSopenharmony_ci
50d4afb5ceSopenharmony_ciDestruction of the stream we're calling back on inside the callback is tricky,
51d4afb5ceSopenharmony_ciit's preferable to return `LWSSSSRET_DESTROY_ME` if it is required, and let the
52d4afb5ceSopenharmony_cicaller handle it.  But in some cases, helpers called from the callbacks may
53d4afb5ceSopenharmony_cidestroy the handle themselves, in that case the handler should return
54d4afb5ceSopenharmony_ci`LWSSSSRET_SS_HANDLE_DESTROYED` indicating that the handle is already destroyed.
55d4afb5ceSopenharmony_ci
56d4afb5ceSopenharmony_ci## Secure Streams SERVER State lifecycle
57d4afb5ceSopenharmony_ci
58d4afb5ceSopenharmony_ci![overview](/doc-assets/ss-state-flow-server.png)
59d4afb5ceSopenharmony_ci
60d4afb5ceSopenharmony_ciYou can also run servers defined using Secure Streams, the main difference is
61d4afb5ceSopenharmony_cithat the user code must assertively create a secure stream of the server type
62d4afb5ceSopenharmony_ciin order to create the vhost and listening socket.  When this stream is
63d4afb5ceSopenharmony_cidestroyed, the vhost is destroyed and the listen socket closed, otherwise it
64d4afb5ceSopenharmony_cidoes not perform any rx or tx, it just represents the server lifecycle.
65d4afb5ceSopenharmony_ci
66d4afb5ceSopenharmony_ciWhen client connections randomly arrive at the listen socket, new Secure Stream
67d4afb5ceSopenharmony_ciobjects are created along with accept sockets to represent each client
68d4afb5ceSopenharmony_ciconnection.  As they represent the incoming connection, their lifecycle is the
69d4afb5ceSopenharmony_cisame as that of the underlying connection.  There is no retry concept since as
70d4afb5ceSopenharmony_ciwith eg, http servers, the clients may typically not be routable for new
71d4afb5ceSopenharmony_ciconnections initiated by the server.
72d4afb5ceSopenharmony_ci
73d4afb5ceSopenharmony_ciSince connections at socket level are already established, new connections are
74d4afb5ceSopenharmony_ciimmediately taken through CREATING, CONNECTING, CONNECTED states for
75d4afb5ceSopenharmony_ciconsistency.
76d4afb5ceSopenharmony_ci
77d4afb5ceSopenharmony_ciSome underlying protocols like http are "transactional", the server receives
78d4afb5ceSopenharmony_cia logical request and must reply with a logical response.  The additional
79d4afb5ceSopenharmony_cistate `LWSSSCS_SERVER_TXN` provides a point where the user code can set
80d4afb5ceSopenharmony_citransaction metadata before or in place of sending any payload.  It's also
81d4afb5ceSopenharmony_cipossible to defer this until any rx related to the transaction was received,
82d4afb5ceSopenharmony_cibut commonly with http requests, there is no rx / body.  Configuring the
83d4afb5ceSopenharmony_ciresponse there may look like
84d4afb5ceSopenharmony_ci
85d4afb5ceSopenharmony_ci```
86d4afb5ceSopenharmony_ci		/*
87d4afb5ceSopenharmony_ci		 * We do want to ack the transaction...
88d4afb5ceSopenharmony_ci		 */
89d4afb5ceSopenharmony_ci		lws_ss_server_ack(m->ss, 0);
90d4afb5ceSopenharmony_ci		/*
91d4afb5ceSopenharmony_ci		 * ... it's going to be text/html...
92d4afb5ceSopenharmony_ci		 */
93d4afb5ceSopenharmony_ci		lws_ss_set_metadata(m->ss, "mime", "text/html", 9);
94d4afb5ceSopenharmony_ci		/*
95d4afb5ceSopenharmony_ci		 * ...it's going to be 128 byte (and request tx)
96d4afb5ceSopenharmony_ci		 */
97d4afb5ceSopenharmony_ci		lws_ss_request_tx_len(m->ss, 128);
98d4afb5ceSopenharmony_ci```
99d4afb5ceSopenharmony_ci
100d4afb5ceSopenharmony_ciOtherwise the general api usage is very similar to client usage.
101d4afb5ceSopenharmony_ci
102d4afb5ceSopenharmony_ci## Convention for rx and tx callback return
103d4afb5ceSopenharmony_ci
104d4afb5ceSopenharmony_ciFunction|Return|Meaning
105d4afb5ceSopenharmony_ci---|---|---
106d4afb5ceSopenharmony_citx|`LWSSSSRET_OK`|Send the amount of `buf` stored in `*len`
107d4afb5ceSopenharmony_citx|`LWSSSSRET_TX_DONT_SEND`|Do not send anything
108d4afb5ceSopenharmony_citx|`LWSSSSRET_DISCONNECT_ME`|Close the current connection
109d4afb5ceSopenharmony_citx|`LWSSSSRET_DESTROY_ME`|Destroy the Secure Stream
110d4afb5ceSopenharmony_cirx|>=0|accepted
111d4afb5ceSopenharmony_cirx|<0|Close the current connection
112d4afb5ceSopenharmony_ci
113d4afb5ceSopenharmony_ci# JSON Policy Database
114d4afb5ceSopenharmony_ci
115d4afb5ceSopenharmony_ciExample JSON policy... formatting is shown for clarity but whitespace can be
116d4afb5ceSopenharmony_ciomitted in the actual policy.
117d4afb5ceSopenharmony_ci
118d4afb5ceSopenharmony_ciOrdering is not critical in itself, but forward references are not allowed,
119d4afb5ceSopenharmony_cithings must be defined before they are allowed to be referenced later in the
120d4afb5ceSopenharmony_ciJSON.
121d4afb5ceSopenharmony_ci
122d4afb5ceSopenharmony_ci
123d4afb5ceSopenharmony_ci```
124d4afb5ceSopenharmony_ci{
125d4afb5ceSopenharmony_ci	"release": "01234567",
126d4afb5ceSopenharmony_ci	"product": "myproduct",
127d4afb5ceSopenharmony_ci	"schema-version": 1,
128d4afb5ceSopenharmony_ci	"retry": [{
129d4afb5ceSopenharmony_ci		"default": {
130d4afb5ceSopenharmony_ci			"backoff": [1000, 2000, 3000, 5000, 10000],
131d4afb5ceSopenharmony_ci			"conceal": 5,
132d4afb5ceSopenharmony_ci			"jitterpc": 20
133d4afb5ceSopenharmony_ci		}
134d4afb5ceSopenharmony_ci	}],
135d4afb5ceSopenharmony_ci	"certs": [{
136d4afb5ceSopenharmony_ci		"isrg_root_x1": "MIIFazCCA1OgAw...AnX5iItreGCc="
137d4afb5ceSopenharmony_ci	}, {
138d4afb5ceSopenharmony_ci		"LEX3_isrg_root_x1": "MIIFjTCCA3WgAwIB...WEsikxqEt"
139d4afb5ceSopenharmony_ci	}],
140d4afb5ceSopenharmony_ci	"trust_stores": [{
141d4afb5ceSopenharmony_ci		"le_via_isrg": ["isrg_root_x1", "LEX3_isrg_root_x1"]
142d4afb5ceSopenharmony_ci	}],
143d4afb5ceSopenharmony_ci	"s": [{
144d4afb5ceSopenharmony_ci		"mintest": {
145d4afb5ceSopenharmony_ci			"endpoint": "warmcat.com",
146d4afb5ceSopenharmony_ci			"port": 4443,
147d4afb5ceSopenharmony_ci			"protocol": "h1get",
148d4afb5ceSopenharmony_ci			"aux": "index.html",
149d4afb5ceSopenharmony_ci			"plugins": [],
150d4afb5ceSopenharmony_ci			"tls": true,
151d4afb5ceSopenharmony_ci			"opportunistic": true,
152d4afb5ceSopenharmony_ci			"retry": "default",
153d4afb5ceSopenharmony_ci			"tls_trust_store": "le_via_isrg"
154d4afb5ceSopenharmony_ci		}
155d4afb5ceSopenharmony_ci	}]
156d4afb5ceSopenharmony_ci}
157d4afb5ceSopenharmony_ci```
158d4afb5ceSopenharmony_ci
159d4afb5ceSopenharmony_ci### `Release`
160d4afb5ceSopenharmony_ci
161d4afb5ceSopenharmony_ciIdentifies the policy version
162d4afb5ceSopenharmony_ci
163d4afb5ceSopenharmony_ci### `Product`
164d4afb5ceSopenharmony_ci
165d4afb5ceSopenharmony_ciIdentifies the product the policy should apply to
166d4afb5ceSopenharmony_ci
167d4afb5ceSopenharmony_ci### `Schema-version`
168d4afb5ceSopenharmony_ci
169d4afb5ceSopenharmony_ciThe minimum version of the policy parser required to parse this policy
170d4afb5ceSopenharmony_ci
171d4afb5ceSopenharmony_ci### `via-socks5`
172d4afb5ceSopenharmony_ci
173d4afb5ceSopenharmony_ciOptional redirect for Secure Streams client traffic through a socks5
174d4afb5ceSopenharmony_ciproxy given in the format `address:port`, eg, `127.0.0.1:12345`.
175d4afb5ceSopenharmony_ci
176d4afb5ceSopenharmony_ci### `retry`
177d4afb5ceSopenharmony_ci
178d4afb5ceSopenharmony_ciA list of backoff schemes referred to in the policy
179d4afb5ceSopenharmony_ci
180d4afb5ceSopenharmony_ci### `backoff`
181d4afb5ceSopenharmony_ci
182d4afb5ceSopenharmony_ciAn array of ms delays for each retry in turn
183d4afb5ceSopenharmony_ci
184d4afb5ceSopenharmony_ci### `conceal`
185d4afb5ceSopenharmony_ci
186d4afb5ceSopenharmony_ciThe number of retries to conceal from higher layers before giving errors.  If
187d4afb5ceSopenharmony_cithis is larger than the number of times in the backoff array, then the last time
188d4afb5ceSopenharmony_ciis used for the extra delays.  65535 means never stop trying.
189d4afb5ceSopenharmony_ci
190d4afb5ceSopenharmony_ci### `jitterpc`
191d4afb5ceSopenharmony_ci
192d4afb5ceSopenharmony_ciPercentage of the delay times mentioned in the backoff array that may be
193d4afb5ceSopenharmony_cirandomly added to the figure from the array.  For example with an array entry of
194d4afb5ceSopenharmony_ci1000ms, and jitterpc of 20%, actual delays will be chosen randomly from 1000ms
195d4afb5ceSopenharmony_cithrough 1200ms.  This is to stop retry storms triggered by a single event like
196d4afb5ceSopenharmony_cian outage becoming synchronized into a DoS.
197d4afb5ceSopenharmony_ci
198d4afb5ceSopenharmony_ci### `certs`
199d4afb5ceSopenharmony_ci
200d4afb5ceSopenharmony_ciCertificates needed for validation should be listed here each with a name.  The
201d4afb5ceSopenharmony_ciformat is base64 DER, which is the same as the part of PEM that is inside the
202d4afb5ceSopenharmony_cistart and end lines.
203d4afb5ceSopenharmony_ci
204d4afb5ceSopenharmony_ci### `trust_stores`
205d4afb5ceSopenharmony_ci
206d4afb5ceSopenharmony_ciChains of certificates given in the `certs` section may be named and described
207d4afb5ceSopenharmony_ciinside the `trust_stores` section.  Each entry in `trust_stores` is created as
208d4afb5ceSopenharmony_cia vhost + tls context with the given name.  Stream types can later be associated
209d4afb5ceSopenharmony_ciwith one of these to enforce validity checking of the remote server.
210d4afb5ceSopenharmony_ci
211d4afb5ceSopenharmony_ciEntries should be named using "name" and the stack array defined using "stack"
212d4afb5ceSopenharmony_ci
213d4afb5ceSopenharmony_ci### `auth`
214d4afb5ceSopenharmony_ci
215d4afb5ceSopenharmony_ciOptional section describing a map of available authentication streamtypes to
216d4afb5ceSopenharmony_ciauth token blob indexes.
217d4afb5ceSopenharmony_ci
218d4afb5ceSopenharmony_ci```
219d4afb5ceSopenharmony_ci...
220d4afb5ceSopenharmony_ci "auth": [{"name":"newauth","type":"sigv4", "blob":0}]
221d4afb5ceSopenharmony_ci...
222d4afb5ceSopenharmony_ci```
223d4afb5ceSopenharmony_ci
224d4afb5ceSopenharmony_ciStreams can indicate they depend on a valid auth token from one of these schemes
225d4afb5ceSopenharmony_ciby using the `"use_auth": "name"` member in the streamtype definition, where name
226d4afb5ceSopenharmony_ciis, eg, "sigv4" in the example above.  If "use_auth" is not in the streamtype
227d4afb5ceSopenharmony_cidefinition, default auth is lwa if "http_auth_header" is there.
228d4afb5ceSopenharmony_ci
229d4afb5ceSopenharmony_ci### `auth[].name`
230d4afb5ceSopenharmony_ci
231d4afb5ceSopenharmony_ciThis is the name of the authentication scheme used by other streamtypes
232d4afb5ceSopenharmony_ci
233d4afb5ceSopenharmony_ci### `auth[].type`
234d4afb5ceSopenharmony_ci
235d4afb5ceSopenharmony_ciIndicate the auth type, e.g. sigv4
236d4afb5ceSopenharmony_ci
237d4afb5ceSopenharmony_ci### `auth[].streamtype`
238d4afb5ceSopenharmony_ci
239d4afb5ceSopenharmony_ciThis is the auth streamtype to be used to refresh the authentication token
240d4afb5ceSopenharmony_ci
241d4afb5ceSopenharmony_ci### `auth[].blob`
242d4afb5ceSopenharmony_ci
243d4afb5ceSopenharmony_ciThis is the auth blob index the authentication token is stored into and retreived
244d4afb5ceSopenharmony_cifrom system blob, currently up to 4 blobs.
245d4afb5ceSopenharmony_ci
246d4afb5ceSopenharmony_ci
247d4afb5ceSopenharmony_ci### `s`
248d4afb5ceSopenharmony_ci
249d4afb5ceSopenharmony_ciThese are an array of policies for the supported stream type names.
250d4afb5ceSopenharmony_ci
251d4afb5ceSopenharmony_ci### `server`
252d4afb5ceSopenharmony_ci
253d4afb5ceSopenharmony_ci**SERVER ONLY**: if set to `true`, the policy describes a secure streams
254d4afb5ceSopenharmony_ciserver.
255d4afb5ceSopenharmony_ci
256d4afb5ceSopenharmony_ci### `endpoint`
257d4afb5ceSopenharmony_ci
258d4afb5ceSopenharmony_ci**CLIENT**: The DNS address the secure stream should connect to.
259d4afb5ceSopenharmony_ci
260d4afb5ceSopenharmony_ciThis may contain string symbols which will be replaced with the
261d4afb5ceSopenharmony_cicorresponding streamtype metadata value at runtime.  Eg, if the
262d4afb5ceSopenharmony_cistreamtype lists a metadata name "region", it's then possible to
263d4afb5ceSopenharmony_cidefine the endpoint as, eg, `${region}.mysite.com`, and before
264d4afb5ceSopenharmony_ciattempting the connection setting the stream's metadata item
265d4afb5ceSopenharmony_ci"region" to the desired value, eg, "uk".
266d4afb5ceSopenharmony_ci
267d4afb5ceSopenharmony_ciIf the endpoint string begins with `+`, then it's understood to
268d4afb5ceSopenharmony_cimean a connection to a Unix Domain Socket, for Linux `+@` means
269d4afb5ceSopenharmony_cithe following Unix Domain Socket is in the Linux Abstract
270d4afb5ceSopenharmony_ciNamespace and doesn't have a filesystem footprint.  This is only
271d4afb5ceSopenharmony_cisupported on unix-type and windows platforms and when lws was
272d4afb5ceSopenharmony_ciconfigured with `-DLWS_UNIX_SOCK=1`
273d4afb5ceSopenharmony_ci
274d4afb5ceSopenharmony_ci**SERVER**: If given, the network interface name or IP address the listen socket
275d4afb5ceSopenharmony_cishould bind to.
276d4afb5ceSopenharmony_ci
277d4afb5ceSopenharmony_ci**SERVER**: If begins with '!', the rest of the endpoint name is the
278d4afb5ceSopenharmony_civhost name of an existing vhost to bind to, instead of creating a new
279d4afb5ceSopenharmony_cione.  This is useful when the vhost layout is already being managed by
280d4afb5ceSopenharmony_cilejp-conf JSON and it's more convenient to put the details in there.
281d4afb5ceSopenharmony_ci
282d4afb5ceSopenharmony_ci### `port`
283d4afb5ceSopenharmony_ci
284d4afb5ceSopenharmony_ci**CLIENT**: The port number as an integer on the endpoint to connect to
285d4afb5ceSopenharmony_ci
286d4afb5ceSopenharmony_ci**SERVER**: The port number the server will listen on
287d4afb5ceSopenharmony_ci
288d4afb5ceSopenharmony_ci### `protocol`
289d4afb5ceSopenharmony_ci
290d4afb5ceSopenharmony_ci**CLIENT**: The wire protocol to connect to the endpoint with.  Currently
291d4afb5ceSopenharmony_cisupported streamtypes are
292d4afb5ceSopenharmony_ci
293d4afb5ceSopenharmony_ci|Wire protocol|Description|
294d4afb5ceSopenharmony_ci|---|---|
295d4afb5ceSopenharmony_ci|h1|http/1|
296d4afb5ceSopenharmony_ci|h2|http/2|
297d4afb5ceSopenharmony_ci|ws|http/1 Websockets|
298d4afb5ceSopenharmony_ci|mqtt|mqtt 3.1.1|
299d4afb5ceSopenharmony_ci|raw||
300d4afb5ceSopenharmony_ci
301d4afb5ceSopenharmony_ciRaw protocol is a bit different than the others in that there is no protocol framing,
302d4afb5ceSopenharmony_ciwhatever is received on the connection is passed to the user rx callback and whatever
303d4afb5ceSopenharmony_cithe tx callback provides is issued on to the connection.  Because tcp can be
304d4afb5ceSopenharmony_ciarbitrarily fragmented by any intermediary, such streams have to be regarded as an
305d4afb5ceSopenharmony_ciordered bytestream that may be fragmented at any byte without any meaning in terms
306d4afb5ceSopenharmony_ciof message boundaries, for that reason SOM and EOM are ignored with raw.
307d4afb5ceSopenharmony_ci
308d4afb5ceSopenharmony_ci### `allow_redirects`
309d4afb5ceSopenharmony_ci
310d4afb5ceSopenharmony_ciBy default redirects are not followed, if you wish a streamtype to observe them, eg,
311d4afb5ceSopenharmony_cibecause that's how it responds to a POST, set `"allow_redirects": true`
312d4afb5ceSopenharmony_ci
313d4afb5ceSopenharmony_ci### `tls`
314d4afb5ceSopenharmony_ci
315d4afb5ceSopenharmony_ciSet to `true` to enforce the stream travelling in a tls tunnel
316d4afb5ceSopenharmony_ci
317d4afb5ceSopenharmony_ci### `client cert`
318d4afb5ceSopenharmony_ci
319d4afb5ceSopenharmony_ciSet if the stream needs to authenticate itself using a tls client certificate.
320d4afb5ceSopenharmony_ciSet to the certificate index counting from 0+.  The certificates are managed
321d4afb5ceSopenharmony_ciusing lws_sytstem blobs.
322d4afb5ceSopenharmony_ci
323d4afb5ceSopenharmony_ci### `opportunistic`
324d4afb5ceSopenharmony_ci
325d4afb5ceSopenharmony_ciSet to `true` if the connection may be left dropped except when in use
326d4afb5ceSopenharmony_ci
327d4afb5ceSopenharmony_ci### `nailed_up`
328d4afb5ceSopenharmony_ci
329d4afb5ceSopenharmony_ciSet to `true` to have lws retry if the connection carrying this stream should
330d4afb5ceSopenharmony_ciever drop.
331d4afb5ceSopenharmony_ci
332d4afb5ceSopenharmony_ci### `retry`
333d4afb5ceSopenharmony_ci
334d4afb5ceSopenharmony_ciThe name of the policy described in the `retry` section to apply to this
335d4afb5ceSopenharmony_ciconnection for retry + backoff
336d4afb5ceSopenharmony_ci
337d4afb5ceSopenharmony_ci### `timeout_ms`
338d4afb5ceSopenharmony_ci
339d4afb5ceSopenharmony_ciOptional timeout associated with streams of this streamtype.
340d4afb5ceSopenharmony_ci
341d4afb5ceSopenharmony_ciIf user code applies the `lws_ss_start_timeout()` api on a stream with a
342d4afb5ceSopenharmony_citimeout of LWSSS_TIMEOUT_FROM_POLICY, the `timeout_ms` entry given in the
343d4afb5ceSopenharmony_cipolicy is applied.
344d4afb5ceSopenharmony_ci
345d4afb5ceSopenharmony_ci### `perf`
346d4afb5ceSopenharmony_ci
347d4afb5ceSopenharmony_ciIf set to true, and lws was built with `LWS_WITH_CONMON`, causes this streamtype
348d4afb5ceSopenharmony_cito receive additional rx payload with the `LWSSS_FLAG_PERF_JSON` flag set on it,
349d4afb5ceSopenharmony_cithat is JSON representing the onward connection performance information.
350d4afb5ceSopenharmony_ci
351d4afb5ceSopenharmony_ciThese are based on the information captured in the struct defined in
352d4afb5ceSopenharmony_cilibwebsockets/lws-conmon.h, represented in JSON
353d4afb5ceSopenharmony_ci
354d4afb5ceSopenharmony_ci```
355d4afb5ceSopenharmony_ci	{
356d4afb5ceSopenharmony_ci	  "peer": "46.105.127.147",
357d4afb5ceSopenharmony_ci	  "dns_us": 1234,
358d4afb5ceSopenharmony_ci	  "sockconn_us": 1234,
359d4afb5ceSopenharmony_ci	  "tls_us": 1234,
360d4afb5ceSopenharmony_ci	  "txn_resp_us": 1234,
361d4afb5ceSopenharmony_ci	  "dns":["46.105.127.147", "2001:41d0:2:ee93::1"]
362d4afb5ceSopenharmony_ci	}
363d4afb5ceSopenharmony_ci```
364d4afb5ceSopenharmony_ci
365d4afb5ceSopenharmony_ciStreamtypes without "perf": true will never see the special rx payloads.
366d4afb5ceSopenharmony_ciNotice that the `LWSSS_FLAG_PERF_JSON` payloads must be handled out of band
367d4afb5ceSopenharmony_cifor the normal payloads, as they can appear inside normal payload messages.
368d4afb5ceSopenharmony_ci
369d4afb5ceSopenharmony_ci### `tls_trust_store`
370d4afb5ceSopenharmony_ci
371d4afb5ceSopenharmony_ciThe name of the trust store described in the `trust_stores` section to apply
372d4afb5ceSopenharmony_cito validate the remote server cert.
373d4afb5ceSopenharmony_ci
374d4afb5ceSopenharmony_ciIf missing and tls is enabled on the streamtype, then validation is
375d4afb5ceSopenharmony_ciattempted using the OS trust store, otherwise the connection fails.
376d4afb5ceSopenharmony_ci
377d4afb5ceSopenharmony_ci### `use_auth`
378d4afb5ceSopenharmony_ci
379d4afb5ceSopenharmony_ciIndicate that the streamtype should use the named auth type from the `auth`
380d4afb5ceSopenharmony_ciarray in the policy
381d4afb5ceSopenharmony_ci
382d4afb5ceSopenharmony_ci### `aws_region`
383d4afb5ceSopenharmony_ci
384d4afb5ceSopenharmony_ciIndicate which metadata should be used to set aws region for certain streamtype
385d4afb5ceSopenharmony_ci
386d4afb5ceSopenharmony_ci### `aws_service`
387d4afb5ceSopenharmony_ci
388d4afb5ceSopenharmony_ciIndicate which metadata should be used to set aws service for certain streamtype
389d4afb5ceSopenharmony_ci
390d4afb5ceSopenharmony_ci### `direct_proto_str`
391d4afb5ceSopenharmony_ci
392d4afb5ceSopenharmony_ciIf set to `true`, application can use `lws_ss_set_metadata()` to directly set protocol related string and use `lws_ss_get_metadata` to fetch certain protocol related string.  Please note that currently HTTP header is the supported protocol string.  The `name` parameter is the name of HTTP header name (**with ':'**, e.g. `"Content-Type:"`) and `value` is the header's value. `LWS_WITH_SS_DIRECT_PROTOCOL_STR` flag needs to be configured during compilation for this.  Currently it's only work for non-proxy case.
393d4afb5ceSopenharmony_ci
394d4afb5ceSopenharmony_ci### `server_cert`
395d4afb5ceSopenharmony_ci
396d4afb5ceSopenharmony_ci**SERVER ONLY**: subject to change... the name of the x.509 cert that is the
397d4afb5ceSopenharmony_ciserver's tls certificate
398d4afb5ceSopenharmony_ci
399d4afb5ceSopenharmony_ci### `server_key`
400d4afb5ceSopenharmony_ci
401d4afb5ceSopenharmony_ci**SERVER ONLY**: subject to change... the name of the x.509 cert that is the
402d4afb5ceSopenharmony_ciserver's tls key
403d4afb5ceSopenharmony_ci
404d4afb5ceSopenharmony_ci### `swake_validity`
405d4afb5ceSopenharmony_ci
406d4afb5ceSopenharmony_ciSet to `true` if this streamtype is important enough for the functioning of the
407d4afb5ceSopenharmony_cidevice that its locally-initiated periodic connection validity checks of the
408d4afb5ceSopenharmony_ciinterval described in the associated retry / backoff selection, are important
409d4afb5ceSopenharmony_cienough to wake the whole system from low power suspend so they happen on
410d4afb5ceSopenharmony_cischedule.
411d4afb5ceSopenharmony_ci
412d4afb5ceSopenharmony_ci### `proxy_buflen`
413d4afb5ceSopenharmony_ci
414d4afb5ceSopenharmony_ciOnly used when the streamtype is proxied... sets the maximum size of the
415d4afb5ceSopenharmony_cipayload buffering (in bytes) the proxy will hold for this type of stream.  If
416d4afb5ceSopenharmony_cithe endpoint dumps a lot of data without any flow control, this may need to
417d4afb5ceSopenharmony_cibe correspondingly large.  Default is 32KB.
418d4afb5ceSopenharmony_ci
419d4afb5ceSopenharmony_ci### `proxy_buflen_rxflow_on_above`, `proxy_buflen_rxflow_off_below`
420d4afb5ceSopenharmony_ci
421d4afb5ceSopenharmony_ciWhen `proxy_buflen` is set, you can also wire up the amount of buffered
422d4afb5ceSopenharmony_cidata intended for the client held at the proxy, to the onward ss wsi
423d4afb5ceSopenharmony_cirx flow control state.  If more than `proxy_buflen_rxflow_on_above`
424d4afb5ceSopenharmony_cibytes are buffered, rx flow control is set stopping further rx.  Once
425d4afb5ceSopenharmony_cithe dsh is drained below `proxy_buflen_rxflow_off_below`, the rx flow
426d4afb5ceSopenharmony_cicontrol is released and RX resumes.
427d4afb5ceSopenharmony_ci
428d4afb5ceSopenharmony_ci### `client_buflen`
429d4afb5ceSopenharmony_ci
430d4afb5ceSopenharmony_ciOnly used when the streamtype is proxied... sets the maximum size of the
431d4afb5ceSopenharmony_cipayload buffering (in bytes) the client will hold for this type of stream.  If
432d4afb5ceSopenharmony_cithe client sends a lot of data without any flow control, this may need to
433d4afb5ceSopenharmony_cibe correspondingly large.  Default is 32KB.
434d4afb5ceSopenharmony_ci
435d4afb5ceSopenharmony_ci### `attr_priority`
436d4afb5ceSopenharmony_ci
437d4afb5ceSopenharmony_ciA number between 0 (normal priority) and 6 (very high priority).  7 is also
438d4afb5ceSopenharmony_cipossible, but requires CAP_NET_ADMIN on Linux and is reserved for network
439d4afb5ceSopenharmony_ciadministration packets.  Normally default priority is fine, but under some
440d4afb5ceSopenharmony_ciconditions when transporting over IP packets, you may want to control the
441d4afb5ceSopenharmony_ciIP packet ToS priority for the streamtype by using this.
442d4afb5ceSopenharmony_ci
443d4afb5ceSopenharmony_ci### `attr_low_latency`
444d4afb5ceSopenharmony_ci
445d4afb5ceSopenharmony_ciThis is a flag indicating that the streamtype packets should be transported
446d4afb5ceSopenharmony_ciin a way that results in lower latency where there is a choice.  For IP packets,
447d4afb5ceSopenharmony_cithis sets the ToS "low delay" flag on packets from this streamtype.
448d4afb5ceSopenharmony_ci
449d4afb5ceSopenharmony_ci### `attr_high_throughput`
450d4afb5ceSopenharmony_ci
451d4afb5ceSopenharmony_ciThis is a flag indicating that this streamtype should be expected to produce
452d4afb5ceSopenharmony_cibulk content that requires high throughput.  For IP packets,
453d4afb5ceSopenharmony_cithis sets the ToS "high throughput" flag on packets from this streamtype.
454d4afb5ceSopenharmony_ci
455d4afb5ceSopenharmony_ci### `attr_high_reliability`
456d4afb5ceSopenharmony_ci
457d4afb5ceSopenharmony_ciThis is a flag indicating that extra efforts should be made to deliver packets
458d4afb5ceSopenharmony_cifrom this streamtype where possible.  For IP packets, this sets the ToS "high
459d4afb5ceSopenharmony_cireliability" flag on packets from this streamtype.
460d4afb5ceSopenharmony_ci
461d4afb5ceSopenharmony_ci### `attr_low_cost`
462d4afb5ceSopenharmony_ci
463d4afb5ceSopenharmony_ciThis is a flag indicating that packets from this streamtype should be routed as
464d4afb5ceSopenharmony_ciinexpensively as possible by trading off latency and reliability where there is
465d4afb5ceSopenharmony_cia choice.  For IP packets, this sets the ToS "low cost" flag on packets from
466d4afb5ceSopenharmony_cithis streamtype.
467d4afb5ceSopenharmony_ci
468d4afb5ceSopenharmony_ci### `metadata`
469d4afb5ceSopenharmony_ci
470d4afb5ceSopenharmony_ciThis allows declaring basically dynamic symbol names to be used by the streamtype,
471d4afb5ceSopenharmony_cialong with an optional mapping to a protocol-specific entity such as a given
472d4afb5ceSopenharmony_cihttp header.  Eg:
473d4afb5ceSopenharmony_ci
474d4afb5ceSopenharmony_ci```
475d4afb5ceSopenharmony_ci		"metadata": [ { "myname": "" }, { "ctype": "content-type:" } ],
476d4afb5ceSopenharmony_ci```
477d4afb5ceSopenharmony_ci
478d4afb5ceSopenharmony_ciIn this example "ctype" is associated with the http header "content-type" while
479d4afb5ceSopenharmony_ci"myname" doesn't have any association to a header.
480d4afb5ceSopenharmony_ci
481d4afb5ceSopenharmony_ciSymbol names may be used in the other policy for the streamtype for string
482d4afb5ceSopenharmony_cisubstitution using the syntax like `xxx${myname}yyy`, forward references are
483d4afb5ceSopenharmony_civalid but the scope of the symbols is just the streamtype the metadata is
484d4afb5ceSopenharmony_cidefined for.
485d4afb5ceSopenharmony_ci
486d4afb5ceSopenharmony_ciClient code can set metadata by name, using the `lws_ss_set_metadata()` api, this
487d4afb5ceSopenharmony_cishould be done before a transaction.  And for metadata associated with a
488d4afb5ceSopenharmony_ciprotocol-specific entity, like http headers, if incoming responses contain the
489d4afb5ceSopenharmony_cimentioned header, the metadata symbol is set to that value at the client before
490d4afb5ceSopenharmony_ciany rx proceeds.
491d4afb5ceSopenharmony_ci
492d4afb5ceSopenharmony_ciMetadata continues to work the same for the client in the case it is proxying its
493d4afb5ceSopenharmony_ciconnectivity, metadata is passed in both directions serialized over the proxy link.
494d4afb5ceSopenharmony_ci
495d4afb5ceSopenharmony_ci## http transport
496d4afb5ceSopenharmony_ci
497d4afb5ceSopenharmony_ci### `http_method`
498d4afb5ceSopenharmony_ci
499d4afb5ceSopenharmony_ciHTTP method to use with http-related protocols, like GET or POST.
500d4afb5ceSopenharmony_ciNot required for ws.
501d4afb5ceSopenharmony_ci
502d4afb5ceSopenharmony_ci### `http_expect`
503d4afb5ceSopenharmony_ci
504d4afb5ceSopenharmony_ciOptionally indicates that success for HTTP transactions using this
505d4afb5ceSopenharmony_cistreamtype is different than the default 200 - 299.
506d4afb5ceSopenharmony_ci
507d4afb5ceSopenharmony_ciEg, you may choose to set this to 204 for Captive Portal Detect usage
508d4afb5ceSopenharmony_ciif that's what you expect the server to reply with to indicate
509d4afb5ceSopenharmony_cisuccess.  In that case, anything other than 204 will be treated as a
510d4afb5ceSopenharmony_ciconnection failure.
511d4afb5ceSopenharmony_ci
512d4afb5ceSopenharmony_ci### `http_fail_redirect`
513d4afb5ceSopenharmony_ci
514d4afb5ceSopenharmony_ciSet to `true` if you want to fail the connection on meeting an
515d4afb5ceSopenharmony_cihttp redirect.  This is needed to, eg, detect Captive Portals
516d4afb5ceSopenharmony_cicorrectly.  Normally, if on https, you would want the default behaviour
517d4afb5ceSopenharmony_ciof following the redirect.
518d4afb5ceSopenharmony_ci
519d4afb5ceSopenharmony_ci### `http_url`
520d4afb5ceSopenharmony_ci
521d4afb5ceSopenharmony_ciUrl path to use with http-related protocols
522d4afb5ceSopenharmony_ci
523d4afb5ceSopenharmony_ciThe URL path can include metatadata like this
524d4afb5ceSopenharmony_ci
525d4afb5ceSopenharmony_ci"/mypath?whatever=${metadataname}"
526d4afb5ceSopenharmony_ci
527d4afb5ceSopenharmony_ci${metadataname} will be replaced by the current value of the
528d4afb5ceSopenharmony_cisame metadata name.  The metadata names must be listed in the
529d4afb5ceSopenharmony_ci"metadata": [ ] section.
530d4afb5ceSopenharmony_ci
531d4afb5ceSopenharmony_ci### `http_resp_map`
532d4afb5ceSopenharmony_ci
533d4afb5ceSopenharmony_ciIf your server overloads the meaning of the http transport response code with
534d4afb5ceSopenharmony_ciserver-custom application codes, you can map these to discrete Secure Streams
535d4afb5ceSopenharmony_cistate callbacks using a JSON map, eg
536d4afb5ceSopenharmony_ci
537d4afb5ceSopenharmony_ci```
538d4afb5ceSopenharmony_ci		"http_resp_map": [ { "530": 1530 }, { "531": 1531 } ],
539d4afb5ceSopenharmony_ci```
540d4afb5ceSopenharmony_ci
541d4afb5ceSopenharmony_ciIt's not recommended to abuse the transport layer http response code by
542d4afb5ceSopenharmony_cimixing it with application state information like this, but if it's dealing
543d4afb5ceSopenharmony_ciwith legacy serverside that takes this approach, it's possible to handle it
544d4afb5ceSopenharmony_ciin SS this way while removing the dependency on http.
545d4afb5ceSopenharmony_ci
546d4afb5ceSopenharmony_ci### `http_auth_header`
547d4afb5ceSopenharmony_ci
548d4afb5ceSopenharmony_ciThe name of the header that takes the auth token, with a trailing ':', eg
549d4afb5ceSopenharmony_ci
550d4afb5ceSopenharmony_ci```
551d4afb5ceSopenharmony_ci  "http_auth_header": "authorization:"
552d4afb5ceSopenharmony_ci```
553d4afb5ceSopenharmony_ci
554d4afb5ceSopenharmony_ci### `http_dsn_header`
555d4afb5ceSopenharmony_ci
556d4afb5ceSopenharmony_ciThe name of the header that takes the dsn token, with a trailing ':', eg
557d4afb5ceSopenharmony_ci
558d4afb5ceSopenharmony_ci```
559d4afb5ceSopenharmony_ci  "http_dsn_header": "x-dsn:"
560d4afb5ceSopenharmony_ci```
561d4afb5ceSopenharmony_ci
562d4afb5ceSopenharmony_ci### `http_fwv_header`
563d4afb5ceSopenharmony_ci
564d4afb5ceSopenharmony_ciThe name of the header that takes the firmware version token, with a trailing ':', eg
565d4afb5ceSopenharmony_ci
566d4afb5ceSopenharmony_ci```
567d4afb5ceSopenharmony_ci  "http_fwv_header": "x-fw-version:"
568d4afb5ceSopenharmony_ci```
569d4afb5ceSopenharmony_ci
570d4afb5ceSopenharmony_ci### `http_devtype_header`
571d4afb5ceSopenharmony_ci
572d4afb5ceSopenharmony_ciThe name of the header that takes the device type token, with a trailing ':', eg
573d4afb5ceSopenharmony_ci
574d4afb5ceSopenharmony_ci```
575d4afb5ceSopenharmony_ci  "http_devtype_header": "x-device-type:"
576d4afb5ceSopenharmony_ci```
577d4afb5ceSopenharmony_ci
578d4afb5ceSopenharmony_ci### `http_auth_preamble`
579d4afb5ceSopenharmony_ci
580d4afb5ceSopenharmony_ciAn optional string that precedes the auth token, eg
581d4afb5ceSopenharmony_ci
582d4afb5ceSopenharmony_ci```
583d4afb5ceSopenharmony_ci "http_auth_preamble": "bearer "
584d4afb5ceSopenharmony_ci```
585d4afb5ceSopenharmony_ci
586d4afb5ceSopenharmony_ci### `auth_hexify`
587d4afb5ceSopenharmony_ci
588d4afb5ceSopenharmony_ciConvert the auth token to hex ('A' -> "41") before transporting.  Not necessary if the
589d4afb5ceSopenharmony_ciauth token is already in printable string format suitable for transport.  Needed if the
590d4afb5ceSopenharmony_ciauth token is a chunk of 8-bit binary.
591d4afb5ceSopenharmony_ci
592d4afb5ceSopenharmony_ci### `nghttp2_quirk_end_stream`
593d4afb5ceSopenharmony_ci
594d4afb5ceSopenharmony_ciSet this to `true` if the peer server has the quirk it won't send a response until we have
595d4afb5ceSopenharmony_cisent an `END_STREAM`, even though we have sent headers with `END_HEADERS`.
596d4afb5ceSopenharmony_ci
597d4afb5ceSopenharmony_ci### `h2q_oflow_txcr`
598d4afb5ceSopenharmony_ci
599d4afb5ceSopenharmony_ciSet this to `true` if the peer server has the quirk it sends an maximum initial tx credit
600d4afb5ceSopenharmony_ciof 0x7fffffff and then later increments it illegally.
601d4afb5ceSopenharmony_ci
602d4afb5ceSopenharmony_ci### `http_multipart_ss_in`
603d4afb5ceSopenharmony_ci
604d4afb5ceSopenharmony_ciIndicates that SS should parse any incoming multipart mime on this stream
605d4afb5ceSopenharmony_ci
606d4afb5ceSopenharmony_ci### `http_multipart_name`
607d4afb5ceSopenharmony_ci
608d4afb5ceSopenharmony_ciIndicates this stream goes out using multipart mime, and provides the name part of the
609d4afb5ceSopenharmony_cimultipart header
610d4afb5ceSopenharmony_ci
611d4afb5ceSopenharmony_ci### `http_multipart_filename`
612d4afb5ceSopenharmony_ci
613d4afb5ceSopenharmony_ciIndicates this stream goes out using multipart mime, and provides the filename part of the
614d4afb5ceSopenharmony_cimultipart header
615d4afb5ceSopenharmony_ci
616d4afb5ceSopenharmony_ci### `http_multipart_content_type`
617d4afb5ceSopenharmony_ci
618d4afb5ceSopenharmony_ciThe `content-type` to mark up the multipart mime section with if present
619d4afb5ceSopenharmony_ci
620d4afb5ceSopenharmony_ci### `http_www_form_urlencoded`
621d4afb5ceSopenharmony_ci
622d4afb5ceSopenharmony_ciIndicate the data is sent in `x-www-form-urlencoded` form
623d4afb5ceSopenharmony_ci
624d4afb5ceSopenharmony_ci### `http_cookies`
625d4afb5ceSopenharmony_ci
626d4afb5ceSopenharmony_ciThis streamtype should store and bring out http cookies from the peer.
627d4afb5ceSopenharmony_ci
628d4afb5ceSopenharmony_ci### `rideshare`
629d4afb5ceSopenharmony_ci
630d4afb5ceSopenharmony_ciFor special cases where one logically separate stream travels with another when using this
631d4afb5ceSopenharmony_ciprotocol.  Eg, a single multipart mime transaction carries content from two or more streams.
632d4afb5ceSopenharmony_ci
633d4afb5ceSopenharmony_ci## ws transport
634d4afb5ceSopenharmony_ci
635d4afb5ceSopenharmony_ci### `ws_subprotocol`
636d4afb5ceSopenharmony_ci
637d4afb5ceSopenharmony_ci** CLIENT **: Name of the ws subprotocol to request from the server
638d4afb5ceSopenharmony_ci
639d4afb5ceSopenharmony_ci** SERVER **: Name of the subprotocol we will accept
640d4afb5ceSopenharmony_ci
641d4afb5ceSopenharmony_ci### `ws_binary`
642d4afb5ceSopenharmony_ci
643d4afb5ceSopenharmony_ciUse if the ws messages are binary
644d4afb5ceSopenharmony_ci
645d4afb5ceSopenharmony_ci### `ws_prioritize_reads`
646d4afb5ceSopenharmony_ci
647d4afb5ceSopenharmony_ciSet `true` if the event loop should prioritize keeping up with input at the
648d4afb5ceSopenharmony_cipotential expense of output latency.
649d4afb5ceSopenharmony_ci
650d4afb5ceSopenharmony_ci## MQTT transport
651d4afb5ceSopenharmony_ci
652d4afb5ceSopenharmony_ci### `mqtt_topic`
653d4afb5ceSopenharmony_ci
654d4afb5ceSopenharmony_ciSet the topic this streamtype uses for writes
655d4afb5ceSopenharmony_ci
656d4afb5ceSopenharmony_ci### `mqtt_subscribe`
657d4afb5ceSopenharmony_ci
658d4afb5ceSopenharmony_ciSet the topic this streamtype subscribes to
659d4afb5ceSopenharmony_ci
660d4afb5ceSopenharmony_ci### `mqtt qos`
661d4afb5ceSopenharmony_ci
662d4afb5ceSopenharmony_ciSet the QOS level for this streamtype
663d4afb5ceSopenharmony_ci
664d4afb5ceSopenharmony_ci### `mqtt_retain`
665d4afb5ceSopenharmony_ci
666d4afb5ceSopenharmony_ciSet to true if this streamtype should use MQTT's "retain" feature.
667d4afb5ceSopenharmony_ci
668d4afb5ceSopenharmony_ci### `mqtt_keep_alive`
669d4afb5ceSopenharmony_ci
670d4afb5ceSopenharmony_ci16-bit number representing MQTT keep alive for the stream.
671d4afb5ceSopenharmony_ci
672d4afb5ceSopenharmony_ciThis is applied at connection time... where different streams may bind to the
673d4afb5ceSopenharmony_cisame underlying MQTT connection, all the streams should have an identical
674d4afb5ceSopenharmony_cisetting for this.
675d4afb5ceSopenharmony_ci
676d4afb5ceSopenharmony_ci### `mqtt_clean_start`
677d4afb5ceSopenharmony_ci
678d4afb5ceSopenharmony_ciSet to true if the connection should use MQTT's "clean start" feature.
679d4afb5ceSopenharmony_ci
680d4afb5ceSopenharmony_ciThis is applied at connection time... where different streams may bind to the
681d4afb5ceSopenharmony_cisame underlying MQTT connection, all the streams should have an identical
682d4afb5ceSopenharmony_cisetting for this.
683d4afb5ceSopenharmony_ci
684d4afb5ceSopenharmony_ci### `mqtt_will_topic`
685d4afb5ceSopenharmony_ci
686d4afb5ceSopenharmony_ciSet the topic of the connection's will message, if any (there is none by default).
687d4afb5ceSopenharmony_ci
688d4afb5ceSopenharmony_ciThis is applied at connection time... where different streams may bind to the
689d4afb5ceSopenharmony_cisame underlying MQTT connection, all the streams should have an identical
690d4afb5ceSopenharmony_cisetting for this.
691d4afb5ceSopenharmony_ci
692d4afb5ceSopenharmony_ci### `mqtt_will_message`
693d4afb5ceSopenharmony_ci
694d4afb5ceSopenharmony_ciSet the content of the connect's will message, if any (there is none by default).
695d4afb5ceSopenharmony_ci
696d4afb5ceSopenharmony_ciThis is applied at connection time... where different streams may bind to the
697d4afb5ceSopenharmony_cisame underlying MQTT connection, all the streams should have an identical
698d4afb5ceSopenharmony_cisetting for this.
699d4afb5ceSopenharmony_ci
700d4afb5ceSopenharmony_ci### `mqtt_will_qos`
701d4afb5ceSopenharmony_ci
702d4afb5ceSopenharmony_ciSet the QoS of the will message, if any (there is none by default).
703d4afb5ceSopenharmony_ci
704d4afb5ceSopenharmony_ciThis is applied at connection time... where different streams may bind to the
705d4afb5ceSopenharmony_cisame underlying MQTT connection, all the streams should have an identical
706d4afb5ceSopenharmony_cisetting for this.
707d4afb5ceSopenharmony_ci
708d4afb5ceSopenharmony_ci### `mqtt_will_retain`
709d4afb5ceSopenharmony_ci
710d4afb5ceSopenharmony_ciSet to true if the connection should use MQTT's "will retain" feature, if there
711d4afb5ceSopenharmony_ciis a will message (there is none by default).
712d4afb5ceSopenharmony_ci
713d4afb5ceSopenharmony_ciThis is applied at connection time... where different streams may bind to the
714d4afb5ceSopenharmony_cisame underlying MQTT connection, all the streams should have an identical
715d4afb5ceSopenharmony_cisetting for this.
716d4afb5ceSopenharmony_ci
717d4afb5ceSopenharmony_ci## Loading and using updated remote policy
718d4afb5ceSopenharmony_ci
719d4afb5ceSopenharmony_ciIf the default, hardcoded policy includes a streamtype `fetch_policy`,
720d4afb5ceSopenharmony_ciduring startup when lws_system reaches the POLICY state, lws will use
721d4afb5ceSopenharmony_cia Secure Stream of type `fetch_policy` to download, parse and update
722d4afb5ceSopenharmony_cithe policy to use it.
723d4afb5ceSopenharmony_ci
724d4afb5ceSopenharmony_ciThe secure-streams-proxy minimal example shows how this is done and
725d4afb5ceSopenharmony_cifetches its real policy from warmcat.com at startup using the built-in
726d4afb5ceSopenharmony_cione.
727d4afb5ceSopenharmony_ci
728d4afb5ceSopenharmony_ci## Applying streamtype policy overlays
729d4afb5ceSopenharmony_ci
730d4afb5ceSopenharmony_ciThis is intended for modifying policies at runtime for testing, eg, to
731d4afb5ceSopenharmony_ciforce error paths to be taken.  After the main policy is processed, you
732d4afb5ceSopenharmony_cimay parse additional, usually smaller policy fragments on top of it.
733d4afb5ceSopenharmony_ci
734d4afb5ceSopenharmony_ciWhere streamtype names in the new fragment already exist in the current
735d4afb5ceSopenharmony_ciparsed policy, the settings in the fragment are applied over the parsed
736d4afb5ceSopenharmony_cipolicy, overriding settings.  There's a simple api to enable this by
737d4afb5ceSopenharmony_cigiving it the override JSON in one string
738d4afb5ceSopenharmony_ci
739d4afb5ceSopenharmony_ci```
740d4afb5ceSopenharmony_ciint
741d4afb5ceSopenharmony_cilws_ss_policy_overlay(struct lws_context *context, const char *overlay);
742d4afb5ceSopenharmony_ci```
743d4afb5ceSopenharmony_ci
744d4afb5ceSopenharmony_cibut there are also other apis available that can statefully process
745d4afb5ceSopenharmony_cilarger overlay fragments if needed.
746d4afb5ceSopenharmony_ci
747d4afb5ceSopenharmony_ciAn example overlay fragment looks like this
748d4afb5ceSopenharmony_ci
749d4afb5ceSopenharmony_ci```
750d4afb5ceSopenharmony_ci	{ "s": [{ "captive_portal_detect": {
751d4afb5ceSopenharmony_ci		"endpoint": "google.com",
752d4afb5ceSopenharmony_ci		"http_url": "/",
753d4afb5ceSopenharmony_ci		"port": 80
754d4afb5ceSopenharmony_ci	}}]}
755d4afb5ceSopenharmony_ci```
756d4afb5ceSopenharmony_ci
757d4afb5ceSopenharmony_ciie the overlay fragment completely follows the structure of the main policy,
758d4afb5ceSopenharmony_cijust misses out anything it doesn't override.
759d4afb5ceSopenharmony_ci
760d4afb5ceSopenharmony_ciCurrently ONLY streamtypes may be overridden.
761d4afb5ceSopenharmony_ci
762d4afb5ceSopenharmony_ciYou can see an example of this in use in `minimal-secure-streams` example
763d4afb5ceSopenharmony_ciwhere `--force-portal` and `--force-no-internet` options cause the captive
764d4afb5ceSopenharmony_ciportal detect streamtype to be overridden to force the requested kind of
765d4afb5ceSopenharmony_cioutcome.
766d4afb5ceSopenharmony_ci
767d4afb5ceSopenharmony_ci## Captive Portal Detection
768d4afb5ceSopenharmony_ci
769d4afb5ceSopenharmony_ciIf the policy contains a streamtype `captive_portal_detect` then the
770d4afb5ceSopenharmony_citype of transaction described there is automatically performed after
771d4afb5ceSopenharmony_ciacquiring a DHCP address to try to determine the captive portal
772d4afb5ceSopenharmony_cisituation.
773d4afb5ceSopenharmony_ci
774d4afb5ceSopenharmony_ci```
775d4afb5ceSopenharmony_ci		"captive_portal_detect": {
776d4afb5ceSopenharmony_ci                        "endpoint": "connectivitycheck.android.com",
777d4afb5ceSopenharmony_ci                        "port": 80,
778d4afb5ceSopenharmony_ci                        "protocol": "h1",
779d4afb5ceSopenharmony_ci                        "http_method": "GET",
780d4afb5ceSopenharmony_ci                        "http_url": "generate_204",
781d4afb5ceSopenharmony_ci                        "opportunistic": true,
782d4afb5ceSopenharmony_ci                        "http_expect": 204,
783d4afb5ceSopenharmony_ci			"http_fail_redirect": true
784d4afb5ceSopenharmony_ci                }
785d4afb5ceSopenharmony_ci```
786d4afb5ceSopenharmony_ci
787d4afb5ceSopenharmony_ci## Stream serialization and proxying
788d4afb5ceSopenharmony_ci
789d4afb5ceSopenharmony_ciBy default Secure Streams expects to make the outgoing connection described in
790d4afb5ceSopenharmony_cithe policy in the same process / thread, this suits the case where all the
791d4afb5ceSopenharmony_ciparticipating clients are in the same statically-linked image.
792d4afb5ceSopenharmony_ci
793d4afb5ceSopenharmony_ciIn this case the `lws_ss_` apis are fulfilled locally by secure-streams.c and
794d4afb5ceSopenharmony_cipolicy.c for policy lookups.
795d4afb5ceSopenharmony_ci
796d4afb5ceSopenharmony_ciHowever it also supports serialization, where the SS api can be streamed over
797d4afb5ceSopenharmony_cianother transport such as a Unix Domain Socket connection.  This suits the case
798d4afb5ceSopenharmony_ciwhere the clients are actually in different processes in, eg, Linux or Android.
799d4afb5ceSopenharmony_ci
800d4afb5ceSopenharmony_ciIn those cases, you run a proxy process (minimal-secure-streams-proxy) that
801d4afb5ceSopenharmony_cilistens on a Unix Domain Socket and is connected to by one or more other
802d4afb5ceSopenharmony_ciprocesses that pass their SS API activity to the proxy for fulfilment (or
803d4afb5ceSopenharmony_cionward proxying).
804d4afb5ceSopenharmony_ci
805d4afb5ceSopenharmony_ciEach Secure Stream that is created then in turn creates a private Unix Domain
806d4afb5ceSopenharmony_ciSocket connection to the proxy for each stream.
807d4afb5ceSopenharmony_ci
808d4afb5ceSopenharmony_ciIn this case the proxy uses secure-streams.c and policy.c as before to fulfil
809d4afb5ceSopenharmony_cithe inbound proxy streams, but uses secure-streams-serialize.c to serialize and
810d4afb5ceSopenharmony_cideserialize the proxied SS API activity.  The proxy clients define
811d4afb5ceSopenharmony_ciLWS_SS_USE_SSPC either very early in their sources before the includes, or on
812d4afb5ceSopenharmony_cithe compiler commandline... this causes the lws_ss_ apis to be replaced at
813d4afb5ceSopenharmony_cipreprocessor time with lws_sspc_ equivalents.  These serialize the api action
814d4afb5ceSopenharmony_ciand pass it to the proxy over a Unix Domain Socket for fulfilment, the results
815d4afb5ceSopenharmony_ciand state changes etc are streamed over the Unix Domain Socket and presented to
816d4afb5ceSopenharmony_cithe application exactly the same as if it was being fulfilled locally.
817d4afb5ceSopenharmony_ci
818d4afb5ceSopenharmony_ciTo demonstrate this, some minimal examples, eg, minimal-secure-streams and
819d4afb5ceSopenharmony_cimimimal-secure-streams-avs build themselves both ways, once with direct SS API
820d4afb5ceSopenharmony_cifulfilment and once with Unix Domain Socket proxying and -client appended on the
821d4afb5ceSopenharmony_ciexecutable name.  To test the -client variants, run minimal-secure-streams-proxy
822d4afb5ceSopenharmony_cion the same machine.
823d4afb5ceSopenharmony_ci
824d4afb5ceSopenharmony_ci## Complicated scenarios with secure streams proxy
825d4afb5ceSopenharmony_ci
826d4afb5ceSopenharmony_ciAs mentioned above, Secure Streams has two modes, by default the application
827d4afb5ceSopenharmony_cidirectly parses the policy and makes the outgoing connections itself.
828d4afb5ceSopenharmony_ciHowever when configured at cmake with
829d4afb5ceSopenharmony_ci
830d4afb5ceSopenharmony_ci```
831d4afb5ceSopenharmony_ci-DLWS_WITH_SOCKS5=1 -DLWS_WITH_SECURE_STREAMS=1 -DLWS_WITH_SECURE_STREAMS_PROXY_API=1 -DLWS_WITH_MINIMAL_EXAMPLES=1
832d4afb5ceSopenharmony_ci```
833d4afb5ceSopenharmony_ci
834d4afb5ceSopenharmony_ciand define `LWS_SS_USE_SSPC` when building the application, applications forward
835d4afb5ceSopenharmony_citheir network requests to a local or remote SS proxy for fulfilment... and only
836d4afb5ceSopenharmony_cithe SS proxy has the system policy.  By default, the SS proxy is on the local
837d4afb5ceSopenharmony_cimachine and is connected to via a Unix Domain Socket, but tcp links are also
838d4afb5ceSopenharmony_cipossible.  (Note the proxied traffic is not encrypyed by default.)
839d4afb5ceSopenharmony_ci
840d4afb5ceSopenharmony_ciUsing the configuration above, the example SS applications are built two ways,
841d4afb5ceSopenharmony_cionce for direct connection fulfilment (eg, `./bin/lws-minimal-secure-streams`),
842d4afb5ceSopenharmony_ciand once with `LWS_SS_USE_SSPC` also defined so it connects via an SS proxy,
843d4afb5ceSopenharmony_ci(eg, `./bin/lws-minimal-secure-streams-client`).
844d4afb5ceSopenharmony_ci
845d4afb5ceSopenharmony_ci## Testing an example scenario with SS Proxy and socks5 proxy
846d4afb5ceSopenharmony_ci
847d4afb5ceSopenharmony_ci```
848d4afb5ceSopenharmony_ci [ SS application ] --- tcp --- [ socks 5 proxy ] --- tcp --- [ SS proxy ] --- internet
849d4afb5ceSopenharmony_ci```
850d4afb5ceSopenharmony_ci
851d4afb5ceSopenharmony_ciIn this scenario, everything is on localhost, the socks5 proxy listens on :1337 and
852d4afb5ceSopenharmony_cithe SS proxy listens on :1234.  The SS application connects to the socks5
853d4afb5ceSopenharmony_ciproxy to get to the SS proxy, which then goes out to the internet
854d4afb5ceSopenharmony_ci
855d4afb5ceSopenharmony_ci### 1 Start the SS proxy
856d4afb5ceSopenharmony_ci
857d4afb5ceSopenharmony_ciTell it to listen on lo interface on port 1234
858d4afb5ceSopenharmony_ci
859d4afb5ceSopenharmony_ci```
860d4afb5ceSopenharmony_ci$ ./bin/lws-minimal-secure-streams-proxy -p 1234 -i lo
861d4afb5ceSopenharmony_ci```
862d4afb5ceSopenharmony_ci
863d4afb5ceSopenharmony_ci### 2 Start the SOCKS5 proxy
864d4afb5ceSopenharmony_ci
865d4afb5ceSopenharmony_ci```
866d4afb5ceSopenharmony_ci$ ssh -D 1337 -N -v localhost
867d4afb5ceSopenharmony_ci```
868d4afb5ceSopenharmony_ci
869d4afb5ceSopenharmony_ciThe -v makes connections to the proxy visible in the terminal for testing
870d4afb5ceSopenharmony_ci
871d4afb5ceSopenharmony_ci### 3 Run the SS application
872d4afb5ceSopenharmony_ci
873d4afb5ceSopenharmony_ciThe application is told to make all connections via the socks5 proxy at
874d4afb5ceSopenharmony_ci127.0.0.1:1337, and to fulfil its SS connections via an SS proxy, binding
875d4afb5ceSopenharmony_ciconnections to 127.0.0.1 (ipv4 lo interface, -1), to 127.0.0.1:1234 (-a/-p).
876d4afb5ceSopenharmony_ci
877d4afb5ceSopenharmony_ci```
878d4afb5ceSopenharmony_cisocks_proxy=127.0.0.1:1337 ./bin/lws-minimal-secure-streams-client -p 1234 -i 127.0.0.1 -a 127.0.0.1
879d4afb5ceSopenharmony_ci```
880d4afb5ceSopenharmony_ci
881d4afb5ceSopenharmony_ciYou can confirm this goes through the ssh socks5 proxy to get to the SS proxy
882d4afb5ceSopenharmony_ciand fulfil the connection.
883d4afb5ceSopenharmony_ci
884d4afb5ceSopenharmony_ci## Using static policies
885d4afb5ceSopenharmony_ci
886d4afb5ceSopenharmony_ciIf one of your targets is too constrained to make use of dynamic JSON policies, but
887d4afb5ceSopenharmony_ciusing SS and the policies is attractive for wider reasons, you can use a static policy
888d4afb5ceSopenharmony_cibuilt into the firmware for the constrained target.
889d4afb5ceSopenharmony_ci
890d4afb5ceSopenharmony_ciThe secure-streams example "policy2c" (which runs on the build machine, not the device)
891d4afb5ceSopenharmony_ci
892d4afb5ceSopenharmony_cihttps://libwebsockets.org/git/libwebsockets/tree/minimal-examples/secure-streams/minimal-secure-streams-policy2c
893d4afb5ceSopenharmony_ci
894d4afb5ceSopenharmony_ciaccepts a normal JSON policy on stdin, and emits a C code representation that can be
895d4afb5ceSopenharmony_ciincluded directly in the firmware.
896d4afb5ceSopenharmony_ci
897d4afb5ceSopenharmony_cihttps://libwebsockets.org/git/libwebsockets/tree/minimal-examples/secure-streams/minimal-secure-streams-staticpolicy/static-policy.h
898d4afb5ceSopenharmony_ci
899d4afb5ceSopenharmony_ciUsing this technique it's possible to standardize on maintaining JSON policies across a
900d4afb5ceSopenharmony_cirange of devices with different contraints, and use the C conversion of the policy on devices
901d4afb5ceSopenharmony_cithat are too small.
902d4afb5ceSopenharmony_ci
903d4afb5ceSopenharmony_ciThe Cmake option `LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY` should be enabled to use this
904d4afb5ceSopenharmony_cimode, it will not build the JSON parser (and the option for LEJP can also be disabled if
905d4afb5ceSopenharmony_ciyou're not otherwise using it, saving an additional couple of KB).
906d4afb5ceSopenharmony_ci
907d4afb5ceSopenharmony_ciNotice policy2c example tool must be built with `LWS_ROLE_H1`, `LWS_ROLE_H2`, `LWS_ROLE_WS`
908d4afb5ceSopenharmony_ciand `LWS_ROLE_MQTT` enabled so it can handle any kind of policy.
909d4afb5ceSopenharmony_ci
910d4afb5ceSopenharmony_ci## HTTP and ws serving
911d4afb5ceSopenharmony_ci
912d4afb5ceSopenharmony_ciAll ws servers start out as http servers... for that reason ws serving is
913d4afb5ceSopenharmony_cihandled as part of http serving, if you give the `ws_subprotocol` entry to the
914d4afb5ceSopenharmony_cistreamtype additionally, the server will also accept upgrades to ws.
915d4afb5ceSopenharmony_ci
916d4afb5ceSopenharmony_ciTo help the user code understand if the upgrade occurred, there's a special
917d4afb5ceSopenharmony_cistate `LWSSSCS_SERVER_UPGRADE`, so subsequent rx and tx can be understood to
918d4afb5ceSopenharmony_cihave come from the upgraded protocol.  To allow separation of rx and tx
919d4afb5ceSopenharmony_cihandling between http and ws, there's a ss api `lws_ss_change_handlers()`
920d4afb5ceSopenharmony_ciwhich allows dynamically setting SS handlers.
921d4afb5ceSopenharmony_ci
922d4afb5ceSopenharmony_ciSince the http and ws upgrade identity is encapsulated in one streamtype, the
923d4afb5ceSopenharmony_ciuser object for the server streamtype should contain related user data for both
924d4afb5ceSopenharmony_cihttp and ws underlying protocol identity.
925