]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/net.c
net_Accept: remove timeout parameter
[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 #include "../vlc.h"
43 #include "../libs.h"
44
45 /*****************************************************************************
46  *
47  *****************************************************************************/
48 static int vlclua_url_parse( lua_State *L )
49 {
50     const char *psz_url = luaL_checkstring( L, 1 );
51     const char *psz_option = luaL_optstring( L, 2, NULL );
52     vlc_url_t url;
53
54     vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
55
56     lua_newtable( L );
57     lua_pushstring( L, url.psz_protocol );
58     lua_setfield( L, -2, "protocol" );
59     lua_pushstring( L, url.psz_username );
60     lua_setfield( L, -2, "username" );
61     lua_pushstring( L, url.psz_password );
62     lua_setfield( L, -2, "password" );
63     lua_pushstring( L, url.psz_host );
64     lua_setfield( L, -2, "host" );
65     lua_pushinteger( L, url.i_port );
66     lua_setfield( L, -2, "port" );
67     lua_pushstring( L, url.psz_path );
68     lua_setfield( L, -2, "path" );
69     lua_pushstring( L, url.psz_option );
70     lua_setfield( L, -2, "option" );
71
72     vlc_UrlClean( &url );
73
74     return 1;
75 }
76
77 /*****************************************************************************
78  * Net listen
79  *****************************************************************************/
80 static int vlclua_net_listen_close( lua_State * );
81 static int vlclua_net_accept( lua_State * );
82
83 static const luaL_Reg vlclua_net_listen_reg[] = {
84     { "accept", vlclua_net_accept },
85     { NULL, NULL }
86 };
87
88 static int vlclua_net_listen_tcp( lua_State *L )
89 {
90     vlc_object_t *p_this = vlclua_get_this( L );
91     const char *psz_host = luaL_checkstring( L, 1 );
92     int i_port = luaL_checkint( L, 2 );
93     int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
94     if( pi_fd == NULL )
95         return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
96
97     int **ppi_fd = lua_newuserdata( L, sizeof( int * ) );
98     *ppi_fd = pi_fd;
99
100     if( luaL_newmetatable( L, "net_listen" ) )
101     {
102         lua_newtable( L );
103         luaL_register( L, NULL, vlclua_net_listen_reg );
104         lua_setfield( L, -2, "__index" );
105         lua_pushcfunction( L, vlclua_net_listen_close );
106         lua_setfield( L, -2, "__gc" );
107     }
108
109     lua_setmetatable( L, -2 );
110     return 1;
111 }
112
113 static int vlclua_net_listen_close( lua_State *L )
114 {
115     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
116     net_ListenClose( *ppi_fd );
117     return 0;
118 }
119
120 static int vlclua_net_accept( lua_State *L )
121 {
122     vlc_object_t *p_this = vlclua_get_this( L );
123     int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" );
124     int i_fd = net_Accept( p_this, *ppi_fd );
125     lua_pushinteger( L, i_fd );
126     return 1;
127 }
128
129 /*****************************************************************************
130  *
131  *****************************************************************************/
132 static int vlclua_net_close( lua_State *L )
133 {
134     int i_fd = luaL_checkint( L, 1 );
135     net_Close( i_fd );
136     return 0;
137 }
138
139 static int vlclua_net_send( lua_State *L )
140 {
141     int i_fd = luaL_checkint( L, 1 );
142     size_t i_len;
143     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
144     i_len = luaL_optint( L, 3, i_len );
145     i_len = send( i_fd, psz_buffer, i_len, 0 );
146     lua_pushinteger( L, i_len );
147     return 1;
148 }
149
150 static int vlclua_net_recv( lua_State *L )
151 {
152     int i_fd = luaL_checkint( L, 1 );
153     size_t i_len = luaL_optint( L, 2, 1 );
154     char psz_buffer[i_len];
155     i_len = recv( i_fd, psz_buffer, i_len, 0 );
156     lua_pushlstring( L, psz_buffer, i_len );
157     return 1;
158 }
159
160 /*****************************************************************************
161  *
162  *****************************************************************************/
163 static int vlclua_net_select( lua_State *L )
164 {
165     int i_ret;
166     size_t i_nfds = luaL_checkint( L, 1 );
167     fd_set *fds_read = (fd_set*)luaL_checkudata( L, 2, "fd_set" );
168     fd_set *fds_write = (fd_set*)luaL_checkudata( L, 3, "fd_set" );
169     double f_timeout = luaL_checknumber( L, 4 );
170     struct timeval timeout;
171
172 #ifndef WIN32
173     if( i_nfds > FD_SETSIZE )
174         i_nfds = FD_SETSIZE;
175 #endif
176     timeout.tv_sec = (int)f_timeout;
177     timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
178     i_ret = select( i_nfds, fds_read, fds_write, 0, &timeout );
179     lua_pushinteger( L, i_ret );
180     lua_pushinteger( L, (double)timeout.tv_sec+((double)timeout.tv_usec)/1e-6 );
181     return 2;
182 }
183
184 /*****************************************************************************
185  *
186  *****************************************************************************/
187 static int vlclua_fd_clr( lua_State * );
188 static int vlclua_fd_isset( lua_State * );
189 static int vlclua_fd_set( lua_State * );
190 static int vlclua_fd_zero( lua_State * );
191
192 static const luaL_Reg vlclua_fd_set_reg[] = {
193     { "clr", vlclua_fd_clr },
194     { "isset", vlclua_fd_isset },
195     { "set", vlclua_fd_set },
196     { "zero", vlclua_fd_zero },
197     { NULL, NULL }
198 };
199
200 static int vlclua_fd_set_new( lua_State *L )
201 {
202     fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
203     FD_ZERO( fds );
204
205     if( luaL_newmetatable( L, "fd_set" ) )
206     {
207         lua_newtable( L );
208         luaL_register( L, NULL, vlclua_fd_set_reg );
209         lua_setfield( L, -2, "__index" );
210     }
211
212     lua_setmetatable( L, -2 );
213     return 1;
214 }
215
216 static int vlclua_fd_clr( lua_State *L )
217 {
218     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
219     int i_fd = luaL_checkint( L, 2 );
220     FD_CLR( i_fd, fds );
221     return 0;
222 }
223
224 static int vlclua_fd_isset( lua_State *L )
225 {
226     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
227     int i_fd = luaL_checkint( L, 2 );
228     lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
229     return 1;
230 }
231
232 static int vlclua_fd_set( lua_State *L )
233 {
234     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
235     size_t i_fd = luaL_checkint( L, 2 );
236     /* FIXME: we should really use poll() instead here, but that breaks the
237      * VLC/LUA API. On Windows, overflow protection is built-in FD_SET, not
238      * on POSIX. In both cases, run-time behavior will however be wrong. */
239 #ifndef WIN32
240     if( i_fd < FD_SETSIZE )
241 #endif
242         FD_SET( i_fd, fds );
243     return 0;
244 }
245
246 static int vlclua_fd_zero( lua_State *L )
247 {
248     fd_set *fds = (fd_set*)luaL_checkudata( L, 1, "fd_set" );
249     FD_ZERO( fds );
250     return 0;
251 }
252
253 /*****************************************************************************
254  *
255  *****************************************************************************/
256 /*
257 static int vlclua_fd_open( lua_State *L )
258 {
259 }
260 */
261
262 static int vlclua_fd_write( lua_State *L )
263 {
264     int i_fd = luaL_checkint( L, 1 );
265     size_t i_len;
266     ssize_t i_ret;
267     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
268     i_len = luaL_optint( L, 3, i_len );
269     i_ret = write( i_fd, psz_buffer, i_len );
270     lua_pushinteger( L, i_ret );
271     return 1;
272 }
273
274 static int vlclua_fd_read( lua_State *L )
275 {
276     int i_fd = luaL_checkint( L, 1 );
277     size_t i_len = luaL_optint( L, 2, 1 );
278     char psz_buffer[i_len];
279     i_len = read( i_fd, psz_buffer, i_len );
280     lua_pushlstring( L, psz_buffer, i_len );
281     return 1;
282 }
283
284 /*****************************************************************************
285  *
286  *****************************************************************************/
287 static int vlclua_stat( lua_State *L )
288 {
289 #ifdef HAVE_SYS_STAT_H
290     const char *psz_path = luaL_checkstring( L, 1 );
291     struct stat s;
292     if( utf8_stat( psz_path, &s ) )
293         return 0;
294         //return luaL_error( L, "Couldn't stat %s.", psz_path );
295     lua_newtable( L );
296     if( S_ISREG( s.st_mode ) )
297         lua_pushstring( L, "file" );
298     else if( S_ISDIR( s.st_mode ) )
299         lua_pushstring( L, "dir" );
300 #ifdef S_ISCHR
301     else if( S_ISCHR( s.st_mode ) )
302         lua_pushstring( L, "character device" );
303 #endif
304 #ifdef S_ISBLK
305     else if( S_ISBLK( s.st_mode ) )
306         lua_pushstring( L, "block device" );
307 #endif
308 #ifdef S_ISFIFO
309     else if( S_ISFIFO( s.st_mode ) )
310         lua_pushstring( L, "fifo" );
311 #endif
312 #ifdef S_ISLNK
313     else if( S_ISLNK( s.st_mode ) )
314         lua_pushstring( L, "symbolic link" );
315 #endif
316 #ifdef S_ISSOCK
317     else if( S_ISSOCK( s.st_mode ) )
318         lua_pushstring( L, "socket" );
319 #endif
320     else
321         lua_pushstring( L, "unknown" );
322     lua_setfield( L, -2, "type" );
323     lua_pushinteger( L, s.st_mode );
324     lua_setfield( L, -2, "mode" );
325     lua_pushinteger( L, s.st_uid );
326     lua_setfield( L, -2, "uid" );
327     lua_pushinteger( L, s.st_gid );
328     lua_setfield( L, -2, "gid" );
329     lua_pushinteger( L, s.st_size );
330     lua_setfield( L, -2, "size" );
331     lua_pushinteger( L, s.st_atime );
332     lua_setfield( L, -2, "access_time" );
333     lua_pushinteger( L, s.st_mtime );
334     lua_setfield( L, -2, "modification_time" );
335     lua_pushinteger( L, s.st_ctime );
336     lua_setfield( L, -2, "creation_time" );
337     return 1;
338 #else
339 #   warning "Woops, looks like we don't have stat on your platform"
340     return luaL_error( L, "System is missing <sys/stat.h>" );
341 #endif
342 }
343
344 static int vlclua_opendir( lua_State *L )
345 {
346     const char *psz_dir = luaL_checkstring( L, 1 );
347     DIR *p_dir;
348     int i = 0;
349
350     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
351         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
352
353     lua_newtable( L );
354     for( ;; )
355     {
356         char *psz_filename = utf8_readdir( p_dir );
357         if( !psz_filename ) break;
358         i++;
359         lua_pushstring( L, psz_filename );
360         lua_rawseti( L, -2, i );
361         free( psz_filename );
362     }
363     closedir( p_dir );
364     return 1;
365 }
366
367 /*****************************************************************************
368  *
369  *****************************************************************************/
370 static const luaL_Reg vlclua_net_reg[] = {
371     { "url_parse", vlclua_url_parse },
372     { "listen_tcp", vlclua_net_listen_tcp },
373     { "close", vlclua_net_close },
374     { "send", vlclua_net_send },
375     { "recv", vlclua_net_recv },
376     { "select", vlclua_net_select },
377     { "fd_set_new", vlclua_fd_set_new },
378     { "read", vlclua_fd_read },
379     { "write", vlclua_fd_write },
380     { "stat", vlclua_stat }, /* Not really "net" */
381     { "opendir", vlclua_opendir }, /* Not really "net" */
382     { NULL, NULL }
383 };
384
385 void luaopen_net( lua_State *L )
386 {
387     lua_newtable( L );
388     luaL_register( L, NULL, vlclua_net_reg );
389     lua_setfield( L, -2, "net" );
390 }