icinga2/lib/base/netstring.cpp

135 lines
4.2 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2013-03-16 21:18:53 +01:00
#include "base/netstring.h"
2013-04-04 16:08:02 +02:00
#include "base/utility.h"
2013-03-16 21:18:53 +01:00
#include <sstream>
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.
* @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
*/
2012-11-22 12:04:32 +01:00
bool NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str)
2012-03-28 13:24:49 +02:00
{
/* 16 bytes are enough for the header */
2013-04-04 16:08:02 +02:00
const size_t header_length = 16;
size_t read_length;
char *header = static_cast<char *>(malloc(header_length));
2012-07-17 20:41:06 +02:00
2013-04-04 16:08:02 +02:00
if (header == NULL)
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::bad_alloc());
2012-07-17 20:41:06 +02:00
2013-04-04 16:08:02 +02:00
read_length = 0;
2013-04-04 16:08:02 +02:00
while (read_length < header_length) {
/* Read one byte. */
int rc = stream->Read(header + read_length, 1);
if (rc == 0) {
if (read_length == 0)
return false;
BOOST_THROW_EXCEPTION(std::runtime_error("Read() failed."));
}
ASSERT(rc == 1);
read_length++;
if (header[read_length - 1] == ':') {
break;
} else if (header_length == read_length) {
free(header);
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing :)"));
}
}
/* minimum netstring length is 3 */
if (read_length < 3) {
free(header);
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (short header)"));
}
2012-03-28 13:24:49 +02:00
/* no leading zeros allowed */
2013-04-04 16:08:02 +02:00
if (header[0] == '0' && isdigit(header[1])) {
free(header);
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (leading zero)"));
}
2012-03-28 13:24:49 +02:00
size_t len, i;
len = 0;
2013-04-04 16:08:02 +02:00
for (i = 0; i < read_length && isdigit(header[i]); i++) {
2012-03-28 13:24:49 +02:00
/* length specifier must have at most 9 characters */
if (i >= 9) {
2013-04-04 16:08:02 +02:00
free(header);
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
2013-04-04 16:08:02 +02:00
len = len * 10 + (header[i] - '0');
2012-03-28 13:24:49 +02:00
}
2013-04-04 16:08:02 +02:00
free(header);
/* read the whole message */
2013-04-04 16:08:02 +02:00
size_t data_length = len + 1;
2013-04-04 16:08:02 +02:00
char *data = static_cast<char *>(malloc(data_length));
2013-04-04 16:08:02 +02:00
if (data == NULL) {
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::bad_alloc());
}
2013-04-04 16:08:02 +02:00
size_t rc = stream->Read(data, data_length);
if (rc != data_length)
BOOST_THROW_EXCEPTION(std::runtime_error("Read() failed."));
2012-03-28 13:24:49 +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 ,)"));
2013-04-04 16:08:02 +02:00
*str = String(&data[0], &data[len]);
2012-03-28 13:24:49 +02:00
2013-04-04 16:08:02 +02:00
free(data);
2012-03-28 13:24:49 +02:00
2012-04-18 15:22:25 +02:00
return true;
2012-03-28 13:24:49 +02:00
}
2012-05-18 22:53:35 +02:00
/**
2013-04-04 16:08:02 +02:00
* Writes data into a stream using the netstring format.
2012-05-18 22:53:35 +02:00
*
2012-11-22 12:04:32 +01:00
* @param stream The stream.
* @param str The String that is to be written.
2012-05-18 22:53:35 +02:00
*/
2012-11-22 12:04:32 +01:00
void 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;
msgbuf << str.GetLength() << ":" << 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());
2012-03-28 13:24:49 +02:00
}