Skip to content

Commit

Permalink
msg: Fix Windows IPv6 support
Browse files Browse the repository at this point in the history
The Windows AF_INET6 definition doesn't match the Linux one, thus
sockaddr structures must be converted when being sent over the wire.
Otherwise, Linux hosts will not be able to recognize the address
family and reject Windows client connections:

  handle_client_ident peer is trying to reach
  v2:(unrecognized address family 23)/0 which is not us

Note that this isn't the case with AF_INET, which has the same
definition on both platforms.

Signed-off-by: Lucian Petrut <[email protected]>
  • Loading branch information
petrutlucian94 committed Nov 19, 2021
1 parent 93054a3 commit 475e7a6
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/msg/msg_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

#define MAX_PORT_NUMBER 65535

#ifdef _WIN32
// ceph_sockaddr_storage matches the Linux format.
#define AF_INET6_LINUX 10
#endif

namespace ceph {
class Formatter;
}
Expand Down Expand Up @@ -161,6 +166,14 @@ static inline void encode(const sockaddr_storage& a, ceph::buffer::list& bl) {
(unsigned char*)(&ss + 1) - dst);
::memcpy(dst, src, copy_size);
encode(ss, bl);
#elif defined(_WIN32)
ceph_sockaddr_storage ss{};
::memcpy(&ss, &a, std::min(sizeof(ss), sizeof(a)));
// The Windows AF_INET6 definition doesn't match the Linux one.
if (a.ss_family == AF_INET6) {
ss.ss_family = AF_INET6_LINUX;
}
encode(ss, bl);
#else
ceph_sockaddr_storage ss;
::memset(&ss, '\0', sizeof(ss));
Expand All @@ -186,6 +199,13 @@ static inline void decode(sockaddr_storage& a,
auto const copy_size = std::min((unsigned char*)(&ss + 1) - src,
(unsigned char*)(&a + 1) - dst);
::memcpy(dst, src, copy_size);
#elif defined(_WIN32)
ceph_sockaddr_storage ss{};
decode(ss, bl);
::memcpy(&a, &ss, std::min(sizeof(ss), sizeof(a)));
if (a.ss_family == AF_INET6_LINUX) {
a.ss_family = AF_INET6;
}
#else
ceph_sockaddr_storage ss{};
decode(ss, bl);
Expand Down Expand Up @@ -463,7 +483,11 @@ struct entity_addr_t {
encode(elen, bl);
if (elen) {
uint16_t ss_family = u.sa.sa_family;

#if defined(_WIN32)
if (ss_family == AF_INET6) {
ss_family = AF_INET6_LINUX;
}
#endif
encode(ss_family, bl);
elen -= sizeof(u.sa.sa_family);
bl.append(u.sa.sa_data, elen);
Expand Down Expand Up @@ -494,6 +518,11 @@ struct entity_addr_t {
throw ceph::buffer::malformed_input("elen smaller than family len");
}
decode(ss_family, bl);
#if defined(_WIN32)
if (ss_family == AF_INET6_LINUX) {
ss_family = AF_INET6;
}
#endif
u.sa.sa_family = ss_family;
elen -= sizeof(ss_family);
if (elen > get_sockaddr_len() - sizeof(u.sa.sa_family)) {
Expand Down

0 comments on commit 475e7a6

Please sign in to comment.