1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2022 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <openvpn/common/size.hpp>
#include <openvpn/common/hexstr.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/frame/frame.hpp>
namespace openvpn {
namespace WS {
class ChunkedHelper
{
enum State
{
hex,
post_hex,
post_hex_lf,
post_chunk_cr,
post_chunk_lf,
post_content_cr,
post_content_lf,
done,
chunk,
};
public:
ChunkedHelper()
: state(hex),
size(0)
{
}
template <typename PARENT>
bool receive(PARENT &callback, BufferAllocated &buf)
{
while (buf.defined())
{
if (state == chunk)
{
if (size)
{
if (buf.size() <= size)
{
size -= buf.size();
callback.chunked_content_in(buf);
break;
}
else
{
BufferAllocated content(buf.read_alloc(size), size, 0);
size = 0;
callback.chunked_content_in(content);
}
}
else
state = post_chunk_cr;
}
else if (state == done)
break;
else
{
const char c = char(buf.pop_front());
reprocess:
switch (state)
{
case hex:
{
const int v = parse_hex_char(c);
if (v >= 0)
size = (size << 4) + v;
else
{
state = post_hex;
goto reprocess;
}
}
break;
case post_hex:
if (c == '\r')
state = post_hex_lf;
break;
case post_hex_lf:
if (c == '\n')
{
if (size)
state = chunk;
else
state = post_content_cr;
}
else
{
state = post_hex;
goto reprocess;
}
break;
case post_chunk_cr:
if (c == '\r')
state = post_chunk_lf;
break;
case post_chunk_lf:
if (c == '\n')
state = hex;
else
{
state = post_chunk_cr;
goto reprocess;
}
break;
case post_content_cr:
if (c == '\r')
state = post_content_lf;
break;
case post_content_lf:
if (c == '\n')
state = done;
else
{
state = post_content_cr;
goto reprocess;
}
break;
default: // should never be reached
break;
}
}
}
return state == done;
}
static BufferPtr transmit(BufferPtr buf)
{
const size_t headroom = 24;
const size_t tailroom = 16;
static const char crlf[] = "\r\n";
if (!buf || buf->offset() < headroom || buf->remaining() < tailroom)
{
// insufficient headroom/tailroom, must realloc
Frame::Context fc(headroom, 0, tailroom, 0, sizeof(size_t), 0);
buf = fc.copy(buf);
}
size_t size = buf->size();
buf->prepend((unsigned char *)crlf, 2);
if (size)
{
while (size)
{
char *pc = (char *)buf->prepend_alloc(1);
*pc = render_hex_char(size & 0xF);
size >>= 4;
}
}
else
{
char *pc = (char *)buf->prepend_alloc(1);
*pc = '0';
}
buf->write((unsigned char *)crlf, 2);
return buf;
}
private:
State state;
size_t size;
};
} // namespace WS
} // namespace openvpn
|