]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added my_hostdomain() function which mbox code now uses instead of doing that internally.
authorTimo Sirainen <tss@iki.fi>
Wed, 7 Jan 2009 18:45:37 +0000 (13:45 -0500)
committerTimo Sirainen <tss@iki.fi>
Wed, 7 Jan 2009 18:45:37 +0000 (13:45 -0500)
Based on patch by Apple.

--HG--
branch : HEAD

src/lib-storage/index/mbox/mbox-save.c
src/lib/hostpid.c
src/lib/hostpid.h

index 5c7678ccccd9d5a95202837cee2ede33cf20b18d..226d66f83e436dd3377742a0850654ef39f8e986 100644 (file)
@@ -27,7 +27,6 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
-#include <netdb.h>
 #include <utime.h>
 
 #define MBOX_DELIVERY_ID_RAND_BYTES (64/8)
@@ -58,8 +57,6 @@ struct mbox_save_context {
        unsigned int finished:1;
 };
 
-static char my_hostdomain[256] = "";
-
 static int write_error(struct mbox_save_context *ctx)
 {
        mbox_set_syscall_error(ctx->mbox, "write()");
@@ -114,23 +111,8 @@ static int mbox_append_lf(struct mbox_save_context *ctx)
 static int write_from_line(struct mbox_save_context *ctx, time_t received_date,
                           const char *from_envelope)
 {
-       const char *name;
        int ret;
 
-       if (*my_hostdomain == '\0') {
-               struct hostent *hent;
-
-               hent = gethostbyname(my_hostname);
-
-               name = hent != NULL ? hent->h_name : NULL;
-               if (name == NULL) {
-                       /* failed, use just the hostname */
-                       name = my_hostname;
-               }
-
-               i_strocpy(my_hostdomain, name, sizeof(my_hostdomain));
-       }
-
        T_BEGIN {
                const char *line;
 
@@ -139,7 +121,7 @@ static int write_from_line(struct mbox_save_context *ctx, time_t received_date,
                                &ctx->mbox->storage->storage;
 
                        from_envelope = t_strconcat(storage->ns->user->username,
-                                                   "@", my_hostdomain, NULL);
+                                                   "@", my_hostdomain(), NULL);
                }
 
                /* save in local timezone, no matter what it was given with */
index 1845d7d315360fd1e05796bb28ca73f4a8c49a07..c616fd5cd87bcbaedfa173822a1789ace0b2d038 100644 (file)
@@ -4,10 +4,13 @@
 #include "hostpid.h"
 
 #include <unistd.h>
+#include <netdb.h>
 
 const char *my_hostname = NULL;
 const char *my_pid = NULL;
 
+static char *my_domain = NULL;
+
 void hostpid_init(void)
 {
        static char hostname[256], pid[MAX_INT_STRLEN];
@@ -17,6 +20,26 @@ void hostpid_init(void)
        hostname[sizeof(hostname)-1] = '\0';
        my_hostname = hostname;
 
+       /* allow calling hostpid_init() multiple times to reset hostname */
+       i_free_and_null(my_domain);
+
        i_strocpy(pid, dec2str(getpid()), sizeof(pid));
        my_pid = pid;
 }
+
+const char *my_hostdomain(void)
+{
+       struct hostent *hent;
+       const char *name;
+
+       if (my_domain == NULL) {
+               hent = gethostbyname(my_hostname);
+               name = hent != NULL ? hent->h_name : NULL;
+               if (name == NULL) {
+                       /* failed, use just the hostname */
+                       name = my_hostname;
+               }
+               my_domain = i_strdup(name);
+       }
+       return my_domain;
+}
index f5eb2987b54d800f1e3de0441eebcf4599edfbea..d6ccffeea8ce8d824faa1222df00e221594715bb 100644 (file)
@@ -4,9 +4,12 @@
 extern const char *my_hostname;
 extern const char *my_pid;
 
-/* Initializes my_hostname and my_pid. Done only once, so it's safe and
-   fast to call this function multiple times. */
+/* Initializes my_hostname and my_pid. */
 void hostpid_init(void);
 
+/* Returns the current host+domain, or if it fails fallback to returning
+   hostname. */
+const char *my_hostdomain(void);
+
 #endif