]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/net.c
Add --data-path option. Access the src share directory now works from build tree.
[vlc] / modules / misc / 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 #include <vlc_common.h>
36 #include <vlc_network.h>
37 #include <vlc_url.h>
38
39 #include <lua.h>        /* Low level lua C API */
40 #include <lauxlib.h>    /* Higher level C API */
41
42 #ifdef HAVE_POLL
43 #include <poll.h>       /* poll structures and defines */
44 #endif
45
46 #include "../vlc.h"
47 #include "../libs.h"
48
49 /*****************************************************************************
50  *
51  *****************************************************************************/
52 static int vlclua_url_parse( lua_State *L )
53 {
54     const char *psz_url = luaL_checkstring( L, 1 );
55     const char *psz_option = luaL_optstring( L, 2, NULL );
56     vlc_url_t url;
57
58     vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
59
60     lua_newtable( L );
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" );
75
76     vlc_UrlClean( &url );
77
78     return 1;
79 }
80
81 /*****************************************************************************
82  * Net listen
83  *****************************************************************************/
84 static int vlclua_net_listen_close( lua_State * );
85 static int vlclua_net_accept( lua_State * );
86
87 static const luaL_Reg vlclua_net_listen_reg[] = {
88     { "accept", vlclua_net_accept },
89     { NULL, NULL }
90 };
91
92 static int vlclua_net_listen_tcp( lua_State *L )
93 {
94     vlc_object_t *p_this = vlclua_get_this( L );
95     const char *psz_host = luaL_checkstring( L, 1 );
96     int i_port = luaL_checkint( L, 2 );
97     int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
98     if( pi_fd == NULL )
99         return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
100
101     int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
102     *ppi_fd = pi_fd;
103
104     if( luaL_newmetatable( L, "net_listen" ) )
105     {
106         lua_newtable( L );
107         luaL_register( L, NULL, vlclua_net_listen_reg );
108         lua_setfield( L, -2, "__index" );
109         lua_pushcfunction( L, vlclua_net_listen_close );
110         lua_setfield( L, -2, "__gc" );
111     }
112
113     lua_setmetatable( L, -2 );
114     return 1;
115 }
116
117 static int vlclua_net_listen_close( lua_State *L )
118 {
119     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
120     net_ListenClose( *ppi_fd );
121     return 0;
122 }
123
124 static int vlclua_net_accept( lua_State *L )
125 {
126     vlc_object_t *p_this = vlclua_get_this( L );
127     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
128     int *pi_fd = *ppi_fd;
129     int i_timeout = luaL_optint( L, 2, -1 ); /* block is default */
130
131     /* Implement net_Accept with timeout */
132     int i_fd = -1;
133
134     unsigned int i_count = 1;
135     while( pi_fd[i_count] != -1 )
136         i_count++;
137
138     struct pollfd ufd[i_count+1];
139     unsigned int i;
140     for( i = 0; i < i_count; i++ )
141     {
142         ufd[i].fd = pi_fd[i];
143         ufd[i].events = POLLIN;
144     }
145
146     if( poll( ufd, i_count, i_timeout ) > 0 )
147     {
148         for( i = 0; i < i_count; i++ )
149         {
150             if( !ufd[i].revents ) continue;
151             i_fd = net_AcceptSingle( p_this, ufd[i].fd );
152             if( i_fd == -1 ) continue;
153             memmove( pi_fd + i, pi_fd + i + 1, i_count - (i + 1) );
154             pi_fd[i_count - 1] = ufd[i].fd;
155         }
156     }
157
158     lua_pushinteger( L, i_fd );
159     return 1;
160 }
161
162 /*****************************************************************************
163  *
164  *****************************************************************************/
165 static int vlclua_net_close( lua_State *L )
166 {
167     int i_fd = luaL_checkint( L, 1 );
168     net_Close( i_fd );
169     return 0;
170 }
171
172 static int vlclua_net_send( lua_State *L )
173 {
174     int i_fd = luaL_checkint( L, 1 );
175     size_t i_len;
176     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
177     i_len = luaL_optint( L, 3, i_len );
178     i_len = send( i_fd, psz_buffer, i_len, 0 );
179     lua_pushinteger( L, i_len );
180     return 1;
181 }
182
183 static int vlclua_net_recv( lua_State *L )
184 {
185     int i_fd = luaL_checkint( L, 1 );
186     size_t i_len = luaL_optint( L, 2, 1 );
187     char psz_buffer[i_len];
188     i_len = recv( i_fd, psz_buffer, i_len, 0 );
189     lua_pushlstring( L, psz_buffer, i_len );
190     return 1;
191 }
192
193 /*****************************************************************************
194  *
195  *****************************************************************************/
196 static int vlclua_net_select( lua_State *L )
197 {
198     int i_ret;
199     size_t i_nfds = luaL_checkint( L, 1 );
200     fd_set *fds_read = (fd_set*)luaL_checkudata( L, 2, "fd_set" );
201     fd_set *fds_write = (fd_set*)luaL_checkudata( L, 3, "fd_set" );
202     double f_timeout = luaL_checknumber( L, 4 );
203     struct timeval timeout;
204
205 #ifndef WIN32
206     if( i_nfds > FD_SETSIZE )
207         i_nfds = FD_SETSIZE;
208 #endif
209     timeout.tv_sec = (int)f_timeout;
210     timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
211     i_ret = select( i_nfds, fds_read, fds_write, 0, &timeout );
212     lua_pushinteger( L, i_ret );
213     lua_pushinteger( L, (double)timeout.tv_sec+((double)timeout.tv_usec)/1e-6 );
214     return 2;
215 }
216
217 /*****************************************************************************
218  *
219  *****************************************************************************/
220 static int vlclua_fd_clr( lua_State * );
221 static int vlclua_fd_isset( lua_State * );
222 static int vlclua_fd_set( lua_State * );
223 static int vlclua_fd_zero( lua_State * );
224
225 static const luaL_Reg vlclua_fd_set_reg[] = {
226     { "clr", vlclua_fd_clr },
227     { "isset", vlclua_fd_isset },
228     { "set", vlclua_fd_set },
229     { "zero", vlclua_fd_zero },
230     { NULL, NULL }
231 };
232
233 static int vlclua_fd_set_new( lua_State *L )
234 {
235     fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
236     FD_ZERO( fds );
237
238     if( luaL_newmetatable( L, "fd_set" ) )
239     {
240         lua_newtable( L );
241         luaL_register( L, NULL, vlclua_fd_set_reg );
242         lua_setfield( L, -2, "__index" );
243     }
244
245     lua_setmetatable( L, -2 );
246     return 1;
247 }
248
249 static int vlclua_fd_clr( lua_State *L )
250 {
251     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
252     int i_fd = luaL_checkint( L, 2 );
253     FD_CLR( i_fd, fds );
254     return 0;
255 }
256
257 static int vlclua_fd_isset( lua_State *L )
258 {
259     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
260     int i_fd = luaL_checkint( L, 2 );
261     lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
262     return 1;
263 }
264
265 static int vlclua_fd_set( lua_State *L )
266 {
267     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
268     size_t i_fd = luaL_checkint( L, 2 );
269     /* FIXME: we should really use poll() instead here, but that breaks the
270      * VLC/LUA API. On Windows, overflow protection is built-in FD_SET, not
271      * on POSIX. In both cases, run-time behavior will however be wrong. */
272 #ifndef WIN32
273     if( i_fd < FD_SETSIZE )
274 #endif
275         FD_SET( i_fd, fds );
276     return 0;
277 }
278
279 static int vlclua_fd_zero( lua_State *L )
280 {
281     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
282     FD_ZERO( fds );
283     return 0;
284 }
285
286 /*****************************************************************************
287  *
288  *****************************************************************************/
289 /*
290 static int vlclua_fd_open( lua_State *L )
291 {
292 }
293 */
294
295 static int vlclua_fd_write( lua_State *L )
296 {
297     int i_fd = luaL_checkint( L, 1 );
298     size_t i_len;
299     ssize_t i_ret;
300     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
301     i_len = luaL_optint( L, 3, i_len );
302     i_ret = write( i_fd, psz_buffer, i_len );
303     lua_pushinteger( L, i_ret );
304     return 1;
305 }
306
307 static int vlclua_fd_read( lua_State *L )
308 {
309     int i_fd = luaL_checkint( L, 1 );
310     size_t i_len = luaL_optint( L, 2, 1 );
311     char psz_buffer[i_len];
312     i_len = read( i_fd, psz_buffer, i_len );
313     lua_pushlstring( L, psz_buffer, i_len );
314     return 1;
315 }
316
317 /*****************************************************************************
318  *
319  *****************************************************************************/
320 static int vlclua_stat( lua_State *L )
321 {
322 #ifdef HAVE_SYS_STAT_H
323     const char *psz_path = luaL_checkstring( L, 1 );
324     struct stat s;
325     if( utf8_stat( psz_path, &s ) )
326         return 0;
327         //return luaL_error( L, "Couldn't stat %s.", psz_path );
328     lua_newtable( L );
329     if( S_ISREG( s.st_mode ) )
330         lua_pushstring( L, "file" );
331     else if( S_ISDIR( s.st_mode ) )
332         lua_pushstring( L, "dir" );
333 #ifdef S_ISCHR
334     else if( S_ISCHR( s.st_mode ) )
335         lua_pushstring( L, "character device" );
336 #endif
337 #ifdef S_ISBLK
338     else if( S_ISBLK( s.st_mode ) )
339         lua_pushstring( L, "block device" );
340 #endif
341 #ifdef S_ISFIFO
342     else if( S_ISFIFO( s.st_mode ) )
343         lua_pushstring( L, "fifo" );
344 #endif
345 #ifdef S_ISLNK
346     else if( S_ISLNK( s.st_mode ) )
347         lua_pushstring( L, "symbolic link" );
348 #endif
349 #ifdef S_ISSOCK
350     else if( S_ISSOCK( s.st_mode ) )
351         lua_pushstring( L, "socket" );
352 #endif
353     else
354         lua_pushstring( L, "unknown" );
355     lua_setfield( L, -2, "type" );
356     lua_pushinteger( L, s.st_mode );
357     lua_setfield( L, -2, "mode" );
358     lua_pushinteger( L, s.st_uid );
359     lua_setfield( L, -2, "uid" );
360     lua_pushinteger( L, s.st_gid );
361     lua_setfield( L, -2, "gid" );
362     lua_pushinteger( L, s.st_size );
363     lua_setfield( L, -2, "size" );
364     lua_pushinteger( L, s.st_atime );
365     lua_setfield( L, -2, "access_time" );
366     lua_pushinteger( L, s.st_mtime );
367     lua_setfield( L, -2, "modification_time" );
368     lua_pushinteger( L, s.st_ctime );
369     lua_setfield( L, -2, "creation_time" );
370     return 1;
371 #else
372 #   warning "Woops, looks like we don't have stat on your platform"
373     return luaL_error( L, "System is missing <sys/stat.h>" );
374 #endif
375 }
376
377 static int vlclua_opendir( lua_State *L )
378 {
379     const char *psz_dir = luaL_checkstring( L, 1 );
380     DIR *p_dir;
381     int i = 0;
382
383     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
384         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
385
386     lua_newtable( L );
387     for( ;; )
388     {
389         char *psz_filename = utf8_readdir( p_dir );
390         if( !psz_filename ) break;
391         i++;
392         lua_pushstring( L, psz_filename );
393         lua_rawseti( L, -2, i );
394         free( psz_filename );
395     }
396     closedir( p_dir );
397     return 1;
398 }
399
400 /*****************************************************************************
401  *
402  *****************************************************************************/
403 static const luaL_Reg vlclua_net_reg[] = {
404     { "url_parse", vlclua_url_parse },
405     { "listen_tcp", vlclua_net_listen_tcp },
406     { "close", vlclua_net_close },
407     { "send", vlclua_net_send },
408     { "recv", vlclua_net_recv },
409     { "select", vlclua_net_select },
410     { "fd_set_new", vlclua_fd_set_new },
411     { "read", vlclua_fd_read },
412     { "write", vlclua_fd_write },
413     { "stat", vlclua_stat }, /* Not really "net" */
414     { "opendir", vlclua_opendir }, /* Not really "net" */
415     { NULL, NULL }
416 };
417
418 void luaopen_net( lua_State *L )
419 {
420     lua_newtable( L );
421     luaL_register( L, NULL, vlclua_net_reg );
422     lua_setfield( L, -2, "net" );
423 }