]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
Fixed hostnamestr() for hosts that can't find themselves using gethostbyname() (Benoi...
authormortenp <none@none>
Wed, 6 Dec 2006 13:33:24 +0000 (00:33 +1100)
committermortenp <none@none>
Wed, 6 Dec 2006 13:33:24 +0000 (00:33 +1100)
ChangeLog
src/strgen.c

index a587a6e33d55036f5fe48fe3c8a881bff33a75cb..307c6047cf8dcb9c8e9d5da6b55c49c39bfc7ed6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,5 @@
+ o Fixed hostnamestr() for hosts that can't find themselves using
+   gethostbyname() (Benoit Dolez)
  o Add 'modnonsubposts' tunable that when set will moderate all posts
    from non subscribers
  o Fixed requeue for lists with noarchive enabled
index ed97f5f13ef59ccb6d3ddea9a81284d746f687f2..5dc65f39aaefad2645aae86762c975bdf84bfefd 100644 (file)
@@ -30,6 +30,7 @@
 #include <libgen.h>
 #include <time.h>
 #include <ctype.h>
+#include <errno.h>
 
 #include "strgen.h"
 #include "wrappers.h"
@@ -136,16 +137,50 @@ char *concatstr(int count, ...)
 
 char *hostnamestr()
 {
-        struct hostent *hostlookup;
-        char hostname[1024];
+       struct hostent *hostlookup;
+       char *hostname = NULL;
+       size_t len = 512;
+
+       for (;;) {
+               len *= 2;
+               free(hostname);
+
+               hostname = malloc(len);
+               hostname[len-1] = '\0';
+
+               /* gethostname() is allowed to:
+                * a) return -1 and undefined in hostname
+                * b) return 0 and an unterminated string in hostname
+                * c) return 0 and a NUL-terminated string in hostname
+                *
+                * We keep expanding the buffer until the hostname is
+                * NUL-terminated (and pray that it is not truncated)
+                * or an error occurs.
+                */
+               if (gethostname(hostname, len - 1)) {
+                       if (errno == ENAMETOOLONG) {
+                               continue;
+                       }
+                       myfree(hostname);
+                       return mystrdup("localhost");
+               }
+               
+               if (hostname[len-1] == '\0') {
+                       break;
+               }
+       }
 
-        /* TODO use dynamic allocation */
-        gethostname(hostname, sizeof(hostname) - 1);
-        /* gethostname() is allowed to return an unterminated string */
-        hostname[sizeof(hostname)-1] = '\0';
-        hostlookup = gethostbyname(hostname);
+       if (strchr(hostname, '.')) {
+               /* hostname is FQDN */
+               return hostname;
+       }
+
+       if ((hostlookup = gethostbyname(hostname))) {
+               myfree(hostname);
+               return mystrdup(hostlookup->h_name);
+       }
 
-        return mystrdup(hostlookup->h_name);
+       return hostname;
 }
 
 char *mydirname(const char *path)