$OpenBSD: patch-kdecore_network_kresolver_cpp,v 1.4 2005/02/27 07:25:51 brad Exp $
--- kdecore/network/kresolver.cpp.orig	Sun Nov 28 12:30:44 2004
+++ kdecore/network/kresolver.cpp	Sun Feb 27 02:18:36 2005
@@ -32,6 +32,7 @@
 #include <time.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
+#include <stdlib.h>
 
 // Qt includes
 #include <qapplication.h>
@@ -58,6 +59,10 @@
 #include "kresolver_p.h"
 #include "ksocketaddress.h"
 
+#ifdef __OpenBSD__
+#define USE_OPENBSD 1
+#endif
+
 using namespace KNetwork;
 using namespace KNetwork::Internal;
 
@@ -622,17 +627,28 @@ QStrList KResolver::protocolName(int pro
   pe = getprotobynumber(protonum);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct protoent protobuf;
+  struct protoent_data pdata;
+  ::memset(&pdata, sizeof pdata, 0);
+
+  if (getprotobynumber_r(protonum, &protobuf, &pdata) == 0)
+    pe = &protobuf;
+  else
+    pe = 0;
+
+# else
   size_t buflen = 1024;
   struct protoent protobuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobynumber_r which returns struct *protoent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobynumber_r which returns struct *protoent or NULL
       if ((pe = getprotobynumber_r(protonum, &protobuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getprotobynumber_r(protonum, &protobuf, buf, buflen, &pe) == ERANGE)
-# endif
+#  endif
 	{
 	  buflen += 1024;
 	  delete [] buf;
@@ -641,6 +657,7 @@ QStrList KResolver::protocolName(int pro
 	break;
     }
   while (pe == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -653,7 +670,9 @@ QStrList KResolver::protocolName(int pro
     }
 
 #ifdef HAVE_GETPROTOBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
@@ -668,17 +687,27 @@ QStrList KResolver::protocolName(const c
   pe = getprotobyname(protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct protoent protobuf;
+  struct protoent_data pdata;
+  ::memset(&pdata, sizeof pdata, 0);
+
+  if (getprotobyname_r(protoname, &protobuf, &pdata) == 0)
+    pe = &protobuf;
+  else
+    pe = 0;
+# else
   size_t buflen = 1024;
   struct protoent protobuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
       if ((pe = getprotobyname_r(protoname, &protobuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getprotobyname_r(protoname, &protobuf, buf, buflen, &pe) == ERANGE)
-# endif
+#  endif
 	{
 	  buflen += 1024;
 	  delete [] buf;
@@ -687,6 +716,7 @@ QStrList KResolver::protocolName(const c
 	break;
     }
   while (pe == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -699,7 +729,9 @@ QStrList KResolver::protocolName(const c
     }
 
 #ifdef HAVE_GETPROTOBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
@@ -714,17 +746,28 @@ int KResolver::protocolNumber(const char
   pe = getprotobyname(protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct protoent protobuf;
+  struct protoent_data pdata;
+  ::memset(&pdata, sizeof pdata, 0);
+
+  if (getprotobyname_r(protoname, &protobuf, &pdata) == 0)
+    pe = &protobuf;
+  else
+    pe = 0;
+
+# else
   size_t buflen = 1024;
   struct protoent protobuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
       if ((pe = getprotobyname_r(protoname, &protobuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getprotobyname_r(protoname, &protobuf, buf, buflen, &pe) == ERANGE)
-# endif
+#  endif
 	{
 	  buflen += 1024;
 	  delete [] buf;
@@ -733,6 +776,7 @@ int KResolver::protocolNumber(const char
 	break;
     }
   while (pe == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -741,7 +785,9 @@ int KResolver::protocolNumber(const char
     protonum = pe->p_proto;
 
 #ifdef HAVE_GETPROTOBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return protonum;
@@ -756,17 +802,27 @@ int KResolver::servicePort(const char *s
   se = getservbyname(servname, protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct servent servbuf;
+  struct servent_data sdata;
+  ::memset(&sdata, sizeof sdata, 0);
+  if (getservbyname_r(servname, protoname, &servbuf, &sdata) == 0)
+    se = &servbuf;
+  else
+    se = 0;
+
+# else
   size_t buflen = 1024;
   struct servent servbuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
       if ((se = getservbyname_r(servname, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getservbyname_r(servname, protoname, &servbuf, buf, buflen, &se) == ERANGE)
-# endif
+#  endif
 	{
 	  buflen += 1024;
 	  delete [] buf;
@@ -775,6 +831,7 @@ int KResolver::servicePort(const char *s
 	break;
     }
   while (se == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -783,7 +840,9 @@ int KResolver::servicePort(const char *s
     servport = ntohs(se->s_port);
 
 #ifdef HAVE_GETSERVBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return servport;
@@ -798,17 +857,27 @@ QStrList KResolver::serviceName(const ch
   se = getservbyname(servname, protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct servent servbuf;
+  struct servent_data sdata;
+  ::memset(&sdata, sizeof sdata, 0);
+  if (getservbyname_r(servname, protoname, &servbuf, &sdata) == 0)
+    se = &servbuf;
+  else
+    se = 0;
+
+# else
   size_t buflen = 1024;
   struct servent servbuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
       if ((se = getservbyname_r(servname, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getservbyname_r(servname, protoname, &servbuf, buf, buflen, &se) == ERANGE)
-# endif
+#  endif
 	{
 	  buflen += 1024;
 	  delete [] buf;
@@ -817,6 +886,7 @@ QStrList KResolver::serviceName(const ch
 	break;
     }
   while (se == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -829,7 +899,9 @@ QStrList KResolver::serviceName(const ch
     }
 
 #ifdef HAVE_GETSERVBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
@@ -844,17 +916,27 @@ QStrList KResolver::serviceName(int port
   se = getservbyport(port, protoname);
 
 #else
+# ifdef USE_OPENBSD // OpenBSD uses an HP/IBM/DEC API
+  struct servent servbuf;
+  struct servent_data sdata;
+  ::memset(&sdata, sizeof sdata, 0);
+  if (getservbyport_r(port, protoname, &servbuf, &sdata) == 0)
+    se = &servbuf;
+  else
+    se = 0;
+
+# else
   size_t buflen = 1024;
   struct servent servbuf;
   char *buf;
   do
     {
       buf = new char[buflen];
-# ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyport_r which returns struct *servent or NULL
+#  ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyport_r which returns struct *servent or NULL
       if ((se = getservbyport_r(port, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
-# else
+#  else
       if (getservbyport_r(port, protoname, &servbuf, buf, buflen, &se) == ERANGE)
-# endif
+#  endif
 	{
 	  buflen += 1024;
 	  delete [] buf;
@@ -863,6 +945,7 @@ QStrList KResolver::serviceName(int port
 	break;
     }
   while (se == 0L);
+# endif
 #endif
 
   // Do common processing
@@ -875,7 +958,9 @@ QStrList KResolver::serviceName(int port
     }
 
 #ifdef HAVE_GETSERVBYNAME_R
+# ifndef USE_OPENBSD
   delete [] buf;
+# endif
 #endif
 
   return lst;
@@ -889,6 +974,9 @@ static QString ToUnicode(const QString& 
 // implement the ToAscii function, as described by IDN documents
 QCString KResolver::domainToAscii(const QString& unicodeDomain)
 {
+  if (getenv("KDE_USE_IDN") == 0L)
+    return unicodeDomain.latin1();
+
   QCString retval;
   // RFC 3490, section 4 describes the operation:
   // 1) this is a query, so don't allow unassigned
@@ -928,6 +1016,8 @@ QString KResolver::domainToUnicode(const
 {
   if (asciiDomain.isEmpty())
     return asciiDomain;
+  if (getenv("KDE_USE_IDN") == 0L)
+    return asciiDomain;;
 
   QString retval;
 
