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 );
204 double f_timeout = luaL_optnumber( L, 2, -1. );
208 while( lua_next( L, 1 ) )
213 struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
214 vlc_cleanup_push( free, p_fds );
217 while( lua_next( L, 1 ) )
219 p_fds[i].fd = luaL_checkinteger( L, -2 );
220 p_fds[i].events = luaL_checkinteger( L, -1 );
221 p_fds[i].revents = 0;
226 int i_ret = poll( p_fds, i_fds, f_timeout < 0. ? -1 : (int)(f_timeout*1000) );
227 for( i = 0; i < i_fds; i++ )
229 lua_pushinteger( L, p_fds[i].fd );
230 lua_pushinteger( L, p_fds[i].revents );
231 lua_settable( L, 1 );
233 lua_pushinteger( L, i_ret );
238 /*****************************************************************************
240 *****************************************************************************/
242 static int vlclua_fd_open( lua_State *L )
247 static int vlclua_fd_write( lua_State *L )
249 int i_fd = luaL_checkint( L, 1 );
252 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
253 i_len = luaL_optint( L, 3, i_len );
254 i_ret = write( i_fd, psz_buffer, i_len );
255 lua_pushinteger( L, i_ret );
259 static int vlclua_fd_read( lua_State *L )
261 int i_fd = luaL_checkint( L, 1 );
262 size_t i_len = luaL_optint( L, 2, 1 );
263 char psz_buffer[i_len];
264 ssize_t i_ret = read( i_fd, psz_buffer, i_len );
265 lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
269 /*****************************************************************************
271 *****************************************************************************/
272 static int vlclua_stat( lua_State *L )
274 #ifdef HAVE_SYS_STAT_H
275 const char *psz_path = luaL_checkstring( L, 1 );
277 if( vlc_stat( psz_path, &s ) )
279 //return luaL_error( L, "Couldn't stat %s.", psz_path );
281 if( S_ISREG( s.st_mode ) )
282 lua_pushliteral( L, "file" );
283 else if( S_ISDIR( s.st_mode ) )
284 lua_pushliteral( L, "dir" );
286 else if( S_ISCHR( s.st_mode ) )
287 lua_pushliteral( L, "character device" );
290 else if( S_ISBLK( s.st_mode ) )
291 lua_pushliteral( L, "block device" );
294 else if( S_ISFIFO( s.st_mode ) )
295 lua_pushliteral( L, "fifo" );
298 else if( S_ISLNK( s.st_mode ) )
299 lua_pushliteral( L, "symbolic link" );
302 else if( S_ISSOCK( s.st_mode ) )
303 lua_pushliteral( L, "socket" );
306 lua_pushliteral( L, "unknown" );
307 lua_setfield( L, -2, "type" );
308 lua_pushinteger( L, s.st_mode );
309 lua_setfield( L, -2, "mode" );
310 lua_pushinteger( L, s.st_uid );
311 lua_setfield( L, -2, "uid" );
312 lua_pushinteger( L, s.st_gid );
313 lua_setfield( L, -2, "gid" );
314 lua_pushinteger( L, s.st_size );
315 lua_setfield( L, -2, "size" );
316 lua_pushinteger( L, s.st_atime );
317 lua_setfield( L, -2, "access_time" );
318 lua_pushinteger( L, s.st_mtime );
319 lua_setfield( L, -2, "modification_time" );
320 lua_pushinteger( L, s.st_ctime );
321 lua_setfield( L, -2, "creation_time" );
324 # warning "Woops, looks like we don't have stat on your platform"
325 return luaL_error( L, "System is missing <sys/stat.h>" );
329 static int vlclua_opendir( lua_State *L )
331 const char *psz_dir = luaL_checkstring( L, 1 );
335 if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
336 return luaL_error( L, "cannot open directory `%s'.", psz_dir );
341 char *psz_filename = vlc_readdir( p_dir );
342 if( !psz_filename ) break;
344 lua_pushstring( L, psz_filename );
345 lua_rawseti( L, -2, i );
346 free( psz_filename );
352 /*****************************************************************************
354 *****************************************************************************/
355 static const luaL_Reg vlclua_net_reg[] = {
356 { "url_parse", vlclua_url_parse },
357 { "listen_tcp", vlclua_net_listen_tcp },
358 { "connect_tcp", vlclua_net_connect_tcp },
359 { "close", vlclua_net_close },
360 { "send", vlclua_net_send },
361 { "recv", vlclua_net_recv },
362 { "poll", vlclua_net_poll },
363 { "read", vlclua_fd_read },
364 { "write", vlclua_fd_write },
365 { "stat", vlclua_stat }, /* Not really "net" */
366 { "opendir", vlclua_opendir }, /* Not really "net" */
370 void luaopen_net( lua_State *L )
373 luaL_register( L, NULL, vlclua_net_reg );
374 #define ADD_CONSTANT( name, value ) \
375 lua_pushinteger( L, value ); \
376 lua_setfield( L, -2, name );
377 ADD_CONSTANT( "POLLIN", POLLIN )
378 ADD_CONSTANT( "POLLPRI", POLLPRI )
379 ADD_CONSTANT( "POLLOUT", POLLOUT )
380 ADD_CONSTANT( "POLLERR", POLLERR )
381 ADD_CONSTANT( "POLLHUP", POLLHUP )
382 ADD_CONSTANT( "POLLNVAL", POLLNVAL )
383 lua_setfield( L, -2, "net" );