Page 1 of 1

Example to TCP/IP SSL

Posted: Wed Mar 13, 2024 2:41 pm
by maxwellesp32
Hi,

i have a problem with the Code below, i want a TCP-IP SSL Listener that connect to a Server (Same IP 127.0.0.1 and PORT 3333) The Server are a Python programm, my probleme are with the Python Listener or the QModbusmaster programm the connection with the Server are succesful but with my esp Programm the connection are not possible and the code close the socket.

At the second state of my code i want a SSL-Cryptographie with the IP and the Port but i have no idee how its possible, i know i need a x509 Certificate but the code structure of the Listener
  1. /*
  2.  * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3.  *
  4.  * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5.  */
  6. #include "sdkconfig.h"
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/socket.h>
  10. #include <errno.h>
  11. #include <netdb.h>            // struct addrinfo
  12. #include <arpa/inet.h>
  13. #include "esp_netif.h"
  14. #include "esp_log.h"
  15. #if defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  16. #include "addr_from_stdin.h"
  17. #endif
  18.  
  19. #if defined(CONFIG_EXAMPLE_IPV4)
  20. #define HOST_IP_ADDR CONFIG_EXAMPLE_IPV4_ADDR
  21. #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  22. #define HOST_IP_ADDR ""
  23. #endif
  24.  
  25. #define PORT CONFIG_EXAMPLE_PORT
  26.  
  27. static const char *TAG = "example";
  28. static const char *payload = "Message from ESP32 ";
  29.  
  30.  
  31. void tcp_client(void)
  32. {
  33.     char rx_buffer[128];
  34.     char host_ip[] = HOST_IP_ADDR;
  35.     int addr_family = 0;
  36.     int ip_protocol = 0;
  37.  
  38.     while (1) {
  39. #if defined(CONFIG_EXAMPLE_IPV4)
  40.         struct sockaddr_in dest_addr;
  41.         inet_pton(AF_INET, host_ip, &dest_addr.sin_addr);
  42.         dest_addr.sin_family = AF_INET;
  43.         dest_addr.sin_port = htons(PORT);
  44.         addr_family = AF_INET;
  45.         ip_protocol = IPPROTO_IP;
  46. #elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
  47.         struct sockaddr_storage dest_addr = { 0 };
  48.         ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_STREAM, &ip_protocol, &addr_family, &dest_addr));
  49. #endif
  50.  
  51.         int sock =  socket(addr_family, SOCK_STREAM, ip_protocol);
  52.         if (sock < 0) {
  53.             ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
  54.             break;
  55.         }
  56.         ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, PORT);
  57.  
  58.         int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  59.         if (err != 0) {
  60.             ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
  61.             break;
  62.         }
  63.         ESP_LOGI(TAG, "Successfully connected");
  64.  
  65.         while (1) {
  66.             int err = send(sock, payload, strlen(payload), 0);
  67.             if (err < 0) {
  68.                 ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
  69.                 break;
  70.             }
  71.  
  72.             int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
  73.             // Error occurred during receiving
  74.             if (len < 0) {
  75.                 ESP_LOGE(TAG, "recv failed: errno %d", errno);
  76.                 break;
  77.             }
  78.             // Data received
  79.             else {
  80.                 rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
  81.                 ESP_LOGI(TAG, "Received %d bytes from %s:", len, host_ip);
  82.                 ESP_LOGI(TAG, "%s", rx_buffer);
  83.             }
  84.         }
  85.  
  86.         if (sock != -1) {
  87.             ESP_LOGE(TAG, "Shutting down socket and restarting...");
  88.             shutdown(sock, 0);
  89.             close(sock);
  90.         }
  91.     }
  92. }

and now the python code
  1. import socket
  2. import ssl
  3.  
  4. SERVER_IP = "127.0.0.1"  # IP-Adresse des ESP32-Servers
  5. SERVER_PORT = 3333
  6.  
  7.     context = ssl.create_default_context()
  8.     with socket.create_connection((SERVER_IP, SERVER_PORT)) as sock:
  9.         with context.wrap_socket(sock, server_hostname=SERVER_IP) as ssock:
  10.             # Daten senden
  11.             message = "Hello from Python!"
  12.             ssock.sendall(message.encode())
  13.  
  14.             # Antwort vom Server empfangen
  15.             response = ssock.recv(1024)
  16.             print("Received:", response.decode())
  17.  

Re: Example to TCP/IP SSL

Posted: Thu Mar 14, 2024 9:54 am
by MicroController
You're using the wrong IP address. 127.0.0.1 is 'localhost', i.e. a loopback address of the machine the code is running on, it is local to and only reachable from that machine itself. To connect from an ESP to a server, you need to use that server's network IP address.