]> git.sesse.net Git - vlc/blob - modules/lua/libs/net.c
4c3ee72431f290db7ffefdb91906171cffdfb4c4
[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
45 #include "../vlc.h"
46 #include "../libs.h"
47
48 /*****************************************************************************
49  *
50  *****************************************************************************/
51 static int vlclua_url_parse( lua_State *L )
52 {
53     const char *psz_url = luaL_checkstring( L, 1 );
54     const char *psz_option = luaL_optstring( L, 2, NULL );
55     vlc_url_t url;
56
57     vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
58
59     lua_newtable( L );
60     lua_pushstring( L, url.psz_protocol );
61     lua_setfield( L, -2, "protocol" );
62     lua_pushstring( L, url.psz_username );
63     lua_setfield( L, -2, "username" );
64     lua_pushstring( L, url.psz_password );
65     lua_setfield( L, -2, "password" );
66     lua_pushstring( L, url.psz_host );
67     lua_setfield( L, -2, "host" );
68     lua_pushinteger( L, url.i_port );
69     lua_setfield( L, -2, "port" );
70     lua_pushstring( L, url.psz_path );
71     lua_setfield( L, -2, "path" );
72     lua_pushstring( L, url.psz_option );
73     lua_setfield( L, -2, "option" );
74
75     vlc_UrlClean( &url );
76
77     return 1;
78 }
79
80 /*****************************************************************************
81  * Net listen
82  *****************************************************************************/
83 static int vlclua_net_listen_close( lua_State * );
84 static int vlclua_net_accept( lua_State * );
85 static int vlclua_net_fds( lua_State * );
86
87 static const luaL_Reg vlclua_net_listen_reg[] = {
88     { "accept", vlclua_net_accept },
89     { "fds", vlclua_net_fds },
90     { NULL, NULL }
91 };
92
93 static int vlclua_net_listen_tcp( lua_State *L )
94 {
95     vlc_object_t *p_this = vlclua_get_this( L );
96     const char *psz_host = luaL_checkstring( L, 1 );
97     int i_port = luaL_checkint( L, 2 );
98     int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
99     if( pi_fd == NULL )
100         return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
101
102     int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
103     *ppi_fd = pi_fd;
104
105     if( luaL_newmetatable( L, "net_listen" ) )
106     {
107         lua_newtable( L );
108         luaL_register( L, NULL, vlclua_net_listen_reg );
109         lua_setfield( L, -2, "__index" );
110         lua_pushcfunction( L, vlclua_net_listen_close );
111         lua_setfield( L, -2, "__gc" );
112     }
113
114     lua_setmetatable( L, -2 );
115     return 1;
116 }
117
118 static int vlclua_net_listen_close( lua_State *L )
119 {
120     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
121     net_ListenClose( *ppi_fd );
122     return 0;
123 }
124
125 static int vlclua_net_fds( lua_State *L )
126 {
127     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
128     int *pi_fd = *ppi_fd;
129
130     int i_count = 0;
131     while( pi_fd[i_count] != -1 )
132         lua_pushinteger( L, pi_fd[i_count++] );
133
134     return i_count;
135 }
136
137 static int vlclua_net_accept( lua_State *L )
138 {
139     vlc_object_t *p_this = vlclua_get_this( L );
140     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
141     int i_fd = net_Accept( p_this, *ppi_fd );
142
143     lua_pushinteger( L, i_fd );
144     return 1;
145 }
146
147 /*****************************************************************************
148  *
149  *****************************************************************************/
150 static int vlclua_net_connect_tcp( lua_State *L )
151 {
152     vlc_object_t *p_this = vlclua_get_this( L );
153     const char *psz_host = luaL_checkstring( L, 1 );
154     int i_port = luaL_checkint( L, 2 );
155     int i_fd = net_Connect( p_this, psz_host, i_port, SOCK_STREAM, IPPROTO_TCP );
156     lua_pushinteger( L, i_fd );
157     return 1;
158 }
159
160 static int vlclua_net_close( lua_State *L )
161 {
162     int i_fd = luaL_checkint( L, 1 );
163     net_Close( i_fd );
164     return 0;
165 }
166
167 static int vlclua_net_send( lua_State *L )
168 {
169     int i_fd = luaL_checkint( L, 1 );
170     size_t i_len;
171     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
172     i_len = luaL_optint( L, 3, i_len );
173     i_len = send( i_fd, psz_buffer, i_len, 0 );
174     lua_pushinteger( L, i_len );
175     return 1;
176 }
177
178 static int vlclua_net_recv( lua_State *L )
179 {
180     int i_fd = luaL_checkint( L, 1 );
181     size_t i_len = luaL_optint( L, 2, 1 );
182     char psz_buffer[i_len];
183     ssize_t i_ret = recv( i_fd, psz_buffer, i_len, 0 );
184     if( i_ret > 0 )
185         lua_pushlstring( L, psz_buffer, i_ret );
186     else
187         lua_pushnil( L );
188     return 1;
189 }
190
191 /*****************************************************************************
192  *
193  *****************************************************************************/
194 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
195 static int vlclua_net_poll( lua_State *L )
196 {
197     luaL_checktype( L, 1, LUA_TTABLE );
198
199     int i_fds = 0;
200     lua_pushnil( L );
201     while( lua_next( L, 1 ) )
202     {
203         i_fds++;
204         lua_pop( L, 1 );
205     }
206     struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
207     vlc_cleanup_push( free, p_fds );
208     lua_pushnil( L );
209     int i = 0;
210     while( lua_next( L, 1 ) )
211     {
212         p_fds[i].fd = luaL_checkinteger( L, -2 );
213         p_fds[i].events = luaL_checkinteger( L, -1 );
214         p_fds[i].revents = 0;
215         lua_pop( L, 1 );
216         i++;
217     }
218
219     int i_ret;
220     do
221         i_ret = poll( p_fds, i_fds, -1 );
222     while( i_ret == -1 );
223
224     for( i = 0; i < i_fds; i++ )
225     {
226         lua_pushinteger( L, p_fds[i].fd );
227         lua_pushinteger( L, p_fds[i].revents );
228         lua_settable( L, 1 );
229     }
230     lua_pushinteger( L, i_ret );
231     vlc_cleanup_run();
232     return 1;
233 }
234
235 /*****************************************************************************
236  *
237  *****************************************************************************/
238 /*
239 static int vlclua_fd_open( lua_State *L )
240 {
241 }
242 */
243
244 static int vlclua_fd_write( lua_State *L )
245 {
246     int i_fd = luaL_checkint( L, 1 );
247     size_t i_len;
248     ssize_t i_ret;
249     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
250     i_len = luaL_optint( L, 3, i_len );
251     i_ret = write( i_fd, psz_buffer, i_len );
252     lua_pushinteger( L, i_ret );
253     return 1;
254 }
255
256 static int vlclua_fd_read( lua_State *L )
257 {
258     int i_fd = luaL_checkint( L, 1 );
259     size_t i_len = luaL_optint( L, 2, 1 );
260     char psz_buffer[i_len];
261     ssize_t i_ret = read( i_fd, psz_buffer, i_len );
262     if( i_ret > 0 )
263         lua_pushlstring( L, psz_buffer, i_ret );
264     else
265         lua_pushnil( L );
266     return 1;
267 }
268
269 /*****************************************************************************
270  *
271  *****************************************************************************/
272 static int vlclua_stat( lua_State *L )
273 {
274     const char *psz_path = luaL_checkstring( L, 1 );
275     struct stat s;
276     if( vlc_stat( psz_path, &s ) )
277         return 0;
278         //return luaL_error( L, "Couldn't stat %s.", psz_path );
279     lua_newtable( L );
280     if( S_ISREG( s.st_mode ) )
281         lua_pushliteral( L, "file" );
282     else if( S_ISDIR( s.st_mode ) )
283         lua_pushliteral( L, "dir" );
284 #ifdef S_ISCHR
285     else if( S_ISCHR( s.st_mode ) )
286         lua_pushliteral( L, "character device" );
287 #endif
288 #ifdef S_ISBLK
289     else if( S_ISBLK( s.st_mode ) )
290         lua_pushliteral( L, "block device" );
291 #endif
292 #ifdef S_ISFIFO
293     else if( S_ISFIFO( s.st_mode ) )
294         lua_pushliteral( L, "fifo" );
295 #endif
296 #ifdef S_ISLNK
297     else if( S_ISLNK( s.st_mode ) )
298         lua_pushliteral( L, "symbolic link" );
299 #endif
300 #ifdef S_ISSOCK
301     else if( S_ISSOCK( s.st_mode ) )
302         lua_pushliteral( L, "socket" );
303 #endif
304     else
305         lua_pushliteral( L, "unknown" );
306     lua_setfield( L, -2, "type" );
307     lua_pushinteger( L, s.st_mode );
308     lua_setfield( L, -2, "mode" );
309     lua_pushinteger( L, s.st_uid );
310     lua_setfield( L, -2, "uid" );
311     lua_pushinteger( L, s.st_gid );
312     lua_setfield( L, -2, "gid" );
313     lua_pushinteger( L, s.st_size );
314     lua_setfield( L, -2, "size" );
315     lua_pushinteger( L, s.st_atime );
316     lua_setfield( L, -2, "access_time" );
317     lua_pushinteger( L, s.st_mtime );
318     lua_setfield( L, -2, "modification_time" );
319     lua_pushinteger( L, s.st_ctime );
320     lua_setfield( L, -2, "creation_time" );
321     return 1;
322 }
323
324 static int vlclua_opendir( lua_State *L )
325 {
326     const char *psz_dir = luaL_checkstring( L, 1 );
327     DIR *p_dir;
328     int i = 0;
329
330     if( ( p_dir = vlc_opendir( psz_dir ) ) == NULL )
331         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
332
333     lua_newtable( L );
334     for( ;; )
335     {
336         char *psz_filename = vlc_readdir( p_dir );
337         if( !psz_filename ) break;
338         i++;
339         lua_pushstring( L, psz_filename );
340         lua_rawseti( L, -2, i );
341         free( psz_filename );
342     }
343     closedir( p_dir );
344     return 1;
345 }
346
347 /*****************************************************************************
348  *
349  *****************************************************************************/
350 static const luaL_Reg vlclua_net_reg[] = {
351     { "url_parse", vlclua_url_parse },
352     { "listen_tcp", vlclua_net_listen_tcp },
353     { "connect_tcp", vlclua_net_connect_tcp },
354     { "close", vlclua_net_close },
355     { "send", vlclua_net_send },
356     { "recv", vlclua_net_recv },
357     { "poll", vlclua_net_poll },
358     { "read", vlclua_fd_read },
359     { "write", vlclua_fd_write },
360     { "stat", vlclua_stat }, /* Not really "net" */
361     { "opendir", vlclua_opendir }, /* Not really "net" */
362     { NULL, NULL }
363 };
364
365 void luaopen_net( lua_State *L )
366 {
367     lua_newtable( L );
368     luaL_register( L, NULL, vlclua_net_reg );
369 #define ADD_CONSTANT( name, value )    \
370     lua_pushinteger( L, value ); \
371     lua_setfield( L, -2, name );
372     ADD_CONSTANT( "POLLIN", POLLIN )
373     ADD_CONSTANT( "POLLPRI", POLLPRI )
374     ADD_CONSTANT( "POLLOUT", POLLOUT )
375     ADD_CONSTANT( "POLLERR", POLLERR )
376     ADD_CONSTANT( "POLLHUP", POLLHUP )
377     ADD_CONSTANT( "POLLNVAL", POLLNVAL )
378     lua_setfield( L, -2, "net" );
379 }