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