}
}
/* Poll socket */
- if (poll_sync_socket (c->socket, client->read_timeout, POLL_IN) == -1) {
+ if ((r = poll_sync_socket (c->socket, client->read_timeout, POLL_IN)) <= 0) {
if (*err == NULL) {
- *err = g_error_new (G_RSPAMD_ERROR, errno, "Could not connect to server %s: %s",
+ if (r == 0) {
+ errno = ETIMEDOUT;
+ }
+ *err = g_error_new (G_RSPAMD_ERROR, errno, "Cannot read reply from controller %s: %s",
c->server->name, strerror (errno));
}
upstream_fail (&c->server->up, c->connection_time);
make_socket_nonblocking (c->socket);
/* Poll socket */
do {
- if (poll_sync_socket (c->socket, client->read_timeout, POLL_IN) == -1) {
+ if ((r = poll_sync_socket (c->socket, client->read_timeout, POLL_IN)) <= 0) {
if (*err == NULL) {
+ if (r == 0) {
+ errno = ETIMEDOUT;
+ }
*err = g_error_new (G_RSPAMD_ERROR, errno, "Cannot read reply from controller %s: %s",
c->server->name, strerror (errno));
}
gint r;
static const gchar greeting_str[] = "Rspamd";
- if (poll_sync_socket (c->socket, client->read_timeout, POLL_IN) == -1) {
+ if ((r = poll_sync_socket (c->socket, client->read_timeout, POLL_IN)) <= 0) {
if (*err == NULL) {
+ if (r == 0) {
+ errno = ETIMEDOUT;
+ }
*err = g_error_new (G_RSPAMD_ERROR, errno, "Cannot read reply from controller %s: %s",
c->server->name, strerror (errno));
}
d->in_buf->pos += r;
d->in_buf->data->len += r;
}
- debug_ip("read %z characters, policy is %s, watermark is: %z", r,
- d->policy == BUFFER_LINE ? "LINE" : "CHARACTER", d->nchars);
+ debug_ip("read %z characters, policy is %s, watermark is: %z, buffer has %z bytes", r,
+ d->policy == BUFFER_LINE ? "LINE" : "CHARACTER", d->nchars, d->in_buf->data->len);
}
saved_policy = d->policy;
free_task (task, FALSE);
i = rspamd_snprintf (out_buf, sizeof (out_buf), "learn ok" CRLF END);
+ session->state = STATE_REPLY;
if (!rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
return FALSE;
}
- session->state = STATE_REPLY;
break;
case STATE_LEARN_SPAM_PRE:
session->learn_buf = in;
}
}
else if (r == 0) {
- session->state = STATE_LEARN;
+ session->state = STATE_LEARN_SPAM;
task->dispatcher = session->dispatcher;
session->learn_task = task;
rspamd_dispatcher_pause (session->dispatcher);
}
if (session->state == STATE_REPLY || session->state == STATE_QUIT) {
- return controller_write_socket (session);
+ rspamd_dispatcher_restore (session->dispatcher);
}
return TRUE;
destroy_session (session->s);
return FALSE;
}
- else if (session->state == STATE_LEARN) {
+ else if (session->state == STATE_LEARN_SPAM) {
/* Perform actual learn here */
if (! learn_task_spam (session->learn_classifier, session->learn_task, session->in_class, &err)) {
if (err) {
if (!rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) {
return FALSE;
}
+ return TRUE;
}
else if (session->state == STATE_REPLY) {
session->state = STATE_COMMAND;
accept_socket (gint fd, short what, void *arg)
{
struct rspamd_worker *worker = (struct rspamd_worker *)arg;
- struct sockaddr_storage ss;
+ union sa_union su;
struct controller_session *new_session;
struct timeval *io_tv;
- socklen_t addrlen = sizeof (ss);
+ socklen_t addrlen = sizeof (su.ss);
gint nfd;
struct rspamd_controller_ctx *ctx;
ctx = worker->ctx;
- if ((nfd = accept_from_socket (fd, (struct sockaddr *)&ss, &addrlen)) == -1) {
+ if ((nfd = accept_from_socket (fd, (struct sockaddr *)&su.ss, &addrlen)) == -1) {
return;
}
new_session->s = new_async_session (new_session->session_pool, free_session, new_session);
new_session->dispatcher = rspamd_create_dispatcher (nfd, BUFFER_LINE, controller_read_socket, controller_write_socket, controller_err_socket, io_tv, (void *)new_session);
+ if (su.ss.ss_family == AF_UNIX) {
+ msg_info ("accepted connection from unix socket");
+ new_session->dispatcher->peer_addr = INADDR_LOOPBACK;
+ }
+ else if (su.ss.ss_family == AF_INET) {
+ msg_info ("accepted connection from %s port %d",
+ inet_ntoa (su.s4.sin_addr), ntohs (su.s4.sin_port));
+ memcpy (&new_session->dispatcher->peer_addr, &su.s4.sin_addr,
+ sizeof (guint32));
+ }
if (! rspamd_dispatcher_write (new_session->dispatcher, greetingbuf, strlen (greetingbuf), FALSE, FALSE)) {
msg_warn ("cannot write greeting");
}