Jelajahi Sumber

client: add SinglePacketSource

ambrop7 14 tahun lalu
induk
melakukan
ecd08cb3ef
3 mengubah file dengan 145 tambahan dan 0 penghapusan
  1. 1 0
      client/CMakeLists.txt
  2. 78 0
      client/SinglePacketSource.c
  3. 66 0
      client/SinglePacketSource.h

+ 1 - 0
client/CMakeLists.txt

@@ -14,6 +14,7 @@ add_executable(badvpn-client
     DataProtoKeepaliveSource.c
     PeerChat.c
     SCOutmsgEncoder.c
+    SinglePacketSource.c
 )
 target_link_libraries(badvpn-client system flow flowextra tuntap server_conection security ${NSPR_LIBRARIES} ${NSS_LIBRARIES})
 

+ 78 - 0
client/SinglePacketSource.c

@@ -0,0 +1,78 @@
+/**
+ * @file SinglePacketSource.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 <string.h>
+
+#include <misc/debug.h>
+
+#include "SinglePacketSource.h"
+
+static void output_handler_recv (SinglePacketSource *o, uint8_t *data)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    // if we already sent one packet, stop
+    if (o->sent) {
+        return;
+    }
+    
+    // set sent
+    o->sent = 1;
+    
+    // write packet
+    memcpy(data, o->packet, o->packet_len);
+    
+    // done
+    PacketRecvInterface_Done(&o->output, o->packet_len);
+}
+
+void SinglePacketSource_Init (SinglePacketSource *o, uint8_t *packet, int packet_len, BPendingGroup *pg)
+{
+    ASSERT(packet_len >= 0)
+    
+    // init arguments
+    o->packet = packet;
+    o->packet_len = packet_len;
+    
+    // set not sent
+    o->sent = 0;
+    
+    // init output
+    PacketRecvInterface_Init(&o->output, o->packet_len, (PacketRecvInterface_handler_recv)output_handler_recv, o, pg);
+    
+    DebugObject_Init(&o->d_obj);
+}
+
+void SinglePacketSource_Free (SinglePacketSource *o)
+{
+    DebugObject_Free(&o->d_obj);
+    
+    // free output
+    PacketRecvInterface_Free(&o->output);
+}
+
+PacketRecvInterface * SinglePacketSource_GetOutput (SinglePacketSource *o)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    return &o->output;
+}

+ 66 - 0
client/SinglePacketSource.h

@@ -0,0 +1,66 @@
+/**
+ * @file SinglePacketSource.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_SINGLEPACKETSOURCE_H
+#define BADVPN_SINGLEPACKETSOURCE_H
+
+#include <base/DebugObject.h>
+#include <flow/PacketRecvInterface.h>
+
+/**
+ * An object which provides a single packet through {@link PacketRecvInterface}.
+ */
+typedef struct {
+    uint8_t *packet;
+    int packet_len;
+    int sent;
+    PacketRecvInterface output;
+    DebugObject d_obj;
+} SinglePacketSource;
+
+/**
+ * Initializes the object.
+ * 
+ * @param o the object
+ * @param packet packet to provide to the output. Must stay available until the packet is provided.
+ * @param packet_len length of packet. Must be >=0.
+ * @param pg pending group we live in
+ */
+void SinglePacketSource_Init (SinglePacketSource *o, uint8_t *packet, int packet_len, BPendingGroup *pg);
+
+/**
+ * Frees the object.
+ * 
+ * @param o the object
+ */
+void SinglePacketSource_Free (SinglePacketSource *o);
+
+/**
+ * Returns the output interface.
+ * The MTU of the interface will be packet_len.
+ * 
+ * @param o the object
+ * @return output interface
+ */
+PacketRecvInterface * SinglePacketSource_GetOutput (SinglePacketSource *o);
+
+#endif