[vlc-devel] [PATCH 1/1] udp: support packets dump in aceess module

Tzu-Jung Lee roylee17 at gmail.com
Thu Jul 11 13:10:02 CEST 2013


Signed-off-by: Tzu-Jung Lee <tjlee at ambarella.com>
---
 modules/access/udp.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 51 insertions(+), 6 deletions(-)

diff --git a/modules/access/udp.c b/modules/access/udp.c
index 6613ebe..bf00959 100644
--- a/modules/access/udp.c
+++ b/modules/access/udp.c
@@ -36,16 +36,24 @@
 # include "config.h"
 #endif
 
+#include <fcntl.h>
+
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_access.h>
 #include <vlc_network.h>
+#include <vlc_fs.h>
 
 #define MTU 65535
+#define CFG_PREFIX "udp-"
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
+#define DUMP_TEXT N_("Dump packets to a file")
+#define DUMP_LONGTEXT N_("Dump the exact packets we sent to a file for " \
+                          "debugging or diagnostic purpose." )
+
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
@@ -59,10 +67,17 @@ vlc_module_begin ()
 
     set_capability( "access", 0 )
     add_shortcut( "udp", "udpstream", "udp4", "udp6" )
+    add_string (CFG_PREFIX "dump", NULL, DUMP_TEXT, DUMP_LONGTEXT, true)
 
     set_callbacks( Open, Close )
 vlc_module_end ()
 
+struct access_sys_t
+{
+    int           i_handle;
+    int           i_fd;
+};
+
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -75,12 +90,19 @@ static int Control( access_t *, int, va_list );
 static int Open( vlc_object_t *p_this )
 {
     access_t     *p_access = (access_t*)p_this;
+    access_sys_t *p_sys;
 
     char *psz_name = strdup( p_access->psz_location );
     char *psz_parser;
     const char *psz_server_addr, *psz_bind_addr = "";
     int  i_bind_port = 1234, i_server_port = 0;
-    int fd;
+    int i_handle;
+    int i_fd = -1;
+
+    if( !( p_sys = malloc ( sizeof( *p_sys ) ) ) )
+        return VLC_ENOMEM;
+
+    p_access->p_sys = p_sys;
 
     /* Set up p_access */
     access_InitFields( p_access );
@@ -128,15 +150,34 @@ static int Open( vlc_object_t *p_this )
     msg_Dbg( p_access, "opening server=%s:%d local=%s:%d",
              psz_server_addr, i_server_port, psz_bind_addr, i_bind_port );
 
-    fd = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
+    i_handle = net_OpenDgram( p_access, psz_bind_addr, i_bind_port,
                         psz_server_addr, i_server_port, IPPROTO_UDP );
     free (psz_name);
-    if( fd == -1 )
+    if( i_handle == -1 )
     {
         msg_Err( p_access, "cannot open socket" );
         return VLC_EGENERIC;
     }
-    p_access->p_sys = (void *)(intptr_t)fd;
+
+    const char *filename = var_InheritString (p_access, CFG_PREFIX "dump");
+    if ( filename )
+    {
+        i_fd = vlc_open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
+        if ( i_fd < 0 )
+        {
+            msg_Err( p_access, "cannot open %s for dumping received packets",
+                     filename);
+            return VLC_EGENERIC;
+        }
+        msg_Info( p_access, "dump received packets into %s", filename);
+    }
+    else
+    {
+        msg_Info( p_access, "NO dump");
+    }
+
+    p_sys->i_handle = i_handle;
+    p_sys->i_fd = i_fd;
 
     return VLC_SUCCESS;
 }
@@ -198,14 +239,15 @@ static int Control( access_t *p_access, int i_query, va_list args )
  *****************************************************************************/
 static block_t *BlockUDP( access_t *p_access )
 {
-    int fd = (intptr_t)p_access->p_sys;
+    int i_handle = p_access->p_sys->i_handle;
+    int i_fd = p_access->p_sys->i_fd;
 
     /* Read data */
     block_t *p_block = block_Alloc( MTU );
     if( unlikely(p_block == NULL) )
         return NULL;
 
-    ssize_t len = net_Read( p_access, fd, NULL,
+    ssize_t len = net_Read( p_access, i_handle, NULL,
                             p_block->p_buffer, MTU, false );
     if( len < 0 )
     {
@@ -213,5 +255,8 @@ static block_t *BlockUDP( access_t *p_access )
         return NULL;
     }
 
+    if( i_fd != -1 )
+        write(i_fd, p_block->p_buffer, len);
+
     return block_Realloc( p_block, 0, len );
 }
-- 
1.8.2.1




More information about the vlc-devel mailing list