]> git.sesse.net Git - vlc/blob - modules/misc/lua/net.c
Add a new type of VLC Lua module: Interfaces.
[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 #include <vlc/vlc.h>
32 #include <vlc_network.h>
33 #include <vlc_url.h>
34
35 #include <lua.h>        /* Low level lua C API */
36 #include <lauxlib.h>    /* Higher level C API */
37 #include <lualib.h>     /* Lua libs */
38
39 #include "vlc.h"
40
41 /*****************************************************************************
42  *
43  *****************************************************************************/
44 int vlclua_url_parse( lua_State *L )
45 {
46     const char *psz_url = luaL_checkstring( L, 1 );
47     const char *psz_option = luaL_optstring( L, 2, NULL );
48     vlc_url_t url;
49
50     vlc_UrlParse( &url, psz_url, psz_option?*psz_option:0 );
51
52     lua_newtable( L );
53     lua_pushstring( L, url.psz_protocol );
54     lua_setfield( L, -2, "protocol" );
55     lua_pushstring( L, url.psz_username );
56     lua_setfield( L, -2, "username" );
57     lua_pushstring( L, url.psz_password );
58     lua_setfield( L, -2, "password" );
59     lua_pushstring( L, url.psz_host );
60     lua_setfield( L, -2, "host" );
61     lua_pushinteger( L, url.i_port );
62     lua_setfield( L, -2, "port" );
63     lua_pushstring( L, url.psz_path );
64     lua_setfield( L, -2, "path" );
65     lua_pushstring( L, url.psz_option );
66     lua_setfield( L, -2, "option" );
67
68     vlc_UrlClean( &url );
69
70     return 1;
71 }
72
73 int vlclua_net_listen_tcp( lua_State *L )
74 {
75     vlc_object_t *p_this = vlclua_get_this( L );
76     const char *psz_host = luaL_checkstring( L, 1 );
77     int i_port = luaL_checkint( L, 2 );
78     int *pi_fd = net_ListenTCP( p_this, psz_host, i_port );
79     if( pi_fd == NULL )
80         return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port );
81     lua_pushlightuserdata( L, pi_fd );
82     return 1;
83 }
84
85 int vlclua_net_listen_close( lua_State *L )
86 {
87     int *pi_fd = (int*)luaL_checklightuserdata( L, 1 );
88     net_ListenClose( pi_fd );
89     return 0;
90 }
91
92 int vlclua_net_accept( lua_State *L )
93 {
94     vlc_object_t *p_this = vlclua_get_this( L );
95     int *pi_fd = (int*)luaL_checklightuserdata( L, 1 );
96     mtime_t i_wait = luaL_optint( L, 2, -1 ); /* default to block */
97     int i_fd = net_Accept( p_this, pi_fd, i_wait );
98     lua_pushinteger( L, i_fd );
99     return 1;
100 }
101
102 int vlclua_net_close( lua_State *L )
103 {
104     int i_fd = luaL_checkint( L, 1 );
105     net_Close( i_fd );
106     return 0;
107 }
108
109 int vlclua_net_send( lua_State *L )
110 {
111     int i_fd = luaL_checkint( L, 1 );
112     size_t i_len;
113     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
114     i_len = luaL_optint( L, 3, i_len );
115     i_len = send( i_fd, psz_buffer, i_len, 0 );
116     lua_pushinteger( L, i_len );
117     return 1;
118 }
119
120 int vlclua_net_recv( lua_State *L )
121 {
122     int i_fd = luaL_checkint( L, 1 );
123     size_t i_len = luaL_optint( L, 2, 1 );
124     char psz_buffer[i_len];
125     i_len = recv( i_fd, psz_buffer, i_len, 0 );
126     lua_pushlstring( L, psz_buffer, i_len );
127     return 1;
128 }
129
130 int vlclua_net_select( lua_State *L )
131 {
132     int i_ret;
133     int i_nfds = luaL_checkint( L, 1 );
134     fd_set *fds_read = (fd_set*)luaL_checkuserdata( L, 2, sizeof( fd_set ) );
135     fd_set *fds_write = (fd_set*)luaL_checkuserdata( L, 3, sizeof( fd_set ) );
136     double f_timeout = luaL_checknumber( L, 4 );
137     struct timeval timeout;
138     timeout.tv_sec = (int)f_timeout;
139     timeout.tv_usec = (int)(1e6*(f_timeout-(double)((int)f_timeout)));
140     i_ret = select( i_nfds, fds_read, fds_write, 0, &timeout );
141     lua_pushinteger( L, i_ret );
142     lua_pushinteger( L, (double)timeout.tv_sec+((double)timeout.tv_usec)/1e-6 );
143     return 2;
144 }
145
146 int vlclua_fd_set_new( lua_State *L )
147 {
148     fd_set *fds = (fd_set*)lua_newuserdata( L, sizeof( fd_set ) );
149     FD_ZERO( fds );
150     return 1;
151 }
152
153 int vlclua_fd_clr( lua_State *L )
154 {
155     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
156     int i_fd = luaL_checkint( L, 2 );
157     FD_CLR( i_fd, fds );
158     return 0;
159 }
160 int vlclua_fd_isset( lua_State *L )
161 {
162     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
163     int i_fd = luaL_checkint( L, 2 );
164     lua_pushboolean( L, FD_ISSET( i_fd, fds ) );
165     return 1;
166 }
167 int vlclua_fd_set( lua_State *L )
168 {
169     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
170     int i_fd = luaL_checkint( L, 2 );
171     FD_SET( i_fd, fds );
172     return 0;
173 }
174 int vlclua_fd_zero( lua_State *L )
175 {
176     fd_set *fds = (fd_set*)luaL_checkuserdata( L, 1, sizeof( fd_set ) );
177     FD_ZERO( fds );
178     return 0;
179 }
180
181 /*
182 int vlclua_fd_open( lua_State *L )
183 {
184 }
185 */
186
187 int vlclua_fd_write( lua_State *L )
188 {
189     int i_fd = luaL_checkint( L, 1 );
190     size_t i_len;
191     ssize_t i_ret;
192     const char *psz_buffer = luaL_checklstring( L, 2, &i_len );
193     i_len = luaL_optint( L, 3, i_len );
194     i_ret = write( i_fd, psz_buffer, i_len );
195     lua_pushinteger( L, i_ret );
196     return 1;
197 }
198
199 int vlclua_fd_read( lua_State *L )
200 {
201     int i_fd = luaL_checkint( L, 1 );
202     size_t i_len = luaL_optint( L, 2, 1 );
203     char psz_buffer[i_len];
204     i_len = read( i_fd, psz_buffer, i_len );
205     lua_pushlstring( L, psz_buffer, i_len );
206     return 1;
207 }