1 /* 2 * libwebsockets - small server side websockets and web server implementation 3 * 4 * Copyright (C) 2010 - 2019 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 25 /*! \defgroup Protocols-and-Plugins Protocols and Plugins 26 * \ingroup lwsapi 27 * 28 * ##Protocol and protocol plugin -related apis 29 * 30 * Protocols bind ws protocol names to a custom callback specific to that 31 * protocol implementaion. 32 * 33 * A list of protocols can be passed in at context creation time, but it is 34 * also legal to leave that NULL and add the protocols and their callback code 35 * using plugins. 36 * 37 * Plugins are much preferable compared to cut and pasting code into an 38 * application each time, since they can be used standalone. 39 */ 40 ///@{ 41 /** struct lws_protocols - List of protocols and handlers client or server 42 * supports. */ 43 44 struct lws_protocols { 45 const char *name; 46 /**< Protocol name that must match the one given in the client 47 * Javascript new WebSocket(url, 'protocol') name. */ 48 lws_callback_function *callback; 49 /**< The service callback used for this protocol. It allows the 50 * service action for an entire protocol to be encapsulated in 51 * the protocol-specific callback */ 52 size_t per_session_data_size; 53 /**< Each new connection using this protocol gets 54 * this much memory allocated on connection establishment and 55 * freed on connection takedown. A pointer to this per-connection 56 * allocation is passed into the callback in the 'user' parameter */ 57 size_t rx_buffer_size; 58 /**< lws allocates this much space for rx data and informs callback 59 * when something came. Due to rx flow control, the callback may not 60 * be able to consume it all without having to return to the event 61 * loop. That is supported in lws. 62 * 63 * If .tx_packet_size is 0, this also controls how much may be sent at 64 * once for backwards compatibility. 65 */ 66 unsigned int id; 67 /**< ignored by lws, but useful to contain user information bound 68 * to the selected protocol. For example if this protocol was 69 * called "myprotocol-v2", you might set id to 2, and the user 70 * code that acts differently according to the version can do so by 71 * switch (wsi->a.protocol->id), user code might use some bits as 72 * capability flags based on selected protocol version, etc. */ 73 void *user; /**< ignored by lws, but user code can pass a pointer 74 here it can later access from the protocol callback */ 75 size_t tx_packet_size; 76 /**< 0 indicates restrict send() size to .rx_buffer_size for backwards- 77 * compatibility. 78 * If greater than zero, a single send() is restricted to this amount 79 * and any remainder is buffered by lws and sent afterwards also in 80 * these size chunks. Since that is expensive, it's preferable 81 * to restrict one fragment you are trying to send to match this 82 * size. 83 */ 84 85 /* Add new things just above here ---^ 86 * This is part of the ABI, don't needlessly break compatibility */ 87 }; 88 89 /** 90 * lws_vhost_name_to_protocol() - get vhost's protocol object from its name 91 * 92 * \param vh: vhost to search 93 * \param name: protocol name 94 * 95 * Returns NULL or a pointer to the vhost's protocol of the requested name 96 */ 97 LWS_VISIBLE LWS_EXTERN const struct lws_protocols * 98 lws_vhost_name_to_protocol(struct lws_vhost *vh, const char *name); 99 100 /** 101 * lws_get_protocol() - Returns a protocol pointer from a websocket 102 * connection. 103 * \param wsi: pointer to struct websocket you want to know the protocol of 104 * 105 * 106 * Some apis can act on all live connections of a given protocol, 107 * this is how you can get a pointer to the active protocol if needed. 108 */ 109 LWS_VISIBLE LWS_EXTERN const struct lws_protocols * 110 lws_get_protocol(struct lws *wsi); 111 112 /** lws_protocol_get() - deprecated: use lws_get_protocol */ 113 LWS_VISIBLE LWS_EXTERN const struct lws_protocols * 114 lws_protocol_get(struct lws *wsi) LWS_WARN_DEPRECATED; 115 116 /** 117 * lws_protocol_vh_priv_zalloc() - Allocate and zero down a protocol's per-vhost 118 * storage 119 * \param vhost: vhost the instance is related to 120 * \param prot: protocol the instance is related to 121 * \param size: bytes to allocate 122 * 123 * Protocols often find it useful to allocate a per-vhost struct, this is a 124 * helper to be called in the per-vhost init LWS_CALLBACK_PROTOCOL_INIT 125 */ 126 LWS_VISIBLE LWS_EXTERN void * 127 lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, 128 const struct lws_protocols *prot, int size); 129 130 /** 131 * lws_protocol_vh_priv_get() - retreive a protocol's per-vhost storage 132 * 133 * \param vhost: vhost the instance is related to 134 * \param prot: protocol the instance is related to 135 * 136 * Recover a pointer to the allocated per-vhost storage for the protocol created 137 * by lws_protocol_vh_priv_zalloc() earlier 138 */ 139 LWS_VISIBLE LWS_EXTERN void * 140 lws_protocol_vh_priv_get(struct lws_vhost *vhost, 141 const struct lws_protocols *prot); 142 143 /** 144 * lws_vhd_find_by_pvo() - find a partner vhd 145 * 146 * \param cx: the lws_context 147 * \param protname: the name of the lws_protocol the vhd belongs to 148 * \param pvo_name: the name of a pvo that must exist bound to the vhd 149 * \param pvo_value: the required value of the named pvo 150 * 151 * This allows architectures with multiple protocols bound together to 152 * cleanly discover partner protocol instances even on completely 153 * different vhosts. For example, a proxy may consist of two protocols 154 * listening on different vhosts, and there may be multiple instances 155 * of the proxy in the same process. It's desirable that each side of 156 * the proxy is an independent protocol that can be freely bound to any 157 * vhost, eg, allowing Unix Domain to tls / h2 proxying, or each side 158 * bound to different network interfaces for localhost-only visibility 159 * on one side, using existing vhost management. 160 * 161 * That leaves the problem that the two sides have to find each other 162 * and bind at runtime. This api allows each side to specify the 163 * protocol name, and a common pvo name and pvo value that indicates 164 * the two sides belong together, and search through all the instantiated 165 * vhost-protocols looking for a match. If found, the private allocation 166 * (aka "vhd" of the match is returned). NULL is returned on no match. 167 * 168 * Since this can only succeed when called by the last of the two 169 * protocols to be instantiated, both sides should call it and handle 170 * NULL gracefully, since it may mean that they were first and their 171 * partner vhsot-protocol has not been instantiated yet. 172 */ 173 LWS_VISIBLE LWS_EXTERN void * 174 lws_vhd_find_by_pvo(struct lws_context *cx, const char *protname, 175 const char *pvo_name, const char *pvo_value); 176 177 178 /** 179 * lws_adjust_protocol_psds - change a vhost protocol's per session data size 180 * 181 * \param wsi: a connection with the protocol to change 182 * \param new_size: the new size of the per session data size for the protocol 183 * 184 * Returns user_space for the wsi, after allocating 185 * 186 * This should not be used except to initalize a vhost protocol's per session 187 * data size one time, before any connections are accepted. 188 * 189 * Sometimes the protocol wraps another protocol and needs to discover and set 190 * its per session data size at runtime. 191 */ 192 LWS_VISIBLE LWS_EXTERN void * 193 lws_adjust_protocol_psds(struct lws *wsi, size_t new_size); 194 195 /** 196 * lws_finalize_startup() - drop initial process privileges 197 * 198 * \param context: lws context 199 * 200 * This is called after the end of the vhost protocol initializations, but 201 * you may choose to call it earlier 202 */ 203 LWS_VISIBLE LWS_EXTERN int 204 lws_finalize_startup(struct lws_context *context); 205 206 /** 207 * lws_pvo_search() - helper to find a named pvo in a linked-list 208 * 209 * \param pvo: the first pvo in the linked-list 210 * \param name: the name of the pvo to return if found 211 * 212 * Returns NULL, or a pointer to the name pvo in the linked-list 213 */ 214 LWS_VISIBLE LWS_EXTERN const struct lws_protocol_vhost_options * 215 lws_pvo_search(const struct lws_protocol_vhost_options *pvo, const char *name); 216 217 /** 218 * lws_pvo_get_str() - retreive a string pvo value 219 * 220 * \param in: the first pvo in the linked-list 221 * \param name: the name of the pvo to return if found 222 * \param result: pointer to a const char * to get the result if any 223 * 224 * Returns 0 if found and *result set, or nonzero if not found 225 */ 226 LWS_VISIBLE LWS_EXTERN int 227 lws_pvo_get_str(void *in, const char *name, const char **result); 228 229 LWS_VISIBLE LWS_EXTERN int 230 lws_protocol_init(struct lws_context *context); 231 232 #define LWS_PLUGIN_API_MAGIC 191 233 234 /* 235 * Abstract plugin header for any kind of plugin class, always at top of 236 * actual class plugin export type. 237 * 238 * The export type object must be exported with the same name as the plugin 239 * file, eg, libmyplugin.so must export a const one of these as the symbol 240 * "myplugin". 241 * 242 * That is the only expected export from the plugin. 243 */ 244 245 typedef struct lws_plugin_header { 246 const char *name; 247 const char *_class; 248 const char *lws_build_hash; /* set to LWS_BUILD_HASH */ 249 250 unsigned int api_magic; 251 /* set to LWS_PLUGIN_API_MAGIC at plugin build time */ 252 253 /* plugin-class specific superclass data follows */ 254 } lws_plugin_header_t; 255 256 /* 257 * "lws_protocol_plugin" class export, for lws_protocol implementations done 258 * as plugins 259 */ 260 typedef struct lws_plugin_protocol { 261 lws_plugin_header_t hdr; 262 263 const struct lws_protocols *protocols; /**< array of supported protocols provided by plugin */ 264 const struct lws_extension *extensions; /**< array of extensions provided by plugin */ 265 int count_protocols; /**< how many protocols */ 266 int count_extensions; /**< how many extensions */ 267 } lws_plugin_protocol_t; 268 269 270 /* 271 * This is the dynamic, runtime created part of the plugin instantiation. 272 * These are kept in a linked-list and destroyed with the context. 273 */ 274 275 struct lws_plugin { 276 struct lws_plugin *list; /**< linked list */ 277 278 const lws_plugin_header_t *hdr; 279 280 union { 281 #if defined(LWS_WITH_LIBUV) && defined(UV_ERRNO_MAP) 282 #if (UV_VERSION_MAJOR > 0) 283 uv_lib_t lib; /**< shared library pointer */ 284 #endif 285 #endif 286 void *l; /**< */ 287 } u; 288 }; 289 290 /* 291 * Event lib library plugin type (when LWS_WITH_EVLIB_PLUGINS) 292 * Public so new event libs can equally be supported outside lws itself 293 */ 294 295 typedef struct lws_plugin_evlib { 296 lws_plugin_header_t hdr; 297 const struct lws_event_loop_ops *ops; 298 } lws_plugin_evlib_t; 299 300 typedef int (*each_plugin_cb_t)(struct lws_plugin *p, void *user); 301 302 /** 303 * lws_plugins_init() - dynamically load plugins of matching class from dirs 304 * 305 * \param pplugin: pointer to linked-list for this kind of plugin 306 * \param d: array of directory paths to look in 307 * \param _class: class string that plugin must declare 308 * \param filter: NULL, or a string that must appear after the third char of the plugin filename 309 * \param each: NULL, or each_plugin_cb_t callback for each instantiated plugin 310 * \param each_user: pointer passed to each callback 311 * 312 * Allows you to instantiate a class of plugins to a specified linked-list. 313 * The each callback allows you to init each inistantiated callback and pass a 314 * pointer each_user to it. 315 * 316 * To take down the plugins, pass a pointer to the linked-list head to 317 * lws_plugins_destroy. 318 * 319 * This is used for lws protocol plugins but you can define your own plugin 320 * class name like "mypluginclass", declare it in your plugin headers, and load 321 * your own plugins to your own list using this api the same way. 322 */ 323 LWS_VISIBLE LWS_EXTERN int 324 lws_plugins_init(struct lws_plugin **pplugin, const char * const *d, 325 const char *_class, const char *filter, 326 each_plugin_cb_t each, void *each_user); 327 328 /** 329 * lws_plugins_destroy() - dynamically unload list of plugins 330 * 331 * \param pplugin: pointer to linked-list for this kind of plugin 332 * \param each: NULL, or each_plugin_cb_t callback for each instantiated plugin 333 * \param each_user: pointer passed to each callback 334 * 335 * Allows you to destroy a class of plugins from a specified linked-list 336 * created by a call to lws_plugins_init(). 337 * 338 * The each callback allows you to deinit each inistantiated callback and pass a 339 * pointer each_user to it, just before its footprint is destroyed. 340 */ 341 LWS_VISIBLE LWS_EXTERN int 342 lws_plugins_destroy(struct lws_plugin **pplugin, each_plugin_cb_t each, 343 void *each_user); 344 345 #if defined(LWS_WITH_PLUGINS_BUILTIN) 346 347 /* provide exports for builtin plugin protocols */ 348 349 extern const struct lws_protocols post_demo_protocols[1]; 350 extern const struct lws_protocols lws_raw_proxy_protocols[1]; 351 extern const struct lws_protocols lws_status_protocols[1]; 352 extern const struct lws_protocols lws_mirror_protocols[1]; 353 extern const struct lws_protocols lws_ssh_base_protocols[2]; 354 extern const struct lws_protocols post_demo_protocols[1]; 355 extern const struct lws_protocols dumb_increment_protocols[1]; 356 extern const struct lws_protocols deaddrop_protocols[1]; 357 extern const struct lws_protocols lws_raw_test_protocols[1]; 358 extern const struct lws_protocols lws_sshd_demo_protocols[1]; 359 extern const struct lws_protocols lws_acme_client_protocols[1]; 360 extern const struct lws_protocols client_loopback_test_protocols[1]; 361 extern const struct lws_protocols fulltext_demo_protocols[1]; 362 extern const struct lws_protocols lws_openmetrics_export_protocols[ 363 #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_CLIENT) && defined(LWS_ROLE_WS) 364 4 365 #else 366 #if defined(LWS_WITH_SERVER) 367 3 368 #else 369 1 370 #endif 371 #endif 372 ]; 373 374 #define LWSOMPROIDX_DIRECT_HTTP_SERVER 0 375 #define LWSOMPROIDX_PROX_HTTP_SERVER 1 376 #define LWSOMPROIDX_PROX_WS_SERVER 2 377 #define LWSOMPROIDX_PROX_WS_CLIENT 3 378 379 #endif 380 381 ///@} 382