2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-07-24 13:13:02 +02:00
|
|
|
#include "i2-base.h"
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-18 22:53:35 +02:00
|
|
|
/**
|
2012-11-22 12:04:32 +01: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.
|
2012-08-02 09:38:08 +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
|
|
|
{
|
2013-01-30 10:52:52 +01:00
|
|
|
/* 16 bytes are enough for the header */
|
|
|
|
size_t peek_length, buffer_length = 16;
|
2012-08-08 08:34:15 +02:00
|
|
|
char *buffer = static_cast<char *>(malloc(buffer_length));
|
2012-07-17 20:41:06 +02:00
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
if (buffer == NULL)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(bad_alloc());
|
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
peek_length = stream->Peek(buffer, buffer_length);
|
2012-11-23 11:02:34 +01:00
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
/* minimum netString length is 3 */
|
|
|
|
if (peek_length < 3) {
|
2012-11-23 11:02:34 +01:00
|
|
|
free(buffer);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-16 00:05:24 +02:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
/* no leading zeros allowed */
|
2012-07-16 00:05:24 +02:00
|
|
|
if (buffer[0] == '0' && isdigit(buffer[1])) {
|
|
|
|
free(buffer);
|
2012-08-02 09:38:08 +02:00
|
|
|
throw_exception(invalid_argument("Invalid netString (leading zero)"));
|
2012-07-16 00:05:24 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
size_t len, i;
|
|
|
|
|
|
|
|
len = 0;
|
2013-01-30 10:52:52 +01:00
|
|
|
for (i = 0; i < peek_length && isdigit(buffer[i]); i++) {
|
2012-03-28 13:24:49 +02:00
|
|
|
/* length specifier must have at most 9 characters */
|
2012-07-16 00:05:24 +02:00
|
|
|
if (i >= 9) {
|
|
|
|
free(buffer);
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Length specifier must not exceed 9 characters"));
|
2012-07-16 00:05:24 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
len = len * 10 + (buffer[i] - '0');
|
|
|
|
}
|
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
/* read the whole message */
|
2012-07-16 00:05:24 +02:00
|
|
|
buffer_length = i + 1 + len + 1;
|
|
|
|
|
2012-08-08 08:34:15 +02:00
|
|
|
char *new_buffer = static_cast<char *>(realloc(buffer, buffer_length));
|
2012-07-16 00:05:24 +02:00
|
|
|
|
|
|
|
if (new_buffer == NULL) {
|
|
|
|
free(buffer);
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(bad_alloc());
|
2012-07-16 00:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = new_buffer;
|
|
|
|
|
2013-01-30 10:52:52 +01:00
|
|
|
peek_length = stream->Peek(buffer, buffer_length);
|
|
|
|
|
|
|
|
if (peek_length < buffer_length)
|
|
|
|
return false;
|
2012-07-16 00:05:24 +02:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
/* check for the colon delimiter */
|
2012-07-16 00:05:24 +02:00
|
|
|
if (buffer[i] != ':') {
|
|
|
|
free(buffer);
|
2012-08-02 09:38:08 +02:00
|
|
|
throw_exception(invalid_argument("Invalid NetString (missing :)"));
|
2012-07-16 00:05:24 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
/* check for the comma delimiter after the String */
|
2012-07-16 00:05:24 +02:00
|
|
|
if (buffer[i + 1 + len] != ',') {
|
|
|
|
free(buffer);
|
2012-08-02 09:38:08 +02:00
|
|
|
throw_exception(invalid_argument("Invalid NetString (missing ,)"));
|
2012-07-16 00:05:24 +02:00
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
*str = String(&buffer[i + 1], &buffer[i + 1 + len]);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-07-16 00:05:24 +02:00
|
|
|
free(buffer);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
/* remove the data from the stream */
|
2013-01-30 10:52:52 +01:00
|
|
|
stream->Read(NULL, peek_length);
|
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
|
|
|
/**
|
2012-11-22 12:04:32 +01: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.
|
2012-08-02 09:38:08 +02:00
|
|
|
* @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
|
|
|
{
|
2012-06-16 03:42:54 +02:00
|
|
|
stringstream prefixbuf;
|
2012-08-02 09:38:08 +02:00
|
|
|
prefixbuf << str.GetLength() << ":";
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String prefix = prefixbuf.str();
|
2012-11-22 12:04:32 +01:00
|
|
|
stream->Write(prefix.CStr(), prefix.GetLength());
|
|
|
|
stream->Write(str.CStr(), str.GetLength());
|
|
|
|
stream->Write(",", 1);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|