1 /*****************************************************************************
2 * net.c: Network related functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_network.h>
35 #include <lua.h> /* Low level lua C API */
36 #include <lauxlib.h> /* Higher level C API */
37 #include <lualib.h> /* Lua libs */
41 /*****************************************************************************
43 *****************************************************************************/
44 int vlclua_url_parse( lua_State *L )
46 const char *psz_url = luaL_checkstring( L, 1 );
47 const char *psz_option = luaL_optstring( L, 2, NULL );
50 vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
53 lua_pushstring( L, url.psz_protocol );
54 lua_setfield( L, -2, "protocol" );
55 lua_pushstring( L, url.psz_username );
56 lua_setfield( L, -2, "username" );
57 lua_pushstring( L, url.psz_password );
58 lua_setfield( L, -2, "password" );
59 lua_pushstring( L, url.psz_host );
60 lua_setfield( L, -2, "host" );
61 lua_pushinteger( L, url.i_port );
62 lua_setfield( L, -2, "port" );
63 lua_pushstring( L, url.psz_path );
64 lua_setfield( L, -2, "path" );
65 lua_pushstring( L, url.psz_option );
66 lua_setfield( L, -2, "option" );
73 int vlclua_net_listen_tcp( lua_State *L )
75 vlc_object_t *p_this = vlclua_get_this( L );
76 const char *psz_host = luaL_checkstring( L, 1 );
77 int i_port = luaL_checkint( L, 2 );
78 int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
80 return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
81 lua_pushlightuserdata( L, pi_fd );
85 int vlclua_net_listen_close( lua_State *L )
87 int *pi_fd = (int*)luaL_checklightuserdata( L, 1 );
88 net_ListenClose( pi_fd );
92 int vlclua_net_accept( lua_State *L )
94 vlc_object_t *p_this = vlclua_get_this( L );
95 int *pi_fd = (int*)luaL_checklightuserdata( L, 1 );
96 mtime_t i_wait = luaL_optint( L, 2, -1 ); /* default to block */
97 int i_fd = net_Accept( p_this, pi_fd, i_wait );
98 lua_pushinteger( L, i_fd );
102 int vlclua_net_close( lua_State *L )
104 int i_fd = luaL_checkint( L, 1 );
109 int vlclua_net_send( lua_State *L )
111 int i_fd = luaL_checkint( L, 1 );
113 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
114 i_len = luaL_optint( L, 3, i_len );
115 i_len = send( i_fd, psz_buffer, i_len, 0 );
116 lua_pushinteger( L, i_len );
120 int vlclua_net_recv( lua_State *L )
122 int i_fd = luaL_checkint( L, 1 );
123 size_t i_len = luaL_optint( L, 2, 1 );
124 char psz_buffer[i_len];
125 i_len = recv( i_fd, psz_buffer, i_len, 0 );
126 lua_pushlstring( L, psz_buffer, i_len );
130 int vlclua_net_select( lua_State *L )
133 int i_nfds = luaL_checkint( L, 1 );
134 fd_set *fds_read = (fd_set*)luaL_checkuserdata( L, 2, sizeof( fd_set ) );
135 fd_set *fds_write = (fd_set*)luaL_checkuserdata( L, 3, sizeof( fd_set ) );
136 double f_timeout = luaL_checknumber( L, 4 );
137 struct timeval timeout;
138 timeout.tv_sec = (int)f_timeout;
139 timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
140 i_ret = select( i_nfds, fds_read, fds_write, 0, &timeout );
141 lua_pushinteger( L, i_ret );
142 lua_pushinteger( L, (double)timeout.tv_sec+((double)timeout.tv_usec)/1e-6 );
146 int vlclua_fd_set_new( lua_State *L )
148 fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
153 int vlclua_fd_clr( lua_State *L )
155 fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
156 int i_fd = luaL_checkint( L, 2 );
160 int vlclua_fd_isset( lua_State *L )
162 fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
163 int i_fd = luaL_checkint( L, 2 );
164 lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
167 int vlclua_fd_set( lua_State *L )
169 fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
170 int i_fd = luaL_checkint( L, 2 );
174 int vlclua_fd_zero( lua_State *L )
176 fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
182 int vlclua_fd_open( lua_State *L )
187 int vlclua_fd_write( lua_State *L )
189 int i_fd = luaL_checkint( L, 1 );
192 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
193 i_len = luaL_optint( L, 3, i_len );
194 i_ret = write( i_fd, psz_buffer, i_len );
195 lua_pushinteger( L, i_ret );
199 int vlclua_fd_read( lua_State *L )
201 int i_fd = luaL_checkint( L, 1 );
202 size_t i_len = luaL_optint( L, 2, 1 );
203 char psz_buffer[i_len];
204 i_len = read( i_fd, psz_buffer, i_len );
205 lua_pushlstring( L, psz_buffer, i_len );