libwebsockets
Lightweight C library for HTML5 websockets
lws-system.h
1  /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2020 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 
33 typedef 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 
42  LWS_SYSBLOB_TYPE_COUNT /* ... always last */
43 } lws_system_blob_item_t;
44 
45 /* opaque generic blob whose content may be on-the-heap or pointed-to
46  * directly case by case. When it's on the heap, it can be produced by
47  * appending (it's a buflist underneath). Either way, it can be consumed by
48  * copying out a given length from a given offset.
49  */
50 
51 typedef struct lws_system_blob lws_system_blob_t;
52 
53 LWS_EXTERN LWS_VISIBLE void
54 lws_system_blob_direct_set(lws_system_blob_t *b, const uint8_t *ptr, size_t len);
55 
56 LWS_EXTERN LWS_VISIBLE void
57 lws_system_blob_heap_empty(lws_system_blob_t *b);
58 
59 LWS_EXTERN LWS_VISIBLE int
60 lws_system_blob_heap_append(lws_system_blob_t *b, const uint8_t *ptr, size_t len);
61 
62 LWS_EXTERN LWS_VISIBLE size_t
63 lws_system_blob_get_size(lws_system_blob_t *b);
64 
65 /* return 0 and sets *ptr to point to blob data if possible, nonzero = fail */
66 LWS_EXTERN LWS_VISIBLE int
67 lws_system_blob_get_single_ptr(lws_system_blob_t *b, const uint8_t **ptr);
68 
69 LWS_EXTERN LWS_VISIBLE int
70 lws_system_blob_get(lws_system_blob_t *b, uint8_t *ptr, size_t *len, size_t ofs);
71 
72 LWS_EXTERN LWS_VISIBLE void
73 lws_system_blob_destroy(lws_system_blob_t *b);
74 
75 /*
76  * Get the opaque blob for index idx of various system blobs. Returns 0 if
77  * *b was set otherwise nonzero means out of range
78  */
79 
80 LWS_EXTERN LWS_VISIBLE lws_system_blob_t *
81 lws_system_get_blob(struct lws_context *context, lws_system_blob_item_t type,
82  int idx);
83 
84 /*
85  * Lws view of system state... normal operation from user code perspective is
86  * dependent on implicit (eg, knowing the date for cert validation) and
87  * explicit dependencies.
88  *
89  * Bit of lws and user code can register notification handlers that can enforce
90  * dependent operations before state transitions can complete.
91  */
92 
93 typedef enum { /* keep system_state_names[] in sync in context.c */
94  LWS_SYSTATE_UNKNOWN,
95 
96  LWS_SYSTATE_CONTEXT_CREATED, /* context was just created */
97  LWS_SYSTATE_INITIALIZED, /* protocols initialized. Lws itself
98  * can operate normally */
99  LWS_SYSTATE_IFACE_COLDPLUG, /* existing net ifaces iterated */
100  LWS_SYSTATE_DHCP, /* at least one net iface configured */
101  LWS_SYSTATE_CPD_PRE_TIME, /* Captive portal detect without valid
102  * time, good for non-https tests... if
103  * you care about it, implement and
104  * call lws_system_ops_t
105  * .captive_portal_detect_request()
106  * and move the state forward according
107  * to the result. */
108  LWS_SYSTATE_TIME_VALID, /* ntpclient ran, or hw time valid...
109  * tls cannot work until we reach here
110  */
111  LWS_SYSTATE_CPD_POST_TIME, /* Captive portal detect after time was
112  * time, good for https tests... if
113  * you care about it, implement and
114  * call lws_system_ops_t
115  * .captive_portal_detect_request()
116  * and move the state forward according
117  * to the result. */
118 
119  LWS_SYSTATE_POLICY_VALID, /* user code knows how to operate... */
120  LWS_SYSTATE_REGISTERED, /* device has an identity... */
121  LWS_SYSTATE_AUTH1, /* identity used for main auth token */
122  LWS_SYSTATE_AUTH2, /* identity used for optional auth */
123 
124  LWS_SYSTATE_OPERATIONAL, /* user code can operate normally */
125 
126  LWS_SYSTATE_POLICY_INVALID, /* user code is changing its policies
127  * drop everything done with old
128  * policy, switch to new then enter
129  * LWS_SYSTATE_POLICY_VALID */
130 } lws_system_states_t;
131 
132 /* Captive Portal Detect -related */
133 
134 typedef enum {
135  LWS_CPD_UNKNOWN = 0, /* test didn't happen ince last DHCP acq yet */
136  LWS_CPD_INTERNET_OK, /* no captive portal: our CPD test passed OK,
137  * we can go out on the internet */
138  LWS_CPD_CAPTIVE_PORTAL, /* we inferred we're behind a captive portal */
139  LWS_CPD_NO_INTERNET, /* we couldn't touch anything */
140 } lws_cpd_result_t;
141 
142 
143 typedef void (*lws_attach_cb_t)(struct lws_context *context, int tsi, void *opaque);
144 struct lws_attach_item;
145 
146 typedef struct lws_system_ops {
147  int (*reboot)(void);
148  int (*set_clock)(lws_usec_t us);
149  int (*attach)(struct lws_context *context, int tsi, lws_attach_cb_t cb,
150  lws_system_states_t state, void *opaque,
151  struct lws_attach_item **get);
166  int (*captive_portal_detect_request)(struct lws_context *context);
173  uint32_t wake_latency_us;
177 
178 #if defined(LWS_WITH_SYS_STATE)
179 
188 LWS_EXTERN LWS_VISIBLE lws_state_manager_t *
189 lws_system_get_state_manager(struct lws_context *context);
190 
191 #endif
192 
193 /* wrappers handle NULL members or no ops struct set at all cleanly */
194 
195 #define LWSSYSGAUTH_HEX (1 << 0)
196 
205 LWS_EXTERN LWS_VISIBLE const lws_system_ops_t *
206 lws_system_get_ops(struct lws_context *context);
207 
208 #if defined(LWS_WITH_SYS_STATE)
209 
218 LWS_EXTERN LWS_VISIBLE struct lws_context *
219 lws_system_context_from_system_mgr(lws_state_manager_t *mgr);
220 
221 #endif
222 
254 LWS_EXTERN LWS_VISIBLE int
255 __lws_system_attach(struct lws_context *context, int tsi, lws_attach_cb_t cb,
256  lws_system_states_t state, void *opaque,
257  struct lws_attach_item **get);
258 
259 
260 typedef int (*dhcpc_cb_t)(void *opaque, int af, uint8_t *ip, int ip_len);
261 
274 LWS_EXTERN LWS_VISIBLE int
275 lws_dhcpc_request(struct lws_context *c, const char *i, int af, dhcpc_cb_t cb,
276  void *opaque);
277 
286 LWS_EXTERN LWS_VISIBLE int
287 lws_dhcpc_remove(struct lws_context *context, const char *iface);
288 
298 LWS_EXTERN LWS_VISIBLE int
299 lws_dhcpc_status(struct lws_context *context, lws_sockaddr46 *sa46);
300 
310 LWS_EXTERN LWS_VISIBLE int
311 lws_system_cpd_start(struct lws_context *context);
312 
313 
323 LWS_EXTERN LWS_VISIBLE void
324 lws_system_cpd_set(struct lws_context *context, lws_cpd_result_t result);
325 
326 
335 LWS_EXTERN LWS_VISIBLE lws_cpd_result_t
336 lws_system_cpd_state_get(struct lws_context *context);
Definition: lws-system.h:146
int(* attach)(struct lws_context *context, int tsi, lws_attach_cb_t cb, lws_system_states_t state, void *opaque, struct lws_attach_item **get)
Definition: lws-system.h:149
int(* captive_portal_detect_request)(struct lws_context *context)
Definition: lws-system.h:166
uint32_t wake_latency_us
Definition: lws-system.h:173
Definition: lws-network-helper.h:36