Module rspamd_tcp

Rspamd 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:

  • partial mode is used when you need to call a continuation routine each time data is available for read
  • complete mode calls for continuation merely when all data is read from socket (e.g. when a server sends reply and closes a connection)

Example:

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,
})

Brief content:

Functions:

rspamd_tcp.request({params})

Methods:

tcp:close()

tcp:set_timeout(seconds)

tcp:add_read(callback, [pattern])

tcp:add_write(callback, data)

tcp:shift_callback()

Functions

The module rspamd_tcp defines the following functions.

Function 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 use
  • data: a table of strings or rspamd_text objects that contains data pieces
  • callback: continuation function (required)
  • on_connect: callback called on connection success
  • timeout: floating point value that specifies timeout for IO operations in seconds
  • partial: boolean flag that specifies that callback should be called on any data portion received
  • stop_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 sent

Back to module description.

Methods

The module rspamd_tcp defines the following methods.

Method tcp:close()

Closes TCP connection

Parameters:

No parameters

Returns:

No return

Back to module description.

Method tcp:set_timeout(seconds)

Sets new timeout for a TCP connection in seconds

Parameters:

  • seconds {number}: floating point value that specifies new timeout

Returns:

No return

Back to module description.

Method tcp:add_read(callback, [pattern])

Adds new read event to the tcp connection

Parameters:

  • callback {function}: to be called when data is read
  • pattern {string}: optional stop pattern

Returns:

No return

Back to module description.

Method tcp:add_write(callback, data)

Adds new write event to the tcp connection

Parameters:

  • optional {function}: callback to be called when data is completely written
  • data {table/string/text}: to send to a remote server

Returns:

No return

Back to module description.

Method 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.