Subject: LWS_UNUSED
[platform/upstream/libwebsockets.git] / lib / extension-permessage-deflate.c
1 /*
2  * ./lib/extension-permessage-deflate.c
3  *
4  *  Copyright (C) 2016 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 #include "private-libwebsockets.h"
23 #include "extension-permessage-deflate.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #define LWS_ZLIB_MEMLEVEL 8
29
30 const struct lws_ext_options lws_ext_pm_deflate_options[] = {
31         /* public RFC7692 settings */
32         { "server_no_context_takeover", EXTARG_NONE },
33         { "client_no_context_takeover", EXTARG_NONE },
34         { "server_max_window_bits",     EXTARG_OPT_DEC },
35         { "client_max_window_bits",     EXTARG_OPT_DEC },
36         /* ones only user code can set */
37         { "rx_buf_size",                EXTARG_DEC },
38         { "tx_buf_size",                EXTARG_DEC },
39         { "compression_level",          EXTARG_DEC },
40         { "mem_level",                  EXTARG_DEC },
41         { NULL, 0 }, /* sentinel */
42 };
43
44 static void
45 lws_extension_pmdeflate_restrict_args(struct lws *wsi,
46                                       struct lws_ext_pm_deflate_priv *priv)
47 {
48         int n, extra;
49
50         /* cap the RX buf at the nearest power of 2 to protocol rx buf */
51
52         n = wsi->context->pt_serv_buf_size;
53         if (wsi->protocol->rx_buffer_size)
54                 n =  wsi->protocol->rx_buffer_size;
55
56         extra = 7;
57         while (n >= 1 << (extra + 1))
58                 extra++;
59
60         if (extra < priv->args[PMD_RX_BUF_PWR2]) {
61                 priv->args[PMD_RX_BUF_PWR2] = extra;
62                 lwsl_err(" Capping pmd rx to %d\n", 1 << extra);
63         }
64 }
65
66 LWS_VISIBLE int
67 lws_extension_callback_pm_deflate(struct lws_context *context,
68                                   const struct lws_extension *ext,
69                                   struct lws *wsi,
70                                   enum lws_extension_callback_reasons reason,
71                                   void *user, void *in, size_t len)
72 {
73         struct lws_ext_pm_deflate_priv *priv =
74                                      (struct lws_ext_pm_deflate_priv *)user;
75         struct lws_tokens *eff_buf = (struct lws_tokens *)in;
76         static unsigned char trail[] = { 0, 0, 0xff, 0xff };
77         int n, ret = 0, was_fin = 0, extra;
78         struct lws_ext_option_arg *oa;
79
80         switch (reason) {
81         case LWS_EXT_CB_NAMED_OPTION_SET:
82                 oa = in;
83                 if (!oa->option_name)
84                         break;
85                 for (n = 0; n < ARRAY_SIZE(lws_ext_pm_deflate_options); n++)
86                         if (!strcmp(lws_ext_pm_deflate_options[n].name, oa->option_name))
87                                 break;
88
89                 if (n == ARRAY_SIZE(lws_ext_pm_deflate_options))
90                         break;
91                 oa->option_index = n;
92
93                 /* fallthru */
94
95         case LWS_EXT_CB_OPTION_SET:
96                 oa = in;
97                 lwsl_info("%s: option set: idx %d, %s, len %d\n", __func__,
98                           oa->option_index, oa->start, oa->len);
99                 if (oa->start)
100                         priv->args[oa->option_index] = atoi(oa->start);
101                 else
102                         priv->args[oa->option_index] = 1;
103
104                 lws_extension_pmdeflate_restrict_args(wsi, priv);
105                 break;
106
107         case LWS_EXT_CB_OPTION_CONFIRM:
108                 if (priv->args[PMD_SERVER_MAX_WINDOW_BITS] < 8 ||
109                     priv->args[PMD_SERVER_MAX_WINDOW_BITS] > 15 ||
110                     priv->args[PMD_CLIENT_MAX_WINDOW_BITS] < 8 ||
111                     priv->args[PMD_CLIENT_MAX_WINDOW_BITS] > 15)
112                         return -1;
113                 break;
114
115         case LWS_EXT_CB_CLIENT_CONSTRUCT:
116         case LWS_EXT_CB_CONSTRUCT:
117
118                 n = context->pt_serv_buf_size;
119                 if (wsi->protocol->rx_buffer_size)
120                         n =  wsi->protocol->rx_buffer_size;
121
122                 if (n < 128) {
123                         lwsl_err(" permessage-deflate requires the protocol (%s) to have an RX buffer >= 128\n",
124                                         wsi->protocol->name);
125                         return -1;
126                 }
127
128                 /* fill in **user */
129                 priv = lws_zalloc(sizeof(*priv));
130                 *((void **)user) = priv;
131                 lwsl_ext("%s: LWS_EXT_CB_*CONSTRUCT\n", __func__);
132                 memset(priv, 0, sizeof(*priv));
133
134                 /* fill in pointer to options list */
135                 if (in)
136                         *((const struct lws_ext_options **)in) =
137                                         lws_ext_pm_deflate_options;
138
139                 /* fallthru */
140
141         case LWS_EXT_CB_OPTION_DEFAULT:
142
143                 /* set the public, RFC7692 defaults... */
144
145                 priv->args[PMD_SERVER_NO_CONTEXT_TAKEOVER] = 0,
146                 priv->args[PMD_CLIENT_NO_CONTEXT_TAKEOVER] = 0;
147                 priv->args[PMD_SERVER_MAX_WINDOW_BITS] = 15;
148                 priv->args[PMD_CLIENT_MAX_WINDOW_BITS] = 15;
149
150                 /* ...and the ones the user code can override */
151
152                 priv->args[PMD_RX_BUF_PWR2] = 10; /* ie, 1024 */
153                 priv->args[PMD_TX_BUF_PWR2] = 10; /* ie, 1024 */
154                 priv->args[PMD_COMP_LEVEL] = 1;
155                 priv->args[PMD_MEM_LEVEL] = 8;
156
157                 lws_extension_pmdeflate_restrict_args(wsi, priv);
158                 break;
159
160         case LWS_EXT_CB_DESTROY:
161                 lwsl_ext("%s: LWS_EXT_CB_DESTROY\n", __func__);
162                 lws_free(priv->buf_rx_inflated);
163                 lws_free(priv->buf_tx_deflated);
164                 if (priv->rx_init)
165                         (void)inflateEnd(&priv->rx);
166                 if (priv->tx_init)
167                         (void)deflateEnd(&priv->tx);
168                 lws_free(priv);
169                 return ret;
170
171         case LWS_EXT_CB_PAYLOAD_RX:
172                 lwsl_ext(" %s: LWS_EXT_CB_PAYLOAD_RX: in %d, existing in %d\n",
173                          __func__, eff_buf->token_len, priv->rx.avail_in);
174                 if (!(wsi->u.ws.rsv_first_msg & 0x40))
175                         return 0;
176
177 #if 0
178                 for (n = 0; n < eff_buf->token_len; n++) {
179                         printf("%02X ", (unsigned char)eff_buf->token[n]);
180                         if ((n & 15) == 15)
181                                 printf("\n");
182                 }
183                 printf("\n");
184 #endif
185                 if (!priv->rx_init)
186                         if (inflateInit2(&priv->rx, -priv->args[PMD_SERVER_MAX_WINDOW_BITS]) != Z_OK) {
187                                 lwsl_err("%s: iniflateInit failed\n", __func__);
188                                 return -1;
189                         }
190                 priv->rx_init = 1;
191                 if (!priv->buf_rx_inflated)
192                         priv->buf_rx_inflated = lws_malloc(LWS_PRE + 7 + 5 +
193                                             (1 << priv->args[PMD_RX_BUF_PWR2]));
194                 if (!priv->buf_rx_inflated) {
195                         lwsl_err("%s: OOM\n", __func__);
196                         return -1;
197                 }
198
199                 /*
200                  * We have to leave the input stream alone if we didn't
201                  * finish with it yet.  The input stream is held in the wsi
202                  * rx buffer by the caller, so this assumption is safe while
203                  * we block new rx while draining the existing rx
204                  */
205                 if (!priv->rx.avail_in && eff_buf->token && eff_buf->token_len) {
206                         priv->rx.next_in = (unsigned char *)eff_buf->token;
207                         priv->rx.avail_in = eff_buf->token_len;
208                 }
209                 priv->rx.next_out = priv->buf_rx_inflated + LWS_PRE;
210                 eff_buf->token = (char *)priv->rx.next_out;
211                 priv->rx.avail_out = 1 << priv->args[PMD_RX_BUF_PWR2];
212
213                 if (priv->rx_held_valid) {
214                         lwsl_ext("-- RX piling on held byte --\n");
215                         *(priv->rx.next_out++) = priv->rx_held;
216                         priv->rx.avail_out--;
217                         priv->rx_held_valid = 0;
218                 }
219
220                 /* if...
221                  *
222                  *  - he has no remaining input content for this message, and
223                  *  - and this is the final fragment, and
224                  *  - we used everything that could be drained on the input side
225                  *
226                  * ...then put back the 00 00 FF FF the sender stripped as our
227                  * input to zlib
228                  */
229                 if (!priv->rx.avail_in && wsi->u.ws.final &&
230                     !wsi->u.ws.rx_packet_length) {
231                         lwsl_ext("RX APPEND_TRAILER-DO\n");
232                         was_fin = 1;
233                         priv->rx.next_in = trail;
234                         priv->rx.avail_in = sizeof(trail);
235                 }
236
237                 n = inflate(&priv->rx, Z_NO_FLUSH);
238                 lwsl_ext("inflate ret %d, avi %d, avo %d, wsifinal %d\n", n,
239                          priv->rx.avail_in, priv->rx.avail_out, wsi->u.ws.final);
240                 switch (n) {
241                 case Z_NEED_DICT:
242                 case Z_STREAM_ERROR:
243                 case Z_DATA_ERROR:
244                 case Z_MEM_ERROR:
245                         lwsl_info("zlib error inflate %d: %s\n",
246                                   n, priv->rx.msg);
247                         return -1;
248                 }
249                 /*
250                  * If we did not already send in the 00 00 FF FF, and he's
251                  * out of input, he did not EXACTLY fill the output buffer
252                  * (which is ambiguous and we will force it to go around
253                  * again by withholding a byte), and he's otherwise working on
254                  * being a FIN fragment, then do the FIN message processing
255                  * of faking up the 00 00 FF FF that the sender stripped.
256                  */
257                 if (!priv->rx.avail_in && wsi->u.ws.final &&
258                     !wsi->u.ws.rx_packet_length && !was_fin &&
259                     priv->rx.avail_out /* ambiguous as to if it is the end */
260                 ) {
261                         lwsl_ext("RX APPEND_TRAILER-DO\n");
262                         was_fin = 1;
263                         priv->rx.next_in = trail;
264                         priv->rx.avail_in = sizeof(trail);
265                         n = inflate(&priv->rx, Z_SYNC_FLUSH);
266                         lwsl_ext("RX trailer inf returned %d, avi %d, avo %d\n", n,
267                                  priv->rx.avail_in, priv->rx.avail_out);
268                         switch (n) {
269                         case Z_NEED_DICT:
270                         case Z_STREAM_ERROR:
271                         case Z_DATA_ERROR:
272                         case Z_MEM_ERROR:
273                                 lwsl_info("zlib error inflate %d: %s\n",
274                                           n, priv->rx.msg);
275                                 return -1;
276                         }
277                 }
278                 /*
279                  * we must announce in our returncode now if there is more
280                  * output to be expected from inflate, so we can decide to
281                  * set the FIN bit on this bufferload or not.  However zlib
282                  * is ambiguous when we exactly filled the inflate buffer.  It
283                  * does not give us a clue as to whether we should understand
284                  * that to mean he ended on a buffer boundary, or if there is
285                  * more in the pipeline.
286                  *
287                  * So to work around that safely, if it used all output space
288                  * exactly, we ALWAYS say there is more coming and we withhold
289                  * the last byte of the buffer to guarantee that is true.
290                  *
291                  * That still leaves us at least one byte to finish with a FIN
292                  * on, even if actually nothing more is coming from the next
293                  * inflate action itself.
294                  */
295                 if (!priv->rx.avail_out) { /* he used all available out buf */
296                         lwsl_ext("-- rx grabbing held --\n");
297                         /* snip the last byte and hold it for next time */
298                         priv->rx_held = *(--priv->rx.next_out);
299                         priv->rx_held_valid = 1;
300                 }
301
302                 eff_buf->token_len = (char *)priv->rx.next_out - eff_buf->token;
303                 priv->count_rx_between_fin += eff_buf->token_len;
304
305                 lwsl_ext("  %s: RX leaving with new effbuff len %d, "
306                          "ret %d, rx.avail_in=%d, TOTAL RX since FIN %lu\n",
307                          __func__, eff_buf->token_len, priv->rx_held_valid,
308                          priv->rx.avail_in,
309                          (unsigned long)priv->count_rx_between_fin);
310
311                 if (was_fin) {
312                         priv->count_rx_between_fin = 0;
313                         if (priv->args[PMD_SERVER_NO_CONTEXT_TAKEOVER]) {
314                                 (void)inflateEnd(&priv->rx);
315                                 priv->rx_init = 0;
316                         }
317                 }
318 #if 0
319                 for (n = 0; n < eff_buf->token_len; n++)
320                         putchar(eff_buf->token[n]);
321                 puts("\n");
322 #endif
323
324                 return priv->rx_held_valid;
325
326         case LWS_EXT_CB_PAYLOAD_TX:
327
328                 if (!priv->tx_init)
329                         if (deflateInit2(&priv->tx, priv->args[PMD_COMP_LEVEL],
330                                          Z_DEFLATED,
331                                          -priv->args[PMD_CLIENT_MAX_WINDOW_BITS +
332                                                      !wsi->vhost->listen_port],
333                                          priv->args[PMD_MEM_LEVEL],
334                                          Z_DEFAULT_STRATEGY) != Z_OK) {
335                                 lwsl_ext("inflateInit2 failed\n");
336                                 return 1;
337                         }
338                 priv->tx_init = 1;
339                 if (!priv->buf_tx_deflated)
340                         priv->buf_tx_deflated = lws_malloc(LWS_PRE + 7 + 5 +
341                                             (1 << priv->args[PMD_TX_BUF_PWR2]));
342                 if (!priv->buf_tx_deflated) {
343                         lwsl_err("%s: OOM\n", __func__);
344                         return -1;
345                 }
346
347                 if (eff_buf->token) {
348                         lwsl_ext("%s: TX: eff_buf length %d\n", __func__,
349                                  eff_buf->token_len);
350                         priv->tx.next_in = (unsigned char *)eff_buf->token;
351                         priv->tx.avail_in = eff_buf->token_len;
352                 }
353
354 #if 0
355                 for (n = 0; n < eff_buf->token_len; n++) {
356                         printf("%02X ", (unsigned char)eff_buf->token[n]);
357                         if ((n & 15) == 15)
358                                 printf("\n");
359                 }
360                 printf("\n");
361 #endif
362
363                 priv->tx.next_out = priv->buf_tx_deflated + LWS_PRE + 5;
364                 eff_buf->token = (char *)priv->tx.next_out;
365                 priv->tx.avail_out = 1 << priv->args[PMD_TX_BUF_PWR2];
366
367                 n = deflate(&priv->tx, Z_SYNC_FLUSH);
368                 if (n == Z_STREAM_ERROR) {
369                         lwsl_ext("%s: Z_STREAM_ERROR\n", __func__);
370                         return -1;
371                 }
372
373                 if (priv->tx_held_valid) {
374                         priv->tx_held_valid = 0;
375                         if (priv->tx.avail_out == 1 << priv->args[PMD_TX_BUF_PWR2])
376                                 /*
377                                  * we can get a situation he took something in
378                                  * but did not generate anything out, at the end
379                                  * of a message (eg, next thing he sends is 80
380                                  * 00, a zero length FIN, like Authobahn can
381                                  * send).
382                                  * If we have come back as a FIN, we must not
383                                  * place the pending trailer 00 00 FF FF, just
384                                  * the 1 byte of live data
385                                  */
386                                 *(--eff_buf->token) = priv->tx_held[0];
387                         else {
388                                 /* he generated data, prepend whole pending */
389                                 eff_buf->token -= 5;
390                                 for (n = 0; n < 5; n++)
391                                         eff_buf->token[n] = priv->tx_held[n];
392
393                         }
394                 }
395                 priv->compressed_out = 1;
396                 eff_buf->token_len = (int)(priv->tx.next_out -
397                                            (unsigned char *)eff_buf->token);
398
399                 /*
400                  * we must announce in our returncode now if there is more
401                  * output to be expected from inflate, so we can decide to
402                  * set the FIN bit on this bufferload or not.  However zlib
403                  * is ambiguous when we exactly filled the inflate buffer.  It
404                  * does not give us a clue as to whether we should understand
405                  * that to mean he ended on a buffer boundary, or if there is
406                  * more in the pipeline.
407                  *
408                  * Worse, the guy providing the stuff we are sending may not
409                  * know until after that this was, actually, the last chunk,
410                  * that can happen even if we did not fill the output buf, ie
411                  * he may send after this a zero-length FIN fragment.
412                  *
413                  * This is super difficult because we must snip the last 4
414                  * bytes in the case this is the last compressed output of the
415                  * message.  The only way to deal with it is defer sending the
416                  * last 5 bytes of each frame until the next one, when we will
417                  * be in a position to understand if that has a FIN or not.
418                  */
419
420                 extra = !!(len & LWS_WRITE_NO_FIN) || !priv->tx.avail_out;
421
422                 if (eff_buf->token_len >= 4 + extra) {
423                         lwsl_ext("tx held %d\n", 4 + extra);
424                         priv->tx_held_valid = extra;
425                         for (n = 3 + extra; n >= 0; n--)
426                                 priv->tx_held[n] = *(--priv->tx.next_out);
427                         eff_buf->token_len -= 4 + extra;
428                 }
429                 lwsl_ext("  TX rewritten with new effbuff len %d, ret %d\n",
430                          eff_buf->token_len, !priv->tx.avail_out);
431
432                 return !priv->tx.avail_out; /* 1 == have more tx pending */
433
434         case LWS_EXT_CB_PACKET_TX_PRESEND:
435                 if (!priv->compressed_out)
436                         break;
437                 priv->compressed_out = 0;
438
439                 if ((*(eff_buf->token) & 0x80) && priv->args[PMD_CLIENT_NO_CONTEXT_TAKEOVER]) {
440                         (void)deflateEnd(&priv->tx);
441                         priv->tx_init = 0;
442                 }
443
444                 n = *(eff_buf->token) & 15;
445                 /* set RSV1, but not on CONTINUATION */
446                 if (n == LWSWSOPC_TEXT_FRAME || n == LWSWSOPC_BINARY_FRAME)
447                         *eff_buf->token |= 0x40;
448 #if 0
449                 for (n = 0; n < eff_buf->token_len; n++) {
450                         printf("%02X ", (unsigned char)eff_buf->token[n]);
451                         if ((n & 15) == 15)
452                                 puts("\n");
453                 }
454                 puts("\n");
455 #endif
456                 lwsl_ext("%s: tx opcode 0x%02X\n", __func__,
457                          (unsigned char)*eff_buf->token);
458                 break;
459
460         default:
461                 break;
462         }
463
464         return 0;
465 }
466