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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_network.h>
40 #include <lua.h> /* Low level lua C API */
41 #include <lauxlib.h> /* Higher level C API */
44 #include <poll.h> /* poll structures and defines */
52 /*****************************************************************************
54 *****************************************************************************/
55 static int vlclua_url_parse( lua_State *L )
57 const char *psz_url = luaL_checkstring( L, 1 );
58 const char *psz_option = luaL_optstring( L, 2, NULL );
61 vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
64 lua_pushstring( L, url.psz_protocol );
65 lua_setfield( L, -2, "protocol" );
66 lua_pushstring( L, url.psz_username );
67 lua_setfield( L, -2, "username" );
68 lua_pushstring( L, url.psz_password );
69 lua_setfield( L, -2, "password" );
70 lua_pushstring( L, url.psz_host );
71 lua_setfield( L, -2, "host" );
72 lua_pushinteger( L, url.i_port );
73 lua_setfield( L, -2, "port" );
74 lua_pushstring( L, url.psz_path );
75 lua_setfield( L, -2, "path" );
76 lua_pushstring( L, url.psz_option );
77 lua_setfield( L, -2, "option" );
84 /*****************************************************************************
86 *****************************************************************************/
87 static int vlclua_net_listen_close( lua_State * );
88 static int vlclua_net_accept( lua_State * );
89 static int vlclua_net_fds( lua_State * );
91 static const luaL_Reg vlclua_net_listen_reg[] = {
92 { "accept", vlclua_net_accept },
93 { "fds", vlclua_net_fds },
97 static int vlclua_net_listen_tcp( lua_State *L )
99 vlc_object_t *p_this = vlclua_get_this( L );
100 const char *psz_host = luaL_checkstring( L, 1 );
101 int i_port = luaL_checkint( L, 2 );
102 int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
104 return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
106 int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
109 if( luaL_newmetatable( L, "net_listen" ) )
112 luaL_register( L, NULL, vlclua_net_listen_reg );
113 lua_setfield( L, -2, "__index" );
114 lua_pushcfunction( L, vlclua_net_listen_close );
115 lua_setfield( L, -2, "__gc" );
118 lua_setmetatable( L, -2 );
122 static int vlclua_net_listen_close( lua_State *L )
124 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
125 net_ListenClose( *ppi_fd );
129 static int vlclua_net_fds( lua_State *L )
131 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
132 int *pi_fd = *ppi_fd;
135 while( pi_fd[i_count] != -1 )
136 lua_pushinteger( L, pi_fd[i_count++] );
141 static int vlclua_net_accept( lua_State *L )
143 vlc_object_t *p_this = vlclua_get_this( L );
144 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
145 int i_fd = net_Accept( p_this, *ppi_fd );
147 lua_pushinteger( L, i_fd );
151 /*****************************************************************************
153 *****************************************************************************/
154 static int vlclua_net_connect_tcp( lua_State *L )
156 vlc_object_t *p_this = vlclua_get_this( L );
157 const char *psz_host = luaL_checkstring( L, 1 );
158 int i_port = luaL_checkint( L, 2 );
159 int i_fd = net_Connect( p_this, psz_host, i_port, SOCK_STREAM, IPPROTO_TCP );
160 lua_pushinteger( L, i_fd );
164 static int vlclua_net_close( lua_State *L )
166 int i_fd = luaL_checkint( L, 1 );
171 static int vlclua_net_send( lua_State *L )
173 int i_fd = luaL_checkint( L, 1 );
175 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
176 i_len = luaL_optint( L, 3, i_len );
177 i_len = send( i_fd, psz_buffer, i_len, 0 );
178 lua_pushinteger( L, i_len );
182 static int vlclua_net_recv( lua_State *L )
184 int i_fd = luaL_checkint( L, 1 );
185 size_t i_len = luaL_optint( L, 2, 1 );
186 char psz_buffer[i_len];
187 ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
188 lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
192 /*****************************************************************************
194 *****************************************************************************/
195 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
196 static int vlclua_net_poll( lua_State *L )
198 luaL_checktype( L, 1, LUA_TTABLE );
199 double f_timeout = luaL_optnumber( L, 2, -1. );
203 while( lua_next( L, 1 ) )
208 struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
209 vlc_cleanup_push( free, p_fds );
212 while( lua_next( L, 1 ) )
214 p_fds[i].fd = luaL_checkinteger( L, -2 );
215 p_fds[i].events = luaL_checkinteger( L, -1 );
216 p_fds[i].revents = 0;
221 int i_ret = poll( p_fds, i_fds, f_timeout < 0. ? -1 : (int)(f_timeout*1000) );
222 for( i = 0; i < i_fds; i++ )
224 lua_pushinteger( L, p_fds[i].fd );
225 lua_pushinteger( L, p_fds[i].revents );
226 lua_settable( L, 1 );
228 lua_pushinteger( L, i_ret );
233 /*****************************************************************************
235 *****************************************************************************/
237 static int vlclua_fd_open( lua_State *L )
242 static int vlclua_fd_write( lua_State *L )
244 int i_fd = luaL_checkint( L, 1 );
247 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
248 i_len = luaL_optint( L, 3, i_len );
249 i_ret = write( i_fd, psz_buffer, i_len );
250 lua_pushinteger( L, i_ret );
254 static int vlclua_fd_read( lua_State *L )
256 int i_fd = luaL_checkint( L, 1 );
257 size_t i_len = luaL_optint( L, 2, 1 );
258 char psz_buffer[i_len];
259 ssize_t i_ret = read( i_fd, psz_buffer, i_len );
260 lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
264 /*****************************************************************************
266 *****************************************************************************/
267 static int vlclua_stat( lua_State *L )
269 #ifdef HAVE_SYS_STAT_H
270 const char *psz_path = luaL_checkstring( L, 1 );
272 if( vlc_stat( psz_path, &s ) )
274 //return luaL_error( L, "Couldn't stat %s.", psz_path );
276 if( S_ISREG( s.st_mode ) )
277 lua_pushliteral( L, "file" );
278 else if( S_ISDIR( s.st_mode ) )
279 lua_pushliteral( L, "dir" );
281 else if( S_ISCHR( s.st_mode ) )
282 lua_pushliteral( L, "character device" );
285 else if( S_ISBLK( s.st_mode ) )
286 lua_pushliteral( L, "block device" );
289 else if( S_ISFIFO( s.st_mode ) )
290 lua_pushliteral( L, "fifo" );
293 else if( S_ISLNK( s.st_mode ) )
294 lua_pushliteral( L, "symbolic link" );
297 else if( S_ISSOCK( s.st_mode ) )
298 lua_pushliteral( L, "socket" );
301 lua_pushliteral( L, "unknown" );
302 lua_setfield( L, -2, "type" );
303 lua_pushinteger( L, s.st_mode );
304 lua_setfield( L, -2, "mode" );
305 lua_pushinteger( L, s.st_uid );
306 lua_setfield( L, -2, "uid" );
307 lua_pushinteger( L, s.st_gid );
308 lua_setfield( L, -2, "gid" );
309 lua_pushinteger( L, s.st_size );
310 lua_setfield( L, -2, "size" );
311 lua_pushinteger( L, s.st_atime );
312 lua_setfield( L, -2, "access_time" );
313 lua_pushinteger( L, s.st_mtime );
314 lua_setfield( L, -2, "modification_time" );
315 lua_pushinteger( L, s.st_ctime );
316 lua_setfield( L, -2, "creation_time" );
319 # warning "Woops, looks like we don't have stat on your platform"
320 return luaL_error( L, "System is missing <sys/stat.h>" );
324 static int vlclua_opendir( lua_State *L )
326 const char *psz_dir = luaL_checkstring( L, 1 );
330 if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
331 return luaL_error( L, "cannot open directory `%s'.", psz_dir );
336 char *psz_filename = vlc_readdir( p_dir );
337 if( !psz_filename ) break;
339 lua_pushstring( L, psz_filename );
340 lua_rawseti( L, -2, i );
341 free( psz_filename );
347 /*****************************************************************************
349 *****************************************************************************/
350 static const luaL_Reg vlclua_net_reg[] = {
351 { "url_parse", vlclua_url_parse },
352 { "listen_tcp", vlclua_net_listen_tcp },
353 { "connect_tcp", vlclua_net_connect_tcp },
354 { "close", vlclua_net_close },
355 { "send", vlclua_net_send },
356 { "recv", vlclua_net_recv },
357 { "poll", vlclua_net_poll },
358 { "read", vlclua_fd_read },
359 { "write", vlclua_fd_write },
360 { "stat", vlclua_stat }, /* Not really "net" */
361 { "opendir", vlclua_opendir }, /* Not really "net" */
365 void luaopen_net( lua_State *L )
368 luaL_register( L, NULL, vlclua_net_reg );
369 #define ADD_CONSTANT( name, value ) \
370 lua_pushinteger( L, value ); \
371 lua_setfield( L, -2, name );
372 ADD_CONSTANT( "POLLIN", POLLIN )
373 ADD_CONSTANT( "POLLPRI", POLLPRI )
374 ADD_CONSTANT( "POLLOUT", POLLOUT )
375 ADD_CONSTANT( "POLLERR", POLLERR )
376 ADD_CONSTANT( "POLLHUP", POLLHUP )
377 ADD_CONSTANT( "POLLNVAL", POLLNVAL )
378 lua_setfield( L, -2, "net" );