ESPHome  2024.9.2
socket.cpp
Go to the documentation of this file.
1 #include "socket.h"
2 #if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
3 #include <cerrno>
4 #include <cstring>
5 #include <string>
6 #include "esphome/core/log.h"
7 
8 namespace esphome {
9 namespace socket {
10 
12 
13 std::unique_ptr<Socket> socket_ip(int type, int protocol) {
14 #if USE_NETWORK_IPV6
15  return socket(AF_INET6, type, protocol);
16 #else
17  return socket(AF_INET, type, protocol);
18 #endif /* USE_NETWORK_IPV6 */
19 }
20 
21 socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port) {
22 #if USE_NETWORK_IPV6
23  if (ip_address.find(':') != std::string::npos) {
24  if (addrlen < sizeof(sockaddr_in6)) {
25  errno = EINVAL;
26  return 0;
27  }
28  auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
29  memset(server, 0, sizeof(sockaddr_in6));
30  server->sin6_family = AF_INET6;
31  server->sin6_port = htons(port);
32 
33  ip6_addr_t ip6;
34  inet6_aton(ip_address.c_str(), &ip6);
35  memcpy(server->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
36  return sizeof(sockaddr_in6);
37  }
38 #endif /* USE_NETWORK_IPV6 */
39  if (addrlen < sizeof(sockaddr_in)) {
40  errno = EINVAL;
41  return 0;
42  }
43  auto *server = reinterpret_cast<sockaddr_in *>(addr);
44  memset(server, 0, sizeof(sockaddr_in));
45  server->sin_family = AF_INET;
46  server->sin_addr.s_addr = inet_addr(ip_address.c_str());
47  server->sin_port = htons(port);
48  return sizeof(sockaddr_in);
49 }
50 
51 socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {
52 #if USE_NETWORK_IPV6
53  if (addrlen < sizeof(sockaddr_in6)) {
54  errno = EINVAL;
55  return 0;
56  }
57  auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
58  memset(server, 0, sizeof(sockaddr_in6));
59  server->sin6_family = AF_INET6;
60  server->sin6_port = htons(port);
61  server->sin6_addr = IN6ADDR_ANY_INIT;
62  return sizeof(sockaddr_in6);
63 #else
64  if (addrlen < sizeof(sockaddr_in)) {
65  errno = EINVAL;
66  return 0;
67  }
68  auto *server = reinterpret_cast<sockaddr_in *>(addr);
69  memset(server, 0, sizeof(sockaddr_in));
70  server->sin_family = AF_INET;
71  server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
72  server->sin_port = htons(port);
73  return sizeof(sockaddr_in);
74 #endif /* USE_NETWORK_IPV6 */
75 }
76 } // namespace socket
77 } // namespace esphome
78 #endif
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:13
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:51
uint32_t socklen_t
Definition: headers.h:97
uint8_t type
Implementation of SPI Controller mode.
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:21
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.