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