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