rspamd_tcpRspamd TCP module represents generic TCP asynchronous client available from LUA code. This module hides all complexity: DNS resolving, sessions management, zero-copy text transfers and so on under the hood. It can work in partial or complete modes:
local logger = require "rspamd_logger"
local tcp = require "rspamd_tcp"
rspamd_config.SYM = function(task)
local function cb(err, data)
logger.infox('err: %1, data: %2', err, tostring(data))
end
tcp.request({
task = task,
host = "google.com",
port = 80,
data = {"GET / HTTP/1.0\r\n", "Host: google.com\r\n", "\r\n"},
callback = cb})
end
-- New TCP syntax test
rspamd_config:register_symbol({
name = 'TCP_TEST',
type = "normal",
callback = function(task)
local logger = require "rspamd_logger"
local function rcpt_done_cb(err, data, conn)
logger.errx(task, 'RCPT: got reply: %s, error: %s', data, err)
conn:close()
end
local function rcpt_cb(err, conn)
logger.errx(task, 'written rcpt, error: %s', err)
conn:add_read(rcpt_done_cb, '\r\n')
end
local function from_done_cb(err, data, conn)
logger.errx(task, 'FROM: got reply: %s, error: %s', data, err)
conn:add_write(rcpt_cb, 'RCPT TO: <hui@yandex.ru>\r\n')
end
local function from_cb(err, conn)
logger.errx(task, 'written from, error: %s', err)
conn:add_read(from_done_cb, '\r\n')
end
local function hello_done_cb(err, data, conn)
logger.errx(task, 'HELO: got reply: %s, error: %s', data, err)
conn:add_write(from_cb, 'MAIL FROM: <>\r\n')
end
local function hello_cb(err, conn)
logger.errx(task, 'written hello, error: %s', err)
conn:add_read(hello_done_cb, '\r\n')
end
local function init_cb(err, data, conn)
logger.errx(task, 'got reply: %s, error: %s', data, err)
conn:add_write(hello_cb, 'HELO example.com\r\n')
end
tcp.request{
task = task,
callback = init_cb,
stop_pattern = '\r\n',
host = 'mx.yandex.ru',
port = 25
end,
priority = 10,
})Functions:
Methods:
The module rspamd_tcp defines the following functions.
rspamd_tcp.request({params})This function creates and sends TCP request to the specified host and port, resolves hostname (if needed) and invokes continuation callback upon data received from the remote peer. This function accepts table of arguments with the following attributes
task: rspamd task objects (implies pool, session, ev_base and resolver arguments)ev_base: event base (if no task specified)resolver: DNS resolver (no task)session: events session (no task)pool: memory pool (no task)host: IP or name of the peer (required)port: remote port to usedata: a table of strings or rspamd_text objects that contains data piecescallback: continuation function (required)on_connect: callback called on connection successtimeout: floating point value that specifies timeout for IO operations in secondspartial: boolean flag that specifies that callback should be called on any data portion receivedstop_pattern: stop reading on finding a certain pattern (e.g. \r\n.\r\n for smtp)shutdown: half-close socket after writing (boolean: default false)read: read response after sending request (boolean: default true)Parameters:
No parameters
Returns:
{boolean}: true if request has been sentBack to module description.
The module rspamd_tcp defines the following methods.
tcp:close()Closes TCP connection
Parameters:
No parameters
Returns:
No return
Back to module description.
tcp:set_timeout(seconds)Sets new timeout for a TCP connection in seconds
Parameters:
seconds {number}: floating point value that specifies new timeoutReturns:
No return
Back to module description.
tcp:add_read(callback, [pattern])Adds new read event to the tcp connection
Parameters:
callback {function}: to be called when data is readpattern {string}: optional stop patternReturns:
No return
Back to module description.
tcp:add_write(callback, data)Adds new write event to the tcp connection
Parameters:
optional {function}: callback to be called when data is completely writtendata {table/string/text}: to send to a remote serverReturns:
No return
Back to module description.
tcp:shift_callback()Shifts the current callback and go to the next one (if any)
Parameters:
No parameters
Returns:
No return
Back to module description.
Back to top.