]> git.sesse.net Git - vlc/blob - modules/lua/libs/net.c
dbus: remove unused variable
[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     if( i_ret > 0 )
194         lua_pushlstring( L, psz_buffer, i_ret );
195     else
196         lua_pushnil( L );
197     return 1;
198 }
199
200 /*****************************************************************************
201  *
202  *****************************************************************************/
203 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
204 static int vlclua_net_poll( lua_State *L )
205 {
206     luaL_checktype( L, 1, LUA_TTABLE );
207
208     int i_fds = 0;
209     lua_pushnil( L );
210     while( lua_next( L, 1 ) )
211     {
212         i_fds++;
213         lua_pop( L, 1 );
214     }
215     struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
216     vlc_cleanup_push( free, p_fds );
217     lua_pushnil( L );
218     int i = 0;
219     while( lua_next( L, 1 ) )
220     {
221         p_fds[i].fd = luaL_checkinteger( L, -2 );
222         p_fds[i].events = luaL_checkinteger( L, -1 );
223         p_fds[i].revents = 0;
224         lua_pop( L, 1 );
225         i++;
226     }
227
228     int i_ret;
229     do
230         i_ret = poll( p_fds, i_fds, -1 );
231     while( i_ret == -1 );
232
233     for( i = 0; i < i_fds; i++ )
234     {
235         lua_pushinteger( L, p_fds[i].fd );
236         lua_pushinteger( L, p_fds[i].revents );
237         lua_settable( L, 1 );
238     }
239     lua_pushinteger( L, i_ret );
240     vlc_cleanup_run();
241     return 1;
242 }
243
244 /*****************************************************************************
245  *
246  *****************************************************************************/
247 /*
248 static int vlclua_fd_open( lua_State *L )
249 {
250 }
251 */
252
253 static int vlclua_fd_write( lua_State *L )
254 {
255     int i_fd = luaL_checkint( L, 1 );
256     size_t i_len;
257     ssize_t i_ret;
258     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
259     i_len = luaL_optint( L, 3, i_len );
260     i_ret = write( i_fd, psz_buffer, i_len );
261     lua_pushinteger( L, i_ret );
262     return 1;
263 }
264
265 static int vlclua_fd_read( lua_State *L )
266 {
267     int i_fd = luaL_checkint( L, 1 );
268     size_t i_len = luaL_optint( L, 2, 1 );
269     char psz_buffer[i_len];
270     ssize_t i_ret = read( i_fd, psz_buffer, i_len );
271     if( i_ret > 0 )
272         lua_pushlstring( L, psz_buffer, i_ret );
273     else
274         lua_pushnil( L );
275     return 1;
276 }
277
278 /*****************************************************************************
279  *
280  *****************************************************************************/
281 static int vlclua_stat( lua_State *L )
282 {
283 #ifdef HAVE_SYS_STAT_H
284     const char *psz_path = luaL_checkstring( L, 1 );
285     struct stat s;
286     if( vlc_stat( psz_path, &s ) )
287         return 0;
288         //return luaL_error( L, "Couldn't stat %s.", psz_path );
289     lua_newtable( L );
290     if( S_ISREG( s.st_mode ) )
291         lua_pushliteral( L, "file" );
292     else if( S_ISDIR( s.st_mode ) )
293         lua_pushliteral( L, "dir" );
294 #ifdef S_ISCHR
295     else if( S_ISCHR( s.st_mode ) )
296         lua_pushliteral( L, "character device" );
297 #endif
298 #ifdef S_ISBLK
299     else if( S_ISBLK( s.st_mode ) )
300         lua_pushliteral( L, "block device" );
301 #endif
302 #ifdef S_ISFIFO
303     else if( S_ISFIFO( s.st_mode ) )
304         lua_pushliteral( L, "fifo" );
305 #endif
306 #ifdef S_ISLNK
307     else if( S_ISLNK( s.st_mode ) )
308         lua_pushliteral( L, "symbolic link" );
309 #endif
310 #ifdef S_ISSOCK
311     else if( S_ISSOCK( s.st_mode ) )
312         lua_pushliteral( L, "socket" );
313 #endif
314     else
315         lua_pushliteral( L, "unknown" );
316     lua_setfield( L, -2, "type" );
317     lua_pushinteger( L, s.st_mode );
318     lua_setfield( L, -2, "mode" );
319     lua_pushinteger( L, s.st_uid );
320     lua_setfield( L, -2, "uid" );
321     lua_pushinteger( L, s.st_gid );
322     lua_setfield( L, -2, "gid" );
323     lua_pushinteger( L, s.st_size );
324     lua_setfield( L, -2, "size" );
325     lua_pushinteger( L, s.st_atime );
326     lua_setfield( L, -2, "access_time" );
327     lua_pushinteger( L, s.st_mtime );
328     lua_setfield( L, -2, "modification_time" );
329     lua_pushinteger( L, s.st_ctime );
330     lua_setfield( L, -2, "creation_time" );
331     return 1;
332 #else
333 #   warning "Woops, looks like we don't have stat on your platform"
334     return luaL_error( L, "System is missing <sys/stat.h>" );
335 #endif
336 }
337
338 static int vlclua_opendir( lua_State *L )
339 {
340     const char *psz_dir = luaL_checkstring( L, 1 );
341     DIR *p_dir;
342     int i = 0;
343
344     if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
345         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
346
347     lua_newtable( L );
348     for( ;; )
349     {
350         char *psz_filename = vlc_readdir( p_dir );
351         if( !psz_filename ) break;
352         i++;
353         lua_pushstring( L, psz_filename );
354         lua_rawseti( L, -2, i );
355         free( psz_filename );
356     }
357     closedir( p_dir );
358     return 1;
359 }
360
361 /*****************************************************************************
362  *
363  *****************************************************************************/
364 static const luaL_Reg vlclua_net_reg[] = {
365     { "url_parse", vlclua_url_parse },
366     { "listen_tcp", vlclua_net_listen_tcp },
367     { "connect_tcp", vlclua_net_connect_tcp },
368     { "close", vlclua_net_close },
369     { "send", vlclua_net_send },
370     { "recv", vlclua_net_recv },
371     { "poll", vlclua_net_poll },
372     { "read", vlclua_fd_read },
373     { "write", vlclua_fd_write },
374     { "stat", vlclua_stat }, /* Not really "net" */
375     { "opendir", vlclua_opendir }, /* Not really "net" */
376     { NULL, NULL }
377 };
378
379 void luaopen_net( lua_State *L )
380 {
381     lua_newtable( L );
382     luaL_register( L, NULL, vlclua_net_reg );
383 #define ADD_CONSTANT( name, value )    \
384     lua_pushinteger( L, value ); \
385     lua_setfield( L, -2, name );
386     ADD_CONSTANT( "POLLIN", POLLIN )
387     ADD_CONSTANT( "POLLPRI", POLLPRI )
388     ADD_CONSTANT( "POLLOUT", POLLOUT )
389     ADD_CONSTANT( "POLLERR", POLLERR )
390     ADD_CONSTANT( "POLLHUP", POLLHUP )
391     ADD_CONSTANT( "POLLNVAL", POLLNVAL )
392     lua_setfield( L, -2, "net" );
393 }