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 *****************************************************************************/
36 #include <poll.h> /* poll structures and defines */
40 #include <vlc_common.h>
41 #include <vlc_network.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static int vlclua_url_parse( lua_State *L )
53 const char *psz_url = luaL_checkstring( L, 1 );
54 const char *psz_option = luaL_optstring( L, 2, NULL );
57 vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
60 lua_pushstring( L, url.psz_protocol );
61 lua_setfield( L, -2, "protocol" );
62 lua_pushstring( L, url.psz_username );
63 lua_setfield( L, -2, "username" );
64 lua_pushstring( L, url.psz_password );
65 lua_setfield( L, -2, "password" );
66 lua_pushstring( L, url.psz_host );
67 lua_setfield( L, -2, "host" );
68 lua_pushinteger( L, url.i_port );
69 lua_setfield( L, -2, "port" );
70 lua_pushstring( L, url.psz_path );
71 lua_setfield( L, -2, "path" );
72 lua_pushstring( L, url.psz_option );
73 lua_setfield( L, -2, "option" );
80 /*****************************************************************************
82 *****************************************************************************/
83 static int vlclua_net_listen_close( lua_State * );
84 static int vlclua_net_accept( lua_State * );
85 static int vlclua_net_fds( lua_State * );
87 static const luaL_Reg vlclua_net_listen_reg[] = {
88 { "accept", vlclua_net_accept },
89 { "fds", vlclua_net_fds },
93 static int vlclua_net_listen_tcp( lua_State *L )
95 vlc_object_t *p_this = vlclua_get_this( L );
96 const char *psz_host = luaL_checkstring( L, 1 );
97 int i_port = luaL_checkint( L, 2 );
98 int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
100 return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
102 int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
105 if( luaL_newmetatable( L, "net_listen" ) )
108 luaL_register( L, NULL, vlclua_net_listen_reg );
109 lua_setfield( L, -2, "__index" );
110 lua_pushcfunction( L, vlclua_net_listen_close );
111 lua_setfield( L, -2, "__gc" );
114 lua_setmetatable( L, -2 );
118 static int vlclua_net_listen_close( lua_State *L )
120 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
121 net_ListenClose( *ppi_fd );
125 static int vlclua_net_fds( lua_State *L )
127 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
128 int *pi_fd = *ppi_fd;
131 while( pi_fd[i_count] != -1 )
132 lua_pushinteger( L, pi_fd[i_count++] );
137 static int vlclua_net_accept( lua_State *L )
139 vlc_object_t *p_this = vlclua_get_this( L );
140 int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
141 int i_fd = net_Accept( p_this, *ppi_fd );
143 lua_pushinteger( L, i_fd );
147 /*****************************************************************************
149 *****************************************************************************/
150 static int vlclua_net_connect_tcp( lua_State *L )
152 vlc_object_t *p_this = vlclua_get_this( L );
153 const char *psz_host = luaL_checkstring( L, 1 );
154 int i_port = luaL_checkint( L, 2 );
155 int i_fd = net_Connect( p_this, psz_host, i_port, SOCK_STREAM, IPPROTO_TCP );
156 lua_pushinteger( L, i_fd );
160 static int vlclua_net_close( lua_State *L )
162 int i_fd = luaL_checkint( L, 1 );
167 static int vlclua_net_send( lua_State *L )
169 int i_fd = luaL_checkint( L, 1 );
171 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
172 i_len = luaL_optint( L, 3, i_len );
173 i_len = send( i_fd, psz_buffer, i_len, 0 );
174 lua_pushinteger( L, i_len );
178 static int vlclua_net_recv( lua_State *L )
180 int i_fd = luaL_checkint( L, 1 );
181 size_t i_len = luaL_optint( L, 2, 1 );
182 char psz_buffer[i_len];
183 ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
185 lua_pushlstring( L, psz_buffer, i_ret );
191 /*****************************************************************************
193 *****************************************************************************/
194 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
195 static int vlclua_net_poll( lua_State *L )
197 luaL_checktype( L, 1, LUA_TTABLE );
201 while( lua_next( L, 1 ) )
206 struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
207 vlc_cleanup_push( free, p_fds );
210 while( lua_next( L, 1 ) )
212 p_fds[i].fd = luaL_checkinteger( L, -2 );
213 p_fds[i].events = luaL_checkinteger( L, -1 );
214 p_fds[i].revents = 0;
221 i_ret = poll( p_fds, i_fds, -1 );
222 while( i_ret == -1 );
224 for( i = 0; i < i_fds; i++ )
226 lua_pushinteger( L, p_fds[i].fd );
227 lua_pushinteger( L, p_fds[i].revents );
228 lua_settable( L, 1 );
230 lua_pushinteger( L, i_ret );
235 /*****************************************************************************
237 *****************************************************************************/
239 static int vlclua_fd_open( lua_State *L )
244 static int vlclua_fd_write( lua_State *L )
246 int i_fd = luaL_checkint( L, 1 );
249 const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
250 i_len = luaL_optint( L, 3, i_len );
251 i_ret = write( i_fd, psz_buffer, i_len );
252 lua_pushinteger( L, i_ret );
256 static int vlclua_fd_read( lua_State *L )
258 int i_fd = luaL_checkint( L, 1 );
259 size_t i_len = luaL_optint( L, 2, 1 );
260 char psz_buffer[i_len];
261 ssize_t i_ret = read( i_fd, psz_buffer, i_len );
263 lua_pushlstring( L, psz_buffer, i_ret );
269 /*****************************************************************************
271 *****************************************************************************/
272 static int vlclua_stat( lua_State *L )
274 const char *psz_path = luaL_checkstring( L, 1 );
276 if( vlc_stat( psz_path, &s ) )
278 //return luaL_error( L, "Couldn't stat %s.", psz_path );
280 if( S_ISREG( s.st_mode ) )
281 lua_pushliteral( L, "file" );
282 else if( S_ISDIR( s.st_mode ) )
283 lua_pushliteral( L, "dir" );
285 else if( S_ISCHR( s.st_mode ) )
286 lua_pushliteral( L, "character device" );
289 else if( S_ISBLK( s.st_mode ) )
290 lua_pushliteral( L, "block device" );
293 else if( S_ISFIFO( s.st_mode ) )
294 lua_pushliteral( L, "fifo" );
297 else if( S_ISLNK( s.st_mode ) )
298 lua_pushliteral( L, "symbolic link" );
301 else if( S_ISSOCK( s.st_mode ) )
302 lua_pushliteral( L, "socket" );
305 lua_pushliteral( L, "unknown" );
306 lua_setfield( L, -2, "type" );
307 lua_pushinteger( L, s.st_mode );
308 lua_setfield( L, -2, "mode" );
309 lua_pushinteger( L, s.st_uid );
310 lua_setfield( L, -2, "uid" );
311 lua_pushinteger( L, s.st_gid );
312 lua_setfield( L, -2, "gid" );
313 lua_pushinteger( L, s.st_size );
314 lua_setfield( L, -2, "size" );
315 lua_pushinteger( L, s.st_atime );
316 lua_setfield( L, -2, "access_time" );
317 lua_pushinteger( L, s.st_mtime );
318 lua_setfield( L, -2, "modification_time" );
319 lua_pushinteger( L, s.st_ctime );
320 lua_setfield( L, -2, "creation_time" );
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" );