]> git.sesse.net Git - vlc/blob - modules/misc/lua/net.c
Fix test program.
[vlc] / modules / misc / lua / net.c
1 /*****************************************************************************
2  * net.c: Network related functions
3  *****************************************************************************
4  * Copyright (C) 2007 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 #include <lualib.h>     /* Lua libs */
42
43 #include "vlc.h"
44
45 /*****************************************************************************
46  *
47  *****************************************************************************/
48 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 int vlclua_net_listen_tcp( lua_State *L )
78 {
79     vlc_object_t *p_this = vlclua_get_this( L );
80     const char *psz_host = luaL_checkstring( L, 1 );
81     int i_port = luaL_checkint( L, 2 );
82     int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
83     if( pi_fd == NULL )
84         return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
85     lua_pushlightuserdata( L, pi_fd );
86     return 1;
87 }
88
89 int vlclua_net_listen_close( lua_State *L )
90 {
91     int *pi_fd = (int*)luaL_checklightuserdata( L, 1 );
92     net_ListenClose( pi_fd );
93     return 0;
94 }
95
96 int vlclua_net_accept( lua_State *L )
97 {
98     vlc_object_t *p_this = vlclua_get_this( L );
99     int *pi_fd = (int*)luaL_checklightuserdata( L, 1 );
100     mtime_t i_wait = luaL_optint( L, 2, -1 ); /* default to block */
101     int i_fd = net_Accept( p_this, pi_fd, i_wait );
102     lua_pushinteger( L, i_fd );
103     return 1;
104 }
105
106 int vlclua_net_close( lua_State *L )
107 {
108     int i_fd = luaL_checkint( L, 1 );
109     net_Close( i_fd );
110     return 0;
111 }
112
113 int vlclua_net_send( lua_State *L )
114 {
115     int i_fd = luaL_checkint( L, 1 );
116     size_t i_len;
117     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
118     i_len = luaL_optint( L, 3, i_len );
119     i_len = send( i_fd, psz_buffer, i_len, 0 );
120     lua_pushinteger( L, i_len );
121     return 1;
122 }
123
124 int vlclua_net_recv( lua_State *L )
125 {
126     int i_fd = luaL_checkint( L, 1 );
127     size_t i_len = luaL_optint( L, 2, 1 );
128     char psz_buffer[i_len];
129     i_len = recv( i_fd, psz_buffer, i_len, 0 );
130     lua_pushlstring( L, psz_buffer, i_len );
131     return 1;
132 }
133
134 int vlclua_net_select( lua_State *L )
135 {
136     int i_ret;
137     size_t i_nfds = luaL_checkint( L, 1 );
138     fd_set *fds_read = (fd_set*)luaL_checkuserdata( L, 2, sizeof( fd_set ) );
139     fd_set *fds_write = (fd_set*)luaL_checkuserdata( L, 3, sizeof( fd_set ) );
140     double f_timeout = luaL_checknumber( L, 4 );
141     struct timeval timeout;
142
143 #ifndef WIN32
144     if( i_nfds > FD_SETSIZE )
145         i_nfds = FD_SETSIZE;
146 #endif
147     timeout.tv_sec = (int)f_timeout;
148     timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
149     i_ret = select( i_nfds, fds_read, fds_write, 0, &timeout );
150     lua_pushinteger( L, i_ret );
151     lua_pushinteger( L, (double)timeout.tv_sec+((double)timeout.tv_usec)/1e-6 );
152     return 2;
153 }
154
155 int vlclua_fd_set_new( lua_State *L )
156 {
157     fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
158     FD_ZERO( fds );
159     return 1;
160 }
161
162 int vlclua_fd_clr( lua_State *L )
163 {
164     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
165     int i_fd = luaL_checkint( L, 2 );
166     FD_CLR( i_fd, fds );
167     return 0;
168 }
169 int vlclua_fd_isset( lua_State *L )
170 {
171     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
172     int i_fd = luaL_checkint( L, 2 );
173     lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
174     return 1;
175 }
176 int vlclua_fd_set( lua_State *L )
177 {
178     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
179     size_t i_fd = luaL_checkint( L, 2 );
180     /* FIXME: we should really use poll() instead here, but that breaks the
181      * VLC/LUA API. On Windows, overflow protection is built-in FD_SET, not
182      * on POSIX. In both cases, run-time behavior will however be wrong. */
183 #ifndef WIN32
184     if( i_fd < FD_SETSIZE )
185 #endif
186         FD_SET( i_fd, fds );
187     return 0;
188 }
189 int vlclua_fd_zero( lua_State *L )
190 {
191     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
192     FD_ZERO( fds );
193     return 0;
194 }
195
196 /*
197 int vlclua_fd_open( lua_State *L )
198 {
199 }
200 */
201
202 int vlclua_fd_write( lua_State *L )
203 {
204     int i_fd = luaL_checkint( L, 1 );
205     size_t i_len;
206     ssize_t i_ret;
207     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
208     i_len = luaL_optint( L, 3, i_len );
209     i_ret = write( i_fd, psz_buffer, i_len );
210     lua_pushinteger( L, i_ret );
211     return 1;
212 }
213
214 int vlclua_fd_read( lua_State *L )
215 {
216     int i_fd = luaL_checkint( L, 1 );
217     size_t i_len = luaL_optint( L, 2, 1 );
218     char psz_buffer[i_len];
219     i_len = read( i_fd, psz_buffer, i_len );
220     lua_pushlstring( L, psz_buffer, i_len );
221     return 1;
222 }
223
224 int vlclua_stat( lua_State *L )
225 {
226 #ifdef HAVE_SYS_STAT_H
227     const char *psz_path = luaL_checkstring( L, 1 );
228     struct stat s;
229     if( utf8_stat( psz_path, &s ) )
230         return 0;
231         //return luaL_error( L, "Couldn't stat %s.", psz_path );
232     lua_newtable( L );
233     if( S_ISREG( s.st_mode ) )
234         lua_pushstring( L, "file" );
235     else if( S_ISDIR( s.st_mode ) )
236         lua_pushstring( L, "dir" );
237 #ifdef S_ISCHR
238     else if( S_ISCHR( s.st_mode ) )
239         lua_pushstring( L, "character device" );
240 #endif
241 #ifdef S_ISBLK
242     else if( S_ISBLK( s.st_mode ) )
243         lua_pushstring( L, "block device" );
244 #endif
245 #ifdef S_ISFIFO
246     else if( S_ISFIFO( s.st_mode ) )
247         lua_pushstring( L, "fifo" );
248 #endif
249 #ifdef S_ISLNK
250     else if( S_ISLNK( s.st_mode ) )
251         lua_pushstring( L, "symbolic link" );
252 #endif
253 #ifdef S_ISSOCK
254     else if( S_ISSOCK( s.st_mode ) )
255         lua_pushstring( L, "socket" );
256 #endif
257     else
258         lua_pushstring( L, "unknown" );
259     lua_setfield( L, -2, "type" );
260     lua_pushinteger( L, s.st_mode );
261     lua_setfield( L, -2, "mode" );
262     lua_pushinteger( L, s.st_uid );
263     lua_setfield( L, -2, "uid" );
264     lua_pushinteger( L, s.st_gid );
265     lua_setfield( L, -2, "gid" );
266     lua_pushinteger( L, s.st_size );
267     lua_setfield( L, -2, "size" );
268     lua_pushinteger( L, s.st_atime );
269     lua_setfield( L, -2, "access_time" );
270     lua_pushinteger( L, s.st_mtime );
271     lua_setfield( L, -2, "modification_time" );
272     lua_pushinteger( L, s.st_ctime );
273     lua_setfield( L, -2, "creation_time" );
274     return 1;
275 #else
276 #   warning "Woops, looks like we don't have stat on your platform"
277     return luaL_error( L, "System is missing <sys/stat.h>" );
278 #endif
279 }
280
281 int vlclua_opendir( lua_State *L )
282 {
283     const char *psz_dir = luaL_checkstring( L, 1 );
284     DIR *p_dir;
285     int i = 0;
286
287     if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL )
288         return luaL_error( L, "cannot open directory `%s'.", psz_dir );
289
290     lua_newtable( L );
291     for( ;; )
292     {
293         char *psz_filename = utf8_readdir( p_dir );
294         if( !psz_filename ) break;
295         i++;
296         lua_pushstring( L, psz_filename );
297         lua_rawseti( L, -2, i );
298         free( psz_filename );
299     }
300     closedir( p_dir );
301     return 1;
302 }