summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/tun.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/esp32/components/eos/tun.c')
-rw-r--r--fw/esp32/components/eos/tun.c46
1 files changed, 35 insertions, 11 deletions
diff --git a/fw/esp32/components/eos/tun.c b/fw/esp32/components/eos/tun.c
index f27fcfe..cb1dde6 100644
--- a/fw/esp32/components/eos/tun.c
+++ b/fw/esp32/components/eos/tun.c
@@ -4,40 +4,49 @@
#include <lwip/pbuf.h>
#include <lwip/netif.h>
#include <lwip/tcpip.h>
-#include <lwip/etharp.h>
+#include <lwip/ip.h>
+#include <lwip/lwip_napt.h>
+
+#include <esp_log.h>
#include "net.h"
#include "app.h"
#include "tun.h"
+static const char *TAG = "EOS TUN";
+
static ip4_addr_t ipaddr, netmask, gw;
static struct netif netif_tun;
static err_t IRAM_ATTR tun_output(struct netif *netif, struct pbuf *p, const struct ip4_addr *ipaddr) {
- unsigned char *buf;
+ EOSMessage msg;
struct pbuf *q;
for (q = p; q != NULL; q = q->next) {
- if (q->len > EOS_APP_MTU) continue;
-
- buf = eos_app_alloc();
- memcpy(buf, q->payload, q->len);
- eos_app_send(EOS_APP_MTYPE_TUN, buf, q->len);
+ eos_app_alloc(&msg);
+ if (q->len > msg.size) {
+ eos_app_free(&msg);
+ continue;
+ }
+
+ memcpy(msg.buffer, q->payload, q->len);
+ eos_app_send(EOS_APP_MTYPE_TUN, &msg, q->len);
}
return ERR_OK;
}
-static void IRAM_ATTR tun_input(unsigned char mtype, unsigned char *buffer, uint16_t len) {
+static void IRAM_ATTR tun_input(unsigned char mtype, EOSMessage *msg, uint16_t len) {
struct netif *netif = &netif_tun;
struct pbuf *p;
int rv;
if (!netif_is_up(netif)) return;
- p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
+ p = pbuf_alloc(PBUF_LINK, len, PBUF_RAM);
if (p == NULL) return;
- memcpy(p->payload, buffer, len);
+
+ memcpy(p->payload, msg->buffer, len);
rv = netif->input(p, netif);
if (rv != ERR_OK) {
pbuf_free(p);
@@ -55,16 +64,31 @@ static err_t tun_init(struct netif *netif) {
return ERR_OK;
}
+void eos_tun_portmap_add(uint32_t ext_addr) {
+ ip4_addr_t app_addr;
+
+ IP4_ADDR(&app_addr, 192,168,152,1);
+ ip_portmap_add(IP_PROTO_TCP, ext_addr, 22, app_addr.addr, 22);
+}
+
+void eos_tun_portmap_remove(void) {
+ ip_portmap_remove(IP_PROTO_TCP, 22);
+}
+
void eos_tun_init(void) {
struct netif *rv;
IP4_ADDR(&gw, 0,0,0,0);
- IP4_ADDR(&ipaddr, 192,168,10,2);
+ IP4_ADDR(&ipaddr, 192,168,152,2);
IP4_ADDR(&netmask, 255,255,255,0);
rv = netif_add(&netif_tun, &ipaddr, &netmask, &gw, NULL, tun_init, tcpip_input);
assert(rv != NULL);
netif_set_up(&netif_tun);
+ ip_napt_enable_netif(&netif_tun, 1);
eos_app_set_handler(EOS_APP_MTYPE_TUN, tun_input);
+
+ ESP_LOGI(TAG, "INIT");
+ ESP_LOGI(TAG, "addres: %x", (unsigned int)ipaddr.addr);
}