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 */
43 #include <poll.h> /* poll structures and defines */
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 *pi_fd = *ppi_fd;
143 int i_timeout = luaL_optint( L, 2, -1 ); /* block is default */
145 /* Implement net_Accept with timeout */
148 unsigned int i_count = 1;
149 while( pi_fd[i_count] != -1 )
152 struct pollfd ufd[i_count+1];
154 for( i = 0; i < i_count; i++ )
156 ufd[i].fd = pi_fd[i];
157 ufd[i].events = POLLIN;
160 if( poll( ufd, i_count, i_timeout ) > 0 )
162 for( i = 0; i < i_count; i++ )
164 if( !ufd[i].revents ) continue;
165 i_fd = net_AcceptSingle( p_this, ufd[i].fd );
166 if( i_fd == -1 ) continue;
167 memmove( pi_fd + i, pi_fd + i + 1, i_count - (i + 1) );
168 pi_fd[i_count - 1] = ufd[i].fd;
172 lua_pushinteger( L, i_fd );
176 /*****************************************************************************
178 *****************************************************************************/
179 static int vlclua_net_close( lua_State *L )
181 int i_fd = luaL_checkint( L, 1 );
186 static int vlclua_net_send( lua_State *L )
188 int i_fd = luaL_checkint( L, 1 );
190 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
191 i_len = luaL_optint( L, 3, i_len );
192 i_len = send( i_fd, psz_buffer, i_len, 0 );
193 lua_pushinteger( L, i_len );
197 static int vlclua_net_recv( lua_State *L )
199 int i_fd = luaL_checkint( L, 1 );
200 size_t i_len = luaL_optint( L, 2, 1 );
201 char psz_buffer[i_len];
202 i_len = recv( i_fd, psz_buffer, i_len, 0 );
203 lua_pushlstring( L, psz_buffer, i_len );
207 /*****************************************************************************
209 *****************************************************************************/
210 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
211 static int vlclua_net_poll( lua_State *L )
213 luaL_checktype( L, 1, LUA_TTABLE );
214 double f_timeout = luaL_optnumber( L, 2, -1. );
218 while( lua_next( L, 1 ) )
223 struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
226 while( lua_next( L, 1 ) )
228 p_fds[i].fd = luaL_checkinteger( L, -2 );
229 p_fds[i].events = luaL_checkinteger( L, -1 );
230 p_fds[i].revents = 0;
235 int i_ret = poll( p_fds, i_fds, f_timeout < 0. ? -1 : (int)(f_timeout*1000) );
236 for( i = 0; i < i_fds; i++ )
238 lua_pushinteger( L, p_fds[i].fd );
239 lua_pushinteger( L, p_fds[i].revents );
240 lua_settable( L, 1 );
243 lua_pushinteger( L, i_ret );
247 static int vlclua_net_select( lua_State *L )
250 size_t i_nfds = luaL_checkint( L, 1 );
251 fd_set *fds_read = (fd_set*)luaL_checkudata( L, 2, "fd_set" );
252 fd_set *fds_write = (fd_set*)luaL_checkudata( L, 3, "fd_set" );
253 double f_timeout = luaL_checknumber( L, 4 );
254 struct timeval timeout;
257 if( i_nfds > FD_SETSIZE )
260 if( f_timeout >= 0. )
262 timeout.tv_sec = (int)f_timeout;
263 timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
265 i_ret = select( i_nfds, fds_read, fds_write, 0, f_timeout >= 0. ? &timeout : NULL );
266 lua_pushinteger( L, i_ret );
270 /*****************************************************************************
272 *****************************************************************************/
273 static int vlclua_fd_clr( lua_State * );
274 static int vlclua_fd_isset( lua_State * );
275 static int vlclua_fd_set( lua_State * );
276 static int vlclua_fd_zero( lua_State * );
278 static const luaL_Reg vlclua_fd_set_reg[] = {
279 { "clr", vlclua_fd_clr },
280 { "isset", vlclua_fd_isset },
281 { "set", vlclua_fd_set },
282 { "zero", vlclua_fd_zero },
286 static int vlclua_fd_set_new( lua_State *L )
288 fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
291 if( luaL_newmetatable( L, "fd_set" ) )
294 luaL_register( L, NULL, vlclua_fd_set_reg );
295 lua_setfield( L, -2, "__index" );
298 lua_setmetatable( L, -2 );
302 static int vlclua_fd_clr( lua_State *L )
304 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
305 int i_fd = luaL_checkint( L, 2 );
310 static int vlclua_fd_isset( lua_State *L )
312 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
313 int i_fd = luaL_checkint( L, 2 );
314 lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
318 static int vlclua_fd_set( lua_State *L )
320 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
321 size_t i_fd = luaL_checkint( L, 2 );
322 /* FIXME: we should really use poll() instead here, but that breaks the
323 * VLC/LUA API. On Windows, overflow protection is built-in FD_SET, not
324 * on POSIX. In both cases, run-time behavior will however be wrong. */
326 if( i_fd < FD_SETSIZE )
332 static int vlclua_fd_zero( lua_State *L )
334 fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
339 /*****************************************************************************
341 *****************************************************************************/
343 static int vlclua_fd_open( lua_State *L )
348 static int vlclua_fd_write( lua_State *L )
350 int i_fd = luaL_checkint( L, 1 );
353 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
354 i_len = luaL_optint( L, 3, i_len );
355 i_ret = write( i_fd, psz_buffer, i_len );
356 lua_pushinteger( L, i_ret );
360 static int vlclua_fd_read( lua_State *L )
362 int i_fd = luaL_checkint( L, 1 );
363 size_t i_len = luaL_optint( L, 2, 1 );
364 char psz_buffer[i_len];
365 i_len = read( i_fd, psz_buffer, i_len );
366 lua_pushlstring( L, psz_buffer, i_len );
370 /*****************************************************************************
372 *****************************************************************************/
373 static int vlclua_stat( lua_State *L )
375 #ifdef HAVE_SYS_STAT_H
376 const char *psz_path = luaL_checkstring( L, 1 );
378 if( utf8_stat( psz_path, &s ) )
380 //return luaL_error( L, "Couldn't stat %s.", psz_path );
382 if( S_ISREG( s.st_mode ) )
383 lua_pushstring( L, "file" );
384 else if( S_ISDIR( s.st_mode ) )
385 lua_pushstring( L, "dir" );
387 else if( S_ISCHR( s.st_mode ) )
388 lua_pushstring( L, "character device" );
391 else if( S_ISBLK( s.st_mode ) )
392 lua_pushstring( L, "block device" );
395 else if( S_ISFIFO( s.st_mode ) )
396 lua_pushstring( L, "fifo" );
399 else if( S_ISLNK( s.st_mode ) )
400 lua_pushstring( L, "symbolic link" );
403 else if( S_ISSOCK( s.st_mode ) )
404 lua_pushstring( L, "socket" );
407 lua_pushstring( L, "unknown" );
408 lua_setfield( L, -2, "type" );
409 lua_pushinteger( L, s.st_mode );
410 lua_setfield( L, -2, "mode" );
411 lua_pushinteger( L, s.st_uid );
412 lua_setfield( L, -2, "uid" );
413 lua_pushinteger( L, s.st_gid );
414 lua_setfield( L, -2, "gid" );
415 lua_pushinteger( L, s.st_size );
416 lua_setfield( L, -2, "size" );
417 lua_pushinteger( L, s.st_atime );
418 lua_setfield( L, -2, "access_time" );
419 lua_pushinteger( L, s.st_mtime );
420 lua_setfield( L, -2, "modification_time" );
421 lua_pushinteger( L, s.st_ctime );
422 lua_setfield( L, -2, "creation_time" );
425 # warning "Woops, looks like we don't have stat on your platform"
426 return luaL_error( L, "System is missing <sys/stat.h>" );
430 static int vlclua_opendir( lua_State *L )
432 const char *psz_dir = luaL_checkstring( L, 1 );
436 if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
437 return luaL_error( L, "cannot open directory `%s'.", psz_dir );
442 char *psz_filename = utf8_readdir( p_dir );
443 if( !psz_filename ) break;
445 lua_pushstring( L, psz_filename );
446 lua_rawseti( L, -2, i );
447 free( psz_filename );
453 /*****************************************************************************
455 *****************************************************************************/
456 static const luaL_Reg vlclua_net_reg[] = {
457 { "url_parse", vlclua_url_parse },
458 { "listen_tcp", vlclua_net_listen_tcp },
459 { "close", vlclua_net_close },
460 { "send", vlclua_net_send },
461 { "recv", vlclua_net_recv },
462 { "poll", vlclua_net_poll },
463 { "select", vlclua_net_select },
464 { "fd_set_new", vlclua_fd_set_new },
465 { "read", vlclua_fd_read },
466 { "write", vlclua_fd_write },
467 { "stat", vlclua_stat }, /* Not really "net" */
468 { "opendir", vlclua_opendir }, /* Not really "net" */
472 void luaopen_net( lua_State *L )
475 luaL_register( L, NULL, vlclua_net_reg );
476 #define ADD_CONSTANT( name, value ) \
477 lua_pushinteger( L, value ); \
478 lua_setfield( L, -2, name );
479 ADD_CONSTANT( "POLLIN", POLLIN )
480 ADD_CONSTANT( "POLLPRI", POLLPRI )
481 ADD_CONSTANT( "POLLOUT", POLLOUT )
482 ADD_CONSTANT( "POLLERR", POLLERR )
483 ADD_CONSTANT( "POLLHUP", POLLHUP )
484 ADD_CONSTANT( "POLLNVAL", POLLNVAL )
485 lua_setfield( L, -2, "net" );