1 /*****************************************************************************
2 * net.c: Network related functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 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 *****************************************************************************/
36 #include <poll.h> /* poll structures and defines */
40 #include <vlc_common.h>
41 #include <vlc_network.h>
44 #include <vlc_interface.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 static int vlclua_url_parse( lua_State *L )
54 const char *psz_url = luaL_checkstring( L, 1 );
55 const char *psz_option = luaL_optstring( L, 2, NULL );
58 vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
61 lua_pushstring( L, url.psz_protocol );
62 lua_setfield( L, -2, "protocol" );
63 lua_pushstring( L, url.psz_username );
64 lua_setfield( L, -2, "username" );
65 lua_pushstring( L, url.psz_password );
66 lua_setfield( L, -2, "password" );
67 lua_pushstring( L, url.psz_host );
68 lua_setfield( L, -2, "host" );
69 lua_pushinteger( L, url.i_port );
70 lua_setfield( L, -2, "port" );
71 lua_pushstring( L, url.psz_path );
72 lua_setfield( L, -2, "path" );
73 lua_pushstring( L, url.psz_option );
74 lua_setfield( L, -2, "option" );
81 /*****************************************************************************
83 *****************************************************************************/
84 static int vlclua_net_listen_close( lua_State * );
85 static int vlclua_net_accept( lua_State * );
86 static int vlclua_net_fds( lua_State * );
88 static const luaL_Reg vlclua_net_listen_reg[] = {
89 { "accept", vlclua_net_accept },
90 { "fds", vlclua_net_fds },
94 static int vlclua_net_listen_tcp( lua_State *L )
96 vlc_object_t *p_this = vlclua_get_this( L );
97 const char *psz_host = luaL_checkstring( L, 1 );
98 int i_port = luaL_checkint( L, 2 );
99 int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
101 return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
103 int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
106 if( luaL_newmetatable( L, "net_listen" ) )
109 luaL_register( L, NULL, vlclua_net_listen_reg );
110 lua_setfield( L, -2, "__index" );
111 lua_pushcfunction( L, vlclua_net_listen_close );
112 lua_setfield( L, -2, "__gc" );
115 lua_setmetatable( L, -2 );
119 static int vlclua_net_listen_close( lua_State *L )
121 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
122 net_ListenClose( *ppi_fd );
126 static int vlclua_net_fds( lua_State *L )
128 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
129 int *pi_fd = *ppi_fd;
132 while( pi_fd[i_count] != -1 )
133 lua_pushinteger( L, pi_fd[i_count++] );
138 static int vlclua_net_accept( lua_State *L )
140 vlc_object_t *p_this = vlclua_get_this( L );
141 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
142 int i_fd = net_Accept( p_this, *ppi_fd );
144 lua_pushinteger( L, i_fd );
148 /*****************************************************************************
150 *****************************************************************************/
151 static int vlclua_net_connect_tcp( lua_State *L )
153 vlc_object_t *p_this = vlclua_get_this( L );
154 const char *psz_host = luaL_checkstring( L, 1 );
155 int i_port = luaL_checkint( L, 2 );
156 int i_fd = net_Connect( p_this, psz_host, i_port, SOCK_STREAM, IPPROTO_TCP );
157 lua_pushinteger( L, i_fd );
161 static int vlclua_net_close( lua_State *L )
163 int i_fd = luaL_checkint( L, 1 );
168 static int vlclua_net_send( lua_State *L )
170 int i_fd = luaL_checkint( L, 1 );
172 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
173 i_len = luaL_optint( L, 3, i_len );
174 i_len = send( i_fd, psz_buffer, i_len, 0 );
175 lua_pushinteger( L, i_len );
179 static int vlclua_net_recv( lua_State *L )
181 int i_fd = luaL_checkint( L, 1 );
182 size_t i_len = luaL_optint( L, 2, 1 );
183 char psz_buffer[i_len];
184 ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
186 lua_pushlstring( L, psz_buffer, i_ret );
193 /*****************************************************************************
195 *****************************************************************************/
196 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
197 static int vlclua_net_poll( lua_State *L )
199 intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L );
200 intf_sys_t *sys = intf->p_sys;
202 luaL_checktype( L, 1, LUA_TTABLE );
206 while( lua_next( L, 1 ) )
212 struct pollfd *p_fds = xmalloc( i_fds * sizeof( *p_fds ) );
216 p_fds[0].fd = sys->fd[0];
217 p_fds[0].events = POLLIN;
218 while( lua_next( L, 1 ) )
220 p_fds[i].fd = luaL_checkinteger( L, -2 );
221 p_fds[i].events = luaL_checkinteger( L, -1 );
228 i_ret = poll( p_fds, i_fds, -1 );
229 while( i_ret == -1 && errno == EINTR );
231 for( i = 1; i < i_fds; i++ )
233 lua_pushinteger( L, p_fds[i].fd );
234 lua_pushinteger( L, p_fds[i].revents );
235 lua_settable( L, 1 );
237 lua_pushinteger( L, i_ret );
239 if( p_fds[0].revents )
240 i_ret = luaL_error( L, "Interrupted." );
248 /*****************************************************************************
250 *****************************************************************************/
252 static int vlclua_fd_open( lua_State *L )
257 static int vlclua_fd_write( lua_State *L )
259 int i_fd = luaL_checkint( L, 1 );
262 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
263 i_len = luaL_optint( L, 3, i_len );
264 i_ret = write( i_fd, psz_buffer, i_len );
265 lua_pushinteger( L, i_ret );
269 static int vlclua_fd_read( lua_State *L )
271 int i_fd = luaL_checkint( L, 1 );
272 size_t i_len = luaL_optint( L, 2, 1 );
273 char psz_buffer[i_len];
274 ssize_t i_ret = read( i_fd, psz_buffer, i_len );
276 lua_pushlstring( L, psz_buffer, i_ret );
283 /*****************************************************************************
285 *****************************************************************************/
286 static int vlclua_stat( lua_State *L )
288 const char *psz_path = luaL_checkstring( L, 1 );
290 if( vlc_stat( psz_path, &s ) )
292 //return luaL_error( L, "Couldn't stat %s.", psz_path );
294 if( S_ISREG( s.st_mode ) )
295 lua_pushliteral( L, "file" );
296 else if( S_ISDIR( s.st_mode ) )
297 lua_pushliteral( L, "dir" );
299 else if( S_ISCHR( s.st_mode ) )
300 lua_pushliteral( L, "character device" );
303 else if( S_ISBLK( s.st_mode ) )
304 lua_pushliteral( L, "block device" );
307 else if( S_ISFIFO( s.st_mode ) )
308 lua_pushliteral( L, "fifo" );
311 else if( S_ISLNK( s.st_mode ) )
312 lua_pushliteral( L, "symbolic link" );
315 else if( S_ISSOCK( s.st_mode ) )
316 lua_pushliteral( L, "socket" );
319 lua_pushliteral( L, "unknown" );
320 lua_setfield( L, -2, "type" );
321 lua_pushinteger( L, s.st_mode );
322 lua_setfield( L, -2, "mode" );
323 lua_pushinteger( L, s.st_uid );
324 lua_setfield( L, -2, "uid" );
325 lua_pushinteger( L, s.st_gid );
326 lua_setfield( L, -2, "gid" );
327 lua_pushinteger( L, s.st_size );
328 lua_setfield( L, -2, "size" );
329 lua_pushinteger( L, s.st_atime );
330 lua_setfield( L, -2, "access_time" );
331 lua_pushinteger( L, s.st_mtime );
332 lua_setfield( L, -2, "modification_time" );
333 lua_pushinteger( L, s.st_ctime );
334 lua_setfield( L, -2, "creation_time" );
338 static int vlclua_opendir( lua_State *L )
340 const char *psz_dir = luaL_checkstring( L, 1 );
344 if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
345 return luaL_error( L, "cannot open directory `%s'.", psz_dir );
350 char *psz_filename = vlc_readdir( p_dir );
351 if( !psz_filename ) break;
353 lua_pushstring( L, psz_filename );
354 lua_rawseti( L, -2, i );
355 free( psz_filename );
361 /*****************************************************************************
363 *****************************************************************************/
364 static const luaL_Reg vlclua_net_reg[] = {
365 { "url_parse", vlclua_url_parse },
366 { "listen_tcp", vlclua_net_listen_tcp },
367 { "connect_tcp", vlclua_net_connect_tcp },
368 { "close", vlclua_net_close },
369 { "send", vlclua_net_send },
370 { "recv", vlclua_net_recv },
372 { "poll", vlclua_net_poll },
373 { "read", vlclua_fd_read },
374 { "write", vlclua_fd_write },
376 { "stat", vlclua_stat }, /* Not really "net" */
377 { "opendir", vlclua_opendir }, /* Not really "net" */
381 void luaopen_net( lua_State *L )
384 luaL_register( L, NULL, vlclua_net_reg );
385 #define ADD_CONSTANT( name, value ) \
386 lua_pushinteger( L, value ); \
387 lua_setfield( L, -2, name );
388 ADD_CONSTANT( "POLLIN", POLLIN )
389 ADD_CONSTANT( "POLLPRI", POLLPRI )
390 ADD_CONSTANT( "POLLOUT", POLLOUT )
391 ADD_CONSTANT( "POLLERR", POLLERR )
392 ADD_CONSTANT( "POLLHUP", POLLHUP )
393 ADD_CONSTANT( "POLLNVAL", POLLNVAL )
394 lua_setfield( L, -2, "net" );