1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

mbedtls: attempt to remove dependency on net_sockets.c

The mbedtls openssl wrapper wants to use exports from mbedtls' net_sockets.c,
but this is only supposed to work on *nix and windows.  Typically people
are using mbedtls on RTOS type platforms and to use it, net_sockets.c
needs some hacking.

Try to avoid that situation by porting the two exports we need into the
lws plat code and call from the wrapper.
This commit is contained in:
Andy Green 2020-03-27 13:24:44 +00:00
parent 2f6e0ed10b
commit be32d0554e
6 changed files with 242 additions and 2 deletions

View file

@ -1408,6 +1408,13 @@ lws_socks5c_handle_state(struct lws *wsi, struct lws_pollfd *pollfd,
int
lws_socks5c_greet(struct lws *wsi, const char **pcce);
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len);
int
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
enum {
LW5CHS_RET_RET0,
LW5CHS_RET_BAIL3,

View file

@ -24,6 +24,14 @@
#include "private-lib-core.h"
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
int
lws_send_pipe_choked(struct lws *wsi)
{
@ -259,3 +267,56 @@ lws_plat_ifconfig_ip(const char *ifname, int fd, uint8_t *ip, uint8_t *mask_ip,
return -1;
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = write(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_WRITE;
if (errno == EPIPE || errno == ECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
if( errno == EINTR )
return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_NET_SEND_FAILED;
}
int
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = (int)read(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_READ;
if (errno == EPIPE || errno == ECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
if (errno == EINTR)
return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
#endif

View file

@ -24,6 +24,13 @@
#include "private-lib-core.h"
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
int
lws_plat_pipe_create(struct lws *wsi)
@ -247,4 +254,55 @@ lws_plat_inet_pton(int af, const char *src, void *dst)
return 1;
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = write(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_WRITE;
if (errno == EPIPE || errno == ECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
if( errno == EINTR )
return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_NET_SEND_FAILED;
}
int
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = (int)read(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_READ;
if (errno == EPIPE || errno == ECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
if (errno == EINTR)
return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
#endif

View file

@ -34,7 +34,13 @@
#include <pwd.h>
#include <grp.h>
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
int
lws_send_pipe_choked(struct lws *wsi)
@ -455,3 +461,55 @@ lws_plat_ifconfig_ip(const char *ifname, int fd, uint8_t *ip, uint8_t *mask_ip,
return -1;
#endif
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = write(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_WRITE;
if (errno == EPIPE || errno == ECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
if( errno == EINTR )
return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_NET_SEND_FAILED;
}
int
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = (int)read(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_READ;
if (errno == EPIPE || errno == ECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
if (errno == EINTR)
return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
#endif

View file

@ -27,6 +27,13 @@
#endif
#include "private-lib-core.h"
#if defined(LWS_WITH_MBEDTLS)
#if defined(LWS_HAVE_MBEDTLS_NET_SOCKETS)
#include "mbedtls/net_sockets.h"
#else
#include "mbedtls/net.h"
#endif
#endif
int
lws_send_pipe_choked(struct lws *wsi)
@ -380,3 +387,49 @@ lws_plat_ifconfig_ip(const char *ifname, int fd, uint8_t *ip, uint8_t *mask_ip,
return -1;
}
#if defined(LWS_WITH_MBEDTLS)
int
lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = write(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_WRITE;
if (WSAGetLastError() == WSAECONNRESET )
return( MBEDTLS_ERR_NET_CONN_RESET );
return MBEDTLS_ERR_NET_SEND_FAILED;
}
int
lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len)
{
int fd = ((mbedtls_net_context *) ctx)->fd;
int ret;
if (fd < 0)
return MBEDTLS_ERR_NET_INVALID_CONTEXT;
ret = (int)read(fd, buf, len);
if (ret >= 0)
return ret;
if (errno == EAGAIN || errno == EWOULDBLOCK)
return MBEDTLS_ERR_SSL_WANT_READ;
if (WSAGetLastError() == WSAECONNRESET)
return MBEDTLS_ERR_NET_CONN_RESET;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
#endif

View file

@ -67,6 +67,7 @@ struct pkey_pm
unsigned int max_content_len;
/*********************************************************************************************/
/************************************ SSL arch interface *************************************/
@ -185,7 +186,9 @@ int ssl_pm_new(SSL *ssl)
goto mbedtls_err2;
}
mbedtls_ssl_set_bio(&ssl_pm->ssl, &ssl_pm->fd, mbedtls_net_send, mbedtls_net_recv, NULL);
mbedtls_ssl_set_bio(&ssl_pm->ssl, &ssl_pm->fd,
lws_plat_mbedtls_net_send,
lws_plat_mbedtls_net_recv, NULL);
ssl->ssl_pm = ssl_pm;