Skip to content

Commit e5e11be

Browse files
committed
Reconstruct as Arduino Library.
1 parent c5103d8 commit e5e11be

11 files changed

+243
-24
lines changed

Diff for: .vscode/c_cpp_properties.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux",
5+
"includePath": [
6+
"${workspaceFolder}/**",
7+
"${env:HOME}/.arduino15/packages/esp32/hardware/esp32/1.0.4/**"
8+
],
9+
"forcedInclude": ["Arduino.h"],
10+
"defines": [],
11+
"compilerPath": "/usr/bin/clang",
12+
"cStandard": "c11",
13+
"cppStandard": "c++14",
14+
"intelliSenseMode": "linux-gcc-x64"
15+
}
16+
],
17+
"version": 4
18+
}

Diff for: .vscode/tasks.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "build",
8+
"type": "shell",
9+
"options": {
10+
"cwd": "${workspaceFolder}/examples/ping"
11+
},
12+
"command": "bash ./build.sh",
13+
"group": {
14+
"kind": "build",
15+
"isDefault": true
16+
}
17+
}
18+
]
19+
}

Diff for: CMakeLists.txt

-12
This file was deleted.

Diff for: examples/uptime_post/uptime_post.ino

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <WiFi.h>
2+
#include <WireGuard.hpp>
3+
#include <HTTPClient.h>
4+
5+
char ssid[] = "ds9";
6+
char password[] = "hogeFugapiyo";
7+
8+
// WireGuard configuration --- UPDATE this configuration from JSON
9+
char private_key[] = "(Private Key) "; // [Interface] PrivateKey
10+
IPAddress local_ip(1,2,3,4); // [Interface] Address
11+
char public_key[] = "(Public Key)"; // [Peer] PublicKey
12+
char endpoint_address[] = "link.arc.soracom.io"; // [Peer] Endpoint
13+
int endpoint_port = 11010; // [Peer] Endpoint
14+
15+
static constexpr const uint32_t UPDATE_INTERVAL_MS = 5000;
16+
17+
static WireGuard wg;
18+
static HTTPClient httpClient;
19+
20+
void setup()
21+
{
22+
Serial.begin(115200);
23+
Serial.println("Connecting to the AP...");
24+
WiFi.begin(ssid, password);
25+
while( !WiFi.isConnected() ) {
26+
delay(1000);
27+
}
28+
Serial.println("Adjusting system time...");
29+
configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com");
30+
31+
Serial.println("Connected. Initializing WireGuard...");
32+
wg.begin(
33+
local_ip,
34+
private_key,
35+
endpoint_address,
36+
public_key,
37+
endpoint_port);
38+
}
39+
40+
void loop()
41+
{
42+
WiFiClient client;
43+
44+
if( !client.connect("uni.soracom.io", 80) ) {
45+
Serial.println("Failed to connect...");
46+
delay(5000);
47+
return;
48+
}
49+
50+
uint64_t uptime_msec = millis();
51+
Serial.printf("Sending uptime %lu [ms]\r\n", uptime_msec);
52+
String json;
53+
json += "{\"uptime_msec\":";
54+
json.concat(static_cast<unsigned long>(uptime_msec));
55+
json += "}";
56+
Serial.printf("payload: %s\r\n", json.c_str());
57+
58+
client.write("POST / HTTP/1.1\r\n");
59+
client.write("Host: harvest.soracom.io\r\n");
60+
client.write("Connection: Keep-Alive\r\n");
61+
client.write("Keep-Alive: timeout=5, max=2\r\n");
62+
client.write("Content-Type: application/json\r\n");
63+
client.write("Content-Length: ");
64+
client.write(String(json.length(), 10).c_str());
65+
client.write("\r\n\r\n");
66+
client.write(json.c_str());
67+
68+
while(client.connected()) {
69+
auto line = client.readStringUntil('\n');
70+
Serial.write(line.c_str());
71+
Serial.write("\n");
72+
if( line == "\r" ) break;
73+
}
74+
if(client.connected()) {
75+
uint8_t buffer[256];
76+
size_t bytesToRead = 0;
77+
while((bytesToRead = client.available()) > 0) {
78+
bytesToRead = bytesToRead > sizeof(buffer) ? sizeof(buffer) : bytesToRead;
79+
auto bytesRead = client.readBytes(buffer, bytesToRead);
80+
Serial.write(buffer, bytesRead);
81+
}
82+
}
83+
delay(UPDATE_INTERVAL_MS);
84+
}

