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