icinga2/jsonrpc/netstring.cpp

94 lines
3.4 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. *
******************************************************************************/
2012-03-28 13:24:49 +02:00
#include "i2-jsonrpc.h"
using namespace icinga;
2012-05-18 22:53:35 +02:00
/**
* Reads data from a FIFO in netstring format.
*
* @param fifo The FIFO to read from.
* @param[out] str The string that has been read from the FIFO.
* @returns true if a complete string was read from the FIFO, false otherwise.
* @exception InvalidNetstringException The input stream is invalid.
* @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
*/
bool Netstring::ReadStringFromFIFO(FIFO::Ptr fifo, string *str)
2012-03-28 13:24:49 +02:00
{
size_t buffer_length = fifo->GetSize();
2012-03-28 21:20:13 +02:00
char *buffer = (char *)fifo->GetReadBuffer();
2012-03-28 13:24:49 +02:00
/* minimum netstring length is 3 */
if (buffer_length < 3)
2012-04-18 15:22:25 +02:00
return false;
2012-03-28 13:24:49 +02:00
/* no leading zeros allowed */
if (buffer[0] == '0' && isdigit(buffer[1]))
2012-05-26 21:30:04 +02:00
throw invalid_argument("Invalid netstring (leading zero)");
2012-03-28 13:24:49 +02:00
size_t len, i;
len = 0;
for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) {
/* length specifier must have at most 9 characters */
if (i >= 9)
2012-05-26 21:30:04 +02:00
throw invalid_argument("Length specifier must not exceed 9 characters");
2012-03-28 13:24:49 +02:00
len = len * 10 + (buffer[i] - '0');
}
/* make sure the buffer is large enough */
if (i + len + 1 >= buffer_length)
2012-04-18 15:22:25 +02:00
return false;
2012-03-28 13:24:49 +02:00
/* check for the colon delimiter */
if (buffer[i++] != ':')
2012-05-26 21:30:04 +02:00
throw invalid_argument("Invalid Netstring (missing :)");
2012-03-28 13:24:49 +02:00
/* check for the comma delimiter after the string */
if (buffer[i + len] != ',')
2012-05-26 21:30:04 +02:00
throw invalid_argument("Invalid Netstring (missing ,)");
2012-03-28 13:24:49 +02:00
*str = string(&buffer[i], &buffer[i + len]);
2012-03-28 13:24:49 +02:00
/* remove the data from the fifo */
fifo->Read(NULL, i + len + 1);
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
/**
* Writes data into a FIFO using the netstring format.
*
* @param fifo The FIFO.
* @param str The string that is to be written.
*/
void Netstring::WriteStringToFIFO(FIFO::Ptr fifo, const string& str)
2012-03-28 13:24:49 +02:00
{
2012-06-16 03:42:54 +02:00
stringstream prefixbuf;
prefixbuf << str.size() << ":";
2012-03-28 13:24:49 +02:00
2012-06-16 03:42:54 +02:00
string prefix = prefixbuf.str();
fifo->Write(prefix.c_str(), prefix.size());
fifo->Write(str.c_str(), str.size());
2012-03-28 13:24:49 +02:00
fifo->Write(",", 1);
}