Skip to content

Commit

Permalink
lib-smtp: server: recipient: Add reference counting.
Browse files Browse the repository at this point in the history
Unlike the server command, this is not strictly necessary for the recipient
object, but we add this anyway to prevent future problems when the recipient
implementation becomes more complex.
  • Loading branch information
stephanbosch authored and villesavolainen committed Feb 12, 2019
1 parent 43fa7cd commit ce9373c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/lib-smtp/smtp-server-private.h
Expand Up @@ -113,6 +113,7 @@ struct smtp_server_command {

struct smtp_server_recipient_private {
struct smtp_server_recipient rcpt;
int refcount;

struct smtp_server_recipient_hook *hooks_head, *hooks_tail;
};
Expand Down Expand Up @@ -374,6 +375,8 @@ void smtp_server_connection_set_proxy_data(struct smtp_server_connection *conn,
struct smtp_server_recipient *
smtp_server_recipient_create(struct smtp_server_cmd_ctx *cmd,
const struct smtp_address *rcpt_to);
void smtp_server_recipient_ref(struct smtp_server_recipient *rcpt);
bool smtp_server_recipient_unref(struct smtp_server_recipient **_rcpt);
void smtp_server_recipient_destroy(struct smtp_server_recipient **_rcpt);

void smtp_server_recipient_approved(struct smtp_server_recipient *rcpt);
Expand Down
26 changes: 24 additions & 2 deletions src/lib-smtp/smtp-server-recipient.c
Expand Up @@ -19,6 +19,7 @@ smtp_server_recipient_create(struct smtp_server_cmd_ctx *cmd,

pool = pool_alloconly_create("smtp server recipient", 512);
prcpt = p_new(pool, struct smtp_server_recipient_private, 1);
prcpt->refcount = 1;
prcpt->rcpt.pool = pool;
prcpt->rcpt.conn = cmd->conn;
prcpt->rcpt.cmd = cmd;
Expand All @@ -27,19 +28,40 @@ smtp_server_recipient_create(struct smtp_server_cmd_ctx *cmd,
return &prcpt->rcpt;
}

void smtp_server_recipient_destroy(struct smtp_server_recipient **_rcpt)
void smtp_server_recipient_ref(struct smtp_server_recipient *rcpt)
{
struct smtp_server_recipient_private *prcpt =
(struct smtp_server_recipient_private *)rcpt;

i_assert(prcpt->refcount > 0);
prcpt->refcount++;
}

bool smtp_server_recipient_unref(struct smtp_server_recipient **_rcpt)
{
struct smtp_server_recipient *rcpt = *_rcpt;
struct smtp_server_recipient_private *prcpt =
(struct smtp_server_recipient_private *)rcpt;

*_rcpt = NULL;

if (rcpt == NULL)
return;
return FALSE;

i_assert(prcpt->refcount > 0);
if (--prcpt->refcount > 0)
return TRUE;

smtp_server_recipient_call_hooks(
rcpt, SMTP_SERVER_RECIPIENT_HOOK_DESTROY);

pool_unref(&rcpt->pool);
return FALSE;
}

void smtp_server_recipient_destroy(struct smtp_server_recipient **_rcpt)
{
smtp_server_recipient_unref(_rcpt);
}

void smtp_server_recipient_approved(struct smtp_server_recipient *rcpt)
Expand Down

0 comments on commit ce9373c

Please sign in to comment.