Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support LibreTiny platform #4

Merged
merged 1 commit into from
May 31, 2024
Merged
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
3 changes: 2 additions & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"license": "BSD-3-Clause",
"platforms": [
"espressif32",
"espressif8266"
"espressif8266",
"libretiny"
],
"frameworks":[
"espidf",
Expand Down
12 changes: 12 additions & 0 deletions src/esp_wireguard.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ esp_err_t esp_wireguard_init(wireguard_config_t *config, wireguard_ctx_t *ctx)

err = wireguard_platform_init();
if (err != ESP_OK) {
#if !defined(LIBRETINY)
ESP_LOGE(TAG, "wireguard_platform_init: %s", esp_err_to_name(err));
#else // !defined(LIBRETINY)
ESP_LOGE(TAG, "wireguard_platform_init: %d", err);
#endif // !defined(LIBRETINY)
goto fail;
}
ctx->config = config;
Expand All @@ -233,7 +237,11 @@ esp_err_t esp_wireguard_connect(wireguard_ctx_t *ctx)
if (ctx->netif == NULL) {
err = esp_wireguard_netif_create(ctx->config);
if (err != ESP_OK) {
#if !defined(LIBRETINY)
ESP_LOGE(TAG, "netif_create: %s", esp_err_to_name(err));
#else // !defined(LIBRETINY)
ESP_LOGE(TAG, "netif_create: %d", err);
#endif // !defined(LIBRETINY)
goto fail;
}
ctx->netif = wg_netif;
Expand Down Expand Up @@ -271,7 +279,11 @@ esp_err_t esp_wireguard_connect(wireguard_ctx_t *ctx)
/* Initialize the first WireGuard peer structure */
err = esp_wireguard_peer_init(ctx->config, &peer);
if (err != ESP_OK) {
#if !defined(LIBRETINY)
ESP_LOGE(TAG, "peer_init: %s", esp_err_to_name(err));
#else // !defined(LIBRETINY)
ESP_LOGE(TAG, "peer_init: %d", err);
#endif // !defined(LIBRETINY)
goto fail;
}

Expand Down
6 changes: 5 additions & 1 deletion src/esp_wireguard_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
#if !defined(__ESP_WIREGUARD_ERR__H__)
#define __ESP_WIREGUARD_ERR__H__

#if defined(ESP8266) && !defined(IDF_VER)
#if defined(LIBRETINY)
#undef esp_err_t
#endif

#if defined(ESP8266) && !defined(IDF_VER) || defined(LIBRETINY)
typedef int esp_err_t;

#define ESP_OK 0 /*!< esp_err_t value indicating success (no error) */
Expand Down
6 changes: 4 additions & 2 deletions src/esp_wireguard_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
#define ESP_LOGV(tag, ...) _noop(tag, __VA_ARGS__)
#endif

#else // defined(ESP8266) && !defined(IDF_VER)
#elif defined(LIBRETINY)
#include <libretiny.h>
#else // defined(LIBRETINY)
#include <esp_log.h>
#endif // defined(ESP8266) && !defined(IDF_VER)
#endif // defined(ESP8266) && !defined(IDF_VER)

#endif // __ESP_WIREGUARD_LOG__H__
13 changes: 11 additions & 2 deletions src/wireguard-platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@
#include "lwip/sys.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/version.h"

#if defined(ESP8266) && !defined(IDF_VER)
#include <osapi.h>
#define esp_fill_random(out, size) os_get_random(out, size)
#else // defined(ESP8266) && !defined(IDF_VER)
#elif defined(LIBRETINY)
#include <libretiny.h>
#define esp_fill_random(out, size) lt_rand_bytes(out, size)
#else // defined(LIBRETINY)
#include <esp_system.h>
#endif // defined(ESP8266) && !defined(IDF_VER)
#endif // defined(ESP8266) && !defined(IDF_VER)

#include "esp_wireguard_err.h"
#include "esp_wireguard_log.h"
Expand All @@ -56,8 +60,13 @@
#define ENTROPY_CUSTOM_DATA_LENGTH (0)
#define TAG "wireguard-platform"

#if MBEDTLS_VERSION_NUMBER >= 0x020D0000
static struct mbedtls_ctr_drbg_context random_context;
static struct mbedtls_entropy_context entropy_context;
#else
static mbedtls_ctr_drbg_context random_context;
static mbedtls_entropy_context entropy_context;
#endif

static int entropy_hw_random_source( void *data, unsigned char *output, size_t len, size_t *olen ) {
esp_fill_random(output, len);
Expand Down
18 changes: 12 additions & 6 deletions src/wireguardif.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
#include "esp_wireguard_log.h"
#include "esp_wireguard_err.h"

#if !defined(ESP8266) || defined(IDF_VER)
#if (!defined(ESP8266) || defined(IDF_VER)) && !defined(LIBRETINY)
#include <sys/socket.h>
#include "esp_netif.h"
#endif // !defined(ESP8266) || defined(IDF_VER)
#endif // (!defined(ESP8266) || defined(IDF_VER)) && !defined(LIBRETINY)

#include "wireguard.h"
#include "crypto.h"
Expand Down Expand Up @@ -948,7 +948,7 @@ err_t wireguardif_init(struct netif *netif) {

struct netif* underlying_netif = NULL;

#if !defined(ESP8266) || defined(IDF_VER)
#if (!defined(ESP8266) || defined(IDF_VER)) && !defined(LIBRETINY)
char lwip_netif_name[8] = {0,};

// list of interfaces to try to bind wireguard to
Expand All @@ -970,15 +970,21 @@ err_t wireguardif_init(struct netif *netif) {
}

underlying_netif = netif_find(lwip_netif_name);
#else // !defined(ESP8266) || defined(IDF_VER)
underlying_netif = netif_default;
#endif // !defined(ESP8266) || defined(IDF_VER)

if (underlying_netif == NULL) {
ESP_LOGE(TAG, "netif_find: cannot find %s (%s)", ifkey, lwip_netif_name);
result = ERR_IF;
goto fail;
}
#else // (!defined(ESP8266) || defined(IDF_VER)) && !defined(LIBRETINY)
underlying_netif = netif_default;

if (underlying_netif == NULL) {
ESP_LOGE(TAG, "netif_find: cannot find default netif");
result = ERR_IF;
goto fail;
}
#endif // (!defined(ESP8266) || defined(IDF_VER)) && !defined(LIBRETINY)

ESP_LOGV(TAG, "underlying_netif = %p", underlying_netif);

Expand Down