Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,12 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap)
#ifdef HAVE_NETX
ctx->CBIORecv = NetX_Receive;
ctx->CBIOSend = NetX_Send;
#ifdef WOLFSSL_DTLS
if (method->version.major == DTLS_MAJOR) {
ctx->CBIORecv = NetX_ReceiveFrom;
ctx->CBIOSend = NetX_SendTo;
}
#endif
#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
ctx->CBIORecv = Mynewt_Receive;
ctx->CBIOSend = Mynewt_Send;
Expand Down Expand Up @@ -7950,9 +7956,13 @@ int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
if (wc_InitRwLock(&ssl->buffers.dtlsCtx.peerLock) != 0)
return BAD_MUTEX_E;
#endif

#ifdef HAVE_NETX
ssl->IOCB_ReadCtx = &ssl->nxCtx; /* default NetX IO ctx, same for read */
ssl->IOCB_WriteCtx = &ssl->nxCtx; /* and write */
#else
ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx; /* prevent invalid pointer access if not */
ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx; /* correctly set */
#endif
#else
#ifdef HAVE_NETX
ssl->IOCB_ReadCtx = &ssl->nxCtx; /* default NetX IO ctx, same for read */
Expand Down
138 changes: 128 additions & 10 deletions src/wolfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,7 @@

#ifdef HAVE_NETX

/* The NetX receive callback
/* The NetX receive callback for TLS
* return : bytes read, or error
*/
int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
Expand All @@ -2543,13 +2543,13 @@

(void)ssl;

