director-connection.c revision 5f5870385cff47efd2f58e7892f251cf13761528
/* Copyright (c) 2010-2012 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "network.h"
#include "istream.h"
#include "ostream.h"
#include "str.h"
#include "llist.h"
#include "master-service.h"
#include "mail-host.h"
#include "director.h"
#include "director-host.h"
#include "director-request.h"
#include "user-directory.h"
#include "director-connection.h"
#include <stdlib.h>
#include <unistd.h>
#define DIRECTOR_VERSION_NAME "director"
#define DIRECTOR_VERSION_MAJOR 1
#define DIRECTOR_VERSION_MINOR 0
#define MAX_INBUF_SIZE 1024
/* Max idling time while connecting/handshaking before disconnecting */
/* How long to wait for PONG after PING request */
/* How long to wait to send PING when connection is idle */
/* How long to wait before sending PING while waiting for SYNC reply */
#define DIRECTOR_CONNECTION_SYNC_TIMEOUT_MSECS 1000
/* If outgoing director connection exists for less than this many seconds,
mark the host as failed so we won't try to reconnect to it immediately */
#define DIRECTOR_SUCCESS_MIN_CONNECT_SECS 10
struct director_connection {
char *name;
/* for incoming connections the director host isn't known until
ME-line is received */
struct director_host *host;
int fd;
struct user_directory_iter *user_iter;
unsigned int in:1;
unsigned int connected:1;
unsigned int version_received:1;
unsigned int me_received:1;
unsigned int handshake_received:1;
unsigned int ignore_host_events:1;
unsigned int handshake_sending_hosts:1;
unsigned int ping_waiting:1;
unsigned int sync_ping:1;
};
static bool
const char *const *args,
{
i_error("director(%s): Command has invalid IP address: %s",
return FALSE;
}
i_error("director(%s): Command has invalid port: %s",
return FALSE;
}
return TRUE;
}
const char *const *args)
{
struct director_host *host;
const char *connect_str;
unsigned int port;
return FALSE;
i_error("Remote director thinks it's someone else "
"(connected to %s:%u, remote says it's %s:%u)",
return FALSE;
}
/* the host is up now, make sure we can connect to it immediately
if needed */
host->last_failed = 0;
return TRUE;
/* make sure we don't keep old sequence values across restarts */
/* make sure this is the correct incoming connection */
/* probably we're trying to find our own ip. it's no */
i_error("Connection from self, dropping");
return FALSE;
/* no conflicts yet */
i_warning("Dropping existing connection %s "
"in favor of its new connection %s",
} else {
/* the old connection is the correct one.
refer the client there. */
i_warning("Director connection %s tried to connect to "
"us, should use %s instead",
"CONNECT\t%s\t%u\n",
/* also make sure that the connection is alive */
return FALSE;
}
/* this new connection is the correct one. disconnect the old
one, but before that tell it to connect to the new one.
that message might not reach it, so also send the same
message to right side. */
i_warning("Replacing director connection %s with %s",
}
/* tell the ring's right side to connect to this new director. */
else {
/* there are only two directors, and we already have
a connection to this server. */
}
} else {
/* there are only two directors. connect to the other one. */
}
return TRUE;
}
static bool
{
return TRUE;
}
}
i_error("User hash %u is being redirected to two hosts: "
"%s and %s", username_hash,
/* we want all the directors to redirect the user to same
server, but we don't want two directors fighting over which
server it belongs to, so always use the lower IP address */
/* change the host. we'll also need to remove the user
from the old host's user_count, because we can't
keep track of the user for more than one host */
}
}
return ret;
}
static bool
const char *const *args)
{
unsigned int username_hash, timestamp;
i_error("director(%s): Invalid USER handshake args",
return FALSE;
}
i_error("director(%s): USER used unknown host %s in handshake",
return FALSE;
}
return TRUE;
}
static bool
{
unsigned int username_hash;
return FALSE;
}
/* we probably just removed this host. */
return TRUE;
}
return TRUE;
}
const char *const *args)
{
struct director_host *host;
unsigned int port;
return FALSE;
/* already have this. just reset its last_failed timestamp,
since it might be up now. */
host->last_failed = 0;
return TRUE;
}
/* save the director and forward it */
return TRUE;
}
static bool
const char *const *args)
{
unsigned int remote_ring_completed;
i_error("director(%s): Invalid HOST-HAND-START args",
return FALSE;
}
/* clear everything we have and use only what remote sends us */
while (array_count(hosts) > 0) {
}
/* ignore whatever remote sends */
}
return TRUE;
}
static int
const char *const **_args,
struct director_host **host_r)
{
struct director_host *host;
i_error("director(%s): Command is missing parameters: %s",
return -1;
}
/* director is already gone, but we can't be sure if this
command was sent everywhere. re-send it as if it was from
ourself. */
} else {
/* already seen this */
return 1;
}
}
return 0;
}
static bool
struct director_host *dir_host)
{
unsigned int vhost_count;
bool update;
return FALSE;
}
if (conn->ignore_host_events) {
/* remote is sending hosts in a handshake, but it doesn't have
a completed ring and we do. */
return TRUE;
}
} else {
}
if (update) {
host, vhost_count);
}
return TRUE;
}
static bool
const char *const *args)
{
}
static bool
{
struct director_host *dir_host;
int ret;
return ret > 0;
}
static bool
const char *const *args)
{
struct director_host *dir_host;
int ret;
return ret > 0;
return FALSE;
}
return TRUE;
}
static bool
const char *const *args)
{
struct director_host *dir_host;
int ret;
return ret > 0;
return FALSE;
}
return TRUE;
}
static bool
const char *const *args)
{
struct director_host *dir_host;
unsigned int username_hash;
int ret;
return ret > 0;
return FALSE;
}
}
return TRUE;
}
static bool
const char *const *args)
{
unsigned int username_hash;
return FALSE;
}
return TRUE;
}
static bool
const char *const *args)
{
struct director_host *dir_host;
unsigned int username_hash;
int ret;
return ret > 0;
i_error("director(%s): Invalid USER-KILLED-EVERYWHERE args",
return FALSE;
}
return TRUE;
}
{
/* handshaked to left side. tell it we've received the
whole handshake. */
/* tell the right director about the left one */
t_strdup_printf("DIRECTOR\t%s\t%u\n",
}
}
/* we're connected to both directors. see if the ring is
finished by sending a SYNC. if we get it back, it's done. */
t_strdup_printf("SYNC\t%s\t%u\t%u\n",
}
}
static bool
{
struct director_host *host;
unsigned int port;
/* both incoming and outgoing connections get VERSION and ME */
i_error("director(%s): Wrong protocol in socket "
"(%s vs %s)",
return FALSE;
i_error("director(%s): Incompatible protocol version: "
return FALSE;
}
return TRUE;
}
if (!conn->version_received) {
return FALSE;
}
/* only outgoing connections get a CONNECT reference */
/* remote wants us to connect elsewhere */
return FALSE;
/* reset failure timestamp so we'll actually try to
connect there. */
host->last_failed = 0;
return FALSE;
}
/* only incoming connections get DIRECTOR and HOST lists */
/* allow hosts from all connections always,
this could be an host update */
if (conn->handshake_sending_hosts)
else
}
if (conn->handshake_sending_hosts &&
return TRUE;
}
/* only incoming connections get a full USER list, but outgoing
connections can also receive USER updates during handshake and
it wouldn't be safe to ignore them. */
else
}
/* both get DONE */
return TRUE;
}
i_error("director(%s): Invalid handshake command: %s "
return FALSE;
}
static void
struct director_host *host,
{
/* stale SYNC event */
return;
}
if (!dir->ring_handshaked) {
/* the ring is handshaked */
} else if (dir->ring_synced) {
/* duplicate SYNC (which was sent just in case the
previous one got lost) */
} else {
i_debug("Ring is synced (%s sent seq=%u)",
}
}
} else {
/* forward it to the connection on right */
}
}
}
{
struct director_host *host;
return FALSE;
}
/* find the originating director. if we don't see it, it was already
removed and we can ignore this sync. */
return TRUE;
}
const char *const *args)
{
struct director_host *host;
unsigned int port;
return FALSE;
}
i_error("Received CONNECT request to unknown host %s:%u",
return TRUE;
}
/* remote suggests us to connect elsewhere */
/* the old connection is the correct one */
i_debug("Ignoring CONNECT request to %s "
"(current right is %s)",
}
return TRUE;
}
i_debug("Received CONNECT request to %s, "
} else {
i_debug("Received CONNECT request to %s, "
"replacing current right %s",
}
}
/* connect here */
return TRUE;
}
{
if (!conn->ping_waiting)
return TRUE;
return TRUE;
}
static bool
const char *line)
{
return FALSE;
}
return TRUE;
}
return director_cmd_pong(conn);
if (!conn->handshake_received) {
/* invalid commands during handshake,
we probably don't want to reconnect here */
i_debug("director(%s): Handshaking failed",
}
return FALSE;
}
return TRUE;
}
i_error("director(%s): Unknown command (in this state): %s",
return FALSE;
}
{
char *line;
bool ret;
case 0:
return;
case -1:
/* disconnected */
" before handshake finished");
return;
case -2:
/* buffer full */
i_error("BUG: Director %s sent us more than %d bytes",
return;
}
T_BEGIN {
} T_END;
if (!ret) {
i_debug("director(%s): Invalid input, disconnecting",
}
break;
}
}
}
{
struct director_host *const *hostp;
}
}
static void
{
}
}
{
int ret;
user)) {
/* user is already expired */
continue;
}
T_BEGIN {
const char *line;
} T_END;
/* continue later */
return ret;
}
}
}
return ret;
}
{
return director_connection_send_users(conn);
else
}
static void
{
else
}
static struct director_connection *
{
struct director_connection *conn;
return conn;
}
{
"ME\t%s\t%u\n",
}
struct director_connection *
{
struct director_connection *conn;
return conn;
}
{
int err;
return;
}
/* see if we should disconnect or keep the existing
connection. */
/* the old connection is the correct one */
i_warning("Aborting incorrect outgoing connection to %s "
"(already connected to correct one: %s)",
return;
}
i_warning("Replacing director connection %s with %s",
}
(void)director_connection_send_users(conn);
}
struct director_connection *
struct director_host *host)
{
struct director_connection *conn;
/* make sure we don't keep old sequence values across restarts */
/* use IO_READ instead of IO_WRITE, so that we don't assign
dir->right until remote has actually sent some data */
return conn;
}
{
i_error("close(director connection) failed: %m");
/* we aren't synced until we're again connected to a ring */
}
}
{
}
{
}
const char *data)
{
return;
if (ret < 0)
else {
i_error("director(%s): Output buffer full, "
}
}
}
struct director_host *skip_host,
const char *data)
{
}
{
}
{
if (conn->ping_waiting)
return;
}
{
}
struct director_host *
{
}
struct director_connection *
struct director_host *host)
{
struct director_connection *conn;
return conn;
}
return NULL;
}
{
}
{
}
{
/* switch to faster ping timeout. avoid reseting the timeout if it's
already fast. */
return;
}
{
struct director_connection *conn;
}
}