From eee98c17e51bc1ec4a9831f2b222b8a805788e6b Mon Sep 17 00:00:00 2001 From: mrabine Date: Thu, 27 Aug 2026 22:29:59 +0200 Subject: [PATCH 1/4] Cleanup socket API --- core/include/join/async_datagram_socket.hpp | 75 ------------------- core/include/join/async_socket.hpp | 11 +-- core/include/join/datagram_socket.hpp | 31 +++----- core/include/join/socket.hpp | 36 +-------- core/include/join/stream_socket.hpp | 8 +- core/include/join/utils.hpp | 32 ++++++++ .../tests/icmp_async_datagram_socket_test.cpp | 2 +- core/tests/icmp_socket_test.cpp | 12 +-- core/tests/ip_address_test.cpp | 43 +++++++++-- core/tests/mac_address_test.cpp | 55 +++++++++++++- core/tests/raw_async_socket_test.cpp | 4 +- core/tests/raw_socket_test.cpp | 14 +--- core/tests/tcp_socket_test.cpp | 10 --- core/tests/udp_async_datagram_socket_test.cpp | 5 +- core/tests/udp_socket_test.cpp | 21 +++--- .../tests/unix_async_datagram_socket_test.cpp | 5 +- core/tests/unix_datagram_socket_test.cpp | 28 ++++--- core/tests/unix_stream_acceptor_test.cpp | 7 +- core/tests/unix_stream_socket_test.cpp | 22 +++--- core/tests/utils_test.cpp | 12 +++ crypto/include/join/dtls_wrapper.hpp | 4 +- crypto/include/join/tls.hpp | 12 +-- fabric/include/join/dhcp.hpp | 6 +- fabric/include/join/resolver.hpp | 10 +-- fabric/src/ping.cpp | 4 +- fabric/tests/dhcp_test.cpp | 2 +- fabric/tests/netlink_socket_test.cpp | 10 --- 27 files changed, 229 insertions(+), 252 deletions(-) diff --git a/core/include/join/async_datagram_socket.hpp b/core/include/join/async_datagram_socket.hpp index db9fe603..753db866 100644 --- a/core/include/join/async_datagram_socket.hpp +++ b/core/include/join/async_datagram_socket.hpp @@ -276,81 +276,6 @@ namespace join { return this->_socket.ttl (); } - - protected: - /** - * @brief method called when an operation completes. - * @param op completed operation. - * @param result number of bytes transferred, or operation specific value. - */ - void onComplete (IoOperation* op, int result) override - { - dispatch (op, (result < 0) ? std::error_code (-result, std::generic_category ()) : std::error_code (), - (result > 0) ? static_cast (result) : 0); - } - - /** - * @brief method called when an operation is cancelled. - * @param op cancelled operation. - * @param result negative errno. - */ - void onCancel (IoOperation* op, [[maybe_unused]] int result) override - { - dispatch (op, make_error_code (std::errc::operation_canceled), 0); - } - - /** - * @brief invoke the handler owning the given operation slot. - * @param op completed or cancelled operation. - * @param code error code to report. - * @param size number of bytes transferred. - */ - void dispatch (IoOperation* op, const std::error_code& code, size_t size) noexcept - { - if (op == &this->_ops->read.op) - { - this->_ops->read.state.store (AsyncOperation::Dispatching, std::memory_order_release); - - ReadHandler handler = std::move (this->_onRead); - std::error_code result = code; - - if (JOIN_LIKELY (!result)) - { - if (JOIN_UNLIKELY (size == 0)) - { - result = make_error_code (Errc::ConnectionClosed); - this->_socket._state = Socket::Disconnected; - } - else if (JOIN_UNLIKELY (this->_ops->readMsg.msg_flags & MSG_TRUNC)) - { - result = make_error_code (Errc::MessageTooLong); - } - } - - if (JOIN_LIKELY (handler)) - { - handler (result, size); - } - - AsyncOperation::State expected = AsyncOperation::Dispatching; - this->_ops->read.state.compare_exchange_strong (expected, AsyncOperation::Idle, - std::memory_order_release, std::memory_order_relaxed); - return; - } - - this->_ops->write.state.store (AsyncOperation::Dispatching, std::memory_order_release); - - WriteHandler handler = std::move (this->_onWrite); - - if (JOIN_LIKELY (handler)) - { - handler (code, size); - } - - AsyncOperation::State expected = AsyncOperation::Dispatching; - this->_ops->write.state.compare_exchange_strong (expected, AsyncOperation::Idle, std::memory_order_release, - std::memory_order_relaxed); - } }; } diff --git a/core/include/join/async_socket.hpp b/core/include/join/async_socket.hpp index 2190f291..52f82778 100644 --- a/core/include/join/async_socket.hpp +++ b/core/include/join/async_socket.hpp @@ -547,16 +547,9 @@ namespace join ReadHandler handler = std::move (_onRead); std::error_code result = code; - if (JOIN_LIKELY (!result)) + if (JOIN_UNLIKELY (!result && (_ops->readMsg.msg_flags & MSG_TRUNC))) { - if (JOIN_UNLIKELY (size == 0)) - { - result = make_error_code (Errc::ConnectionClosed); // LCOV_EXCL_LINE - } - else if (JOIN_UNLIKELY (_ops->readMsg.msg_flags & MSG_TRUNC)) - { - result = make_error_code (Errc::MessageTooLong); - } + result = make_error_code (Errc::MessageTooLong); } if (JOIN_LIKELY (handler)) diff --git a/core/include/join/datagram_socket.hpp b/core/include/join/datagram_socket.hpp index b6eed432..b10ea4bd 100644 --- a/core/include/join/datagram_socket.hpp +++ b/core/include/join/datagram_socket.hpp @@ -39,10 +39,6 @@ namespace join template class BasicDatagramSocket final : public BasicSocket { - /// friendship with basic asynchronous datagram socket - template - friend class BasicAsyncDatagramSocket; - public: using Ptr = std::unique_ptr>; using Mode = typename BasicSocket::Mode; @@ -145,9 +141,11 @@ namespace join if ((protocol.family () == AF_INET6) && (::setsockopt (this->_handle, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof (off)) == -1)) { + // LCOV_EXCL_START lastError = std::error_code (errno, std::generic_category ()); close (); return -1; + // LCOV_EXCL_STOP } } @@ -156,9 +154,11 @@ namespace join if ((protocol.family () == AF_INET) && (::setsockopt (this->_handle, IPPROTO_IP, IP_HDRINCL, &off, sizeof (off)) == -1)) { + // LCOV_EXCL_START lastError = std::error_code (errno, std::generic_category ()); close (); return -1; + // LCOV_EXCL_STOP } this->setOption (Option::MulticastTtl, _ttl); @@ -185,7 +185,7 @@ namespace join if ((this->_state == State::Closed) && (open (endpoint.protocol ()) == -1)) { - return -1; + return -1; // LCOV_EXCL_LINE } if (::connect (this->_handle, endpoint.addr (), endpoint.length ()) == -1) @@ -218,11 +218,13 @@ namespace join sizeof (struct sockaddr_storage)); if (result == -1) { + // LCOV_EXCL_START if (errno != EAFNOSUPPORT) { lastError = std::error_code (errno, std::generic_category ()); return -1; } + // LCOV_EXCL_STOP } this->_state = State::Disconnected; @@ -248,7 +250,7 @@ namespace join * @param endpoint endpoint from where data are coming (optional). * @return The number of bytes received, -1 on failure. */ - int readFrom (char* data, unsigned long maxSize, Endpoint* endpoint = nullptr) noexcept + int readFrom (char* data, size_t maxSize, Endpoint* endpoint = nullptr) noexcept { struct sockaddr_storage sa; @@ -265,18 +267,9 @@ namespace join message.msg_controllen = 0; int size = ::recvmsg (this->_handle, &message, 0); - if (size < 1) + if (size == -1) { - if (size == -1) - { - lastError = std::error_code (errno, std::generic_category ()); - } - else - { - lastError = make_error_code (Errc::ConnectionClosed); - this->_state = State::Disconnected; - } - + lastError = std::error_code (errno, std::generic_category ()); return -1; } @@ -301,11 +294,11 @@ namespace join * @param endpoint endpoint where to write the data. * @return the number of bytes written, -1 on failure. */ - int writeTo (const char* data, unsigned long maxSize, const Endpoint& endpoint) noexcept + int writeTo (const char* data, size_t maxSize, const Endpoint& endpoint) noexcept { if ((this->_state == State::Closed) && (open (endpoint.protocol ()) == -1)) { - return -1; + return -1; // LCOV_EXCL_LINE } int result = ::sendto (this->_handle, data, maxSize, 0, endpoint.addr (), endpoint.length ()); diff --git a/core/include/join/socket.hpp b/core/include/join/socket.hpp index c48b90d3..94050e35 100644 --- a/core/include/join/socket.hpp +++ b/core/include/join/socket.hpp @@ -332,7 +332,7 @@ namespace join * @param maxSize maximum number of bytes to read. * @return the number of bytes received, -1 on failure. */ - int read (char* data, unsigned long maxSize) noexcept + int read (char* data, size_t maxSize) noexcept { struct iovec iov; iov.iov_base = data; @@ -386,7 +386,7 @@ namespace join * @param maxSize maximum number of bytes to write. * @return the number of bytes written, -1 on failure. */ - int write (const char* data, unsigned long maxSize) noexcept + int write (const char* data, size_t maxSize) noexcept { struct iovec iov; iov.iov_base = const_cast (data); @@ -629,38 +629,6 @@ namespace join return _handle; } - /** - * @brief get standard 1s complement checksum. - * @param data data pointer. - * @param len data len. - * @param current Current sum. - * @return checksum. - */ - static uint16_t checksum (const uint16_t* data, size_t len, uint16_t current = 0) - { - uint32_t sum = current; - - while (len > 1) - { - sum += *data++; - len -= 2; - } - - if (len == 1) - { -#if __BYTE_ORDER == __LITTLE_ENDIAN - sum += *reinterpret_cast (data); -#else - sum += *reinterpret_cast (data) << 8; -#endif - } - - sum = (sum >> 16) + (sum & 0xffff); - sum += (sum >> 16); - - return static_cast (~sum); - } - /** * @brief wait for the socket handle to become ready. * @param wantRead set to true if want read diff --git a/core/include/join/stream_socket.hpp b/core/include/join/stream_socket.hpp index 59a18df5..185f559f 100644 --- a/core/include/join/stream_socket.hpp +++ b/core/include/join/stream_socket.hpp @@ -295,9 +295,9 @@ namespace join * @param timeout timeout in milliseconds. * @return 0 on success, -1 on failure. */ - int readExactly (char* data, unsigned long size, int timeout = 0) noexcept + int readExactly (char* data, size_t size, int timeout = 0) noexcept { - unsigned long numRead = 0; + size_t numRead = 0; while (numRead < size) { @@ -328,9 +328,9 @@ namespace join * @param timeout timeout in milliseconds. * @return 0 on success, -1 on failure. */ - int writeExactly (const char* data, unsigned long size, int timeout = 0) noexcept + int writeExactly (const char* data, size_t size, int timeout = 0) noexcept { - unsigned long numWrite = 0; + size_t numWrite = 0; while (numWrite < size) { diff --git a/core/include/join/utils.hpp b/core/include/join/utils.hpp index 9d824f15..5e4bbe72 100644 --- a/core/include/join/utils.hpp +++ b/core/include/join/utils.hpp @@ -347,6 +347,38 @@ namespace join return getline (istream, line, max); } + /** + * @brief get standard 1s complement checksum. + * @param data data pointer. + * @param len data len. + * @param current current sum. + * @return checksum. + */ + inline uint16_t checksum (const uint16_t* data, size_t len, uint16_t current = 0) noexcept + { + uint32_t sum = current; + + while (len > 1) + { + sum += *data++; + len -= 2; + } + + if (len == 1) + { +#if __BYTE_ORDER == __LITTLE_ENDIAN + sum += *reinterpret_cast (data); +#else + sum += *reinterpret_cast (data) << 8; +#endif + } + + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + + return static_cast (~sum); + } + /** * @brief dump data to standard output stream. * @param data data to dump. diff --git a/core/tests/icmp_async_datagram_socket_test.cpp b/core/tests/icmp_async_datagram_socket_test.cpp index 50efbb1f..950f5f12 100644 --- a/core/tests/icmp_async_datagram_socket_test.cpp +++ b/core/tests/icmp_async_datagram_socket_test.cpp @@ -54,7 +54,7 @@ class IcmpAsyncDatagramSocket : public ::testing::Test icmp->checksum = 0; icmp->un.echo.sequence = htons (1); icmp->un.echo.id = htons (getpid () & 0xFFFF); - icmp->checksum = Icmp::Socket::checksum (reinterpret_cast (icmp), sizeof (struct icmphdr), 0); + icmp->checksum = join::checksum (reinterpret_cast (icmp), sizeof (struct icmphdr), 0); } protected: diff --git a/core/tests/icmp_socket_test.cpp b/core/tests/icmp_socket_test.cpp index b9da1233..1aa6d3de 100644 --- a/core/tests/icmp_socket_test.cpp +++ b/core/tests/icmp_socket_test.cpp @@ -52,7 +52,7 @@ class IcmpSocket : public ::testing::Test icmp->checksum = 0; icmp->un.echo.sequence = htons (1); icmp->un.echo.id = htons (getpid () & 0xFFFF); - icmp->checksum = Icmp::Socket::checksum (reinterpret_cast (icmp), sizeof (struct icmphdr), 0); + icmp->checksum = join::checksum (reinterpret_cast (icmp), sizeof (struct icmphdr), 0); } protected: @@ -551,16 +551,6 @@ TEST_F (IcmpSocket, ttl) ASSERT_EQ (icmpSocket.ttl (), 60); } -/** - * @brief Test checksum method. - */ -TEST_F (IcmpSocket, checksum) -{ - std::string buffer ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); - - ASSERT_EQ (Icmp::Socket::checksum (reinterpret_cast (&buffer[0]), buffer.size (), 0), 19349); -} - /** * @brief Test is lower method. */ diff --git a/core/tests/ip_address_test.cpp b/core/tests/ip_address_test.cpp index 3136f0c3..bd1602da 100644 --- a/core/tests/ip_address_test.cpp +++ b/core/tests/ip_address_test.cpp @@ -32,8 +32,39 @@ // C++. #include +// C. +#include + using join::IpAddress; +/** + * @brief find an interface whose scope identifier is greater than the loopback one. + * @return interface name, empty if none was found. + */ +static std::string scopedInterface () +{ + struct if_nameindex* list = ::if_nameindex (); + std::string interface; + + if (list == nullptr) + { + return interface; + } + + for (struct if_nameindex* it = list; it->if_index != 0; ++it) + { + if (it->if_index > 1) + { + interface = it->if_name; + break; + } + } + + ::if_freenameindex (list); + + return interface; +} + /** * @brief Test default construction. */ @@ -1822,7 +1853,7 @@ TEST (IpAddress, equal) ip1 = "fe80::57f3:baa4:fc3a:890a%lo"; ASSERT_TRUE (ip1 == "fe80::57f3:baa4:fc3a:890a%lo"); - ip2 = "fe80::57f3:baa4:fc3a:890a%eth0"; + ip2 = "fe80::57f3:baa4:fc3a:890a%" + scopedInterface (); ASSERT_FALSE (ip1 == ip2); } @@ -1886,7 +1917,7 @@ TEST (IpAddress, different) ip1 = "fe80::57f3:baa4:fc3a:890a%lo"; ASSERT_FALSE (ip1 != "fe80::57f3:baa4:fc3a:890a%lo"); - ip2 = "fe80::57f3:baa4:fc3a:890a%eth0"; + ip2 = "fe80::57f3:baa4:fc3a:890a%" + scopedInterface (); ASSERT_TRUE (ip1 != ip2); } @@ -1970,7 +2001,7 @@ TEST (IpAddress, lower) ip1 = "fe80::57f3:baa4:fc3a:890a%lo"; ASSERT_FALSE (ip1 < "fe80::57f3:baa4:fc3a:890a%lo"); - ip2 = "fe80::57f3:baa4:fc3a:890a%eth0"; + ip2 = "fe80::57f3:baa4:fc3a:890a%" + scopedInterface (); ASSERT_TRUE (ip1 < ip2); } @@ -2054,7 +2085,7 @@ TEST (IpAddress, lowerOrEqual) ip1 = "fe80::57f3:baa4:fc3a:890a%lo"; ASSERT_TRUE (ip1 <= "fe80::57f3:baa4:fc3a:890a%lo"); - ip2 = "fe80::57f3:baa4:fc3a:890a%eth0"; + ip2 = "fe80::57f3:baa4:fc3a:890a%" + scopedInterface (); ASSERT_TRUE (ip1 <= ip2); } @@ -2138,7 +2169,7 @@ TEST (IpAddress, greater) ip1 = "fe80::57f3:baa4:fc3a:890a%lo"; ASSERT_FALSE (ip1 > "fe80::57f3:baa4:fc3a:890a%lo"); - ip2 = "fe80::57f3:baa4:fc3a:890a%eth0"; + ip2 = "fe80::57f3:baa4:fc3a:890a%" + scopedInterface (); ASSERT_FALSE (ip1 > ip2); } @@ -2222,7 +2253,7 @@ TEST (IpAddress, greaterOrEqual) ip1 = "fe80::57f3:baa4:fc3a:890a%lo"; ASSERT_TRUE (ip1 >= "fe80::57f3:baa4:fc3a:890a%lo"); - ip2 = "fe80::57f3:baa4:fc3a:890a%eth0"; + ip2 = "fe80::57f3:baa4:fc3a:890a%" + scopedInterface (); ASSERT_FALSE (ip1 >= ip2); } diff --git a/core/tests/mac_address_test.cpp b/core/tests/mac_address_test.cpp index 30d63da5..970003cf 100644 --- a/core/tests/mac_address_test.cpp +++ b/core/tests/mac_address_test.cpp @@ -30,11 +30,55 @@ #include // C. +#include #include +#include #include using join::MacAddress; +/** + * @brief find the first ethernet interface having a hardware address. + * @param mac hardware address of the interface found. + * @return interface name, empty if none was found. + */ +static std::string ethernetInterface (MacAddress& mac) +{ + struct ifaddrs* ifap = nullptr; + std::string interface; + + if (::getifaddrs (&ifap) == -1) + { + return interface; + } + + for (struct ifaddrs* ifa = ifap; ifa != nullptr; ifa = ifa->ifa_next) + { + if ((ifa->ifa_addr == nullptr) || (ifa->ifa_addr->sa_family != AF_PACKET) || (ifa->ifa_flags & IFF_LOOPBACK)) + { + continue; + } + + const struct sockaddr_ll* hwaddr = reinterpret_cast (ifa->ifa_addr); + if ((hwaddr->sll_hatype != ARPHRD_ETHER) || (hwaddr->sll_halen != IFHWADDRLEN)) + { + continue; + } + + MacAddress found (hwaddr->sll_addr, hwaddr->sll_halen); + if (!found.isWildcard ()) + { + interface = ifa->ifa_name; + mac = found; + break; + } + } + + ::freeifaddrs (ifap); + + return interface; +} + /** * @brief Test default construction. */ @@ -332,8 +376,17 @@ TEST (MacAddress, cend) */ TEST (MacAddress, address) { + MacAddress expected; + std::string interface = ethernetInterface (expected); + ASSERT_TRUE (MacAddress::address ("bar0").isWildcard ()); - ASSERT_FALSE (MacAddress::address ("eth0").isWildcard ()) << join::lastError.message (); + + if (interface.empty ()) + { + GTEST_SKIP () << "no ethernet interface available"; + } + + ASSERT_EQ (MacAddress::address (interface), expected) << join::lastError.message (); } /** diff --git a/core/tests/raw_async_socket_test.cpp b/core/tests/raw_async_socket_test.cpp index 907cc476..f0769563 100644 --- a/core/tests/raw_async_socket_test.cpp +++ b/core/tests/raw_async_socket_test.cpp @@ -65,7 +65,7 @@ class RawAsyncSocket : public ::testing::Test _packet.udp.len = htons (sizeof (Packet) - sizeof (_packet.eth) - sizeof (_packet.ip)); _packet.ip.tot_len = _packet.udp.len; _packet.udp.check = - Raw::Socket::checksum (reinterpret_cast (&_packet.ip), sizeof (Packet) - sizeof (_packet.eth)); + join::checksum (reinterpret_cast (&_packet.ip), sizeof (Packet) - sizeof (_packet.eth)); // fill in IP header. _packet.ip.ihl = sizeof (_packet.ip) >> 2; @@ -75,7 +75,7 @@ class RawAsyncSocket : public ::testing::Test _packet.ip.id = htons (join::randomize ()); _packet.ip.frag_off = htons (IP_DF); _packet.ip.ttl = IPDEFTTL; - _packet.ip.check = Raw::Socket::checksum (reinterpret_cast (&_packet.ip), sizeof (_packet.ip)); + _packet.ip.check = join::checksum (reinterpret_cast (&_packet.ip), sizeof (_packet.ip)); // fill in ETH header. memcpy (_packet.eth.h_dest, MacAddress::wildcard.addr (), 6); diff --git a/core/tests/raw_socket_test.cpp b/core/tests/raw_socket_test.cpp index 851a8950..09943ad3 100644 --- a/core/tests/raw_socket_test.cpp +++ b/core/tests/raw_socket_test.cpp @@ -64,7 +64,7 @@ class RawSocket : public Raw::Socket, public EventHandler, public ::testing::Tes _packet.udp.len = htons (sizeof (Packet) - sizeof (_packet.eth) - sizeof (_packet.ip)); _packet.ip.tot_len = _packet.udp.len; _packet.udp.check = - Raw::Socket::checksum (reinterpret_cast (&_packet.ip), sizeof (Packet) - sizeof (_packet.eth)); + join::checksum (reinterpret_cast (&_packet.ip), sizeof (Packet) - sizeof (_packet.eth)); // fill in IP header. _packet.ip.ihl = sizeof (_packet.ip) >> 2; @@ -74,7 +74,7 @@ class RawSocket : public Raw::Socket, public EventHandler, public ::testing::Tes _packet.ip.id = htons (join::randomize ()); _packet.ip.frag_off = htons (IP_DF); _packet.ip.ttl = IPDEFTTL; - _packet.ip.check = Raw::Socket::checksum (reinterpret_cast (&_packet.ip), sizeof (_packet.ip)); + _packet.ip.check = join::checksum (reinterpret_cast (&_packet.ip), sizeof (_packet.ip)); // fill in ETH header. memcpy (_packet.eth.h_dest, MacAddress::wildcard.addr (), 6); @@ -408,16 +408,6 @@ TEST_F (RawSocket, handle) ASSERT_EQ (rawSocket.handle (), -1); } -/** - * @brief Test checksum method. - */ -TEST_F (RawSocket, checksum) -{ - std::string buffer ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); - - ASSERT_EQ (Raw::Socket::checksum (reinterpret_cast (&buffer[0]), buffer.size (), 0), 19349); -} - /** * @brief Test lower method. */ diff --git a/core/tests/tcp_socket_test.cpp b/core/tests/tcp_socket_test.cpp index b083a31a..8df73ad9 100644 --- a/core/tests/tcp_socket_test.cpp +++ b/core/tests/tcp_socket_test.cpp @@ -633,16 +633,6 @@ TEST_F (TcpSocket, mtu) ASSERT_EQ (tcpSocket.mtu (), -1); } -/** - * @brief Test checksum method. - */ -TEST_F (TcpSocket, checksum) -{ - std::string buffer ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); - - ASSERT_EQ (Tcp::Socket::checksum (reinterpret_cast (&buffer[0]), buffer.size (), 0), 19349); -} - /** * @brief Test lower method. */ diff --git a/core/tests/udp_async_datagram_socket_test.cpp b/core/tests/udp_async_datagram_socket_test.cpp index 15129c49..1554eb7c 100644 --- a/core/tests/udp_async_datagram_socket_test.cpp +++ b/core/tests/udp_async_datagram_socket_test.cpp @@ -535,11 +535,10 @@ TEST_F (UdpAsyncDatagramSocket, empty) ASSERT_TRUE (_cond.timedWait (lock, std::chrono::milliseconds (_timeout), [] () { return _completions >= 1; })); - ASSERT_EQ (_code, Errc::ConnectionClosed); + ASSERT_FALSE (_code) << _code.message (); + ASSERT_EQ (_transferred, 0u); } - ASSERT_FALSE (client.connected ()); - sender.close (); client.close (); } diff --git a/core/tests/udp_socket_test.cpp b/core/tests/udp_socket_test.cpp index f6d56505..35ff320d 100644 --- a/core/tests/udp_socket_test.cpp +++ b/core/tests/udp_socket_test.cpp @@ -71,7 +71,7 @@ class UdpSocket : public EventHandler, public ::testing::Test { Udp::Endpoint from; int nread = _server.readFrom (buffer.get (), _server.canRead (), &from); - if (nread > 0) + if (nread >= 0) { _server.writeTo (buffer.get (), nread, from); } @@ -272,6 +272,11 @@ TEST_F (UdpSocket, readFrom) ASSERT_TRUE (udpSocket.waitReadyRead (_timeout)) << join::lastError.message (); ASSERT_EQ (udpSocket.readFrom (data, udpSocket.canRead (), &from), sizeof (data)) << join::lastError.message (); + ASSERT_TRUE (udpSocket.waitReadyWrite (_timeout)) << join::lastError.message (); + ASSERT_EQ (udpSocket.write ("", 0), 0) << join::lastError.message (); + ASSERT_TRUE (udpSocket.waitReadyRead (_timeout)) << join::lastError.message (); + ASSERT_EQ (udpSocket.readFrom (data, sizeof (data), &from), 0) << join::lastError.message (); + char small[4]; ASSERT_TRUE (udpSocket.waitReadyWrite (_timeout)) << join::lastError.message (); @@ -535,6 +540,10 @@ TEST_F (UdpSocket, mtu) Udp::Socket udpSocket (Udp::Socket::Blocking); ASSERT_EQ (udpSocket.mtu (), -1); + ASSERT_EQ (udpSocket.open (), 0) << join::lastError.message (); + ASSERT_EQ (udpSocket.mtu (), -1); + udpSocket.close (); + ASSERT_EQ (udpSocket.connect ({"127.0.0.1", _port}), 0) << join::lastError.message (); ASSERT_NE (udpSocket.mtu (), -1) << join::lastError.message (); udpSocket.close (); @@ -545,16 +554,6 @@ TEST_F (UdpSocket, mtu) udpSocket.close (); } -/** - * @brief Test checksum method. - */ -TEST_F (UdpSocket, checksum) -{ - std::string buffer ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); - - ASSERT_EQ (Udp::Socket::checksum (reinterpret_cast (&buffer[0]), buffer.size (), 0), 19349); -} - /** * @brief Test is lower method. */ diff --git a/core/tests/unix_async_datagram_socket_test.cpp b/core/tests/unix_async_datagram_socket_test.cpp index 917a53c8..748de15a 100644 --- a/core/tests/unix_async_datagram_socket_test.cpp +++ b/core/tests/unix_async_datagram_socket_test.cpp @@ -551,11 +551,10 @@ TEST_F (UnixAsyncDatagramSocket, empty) ASSERT_TRUE (_cond.timedWait (lock, std::chrono::milliseconds (_timeout), [] () { return _completions >= 1; })); - ASSERT_EQ (_code, Errc::ConnectionClosed); + ASSERT_FALSE (_code) << _code.message (); + ASSERT_EQ (_transferred, 0u); } - ASSERT_FALSE (client.connected ()); - sender.close (); client.close (); } diff --git a/core/tests/unix_datagram_socket_test.cpp b/core/tests/unix_datagram_socket_test.cpp index 900d346a..6c24e289 100644 --- a/core/tests/unix_datagram_socket_test.cpp +++ b/core/tests/unix_datagram_socket_test.cpp @@ -29,6 +29,9 @@ // Libraries. #include +// C. +#include + using join::Errc; using join::ReactorThread; using join::EventHandler; @@ -40,6 +43,15 @@ using join::UnixDgram; class UnixDgramSocket : public EventHandler, public ::testing::Test { protected: + /** + * @brief Tears down the test suite. + */ + static void TearDownTestSuite () + { + ::unlink (_serverpath.c_str ()); + ::unlink (_clientpath.c_str ()); + } + /** * @brief Sets up the test fixture. */ @@ -69,7 +81,7 @@ class UnixDgramSocket : public EventHandler, public ::testing::Test { UnixDgram::Endpoint from; int nread = _server.readFrom (buffer.get (), _server.canRead (), &from); - if (nread > 0) + if (nread >= 0) { _server.writeTo (buffer.get (), nread, from); } @@ -250,6 +262,10 @@ TEST_F (UnixDgramSocket, readFrom) ASSERT_EQ (unixSocket.write (data, sizeof (data)), sizeof (data)) << join::lastError.message (); ASSERT_TRUE (unixSocket.waitReadyRead (_timeout)) << join::lastError.message (); ASSERT_EQ (unixSocket.readFrom (data, unixSocket.canRead (), &from), sizeof (data)) << join::lastError.message (); + ASSERT_TRUE (unixSocket.waitReadyWrite (_timeout)) << join::lastError.message (); + ASSERT_EQ (unixSocket.write ("", 0), 0) << join::lastError.message (); + ASSERT_TRUE (unixSocket.waitReadyRead (_timeout)) << join::lastError.message (); + ASSERT_EQ (unixSocket.readFrom (data, sizeof (data), &from), 0) << join::lastError.message (); ASSERT_EQ (from, UnixDgram::Endpoint (_serverpath)); unixSocket.close (); } @@ -485,16 +501,6 @@ TEST_F (UnixDgramSocket, mtu) ASSERT_EQ (unixSocket.mtu (), -1); } -/** - * @brief Test checksum method. - */ -TEST_F (UnixDgramSocket, checksum) -{ - std::string buffer ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); - - ASSERT_EQ (UnixDgram::Socket::checksum (reinterpret_cast (&buffer[0]), buffer.size (), 0), 19349); -} - /** * @brief Test is lower method. */ diff --git a/core/tests/unix_stream_acceptor_test.cpp b/core/tests/unix_stream_acceptor_test.cpp index dbcaaf0a..1bb88c77 100644 --- a/core/tests/unix_stream_acceptor_test.cpp +++ b/core/tests/unix_stream_acceptor_test.cpp @@ -28,6 +28,9 @@ // Libraries. #include +// C. +#include + using join::Errc; using join::UnixStream; @@ -197,5 +200,7 @@ TEST (UnixAcceptor, handle) int main (int argc, char** argv) { testing::InitGoogleTest (&argc, argv); - return RUN_ALL_TESTS (); + int result = RUN_ALL_TESTS (); + ::unlink (path.c_str ()); + return result; } diff --git a/core/tests/unix_stream_socket_test.cpp b/core/tests/unix_stream_socket_test.cpp index b44741ce..78d37af8 100644 --- a/core/tests/unix_stream_socket_test.cpp +++ b/core/tests/unix_stream_socket_test.cpp @@ -29,6 +29,9 @@ // Libraries. #include +// C. +#include + using join::Errc; using join::ReactorThread; using join::EventHandler; @@ -40,6 +43,15 @@ using join::UnixStream; class UnixStreamSocket : public EventHandler, public ::testing::Test { protected: + /** + * @brief Tears down the test suite. + */ + static void TearDownTestSuite () + { + ::unlink (_serverpath.c_str ()); + ::unlink (_clientpath.c_str ()); + } + /** * @brief Sets up the test fixture. */ @@ -567,16 +579,6 @@ TEST_F (UnixStreamSocket, mtu) ASSERT_EQ (unixSocket.mtu (), -1); } -/** - * @brief Test checksum method. - */ -TEST_F (UnixStreamSocket, checksum) -{ - std::string buffer ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); - - ASSERT_EQ (UnixStream::Socket::checksum (reinterpret_cast (&buffer[0]), buffer.size (), 0), 19349); -} - /** * @brief Test lower method. */ diff --git a/core/tests/utils_test.cpp b/core/tests/utils_test.cpp index 26a41681..36f8e408 100644 --- a/core/tests/utils_test.cpp +++ b/core/tests/utils_test.cpp @@ -174,6 +174,18 @@ TEST (Utils, getline) ASSERT_EQ (line, "ok"); } +/** + * @brief Test checksum. + */ +TEST (Utils, checksum) +{ + std::string odd ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); + ASSERT_EQ (join::checksum (reinterpret_cast (&odd[0]), odd.size (), 0), 19349); + + std::string even ({'\xD2', '\xB6', '\x69', '\xFD'}); + ASSERT_EQ (join::checksum (reinterpret_cast (&even[0]), even.size ()), 19395); +} + /** * @brief Test dump. */ diff --git a/crypto/include/join/dtls_wrapper.hpp b/crypto/include/join/dtls_wrapper.hpp index 51d63537..927305d7 100644 --- a/crypto/include/join/dtls_wrapper.hpp +++ b/crypto/include/join/dtls_wrapper.hpp @@ -49,7 +49,7 @@ namespace join * @param endpoint endpoint from where data are coming (optional). * @return The number of bytes received, -1 on failure. */ - int readFrom (char* buf, unsigned long len, Endpoint* endpoint = nullptr) noexcept + int readFrom (char* buf, size_t len, Endpoint* endpoint = nullptr) noexcept { if (this->_ssl) { @@ -92,7 +92,7 @@ namespace join * @param endpoint endpoint where to write the data. * @return the number of bytes written, -1 on failure. */ - int writeTo (const char* buf, unsigned long len, const Endpoint& endpoint) noexcept + int writeTo (const char* buf, size_t len, const Endpoint& endpoint) noexcept { if (this->_ssl) { diff --git a/crypto/include/join/tls.hpp b/crypto/include/join/tls.hpp index 51726d59..88720196 100644 --- a/crypto/include/join/tls.hpp +++ b/crypto/include/join/tls.hpp @@ -551,7 +551,7 @@ namespace join * @param len maximum number of bytes to read. * @return number of bytes read on success, -1 on failure. */ - int read (char* buf, unsigned long len) noexcept + int read (char* buf, size_t len) noexcept { if (_ssl) { @@ -574,9 +574,9 @@ namespace join * @param timeout timeout in milliseconds. * @return 0 on success, -1 on failure. */ - int readExactly (char* data, unsigned long size, int timeout = 0) + int readExactly (char* data, size_t size, int timeout = 0) { - unsigned long numRead = 0; + size_t numRead = 0; while (numRead < size) { @@ -627,7 +627,7 @@ namespace join * @param len number of bytes to write. * @return number of bytes written on success, -1 on failure. */ - int write (const char* buf, unsigned long len) noexcept + int write (const char* buf, size_t len) noexcept { if (_ssl) { @@ -650,9 +650,9 @@ namespace join * @param timeout timeout in milliseconds. * @return 0 on success, -1 on failure. */ - int writeExactly (const char* data, unsigned long size, int timeout = 0) + int writeExactly (const char* data, size_t size, int timeout = 0) { - unsigned long numWrite = 0; + size_t numWrite = 0; while (numWrite < size) { diff --git a/fabric/include/join/dhcp.hpp b/fabric/include/join/dhcp.hpp index 38d67454..4c65f1f2 100644 --- a/fabric/include/join/dhcp.hpp +++ b/fabric/include/join/dhcp.hpp @@ -207,7 +207,7 @@ namespace join ::memcpy (scratch.data () + sizeof (pseudo), &frame.udp, sizeof (frame.udp)); ::memcpy (scratch.data () + sizeof (pseudo) + sizeof (frame.udp), payload, size); - uint16_t sum = Socket::checksum (reinterpret_cast (scratch.data ()), scratch.size ()); + uint16_t sum = join::checksum (reinterpret_cast (scratch.data ()), scratch.size ()); return sum ? sum : 0xffff; } @@ -261,7 +261,7 @@ namespace join ::memcpy (&frame->ip.saddr, source.addr (), sizeof (frame->ip.saddr)); ::memcpy (&frame->ip.daddr, destination.addr (), sizeof (frame->ip.daddr)); frame->ip.check = 0; - frame->ip.check = Socket::checksum (reinterpret_cast (&frame->ip), sizeof (frame->ip)); + frame->ip.check = join::checksum (reinterpret_cast (&frame->ip), sizeof (frame->ip)); frame->udp.check = udpChecksum (*frame, payload.data (), payload.size ()); @@ -298,7 +298,7 @@ namespace join const uint16_t check = header.check; header.check = 0; - if (check != Socket::checksum (reinterpret_cast (&header), sizeof (header))) + if (check != join::checksum (reinterpret_cast (&header), sizeof (header))) { return nullptr; } diff --git a/fabric/include/join/resolver.hpp b/fabric/include/join/resolver.hpp index 7a4d8996..1f0c89e2 100644 --- a/fabric/include/join/resolver.hpp +++ b/fabric/include/join/resolver.hpp @@ -757,7 +757,7 @@ namespace join * @param maxSize maximum number of bytes to read. * @return number of bytes read, or -1 on error. */ - virtual int read (char* data, unsigned long maxSize) noexcept + virtual int read (char* data, size_t maxSize) noexcept { return _socket.read (data, maxSize); } @@ -768,7 +768,7 @@ namespace join * @param size number of bytes to write. * @return number of bytes written, or -1 on error. */ - virtual int write (const char* data, unsigned long size) noexcept + virtual int write (const char* data, size_t size) noexcept { return _socket.write (data, size); } @@ -1311,7 +1311,7 @@ namespace join * @param maxSize maximum number of bytes to read. * @return number of bytes read, or -1 on error. */ - int read (char* data, unsigned long maxSize) noexcept override final + int read (char* data, size_t maxSize) noexcept override final { if (_offset < _frameHeaderSize) { @@ -1377,11 +1377,11 @@ namespace join * @param size number of bytes to write. * @return number of bytes written, or -1 on error. */ - int write (const char* data, unsigned long size) noexcept override final + int write (const char* data, size_t size) noexcept override final { uint16_t msgLength = htons (static_cast (size)); const char* p = reinterpret_cast (&msgLength); - unsigned long remaining = sizeof (msgLength); + size_t remaining = sizeof (msgLength); while (remaining > 0) { diff --git a/fabric/src/ping.cpp b/fabric/src/ping.cpp index 75d2ea83..0d0f8b20 100644 --- a/fabric/src/ping.cpp +++ b/fabric/src/ping.cpp @@ -607,7 +607,7 @@ int Ping::echo (Icmp::Socket& socket, PingStats& stats, std::chrono::millisecond icmp->checksum = 0; icmp->un.echo.sequence = htons (sequence); icmp->un.echo.id = htons (_identity); - icmp->checksum = Icmp::Socket::checksum (reinterpret_cast (icmp), size, 0); + icmp->checksum = join::checksum (reinterpret_cast (icmp), size, 0); } int result = -1; @@ -790,7 +790,7 @@ void Ping::onReadable (int fd) struct icmphdr* icmp = reinterpret_cast (_buffer.get () + offset); if ((icmp->type != ICMP_ECHOREPLY) || (ntohs (icmp->un.echo.id) != _identity) || - Icmp::Socket::checksum (reinterpret_cast (icmp), packetSize, 0)) + join::checksum (reinterpret_cast (icmp), packetSize, 0)) { return; // LCOV_EXCL_LINE } diff --git a/fabric/tests/dhcp_test.cpp b/fabric/tests/dhcp_test.cpp index b5f7a973..bad70e14 100644 --- a/fabric/tests/dhcp_test.cpp +++ b/fabric/tests/dhcp_test.cpp @@ -283,7 +283,7 @@ class DhcpTest : public ::testing::Test, public Dhcp::Server ::memcpy (&frame->ip.saddr, IpAddress (_server).addr (), sizeof (frame->ip.saddr)); ::memcpy (&frame->ip.daddr, IpAddress::ipv4Broadcast.addr (), sizeof (frame->ip.daddr)); frame->ip.check = 0; - frame->ip.check = Dhcp::Socket::checksum (reinterpret_cast (&frame->ip), sizeof (frame->ip)); + frame->ip.check = join::checksum (reinterpret_cast (&frame->ip), sizeof (frame->ip)); const bool boot = (packet.op == DhcpMessage::BootRequest); diff --git a/fabric/tests/netlink_socket_test.cpp b/fabric/tests/netlink_socket_test.cpp index 93a8f4dd..58360400 100644 --- a/fabric/tests/netlink_socket_test.cpp +++ b/fabric/tests/netlink_socket_test.cpp @@ -519,16 +519,6 @@ TEST_F (NetlinkSocket, mtu) netlinkSocket.close (); } -/** - * @brief Test checksum method. - */ -TEST_F (NetlinkSocket, checksum) -{ - std::string buffer ({'\xD2', '\xB6', '\x69', '\xFD', '\x2E'}); - - ASSERT_EQ (Netlink::Socket::checksum (reinterpret_cast (&buffer[0]), buffer.size (), 0), 19349); -} - /** * @brief Test is lower method. */ From 1702d8e5c2272a182cac0d5165c696efe57e9a25 Mon Sep 17 00:00:00 2001 From: mrabine Date: Thu, 27 Aug 2026 22:45:09 +0200 Subject: [PATCH 2/4] fix empty datagram --- core/include/join/socket.hpp | 12 ++---------- core/include/join/stream_socket.hpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/include/join/socket.hpp b/core/include/join/socket.hpp index 94050e35..57d1159c 100644 --- a/core/include/join/socket.hpp +++ b/core/include/join/socket.hpp @@ -347,17 +347,9 @@ namespace join message.msg_controllen = 0; int size = ::recvmsg (_handle, &message, 0); - if (size < 1) + if (size == -1) { - if (size == -1) - { - lastError = std::error_code (errno, std::generic_category ()); - } - else - { - lastError = make_error_code (Errc::ConnectionClosed); - } - + lastError = std::error_code (errno, std::generic_category ()); return -1; } diff --git a/core/include/join/stream_socket.hpp b/core/include/join/stream_socket.hpp index 185f559f..32631eb5 100644 --- a/core/include/join/stream_socket.hpp +++ b/core/include/join/stream_socket.hpp @@ -288,6 +288,24 @@ namespace join _remote = {}; } + /** + * @brief read data. + * @param data buffer used to store the data received. + * @param maxSize maximum number of bytes to read. + * @return the number of bytes received, -1 on failure. + */ + int read (char* data, size_t maxSize) noexcept + { + int size = BasicSocket::read (data, maxSize); + if (size == 0) + { + lastError = make_error_code (Errc::ConnectionClosed); + return -1; + } + + return size; + } + /** * @brief read data until size is reached or an error occurred. * @param data buffer used to store the data received. From ac2d12754c7c9cba0ac3536b8b31ae768f5147b7 Mon Sep 17 00:00:00 2001 From: mrabine Date: Thu, 27 Aug 2026 22:59:44 +0200 Subject: [PATCH 3/4] ret value --- core/include/join/datagram_socket.hpp | 8 ++++---- core/include/join/socket.hpp | 10 +++++----- core/include/join/socket_stream.hpp | 2 +- core/include/join/stream_socket.hpp | 8 ++++---- core/tests/raw_socket_test.cpp | 2 +- core/tests/tcp_socket_stream_test.cpp | 2 +- core/tests/tcp_socket_test.cpp | 2 +- core/tests/udp_socket_test.cpp | 2 +- core/tests/unix_datagram_socket_test.cpp | 2 +- core/tests/unix_stream_socket_test.cpp | 2 +- crypto/include/join/dtls_wrapper.hpp | 4 ++-- crypto/include/join/tls.hpp | 8 ++++---- crypto/tests/dtls_wrapper_test.cpp | 2 +- crypto/tests/tls_stream_test.cpp | 2 +- crypto/tests/tls_wrapper_test.cpp | 2 +- fabric/include/join/dhcp.hpp | 2 +- fabric/include/join/nameserver.hpp | 4 ++-- fabric/include/join/resolver.hpp | 18 +++++++++--------- fabric/src/ping.cpp | 2 +- 19 files changed, 42 insertions(+), 42 deletions(-) diff --git a/core/include/join/datagram_socket.hpp b/core/include/join/datagram_socket.hpp index b10ea4bd..83f93d17 100644 --- a/core/include/join/datagram_socket.hpp +++ b/core/include/join/datagram_socket.hpp @@ -250,7 +250,7 @@ namespace join * @param endpoint endpoint from where data are coming (optional). * @return The number of bytes received, -1 on failure. */ - int readFrom (char* data, size_t maxSize, Endpoint* endpoint = nullptr) noexcept + ssize_t readFrom (char* data, size_t maxSize, Endpoint* endpoint = nullptr) noexcept { struct sockaddr_storage sa; @@ -266,7 +266,7 @@ namespace join message.msg_control = nullptr; message.msg_controllen = 0; - int size = ::recvmsg (this->_handle, &message, 0); + ssize_t size = ::recvmsg (this->_handle, &message, 0); if (size == -1) { lastError = std::error_code (errno, std::generic_category ()); @@ -294,14 +294,14 @@ namespace join * @param endpoint endpoint where to write the data. * @return the number of bytes written, -1 on failure. */ - int writeTo (const char* data, size_t maxSize, const Endpoint& endpoint) noexcept + ssize_t writeTo (const char* data, size_t maxSize, const Endpoint& endpoint) noexcept { if ((this->_state == State::Closed) && (open (endpoint.protocol ()) == -1)) { return -1; // LCOV_EXCL_LINE } - int result = ::sendto (this->_handle, data, maxSize, 0, endpoint.addr (), endpoint.length ()); + ssize_t result = ::sendto (this->_handle, data, maxSize, 0, endpoint.addr (), endpoint.length ()); if (result < 0) { lastError = std::error_code (errno, std::generic_category ()); diff --git a/core/include/join/socket.hpp b/core/include/join/socket.hpp index 57d1159c..f488781e 100644 --- a/core/include/join/socket.hpp +++ b/core/include/join/socket.hpp @@ -302,7 +302,7 @@ namespace join * @brief get the number of readable bytes. * @return the number of readable bytes, -1 on failure. */ - int canRead () const noexcept + ssize_t canRead () const noexcept { int available = 0; @@ -332,7 +332,7 @@ namespace join * @param maxSize maximum number of bytes to read. * @return the number of bytes received, -1 on failure. */ - int read (char* data, size_t maxSize) noexcept + ssize_t read (char* data, size_t maxSize) noexcept { struct iovec iov; iov.iov_base = data; @@ -346,7 +346,7 @@ namespace join message.msg_control = nullptr; message.msg_controllen = 0; - int size = ::recvmsg (_handle, &message, 0); + ssize_t size = ::recvmsg (_handle, &message, 0); if (size == -1) { lastError = std::error_code (errno, std::generic_category ()); @@ -378,7 +378,7 @@ namespace join * @param maxSize maximum number of bytes to write. * @return the number of bytes written, -1 on failure. */ - int write (const char* data, size_t maxSize) noexcept + ssize_t write (const char* data, size_t maxSize) noexcept { struct iovec iov; iov.iov_base = const_cast (data); @@ -392,7 +392,7 @@ namespace join message.msg_control = nullptr; message.msg_controllen = 0; - int result = ::sendmsg (_handle, &message, 0); + ssize_t result = ::sendmsg (_handle, &message, 0); if (result == -1) { lastError = std::error_code (errno, std::generic_category ()); diff --git a/core/include/join/socket_stream.hpp b/core/include/join/socket_stream.hpp index 96238c78..891de8f6 100644 --- a/core/include/join/socket_stream.hpp +++ b/core/include/join/socket_stream.hpp @@ -241,7 +241,7 @@ namespace join { for (;;) { - int nread = _socket.read (eback (), _bufsize); + ssize_t nread = _socket.read (eback (), _bufsize); if (nread == -1) { if (lastError == Errc::TemporaryError) diff --git a/core/include/join/stream_socket.hpp b/core/include/join/stream_socket.hpp index 32631eb5..6a31709f 100644 --- a/core/include/join/stream_socket.hpp +++ b/core/include/join/stream_socket.hpp @@ -294,9 +294,9 @@ namespace join * @param maxSize maximum number of bytes to read. * @return the number of bytes received, -1 on failure. */ - int read (char* data, size_t maxSize) noexcept + ssize_t read (char* data, size_t maxSize) noexcept { - int size = BasicSocket::read (data, maxSize); + ssize_t size = BasicSocket::read (data, maxSize); if (size == 0) { lastError = make_error_code (Errc::ConnectionClosed); @@ -319,7 +319,7 @@ namespace join while (numRead < size) { - int result = this->read (data + numRead, size - numRead); + ssize_t result = this->read (data + numRead, size - numRead); if (result == -1) { if (lastError == Errc::TemporaryError) @@ -352,7 +352,7 @@ namespace join while (numWrite < size) { - int result = this->write (data + numWrite, size - numWrite); + ssize_t result = this->write (data + numWrite, size - numWrite); if (result == -1) { if (lastError == Errc::TemporaryError) diff --git a/core/tests/raw_socket_test.cpp b/core/tests/raw_socket_test.cpp index 09943ad3..62682fad 100644 --- a/core/tests/raw_socket_test.cpp +++ b/core/tests/raw_socket_test.cpp @@ -110,7 +110,7 @@ class RawSocket : public Raw::Socket, public EventHandler, public ::testing::Tes auto buffer = std::make_unique (this->canRead ()); if (buffer) { - int nread = this->read (buffer.get (), this->canRead ()); + ssize_t nread = this->read (buffer.get (), this->canRead ()); if (size_t (nread) < sizeof (Packet)) { return; diff --git a/core/tests/tcp_socket_stream_test.cpp b/core/tests/tcp_socket_stream_test.cpp index 4af0caf3..4eee1e8f 100644 --- a/core/tests/tcp_socket_stream_test.cpp +++ b/core/tests/tcp_socket_stream_test.cpp @@ -73,7 +73,7 @@ class TcpSocketStream : public EventHandler, public ::testing::Test for (;;) { // echo received data. - int nread = sock.read (buf, sizeof (buf)); + ssize_t nread = sock.read (buf, sizeof (buf)); if (nread == -1) { if (join::lastError == Errc::TemporaryError) diff --git a/core/tests/tcp_socket_test.cpp b/core/tests/tcp_socket_test.cpp index 8df73ad9..0d2dd25d 100644 --- a/core/tests/tcp_socket_test.cpp +++ b/core/tests/tcp_socket_test.cpp @@ -72,7 +72,7 @@ class TcpSocket : public EventHandler, public ::testing::Test for (;;) { // echo received data. - int nread = sock.read (buf, sizeof (buf)); + ssize_t nread = sock.read (buf, sizeof (buf)); if (nread == -1) { if (join::lastError == Errc::TemporaryError) diff --git a/core/tests/udp_socket_test.cpp b/core/tests/udp_socket_test.cpp index 35ff320d..c9390aba 100644 --- a/core/tests/udp_socket_test.cpp +++ b/core/tests/udp_socket_test.cpp @@ -70,7 +70,7 @@ class UdpSocket : public EventHandler, public ::testing::Test if (buffer) { Udp::Endpoint from; - int nread = _server.readFrom (buffer.get (), _server.canRead (), &from); + ssize_t nread = _server.readFrom (buffer.get (), _server.canRead (), &from); if (nread >= 0) { _server.writeTo (buffer.get (), nread, from); diff --git a/core/tests/unix_datagram_socket_test.cpp b/core/tests/unix_datagram_socket_test.cpp index 6c24e289..9e16a67c 100644 --- a/core/tests/unix_datagram_socket_test.cpp +++ b/core/tests/unix_datagram_socket_test.cpp @@ -80,7 +80,7 @@ class UnixDgramSocket : public EventHandler, public ::testing::Test if (buffer) { UnixDgram::Endpoint from; - int nread = _server.readFrom (buffer.get (), _server.canRead (), &from); + ssize_t nread = _server.readFrom (buffer.get (), _server.canRead (), &from); if (nread >= 0) { _server.writeTo (buffer.get (), nread, from); diff --git a/core/tests/unix_stream_socket_test.cpp b/core/tests/unix_stream_socket_test.cpp index 78d37af8..861dc0a8 100644 --- a/core/tests/unix_stream_socket_test.cpp +++ b/core/tests/unix_stream_socket_test.cpp @@ -83,7 +83,7 @@ class UnixStreamSocket : public EventHandler, public ::testing::Test for (;;) { // echo received data. - int nread = sock.read (buf, sizeof (buf)); + ssize_t nread = sock.read (buf, sizeof (buf)); if (nread == -1) { if (join::lastError == Errc::TemporaryError) diff --git a/crypto/include/join/dtls_wrapper.hpp b/crypto/include/join/dtls_wrapper.hpp index 927305d7..a67dacaa 100644 --- a/crypto/include/join/dtls_wrapper.hpp +++ b/crypto/include/join/dtls_wrapper.hpp @@ -49,7 +49,7 @@ namespace join * @param endpoint endpoint from where data are coming (optional). * @return The number of bytes received, -1 on failure. */ - int readFrom (char* buf, size_t len, Endpoint* endpoint = nullptr) noexcept + ssize_t readFrom (char* buf, size_t len, Endpoint* endpoint = nullptr) noexcept { if (this->_ssl) { @@ -92,7 +92,7 @@ namespace join * @param endpoint endpoint where to write the data. * @return the number of bytes written, -1 on failure. */ - int writeTo (const char* buf, size_t len, const Endpoint& endpoint) noexcept + ssize_t writeTo (const char* buf, size_t len, const Endpoint& endpoint) noexcept { if (this->_ssl) { diff --git a/crypto/include/join/tls.hpp b/crypto/include/join/tls.hpp index 88720196..0abcc4b6 100644 --- a/crypto/include/join/tls.hpp +++ b/crypto/include/join/tls.hpp @@ -551,7 +551,7 @@ namespace join * @param len maximum number of bytes to read. * @return number of bytes read on success, -1 on failure. */ - int read (char* buf, size_t len) noexcept + ssize_t read (char* buf, size_t len) noexcept { if (_ssl) { @@ -580,7 +580,7 @@ namespace join while (numRead < size) { - int result = read (data + numRead, size - numRead); + ssize_t result = read (data + numRead, size - numRead); if (result == -1) { if (lastError == Errc::TemporaryError) @@ -627,7 +627,7 @@ namespace join * @param len number of bytes to write. * @return number of bytes written on success, -1 on failure. */ - int write (const char* buf, size_t len) noexcept + ssize_t write (const char* buf, size_t len) noexcept { if (_ssl) { @@ -656,7 +656,7 @@ namespace join while (numWrite < size) { - int result = write (data + numWrite, size - numWrite); + ssize_t result = write (data + numWrite, size - numWrite); if (result == -1) { if (lastError == Errc::TemporaryError) diff --git a/crypto/tests/dtls_wrapper_test.cpp b/crypto/tests/dtls_wrapper_test.cpp index 7794cb66..98d05e77 100644 --- a/crypto/tests/dtls_wrapper_test.cpp +++ b/crypto/tests/dtls_wrapper_test.cpp @@ -216,7 +216,7 @@ class DtlsSocket : public EventHandler, public ::testing::Test { char buffer[65536]; Udp::Endpoint from; - int nread = _socket.readFrom (buffer, sizeof (buffer), &from); + ssize_t nread = _socket.readFrom (buffer, sizeof (buffer), &from); if (nread > 0) { _socket.writeTo (buffer, nread, from); diff --git a/crypto/tests/tls_stream_test.cpp b/crypto/tests/tls_stream_test.cpp index 0fdb9294..07737b45 100644 --- a/crypto/tests/tls_stream_test.cpp +++ b/crypto/tests/tls_stream_test.cpp @@ -221,7 +221,7 @@ class TlsSocketStream : public EventHandler, public ::testing::Test for (;;) { // echo received data. - int nread = stream.socket ().read (buf, sizeof (buf)); + ssize_t nread = stream.socket ().read (buf, sizeof (buf)); if (nread == -1) { if (join::lastError == Errc::TemporaryError) diff --git a/crypto/tests/tls_wrapper_test.cpp b/crypto/tests/tls_wrapper_test.cpp index d4277373..192a49dd 100644 --- a/crypto/tests/tls_wrapper_test.cpp +++ b/crypto/tests/tls_wrapper_test.cpp @@ -222,7 +222,7 @@ class TlsSocket : public EventHandler, public ::testing::Test char buf[1024]; for (;;) { - int nread = tls.read (buf, sizeof (buf)); + ssize_t nread = tls.read (buf, sizeof (buf)); if (nread == -1) { if (join::lastError == Errc::TemporaryError) diff --git a/fabric/include/join/dhcp.hpp b/fabric/include/join/dhcp.hpp index 4c65f1f2..7047e33c 100644 --- a/fabric/include/join/dhcp.hpp +++ b/fabric/include/join/dhcp.hpp @@ -174,7 +174,7 @@ namespace join */ void onReadable ([[maybe_unused]] int fd) override final { - int size = _socket.read (_buffer.get (), sizeof (Frame) + Protocol::maxMsgSize); + ssize_t size = _socket.read (_buffer.get (), sizeof (Frame) + Protocol::maxMsgSize); if (size <= 0) { return; // LCOV_EXCL_LINE diff --git a/fabric/include/join/nameserver.hpp b/fabric/include/join/nameserver.hpp index 37823737..be87d0a1 100644 --- a/fabric/include/join/nameserver.hpp +++ b/fabric/include/join/nameserver.hpp @@ -153,7 +153,7 @@ namespace join virtual void onReadable ([[maybe_unused]] int fd) override { Endpoint from; - int size = _socket.readFrom (_buffer.get (), Protocol::maxMsgSize, &from); + ssize_t size = _socket.readFrom (_buffer.get (), Protocol::maxMsgSize, &from); if (size >= int (_headerSize)) { std::stringstream data; @@ -682,7 +682,7 @@ namespace join void onReadable ([[maybe_unused]] int fd) override final { Endpoint from; - int size = this->_socket.readFrom (this->_buffer.get (), Protocol::maxMsgSize, &from); + ssize_t size = this->_socket.readFrom (this->_buffer.get (), Protocol::maxMsgSize, &from); if (size >= int (this->_headerSize)) { std::stringstream data; diff --git a/fabric/include/join/resolver.hpp b/fabric/include/join/resolver.hpp index 1f0c89e2..64c5a909 100644 --- a/fabric/include/join/resolver.hpp +++ b/fabric/include/join/resolver.hpp @@ -757,7 +757,7 @@ namespace join * @param maxSize maximum number of bytes to read. * @return number of bytes read, or -1 on error. */ - virtual int read (char* data, size_t maxSize) noexcept + virtual ssize_t read (char* data, size_t maxSize) noexcept { return _socket.read (data, maxSize); } @@ -768,7 +768,7 @@ namespace join * @param size number of bytes to write. * @return number of bytes written, or -1 on error. */ - virtual int write (const char* data, size_t size) noexcept + virtual ssize_t write (const char* data, size_t size) noexcept { return _socket.write (data, size); } @@ -883,7 +883,7 @@ namespace join */ void onReadable ([[maybe_unused]] int fd) override final { - int size = read (_buffer.get (), Protocol::maxMsgSize); + ssize_t size = read (_buffer.get (), Protocol::maxMsgSize); if (size >= int (_headerSize)) { std::stringstream data; @@ -1311,11 +1311,11 @@ namespace join * @param maxSize maximum number of bytes to read. * @return number of bytes read, or -1 on error. */ - int read (char* data, size_t maxSize) noexcept override final + ssize_t read (char* data, size_t maxSize) noexcept override final { if (_offset < _frameHeaderSize) { - int nread = this->_socket.read (data + _offset, _frameHeaderSize - _offset); + ssize_t nread = this->_socket.read (data + _offset, _frameHeaderSize - _offset); if (nread == -1) { if (lastError != Errc::TemporaryError) @@ -1345,7 +1345,7 @@ namespace join } } - int nread = this->_socket.read (data + (_offset - _frameHeaderSize), _size - (_offset - _frameHeaderSize)); + ssize_t nread = this->_socket.read (data + (_offset - _frameHeaderSize), _size - (_offset - _frameHeaderSize)); if (nread == -1) { if (lastError != Errc::TemporaryError) @@ -1377,7 +1377,7 @@ namespace join * @param size number of bytes to write. * @return number of bytes written, or -1 on error. */ - int write (const char* data, size_t size) noexcept override final + ssize_t write (const char* data, size_t size) noexcept override final { uint16_t msgLength = htons (static_cast (size)); const char* p = reinterpret_cast (&msgLength); @@ -1385,7 +1385,7 @@ namespace join while (remaining > 0) { - int result = this->_socket.write (p, remaining); + ssize_t result = this->_socket.write (p, remaining); if (result == -1) { if (lastError == Errc::TemporaryError) @@ -1404,7 +1404,7 @@ namespace join while (remaining > 0) { - int result = this->_socket.write (p, remaining); + ssize_t result = this->_socket.write (p, remaining); if (result == -1) { if (lastError == Errc::TemporaryError) diff --git a/fabric/src/ping.cpp b/fabric/src/ping.cpp index 0d0f8b20..6cdf5fd4 100644 --- a/fabric/src/ping.cpp +++ b/fabric/src/ping.cpp @@ -623,7 +623,7 @@ int Ping::echo (Icmp::Socket& socket, PingStats& stats, std::chrono::millisecond request->sent = std::chrono::steady_clock::now (); - int written = socket.write (data.get (), size); + ssize_t written = socket.write (data.get (), size); if ((written == -1) && (lastError == std::errc::no_buffer_space) && (attempt == 0)) { From 79dc8585d1c185318899003d095a7b5bc513901d Mon Sep 17 00:00:00 2001 From: mrabine Date: Thu, 27 Aug 2026 23:01:55 +0200 Subject: [PATCH 4/4] format --- fabric/include/join/resolver.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fabric/include/join/resolver.hpp b/fabric/include/join/resolver.hpp index 64c5a909..3105f756 100644 --- a/fabric/include/join/resolver.hpp +++ b/fabric/include/join/resolver.hpp @@ -1345,7 +1345,8 @@ namespace join } } - ssize_t nread = this->_socket.read (data + (_offset - _frameHeaderSize), _size - (_offset - _frameHeaderSize)); + ssize_t nread = + this->_socket.read (data + (_offset - _frameHeaderSize), _size - (_offset - _frameHeaderSize)); if (nread == -1) { if (lastError != Errc::TemporaryError)