Diff for: library.properties

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=WireGuard-ESP32
2+
version=0.1.0
3+
author=Kenta Ida
4+
maintainer=Kenta Ida <[email protected]>
5+
sentence=WireGuard implementation for Arduino ESP32
6+
paragraph=WireGuard implementation for Arduino ESP32
7+
category=Communication
8+
url=https://github.com/ciniml/Arduino-WireGuard-ESP32
9+
includes=WireGuard.hpp
10+
architectures=esp32

Diff for: src/WireGuard.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "WireGuard.hpp"
2+
3+
#include "freertos/FreeRTOS.h"
4+
#include "freertos/task.h"
5+
#include "freertos/event_groups.h"
6+
#include "esp_system.h"
7+
#include "esp_log.h"
8+
9+
#include "lwip/err.h"
10+
#include "lwip/sys.h"
11+
#include "lwip/ip.h"
12+
#include "lwip/netdb.h"
13+
14+
extern "C" {
15+
#include "wireguardif.h"
16+
#include "wireguard-platform.h"
17+
}
18+
19+
// Wireguard instance
20+
static struct netif wg_netif_struct = {0};
21+
static struct netif *wg_netif = NULL;
22+
static uint8_t wireguard_peer_index = WIREGUARDIF_INVALID_INDEX;
23+
24+
#define TAG "WireGuard"
25+
26+
void WireGuard::begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort) {
27+
struct wireguardif_init_data wg;
28+
struct wireguardif_peer peer;
29+
ip_addr_t ipaddr = IPADDR4_INIT(static_cast<uint32_t>(localIP));
30+
ip_addr_t netmask = IPADDR4_INIT_BYTES(255, 255, 255, 255);
31+
ip_addr_t gateway = IPADDR4_INIT_BYTES(0, 0, 0, 0);
32+
33+
assert(privateKey != NULL);
34+
assert(remotePeerAddress != NULL);
35+
assert(remotePeerPublicKey != NULL);
36+
assert(remotePeerPort != 0);
37+
38+
// Setup the WireGuard device structure
39+
wg.private_key = privateKey;
40+
wg.listen_port = remotePeerPort;
41+
42+
wg.bind_netif = NULL;
43+
44+
// Register the new WireGuard network interface with lwIP
45+
wg_netif = netif_add(&wg_netif_struct, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gateway), &wg, &wireguardif_init, &ip_input);
46+
47+
// Mark the interface as administratively up, link up flag is set automatically when peer connects
48+
netif_set_up(wg_netif);
49+
50+
// Initialise the first WireGuard peer structure
51+
wireguardif_peer_init(&peer);
52+
peer.public_key = remotePeerPublicKey;
53+
peer.preshared_key = NULL;
54+
// Allow all IPs through tunnel
55+
{
56+
ip_addr_t allowed_ip = IPADDR4_INIT_BYTES(0, 0, 0, 0);
57+
peer.allowed_ip = allowed_ip;
58+
ip_addr_t allowed_mask = IPADDR4_INIT_BYTES(0, 0, 0, 0);
59+
peer.allowed_mask = allowed_mask;
60+
}
61+
// If we know the endpoint's address can add here
62+
{
63+
ip_addr_t endpoint_ip = IPADDR4_INIT_BYTES(0, 0, 0, 0);
64+
struct addrinfo *res = NULL;
65+
struct addrinfo hint;
66+
memset(&hint, 0, sizeof(hint));
67+
memset(&endpoint_ip, 0, sizeof(endpoint_ip));
68+
ESP_ERROR_CHECK(lwip_getaddrinfo(remotePeerAddress, NULL, &hint, &res) == 0 ? ESP_OK : ESP_FAIL);
69+
struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
70+
inet_addr_to_ip4addr(ip_2_ip4(&endpoint_ip), &addr4);
71+
lwip_freeaddrinfo(res);
72+
73+
peer.endpoint_ip = endpoint_ip;
74+
ESP_LOGI(TAG, "%s is %3d.%3d.%3d.%3d"
75+
, remotePeerAddress
76+
, (endpoint_ip.u_addr.ip4.addr >> 0) & 0xff
77+
, (endpoint_ip.u_addr.ip4.addr >> 8) & 0xff
78+
, (endpoint_ip.u_addr.ip4.addr >> 16) & 0xff
79+
, (endpoint_ip.u_addr.ip4.addr >> 24) & 0xff
80+
);
81+
}
82+
peer.endport_port = remotePeerPort;
83+
84+
// Initialize the platform
85+
wireguard_platform_init();
86+
// Register the new WireGuard peer with the netwok interface
87+
wireguardif_add_peer(wg_netif, &peer, &wireguard_peer_index);
88+
if ((wireguard_peer_index != WIREGUARDIF_INVALID_INDEX) && !ip_addr_isany(&peer.endpoint_ip)) {
89+
// Start outbound connection to peer
90+
ESP_LOGI(TAG, "connecting wireguard...");
91+
wireguardif_connect(wg_netif, wireguard_peer_index);
92+
netif_set_default(wg_netif);
93+
}
94+
}

