blob: d910b2d70d56028b790a6d1c98cf5bbb15405b85 [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>
Andy Green75006172013-01-22 12:32:11 +080058This function takes a pollfd that has POLLIN or POLLOUT activity and
59services it according to the state of the associated struct libwebsocket.
60<p>
61The one call deals with all "service" that might happen on a socket
62including listen accepts, http files as well as websocket protocol.
Andy Green9f990342011-02-12 11:57:45 +000063</blockquote>
64<hr>
Andy Green6964bb52011-01-23 16:50:33 +000065<h2>libwebsocket_context_destroy - Destroy the websocket context</h2>
66<i>void</i>
67<b>libwebsocket_context_destroy</b>
Peter Hinz56885f32011-03-02 22:03:47 +000068(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Green6964bb52011-01-23 16:50:33 +000069<h3>Arguments</h3>
70<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000071<dt><b>context</b>
Andy Green6964bb52011-01-23 16:50:33 +000072<dd>Websocket context
73</dl>
74<h3>Description</h3>
75<blockquote>
76This function closes any active connections and then frees the
77context. After calling this, any further use of the context is
78undefined.
79</blockquote>
80<hr>
81<h2>libwebsocket_service - Service any pending websocket activity</h2>
82<i>int</i>
83<b>libwebsocket_service</b>
Peter Hinz56885f32011-03-02 22:03:47 +000084(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green6964bb52011-01-23 16:50:33 +000085<i>int</i> <b>timeout_ms</b>)
86<h3>Arguments</h3>
87<dl>
Peter Hinz56885f32011-03-02 22:03:47 +000088<dt><b>context</b>
Andy Green6964bb52011-01-23 16:50:33 +000089<dd>Websocket context
90<dt><b>timeout_ms</b>
91<dd>Timeout for poll; 0 means return immediately if nothing needed
92service otherwise block and service immediately, returning
93after the timeout if nothing needed service.
94</dl>
95<h3>Description</h3>
96<blockquote>
97This function deals with any pending websocket traffic, for three
98kinds of event. It handles these events on both server and client
99types of connection the same.
100<p>
1011) Accept new connections to our context's server
102<p>
1032) Perform pending broadcast writes initiated from other forked
104processes (effectively serializing asynchronous broadcasts)
105<p>
1063) Call the receive callback for incoming frame data received by
107server or client connections.
108<p>
109You need to call this service function periodically to all the above
110functions to happen; if your application is single-threaded you can
111just call it in your main event loop.
112<p>
113Alternatively you can fork a new process that asynchronously handles
114calling this service in a loop. In that case you are happy if this
115call blocks your thread until it needs to take care of something and
116would call it with a large nonzero timeout. Your loop then takes no
117CPU while there is nothing happening.
118<p>
119If you are calling it in a single-threaded app, you don't want it to
120wait around blocking other things in your loop from happening, so you
121would call it with a timeout_ms of 0, so it returns immediately if
122nothing is pending, or as soon as it services whatever was pending.
123</blockquote>
124<hr>
Andy Green32375b72011-02-19 08:32:53 +0000125<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 +0000126<i>int</i>
127<b>libwebsocket_callback_on_writable</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000128(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000129<i>struct libwebsocket *</i> <b>wsi</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000130<h3>Arguments</h3>
131<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000132<dt><b>context</b>
Andy Green32375b72011-02-19 08:32:53 +0000133<dd>libwebsockets context
Andy Green90c7cbc2011-01-27 06:26:52 +0000134<dt><b>wsi</b>
135<dd>Websocket connection instance to get callback for
136</dl>
137<hr>
138<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>
139<i>int</i>
140<b>libwebsocket_callback_on_writable_all_protocol</b>
141(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>)
142<h3>Arguments</h3>
143<dl>
144<dt><b>protocol</b>
145<dd>Protocol whose connections will get callbacks
146</dl>
147<hr>
Andy Greenbe93fef2011-02-14 20:25:43 +0000148<h2>libwebsocket_set_timeout - marks the wsi as subject to a timeout</h2>
149<i>void</i>
150<b>libwebsocket_set_timeout</b>
151(<i>struct libwebsocket *</i> <b>wsi</b>,
152<i>enum pending_timeout</i> <b>reason</b>,
153<i>int</i> <b>secs</b>)
154<h3>Arguments</h3>
155<dl>
156<dt><b>wsi</b>
157<dd>Websocket connection instance
158<dt><b>reason</b>
159<dd>timeout reason
160<dt><b>secs</b>
161<dd>how many seconds
162</dl>
163<h3>Description</h3>
164<blockquote>
165<p>
166You will not need this unless you are doing something special
167</blockquote>
168<hr>
Andy Greena6cbece2011-01-27 20:06:03 +0000169<h2>libwebsocket_get_socket_fd - returns the socket file descriptor</h2>
170<i>int</i>
171<b>libwebsocket_get_socket_fd</b>
172(<i>struct libwebsocket *</i> <b>wsi</b>)
173<h3>Arguments</h3>
174<dl>
175<dt><b>wsi</b>
176<dd>Websocket connection instance
177</dl>
178<h3>Description</h3>
179<blockquote>
180<p>
181You will not need this unless you are doing something special
182</blockquote>
183<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000184<h2>libwebsocket_rx_flow_control - Enable and disable socket servicing for receieved packets.</h2>
185<i>int</i>
186<b>libwebsocket_rx_flow_control</b>
187(<i>struct libwebsocket *</i> <b>wsi</b>,
188<i>int</i> <b>enable</b>)
189<h3>Arguments</h3>
190<dl>
191<dt><b>wsi</b>
192<dd>Websocket connection instance to get callback for
193<dt><b>enable</b>
194<dd>0 = disable read servicing for this connection, 1 = enable
195</dl>
196<h3>Description</h3>
197<blockquote>
198<p>
199If the output side of a server process becomes choked, this allows flow
200control for the input side.
201</blockquote>
202<hr>
Andy Green2ac5a6f2011-01-28 10:00:18 +0000203<h2>libwebsocket_canonical_hostname - returns this host's hostname</h2>
204<i>const char *</i>
205<b>libwebsocket_canonical_hostname</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000206(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Green2ac5a6f2011-01-28 10:00:18 +0000207<h3>Arguments</h3>
208<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000209<dt><b>context</b>
Andy Green2ac5a6f2011-01-28 10:00:18 +0000210<dd>Websocket context
211</dl>
212<h3>Description</h3>
213<blockquote>
214<p>
215This is typically used by client code to fill in the host parameter
216when making a client connection. You can only call it after the context
217has been created.
218</blockquote>
219<hr>
Andy Green4739e5c2011-01-22 12:51:57 +0000220<h2>libwebsocket_create_context - Create the websocket handler</h2>
Andy Greene92cd172011-01-19 13:11:55 +0000221<i>struct libwebsocket_context *</i>
Andy Green4739e5c2011-01-22 12:51:57 +0000222<b>libwebsocket_create_context</b>
Andy Green62a12932010-11-03 11:19:23 +0000223(<i>int</i> <b>port</b>,
Peter Hinz56885f32011-03-02 22:03:47 +0000224<i>const char *</i> <b>interf</b>,
Andy Greenb45993c2010-12-18 15:13:50 +0000225<i>struct libwebsocket_protocols *</i> <b>protocols</b>,
Andy Greend6e09112011-03-05 16:12:15 +0000226<i>struct libwebsocket_extension *</i> <b>extensions</b>,
Andy Green3faa9c72010-11-08 17:03:03 +0000227<i>const char *</i> <b>ssl_cert_filepath</b>,
228<i>const char *</i> <b>ssl_private_key_filepath</b>,
David Galeano2f82be82013-01-09 16:25:54 +0800229<i>const char *</i> <b>ssl_ca_filepath</b>,
Andy Green3faa9c72010-11-08 17:03:03 +0000230<i>int</i> <b>gid</b>,
Andy Green8014b292011-01-30 20:57:25 +0000231<i>int</i> <b>uid</b>,
Alon Levy0291eb32012-10-19 11:21:56 +0200232<i>unsigned int</i> <b>options</b>,
233<i>void *</i> <b>user</b>)
Andy Green62a12932010-11-03 11:19:23 +0000234<h3>Arguments</h3>
235<dl>
236<dt><b>port</b>
Andy Green4739e5c2011-01-22 12:51:57 +0000237<dd>Port to listen on... you can use 0 to suppress listening on
238any port, that's what you want if you are not running a
239websocket server at all but just using it as a client
Peter Hinz56885f32011-03-02 22:03:47 +0000240<dt><b>interf</b>
Andy Green32375b72011-02-19 08:32:53 +0000241<dd>NULL to bind the listen socket to all interfaces, or the
242interface name, eg, "eth2"
Andy Green4f3943a2010-11-12 10:44:16 +0000243<dt><b>protocols</b>
244<dd>Array of structures listing supported protocols and a protocol-
245specific callback for each one. The list is ended with an
246entry that has a NULL callback pointer.
Andy Greenb45993c2010-12-18 15:13:50 +0000247It's not const because we write the owning_server member
Andy Greenc5114822011-03-06 10:29:35 +0000248<dt><b>extensions</b>
249<dd>NULL or array of libwebsocket_extension structs listing the
Andy Green3182ece2013-01-20 17:08:31 +0800250extensions this context supports. If you configured with
251--without-extensions, you should give NULL here.
Andy Green3faa9c72010-11-08 17:03:03 +0000252<dt><b>ssl_cert_filepath</b>
253<dd>If libwebsockets was compiled to use ssl, and you want
254to listen using SSL, set to the filepath to fetch the
255server cert from, otherwise NULL for unencrypted
256<dt><b>ssl_private_key_filepath</b>
257<dd>filepath to private key if wanting SSL mode,
258else ignored
David Galeano2f82be82013-01-09 16:25:54 +0800259<dt><b>ssl_ca_filepath</b>
Andy Green988bd982013-01-10 12:26:13 +0800260<dd>CA certificate filepath or NULL
Andy Green3faa9c72010-11-08 17:03:03 +0000261<dt><b>gid</b>
262<dd>group id to change to after setting listen socket, or -1.
263<dt><b>uid</b>
264<dd>user id to change to after setting listen socket, or -1.
Andy Greenbfb051f2011-02-09 08:49:14 +0000265<dt><b>options</b>
266<dd>0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK
Andy Green788c4a82012-10-22 12:29:57 +0100267<dt><b>user</b>
268<dd>optional user pointer that can be recovered via the context
269pointer using libwebsocket_context_user
Andy Green62a12932010-11-03 11:19:23 +0000270</dl>
271<h3>Description</h3>
272<blockquote>
Andy Green47943ae2010-11-12 11:15:49 +0000273This function creates the listening socket and takes care
Andy Green62a12932010-11-03 11:19:23 +0000274of all initialization in one step.
275<p>
Andy Greene92cd172011-01-19 13:11:55 +0000276After initialization, it returns a struct libwebsocket_context * that
277represents this server. After calling, user code needs to take care
278of calling <b>libwebsocket_service</b> with the context pointer to get the
279server's sockets serviced. This can be done in the same process context
280or a forked process, or another thread,
Andy Green47943ae2010-11-12 11:15:49 +0000281<p>
282The protocol callback functions are called for a handful of events
283including http requests coming in, websocket connections becoming
Andy Green62a12932010-11-03 11:19:23 +0000284established, and data arriving; it's also called periodically to allow
285async transmission.
286<p>
Andy Green47943ae2010-11-12 11:15:49 +0000287HTTP requests are sent always to the FIRST protocol in <tt><b>protocol</b></tt>, since
288at that time websocket protocol has not been negotiated. Other
289protocols after the first one never see any HTTP callack activity.
290<p>
Andy Green62a12932010-11-03 11:19:23 +0000291The server created is a simple http server by default; part of the
292websocket standard is upgrading this http connection to a websocket one.
293<p>
294This allows the same server to provide files like scripts and favicon /
295images or whatever over http and dynamic data over websockets all in
296one place; they're all handled in the user callback.
297</blockquote>
298<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000299<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>
300<i>int</i>
301<b>libwebsockets_fork_service_loop</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000302(<i>struct libwebsocket_context *</i> <b>context</b>)
Andy Greene92cd172011-01-19 13:11:55 +0000303<h3>Arguments</h3>
304<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000305<dt><b>context</b>
Andy Greene92cd172011-01-19 13:11:55 +0000306<dd>server context returned by creation function
307</dl>
308<hr>
Andy Greenb45993c2010-12-18 15:13:50 +0000309<h2>libwebsockets_get_protocol - Returns a protocol pointer from a websocket connection.</h2>
310<i>const struct libwebsocket_protocols *</i>
311<b>libwebsockets_get_protocol</b>
312(<i>struct libwebsocket *</i> <b>wsi</b>)
313<h3>Arguments</h3>
314<dl>
315<dt><b>wsi</b>
316<dd>pointer to struct websocket you want to know the protocol of
317</dl>
318<h3>Description</h3>
319<blockquote>
320<p>
321This is useful to get the protocol to broadcast back to from inside
322the callback.
323</blockquote>
324<hr>
Andy Greene92cd172011-01-19 13:11:55 +0000325<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 +0000326<i>int</i>
327<b>libwebsockets_broadcast</b>
328(<i>const struct libwebsocket_protocols *</i> <b>protocol</b>,
329<i>unsigned char *</i> <b>buf</b>,
330<i>size_t</i> <b>len</b>)
331<h3>Arguments</h3>
332<dl>
333<dt><b>protocol</b>
334<dd>pointer to the protocol you will broadcast to all members of
335<dt><b>buf</b>
336<dd>buffer containing the data to be broadcase. NOTE: this has to be
337allocated with LWS_SEND_BUFFER_PRE_PADDING valid bytes before
338the pointer and LWS_SEND_BUFFER_POST_PADDING afterwards in the
339case you are calling this function from callback context.
340<dt><b>len</b>
341<dd>length of payload data in buf, starting from buf.
342</dl>
343<h3>Description</h3>
344<blockquote>
345This function allows bulk sending of a packet to every connection using
346the given protocol. It does not send the data directly; instead it calls
347the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback
348wants to actually send the data for that connection, the callback itself
349should call <b>libwebsocket_write</b>.
350<p>
351<b>libwebsockets_broadcast</b> can be called from another fork context without
352having to take any care about data visibility between the processes, it'll
353"just work".
354</blockquote>
355<hr>
Andy Greenacbaee62013-01-18 22:00:22 +0800356<h2>lws_confirm_legit_wsi - </h2>
357<i>int</i>
358<b>lws_confirm_legit_wsi</b>
359(<i>struct libwebsocket *</i> <b>wsi</b>)
360<h3>Arguments</h3>
361<dl>
362<dt><b>wsi</b>
363<dd>struct libwebsocket to assess
364</dl>
365<h3>Description</h3>
366<blockquote>
367Performs consistecy checks on what the wsi claims and what the
368polling arrays hold. This'll catch a closed wsi still in use.
369Don't try to use on the listen (nonconnection) wsi as it will
370fail it. Otherwise 0 return == wsi seems consistent.
371</blockquote>
372<hr>
Andy Green43db0452013-01-10 19:50:35 +0800373<h2>lws_set_log_level - Set the logging bitfield</h2>
374<i>void</i>
375<b>lws_set_log_level</b>
Andy Greende8f27a2013-01-12 09:17:42 +0800376(<i>int</i> <b>level</b>,
Andy Green058ba812013-01-19 11:32:18 +0800377<i>void (*</i><b>log_emit_function</b>) <i>(int level, const char *line)</i>)
Andy Green43db0452013-01-10 19:50:35 +0800378<h3>Arguments</h3>
379<dl>
380<dt><b>level</b>
381<dd>OR together the LLL_ debug contexts you want output from
Andy Greende8f27a2013-01-12 09:17:42 +0800382<dt><b>log_emit_function</b>
383<dd>NULL to leave it as it is, or a user-supplied
384function to perform log string emission instead of
385the default stderr one.
Andy Green43db0452013-01-10 19:50:35 +0800386</dl>
387<h3>Description</h3>
388<blockquote>
Andy Greende8f27a2013-01-12 09:17:42 +0800389log level defaults to "err" and "warn" contexts enabled only and
390emission on stderr.
Andy Green43db0452013-01-10 19:50:35 +0800391</blockquote>
392<hr>
Andy Green2fd3f2f2013-01-18 09:49:20 +0800393<h2>lws_frame_is_binary - </h2>
394<i>int</i>
395<b>lws_frame_is_binary</b>
396(<i>struct libwebsocket *</i> <b>wsi</b>)
397<h3>Arguments</h3>
398<dl>
399<dt><b>wsi</b>
400<dd>the connection we are inquiring about
401</dl>
402<h3>Description</h3>
403<blockquote>
404This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
405it's interested to see if the frame it's dealing with was sent in binary
406mode.
407</blockquote>
408<hr>
Andy Green38e57bb2011-01-19 12:20:27 +0000409<h2>libwebsockets_remaining_packet_payload - Bytes to come before "overall" rx packet is complete</h2>
410<i>size_t</i>
411<b>libwebsockets_remaining_packet_payload</b>
412(<i>struct libwebsocket *</i> <b>wsi</b>)
413<h3>Arguments</h3>
414<dl>
415<dt><b>wsi</b>
416<dd>Websocket instance (available from user callback)
417</dl>
418<h3>Description</h3>
419<blockquote>
420This function is intended to be called from the callback if the
421user code is interested in "complete packets" from the client.
422libwebsockets just passes through payload as it comes and issues a buffer
423additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
424callback handler can use this API to find out if the buffer it has just
425been given is the last piece of a "complete packet" from the client --
426when that is the case <b>libwebsockets_remaining_packet_payload</b> will return
4270.
428<p>
429Many protocols won't care becuse their packets are always small.
430</blockquote>
431<hr>
Andy Green90c7cbc2011-01-27 06:26:52 +0000432<h2>libwebsocket_client_connect - Connect to another websocket server</h2>
433<i>struct libwebsocket *</i>
434<b>libwebsocket_client_connect</b>
Peter Hinz56885f32011-03-02 22:03:47 +0000435(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green90c7cbc2011-01-27 06:26:52 +0000436<i>const char *</i> <b>address</b>,
437<i>int</i> <b>port</b>,
438<i>int</i> <b>ssl_connection</b>,
439<i>const char *</i> <b>path</b>,
440<i>const char *</i> <b>host</b>,
441<i>const char *</i> <b>origin</b>,
Andy Greenbfb051f2011-02-09 08:49:14 +0000442<i>const char *</i> <b>protocol</b>,
443<i>int</i> <b>ietf_version_or_minus_one</b>)
Andy Green90c7cbc2011-01-27 06:26:52 +0000444<h3>Arguments</h3>
445<dl>
Peter Hinz56885f32011-03-02 22:03:47 +0000446<dt><b>context</b>
Andy Green90c7cbc2011-01-27 06:26:52 +0000447<dd>Websocket context
448<dt><b>address</b>
449<dd>Remote server address, eg, "myserver.com"
450<dt><b>port</b>
451<dd>Port to connect to on the remote server, eg, 80
452<dt><b>ssl_connection</b>
453<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
454signed certs
455<dt><b>path</b>
456<dd>Websocket path on server
457<dt><b>host</b>
458<dd>Hostname on server
459<dt><b>origin</b>
460<dd>Socket origin name
461<dt><b>protocol</b>
462<dd>Comma-separated list of protocols being asked for from
463the server, or just one. The server will pick the one it
464likes best.
Andy Greenbfb051f2011-02-09 08:49:14 +0000465<dt><b>ietf_version_or_minus_one</b>
466<dd>-1 to ask to connect using the default, latest
467protocol supported, or the specific protocol ordinal
Andy Green90c7cbc2011-01-27 06:26:52 +0000468</dl>
469<h3>Description</h3>
470<blockquote>
471This function creates a connection to a remote server
472</blockquote>
473<hr>
David Brooks2c60d952012-04-20 12:19:01 +0800474<h2>libwebsocket_client_connect_extended - Connect to another websocket server</h2>
475<i>struct libwebsocket *</i>
476<b>libwebsocket_client_connect_extended</b>
477(<i>struct libwebsocket_context *</i> <b>context</b>,
478<i>const char *</i> <b>address</b>,
479<i>int</i> <b>port</b>,
480<i>int</i> <b>ssl_connection</b>,
481<i>const char *</i> <b>path</b>,
482<i>const char *</i> <b>host</b>,
483<i>const char *</i> <b>origin</b>,
484<i>const char *</i> <b>protocol</b>,
485<i>int</i> <b>ietf_version_or_minus_one</b>,
486<i>void *</i> <b>userdata</b>)
487<h3>Arguments</h3>
488<dl>
489<dt><b>context</b>
490<dd>Websocket context
491<dt><b>address</b>
492<dd>Remote server address, eg, "myserver.com"
493<dt><b>port</b>
494<dd>Port to connect to on the remote server, eg, 80
495<dt><b>ssl_connection</b>
496<dd>0 = ws://, 1 = wss:// encrypted, 2 = wss:// allow self
497signed certs
498<dt><b>path</b>
499<dd>Websocket path on server
500<dt><b>host</b>
501<dd>Hostname on server
502<dt><b>origin</b>
503<dd>Socket origin name
504<dt><b>protocol</b>
505<dd>Comma-separated list of protocols being asked for from
506the server, or just one. The server will pick the one it
507likes best.
508<dt><b>ietf_version_or_minus_one</b>
509<dd>-1 to ask to connect using the default, latest
510protocol supported, or the specific protocol ordinal
511<dt><b>userdata</b>
512<dd>Pre-allocated user data
513</dl>
514<h3>Description</h3>
515<blockquote>
516This function creates a connection to a remote server
517</blockquote>
518<hr>
Andy Green8f037e42010-12-19 22:13:26 +0000519<h2>callback - User server actions</h2>
Andy Green07b56e62011-10-03 19:30:22 +0800520<i>LWS_EXTERN int</i>
Andy Green8f037e42010-12-19 22:13:26 +0000521<b>callback</b>
Darin Willitsc19456f2011-02-14 17:52:39 +0000522(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green62c54d22011-02-14 09:14:25 +0000523<i>struct libwebsocket *</i> <b>wsi</b>,
Andy Green8f037e42010-12-19 22:13:26 +0000524<i>enum libwebsocket_callback_reasons</i> <b>reason</b>,
525<i>void *</i> <b>user</b>,
526<i>void *</i> <b>in</b>,
527<i>size_t</i> <b>len</b>)
528<h3>Arguments</h3>
529<dl>
Andy Green32375b72011-02-19 08:32:53 +0000530<dt><b>context</b>
531<dd>Websockets context
Andy Green8f037e42010-12-19 22:13:26 +0000532<dt><b>wsi</b>
533<dd>Opaque websocket instance pointer
534<dt><b>reason</b>
535<dd>The reason for the call
536<dt><b>user</b>
537<dd>Pointer to per-session user data allocated by library
538<dt><b>in</b>
539<dd>Pointer used for some callback reasons
540<dt><b>len</b>
541<dd>Length set for some callback reasons
542</dl>
543<h3>Description</h3>
544<blockquote>
545This callback is the way the user controls what is served. All the
546protocol detail is hidden and handled by the library.
547<p>
548For each connection / session there is user data allocated that is
549pointed to by "user". You set the size of this user data area when
550the library is initialized with libwebsocket_create_server.
551<p>
552You get an opportunity to initialize user data when called back with
553LWS_CALLBACK_ESTABLISHED reason.
554</blockquote>
555<h3>LWS_CALLBACK_ESTABLISHED</h3>
556<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000557after the server completes a handshake with
558an incoming client
559</blockquote>
David Brooks2c60d952012-04-20 12:19:01 +0800560<h3>LWS_CALLBACK_CLIENT_CONNECTION_ERROR</h3>
561<blockquote>
562the request client connection has
563been unable to complete a handshake with the remote server
564</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000565<h3>LWS_CALLBACK_CLIENT_ESTABLISHED</h3>
566<blockquote>
567after your client connection completed
568a handshake with the remote server
Andy Green8f037e42010-12-19 22:13:26 +0000569</blockquote>
570<h3>LWS_CALLBACK_CLOSED</h3>
571<blockquote>
572when the websocket session ends
573</blockquote>
574<h3>LWS_CALLBACK_BROADCAST</h3>
575<blockquote>
576signal to send to client (you would use
577<b>libwebsocket_write</b> taking care about the
578special buffer requirements
579</blockquote>
580<h3>LWS_CALLBACK_RECEIVE</h3>
581<blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000582data has appeared for this server endpoint from a
583remote client, it can be found at *in and is
584len bytes long
585</blockquote>
Andy Greena6cbece2011-01-27 20:06:03 +0000586<h3>LWS_CALLBACK_CLIENT_RECEIVE_PONG</h3>
587<blockquote>
588if you elected to see PONG packets,
589they appear with this callback reason. PONG
590packets only exist in 04+ protocol
591</blockquote>
Andy Green90c7cbc2011-01-27 06:26:52 +0000592<h3>LWS_CALLBACK_CLIENT_RECEIVE</h3>
593<blockquote>
594data has appeared from the server for the
595client connection, it can be found at *in and
596is len bytes long
Andy Green8f037e42010-12-19 22:13:26 +0000597</blockquote>
598<h3>LWS_CALLBACK_HTTP</h3>
599<blockquote>
600an http request has come from a client that is not
601asking to upgrade the connection to a websocket
602one. This is a chance to serve http content,
603for example, to send a script to the client
604which will then open the websockets connection.
Andy Green7619c472011-01-23 17:47:08 +0000605<tt><b>in</b></tt> points to the URI path requested and
Andy Green8f037e42010-12-19 22:13:26 +0000606<b>libwebsockets_serve_http_file</b> makes it very
607simple to send back a file to the client.
Andy Green24b588b2013-01-13 09:53:18 +0800608Normally after sending the file you are done
609with the http connection, since the rest of the
610activity will come by websockets from the script
611that was delivered by http, so you will want to
612return 1; to close and free up the connection.
613That's important because it uses a slot in the
614total number of client connections allowed set
615by MAX_CLIENTS.
Andy Green8f037e42010-12-19 22:13:26 +0000616</blockquote>
Andy Greend280b6e2013-01-15 13:40:23 +0800617<h3>LWS_CALLBACK_HTTP_FILE_COMPLETION</h3>
618<blockquote>
619a file requested to be send down
620http link has completed.
621</blockquote>
Andy Greene9739ed2011-03-07 21:40:59 +0000622<h3>LWS_CALLBACK_SERVER_WRITEABLE</h3>
Andy Green90c7cbc2011-01-27 06:26:52 +0000623<blockquote>
Andy Greene9739ed2011-03-07 21:40:59 +0000624If you call
Andy Green90c7cbc2011-01-27 06:26:52 +0000625<b>libwebsocket_callback_on_writable</b> on a connection, you will
Andy Greene9739ed2011-03-07 21:40:59 +0000626get one of these callbacks coming when the connection socket
627is able to accept another write packet without blocking.
628If it already was able to take another packet without blocking,
629you'll get this callback at the next call to the service loop
630function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE
631and servers get LWS_CALLBACK_SERVER_WRITEABLE.
Andy Green90c7cbc2011-01-27 06:26:52 +0000632</blockquote>
Andy Green07034092011-02-13 08:37:12 +0000633<h3>LWS_CALLBACK_FILTER_NETWORK_CONNECTION</h3>
634<blockquote>
635called when a client connects to
636the server at network level; the connection is accepted but then
637passed to this callback to decide whether to hang up immediately
638or not, based on the client IP. <tt><b>user</b></tt> contains the connection
639socket's descriptor. Return non-zero to terminate
640the connection before sending or receiving anything.
641Because this happens immediately after the network connection
642from the client, there's no websocket protocol selected yet so
643this callback is issued only to protocol 0.
644</blockquote>
Andy Greenc85619d2011-02-13 08:25:26 +0000645<h3>LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION</h3>
646<blockquote>
647called when the handshake has
648been received and parsed from the client, but the response is
649not sent yet. Return non-zero to disallow the connection.
Andy Green07034092011-02-13 08:37:12 +0000650<tt><b>user</b></tt> is a pointer to an array of struct lws_tokens, you can
651use the header enums lws_token_indexes from libwebsockets.h
652to check for and read the supported header presence and
653content before deciding to allow the handshake to proceed or
654to kill the connection.
Andy Green0894bda2011-02-19 09:09:11 +0000655</blockquote>
656<h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS</h3>
657<blockquote>
Andy Green6901cb32011-02-21 08:06:47 +0000658if configured for
Andy Green0894bda2011-02-19 09:09:11 +0000659including OpenSSL support, this callback allows your user code
660to perform extra <b>SSL_CTX_load_verify_locations</b> or similar
661calls to direct OpenSSL where to find certificates the client
662can use to confirm the remote server identity. <tt><b>user</b></tt> is the
663OpenSSL SSL_CTX*
Andy Green6901cb32011-02-21 08:06:47 +0000664</blockquote>
665<h3>LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS</h3>
666<blockquote>
667if configured for
668including OpenSSL support, this callback allows your user code
669to load extra certifcates into the server which allow it to
670verify the validity of certificates returned by clients. <tt><b>user</b></tt>
671is the server's OpenSSL SSL_CTX*
672</blockquote>
673<h3>LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION</h3>
674<blockquote>
675if the
676libwebsockets context was created with the option
677LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this
678callback is generated during OpenSSL verification of the cert
679sent from the client. It is sent to protocol[0] callback as
680no protocol has been negotiated on the connection yet.
681Notice that the libwebsockets context and wsi are both NULL
682during this callback. See
683</blockquote>
684<h3>http</h3>
685<blockquote>
686//www.openssl.org/docs/ssl/SSL_CTX_set_verify.html
687to understand more detail about the OpenSSL callback that
688generates this libwebsockets callback and the meanings of the
689arguments passed. In this callback, <tt><b>user</b></tt> is the x509_ctx,
690<tt><b>in</b></tt> is the ssl pointer and <tt><b>len</b></tt> is preverify_ok
691Notice that this callback maintains libwebsocket return
692conventions, return 0 to mean the cert is OK or 1 to fail it.
693This also means that if you don't handle this callback then
694the default callback action of returning 0 allows the client
695certificates.
Andy Green385e7ad2011-03-01 21:06:02 +0000696</blockquote>
697<h3>LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER</h3>
698<blockquote>
699this callback happens
700when a client handshake is being compiled. <tt><b>user</b></tt> is NULL,
701<tt><b>in</b></tt> is a char **, it's pointing to a char * which holds the
702next location in the header buffer where you can add
703headers, and <tt><b>len</b></tt> is the remaining space in the header buffer,
704which is typically some hundreds of bytes. So, to add a canned
705cookie, your handler code might look similar to:
706<p>
707char **p = (char **)in;
708<p>
709if (len &lt; 100)
710return 1;
711<p>
712*p += sprintf(*p, "Cookie: a=b\x0d\x0a");
713<p>
714return 0;
715<p>
716Notice if you add anything, you just have to take care about
717the CRLF on the line you added. Obviously this callback is
718optional, if you don't handle it everything is fine.
719<p>
720Notice the callback is coming to protocols[0] all the time,
721because there is no specific protocol handshook yet.
Andy Greenc5114822011-03-06 10:29:35 +0000722</blockquote>
723<h3>LWS_CALLBACK_CONFIRM_EXTENSION_OKAY</h3>
724<blockquote>
725When the server handshake code
726sees that it does support a requested extension, before
727accepting the extension by additing to the list sent back to
728the client it gives this callback just to check that it's okay
729to use that extension. It calls back to the requested protocol
730and with <tt><b>in</b></tt> being the extension name, <tt><b>len</b></tt> is 0 and <tt><b>user</b></tt> is
731valid. Note though at this time the ESTABLISHED callback hasn't
732happened yet so if you initialize <tt><b>user</b></tt> content there, <tt><b>user</b></tt>
733content during this callback might not be useful for anything.
734Notice this callback comes to protocols[0].
Andy Greenc6517fa2011-03-06 13:15:29 +0000735</blockquote>
736<h3>LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED</h3>
737<blockquote>
738When a client
739connection is being prepared to start a handshake to a server,
740each supported extension is checked with protocols[0] callback
741with this reason, giving the user code a chance to suppress the
742claim to support that extension by returning non-zero. If
743unhandled, by default 0 will be returned and the extension
744support included in the header to the server. Notice this
745callback comes to protocols[0].
Andy Greenc85619d2011-02-13 08:25:26 +0000746<p>
747The next four reasons are optional and only need taking care of if you
748will be integrating libwebsockets sockets into an external polling
749array.
750</blockquote>
751<h3>LWS_CALLBACK_ADD_POLL_FD</h3>
752<blockquote>
753libwebsocket deals with its <b>poll</b> loop
754internally, but in the case you are integrating with another
755server you will need to have libwebsocket sockets share a
756polling array with the other server. This and the other
757POLL_FD related callbacks let you put your specialized
758poll array interface code in the callback for protocol 0, the
759first protocol you support, usually the HTTP protocol in the
760serving case. This callback happens when a socket needs to be
761</blockquote>
762<h3>added to the polling loop</h3>
763<blockquote>
764<tt><b>user</b></tt> contains the fd, and
765<tt><b>len</b></tt> is the events bitmap (like, POLLIN). If you are using the
766internal polling loop (the "service" callback), you can just
767ignore these callbacks.
768</blockquote>
769<h3>LWS_CALLBACK_DEL_POLL_FD</h3>
770<blockquote>
771This callback happens when a socket descriptor
772needs to be removed from an external polling array. <tt><b>user</b></tt> is
773the socket desricptor. If you are using the internal polling
774loop, you can just ignore it.
775</blockquote>
776<h3>LWS_CALLBACK_SET_MODE_POLL_FD</h3>
777<blockquote>
778This callback happens when libwebsockets
779wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
780The handler should OR <tt><b>len</b></tt> on to the events member of the pollfd
781struct for this socket descriptor. If you are using the
782internal polling loop, you can just ignore it.
783</blockquote>
784<h3>LWS_CALLBACK_CLEAR_MODE_POLL_FD</h3>
785<blockquote>
786This callback occurs when libwebsockets
787wants to modify the events for the socket descriptor in <tt><b>user</b></tt>.
788The handler should AND ~<tt><b>len</b></tt> on to the events member of the
789pollfd struct for this socket descriptor. If you are using the
790internal polling loop, you can just ignore it.
791</blockquote>
Andy Green8f037e42010-12-19 22:13:26 +0000792<hr>
Andy Green57b4e9a2011-03-06 13:14:46 +0000793<h2>extension_callback - Hooks to allow extensions to operate</h2>
Andy Green07b56e62011-10-03 19:30:22 +0800794<i>LWS_EXTERN int</i>
Andy Green57b4e9a2011-03-06 13:14:46 +0000795<b>extension_callback</b>
796(<i>struct libwebsocket_context *</i> <b>context</b>,
Andy Green46c2ea02011-03-22 09:04:01 +0000797<i>struct libwebsocket_extension *</i> <b>ext</b>,
Andy Green57b4e9a2011-03-06 13:14:46 +0000798<i>struct libwebsocket *</i> <b>wsi</b>,
David Brooks2c60d952012-04-20 12:19:01 +0800799<i>enum libwebsocket_extension_callback_reasons</i> <b>reason</b>,
Andy Green57b4e9a2011-03-06 13:14:46 +0000800<i>void *</i> <b>user</b>,
801<i>void *</i> <b>in</b>,
802<i>size_t</i> <b>len</b>)
803<h3>Arguments</h3>
804<dl>
805<dt><b>context</b>
806<dd>Websockets context
Andy Green46c2ea02011-03-22 09:04:01 +0000807<dt><b>ext</b>
808<dd>This extension
Andy Green57b4e9a2011-03-06 13:14:46 +0000809<dt><b>wsi</b>
810<dd>Opaque websocket instance pointer
811<dt><b>reason</b>
812<dd>The reason for the call
813<dt><b>user</b>
814<dd>Pointer to per-session user data allocated by library
815<dt><b>in</b>
816<dd>Pointer used for some callback reasons
817<dt><b>len</b>
818<dd>Length set for some callback reasons
819</dl>
820<h3>Description</h3>
821<blockquote>
822Each extension that is active on a particular connection receives
823callbacks during the connection lifetime to allow the extension to
824operate on websocket data and manage itself.
825<p>
826Libwebsockets takes care of allocating and freeing "user" memory for
827each active extension on each connection. That is what is pointed to
828by the <tt><b>user</b></tt> parameter.
829</blockquote>
830<h3>LWS_EXT_CALLBACK_CONSTRUCT</h3>
831<blockquote>
832called when the server has decided to
833select this extension from the list provided by the client,
834just before the server will send back the handshake accepting
835the connection with this extension active. This gives the
836extension a chance to initialize its connection context found
837in <tt><b>user</b></tt>.
838</blockquote>
Andy Green2366b1c2011-03-06 13:15:31 +0000839<h3>LWS_EXT_CALLBACK_CLIENT_CONSTRUCT</h3>
840<blockquote>
841same as LWS_EXT_CALLBACK_CONSTRUCT
842but called when client is instantiating this extension. Some
843extensions will work the same on client and server side and then
844you can just merge handlers for both CONSTRUCTS.
845</blockquote>
Andy Green57b4e9a2011-03-06 13:14:46 +0000846<h3>LWS_EXT_CALLBACK_DESTROY</h3>
847<blockquote>
848called when the connection the extension was
849being used on is about to be closed and deallocated. It's the
850last chance for the extension to deallocate anything it has
851allocated in the user data (pointed to by <tt><b>user</b></tt>) before the
Andy Green2366b1c2011-03-06 13:15:31 +0000852user data is deleted. This same callback is used whether you
853are in client or server instantiation context.
Andy Green57b4e9a2011-03-06 13:14:46 +0000854</blockquote>
855<h3>LWS_EXT_CALLBACK_PACKET_RX_PREPARSE</h3>
856<blockquote>
857when this extension was active on
858a connection, and a packet of data arrived at the connection,
859it is passed to this callback to give the extension a chance to
860change the data, eg, decompress it. <tt><b>user</b></tt> is pointing to the
861extension's private connection context data, <tt><b>in</b></tt> is pointing
862to an lws_tokens struct, it consists of a char * pointer called
863token, and an int called token_len. At entry, these are
864set to point to the received buffer and set to the content
865length. If the extension will grow the content, it should use
866a new buffer allocated in its private user context data and
867set the pointed-to lws_tokens members to point to its buffer.
868</blockquote>
869<h3>LWS_EXT_CALLBACK_PACKET_TX_PRESEND</h3>
870<blockquote>
871this works the same way as
872LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the
873extension a chance to change websocket data just before it will
874be sent out. Using the same lws_token pointer scheme in <tt><b>in</b></tt>,
875the extension can change the buffer and the length to be
876transmitted how it likes. Again if it wants to grow the
877buffer safely, it should copy the data into its own buffer and
878set the lws_tokens token pointer to it.
879</blockquote>
880<hr>
Andy Green4f3943a2010-11-12 10:44:16 +0000881<h2>struct libwebsocket_protocols - List of protocols and handlers server supports.</h2>
882<b>struct libwebsocket_protocols</b> {<br>
883&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
David Brooks2c60d952012-04-20 12:19:01 +0800884&nbsp; &nbsp; <i>callback_function *</i> <b>callback</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000885&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenb45993c2010-12-18 15:13:50 +0000886&nbsp; &nbsp; <i>struct libwebsocket_context *</i> <b>owning_server</b>;<br>
887&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_port</b>;<br>
888&nbsp; &nbsp; <i>int</i> <b>broadcast_socket_user_fd</b>;<br>
889&nbsp; &nbsp; <i>int</i> <b>protocol_index</b>;<br>
Andy Green4f3943a2010-11-12 10:44:16 +0000890};<br>
891<h3>Members</h3>
892<dl>
893<dt><b>name</b>
894<dd>Protocol name that must match the one given in the client
895Javascript new WebSocket(url, 'protocol') name
896<dt><b>callback</b>
897<dd>The service callback used for this protocol. It allows the
898service action for an entire protocol to be encapsulated in
899the protocol-specific callback
900<dt><b>per_session_data_size</b>
901<dd>Each new connection using this protocol gets
902this much memory allocated on connection establishment and
903freed on connection takedown. A pointer to this per-connection
904allocation is passed into the callback in the 'user' parameter
Andy Greenb45993c2010-12-18 15:13:50 +0000905<dt><b>owning_server</b>
906<dd>the server init call fills in this opaque pointer when
907registering this protocol with the server.
908<dt><b>broadcast_socket_port</b>
909<dd>the server init call fills this in with the
910localhost port number used to forward broadcasts for this
911protocol
912<dt><b>broadcast_socket_user_fd</b>
913<dd>the server init call fills this in ... the <b>main</b>
914process context can write to this socket to perform broadcasts
915(use the <b>libwebsockets_broadcast</b> api to do this instead,
916it works from any process context)
917<dt><b>protocol_index</b>
918<dd>which protocol we are starting from zero
Andy Green4f3943a2010-11-12 10:44:16 +0000919</dl>
920<h3>Description</h3>
921<blockquote>
922This structure represents one protocol supported by the server. An
923array of these structures is passed to <b>libwebsocket_create_server</b>
924allows as many protocols as you like to be handled by one server.
925</blockquote>
926<hr>
Andy Greend6e09112011-03-05 16:12:15 +0000927<h2>struct libwebsocket_extension - An extension we know how to cope with</h2>
928<b>struct libwebsocket_extension</b> {<br>
929&nbsp; &nbsp; <i>const char *</i> <b>name</b>;<br>
David Brooks2c60d952012-04-20 12:19:01 +0800930&nbsp; &nbsp; <i>extension_callback_function *</i> <b>callback</b>;<br>
Andy Greend6e09112011-03-05 16:12:15 +0000931&nbsp; &nbsp; <i>size_t</i> <b>per_session_data_size</b>;<br>
Andy Greenaa6fc442012-04-12 13:26:49 +0800932&nbsp; &nbsp; <i>void *</i> <b>per_context_private_data</b>;<br>
Andy Greend6e09112011-03-05 16:12:15 +0000933};<br>
934<h3>Members</h3>
935<dl>
936<dt><b>name</b>
937<dd>Formal extension name, eg, "deflate-stream"
938<dt><b>callback</b>
939<dd>Service callback
940<dt><b>per_session_data_size</b>
941<dd>Libwebsockets will auto-malloc this much
942memory for the use of the extension, a pointer
943to it comes in the <tt><b>user</b></tt> callback parameter
Andy Greenaa6fc442012-04-12 13:26:49 +0800944<dt><b>per_context_private_data</b>
945<dd>Optional storage for this externsion that
946is per-context, so it can track stuff across
947all sessions, etc, if it wants
Andy Greend6e09112011-03-05 16:12:15 +0000948</dl>
949<hr>