2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 12:06:41 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/netstring.hpp"
|
|
|
|
#include "base/debug.hpp"
|
2019-02-18 15:21:50 +01:00
|
|
|
#include "base/tlsstream.hpp"
|
2019-02-19 11:20:39 +01:00
|
|
|
#include <cstdint>
|
2019-02-18 15:21:50 +01:00
|
|
|
#include <memory>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2019-02-19 11:20:39 +01:00
|
|
|
#include <utility>
|
2019-02-18 15:21:50 +01:00
|
|
|
#include <boost/asio/buffer.hpp>
|
2019-02-19 11:20:39 +01:00
|
|
|
#include <boost/asio/read.hpp>
|
2019-02-18 15:21:50 +01:00
|
|
|
#include <boost/asio/spawn.hpp>
|
|
|
|
#include <boost/asio/write.hpp>
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-18 22:53:35 +02:00
|
|
|
/**
|
2013-04-04 16:08:02 +02:00
|
|
|
* Reads data from a stream in netstring format.
|
2012-05-18 22:53:35 +02:00
|
|
|
*
|
2012-11-22 12:04:32 +01:00
|
|
|
* @param stream The stream to read from.
|
2012-09-17 13:35:55 +02:00
|
|
|
* @param[out] str The String that has been read from the IOQueue.
|
|
|
|
* @returns true if a complete String was read from the IOQueue, false otherwise.
|
2012-07-16 00:05:24 +02:00
|
|
|
* @exception invalid_argument The input stream is invalid.
|
2013-04-04 16:08:02 +02:00
|
|
|
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
|
2012-05-18 22:53:35 +02:00
|
|
|
*/
|
2018-03-01 09:47:29 +01:00
|
|
|
StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str, StreamReadContext& context,
|
|
|
|
bool may_wait, ssize_t maxMessageLength)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2015-02-14 17:40:29 +01:00
|
|
|
if (context.Eof)
|
2015-02-14 16:34:36 +01:00
|
|
|
return StatusEof;
|
2012-07-17 20:41:06 +02:00
|
|
|
|
2015-02-14 18:48:33 +01:00
|
|
|
if (context.MustRead) {
|
2015-06-24 09:44:59 +02:00
|
|
|
if (!context.FillFromStream(stream, may_wait)) {
|
2015-02-14 18:48:33 +01:00
|
|
|
context.Eof = true;
|
|
|
|
return StatusEof;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.MustRead = false;
|
2015-02-14 17:40:29 +01:00
|
|
|
}
|
2012-07-17 20:41:06 +02:00
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
size_t header_length = 0;
|
2012-11-23 11:02:34 +01:00
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
for (size_t i = 0; i < context.Size; i++) {
|
|
|
|
if (context.Buffer[i] == ':') {
|
|
|
|
header_length = i;
|
2015-03-02 12:46:16 +01:00
|
|
|
|
|
|
|
/* make sure there's a header */
|
|
|
|
if (header_length == 0)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (no length specifier)"));
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
break;
|
2015-02-14 16:34:36 +01:00
|
|
|
} else if (i > 16)
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing :)"));
|
|
|
|
}
|
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
if (header_length == 0) {
|
|
|
|
context.MustRead = true;
|
|
|
|
return StatusNeedData;
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
/* no leading zeros allowed */
|
2015-02-14 16:34:36 +01:00
|
|
|
if (context.Buffer[0] == '0' && isdigit(context.Buffer[1]))
|
2013-04-04 16:08:02 +02:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (leading zero)"));
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
size_t len, i;
|
|
|
|
|
|
|
|
len = 0;
|
2015-02-14 16:34:36 +01:00
|
|
|
for (i = 0; i < header_length && isdigit(context.Buffer[i]); i++) {
|
2012-03-28 13:24:49 +02:00
|
|
|
/* length specifier must have at most 9 characters */
|
2015-02-14 16:34:36 +01:00
|
|
|
if (i >= 9)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Length specifier must not exceed 9 characters"));
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
len = len * 10 + (context.Buffer[i] - '0');
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
/* read the whole message */
|
2013-04-04 16:08:02 +02:00
|
|
|
size_t data_length = len + 1;
|
2012-07-16 00:05:24 +02:00
|
|
|
|
2019-03-08 14:07:29 +01:00
|
|
|
if (maxMessageLength >= 0 && data_length > (size_t)maxMessageLength) {
|
2018-03-01 09:47:29 +01:00
|
|
|
std::stringstream errorMessage;
|
2018-03-05 13:22:43 +01:00
|
|
|
errorMessage << "Max data length exceeded: " << (maxMessageLength / 1024) << " KB";
|
2018-03-01 09:47:29 +01:00
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(errorMessage.str()));
|
|
|
|
}
|
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
char *data = context.Buffer + header_length + 1;
|
2012-07-16 00:05:24 +02:00
|
|
|
|
2015-03-02 14:10:26 +01:00
|
|
|
if (context.Size < header_length + 1 + data_length) {
|
2015-02-14 16:34:36 +01:00
|
|
|
context.MustRead = true;
|
|
|
|
return StatusNeedData;
|
2012-07-16 00:05:24 +02:00
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (data[len] != ',')
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing ,)"));
|
2015-03-02 14:10:26 +01:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
*str = String(&data[0], &data[len]);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
context.DropData(header_length + 1 + len + 1);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
return StatusNewItem;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-18 22:53:35 +02:00
|
|
|
/**
|
2017-11-13 16:26:21 +01:00
|
|
|
* Writes data into a stream using the netstring format and returns bytes written.
|
2012-05-18 22:53:35 +02:00
|
|
|
*
|
2012-11-22 12:04:32 +01:00
|
|
|
* @param stream The stream.
|
2012-08-02 09:38:08 +02:00
|
|
|
* @param str The String that is to be written.
|
2017-11-13 16:26:21 +01:00
|
|
|
*
|
|
|
|
* @return The amount of bytes written.
|
2012-05-18 22:53:35 +02:00
|
|
|
*/
|
2017-11-13 16:26:21 +01:00
|
|
|
size_t NetString::WriteStringToStream(const Stream::Ptr& stream, const String& str)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2013-04-04 16:08:02 +02:00
|
|
|
std::ostringstream msgbuf;
|
2016-08-20 23:46:44 +02:00
|
|
|
WriteStringToStream(msgbuf, str);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
String msg = msgbuf.str();
|
|
|
|
stream->Write(msg.CStr(), msg.GetLength());
|
2017-11-13 16:26:21 +01:00
|
|
|
return msg.GetLength();
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
2016-08-20 23:46:44 +02:00
|
|
|
|
2019-02-25 18:12:32 +01:00
|
|
|
/**
|
|
|
|
* Reads data from a stream in netstring format.
|
|
|
|
*
|
|
|
|
* @param stream The stream to read from.
|
|
|
|
* @returns The String that has been read from the IOQueue.
|
|
|
|
* @exception invalid_argument The input stream is invalid.
|
|
|
|
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
|
|
|
|
*/
|
|
|
|
String NetString::ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& stream,
|
|
|
|
ssize_t maxMessageLength)
|
|
|
|
{
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
|
|
|
size_t len = 0;
|
|
|
|
bool leadingZero = false;
|
|
|
|
|
|
|
|
for (uint_fast8_t readBytes = 0;; ++readBytes) {
|
|
|
|
char byte = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
asio::mutable_buffer byteBuf (&byte, 1);
|
|
|
|
asio::read(*stream, byteBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isdigit(byte)) {
|
|
|
|
if (readBytes == 9) {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Length specifier must not exceed 9 characters"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leadingZero) {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (leading zero)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
len = len * 10u + size_t(byte - '0');
|
|
|
|
|
|
|
|
if (!readBytes && byte == '0') {
|
|
|
|
leadingZero = true;
|
|
|
|
}
|
|
|
|
} else if (byte == ':') {
|
|
|
|
if (!readBytes) {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (no length specifier)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing :)"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxMessageLength >= 0 && len > maxMessageLength) {
|
|
|
|
std::stringstream errorMessage;
|
|
|
|
errorMessage << "Max data length exceeded: " << (maxMessageLength / 1024) << " KB";
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(errorMessage.str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
String payload;
|
|
|
|
|
|
|
|
if (len) {
|
|
|
|
payload.Append(len, 0);
|
|
|
|
|
|
|
|
asio::mutable_buffer payloadBuf (&*payload.Begin(), payload.GetLength());
|
|
|
|
asio::read(*stream, payloadBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
char trailer = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
asio::mutable_buffer trailerBuf (&trailer, 1);
|
|
|
|
asio::read(*stream, trailerBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trailer != ',') {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing ,)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(payload);
|
|
|
|
}
|
|
|
|
|
2019-02-19 11:20:39 +01:00
|
|
|
/**
|
|
|
|
* Reads data from a stream in netstring format.
|
|
|
|
*
|
|
|
|
* @param stream The stream to read from.
|
|
|
|
* @returns The String that has been read from the IOQueue.
|
|
|
|
* @exception invalid_argument The input stream is invalid.
|
|
|
|
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
|
|
|
|
*/
|
|
|
|
String NetString::ReadStringFromStream(const std::shared_ptr<AsioTlsStream>& stream,
|
|
|
|
boost::asio::yield_context yc, ssize_t maxMessageLength)
|
|
|
|
{
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
|
|
|
size_t len = 0;
|
|
|
|
bool leadingZero = false;
|
|
|
|
|
|
|
|
for (uint_fast8_t readBytes = 0;; ++readBytes) {
|
|
|
|
char byte = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
asio::mutable_buffer byteBuf (&byte, 1);
|
|
|
|
asio::async_read(*stream, byteBuf, yc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isdigit(byte)) {
|
|
|
|
if (readBytes == 9) {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Length specifier must not exceed 9 characters"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leadingZero) {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (leading zero)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
len = len * 10u + size_t(byte - '0');
|
|
|
|
|
|
|
|
if (!readBytes && byte == '0') {
|
|
|
|
leadingZero = true;
|
|
|
|
}
|
|
|
|
} else if (byte == ':') {
|
|
|
|
if (!readBytes) {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (no length specifier)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing :)"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxMessageLength >= 0 && len > maxMessageLength) {
|
|
|
|
std::stringstream errorMessage;
|
|
|
|
errorMessage << "Max data length exceeded: " << (maxMessageLength / 1024) << " KB";
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(errorMessage.str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
String payload;
|
|
|
|
|
|
|
|
if (len) {
|
|
|
|
payload.Append(len, 0);
|
|
|
|
|
|
|
|
asio::mutable_buffer payloadBuf (&*payload.Begin(), payload.GetLength());
|
|
|
|
asio::async_read(*stream, payloadBuf, yc);
|
|
|
|
}
|
|
|
|
|
|
|
|
char trailer = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
asio::mutable_buffer trailerBuf (&trailer, 1);
|
|
|
|
asio::async_read(*stream, trailerBuf, yc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trailer != ',') {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing ,)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(payload);
|
|
|
|
}
|
|
|
|
|
2019-02-25 18:12:32 +01:00
|
|
|
/**
|
|
|
|
* Writes data into a stream using the netstring format and returns bytes written.
|
|
|
|
*
|
|
|
|
* @param stream The stream.
|
|
|
|
* @param str The String that is to be written.
|
|
|
|
*
|
|
|
|
* @return The amount of bytes written.
|
|
|
|
*/
|
|
|
|
size_t NetString::WriteStringToStream(const std::shared_ptr<AsioTlsStream>& stream, const String& str)
|
|
|
|
{
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
WriteStringToStream(msgbuf, str);
|
|
|
|
|
|
|
|
String msg = msgbuf.str();
|
|
|
|
asio::const_buffer msgBuf (msg.CStr(), msg.GetLength());
|
|
|
|
|
|
|
|
asio::write(*stream, msgBuf);
|
|
|
|
|
|
|
|
return msg.GetLength();
|
|
|
|
}
|
|
|
|
|
2019-02-18 15:21:50 +01:00
|
|
|
/**
|
|
|
|
* Writes data into a stream using the netstring format and returns bytes written.
|
|
|
|
*
|
|
|
|
* @param stream The stream.
|
|
|
|
* @param str The String that is to be written.
|
|
|
|
*
|
|
|
|
* @return The amount of bytes written.
|
|
|
|
*/
|
|
|
|
size_t NetString::WriteStringToStream(const std::shared_ptr<AsioTlsStream>& stream, const String& str, boost::asio::yield_context yc)
|
|
|
|
{
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
WriteStringToStream(msgbuf, str);
|
|
|
|
|
|
|
|
String msg = msgbuf.str();
|
|
|
|
asio::const_buffer msgBuf (msg.CStr(), msg.GetLength());
|
|
|
|
|
|
|
|
asio::async_write(*stream, msgBuf, yc);
|
|
|
|
|
|
|
|
return msg.GetLength();
|
|
|
|
}
|
|
|
|
|
2016-08-20 23:46:44 +02:00
|
|
|
/**
|
|
|
|
* Writes data into a stream using the netstring format.
|
|
|
|
*
|
|
|
|
* @param stream The stream.
|
|
|
|
* @param str The String that is to be written.
|
|
|
|
*/
|
|
|
|
void NetString::WriteStringToStream(std::ostream& stream, const String& str)
|
|
|
|
{
|
|
|
|
stream << str.GetLength() << ":" << str << ",";
|
|
|
|
}
|