forked from hugsy/proxenet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin-perl.c
279 lines (210 loc) · 5.25 KB
/
plugin-perl.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifdef _PERL_PLUGIN
/*******************************************************************************
*
* Perl plugin
*
*/
#include <EXTERN.h>
#include <perl.h>
#include <string.h>
#include <alloca.h>
#include "core.h"
#include "plugin.h"
#include "plugin-perl.h"
#include "utils.h"
#include "main.h"
static PerlInterpreter *my_perl;
/**
*
*/
static int proxenet_perl_load_file(plugin_t* plugin)
{
char *pathname = NULL;
size_t pathlen = 0;
SV* sv = NULL;
int nb_res = -1;
SV* package_sv = NULL;
char *required = NULL;
char *package_name = NULL;
size_t package_len, len = 0;
int ret = -1;
pathlen = strlen(cfg->plugins_path) + 1 + strlen(plugin->filename) + 1;
pathname = (char*) alloca(pathlen+1);
proxenet_xzero(pathname, pathlen+1);
snprintf(pathname, pathlen, "%s/%s", cfg->plugins_path, plugin->filename);
#ifdef DEBUG
xlog(LOG_DEBUG, "[Perl] Loading '%s'\n", pathname);
#endif
/* Load the file through perl's require mechanism */
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
PUTBACK;
sv = newSVpvf("$package = require q%c%s%c", 0, pathname, 0);
nb_res = eval_sv(sv_2mortal(sv), G_EVAL);
if (nb_res != 1) {
xlog(LOG_ERROR,
"[Perl] Invalid number of response returned while loading '%s' (got %d, expected 1)\n",
pathname,
nb_res);
} else if (SvTRUE(ERRSV)) {
xlog(LOG_ERROR, "[Perl] Eval error for '%s': %s\n", pathname, SvPV_nolen(ERRSV));
} else {
/* Get the package name from the package (which should follow the convention...) */
package_sv = get_sv("package", 0);
/* Check if the SV* stores a string */
if (!SvPOK(package_sv)) {
xlog(LOG_ERROR, "[Perl] Invalid convention for '%s': the package should return a string\n", pathname);
} else {
required = (char*) SvPV_nolen(package_sv);
package_len = strlen(required);
package_name = (char*) alloca(package_len+1);
proxenet_xzero(package_name, package_len+1);
memcpy(package_name, required, package_len);
#ifdef DEBUG
xlog(LOG_DEBUG, "[Perl] Package of name '%s' loaded\n", package_name);
#endif
/* Save the functions' full name to call them later */
len = package_len + 2 + strlen(CFG_REQUEST_PLUGIN_FUNCTION);
plugin->pre_function = proxenet_xmalloc(len + 1);
snprintf(plugin->pre_function, len+1, "%s::%s", package_name, CFG_REQUEST_PLUGIN_FUNCTION);
len = package_len + 2 + strlen(CFG_RESPONSE_PLUGIN_FUNCTION);
plugin->post_function = proxenet_xmalloc(len + 1);
snprintf(plugin->post_function, len+1, "%s::%s", package_name, CFG_RESPONSE_PLUGIN_FUNCTION);
ret = 0;
}
}
SPAGAIN;
PUTBACK;
FREETMPS;
LEAVE;
return ret;
}
/**
*
*/
int proxenet_perl_initialize_vm(plugin_t* plugin)
{
interpreter_t *interpreter;
interpreter = plugin->interpreter;
/* In order to perl_parse nothing */
char *args[2] = {
"",
"/dev/null"
};
/* checks */
if (!interpreter->ready){
#ifdef DEBUG
xlog(LOG_DEBUG, "[Perl] %s\n", "Initializing VM");
#endif
/* vm init */
my_perl = perl_alloc();
perl_construct(my_perl);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
if (!my_perl) {
xlog(LOG_ERROR, "[Perl] %s\n", "failed init-ing vm");
return -1;
}
interpreter->vm = (void*) my_perl;
interpreter->ready = true;
perl_parse(my_perl, NULL, 2, args, (char **)NULL);
}
return proxenet_perl_load_file(plugin);
}
/**
*
*/
int proxenet_perl_destroy_vm(plugin_t* plugin)
{
if (!plugin->interpreter->ready)
return -1;
perl_destruct(my_perl);
perl_free(my_perl);
plugin->interpreter->vm = NULL;
plugin->interpreter->ready = false;
return 0;
}
/**
*
*/
static char* proxenet_perl_execute_function(plugin_t* plugin, const char* fname, long rid, char* request_str, size_t* request_size)
{
dSP;
char *res, *data;
int nb_res;
size_t len;
SV* sv = NULL;
res = data = NULL;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVuv(rid)));
XPUSHs(sv_2mortal(newSVpvn(request_str, *request_size)));
PUTBACK;
nb_res = call_pv(fname, G_SCALAR);
SPAGAIN;
if (nb_res != 1) {
xlog(LOG_ERROR, "[Perl] Invalid number of response returned (got %d, expected 1)\n", nb_res);
data = NULL;
} else {
sv = POPs;
res = SvPV(sv, len);
data = (char*) proxenet_xmalloc(len+1);
memcpy(data, res, len);
*request_size = len;
}
PUTBACK;
FREETMPS;
LEAVE;
return data;
}
/**
*
*/
static void proxenet_perl_lock_vm(interpreter_t *interpreter)
{
pthread_mutex_lock(&interpreter->mutex);
}
/**
*
*/
static void proxenet_perl_unlock_vm(interpreter_t *interpreter)
{
pthread_mutex_unlock(&interpreter->mutex);
}
/**
*
*/
char* proxenet_perl_plugin(plugin_t* plugin, request_t* request)
{
interpreter_t *interpreter;
const char *function_name;
char *buf;
interpreter = plugin->interpreter;
if (!interpreter->ready)
return NULL;
if (request->type == REQUEST)
function_name = plugin->pre_function;
else
function_name = plugin->post_function;
proxenet_perl_lock_vm(interpreter);
buf = proxenet_perl_execute_function(plugin, function_name, request->id, request->data, &request->size);
proxenet_perl_unlock_vm(interpreter);
return buf;
}
/**
*
*/
void proxenet_perl_preinitialisation(int argc, char** argv, char** envp)
{
PERL_SYS_INIT3(&argc, &argv, &envp);
}
/**
*
*/
void proxenet_perl_postdeletion()
{
PERL_SYS_TERM();
}
#endif /* _PERL_PLUGIN */