mbed3 warning cleaning
[platform/upstream/libwebsockets.git] / lib / output.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2014 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
24 static int
25 libwebsocket_0405_frame_mask_generate(struct libwebsocket *wsi)
26 {
27         int n;
28
29         /* fetch the per-frame nonce */
30
31         n = libwebsockets_get_random(wsi->protocol->owning_server,
32                                            wsi->u.ws.frame_masking_nonce_04, 4);
33         if (n != 4) {
34                 lwsl_parser("Unable to read from random device %s %d\n",
35                                                      SYSTEM_RANDOM_FILEPATH, n);
36                 return 1;
37         }
38
39         /* start masking from first byte of masking key buffer */
40         wsi->u.ws.frame_mask_index = 0;
41
42         return 0;
43 }
44
45 #ifdef _DEBUG
46
47 LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
48 {
49         int n;
50         int m;
51         int start;
52         unsigned char *buf = (unsigned char *)vbuf;
53         char line[80];
54         char *p;
55
56         lwsl_parser("\n");
57
58         for (n = 0; n < len;) {
59                 start = n;
60                 p = line;
61
62                 p += sprintf(p, "%04X: ", start);
63
64                 for (m = 0; m < 16 && n < len; m++)
65                         p += sprintf(p, "%02X ", buf[n++]);
66                 while (m++ < 16)
67                         p += sprintf(p, "   ");
68
69                 p += sprintf(p, "   ");
70
71                 for (m = 0; m < 16 && (start + m) < len; m++) {
72                         if (buf[start + m] >= ' ' && buf[start + m] < 127)
73                                 *p++ = buf[start + m];
74                         else
75                                 *p++ = '.';
76                 }
77                 while (m++ < 16)
78                         *p++ = ' ';
79
80                 *p++ = '\n';
81                 *p = '\0';
82                 lwsl_debug("%s", line);
83         }
84         lwsl_debug("\n");
85 }
86
87 #endif
88
89 /*
90  * notice this returns number of bytes consumed, or -1
91  */
92
93 int lws_issue_raw(struct libwebsocket *wsi, unsigned char *buf, size_t len)
94 {
95         struct libwebsocket_context *context = wsi->protocol->owning_server;
96         int n;
97         size_t real_len = len;
98         int m;
99         
100         if (!len)
101                 return 0;
102         /* just ignore sends after we cleared the truncation buffer */
103         if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
104                                                 !wsi->truncated_send_len)
105                 return len;
106
107         if (wsi->truncated_send_len && (buf < wsi->truncated_send_malloc ||
108                         buf > (wsi->truncated_send_malloc +
109                                 wsi->truncated_send_len +
110                                 wsi->truncated_send_offset))) {
111                 lwsl_err("****** %x Sending new, pending truncated ...\n", wsi);
112                 assert(0);
113         }
114
115         m = lws_ext_callback_for_each_active(wsi,
116                         LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, &buf, len);
117         if (m < 0)
118                 return -1;
119         if (m) /* handled */ {
120                 n = m;
121                 goto handle_truncated_send;
122         }
123         if (!lws_socket_is_valid(wsi->sock))
124                 lwsl_warn("** error invalid sock but expected to send\n");
125
126         /*
127          * nope, send it on the socket directly
128          */
129         lws_latency_pre(context, wsi);
130         n = lws_ssl_capable_write(wsi, buf, len);
131         lws_latency(context, wsi, "send lws_issue_raw", n, (unsigned int)n == len);
132
133         switch (n) {
134         case LWS_SSL_CAPABLE_ERROR:
135                 /* we're going to close, let close know sends aren't possible */
136                 wsi->socket_is_permanently_unusable = 1;
137                 return -1;
138         case LWS_SSL_CAPABLE_MORE_SERVICE:
139                 /* nothing got sent, not fatal, retry the whole thing later */
140                 n = 0;
141                 break;
142         }
143
144 handle_truncated_send:
145         /*
146          * we were already handling a truncated send?
147          */
148         if (wsi->truncated_send_len) {
149                 lwsl_info("***** %x partial send moved on by %d (vs %d)\n",
150                                                              wsi, n, real_len);
151                 wsi->truncated_send_offset += n;
152                 wsi->truncated_send_len -= n;
153
154                 if (!wsi->truncated_send_len) {
155                         lwsl_info("***** %x partial send completed\n", wsi);
156                         /* done with it, but don't free it */
157                         n = real_len;
158                         if (wsi->state == WSI_STATE_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
159                                 lwsl_info("***** %x signalling to close now\n", wsi);
160                                 return -1; /* retry closing now */
161                         }
162                 }
163                 /* always callback on writeable */
164                 libwebsocket_callback_on_writable(
165                                              wsi->protocol->owning_server, wsi);
166
167                 return n;
168         }
169
170         if ((unsigned int)n == real_len)
171                 /* what we just sent went out cleanly */
172                 return n;
173
174         if (n && wsi->u.ws.clean_buffer)
175                 /*
176                  * This buffer unaffected by extension rewriting.
177                  * It means the user code is expected to deal with
178                  * partial sends.  (lws knows the header was already
179                  * sent, so on next send will just resume sending
180                  * payload)
181                  */
182                  return n;
183
184         /*
185          * Newly truncated send.  Buffer the remainder (it will get
186          * first priority next time the socket is writable)
187          */
188         lwsl_info("***** %x new partial sent %d from %d total\n",
189                                                       wsi, n, real_len);
190
191         /*
192          *  - if we still have a suitable malloc lying around, use it
193          *  - or, if too small, reallocate it
194          *  - or, if no buffer, create it
195          */
196         if (!wsi->truncated_send_malloc ||
197                         real_len - n > wsi->truncated_send_allocation) {
198                 lws_free(wsi->truncated_send_malloc);
199
200                 wsi->truncated_send_allocation = real_len - n;
201                 wsi->truncated_send_malloc = lws_malloc(real_len - n);
202                 if (!wsi->truncated_send_malloc) {
203                         lwsl_err("truncated send: unable to malloc %d\n",
204                                                           real_len - n);
205                         return -1;
206                 }
207         }
208         wsi->truncated_send_offset = 0;
209         wsi->truncated_send_len = real_len - n;
210         memcpy(wsi->truncated_send_malloc, buf + n, real_len - n);
211
212         /* since something buffered, force it to get another chance to send */
213         libwebsocket_callback_on_writable(wsi->protocol->owning_server, wsi);
214
215         return real_len;
216 }
217
218 /**
219  * libwebsocket_write() - Apply protocol then write data to client
220  * @wsi:        Websocket instance (available from user callback)
221  * @buf:        The data to send.  For data being sent on a websocket
222  *              connection (ie, not default http), this buffer MUST have
223  *              LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE the pointer
224  *              and an additional LWS_SEND_BUFFER_POST_PADDING bytes valid
225  *              in the buffer after (buf + len).  This is so the protocol
226  *              header and trailer data can be added in-situ.
227  * @len:        Count of the data bytes in the payload starting from buf
228  * @protocol:   Use LWS_WRITE_HTTP to reply to an http connection, and one
229  *              of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate
230  *              data on a websockets connection.  Remember to allow the extra
231  *              bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT
232  *              are used.
233  *
234  *      This function provides the way to issue data back to the client
235  *      for both http and websocket protocols.
236  *
237  *      In the case of sending using websocket protocol, be sure to allocate
238  *      valid storage before and after buf as explained above.  This scheme
239  *      allows maximum efficiency of sending data and protocol in a single
240  *      packet while not burdening the user code with any protocol knowledge.
241  *
242  *      Return may be -1 for a fatal error needing connection close, or a
243  *      positive number reflecting the amount of bytes actually sent.  This
244  *      can be less than the requested number of bytes due to OS memory
245  *      pressure at any given time.
246  */
247
248 LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
249                           size_t len, enum libwebsocket_write_protocol protocol)
250 {
251         int n;
252         int pre = 0;
253         int post = 0;
254         int masked7 = wsi->mode == LWS_CONNMODE_WS_CLIENT;
255         unsigned char *dropmask = NULL;
256         unsigned char is_masked_bit = 0;
257         size_t orig_len = len;
258         struct lws_tokens eff_buf;
259
260         if (len == 0 && protocol != LWS_WRITE_CLOSE &&
261                      protocol != LWS_WRITE_PING && protocol != LWS_WRITE_PONG) {
262                 lwsl_warn("zero length libwebsocket_write attempt\n");
263                 return 0;
264         }
265
266         if (protocol == LWS_WRITE_HTTP ||
267             protocol == LWS_WRITE_HTTP_FINAL ||
268             protocol == LWS_WRITE_HTTP_HEADERS)
269                 goto send_raw;
270
271         /* websocket protocol, either binary or text */
272
273         if (wsi->state != WSI_STATE_ESTABLISHED &&
274             !(wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY &&
275               protocol == LWS_WRITE_CLOSE))
276                 return -1;
277
278         /* if we are continuing a frame that already had its header done */
279
280         if (wsi->u.ws.inside_frame)
281                 goto do_more_inside_frame;
282
283         wsi->u.ws.clean_buffer = 1;
284
285         /*
286          * give a chance to the extensions to modify payload
287          * pre-TX mangling is not allowed to truncate
288          */
289         eff_buf.token = (char *)buf;
290         eff_buf.token_len = len;
291
292         switch (protocol) {
293         case LWS_WRITE_PING:
294         case LWS_WRITE_PONG:
295         case LWS_WRITE_CLOSE:
296                 break;
297         default:
298                 if (lws_ext_callback_for_each_active(wsi,
299                                LWS_EXT_CALLBACK_PAYLOAD_TX, &eff_buf, 0) < 0)
300                         return -1;
301         }
302
303         /*
304          * an extension did something we need to keep... for example, if
305          * compression extension, it has already updated its state according
306          * to this being issued
307          */
308         if ((char *)buf != eff_buf.token)
309                 /*
310                  * extension recreated it:
311                  * need to buffer this if not all sent
312                  */
313                 wsi->u.ws.clean_buffer = 0;
314
315         buf = (unsigned char *)eff_buf.token;
316         len = eff_buf.token_len;
317
318         switch (wsi->ietf_spec_revision) {
319         case 13:
320
321                 if (masked7) {
322                         pre += 4;
323                         dropmask = &buf[0 - pre];
324                         is_masked_bit = 0x80;
325                 }
326
327                 switch (protocol & 0xf) {
328                 case LWS_WRITE_TEXT:
329                         n = LWS_WS_OPCODE_07__TEXT_FRAME;
330                         break;
331                 case LWS_WRITE_BINARY:
332                         n = LWS_WS_OPCODE_07__BINARY_FRAME;
333                         break;
334                 case LWS_WRITE_CONTINUATION:
335                         n = LWS_WS_OPCODE_07__CONTINUATION;
336                         break;
337
338                 case LWS_WRITE_CLOSE:
339                         n = LWS_WS_OPCODE_07__CLOSE;
340
341                         /*
342                          * 06+ has a 2-byte status code in network order
343                          * we can do this because we demand post-buf
344                          */
345
346                         if (wsi->u.ws.close_reason) {
347                                 /* reason codes count as data bytes */
348                                 buf -= 2;
349                                 buf[0] = wsi->u.ws.close_reason >> 8;
350                                 buf[1] = wsi->u.ws.close_reason;
351                                 len += 2;
352                         }
353                         break;
354                 case LWS_WRITE_PING:
355                         n = LWS_WS_OPCODE_07__PING;
356                         break;
357                 case LWS_WRITE_PONG:
358                         n = LWS_WS_OPCODE_07__PONG;
359                         break;
360                 default:
361                         lwsl_warn("lws_write: unknown write opc / protocol\n");
362                         return -1;
363                 }
364
365                 if (!(protocol & LWS_WRITE_NO_FIN))
366                         n |= 1 << 7;
367
368                 if (len < 126) {
369                         pre += 2;
370                         buf[-pre] = n;
371                         buf[-pre + 1] = len | is_masked_bit;
372                 } else {
373                         if (len < 65536) {
374                                 pre += 4;
375                                 buf[-pre] = n;
376                                 buf[-pre + 1] = 126 | is_masked_bit;
377                                 buf[-pre + 2] = len >> 8;
378                                 buf[-pre + 3] = len;
379                         } else {
380                                 pre += 10;
381                                 buf[-pre] = n;
382                                 buf[-pre + 1] = 127 | is_masked_bit;
383 #if defined __LP64__
384                                         buf[-pre + 2] = (len >> 56) & 0x7f;
385                                         buf[-pre + 3] = len >> 48;
386                                         buf[-pre + 4] = len >> 40;
387                                         buf[-pre + 5] = len >> 32;
388 #else
389                                         buf[-pre + 2] = 0;
390                                         buf[-pre + 3] = 0;
391                                         buf[-pre + 4] = 0;
392                                         buf[-pre + 5] = 0;
393 #endif
394                                 buf[-pre + 6] = len >> 24;
395                                 buf[-pre + 7] = len >> 16;
396                                 buf[-pre + 8] = len >> 8;
397                                 buf[-pre + 9] = len;
398                         }
399                 }
400                 break;
401         }
402
403 do_more_inside_frame:
404
405         /*
406          * Deal with masking if we are in client -> server direction and
407          * the protocol demands it
408          */
409
410         if (wsi->mode == LWS_CONNMODE_WS_CLIENT) {
411
412                 if (!wsi->u.ws.inside_frame)
413                         if (libwebsocket_0405_frame_mask_generate(wsi)) {
414                                 lwsl_err("frame mask generation failed\n");
415                                 return -1;
416                         }
417
418                 /*
419                  * in v7, just mask the payload
420                  */
421                 if (dropmask) { /* never set if already inside frame */
422                         for (n = 4; n < (int)len + 4; n++)
423                                 dropmask[n] = dropmask[n] ^
424                                 wsi->u.ws.frame_masking_nonce_04[
425                                         (wsi->u.ws.frame_mask_index++) & 3];
426
427                         /* copy the frame nonce into place */
428                         memcpy(dropmask, wsi->u.ws.frame_masking_nonce_04, 4);
429                 }
430         }
431
432 send_raw:
433         switch (protocol) {
434         case LWS_WRITE_CLOSE:
435 /*              lwsl_hexdump(&buf[-pre], len + post); */
436         case LWS_WRITE_HTTP:
437         case LWS_WRITE_HTTP_FINAL:
438         case LWS_WRITE_HTTP_HEADERS:
439         case LWS_WRITE_PONG:
440         case LWS_WRITE_PING:
441 #ifdef LWS_USE_HTTP2
442                 if (wsi->mode == LWS_CONNMODE_HTTP2_SERVING) {
443                         unsigned char flags = 0;
444
445                         n = LWS_HTTP2_FRAME_TYPE_DATA;
446                         if (protocol == LWS_WRITE_HTTP_HEADERS) {
447                                 n = LWS_HTTP2_FRAME_TYPE_HEADERS;
448                                 flags = LWS_HTTP2_FLAG_END_HEADERS;
449                                 if (wsi->u.http2.send_END_STREAM)
450                                         flags |= LWS_HTTP2_FLAG_END_STREAM;
451                         }
452                         
453                         if ((protocol == LWS_WRITE_HTTP || protocol == LWS_WRITE_HTTP_FINAL) && wsi->u.http.content_length) {
454                                 wsi->u.http.content_remain -= len;
455                                 lwsl_info("%s: content_remain = %lu\n", __func__, wsi->u.http.content_remain);
456                                 if (!wsi->u.http.content_remain) {
457                                         lwsl_info("%s: selecting final write mode\n", __func__);
458                                         protocol = LWS_WRITE_HTTP_FINAL;
459                                 }
460                         }
461                         
462                         if (protocol == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
463                                 lwsl_info("%s: setting END_STREAM\n", __func__);
464                                 flags |= LWS_HTTP2_FLAG_END_STREAM;
465                         }
466
467                         return lws_http2_frame_write(wsi, n, flags, wsi->u.http2.my_stream_id, len, buf);
468                 }
469 #endif
470                 return lws_issue_raw(wsi, (unsigned char *)buf - pre,
471                                                               len + pre + post);
472         default:
473                 break;
474         }
475
476         wsi->u.ws.inside_frame = 1;
477
478         /*
479          * give any active extensions a chance to munge the buffer
480          * before send.  We pass in a pointer to an lws_tokens struct
481          * prepared with the default buffer and content length that's in
482          * there.  Rather than rewrite the default buffer, extensions
483          * that expect to grow the buffer can adapt .token to
484          * point to their own per-connection buffer in the extension
485          * user allocation.  By default with no extensions or no
486          * extension callback handling, just the normal input buffer is
487          * used then so it is efficient.
488          *
489          * callback returns 1 in case it wants to spill more buffers
490          *
491          * This takes care of holding the buffer if send is incomplete, ie,
492          * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
493          * the buffer).  If wsi->u.ws.clean_buffer is 1, it will instead
494          * return to the user code how much OF THE USER BUFFER was consumed.
495          */
496
497         n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre + post);
498         if (n <= 0)
499                 return n;
500
501         if (n == (int)len + pre + post) {
502                 /* everything in the buffer was handled (or rebuffered...) */
503                 wsi->u.ws.inside_frame = 0;
504                 return orig_len;
505         }
506
507         /*
508          * it is how many bytes of user buffer got sent... may be < orig_len
509          * in which case callback when writable has already been arranged
510          * and user code can call libwebsocket_write() again with the rest
511          * later.
512          */
513
514         return n - (pre + post);
515 }
516
517 LWS_VISIBLE int libwebsockets_serve_http_file_fragment(
518                 struct libwebsocket_context *context, struct libwebsocket *wsi)
519 {
520         int n;
521         int m;
522
523         while (!lws_send_pipe_choked(wsi)) {
524
525                 if (wsi->truncated_send_len) {
526                         if (lws_issue_raw(wsi, wsi->truncated_send_malloc +
527                                         wsi->truncated_send_offset,
528                                                        wsi->truncated_send_len) < 0) {
529                                 lwsl_info("closing from libwebsockets_serve_http_file_fragment\n");
530                                 return -1;
531                         }
532                         continue;
533                 }
534
535                 if (wsi->u.http.filepos == wsi->u.http.filelen)
536                         goto all_sent;
537
538                 compatible_file_read(n, wsi->u.http.fd, context->service_buffer,
539                                                sizeof(context->service_buffer));
540                 if (n < 0)
541                         return -1; /* caller will close */
542                 if (n) {
543                         libwebsocket_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT, AWAITING_TIMEOUT);
544                         wsi->u.http.filepos += n;
545                         m = libwebsocket_write(wsi, context->service_buffer, n,
546                                                wsi->u.http.filepos == wsi->u.http.filelen ? LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP);
547                         if (m < 0)
548                                 return -1;
549
550                         if (m != n)
551                                 /* adjust for what was not sent */
552                                 if (compatible_file_seek_cur(wsi->u.http.fd, m - n) < 0)
553                                         return -1;
554                 }
555 all_sent:
556                 if (!wsi->truncated_send_len &&
557                                 wsi->u.http.filepos == wsi->u.http.filelen) {
558                         wsi->state = WSI_STATE_HTTP;
559
560                         /* we might be in keepalive, so close it off here */
561                         compatible_file_close(wsi->u.http.fd);
562                         wsi->u.http.fd = LWS_INVALID_FILE;
563
564                         if (wsi->protocol->callback)
565                                 /* ignore callback returned value */
566                                 user_callback_handle_rxflow(
567                                         wsi->protocol->callback, context, wsi,
568                                         LWS_CALLBACK_HTTP_FILE_COMPLETION,
569                                         wsi->user_space, NULL, 0);
570                         return 1;  /* >0 indicates completed */
571                 }
572         }
573
574         lwsl_info("choked before able to send whole file (post)\n");
575         libwebsocket_callback_on_writable(context, wsi);
576
577         return 0; /* indicates further processing must be done */
578 }
579
580 LWS_VISIBLE int
581 lws_ssl_capable_read_no_ssl(struct libwebsocket_context *context,
582                             struct libwebsocket *wsi, unsigned char *buf, int len)
583 {
584         int n;
585
586         (void)context;
587         
588         n = recv(wsi->sock, (char *)buf, len, 0);
589         if (n >= 0)
590                 return n;
591
592         if (LWS_ERRNO == LWS_EAGAIN ||
593             LWS_ERRNO == LWS_EWOULDBLOCK ||
594             LWS_ERRNO == LWS_EINTR)
595                 return LWS_SSL_CAPABLE_MORE_SERVICE;
596     
597         lwsl_warn("error on reading from skt\n");
598         return LWS_SSL_CAPABLE_ERROR;
599 }
600
601 LWS_VISIBLE int
602 lws_ssl_capable_write_no_ssl(struct libwebsocket *wsi, unsigned char *buf, int len)
603 {
604         int n;
605         
606         n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL);
607         if (n >= 0)
608                 return n;
609
610         if (LWS_ERRNO == LWS_EAGAIN ||
611             LWS_ERRNO == LWS_EWOULDBLOCK ||
612             LWS_ERRNO == LWS_EINTR) {
613                 if (LWS_ERRNO == LWS_EWOULDBLOCK)
614                         lws_set_blocking_send(wsi);
615
616                 return LWS_SSL_CAPABLE_MORE_SERVICE;
617         }
618         lwsl_debug("ERROR writing len %d to skt %d\n", len, n);
619         return LWS_SSL_CAPABLE_ERROR;
620 }
621
622 LWS_VISIBLE int
623 lws_ssl_pending_no_ssl(struct libwebsocket *wsi)
624 {
625         (void)wsi;
626         return 0;
627 }