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