if (nxCtx == NULL || nxCtx->nxSocket == NULL) {
if (nxCtx == NULL || nxCtx->nxTcpSocket == NULL) {
WOLFSSL_MSG("NetX Recv NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

if (nxCtx->nxPacket == NULL) {
status = nx_tcp_socket_receive(nxCtx->nxSocket, &nxCtx->nxPacket,
status = nx_tcp_socket_receive(nxCtx->nxTcpSocket, &nxCtx->nxPacket,
nxCtx->nxWait);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Recv receive error");
Expand Down Expand Up @@ -2586,7 +2586,7 @@
}


/* The NetX send callback
/* The NetX send callback for TLS
* return : bytes sent, or error
*/
int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx)
Expand All @@ -2598,12 +2598,12 @@

(void)ssl;

if (nxCtx == NULL || nxCtx->nxSocket == NULL) {
if (nxCtx == NULL || nxCtx->nxTcpSocket == NULL) {
WOLFSSL_MSG("NetX Send NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

pool = nxCtx->nxSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool;
pool = nxCtx->nxTcpSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool;
status = nx_packet_allocate(pool, &packet, NX_TCP_PACKET,
nxCtx->nxWait);
if (status != NX_SUCCESS) {
Expand All @@ -2618,7 +2618,7 @@
return WOLFSSL_CBIO_ERR_GENERAL;
}

status = nx_tcp_socket_send(nxCtx->nxSocket, packet, nxCtx->nxWait);
status = nx_tcp_socket_send(nxCtx->nxTcpSocket, packet, nxCtx->nxWait);
if (status != NX_SUCCESS) {
nx_packet_release(packet);
WOLFSSL_MSG("NetX Send socket send error");
Expand All @@ -2628,13 +2628,131 @@
return sz;
}

/* The NetX receive callback for DTLS
* return : bytes read, or error
*/
int NetX_ReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
{
NetX_Ctx* nxCtx = (NetX_Ctx*)ctx;
ULONG left;
ULONG total;
ULONG copied = 0;
UINT status;

(void)ssl;

if (nxCtx == NULL || nxCtx->nxUdpSocket == NULL) {
WOLFSSL_MSG("NetX Recv NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

if (nxCtx->nxPacket == NULL) {
status = nx_udp_socket_receive(nxCtx->nxUdpSocket, &nxCtx->nxPacket,
nxCtx->nxWait);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Recv receive error");
return WOLFSSL_CBIO_ERR_GENERAL;
}
}

if (nxCtx->nxPacket) {
status = nx_packet_length_get(nxCtx->nxPacket, &total);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Recv length get error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

left = total - nxCtx->nxOffset;
status = nx_packet_data_extract_offset(nxCtx->nxPacket, nxCtx->nxOffset,
buf, sz, &copied);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Recv data extract offset error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

nxCtx->nxOffset += copied;

if (copied == left) {
WOLFSSL_MSG("NetX Recv Drained packet");
nx_packet_release(nxCtx->nxPacket);
nxCtx->nxPacket = NULL;
nxCtx->nxOffset = 0;
}
}

return copied;
}

/* The NetX send callback for DTLS
* return : bytes sent, or error
*/
int NetX_SendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
{
NetX_Ctx* nxCtx = (NetX_Ctx*)ctx;
NX_PACKET* packet;
NX_PACKET_POOL* pool; /* shorthand */
UINT status;

(void)ssl;

if (nxCtx == NULL || nxCtx->nxUdpSocket == NULL

Check failure on line 2698 in src/wolfio.c

View workflow job for this annotation

GitHub Actions / check

trailing-whitespace

trailing whitespace
|| nxCtx->nxdIp == NULL || nxCtx->nxPort == NULL) {
WOLFSSL_MSG("NetX Send NULL parameters");
return WOLFSSL_CBIO_ERR_GENERAL;
}

pool = nxCtx->nxUdpSocket->nx_udp_socket_ip_ptr->nx_ip_default_packet_pool;
status = nx_packet_allocate(pool, &packet, NX_UDP_PACKET,
nxCtx->nxWait);
if (status != NX_SUCCESS) {
WOLFSSL_MSG("NetX Send packet alloc error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

status = nx_packet_data_append(packet, buf, sz, pool, nxCtx->nxWait);
if (status != NX_SUCCESS) {
nx_packet_release(packet);
WOLFSSL_MSG("NetX Send data append error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

if(nxCtx->nxdIp->nxd_ip_version == NX_IP_VERSION_V4)
{
status = nx_udp_socket_send(nxCtx->nxUdpSocket, packet, nxCtx->nxdIp->nxd_ip_address.v4, (UINT)(*nxCtx->nxPort));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overlong lines added:
src/wolfio.c:2721         status = nx_udp_socket_send(nxCtx->nxUdpSocket, packet, nxCtx->nxdIp->nxd_ip_address.v4, (UINT)(*nxCtx->nxPort));
src/wolfio.c:2729         status = nxd_udp_socket_send(nxCtx->nxUdpSocket, packet, nxCtx->nxdIp, (UINT)(*nxCtx->nxPort));
src/wolfio.c:2749 void wolfSSL_SetIO_NetX_Dtls(WOLFSSL* ssl, NX_UDP_SOCKET* nxsocket, NXD_ADDRESS *nxdip, USHORT *nxport, ULONG waitoption)
wolfssl/wolfio.h:793     WOLFSSL_LOCAL int NetX_ReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx);
wolfssl/wolfio.h:798     WOLFSSL_API void wolfSSL_SetIO_NetX_Dtls(WOLFSSL* ssl, NX_UDP_SOCKET* nxsocket, 
    check-source-text fail_A

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guarded with WOLFSSL_DTLS

if (status != NX_SUCCESS) {
nx_packet_release(packet);
WOLFSSL_MSG("NetX Send socket send error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

Check failure on line 2726 in src/wolfio.c

View workflow job for this annotation

GitHub Actions / check

trailing-whitespace

trailing whitespace
}else
{
status = nxd_udp_socket_send(nxCtx->nxUdpSocket, packet, nxCtx->nxdIp, (UINT)(*nxCtx->nxPort));
if (status != NX_SUCCESS) {
nx_packet_release(packet);
WOLFSSL_MSG("NetX Send socket send error");
return WOLFSSL_CBIO_ERR_GENERAL;
}

Check failure on line 2734 in src/wolfio.c

View workflow job for this annotation

GitHub Actions / check

trailing-whitespace

trailing whitespace
}

return sz;
}

/* like set_fd, but for default NetX context */
void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket, ULONG waitoption)
{
if (ssl) {
ssl->nxCtx.nxTcpSocket = nxsocket;
ssl->nxCtx.nxWait = waitoption;
}
}

void wolfSSL_SetIO_NetX_Dtls(WOLFSSL* ssl, NX_UDP_SOCKET* nxsocket, NXD_ADDRESS *nxdip, USHORT *nxport, ULONG waitoption)
{
if (ssl) {
ssl->nxCtx.nxSocket = nxSocket;
ssl->nxCtx.nxWait = waitOption;
ssl->nxCtx.nxUdpSocket = nxsocket;

Check failure on line 2752 in src/wolfio.c

View workflow job for this annotation

GitHub Actions / check

trailing-whitespace

trailing whitespace
ssl->nxCtx.nxdIp = nxdip;
ssl->nxCtx.nxPort = nxport;
ssl->nxCtx.nxWait = waitoption;
}
}

Expand Down
5 changes: 4 additions & 1 deletion wolfssl/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5772,10 +5772,13 @@ typedef struct DtlsMsg {

/* NETX I/O Callback default */
typedef struct NetX_Ctx {
NX_TCP_SOCKET* nxSocket; /* send/recv socket handle */
NX_TCP_SOCKET* nxTcpSocket; /* send/recv tcp socket handle */
NX_UDP_SOCKET* nxUdpSocket; /* send/recv udp socket handle */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps only include this in the struct when WOLFSSL_DTLS is defined? Same with the other two at bottom?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guarded with WOLFSSL_DTLS

NX_PACKET* nxPacket; /* incoming packet handle for short reads */
ULONG nxOffset; /* offset already read from nxPacket */
ULONG nxWait; /* wait option flag */
NXD_ADDRESS* nxdIp; /* IP address for udp send*/
USHORT* nxPort; /* Port number for udp recv*/
} NetX_Ctx;

#endif
Expand Down
6 changes: 6 additions & 0 deletions wolfssl/wolfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,15 @@
#ifdef HAVE_NETX
WOLFSSL_LOCAL int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx);
WOLFSSL_LOCAL int NetX_Send(WOLFSSL *ssl, char *buf, int sz, void *ctx);
WOLFSSL_LOCAL int NetX_ReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx);
WOLFSSL_LOCAL int NetX_SendTo(WOLFSSL *ssl, char *buf, int sz, void *ctx);

WOLFSSL_API void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket,
ULONG waitoption);
WOLFSSL_API void wolfSSL_SetIO_NetX_Dtls(WOLFSSL* ssl, NX_UDP_SOCKET* nxsocket,

Check failure on line 798 in wolfssl/wolfio.h

View workflow job for this annotation

GitHub Actions / check

trailing-whitespace

trailing whitespace
NXD_ADDRESS *nxdip,

Check failure on line 799 in wolfssl/wolfio.h

View workflow job for this annotation

GitHub Actions / check

trailing-whitespace

trailing whitespace
USHORT* nxport,

Check failure on line 800 in wolfssl/wolfio.h

View workflow job for this annotation

GitHub Actions / check

trailing-whitespace

trailing whitespace
ULONG waitoption);
#endif /* HAVE_NETX */

#ifdef MICRIUM
Expand Down
Loading