module.h 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 3 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, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <libknot/processing/layer.h>
20 21
#include "lib/defines.h"
#include "lib/utils.h"
22 23 24 25 26

/*
 * Forward decls
 */
struct kr_module;
27
struct kr_prop;
28 29 30

/*
 * API definition.
31
 * @cond internal
32 33 34 35
 */
typedef uint32_t (module_api_cb)(void);
typedef int (module_init_cb)(struct kr_module *);
typedef int (module_deinit_cb)(struct kr_module *);
36
typedef int (module_config_cb)(struct kr_module *, const char *);
37
typedef const knot_layer_api_t* (module_layer_cb)(struct kr_module *);
38
typedef struct kr_prop *(module_prop_cb)(void);
39
typedef char *(kr_prop_cb)(void *, struct kr_module *, const char *);
40
#define KR_MODULE_API ((uint32_t) 0x20150402)
41
/* @endcond */
42

43 44 45 46 47
/**
 * Module property (named callable).
 * A module property has a free-form JSON output (and optional input).
 */
struct kr_prop {
48
    kr_prop_cb *cb;
49 50 51 52 53 54 55
    const char *name;
    const char *info;
};

/**
 * Module representation.
 */
56
struct kr_module {
57 58 59 60 61 62 63 64
    char *name;               /**< Name. */
    module_init_cb   *init;   /**< Constructor */
    module_deinit_cb *deinit; /**< Destructor */
    module_config_cb *config; /**< Configuration */
    module_layer_cb  *layer;  /**< Layer getter */
    struct kr_prop   *props;  /**< Properties */
    void *lib;                /**< Shared library handle or RTLD_DEFAULT */
    void *data;               /**< Custom data context. */
65 66
};

67 68
/**
 * Load module instance into memory.
69
 *
70 71 72 73 74
 * @param module module structure
 * @param name module name
 * @param path module search path
 * @return 0 or an error
 */
75
KR_EXPORT
76
int kr_module_load(struct kr_module *module, const char *name, const char *path);
77

78 79
/**
 * Unload module instance.
80
 *
81 82
 * @param module module structure
 */
83
KR_EXPORT
84 85
void kr_module_unload(struct kr_module *module);

86 87
/**
 * Export module API version (place this at the end of your module).
88
 *
89 90 91
 * @param module module name (f.e. hints)
 */
#define KR_MODULE_EXPORT(module) \
92
    KR_EXPORT uint32_t module ## _api() { return KR_MODULE_API; }