1 /*****************************************************************************
2 * acl.c: Access list related functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
38 #include <lua.h> /* Low level lua C API */
39 #include <lauxlib.h> /* Higher level C API */
44 /*****************************************************************************
46 *****************************************************************************/
47 static int vlclua_acl_create_inner( lua_State
*, vlc_acl_t
* );
48 static int vlclua_acl_delete( lua_State
* );
49 static int vlclua_acl_check( lua_State
* );
50 static int vlclua_acl_duplicate( lua_State
* );
51 static int vlclua_acl_add_host( lua_State
* );
52 static int vlclua_acl_add_net( lua_State
* );
53 static int vlclua_acl_load_file( lua_State
* );
55 static const luaL_Reg vlclua_acl_reg
[] = {
56 { "check", vlclua_acl_check
},
57 { "duplicate", vlclua_acl_duplicate
},
58 { "add_host", vlclua_acl_add_host
},
59 { "add_net", vlclua_acl_add_net
},
60 { "load_file", vlclua_acl_load_file
},
64 static int vlclua_acl_create( lua_State
*L
)
66 vlc_object_t
*p_this
= vlclua_get_this( L
);
67 bool b_allow
= luaL_checkboolean( L
, 1 ) ? true : false;
68 vlc_acl_t
*p_acl
= ACL_Create( p_this
, b_allow
);
69 return vlclua_acl_create_inner( L
, p_acl
);
72 static int vlclua_acl_create_inner( lua_State
*L
, vlc_acl_t
*p_acl
)
75 return luaL_error( L
, "ACL creation failed." );
77 vlc_acl_t
**pp_acl
= lua_newuserdata( L
, sizeof( vlc_acl_t
* ) );
80 if( luaL_newmetatable( L
, "acl" ) )
83 luaL_register( L
, NULL
, vlclua_acl_reg
);
84 lua_setfield( L
, -2, "__index" );
85 lua_pushcfunction( L
, vlclua_acl_delete
);
86 lua_setfield( L
, -2, "__gc" );
89 lua_setmetatable( L
, -2 );
93 static int vlclua_acl_delete( lua_State
*L
)
95 vlc_acl_t
**pp_acl
= (vlc_acl_t
**)luaL_checkudata( L
, 1, "acl" );
96 ACL_Destroy( *pp_acl
);
100 static int vlclua_acl_check( lua_State
*L
)
102 vlc_acl_t
**pp_acl
= (vlc_acl_t
**)luaL_checkudata( L
, 1, "acl" );
103 const char *psz_ip
= luaL_checkstring( L
, 2 );
104 lua_pushinteger( L
, ACL_Check( *pp_acl
, psz_ip
) );
108 static int vlclua_acl_duplicate( lua_State
*L
)
110 vlc_object_t
*p_this
= vlclua_get_this( L
);
111 vlc_acl_t
**pp_acl
= (vlc_acl_t
**)luaL_checkudata( L
, 1, "acl" );
112 vlc_acl_t
*p_acl_new
= ACL_Duplicate( p_this
, *pp_acl
);
113 return vlclua_acl_create_inner( L
, p_acl_new
);
116 static int vlclua_acl_add_host( lua_State
*L
)
118 vlc_acl_t
**pp_acl
= (vlc_acl_t
**)luaL_checkudata( L
, 1, "acl" );
119 const char *psz_ip
= luaL_checkstring( L
, 2 );
120 bool b_allow
= luaL_checkboolean( L
, 3 ) ? true : false;
121 lua_pushinteger( L
, ACL_AddHost( *pp_acl
, psz_ip
, b_allow
) );
125 static int vlclua_acl_add_net( lua_State
*L
)
127 vlc_acl_t
**pp_acl
= (vlc_acl_t
**)luaL_checkudata( L
, 1, "acl" );
128 const char *psz_ip
= luaL_checkstring( L
, 2 );
129 int i_len
= luaL_checkint( L
, 3 );
130 bool b_allow
= luaL_checkboolean( L
, 4 ) ? true : false;
131 lua_pushinteger( L
, ACL_AddNet( *pp_acl
, psz_ip
, i_len
, b_allow
) );
135 static int vlclua_acl_load_file( lua_State
*L
)
137 vlc_acl_t
**pp_acl
= (vlc_acl_t
**)luaL_checkudata( L
, 1, "acl" );
138 const char *psz_path
= luaL_checkstring( L
, 2 );
139 lua_pushinteger( L
, ACL_LoadFile( *pp_acl
, psz_path
) );
143 void luaopen_acl( lua_State
*L
)
145 lua_pushcfunction( L
, vlclua_acl_create
);
146 lua_setfield( L
, -2, "acl" );