aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_upstream.c
blob: f9e74e02708b6bc12c72c534ea0d4cfa289aaaef (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* Copyright (c) 2010-2011, Vsevolod Stakhov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *       * Redistributions of source code must retain the above copyright
 *         notice, this list of conditions and the following disclaimer.
 *       * Redistributions in binary form must reproduce the above copyright
 *         notice, this list of conditions and the following disclaimer in the
 *         documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "lua_common.h"
#include "upstream.h"
#include "cfg_file.h"

/* Upstream timeouts */
#define DEFAULT_UPSTREAM_ERROR_TIME 10
#define DEFAULT_UPSTREAM_DEAD_TIME 300
#define DEFAULT_UPSTREAM_MAXERRORS 10

/**
 * This module implements upstreams manipulation from lua
 */
/* Upstream list functions */
LUA_FUNCTION_DEF (upstream_list, create);
LUA_FUNCTION_DEF (upstream_list, destroy);
LUA_FUNCTION_DEF (upstream_list, get_upstream_by_hash);
LUA_FUNCTION_DEF (upstream_list, get_upstream_round_robin);
LUA_FUNCTION_DEF (upstream_list, get_upstream_master_slave);

static const struct luaL_reg  upstream_list_m[] = {

	LUA_INTERFACE_DEF (upstream_list, get_upstream_by_hash),
	LUA_INTERFACE_DEF (upstream_list, get_upstream_round_robin),
	LUA_INTERFACE_DEF (upstream_list, get_upstream_master_slave),
	{"__tostring", lua_class_tostring},
	{"__gc", lua_upstream_list_destroy},
	{NULL, NULL}
};
static const struct luaL_reg  upstream_list_f[] = {
	LUA_INTERFACE_DEF (upstream_list, create),
	{NULL, NULL}
};

/* Upstream functions */
LUA_FUNCTION_DEF (upstream, create);
LUA_FUNCTION_DEF (upstream, destroy);
LUA_FUNCTION_DEF (upstream, ok);
LUA_FUNCTION_DEF (upstream, fail);
LUA_FUNCTION_DEF (upstream, get_ip);
LUA_FUNCTION_DEF (upstream, get_port);
LUA_FUNCTION_DEF (upstream, get_ip_string);
LUA_FUNCTION_DEF (upstream, get_priority);

static const struct luaL_reg  upstream_m[] = {
	LUA_INTERFACE_DEF (upstream, ok),
	LUA_INTERFACE_DEF (upstream, fail),
	LUA_INTERFACE_DEF (upstream, get_ip),
	LUA_INTERFACE_DEF (upstream, get_ip_string),
	LUA_INTERFACE_DEF (upstream, get_port),
	LUA_INTERFACE_DEF (upstream, get_priority),
	LUA_INTERFACE_DEF (upstream, destroy),
	{"__tostring", lua_class_tostring},
	{NULL, NULL}
};
static const struct luaL_reg  upstream_f[] = {
	LUA_INTERFACE_DEF (upstream, create),
	{NULL, NULL}
};

/* Upstream class */
struct lua_upstream {
	struct upstream up;
	gchar *def;
	guint16 port;
	struct in_addr addr;
};

static struct lua_upstream	*
lua_check_upstream (lua_State * L)
{
	void									*ud = luaL_checkudata (L, 1, "rspamd{upstream}");

	luaL_argcheck (L, ud != NULL, 1, "'upstream' expected");
	return ud ? *((struct lua_upstream **)ud) : NULL;
}

/**
 * Create new upstream from its string definition like 'ip[:port[:priority]]' or 'host[:port[:priority]]'
 * @param L
 * @return upstream structure
 */
static gint
lua_upstream_create (lua_State *L)
{
	struct lua_upstream						*new, **pnew;
	const gchar								*def;

	def = luaL_checkstring (L, 1);
	if (def) {
		new = g_slice_alloc0 (sizeof (struct lua_upstream));
		new->def = g_strdup (def);
		if (!parse_host_port_priority (new->def, &new->addr, &new->port, &new->up.priority)) {
			g_free (new->def);
			g_slice_free1 (sizeof (struct lua_upstream), new);
			lua_pushnil (L);
		}
		else {
			pnew = lua_newuserdata (L, sizeof (struct lua_upstream *));
			lua_setclass (L, "rspamd{upstream}", -1);
			*pnew = new;
		}
	}

	return 1;
}

/**
 * Destroy a single upstream object
 * @param L
 * @return
 */
static gint
lua_upstream_destroy (lua_State *L)
{
	struct lua_upstream						*up = lua_check_upstream (L);

	if (up) {
		g_free (up->def);
		g_slice_free1 (sizeof (struct lua_upstream), up);
	}

	return 0;
}

/**
 * Get ip of upstream in numeric form (guint32)
 * @param L
 * @return
 */
static gint
lua_upstream_get_ip (lua_State *L)
{
	struct lua_upstream						*up = lua_check_upstream (L);

	if (up) {
		lua_pushinteger (L, up->addr.s_addr);
	}
	else {
		lua_pushnil (L);
	}

	return 1;
}

/**
 * Get ip of upstream in string form
 * @param L
 * @return
 */
static gint
lua_upstream_get_ip_string (lua_State *L)
{
	struct lua_upstream						*up = lua_check_upstream (L);

	if (up) {
		lua_pushstring (L, inet_ntoa (up->addr));
	}
	else {
		lua_pushnil (L);
	}

	return 1;
}

/**
 * Get port of upstream in numeric form
 * @param L
 * @return
 */
static gint
lua_upstream_get_port (lua_State *L)
{
	struct lua_upstream						*up = lua_check_upstream (L);

	if (up) {
		lua_pushinteger (L, up->port);
	}
	else {
		lua_pushnil (L);
	}

	return 1;
}

/**
 * Get port of upstream in numeric form
 * @param L
 * @return
 */
static gint
lua_upstream_get_priority (lua_State *L)
{
	struct lua_upstream						*up = lua_check_upstream (L);

	if (up) {
		lua_pushinteger (L, up->up.priority);
	}
	else {
		lua_pushnil (L);
	}

	return 1;
}

/**
 * Make upstream fail, the second argument is time, if absent the current time is used
 * @param L
 * @return
 */
static gint
lua_upstream_fail (lua_State *L)
{
	struct lua_upstream						*up = lua_check_upstream (L);
	time_t									 now;

	if (up) {
		if (lua_gettop (L) >= 2) {
			now = luaL_checkinteger (L, 2);
		}
		else {
			now = time (NULL);
		}
		upstream_fail (&up->up, now);
	}

	return 0;
}

/**
 * Make upstream success, the second argument is time, if absent the current time is used
 * @param L
 * @return
 */
static gint
lua_upstream_ok (lua_State *L)
{
	struct lua_upstream						*up = lua_check_upstream (L);
	time_t									 now;

	if (up) {
		if (lua_gettop (L) >= 2) {
			now = luaL_checkinteger (L, 2);
		}
		else {
			now = time (NULL);
		}
		upstream_ok (&up->up, now);
	}

	return 0;
}

/* Upstream list class */
struct lua_upstream_list {
	struct lua_upstream *upstreams;
	guint count;
};

static struct lua_upstream_list	*
lua_check_upstream_list (lua_State * L)
{
	void									*ud = luaL_checkudata (L, 1, "rspamd{upstream_list}");

	luaL_argcheck (L, ud != NULL, 1, "'upstream_list' expected");
	return ud ? *((struct lua_upstream_list **)ud) : NULL;
}

/**
 * Create new upstream list from its string definition like '<upstream>,<upstream>;<upstream>'
 * @param L
 * @return upstream list structure
 */
static gint
lua_upstream_list_create (lua_State *L)
{
	struct lua_upstream_list				*new, **pnew;
	struct lua_upstream						*cur;
	const gchar								*def;
	char									**tokens;
	guint									 i, default_port = 0;

	def = luaL_checkstring (L, 1);
	if (def) {
		if (lua_gettop (L) >= 2) {
			default_port = luaL_checkinteger (L, 2);
		}
		new = g_slice_alloc0 (sizeof (struct lua_upstream_list));

		tokens = g_strsplit_set (def, ",;", 0);
		if (!tokens || !tokens[0]) {
			goto err;
		}
		new->count = g_strv_length (tokens);
		new->upstreams = g_slice_alloc0 (new->count * sizeof (struct lua_upstream));

		for (i = 0; i < new->count; i ++) {
			cur = &new->upstreams[i];
			if (!parse_host_port_priority (tokens[i], &cur->addr, &cur->port, &cur->up.priority)) {
				goto err;
			}
			if (cur->port == 0) {
				cur->port = default_port;
			}
		}
		pnew = lua_newuserdata (L, sizeof (struct upstream_list *));
		lua_setclass (L, "rspamd{upstream_list}", -1);
		*pnew = new;
	}

	return 1;
err:
	if (tokens) {
		g_strfreev (tokens);
	}
	if (new->upstreams) {
		g_slice_free1 (new->count * sizeof (struct lua_upstream), new->upstreams);
	}
	g_slice_free1 (sizeof (struct lua_upstream_list), new);
	lua_pushnil (L);
	return 1;
}

/**
 * Destroy a single upstream list object
 * @param L
 * @return
 */
static gint
lua_upstream_list_destroy (lua_State *L)
{
	struct lua_upstream_list					*upl = lua_check_upstream_list (L);

	if (upl) {
		if (upl->upstreams) {
			g_slice_free1 (upl->count * sizeof (struct lua_upstream), upl->upstreams);
		}
		g_slice_free1 (sizeof (struct lua_upstream_list), upl);
	}

	return 0;
}

/**
 * Get upstream by hash from key, params are: key and time (optional)
 * @param L
 * @return
 */
static gint
lua_upstream_list_get_upstream_by_hash (lua_State *L)
{
	struct lua_upstream_list					*upl;
	struct lua_upstream							*selected, **pselected;
	time_t										 now;
	const gchar									*key;

	upl = lua_check_upstream_list (L);
	if (upl) {
		key = luaL_checkstring (L, 2);
		if (key) {
			if (lua_gettop (L) >= 3) {
				now = luaL_checkinteger (L, 3);
			}
			else {
				now = time (NULL);
			}
			selected = (struct lua_upstream *)get_upstream_by_hash (upl->upstreams, upl->count,
					sizeof (struct lua_upstream), now,
					DEFAULT_UPSTREAM_ERROR_TIME, DEFAULT_UPSTREAM_DEAD_TIME, DEFAULT_UPSTREAM_MAXERRORS,
					key, 0);
			if (selected) {
				pselected = lua_newuserdata (L, sizeof (struct lua_upstream *));
				lua_setclass (L, "rspamd{upstream}", -1);
				*pselected = selected;
			}
			else {
				lua_pushnil (L);
			}
		}
		else {
			lua_pushnil (L);
		}
	}
	else {
		lua_pushnil (L);
	}

	return 1;
}

/**
 * Get upstream round robin (by current weight), params are: time (optional)
 * @param L
 * @return
 */
static gint
lua_upstream_list_get_upstream_round_robin (lua_State *L)
{
	struct lua_upstream_list					*upl;
	struct lua_upstream							*selected, **pselected;
	time_t										 now;

	upl = lua_check_upstream_list (L);
	if (upl) {
		if (lua_gettop (L) >= 2) {
			now = luaL_checkinteger (L, 2);
		}
		else {
			now = time (NULL);
		}
		selected = (struct lua_upstream *)get_upstream_round_robin (upl->upstreams, upl->count,
				sizeof (struct lua_upstream), now,
				DEFAULT_UPSTREAM_ERROR_TIME, DEFAULT_UPSTREAM_DEAD_TIME, DEFAULT_UPSTREAM_MAXERRORS);
		if (selected) {
			pselected = lua_newuserdata (L, sizeof (struct lua_upstream *));
			lua_setclass (L, "rspamd{upstream}", -1);
			*pselected = selected;
		}
		else {
			lua_pushnil (L);
		}
	}
	else {
		lua_pushnil (L);
	}

	return 1;
}

/**
 * Get upstream master slave order (by static priority), params are: time (optional)
 * @param L
 * @return
 */
static gint
lua_upstream_list_get_upstream_master_slave (lua_State *L)
{
	struct lua_upstream_list					*upl;
	struct lua_upstream							*selected, **pselected;
	time_t										 now;

	upl = lua_check_upstream_list (L);
	if (upl) {
		if (lua_gettop (L) >= 2) {
			now = luaL_checkinteger (L, 2);
		}
		else {
			now = time (NULL);
		}
		selected = (struct lua_upstream *)get_upstream_master_slave (upl->upstreams, upl->count,
				sizeof (struct lua_upstream), now,
				DEFAULT_UPSTREAM_ERROR_TIME, DEFAULT_UPSTREAM_DEAD_TIME, DEFAULT_UPSTREAM_MAXERRORS);
		if (selected) {
			pselected = lua_newuserdata (L, sizeof (struct lua_upstream *));
			lua_setclass (L, "rspamd{upstream}", -1);
			*pselected = selected;
		}
		else {
			lua_pushnil (L);
		}
	}
	else {
		lua_pushnil (L);
	}

	return 1;
}


gint
luaopen_upstream (lua_State * L)
{
	luaL_newmetatable (L, "rspamd{upstream_list}");
	lua_pushstring (L, "__index");
	lua_pushvalue (L, -2);
	lua_settable (L, -3);

	lua_pushstring (L, "class");
	lua_pushstring (L, "rspamd{upstream_list}");
	lua_rawset (L, -3);

	luaL_openlib (L, NULL, upstream_list_m, 0);
	luaL_openlib (L, "upstream_list", upstream_list_f, 0);

	luaL_newmetatable (L, "rspamd{upstream}");
	lua_pushstring (L, "__index");
	lua_pushvalue (L, -2);
	lua_settable (L, -3);

	lua_pushstring (L, "class");
	lua_pushstring (L, "rspamd{upstream}");
	lua_rawset (L, -3);

	luaL_openlib (L, NULL, upstream_m, 0);
	luaL_openlib (L, "upstream", upstream_f, 0);

	return 1;
}