remove fixed rx buffer allow definition per protocol
[platform/upstream/libwebsockets.git] / libwebsockets-api-doc.html
1 <h2>libwebsocket_client_connect - Connect to another websocket server</h2>
2 <i>struct libwebsocket *</i>
3 <b>libwebsocket_client_connect</b>
4 (<i>struct libwebsocket_context *</i> <b>context</b>,
5 <i>const char *</i> <b>address</b>,
6 <i>int</i> <b>port</b>,
7 <i>int</i> <b>ssl_connection</b>,
8 <i>const char *</i> <b>path</b>,
9 <i>const char *</i> <b>host</b>,
10 <i>const char *</i> <b>origin</b>,
11 <i>const char *</i> <b>protocol</b>,
12 <i>int</i> <b>ietf_version_or_minus_one</b>)
13 <h3>Arguments</h3>
14 <dl>
15 <dt><b>context</b>
16 <dd>Websocket context
17 <dt><b>address</b>
18 <dd>Remote server address, eg, "myserver.com"
19 <dt><b>port</b>
20 <dd>Port to connect to on the remote server, eg, 80
21 <dt><b>ssl_connection</b>
22 <dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
23 signed certs
24 <dt><b>path</b>
25 <dd>Websocket path on server
26 <dt><b>host</b>
27 <dd>Hostname on server
28 <dt><b>origin</b>
29 <dd>Socket origin name
30 <dt><b>protocol</b>
31 <dd>Comma-separated list of protocols being asked for from
32 the server, or just one.  The server will pick the one it
33 likes best.
34 <dt><b>ietf_version_or_minus_one</b>
35 <dd>-1 to ask to connect using the default, latest
36 protocol supported, or the specific protocol ordinal
37 </dl>
38 <h3>Description</h3>
39 <blockquote>
40 This function creates a connection to a remote server
41 </blockquote>
42 <hr>
43 <h2>libwebsocket_client_connect_extended - Connect to another websocket server</h2>
44 <i>struct libwebsocket *</i>
45 <b>libwebsocket_client_connect_extended</b>
46 (<i>struct libwebsocket_context *</i> <b>context</b>,
47 <i>const char *</i> <b>address</b>,
48 <i>int</i> <b>port</b>,
49 <i>int</i> <b>ssl_connection</b>,
50 <i>const char *</i> <b>path</b>,
51 <i>const char *</i> <b>host</b>,
52 <i>const char *</i> <b>origin</b>,
53 <i>const char *</i> <b>protocol</b>,
54 <i>int</i> <b>ietf_version_or_minus_one</b>,
55 <i>void *</i> <b>userdata</b>)
56 <h3>Arguments</h3>
57 <dl>
58 <dt><b>context</b>
59 <dd>Websocket context
60 <dt><b>address</b>
61 <dd>Remote server address, eg, "myserver.com"
62 <dt><b>port</b>
63 <dd>Port to connect to on the remote server, eg, 80
64 <dt><b>ssl_connection</b>
65 <dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
66 signed certs
67 <dt><b>path</b>
68 <dd>Websocket path on server
69 <dt><b>host</b>
70 <dd>Hostname on server
71 <dt><b>origin</b>
72 <dd>Socket origin name
73 <dt><b>protocol</b>
74 <dd>Comma-separated list of protocols being asked for from
75 the server, or just one.  The server will pick the one it
76 likes best.
77 <dt><b>ietf_version_or_minus_one</b>
78 <dd>-1 to ask to connect using the default, latest
79 protocol supported, or the specific protocol ordinal
80 <dt><b>userdata</b>
81 <dd>Pre-allocated user data
82 </dl>
83 <h3>Description</h3>
84 <blockquote>
85 This function creates a connection to a remote server
86 </blockquote>
87 <hr>
88 <h2>lws_get_library_version - </h2>
89 <i>const char *</i>
90 <b>lws_get_library_version</b>
91 (<i></i> <b>void</b>)
92 <h3>Arguments</h3>
93 <dl>
94 <dt><b>void</b>
95 <dd>no arguments
96 </dl>
97 <h3>Description</h3>
98 <blockquote>
99 <p>
100 returns a const char * to a string like "1.1 178d78c"
101 representing the library version followed by the git head hash it
102 was built from
103 </blockquote>
104 <hr>
105 <h2>libwebsockets_hangup_on_client - Server calls to terminate client connection</h2>
106 <i>void</i>
107 <b>libwebsockets_hangup_on_client</b>
108 (<i>struct libwebsocket_context *</i> <b>context</b>,
109 <i>int</i> <b>fd</b>)
110 <h3>Arguments</h3>
111 <dl>
112 <dt><b>context</b>
113 <dd>libwebsockets context
114 <dt><b>fd</b>
115 <dd>Connection socket descriptor
116 </dl>
117 <hr>
118 <h2>libwebsockets_get_peer_addresses - Get client address information</h2>
119 <i>void</i>
120 <b>libwebsockets_get_peer_addresses</b>
121 (<i>struct libwebsocket_context *</i> <b>context</b>,
122 <i>struct libwebsocket *</i> <b>wsi</b>,
123 <i>int</i> <b>fd</b>,
124 <i>char *</i> <b>name</b>,
125 <i>int</i> <b>name_len</b>,
126 <i>char *</i> <b>rip</b>,
127 <i>int</i> <b>rip_len</b>)
128 <h3>Arguments</h3>
129 <dl>
130 <dt><b>context</b>
131 <dd>Libwebsockets context
132 <dt><b>wsi</b>
133 <dd>Local struct libwebsocket associated with
134 <dt><b>fd</b>
135 <dd>Connection socket descriptor
136 <dt><b>name</b>
137 <dd>Buffer to take client address name
138 <dt><b>name_len</b>
139 <dd>Length of client address name buffer
140 <dt><b>rip</b>
141 <dd>Buffer to take client address IP qotted quad
142 <dt><b>rip_len</b>
143 <dd>Length of client address IP buffer
144 </dl>
145 <h3>Description</h3>
146 <blockquote>
147 This function fills in <tt><b>name</b></tt> and <tt><b>rip</b></tt> with the name and IP of
148 the client connected with socket descriptor <tt><b>fd</b></tt>.  Names may be
149 truncated if there is not enough room.  If either cannot be
150 determined, they will be returned as valid zero-length strings.
151 </blockquote>
152 <hr>
153 <h2>libwebsocket_service_fd - Service polled socket with something waiting</h2>
154 <i>int</i>
155 <b>libwebsocket_service_fd</b>
156 (<i>struct libwebsocket_context *</i> <b>context</b>,
157 <i>struct pollfd *</i> <b>pollfd</b>)
158 <h3>Arguments</h3>
159 <dl>
160 <dt><b>context</b>
161 <dd>Websocket context
162 <dt><b>pollfd</b>
163 <dd>The pollfd entry describing the socket fd and which events
164 happened.
165 </dl>
166 <h3>Description</h3>
167 <blockquote>
168 This function takes a pollfd that has POLLIN or POLLOUT activity and
169 services it according to the state of the associated struct libwebsocket.
170 <p>
171 The one call deals with all "service" that might happen on a socket
172 including listen accepts, http files as well as websocket protocol.
173 </blockquote>
174 <hr>
175 <h2>libwebsocket_context_destroy - Destroy the websocket context</h2>
176 <i>void</i>
177 <b>libwebsocket_context_destroy</b>
178 (<i>struct libwebsocket_context *</i> <b>context</b>)
179 <h3>Arguments</h3>
180 <dl>
181 <dt><b>context</b>
182 <dd>Websocket context
183 </dl>
184 <h3>Description</h3>
185 <blockquote>
186 This function closes any active connections and then frees the
187 context.  After calling this, any further use of the context is
188 undefined.
189 </blockquote>
190 <hr>
191 <h2>libwebsocket_context_user - get the user data associated with the whole context</h2>
192 <i>LWS_EXTERN void *</i>
193 <b>libwebsocket_context_user</b>
194 (<i>struct libwebsocket_context *</i> <b>context</b>)
195 <h3>Arguments</h3>
196 <dl>
197 <dt><b>context</b>
198 <dd>Websocket context
199 </dl>
200 <h3>Description</h3>
201 <blockquote>
202 This returns the optional user allocation that can be attached to
203 the context the sockets live in at context_create time.  It's a way
204 to let all sockets serviced in the same context share data without
205 using globals statics in the user code.
206 </blockquote>
207 <hr>
208 <h2>libwebsocket_service - Service any pending websocket activity</h2>
209 <i>int</i>
210 <b>libwebsocket_service</b>
211 (<i>struct libwebsocket_context *</i> <b>context</b>,
212 <i>int</i> <b>timeout_ms</b>)
213 <h3>Arguments</h3>
214 <dl>
215 <dt><b>context</b>
216 <dd>Websocket context
217 <dt><b>timeout_ms</b>
218 <dd>Timeout for poll; 0 means return immediately if nothing needed
219 service otherwise block and service immediately, returning
220 after the timeout if nothing needed service.
221 </dl>
222 <h3>Description</h3>
223 <blockquote>
224 This function deals with any pending websocket traffic, for three
225 kinds of event.  It handles these events on both server and client
226 types of connection the same.
227 <p>
228 1) Accept new connections to our context's server
229 <p>
230 2) Call the receive callback for incoming frame data received by
231 server or client connections.
232 <p>
233 You need to call this service function periodically to all the above
234 functions to happen; if your application is single-threaded you can
235 just call it in your main event loop.
236 <p>
237 Alternatively you can fork a new process that asynchronously handles
238 calling this service in a loop.  In that case you are happy if this
239 call blocks your thread until it needs to take care of something and
240 would call it with a large nonzero timeout.  Your loop then takes no
241 CPU while there is nothing happening.
242 <p>
243 If you are calling it in a single-threaded app, you don't want it to
244 wait around blocking other things in your loop from happening, so you
245 would call it with a timeout_ms of 0, so it returns immediately if
246 nothing is pending, or as soon as it services whatever was pending.
247 </blockquote>
248 <hr>
249 <h2>libwebsocket_callback_on_writable - Request a callback when this socket becomes able to be written to without blocking</h2>
250 <i>int</i>
251 <b>libwebsocket_callback_on_writable</b>
252 (<i>struct libwebsocket_context *</i> <b>context</b>,
253 <i>struct libwebsocket *</i> <b>wsi</b>)
254 <h3>Arguments</h3>
255 <dl>
256 <dt><b>context</b>
257 <dd>libwebsockets context
258 <dt><b>wsi</b>
259 <dd>Websocket connection instance to get callback for
260 </dl>
261 <hr>
262 <h2>libwebsocket_callback_on_writable_all_protocol - Request a callback for all connections using the given protocol when it becomes possible to write to each socket without blocking in turn.</h2>
263 <i>int</i>
264 <b>libwebsocket_callback_on_writable_all_protocol</b>
265 (<i>const struct libwebsocket_protocols *</i> <b>protocol</b>)
266 <h3>Arguments</h3>
267 <dl>
268 <dt><b>protocol</b>
269 <dd>Protocol whose connections will get callbacks
270 </dl>
271 <hr>
272 <h2>libwebsocket_set_timeout - marks the wsi as subject to a timeout</h2>
273 <i>void</i>
274 <b>libwebsocket_set_timeout</b>
275 (<i>struct libwebsocket *</i> <b>wsi</b>,
276 <i>enum pending_timeout</i> <b>reason</b>,
277 <i>int</i> <b>secs</b>)
278 <h3>Arguments</h3>
279 <dl>
280 <dt><b>wsi</b>
281 <dd>Websocket connection instance
282 <dt><b>reason</b>
283 <dd>timeout reason
284 <dt><b>secs</b>
285 <dd>how many seconds
286 </dl>
287 <h3>Description</h3>
288 <blockquote>
289 <p>
290 You will not need this unless you are doing something special
291 </blockquote>
292 <hr>
293 <h2>libwebsocket_get_socket_fd - returns the socket file descriptor</h2>
294 <i>int</i>
295 <b>libwebsocket_get_socket_fd</b>
296 (<i>struct libwebsocket *</i> <b>wsi</b>)
297 <h3>Arguments</h3>
298 <dl>
299 <dt><b>wsi</b>
300 <dd>Websocket connection instance
301 </dl>
302 <h3>Description</h3>
303 <blockquote>
304 <p>
305 You will not need this unless you are doing something special
306 </blockquote>
307 <hr>
308 <h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2>
309 <i>int</i>
310 <b>libwebsocket_rx_flow_control</b>
311 (<i>struct libwebsocket *</i> <b>wsi</b>,
312 <i>int</i> <b>enable</b>)
313 <h3>Arguments</h3>
314 <dl>
315 <dt><b>wsi</b>
316 <dd>Websocket connection instance to get callback for
317 <dt><b>enable</b>
318 <dd>0 = disable read servicing for this connection, 1 = enable
319 </dl>
320 <h3>Description</h3>
321 <blockquote>
322 <p>
323 If the output side of a server process becomes choked, this allows flow
324 control for the input side.
325 </blockquote>
326 <hr>
327 <h2>libwebsocket_canonical_hostname - returns this host's hostname</h2>
328 <i>const char *</i>
329 <b>libwebsocket_canonical_hostname</b>
330 (<i>struct libwebsocket_context *</i> <b>context</b>)
331 <h3>Arguments</h3>
332 <dl>
333 <dt><b>context</b>
334 <dd>Websocket context
335 </dl>
336 <h3>Description</h3>
337 <blockquote>
338 <p>
339 This is typically used by client code to fill in the host parameter
340 when making a client connection.  You can only call it after the context
341 has been created.
342 </blockquote>
343 <hr>
344 <h2>libwebsocket_create_context - Create the websocket handler</h2>
345 <i>struct libwebsocket_context *</i>
346 <b>libwebsocket_create_context</b>
347 (<i>int</i> <b>port</b>,
348 <i>const char *</i> <b>interf</b>,
349 <i>struct libwebsocket_protocols *</i> <b>protocols</b>,
350 <i>struct libwebsocket_extension *</i> <b>extensions</b>,
351 <i>const char *</i> <b>ssl_cert_filepath</b>,
352 <i>const char *</i> <b>ssl_private_key_filepath</b>,
353 <i>const char *</i> <b>ssl_ca_filepath</b>,
354 <i>int</i> <b>gid</b>,
355 <i>int</i> <b>uid</b>,
356 <i>unsigned int</i> <b>options</b>,
357 <i>void *</i> <b>user</b>)
358 <h3>Arguments</h3>
359 <dl>
360 <dt><b>port</b>
361 <dd>Port to listen on... you can use 0 to suppress listening on
362 any port, that's what you want if you are not running a
363 websocket server at all but just using it as a client
364 <dt><b>interf</b>
365 <dd>NULL to bind the listen socket to all interfaces, or the
366 interface name, eg, "eth2"
367 <dt><b>protocols</b>
368 <dd>Array of structures listing supported protocols and a protocol-
369 specific callback for each one.  The list is ended with an
370 entry that has a NULL callback pointer.
371 It's not const because we write the owning_server member
372 <dt><b>extensions</b>
373 <dd>NULL or array of libwebsocket_extension structs listing the
374 extensions this context supports.  If you configured with
375 --without-extensions, you should give NULL here.
376 <dt><b>ssl_cert_filepath</b>
377 <dd>If libwebsockets was compiled to use ssl, and you want
378 to listen using SSL, set to the filepath to fetch the
379 server cert from, otherwise NULL for unencrypted
380 <dt><b>ssl_private_key_filepath</b>
381 <dd>filepath to private key if wanting SSL mode,
382 else ignored
383 <dt><b>ssl_ca_filepath</b>
384 <dd>CA certificate filepath or NULL
385 <dt><b>gid</b>
386 <dd>group id to change to after setting listen socket, or -1.
387 <dt><b>uid</b>
388 <dd>user id to change to after setting listen socket, or -1.
389 <dt><b>options</b>
390 <dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
391 <dt><b>user</b>
392 <dd>optional user pointer that can be recovered via the context
393 pointer using libwebsocket_context_user 
394 </dl>
395 <h3>Description</h3>
396 <blockquote>
397 This function creates the listening socket and takes care
398 of all initialization in one step.
399 <p>
400 After initialization, it returns a struct libwebsocket_context * that
401 represents this server.  After calling, user code needs to take care
402 of calling <b>libwebsocket_service</b> with the context pointer to get the
403 server's sockets serviced.  This can be done in the same process context
404 or a forked process, or another thread,
405 <p>
406 The protocol callback functions are called for a handful of events
407 including http requests coming in, websocket connections becoming
408 established, and data arriving; it's also called periodically to allow
409 async transmission.
410 <p>
411 HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
412 at that time websocket protocol has not been negotiated.  Other
413 protocols after the first one never see any HTTP callack activity.
414 <p>
415 The server created is a simple http server by default; part of the
416 websocket standard is upgrading this http connection to a websocket one.
417 <p>
418 This allows the same server to provide files like scripts and favicon /
419 images or whatever over http and dynamic data over websockets all in
420 one place; they're all handled in the user callback.
421 </blockquote>
422 <hr>
423 <h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
424 <i>const struct libwebsocket_protocols *</i>
425 <b>libwebsockets_get_protocol</b>
426 (<i>struct libwebsocket *</i> <b>wsi</b>)
427 <h3>Arguments</h3>
428 <dl>
429 <dt><b>wsi</b>
430 <dd>pointer to struct websocket you want to know the protocol of
431 </dl>
432 <h3>Description</h3>
433 <blockquote>
434 <p>
435 Some apis can act on all live connections of a given protocol,
436 this is how you can get a pointer to the active protocol if needed.
437 </blockquote>
438 <hr>
439 <h2>lws_set_log_level - Set the logging bitfield</h2>
440 <i>void</i>
441 <b>lws_set_log_level</b>
442 (<i>int</i> <b>level</b>,
443 <i>void (*</i><b>log_emit_function</b>) <i>(int level, const char *line)</i>)
444 <h3>Arguments</h3>
445 <dl>
446 <dt><b>level</b>
447 <dd>OR together the LLL_ debug contexts you want output from
448 <dt><b>log_emit_function</b>
449 <dd>NULL to leave it as it is, or a user-supplied
450 function to perform log string emission instead of
451 the default stderr one.
452 </dl>
453 <h3>Description</h3>
454 <blockquote>
455 log level defaults to "err" and "warn" contexts enabled only and
456 emission on stderr.
457 </blockquote>
458 <hr>
459 <h2>libwebsocket_write - Apply protocol then write data to client</h2>
460 <i>int</i>
461 <b>libwebsocket_write</b>
462 (<i>struct libwebsocket *</i> <b>wsi</b>,
463 <i>unsigned char *</i> <b>buf</b>,
464 <i>size_t</i> <b>len</b>,
465 <i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
466 <h3>Arguments</h3>
467 <dl>
468 <dt><b>wsi</b>
469 <dd>Websocket instance (available from user callback)
470 <dt><b>buf</b>
471 <dd>The data to send.  For data being sent on a websocket
472 connection (ie, not default http), this buffer MUST have
473 LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
474 and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
475 in the buffer after (buf + len).  This is so the protocol
476 header and trailer data can be added in-situ.
477 <dt><b>len</b>
478 <dd>Count of the data bytes in the payload starting from buf
479 <dt><b>protocol</b>
480 <dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
481 of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
482 data on a websockets connection.  Remember to allow the extra
483 bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
484 are used.
485 </dl>
486 <h3>Description</h3>
487 <blockquote>
488 This function provides the way to issue data back to the client
489 for both http and websocket protocols.
490 <p>
491 In the case of sending using websocket protocol, be sure to allocate
492 valid storage before and after buf as explained above.  This scheme
493 allows maximum efficiency of sending data and protocol in a single
494 packet while not burdening the user code with any protocol knowledge.
495 </blockquote>
496 <hr>
497 <h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
498 <i>int</i>
499 <b>libwebsockets_serve_http_file</b>
500 (<i>struct libwebsocket_context *</i> <b>context</b>,
501 <i>struct libwebsocket *</i> <b>wsi</b>,
502 <i>const char *</i> <b>file</b>,
503 <i>const char *</i> <b>content_type</b>)
504 <h3>Arguments</h3>
505 <dl>
506 <dt><b>context</b>
507 <dd>libwebsockets context
508 <dt><b>wsi</b>
509 <dd>Websocket instance (available from user callback)
510 <dt><b>file</b>
511 <dd>The file to issue over http
512 <dt><b>content_type</b>
513 <dd>The http content type, eg, text/html
514 </dl>
515 <h3>Description</h3>
516 <blockquote>
517 This function is intended to be called from the callback in response
518 to http requests from the client.  It allows the callback to issue
519 local files down the http link in a single step.
520 <p>
521 Returning &lt;0 indicates error and the wsi should be closed.  Returning
522 &gt;0 indicates the file was completely sent and the wsi should be closed.
523 ==0 indicates the file transfer is started and needs more service later,
524 the wsi should be left alone.
525 </blockquote>
526 <hr>
527 <h2>lws_frame_is_binary - </h2>
528 <i>int</i>
529 <b>lws_frame_is_binary</b>
530 (<i>struct libwebsocket *</i> <b>wsi</b>)
531 <h3>Arguments</h3>
532 <dl>
533 <dt><b>wsi</b>
534 <dd>the connection we are inquiring about
535 </dl>
536 <h3>Description</h3>
537 <blockquote>
538 This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
539 it's interested to see if the frame it's dealing with was sent in binary
540 mode.
541 </blockquote>
542 <hr>
543 <h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
544 <i>size_t</i>
545 <b>libwebsockets_remaining_packet_payload</b>
546 (<i>struct libwebsocket *</i> <b>wsi</b>)
547 <h3>Arguments</h3>
548 <dl>
549 <dt><b>wsi</b>
550 <dd>Websocket instance (available from user callback)
551 </dl>
552 <h3>Description</h3>
553 <blockquote>
554 This function is intended to be called from the callback if the
555 user code is interested in "complete packets" from the client.
556 libwebsockets just passes through payload as it comes and issues a buffer
557 additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE
558 callback handler can use this API to find out if the buffer it has just
559 been given is the last piece of a "complete packet" from the client --
560 when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
561 0.
562 <p>
563 Many protocols won't care becuse their packets are always small.
564 </blockquote>
565 <hr>
566 <h2>callback - User server actions</h2>
567 <i>LWS_EXTERN int</i>
568 <b>callback</b>
569 (<i>struct libwebsocket_context *</i> <b>context</b>,
570 <i>struct libwebsocket *</i> <b>wsi</b>,
571 <i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
572 <i>void *</i> <b>user</b>,
573 <i>void *</i> <b>in</b>,
574 <i>size_t</i> <b>len</b>)
575 <h3>Arguments</h3>
576 <dl>
577 <dt><b>context</b>
578 <dd>Websockets context
579 <dt><b>wsi</b>
580 <dd>Opaque websocket instance pointer
581 <dt><b>reason</b>
582 <dd>The reason for the call
583 <dt><b>user</b>
584 <dd>Pointer to per-session user data allocated by library
585 <dt><b>in</b>
586 <dd>Pointer used for some callback reasons
587 <dt><b>len</b>
588 <dd>Length set for some callback reasons
589 </dl>
590 <h3>Description</h3>
591 <blockquote>
592 This callback is the way the user controls what is served.  All the
593 protocol detail is hidden and handled by the library.
594 <p>
595 For each connection / session there is user data allocated that is
596 pointed to by "user".  You set the size of this user data area when
597 the library is initialized with libwebsocket_create_server.
598 <p>
599 You get an opportunity to initialize user data when called back with
600 LWS_CALLBACK_ESTABLISHED reason.
601 </blockquote>
602 <h3>LWS_CALLBACK_ESTABLISHED</h3>
603 <blockquote>
604 after the server completes a handshake with
605 an incoming client
606 </blockquote>
607 <h3>LWS_CALLBACK_CLIENT_CONNECTION_ERROR</h3>
608 <blockquote>
609 the request client connection has
610 been unable to complete a handshake with the remote server
611 </blockquote>
612 <h3>LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH</h3>
613 <blockquote>
614 this is the last chance for the
615 client user code to examine the http headers
616 and decide to reject the connection.  If the
617 content in the headers is interesting to the
618 client (url, etc) it needs to copy it out at
619 this point since it will be destroyed before
620 the CLIENT_ESTABLISHED call
621 </blockquote>
622 <h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
623 <blockquote>
624 after your client connection completed
625 a handshake with the remote server
626 </blockquote>
627 <h3>LWS_CALLBACK_CLOSED</h3>
628 <blockquote>
629 when the websocket session ends
630 </blockquote>
631 <h3>LWS_CALLBACK_RECEIVE</h3>
632 <blockquote>
633 data has appeared for this server endpoint from a
634 remote client, it can be found at *in and is
635 len bytes long
636 </blockquote>
637 <h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3>
638 <blockquote>
639 if you elected to see PONG packets,
640 they appear with this callback reason.  PONG
641 packets only exist in 04+ protocol
642 </blockquote>
643 <h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
644 <blockquote>
645 data has appeared from the server for the
646 client connection, it can be found at *in and
647 is len bytes long
648 </blockquote>
649 <h3>LWS_CALLBACK_HTTP</h3>
650 <blockquote>
651 an http request has come from a client that is not
652 asking to upgrade the connection to a websocket
653 one.  This is a chance to serve http content,
654 for example, to send a script to the client
655 which will then open the websockets connection.
656 <tt><b>in</b></tt> points to the URI path requested and
657 <b>libwebsockets_serve_http_file</b> makes it very
658 simple to send back a file to the client.
659 Normally after sending the file you are done
660 with the http connection, since the rest of the
661 activity will come by websockets from the script
662 that was delivered by http, so you will want to
663 return 1; to close and free up the connection.
664 That's important because it uses a slot in the
665 total number of client connections allowed set
666 by MAX_CLIENTS.
667 </blockquote>
668 <h3>LWS_CALLBACK_HTTP_FILE_COMPLETION</h3>
669 <blockquote>
670 a file requested to be send down
671 http link has completed.
672 </blockquote>
673 <h3>LWS_CALLBACK_SERVER_WRITEABLE</h3>
674 <blockquote>
675 If you call
676 <b>libwebsocket_callback_on_writable</b> on a connection, you will
677 get one of these callbacks coming when the connection socket
678 is able to accept another write packet without blocking.
679 If it already was able to take another packet without blocking,
680 you'll get this callback at the next call to the service loop
681 function.  Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
682 and servers get LWS_CALLBACK_SERVER_WRITEABLE.
683 </blockquote>
684 <h3>LWS_CALLBACK_FILTER_NETWORK_CONNECTION</h3>
685 <blockquote>
686 called when a client connects to
687 the server at network level; the connection is accepted but then
688 passed to this callback to decide whether to hang up immediately
689 or not, based on the client IP.  <tt><b>user</b></tt> contains the connection
690 socket's descriptor.  Return non-zero to terminate
691 the connection before sending or receiving anything.
692 Because this happens immediately after the network connection
693 from the client, there's no websocket protocol selected yet so
694 this callback is issued only to protocol 0.
695 </blockquote>
696 <h3>LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION</h3>
697 <blockquote>
698 called when the handshake has
699 been received and parsed from the client, but the response is
700 not sent yet.  Return non-zero to disallow the connection.
701 <tt><b>user</b></tt> is a pointer to an array of struct lws_tokens, you can
702 use the header enums lws_token_indexes from libwebsockets.h
703 to check for and read the supported header presence and
704 content before deciding to allow the handshake to proceed or
705 to kill the connection.
706 </blockquote>
707 <h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS</h3>
708 <blockquote>
709 if configured for
710 including OpenSSL support, this callback allows your user code
711 to perform extra <b>SSL_CTX_load_verify_locations</b> or similar
712 calls to direct OpenSSL where to find certificates the client
713 can use to confirm the remote server identity.  <tt><b>user</b></tt> is the
714 OpenSSL SSL_CTX*
715 </blockquote>
716 <h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS</h3>
717 <blockquote>
718 if configured for
719 including OpenSSL support, this callback allows your user code
720 to load extra certifcates into the server which allow it to
721 verify the validity of certificates returned by clients.  <tt><b>user</b></tt>
722 is the server's OpenSSL SSL_CTX*
723 </blockquote>
724 <h3>LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION</h3>
725 <blockquote>
726 if the
727 libwebsockets context was created with the option
728 LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
729 callback is generated during OpenSSL verification of the cert
730 sent from the client.  It is sent to protocol[0] callback as
731 no protocol has been negotiated on the connection yet.
732 Notice that the libwebsockets context and wsi are both NULL
733 during this callback.  See
734 </blockquote>
735 <h3>http</h3>
736 <blockquote>
737 //www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
738 to understand more detail about the OpenSSL callback that
739 generates this libwebsockets callback and the meanings of the
740 arguments passed.  In this callback, <tt><b>user</b></tt> is the x509_ctx,
741 <tt><b>in</b></tt> is the ssl pointer and <tt><b>len</b></tt> is preverify_ok
742 Notice that this callback maintains libwebsocket return
743 conventions, return 0 to mean the cert is OK or 1 to fail it.
744 This also means that if you don't handle this callback then
745 the default callback action of returning 0 allows the client
746 certificates.
747 </blockquote>
748 <h3>LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER</h3>
749 <blockquote>
750 this callback happens
751 when a client handshake is being compiled.  <tt><b>user</b></tt> is NULL,
752 <tt><b>in</b></tt> is a char **, it's pointing to a char * which holds the
753 next location in the header buffer where you can add
754 headers, and <tt><b>len</b></tt> is the remaining space in the header buffer,
755 which is typically some hundreds of bytes.  So, to add a canned
756 cookie, your handler code might look similar to:
757 <p>
758 char **p = (char **)in;
759 <p>
760 if (len &lt; 100)
761 return 1;
762 <p>
763 *p += sprintf(*p, "Cookie: a=b\x0d\x0a");
764 <p>
765 return 0;
766 <p>
767 Notice if you add anything, you just have to take care about
768 the CRLF on the line you added.  Obviously this callback is
769 optional, if you don't handle it everything is fine.
770 <p>
771 Notice the callback is coming to protocols[0] all the time,
772 because there is no specific protocol handshook yet.
773 </blockquote>
774 <h3>LWS_CALLBACK_CONFIRM_EXTENSION_OKAY</h3>
775 <blockquote>
776 When the server handshake code
777 sees that it does support a requested extension, before
778 accepting the extension by additing to the list sent back to
779 the client it gives this callback just to check that it's okay
780 to use that extension.  It calls back to the requested protocol
781 and with <tt><b>in</b></tt> being the extension name, <tt><b>len</b></tt> is 0 and <tt><b>user</b></tt> is
782 valid.  Note though at this time the ESTABLISHED callback hasn't
783 happened yet so if you initialize <tt><b>user</b></tt> content there, <tt><b>user</b></tt>
784 content during this callback might not be useful for anything.
785 Notice this callback comes to protocols[0].
786 </blockquote>
787 <h3>LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED</h3>
788 <blockquote>
789 When a client
790 connection is being prepared to start a handshake to a server,
791 each supported extension is checked with protocols[0] callback
792 with this reason, giving the user code a chance to suppress the
793 claim to support that extension by returning non-zero.  If
794 unhandled, by default 0 will be returned and the extension
795 support included in the header to the server.  Notice this
796 callback comes to protocols[0].
797 <p>
798 The next four reasons are optional and only need taking care of if you
799 will be integrating libwebsockets sockets into an external polling
800 array.
801 </blockquote>
802 <h3>LWS_CALLBACK_ADD_POLL_FD</h3>
803 <blockquote>
804 libwebsocket deals with its <b>poll</b> loop
805 internally, but in the case you are integrating with another
806 server you will need to have libwebsocket sockets share a
807 polling array with the other server.  This and the other
808 POLL_FD related callbacks let you put your specialized
809 poll array interface code in the callback for protocol 0, the
810 first protocol you support, usually the HTTP protocol in the
811 serving case.  This callback happens when a socket needs to be
812 </blockquote>
813 <h3>added to the polling loop</h3>
814 <blockquote>
815 <tt><b>user</b></tt> contains the fd, and
816 <tt><b>len</b></tt> is the events bitmap (like, POLLIN).  If you are using the
817 internal polling loop (the "service" callback), you can just
818 ignore these callbacks.
819 </blockquote>
820 <h3>LWS_CALLBACK_DEL_POLL_FD</h3>
821 <blockquote>
822 This callback happens when a socket descriptor
823 needs to be removed from an external polling array.  <tt><b>user</b></tt> is
824 the socket desricptor.  If you are using the internal polling
825 loop, you can just ignore it.
826 </blockquote>
827 <h3>LWS_CALLBACK_SET_MODE_POLL_FD</h3>
828 <blockquote>
829 This callback happens when libwebsockets
830 wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
831 The handler should OR <tt><b>len</b></tt> on to the events member of the pollfd
832 struct for this socket descriptor.  If you are using the
833 internal polling loop, you can just ignore it.
834 </blockquote>
835 <h3>LWS_CALLBACK_CLEAR_MODE_POLL_FD</h3>
836 <blockquote>
837 This callback occurs when libwebsockets
838 wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
839 The handler should AND ~<tt><b>len</b></tt> on to the events member of the
840 pollfd struct for this socket descriptor.  If you are using the
841 internal polling loop, you can just ignore it.
842 </blockquote>
843 <hr>
844 <h2>extension_callback - Hooks to allow extensions to operate</h2>
845 <i>LWS_EXTERN int</i>
846 <b>extension_callback</b>
847 (<i>struct libwebsocket_context *</i> <b>context</b>,
848 <i>struct libwebsocket_extension *</i> <b>ext</b>,
849 <i>struct libwebsocket *</i> <b>wsi</b>,
850 <i>enum libwebsocket_extension_callback_reasons</i> <b>reason</b>,
851 <i>void *</i> <b>user</b>,
852 <i>void *</i> <b>in</b>,
853 <i>size_t</i> <b>len</b>)
854 <h3>Arguments</h3>
855 <dl>
856 <dt><b>context</b>
857 <dd>Websockets context
858 <dt><b>ext</b>
859 <dd>This extension
860 <dt><b>wsi</b>
861 <dd>Opaque websocket instance pointer
862 <dt><b>reason</b>
863 <dd>The reason for the call
864 <dt><b>user</b>
865 <dd>Pointer to per-session user data allocated by library
866 <dt><b>in</b>
867 <dd>Pointer used for some callback reasons
868 <dt><b>len</b>
869 <dd>Length set for some callback reasons
870 </dl>
871 <h3>Description</h3>
872 <blockquote>
873 Each extension that is active on a particular connection receives
874 callbacks during the connection lifetime to allow the extension to
875 operate on websocket data and manage itself.
876 <p>
877 Libwebsockets takes care of allocating and freeing "user" memory for
878 each active extension on each connection.  That is what is pointed to
879 by the <tt><b>user</b></tt> parameter.
880 </blockquote>
881 <h3>LWS_EXT_CALLBACK_CONSTRUCT</h3>
882 <blockquote>
883 called when the server has decided to
884 select this extension from the list provided by the client,
885 just before the server will send back the handshake accepting
886 the connection with this extension active.  This gives the
887 extension a chance to initialize its connection context found
888 in <tt><b>user</b></tt>.
889 </blockquote>
890 <h3>LWS_EXT_CALLBACK_CLIENT_CONSTRUCT</h3>
891 <blockquote>
892 same as LWS_EXT_CALLBACK_CONSTRUCT
893 but called when client is instantiating this extension.  Some
894 extensions will work the same on client and server side and then
895 you can just merge handlers for both CONSTRUCTS.
896 </blockquote>
897 <h3>LWS_EXT_CALLBACK_DESTROY</h3>
898 <blockquote>
899 called when the connection the extension was
900 being used on is about to be closed and deallocated.  It's the
901 last chance for the extension to deallocate anything it has
902 allocated in the user data (pointed to by <tt><b>user</b></tt>) before the
903 user data is deleted.  This same callback is used whether you
904 are in client or server instantiation context.
905 </blockquote>
906 <h3>LWS_EXT_CALLBACK_PACKET_RX_PREPARSE</h3>
907 <blockquote>
908 when this extension was active on
909 a connection, and a packet of data arrived at the connection,
910 it is passed to this callback to give the extension a chance to
911 change the data, eg, decompress it.  <tt><b>user</b></tt> is pointing to the
912 extension's private connection context data, <tt><b>in</b></tt> is pointing
913 to an lws_tokens struct, it consists of a char * pointer called
914 token, and an int called token_len.  At entry, these are
915 set to point to the received buffer and set to the content
916 length.  If the extension will grow the content, it should use
917 a new buffer allocated in its private user context data and
918 set the pointed-to lws_tokens members to point to its buffer.
919 </blockquote>
920 <h3>LWS_EXT_CALLBACK_PACKET_TX_PRESEND</h3>
921 <blockquote>
922 this works the same way as
923 LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
924 extension a chance to change websocket data just before it will
925 be sent out.  Using the same lws_token pointer scheme in <tt><b>in</b></tt>,
926 the extension can change the buffer and the length to be
927 transmitted how it likes.  Again if it wants to grow the
928 buffer safely, it should copy the data into its own buffer and
929 set the lws_tokens token pointer to it.
930 </blockquote>
931 <hr>
932 <h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
933 <b>struct libwebsocket_protocols</b> {<br>
934 &nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
935 &nbsp; &nbsp; <i>callback_function *</i> <b>callback</b>;<br>
936 &nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
937 &nbsp; &nbsp; <i>size_t</i> <b>rx_buffer_size</b>;<br>
938 &nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
939 &nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
940 };<br>
941 <h3>Members</h3>
942 <dl>
943 <dt><b>name</b>
944 <dd>Protocol name that must match the one given in the client
945 Javascript new WebSocket(url, 'protocol') name
946 <dt><b>callback</b>
947 <dd>The service callback used for this protocol.  It allows the
948 service action for an entire protocol to be encapsulated in
949 the protocol-specific callback
950 <dt><b>per_session_data_size</b>
951 <dd>Each new connection using this protocol gets
952 this much memory allocated on connection establishment and
953 freed on connection takedown.  A pointer to this per-connection
954 allocation is passed into the callback in the 'user' parameter
955 <dt><b>rx_buffer_size</b>
956 <dd>if you want atomic frames delivered to the callback, you
957 should set this to the size of the biggest legal frame that
958 you support.  If the frame size is exceeded, there is no
959 error, but the buffer will spill to the user callback when
960 full, which you can detect by using
961 <b>libwebsockets_remaining_packet_payload</b>.  Notice that you
962 just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING
963 and post-padding are automatically also allocated on top.
964 <dt><b>owning_server</b>
965 <dd>the server init call fills in this opaque pointer when
966 registering this protocol with the server.
967 <dt><b>protocol_index</b>
968 <dd>which protocol we are starting from zero
969 </dl>
970 <h3>Description</h3>
971 <blockquote>
972 This structure represents one protocol supported by the server.  An
973 array of these structures is passed to <b>libwebsocket_create_server</b>
974 allows as many protocols as you like to be handled by one server.
975 </blockquote>
976 <hr>
977 <h2>struct libwebsocket_extension - An extension we know how to cope with</h2>
978 <b>struct libwebsocket_extension</b> {<br>
979 &nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
980 &nbsp; &nbsp; <i>extension_callback_function *</i> <b>callback</b>;<br>
981 &nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
982 &nbsp; &nbsp; <i>void *</i> <b>per_context_private_data</b>;<br>
983 };<br>
984 <h3>Members</h3>
985 <dl>
986 <dt><b>name</b>
987 <dd>Formal extension name, eg, "deflate-stream"
988 <dt><b>callback</b>
989 <dd>Service callback
990 <dt><b>per_session_data_size</b>
991 <dd>Libwebsockets will auto-malloc this much
992 memory for the use of the extension, a pointer
993 to it comes in the <tt><b>user</b></tt> callback parameter
994 <dt><b>per_context_private_data</b>
995 <dd>Optional storage for this extension that
996 is per-context, so it can track stuff across
997 all sessions, etc, if it wants
998 </dl>
999 <hr>