Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Sep 2002 12:51:13 -0700 (PDT)
From:      Jeff Jirsa <jeff@unixconsults.com>
To:        Jev <jev@ecad.org>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: gethostbyname_r() fbsd equiv?
Message-ID:  <20020913123217.V83028-100000@boris.st.hmc.edu>
In-Reply-To: <20020913164315.GA42832@ecad.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 13 Sep 2002, Jev wrote:

> On Fri, Sep 13, 2002 at 09:34:31AM -0700, Jeff Jirsa wrote:
> [snip]
> > An alternative that you may which to consider is the vocal suite from
> > vovida.org. The license is similar to a BSD style license, will compile on
> > FreeBSD out of the box (use gmake rather than make), and likely has much
> > more 'features' than you'll ever want to use. I _think_ it requires the
> > GNU sip implementation (from http://www.gnu.org/software/osip/ ), but I'm
> > not 100% certain. It's likely that the vocal package will give you a firm
> > groundwork to expand, but unlikely that you'll want to use it "as-is" in
> > its present state.
> >
>
>
> Im allready using vocal 1.4.0 on a freebsd box :) And it is working
> pretty well, tho seems slightly bloated. I wanted to look at partysip to
> see what it was like, and port it while I was at it.
>
> I don't think it uses osip, but like you, im not 100% certain.
>


You can use the LGPL'd libcext_lgpl library from the vocal contrib to
supplement gethostbyname. Try the patch below to
<partysip>/ppl/unix/


