ARTICLE AD BOX
I've got a class UdpPeer with setters:
class UdpPeer { struct sockaddr_storage addr; socklen_t; public: void setAddr(struct sockaddr_storage const& addr, const socklen_t addrlen); void setAddr(std::string const& addr);Is there any way in the second method (with std::string arg) to determine the IP address family contained in string?
4

By "IP address family" do you mean IPv4 versus IPv6 addresses?
2026-05-19 16:38:16 +00:00
Commented 15 hours ago
What's the socklen_t; member supposed to be? Sidenote: By the looks of it, addr should be a std::variant<sockaddr_in, sockaddr_in6, sockaddr_un> instead of a sockaddr_storage. I doubt you'd need the socklen_t if you stored the actual sockaddr_* in a std::variant.
2026-05-19 16:52:01 +00:00
Commented 15 hours ago
@TedLyngmo: AFAIK sockaddr_un as declared is often smaller than the actual size of the socket path supported by the system (I think it is a VLA if I got my terminology correct?) and only sockaddr_storage is guaranteed to be "large enough to cover any sockaddr supported by the system", and on top of that, the presence or absence of \x00 padding may be significant in at least Linux 'abstract' socket addresses so the length needs to be known...
2026-05-19 18:04:48 +00:00
Commented 13 hours ago
@grawity Oh that'd make it a flexible array then. Ok, that's solvable by adding a sockaddr_storage to the std::variant without using it.
2026-05-19 19:45:53 +00:00
Commented 12 hours ago