Skip to content

Commit b273c6b

Browse files
committed
feat(device): add ValentPacketInputStream
Add a packet reading stream, both for convenience and to handle untrusted sources that may attempt DoS attacks by overfilling the input buffer. When a source is verified, the stream can be marked as trusted, allowing the buffer to expand as necessary.
1 parent 9282ac6 commit b273c6b

4 files changed

Lines changed: 503 additions & 30 deletions

File tree

src/libvalent/device/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ libvalent_device_public_headers = [
2323
libvalent_device_private_headers = [
2424
'valent-device-impl.h',
2525
'valent-device-private.h',
26+
'valent-packet-input-stream.h',
2627
]
2728

2829
libvalent_device_enum_headers = [
@@ -46,6 +47,7 @@ libvalent_device_public_sources = [
4647
'valent-device-plugin.c',
4748
'valent-device-transfer.c',
4849
'valent-packet.c',
50+
'valent-packet-input-stream.c',
4951
]
5052

5153

src/libvalent/device/valent-channel.c

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "valent-certificate.h"
1515
#include "valent-packet.h"
16+
#include "valent-packet-input-stream.h"
1617

1718
#include "valent-channel.h"
1819

@@ -55,7 +56,7 @@ typedef struct
5556
JsonNode *peer_identity;
5657

5758
/* Packet Buffer */
58-
GDataInputStream *input_buffer;
59+
ValentPacketInputStream *input_buffer;
5960
GMainLoop *output_buffer;
6061
} ValentChannelPrivate;
6162

@@ -278,10 +279,13 @@ valent_channel_set_base_stream (ValentChannel *self,
278279
g_autoptr (GThread) thread = NULL;
279280
g_autoptr (GError) error = NULL;
280281

282+
// FIXME: associate the device paired state with the channel trust
281283
input_stream = g_io_stream_get_input_stream (base_stream);
282-
priv->input_buffer = g_object_new (G_TYPE_DATA_INPUT_STREAM,
284+
priv->input_buffer = g_object_new (VALENT_TYPE_PACKET_INPUT_STREAM,
283285
"base-stream", input_stream,
286+
"buffer-size", VALENT_PACKET_DEFAULT_BUFFER_SIZE,
284287
"close-base-stream", FALSE,
288+
"trusted", TRUE,
285289
NULL);
286290

287291
context = g_main_context_new ();
@@ -761,34 +765,15 @@ valent_channel_close_finish (ValentChannel *channel,
761765
}
762766

763767
static void
764-
valent_channel_read_packet_cb (GObject *object,
765-
GAsyncResult *result,
766-
gpointer user_data)
768+
valent_channel_read_packet_cb (ValentPacketInputStream *stream,
769+
GAsyncResult *result,
770+
gpointer user_data)
767771
{
768772
g_autoptr (GTask) task = G_TASK (g_steal_pointer (&user_data));
769-
g_autofree char *line = NULL;
770773
JsonNode *packet = NULL;
771774
GError *error = NULL;
772775

773-
line = g_data_input_stream_read_line_finish_utf8 (G_DATA_INPUT_STREAM (object),
774-
result,
775-
NULL,
776-
&error);
777-
if (error != NULL)
778-
{
779-
g_task_return_error (task, g_steal_pointer (&error));
780-
return;
781-
}
782-
else if (line == NULL)
783-
{
784-
g_task_return_new_error (task,
785-
G_IO_ERROR,
786-
G_IO_ERROR_CONNECTION_CLOSED,
787-
"Channel is closed");
788-
return;
789-
}
790-
791-
packet = valent_packet_deserialize (line, &error);
776+
packet = valent_packet_input_stream_read_packet_finish (stream, result, &error);
792777
if (packet == NULL)
793778
{
794779
g_task_return_error (task, g_steal_pointer (&error));
@@ -832,11 +817,10 @@ valent_channel_read_packet (ValentChannel *channel,
832817

833818
if (!valent_channel_return_error_if_closed (channel, task))
834819
{
835-
g_data_input_stream_read_line_async (priv->input_buffer,
836-
G_PRIORITY_DEFAULT,
837-
cancellable,
838-
valent_channel_read_packet_cb,
839-
g_object_ref (task));
820+
valent_packet_input_stream_read_packet_async (priv->input_buffer,
821+
cancellable,
822+
(GAsyncReadyCallback)valent_channel_read_packet_cb,
823+
g_object_ref (task));
840824
valent_object_unlock (VALENT_OBJECT (channel));
841825
}
842826

0 commit comments

Comments
 (0)