LwIP v2 support

This commit is contained in:
Our Air Quality 2017-06-06 12:47:21 +10:00
parent 1cfded6389
commit cd23acaa4a
39 changed files with 1357 additions and 664 deletions

View file

@ -48,7 +48,7 @@ void user_init(void)
};
sdk_wifi_softap_set_config(&ap_config);
ip_addr_t first_client_ip;
ip4_addr_t first_client_ip;
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
dhcpserver_start(&first_client_ip, 4);
@ -65,14 +65,14 @@ static void telnetTask(void *pvParameters)
printf("Status monitor: Failed to allocate socket.\r\n");
return;
}
netconn_bind(nc, IP_ADDR_ANY, TELNET_PORT);
netconn_bind(nc, IP_ANY_TYPE, TELNET_PORT);
netconn_listen(nc);
while(1) {
struct netconn *client = NULL;
err_t err = netconn_accept(nc, &client);
if ( err != ERR_OK ) {
if (err != ERR_OK) {
if(client)
netconn_delete(client);
continue;
@ -88,9 +88,8 @@ static void telnetTask(void *pvParameters)
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
snprintf(buf, sizeof(buf), "Free heap %d bytes\r\n", (int)xPortGetFreeHeapSize());
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
snprintf(buf, sizeof(buf), "Your address is %d.%d.%d.%d\r\n\r\n",
ip4_addr1(&client_addr), ip4_addr2(&client_addr),
ip4_addr3(&client_addr), ip4_addr4(&client_addr));
char abuf[40];
snprintf(buf, sizeof(buf), "Your address is %s\r\n\r\n", ipaddr_ntoa_r(&client_addr, abuf, sizeof(abuf)));
netconn_write(client, buf, strlen(buf), NETCONN_COPY);
netconn_delete(client);
}

View file

@ -20,9 +20,9 @@
#include "ssid_config.h"
#define WEB_SERVER "chainxor.org"
#define WEB_SERVER "ipv6.google.com"
#define WEB_PORT 80
#define WEB_URL "http://chainxor.org/"
#define WEB_PATH "/"
void http_get_task(void *pvParameters)
{
@ -31,7 +31,7 @@ void http_get_task(void *pvParameters)
while(1) {
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
@ -39,7 +39,7 @@ void http_get_task(void *pvParameters)
printf("Running DNS lookup for %s...\r\n", WEB_SERVER);
int err = getaddrinfo(WEB_SERVER, "80", &hints, &res);
if(err != 0 || res == NULL) {
if (err != 0 || res == NULL) {
printf("DNS lookup failed err=%d res=%p\r\n", err, res);
if(res)
freeaddrinfo(res);
@ -47,9 +47,24 @@ void http_get_task(void *pvParameters)
failures++;
continue;
}
/* Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(*addr));
{
struct netif *netif = sdk_system_get_netif(0);
int i;
for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
printf(" ip6 %d state %x\n", i, netif_ip6_addr_state(netif, i));
if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)))
printf(" ip6 addr %d = %s\n", i, ip6addr_ntoa(netif_ip6_addr(netif, i)));
}
}
struct sockaddr *sa = res->ai_addr;
if (sa->sa_family == AF_INET) {
printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(((struct sockaddr_in *)sa)->sin_addr));
}
if (sa->sa_family == AF_INET6) {
printf("DNS lookup succeeded. IP=%s\r\n", inet6_ntoa(((struct sockaddr_in6 *)sa)->sin6_addr));
}
int s = socket(res->ai_family, res->ai_socktype, 0);
if(s < 0) {
@ -75,8 +90,10 @@ void http_get_task(void *pvParameters)
freeaddrinfo(res);
const char *req =
"GET "WEB_URL"\r\n"
"GET "WEB_PATH" HTTP/1.1\r\n"
"Host: "WEB_SERVER"\r\n"
"User-Agent: esp-open-rtos/0.1 esp8266\r\n"
"Connection: close\r\n"
"\r\n";
if (write(s, req, strlen(req)) < 0) {
printf("... socket send failed\r\n");
@ -126,6 +143,6 @@ void user_init(void)
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
xTaskCreate(&http_get_task, "get_task", 256, NULL, 2, NULL);
xTaskCreate(&http_get_task, "get_task", 384, NULL, 2, NULL);
}

View file

@ -202,9 +202,8 @@ void tls_server_task(void *pvParameters)
socklen_t peer_addr_len = sizeof(struct sockaddr_in);
getpeername(client_ctx.fd, (struct sockaddr *)&peer_addr, &peer_addr_len);
unsigned char buf[256];
int len = sprintf((char *) buf, "O hai, client %d.%d.%d.%d:%d\nFree heap size is %d bytes\n",
ip4_addr1(&peer_addr.sin_addr), ip4_addr2(&peer_addr.sin_addr),
ip4_addr3(&peer_addr.sin_addr), ip4_addr4(&peer_addr.sin_addr),
int len = sprintf((char *) buf, "O hai, client " IPSTR ":%d\nFree heap size is %d bytes\n",
IP2STR((ip4_addr_t *)&peer_addr.sin_addr.s_addr),
peer_addr.sin_port, xPortGetFreeHeapSize());
while((ret = mbedtls_ssl_write(&ssl, buf, len)) <= 0)
{

View file

@ -146,9 +146,8 @@ void tls_server_task(void *pvParameters)
/* Prepare a message to the client */
unsigned char buf[100];
int len = sprintf((char *) buf, "O hai, client %d.%d.%d.%d:%d\r\nFree heap size is %d bytes\r\n",
ip4_addr1(&sa.sin_addr), ip4_addr2(&sa.sin_addr),
ip4_addr3(&sa.sin_addr), ip4_addr4(&sa.sin_addr),
int len = sprintf((char *) buf, "O hai, client " IPSTR ":%d\r\nFree heap size is %d bytes\r\n",
IP2STR((ip4_addr_t *)&sa.sin_addr.s_addr),
ntohs(sa.sin_port), xPortGetFreeHeapSize());
/* Send the message and close the connection */

View file

@ -1,7 +1,5 @@
#define LWIP_IGMP 1
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
/* Use the defaults for everything else */
#include_next <lwipopts.h>

View file

@ -1,6 +1,7 @@
#include <string.h>
#include <lwip/udp.h>
#include <lwip/igmp.h>
#include <lwip/ip_addr.h>
#include <espressif/esp_common.h>
#include "upnp.h"
@ -18,13 +19,13 @@ static const char* get_my_ip(void)
}
/**
* @brief This function joins a multicast group witht he specified ip/port
* @brief This function joins a multicast group with the specified ip/port
* @param group_ip the specified multicast group ip
* @param group_port the specified multicast port number
* @param recv the lwip UDP callback
* @retval udp_pcb* or NULL if joining failed
*/
static struct udp_pcb* mcast_join_group(char *group_ip, uint16_t group_port, void (* recv)(void * arg, struct udp_pcb * upcb, struct pbuf * p, struct ip_addr * addr, u16_t port))
static struct udp_pcb* mcast_join_group(char *group_ip, uint16_t group_port, void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port))
{
bool status = false;
struct udp_pcb *upcb;
@ -36,7 +37,7 @@ static struct udp_pcb* mcast_join_group(char *group_ip, uint16_t group_port, voi
printf("Error, udp_new failed");
break;
}
udp_bind(upcb, IP_ADDR_ANY, group_port);
udp_bind(upcb, IP4_ADDR_ANY, group_port);
struct netif* netif = sdk_system_get_netif(STATION_IF);
if (!netif) {
printf("Error, netif is null");
@ -46,10 +47,10 @@ static struct udp_pcb* mcast_join_group(char *group_ip, uint16_t group_port, voi
netif->flags |= NETIF_FLAG_IGMP;
igmp_start(netif);
}
ip_addr_t ipgroup;
ipaddr_aton(group_ip, &ipgroup);
err_t err = igmp_joingroup(&netif->ip_addr, &ipgroup);
if(ERR_OK != err) {
ip4_addr_t ipgroup;
ip4addr_aton(group_ip, &ipgroup);
err_t err = igmp_joingroup_netif(netif, &ipgroup);
if (ERR_OK != err) {
printf("Failed to join multicast group: %d", err);
break;
}
@ -68,7 +69,7 @@ static struct udp_pcb* mcast_join_group(char *group_ip, uint16_t group_port, voi
return upcb;
}
static void send(struct udp_pcb *upcb, struct ip_addr *addr, u16_t port)
static void send_udp(struct udp_pcb *upcb, const ip_addr_t *addr, u16_t port)
{
struct pbuf *p;
char msg[500];
@ -110,14 +111,14 @@ static void send(struct udp_pcb *upcb, struct ip_addr *addr, u16_t port)
* @param port the remote port from which the packet was received
* @retval None
*/
static void receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
static void receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
if (p) {
printf("Msg received port:%d len:%d\n", port, p->len);
uint8_t *buf = (uint8_t*) p->payload;
printf("Msg received port:%d len:%d\nbuf: %s\n", port, p->len, buf);
send(upcb, addr, port);
send_udp(upcb, addr, port);
pbuf_free(p);
}