udp_server.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_LWIP_NETBUF_RECVINFO) && !defined(CONFIG_EXAMPLE_IPV6)
  51. int enable = 1;
  52. lwip_setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &enable, sizeof(enable));
  53. #endif
  54. #if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
  55. if (addr_family == AF_INET6) {
  56. // Note that by default IPV6 binds to both protocols, it is must be disabled
  57. // if both protocols used at the same time (used in CI)
  58. int opt = 1;
  59. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  60. setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
  61. }
  62. #endif
  63. // Set timeout
  64. struct timeval timeout;
  65. timeout.tv_sec = 10;
  66. timeout.tv_usec = 0;
  67. setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
  68. int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  69. if (err < 0) {
  70. ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
  71. }
  72. ESP_LOGI(TAG, "Socket bound, port %d", PORT);
  73. struct sockaddr_storage source_addr; // Large enough for both IPv4 or IPv6
  74. socklen_t socklen = sizeof(source_addr);
  75. #if defined(CONFIG_LWIP_NETBUF_RECVINFO) && !defined(CONFIG_EXAMPLE_IPV6)
  76. struct iovec iov;
  77. struct msghdr msg;
  78. struct cmsghdr *cmsgtmp;
  79. u8_t cmsg_buf[CMSG_SPACE(sizeof(struct in_pktinfo))];
  80. iov.iov_base = rx_buffer;
  81. iov.iov_len = sizeof(rx_buffer);
  82. msg.msg_control = cmsg_buf;
  83. msg.msg_controllen = sizeof(cmsg_buf);
  84. msg.msg_flags = 0;
  85. msg.msg_iov = &iov;
  86. msg.msg_iovlen = 1;
  87. msg.msg_name = (struct sockaddr *)&source_addr;
  88. msg.msg_namelen = socklen;
  89. #endif
  90. while (1) {
  91. ESP_LOGI(TAG, "Waiting for data");
  92. #if defined(CONFIG_LWIP_NETBUF_RECVINFO) && !defined(CONFIG_EXAMPLE_IPV6)
  93. int len = recvmsg(sock, &msg, 0);
  94. #else
  95. int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
  96. #endif
  97. // Error occurred during receiving
  98. if (len < 0) {
  99. ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
  100. break;
  101. }
  102. // Data received
  103. else {
  104. // Get the sender's ip address as string
  105. if (source_addr.ss_family == PF_INET) {
  106. inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);
  107. #if defined(CONFIG_LWIP_NETBUF_RECVINFO) && !defined(CONFIG_EXAMPLE_IPV6)
  108. for ( cmsgtmp = CMSG_FIRSTHDR(&msg); cmsgtmp != NULL; cmsgtmp = CMSG_NXTHDR(&msg, cmsgtmp) ) {
  109. if ( cmsgtmp->cmsg_level == IPPROTO_IP && cmsgtmp->cmsg_type == IP_PKTINFO ) {
  110. struct in_pktinfo *pktinfo;
  111. pktinfo = (struct in_pktinfo*)CMSG_DATA(cmsgtmp);
  112. ESP_LOGI(TAG, "dest ip: %s", inet_ntoa(pktinfo->ipi_addr));
  113. }
  114. }
  115. #endif
  116. } else if (source_addr.ss_family == PF_INET6) {
  117. inet6_ntoa_r(((struct sockaddr_in6 *)&source_addr)->sin6_addr, addr_str, sizeof(addr_str) - 1);
  118. }
  119. rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...
  120. ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
  121. ESP_LOGI(TAG, "%s", rx_buffer);
  122. int err = sendto(sock, rx_buffer, len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr));
  123. if (err < 0) {
  124. ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  125. break;
  126. }
  127. }
  128. }
  129. if (sock != -1) {
  130. ESP_LOGE(TAG, "Shutting down socket and restarting...");
  131. shutdown(sock, 0);
  132. close(sock);
  133. }
  134. }
  135. vTaskDelete(NULL);
  136. }
  137. void app_main(void)
  138. {
  139. ESP_ERROR_CHECK(nvs_flash_init());
  140. ESP_ERROR_CHECK(esp_netif_init());
  141. ESP_ERROR_CHECK(esp_event_loop_create_default());
  142. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
  143. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  144. * examples/protocols/README.md for more information about this function.
  145. */
  146. ESP_ERROR_CHECK(example_connect());
  147. #ifdef CONFIG_EXAMPLE_IPV4
  148. xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET, 5, NULL);
  149. #endif
  150. #ifdef CONFIG_EXAMPLE_IPV6
  151. xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET6, 5, NULL);
  152. #endif
  153. }