]> git.sesse.net Git - vlc/blob - modules/lua/libs/net.c
Lua: disable net.(poll|read|write) on Windows and document
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <errno.h>
32 #ifdef _WIN32
33 #include <io.h>
34 #endif
35 #ifdef HAVE_POLL
36 #include <poll.h>       /* poll structures and defines */
37 #endif
38 #include <sys/stat.h>
39
40 #include <vlc_common.h>
41 #include <vlc_network.h>
42 #include <vlc_url.h>
43 #include <vlc_fs.h>
44 #include <vlc_interface.h>
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     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
129     int *pi_fd = *ppi_fd;
130
131     int i_count = 0;
132     while( pi_fd[i_count] != -1 )
133         lua_pushinteger( L, pi_fd[i_count++] );
134
135     return i_count;
136 }
137
138 static int vlclua_net_accept( lua_State *L )
139 {
140     vlc_object_t *p_this = vlclua_get_this( L );
141     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
142     int i_fd = net_Accept( p_this, *ppi_fd );
143
144     lua_pushinteger( L, i_fd );
145     return 1;
146 }
147
148 /*****************************************************************************
149  *
150  *****************************************************************************/
151 static int vlclua_net_connect_tcp( lua_State *L )
152 {
153     vlc_object_t *p_this = vlclua_get_this( L );
154     const char *psz_host = luaL_checkstring( L, 1 );
155     int i_port = luaL_checkint( L, 2 );
156     int i_fd = net_Connect( p_this, psz_host, i_port, SOCK_STREAM, IPPROTO_TCP );
157     lua_pushinteger( L, i_fd );
158     return 1;
159 }
160
161 static int vlclua_net_close( lua_State *L )
162 {
163     int i_fd = luaL_checkint( L, 1 );
164     net_Close( i_fd );
165     return 0;
166 }
167
168 static int vlclua_net_send( lua_State *L )
169 {
170     int i_fd = luaL_checkint( L, 1 );
171     size_t i_len;
172     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
173     i_len = luaL_optint( L, 3, i_len );
174     i_len = send( i_fd, psz_buffer, i_len, 0 );
175     lua_pushinteger( L, i_len );
176     return 1;
177 }
178
179 static int vlclua_net_recv( lua_State *L )
180 {
181     int i_fd = luaL_checkint( L, 1 );
182     size_t i_len = luaL_optint( L, 2, 1 );
183     char psz_buffer[i_len];
184     ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
185     if( i_ret > 0 )
186         lua_pushlstring( L, psz_buffer, i_ret );
187     else
188         lua_pushnil( L );
189     return 1;
190 }
191
192 #ifndef _WIN32
193 /*****************************************************************************
194  *
195  *****************************************************************************/
196 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
197 static int vlclua_net_poll( lua_State *L )
198 {
199     intf_thread_t *intf = (intf_thread_t *)vlclua_get_this( L );
200     intf_sys_t *sys = intf->p_sys;
201
202     luaL_checktype( L, 1, LUA_TTABLE );
203
204     int i_fds = 1;
205     lua_pushnil( L );
206     while( lua_next( L, 1 ) )
207     {
208         i_fds++;
209         lua_pop( L, 1 );
210     }
211
212     struct pollfd *p_fds = xmalloc( i_fds * sizeof( *p_fds ) );
213
214     lua_pushnil( L );
215     int i = 1;
216     p_fds[0].fd = sys->fd[0];
217     p_fds[0].events = POLLIN;
218     while( lua_next( L, 1 ) )
219     {
220         p_fds[i].fd = luaL_checkinteger( L, -2 );
221         p_fds[i].events = luaL_checkinteger( L, -1 );
222         lua_pop( L, 1 );
223         i++;
224     }
225
226     int i_ret;
227     do
228         i_ret = poll( p_fds, i_fds, -1 );
229     while( i_ret == -1 && errno == EINTR );
230
231     for( i = 1; i < i_fds; i++ )
232     {
233         lua_pushinteger( L, p_fds[i].fd );
234         lua_pushinteger( L, p_fds[i].revents );
235         lua_settable( L, 1 );
236     }
237     lua_pushinteger( L, i_ret );
238
239     if( p_fds[0].revents )
240         i_ret = luaL_error( L, "Interrupted." );
241     else
242         i_ret = 1;
243     free( p_fds );
244
245     return i_ret;
246 }
247
248 /*****************************************************************************
249  *
250  *****************************************************************************/
251 /*
252 static int vlclua_fd_open( lua_State *L )
253 {
254 }
255 */
256
257 static int vlclua_fd_write( lua_State *L )
258 {
259     int i_fd = luaL_checkint( L, 1 );
260     size_t i_len;
261     ssize_t i_ret;
262     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
263     i_len = luaL_optint( L, 3, i_len );
264     i_ret = write( i_fd, psz_buffer, i_len );
265     lua_pushinteger( L, i_ret );
266     return 1;
267 }
268
269 static int vlclua_fd_read( lua_State *L )
270 {
271     int i_fd = luaL_checkint( L, 1 );
272     size_t i_len = luaL_optint( L, 2, 1 );
273     char psz_buffer[i_len];
274     ssize_t i_ret = read( i_fd, psz_buffer, i_len );
275     if( i_ret > 0 )
276         lua_pushlstring( L, psz_buffer, i_ret );
277     else
278         lua_pushnil( L );
279     return 1;
280 }
281 #endif
282
283 /*****************************************************************************
284  *
285  *****************************************************************************/
286 static int vlclua_stat( lua_State *L )
287 {
288     const char *psz_path = luaL_checkstring( L, 1 );
289     struct stat s;
290     if( vlc_stat( psz_path, &s ) )
291         return 0;
292         //return luaL_error( L, "Couldn't stat %s.", psz_path );
293     lua_newtable( L );
294     if( S_ISREG( s.st_mode ) )
295         lua_pushliteral( L, "file" );
296     else if( S_ISDIR( s.st_mode ) )
297         lua_pushliteral( L, "dir" );
298 #ifdef S_ISCHR
299     else if( S_ISCHR( s.st_mode ) )
300         lua_pushliteral( L, "character device" );
301 #endif
302 #ifdef S_ISBLK
303     else if( S_ISBLK( s.st_mode ) )
304         lua_pushliteral( L, "block device" );
305 #endif
306 #ifdef S_ISFIFO
307     else if( S_ISFIFO( s.st_mode ) )
308         lua_pushliteral( L, "fifo" );
309 #endif
310 #ifdef S_ISLNK
311     else if( S_ISLNK( s.st_mode ) )
312         lua_pushliteral( L, "symbolic link" );
313 #endif
314 #ifdef S_ISSOCK
315     else if( S_ISSOCK( s.st_mode ) )
316         lua_pushliteral( L, "socket" );
317 #endif
318     else
319         lua_pushliteral( L, "unknown" );
320     lua_setfield( L, -2, "type" );
321     lua_pushinteger( L, s.st_mode );
322     lua_setfield( L, -2, "mode" );
323     lua_pushinteger( L, s.st_uid );
324     lua_setfield( L, -2, "uid" );
325     lua_pushinteger( L, s.st_gid );
326     lua_setfield( L, -2, "gid" );
327     lua_pushinteger( L, s.st_size );
328     lua_setfield( L, -2, "size" );
329     lua_pushinteger( L, s.st_atime );
330     lua_setfield( L, -2, "access_time" );
331     lua_pushinteger( L, s.st_mtime );
332     lua_setfield( L, -2, "modification_time" );
333     lua_pushinteger( L, s.st_ctime );
334     lua_setfield( L, -2, "creation_time" );
335     return 1;
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 #ifndef _WIN32
372     { "poll", vlclua_net_poll },
373     { "read", vlclua_fd_read },
374     { "write", vlclua_fd_write },
375 #endif
376     { "stat", vlclua_stat }, /* Not really "net" */
377     { "opendir", vlclua_opendir }, /* Not really "net" */
378     { NULL, NULL }
379 };
380
381 void luaopen_net( lua_State *L )
382 {
383     lua_newtable( L );
384     luaL_register( L, NULL, vlclua_net_reg );
385 #define ADD_CONSTANT( name, value )    \
386     lua_pushinteger( L, value ); \
387     lua_setfield( L, -2, name );
388     ADD_CONSTANT( "POLLIN", POLLIN )
389     ADD_CONSTANT( "POLLPRI", POLLPRI )
390     ADD_CONSTANT( "POLLOUT", POLLOUT )
391     ADD_CONSTANT( "POLLERR", POLLERR )
392     ADD_CONSTANT( "POLLHUP", POLLHUP )
393     ADD_CONSTANT( "POLLNVAL", POLLNVAL )
394     lua_setfield( L, -2, "net" );
395 }