kdecore Library API Documentation

kresolver.cpp

00001 /*  -*- C++ -*-
00002  *  Copyright (C) 2003-2005 Thiago Macieira <thiago.macieira@kdemail.net>
00003  *
00004  *
00005  *  Permission is hereby granted, free of charge, to any person obtaining
00006  *  a copy of this software and associated documentation files (the
00007  *  "Software"), to deal in the Software without restriction, including
00008  *  without limitation the rights to use, copy, modify, merge, publish,
00009  *  distribute, sublicense, and/or sell copies of the Software, and to
00010  *  permit persons to whom the Software is furnished to do so, subject to
00011  *  the following conditions:
00012  *
00013  *  The above copyright notice and this permission notice shall be included
00014  *  in all copies or substantial portions of the Software.
00015  *
00016  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00017  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00020  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00021  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00022  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023  */
00024 
00025 #include "config.h"
00026 
00027 // System includes
00028 #include <sys/types.h>
00029 #include <sys/socket.h>
00030 #include <errno.h>
00031 #include <netdb.h>
00032 #include <time.h>
00033 #include <arpa/inet.h>
00034 #include <netinet/in.h>
00035 #include <stdlib.h>
00036 
00037 // Qt includes
00038 #include <qapplication.h>
00039 #include <qstring.h>
00040 #include <qcstring.h>
00041 #include <qstrlist.h>
00042 #include <qstringlist.h>
00043 #include <qshared.h>
00044 #include <qdatetime.h>
00045 #include <qtimer.h>
00046 #include <qmutex.h>
00047 #include <qguardedptr.h>
00048 
00049 // IDN
00050 #ifdef HAVE_IDNA_H
00051 # include <stdlib.h>
00052 # include <idna.h>
00053 #endif
00054 
00055 // KDE
00056 #include <klocale.h>
00057 
00058 // Us
00059 #include "kresolver.h"
00060 #include "kresolver_p.h"
00061 #include "ksocketaddress.h"
00062 
00063 #ifdef NEED_MUTEX
00064 #warning "mutex"
00065 QMutex getXXbyYYmutex;
00066 #endif
00067 
00068 using namespace KNetwork;
00069 using namespace KNetwork::Internal;
00070 
00072 // class KResolverEntry
00073 
00074 class KNetwork::KResolverEntryPrivate: public QShared
00075 {
00076 public:
00077   KSocketAddress addr;
00078   int socktype;
00079   int protocol;
00080   QString canonName;
00081   QCString encodedName;
00082 
00083   inline KResolverEntryPrivate() :
00084     socktype(0), protocol(0)
00085   { }
00086 };
00087 
00088 // default constructor
00089 KResolverEntry::KResolverEntry() :
00090   d(0L)
00091 {
00092 }
00093 
00094 // constructor with stuff
00095 KResolverEntry::KResolverEntry(const KSocketAddress& addr, int socktype, int protocol,
00096                    const QString& canonName, const QCString& encodedName) :
00097   d(new KResolverEntryPrivate)
00098 {
00099   d->addr = addr;
00100   d->socktype = socktype;
00101   d->protocol = protocol;
00102   d->canonName = canonName;
00103   d->encodedName = encodedName;
00104 }
00105 
00106 // constructor with even more stuff
00107 KResolverEntry::KResolverEntry(const struct sockaddr* sa, Q_UINT16 salen, int socktype,
00108                    int protocol, const QString& canonName,
00109                    const QCString& encodedName) :
00110   d(new KResolverEntryPrivate)
00111 {
00112   d->addr = KSocketAddress(sa, salen);
00113   d->socktype = socktype;
00114   d->protocol = protocol;
00115   d->canonName = canonName;
00116   d->encodedName = encodedName;
00117 }
00118 
00119 // copy constructor
00120 KResolverEntry::KResolverEntry(const KResolverEntry& that) :
00121   d(0L)
00122 {
00123   *this = that;
00124 }
00125 
00126 // destructor
00127 KResolverEntry::~KResolverEntry()
00128 {
00129   if (d == 0L)
00130     return;
00131 
00132   if (d->deref())
00133     delete d;
00134 }
00135 
00136 // returns the socket address
00137 KSocketAddress KResolverEntry::address() const
00138 {
00139   return d ? d->addr : KSocketAddress();
00140 }
00141 
00142 // returns the length
00143 Q_UINT16 KResolverEntry::length() const
00144 {
00145   return d ? d->addr.length() : 0;
00146 }
00147 
00148 // returns the family
00149 int KResolverEntry::family() const
00150 {
00151   return d ? d->addr.family() : AF_UNSPEC;
00152 }
00153 
00154 // returns the canonical name
00155 QString KResolverEntry::canonicalName() const
00156 {
00157   return d ? d->canonName : QString::null;
00158 }
00159 
00160 // returns the encoded name
00161 QCString KResolverEntry::encodedName() const
00162 {
00163   return d ? d->encodedName : QCString();
00164 }
00165 
00166 // returns the socket type
00167 int KResolverEntry::socketType() const
00168 {
00169   return d ? d->socktype : 0;
00170 }
00171 
00172 // returns the protocol
00173 int KResolverEntry::protocol() const
00174 {
00175   return d ? d->protocol : 0;
00176 }
00177 
00178 // assignment operator
00179 KResolverEntry& KResolverEntry::operator= (const KResolverEntry& that)
00180 {
00181   // copy the data
00182   if (that.d)
00183     that.d->ref();
00184 
00185   if (d && d->deref())
00186     delete d;
00187 
00188   d = that.d;
00189   return *this;
00190 }
00191 
00193 // class KResolverResults
00194 
00195 class KNetwork::KResolverResultsPrivate
00196 {
00197 public:
00198   QString node, service;
00199   int errorcode, syserror;
00200 
00201   KResolverResultsPrivate() :
00202     errorcode(0), syserror(0)
00203   { }
00204 };
00205 
00206 // default constructor
00207 KResolverResults::KResolverResults()
00208   : d(new KResolverResultsPrivate)
00209 {
00210 }
00211 
00212 // copy constructor
00213 KResolverResults::KResolverResults(const KResolverResults& other)
00214   : QValueList<KResolverEntry>(other), d(new KResolverResultsPrivate)
00215 {
00216   *d = *other.d;
00217 }
00218 
00219 // destructor
00220 KResolverResults::~KResolverResults()
00221 {
00222   delete d;
00223 }
00224 
00225 // assignment operator
00226 KResolverResults&
00227 KResolverResults::operator= (const KResolverResults& other)
00228 {
00229   if (this == &other)
00230     return *this;
00231 
00232   // copy over the other data
00233   *d = *other.d;
00234 
00235   // now let QValueList do the rest of the work
00236   QValueList<KResolverEntry>::operator =(other);
00237 
00238   return *this;
00239 }
00240 
00241 // gets the error code
00242 int KResolverResults::error() const
00243 {
00244   return d->errorcode;
00245 }
00246 
00247 // gets the system errno
00248 int KResolverResults::systemError() const
00249 {
00250   return d->syserror;
00251 }
00252 
00253 // sets the error codes
00254 void KResolverResults::setError(int errorcode, int systemerror)
00255 {
00256   d->errorcode = errorcode;
00257   d->syserror = systemerror;
00258 }
00259 
00260 // gets the hostname
00261 QString KResolverResults::nodeName() const
00262 {
00263   return d->node;
00264 }
00265 
00266 // gets the service name
00267 QString KResolverResults::serviceName() const
00268 {
00269   return d->service;
00270 }
00271 
00272 // sets the address
00273 void KResolverResults::setAddress(const QString& node,
00274                   const QString& service)
00275 {
00276   d->node = node;
00277   d->service = service;
00278 }
00279 
00280 void KResolverResults::virtual_hook( int, void* )
00281 { /*BASE::virtual_hook( id, data );*/ }
00282 
00283 
00285 // class KResolver
00286 
00287 QStringList *KResolver::idnDomains = 0;
00288 
00289 
00290 // default constructor
00291 KResolver::KResolver(QObject *parent, const char *name)
00292   : QObject(parent, name), d(new KResolverPrivate(this))
00293 {
00294 }
00295 
00296 // constructor with host and service
00297 KResolver::KResolver(const QString& nodename, const QString& servicename,
00298            QObject *parent, const char *name)
00299   : QObject(parent, name), d(new KResolverPrivate(this, nodename, servicename))
00300 {
00301 }
00302 
00303 // destructor
00304 KResolver::~KResolver()
00305 {
00306   cancel(false);
00307   delete d;
00308 }
00309 
00310 // get the status
00311 int KResolver::status() const
00312 {
00313   return d->status;
00314 }
00315 
00316 // get the error code
00317 int KResolver::error() const
00318 {
00319   return d->errorcode;
00320 }
00321 
00322 // get the errno
00323 int KResolver::systemError() const
00324 {
00325   return d->syserror;
00326 }
00327 
00328 // are we running?
00329 bool KResolver::isRunning() const
00330 {
00331   return d->status > 0 && d->status < Success;
00332 }
00333 
00334 // get the hostname
00335 QString KResolver::nodeName() const
00336 {
00337   return d->input.node;
00338 }
00339 
00340 // get the service
00341 QString KResolver::serviceName() const
00342 {
00343   return d->input.service;
00344 }
00345 
00346 // sets the hostname
00347 void KResolver::setNodeName(const QString& nodename)
00348 {
00349   // don't touch those values if we're working!
00350   if (!isRunning())
00351     {
00352       d->input.node = nodename;
00353       d->status = Idle;
00354       d->results.setAddress(nodename, d->input.service);
00355     }
00356 }
00357 
00358 // sets the service
00359 void KResolver::setServiceName(const QString& service)
00360 {
00361   // don't change if running
00362   if (!isRunning())
00363     {
00364       d->input.service = service;
00365       d->status = Idle;
00366       d->results.setAddress(d->input.node, service);
00367     }
00368 }
00369 
00370 // sets the address
00371 void KResolver::setAddress(const QString& nodename, const QString& service)
00372 {
00373   setNodeName(nodename);
00374   setServiceName(service);
00375 }
00376 
00377 // get the flags
00378 int KResolver::flags() const
00379 {
00380   return d->input.flags;
00381 }
00382 
00383 // sets the flags
00384 int KResolver::setFlags(int flags)
00385 {
00386   int oldflags = d->input.flags;
00387   if (!isRunning())
00388     {
00389       d->input.flags = flags;
00390       d->status = Idle;
00391     }
00392   return oldflags;
00393 }
00394 
00395 // sets the family mask
00396 void KResolver::setFamily(int families)
00397 {
00398   if (!isRunning())
00399     {
00400       d->input.familyMask = families;
00401       d->status = Idle;
00402     }
00403 }
00404 
00405 // sets the socket type
00406 void KResolver::setSocketType(int type)
00407 {
00408   if (!isRunning())
00409     {
00410       d->input.socktype = type;
00411       d->status = Idle;
00412     }
00413 }
00414 
00415 // sets the protocol
00416 void KResolver::setProtocol(int protonum, const char *name)
00417 {
00418   if (isRunning())
00419     return;         // can't change now
00420 
00421   // we copy the given protocol name. If it isn't an empty string
00422   // and the protocol number was 0, we will look it up in /etc/protocols
00423   // we also leave the error reporting to the actual lookup routines, in
00424   // case the given protocol name doesn't exist
00425 
00426   d->input.protocolName = name;
00427   if (protonum == 0 && name != 0L && *name != '\0')
00428     {
00429       // must look up the protocol number
00430       d->input.protocol = KResolver::protocolNumber(name);
00431     }
00432   else
00433     d->input.protocol = protonum;
00434   d->status = Idle;
00435 }
00436 
00437 bool KResolver::start()
00438 {
00439   if (!isRunning())
00440     {
00441       d->results.empty();
00442 
00443       // is there anything to be queued?
00444       if (d->input.node.isEmpty() && d->input.service.isEmpty())
00445     {
00446       d->status = KResolver::Success;
00447       emitFinished();
00448     }
00449       else
00450     KResolverManager::manager()->enqueue(this, 0L);
00451     }
00452 
00453   return true;
00454 }
00455 
00456 bool KResolver::wait(int msec)
00457 {
00458   if (!isRunning())
00459     {
00460       emitFinished();
00461       return true;
00462     }
00463 
00464   QMutexLocker locker(&d->mutex);
00465 
00466   if (!isRunning())
00467     {
00468       // it was running and no longer is?
00469       // That means the manager has finished its processing and has posted
00470       // an event for the signal to be emitted already. This means the signal
00471       // will be emitted twice!
00472 
00473       emitFinished();
00474       return true;
00475     }
00476   else
00477     {
00478       QTime t;
00479       t.start();
00480 
00481       while (!msec || t.elapsed() < msec)
00482     {
00483       // wait on the manager to broadcast completion
00484       d->waiting = true;
00485       if (msec)
00486         KResolverManager::manager()->notifyWaiters.wait(&d->mutex, msec - t.elapsed());
00487       else
00488         KResolverManager::manager()->notifyWaiters.wait(&d->mutex);
00489 
00490       // the manager has processed
00491       // see if this object is done
00492       if (!isRunning())
00493         {
00494           // it's done
00495           d->waiting = false;
00496           emitFinished();
00497           return true;
00498         }
00499     }
00500 
00501       // if we've got here, we've timed out
00502       d->waiting = false;
00503       return false;
00504     }
00505 }
00506 
00507 void KResolver::cancel(bool emitSignal)
00508 {
00509   KResolverManager::manager()->dequeue(this);
00510   if (emitSignal)
00511     emitFinished();
00512 }
00513 
00514 KResolverResults
00515 KResolver::results() const
00516 {
00517   if (!isRunning())
00518     return d->results;
00519 
00520   // return a dummy, empty result
00521   KResolverResults r;
00522   r.setAddress(d->input.node, d->input.service);
00523   r.setError(d->errorcode, d->syserror);
00524   return r;
00525 }
00526 
00527 bool KResolver::event(QEvent* e)
00528 {
00529   if (static_cast<int>(e->type()) == KResolverManager::ResolutionCompleted)
00530     {
00531       emitFinished();
00532       return true;
00533     }
00534 
00535   return false;
00536 }
00537 
00538 void KResolver::emitFinished()
00539 {
00540   if (isRunning())
00541     d->status = KResolver::Success;
00542 
00543   QGuardedPtr<QObject> p = this; // guard against deletion
00544 
00545   emit finished(d->results);
00546 
00547   if (p && d->deleteWhenDone)
00548     deleteLater();      // in QObject
00549 }
00550 
00551 QString KResolver::errorString(int errorcode, int syserror)
00552 {
00553   // no i18n now...
00554   static const char * const messages[] =
00555   {
00556     I18N_NOOP("no error"),  // NoError
00557     I18N_NOOP("requested family not supported for this host name"), // AddrFamily
00558     I18N_NOOP("temporary failure in name resolution"),  // TryAgain
00559     I18N_NOOP("non-recoverable failure in name resolution"), // NonRecoverable
00560     I18N_NOOP("invalid flags"),         // BadFlags
00561     I18N_NOOP("memory allocation failure"), // Memory
00562     I18N_NOOP("name or service not known"), // NoName
00563     I18N_NOOP("requested family not supported"),    // UnsupportedFamily
00564     I18N_NOOP("requested service not supported for this socket type"), // UnsupportedService
00565     I18N_NOOP("requested socket type not supported"),   // UnsupportedSocketType
00566     I18N_NOOP("unknown error"),         // UnknownError
00567     I18N_NOOP2("1: the i18n'ed system error code, from errno",
00568           "system error: %1")       // SystemError
00569   };
00570 
00571   // handle the special value
00572   if (errorcode == Canceled)
00573     return i18n("request was canceled");
00574 
00575   if (errorcode > 0 || errorcode < SystemError)
00576     return QString::null;
00577 
00578   QString msg = i18n(messages[-errorcode]);
00579   if (errorcode == SystemError)
00580     msg.arg(QString::fromLocal8Bit(strerror(syserror)));
00581 
00582   return msg;
00583 }
00584 
00585 KResolverResults
00586 KResolver::resolve(const QString& host, const QString& service, int flags,
00587           int families)
00588 {
00589   KResolver qres(host, service, qApp, "synchronous KResolver");
00590   qres.setFlags(flags);
00591   qres.setFamily(families);
00592   qres.start();
00593   qres.wait();
00594   return qres.results();
00595 }
00596 
00597 bool KResolver::resolveAsync(QObject* userObj, const char *userSlot,
00598                  const QString& host, const QString& service,
00599                  int flags, int families)
00600 {
00601   KResolver* qres = new KResolver(host, service, qApp, "asynchronous KResolver");
00602   QObject::connect(qres, SIGNAL(finished(KResolverResults)), userObj, userSlot);
00603   qres->setFlags(flags);
00604   qres->setFamily(families);
00605   qres->d->deleteWhenDone = true; // this is the only difference from the example code
00606   return qres->start();
00607 }
00608 
00609 QStrList KResolver::protocolName(int protonum)
00610 {
00611   struct protoent *pe;
00612 #ifndef HAVE_GETPROTOBYNAME_R
00613   QMutexLocker locker(&getXXbyYYmutex);
00614 
00615   pe = getprotobynumber(protonum);
00616 
00617 #else
00618   size_t buflen = 1024;
00619   struct protoent protobuf;
00620   char *buf;
00621   do
00622     {
00623       buf = new char[buflen];
00624 # ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobynumber_r which returns struct *protoent or NULL
00625       if ((pe = getprotobynumber_r(protonum, &protobuf, buf, buflen)) && (errno == ERANGE))
00626 # else
00627       if (getprotobynumber_r(protonum, &protobuf, buf, buflen, &pe) == ERANGE)
00628 # endif
00629     {
00630       buflen += 1024;
00631       delete [] buf;
00632     }
00633       else
00634     break;
00635     }
00636   while (pe == 0L);
00637 #endif
00638 
00639   // Do common processing
00640   QStrList lst(true);   // use deep copies
00641   if (pe != NULL)
00642     {
00643       lst.append(pe->p_name);
00644       for (char **p = pe->p_aliases; *p; p++)
00645     lst.append(*p);
00646     }
00647 
00648 #ifdef HAVE_GETPROTOBYNAME_R
00649   delete [] buf;
00650 #endif
00651 
00652   return lst;
00653 }
00654 
00655 QStrList KResolver::protocolName(const char *protoname)
00656 {
00657   struct protoent *pe;
00658 #ifndef HAVE_GETPROTOBYNAME_R
00659   QMutexLocker locker(&getXXbyYYmutex);
00660 
00661   pe = getprotobyname(protoname);
00662 
00663 #else
00664   size_t buflen = 1024;
00665   struct protoent protobuf;
00666   char *buf;
00667   do
00668     {
00669       buf = new char[buflen];
00670 # ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
00671       if ((pe = getprotobyname_r(protoname, &protobuf, buf, buflen)) && (errno == ERANGE))
00672 # else
00673       if (getprotobyname_r(protoname, &protobuf, buf, buflen, &pe) == ERANGE)
00674 # endif
00675     {
00676       buflen += 1024;
00677       delete [] buf;
00678     }
00679       else
00680     break;
00681     }
00682   while (pe == 0L);
00683 #endif
00684 
00685   // Do common processing
00686   QStrList lst(true);   // use deep copies
00687   if (pe != NULL)
00688     {
00689       lst.append(pe->p_name);
00690       for (char **p = pe->p_aliases; *p; p++)
00691     lst.append(*p);
00692     }
00693 
00694 #ifdef HAVE_GETPROTOBYNAME_R
00695   delete [] buf;
00696 #endif
00697 
00698   return lst;
00699 }
00700 
00701 int KResolver::protocolNumber(const char *protoname)
00702 {
00703   struct protoent *pe;
00704 #ifndef HAVE_GETPROTOBYNAME_R
00705   QMutexLocker locker(&getXXbyYYmutex);
00706 
00707   pe = getprotobyname(protoname);
00708 
00709 #else
00710   size_t buflen = 1024;
00711   struct protoent protobuf;
00712   char *buf;
00713   do
00714     {
00715       buf = new char[buflen];
00716 # ifdef USE_SOLARIS // Solaris uses a 4 argument getprotobyname_r which returns struct *protoent or NULL
00717       if ((pe = getprotobyname_r(protoname, &protobuf, buf, buflen)) && (errno == ERANGE))
00718 # else
00719       if (getprotobyname_r(protoname, &protobuf, buf, buflen, &pe) == ERANGE)
00720 # endif
00721     {
00722       buflen += 1024;
00723       delete [] buf;
00724     }
00725       else
00726     break;
00727     }
00728   while (pe == 0L);
00729 #endif
00730 
00731   // Do common processing
00732   int protonum = -1;
00733   if (pe != NULL)
00734     protonum = pe->p_proto;
00735 
00736 #ifdef HAVE_GETPROTOBYNAME_R
00737   delete [] buf;
00738 #endif
00739 
00740   return protonum;
00741 }
00742 
00743 int KResolver::servicePort(const char *servname, const char *protoname)
00744 {
00745   struct servent *se;
00746 #ifndef HAVE_GETSERVBYNAME_R
00747   QMutexLocker locker(&getXXbyYYmutex);
00748 
00749   se = getservbyname(servname, protoname);
00750 
00751 #else
00752   size_t buflen = 1024;
00753   struct servent servbuf;
00754   char *buf;
00755   do
00756     {
00757       buf = new char[buflen];
00758 # ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
00759       if ((se = getservbyname_r(servname, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
00760 # else
00761       if (getservbyname_r(servname, protoname, &servbuf, buf, buflen, &se) == ERANGE)
00762 # endif
00763     {
00764       buflen += 1024;
00765       delete [] buf;
00766     }
00767       else
00768     break;
00769     }
00770   while (se == 0L);
00771 #endif
00772 
00773   // Do common processing
00774   int servport = -1;
00775   if (se != NULL)
00776     servport = ntohs(se->s_port);
00777 
00778 #ifdef HAVE_GETSERVBYNAME_R
00779   delete [] buf;
00780 #endif
00781 
00782   return servport;
00783 }
00784 
00785 QStrList KResolver::serviceName(const char* servname, const char *protoname)
00786 {
00787   struct servent *se;
00788 #ifndef HAVE_GETSERVBYNAME_R
00789   QMutexLocker locker(&getXXbyYYmutex);
00790 
00791   se = getservbyname(servname, protoname);
00792 
00793 #else
00794   size_t buflen = 1024;
00795   struct servent servbuf;
00796   char *buf;
00797   do
00798     {
00799       buf = new char[buflen];
00800 # ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyname_r which returns struct *servent or NULL
00801       if ((se = getservbyname_r(servname, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
00802 # else
00803       if (getservbyname_r(servname, protoname, &servbuf, buf, buflen, &se) == ERANGE)
00804 # endif
00805     {
00806       buflen += 1024;
00807       delete [] buf;
00808     }
00809       else
00810     break;
00811     }
00812   while (se == 0L);
00813 #endif
00814 
00815   // Do common processing
00816   QStrList lst(true);   // use deep copies
00817   if (se != NULL)
00818     {
00819       lst.append(se->s_name);
00820       for (char **p = se->s_aliases; *p; p++)
00821     lst.append(*p);
00822     }
00823 
00824 #ifdef HAVE_GETSERVBYNAME_R
00825   delete [] buf;
00826 #endif
00827 
00828   return lst;
00829 }
00830 
00831 QStrList KResolver::serviceName(int port, const char *protoname)
00832 {
00833   struct servent *se;
00834 #ifndef HAVE_GETSERVBYPORT_R
00835   QMutexLocker locker(&getXXbyYYmutex);
00836 
00837   se = getservbyport(port, protoname);
00838 
00839 #else
00840   size_t buflen = 1024;
00841   struct servent servbuf;
00842   char *buf;
00843   do
00844     {
00845       buf = new char[buflen];
00846 # ifdef USE_SOLARIS // Solaris uses a 5 argument getservbyport_r which returns struct *servent or NULL
00847       if ((se = getservbyport_r(port, protoname, &servbuf, buf, buflen)) && (errno == ERANGE))
00848 # else
00849       if (getservbyport_r(port, protoname, &servbuf, buf, buflen, &se) == ERANGE)
00850 # endif
00851     {
00852       buflen += 1024;
00853       delete [] buf;
00854     }
00855       else
00856     break;
00857     }
00858   while (se == 0L);
00859 #endif
00860 
00861   // Do common processing
00862   QStrList lst(true);   // use deep copies
00863   if (se != NULL)
00864     {
00865       lst.append(se->s_name);
00866       for (char **p = se->s_aliases; *p; p++)
00867     lst.append(*p);
00868     }
00869 
00870 #ifdef HAVE_GETSERVBYPORT_R
00871   delete [] buf;
00872 #endif
00873 
00874   return lst;
00875 }
00876 
00877 // forward declaration
00878 static QStringList splitLabels(const QString& unicodeDomain);
00879 static QCString ToASCII(const QString& label);
00880 static QString ToUnicode(const QString& label);
00881 
00882 static QStringList *KResolver_initIdnDomains()
00883 {
00884   const char *kde_use_idn = getenv("KDE_USE_IDN");
00885   if (!kde_use_idn)
00886      kde_use_idn = "at:br:ch:cn:de:dk:kr:jp:li:no:se:tw";
00887   return new QStringList(QStringList::split(':', QString::fromLatin1(kde_use_idn).lower()));
00888 }
00889 
00890 // implement the ToAscii function, as described by IDN documents
00891 QCString KResolver::domainToAscii(const QString& unicodeDomain)
00892 {
00893   if (!idnDomains)
00894     idnDomains = KResolver_initIdnDomains();
00895 
00896   QCString retval;
00897   // RFC 3490, section 4 describes the operation:
00898   // 1) this is a query, so don't allow unassigned
00899 
00900   // 2) split the domain into individual labels, without
00901   // separators.
00902   QStringList input = splitLabels(unicodeDomain);
00903 
00904   // Do we allow IDN names for this TLD?
00905   if (input.count() && !idnDomains->contains(input[input.count()-1].lower()))
00906     return input.join(".").lower().latin1(); // No IDN allowed for this TLD
00907 
00908   // 3) decide whether to enforce the STD3 rules for chars < 0x7F
00909   // we don't enforce
00910 
00911   // 4) for each label, apply ToASCII
00912   QStringList::Iterator it = input.begin();
00913   const QStringList::Iterator end = input.end();
00914   for ( ; it != end; ++it)
00915     {
00916       QCString cs = ToASCII(*it);
00917       if (cs.isNull())
00918     return QCString();  // error!
00919 
00920       // no, all is Ok.
00921       if (!retval.isEmpty())
00922     retval += '.';
00923       retval += cs;
00924     }
00925 
00926   return retval;
00927 }
00928 
00929 QString KResolver::domainToUnicode(const QCString& asciiDomain)
00930 {
00931   return domainToUnicode(QString::fromLatin1(asciiDomain));
00932 }
00933 
00934 // implement the ToUnicode function, as described by IDN documents
00935 QString KResolver::domainToUnicode(const QString& asciiDomain)
00936 {
00937   if (asciiDomain.isEmpty())
00938     return asciiDomain;
00939   if (!idnDomains)
00940     idnDomains = KResolver_initIdnDomains();
00941 
00942   QString retval;
00943 
00944   // draft-idn-idna-14.txt, section 4 describes the operation:
00945   // 1) this is a query, so don't allow unassigned
00946   //   besides, input is ASCII
00947 
00948   // 2) split the domain into individual labels, without
00949   // separators.
00950   QStringList input = splitLabels(asciiDomain);
00951 
00952   // Do we allow IDN names for this TLD?
00953   if (input.count() && !idnDomains->contains(input[input.count()-1].lower()))
00954     return asciiDomain.lower(); // No TLDs allowed
00955 
00956   // 3) decide whether to enforce the STD3 rules for chars < 0x7F
00957   // we don't enforce
00958 
00959   // 4) for each label, apply ToUnicode
00960   QStringList::Iterator it;
00961   const QStringList::Iterator end = input.end();
00962   for (it = input.begin(); it != end; ++it)
00963     {
00964       QString label = ToUnicode(*it).lower();
00965 
00966       // ToUnicode can't fail
00967       if (!retval.isEmpty())
00968     retval += '.';
00969       retval += label;
00970     }
00971 
00972   return retval;
00973 }
00974 
00975 QString KResolver::normalizeDomain(const QString& domain)
00976 {
00977   return domainToUnicode(domainToAscii(domain));
00978 }
00979 
00980 void KResolver::virtual_hook( int, void* )
00981 { /*BASE::virtual_hook( id, data );*/ }
00982 
00983 // here follows IDN functions
00984 // all IDN functions conform to the following documents:
00985 //  RFC 3454 - Preparation of Internationalized Strings
00986 //  RFC 3490 - Internationalizing Domain Names in Applications (IDNA)
00987 //  RFC 3491 - Nameprep: A Stringprep Profile for
00988 //                Internationalized Domain Names (IDN
00989 //  RFC 3492 - Punycode: A Bootstring encoding of Unicode
00990 //          for Internationalized Domain Names in Applications (IDNA)
00991 
00992 static QStringList splitLabels(const QString& unicodeDomain)
00993 {
00994   // From RFC 3490 section 3.1:
00995   // "Whenever dots are used as label separators, the following characters
00996   // MUST be recognized as dots: U+002E (full stop), U+3002 (ideographic full
00997   // stop), U+FF0E (fullwidth full stop), U+FF61 (halfwidth ideographic full
00998   // stop)."
00999   static const unsigned int separators[] = { 0x002E, 0x3002, 0xFF0E, 0xFF61 };
01000 
01001   QStringList lst;
01002   int start = 0;
01003   uint i;
01004   for (i = 0; i < unicodeDomain.length(); i++)
01005     {
01006       unsigned int c = unicodeDomain[i].unicode();
01007 
01008       if (c == separators[0] ||
01009       c == separators[1] ||
01010       c == separators[2] ||
01011       c == separators[3])
01012     {
01013       // found a separator!
01014       lst << unicodeDomain.mid(start, i - start);
01015       start = i + 1;
01016     }
01017     }
01018   if ((long)i >= start)
01019     // there is still one left
01020     lst << unicodeDomain.mid(start, i - start);
01021 
01022   return lst;
01023 }
01024 
01025 static QCString ToASCII(const QString& label)
01026 {
01027 #ifdef HAVE_IDNA_H
01028   // We do disable it by default for security reasons for now.
01029   if ( getenv("KDE_NO_IPV6") && strcmp( getenv("KDE_NO_IPV6"), "no" ) )
01030      return label.latin1();
01031 
01032   // We have idna.h, so we can use the idna_to_ascii
01033   // function :)
01034 
01035   if (label.length() > 64)
01036     return (char*)0L;       // invalid label
01037 
01038   if (label.length() == 0)
01039     // this is allowed
01040     return QCString("");    // empty, not null
01041 
01042   QCString retval;
01043   char buf[65];
01044 
01045   Q_UINT32* ucs4 = new Q_UINT32[label.length() + 1];
01046 
01047   uint i;
01048   for (i = 0; i < label.length(); i++)
01049     ucs4[i] = (unsigned long)label[i].unicode();
01050   ucs4[i] = 0;          // terminate with NUL, just to be on the safe side
01051 
01052   if (idna_to_ascii_4i(ucs4, label.length(), buf, 0) == IDNA_SUCCESS)
01053     // success!
01054     retval = buf;
01055 
01056   delete [] ucs4;
01057   return retval;
01058 #else
01059   return label.latin1();
01060 #endif
01061 }
01062 
01063 static QString ToUnicode(const QString& label)
01064 {
01065 #ifdef HAVE_IDNA_H
01066   // We do disable it by default for security reasons for now.
01067   if ( getenv("KDE_NO_IPV6") && strcmp( getenv("KDE_NO_IPV6"), "no" ) )
01068      return label;
01069 
01070   // We have idna.h, so we can use the idna_to_unicode
01071   // function :)
01072 
01073   Q_UINT32 *ucs4_input, *ucs4_output;
01074   size_t outlen;
01075 
01076   ucs4_input = new Q_UINT32[label.length() + 1];
01077   for (uint i = 0; i < label.length(); i++)
01078     ucs4_input[i] = (unsigned long)label[i].unicode();
01079 
01080   // try the same length for output
01081   ucs4_output = new Q_UINT32[outlen = label.length()];
01082 
01083   idna_to_unicode_44i(ucs4_input, label.length(),
01084               ucs4_output, &outlen,
01085               0);
01086 
01087   if (outlen > label.length())
01088     {
01089       // it must have failed
01090       delete [] ucs4_output;
01091       ucs4_output = new Q_UINT32[outlen];
01092 
01093       idna_to_unicode_44i(ucs4_input, label.length(),
01094               ucs4_output, &outlen,
01095               0);
01096     }
01097 
01098   // now set the answer
01099   QString result;
01100   result.setLength(outlen);
01101   for (uint i = 0; i < outlen; i++)
01102     result[i] = (unsigned int)ucs4_output[i];
01103 
01104   delete [] ucs4_input;
01105   delete [] ucs4_output;
01106 
01107   return result;
01108 #else
01109   return label;
01110 #endif
01111 }
01112 
01113 #include "kresolver.moc"
KDE Logo
This file is part of the documentation for kdecore Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Sep 13 03:28:26 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003