acb27b1342b4ee7ebb93e704df097e39d7360861
[profile/ivi/libwebsockets.git] / lib / libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #ifndef __LIBWEBSOCKET_H__
23 #define __LIBWEBSOCKET_H__
24
25 #include <poll.h>
26
27 #define CONTEXT_PORT_NO_LISTEN 0
28
29
30 enum libwebsocket_context_options {
31         LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK = 1,
32 };
33
34 enum libwebsocket_callback_reasons {
35         LWS_CALLBACK_ESTABLISHED,
36         LWS_CALLBACK_CLIENT_ESTABLISHED,
37         LWS_CALLBACK_CLOSED,
38         LWS_CALLBACK_RECEIVE,
39         LWS_CALLBACK_CLIENT_RECEIVE,
40         LWS_CALLBACK_CLIENT_RECEIVE_PONG,
41         LWS_CALLBACK_CLIENT_WRITEABLE,
42         LWS_CALLBACK_HTTP,
43         LWS_CALLBACK_BROADCAST,
44         LWS_CALLBACK_FILTER_NETWORK_CONNECTION,
45         LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
46
47         /* external poll() management support */
48         LWS_CALLBACK_ADD_POLL_FD,
49         LWS_CALLBACK_DEL_POLL_FD,
50         LWS_CALLBACK_SET_MODE_POLL_FD,
51         LWS_CALLBACK_CLEAR_MODE_POLL_FD,
52 };
53
54 enum libwebsocket_write_protocol {
55         LWS_WRITE_TEXT,
56         LWS_WRITE_BINARY,
57         LWS_WRITE_HTTP,
58
59         /* special 04+ opcodes */
60
61         LWS_WRITE_CLOSE,
62         LWS_WRITE_PING,
63         LWS_WRITE_PONG,
64
65         /* flags */
66
67         LWS_WRITE_NO_FIN = 0x40,
68         /*
69          * client packet payload goes out on wire unmunged
70          * only useful for security tests since normal servers cannot
71          * decode the content if used
72          */
73         LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
74 };
75
76 /*
77  * you need these to look at headers that have been parsed if using the
78  * LWS_CALLBACK_FILTER_CONNECTION callback.  If a header from the enum
79  * list below is absent, .token = NULL and token_len = 0.  Otherwise .token
80  * points to .token_len chars containing that header content.
81  */
82
83 struct lws_tokens {
84         char *token;
85         int token_len;
86 };
87
88 enum lws_token_indexes {
89         WSI_TOKEN_GET_URI,
90         WSI_TOKEN_HOST,
91         WSI_TOKEN_CONNECTION,
92         WSI_TOKEN_KEY1,
93         WSI_TOKEN_KEY2,
94         WSI_TOKEN_PROTOCOL,
95         WSI_TOKEN_UPGRADE,
96         WSI_TOKEN_ORIGIN,
97         WSI_TOKEN_DRAFT,
98         WSI_TOKEN_CHALLENGE,
99
100         /* new for 04 */
101         WSI_TOKEN_KEY,
102         WSI_TOKEN_VERSION,
103         WSI_TOKEN_SWORIGIN,
104
105         /* new for 05 */
106         WSI_TOKEN_EXTENSIONS,
107
108         /* client receives these */
109         WSI_TOKEN_ACCEPT,
110         WSI_TOKEN_NONCE,
111         WSI_TOKEN_HTTP,
112
113         /* always last real token index*/
114         WSI_TOKEN_COUNT,
115         /* parser state additions */
116         WSI_TOKEN_NAME_PART,
117         WSI_TOKEN_SKIPPING,
118         WSI_TOKEN_SKIPPING_SAW_CR,
119         WSI_PARSING_COMPLETE
120 };
121
122 struct libwebsocket;
123 struct libwebsocket_context;
124
125 /* document the generic callback (it's a fake prototype under this) */
126 /**
127  * callback() - User server actions
128  * @context:    Websockets context
129  * @wsi:        Opaque websocket instance pointer
130  * @reason:     The reason for the call
131  * @user:       Pointer to per-session user data allocated by library
132  * @in:         Pointer used for some callback reasons
133  * @len:        Length set for some callback reasons
134  *
135  *      This callback is the way the user controls what is served.  All the
136  *      protocol detail is hidden and handled by the library.
137  *
138  *      For each connection / session there is user data allocated that is
139  *      pointed to by "user".  You set the size of this user data area when
140  *      the library is initialized with libwebsocket_create_server.
141  *
142  *      You get an opportunity to initialize user data when called back with
143  *      LWS_CALLBACK_ESTABLISHED reason.
144  *
145  *      LWS_CALLBACK_ESTABLISHED:  after the server completes a handshake with
146  *                              an incoming client
147  *
148  *      LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
149  *                              a handshake with the remote server
150  *
151  *      LWS_CALLBACK_CLOSED: when the websocket session ends
152  *
153  *      LWS_CALLBACK_BROADCAST: signal to send to client (you would use
154  *                              libwebsocket_write() taking care about the
155  *                              special buffer requirements
156  *
157  *      LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
158  *                              remote client, it can be found at *in and is
159  *                              len bytes long
160  *
161  *      LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
162  *                              they appear with this callback reason.  PONG
163  *                              packets only exist in 04+ protocol
164  *
165  *      LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
166  *                              client connection, it can be found at *in and
167  *                              is len bytes long
168  *
169  *      LWS_CALLBACK_HTTP: an http request has come from a client that is not
170  *                              asking to upgrade the connection to a websocket
171  *                              one.  This is a chance to serve http content,
172  *                              for example, to send a script to the client
173  *                              which will then open the websockets connection.
174  *                              @in points to the URI path requested and
175  *                              libwebsockets_serve_http_file() makes it very
176  *                              simple to send back a file to the client.
177  *
178  *      LWS_CALLBACK_CLIENT_WRITEABLE:  if you call
179  *              libwebsocket_callback_on_writable() on a connection, you will
180  *              get this callback coming when the connection socket is able to
181  *              accept another write packet without blocking.  If it already
182  *              was able to take another packet without blocking, you'll get
183  *              this callback at the next call to the service loop function.
184  *
185  *      LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to
186  *              the server at network level; the connection is accepted but then
187  *              passed to this callback to decide whether to hang up immediately
188  *              or not, based on the client IP.  @user contains the connection
189  *              socket's descriptor.  Return non-zero to terminate
190  *              the connection before sending or receiving anything.
191  *              Because this happens immediately after the network connection
192  *              from the client, there's no websocket protocol selected yet so
193  *              this callback is issued only to protocol 0.
194  *
195  *      LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has
196  *              been received and parsed from the client, but the response is
197  *              not sent yet.  Return non-zero to disallow the connection.
198  *              @user is a pointer to an array of struct lws_tokens, you can
199  *              use the header enums lws_token_indexes from libwebsockets.h
200  *              to check for and read the supported header presence and
201  *              content before deciding to allow the handshake to proceed or
202  *              to kill the connection.
203  *
204  *
205  *      The next four reasons are optional and only need taking care of if you
206  *      will be integrating libwebsockets sockets into an external polling
207  *      array.
208  * 
209  *      LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop
210  *              internally, but in the case you are integrating with another
211  *              server you will need to have libwebsocket sockets share a
212  *              polling array with the other server.  This and the other
213  *              POLL_FD related callbacks let you put your specialized
214  *              poll array interface code in the callback for protocol 0, the
215  *              first protocol you support, usually the HTTP protocol in the
216  *              serving case.  This callback happens when a socket needs to be
217  *              added to the polling loop: @user contains the fd, and
218  *              @len is the events bitmap (like, POLLIN).  If you are using the
219  *              internal polling loop (the "service" callback), you can just
220  *              ignore these callbacks.
221  *
222  *      LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor
223  *              needs to be removed from an external polling array.  @user is
224  *              the socket desricptor.  If you are using the internal polling
225  *              loop, you can just ignore it.
226  *
227  *      LWS_CALLBACK_SET_MODE_POLL_FD: This callback happens when libwebsockets
228  *              wants to modify the events for the socket descriptor in @user.
229  *              The handler should OR @len on to the events member of the pollfd
230  *              struct for this socket descriptor.  If you are using the
231  *              internal polling loop, you can just ignore it.
232  *
233  *      LWS_CALLBACK_CLEAR_MODE_POLL_FD: This callback occurs when libwebsockets
234  *              wants to modify the events for the socket descriptor in @user.
235  *              The handler should AND ~@len on to the events member of the
236  *              pollfd struct for this socket descriptor.  If you are using the
237  *              internal polling loop, you can just ignore it.
238  */
239 extern int callback(struct libwebsocket_context * context,
240                         struct libwebsocket *wsi,
241                          enum libwebsocket_callback_reasons reason, void *user,
242                                                           void *in, size_t len);
243
244 /**
245  * struct libwebsocket_protocols -      List of protocols and handlers server
246  *                                      supports.
247  * @name:       Protocol name that must match the one given in the client
248  *              Javascript new WebSocket(url, 'protocol') name
249  * @callback:   The service callback used for this protocol.  It allows the
250  *              service action for an entire protocol to be encapsulated in
251  *              the protocol-specific callback
252  * @per_session_data_size:      Each new connection using this protocol gets
253  *              this much memory allocated on connection establishment and
254  *              freed on connection takedown.  A pointer to this per-connection
255  *              allocation is passed into the callback in the 'user' parameter
256  * @owning_server:      the server init call fills in this opaque pointer when
257  *              registering this protocol with the server.
258  * @broadcast_socket_port: the server init call fills this in with the
259  *              localhost port number used to forward broadcasts for this
260  *              protocol
261  * @broadcast_socket_user_fd:  the server init call fills this in ... the main()
262  *              process context can write to this socket to perform broadcasts
263  *              (use the libwebsockets_broadcast() api to do this instead,
264  *              it works from any process context)
265  * @protocol_index: which protocol we are starting from zero
266  *
267  *      This structure represents one protocol supported by the server.  An
268  *      array of these structures is passed to libwebsocket_create_server()
269  *      allows as many protocols as you like to be handled by one server.
270  */
271
272 struct libwebsocket_protocols {
273         const char *name;
274         int (*callback)(struct libwebsocket_context * context,
275                         struct libwebsocket *wsi,
276                         enum libwebsocket_callback_reasons reason, void *user,
277                                                           void *in, size_t len);
278         size_t per_session_data_size;
279
280         /*
281          * below are filled in on server init and can be left uninitialized,
282          * no need for user to use them directly either
283          */
284
285         struct libwebsocket_context *owning_server;
286         int broadcast_socket_port;
287         int broadcast_socket_user_fd;
288         int protocol_index;
289 };
290
291 extern struct libwebsocket_context *
292 libwebsocket_create_context(int port, const char * interface,
293                   struct libwebsocket_protocols *protocols,
294                   const char *ssl_cert_filepath,
295                   const char *ssl_private_key_filepath, int gid, int uid,
296                   unsigned int options);
297
298 extern void
299 libwebsocket_context_destroy(struct libwebsocket_context *context);
300
301 extern int
302 libwebsockets_fork_service_loop(struct libwebsocket_context *context);
303
304 extern int
305 libwebsocket_service(struct libwebsocket_context *context, int timeout_ms);
306
307 extern int
308 libwebsocket_service_fd(struct libwebsocket_context *context,
309                                                          struct pollfd *pollfd);
310
311 /*
312  * IMPORTANT NOTICE!
313  *
314  * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY)
315  * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE
316  * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len).
317  *
318  * This allows us to add protocol info before and after the data, and send as
319  * one packet on the network without payload copying, for maximum efficiency.
320  *
321  * So for example you need this kind of code to use libwebsocket_write with a
322  * 128-byte payload
323  *
324  *   char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING];
325  *
326  *   // fill your part of the buffer... for example here it's all zeros
327  *   memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128);
328  *
329  *   libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128);
330  *
331  * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just
332  * use the whole buffer without taking care of the above.
333  */
334
335 /* this is the frame nonce plus two header plus 8 length */
336
337 #define LWS_SEND_BUFFER_PRE_PADDING (4 + 10)
338 #define LWS_SEND_BUFFER_POST_PADDING 1
339
340 extern int
341 libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len,
342                                      enum libwebsocket_write_protocol protocol);
343
344 extern int
345 libwebsockets_serve_http_file(struct libwebsocket *wsi, const char *file,
346                                                      const char *content_type);
347
348 /* notice - you need the pre- and post- padding allocation for buf below */
349
350 extern int
351 libwebsockets_broadcast(const struct libwebsocket_protocols *protocol,
352                                                 unsigned char *buf, size_t len);
353
354 extern const struct libwebsocket_protocols *
355 libwebsockets_get_protocol(struct libwebsocket *wsi);
356
357 extern int
358 libwebsocket_callback_on_writable(struct libwebsocket_context *context,
359                                                       struct libwebsocket *wsi);
360
361 extern int
362 libwebsocket_callback_on_writable_all_protocol(
363                                  const struct libwebsocket_protocols *protocol);
364
365 extern int
366 libwebsocket_get_socket_fd(struct libwebsocket *wsi);
367
368 extern int
369 libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable);
370
371 extern size_t
372 libwebsockets_remaining_packet_payload(struct libwebsocket *wsi);
373
374 extern struct libwebsocket *
375 libwebsocket_client_connect(struct libwebsocket_context *clients,
376                               const char *address,
377                               int port,
378                               int ssl_connection,
379                               const char *path,
380                               const char *host,
381                               const char *origin,
382                               const char *protocol,
383                               int ietf_version_or_minus_one);
384
385 extern const char *
386 libwebsocket_canonical_hostname(struct libwebsocket_context *context);
387
388
389 extern void
390 libwebsockets_get_peer_addresses(int fd, char *name, int name_len,
391                                         char *rip, int rip_len);
392
393 extern void
394 libwebsockets_hangup_on_client(struct libwebsocket_context *context, int fd);
395
396 extern void
397 libwebsocket_close_and_free_session(struct libwebsocket_context *context,
398                                                       struct libwebsocket *wsi);
399
400 #endif