udp_server.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* BSD Socket API Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <string.h>
  8. #include <sys/param.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_system.h"
  12. #include "esp_wifi.h"
  13. #include "esp_event.h"
  14. #include "esp_log.h"
  15. #include "nvs_flash.h"
  16. #include "esp_netif.h"
  17. #include "protocol_examples_common.h"
  18. #include "lwip/err.h"
  19. #include "lwip/sockets.h"
  20. #include "lwip/sys.h"
  21. #include <lwip/netdb.h>
  22. #define PORT CONFIG_EXAMPLE_PORT
  23. static const char *TAG = "example";
  24. static void udp_server_task(void *pvParameters)
  25. {
  26. char rx_buffer[128];
  27. char addr_str[128];
  28. int addr_family = (int)pvParameters;
  29. int ip_protocol = 0;
  30. struct sockaddr_in6 dest_addr;
  31. while (1) {
  32. if (addr_family == AF_INET) {
  33. struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
  34. dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
  35. dest_addr_ip4->sin_family = AF_INET;
  36. dest_addr_ip4->sin_port = htons(PORT);
  37. ip_protocol = IPPROTO_IP;
  38. } else if (addr_family == AF_INET6) {
  39. bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
  40. dest_addr.sin6_family = AF_INET6;
  41. dest_addr.sin6_port = htons(PORT);
  42. ip_protocol = IPPROTO_IPV6;
  43. }
  44. int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
  45. if (sock < 0) {
  46. ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  47. break;
  48. }
  49. ESP_LOGI(TAG, "Socket created");
  50. #if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
  51. if (addr_family == AF_INET6) {
  52. // Note that by default IPV6 binds to both protocols, it is must be disabled
  53. // if both protocols used at the same time (used in CI)
  54. int opt = 1;
  55. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  56. setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
  57. }
  58. #endif
  59. int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  60. if (err < 0) {
  61. ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  62. }
  63. ESP_LOGI(TAG, "Socket bound, port %d", PORT);
  64. while (1) {
  65. ESP_LOGI(TAG, "Waiting for data");
  66. struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
  67. socklen_t socklen = sizeof(source_addr);
  68. int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
  69. // Error occurred during receiving
  70. if (len < 0) {
  71. ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
  72. break;
  73. }
  74. // Data received
  75. else {
  76. // Get the sender's ip address as string
  77. if (source_addr.ss_family == PF_INET) {
  78. inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
  79. } else if (source_addr.ss_family == PF_INET6) {
  80. inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
  81. }
  82. rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...
  83. ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
  84. ESP_LOGI(TAG, "%s", rx_buffer);
  85. int err = sendto(sock, rx_buffer, len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr));
  86. if (err < 0) {
  87. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  88. break;
  89. }
  90. }
  91. }
  92. if (sock != -1) {
  93. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  94. shutdown(sock, 0);
  95. close(sock);
  96. }
  97. }
  98. vTaskDelete(NULL);
  99. }
  100. void app_main(void)
  101. {
  102. ESP_ERROR_CHECK(nvs_flash_init());
  103. ESP_ERROR_CHECK(esp_netif_init());
  104. ESP_ERROR_CHECK(esp_event_loop_create_default());
  105. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  106. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  107. * examples/protocols/README.md for more information about this function.
  108. */
  109. ESP_ERROR_CHECK(example_connect());
  110. #ifdef CONFIG_EXAMPLE_IPV4
  111. xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET, 5, NULL);
  112. #endif
  113. #ifdef CONFIG_EXAMPLE_IPV6
  114. xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET6, 5, NULL);
  115. #endif
  116. }