From: Otto Moerbeek Date: Wed, 26 Jun 2024 12:57:19 +0000 (+0200) Subject: Format sstuff.hh X-Git-Tag: rec-5.2.0-alpha1~207^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7d1ba206bf14072825dec45dc5cb1ec10e27de4;p=thirdparty%2Fpdns.git Format sstuff.hh --- diff --git a/.not-formatted b/.not-formatted index b77e6d5c7c..3d0e0cee8a 100644 --- a/.not-formatted +++ b/.not-formatted @@ -177,7 +177,6 @@ ./pdns/snmp-agent.cc ./pdns/snmp-agent.hh ./pdns/speedtest.cc -./pdns/sstuff.hh ./pdns/standalone_fuzz_target_runner.cc ./pdns/stat_t.hh ./pdns/statbag.cc diff --git a/pdns/sstuff.hh b/pdns/sstuff.hh index 88f6809299..38162e6595 100644 --- a/pdns/sstuff.hh +++ b/pdns/sstuff.hh @@ -46,14 +46,15 @@ using ProtocolType = int; //!< Supported protocol types class Socket : public boost::noncopyable { public: - Socket(int fd): d_socket(fd) + Socket(int fd) : + d_socket(fd) { } //! Construct a socket of specified address family and socket type. - Socket(int af, int st, ProtocolType pt=0) + Socket(int af, int st, ProtocolType pt = 0) { - if((d_socket=socket(af, st, pt))<0) + if ((d_socket = socket(af, st, pt)) < 0) throw NetworkError(stringerror()); setCloseOnExec(d_socket); } @@ -82,7 +83,7 @@ public: closesocket(d_socket); } } - catch(const PDNSException& e) { + catch (const PDNSException& e) { } } @@ -90,27 +91,28 @@ public: std::unique_ptr accept() { struct sockaddr_in remote; - socklen_t remlen=sizeof(remote); + socklen_t remlen = sizeof(remote); memset(&remote, 0, sizeof(remote)); - int s=::accept(d_socket, reinterpret_cast(&remote), &remlen); - if(s<0) { - if(errno==EAGAIN) + int s = ::accept(d_socket, reinterpret_cast(&remote), &remlen); + if (s < 0) { + if (errno == EAGAIN) return nullptr; - throw NetworkError("Accepting a connection: "+stringerror()); + throw NetworkError("Accepting a connection: " + stringerror()); } return std::make_unique(s); } //! Get remote address - bool getRemote(ComboAddress &remote) { - socklen_t remotelen=sizeof(remote); - return (getpeername(d_socket, reinterpret_cast(&remote), &remotelen) >= 0); + bool getRemote(ComboAddress& remote) + { + socklen_t remotelen = sizeof(remote); + return (getpeername(d_socket, reinterpret_cast(&remote), &remotelen) >= 0); } //! Check remote address against netmaskgroup ng - bool acl(const NetmaskGroup &ng) + bool acl(const NetmaskGroup& ng) { ComboAddress remote; if (getRemote(remote)) @@ -135,7 +137,8 @@ public: { try { ::setReuseAddr(d_socket); - } catch (const PDNSException &e) { + } + catch (const PDNSException& e) { throw NetworkError(e.reason); } } @@ -148,40 +151,39 @@ public: throw NetworkError("While setting TCP_FASTOPEN_CONNECT: " + stringerror()); } #else - throw NetworkError("While setting TCP_FASTOPEN_CONNECT: not compiled in"); + throw NetworkError("While setting TCP_FASTOPEN_CONNECT: not compiled in"); #endif } //! Bind the socket to a specified endpoint - void bind(const ComboAddress &local, bool reuseaddr=true) + void bind(const ComboAddress& local, bool reuseaddr = true) { - int tmp=1; - if(reuseaddr && setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&tmp), sizeof tmp)<0) - throw NetworkError("Setsockopt failed: "+stringerror()); + int tmp = 1; + if (reuseaddr && setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&tmp), sizeof tmp) < 0) + throw NetworkError("Setsockopt failed: " + stringerror()); - if(::bind(d_socket, reinterpret_cast(&local), local.getSocklen())<0) - throw NetworkError("While binding: "+stringerror()); + if (::bind(d_socket, reinterpret_cast(&local), local.getSocklen()) < 0) + throw NetworkError("While binding: " + stringerror()); } //! Connect the socket to a specified endpoint - void connect(const ComboAddress &ep, int timeout=0) + void connect(const ComboAddress& ep, int timeout = 0) { - SConnectWithTimeout(d_socket, ep, timeval{timeout,0}); + SConnectWithTimeout(d_socket, ep, timeval{timeout, 0}); } - //! For datagram sockets, receive a datagram and learn where it came from /** 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& remote) + void recvFrom(string& dgram, ComboAddress& remote) { 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); + auto bytes = recvfrom(d_socket, dgram.data(), dgram.size(), 0, reinterpret_cast(&remote), &remlen); if (bytes < 0) { throw NetworkError("After recvfrom: " + stringerror()); } @@ -195,7 +197,7 @@ public: dgram.resize(s_buflen); } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto bytes = recvfrom(d_socket, dgram.data(), dgram.size(), 0, reinterpret_cast(&remote), &remlen); + 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()); @@ -212,7 +214,7 @@ public: void sendTo(const char* msg, size_t len, const ComboAddress& remote) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - if (sendto(d_socket, msg, len, 0, reinterpret_cast(&remote), remote.getSocklen()) < 0) { + if (sendto(d_socket, msg, len, 0, reinterpret_cast(&remote), remote.getSocklen()) < 0) { throw NetworkError("After sendto: " + stringerror()); } } @@ -221,11 +223,10 @@ public: void send(const std::string& msg) { if (::send(d_socket, msg.data(), msg.size(), 0) < 0) { - throw NetworkError("After send: "+stringerror()); + throw NetworkError("After send: " + stringerror()); } } - /** For datagram sockets, send a datagram to a destination \param dgram The datagram \param remote The intended destination of the datagram */ @@ -234,27 +235,25 @@ public: sendTo(dgram.data(), dgram.length(), remote); } - //! Write this data to the socket, taking care that all bytes are written out - void writen(const string &data) + void writen(const string& data) { - if(data.empty()) + if (data.empty()) return; - size_t toWrite=data.length(); + size_t toWrite = data.length(); ssize_t res; - const char *ptr=data.c_str(); + const char* ptr = data.c_str(); do { - res=::send(d_socket, ptr, toWrite, 0); - if(res<0) - throw NetworkError("Writing to a socket: "+stringerror()); - if(!res) + res = ::send(d_socket, ptr, toWrite, 0); + if (res < 0) + throw NetworkError("Writing to a socket: " + stringerror()); + if (!res) throw NetworkError("EOF on socket"); toWrite -= static_cast(res); ptr += static_cast(res); - } while(toWrite); - + } while (toWrite); } //! tries to write toWrite bytes from ptr to the socket @@ -262,54 +261,54 @@ public: \param ptr Location to write from \param toWrite number of bytes to try */ - size_t tryWrite(const char *ptr, size_t toWrite) + size_t tryWrite(const char* ptr, size_t toWrite) { ssize_t res; - res=::send(d_socket,ptr,toWrite,0); - if(res==0) + res = ::send(d_socket, ptr, toWrite, 0); + if (res == 0) throw NetworkError("EOF on writing to a socket"); - if(res>0) + if (res > 0) return res; - if(errno==EAGAIN) + if (errno == EAGAIN) return 0; - throw NetworkError("Writing to a socket: "+stringerror()); + throw NetworkError("Writing to a socket: " + stringerror()); } //! Writes toWrite bytes from ptr to the socket /** Writes toWrite bytes from ptr to the socket. Returns how many bytes were written */ - size_t write(const char *ptr, size_t toWrite) + size_t write(const char* ptr, size_t toWrite) { ssize_t res; - res=::send(d_socket,ptr,toWrite,0); - if(res<0) { - throw NetworkError("Writing to a socket: "+stringerror()); + res = ::send(d_socket, ptr, toWrite, 0); + if (res < 0) { + throw NetworkError("Writing to a socket: " + stringerror()); } return res; } - void writenWithTimeout(const void *buffer, size_t n, int timeout) + void writenWithTimeout(const void* buffer, size_t n, int timeout) { - size_t bytes=n; - const char *ptr = reinterpret_cast(buffer); + size_t bytes = n; + const char* ptr = reinterpret_cast(buffer); ssize_t ret; - while(bytes) { - ret=::write(d_socket, ptr, bytes); - if(ret < 0) { - if(errno == EAGAIN) { - ret=waitForRWData(d_socket, false, timeout, 0); - if(ret < 0) + while (bytes) { + ret = ::write(d_socket, ptr, bytes); + if (ret < 0) { + if (errno == EAGAIN) { + ret = waitForRWData(d_socket, false, timeout, 0); + if (ret < 0) throw NetworkError("Waiting for data write"); - if(!ret) + if (!ret) throw NetworkError("Timeout writing data"); continue; } else - throw NetworkError("Writing data: "+stringerror()); + throw NetworkError("Writing data: " + stringerror()); } - if(!ret) { + if (!ret) { throw NetworkError("Did not fulfill TCP write due to EOF"); } @@ -323,64 +322,64 @@ public: { char c; - ssize_t res=::recv(d_socket,&c,1,0); - if(res) + ssize_t res = ::recv(d_socket, &c, 1, 0); + if (res) return c; return -1; } - void getline(string &data) + void getline(string& data) { - data=""; + data = ""; int c; - while((c=getChar())!=-1) { - data+=(char)c; - if(c=='\n') + while ((c = getChar()) != -1) { + data += (char)c; + if (c == '\n') break; } } //! Reads a block of data from the socket to a string - void read(string &data) + void read(string& data) { d_buffer.resize(s_buflen); - ssize_t res=::recv(d_socket, &d_buffer[0], s_buflen, 0); - if(res<0) - throw NetworkError("Reading from a socket: "+stringerror()); + ssize_t res = ::recv(d_socket, &d_buffer[0], s_buflen, 0); + if (res < 0) + throw NetworkError("Reading from a socket: " + stringerror()); data.assign(d_buffer, 0, static_cast(res)); } //! Reads a block of data from the socket to a block of memory - size_t read(char *buffer, size_t bytes) + size_t read(char* buffer, size_t bytes) { - ssize_t res=::recv(d_socket, buffer, bytes, 0); - if(res<0) - throw NetworkError("Reading from a socket: "+stringerror()); + ssize_t res = ::recv(d_socket, buffer, bytes, 0); + if (res < 0) + throw NetworkError("Reading from a socket: " + stringerror()); return static_cast(res); } /** Read a bock of data from the socket to a block of memory, - * waiting at most 'timeout' seconds for the data to become - * available. Be aware that this does _NOT_ handle partial reads - * for you. - */ + * waiting at most 'timeout' seconds for the data to become + * available. Be aware that this does _NOT_ handle partial reads + * for you. + */ ssize_t readWithTimeout(char* buffer, size_t n, int timeout) { int err = waitForRWData(d_socket, true, timeout, 0); - if(err == 0) + if (err == 0) throw NetworkError("timeout reading"); - if(err < 0) - throw NetworkError("nonblocking read failed: "+stringerror()); + if (err < 0) + throw NetworkError("nonblocking read failed: " + stringerror()); return read(buffer, n); } //! Sets the socket to listen with a default listen backlog of 10 pending connections - void listen(unsigned int length=10) + void listen(unsigned int length = 10) { - if(::listen(d_socket,length)<0) - throw NetworkError("Setting socket to listen: "+stringerror()); + if (::listen(d_socket, length) < 0) + throw NetworkError("Setting socket to listen: " + stringerror()); } //! Returns the internal file descriptor of the socket