Skip to content

Commit a4b6e81

Browse files
committed
initial commit
1 parent a14879b commit a4b6e81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+49417
-0
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,97 @@
11
# lwip_nat_arduino
22
lwip library with NAT feature for Arduino environment
3+
4+
## Install
5+
Install the Arduino environment for the esp8266 as described here: https://github.com/esp8266/Arduino . As you are here you probably did this already...
6+
7+
This extension has been developed for the version 2.5 of the ESP8266 core. Switch to that in the Board Manager, if you havn't done already.
8+
9+
Download this repo to some place. Go to the ".../packages/esp8266/hardware/esp8266/2.5.0/tools/sdk/" dirctory of your installation. Here you rename the directory "lwip" to "lwip.orig". Then you copy the complete directory "lwip" of this repo to this place (in fact you replace "lwip" with my implementation).
10+
11+
Whenever you want to use this library, select *LwIP Variant: "v1.4 Compile from source* in the "Tools" menu of the Arduino shell.
12+
13+
## Usage
14+
The new functions are exported in the "lwip/lwip_napt.h" header:
15+
16+
´´´
17+
/**
18+
* Allocates and initializes the NAPT tables.
19+
*
20+
* @param max_nat max number of enties in the NAPT table (use IP_NAPT_MAX if in doubt)
21+
* @param max_portmap max number of enties in the NAPT table (use IP_PORTMAP_MAX if in doubt)
22+
*/
23+
void
24+
ip_napt_init(uint16_t max_nat, uint8_t max_portmap);
25+
26+
27+
/**
28+
* Enable/Disable NAPT for a specified interface.
29+
*
30+
* @param addr ip address of the interface
31+
* @param enable non-zero to enable NAPT, or 0 to disable.
32+
*/
33+
void
34+
ip_napt_enable(u32_t addr, int enable);
35+
36+
37+
/**
38+
* Enable/Disable NAPT for a specified interface.
39+
*
40+
* @param netif number of the interface: 0 = STA, 1 = AP
41+
* @param enable non-zero to enable NAPT, or 0 to disable.
42+
*/
43+
void
44+
ip_napt_enable_no(u8_t number, int enable);
45+
46+
47+
/**
48+
* Register port mapping on the external interface to internal interface.
49+
* When the same port mapping is registered again, the old mapping is overwritten.
50+
* In this implementation, only 1 unique port mapping can be defined for each target address/port.
51+
*
52+
* @param proto target protocol
53+
* @param maddr ip address of the external interface
54+
* @param mport mapped port on the external interface, in host byte order.
55+
* @param daddr destination ip address
56+
* @param dport destination port, in host byte order.
57+
*/
58+
u8_t
59+
ip_portmap_add(u8_t proto, u32_t maddr, u16_t mport, u32_t daddr, u16_t dport);
60+
61+
62+
/**
63+
* Unregister port mapping on the external interface to internal interface.
64+
*
65+
* @param proto target protocol
66+
* @param maddr ip address of the external interface
67+
*/
68+
u8_t
69+
ip_portmap_remove(u8_t proto, u16_t mport);
70+
71+
72+
/**
73+
* Sets the NAPT timeout for TCP connections.
74+
*
75+
* @param secs timeout in secs
76+
*/
77+
void
78+
ip_napt_set_tcp_timeout(u32_t secs);
79+
80+
81+
/**
82+
* Sets the NAPT timeout for UDP 'connections'.
83+
*
84+
* @param secs timeout in secs
85+
*/
86+
void
87+
ip_napt_set_udp_timeout(u32_t secs);
88+
´´´
89+
90+
In addition, the following extensions to the DHCP server of the AP interface might help:
91+
´´´
92+
void dhcps_set_DNS(struct ip_addr *dns_ip) ICACHE_FLASH_ATTR;
93+
´´´
94+
95+
This sets the DNS server that is distributed to the stations connected to the AP interface.
96+
97+
For an example look into: "WiFiNATRouter.ino" that sets up a basic NAT router between the AP and the STA interface (works like a basic version of https://github.com/martin-ger/esp_wifi_repeater )

WiFiNatRouter.ino

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <ESP8266WiFi.h>
2+
3+
#include "lwip/lwip_napt.h"
4+
5+
extern "C" {
6+
#include "lwip/app/dhcpserver.h"
7+
}
8+
9+
// credentials for ESP8266 STA
10+
const char* sta_ssid = "your_ssid";
11+
const char* sta_password = "your_pw";
12+
13+
// credentials for ESP8266 AP
14+
const char *ap_ssid = "ESPap";
15+
const char *ap_password = "password";
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println();
21+
22+
WiFi.mode(WIFI_AP_STA);
23+
24+
Serial.println("Starting NAT demo");
25+
26+
WiFi.begin(sta_ssid, sta_password);
27+
//WiFi.config(ip, gateway, subnet);
28+
29+
//Wifi connection
30+
while (WiFi.status() != WL_CONNECTED) {
31+
delay(500);
32+
Serial.print(".");
33+
}
34+
35+
Serial.println("");
36+
Serial.print("Connected to ");
37+
Serial.println(sta_ssid);
38+
Serial.print("IP address: ");
39+
Serial.println(WiFi.localIP());
40+
Serial.print("dnsIP address: ");
41+
Serial.println(WiFi.dnsIP());
42+
Serial.print("gatewayIP address: ");
43+
Serial.println(WiFi.gatewayIP());
44+
Serial.print("subnetMask address: ");
45+
Serial.println(WiFi.subnetMask());
46+
47+
48+
Serial.println("");
49+
Serial.println("Configuring access point...");
50+
WiFi.softAP(ap_ssid, ap_password);
51+
52+
IPAddress myIP = WiFi.softAPIP();
53+
Serial.print("AP IP address: ");
54+
Serial.println(myIP);
55+
56+
ip_napt_init(IP_NAPT_MAX, IP_PORTMAP_MAX);
57+
58+
ip_napt_enable_no(1, 1);
59+
60+
dhcps_set_DNS(WiFi.dnsIP());
61+
62+
}
63+
64+
void loop()
65+
{
66+
delay(500);
67+
}

