kresolver.h
00001 /* -*- mode: C++; coding: utf-8; -*- 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 #ifndef KRESOLVER_H 00026 #define KRESOLVER_H 00027 00029 // Needed includes 00030 #include <qvaluelist.h> 00031 #include <qobject.h> 00032 #include "ksocketaddress.h" 00033 00034 00036 // Forward declarations 00037 struct sockaddr; 00038 class QString; 00039 class QCString; 00040 class QStrList; 00041 00043 // Our definitions 00044 00045 namespace KNetwork { 00046 00047 namespace Internal { class KResolverManager; } 00048 00049 class KResolverEntryPrivate; 00066 class KDECORE_EXPORT KResolverEntry 00067 { 00068 public: 00073 KResolverEntry(); 00074 00087 KResolverEntry(const KSocketAddress& addr, int socktype, int protocol, 00088 const QString& canonName = QString::null, 00089 const QCString& encodedName = QCString()); 00090 00104 KResolverEntry(const struct sockaddr *sa, Q_UINT16 salen, int socktype, 00105 int protocol, const QString& canonName = QString::null, 00106 const QCString& encodedName = QCString()); 00107 00113 KResolverEntry(const KResolverEntry &other); 00114 00121 ~KResolverEntry(); 00122 00126 KSocketAddress address() const; 00127 00131 Q_UINT16 length() const; 00132 00136 int family() const; 00137 00142 QString canonicalName() const; 00143 00154 QCString encodedName() const; 00155 00159 int socketType() const; 00160 00164 int protocol() const; 00165 00172 KResolverEntry& operator=(const KResolverEntry& other); 00173 00174 private: 00175 KResolverEntryPrivate* d; 00176 }; 00177 00178 class KResolverResultsPrivate; 00197 class KDECORE_EXPORT KResolverResults: public QValueList<KResolverEntry> 00198 { 00199 public: 00205 KResolverResults(); 00206 00213 KResolverResults(const KResolverResults& other); 00214 00220 virtual ~KResolverResults(); 00221 00228 KResolverResults& operator=(const KResolverResults& other); 00229 00234 int error() const; 00235 00240 int systemError() const; 00241 00248 void setError(int errorcode, int systemerror = 0); 00249 00253 QString nodeName() const; 00254 00258 QString serviceName() const; 00259 00263 void setAddress(const QString& host, const QString& service); 00264 00265 protected: 00266 virtual void virtual_hook( int id, void* data ); 00267 private: 00268 KResolverResultsPrivate* d; 00269 }; 00270 00271 class KResolverPrivate; 00295 class KDECORE_EXPORT KResolver: public QObject 00296 { 00297 Q_OBJECT 00298 00299 public: 00300 00317 enum SocketFamilies 00318 { 00319 UnknownFamily = 0x0001, 00320 00321 UnixFamily = 0x0002, 00322 LocalFamily = UnixFamily, 00323 00324 IPv4Family = 0x0004, 00325 IPv6Family = 0x0008, 00326 InternetFamily = IPv4Family | IPv6Family, 00327 InetFamily = InternetFamily, 00328 00329 KnownFamily = ~UnknownFamily, 00330 AnyFamily = KnownFamily | UnknownFamily 00331 }; 00332 00351 enum Flags 00352 { 00353 Passive = 0x01, 00354 CanonName = 0x02, 00355 NoResolve = 0x04, 00356 NoSrv = 0x08, 00357 Multiport = 0x10, 00358 UseSrv = 0x20 00359 }; 00360 00382 enum ErrorCodes 00383 { 00384 // note: if you change this enum, take a look at KResolver::errorString 00385 NoError = 0, 00386 AddrFamily = -1, 00387 TryAgain = -2, 00388 NonRecoverable = -3, 00389 BadFlags = -4, 00390 Memory = -5, 00391 NoName = -6, 00392 UnsupportedFamily = -7, 00393 UnsupportedService = -8, 00394 UnsupportedSocketType = -9, 00395 UnknownError = -10, 00396 SystemError = -11, 00397 Canceled = -100 00398 }; 00399 00420 enum StatusCodes 00421 { 00422 Idle = 0, 00423 Queued = 1, 00424 InProgress = 5, 00425 PostProcessing = 6, 00426 Success = 10, 00427 //Canceled = -100, // already defined above 00428 Failed = -101 00429 }; 00430 00438 KResolver(QObject * = 0L, const char * = 0L); 00439 00450 KResolver(const QString& nodename, const QString& servicename = QString::null, 00451 QObject * = 0L, const char * = 0L); 00452 00460 virtual ~KResolver(); 00461 00467 int status() const; 00468 00479 int error() const; 00480 00488 int systemError() const; 00489 00493 inline QString errorString() const 00494 { return errorString(error(), systemError()); } 00495 00499 bool isRunning() const; 00500 00504 QString nodeName() const; 00505 00509 QString serviceName() const; 00510 00517 void setNodeName(const QString& nodename); 00518 00525 void setServiceName(const QString& service); 00526 00534 void setAddress(const QString& node, const QString& service); 00535 00541 int flags() const; 00542 00550 int setFlags(int flags); 00551 00558 void setFamily(int families); 00559 00579 void setSocketType(int type); 00580 00605 void setProtocol(int protonum, const char *name = 0L); 00606 00625 bool start(); 00626 00648 bool wait(int msec = 0); 00649 00661 void cancel(bool emitSignal = true); 00662 00673 KResolverResults results() const; 00674 00683 virtual bool event(QEvent*); 00684 00685 signals: 00686 // signals 00687 00704 void finished(KResolverResults results); 00705 00706 private: 00707 void emitFinished(); 00708 00709 public: 00710 // Static functions 00711 00720 static QString errorString(int errorcode, int syserror = 0); 00721 00745 static KResolverResults resolve(const QString& host, const QString& service, 00746 int flags = 0, int families = KResolver::InternetFamily); 00747 00782 static bool resolveAsync(QObject* userObj, const char *userSlot, 00783 const QString& host, const QString& service, 00784 int flags = 0, int families = KResolver::InternetFamily); 00785 00802 static QCString domainToAscii(const QString& unicodeDomain); 00803 00822 static QString domainToUnicode(const QCString& asciiDomain); 00823 00831 static QString domainToUnicode(const QString& asciiDomain); 00832 00856 static QString normalizeDomain(const QString& domain); 00857 00867 static QStrList protocolName(int protonum); 00868 00876 static QStrList protocolName(const char *protoname); 00877 00884 static int protocolNumber(const char *protoname); 00885 00893 static int servicePort(const char *servname, const char *protoname); 00894 00905 static QStrList serviceName(const char *servname, const char *protoname); 00906 00917 static QStrList serviceName(int port, const char *protoname); 00918 00925 static QString localHostName(); 00926 00927 protected: 00928 00932 void setError(int errorcode, int systemerror = 0); 00933 00934 virtual void virtual_hook( int id, void* data ); 00935 private: 00936 KResolverPrivate* d; 00937 friend class KResolverResults; 00938 friend class ::KNetwork::Internal::KResolverManager; 00939 00940 static QStringList *idnDomains; 00941 }; 00942 00943 } // namespace KNetwork 00944 00945 #endif