RIOT-OS support with GNRC UDP/IP sockets

This commit is contained in:
Daniele Lacamera
2018-08-30 11:47:02 +02:00
parent c400c38588
commit 1db036eb75
3 changed files with 107 additions and 2 deletions

View File

@ -2225,4 +2225,78 @@ int uIPGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx)
#endif /* WOLFSSL_UIP */
#ifdef WOLFSSL_GNRC
#include <net/sock.h>
#include <net/sock/tcp.h>
#include <stdio.h>
/* GNRC TCP/IP port, using the native tcp/udp socket api.
* TCP and UDP are currently supported with the callbacks below.
*
*/
/* The GNRC tcp send callback
* return : bytes sent, or error
*/
int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx)
{
sock_tls_t *ctx = (sock_tls_t *)_ctx;
int ret = 0;
(void)ssl;
ret = sock_udp_send(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr);
if (ret <= 0)
return 0;
return ret;
}
/* The GNRC TCP/IP receive callback
* return : nb bytes read, or error
*/
int GNRC_Receive(WOLFSSL *ssl, char *buf, int sz, void *_ctx)
{
sock_udp_ep_t ep;
int ret;
uint32_t timeout = wolfSSL_dtls_get_current_timeout(ssl) * 1000000;
sock_tls_t *ctx = (sock_tls_t *)_ctx;
if (!ctx)
return -1;
(void)ssl;
if (wolfSSL_get_using_nonblock(ctx->ssl)) {
timeout = 0;
}
ret = sock_udp_recv(&ctx->conn.udp, buf, sz, timeout, &ep);
if (ret > 0) {
if (ctx->peer_addr.port == 0)
XMEMCPY(&ctx->peer_addr, &ep, sizeof(sock_udp_ep_t));
}
if (ret == -ETIMEDOUT) {
return WOLFSSL_CBIO_ERR_WANT_READ;
}
return ret;
}
/* GNRC DTLS Generate Cookie callback
* return : number of bytes copied into buf, or error
*/
int GNRC_GenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx)
{
sock_tls_t *ctx = (sock_tls_t *)_ctx;
byte token[32];
byte digest[WC_SHA_DIGEST_SIZE];
int ret = 0;
(void)ssl;
XMEMSET(token, 0, sizeof(token));
XMEMCPY(token, &ctx->peer_addr, sizeof(sock_udp_ep_t));
ret = wc_ShaHash(token, sizeof(sock_udp_ep_t), digest);
if (ret != 0)
return ret;
if (sz > WC_SHA_DIGEST_SIZE)
sz = WC_SHA_DIGEST_SIZE;
XMEMCPY(buf, digest, sz);
return sz;
}
#endif /* WOLFSSL_GNRC */
#endif /* WOLFCRYPT_ONLY */

View File

@ -527,7 +527,6 @@
#ifdef WOLFSSL_RIOT_OS
#define NO_WRITEV
#define TFM_NO_ASM
#define USE_FAST_MATH
#define NO_FILESYSTEM
#define USE_CERT_BUFFERS_2048
#endif

View File

@ -133,7 +133,8 @@
#include "rtipapi.h" /* errno */
#include "socket.h"
#elif !defined(DEVKITPRO) && !defined(WOLFSSL_PICOTCP) \
&& !defined(WOLFSSL_CONTIKI) && !defined(WOLFSSL_WICED)
&& !defined(WOLFSSL_CONTIKI) && !defined(WOLFSSL_WICED) \
&& !defined(WOLFSSL_GNRC) && !defined(WOLFSSL_RIOT_OS)
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
@ -491,6 +492,37 @@ WOLFSSL_API void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
#endif
#ifdef WOLFSSL_GNRC
#include <sock_types.h>
#include <net/gnrc.h>
#include <net/af.h>
#include <net/sock.h>
#include <net/gnrc/tcp.h>
#include <net/gnrc/udp.h>
struct gnrc_wolfssl_ctx {
union socket_connector {
#ifdef MODULE_SOCK_TCP
sock_tcp_t tcp;
#endif
sock_udp_t udp;
} conn;
WOLFSSL_CTX *ctx;
WOLFSSL *ssl;
int closing;
struct _sock_tl_ep peer_addr;
};
typedef struct gnrc_wolfssl_ctx sock_tls_t;
WOLFSSL_LOCAL int GNRC_Receive(WOLFSSL* ssl, char* buf, int sz,
void* ctx);
WOLFSSL_LOCAL int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
#endif
#ifdef WOLFSSL_DTLS
typedef int (*CallbackGenCookie)(WOLFSSL* ssl, unsigned char* buf, int sz,
void* ctx);