]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/net.c
Remove vlc.net.select() and fd_set.
[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
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_close( lua_State *L )
152 {
153     int i_fd = luaL_checkint( L, 1 );
154     net_Close( i_fd );
155     return 0;
156 }
157
158 static int vlclua_net_send( lua_State *L )
159 {
160     int i_fd = luaL_checkint( L, 1 );
161     size_t i_len;
162     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
163     i_len = luaL_optint( L, 3, i_len );
164     i_len = send( i_fd, psz_buffer, i_len, 0 );
165     lua_pushinteger( L, i_len );
166     return 1;
167 }
168
169 static int vlclua_net_recv( lua_State *L )
170 {
171     int i_fd = luaL_checkint( L, 1 );
172     size_t i_len = luaL_optint( L, 2, 1 );
173     char psz_buffer[i_len];
174     i_len = recv( i_fd, psz_buffer, i_len, 0 );
175     lua_pushlstring( L, psz_buffer, i_len );
176     return 1;
177 }
178
179 /*****************************************************************************
180  *
181  *****************************************************************************/
182 /* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
183 static int vlclua_net_poll( lua_State *L )
184 {
185     luaL_checktype( L, 1, LUA_TTABLE );
186     double f_timeout = luaL_optnumber( L, 2, -1. );
187
188     int i_fds = 0;
189     lua_pushnil( L );
190     while( lua_next( L, 1 ) )
191     {
192         i_fds++;
193         lua_pop( L, 1 );
194     }
195     struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
196     lua_pushnil( L );
197     int i = 0;
198     while( lua_next( L, 1 ) )
199     {
200         p_fds[i].fd = luaL_checkinteger( L, -2 );
201         p_fds[i].events = luaL_checkinteger( L, -1 );
202         p_fds[i].revents = 0;
203         lua_pop( L, 1 );
204         i++;
205     }
206
207     int i_ret = poll( p_fds, i_fds, f_timeout < 0. ? -1 : (int)(f_timeout*1000) );
208     for( i = 0; i < i_fds; i++ )
209     {
210         lua_pushinteger( L, p_fds[i].fd );
211         lua_pushinteger( L, p_fds[i].revents );
212         lua_settable( L, 1 );
213     }
214     free( p_fds );
215     lua_pushinteger( L, i_ret );
216     return 1;
217 }
218
219 /*****************************************************************************
220  *
221  *****************************************************************************/
222 /*
223 static int vlclua_fd_open( lua_State *L )
224 {
225 }
226 */
227
228 static int vlclua_fd_write( lua_State *L )
229 {
230     int i_fd = luaL_checkint( L, 1 );
231     size_t i_len;
232     ssize_t i_ret;
233     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
234     i_len = luaL_optint( L, 3, i_len );
235     i_ret = write( i_fd, psz_buffer, i_len );
236     lua_pushinteger( L, i_ret );
237     return 1;
238 }
239
240 static int vlclua_fd_read( lua_State *L )
241 {
242     int i_fd = luaL_checkint( L, 1 );
243     size_t i_len = luaL_optint( L, 2, 1 );
244     char psz_buffer[i_len];
245     i_len = read( i_fd, psz_buffer, i_len );
246     lua_pushlstring( L, psz_buffer, i_len );
247     return 1;
248 }
249
250 /*****************************************************************************
251  *
252  *****************************************************************************/
253 static int vlclua_stat( lua_State *L )
254 {
255 #ifdef HAVE_SYS_STAT_H
256     const char *psz_path = luaL_checkstring( L, 1 );
257     struct stat s;
258     if( utf8_stat( psz_path, &s ) )
259         return 0;
260         //return luaL_error( L, "Couldn't stat %s.", psz_path );
261     lua_newtable( L );
262     if( S_ISREG( s.st_mode ) )
263         lua_pushstring( L, "file" );
264     else if( S_ISDIR( s.st_mode ) )
265         lua_pushstring( L, "dir" );
266 #ifdef S_ISCHR
267     else if( S_ISCHR( s.st_mode ) )
268         lua_pushstring( L, "character device" );
269 #endif
270 #ifdef S_ISBLK
271     else if( S_ISBLK( s.st_mode ) )
272         lua_pushstring( L, "block device" );
273 #endif
274 #ifdef S_ISFIFO
275     else if( S_ISFIFO( s.st_mode ) )
276         lua_pushstring( L, "fifo" );
277 #endif
278 #ifdef S_ISLNK
279     else if( S_ISLNK( s.st_mode ) )
280         lua_pushstring( L, "symbolic link" );
281 #endif
282 #ifdef S_ISSOCK
283     else if( S_ISSOCK( s.st_mode ) )
284         lua_pushstring( L, "socket" );
285 #endif
286     else
287         lua_pushstring( L, "unknown" );
288     lua_setfield( L, -2, "type" );
289     lua_pushinteger( L, s.st_mode );
290     lua_setfield( L, -2, "mode" );
291     lua_pushinteger( L, s.st_uid );
292     lua_setfield( L, -2, "uid" );
293     lua_pushinteger( L, s.st_gid );
294     lua_setfield( L, -2, "gid" );
295     lua_pushinteger( L, s.st_size );
296     lua_setfield( L, -2, "size" );
297     lua_pushinteger( L, s.st_atime );
298     lua_setfield( L, -2, "access_time" );
299     lua_pushinteger( L, s.st_mtime );
300     lua_setfield( L, -2, "modification_time" );
301     lua_pushinteger( L, s.st_ctime );
302     lua_setfield( L, -2, "creation_time" );
303     return 1;
304 #else
305 #   warning "Woops, looks like we don't have stat on your platform"
306     return luaL_error( L, "System is missing <sys/stat.h>" );
307 #endif
308 }
309
310 static int vlclua_opendir( lua_State *L )
311 {
312     const char *psz_dir = luaL_checkstring( L, 1 );
313     DIR *p_dir;
314     int i = 0;
315
316     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
317         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
318
319     lua_newtable( L );
320     for( ;; )
321     {
322         char *psz_filename = utf8_readdir( p_dir );
323         if( !psz_filename ) break;
324         i++;
325         lua_pushstring( L, psz_filename );
326         lua_rawseti( L, -2, i );
327         free( psz_filename );
328     }
329     closedir( p_dir );
330     return 1;
331 }
332
333 /*****************************************************************************
334  *
335  *****************************************************************************/
336 static const luaL_Reg vlclua_net_reg[] = {
337     { "url_parse", vlclua_url_parse },
338     { "listen_tcp", vlclua_net_listen_tcp },
339     { "close", vlclua_net_close },
340     { "send", vlclua_net_send },
341     { "recv", vlclua_net_recv },
342     { "poll", vlclua_net_poll },
343     { "read", vlclua_fd_read },
344     { "write", vlclua_fd_write },
345     { "stat", vlclua_stat }, /* Not really "net" */
346     { "opendir", vlclua_opendir }, /* Not really "net" */
347     { NULL, NULL }
348 };
349
350 void luaopen_net( lua_State *L )
351 {
352     lua_newtable( L );
353     luaL_register( L, NULL, vlclua_net_reg );
354 #define ADD_CONSTANT( name, value )    \
355     lua_pushinteger( L, value ); \
356     lua_setfield( L, -2, name );
357     ADD_CONSTANT( "POLLIN", POLLIN )
358     ADD_CONSTANT( "POLLPRI", POLLPRI )
359     ADD_CONSTANT( "POLLOUT", POLLOUT )
360     ADD_CONSTANT( "POLLERR", POLLERR )
361     ADD_CONSTANT( "POLLHUP", POLLHUP )
362     ADD_CONSTANT( "POLLNVAL", POLLNVAL )
363     lua_setfield( L, -2, "net" );
364 }