• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #if !defined(_GNU_SOURCE)
26 #define _GNU_SOURCE
27 #endif
28 #include "private-lib-core.h"
29 
30 #include <sys/ioctl.h>
31 
32 #if !defined(LWS_DETECTED_PLAT_IOS)
33 #include <net/route.h>
34 #endif
35 
36 #include <net/if.h>
37 
38 #include <pwd.h>
39 #include <grp.h>
40 
41 #if defined(LWS_WITH_MBEDTLS)
42 #if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
43 #include "mbedtls/net_sockets.h"
44 #else
45 #include "mbedtls/net.h"
46 #endif
47 #endif
48 
49 #include <netinet/ip.h>
50 
51 int
lws_send_pipe_choked(struct lws * wsi)52 lws_send_pipe_choked(struct lws *wsi)
53 {
54 	struct lws_pollfd fds;
55 	struct lws *wsi_eff;
56 
57 #if !defined(LWS_WITHOUT_EXTENSIONS)
58 	if (wsi->ws && wsi->ws->tx_draining_ext)
59 		return 1;
60 #endif
61 
62 #if defined(LWS_WITH_HTTP2)
63 	wsi_eff = lws_get_network_wsi(wsi);
64 #else
65 	wsi_eff = wsi;
66 #endif
67 
68 	/* the fact we checked implies we avoided back-to-back writes */
69 	wsi_eff->could_have_pending = 0;
70 
71 	/* treat the fact we got a truncated send pending as if we're choked */
72 	if (lws_has_buffered_out(wsi_eff)
73 #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
74 	    ||wsi->http.comp_ctx.buflist_comp ||
75 	    wsi->http.comp_ctx.may_have_more
76 #endif
77 	    )
78 		return 1;
79 
80 	fds.fd = wsi_eff->desc.sockfd;
81 	fds.events = POLLOUT;
82 	fds.revents = 0;
83 
84 	if (poll(&fds, 1, 0) != 1)
85 		return 1;
86 
87 	if ((fds.revents & POLLOUT) == 0)
88 		return 1;
89 
90 	/* okay to send another packet without blocking */
91 
92 	return 0;
93 }
94 
95 int
lws_plat_set_nonblocking(lws_sockfd_type fd)96 lws_plat_set_nonblocking(lws_sockfd_type fd)
97 {
98 	return fcntl(fd, F_SETFL, O_NONBLOCK) < 0;
99 }
100 
101 int
lws_plat_set_socket_options(struct lws_vhost * vhost,int fd,int unix_skt)102 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd, int unix_skt)
103 {
104 	int optval = 1;
105 	socklen_t optlen = sizeof(optval);
106 
107 #if defined(__APPLE__) || \
108     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
109     defined(__NetBSD__) || \
110     defined(__OpenBSD__) || \
111     defined(__HAIKU__)
112 	struct protoent *tcp_proto;
113 #endif
114 
115 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
116 
117 	if (!unix_skt && vhost->ka_time) {
118 		/* enable keepalive on this socket */
119 		optval = 1;
120 		if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
121 			       (const void *)&optval, optlen) < 0)
122 			return 1;
123 
124 #if defined(__APPLE__) || \
125     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
126     defined(__NetBSD__) || \
127     defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun) || \
128     defined(__HAIKU__)
129 
130 		/*
131 		 * didn't find a way to set these per-socket, need to
132 		 * tune kernel systemwide values
133 		 */
134 #else
135 		/* set the keepalive conditions we want on it too */
136 
137 #if defined(LWS_HAVE_TCP_USER_TIMEOUT)
138 		optval = 1000 * (vhost->ka_time +
139 				 (vhost->ka_interval * vhost->ka_probes));
140 		if (setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT,
141 			       (const void *)&optval, optlen) < 0)
142 			return 1;
143 #endif
144 		optval = vhost->ka_time;
145 		if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
146 			       (const void *)&optval, optlen) < 0)
147 			return 1;
148 
149 		optval = vhost->ka_interval;
150 		if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
151 			       (const void *)&optval, optlen) < 0)
152 			return 1;
153 
154 		optval = vhost->ka_probes;
155 		if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
156 			       (const void *)&optval, optlen) < 0)
157 			return 1;
158 #endif
159 	}
160 
161 #if defined(SO_BINDTODEVICE)
162 	if (!unix_skt && vhost->bind_iface && vhost->iface) {
163 		lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
164 		if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
165 				(socklen_t)strlen(vhost->iface)) < 0) {
166 			lwsl_warn("Failed to bind to device %s\n", vhost->iface);
167 			return 1;
168 		}
169 	}
170 #endif
171 
172 	/* Disable Nagle */
173 	optval = 1;
174 #if defined (__sun) || defined(__QNX__)
175 	if (!unix_skt && setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
176 		return 1;
177 #elif !defined(__APPLE__) && \
178       !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) &&        \
179       !defined(__NetBSD__) && \
180       !defined(__OpenBSD__) && \
181       !defined(__HAIKU__)
182 	if (!unix_skt && setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
183 		return 1;
184 #else
185 	tcp_proto = getprotobyname("TCP");
186 	if (!unix_skt && setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
187 		return 1;
188 #endif
189 
190 	return lws_plat_set_nonblocking(fd);
191 }
192 
193 static const int ip_opt_lws_flags[] = {
194 	LCCSCF_IP_LOW_LATENCY, LCCSCF_IP_HIGH_THROUGHPUT,
195 	LCCSCF_IP_HIGH_RELIABILITY
196 #if !defined(__OpenBSD__)
197 	, LCCSCF_IP_LOW_COST
198 #endif
199 }, ip_opt_val[] = {
200 	IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY
201 #if !defined(__OpenBSD__) && !defined(__sun)
202 	, IPTOS_MINCOST
203 #endif
204 };
205 #if !defined(LWS_WITH_NO_LOGS)
206 static const char *ip_opt_names[] = {
207 	"LOWDELAY", "THROUGHPUT", "RELIABILITY"
208 #if !defined(__OpenBSD__) && !defined(__sun)
209 	, "MINCOST"
210 #endif
211 };
212 #endif
213 
214 int
lws_plat_set_socket_options_ip(lws_sockfd_type fd,uint8_t pri,int lws_flags)215 lws_plat_set_socket_options_ip(lws_sockfd_type fd, uint8_t pri, int lws_flags)
216 {
217 	int optval = (int)pri, ret = 0, n;
218 	socklen_t optlen = sizeof(optval);
219 #if !defined(LWS_WITH_NO_LOGS)
220 	int en;
221 #endif
222 
223 #if 0
224 #if defined(TCP_FASTOPEN_CONNECT)
225 	optval = 1;
226 	if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval,
227 		       sizeof(optval)))
228 		lwsl_warn("%s: FASTOPEN_CONNECT failed\n", __func__);
229 	optval = (int)pri;
230 #endif
231 #endif
232 
233 #if !defined(__APPLE__) && \
234       !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) &&        \
235       !defined(__NetBSD__) && \
236       !defined(__OpenBSD__) && \
237       !defined(__sun) && \
238       !defined(__HAIKU__) && \
239       !defined(__CYGWIN__) && \
240       !defined(__QNX__)
241 
242 	/* the BSDs don't have SO_PRIORITY */
243 
244 	if (pri) { /* 0 is the default already */
245 		if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY,
246 				(const void *)&optval, optlen) < 0) {
247 #if !defined(LWS_WITH_NO_LOGS)
248 			en = errno;
249 			lwsl_warn("%s: unable to set socket pri %d: errno %d\n",
250 				  __func__, (int)pri, en);
251 #endif
252 			ret = 1;
253 		} else
254 			lwsl_notice("%s: set pri %u\n", __func__, pri);
255 	}
256 #endif
257 
258 	for (n = 0; n < 4; n++) {
259 		if (!(lws_flags & ip_opt_lws_flags[n]))
260 			continue;
261 
262 		optval = (int)ip_opt_val[n];
263 		if (setsockopt(fd, IPPROTO_IP, IP_TOS, (const void *)&optval,
264 			       optlen) < 0) {
265 #if !defined(LWS_WITH_NO_LOGS)
266 			en = errno;
267 			lwsl_warn("%s: unable to set %s: errno %d\n", __func__,
268 				  ip_opt_names[n], en);
269 #endif
270 			ret = 1;
271 		} else
272 			lwsl_notice("%s: set ip flag %s\n", __func__,
273 				    ip_opt_names[n]);
274 	}
275 
276 	return ret;
277 }
278 
279 /* cast a struct sockaddr_in6 * into addr for ipv6 */
280 
281 enum {
282 	IP_SCORE_NONE,
283 	IP_SCORE_NONNATIVE,
284 	IP_SCORE_IPV6_SCOPE_BASE,
285 	/* ipv6 scopes */
286 	IP_SCORE_GLOBAL_NATIVE = 18
287 };
288 
289 int
lws_interface_to_sa(int ipv6,const char * ifname,struct sockaddr_in * addr,size_t addrlen)290 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
291 		    size_t addrlen)
292 {
293 	int rc = LWS_ITOSA_NOT_EXIST;
294 
295 	struct ifaddrs *ifr;
296 	struct ifaddrs *ifc;
297 #if defined(LWS_WITH_IPV6)
298 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
299 	unsigned long sco = IP_SCORE_NONE;
300 	unsigned long ts;
301 	const uint8_t *p;
302 #endif
303 
304 	if (getifaddrs(&ifr)) {
305 		lwsl_err("%s: unable to getifaddrs: errno %d\n", __func__, errno);
306 
307 		return LWS_ITOSA_USABLE;
308 	}
309 	for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
310 		if (!ifc->ifa_addr || !ifc->ifa_name)
311 			continue;
312 
313 		lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
314 			   ifc->ifa_name, ifname,
315 			   ifc->ifa_addr->sa_family, ipv6);
316 
317 		if (strcmp(ifc->ifa_name, ifname))
318 			continue;
319 
320 		switch (ifc->ifa_addr->sa_family) {
321 #if defined(AF_PACKET)
322 		case AF_PACKET:
323 			/* interface exists but is not usable */
324 			if (rc == LWS_ITOSA_NOT_EXIST)
325 				rc = LWS_ITOSA_NOT_USABLE;
326 			continue;
327 #endif
328 
329 		case AF_INET:
330 #if defined(LWS_WITH_IPV6)
331 			if (ipv6) {
332 				/* any existing solution is better than this */
333 				if (sco != IP_SCORE_NONE)
334 					break;
335 				sco = IP_SCORE_NONNATIVE;
336 				rc = LWS_ITOSA_USABLE;
337 				/* map IPv4 to IPv6 */
338 				memset((char *)&addr6->sin6_addr, 0,
339 						sizeof(struct in6_addr));
340 				addr6->sin6_addr.s6_addr[10] = 0xff;
341 				addr6->sin6_addr.s6_addr[11] = 0xff;
342 				memcpy(&addr6->sin6_addr.s6_addr[12],
343 				       &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
344 							sizeof(struct in_addr));
345 				lwsl_debug("%s: uplevelling ipv4 bind to ipv6\n", __func__);
346 				break;
347 			}
348 
349 			sco = IP_SCORE_GLOBAL_NATIVE;
350 #endif
351 			rc = LWS_ITOSA_USABLE;
352 			memcpy(addr, (struct sockaddr_in *)ifc->ifa_addr,
353 						    sizeof(struct sockaddr_in));
354 			break;
355 #if defined(LWS_WITH_IPV6)
356 		case AF_INET6:
357 			p = (const uint8_t *)
358 				&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr;
359 			ts = IP_SCORE_IPV6_SCOPE_BASE;
360 			if (p[0] == 0xff)
361 				ts = (unsigned long)(IP_SCORE_IPV6_SCOPE_BASE + (p[1] & 0xf));
362 
363 			if (sco >= ts)
364 				break;
365 
366 			sco = ts;
367 			rc = LWS_ITOSA_USABLE;
368 
369 			memcpy(&addr6->sin6_addr,
370 			     &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
371 						       sizeof(struct in6_addr));
372 			break;
373 #endif
374 		default:
375 			break;
376 		}
377 	}
378 
379 	freeifaddrs(ifr);
380 
381 	if (rc &&
382 	    !lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))
383 		rc = LWS_ITOSA_USABLE;
384 
385 	return rc;
386 }
387 
388 
389 const char *
lws_plat_inet_ntop(int af,const void * src,char * dst,socklen_t cnt)390 lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
391 {
392 	return inet_ntop(af, src, dst, cnt);
393 }
394 
395 int
lws_plat_inet_pton(int af,const char * src,void * dst)396 lws_plat_inet_pton(int af, const char *src, void *dst)
397 {
398 	return inet_pton(af, src, dst);
399 }
400 
401 int
lws_plat_ifname_to_hwaddr(int fd,const char * ifname,uint8_t * hwaddr,int len)402 lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
403 {
404 #if defined(__linux__)
405 	struct ifreq i;
406 
407 	memset(&i, 0, sizeof(i));
408 	lws_strncpy(i.ifr_name, ifname, sizeof(i.ifr_name));
409 
410 	if (ioctl(fd, SIOCGIFHWADDR, &i) < 0)
411 		return -1;
412 
413 	memcpy(hwaddr, &i.ifr_hwaddr.sa_data, 6);
414 
415 	return 6;
416 #else
417 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
418 
419 	return -1;
420 #endif
421 }
422 
423 int
lws_plat_rawudp_broadcast(uint8_t * p,const uint8_t * canned,size_t canned_len,size_t n,int fd,const char * iface)424 lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
425 			  size_t n, int fd, const char *iface)
426 {
427 #if defined(__linux__)
428 	struct sockaddr_ll sll;
429 	uint16_t *p16 = (uint16_t *)p;
430 	uint32_t ucs = 0;
431 
432 	memcpy(p, canned, canned_len);
433 
434 	p[2] = (uint8_t)(n >> 8);
435 	p[3] = (uint8_t)(n);
436 
437 	while (p16 < (uint16_t *)(p + 20))
438 		ucs += ntohs(*p16++);
439 
440 	ucs += ucs >> 16;
441 	ucs ^= 0xffff;
442 
443 	p[10] = (uint8_t)(ucs >> 8);
444 	p[11] = (uint8_t)(ucs);
445 	p[24] = (uint8_t)((n - 20) >> 8);
446 	p[25] = (uint8_t)((n - 20));
447 
448 	memset(&sll, 0, sizeof(sll));
449 	sll.sll_family = AF_PACKET;
450 	sll.sll_protocol = htons(0x800);
451 	sll.sll_halen = 6;
452 	sll.sll_ifindex = (int)if_nametoindex(iface);
453 	memset(sll.sll_addr, 0xff, 6);
454 
455 	return (int)sendto(fd, p, n, 0, (struct sockaddr *)&sll, sizeof(sll));
456 #else
457 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
458 
459 	return -1;
460 #endif
461 }
462 
463 int
lws_plat_if_up(const char * ifname,int fd,int up)464 lws_plat_if_up(const char *ifname, int fd, int up)
465 {
466 #if defined(__linux__)
467 	struct ifreq ifr;
468 
469 	memset(&ifr, 0, sizeof(ifr));
470 	lws_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
471 
472 	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
473 		lwsl_err("%s: SIOCGIFFLAGS fail\n", __func__);
474 		return 1;
475 	}
476 
477 	if (up)
478 		ifr.ifr_flags |= IFF_UP;
479 	else
480 		ifr.ifr_flags &= ~IFF_UP;
481 
482 	if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
483 		lwsl_err("%s: SIOCSIFFLAGS fail\n", __func__);
484 		return 1;
485 	}
486 
487 	return 0;
488 #else
489 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
490 
491 	return -1;
492 #endif
493 }
494 
495 int
lws_plat_BINDTODEVICE(lws_sockfd_type fd,const char * ifname)496 lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
497 {
498 #if defined(__linux__)
499 	struct ifreq i;
500 
501 	memset(&i, 0, sizeof(i));
502 	i.ifr_addr.sa_family = AF_INET;
503 	lws_strncpy(i.ifr_ifrn.ifrn_name, ifname,
504 		    sizeof(i.ifr_ifrn.ifrn_name));
505 	if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &i, sizeof(i)) < 0) {
506 		lwsl_notice("%s: failed %d\n", __func__, LWS_ERRNO);
507 		return 1;
508 	}
509 
510 	return 0;
511 #else
512 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
513 
514 	return -1;
515 #endif
516 }
517 
518 int
lws_plat_ifconfig(int fd,lws_dhcpc_ifstate_t * is)519 lws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)
520 {
521 #if defined(__linux__)
522 	struct rtentry route;
523 	struct ifreq ifr;
524 
525 	memset(&ifr, 0, sizeof(ifr));
526 	memset(&route, 0, sizeof(route));
527 
528 	lws_strncpy(ifr.ifr_name, is->ifname, IFNAMSIZ);
529 
530 	lws_plat_if_up(is->ifname, fd, 0);
531 
532 	memcpy(&ifr.ifr_addr, &is->sa46[LWSDH_SA46_IP], sizeof(struct sockaddr));
533 	if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) {
534 		lwsl_err("%s: SIOCSIFADDR fail\n", __func__);
535 		return 1;
536 	}
537 
538 	if (is->sa46[LWSDH_SA46_IP].sa4.sin_family == AF_INET) {
539 		struct sockaddr_in sin;
540 
541 		memset(&sin, 0, sizeof(sin));
542 		sin.sin_family = AF_INET;
543 		sin.sin_addr.s_addr = *(uint32_t *)&is->nums[LWSDH_IPV4_SUBNET_MASK];
544 		memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
545 		if (ioctl(fd, SIOCSIFNETMASK, &ifr) < 0) {
546 			lwsl_err("%s: SIOCSIFNETMASK fail\n", __func__);
547 			return 1;
548 		}
549 
550 		lws_plat_if_up(is->ifname, fd, 1);
551 
552 		memcpy(&route.rt_gateway,
553 		       &is->sa46[LWSDH_SA46_IPV4_ROUTER].sa4,
554 		       sizeof(struct sockaddr));
555 
556 		sin.sin_addr.s_addr = 0;
557 		memcpy(&route.rt_dst, &sin, sizeof(struct sockaddr));
558 		memcpy(&route.rt_genmask, &sin, sizeof(struct sockaddr));
559 
560 		route.rt_flags = RTF_UP | RTF_GATEWAY;
561 		route.rt_metric = 100;
562 		route.rt_dev = (char *)is->ifname;
563 
564 		if (ioctl(fd, SIOCADDRT, &route) < 0) {
565 			lwsl_err("%s: SIOCADDRT 0x%x fail: %d\n", __func__,
566 				(unsigned int)htonl(*(uint32_t *)&is->
567 					sa46[LWSDH_SA46_IPV4_ROUTER].
568 						sa4.sin_addr.s_addr), LWS_ERRNO);
569 			return 1;
570 		}
571 	} else
572 		lws_plat_if_up(is->ifname, fd, 1);
573 
574 	return 0;
575 #else
576 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
577 
578 	return -1;
579 #endif
580 }
581 
582 int
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost * vhost)583 lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
584 {
585 	return 0;
586 }
587 
588 #if defined(LWS_WITH_MBEDTLS)
589 int
lws_plat_mbedtls_net_send(void * ctx,const uint8_t * buf,size_t len)590 lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
591 {
592 	int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE(fd);
593 	int ret;
594 
595 	if (fd < 0)
596 		return MBEDTLS_ERR_NET_INVALID_CONTEXT;
597 
598 	ret = (int)write(fd, buf, len);
599 	if (ret >= 0)
600 		return ret;
601 
602 	if (errno == EAGAIN || errno == EWOULDBLOCK)
603 		return MBEDTLS_ERR_SSL_WANT_WRITE;
604 
605 	if (errno == EPIPE || errno == ECONNRESET)
606 		return MBEDTLS_ERR_NET_CONN_RESET;
607 
608 	if( errno == EINTR )
609 		return MBEDTLS_ERR_SSL_WANT_WRITE;
610 
611 	return MBEDTLS_ERR_NET_SEND_FAILED;
612 }
613 
614 int
lws_plat_mbedtls_net_recv(void * ctx,unsigned char * buf,size_t len)615 lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
616 {
617 	int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE(fd);
618 	int ret;
619 
620 	if (fd < 0)
621 		return MBEDTLS_ERR_NET_INVALID_CONTEXT;
622 
623 	ret = (int)read(fd, buf, len);
624 	if (ret >= 0)
625 		return ret;
626 
627 	if (errno == EAGAIN || errno == EWOULDBLOCK)
628 		return MBEDTLS_ERR_SSL_WANT_READ;
629 
630 	if (errno == EPIPE || errno == ECONNRESET)
631 		return MBEDTLS_ERR_NET_CONN_RESET;
632 
633 	if (errno == EINTR)
634 		return MBEDTLS_ERR_SSL_WANT_READ;
635 
636 	return MBEDTLS_ERR_NET_RECV_FAILED;
637 }
638 #endif
639