From 66dff8214459f8fcc279c3b2900db98fef09ec23 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 14 Sep 2020 10:44:25 +0200 Subject: [PATCH] Fixed invalid read bug in socket library, added some static accessors --- lib/socket.cpp | 21 +++++++++++++++++++-- lib/socket.h | 2 ++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/socket.cpp b/lib/socket.cpp index cc98322c..7e2abfae 100644 --- a/lib/socket.cpp +++ b/lib/socket.cpp @@ -411,6 +411,16 @@ bool Socket::Buffer::available(unsigned int count){ return false; } +/// Returns true if at least count bytes are available in this buffer. +bool Socket::Buffer::available(unsigned int count) const{ + unsigned int i = 0; + for (std::deque::const_iterator it = data.begin(); it != data.end(); ++it){ + i += (*it).size(); + if (i >= count){return true;} + } + return false; +} + /// Removes count bytes from the buffer, returning them by value. /// Returns an empty string if not all count bytes are available. std::string Socket::Buffer::remove(unsigned int count){ @@ -858,7 +868,7 @@ void Socket::Connection::open(std::string host, int port, bool nonblock, bool wi sSend = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sSend < 0){continue;} if (connect(sSend, rp->ai_addr, rp->ai_addrlen) == 0){ - remoteaddr = *((sockaddr_in6 *)rp->ai_addr); + memcpy(&remoteaddr, rp->ai_addr, rp->ai_addrlen); break; } lastErr += strerror(errno); @@ -931,6 +941,11 @@ Socket::Buffer &Socket::Connection::Received(){ return downbuffer; } +/// Returns a reference to the download buffer. +const Socket::Buffer &Socket::Connection::Received() const{ + return downbuffer; +} + /// Will not buffer anything but always send right away. Blocks. /// Any data that could not be send will block until it can be send or the connection is severed. void Socket::Connection::SendNow(const char *data, size_t len){ @@ -1169,7 +1184,9 @@ void Socket::Connection::setHost(std::string host){ hints.ai_next = NULL; int s = getaddrinfo(host.c_str(), 0, &hints, &result); if (s != 0){return;} - if (result){remoteaddr = *((sockaddr_in6 *)result->ai_addr);} + if (result){ + memcpy(&remoteaddr, result->ai_addr, result->ai_addrlen); + } freeaddrinfo(result); } diff --git a/lib/socket.h b/lib/socket.h index f652fbc4..61e58a2f 100644 --- a/lib/socket.h +++ b/lib/socket.h @@ -63,6 +63,7 @@ namespace Socket{ void prepend(const char *newdata, const unsigned int newdatasize); std::string &get(); bool available(unsigned int count); + bool available(unsigned int count) const; std::string remove(unsigned int count); std::string copy(unsigned int count); void clear(); @@ -141,6 +142,7 @@ namespace Socket{ bool spool(); ///< Updates the downbufferinternal variables. bool peek(); ///< Clears the downbuffer and fills it with peek Buffer &Received(); ///< Returns a reference to the download buffer. + const Buffer &Received() const; ///< Returns a reference to the download buffer. void SendNow(const std::string &data); ///< Will not buffer anything but always send right away. Blocks. void SendNow(const char *data); ///< Will not buffer anything but always send right away. Blocks. void SendNow(const char *data,