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