File: acl.c

package info (click to toggle)
vlc 2.0.3-5%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 155,972 kB
  • sloc: ansic: 350,796; cpp: 80,439; objc: 28,824; sh: 13,349; makefile: 2,096; xml: 1,536; asm: 815; python: 240; perl: 110; sed: 16
file content (147 lines) | stat: -rw-r--r-- 4,886 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*****************************************************************************
 * acl.c: Access list related functions
 *****************************************************************************
 * Copyright (C) 2007-2010 the VideoLAN team
 * $Id: 4deb66e6dd5b89c2e7235613e6a4e2ad2408ab90 $
 *
 * Authors: Antoine Cellerier <dionoea at videolan tod org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifndef  _GNU_SOURCE
#   define  _GNU_SOURCE
#endif

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_acl.h>

#include <lua.h>        /* Low level lua C API */
#include <lauxlib.h>    /* Higher level C API */

#include "../vlc.h"
#include "../libs.h"

/*****************************************************************************
 *
 *****************************************************************************/
static int vlclua_acl_create_inner( lua_State *, vlc_acl_t * );
static int vlclua_acl_delete( lua_State * );
static int vlclua_acl_check( lua_State * );
static int vlclua_acl_duplicate( lua_State * );
static int vlclua_acl_add_host( lua_State * );
static int vlclua_acl_add_net( lua_State * );
static int vlclua_acl_load_file( lua_State * );

static const luaL_Reg vlclua_acl_reg[] = {
    { "check", vlclua_acl_check },
    { "duplicate", vlclua_acl_duplicate },
    { "add_host", vlclua_acl_add_host },
    { "add_net", vlclua_acl_add_net },
    { "load_file", vlclua_acl_load_file },
    { NULL, NULL }
};

static int vlclua_acl_create( lua_State *L )
{
    vlc_object_t *p_this = vlclua_get_this( L );
    bool b_allow = luaL_checkboolean( L, 1 );
    vlc_acl_t *p_acl = ACL_Create( p_this, b_allow );
    return vlclua_acl_create_inner( L, p_acl );
}

static int vlclua_acl_create_inner( lua_State *L, vlc_acl_t *p_acl )
{
    if( !p_acl )
        return luaL_error( L, "ACL creation failed." );

    vlc_acl_t **pp_acl = lua_newuserdata( L, sizeof( vlc_acl_t * ) );
    *pp_acl = p_acl;

    if( luaL_newmetatable( L, "acl" ) )
    {
        lua_newtable( L );
        luaL_register( L, NULL, vlclua_acl_reg );
        lua_setfield( L, -2, "__index" );
        lua_pushcfunction( L, vlclua_acl_delete );
        lua_setfield( L, -2, "__gc" );
    }

    lua_setmetatable( L, -2 );
    return 1;
}

static int vlclua_acl_delete( lua_State *L )
{
    vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
    ACL_Destroy( *pp_acl );
    return 0;
}

static int vlclua_acl_check( lua_State *L )
{
    vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
    const char *psz_ip = luaL_checkstring( L, 2 );
    lua_pushinteger( L, ACL_Check( *pp_acl, psz_ip ) );
    return 1;
}

static int vlclua_acl_duplicate( lua_State *L )
{
    vlc_object_t *p_this = vlclua_get_this( L );
    vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
    vlc_acl_t *p_acl_new = ACL_Duplicate( p_this, *pp_acl );
    return vlclua_acl_create_inner( L, p_acl_new );
}

static int vlclua_acl_add_host( lua_State *L )
{
    vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
    const char *psz_ip = luaL_checkstring( L, 2 );
    bool b_allow = luaL_checkboolean( L, 3 );
    lua_pushinteger( L, ACL_AddHost( *pp_acl, psz_ip, b_allow ) );
    return 1;
}

static int vlclua_acl_add_net( lua_State *L )
{
    vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
    const char *psz_ip = luaL_checkstring( L, 2 );
    int i_len = luaL_checkint( L, 3 );
    bool b_allow = luaL_checkboolean( L, 4 );
    lua_pushinteger( L, ACL_AddNet( *pp_acl, psz_ip, i_len, b_allow ) );
    return 1;
}

static int vlclua_acl_load_file( lua_State *L )
{
    vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
    const char *psz_path = luaL_checkstring( L, 2 );
    lua_pushinteger( L, ACL_LoadFile( *pp_acl, psz_path ) );
    return 1;
}

void luaopen_acl( lua_State *L )
{
    lua_pushcfunction( L, vlclua_acl_create );
    lua_setfield( L, -2, "acl" );
}