1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * This provides a clean way to interface lws user code to be able to
25 * work unchanged on different systems for fetching common system information,
26 * and performing common system operations like reboot.
27 */
28
29/*
30 * Types of system blob that can be set and retreived
31 */
32
33typedef enum {
34 LWS_SYSBLOB_TYPE_AUTH,
35 LWS_SYSBLOB_TYPE_CLIENT_CERT_DER = LWS_SYSBLOB_TYPE_AUTH + 2,
36 LWS_SYSBLOB_TYPE_CLIENT_KEY_DER,
37 LWS_SYSBLOB_TYPE_DEVICE_SERIAL,
38 LWS_SYSBLOB_TYPE_DEVICE_FW_VERSION,
39 LWS_SYSBLOB_TYPE_DEVICE_TYPE,
40 LWS_SYSBLOB_TYPE_NTP_SERVER,
41 LWS_SYSBLOB_TYPE_MQTT_CLIENT_ID,
42 LWS_SYSBLOB_TYPE_MQTT_USERNAME,
43 LWS_SYSBLOB_TYPE_MQTT_PASSWORD,
44
45#if defined(LWS_WITH_SECURE_STREAMS_AUTH_SIGV4)
46 /* extend 4 more auth blobs, each has 2 slots */
47 LWS_SYSBLOB_TYPE_EXT_AUTH1,
48 LWS_SYSBLOB_TYPE_EXT_AUTH2 = LWS_SYSBLOB_TYPE_EXT_AUTH1 + 2,
49 LWS_SYSBLOB_TYPE_EXT_AUTH3 = LWS_SYSBLOB_TYPE_EXT_AUTH2 + 2,
50 LWS_SYSBLOB_TYPE_EXT_AUTH4 = LWS_SYSBLOB_TYPE_EXT_AUTH3 + 2,
51 LWS_SYSBLOB_TYPE_EXT_AUTH4_1,
52#endif
53
54 LWS_SYSBLOB_TYPE_COUNT /* ... always last */
55} lws_system_blob_item_t;
56
57/* opaque generic blob whose content may be on-the-heap or pointed-to
58 * directly case by case. When it's on the heap, it can be produced by
59 * appending (it's a buflist underneath). Either way, it can be consumed by
60 * copying out a given length from a given offset.
61 */
62
63typedef struct lws_system_blob lws_system_blob_t;
64
65LWS_EXTERN LWS_VISIBLE void
66lws_system_blob_direct_set(lws_system_blob_t *b, const uint8_t *ptr, size_t len);
67
68LWS_EXTERN LWS_VISIBLE void
69lws_system_blob_heap_empty(lws_system_blob_t *b);
70
71LWS_EXTERN LWS_VISIBLE int
72lws_system_blob_heap_append(lws_system_blob_t *b, const uint8_t *ptr, size_t len);
73
74LWS_EXTERN LWS_VISIBLE size_t
75lws_system_blob_get_size(lws_system_blob_t *b);
76
77/* return 0 and sets *ptr to point to blob data if possible, nonzero = fail */
78LWS_EXTERN LWS_VISIBLE int
79lws_system_blob_get_single_ptr(lws_system_blob_t *b, const uint8_t **ptr);
80
81LWS_EXTERN LWS_VISIBLE int
82lws_system_blob_get(lws_system_blob_t *b, uint8_t *ptr, size_t *len, size_t ofs);
83
84LWS_EXTERN LWS_VISIBLE void
85lws_system_blob_destroy(lws_system_blob_t *b);
86
87/*
88 * Get the opaque blob for index idx of various system blobs. Returns 0 if
89 * *b was set otherwise nonzero means out of range
90 */
91
92LWS_EXTERN LWS_VISIBLE lws_system_blob_t *
93lws_system_get_blob(struct lws_context *context, lws_system_blob_item_t type,
94 int idx);
95
96/*
97 * Lws view of system state... normal operation from user code perspective is
98 * dependent on implicit (eg, knowing the date for cert validation) and
99 * explicit dependencies.
100 *
101 * Bit of lws and user code can register notification handlers that can enforce
102 * dependent operations before state transitions can complete.
103 */
104
105typedef enum { /* keep system_state_names[] in sync in context.c */
106 LWS_SYSTATE_UNKNOWN,
107
108 LWS_SYSTATE_CONTEXT_CREATED, /* context was just created */
109 LWS_SYSTATE_INITIALIZED, /* protocols initialized. Lws itself
110 * can operate normally */
111 LWS_SYSTATE_COLLECTING_STDIN, /* we are waiting for stdin RX and / or
112 * closure. This is skipped if
113 * system_ops.stdin_rx is NULL */
114 LWS_SYSTATE_IFACE_COLDPLUG, /* existing net ifaces iterated */
115 LWS_SYSTATE_DHCP, /* at least one net iface configured */
116 LWS_SYSTATE_CPD_PRE_TIME, /* Captive portal detect without valid
117 * time, good for non-https tests... if
118 * you care about it, implement and
119 * call lws_system_ops_t
120 * .captive_portal_detect_request()
121 * and move the state forward according
122 * to the result. */
123 LWS_SYSTATE_TIME_VALID, /* ntpclient ran, or hw time valid...
124 * tls cannot work until we reach here
125 */
126 LWS_SYSTATE_CPD_POST_TIME, /* Captive portal detect after time was
127 * time, good for https tests... if
128 * you care about it, implement and
129 * call lws_system_ops_t
130 * .captive_portal_detect_request()
131 * and move the state forward according
132 * to the result. */
133
134 LWS_SYSTATE_POLICY_VALID, /* user code knows how to operate... */
135 LWS_SYSTATE_REGISTERED, /* device has an identity... */
136 LWS_SYSTATE_AUTH1, /* identity used for main auth token */
137 LWS_SYSTATE_AUTH2, /* identity used for optional auth */
138
139 LWS_SYSTATE_ONE_TIME_UPDATES, /* pre-OPERATIONAL one-time updates,
140 * when a firmware needs to perform
141 * one-time upgrades to state before
142 * OPERATIONAL */
143
144 LWS_SYSTATE_OPERATIONAL, /* user code can operate normally */
145
146 LWS_SYSTATE_POLICY_INVALID, /* user code is changing its policies
147 * drop everything done with old
148 * policy, switch to new then enter
149 * LWS_SYSTATE_POLICY_VALID */
150 LWS_SYSTATE_CONTEXT_DESTROYING, /* Context is being destroyed */
151 LWS_SYSTATE_AWAITING_MODAL_UPDATING, /* We're negotiating with the
152 * user code for update mode */
153 LWS_SYSTATE_MODAL_UPDATING, /* We're updating the firmware */
154} lws_system_states_t;
155
156/* Captive Portal Detect -related */
157
158typedef enum {
159 LWS_CPD_UNKNOWN = 0, /* test didn't happen ince last DHCP acq yet */
160 LWS_CPD_INTERNET_OK, /* no captive portal: our CPD test passed OK,
161 * we can go out on the internet */
162 LWS_CPD_CAPTIVE_PORTAL, /* we inferred we're behind a captive portal */
163 LWS_CPD_NO_INTERNET, /* we couldn't touch anything */
164} lws_cpd_result_t;
165
166typedef void (*lws_attach_cb_t)(struct lws_context *context, int tsi, void *opaque);
167struct lws_attach_item;
168
169LWS_EXTERN LWS_VISIBLE int
170lws_tls_jit_trust_got_cert_cb(struct lws_context *cx, void *got_opaque,
171 const uint8_t *skid, size_t skid_len,
172 const uint8_t *der, size_t der_len);
173
174typedef struct lws_system_ops {
175 int (*reboot)(void);
176 int (*set_clock)(lws_usec_t us);
177 int (*attach)(struct lws_context *context, int tsi, lws_attach_cb_t cb,
178 lws_system_states_t state, void *opaque,
179 struct lws_attach_item **get);
180 /**< if \p get is NULL, add an attach callback request to the pt for
181 * \p cb with arg \p opaque, that should be called when we're at or past
182 * system state \p state.
183 *
184 * If \p get is non-NULL, look for the first listed item on the pt whose
185 * state situation is ready, and set *get to point to it. If no items,
186 * or none where the system state is right, set *get to NULL.
187 *
188 * It's done like this so (*attach) can perform system-specific
189 * locking outside of lws core, for both getting and adding items the
190 * same so it is thread-safe. A non-threadsafe helper
191 * __lws_system_attach() is provided to do the actual work inside the
192 * system-specific locking.
193 */
194 int (*captive_portal_detect_request)(struct lws_context *context);
195 /**< Check if we can go out on the internet cleanly, or if we are being
196 * redirected or intercepted by a captive portal.
197 * Start the check that proceeds asynchronously, and report the results
198 * by calling lws_captive_portal_detect_result() api
199 */
200
201#if defined(LWS_WITH_NETWORK)
202 int (*metric_report)(lws_metric_pub_t *mdata);
203 /**< metric \p item is reporting an event of kind \p rpt,
204 * held in \p mdata... return 0 to leave the metric object as it is,
205 * or nonzero to reset it. */
206#endif
207 int (*jit_trust_query)(struct lws_context *cx, const uint8_t *skid,
208 size_t skid_len, void *got_opaque);
209 /**< user defined trust store search, if we do trust a cert with SKID
210 * matching skid / skid_len, then it should get hold of the DER for the
211 * matching root CA and call
212 * lws_tls_jit_trust_got_cert_cb(..., got_opaque) before cleaning up and
213 * returning. The DER should be destroyed if in heap before returning.
214 */
215
216 int (*stdin_rx)(struct lws_context *cx, const char *buf, size_t len);
217 /**< anything from stdin turns up here, eg, echo -n 123 | ./myapp will
218 * cause this to be called with buf = "123" and len=3. If stdin closes
219 * before the event loop terminates, we will be called with buf = NULL
220 * and len = 0 and nothing further, since stdin will be closed. This is
221 * very handy for passing secrets into your app that will not be visible
222 * to others via the commandline and with solid behaviours whenever the
223 * stdin source closes.
224 */
225
226#if defined(LWS_WITH_OTA)
227 lws_ota_ops_t ota_ops;
228 /**< Platform OTA interface to lws_ota, see lws-ota.h */
229#endif
230
231 uint32_t wake_latency_us;
232 /**< time taken for this device to wake from suspend, in us
233 */
234} lws_system_ops_t;
235
236#if defined(LWS_WITH_SYS_STATE)
237
238/**
239 * lws_system_get_state_manager() - return the state mgr object for system state
240 *
241 * \param context: the lws_context
242 *
243 * The returned pointer can be used with the lws_state_ apis
244 */
245
246LWS_EXTERN LWS_VISIBLE lws_state_manager_t *
247lws_system_get_state_manager(struct lws_context *context);
248
249#endif
250
251/* wrappers handle NULL members or no ops struct set at all cleanly */
252
253#define LWSSYSGAUTH_HEX (1 << 0)
254
255/**
256 * lws_system_get_ops() - get ahold of the system ops struct from the context
257 *
258 * \param context: the lws_context
259 *
260 * Returns the system ops struct. It may return NULL and if not, anything in
261 * there may be NULL.
262 */
263LWS_EXTERN LWS_VISIBLE const lws_system_ops_t *
264lws_system_get_ops(struct lws_context *context);
265
266#if defined(LWS_WITH_SYS_STATE)
267
268/**
269 * lws_system_context_from_system_mgr() - return context from system state mgr
270 *
271 * \param mgr: pointer to specifically the system state mgr
272 *
273 * Returns the context from the system state mgr. Helper since the lws_context
274 * is opaque.
275 */
276LWS_EXTERN LWS_VISIBLE struct lws_context *
277lws_system_context_from_system_mgr(lws_state_manager_t *mgr);
278
279#endif
280
281/**
282 * __lws_system_attach() - get and set items on context attach list
283 *
284 * \param context: context to get or set attach items to
285 * \param tsi: thread service index (normally 0)
286 * \param cb: callback to call from context event loop thread
287 * \param state: the lws_system state we have to be in or have passed through
288 * \param opaque: optional pointer to user specific info given to callback
289 * \param get: NULL, or pointer to pointer to take detached tail item on exit
290 *
291 * This allows other threads to enqueue callback requests to happen from a pt's
292 * event loop thread safely. The callback gets the context pointer and a user
293 * opaque pointer that can be optionally given when the item is added to the
294 * attach list.
295 *
296 * This api is the no-locking core function for getting and setting items on the
297 * pt's attach list. The lws_system operation (*attach) is the actual
298 * api that user and internal code calls for this feature, it should perform
299 * system-specific locking, call this helper, release the locking and then
300 * return the result. This api is public only so it can be used in the locked
301 * implementation of (*attach).
302 *
303 * If get is NULL, then the call adds to the head of the pt attach list using
304 * cb, state, and opaque; if get is non-NULL, then *get is set to the first
305 * waiting attached item that meets the state criteria and that item is removed
306 * from the list.
307 *
308 * This is a non-threadsafe helper only designed to be called from
309 * implementations of struct lws_system's (*attach) operation where system-
310 * specific locking has been applied around it, making it threadsafe.
311 */
312LWS_EXTERN LWS_VISIBLE int
313__lws_system_attach(struct lws_context *context, int tsi, lws_attach_cb_t cb,
314 lws_system_states_t state, void *opaque,
315 struct lws_attach_item **get);
316
317
318enum {
319 LWSDH_IPV4_SUBNET_MASK = 0,
320 LWSDH_IPV4_BROADCAST,
321 LWSDH_LEASE_SECS,
322 LWSDH_REBINDING_SECS,
323 LWSDH_RENEWAL_SECS,
324
325 _LWSDH_NUMS_COUNT,
326
327 LWSDH_SA46_IP = 0,
328 LWSDH_SA46_DNS_SRV_1,
329 LWSDH_SA46_DNS_SRV_2,
330 LWSDH_SA46_DNS_SRV_3,
331 LWSDH_SA46_DNS_SRV_4,
332 LWSDH_SA46_IPV4_ROUTER,
333 LWSDH_SA46_NTP_SERVER,
334 LWSDH_SA46_DHCP_SERVER,
335
336 _LWSDH_SA46_COUNT,
337};
338
339#if defined(LWS_WITH_NETWORK)
340typedef struct lws_dhcpc_ifstate {
341 char ifname[16];
342 char domain[64];
343 uint8_t mac[6];
344 uint32_t nums[_LWSDH_NUMS_COUNT];
345 lws_sockaddr46 sa46[_LWSDH_SA46_COUNT];
346} lws_dhcpc_ifstate_t;
347
348typedef int (*dhcpc_cb_t)(void *opaque, lws_dhcpc_ifstate_t *is);
349
350/**
351 * lws_dhcpc_request() - add a network interface to dhcpc management
352 *
353 * \param c: the lws_context
354 * \param i: the interface name, like "eth0"
355 * \param af: address family
356 * \param cb: the change callback
357 * \param opaque: opaque pointer given to the callback
358 *
359 * Register a network interface as being managed by DHCP. lws will proceed to
360 * try to acquire an IP. Requires LWS_WITH_SYS_DHCP_CLIENT at cmake.
361 */
362LWS_EXTERN LWS_VISIBLE int
363lws_dhcpc_request(struct lws_context *c, const char *i, int af, dhcpc_cb_t cb,
364 void *opaque);
365
366/**
367 * lws_dhcpc_remove() - remove a network interface to dhcpc management
368 *
369 * \param context: the lws_context
370 * \param iface: the interface name, like "eth0"
371 *
372 * Remove handling of the network interface from dhcp.
373 */
374LWS_EXTERN LWS_VISIBLE int
375lws_dhcpc_remove(struct lws_context *context, const char *iface);
376
377/**
378 * lws_dhcpc_status() - has any interface reached BOUND state
379 *
380 * \param context: the lws_context
381 * \param sa46: set to a DNS server from a bound interface, or NULL
382 *
383 * Returns 1 if any network interface managed by dhcpc has reached the BOUND
384 * state (has acquired an IP, gateway and DNS server), otherwise 0.
385 */
386LWS_EXTERN LWS_VISIBLE int
387lws_dhcpc_status(struct lws_context *context, lws_sockaddr46 *sa46);
388
389/**
390 * lws_system_cpd_start() - helper to initiate captive portal detection
391 *
392 * \param context: the lws_context
393 *
394 * Resets the context's captive portal state to LWS_CPD_UNKNOWN and calls the
395 * lws_system_ops_t captive_portal_detect_request() implementation to begin
396 * testing the captive portal state.
397 */
398LWS_EXTERN LWS_VISIBLE int
399lws_system_cpd_start(struct lws_context *context);
400
401LWS_EXTERN LWS_VISIBLE void
402lws_system_cpd_start_defer(struct lws_context *cx, lws_usec_t defer_us);
403
404
405/**
406 * lws_system_cpd_set() - report the result of the captive portal detection
407 *
408 * \param context: the lws_context
409 * \param result: one of the LWS_CPD_ constants representing captive portal state
410 *
411 * Sets the context's captive portal detection state to result. User captive
412 * portal detection code would call this once it had a result from its test.
413 */
414LWS_EXTERN LWS_VISIBLE void
415lws_system_cpd_set(struct lws_context *context, lws_cpd_result_t result);
416
417
418/**
419 * lws_system_cpd_state_get() - returns the last tested captive portal state
420 *
421 * \param context: the lws_context
422 *
423 * Returns one of the LWS_CPD_ constants indicating the system's understanding
424 * of the current captive portal situation.
425 */
426LWS_EXTERN LWS_VISIBLE lws_cpd_result_t
427lws_system_cpd_state_get(struct lws_context *context);
428
429enum {
430 LWS_SAS_FLAG__APPEND_COMMANDLINE = (1 << 0)
431};
432
433/**
434 * lws_system_adopt_stdin(): add stdin to be a wsi handled by the event loop
435 *
436 * \param context: the lws_context
437 *
438 * The user code should call this after context creation. It will add stdin
439 * to the context event loop and handle it one of two ways
440 *
441 * 1) flags has LWS_SAS_FLAG__APPEND_COMMANDLINE set: internally manage the
442 * stdin input as additional commandline content that can be accessed
443 * alongside the official commandline context using the
444 * lws_cmdline_option_cx() api. Note this a) requires you to pass your
445 * app argc and argv to the same-named context creation info struct members,
446 * and b) is only effective after context creation. This is very useful if
447 * passing secrets to your app that can't appear on the commandline. Or,
448 *
449 * 2) flags = 0: ignore the received stdin input internally, and call back
450 * rx data (and close) to lws_system to the .stdin_rx callback.
451 *
452 * The callback is called with a buffer and length for received RX from stdin,
453 * which may be arbitrarily fragmented. If it's called with a NULL buffer and
454 * 0 length, it means stdin was closed.
455 *
456 * Your callback should process the provided RX passed to it, and choose to
457 * return 0 to continue to wait for stdin RX, or nonzero to close stdin and
458 * continue the lws_system state startup.
459 */
460LWS_EXTERN LWS_VISIBLE int
461lws_system_adopt_stdin(struct lws_context *cx, unsigned int flags);
462
463
464#endif
465
466