]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/libs/net.c
lua/net.c: revert [14707cbaca9fa] and fix properly
[vlc] / modules / misc / lua / libs / net.c
index ebe5b7852553c992d25de5be2bb069d82573c470..c23f4756b5f3716c5ed9dfc39595a4bbed8da5ad 100644 (file)
@@ -35,6 +35,7 @@
 #include <vlc_common.h>
 #include <vlc_network.h>
 #include <vlc_url.h>
+#include <vlc_fs.h>
 
 #include <lua.h>        /* Low level lua C API */
 #include <lauxlib.h>    /* Higher level C API */
@@ -42,7 +43,9 @@
 #ifdef HAVE_POLL
 #include <poll.h>       /* poll structures and defines */
 #endif
+#include <sys/stat.h>
 
+#include<errno.h>
 #include "../vlc.h"
 #include "../libs.h"
 
@@ -148,6 +151,16 @@ static int vlclua_net_accept( lua_State *L )
 /*****************************************************************************
  *
  *****************************************************************************/
+static int vlclua_net_connect_tcp( lua_State *L )
+{
+    vlc_object_t *p_this = vlclua_get_this( L );
+    const char *psz_host = luaL_checkstring( L, 1 );
+    int i_port = luaL_checkint( L, 2 );
+    int i_fd = net_Connect( p_this, psz_host, i_port, SOCK_STREAM, IPPROTO_TCP );
+    lua_pushinteger( L, i_fd );
+    return 1;
+}
+
 static int vlclua_net_close( lua_State *L )
 {
     int i_fd = luaL_checkint( L, 1 );
@@ -171,8 +184,8 @@ static int vlclua_net_recv( lua_State *L )
     int i_fd = luaL_checkint( L, 1 );
     size_t i_len = luaL_optint( L, 2, 1 );
     char psz_buffer[i_len];
-    i_len = recv( i_fd, psz_buffer, i_len, 0 );
-    lua_pushlstring( L, psz_buffer, i_len );
+    ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
+    lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
     return 1;
 }
 
@@ -193,6 +206,7 @@ static int vlclua_net_poll( lua_State *L )
         lua_pop( L, 1 );
     }
     struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
+    vlc_cleanup_push( free, p_fds );
     lua_pushnil( L );
     int i = 0;
     while( lua_next( L, 1 ) )
@@ -211,8 +225,8 @@ static int vlclua_net_poll( lua_State *L )
         lua_pushinteger( L, p_fds[i].revents );
         lua_settable( L, 1 );
     }
-    free( p_fds );
     lua_pushinteger( L, i_ret );
+    vlc_cleanup_run();
     return 1;
 }
 
@@ -242,8 +256,8 @@ static int vlclua_fd_read( lua_State *L )
     int i_fd = luaL_checkint( L, 1 );
     size_t i_len = luaL_optint( L, 2, 1 );
     char psz_buffer[i_len];
-    i_len = read( i_fd, psz_buffer, i_len );
-    lua_pushlstring( L, psz_buffer, i_len );
+    ssize_t i_ret = read( i_fd, psz_buffer, i_len );
+    lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
     return 1;
 }
 
@@ -255,36 +269,36 @@ static int vlclua_stat( lua_State *L )
 #ifdef HAVE_SYS_STAT_H
     const char *psz_path = luaL_checkstring( L, 1 );
     struct stat s;
-    if( utf8_stat( psz_path, &s ) )
+    if( vlc_stat( psz_path, &s ) )
         return 0;
         //return luaL_error( L, "Couldn't stat %s.", psz_path );
     lua_newtable( L );
     if( S_ISREG( s.st_mode ) )
-        lua_pushstring( L, "file" );
+        lua_pushliteral( L, "file" );
     else if( S_ISDIR( s.st_mode ) )
-        lua_pushstring( L, "dir" );
+        lua_pushliteral( L, "dir" );
 #ifdef S_ISCHR
     else if( S_ISCHR( s.st_mode ) )
-        lua_pushstring( L, "character device" );
+        lua_pushliteral( L, "character device" );
 #endif
 #ifdef S_ISBLK
     else if( S_ISBLK( s.st_mode ) )
-        lua_pushstring( L, "block device" );
+        lua_pushliteral( L, "block device" );
 #endif
 #ifdef S_ISFIFO
     else if( S_ISFIFO( s.st_mode ) )
-        lua_pushstring( L, "fifo" );
+        lua_pushliteral( L, "fifo" );
 #endif
 #ifdef S_ISLNK
     else if( S_ISLNK( s.st_mode ) )
-        lua_pushstring( L, "symbolic link" );
+        lua_pushliteral( L, "symbolic link" );
 #endif
 #ifdef S_ISSOCK
     else if( S_ISSOCK( s.st_mode ) )
-        lua_pushstring( L, "socket" );
+        lua_pushliteral( L, "socket" );
 #endif
     else
-        lua_pushstring( L, "unknown" );
+        lua_pushliteral( L, "unknown" );
     lua_setfield( L, -2, "type" );
     lua_pushinteger( L, s.st_mode );
     lua_setfield( L, -2, "mode" );
@@ -313,13 +327,13 @@ static int vlclua_opendir( lua_State *L )
     DIR *p_dir;
     int i = 0;
 
-    if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
+    if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
 
     lua_newtable( L );
     for( ;; )
     {
-        char *psz_filename = utf8_readdir( p_dir );
+        char *psz_filename = vlc_readdir( p_dir );
         if( !psz_filename ) break;
         i++;
         lua_pushstring( L, psz_filename );
@@ -336,6 +350,7 @@ static int vlclua_opendir( lua_State *L )
 static const luaL_Reg vlclua_net_reg[] = {
     { "url_parse", vlclua_url_parse },
     { "listen_tcp", vlclua_net_listen_tcp },
+    { "connect_tcp", vlclua_net_connect_tcp },
     { "close", vlclua_net_close },
     { "send", vlclua_net_send },
     { "recv", vlclua_net_recv },