]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-smtp: server: recipient: Add reference counting.
authorStephan Bosch <stephan.bosch@dovecot.fi>
Thu, 1 Nov 2018 00:25:10 +0000 (01:25 +0100)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Tue, 12 Feb 2019 13:41:49 +0000 (15:41 +0200)
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.

src/lib-smtp/smtp-server-private.h
src/lib-smtp/smtp-server-recipient.c

index cc75320d4ecb9588cfdd58e63ae3875abc894740..8181a41fb8841ba63bfea5fc05fc98a0acbc62d4 100644 (file)
@@ -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;
 };
@@ -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);
index 6d99e603ecf7e36e7de2472f0d3eab1b53860bbe..76080149d3dad405659b44899f26131638380585 100644 (file)
@@ -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;
@@ -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)