lwip/include/arch/cc.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2001, Swedish Institute of Computer Science.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 3. Neither the name of the Institute nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software
15+
* without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*
29+
* This file is part of the lwIP TCP/IP stack.
30+
*
31+
* Author: Adam Dunkels <adam@sics.se>
32+
*
33+
*/
34+
#ifndef __ARCH_CC_H__
35+
#define __ARCH_CC_H__
36+
37+
//#include <string.h>
38+
#include "c_types.h"
39+
#include "ets_sys.h"
40+
#include "osapi.h"
41+
#define EFAULT 14
42+
43+
//#define LWIP_PROVIDE_ERRNO
44+
45+
#if (1)
46+
#define BYTE_ORDER LITTLE_ENDIAN
47+
#else
48+
#define BYTE_ORDER BIG_ENDIAN
49+
#endif
50+
51+
52+
typedef unsigned char u8_t;
53+
typedef signed char s8_t;
54+
typedef unsigned short u16_t;
55+
typedef signed short s16_t;
56+
typedef unsigned long u32_t;
57+
typedef signed long s32_t;
58+
typedef unsigned long mem_ptr_t;
59+
60+
#define S16_F "d"
61+
#define U16_F "d"
62+
#define X16_F "x"
63+
64+
#define S32_F "d"
65+
#define U32_F "d"
66+
#define X32_F "x"
67+
68+
#define LWIP_ERR_T s32_t
69+
70+
//#define PACK_STRUCT_FIELD(x) x __attribute__((packed))
71+
#define PACK_STRUCT_FIELD(x) x
72+
#define PACK_STRUCT_STRUCT __attribute__((packed))
73+
#define PACK_STRUCT_BEGIN
74+
#define PACK_STRUCT_END
75+
76+
//#define LWIP_DEBUG
77+
78+
#ifdef LWIP_DEBUG
79+
#define LWIP_PLATFORM_DIAG(x) os_printf x
80+
#define LWIP_PLATFORM_ASSERT(x) ETS_ASSERT(x)
81+
#else
82+
#define LWIP_PLATFORM_DIAG(x)
83+
#define LWIP_PLATFORM_ASSERT(x)
84+
#endif
85+
86+
#define SYS_ARCH_DECL_PROTECT(x)
87+
#define SYS_ARCH_PROTECT(x)
88+
#define SYS_ARCH_UNPROTECT(x)
89+
90+
#define LWIP_PLATFORM_BYTESWAP 1
91+
#define LWIP_PLATFORM_HTONS(_n) ((u16_t)((((_n) & 0xff) << 8) | (((_n) >> 8) & 0xff)))
92+
#define LWIP_PLATFORM_HTONL(_n) ((u32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) ))
93+
94+
#if LWIP_RAW
95+
extern u8_t memp_memory_RAW_PCB_base[];
96+
#endif /* LWIP_RAW */
97+
98+
#if LWIP_UDP
99+
extern u8_t memp_memory_UDP_PCB_base[];
100+
#endif /* LWIP_UDP */
101+
102+
#if LWIP_TCP
103+
extern u8_t memp_memory_TCP_PCB_base[];
104+
extern u8_t memp_memory_TCP_PCB_LISTEN_base[];
105+
extern u8_t memp_memory_TCP_SEG_base[] SHMEM_ATTR;
106+
#endif /* LWIP_TCP */
107+
108+
#if (!NO_SYS || (NO_SYS && !NO_SYS_NO_TIMERS)) /* LWIP_TIMERS */
109+
extern u8_t memp_memory_SYS_TIMEOUT_base[];
110+
#endif /* LWIP_TIMERS */
111+
112+
extern u8_t memp_memory_PBUF_base[];
113+
extern u8_t memp_memory_PBUF_POOL_base[];
114+
115+
116+
117+
#endif /* __ARCH_CC_H__ */

lwip/include/arch/perf.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2001, Swedish Institute of Computer Science.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
* 3. Neither the name of the Institute nor the names of its contributors
14+
* may be used to endorse or promote products derived from this software
15+
* without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*
29+
* This file is part of the lwIP TCP/IP stack.
30+
*
31+
* Author: Adam Dunkels <adam@sics.se>
32+
*
33+
*/
34+
#ifndef __PERF_H__
35+
#define __PERF_H__
36+
37+
#define PERF_START /* null definition */
38+
#define PERF_STOP(x) /* null definition */
39+
40+
#endif /* __PERF_H__ */

lwip/include/arch/sys_arch.h

Whitespace-only changes.

0 commit comments

Comments
 (0)