Diff for: src/WireGuard.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
#include <IPAddress.h>
3+
4+
class WireGuard
5+
{
6+
public:
7+
void begin(const IPAddress& localIP, const char* privateKey, const char* remotePeerAddress, const char* remotePeerPublicKey, uint16_t remotePeerPort);
8+
};

Diff for: example/wireguard-platform.c renamed to src/wireguard-platform.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ uint32_t wireguard_sys_now() {
3535
return sys_now();
3636
}
3737

38-
// CHANGE THIS TO GET THE ACTUAL UNIX TIMESTMP IN MILLIS - HANDSHAKES WILL FAIL IF THIS DOESN'T INCREASE EACH TIME CALLED
3938
void wireguard_tai64n_now(uint8_t *output) {
4039
// See https://cr.yp.to/libtai/tai64.html
4140
// 64 bit seconds from 1970 = 8 bytes
4241
// 32 bit nano seconds from current second
42+
43+
struct timeval tv;
44+
gettimeofday(&tv, NULL);
45+
uint64_t millis = (tv.tv_sec * 1000LL + (tv.tv_usec / 1000LL));
4346

44-
uint64_t millis = sys_now();
45-
47+
// uint64_t millis = sys_now();
48+
4649
// Split into seconds offset + nanos
4750
uint64_t seconds = 0x400000000000000aULL + (millis / 1000);
4851
uint32_t nanos = (millis % 1000) * 1000;

Diff for: src/wireguard-platform.h

-1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,4 @@ void wireguard_tai64n_now(uint8_t *output);
6565
// Is the system under load - i.e. should we generate cookie reply message in response to initiation messages
6666
bool wireguard_is_under_load();
6767

68-
6968
#endif /* _WIREGUARD_PLATFORM_H_ */

Diff for: src/wireguard.h

-1
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,4 @@ bool wireguard_decrypt_packet(uint8_t *dst, const uint8_t *src, size_t src_len,
282282
bool wireguard_base64_decode(const char *str, uint8_t *out, size_t *outlen);
283283
bool wireguard_base64_encode(const uint8_t *in, size_t inlen, char *out, size_t *outlen);
284284

285-
286285
#endif /* _WIREGUARD_H_ */

Diff for: src/wireguardif.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include "wireguard.h"
4646
#include "crypto.h"
4747
#include "esp_log.h"
48-
#include "esp_netif.h"
48+
#include "tcpip_adapter.h"
4949

5050
#define WIREGUARDIF_TIMER_MSECS 400
5151

@@ -898,11 +898,8 @@ err_t wireguardif_init(struct netif *netif) {
898898
uint8_t private_key[WIREGUARD_PRIVATE_KEY_LEN];
899899
size_t private_key_len = sizeof(private_key);
900900

901-
char lwip_netif_name[8] = {0,};
902-
esp_netif_get_netif_impl_name( esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), lwip_netif_name);
903-
ESP_LOGI(TAG, "WIFI NETIF: %s", lwip_netif_name);
904-
905-
struct netif* underlying_netif = netif_find(lwip_netif_name);
901+
struct netif* underlying_netif;
902+
tcpip_adapter_get_netif(TCPIP_ADAPTER_IF_STA, &underlying_netif);
906903
ESP_LOGI(TAG, "underlying_netif = %p", underlying_netif);
907904

908905
LWIP_ASSERT("netif != NULL", (netif != NULL));
@@ -931,7 +928,7 @@ err_t wireguardif_init(struct netif *netif) {
931928
if (device) {
932929
device->netif = netif;
933930
device->underlying_netif = underlying_netif;
934-
udp_bind_netif(udp, underlying_netif);
931+
//udp_bind_netif(udp, underlying_netif);
935932

936933
device->udp_pcb = udp;
937934
// Per-wireguard netif/device setup

0 commit comments

Comments
 (0)