From bc282425088455198a7a99511c75914477d4ed32 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 23:14:51 +0200 Subject: Merging upstream version 1.9.3. Signed-off-by: Daniel Baumann --- sstuff.hh | 64 +++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 28 deletions(-) (limited to 'sstuff.hh') diff --git a/sstuff.hh b/sstuff.hh index 208697c..88f6809 100644 --- a/sstuff.hh +++ b/sstuff.hh @@ -38,9 +38,9 @@ #include #include #include "namespaces.hh" +#include "noinitvector.hh" - -typedef int ProtocolType; //!< Supported protocol types +using ProtocolType = int; //!< Supported protocol types //! Representation of a Socket and many of the Berkeley functions available class Socket : public boost::noncopyable @@ -58,12 +58,13 @@ public: setCloseOnExec(d_socket); } - Socket(Socket&& rhs): d_buffer(std::move(rhs.d_buffer)), d_socket(rhs.d_socket) + Socket(Socket&& rhs) noexcept : + d_buffer(std::move(rhs.d_buffer)), d_socket(rhs.d_socket) { rhs.d_socket = -1; } - Socket& operator=(Socket&& rhs) + Socket& operator=(Socket&& rhs) noexcept { if (d_socket != -1) { close(d_socket); @@ -173,57 +174,64 @@ public: /** For datagram sockets, receive a datagram and learn where it came from \param dgram Will be filled with the datagram \param ep Will be filled with the origin of the datagram */ - void recvFrom(string &dgram, ComboAddress &ep) + void recvFrom(string &dgram, ComboAddress& remote) { - socklen_t remlen = sizeof(ep); - ssize_t bytes; - d_buffer.resize(s_buflen); - if((bytes=recvfrom(d_socket, &d_buffer[0], s_buflen, 0, reinterpret_cast(&ep) , &remlen)) <0) - throw NetworkError("After recvfrom: "+stringerror()); - - dgram.assign(d_buffer, 0, static_cast(bytes)); + socklen_t remlen = sizeof(remote); + if (dgram.size() < s_buflen) { + dgram.resize(s_buflen); + } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto bytes = recvfrom(d_socket, dgram.data(), dgram.size(), 0, reinterpret_cast(&remote) , &remlen); + if (bytes < 0) { + throw NetworkError("After recvfrom: " + stringerror()); + } + dgram.resize(static_cast(bytes)); } - bool recvFromAsync(string &dgram) + bool recvFromAsync(PacketBuffer& dgram, ComboAddress& remote) { - struct sockaddr_in remote; socklen_t remlen = sizeof(remote); - ssize_t bytes; - d_buffer.resize(s_buflen); - if((bytes=recvfrom(d_socket, &d_buffer[0], s_buflen, 0, reinterpret_cast(&remote), &remlen))<0) { - if(errno!=EAGAIN) { - throw NetworkError("After async recvfrom: "+stringerror()); + if (dgram.size() < s_buflen) { + dgram.resize(s_buflen); + } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + auto bytes = recvfrom(d_socket, dgram.data(), dgram.size(), 0, reinterpret_cast(&remote), &remlen); + if (bytes < 0) { + if (errno != EAGAIN) { + throw NetworkError("After async recvfrom: " + stringerror()); } else { return false; } } - dgram.assign(d_buffer, 0, static_cast(bytes)); + dgram.resize(static_cast(bytes)); return true; } - //! For datagram sockets, send a datagram to a destination - void sendTo(const char* msg, size_t len, const ComboAddress &ep) + void sendTo(const char* msg, size_t len, const ComboAddress& remote) { - if(sendto(d_socket, msg, len, 0, reinterpret_cast(&ep), ep.getSocklen())<0) - throw NetworkError("After sendto: "+stringerror()); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + if (sendto(d_socket, msg, len, 0, reinterpret_cast(&remote), remote.getSocklen()) < 0) { + throw NetworkError("After sendto: " + stringerror()); + } } //! For connected datagram sockets, send a datagram void send(const std::string& msg) { - if(::send(d_socket, msg.c_str(), msg.size(), 0)<0) + if (::send(d_socket, msg.data(), msg.size(), 0) < 0) { throw NetworkError("After send: "+stringerror()); + } } /** For datagram sockets, send a datagram to a destination \param dgram The datagram - \param ep The intended destination of the datagram */ - void sendTo(const string &dgram, const ComboAddress &ep) + \param remote The intended destination of the datagram */ + void sendTo(const string& dgram, const ComboAddress& remote) { - sendTo(dgram.c_str(), dgram.length(), ep); + sendTo(dgram.data(), dgram.length(), remote); } -- cgit v1.2.3