Skip to content

Commit

Permalink
lib-smtp: server: Add APIs for halting and resuming connection comman…
Browse files Browse the repository at this point in the history
…d handling.

Also adds function to start the connection in a pending (halted) state. This way the greeting can already be sent (over SSL if needed), while deferring command handling until some external activity is completed.
  • Loading branch information
stephanbosch authored and villesavolainen committed Feb 9, 2018
1 parent 52bf7c5 commit 8eb9c89
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/lib-smtp/smtp-server-connection.c
Expand Up @@ -552,7 +552,13 @@ static void smtp_server_connection_input(struct connection *_conn)
"SSL Initialization failed");
return;
}
if (conn->halted) {
smtp_server_connection_input_lock(conn);
return;
}
}
i_assert(!conn->halted);


if (conn->command_queue_count >
conn->server->set.max_pipelined_commands) {
Expand Down Expand Up @@ -937,7 +943,7 @@ smtp_server_connection_create(struct smtp_server *server,
conn->set.capabilities &= ~SMTP_CAPABILITY_STARTTLS;

/* halt input until started */
smtp_server_connection_input_halt(conn);
smtp_server_connection_halt(conn);

smtp_server_connection_debug(conn, "Connection created");

Expand Down Expand Up @@ -969,7 +975,7 @@ smtp_server_connection_create_from_streams(struct smtp_server *server,
conn->created_from_streams = TRUE;

/* halt input until started */
smtp_server_connection_input_halt(conn);
smtp_server_connection_halt(conn);

smtp_server_connection_debug(conn, "Connection created");

Expand Down Expand Up @@ -1144,7 +1150,7 @@ void smtp_server_connection_login(struct smtp_server_connection *conn,
}
}

void smtp_server_connection_start(struct smtp_server_connection *conn)
void smtp_server_connection_start_pending(struct smtp_server_connection *conn)
{
i_assert(!conn->started);
conn->started = TRUE;
Expand All @@ -1154,9 +1160,29 @@ void smtp_server_connection_start(struct smtp_server_connection *conn)

if (!conn->ssl_start)
smtp_server_connection_ready(conn);
else if (conn->ssl_iostream == NULL)
smtp_server_connection_input_unlock(conn);
}

smtp_server_connection_timeout_start(conn);
smtp_server_connection_input_resume(conn);
void smtp_server_connection_start(struct smtp_server_connection *conn)
{
smtp_server_connection_start_pending(conn);
smtp_server_connection_resume(conn);
}

void smtp_server_connection_halt(struct smtp_server_connection *conn)
{
conn->halted = TRUE;
smtp_server_connection_timeout_stop(conn);
if (!conn->started || !conn->ssl_start || conn->ssl_iostream != NULL)
smtp_server_connection_input_lock(conn);
}

void smtp_server_connection_resume(struct smtp_server_connection *conn)
{
smtp_server_connection_input_unlock(conn);
smtp_server_connection_timeout_update(conn);
conn->halted = FALSE;
}

void smtp_server_connection_close(struct smtp_server_connection **_conn,
Expand Down
1 change: 1 addition & 0 deletions src/lib-smtp/smtp-server-private.h
Expand Up @@ -150,6 +150,7 @@ struct smtp_server_connection {
struct smtp_server_stats stats;

bool started:1;
bool halted:1;
bool ssl_start:1;
bool ssl_secured:1;
bool authenticated:1;
Expand Down
9 changes: 9 additions & 0 deletions src/lib-smtp/smtp-server.h
Expand Up @@ -304,6 +304,15 @@ void smtp_server_connection_login(struct smtp_server_connection *conn,
/* Start the connection. Establishes SSL layer immediately if instructed,
and sends the greeting once the connection is ready for commands. */
void smtp_server_connection_start(struct smtp_server_connection *conn);
/* Start the connection, but only establish SSL layer and send greeting;
handling command input is held off until smtp_server_connection_resume() is
called. */
void smtp_server_connection_start_pending(struct smtp_server_connection *conn);

/* Halt connection command input and idle timeout entirely. */
void smtp_server_connection_halt(struct smtp_server_connection *conn);
/* Resume connection command input and idle timeout. */
void smtp_server_connection_resume(struct smtp_server_connection *conn);

void smtp_server_connection_input_lock(struct smtp_server_connection *conn);
void smtp_server_connection_input_unlock(struct smtp_server_connection *conn);
Expand Down

0 comments on commit 8eb9c89

Please sign in to comment.