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