xref: /aosp_15_r20/external/libwebsockets/lib/plat/unix/unix-sockets.c (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
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 
241 	/* the BSDs don't have SO_PRIORITY */
242 
243 	if (pri) { /* 0 is the default already */
244 		if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY,
245 				(const void *)&optval, optlen) < 0) {
246 #if !defined(LWS_WITH_NO_LOGS)
247 			en = errno;
248 			lwsl_warn("%s: unable to set socket pri %d: errno %d\n",
249 				  __func__, (int)pri, en);
250 #endif
251 			ret = 1;
252 		} else
253 			lwsl_notice("%s: set pri %u\n", __func__, pri);
254 	}
255 #endif
256 
257 	for (n = 0; n < 4; n++) {
258 		if (!(lws_flags & ip_opt_lws_flags[n]))
259 			continue;
260 
261 		optval = (int)ip_opt_val[n];
262 		if (setsockopt(fd, IPPROTO_IP, IP_TOS, (const void *)&optval,
263 			       optlen) < 0) {
264 #if !defined(LWS_WITH_NO_LOGS)
265 			en = errno;
266 			lwsl_warn("%s: unable to set %s: errno %d\n", __func__,
267 				  ip_opt_names[n], en);
268 #endif
269 			ret = 1;
270 		} else
271 			lwsl_notice("%s: set ip flag %s\n", __func__,
272 				    ip_opt_names[n]);
273 	}
274 
275 	return ret;
276 }
277 
278 /* cast a struct sockaddr_in6 * into addr for ipv6 */
279 
280 enum {
281 	IP_SCORE_NONE,
282 	IP_SCORE_NONNATIVE,
283 	IP_SCORE_IPV6_SCOPE_BASE,
284 	/* ipv6 scopes */
285 	IP_SCORE_GLOBAL_NATIVE = 18
286 };
287 
288 int
lws_interface_to_sa(int ipv6,const char * ifname,struct sockaddr_in * addr,size_t addrlen)289 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
290 		    size_t addrlen)
291 {
292 	int rc = LWS_ITOSA_NOT_EXIST;
293 
294 	struct ifaddrs *ifr;
295 	struct ifaddrs *ifc;
296 #if defined(LWS_WITH_IPV6)
297 	struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
298 	unsigned long sco = IP_SCORE_NONE;
299 	unsigned long ts;
300 	const uint8_t *p;
301 #endif
302 
303 	if (getifaddrs(&ifr)) {
304 		lwsl_err("%s: unable to getifaddrs: errno %d\n", __func__, errno);
305 
306 		return LWS_ITOSA_USABLE;
307 	}
308 	for (ifc = ifr; ifc != NULL; ifc = ifc->ifa_next) {
309 		if (!ifc->ifa_addr || !ifc->ifa_name)
310 			continue;
311 
312 		lwsl_debug(" interface %s vs %s (fam %d) ipv6 %d\n",
313 			   ifc->ifa_name, ifname,
314 			   ifc->ifa_addr->sa_family, ipv6);
315 
316 		if (strcmp(ifc->ifa_name, ifname))
317 			continue;
318 
319 		switch (ifc->ifa_addr->sa_family) {
320 #if defined(AF_PACKET)
321 		case AF_PACKET:
322 			/* interface exists but is not usable */
323 			if (rc == LWS_ITOSA_NOT_EXIST)
324 				rc = LWS_ITOSA_NOT_USABLE;
325 			continue;
326 #endif
327 
328 		case AF_INET:
329 #if defined(LWS_WITH_IPV6)
330 			if (ipv6) {
331 				/* any existing solution is better than this */
332 				if (sco != IP_SCORE_NONE)
333 					break;
334 				sco = IP_SCORE_NONNATIVE;
335 				rc = LWS_ITOSA_USABLE;
336 				/* map IPv4 to IPv6 */
337 				memset((char *)&addr6->sin6_addr, 0,
338 						sizeof(struct in6_addr));
339 				addr6->sin6_addr.s6_addr[10] = 0xff;
340 				addr6->sin6_addr.s6_addr[11] = 0xff;
341 				memcpy(&addr6->sin6_addr.s6_addr[12],
342 				       &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
343 							sizeof(struct in_addr));
344 				lwsl_debug("%s: uplevelling ipv4 bind to ipv6\n", __func__);
345 				break;
346 			}
347 
348 			sco = IP_SCORE_GLOBAL_NATIVE;
349 #endif
350 			rc = LWS_ITOSA_USABLE;
351 			memcpy(addr, (struct sockaddr_in *)ifc->ifa_addr,
352 						    sizeof(struct sockaddr_in));
353 			break;
354 #if defined(LWS_WITH_IPV6)
355 		case AF_INET6:
356 			p = (const uint8_t *)
357 				&((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr;
358 			ts = IP_SCORE_IPV6_SCOPE_BASE;
359 			if (p[0] == 0xff)
360 				ts = (unsigned long)(IP_SCORE_IPV6_SCOPE_BASE + (p[1] & 0xf));
361 
362 			if (sco >= ts)
363 				break;
364 
365 			sco = ts;
366 			rc = LWS_ITOSA_USABLE;
367 
368 			memcpy(&addr6->sin6_addr,
369 			     &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
370 						       sizeof(struct in6_addr));
371 			break;
372 #endif
373 		default:
374 			break;
375 		}
376 	}
377 
378 	freeifaddrs(ifr);
379 
380 	if (rc &&
381 	    !lws_sa46_parse_numeric_address(ifname, (lws_sockaddr46 *)addr))
382 		rc = LWS_ITOSA_USABLE;
383 
384 	return rc;
385 }
386 
387 
388 const char *
lws_plat_inet_ntop(int af,const void * src,char * dst,socklen_t cnt)389 lws_plat_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
390 {
391 	return inet_ntop(af, src, dst, cnt);
392 }
393 
394 int
lws_plat_inet_pton(int af,const char * src,void * dst)395 lws_plat_inet_pton(int af, const char *src, void *dst)
396 {
397 	return inet_pton(af, src, dst);
398 }
399 
400 int
lws_plat_ifname_to_hwaddr(int fd,const char * ifname,uint8_t * hwaddr,int len)401 lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len)
402 {
403 #if defined(__linux__)
404 	struct ifreq i;
405 
406 	memset(&i, 0, sizeof(i));
407 	lws_strncpy(i.ifr_name, ifname, sizeof(i.ifr_name));
408 
409 	if (ioctl(fd, SIOCGIFHWADDR, &i) < 0)
410 		return -1;
411 
412 	memcpy(hwaddr, &i.ifr_hwaddr.sa_data, 6);
413 
414 	return 6;
415 #else
416 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
417 
418 	return -1;
419 #endif
420 }
421 
422 int
lws_plat_rawudp_broadcast(uint8_t * p,const uint8_t * canned,size_t canned_len,size_t n,int fd,const char * iface)423 lws_plat_rawudp_broadcast(uint8_t *p, const uint8_t *canned, size_t canned_len,
424 			  size_t n, int fd, const char *iface)
425 {
426 #if defined(__linux__)
427 	struct sockaddr_ll sll;
428 	uint16_t *p16 = (uint16_t *)p;
429 	uint32_t ucs = 0;
430 
431 	memcpy(p, canned, canned_len);
432 
433 	p[2] = (uint8_t)(n >> 8);
434 	p[3] = (uint8_t)(n);
435 
436 	while (p16 < (uint16_t *)(p + 20))
437 		ucs += ntohs(*p16++);
438 
439 	ucs += ucs >> 16;
440 	ucs ^= 0xffff;
441 
442 	p[10] = (uint8_t)(ucs >> 8);
443 	p[11] = (uint8_t)(ucs);
444 	p[24] = (uint8_t)((n - 20) >> 8);
445 	p[25] = (uint8_t)((n - 20));
446 
447 	memset(&sll, 0, sizeof(sll));
448 	sll.sll_family = AF_PACKET;
449 	sll.sll_protocol = htons(0x800);
450 	sll.sll_halen = 6;
451 	sll.sll_ifindex = (int)if_nametoindex(iface);
452 	memset(sll.sll_addr, 0xff, 6);
453 
454 	return (int)sendto(fd, p, n, 0, (struct sockaddr *)&sll, sizeof(sll));
455 #else
456 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
457 
458 	return -1;
459 #endif
460 }
461 
462 int
lws_plat_if_up(const char * ifname,int fd,int up)463 lws_plat_if_up(const char *ifname, int fd, int up)
464 {
465 #if defined(__linux__)
466 	struct ifreq ifr;
467 
468 	memset(&ifr, 0, sizeof(ifr));
469 	lws_strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
470 
471 	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
472 		lwsl_err("%s: SIOCGIFFLAGS fail\n", __func__);
473 		return 1;
474 	}
475 
476 	if (up)
477 		ifr.ifr_flags |= IFF_UP;
478 	else
479 		ifr.ifr_flags &= ~IFF_UP;
480 
481 	if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
482 		lwsl_err("%s: SIOCSIFFLAGS fail\n", __func__);
483 		return 1;
484 	}
485 
486 	return 0;
487 #else
488 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
489 
490 	return -1;
491 #endif
492 }
493 
494 int
lws_plat_BINDTODEVICE(lws_sockfd_type fd,const char * ifname)495 lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname)
496 {
497 #if defined(__linux__)
498 	struct ifreq i;
499 
500 	memset(&i, 0, sizeof(i));
501 	i.ifr_addr.sa_family = AF_INET;
502 	lws_strncpy(i.ifr_ifrn.ifrn_name, ifname,
503 		    sizeof(i.ifr_ifrn.ifrn_name));
504 	if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &i, sizeof(i)) < 0) {
505 		lwsl_notice("%s: failed %d\n", __func__, LWS_ERRNO);
506 		return 1;
507 	}
508 
509 	return 0;
510 #else
511 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
512 
513 	return -1;
514 #endif
515 }
516 
517 int
lws_plat_ifconfig(int fd,lws_dhcpc_ifstate_t * is)518 lws_plat_ifconfig(int fd, lws_dhcpc_ifstate_t *is)
519 {
520 #if defined(__linux__)
521 	struct rtentry route;
522 	struct ifreq ifr;
523 
524 	memset(&ifr, 0, sizeof(ifr));
525 	memset(&route, 0, sizeof(route));
526 
527 	lws_strncpy(ifr.ifr_name, is->ifname, IFNAMSIZ);
528 
529 	lws_plat_if_up(is->ifname, fd, 0);
530 
531 	memcpy(&ifr.ifr_addr, &is->sa46[LWSDH_SA46_IP], sizeof(struct sockaddr));
532 	if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) {
533 		lwsl_err("%s: SIOCSIFADDR fail\n", __func__);
534 		return 1;
535 	}
536 
537 	if (is->sa46[LWSDH_SA46_IP].sa4.sin_family == AF_INET) {
538 		struct sockaddr_in sin;
539 
540 		memset(&sin, 0, sizeof(sin));
541 		sin.sin_family = AF_INET;
542 		sin.sin_addr.s_addr = *(uint32_t *)&is->nums[LWSDH_IPV4_SUBNET_MASK];
543 		memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
544 		if (ioctl(fd, SIOCSIFNETMASK, &ifr) < 0) {
545 			lwsl_err("%s: SIOCSIFNETMASK fail\n", __func__);
546 			return 1;
547 		}
548 
549 		lws_plat_if_up(is->ifname, fd, 1);
550 
551 		memcpy(&route.rt_gateway,
552 		       &is->sa46[LWSDH_SA46_IPV4_ROUTER].sa4,
553 		       sizeof(struct sockaddr));
554 
555 		sin.sin_addr.s_addr = 0;
556 		memcpy(&route.rt_dst, &sin, sizeof(struct sockaddr));
557 		memcpy(&route.rt_genmask, &sin, sizeof(struct sockaddr));
558 
559 		route.rt_flags = RTF_UP | RTF_GATEWAY;
560 		route.rt_metric = 100;
561 		route.rt_dev = (char *)is->ifname;
562 
563 		if (ioctl(fd, SIOCADDRT, &route) < 0) {
564 			lwsl_err("%s: SIOCADDRT 0x%x fail: %d\n", __func__,
565 				(unsigned int)htonl(*(uint32_t *)&is->
566 					sa46[LWSDH_SA46_IPV4_ROUTER].
567 						sa4.sin_addr.s_addr), LWS_ERRNO);
568 			return 1;
569 		}
570 	} else
571 		lws_plat_if_up(is->ifname, fd, 1);
572 
573 	return 0;
574 #else
575 	lwsl_err("%s: UNIMPLEMENTED on this platform\n", __func__);
576 
577 	return -1;
578 #endif
579 }
580 
581 int
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost * vhost)582 lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost)
583 {
584 	return 0;
585 }
586 
587 #if defined(LWS_WITH_MBEDTLS)
588 int
lws_plat_mbedtls_net_send(void * ctx,const uint8_t * buf,size_t len)589 lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
590 {
591 	int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE(fd);
592 	int ret;
593 
594 	if (fd < 0)
595 		return MBEDTLS_ERR_NET_INVALID_CONTEXT;
596 
597 	ret = (int)write(fd, buf, len);
598 	if (ret >= 0)
599 		return ret;
600 
601 	if (errno == EAGAIN || errno == EWOULDBLOCK)
602 		return MBEDTLS_ERR_SSL_WANT_WRITE;
603 
604 	if (errno == EPIPE || errno == ECONNRESET)
605 		return MBEDTLS_ERR_NET_CONN_RESET;
606 
607 	if( errno == EINTR )
608 		return MBEDTLS_ERR_SSL_WANT_WRITE;
609 
610 	return MBEDTLS_ERR_NET_SEND_FAILED;
611 }
612 
613 int
lws_plat_mbedtls_net_recv(void * ctx,unsigned char * buf,size_t len)614 lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
615 {
616 	int fd = ((mbedtls_net_context *) ctx)->MBEDTLS_PRIVATE(fd);
617 	int ret;
618 
619 	if (fd < 0)
620 		return MBEDTLS_ERR_NET_INVALID_CONTEXT;
621 
622 	ret = (int)read(fd, buf, len);
623 	if (ret >= 0)
624 		return ret;
625 
626 	if (errno == EAGAIN || errno == EWOULDBLOCK)
627 		return MBEDTLS_ERR_SSL_WANT_READ;
628 
629 	if (errno == EPIPE || errno == ECONNRESET)
630 		return MBEDTLS_ERR_NET_CONN_RESET;
631 
632 	if (errno == EINTR)
633 		return MBEDTLS_ERR_SSL_WANT_READ;
634 
635 	return MBEDTLS_ERR_NET_RECV_FAILED;
636 }
637 #endif
638