Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ambrop72/badvpn.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/flow
diff options
context:
space:
mode:
authorambrop7 <ambrop7@1a93d707-3861-5ebc-ad3b-9740d49b5140>2011-07-12 16:44:43 +0400
committerambrop7 <ambrop7@1a93d707-3861-5ebc-ad3b-9740d49b5140>2011-07-12 16:44:43 +0400
commitb7d4a7cd885758e2ebf2a1142a83fd3603257784 (patch)
tree33f0994ac9fec294407aacc7a3aaabaef1270e99 /flow
parente1ba97e3c3a396c317d98304931fbf4abe829ca6 (diff)
flow: add StreamPacketSender
Diffstat (limited to 'flow')
-rw-r--r--flow/CMakeLists.txt1
-rw-r--r--flow/StreamPacketSender.c83
-rw-r--r--flow/StreamPacketSender.h70
3 files changed, 154 insertions, 0 deletions
diff --git a/flow/CMakeLists.txt b/flow/CMakeLists.txt
index 9ccc945..1b49708 100644
--- a/flow/CMakeLists.txt
+++ b/flow/CMakeLists.txt
@@ -24,5 +24,6 @@ add_library(flow
LineBuffer.c
SingleStreamSender.c
SingleStreamReceiver.c
+ StreamPacketSender.c
)
target_link_libraries(flow base)
diff --git a/flow/StreamPacketSender.c b/flow/StreamPacketSender.c
new file mode 100644
index 0000000..1dd21e1
--- /dev/null
+++ b/flow/StreamPacketSender.c
@@ -0,0 +1,83 @@
+/**
+ * @file StreamPacketSender.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ *
+ * @section LICENSE
+ *
+ * This file is part of BadVPN.
+ *
+ * BadVPN is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * BadVPN 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, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <misc/debug.h>
+
+#include "StreamPacketSender.h"
+
+static void input_handler_send (StreamPacketSender *o, uint8_t *data, int data_len)
+{
+ DebugObject_Access(&o->d_obj);
+ ASSERT(data_len > 0)
+
+ // limit length to MTU and remember
+ if (data_len > o->output_mtu) {
+ o->sending_len = o->output_mtu;
+ } else {
+ o->sending_len = data_len;
+ }
+
+ // send
+ PacketPassInterface_Sender_Send(o->output, data, o->sending_len);
+}
+
+static void output_handler_done (StreamPacketSender *o)
+{
+ DebugObject_Access(&o->d_obj);
+
+ // done
+ StreamPassInterface_Done(&o->input, o->sending_len);
+}
+
+void StreamPacketSender_Init (StreamPacketSender *o, PacketPassInterface *output, BPendingGroup *pg)
+{
+ ASSERT(PacketPassInterface_GetMTU(output) > 0)
+
+ // init arguments
+ o->output = output;
+
+ // remember output MTU
+ o->output_mtu = PacketPassInterface_GetMTU(output);
+
+ // init input
+ StreamPassInterface_Init(&o->input, (StreamPassInterface_handler_send)input_handler_send, o, pg);
+
+ // init output
+ PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
+
+ DebugObject_Init(&o->d_obj);
+}
+
+void StreamPacketSender_Free (StreamPacketSender *o)
+{
+ DebugObject_Free(&o->d_obj);
+
+ // free input
+ StreamPassInterface_Free(&o->input);
+}
+
+StreamPassInterface * StreamPacketSender_GetInput (StreamPacketSender *o)
+{
+ DebugObject_Access(&o->d_obj);
+
+ return &o->input;
+}
diff --git a/flow/StreamPacketSender.h b/flow/StreamPacketSender.h
new file mode 100644
index 0000000..43792bc
--- /dev/null
+++ b/flow/StreamPacketSender.h
@@ -0,0 +1,70 @@
+/**
+ * @file StreamPacketSender.h
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ *
+ * @section LICENSE
+ *
+ * This file is part of BadVPN.
+ *
+ * BadVPN is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * BadVPN 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, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef BADVPN_STREAMPACKETSENDER_H
+#define BADVPN_STREAMPACKETSENDER_H
+
+#include <base/DebugObject.h>
+#include <flow/StreamPassInterface.h>
+#include <flow/PacketPassInterface.h>
+
+/**
+ * Object which breaks an input stream into output packets. The resulting
+ * packets will have positive length, and, when concatenated, will form the
+ * original stream.
+ *
+ * Input is with {@link StreamPassInterface}.
+ * Output is with {@link PacketPassInterface}.
+ */
+typedef struct {
+ PacketPassInterface *output;
+ int output_mtu;
+ StreamPassInterface input;
+ int sending_len;
+ DebugObject d_obj;
+} StreamPacketSender;
+
+/**
+ * Initializes the object.
+ *
+ * @param o the object
+ * @param output output interface. Its MTU must be >0.
+ * @param pg pending group we live in
+ */
+void StreamPacketSender_Init (StreamPacketSender *o, PacketPassInterface *output, BPendingGroup *pg);
+
+/**
+ * Frees the object.
+ *
+ * @param o the object
+ */
+void StreamPacketSender_Free (StreamPacketSender *o);
+
+/**
+ * Returns the input interface.
+ *
+ * @param o the object
+ * @return input interface
+ */
+StreamPassInterface * StreamPacketSender_GetInput (StreamPacketSender *o);
+
+#endif