You'll (perhaps I should, I don't really know) probably need to copy over
some of the LGPL/GPL license (http://www.gnu.org/copyleft/lesser.txt)  to
keep from breaking the respective licenses, but you can worry about that
if/when you distribute it.

Keep in touch, let me know if this works.

Good luck,
- Jeff Jirsa




--- ppldns.c.bak	Fri Sep 13 12:30:34 2002
+++ ppldns.c	Fri Sep 13 12:30:08 2002
@@ -27,6 +27,16 @@
 #include <osip/port.h>
 #include <osip/fifo.h>

+#include <pthread.h>
+#include <netdb.h>
+#include <string.h>
+#include "netdb_r.h"
+
+#ifdef __sparc__
+#define NEED_ALIGNED_ACCESS
+#endif
+
+
 fifo_t *dns_entries;            /* list of domain or FQDN strings to resolv */
 smutex_t *m_dns_result;
 ppl_dns_entry_t *dns_results;
@@ -668,12 +678,13 @@

   /* Jay changed according to TRU64 plateform */
   /* #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) */
+
 #if defined(_OSF_SOURCE)||defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
   gethostbyname_r (hostname,    /* the FQDN */
                    &result_buffer,      /* the result buffer */
                    &result);
   /* #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) */
-#elif defined(__sun__)||defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
+#elif defined(__sun__)||defined(HAVE_FUNC_GETHOSTBYNAME_R_5)||defined(__FreeBSD__)
   result = gethostbyname_r (hostname,   /* the FQDN */
                             &result_buffer,     /* the result buffer */
                             tmp, GETHOSTBYNAME_BUFLEN - 1, &my_error);
@@ -690,6 +701,7 @@
 #error        -DHAVE_FUNC_GETHOSTBYNAME_R_6 (3 arguments)
 #endif

+
   if (!result)
     return my_error;

@@ -822,14 +834,13 @@
   int my_error;

 /* Jay changed according to TRU64 plateform */
-
   /* #if defined(HAVE_FUNC_GETHOSTBYNAME_R_3) */
 #if defined(_OSF_SOURCE)||defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
   gethostbyname_r (hostname,    /* the FQDN */
                    &result_buffer,      /* the result buffer */
                    &result);
   /* #elif defined(HAVE_FUNC_GETHOSTBYNAME_R_5) */
-#elif defined(__sun__)||defined(HAVE_FUNC_GETHOSTBYNAME_R_5)
+#elif defined(__sun__)||defined(HAVE_FUNC_GETHOSTBYNAME_R_5)||defined(__FreeBSD__)
   result = gethostbyname_r (hostname,
                             &result_buffer,
                             tmp, GETHOSTBYNAME_BUFLEN - 1, &my_error);
@@ -987,3 +998,162 @@
 #endif

 #endif
+
+
+static pthread_mutex_t gethostby_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static int
+convert (struct hostent *host, struct hostent *result,
+       char *buf, int buflen, int *h_errnop)
+{
+  int len, i;
+
+  if (!buf || !h_errnop) return -1;
+  *h_errnop = h_errno;
+
+  *result = *host;
+  result->h_name = (char *) buf;
+  /* This is the size. */
+  len = strlen (host->h_name) + 1;
+  if (len > buflen) return -1;
+  buflen -= len;
+  buf += len;
+  strcpy ((char *) result->h_name, host->h_name);
+
+  /* How many aliases and how big the buffer should be? There
+     is always a NULL pointer. */
+  for (len = sizeof (char *), i = 0; host->h_aliases [i]; i++)
+  {
+    /* It should be size of (char *) and the length of string
+       plus 1. */
+    len += strlen (host->h_aliases [i]) + 1 + sizeof (char *);
+  }
+  if (len > buflen) return -1;
+  buflen -= len;
+
+  /* This is an array of char * for h_aliases. */
+#ifdef NEED_ALIGNED_ACCESS
+  {
+      int extra;
+      extra = 4 - (((unsigned long) buf) & 3);
+      if (extra != 4) {
+         if (buflen < extra)
+             return -1;
+         buf = (char *) buf + extra;
+      }
+  }
+#endif
+  result->h_aliases = (char **) buf;
+  buf += (i + 1) * sizeof (char *);
+
+  /* We copy the aliases now. */
+  for (i = 0; host->h_aliases [i]; i++)
+  {
+    result->h_aliases [i] = (char *) buf;
+    strcpy (result->h_aliases [i], host->h_aliases [i]);
+    buf += strlen (host->h_aliases [i]) + 1;
+  }
+  /* This is the last one */
+  result->h_aliases [i] = NULL;
+
+#if BSD >= 43 || defined(h_addr)
+  for (len = sizeof (char *), i = 0; host->h_addr_list [i]; i++)
+  {
+    /* It should be size of (char *) and the length of string
+       plus 1. */
+    len += host->h_length + sizeof (char *);
+  }
+  if (len > buflen) return -1;
+
+  /* This is an array of char * for h_addr_list. */
+#ifdef NEED_ALIGNED_ACCESS
+  {
+      int extra;
+      extra = 4 - (((unsigned long) buf) & 0x3);
+      if (extra != 4) {
+         if (buflen < extra)
+             return -1;
+         buf = ((char *) buf) + extra;
+      }
+  }
+#endif
+  result->h_addr_list = (char **) buf;
+  buf += (i + 1) * sizeof (char *);
+
+  /* We copy the h_addr_list now. */
+  for (i = 0; host->h_addr_list [i]; i++)
+  {
+    result->h_addr_list [i] = (char *) buf;
+    memcpy (result->h_addr_list [i], host->h_addr_list [i], host->h_length);
+    buf += host->h_length;
+  }
+  /* This is the last one */
+  result->h_addr_list [i] = NULL;
+#else
+  len = strlen (host->h_addr) + 1 + sizeof (char *);
+  if (len > buflen) return -1;
+
+  result->h_addr = (char *) buf;
+  strcpy (result->h_addr, host->h_addr);
+#endif
+  return 0;
+}
+
+struct hostent *
+gethostbyaddr_r (const char *addr, int length, int type,
+       struct hostent *result, char *buffer, int buflen,
+       int *h_errnop)
+{
+  struct hostent *host;
+
+  pthread_mutex_lock (&gethostby_mutex);
+
+  host = gethostbyaddr (addr, length, type);
+  if (!host ||
+       convert (host, result, buffer, buflen, h_errnop) != 0)
+  {
+    result = NULL;
+  }
+
+  pthread_mutex_unlock (&gethostby_mutex);
+  return result;
+}
+
+struct hostent *
+gethostbyname_r (const char *name,
+       struct hostent *result, char *buffer, int buflen,
+       int *h_errnop)
+{
+  struct hostent *host;
+
+  pthread_mutex_lock (&gethostby_mutex);
+
+  host = gethostbyname (name);
+  if (!host ||
+       convert (host, result, buffer, buflen, h_errnop) != 0)
+  {
+    result = NULL;
+  }
+
+  pthread_mutex_unlock (&gethostby_mutex);
+  return result;
+}
+
+struct hostent *
+gethostent_r (struct hostent *result, char *buffer, int buflen,
+       int *h_errnop)
+{
+  struct hostent *host;
+
+  pthread_mutex_lock (&gethostby_mutex);
+
+  host = gethostent ();
+  if (!host ||
+       convert (host, result, buffer, buflen, h_errnop) != 0)
+  {
+    result = NULL;
+  }
+
+  pthread_mutex_unlock (&gethostby_mutex);
+  return result;
+}

+++ netdb_r.h  Fri Sep 13 12:45:40 2002
@@ -0,0 +1,22 @@
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct hostent *
+gethostbyaddr_r (const char *addr, int length, int type,
+       struct hostent *result, char *buffer, int buflen,
+       int *h_errnop);
+
+struct hostent *
+gethostbyname_r (const char *name,
+       struct hostent *result, char *buffer, int buflen,
+       int *h_errnop);
+
+struct hostent *
+gethostent_r (struct hostent *result, char *buffer, int buflen,
+       int *h_errnop);
+
+#ifdef __cplusplus
+}
+#endif
+


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020913123217.V83028-100000>