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 *****************************************************************************/
39 #include <vlc_common.h>
40 #include <vlc_network.h>
44 #include <lua.h> /* Low level lua C API */
45 #include <lauxlib.h> /* Higher level C API */
48 #include <poll.h> /* poll structures and defines */
57 /*****************************************************************************
59 *****************************************************************************/
60 static int vlclua_url_parse( lua_State *L )
62 const char *psz_url = luaL_checkstring( L, 1 );
63 const char *psz_option = luaL_optstring( L, 2, NULL );
66 vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
69 lua_pushstring( L, url.psz_protocol );
70 lua_setfield( L, -2, "protocol" );
71 lua_pushstring( L, url.psz_username );
72 lua_setfield( L, -2, "username" );
73 lua_pushstring( L, url.psz_password );
74 lua_setfield( L, -2, "password" );
75 lua_pushstring( L, url.psz_host );
76 lua_setfield( L, -2, "host" );
77 lua_pushinteger( L, url.i_port );
78 lua_setfield( L, -2, "port" );
79 lua_pushstring( L, url.psz_path );
80 lua_setfield( L, -2, "path" );
81 lua_pushstring( L, url.psz_option );
82 lua_setfield( L, -2, "option" );
89 /*****************************************************************************
91 *****************************************************************************/
92 static int vlclua_net_listen_close( lua_State * );
93 static int vlclua_net_accept( lua_State * );
94 static int vlclua_net_fds( lua_State * );
96 static const luaL_Reg vlclua_net_listen_reg[] = {
97 { "accept", vlclua_net_accept },
98 { "fds", vlclua_net_fds },
102 static int vlclua_net_listen_tcp( lua_State *L )
104 vlc_object_t *p_this = vlclua_get_this( L );
105 const char *psz_host = luaL_checkstring( L, 1 );
106 int i_port = luaL_checkint( L, 2 );
107 int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
109 return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
111 int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
114 if( luaL_newmetatable( L, "net_listen" ) )
117 luaL_register( L, NULL, vlclua_net_listen_reg );
118 lua_setfield( L, -2, "__index" );
119 lua_pushcfunction( L, vlclua_net_listen_close );
120 lua_setfield( L, -2, "__gc" );
123 lua_setmetatable( L, -2 );
127 static int vlclua_net_listen_close( lua_State *L )
129 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
130 net_ListenClose( *ppi_fd );
134 static int vlclua_net_fds( lua_State *L )
136 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
137 int *pi_fd = *ppi_fd;
140 while( pi_fd[i_count] != -1 )
141 lua_pushinteger( L, pi_fd[i_count++] );
146 static int vlclua_net_accept( lua_State *L )
148 vlc_object_t *p_this = vlclua_get_this( L );
149 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
150 int i_fd = net_Accept( p_this, *ppi_fd );
152 lua_pushinteger( L, i_fd );
156 /*****************************************************************************
158 *****************************************************************************/
159 static int vlclua_net_connect_tcp( lua_State *L )
161 vlc_object_t *p_this = vlclua_get_this( L );
162 const char *psz_host = luaL_checkstring( L, 1 );
163 int i_port = luaL_checkint( L, 2 );
164 int i_fd = net_Connect( p_this, psz_host, i_port, SOCK_STREAM, IPPROTO_TCP );
165 lua_pushinteger( L, i_fd );
169 static int vlclua_net_close( lua_State *L )
171 int i_fd = luaL_checkint( L, 1 );
176 static int vlclua_net_send( lua_State *L )
178 int i_fd = luaL_checkint( L, 1 );
180 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
181 i_len = luaL_optint( L, 3, i_len );
182 i_len = send( i_fd, psz_buffer, i_len, 0 );
183 lua_pushinteger( L, i_len );
187 static int vlclua_net_recv( lua_State *L )
189 int i_fd = luaL_checkint( L, 1 );
190 size_t i_len = luaL_optint( L, 2, 1 );
191 char psz_buffer[i_len];
192 ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
193 lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
197 /*****************************************************************************
199 *****************************************************************************/
200 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
201 static int vlclua_net_poll( lua_State *L )
203 luaL_checktype( L, 1, LUA_TTABLE );
207 while( lua_next( L, 1 ) )
212 struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
213 vlc_cleanup_push( free, p_fds );
216 while( lua_next( L, 1 ) )
218 p_fds[i].fd = luaL_checkinteger( L, -2 );
219 p_fds[i].events = luaL_checkinteger( L, -1 );
220 p_fds[i].revents = 0;
227 i_ret = poll( p_fds, i_fds, -1 );
228 while( i_ret == -1 );
230 for( i = 0; i < i_fds; i++ )
232 lua_pushinteger( L, p_fds[i].fd );
233 lua_pushinteger( L, p_fds[i].revents );
234 lua_settable( L, 1 );
236 lua_pushinteger( L, i_ret );
241 /*****************************************************************************
243 *****************************************************************************/
245 static int vlclua_fd_open( lua_State *L )
250 static int vlclua_fd_write( lua_State *L )
252 int i_fd = luaL_checkint( L, 1 );
255 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
256 i_len = luaL_optint( L, 3, i_len );
257 i_ret = write( i_fd, psz_buffer, i_len );
258 lua_pushinteger( L, i_ret );
262 static int vlclua_fd_read( lua_State *L )
264 int i_fd = luaL_checkint( L, 1 );
265 size_t i_len = luaL_optint( L, 2, 1 );
266 char psz_buffer[i_len];
267 ssize_t i_ret = read( i_fd, psz_buffer, i_len );
268 lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
272 /*****************************************************************************
274 *****************************************************************************/
275 static int vlclua_stat( lua_State *L )
277 #ifdef HAVE_SYS_STAT_H
278 const char *psz_path = luaL_checkstring( L, 1 );
280 if( vlc_stat( psz_path, &s ) )
282 //return luaL_error( L, "Couldn't stat %s.", psz_path );
284 if( S_ISREG( s.st_mode ) )
285 lua_pushliteral( L, "file" );
286 else if( S_ISDIR( s.st_mode ) )
287 lua_pushliteral( L, "dir" );
289 else if( S_ISCHR( s.st_mode ) )
290 lua_pushliteral( L, "character device" );
293 else if( S_ISBLK( s.st_mode ) )
294 lua_pushliteral( L, "block device" );
297 else if( S_ISFIFO( s.st_mode ) )
298 lua_pushliteral( L, "fifo" );
301 else if( S_ISLNK( s.st_mode ) )
302 lua_pushliteral( L, "symbolic link" );
305 else if( S_ISSOCK( s.st_mode ) )
306 lua_pushliteral( L, "socket" );
309 lua_pushliteral( L, "unknown" );
310 lua_setfield( L, -2, "type" );
311 lua_pushinteger( L, s.st_mode );
312 lua_setfield( L, -2, "mode" );
313 lua_pushinteger( L, s.st_uid );
314 lua_setfield( L, -2, "uid" );
315 lua_pushinteger( L, s.st_gid );
316 lua_setfield( L, -2, "gid" );
317 lua_pushinteger( L, s.st_size );
318 lua_setfield( L, -2, "size" );
319 lua_pushinteger( L, s.st_atime );
320 lua_setfield( L, -2, "access_time" );
321 lua_pushinteger( L, s.st_mtime );
322 lua_setfield( L, -2, "modification_time" );
323 lua_pushinteger( L, s.st_ctime );
324 lua_setfield( L, -2, "creation_time" );
327 # warning "Woops, looks like we don't have stat on your platform"
328 return luaL_error( L, "System is missing <sys/stat.h>" );
332 static int vlclua_opendir( lua_State *L )
334 const char *psz_dir = luaL_checkstring( L, 1 );
338 if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
339 return luaL_error( L, "cannot open directory `%s'.", psz_dir );
344 char *psz_filename = vlc_readdir( p_dir );
345 if( !psz_filename ) break;
347 lua_pushstring( L, psz_filename );
348 lua_rawseti( L, -2, i );
349 free( psz_filename );
355 /*****************************************************************************
357 *****************************************************************************/
358 static const luaL_Reg vlclua_net_reg[] = {
359 { "url_parse", vlclua_url_parse },
360 { "listen_tcp", vlclua_net_listen_tcp },
361 { "connect_tcp", vlclua_net_connect_tcp },
362 { "close", vlclua_net_close },
363 { "send", vlclua_net_send },
364 { "recv", vlclua_net_recv },
365 { "poll", vlclua_net_poll },
366 { "read", vlclua_fd_read },
367 { "write", vlclua_fd_write },
368 { "stat", vlclua_stat }, /* Not really "net" */
369 { "opendir", vlclua_opendir }, /* Not really "net" */
373 void luaopen_net( lua_State *L )
376 luaL_register( L, NULL, vlclua_net_reg );
377 #define ADD_CONSTANT( name, value ) \
378 lua_pushinteger( L, value ); \
379 lua_setfield( L, -2, name );
380 ADD_CONSTANT( "POLLIN", POLLIN )
381 ADD_CONSTANT( "POLLPRI", POLLPRI )
382 ADD_CONSTANT( "POLLOUT", POLLOUT )
383 ADD_CONSTANT( "POLLERR", POLLERR )
384 ADD_CONSTANT( "POLLHUP", POLLHUP )
385 ADD_CONSTANT( "POLLNVAL", POLLNVAL )
386 lua_setfield( L, -2, "net" );