UdpSender now supports simple ip types too

This commit is contained in:
2022-01-26 22:51:17 +01:00
parent 25bb86f9eb
commit bced0e2852
2 changed files with 60 additions and 0 deletions

View File

@@ -95,4 +95,62 @@ tl::expected<void, std::string> UdpSender::send(const struct sockaddr_in6 &recip
return {}; return {};
} }
tl::expected<void, std::string> UdpSender::send(ip_addr_t ip, uint16_t port, std::string_view buf)
{
switch (ip.type)
{
case IPADDR_TYPE_V4:
{
struct sockaddr_in recipient;
recipient.sin_family = AF_INET;
recipient.sin_addr.s_addr = ip.u_addr.ip4.addr;
recipient.sin_port = htons(port);
return send(recipient, buf);
}
case IPADDR_TYPE_V6:
{
struct sockaddr_in6 recipient;
recipient.sin6_family = AF_INET6;
recipient.sin6_addr.un.u32_addr[0] = ip.u_addr.ip6.addr[0];
recipient.sin6_addr.un.u32_addr[1] = ip.u_addr.ip6.addr[1];
recipient.sin6_addr.un.u32_addr[2] = ip.u_addr.ip6.addr[2];
recipient.sin6_addr.un.u32_addr[3] = ip.u_addr.ip6.addr[3];
//recipient.sin6_scope_id = ip.u_addr.ip6.zone;
recipient.sin6_port = htons(port);
return send(recipient, buf);
}
default:
return tl::make_unexpected(fmt::format("unsupported ip type {}", ip.type));
}
}
tl::expected<void, std::string> UdpSender::send(esp_ip_addr_t ip, uint16_t port, std::string_view buf)
{
switch (ip.type)
{
case ESP_IPADDR_TYPE_V4:
{
struct sockaddr_in recipient;
recipient.sin_family = AF_INET;
recipient.sin_addr.s_addr = ip.u_addr.ip4.addr;
recipient.sin_port = htons(port);
return send(recipient, buf);
}
case ESP_IPADDR_TYPE_V6:
{
struct sockaddr_in6 recipient;
recipient.sin6_family = AF_INET6;
recipient.sin6_addr.un.u32_addr[0] = ip.u_addr.ip6.addr[0];
recipient.sin6_addr.un.u32_addr[1] = ip.u_addr.ip6.addr[1];
recipient.sin6_addr.un.u32_addr[2] = ip.u_addr.ip6.addr[2];
recipient.sin6_addr.un.u32_addr[3] = ip.u_addr.ip6.addr[3];
//recipient.sin6_scope_id = ip.u_addr.ip6.zone;
recipient.sin6_port = htons(port);
return send(recipient, buf);
}
default:
return tl::make_unexpected(fmt::format("unsupported ip type {}", ip.type));
}
}
} // namespace wifi_stack } // namespace wifi_stack

View File

@@ -28,6 +28,8 @@ public:
tl::expected<void, std::string> send(const esp_netif_ip_info_t &ip, uint16_t port, std::string_view buf); tl::expected<void, std::string> send(const esp_netif_ip_info_t &ip, uint16_t port, std::string_view buf);
tl::expected<void, std::string> send(const struct sockaddr_in &recipient, std::string_view buf); tl::expected<void, std::string> send(const struct sockaddr_in &recipient, std::string_view buf);
tl::expected<void, std::string> send(const struct sockaddr_in6 &recipient, std::string_view buf); tl::expected<void, std::string> send(const struct sockaddr_in6 &recipient, std::string_view buf);
tl::expected<void, std::string> send(ip_addr_t ip, uint16_t port, std::string_view buf);
tl::expected<void, std::string> send(esp_ip_addr_t ip, uint16_t port, std::string_view buf);
private: private:
const int m_udp_server; const int m_udp_server;