blob: 239f3934d761dec1064794e85e1c182a6e9abddb [file] [log] [blame]
Andy Greenf7ee5492011-02-13 09:04:21 +00001<h2>libwebsockets_hangup_on_client - Server calls to terminate client connection</h2>
2<i>void</i>
3<b>libwebsockets_hangup_on_client</b>
Peter Hinz56885f32011-03-02 22:03:47 +00004(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Greenf7ee5492011-02-13 09:04:21 +00005<i>int</i> <b>fd</b>)
6<h3>Arguments</h3>
7<dl>
Peter Hinz56885f32011-03-02 22:03:47 +00008<dt><b>context</b>
Andy Greenf7ee5492011-02-13 09:04:21 +00009<dd>libwebsockets context
10<dt><b>fd</b>
11<dd>Connection socket descriptor
12</dl>
13<hr>
Andy Green07034092011-02-13 08:37:12 +000014<h2>libwebsockets_get_peer_addresses - Get client address information</h2>
15<i>void</i>
16<b>libwebsockets_get_peer_addresses</b>
17(<i>int</i> <b>fd</b>,
18<i>char *</i> <b>name</b>,
19<i>int</i> <b>name_len</b>,
20<i>char *</i> <b>rip</b>,
21<i>int</i> <b>rip_len</b>)
22<h3>Arguments</h3>
23<dl>
24<dt><b>fd</b>
25<dd>Connection socket descriptor
26<dt><b>name</b>
27<dd>Buffer to take client address name
28<dt><b>name_len</b>
29<dd>Length of client address name buffer
30<dt><b>rip</b>
31<dd>Buffer to take client address IP qotted quad
32<dt><b>rip_len</b>
33<dd>Length of client address IP buffer
34</dl>
35<h3>Description</h3>
36<blockquote>
37This function fills in <tt><b>name</b></tt> and <tt><b>rip</b></tt> with the name and IP of
38the client connected with socket descriptor <tt><b>fd</b></tt>. Names may be
39truncated if there is not enough room. If either cannot be
40determined, they will be returned as valid zero-length strings.
41</blockquote>
42<hr>
Andy Green9f990342011-02-12 11:57:45 +000043<h2>libwebsocket_service_fd - Service polled socket with something waiting</h2>
44<i>int</i>
45<b>libwebsocket_service_fd</b>
Peter Hinz56885f32011-03-02 22:03:47 +000046(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green9f990342011-02-12 11:57:45 +000047<i>struct pollfd *</i> <b>pollfd</b>)
48<h3>Arguments</h3>
49<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000050<dt><b>context</b>
Andy Green9f990342011-02-12 11:57:45 +000051<dd>Websocket context
52<dt><b>pollfd</b>
53<dd>The pollfd entry describing the socket fd and which events
54happened.
55</dl>
56<h3>Description</h3>
57<blockquote>
58This function closes any active connections and then frees the
59context. After calling this, any further use of the context is
60undefined.
61</blockquote>
62<hr>
Andy Green6964bb52011-01-23 16:50:33 +000063<h2>libwebsocket_context_destroy - Destroy the websocket context</h2>
64<i>void</i>
65<b>libwebsocket_context_destroy</b>
Peter Hinz56885f32011-03-02 22:03:47 +000066(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Green6964bb52011-01-23 16:50:33 +000067<h3>Arguments</h3>
68<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000069<dt><b>context</b>
Andy Green6964bb52011-01-23 16:50:33 +000070<dd>Websocket context
71</dl>
72<h3>Description</h3>
73<blockquote>
74This function closes any active connections and then frees the
75context. After calling this, any further use of the context is
76undefined.
77</blockquote>
78<hr>
79<h2>libwebsocket_service - Service any pending websocket activity</h2>
80<i>int</i>
81<b>libwebsocket_service</b>
Peter Hinz56885f32011-03-02 22:03:47 +000082(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green6964bb52011-01-23 16:50:33 +000083<i>int</i> <b>timeout_ms</b>)
84<h3>Arguments</h3>
85<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000086<dt><b>context</b>
Andy Green6964bb52011-01-23 16:50:33 +000087<dd>Websocket context
88<dt><b>timeout_ms</b>
89<dd>Timeout for poll; 0 means return immediately if nothing needed
90service otherwise block and service immediately, returning
91after the timeout if nothing needed service.
92</dl>
93<h3>Description</h3>
94<blockquote>
95This function deals with any pending websocket traffic, for three
96kinds of event. It handles these events on both server and client
97types of connection the same.
98<p>
991) Accept new connections to our context's server
100<p>
1012) Perform pending broadcast writes initiated from other forked
102processes (effectively serializing asynchronous broadcasts)
103<p>
1043) Call the receive callback for incoming frame data received by
105server or client connections.
106<p>
107You need to call this service function periodically to all the above
108functions to happen; if your application is single-threaded you can
109just call it in your main event loop.
110<p>
111Alternatively you can fork a new process that asynchronously handles
112calling this service in a loop. In that case you are happy if this
113call blocks your thread until it needs to take care of something and
114would call it with a large nonzero timeout. Your loop then takes no
115CPU while there is nothing happening.
116<p>
117If you are calling it in a single-threaded app, you don't want it to
118wait around blocking other things in your loop from happening, so you
119would call it with a timeout_ms of 0, so it returns immediately if
120nothing is pending, or as soon as it services whatever was pending.
121</blockquote>
122<hr>
Andy Green32375b72011-02-19 08:32:53 +0000123<h2>libwebsocket_callback_on_writable - Request a callback when this socket becomes able to be written to without blocking</h2>
Andy Green90c7cbc2011-01-27 06:26:52 +0000124<i>int</i>
125<b>libwebsocket_callback_on_writable</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000126(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000127<i>struct libwebsocket *</i> <b>wsi</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000128<h3>Arguments</h3>
129<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000130<dt><b>context</b>
Andy Green32375b72011-02-19 08:32:53 +0000131<dd>libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +0000132<dt><b>wsi</b>
133<dd>Websocket connection instance to get callback for
134</dl>
135<hr>
136<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>
137<i>int</i>
138<b>libwebsocket_callback_on_writable_all_protocol</b>
139(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>)
140<h3>Arguments</h3>
141<dl>
142<dt><b>protocol</b>
143<dd>Protocol whose connections will get callbacks
144</dl>
145<hr>
Andy Greenbe93fef2011-02-14 20:25:43 +0000146<h2>libwebsocket_set_timeout - marks the wsi as subject to a timeout</h2>
147<i>void</i>
148<b>libwebsocket_set_timeout</b>
149(<i>struct libwebsocket *</i> <b>wsi</b>,
150<i>enum pending_timeout</i> <b>reason</b>,
151<i>int</i> <b>secs</b>)
152<h3>Arguments</h3>
153<dl>
154<dt><b>wsi</b>
155<dd>Websocket connection instance
156<dt><b>reason</b>
157<dd>timeout reason
158<dt><b>secs</b>
159<dd>how many seconds
160</dl>
161<h3>Description</h3>
162<blockquote>
163<p>
164You will not need this unless you are doing something special
165</blockquote>
166<hr>
Andy Greena6cbece2011-01-27 20:06:03 +0000167<h2>libwebsocket_get_socket_fd - returns the socket file descriptor</h2>
168<i>int</i>
169<b>libwebsocket_get_socket_fd</b>
170(<i>struct libwebsocket *</i> <b>wsi</b>)
171<h3>Arguments</h3>
172<dl>
173<dt><b>wsi</b>
174<dd>Websocket connection instance
175</dl>
176<h3>Description</h3>
177<blockquote>
178<p>
179You will not need this unless you are doing something special
180</blockquote>
181<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000182<h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2>
183<i>int</i>
184<b>libwebsocket_rx_flow_control</b>
185(<i>struct libwebsocket *</i> <b>wsi</b>,
186<i>int</i> <b>enable</b>)
187<h3>Arguments</h3>
188<dl>
189<dt><b>wsi</b>
190<dd>Websocket connection instance to get callback for
191<dt><b>enable</b>
192<dd>0 = disable read servicing for this connection, 1 = enable
193</dl>
194<h3>Description</h3>
195<blockquote>
196<p>
197If the output side of a server process becomes choked, this allows flow
198control for the input side.
199</blockquote>
200<hr>
Andy Green2ac5a6f2011-01-28 10:00:18 +0000201<h2>libwebsocket_canonical_hostname - returns this host's hostname</h2>
202<i>const char *</i>
203<b>libwebsocket_canonical_hostname</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000204(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Green2ac5a6f2011-01-28 10:00:18 +0000205<h3>Arguments</h3>
206<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000207<dt><b>context</b>
Andy Green2ac5a6f2011-01-28 10:00:18 +0000208<dd>Websocket context
209</dl>
210<h3>Description</h3>
211<blockquote>
212<p>
213This is typically used by client code to fill in the host parameter
214when making a client connection. You can only call it after the context
215has been created.
216</blockquote>
217<hr>
Andy Green4739e5c2011-01-22 12:51:57 +0000218<h2>libwebsocket_create_context - Create the websocket handler</h2>
Andy Greene92cd172011-01-19 13:11:55 +0000219<i>struct libwebsocket_context *</i>
Andy Green4739e5c2011-01-22 12:51:57 +0000220<b>libwebsocket_create_context</b>
Andy Green62a12932010-11-03 11:19:23 +0000221(<i>int</i> <b>port</b>,
Peter Hinz56885f32011-03-02 22:03:47 +0000222<i>const char *</i> <b>interf</b>,
Andy Greenb45993c2010-12-18 15:13:50 +0000223<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Greend6e09112011-03-05 16:12:15 +0000224<i>struct libwebsocket_extension *</i> <b>extensions</b>,
Andy Green3faa9c72010-11-08 17:03:03 +0000225<i>const char *</i> <b>ssl_cert_filepath</b>,
226<i>const char *</i> <b>ssl_private_key_filepath</b>,
227<i>int</i> <b>gid</b>,
Andy Green8014b292011-01-30 20:57:25 +0000228<i>int</i> <b>uid</b>,
229<i>unsigned int</i> <b>options</b>)
Andy Green62a12932010-11-03 11:19:23 +0000230<h3>Arguments</h3>
231<dl>
232<dt><b>port</b>
Andy Green4739e5c2011-01-22 12:51:57 +0000233<dd>Port to listen on... you can use 0 to suppress listening on
234any port, that's what you want if you are not running a
235websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +0000236<dt><b>interf</b>
Andy Green32375b72011-02-19 08:32:53 +0000237<dd>NULL to bind the listen socket to all interfaces, or the
238interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +0000239<dt><b>protocols</b>
240<dd>Array of structures listing supported protocols and a protocol-
241specific callback for each one. The list is ended with an
242entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +0000243It's not const because we write the owning_server member
Andy Green3faa9c72010-11-08 17:03:03 +0000244<dt><b>ssl_cert_filepath</b>
245<dd>If libwebsockets was compiled to use ssl, and you want
246to listen using SSL, set to the filepath to fetch the
247server cert from, otherwise NULL for unencrypted
248<dt><b>ssl_private_key_filepath</b>
249<dd>filepath to private key if wanting SSL mode,
250else ignored
251<dt><b>gid</b>
252<dd>group id to change to after setting listen socket, or -1.
253<dt><b>uid</b>
254<dd>user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +0000255<dt><b>options</b>
256<dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green62a12932010-11-03 11:19:23 +0000257</dl>
258<h3>Description</h3>
259<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +0000260This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +0000261of all initialization in one step.
262<p>
Andy Greene92cd172011-01-19 13:11:55 +0000263After initialization, it returns a struct libwebsocket_context * that
264represents this server. After calling, user code needs to take care
265of calling <b>libwebsocket_service</b> with the context pointer to get the
266server's sockets serviced. This can be done in the same process context
267or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +0000268<p>
269The protocol callback functions are called for a handful of events
270including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +0000271established, and data arriving; it's also called periodically to allow
272async transmission.
273<p>
Andy Green47943ae2010-11-12 11:15:49 +0000274HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
275at that time websocket protocol has not been negotiated. Other
276protocols after the first one never see any HTTP callack activity.
277<p>
Andy Green62a12932010-11-03 11:19:23 +0000278The server created is a simple http server by default; part of the
279websocket standard is upgrading this http connection to a websocket one.
280<p>
281This allows the same server to provide files like scripts and favicon /
282images or whatever over http and dynamic data over websockets all in
283one place; they're all handled in the user callback.
284</blockquote>
285<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000286<h2>libwebsockets_fork_service_loop - Optional helper function forks off a process for the websocket server loop. You don't have to use this but if not, you have to make sure you are calling libwebsocket_service periodically to service the websocket traffic</h2>
287<i>int</i>
288<b>libwebsockets_fork_service_loop</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000289(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Greene92cd172011-01-19 13:11:55 +0000290<h3>Arguments</h3>
291<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000292<dt><b>context</b>
Andy Greene92cd172011-01-19 13:11:55 +0000293<dd>server context returned by creation function
294</dl>
295<hr>
Andy Greenb45993c2010-12-18 15:13:50 +0000296<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
297<i>const struct libwebsocket_protocols *</i>
298<b>libwebsockets_get_protocol</b>
299(<i>struct libwebsocket *</i> <b>wsi</b>)
300<h3>Arguments</h3>
301<dl>
302<dt><b>wsi</b>
303<dd>pointer to struct websocket you want to know the protocol of
304</dl>
305<h3>Description</h3>
306<blockquote>
307<p>
308This is useful to get the protocol to broadcast back to from inside
309the callback.
310</blockquote>
311<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000312<h2>libwebsockets_broadcast - Sends a buffer to the callback for all active connections of the given protocol.</h2>
Andy Greenb45993c2010-12-18 15:13:50 +0000313<i>int</i>
314<b>libwebsockets_broadcast</b>
315(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
316<i>unsigned char *</i> <b>buf</b>,
317<i>size_t</i> <b>len</b>)
318<h3>Arguments</h3>
319<dl>
320<dt><b>protocol</b>
321<dd>pointer to the protocol you will broadcast to all members of
322<dt><b>buf</b>
323<dd>buffer containing the data to be broadcase. NOTE: this has to be
324allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
325the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
326case you are calling this function from callback context.
327<dt><b>len</b>
328<dd>length of payload data in buf, starting from buf.
329</dl>
330<h3>Description</h3>
331<blockquote>
332This function allows bulk sending of a packet to every connection using
333the given protocol. It does not send the data directly; instead it calls
334the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
335wants to actually send the data for that connection, the callback itself
336should call <b>libwebsocket_write</b>.
337<p>
338<b>libwebsockets_broadcast</b> can be called from another fork context without
339having to take any care about data visibility between the processes, it'll
340"just work".
341</blockquote>
342<hr>
Andy Green62a12932010-11-03 11:19:23 +0000343<h2>libwebsocket_write - Apply protocol then write data to client</h2>
344<i>int</i>
345<b>libwebsocket_write</b>
346(<i>struct libwebsocket *</i> <b>wsi</b>,
347<i>unsigned char *</i> <b>buf</b>,
348<i>size_t</i> <b>len</b>,
349<i>enum libwebsocket_write_protocol</i> <b>protocol</b>)
350<h3>Arguments</h3>
351<dl>
352<dt><b>wsi</b>
353<dd>Websocket instance (available from user callback)
354<dt><b>buf</b>
355<dd>The data to send. For data being sent on a websocket
356connection (ie, not default http), this buffer MUST have
357LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
358and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
359in the buffer after (buf + len). This is so the protocol
360header and trailer data can be added in-situ.
361<dt><b>len</b>
362<dd>Count of the data bytes in the payload starting from buf
363<dt><b>protocol</b>
364<dd>Use LWS_WRITE_HTTP to reply to an http connection, and one
365of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
366data on a websockets connection. Remember to allow the extra
367bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
368are used.
369</dl>
370<h3>Description</h3>
371<blockquote>
372This function provides the way to issue data back to the client
373for both http and websocket protocols.
374<p>
375In the case of sending using websocket protocol, be sure to allocate
376valid storage before and after buf as explained above. This scheme
377allows maximum efficiency of sending data and protocol in a single
378packet while not burdening the user code with any protocol knowledge.
379</blockquote>
380<hr>
381<h2>libwebsockets_serve_http_file - Send a file back to the client using http</h2>
382<i>int</i>
383<b>libwebsockets_serve_http_file</b>
384(<i>struct libwebsocket *</i> <b>wsi</b>,
385<i>const char *</i> <b>file</b>,
386<i>const char *</i> <b>content_type</b>)
387<h3>Arguments</h3>
388<dl>
389<dt><b>wsi</b>
390<dd>Websocket instance (available from user callback)
391<dt><b>file</b>
392<dd>The file to issue over http
393<dt><b>content_type</b>
394<dd>The http content type, eg, text/html
395</dl>
396<h3>Description</h3>
397<blockquote>
398This function is intended to be called from the callback in response
399to http requests from the client. It allows the callback to issue
400local files down the http link in a single step.
401</blockquote>
402<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000403<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
404<i>size_t</i>
405<b>libwebsockets_remaining_packet_payload</b>
406(<i>struct libwebsocket *</i> <b>wsi</b>)
407<h3>Arguments</h3>
408<dl>
409<dt><b>wsi</b>
410<dd>Websocket instance (available from user callback)
411</dl>
412<h3>Description</h3>
413<blockquote>
414This function is intended to be called from the callback if the
415user code is interested in "complete packets" from the client.
416libwebsockets just passes through payload as it comes and issues a buffer
417additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
418callback handler can use this API to find out if the buffer it has just
419been given is the last piece of a "complete packet" from the client --
420when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
4210.
422<p>
423Many protocols won't care becuse their packets are always small.
424</blockquote>
425<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000426<h2>libwebsocket_client_connect - Connect to another websocket server</h2>
427<i>struct libwebsocket *</i>
428<b>libwebsocket_client_connect</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000429(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green90c7cbc2011-01-27 06:26:52 +0000430<i>const char *</i> <b>address</b>,
431<i>int</i> <b>port</b>,
432<i>int</i> <b>ssl_connection</b>,
433<i>const char *</i> <b>path</b>,
434<i>const char *</i> <b>host</b>,
435<i>const char *</i> <b>origin</b>,
Andy Greenbfb051f2011-02-09 08:49:14 +0000436<i>const char *</i> <b>protocol</b>,
437<i>int</i> <b>ietf_version_or_minus_one</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000438<h3>Arguments</h3>
439<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000440<dt><b>context</b>
Andy Green90c7cbc2011-01-27 06:26:52 +0000441<dd>Websocket context
442<dt><b>address</b>
443<dd>Remote server address, eg, "myserver.com"
444<dt><b>port</b>
445<dd>Port to connect to on the remote server, eg, 80
446<dt><b>ssl_connection</b>
447<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
448signed certs
449<dt><b>path</b>
450<dd>Websocket path on server
451<dt><b>host</b>
452<dd>Hostname on server
453<dt><b>origin</b>
454<dd>Socket origin name
455<dt><b>protocol</b>
456<dd>Comma-separated list of protocols being asked for from
457the server, or just one. The server will pick the one it
458likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000459<dt><b>ietf_version_or_minus_one</b>
460<dd>-1 to ask to connect using the default, latest
461protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000462</dl>
463<h3>Description</h3>
464<blockquote>
465This function creates a connection to a remote server
466</blockquote>
467<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000468<h2>callback - User server actions</h2>
469<i>int</i>
470<b>callback</b>
Darin Willitsc19456f2011-02-14 17:52:39 +0000471(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000472<i>struct libwebsocket *</i> <b>wsi</b>,
Andy Green8f037e42010-12-19 22:13:26 +0000473<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
474<i>void *</i> <b>user</b>,
475<i>void *</i> <b>in</b>,
476<i>size_t</i> <b>len</b>)
477<h3>Arguments</h3>
478<dl>
Andy Green32375b72011-02-19 08:32:53 +0000479<dt><b>context</b>
480<dd>Websockets context
Andy Green8f037e42010-12-19 22:13:26 +0000481<dt><b>wsi</b>
482<dd>Opaque websocket instance pointer
483<dt><b>reason</b>
484<dd>The reason for the call
485<dt><b>user</b>
486<dd>Pointer to per-session user data allocated by library
487<dt><b>in</b>
488<dd>Pointer used for some callback reasons
489<dt><b>len</b>
490<dd>Length set for some callback reasons
491</dl>
492<h3>Description</h3>
493<blockquote>
494This callback is the way the user controls what is served. All the
495protocol detail is hidden and handled by the library.
496<p>
497For each connection / session there is user data allocated that is
498pointed to by "user". You set the size of this user data area when
499the library is initialized with libwebsocket_create_server.
500<p>
501You get an opportunity to initialize user data when called back with
502LWS_CALLBACK_ESTABLISHED reason.
503</blockquote>
504<h3>LWS_CALLBACK_ESTABLISHED</h3>
505<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000506after the server completes a handshake with
507an incoming client
508</blockquote>
509<h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
510<blockquote>
511after your client connection completed
512a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000513</blockquote>
514<h3>LWS_CALLBACK_CLOSED</h3>
515<blockquote>
516when the websocket session ends
517</blockquote>
518<h3>LWS_CALLBACK_BROADCAST</h3>
519<blockquote>
520signal to send to client (you would use
521<b>libwebsocket_write</b> taking care about the
522special buffer requirements
523</blockquote>
524<h3>LWS_CALLBACK_RECEIVE</h3>
525<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000526data has appeared for this server endpoint from a
527remote client, it can be found at *in and is
528len bytes long
529</blockquote>
Andy Greena6cbece2011-01-27 20:06:03 +0000530<h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3>
531<blockquote>
532if you elected to see PONG packets,
533they appear with this callback reason. PONG
534packets only exist in 04+ protocol
535</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000536<h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
537<blockquote>
538data has appeared from the server for the
539client connection, it can be found at *in and
540is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000541</blockquote>
542<h3>LWS_CALLBACK_HTTP</h3>
543<blockquote>
544an http request has come from a client that is not
545asking to upgrade the connection to a websocket
546one. This is a chance to serve http content,
547for example, to send a script to the client
548which will then open the websockets connection.
Andy Green7619c472011-01-23 17:47:08 +0000549<tt><b>in</b></tt> points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000550<b>libwebsockets_serve_http_file</b> makes it very
551simple to send back a file to the client.
552</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000553<h3>LWS_CALLBACK_CLIENT_WRITEABLE</h3>
554<blockquote>
555if you call
556<b>libwebsocket_callback_on_writable</b> on a connection, you will
557get this callback coming when the connection socket is able to
558accept another write packet without blocking. If it already
559was able to take another packet without blocking, you'll get
560this callback at the next call to the service loop function.
561</blockquote>
Andy Green07034092011-02-13 08:37:12 +0000562<h3>LWS_CALLBACK_FILTER_NETWORK_CONNECTION</h3>
563<blockquote>
564called when a client connects to
565the server at network level; the connection is accepted but then
566passed to this callback to decide whether to hang up immediately
567or not, based on the client IP. <tt><b>user</b></tt> contains the connection
568socket's descriptor. Return non-zero to terminate
569the connection before sending or receiving anything.
570Because this happens immediately after the network connection
571from the client, there's no websocket protocol selected yet so
572this callback is issued only to protocol 0.
573</blockquote>
Andy Greenc85619d2011-02-13 08:25:26 +0000574<h3>LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION</h3>
575<blockquote>
576called when the handshake has
577been received and parsed from the client, but the response is
578not sent yet. Return non-zero to disallow the connection.
Andy Green07034092011-02-13 08:37:12 +0000579<tt><b>user</b></tt> is a pointer to an array of struct lws_tokens, you can
580use the header enums lws_token_indexes from libwebsockets.h
581to check for and read the supported header presence and
582content before deciding to allow the handshake to proceed or
583to kill the connection.
Andy Green0894bda2011-02-19 09:09:11 +0000584</blockquote>
585<h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS</h3>
586<blockquote>
Andy Green6901cb32011-02-21 08:06:47 +0000587if configured for
Andy Green0894bda2011-02-19 09:09:11 +0000588including OpenSSL support, this callback allows your user code
589to perform extra <b>SSL_CTX_load_verify_locations</b> or similar
590calls to direct OpenSSL where to find certificates the client
591can use to confirm the remote server identity. <tt><b>user</b></tt> is the
592OpenSSL SSL_CTX*
Andy Green6901cb32011-02-21 08:06:47 +0000593</blockquote>
594<h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS</h3>
595<blockquote>
596if configured for
597including OpenSSL support, this callback allows your user code
598to load extra certifcates into the server which allow it to
599verify the validity of certificates returned by clients. <tt><b>user</b></tt>
600is the server's OpenSSL SSL_CTX*
601</blockquote>
602<h3>LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION</h3>
603<blockquote>
604if the
605libwebsockets context was created with the option
606LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
607callback is generated during OpenSSL verification of the cert
608sent from the client. It is sent to protocol[0] callback as
609no protocol has been negotiated on the connection yet.
610Notice that the libwebsockets context and wsi are both NULL
611during this callback. See
612</blockquote>
613<h3>http</h3>
614<blockquote>
615//www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
616to understand more detail about the OpenSSL callback that
617generates this libwebsockets callback and the meanings of the
618arguments passed. In this callback, <tt><b>user</b></tt> is the x509_ctx,
619<tt><b>in</b></tt> is the ssl pointer and <tt><b>len</b></tt> is preverify_ok
620Notice that this callback maintains libwebsocket return
621conventions, return 0 to mean the cert is OK or 1 to fail it.
622This also means that if you don't handle this callback then
623the default callback action of returning 0 allows the client
624certificates.
Andy Green385e7ad2011-03-01 21:06:02 +0000625</blockquote>
626<h3>LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER</h3>
627<blockquote>
628this callback happens
629when a client handshake is being compiled. <tt><b>user</b></tt> is NULL,
630<tt><b>in</b></tt> is a char **, it's pointing to a char * which holds the
631next location in the header buffer where you can add
632headers, and <tt><b>len</b></tt> is the remaining space in the header buffer,
633which is typically some hundreds of bytes. So, to add a canned
634cookie, your handler code might look similar to:
635<p>
636char **p = (char **)in;
637<p>
638if (len &lt; 100)
639return 1;
640<p>
641*p += sprintf(*p, "Cookie: a=b\x0d\x0a");
642<p>
643return 0;
644<p>
645Notice if you add anything, you just have to take care about
646the CRLF on the line you added. Obviously this callback is
647optional, if you don't handle it everything is fine.
648<p>
649Notice the callback is coming to protocols[0] all the time,
650because there is no specific protocol handshook yet.
Andy Greenc85619d2011-02-13 08:25:26 +0000651<p>
652The next four reasons are optional and only need taking care of if you
653will be integrating libwebsockets sockets into an external polling
654array.
655</blockquote>
656<h3>LWS_CALLBACK_ADD_POLL_FD</h3>
657<blockquote>
658libwebsocket deals with its <b>poll</b> loop
659internally, but in the case you are integrating with another
660server you will need to have libwebsocket sockets share a
661polling array with the other server. This and the other
662POLL_FD related callbacks let you put your specialized
663poll array interface code in the callback for protocol 0, the
664first protocol you support, usually the HTTP protocol in the
665serving case. This callback happens when a socket needs to be
666</blockquote>
667<h3>added to the polling loop</h3>
668<blockquote>
669<tt><b>user</b></tt> contains the fd, and
670<tt><b>len</b></tt> is the events bitmap (like, POLLIN). If you are using the
671internal polling loop (the "service" callback), you can just
672ignore these callbacks.
673</blockquote>
674<h3>LWS_CALLBACK_DEL_POLL_FD</h3>
675<blockquote>
676This callback happens when a socket descriptor
677needs to be removed from an external polling array. <tt><b>user</b></tt> is
678the socket desricptor. If you are using the internal polling
679loop, you can just ignore it.
680</blockquote>
681<h3>LWS_CALLBACK_SET_MODE_POLL_FD</h3>
682<blockquote>
683This callback happens when libwebsockets
684wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
685The handler should OR <tt><b>len</b></tt> on to the events member of the pollfd
686struct for this socket descriptor. If you are using the
687internal polling loop, you can just ignore it.
688</blockquote>
689<h3>LWS_CALLBACK_CLEAR_MODE_POLL_FD</h3>
690<blockquote>
691This callback occurs when libwebsockets
692wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
693The handler should AND ~<tt><b>len</b></tt> on to the events member of the
694pollfd struct for this socket descriptor. If you are using the
695internal polling loop, you can just ignore it.
696</blockquote>
Andy Green8f037e42010-12-19 22:13:26 +0000697<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000698<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
699<b>struct libwebsocket_protocols</b> {<br>
700&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
Darin Willitsc19456f2011-02-14 17:52:39 +0000701&nbsp; &nbsp; <i>int (*</i><b>callback</b>) <i>(struct libwebsocket_context * context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)</i>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000702&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000703&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
704&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
705&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
706&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000707};<br>
708<h3>Members</h3>
709<dl>
710<dt><b>name</b>
711<dd>Protocol name that must match the one given in the client
712Javascript new WebSocket(url, 'protocol') name
713<dt><b>callback</b>
714<dd>The service callback used for this protocol. It allows the
715service action for an entire protocol to be encapsulated in
716the protocol-specific callback
717<dt><b>per_session_data_size</b>
718<dd>Each new connection using this protocol gets
719this much memory allocated on connection establishment and
720freed on connection takedown. A pointer to this per-connection
721allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000722<dt><b>owning_server</b>
723<dd>the server init call fills in this opaque pointer when
724registering this protocol with the server.
725<dt><b>broadcast_socket_port</b>
726<dd>the server init call fills this in with the
727localhost port number used to forward broadcasts for this
728protocol
729<dt><b>broadcast_socket_user_fd</b>
730<dd>the server init call fills this in ... the <b>main</b>
731process context can write to this socket to perform broadcasts
732(use the <b>libwebsockets_broadcast</b> api to do this instead,
733it works from any process context)
734<dt><b>protocol_index</b>
735<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000736</dl>
737<h3>Description</h3>
738<blockquote>
739This structure represents one protocol supported by the server. An
740array of these structures is passed to <b>libwebsocket_create_server</b>
741allows as many protocols as you like to be handled by one server.
742</blockquote>
743<hr>
Andy Greend6e09112011-03-05 16:12:15 +0000744<h2>struct libwebsocket_extension - An extension we know how to cope with</h2>
745<b>struct libwebsocket_extension</b> {<br>
746&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
747&nbsp; &nbsp; <i>int (*</i><b>callback</b>) <i>(struct libwebsocket_context *context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user,void *in, size_t len)</i>;<br>
748&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
749};<br>
750<h3>Members</h3>
751<dl>
752<dt><b>name</b>
753<dd>Formal extension name, eg, "deflate-stream"
754<dt><b>callback</b>
755<dd>Service callback
756<dt><b>per_session_data_size</b>
757<dd>Libwebsockets will auto-malloc this much
758memory for the use of the extension, a pointer
759to it comes in the <tt><b>user</b></tt> callback parameter
760</dl>
761<hr>