]> git.sesse.net Git - vlc/blob - modules/lua/libs/net.c
aout_VolumeGet: return volume directly
[vlc] / modules / lua / libs / net.c
1 /*****************************************************************************
2  * net.c: Network related functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #ifdef WIN32
36 #include <io.h>
37 #endif
38
39 #include <vlc_common.h>
40 #include <vlc_network.h>
41 #include <vlc_url.h>
42 #include <vlc_fs.h>
43
44 #include <lua.h>        /* Low level lua C API */
45 #include <lauxlib.h>    /* Higher level C API */
46
47 #ifdef HAVE_POLL
48 #include <poll.h>       /* poll structures and defines */
49 #endif
50
51 #include <sys/stat.h>
52
53 #include<errno.h>
54 #include "../vlc.h"
55 #include "../libs.h"
56
57 /*****************************************************************************
58  *
59  *****************************************************************************/
60 static int vlclua_url_parse( lua_State *L )
61 {
62     const char *psz_url = luaL_checkstring( L, 1 );
63     const char *psz_option = luaL_optstring( L, 2, NULL );
64     vlc_url_t url;
65
66     vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
67
68     lua_newtable( L );
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" );
83
84     vlc_UrlClean( &url );
85
86     return 1;
87 }
88
89 /*****************************************************************************
90  * Net listen
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 * );
95
96 static const luaL_Reg vlclua_net_listen_reg[] = {
97     { "accept", vlclua_net_accept },
98     { "fds", vlclua_net_fds },
99     { NULL, NULL }
100 };
101
102 static int vlclua_net_listen_tcp( lua_State *L )
103 {
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 );
108     if( pi_fd == NULL )
109         return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
110
111     int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
112     *ppi_fd = pi_fd;
113
114     if( luaL_newmetatable( L, "net_listen" ) )
115     {
116         lua_newtable( L );
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" );
121     }
122
123     lua_setmetatable( L, -2 );
124     return 1;
125 }
126
127 static int vlclua_net_listen_close( lua_State *L )
128 {
129     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
130     net_ListenClose( *ppi_fd );
131     return 0;
132 }
133
134 static int vlclua_net_fds( lua_State *L )
135 {
136     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
137     int *pi_fd = *ppi_fd;
138
139     int i_count = 0;
140     while( pi_fd[i_count] != -1 )
141         lua_pushinteger( L, pi_fd[i_count++] );
142
143     return i_count;
144 }
145
146 static int vlclua_net_accept( lua_State *L )
147 {
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 );
151
152     lua_pushinteger( L, i_fd );
153     return 1;
154 }
155
156 /*****************************************************************************
157  *
158  *****************************************************************************/
159 static int vlclua_net_connect_tcp( lua_State *L )
160 {
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 );
166     return 1;
167 }
168
169 static int vlclua_net_close( lua_State *L )
170 {
171     int i_fd = luaL_checkint( L, 1 );
172     net_Close( i_fd );
173     return 0;
174 }
175
176 static int vlclua_net_send( lua_State *L )
177 {
178     int i_fd = luaL_checkint( L, 1 );
179     size_t i_len;
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 );
184     return 1;
185 }
186
187 static int vlclua_net_recv( lua_State *L )
188 {
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 );
194     return 1;
195 }
196
197 /*****************************************************************************
198  *
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 )
202 {
203     luaL_checktype( L, 1, LUA_TTABLE );
204
205     int i_fds = 0;
206     lua_pushnil( L );
207     while( lua_next( L, 1 ) )
208     {
209         i_fds++;
210         lua_pop( L, 1 );
211     }
212     struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
213     vlc_cleanup_push( free, p_fds );
214     lua_pushnil( L );
215     int i = 0;
216     while( lua_next( L, 1 ) )
217     {
218         p_fds[i].fd = luaL_checkinteger( L, -2 );
219         p_fds[i].events = luaL_checkinteger( L, -1 );
220         p_fds[i].revents = 0;
221         lua_pop( L, 1 );
222         i++;
223     }
224
225     int i_ret;
226     do
227         i_ret = poll( p_fds, i_fds, -1 );
228     while( i_ret == -1 );
229
230     for( i = 0; i < i_fds; i++ )
231     {
232         lua_pushinteger( L, p_fds[i].fd );
233         lua_pushinteger( L, p_fds[i].revents );
234         lua_settable( L, 1 );
235     }
236     lua_pushinteger( L, i_ret );
237     vlc_cleanup_run();
238     return 1;
239 }
240
241 /*****************************************************************************
242  *
243  *****************************************************************************/
244 /*
245 static int vlclua_fd_open( lua_State *L )
246 {
247 }
248 */
249
250 static int vlclua_fd_write( lua_State *L )
251 {
252     int i_fd = luaL_checkint( L, 1 );
253     size_t i_len;
254     ssize_t i_ret;
255     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
256     i_len = luaL_optint( L, 3, i_len );
257     i_ret = write( i_fd, psz_buffer, i_len );
258     lua_pushinteger( L, i_ret );
259     return 1;
260 }
261
262 static int vlclua_fd_read( lua_State *L )
263 {
264     int i_fd = luaL_checkint( L, 1 );
265     size_t i_len = luaL_optint( L, 2, 1 );
266     char psz_buffer[i_len];
267     ssize_t i_ret = read( i_fd, psz_buffer, i_len );
268     lua_pushlstring( L, psz_buffer, (i_ret >= 0) ? i_ret : 0 );
269     return 1;
270 }
271
272 /*****************************************************************************
273  *
274  *****************************************************************************/
275 static int vlclua_stat( lua_State *L )
276 {
277 #ifdef HAVE_SYS_STAT_H
278     const char *psz_path = luaL_checkstring( L, 1 );
279     struct stat s;
280     if( vlc_stat( psz_path, &s ) )
281         return 0;
282         //return luaL_error( L, "Couldn't stat %s.", psz_path );
283     lua_newtable( L );
284     if( S_ISREG( s.st_mode ) )
285         lua_pushliteral( L, "file" );
286     else if( S_ISDIR( s.st_mode ) )
287         lua_pushliteral( L, "dir" );
288 #ifdef S_ISCHR
289     else if( S_ISCHR( s.st_mode ) )
290         lua_pushliteral( L, "character device" );
291 #endif
292 #ifdef S_ISBLK
293     else if( S_ISBLK( s.st_mode ) )
294         lua_pushliteral( L, "block device" );
295 #endif
296 #ifdef S_ISFIFO
297     else if( S_ISFIFO( s.st_mode ) )
298         lua_pushliteral( L, "fifo" );
299 #endif
300 #ifdef S_ISLNK
301     else if( S_ISLNK( s.st_mode ) )
302         lua_pushliteral( L, "symbolic link" );
303 #endif
304 #ifdef S_ISSOCK
305     else if( S_ISSOCK( s.st_mode ) )
306         lua_pushliteral( L, "socket" );
307 #endif
308     else
309         lua_pushliteral( L, "unknown" );
310     lua_setfield( L, -2, "type" );
311     lua_pushinteger( L, s.st_mode );
312     lua_setfield( L, -2, "mode" );
313     lua_pushinteger( L, s.st_uid );
314     lua_setfield( L, -2, "uid" );
315     lua_pushinteger( L, s.st_gid );
316     lua_setfield( L, -2, "gid" );
317     lua_pushinteger( L, s.st_size );
318     lua_setfield( L, -2, "size" );
319     lua_pushinteger( L, s.st_atime );
320     lua_setfield( L, -2, "access_time" );
321     lua_pushinteger( L, s.st_mtime );
322     lua_setfield( L, -2, "modification_time" );
323     lua_pushinteger( L, s.st_ctime );
324     lua_setfield( L, -2, "creation_time" );
325     return 1;
326 #else
327 #   warning "Woops, looks like we don't have stat on your platform"
328     return luaL_error( L, "System is missing <sys/stat.h>" );
329 #endif
330 }
331
332 static int vlclua_opendir( lua_State *L )
333 {
334     const char *psz_dir = luaL_checkstring( L, 1 );
335     DIR *p_dir;
336     int i = 0;
337
338     if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
339         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
340
341     lua_newtable( L );
342     for( ;; )
343     {
344         char *psz_filename = vlc_readdir( p_dir );
345         if( !psz_filename ) break;
346         i++;
347         lua_pushstring( L, psz_filename );
348         lua_rawseti( L, -2, i );
349         free( psz_filename );
350     }
351     closedir( p_dir );
352     return 1;
353 }
354
355 /*****************************************************************************
356  *
357  *****************************************************************************/
358 static const luaL_Reg vlclua_net_reg[] = {
359     { "url_parse", vlclua_url_parse },
360     { "listen_tcp", vlclua_net_listen_tcp },
361     { "connect_tcp", vlclua_net_connect_tcp },
362     { "close", vlclua_net_close },
363     { "send", vlclua_net_send },
364     { "recv", vlclua_net_recv },
365     { "poll", vlclua_net_poll },
366     { "read", vlclua_fd_read },
367     { "write", vlclua_fd_write },
368     { "stat", vlclua_stat }, /* Not really "net" */
369     { "opendir", vlclua_opendir }, /* Not really "net" */
370     { NULL, NULL }
371 };
372
373 void luaopen_net( lua_State *L )
374 {
375     lua_newtable( L );
376     luaL_register( L, NULL, vlclua_net_reg );
377 #define ADD_CONSTANT( name, value )    \
378     lua_pushinteger( L, value ); \
379     lua_setfield( L, -2, name );
380     ADD_CONSTANT( "POLLIN", POLLIN )
381     ADD_CONSTANT( "POLLPRI", POLLPRI )
382     ADD_CONSTANT( "POLLOUT", POLLOUT )
383     ADD_CONSTANT( "POLLERR", POLLERR )
384     ADD_CONSTANT( "POLLHUP", POLLHUP )
385     ADD_CONSTANT( "POLLNVAL", POLLNVAL )
386     lua_setfield( L, -2, "net" );
387 }