]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/libs/net.c
Lua: Avoid segmentation fault on recv() fail
[vlc] / modules / misc / lua / libs / net.c
index 90eec66871be3b77bd3fa79fbc4127ee0fe6f638..b5227d2ca60c95609831f365e145b7c0317263cd 100644 (file)
@@ -45,6 +45,7 @@
 #endif
 #include <sys/stat.h>
 
+#include<errno.h>
 #include "../vlc.h"
 #include "../libs.h"
 
@@ -150,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 );
@@ -174,8 +185,13 @@ static int vlclua_net_recv( lua_State *L )
     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 );
-    return 1;
+    if( i_len > 0 )
+    {
+        lua_pushlstring( L, psz_buffer, i_len );
+        return 1;
+    }
+    else
+        return 0;
 }
 
 /*****************************************************************************
@@ -339,6 +355,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 },