2018-10-02 10:33:16 -04:00
|
|
|
/* BSD Socket API Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_wifi.h"
|
2018-11-20 11:41:08 -05:00
|
|
|
#include "esp_event.h"
|
2018-10-02 10:33:16 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-08-31 10:19:21 -04:00
|
|
|
#include "esp_netif.h"
|
2018-11-20 11:41:08 -05:00
|
|
|
#include "protocol_examples_common.h"
|
2018-10-02 10:33:16 -04:00
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include <lwip/netdb.h>
|
|
|
|
|
|
|
|
|
2021-02-02 22:27:06 -05:00
|
|
|
#define PORT CONFIG_EXAMPLE_PORT
|
|
|
|
#define KEEPALIVE_IDLE CONFIG_EXAMPLE_KEEPALIVE_IDLE
|
|
|
|
#define KEEPALIVE_INTERVAL CONFIG_EXAMPLE_KEEPALIVE_INTERVAL
|
|
|
|
#define KEEPALIVE_COUNT CONFIG_EXAMPLE_KEEPALIVE_COUNT
|
2018-10-02 10:33:16 -04:00
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
|
2019-06-12 04:33:57 -04:00
|
|
|
static void do_retransmit(const int sock)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char rx_buffer[128];
|
|
|
|
|
|
|
|
do {
|
|
|
|
len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
|
|
|
|
if (len < 0) {
|
|
|
|
ESP_LOGE(TAG, "Error occurred during receiving: errno %d", errno);
|
|
|
|
} else if (len == 0) {
|
|
|
|
ESP_LOGW(TAG, "Connection closed");
|
|
|
|
} else {
|
|
|
|
rx_buffer[len] = 0; // Null-terminate whatever is received and treat it like a string
|
|
|
|
ESP_LOGI(TAG, "Received %d bytes: %s", len, rx_buffer);
|
|
|
|
|
|
|
|
// send() can return less bytes than supplied length.
|
2020-11-10 02:40:01 -05:00
|
|
|
// Walk-around for robust implementation.
|
2019-06-12 04:33:57 -04:00
|
|
|
int to_write = len;
|
|
|
|
while (to_write > 0) {
|
|
|
|
int written = send(sock, rx_buffer + (len - to_write), to_write, 0);
|
|
|
|
if (written < 0) {
|
|
|
|
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
|
|
|
|
}
|
|
|
|
to_write -= written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (len > 0);
|
|
|
|
}
|
2018-10-02 10:33:16 -04:00
|
|
|
|
|
|
|
static void tcp_server_task(void *pvParameters)
|
|
|
|
{
|
|
|
|
char addr_str[128];
|
2020-03-17 15:30:33 -04:00
|
|
|
int addr_family = (int)pvParameters;
|
|
|
|
int ip_protocol = 0;
|
2021-02-02 22:27:06 -05:00
|
|
|
int keepAlive = 1;
|
|
|
|
int keepIdle = KEEPALIVE_IDLE;
|
|
|
|
int keepInterval = KEEPALIVE_INTERVAL;
|
|
|
|
int keepCount = KEEPALIVE_COUNT;
|
2021-01-26 03:17:56 -05:00
|
|
|
struct sockaddr_storage dest_addr;
|
2020-03-17 15:30:33 -04:00
|
|
|
|
|
|
|
if (addr_family == AF_INET) {
|
|
|
|
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
|
|
|
|
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
dest_addr_ip4->sin_family = AF_INET;
|
|
|
|
dest_addr_ip4->sin_port = htons(PORT);
|
|
|
|
ip_protocol = IPPROTO_IP;
|
2021-01-26 03:17:56 -05:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_EXAMPLE_IPV6
|
|
|
|
else if (addr_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *dest_addr_ip6 = (struct sockaddr_in6 *)&dest_addr;
|
|
|
|
bzero(&dest_addr_ip6->sin6_addr.un, sizeof(dest_addr_ip6->sin6_addr.un));
|
|
|
|
dest_addr_ip6->sin6_family = AF_INET6;
|
|
|
|
dest_addr_ip6->sin6_port = htons(PORT);
|
2020-03-17 15:30:33 -04:00
|
|
|
ip_protocol = IPPROTO_IPV6;
|
|
|
|
}
|
2021-01-26 03:17:56 -05:00
|
|
|
#endif
|
2018-10-02 10:33:16 -04:00
|
|
|
|
2019-06-12 04:33:57 -04:00
|
|
|
int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
|
|
|
|
if (listen_sock < 0) {
|
|
|
|
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 03:17:56 -05:00
|
|
|
int opt = 1;
|
|
|
|
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
2020-03-17 15:30:33 -04:00
|
|
|
#if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
|
|
|
|
// Note that by default IPV6 binds to both protocols, it is must be disabled
|
|
|
|
// if both protocols used at the same time (used in CI)
|
|
|
|
setsockopt(listen_sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
|
|
|
|
#endif
|
|
|
|
|
2019-06-12 04:33:57 -04:00
|
|
|
ESP_LOGI(TAG, "Socket created");
|
2018-10-02 10:33:16 -04:00
|
|
|
|
2019-06-12 04:33:57 -04:00
|
|
|
int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
|
|
|
if (err != 0) {
|
|
|
|
ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
|
2020-03-17 15:30:33 -04:00
|
|
|
ESP_LOGE(TAG, "IPPROTO: %d", addr_family);
|
2019-06-12 04:33:57 -04:00
|
|
|
goto CLEAN_UP;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Socket bound, port %d", PORT);
|
|
|
|
|
|
|
|
err = listen(listen_sock, 1);
|
|
|
|
if (err != 0) {
|
|
|
|
ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);
|
|
|
|
goto CLEAN_UP;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2018-10-02 10:33:16 -04:00
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Socket listening");
|
|
|
|
|
2020-11-12 06:41:16 -05:00
|
|
|
struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
|
2020-12-14 22:00:02 -05:00
|
|
|
socklen_t addr_len = sizeof(source_addr);
|
2018-11-20 11:41:08 -05:00
|
|
|
int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);
|
2018-10-02 10:33:16 -04:00
|
|
|
if (sock < 0) {
|
|
|
|
ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-02 22:27:06 -05:00
|
|
|
// Set tcp keepalive option
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int));
|
|
|
|
setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(int));
|
|
|
|
setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int));
|
|
|
|
setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int));
|
2019-06-12 04:33:57 -04:00
|
|
|
// Convert ip address to string
|
2020-11-12 06:41:16 -05:00
|
|
|
if (source_addr.ss_family == PF_INET) {
|
|
|
|
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
|
2021-01-26 03:17:56 -05:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_EXAMPLE_IPV6
|
|
|
|
else if (source_addr.ss_family == PF_INET6) {
|
2020-11-12 06:41:16 -05:00
|
|
|
inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
|
2018-10-02 10:33:16 -04:00
|
|
|
}
|
2021-01-26 03:17:56 -05:00
|
|
|
#endif
|
2019-06-12 04:33:57 -04:00
|
|
|
ESP_LOGI(TAG, "Socket accepted ip address: %s", addr_str);
|
2018-10-02 10:33:16 -04:00
|
|
|
|
2019-06-12 04:33:57 -04:00
|
|
|
do_retransmit(sock);
|
|
|
|
|
|
|
|
shutdown(sock, 0);
|
|
|
|
close(sock);
|
2018-10-02 10:33:16 -04:00
|
|
|
}
|
2019-06-12 04:33:57 -04:00
|
|
|
|
|
|
|
CLEAN_UP:
|
|
|
|
close(listen_sock);
|
2018-10-02 10:33:16 -04:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-10-02 10:33:16 -04:00
|
|
|
{
|
2018-11-20 11:41:08 -05:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 11:41:08 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
2018-10-02 10:33:16 -04:00
|
|
|
|
2020-03-17 15:30:33 -04:00
|
|
|
#ifdef CONFIG_EXAMPLE_IPV4
|
|
|
|
xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLE_IPV6
|
|
|
|
xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET6, 5, NULL);
|
|
|
|
#endif
|
2018-10-02 10:33:16 -04:00
|
|
|
}
|