You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_udp.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*-
  2. * Copyright 2019 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "lua_common.h"
  17. #include "lua_thread_pool.h"
  18. #include "utlist.h"
  19. #include "unix-std.h"
  20. #include <math.h>
  21. #include <src/libutil/libev_helper.h>
  22. static const gchar *M = "rspamd lua udp";
  23. /***
  24. * @module rspamd_udp
  25. * Rspamd UDP module is available from the version 1.9.0 and represents a generic
  26. * UDP asynchronous client available from the LUA code.
  27. * This module is quite simple: it can either send requests to some address or
  28. * it can send requests and wait for replies, potentially handling retransmits.
  29. * @example
  30. local logger = require "rspamd_logger"
  31. local udp = require "rspamd_udp"
  32. rspamd_config.SYM = function(task)
  33. udp.sento{
  34. host = addr, -- must be ip address object (e.g. received by upstream module)
  35. port = 500,
  36. data = {'str1', 'str2'}, -- can be table, string or rspamd_text
  37. timeout = 0.5, -- default = 1s
  38. task = task, -- if has task
  39. session = session, -- optional
  40. ev_base = ev_base, -- if no task available
  41. -- You can include callback and then Rspamd will try to read replies
  42. callback = function(success, data)
  43. -- success is bool, data is either data or an error (string)
  44. end,
  45. retransmits = 0, -- Or more if retransmitting is necessary
  46. }
  47. end
  48. */
  49. static const double default_udp_timeout = 1.0;
  50. LUA_FUNCTION_DEF (udp, sendto);
  51. static const struct luaL_reg udp_libf[] = {
  52. LUA_INTERFACE_DEF (udp, sendto),
  53. {NULL, NULL}
  54. };
  55. struct lua_udp_cbdata {
  56. struct ev_loop *event_loop;
  57. struct rspamd_io_ev ev;
  58. struct rspamd_async_event *async_ev;
  59. struct rspamd_task *task;
  60. rspamd_mempool_t *pool;
  61. rspamd_inet_addr_t *addr;
  62. struct rspamd_symcache_item *item;
  63. struct rspamd_async_session *s;
  64. struct iovec *iov;
  65. lua_State *L;
  66. guint retransmits;
  67. guint iovlen;
  68. gint sock;
  69. gint cbref;
  70. gboolean sent;
  71. };
  72. #define msg_debug_udp(...) rspamd_conditional_debug_fast (NULL, cbd->addr, \
  73. rspamd_lua_udp_log_id, "lua_udp", cbd->pool->tag.uid, \
  74. G_STRFUNC, \
  75. __VA_ARGS__)
  76. INIT_LOG_MODULE(lua_udp)
  77. static inline void
  78. lua_fill_iov (lua_State *L, rspamd_mempool_t *pool,
  79. struct iovec *iov, gint pos)
  80. {
  81. if (lua_type (L, pos) == LUA_TUSERDATA) {
  82. struct rspamd_lua_text *t = lua_check_text (L, pos);
  83. if (t) {
  84. iov->iov_base = rspamd_mempool_alloc (pool, t->len);
  85. iov->iov_len = t->len;
  86. memcpy (iov->iov_base, t->start, t->len);
  87. }
  88. }
  89. else {
  90. const gchar *s;
  91. gsize len;
  92. s = lua_tolstring (L, pos, &len);
  93. iov->iov_base = rspamd_mempool_alloc (pool, len);
  94. iov->iov_len = len;
  95. memcpy (iov->iov_base, s, len);
  96. }
  97. }
  98. static void
  99. lua_udp_cbd_fin (gpointer p)
  100. {
  101. struct lua_udp_cbdata *cbd = (struct lua_udp_cbdata *)p;
  102. if (cbd->sock != -1) {
  103. rspamd_ev_watcher_stop (cbd->event_loop, &cbd->ev);
  104. close (cbd->sock);
  105. }
  106. if (cbd->addr) {
  107. rspamd_inet_address_free (cbd->addr);
  108. }
  109. if (cbd->cbref) {
  110. luaL_unref (cbd->L, LUA_REGISTRYINDEX, cbd->cbref);
  111. }
  112. }
  113. static void
  114. lua_udp_maybe_free (struct lua_udp_cbdata *cbd)
  115. {
  116. if (cbd->item) {
  117. rspamd_symcache_item_async_dec_check (cbd->task, cbd->item, M);
  118. cbd->item = NULL;
  119. }
  120. if (cbd->async_ev) {
  121. rspamd_session_remove_event (cbd->s, lua_udp_cbd_fin, cbd);
  122. }
  123. else {
  124. lua_udp_cbd_fin (cbd);
  125. }
  126. }
  127. enum rspamd_udp_send_result {
  128. RSPAMD_SENT_OK,
  129. RSPAMD_SENT_RETRY,
  130. RSPAMD_SENT_FAILURE
  131. };
  132. static enum rspamd_udp_send_result
  133. lua_try_send_request (struct lua_udp_cbdata *cbd)
  134. {
  135. struct msghdr msg;
  136. gint r;
  137. memset (&msg, 0, sizeof (msg));
  138. msg.msg_iov = cbd->iov;
  139. msg.msg_iovlen = cbd->iovlen;
  140. msg.msg_name = rspamd_inet_address_get_sa (cbd->addr, &msg.msg_namelen);
  141. r = sendmsg (cbd->sock, &msg, 0);
  142. if (r != -1) {
  143. return RSPAMD_SENT_OK;
  144. }
  145. if (errno == EAGAIN || errno == EINTR) {
  146. return RSPAMD_SENT_RETRY;
  147. }
  148. return RSPAMD_SENT_FAILURE;
  149. }
  150. static void
  151. lua_udp_maybe_push_error (struct lua_udp_cbdata *cbd, const gchar *err)
  152. {
  153. if (cbd->cbref != -1) {
  154. gint top;
  155. lua_State *L = cbd->L;
  156. top = lua_gettop (L);
  157. lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref);
  158. /* Error message */
  159. lua_pushboolean (L, false);
  160. lua_pushstring (L, err);
  161. if (cbd->item) {
  162. rspamd_symcache_set_cur_item (cbd->task, cbd->item);
  163. }
  164. if (lua_pcall (L, 2, 0, 0) != 0) {
  165. msg_info ("callback call failed: %s", lua_tostring (L, -1));
  166. }
  167. lua_settop (L, top);
  168. }
  169. lua_udp_maybe_free (cbd);
  170. }
  171. static void
  172. lua_udp_push_data (struct lua_udp_cbdata *cbd, const gchar *data,
  173. gssize len)
  174. {
  175. if (cbd->cbref != -1) {
  176. gint top;
  177. lua_State *L = cbd->L;
  178. top = lua_gettop (L);
  179. lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->cbref);
  180. /* Error message */
  181. lua_pushboolean (L, true);
  182. lua_pushlstring (L, data, len);
  183. if (cbd->item) {
  184. rspamd_symcache_set_cur_item (cbd->task, cbd->item);
  185. }
  186. if (lua_pcall (L, 2, 0, 0) != 0) {
  187. msg_info ("callback call failed: %s", lua_tostring (L, -1));
  188. }
  189. lua_settop (L, top);
  190. }
  191. lua_udp_maybe_free (cbd);
  192. }
  193. static gboolean
  194. lua_udp_maybe_register_event (struct lua_udp_cbdata *cbd)
  195. {
  196. if (cbd->s && !cbd->async_ev) {
  197. cbd->async_ev = rspamd_session_add_event (cbd->s, lua_udp_cbd_fin,
  198. cbd, M);
  199. if (!cbd->async_ev) {
  200. return FALSE;
  201. }
  202. }
  203. if (cbd->task && !cbd->item) {
  204. cbd->item = rspamd_symcache_get_cur_item (cbd->task);
  205. rspamd_symcache_item_async_inc (cbd->task, cbd->item, M);
  206. }
  207. return TRUE;
  208. }
  209. static void
  210. lua_udp_io_handler (gint fd, short what, gpointer p)
  211. {
  212. struct lua_udp_cbdata *cbd = (struct lua_udp_cbdata *)p;
  213. gssize r;
  214. if (what == EV_TIMEOUT) {
  215. if (cbd->sent && cbd->retransmits > 0) {
  216. r = lua_try_send_request (cbd);
  217. if (r == RSPAMD_SENT_OK) {
  218. rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_READ);
  219. lua_udp_maybe_register_event (cbd);
  220. cbd->retransmits --;
  221. }
  222. else if (r == RSPAMD_SENT_FAILURE) {
  223. lua_udp_maybe_push_error (cbd, "write error");
  224. }
  225. else {
  226. cbd->retransmits --;
  227. rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_WRITE);
  228. }
  229. }
  230. else {
  231. if (!cbd->sent) {
  232. lua_udp_maybe_push_error (cbd, "sent timeout");
  233. }
  234. else {
  235. lua_udp_maybe_push_error (cbd, "read timeout");
  236. }
  237. }
  238. }
  239. else if (what == EV_WRITE) {
  240. r = lua_try_send_request (cbd);
  241. if (r == RSPAMD_SENT_OK) {
  242. if (cbd->cbref != -1) {
  243. rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_READ);
  244. cbd->sent = TRUE;
  245. }
  246. else {
  247. lua_udp_maybe_free (cbd);
  248. }
  249. }
  250. else if (r == RSPAMD_SENT_FAILURE) {
  251. lua_udp_maybe_push_error (cbd, "write error");
  252. }
  253. else {
  254. cbd->retransmits --;
  255. rspamd_ev_watcher_reschedule (cbd->event_loop, &cbd->ev, EV_WRITE);
  256. }
  257. }
  258. else if (what == EV_READ) {
  259. guchar udpbuf[4096];
  260. socklen_t slen;
  261. struct sockaddr *sa;
  262. sa = rspamd_inet_address_get_sa (cbd->addr, &slen);
  263. r = recvfrom (cbd->sock, udpbuf, sizeof (udpbuf), 0, sa, &slen);
  264. if (r == -1) {
  265. lua_udp_maybe_push_error (cbd, strerror (errno));
  266. }
  267. else {
  268. lua_udp_push_data (cbd, udpbuf, r);
  269. }
  270. }
  271. }
  272. /***
  273. * @function rspamd_udp.sendto({params})
  274. * This function simply sends data to an external UDP service
  275. *
  276. * - `task`: rspamd task objects (implies `pool`, `session` and `ev_base` arguments)
  277. * - `ev_base`: event base (if no task specified)
  278. * - `session`: events session (no task, optional)
  279. * - `pool`: memory pool (if no task specified)
  280. * - `host`: IP or name of the peer (required)
  281. * - `port`: remote port to use (if `host` has no port part this is required)
  282. * - `data`: a table of strings or `rspamd_text` objects that contains data pieces
  283. * - `retransmits`: number of retransmits if needed
  284. * - `callback`: optional callback if reply should be read
  285. * @return {boolean} true if request has been sent (additional string if it has not)
  286. */
  287. static gint
  288. lua_udp_sendto (lua_State *L) {
  289. LUA_TRACE_POINT;
  290. const gchar *host;
  291. guint port;
  292. struct ev_loop *ev_base = NULL;
  293. struct lua_udp_cbdata *cbd;
  294. struct rspamd_async_session *session = NULL;
  295. struct rspamd_task *task = NULL;
  296. rspamd_inet_addr_t *addr;
  297. rspamd_mempool_t *pool = NULL;
  298. gdouble timeout = default_udp_timeout;
  299. if (lua_type (L, 1) == LUA_TTABLE) {
  300. lua_pushstring (L, "port");
  301. lua_gettable (L, -2);
  302. if (lua_type (L, -1) == LUA_TNUMBER) {
  303. port = lua_tointeger (L, -1);
  304. }
  305. else {
  306. /* We assume that it is a unix socket */
  307. port = 0;
  308. }
  309. lua_pop (L, 1);
  310. lua_pushstring (L, "host");
  311. lua_gettable (L, -2);
  312. if (lua_type (L, -1) == LUA_TSTRING) {
  313. host = luaL_checkstring (L, -1);
  314. if (rspamd_parse_inet_address (&addr,
  315. host, strlen (host), RSPAMD_INET_ADDRESS_PARSE_DEFAULT)) {
  316. if (port != 0) {
  317. rspamd_inet_address_set_port (addr, port);
  318. }
  319. }
  320. else {
  321. lua_pop (L, 1);
  322. return luaL_error (L, "invalid host: %s", host);
  323. }
  324. }
  325. else if (lua_type (L, -1) == LUA_TUSERDATA) {
  326. struct rspamd_lua_ip *lip;
  327. lip = lua_check_ip (L, -1);
  328. if (lip == NULL || lip->addr == NULL) {
  329. lua_pop (L, 1);
  330. return luaL_error (L, "invalid host class");
  331. }
  332. addr = rspamd_inet_address_copy (lip->addr);
  333. if (port != 0) {
  334. rspamd_inet_address_set_port (addr, port);
  335. }
  336. }
  337. else {
  338. lua_pop (L, 1);
  339. return luaL_error (L, "invalid host");
  340. }
  341. lua_pop (L, 1);
  342. lua_pushstring (L, "task");
  343. lua_gettable (L, -2);
  344. if (lua_type (L, -1) == LUA_TUSERDATA) {
  345. task = lua_check_task (L, -1);
  346. ev_base = task->event_loop;
  347. session = task->s;
  348. pool = task->task_pool;
  349. }
  350. lua_pop (L, 1);
  351. if (task == NULL) {
  352. lua_pushstring (L, "ev_base");
  353. lua_gettable (L, -2);
  354. if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{ev_base}")) {
  355. ev_base = *(struct ev_loop **) lua_touserdata (L, -1);
  356. } else {
  357. ev_base = NULL;
  358. }
  359. lua_pop (L, 1);
  360. lua_pushstring (L, "session");
  361. lua_gettable (L, -2);
  362. if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{session}")) {
  363. session = *(struct rspamd_async_session **) lua_touserdata (L, -1);
  364. } else {
  365. session = NULL;
  366. }
  367. lua_pop (L, 1);
  368. lua_pushstring (L, "pool");
  369. lua_gettable (L, -2);
  370. if (rspamd_lua_check_udata_maybe (L, -1, "rspamd{mempool}")) {
  371. pool = *(rspamd_mempool_t **) lua_touserdata (L, -1);
  372. } else {
  373. pool = NULL;
  374. }
  375. lua_pop (L, 1);
  376. }
  377. lua_pushstring (L, "timeout");
  378. lua_gettable (L, -2);
  379. if (lua_type (L, -1) == LUA_TNUMBER) {
  380. timeout = lua_tonumber (L, -1);
  381. }
  382. lua_pop (L, 1);
  383. if (!ev_base || !pool) {
  384. rspamd_inet_address_free (addr);
  385. return luaL_error (L, "invalid arguments");
  386. }
  387. cbd = rspamd_mempool_alloc0 (pool, sizeof (*cbd));
  388. cbd->event_loop = ev_base;
  389. cbd->pool = pool;
  390. cbd->s = session;
  391. cbd->addr = addr;
  392. cbd->sock = rspamd_socket_create (rspamd_inet_address_get_af (addr),
  393. SOCK_DGRAM, 0, TRUE);
  394. cbd->cbref = -1;
  395. cbd->ev.timeout = timeout;
  396. if (cbd->sock == -1) {
  397. rspamd_inet_address_free (addr);
  398. return luaL_error (L, "cannot open socket: %s", strerror (errno));
  399. }
  400. cbd->L = L;
  401. gsize data_len;
  402. lua_pushstring (L, "data");
  403. lua_gettable (L, -2);
  404. if (lua_type (L, -1) == LUA_TTABLE) {
  405. data_len = rspamd_lua_table_size (L, -1);
  406. cbd->iov = rspamd_mempool_alloc (pool,
  407. sizeof (*cbd->iov) * data_len);
  408. for (int i = 0; i < data_len; i ++) {
  409. lua_rawgeti (L, -1, i + 1);
  410. lua_fill_iov (L, pool, &cbd->iov[i], -1);
  411. lua_pop (L, 1);
  412. }
  413. cbd->iovlen = data_len;
  414. }
  415. else {
  416. cbd->iov = rspamd_mempool_alloc (pool, sizeof (*cbd->iov));
  417. cbd->iovlen = 1;
  418. lua_fill_iov (L, pool, cbd->iov, -1);
  419. }
  420. lua_pop (L, 1);
  421. lua_pushstring (L, "callback");
  422. lua_gettable (L, -2);
  423. if (lua_type (L, -1) == LUA_TFUNCTION) {
  424. cbd->cbref = luaL_ref (L, LUA_REGISTRYINDEX);
  425. }
  426. else {
  427. lua_pop (L, 1);
  428. }
  429. lua_pushstring (L, "retransmits");
  430. lua_gettable (L, -2);
  431. if (lua_type (L, -1) == LUA_TNUMBER) {
  432. cbd->retransmits = lua_tonumber (L, -1);
  433. }
  434. lua_pop (L, 1);
  435. enum rspamd_udp_send_result r;
  436. r = lua_try_send_request (cbd);
  437. if (r == RSPAMD_SENT_OK) {
  438. if (cbd->cbref == -1) {
  439. lua_udp_maybe_free (cbd);
  440. }
  441. else {
  442. if (!lua_udp_maybe_register_event (cbd)) {
  443. lua_pushboolean (L, false);
  444. lua_pushstring (L, "session error");
  445. lua_udp_maybe_free (cbd);
  446. return 2;
  447. }
  448. rspamd_ev_watcher_init (&cbd->ev, cbd->sock, EV_READ,
  449. lua_udp_io_handler, cbd);
  450. rspamd_ev_watcher_start (cbd->event_loop, &cbd->ev, timeout);
  451. cbd->sent = TRUE;
  452. }
  453. lua_pushboolean (L, true);
  454. }
  455. else if (r == RSPAMD_SENT_FAILURE) {
  456. lua_pushboolean (L, false);
  457. lua_pushstring (L, strerror (errno));
  458. lua_udp_maybe_free (cbd);
  459. return 2;
  460. }
  461. else {
  462. rspamd_ev_watcher_init (&cbd->ev, cbd->sock, EV_WRITE,
  463. lua_udp_io_handler, cbd);
  464. rspamd_ev_watcher_start (cbd->event_loop, &cbd->ev, timeout);
  465. if (!lua_udp_maybe_register_event (cbd)) {
  466. lua_pushboolean (L, false);
  467. lua_pushstring (L, "session error");
  468. lua_udp_maybe_free (cbd);
  469. return 2;
  470. }
  471. }
  472. }
  473. else {
  474. return luaL_error (L, "invalid arguments");
  475. }
  476. return 1;
  477. }
  478. static gint
  479. lua_load_udp (lua_State * L)
  480. {
  481. lua_newtable (L);
  482. luaL_register (L, NULL, udp_libf);
  483. return 1;
  484. }
  485. void
  486. luaopen_udp (lua_State * L)
  487. {
  488. rspamd_lua_add_preload (L, "rspamd_udp", lua_load_udp);
  489. }