ESPHome  2024.5.0
socket.cpp
Go to the documentation of this file.
1 #include "socket.h"
2 #include <cerrno>
3 #include <cstring>
4 #include <string>
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace socket {
9 
11 
12 std::unique_ptr<Socket> socket_ip(int type, int protocol) {
13 #if USE_NETWORK_IPV6
14  return socket(AF_INET6, type, protocol);
15 #else
16  return socket(AF_INET, type, protocol);
17 #endif /* USE_NETWORK_IPV6 */
18 }
19 
20 socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port) {
21 #if USE_NETWORK_IPV6
22  if (addrlen < sizeof(sockaddr_in6)) {
23  errno = EINVAL;
24  return 0;
25  }
26  auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
27  memset(server, 0, sizeof(sockaddr_in6));
28  server->sin6_family = AF_INET6;
29  server->sin6_port = htons(port);
30 
31  if (ip_address.find('.') != std::string::npos) {
32  server->sin6_addr.un.u32_addr[3] = inet_addr(ip_address.c_str());
33  } else {
34  ip6_addr_t ip6;
35  inet6_aton(ip_address.c_str(), &ip6);
36  memcpy(server->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
37  }
38  return sizeof(sockaddr_in6);
39 #else
40  if (addrlen < sizeof(sockaddr_in)) {
41  errno = EINVAL;
42  return 0;
43  }
44  auto *server = reinterpret_cast<sockaddr_in *>(addr);
45  memset(server, 0, sizeof(sockaddr_in));
46  server->sin_family = AF_INET;
47  server->sin_addr.s_addr = inet_addr(ip_address.c_str());
48  server->sin_port = htons(port);
49  return sizeof(sockaddr_in);
50 #endif /* USE_NETWORK_IPV6 */
51 }
52 
53 socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {
54 #if USE_NETWORK_IPV6
55  if (addrlen < sizeof(sockaddr_in6)) {
56  errno = EINVAL;
57  return 0;
58  }
59  auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
60  memset(server, 0, sizeof(sockaddr_in6));
61  server->sin6_family = AF_INET6;
62  server->sin6_port = htons(port);
63  server->sin6_addr = IN6ADDR_ANY_INIT;
64  return sizeof(sockaddr_in6);
65 #else
66  if (addrlen < sizeof(sockaddr_in)) {
67  errno = EINVAL;
68  return 0;
69  }
70  auto *server = reinterpret_cast<sockaddr_in *>(addr);
71  memset(server, 0, sizeof(sockaddr_in));
72  server->sin_family = AF_INET;
73  server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
74  server->sin_port = htons(port);
75  return sizeof(sockaddr_in);
76 #endif /* USE_NETWORK_IPV6 */
77 }
78 } // namespace socket
79 } // namespace esphome
std::unique_ptr< Socket > socket_ip(int type, int protocol)
Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol...
Definition: socket.cpp:12
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
Definition: socket.cpp:53
uint32_t socklen_t
Definition: headers.h:97
uint8_t type
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port)
Set a sockaddr to the specified address and port for the IP version used by socket_ip().
Definition: socket.cpp:20
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.