ciniml / WireGuard-ESP32-Arduino

WireGuard implementation for ESP32 Arduino
Other
782 stars 60 forks source link

Difficulties with UDP Server (AsyncUDP / WiFiUDP) #22

Open tkndrm opened 1 year ago

tkndrm commented 1 year ago

Hello,

really nice project, i am playing all day with the library and testing different setups. So far i didn't had any issues as TCP or UDP client.

But when i tested WiFiUDP and AsyncUDP as server, i run into weird problems. This is the output when i use AsyncUDP and listen

Sending packet to normal IP as Unicast

UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
        [ 14621][V][wireguardif.c:559] wireguardif_network_rx(): [[WireGuard] ] network_rx: 857ba8c0:51820
        [ 14622][I][wireguardif.c:564] wireguardif_network_rx(): [WireGuard] HANDSHAKE_INITIATION: 857ba8c0:51820
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
        [ 14806][V][wireguardif.c:559] wireguardif_network_rx(): [[WireGuard] ] network_rx: 857ba8c0:51820
        [ 14807][V][wireguardif.c:609] wireguardif_network_rx(): [[WireGuard] ] TRANSPORT_DATA: 857ba8c0:51820
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
        [ 15187][V][wireguardif.c:136] wireguardif_output_to_peer(): [WireGuard] preparing transport data...
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Unicast, From: 192.168.123.133:1234, To: 192.168.123.123:1234, Length: 24, Data: hello world, hello world
        [ 16965][V][wireguardif.c:136] wireguardif_output_to_peer(): [WireGuard] preparing transport data...
        [ 27187][V][wireguardif.c:136] wireguardif_output_to_peer(): [WireGuard] preparing transport data...

Sending packet to normal IP as Broadcast

UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
UDP Packet Type: Broadcast, From: 192.168.123.133:1234, To: 255.255.255.255:1234, Length: 24, Data: hello world, hello world
        [ 32036][V][wireguardif.c:136] wireguardif_output_to_peer(): [WireGuard] preparing transport data...
        [ 40220][V][wireguardif.c:559] wireguardif_network_rx(): [[WireGuard] ] network_rx: 857ba8c0:51820
        [ 40221][V][wireguardif.c:609] wireguardif_network_rx(): [[WireGuard] ] TRANSPORT_DATA: 857ba8c0:51820

Sending same packet to WireGuard IP

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x400896b3  PS      : 0x00060330  A0      : 0x800e7ef5  A1      : 0x3ffcd790  
A2      : 0x00000000  A3      : 0x3ffb5ac8  A4      : 0x00000000  A5      : 0x3ffc46a8  
A6      : 0x00060f20  A7      : 0x00000001  A8      : 0x800e7d36  A9      : 0x3ffcd740  
A10     : 0x00000001  A11     : 0x3ffb5660  A12     : 0x3ffb5614  A13     : 0x00000000  
A14     : 0x3ffb5660  A15     : 0x00000000  SAR     : 0x00000010  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x40089701  LEND    : 0x40089711  LCOUNT  : 0xfffffff3  

Backtrace:0x400896b0:0x3ffcd7900x400e7ef2:0x3ffcd7a0 0x400e7b15:0x3ffcd7c0 0x400e7b68:0x3ffcd7e0 0x400e7bc6:0x3ffcd800 0x400d82a2:0x3ffcd820 0x400d85d3:0x3ffcd850 0x400d860b:0x3ffcd8e0 0x400d8640:0x3ffcd900 

  #0  0x400896b0:0x3ffcd790 in strcmp at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/src/newlib/newlib/libc/machine/xtensa/strcmp.S:467
  #1  0x400e7ef2:0x3ffcd7a0 in esp_netif_get_handle_from_ifkey at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_netif/esp_netif_objects.c:182
  #2  0x400e7b15:0x3ffcd7c0 in netif_from_if at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/tcpip_adapter/tcpip_adapter_compat.c:76
  #3  0x400e7b68:0x3ffcd7e0 in netif_from_if at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/tcpip_adapter/tcpip_adapter_compat.c:74
  #4  0x400e7bc6:0x3ffcd800 in tcpip_adapter_get_netif at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/tcpip_adapter/tcpip_adapter_compat.c:226
  #5  0x400d82a2:0x3ffcd820 in AsyncUDPPacket::AsyncUDPPacket(AsyncUDP*, pbuf*, ip_addr const*, unsigned short, netif*) at /home/user/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src/AsyncUDP.cpp:334
  #6  0x400d85d3:0x3ffcd850 in AsyncUDP::_recv(udp_pcb*, pbuf*, ip_addr const*, unsigned short, netif*) at /home/user/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src/AsyncUDP.cpp:700
  #7  0x400d860b:0x3ffcd8e0 in AsyncUDP::_s_recv(void*, udp_pcb*, pbuf*, ip_addr const*, unsigned short, netif*) at /home/user/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src/AsyncUDP.cpp:709
  #8  0x400d8640:0x3ffcd900 in _udp_task(void*) at /home/user/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src/AsyncUDP.cpp:137

