rehash since gethostname is used in resolver as well
authorHeikki Lindholm <holin@iki.fi>
Fri, 9 Apr 2010 20:24:34 +0000 (20:24 +0000)
committerHeikki Lindholm <holin@iki.fi>
Fri, 9 Apr 2010 20:24:34 +0000 (20:24 +0000)
src/hostlist/hostlist-server.c
src/include/gnunet_os_lib.h
src/util/resolver_api.c

index f3f92e9fb1c2330b178c3bb79218b75630c106d1..6b35269269e450b33a6f22698a7931b8d4ebfebf 100644 (file)
@@ -104,11 +104,6 @@ struct HostSet
   char *data;
 };
 
-/**
- * Local max hostname length (some platforms use sysconf() for it)
- */
-static int max_hostname_length;
-
 /**
  * Task that will produce a new response object.
  */
@@ -405,11 +400,11 @@ adv_create_message ( const struct GNUNET_PeerIdentity * peer,
   unsigned long long port;
 
   char *uri;
-  char hostname[max_hostname_length + 1];
+  char hostname[GNUNET_OS_get_hostname_max_length() + 1];
   char *protocol = "http://";
   char *port_s = GNUNET_malloc(6 * sizeof(char));
 
-  if (0 != gethostname (hostname, sizeof (hostname)))
+  if (0 != gethostname (hostname, sizeof (hostname) - 1))
   {
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
         "Could not get system's hostname, unable to create advertisement message");
@@ -618,16 +613,6 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
 {
   unsigned long long port;
 
-#if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
-  max_hostname_length = sysconf(_SC_HOST_NAME_MAX);
-  if (-1 == max_hostname_length)
-    return GNUNET_SYSERR;
-#elif defined(HOST_NAME_MAX)
-  max_hostname_length = HOST_NAME_MAX;
-#else
-  max_hostname_length = 255; /* sensible default? */
-#endif
-
   sched = s;
   cfg = c;
   stats = st;
index 32d9dc7ff5fb659d8903447ba69034be4dd7ed8d..3aa1f1bad68e4963011629e9bf68ae423656e847 100644 (file)
@@ -154,6 +154,18 @@ typedef int (*GNUNET_OS_NetworkInterfaceProcessor) (void *cls,
 void GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor
                                         proc, void *proc_cls);
 
+/**
+ * @brief Get maximum string length returned by gethostname()
+ */
+#if HAVE_SYSCONF && defined(_SC_HOST_NAME_MAX)
+#define GNUNET_OS_get_hostname_max_length() ({ int __sc_tmp = sysconf(_SC_HOST_NAME_MAX); __sc_tmp <= 0 ? 255 : __sc_tmp; })
+#elif defined(HOST_NAME_MAX)
+#define GNUNET_OS_get_hostname_max_length() HOST_NAME_MAX
+#else
+#define GNUNET_OS_get_hostname_max_length() 255
+#endif
+
+
 /**
  * Get the current CPU load.
  *
index ecd3789f437ba9dbf3d994af3b7ea45c497c1baf..a35f680d309ba6260d784a79c9c2e6fec07eec41 100644 (file)
@@ -25,6 +25,7 @@
  */
 #include "platform.h"
 #include "gnunet_getopt_lib.h"
+#include "gnunet_os_lib.h"
 #include "gnunet_client_lib.h"
 #include "gnunet_protocols.h"
 #include "gnunet_resolver_service.h"
@@ -686,7 +687,7 @@ GNUNET_RESOLVER_hostname_resolve (struct GNUNET_SCHEDULER_Handle *sched,
                                   GNUNET_RESOLVER_AddressCallback callback,
                                   void *cls)
 {
-  char hostname[MAX_HOSTNAME];
+  char hostname[GNUNET_OS_get_hostname_max_length() + 1];
 
   check_config (cfg);
   if (0 != gethostname (hostname, sizeof (hostname) - 1))