struct rspamd_connection {
struct rspamd_server *server;
+ struct rspamd_client *client;
time_t connection_time;
gint socket;
struct rspamd_result *result;
gint version;
};
-static struct rspamd_client *client = NULL;
-
/** Util functions **/
gint
make_socket_nonblocking (gint fd)
}
static struct rspamd_connection *
-rspamd_connect_random_server (gboolean is_control, GError **err)
+rspamd_connect_random_server (struct rspamd_client *client, gboolean is_control, GError **err)
{
struct rspamd_server *selected = NULL;
struct rspamd_connection *new;
new = g_malloc (sizeof (struct rspamd_connection));
new->server = selected;
new->connection_time = now;
+ new->client = client;
/* Create socket */
new->socket = make_tcp_socket (&selected->addr,
is_control ? selected->controller_port : selected->client_port,
}
}
/* Poll socket */
- if ((r = poll_sync_socket (c->socket, client->read_timeout, POLL_IN)) <= 0) {
+ if ((r = poll_sync_socket (c->socket, c->client->read_timeout, POLL_IN)) <= 0) {
if (*err == NULL) {
if (r == 0) {
errno = ETIMEDOUT;
make_socket_nonblocking (c->socket);
/* Poll socket */
do {
- if ((r = poll_sync_socket (c->socket, client->read_timeout, POLL_IN)) <= 0) {
+ if ((r = poll_sync_socket (c->socket, c->client->read_timeout, POLL_IN)) <= 0) {
if (*err == NULL) {
if (r == 0) {
errno = ETIMEDOUT;
gint r;
static const gchar greeting_str[] = "Rspamd";
- if ((r = poll_sync_socket (c->socket, client->read_timeout, POLL_IN)) <= 0) {
+ if ((r = poll_sync_socket (c->socket, c->client->read_timeout, POLL_IN)) <= 0) {
if (*err == NULL) {
if (r == 0) {
errno = ETIMEDOUT;
/*
* Init rspamd client library
*/
-void
+struct rspamd_client*
rspamd_client_init (void)
{
- if (client != NULL) {
- rspamd_client_close ();
- }
+ struct rspamd_client *client;
+
client = g_malloc0 (sizeof (struct rspamd_client));
client->read_timeout = DEFAULT_READ_TIMEOUT;
client->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
+
+ return client;
}
/*
* Add rspamd server
*/
gboolean
-rspamd_add_server (const gchar *host, guint16 port, guint16 controller_port, GError **err)
+rspamd_add_server (struct rspamd_client *client, const gchar *host, guint16 port,
+ guint16 controller_port, GError **err)
{
struct rspamd_server *new;
struct hostent *hent;
* Set timeouts (values in milliseconds)
*/
void
-rspamd_set_timeout (guint connect_timeout, guint read_timeout)
+rspamd_set_timeout (struct rspamd_client *client, guint connect_timeout, guint read_timeout)
{
g_assert (client != NULL);
* Scan message from memory
*/
struct rspamd_result *
-rspamd_scan_memory (const guchar *message, gsize length, GHashTable *headers, GError **err)
+rspamd_scan_memory (struct rspamd_client *client, const guchar *message, gsize length, GHashTable *headers, GError **err)
{
struct rspamd_connection *c;
struct rspamd_result *res = NULL;
g_assert (length > 0);
/* Connect to server */
- c = rspamd_connect_random_server (FALSE, err);
+ c = rspamd_connect_random_server (client, FALSE, err);
if (c == NULL) {
return NULL;
* Scan message from file
*/
struct rspamd_result *
-rspamd_scan_file (const guchar *filename, GHashTable *headers, GError **err)
+rspamd_scan_file (struct rspamd_client *client, const guchar *filename, GHashTable *headers, GError **err)
{
gint fd;
g_assert (client != NULL);
return NULL;
}
- return rspamd_scan_fd (fd, headers, err);
+ return rspamd_scan_fd (client, fd, headers, err);
}
/*
* Scan message from fd
*/
struct rspamd_result *
-rspamd_scan_fd (int fd, GHashTable *headers, GError **err)
+rspamd_scan_fd (struct rspamd_client *client, int fd, GHashTable *headers, GError **err)
{
struct rspamd_connection *c;
struct rspamd_result *res = NULL;
g_assert (client != NULL);
/* Connect to server */
- c = rspamd_connect_random_server (FALSE, err);
+ c = rspamd_connect_random_server (client, FALSE, err);
if (c == NULL) {
return NULL;
* Learn message from memory
*/
gboolean
-rspamd_learn_memory (const guchar *message, gsize length, const gchar *symbol, const gchar *password, GError **err)
+rspamd_learn_memory (struct rspamd_client *client, const guchar *message, gsize length, const gchar *symbol, const gchar *password, GError **err)
{
struct rspamd_connection *c;
GString *in;
g_assert (length > 0);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return FALSE;
* Learn message from file
*/
gboolean
-rspamd_learn_file (const guchar *filename, const gchar *symbol, const gchar *password, GError **err)
+rspamd_learn_file (struct rspamd_client *client, const guchar *filename, const gchar *symbol, const gchar *password, GError **err)
{
gint fd;
g_assert (client != NULL);
return FALSE;
}
- return rspamd_learn_fd (fd, symbol, password, err);
+ return rspamd_learn_fd (client, fd, symbol, password, err);
}
/*
* Learn message from fd
*/
gboolean
-rspamd_learn_fd (int fd, const gchar *symbol, const gchar *password, GError **err)
+rspamd_learn_fd (struct rspamd_client *client, int fd, const gchar *symbol, const gchar *password, GError **err)
{
struct rspamd_connection *c;
GString *in;
g_assert (client != NULL);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return FALSE;
* Learn message from memory
*/
gboolean
-rspamd_learn_spam_memory (const guchar *message, gsize length, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err)
+rspamd_learn_spam_memory (struct rspamd_client *client, const guchar *message, gsize length, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err)
{
struct rspamd_connection *c;
GString *in;
g_assert (length > 0);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return FALSE;
* Learn message from file
*/
gboolean
-rspamd_learn_spam_file (const guchar *filename, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err)
+rspamd_learn_spam_file (struct rspamd_client *client, const guchar *filename, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err)
{
gint fd;
g_assert (client != NULL);
return FALSE;
}
- return rspamd_learn_spam_fd (fd, classifier, is_spam, password, err);
+ return rspamd_learn_spam_fd (client, fd, classifier, is_spam, password, err);
}
/*
* Learn message from fd
*/
gboolean
-rspamd_learn_spam_fd (int fd, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err)
+rspamd_learn_spam_fd (struct rspamd_client *client, int fd, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err)
{
struct rspamd_connection *c;
GString *in;
g_assert (client != NULL);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return FALSE;
* Learn message fuzzy from memory
*/
gboolean
-rspamd_fuzzy_memory (const guchar *message, gsize length, const gchar *password, gint weight, gint flag, gboolean delete, GError **err)
+rspamd_fuzzy_memory (struct rspamd_client *client, const guchar *message, gsize length, const gchar *password, gint weight, gint flag, gboolean delete, GError **err)
{
struct rspamd_connection *c;
GString *in;
g_assert (length > 0);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return FALSE;
* Learn message fuzzy from file
*/
gboolean
-rspamd_fuzzy_file (const guchar *filename, const gchar *password, gint weight, gint flag, gboolean delete, GError **err)
+rspamd_fuzzy_file (struct rspamd_client *client, const guchar *filename, const gchar *password, gint weight, gint flag, gboolean delete, GError **err)
{
gint fd;
g_assert (client != NULL);
return FALSE;
}
- return rspamd_fuzzy_fd (fd, password, weight, flag, delete, err);
+ return rspamd_fuzzy_fd (client, fd, password, weight, flag, delete, err);
}
/*
* Learn message fuzzy from fd
*/
gboolean
-rspamd_fuzzy_fd (int fd, const gchar *password, gint weight, gint flag, gboolean delete, GError **err)
+rspamd_fuzzy_fd (struct rspamd_client *client, int fd, const gchar *password, gint weight, gint flag, gboolean delete, GError **err)
{
struct rspamd_connection *c;
GString *in;
g_assert (client != NULL);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return FALSE;
}
GString *
-rspamd_get_stat (GError **err)
+rspamd_get_stat (struct rspamd_client *client, GError **err)
{
struct rspamd_connection *c;
GString *res;
g_assert (client != NULL);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return NULL;
}
GString *
-rspamd_get_uptime (GError **err)
+rspamd_get_uptime (struct rspamd_client *client, GError **err)
{
struct rspamd_connection *c;
GString *res;
g_assert (client != NULL);
/* Connect to server */
- c = rspamd_connect_random_server (TRUE, err);
+ c = rspamd_connect_random_server (client, TRUE, err);
if (c == NULL) {
return NULL;
void
rspamd_free_result (struct rspamd_result *result)
{
- g_assert (client != NULL);
g_assert (result != NULL);
g_hash_table_destroy (result->headers);
* Close library and free associated resources
*/
void
-rspamd_client_close (void)
+rspamd_client_close (struct rspamd_client *client)
{
- g_assert (client != NULL);
-
g_free (client);
- client = NULL;
}
};
struct rspamd_connection;
+struct rspamd_client;
/**
* Result of scan
*/
/*
* Init rspamd client library
*/
-void rspamd_client_init (void);
+struct rspamd_client* rspamd_client_init (void);
/*
* Add rspamd server
*/
-gboolean rspamd_add_server (const gchar *host, guint16 port, guint16 controller_port, GError **err);
+gboolean rspamd_add_server (struct rspamd_client* client, const gchar *host,
+ guint16 port, guint16 controller_port, GError **err);
/*
* Set timeouts (values in milliseconds)
*/
-void rspamd_set_timeout (guint connect_timeout, guint read_timeout);
+void rspamd_set_timeout (struct rspamd_client* client, guint connect_timeout, guint read_timeout);
/*
* Scan message from memory
*/
-struct rspamd_result * rspamd_scan_memory (const guchar *message, gsize length, GHashTable *headers, GError **err);
+struct rspamd_result * rspamd_scan_memory (struct rspamd_client* client, const guchar *message, gsize length, GHashTable *headers, GError **err);
/*
* Scan message from file
*/
-struct rspamd_result * rspamd_scan_file (const guchar *filename, GHashTable *headers, GError **err);
+struct rspamd_result * rspamd_scan_file (struct rspamd_client* client, const guchar *filename, GHashTable *headers, GError **err);
/*
* Scan message from fd
*/
-struct rspamd_result * rspamd_scan_fd (int fd, GHashTable *headers, GError **err);
+struct rspamd_result * rspamd_scan_fd (struct rspamd_client* client, int fd, GHashTable *headers, GError **err);
/*
* Learn message from memory
*/
-gboolean rspamd_learn_spam_memory (const guchar *message, gsize length, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err);
+gboolean rspamd_learn_spam_memory (struct rspamd_client* client, const guchar *message, gsize length, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err);
/*
* Learn message from file
*/
-gboolean rspamd_learn_spam_file (const guchar *filename, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err);
+gboolean rspamd_learn_spam_file (struct rspamd_client* client, const guchar *filename, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err);
/*
* Learn message from fd
*/
-gboolean rspamd_learn_spam_fd (int fd, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err);
+gboolean rspamd_learn_spam_fd (struct rspamd_client* client, int fd, const gchar *classifier, gboolean is_spam, const gchar *password, GError **err);
/*
* Learn message from memory
*/
-gboolean rspamd_learn_memory (const guchar *message, gsize length, const gchar *symbol, const gchar *password, GError **err);
+gboolean rspamd_learn_memory (struct rspamd_client* client, const guchar *message, gsize length, const gchar *symbol, const gchar *password, GError **err);
/*
* Learn message from file
*/
-gboolean rspamd_learn_file (const guchar *filename, const gchar *symbol, const gchar *password, GError **err);
+gboolean rspamd_learn_file (struct rspamd_client* client, const guchar *filename, const gchar *symbol, const gchar *password, GError **err);
/*
* Learn message from fd
*/
-gboolean rspamd_learn_fd (int fd, const gchar *symbol, const gchar *password, GError **err);
+gboolean rspamd_learn_fd (struct rspamd_client* client, int fd, const gchar *symbol, const gchar *password, GError **err);
/*
* Learn message fuzzy from memory
*/
-gboolean rspamd_fuzzy_memory (const guchar *message, gsize length, const gchar *password, gint weight, gint flag, gboolean delete, GError **err);
+gboolean rspamd_fuzzy_memory (struct rspamd_client* client, const guchar *message, gsize length, const gchar *password, gint weight, gint flag, gboolean delete, GError **err);
/*
* Learn message fuzzy from file
*/
-gboolean rspamd_fuzzy_file (const guchar *filename, const gchar *password, gint weight, gint flag, gboolean delete, GError **err);
+gboolean rspamd_fuzzy_file (struct rspamd_client* client, const guchar *filename, const gchar *password, gint weight, gint flag, gboolean delete, GError **err);
/*
* Learn message fuzzy from fd
*/
-gboolean rspamd_fuzzy_fd (int fd, const gchar *password, gint weight, gint flag, gboolean delete, GError **err);
+gboolean rspamd_fuzzy_fd (struct rspamd_client* client, int fd, const gchar *password, gint weight, gint flag, gboolean delete, GError **err);
/*
* Get statistic from server
*/
-GString *rspamd_get_stat (GError **err);
+GString *rspamd_get_stat (struct rspamd_client* client, GError **err);
/*
* Get uptime from server
*/
-GString *rspamd_get_uptime (GError **err);
+GString *rspamd_get_uptime (struct rspamd_client* client, GError **err);
/*
* Free results
/*
* Close library and free associated resources
*/
-void rspamd_client_close (void);
+void rspamd_client_close (struct rspamd_client *client);
#endif
static gboolean pass_all;
static gboolean tty = FALSE;
static gboolean verbose = FALSE;
+static struct rspamd_client *client = NULL;
static GOptionEntry entries[] =
{
}
}
- if (! rspamd_add_server (vec[0], port, port, &err)) {
+ if (! rspamd_add_server (client, vec[0], port, port, &err)) {
fprintf (stderr, "cannot connect to rspamd server: %s, error: %s\n", connect_str, err->message);
exit (EXIT_FAILURE);
}
in_buf = g_realloc (in_buf, len);
}
}
- res = rspamd_scan_memory (in_buf, r, opts, &err);
+ res = rspamd_scan_memory (client, in_buf, r, opts, &err);
g_hash_table_destroy (opts);
if (err != NULL) {
fprintf (stderr, "cannot scan message: %s\n", err->message);
/* Init options hash */
opts = g_hash_table_new (g_str_hash, g_str_equal);
add_options (opts);
- res = rspamd_scan_file (file, opts, &err);
+ res = rspamd_scan_file (client, file, opts, &err);
g_hash_table_destroy (opts);
if (err != NULL) {
fprintf (stderr, "cannot scan message: %s\n", err->message);
}
}
if (statfile != NULL) {
- if (!rspamd_learn_memory (in_buf, r, statfile, password, &err)) {
+ if (!rspamd_learn_memory (client, in_buf, r, statfile, password, &err)) {
if (err != NULL) {
fprintf (stderr, "cannot learn message: %s\n", err->message);
}
}
}
else if (classifier != NULL) {
- if (!rspamd_learn_spam_memory (in_buf, r, classifier, is_spam, password, &err)) {
+ if (!rspamd_learn_spam_memory (client, in_buf, r, classifier, is_spam, password, &err)) {
if (err != NULL) {
fprintf (stderr, "cannot learn message: %s\n", err->message);
}
}
if (statfile != NULL) {
- if (!rspamd_learn_file (file, statfile, password, &err)) {
+ if (!rspamd_learn_file (client, file, statfile, password, &err)) {
if (err != NULL) {
fprintf (stderr, "cannot learn message: %s\n", err->message);
}
}
}
else if (classifier != NULL) {
- if (!rspamd_learn_spam_file (file, classifier, is_spam, password, &err)) {
+ if (!rspamd_learn_spam_file (client, file, classifier, is_spam, password, &err)) {
if (err != NULL) {
fprintf (stderr, "cannot learn message: %s\n", err->message);
}
in_buf = g_realloc (in_buf, len);
}
}
- if (!rspamd_fuzzy_memory (in_buf, r, password, weight, flag, delete, &err)) {
+ if (!rspamd_fuzzy_memory (client, in_buf, r, password, weight, flag, delete, &err)) {
if (err != NULL) {
fprintf (stderr, "cannot learn message: %s\n", err->message);
}
exit (EXIT_FAILURE);
}
- if (!rspamd_fuzzy_file (file, password, weight, flag, delete, &err)) {
+ if (!rspamd_fuzzy_file (client, file, password, weight, flag, delete, &err)) {
if (err != NULL) {
fprintf (stderr, "cannot learn message: %s\n", err->message);
}
/* Add server */
add_rspamd_server (TRUE);
- res = rspamd_get_stat (&err);
+ res = rspamd_get_stat (client, &err);
if (res == NULL) {
if (err != NULL) {
fprintf (stderr, "cannot stat: %s\n", err->message);
/* Add server */
add_rspamd_server (TRUE);
- res = rspamd_get_uptime (&err);
+ res = rspamd_get_uptime (client, &err);
if (res == NULL) {
if (err != NULL) {
fprintf (stderr, "cannot uptime: %s\n", err->message);
enum rspamc_command cmd;
gint i;
- rspamd_client_init ();
+ client = rspamd_client_init ();
read_cmd_line (&argc, &argv);
- rspamd_set_timeout (1000, timeout * 1000);
+ rspamd_set_timeout (client, 1000, timeout * 1000);
tty = isatty (STDOUT_FILENO);
/* Now read other args from argc and argv */
if (argc == 1) {
}
}
- rspamd_client_close ();
+ rspamd_client_close (client);
return 0;
}