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

    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
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16
 */

Vladimír Čunát's avatar
Vladimír Čunát committed
17 18 19
/** @file
 * Module API definition and functions for (un)loading modules.
 */
20

21 22
#pragma once

23 24
#include "lib/defines.h"
#include "lib/utils.h"
Marek Vavrusa's avatar
Marek Vavrusa committed
25
#include "lib/layer.h"
26 27

struct kr_module;
28
struct kr_prop;
29

30 31 32 33 34

/**
 * Export module API version (place this at the end of your module).
 *
 * @param module module name (f.e. hints)
35
 */
36 37
#define KR_MODULE_EXPORT(module) \
    KR_EXPORT uint32_t module ## _api() { return KR_MODULE_API; }
38
#define KR_MODULE_API ((uint32_t) 0x20180401)
39 40 41

typedef uint32_t (module_api_cb)(void);

42

43
/**
44 45 46 47
 * Module representation.
 *
 * The five symbols (init, ...) may be defined by the module as name_init(), etc;
 * all are optional and missing symbols are represented as NULLs;
48
 */
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
struct kr_module {
	char *name;

	/** Constructor.  Called after loading the module.  @return error code. */
	int (*init)(struct kr_module *self);
	/** Destructor.  Called before unloading the module.  @return error code. */
	int (*deinit)(struct kr_module *self);
	/** Configure with encoded JSON (NULL if missing).  @return error code. */
	int (*config)(struct kr_module *self, const char *input);
	/** Get a pointer to packet processing API specs.  See docs on that type. */
	const kr_layer_api_t * (*layer)(struct kr_module *self);
	/** Get a pointer to list of properties, terminated by { NULL, NULL, NULL }. */
	const struct kr_prop * (*props)(void);

	void *lib;      /**< Shared library handle or RTLD_DEFAULT */
	void *data;     /**< Custom data context. */
65 66 67
};

/**
68 69 70 71 72
 * Module property callback.  Input and output is passed via a JSON encoded in a string.
 *
 * @param env pointer to the lua engine, i.e. struct engine *env (TODO: explicit type)
 * @param input parameter (NULL if missing/nil on lua level)
 * @return a free-form JSON output (malloc-ated)
73
 * @note see l_trampoline() implementation for details about the input/output conversion.
74
 */
75 76 77 78 79 80 81 82 83
typedef char *(kr_prop_cb)(void *env, struct kr_module *self, const char *input);

/**
 * Module property (named callable).
 */
struct kr_prop {
	kr_prop_cb *cb;
	const char *name;
	const char *info;
84 85
};

86

87
/**
88
 * Load a C module instance into memory.
89
 *
90 91 92 93 94
 * @param module module structure
 * @param name module name
 * @param path module search path
 * @return 0 or an error
 */
95
KR_EXPORT
96
int kr_module_load(struct kr_module *module, const char *name, const char *path);
97

98 99
/**
 * Unload module instance.
100
 *
101 102
 * @param module module structure
 */
103
KR_EXPORT
104 105
void kr_module_unload(struct kr_module *module);

106 107 108 109 110 111
/**
 * Get embedded module prototype by name (or NULL).
 */
KR_EXPORT
const struct kr_module * kr_module_embedded(const char *name);