ELF file SHA256: 0000000000000000

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

This is the sourcecode im using

#include <Arduino.h>
#include <WiFi.h>
// #include <WiFiUdp.h>
#include <WireGuard-ESP32.h>
#include <AsyncUDP.h>

AsyncUDP udp;
IPAddress local_ip(172, 16, 23, 2);

char packetBuffer[24];  
char ssid[] = "SSID";
char password[] = "PASSWORD";

char private_key[] = "PRIVATE_KEY";
char public_key[] = "PUBLIC_KEY";
char endpoint_address[] = "192.168.123.133";
int endpoint_port = 51820;

static WireGuard wg;

void setup() {
  Serial.begin(115200);
  Serial.println("Connecting to the AP...");
  WiFi.begin(ssid, password);
  while (!WiFi.isConnected()) {
    delay(100);
  }

  Serial.println(WiFi.localIP());
  Serial.println("Adjusting system time...");
  configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com");

  Serial.print("Connected. Initializing WireGuard... ");
  if (wg.begin(local_ip, private_key, endpoint_address, public_key, endpoint_port)) {
    Serial.println("OK");
  } else {
    Serial.println("FAIL");
  }

  if (udp.listen(1234)) {
    Serial.print("UDP Listening on IP: ");
    Serial.println(WiFi.localIP());

    udp.onPacket([](AsyncUDPPacket packet) {
      Serial.print("UDP Packet Type: ");
      Serial.print(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
      Serial.print(", From: ");
      Serial.print(packet.remoteIP());
      Serial.print(":");
      Serial.print(packet.remotePort());
      Serial.print(", To: ");
      Serial.print(packet.localIP());
      Serial.print(":");
      Serial.print(packet.localPort());
      Serial.print(", Length: ");
      Serial.print(packet.length());
      Serial.print(", Data: ");
      Serial.write(packet.data(), packet.length());
      Serial.println();

      packet.printf("millis %lu", millis());
    });
  }

}

void loop() {
  delay(10);
}

and this is the working sourcecode with WiFiUDP. The only issue is that many times the server is not responsive, until a new Handshake is exchanged.

#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WireGuard-ESP32.h>

WiFiUDP udp;
IPAddress local_ip(172, 16, 23, 2);

char packetBuffer[24];  
char ssid[] = "SSID";
char password[] = "PASSWORD";

char private_key[] = "PRIVATE_KEY";
char public_key[] = "PUBLIC_KEY";
char endpoint_address[] = "192.168.123.133";
int endpoint_port = 51820;

static WireGuard wg;

void setup() {
  Serial.begin(115200);
  Serial.println("Connecting to the AP...");
  WiFi.begin(ssid, password);
  while (!WiFi.isConnected()) {
    delay(100);
  }

  Serial.println(WiFi.localIP());
  Serial.println("Adjusting system time...");
  configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com");

  Serial.print("Connected. Initializing WireGuard... ");
  if (wg.begin(local_ip, private_key, endpoint_address, public_key, endpoint_port)) {
    Serial.println("OK");
  } else {
    Serial.println("FAIL");
  }

  udp.begin(1234);
}

void loop() {
  int packetSize = udp.parsePacket();

  if (packetSize) {
    if(packetSize >= 25){
      Serial.println("Packet size too big!");
      udp.flush(); // without this line, sketch stop working on wireguard IP but still works with normal IP
      return;
    } 

    Serial.print(F("Packet Size: "));
    Serial.print(packetSize);
    Serial.print(F(" Port: "));
    Serial.print(udp.remotePort());
    Serial.print(F(" Data: "));
    udp.read(packetBuffer, 24);

    for (int i = 0; i < sizeof(packetBuffer); i++) {
      if (packetBuffer[i] < 16) {
        Serial.print(F("0"));
      }
      Serial.print(packetBuffer[i], HEX);
      Serial.print(F(" "));
    }
    Serial.println();

    udp.beginPacket(udp.remoteIP(), udp.remotePort());
    udp.printf("millis %lu", millis());
    udp.endPacket();

  }

  delay(10);
}
AndyW999 commented 12 months ago

I am having the same problem with AsyncUDP. Did you come up with a fix or should I get the JLink out?