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>
39 #include <lua.h> /* Low level lua C API */
40 #include <lauxlib.h> /* Higher level C API */
45 /*****************************************************************************
47 *****************************************************************************/
48 static int vlclua_url_parse( lua_State *L )
50 const char *psz_url = luaL_checkstring( L, 1 );
51 const char *psz_option = luaL_optstring( L, 2, NULL );
54 vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
57 lua_pushstring( L, url.psz_protocol );
58 lua_setfield( L, -2, "protocol" );
59 lua_pushstring( L, url.psz_username );
60 lua_setfield( L, -2, "username" );
61 lua_pushstring( L, url.psz_password );
62 lua_setfield( L, -2, "password" );
63 lua_pushstring( L, url.psz_host );
64 lua_setfield( L, -2, "host" );
65 lua_pushinteger( L, url.i_port );
66 lua_setfield( L, -2, "port" );
67 lua_pushstring( L, url.psz_path );
68 lua_setfield( L, -2, "path" );
69 lua_pushstring( L, url.psz_option );
70 lua_setfield( L, -2, "option" );
77 /*****************************************************************************
79 *****************************************************************************/
80 static int vlclua_net_listen_close( lua_State * );
81 static int vlclua_net_accept( lua_State * );
83 static const luaL_Reg vlclua_net_listen_reg[] = {
84 { "accept", vlclua_net_accept },
88 static int vlclua_net_listen_tcp( lua_State *L )
90 vlc_object_t *p_this = vlclua_get_this( L );
91 const char *psz_host = luaL_checkstring( L, 1 );
92 int i_port = luaL_checkint( L, 2 );
93 int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
95 return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
97 int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
100 if( luaL_newmetatable( L, "net_listen" ) )
103 luaL_register( L, NULL, vlclua_net_listen_reg );
104 lua_setfield( L, -2, "__index" );
105 lua_pushcfunction( L, vlclua_net_listen_close );
106 lua_setfield( L, -2, "__gc" );
109 lua_setmetatable( L, -2 );
113 static int vlclua_net_listen_close( lua_State *L )
115 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
116 net_ListenClose( *ppi_fd );
120 static int vlclua_net_accept( lua_State *L )
122 vlc_object_t *p_this = vlclua_get_this( L );
123 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
124 mtime_t i_wait = luaL_optint( L, 2, -1 ); /* default to block */
125 int i_fd = net_Accept( p_this, *ppi_fd, i_wait );
126 lua_pushinteger( L, i_fd );
130 /*****************************************************************************
132 *****************************************************************************/
133 static int vlclua_net_close( lua_State *L )
135 int i_fd = luaL_checkint( L, 1 );
140 static int vlclua_net_send( lua_State *L )
142 int i_fd = luaL_checkint( L, 1 );
144 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
145 i_len = luaL_optint( L, 3, i_len );
146 i_len = send( i_fd, psz_buffer, i_len, 0 );
147 lua_pushinteger( L, i_len );
151 static int vlclua_net_recv( lua_State *L )
153 int i_fd = luaL_checkint( L, 1 );
154 size_t i_len = luaL_optint( L, 2, 1 );
155 char psz_buffer[i_len];
156 i_len = recv( i_fd, psz_buffer, i_len, 0 );
157 lua_pushlstring( L, psz_buffer, i_len );
161 /*****************************************************************************
163 *****************************************************************************/
164 static int vlclua_net_select( lua_State *L )
167 size_t i_nfds = luaL_checkint( L, 1 );
168 fd_set *fds_read = (fd_set*)luaL_checkudata( L, 2, "fd_set" );
169 fd_set *fds_write = (fd_set*)luaL_checkudata( L, 3, "fd_set" );
170 double f_timeout = luaL_checknumber( L, 4 );
171 struct timeval timeout;
174 if( i_nfds > FD_SETSIZE )
177 timeout.tv_sec = (int)f_timeout;
178 timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
179 i_ret = select( i_nfds, fds_read, fds_write, 0, &timeout );
180 lua_pushinteger( L, i_ret );
181 lua_pushinteger( L, (double)timeout.tv_sec+((double)timeout.tv_usec)/1e-6 );
185 /*****************************************************************************
187 *****************************************************************************/
188 static int vlclua_fd_clr( lua_State * );
189 static int vlclua_fd_isset( lua_State * );
190 static int vlclua_fd_set( lua_State * );
191 static int vlclua_fd_zero( lua_State * );
193 static const luaL_Reg vlclua_fd_set_reg[] = {
194 { "clr", vlclua_fd_clr },
195 { "isset", vlclua_fd_isset },
196 { "set", vlclua_fd_set },
197 { "zero", vlclua_fd_zero },
201 static int vlclua_fd_set_new( lua_State *L )
203 fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
206 if( luaL_newmetatable( L, "fd_set" ) )
209 luaL_register( L, NULL, vlclua_fd_set_reg );
210 lua_setfield( L, -2, "__index" );
213 lua_setmetatable( L, -2 );
217 static int vlclua_fd_clr( lua_State *L )
219 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
220 int i_fd = luaL_checkint( L, 2 );
225 static int vlclua_fd_isset( lua_State *L )
227 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
228 int i_fd = luaL_checkint( L, 2 );
229 lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
233 static int vlclua_fd_set( lua_State *L )
235 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
236 size_t i_fd = luaL_checkint( L, 2 );
237 /* FIXME: we should really use poll() instead here, but that breaks the
238 * VLC/LUA API. On Windows, overflow protection is built-in FD_SET, not
239 * on POSIX. In both cases, run-time behavior will however be wrong. */
241 if( i_fd < FD_SETSIZE )
247 static int vlclua_fd_zero( lua_State *L )
249 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
254 /*****************************************************************************
256 *****************************************************************************/
258 static int vlclua_fd_open( lua_State *L )
263 static int vlclua_fd_write( lua_State *L )
265 int i_fd = luaL_checkint( L, 1 );
268 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
269 i_len = luaL_optint( L, 3, i_len );
270 i_ret = write( i_fd, psz_buffer, i_len );
271 lua_pushinteger( L, i_ret );
275 static int vlclua_fd_read( lua_State *L )
277 int i_fd = luaL_checkint( L, 1 );
278 size_t i_len = luaL_optint( L, 2, 1 );
279 char psz_buffer[i_len];
280 i_len = read( i_fd, psz_buffer, i_len );
281 lua_pushlstring( L, psz_buffer, i_len );
285 /*****************************************************************************
287 *****************************************************************************/
288 static int vlclua_stat( lua_State *L )
290 #ifdef HAVE_SYS_STAT_H
291 const char *psz_path = luaL_checkstring( L, 1 );
293 if( utf8_stat( psz_path, &s ) )
295 //return luaL_error( L, "Couldn't stat %s.", psz_path );
297 if( S_ISREG( s.st_mode ) )
298 lua_pushstring( L, "file" );
299 else if( S_ISDIR( s.st_mode ) )
300 lua_pushstring( L, "dir" );
302 else if( S_ISCHR( s.st_mode ) )
303 lua_pushstring( L, "character device" );
306 else if( S_ISBLK( s.st_mode ) )
307 lua_pushstring( L, "block device" );
310 else if( S_ISFIFO( s.st_mode ) )
311 lua_pushstring( L, "fifo" );
314 else if( S_ISLNK( s.st_mode ) )
315 lua_pushstring( L, "symbolic link" );
318 else if( S_ISSOCK( s.st_mode ) )
319 lua_pushstring( L, "socket" );
322 lua_pushstring( L, "unknown" );
323 lua_setfield( L, -2, "type" );
324 lua_pushinteger( L, s.st_mode );
325 lua_setfield( L, -2, "mode" );
326 lua_pushinteger( L, s.st_uid );
327 lua_setfield( L, -2, "uid" );
328 lua_pushinteger( L, s.st_gid );
329 lua_setfield( L, -2, "gid" );
330 lua_pushinteger( L, s.st_size );
331 lua_setfield( L, -2, "size" );
332 lua_pushinteger( L, s.st_atime );
333 lua_setfield( L, -2, "access_time" );
334 lua_pushinteger( L, s.st_mtime );
335 lua_setfield( L, -2, "modification_time" );
336 lua_pushinteger( L, s.st_ctime );
337 lua_setfield( L, -2, "creation_time" );
340 # warning "Woops, looks like we don't have stat on your platform"
341 return luaL_error( L, "System is missing <sys/stat.h>" );
345 static int vlclua_opendir( lua_State *L )
347 const char *psz_dir = luaL_checkstring( L, 1 );
351 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
352 return luaL_error( L, "cannot open directory `%s'.", psz_dir );
357 char *psz_filename = utf8_readdir( p_dir );
358 if( !psz_filename ) break;
360 lua_pushstring( L, psz_filename );
361 lua_rawseti( L, -2, i );
362 free( psz_filename );
368 /*****************************************************************************
370 *****************************************************************************/
371 static const luaL_Reg vlclua_net_reg[] = {
372 { "url_parse", vlclua_url_parse },
373 { "listen_tcp", vlclua_net_listen_tcp },
374 { "close", vlclua_net_close },
375 { "send", vlclua_net_send },
376 { "recv", vlclua_net_recv },
377 { "select", vlclua_net_select },
378 { "fd_set_new", vlclua_fd_set_new },
379 { "fd_read", vlclua_fd_read },
380 { "fd_write", vlclua_fd_write },
381 { "stat", vlclua_stat }, /* Not really "net" */
382 { "opendir", vlclua_opendir }, /* Not really "net" */
386 void luaopen_net( lua_State *L )
389 luaL_register( L, NULL, vlclua_net_reg );
390 lua_setfield( L, -2, "net" );