Fixed invalid read bug in socket library, added some static accessors

This commit is contained in:
Thulinma 2020-09-14 10:44:25 +02:00
parent ef1ee853b9
commit 66dff82144
2 changed files with 21 additions and 2 deletions

View file

@ -411,6 +411,16 @@ bool Socket::Buffer::available(unsigned int count){
return false; 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<std::string>::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. /// Removes count bytes from the buffer, returning them by value.
/// Returns an empty string if not all count bytes are available. /// Returns an empty string if not all count bytes are available.
std::string Socket::Buffer::remove(unsigned int count){ 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); sSend = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sSend < 0){continue;} if (sSend < 0){continue;}
if (connect(sSend, rp->ai_addr, rp->ai_addrlen) == 0){ 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; break;
} }
lastErr += strerror(errno); lastErr += strerror(errno);
@ -931,6 +941,11 @@ Socket::Buffer &Socket::Connection::Received(){
return downbuffer; 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. /// 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. /// 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){ 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; hints.ai_next = NULL;
int s = getaddrinfo(host.c_str(), 0, &hints, &result); int s = getaddrinfo(host.c_str(), 0, &hints, &result);
if (s != 0){return;} if (s != 0){return;}
if (result){remoteaddr = *((sockaddr_in6 *)result->ai_addr);} if (result){
memcpy(&remoteaddr, result->ai_addr, result->ai_addrlen);
}
freeaddrinfo(result); freeaddrinfo(result);
} }

View file

@ -63,6 +63,7 @@ namespace Socket{
void prepend(const char *newdata, const unsigned int newdatasize); void prepend(const char *newdata, const unsigned int newdatasize);
std::string &get(); std::string &get();
bool available(unsigned int count); bool available(unsigned int count);
bool available(unsigned int count) const;
std::string remove(unsigned int count); std::string remove(unsigned int count);
std::string copy(unsigned int count); std::string copy(unsigned int count);
void clear(); void clear();
@ -141,6 +142,7 @@ namespace Socket{
bool spool(); ///< Updates the downbufferinternal variables. bool spool(); ///< Updates the downbufferinternal variables.
bool peek(); ///< Clears the downbuffer and fills it with peek bool peek(); ///< Clears the downbuffer and fills it with peek
Buffer &Received(); ///< Returns a reference to the download buffer. 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 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); ///< Will not buffer anything but always send right away. Blocks.
void SendNow(const char *data, void SendNow(const char *data,