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