File indexing completed on 2024-10-06 12:17:42

0001 /*  -*- mode: C++; coding: utf-8; -*-
0002  *  Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
0003  *
0004  *
0005  *  Permission is hereby granted, free of charge, to any person obtaining
0006  *  a copy of this software and associated documentation files (the
0007  *  "Software"), to deal in the Software without restriction, including
0008  *  without limitation the rights to use, copy, modify, merge, publish,
0009  *  distribute, sublicense, and/or sell copies of the Software, and to
0010  *  permit persons to whom the Software is furnished to do so, subject to
0011  *  the following conditions:
0012  *
0013  *  The above copyright notice and this permission notice shall be included
0014  *  in all copies or substantial portions of the Software.
0015  *
0016  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0020  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0021  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0022  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023  */
0024 
0025 #ifndef KRESOLVER_H
0026 #define KRESOLVER_H
0027 
0028 //////////////////
0029 // Needed includes
0030 #include <QList>
0031 #include <QObject>
0032 #include <QSharedDataPointer>
0033 #include "k3socketaddress.h"
0034 #include "kdemacros.h"
0035 
0036 ////////////////////////
0037 // Forward declarations
0038 struct sockaddr;
0039 class QString;
0040 class QByteArray;
0041 template<typename T> class QSet;
0042 
0043 //////////////////
0044 // Our definitions
0045 
0046 namespace KNetwork
0047 {
0048 
0049 namespace Internal
0050 {
0051 class KResolverManager;
0052 }
0053 
0054 class KResolverEntryPrivate;
0055 /** @class KResolverEntry k3resolver.h k3resolver.h
0056  *  @brief One resolution entry.
0057  *
0058  * This class is one element in the resolution results list.
0059  * It contains the socket address for connecting, as well as
0060  * a bit more of information: the socket type, address family
0061  * and protocol numbers.
0062  *
0063  * This class contains all the information required for creating,
0064  * binding and connecting a socket.
0065  *
0066  * KResolverEntry objects implicitly share data, so copying them
0067  * is quite efficient.
0068  *
0069  * @author Thiago Macieira <thiago@kde.org>
0070  * @deprecated Use KSocketFactory or KLocalSocket instead
0071  */
0072 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KResolverEntry
0073 {
0074 public:
0075     /**
0076      * Default constructor
0077      *
0078      */
0079     KResolverEntry();
0080 
0081     /**
0082      * Constructs a new KResolverEntry from a KSocketAddress
0083      * and other data.
0084      *
0085      * The KSocketAddress @p addr parameter will be deep-copied.
0086      *
0087      * @param addr    the address that was resolved
0088      * @param socktype    the socket type of the resolved address
0089      * @param protocol    the protocol of the resolved address
0090      * @param canonName   the canonical name of the resolved hostname
0091      * @param encodedName the ASCII-compatible encoding of the hostname
0092      */
0093     KResolverEntry(const KSocketAddress &addr, int socktype, int protocol,
0094                    const QString &canonName = QString(),
0095                    const QByteArray &encodedName = QByteArray());
0096 
0097     /**
0098      * Constructs a new KResolverEntry from raw forms of
0099      * socket addresses and other data.
0100      *
0101      * This constructor instead creates an internal KSocketAddress object.
0102      *
0103      * @param sa      the sockaddr structure containing the raw address
0104      * @param salen   the length of the sockaddr structure
0105      * @param socktype    the socket type of the resolved address
0106      * @param protocol    the protocol of the resolved address
0107      * @param canonName   the canonical name of the resolved hostname
0108      * @param encodedName the ASCII-compatible encoding of the hostname
0109      */
0110     KResolverEntry(const struct sockaddr *sa, quint16 salen, int socktype,
0111                    int protocol, const QString &canonName = QString(),
0112                    const QByteArray &encodedName = QByteArray());
0113 
0114     /**
0115      * Copy constructor.
0116      *
0117      * This constructor performs a shallow-copy of the other object.
0118      */
0119     KResolverEntry(const KResolverEntry &other);
0120 
0121     /**
0122      * Destructor.
0123      *
0124      * The destructor frees associated resources with this object. It does
0125      * not destroy shared data.
0126      */
0127     ~KResolverEntry();
0128 
0129     /**
0130      * Retrieves the socket address associated with this entry.
0131      */
0132     KSocketAddress address() const;
0133 
0134     /**
0135      * Retrieves the length of the socket address structure.
0136      */
0137     quint16 length() const;
0138 
0139     /**
0140      * Retrieves the family associated with this socket address.
0141      */
0142     int family() const;
0143 
0144     /**
0145      * Retrieves the canonical name associated with this entry, if there is any.
0146      * If the canonical name was not found, this function returns QString().
0147      */
0148     QString canonicalName() const;
0149 
0150     /**
0151      * Retrieves the encoded domain name associated with this entry, if there is
0152      * any. If this domain has been resolved through DNS, this will be the
0153      * the ACE-encoded hostname.
0154      *
0155      * Returns a null QByteArray if such information is not available.
0156      *
0157      * Please note that this information is NOT to be presented to the user,
0158      * unless requested.
0159      */
0160     QByteArray encodedName() const;
0161 
0162     /**
0163      * Retrieves the socket type associated with this entry.
0164      */
0165     int socketType() const;
0166 
0167     /**
0168      * Retrieves the protocol associated with this entry.
0169      */
0170     int protocol() const;
0171 
0172     /**
0173      * Assignment operator
0174      *
0175      * This function copies the contents of the other object into this one.
0176      * Data will be shared between the two of them.
0177      */
0178     KResolverEntry &operator=(const KResolverEntry &other);
0179 
0180     /**
0181      * Dummy operator== for compilers which need a complete
0182      * instantiated class when exporting to a shared lib
0183      */
0184     KDE_DUMMY_COMPARISON_OPERATOR(KResolverEntry)
0185 
0186 private:
0187     QSharedDataPointer<KResolverEntryPrivate> d;
0188 };
0189 
0190 KDE_DUMMY_QHASH_FUNCTION(KResolverEntry)
0191 
0192 class KResolverResultsPrivate;
0193 /**
0194  * @class KResolverResults k3resolver.h k3resolver.h
0195  * @brief Name and service resolution results.
0196  *
0197  * This object contains the results of a name and service resolution, as
0198  * those performed by KResolver. It is also a descendant of QValueList, so
0199  * you may use all its member functions here to access the elements.
0200  *
0201  * A KResolverResults object is associated with a resolution, so, in addition
0202  * to the resolved elements, you can also retrieve information about the
0203  * resolution process itself, like the nodename that was resolved or an error
0204  * code.
0205  *
0206  * Note Resolver also uses KResolverResults objects to indicate failure, so
0207  * you should test for failure.
0208  *
0209  * @author Thiago Macieira <thiago@kde.org>
0210  * @deprecated Use KSocketFactory or KLocalSocket instead
0211  */
0212 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KResolverResults: public QList<KResolverEntry>
0213 {
0214 public:
0215     /**
0216      * Default constructor.
0217      *
0218      * Constructs an empty list.
0219      */
0220     KResolverResults();
0221 
0222     /**
0223      * Copy constructor
0224      *
0225      * Creates a new object with the contents of the other one. Data will be
0226      * shared by the two objects, like QValueList
0227      */
0228     KResolverResults(const KResolverResults &other);
0229 
0230     /**
0231      * Destructor
0232      *
0233      * Destroys the object and frees associated resources.
0234      */
0235     virtual ~KResolverResults();
0236 
0237     /**
0238      * Assignment operator
0239      *
0240      * Copies the contents of the other container into this one, discarding
0241      * our current values.
0242      */
0243     KResolverResults &operator=(const KResolverResults &other);
0244 
0245     /**
0246      * Retrieves the error code associated with this resolution. The values
0247      * here are the same as in KResolver::ErrorCodes.
0248      */
0249     int error() const;
0250 
0251     /**
0252      * Retrieves the system error code, if any.
0253      * @see KResolver::systemError for more information
0254      */
0255     int systemError() const;
0256 
0257     /**
0258      * Sets the error codes
0259      *
0260      * @param errorcode       the error code in KResolver::ErrorCodes
0261      * @param systemerror the system error code associated, if any
0262      */
0263     void setError(int errorcode, int systemerror = 0);
0264 
0265     /**
0266      * The nodename to which the resolution was performed.
0267      */
0268     QString nodeName() const;
0269 
0270     /**
0271      * The service name to which the resolution was performed.
0272      */
0273     QString serviceName() const;
0274 
0275     /**
0276      * Sets the new nodename and service name
0277      */
0278     void setAddress(const QString &host, const QString &service);
0279 
0280 protected:
0281     /** Standard hack to add virtuals later. @internal */
0282     virtual void virtual_hook(int id, void *data);
0283 private:
0284     QSharedDataPointer<KResolverResultsPrivate> d;
0285 };
0286 
0287 class KResolverPrivate;
0288 /**
0289  * @class KResolver k3resolver.h k3resolver.h
0290  * @brief Name and service resolution class.
0291  *
0292  * This class provides support for doing name-to-binary resolution
0293  * for nodenames and service ports. You should use this class if you
0294  * need specific resolution techniques when creating a socket or if you
0295  * want to inspect the results before calling the socket functions.
0296  *
0297  * You can either create an object and set the options you want in it
0298  * or you can simply call the static member functions, which will create
0299  * standard Resolver objects and dispatch the resolution for you. Normally,
0300  * the static functions will be used, except in cases where specific options
0301  * must be set.
0302  *
0303  * A Resolver object defaults to the following:
0304  * @li address family: any address family
0305  * @li socket type: streaming socket
0306  * @li protocol: implementation-defined. Generally, TCP
0307  * @li host and service: unset
0308  *
0309  * @author Thiago Macieira <thiago@kde.org>
0310  * @deprecated Use KSocketFactory or KLocalSocket instead
0311  */
0312 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KResolver: public QObject
0313 {
0314     Q_OBJECT
0315 
0316 public:
0317 
0318     /**
0319      * Address family selection types
0320      *
0321      * These values can be OR-ed together to form a composite family selection.
0322      *
0323      * @li UnknownFamily: a family that is unknown to the current implementation
0324      * @li KnownFamily: a family that is known to the implementation (the exact
0325      *        opposite of UnknownFamily)
0326      * @li AnyFamilies: any address family is acceptable
0327      * @li InternetFamily: an address for connecting to the Internet
0328      * @li InetFamily: alias for InternetFamily
0329      * @li IPv6Family: an IPv6 address only
0330      * @li IPv4Family: an IPv4 address only
0331      * @li UnixFamily: an address for the local Unix namespace (i.e., Unix sockets)
0332      * @li LocalFamily: alias for UnixFamily
0333      */
0334     enum SocketFamilies {
0335         UnknownFamily = 0x0001,
0336 
0337         UnixFamily = 0x0002,
0338         LocalFamily = UnixFamily,
0339 
0340         IPv4Family = 0x0004,
0341         IPv6Family = 0x0008,
0342         InternetFamily = IPv4Family | IPv6Family,
0343         InetFamily = InternetFamily,
0344 
0345         KnownFamily = ~UnknownFamily,
0346         AnyFamily = KnownFamily | UnknownFamily
0347     };
0348 
0349     /**
0350      * Flags for the resolution.
0351      *
0352      * These flags are used for setting the resolution behaviour for this
0353      * object:
0354      * @li Passive: resolve to a passive socket (i.e., one that can be used for
0355      *        binding to a local interface)
0356      * @li CanonName: request that the canonical name for the given nodename
0357      *        be found and recorded
0358      * @li NoResolve: request that no external resolution be performed. The given
0359      *        nodename and servicename will be resolved locally only.
0360      * @li NoSrv: don't try to use SRV-based name-resolution.
0361      * @li Multiport: the port/service argument is a list of port numbers and
0362      *        ranges. (future extension)
0363      *
0364      * @note SRV-based lookup and Multiport are not implemented yet.
0365      */
0366     enum Flags {
0367         Passive = 0x01,
0368         CanonName = 0x02,
0369         NoResolve = 0x04,
0370         NoSrv = 0x08,
0371         Multiport = 0x10
0372     };
0373 
0374     /**
0375      * Error codes
0376      *
0377      * These are the possible error values that objects of this class
0378      * may return. See errorString() for getting a string representation
0379      * for these errors.
0380      *
0381      * @li AddrFamily: Address family for the given nodename is not supported.
0382      * @li TryAgain: Temporary failure in name resolution. You should try again.
0383      * @li NonRecoverable: Non-recoverable failure in name resolution.
0384      * @li BadFlags: Invalid flags were given.
0385      * @li Memory: Memory allocation failure.
0386      * @li NoName: The specified name or service doesn't exist.
0387      * @li UnsupportedFamily: The requested socket family is not supported.
0388      * @li UnsupportedService: The requested service is not supported for this
0389      *        socket type (i.e., a datagram service in a streaming socket).
0390      * @li UnsupportedSocketType: The requested socket type is not supported.
0391      * @li UnknownError: An unknown, unexpected error occurred.
0392      * @li SystemError: A system error occurred. See systemError().
0393      * @li Canceled: This request was canceled by the user.
0394      */
0395     enum ErrorCodes {
0396         // note: if you change this enum, take a look at KResolver::errorString
0397         NoError = 0,
0398         AddrFamily = -1,
0399         TryAgain = -2,
0400         NonRecoverable = -3,
0401         BadFlags = -4,
0402         Memory = -5,
0403         NoName = -6,
0404         UnsupportedFamily = -7,
0405         UnsupportedService = -8,
0406         UnsupportedSocketType = -9,
0407         UnknownError = -10,
0408         SystemError = -11,
0409         Canceled = -100
0410     };
0411 
0412     /**
0413      * Status codes.
0414      *
0415      * These are the possible status for a Resolver object. A value
0416      * greater than zero indicates normal behaviour, while negative
0417      * values either indicate failure or error.
0418      *
0419      * @li Idle: resolution has not yet been started.
0420      * @li Queued: resolution is queued but not yet in progress.
0421      * @li InProgress: resolution is in progress.
0422      * @li PostProcessing: resolution is in progress.
0423      * @li Success: resolution is done; you can retrieve the results.
0424      * @li Canceled: request canceled by the user.
0425      * @li Failed: resolution is done, but failed.
0426      *
0427      * Note: the status Canceled and the error code Canceled are the same.
0428      *
0429      * Note 2: the status Queued and InProgress might not be distinguishable.
0430      * Some implementations might not differentiate one from the other.
0431      */
0432     enum StatusCodes {
0433         Idle = 0,
0434         Queued = 1,
0435         InProgress = 5,
0436         PostProcessing = 6,
0437         Success = 10,
0438         //Canceled = -100,    // already defined above
0439         Failed = -101
0440     };
0441 
0442     /**
0443      * Default constructor.
0444      *
0445      * Creates an empty Resolver object. You should set the wanted
0446      * names and flags using the member functions before starting
0447      * the name resolution.
0448      *
0449      * @param parent the parent object (see QObject)
0450      */
0451     KResolver(QObject *parent = nullptr);
0452 
0453     /**
0454      * Constructor with host and service names.
0455      *
0456      * Creates a Resolver object with the given host and
0457      * service names. Flags are initialised to 0 and any address family
0458      * will be accepted.
0459      *
0460      * @param nodename    the host name we want resolved
0461      * @param servicename the service name associated, like "http"
0462      * @param parent      the parent object (see QObject)
0463      */
0464     KDELIBS4SUPPORT_DEPRECATED explicit KResolver(const QString &nodename, const QString &servicename = QString(),
0465                        QObject *parent = nullptr);
0466 
0467     /**
0468      * Destructor.
0469      *
0470      * When this object is deleted, it'll destroy all associated
0471      * resources. If the resolution is still in progress, it will be
0472      * canceled and the signal will \b not be emitted.
0473      */
0474     ~KResolver() override;
0475 
0476     /**
0477      * Retrieve the current status of this object.
0478      *
0479      * @see StatusCodes for the possible status codes.
0480      */
0481     int status() const;
0482 
0483     /**
0484      * Retrieve the error code in this object.
0485      *
0486      * This function will return NoError if we are not in
0487      * an error condition. See status() and StatusCodes to
0488      * find out what the current status is.
0489      *
0490      * @see errorString for getting a textual representation of
0491      * this error
0492      */
0493     int error() const;
0494 
0495     /**
0496      * Retrieve the associated system error code in this object.
0497      *
0498      * Many resolution operations may generate an extra error code
0499      * as given by the C errno variable. That value is stored in the
0500      * object and can be retrieved by this function.
0501      */
0502     int systemError() const;
0503 
0504     /**
0505      * Returns the textual representation of the error in this object.
0506      */
0507     QString errorString() const;
0508 
0509     /**
0510      * Returns true if this object is currently running
0511      */
0512     bool isRunning() const;
0513 
0514     /**
0515      * The nodename to which the resolution was/is to be performed.
0516      */
0517     QString nodeName() const;
0518 
0519     /**
0520      * The service name to which the resolution was/is to be performed.
0521      */
0522     QString serviceName() const;
0523 
0524     /**
0525      * Sets the nodename for the resolution.
0526      *
0527      * Set the nodename to QString() to unset it.
0528      * @param nodename        The nodename to be resolved.
0529      */
0530     void setNodeName(const QString &nodename);
0531 
0532     /**
0533      * Sets the service name to be resolved.
0534      *
0535      * Set it to QString() to unset it.
0536      * @param service     The service to be resolved.
0537      */
0538     void setServiceName(const QString &service);
0539 
0540     /**
0541      * Sets both the host and the service names.
0542      *
0543      * Setting either value to QString() will unset them.
0544      * @param node        The nodename
0545      * @param service     The service name
0546      */
0547     void setAddress(const QString &node, const QString &service);
0548 
0549     /**
0550      * Retrieves the flags set for the resolution.
0551      *
0552      * @see Flags for an explanation on what flags are possible
0553      */
0554     int flags() const;
0555 
0556     /**
0557      * Sets the flags.
0558      *
0559      * @param flags       the new flags
0560      * @return            the old flags
0561      * @see Flags for an explanation on the flags
0562      */
0563     int setFlags(int flags);
0564 
0565     /**
0566      * Sets the allowed socket families.
0567      *
0568      * @param families        the families that we want/accept
0569      * @see SocketFamilies for possible values
0570      */
0571     void setFamily(int families);
0572 
0573     /**
0574      * Sets the socket type we want.
0575      *
0576      * The values for the @p type parameter are the SOCK_*
0577      * constants, defined in <sys/socket.h>. The most common
0578      * values are:
0579      *  @li SOCK_STREAM       streaming socket (= reliable, sequenced,
0580      *                connection-based)
0581      *  @li SOCK_DGRAM        datagram socket (= unreliable, connectionless)
0582      *  @li SOCK_RAW      raw socket, with direct access to the
0583      *                container protocol (such as IP)
0584      *
0585      * These three are the only values to which it is guaranteed that
0586      * resolution will work. Some systems may define other constants (such as
0587      * SOCK_RDM for reliable datagrams), but support is implementation-defined.
0588      *
0589      * @param type        the wanted socket type (SOCK_* constants). Set
0590      *                0 to use the default.
0591      */
0592     void setSocketType(int type);
0593 
0594     /**
0595      * Sets the protocol we want.
0596      *
0597      * Protocols are dependent on the selected address family, so you should know
0598      * what you are doing if you use this function. Besides, protocols generally
0599      * are either stream-based or datagram-based, so the value of the socket
0600      * type is also important. The resolution will fail if these values don't match.
0601      *
0602      * When using an Internet socket, the values for the protocol are the
0603      * IPPROTO_* constants, defined in <netinet/in.h>.
0604      *
0605      * You may choose to set the protocol either by its number or by its name, or
0606      * by both. If you set:
0607      * @li the number and the name: both values will be stored internally; you
0608      *        may set the name to an empty value, if wanted
0609      * @li the number only (name = NULL): the name will be searched in the
0610      *        protocols database
0611      * @li the name only (number = 0): the number will be searched in the
0612      *        database
0613      * @li neither name nor number: reset to default behaviour
0614      *
0615      * @param protonum        the protocol number we want
0616      * @param name        the protocol name
0617      */
0618     void setProtocol(int protonum, const char *name = nullptr);
0619 
0620     /**
0621      * Starts the name resolution asynchronously.
0622      *
0623      * This function will queue this object for resolution
0624      * and will return immediately. The status upon exit will either be
0625      * Queued or InProgress or Failed.
0626      *
0627      * This function does nothing if the object is already queued. But if
0628      * it had already succeeded or failed, this function will re-start it.
0629      *
0630      * Note: if both the nodename and the servicename are unset, this function
0631      * will not queue, but will set a success state and emit the signal. Also
0632      * note that in this case and maybe others, the signal finished() might
0633      * be emitted before this function returns.
0634      *
0635      * @return true if this request was successfully queued for asynchronous
0636      *        resolution
0637      */
0638     bool start();
0639 
0640     /**
0641      * Waits for a request to finish resolving.
0642      *
0643      * This function will wait on a running request for its termination. The
0644      * status upon exit will either be Success or Failed or Canceled.
0645      *
0646      * This function may be called from any thread, even one that is not the
0647      * GUI thread or the one that started the resolution process. But note this
0648      * function is not thread-safe nor reentrant: i.e., only one thread can be
0649      * waiting on one given object.
0650      *
0651      * Also note that this function ensures that the finished() signal is
0652      * emitted before it returns. That means that, as a side-effect, whenever
0653      * wait() is called, the signal is emitted on the thread calling wait().
0654      *
0655      * @param msec        the time to wait, in milliseconds or 0 to
0656      *                wait forever
0657      * @return true if the resolution has finished processing, even when it
0658      *         failed or was canceled. False means the wait timed out and
0659      *         the resolution is still running.
0660      */
0661     bool wait(int msec = 0);
0662 
0663     /**
0664      * Cancels a running request
0665      *
0666      * This function will cancel a running request. If the request is not
0667      * currently running or queued, this function does nothing.
0668      *
0669      * Note: if you tell the signal to be emitted, be aware that it might
0670      * or might not be emitted before this function returns.
0671      *
0672      * @param emitSignal  whether to emit the finished() signal or not
0673      */
0674     void cancel(bool emitSignal = true);
0675 
0676     /**
0677      * Retrieves the results of this resolution
0678      *
0679      * Use this function to retrieve the results of the resolution. If no
0680      * data was resolved (yet) or if we failed, this function will return
0681      * an empty object.
0682      *
0683      * @return the resolved data
0684      * @see status for information on finding out if the resolution was successful
0685      */
0686     KResolverResults results() const;
0687 
0688     /**
0689      * Handles events. Reimplemented from QObject.
0690      *
0691      * This function handles the events generated by the manager indicating that
0692      * this object has finished processing.
0693      *
0694      * Do not post events to this object.
0695      */
0696     bool event(QEvent *) override;
0697 
0698 Q_SIGNALS:
0699     // signals
0700 
0701     /**
0702      * This signal is emitted whenever the resolution is finished, one
0703      * way or another (success or failure). The @p results parameter
0704      * will contain the resolved data.
0705      *
0706      * Note: if you are doing multiple resolutions, you can use the
0707      * QObject::sender() function to distinguish one Resolver object from
0708      * another.
0709      *
0710      * @param results     the resolved data; might be empty if the resolution
0711      *            failed
0712      * @see results for information on what the results are
0713      *
0714      * @note This signal is @b always delivered in the GUI event thread, even for
0715      *       resolutions that were started in secondary threads.
0716      */
0717     void finished(const KNetwork::KResolverResults &results);
0718 
0719 private:
0720     void emitFinished();
0721 
0722 public:
0723     // Static functions
0724 
0725     /**
0726      * Returns the string representation of this error code.
0727      *
0728      * @param errorcode   the error code. See ErrorCodes.
0729      * @param syserror    the system error code associated.
0730      * @return        the string representation. This is already
0731      *            i18n'ed.
0732      */
0733     static QString errorString(int errorcode, int syserror = 0);
0734 
0735     /**
0736      * Resolve the nodename and service name synchronously
0737      *
0738      * This static function is provided as convenience for simplifying
0739      * name resolution. It resolves the given host and service names synchronously
0740      * and returns the results it found. It is equivalent to the following code:
0741      *
0742      * \code
0743      *   KResolver qres(host, service);
0744      *   qres.setFlags(flags);
0745      *   qres.setFamily(families)
0746      *   qres.start();
0747      *   qres.wait();
0748      *   return qres.results();
0749      * \endcode
0750      *
0751      * @param host        the nodename to resolve
0752      * @param service     the service to resolve
0753      * @param flags       flags to be used
0754      * @param families        the families to be searched
0755      * @return a KResolverResults object containing the results
0756      * @see KResolverResults for information on how to obtain the error code
0757      */
0758     static KResolverResults resolve(const QString &host, const QString &service,
0759                                     int flags = 0, int families = KResolver::InternetFamily);
0760 
0761     /**
0762      * Start an asynchronous name resolution
0763      *
0764      * This function is provided as a convenience to simplify the resolution
0765      * process. It creates an internal KResolver object, connects the
0766      * finished() signal to the given slot and starts the resolution
0767      * asynchronously. It is more or less equivalent to the following code:
0768      *
0769      * \b Note: this function may trigger the signal before it returns, so
0770      * your code must be prepared for this situation.
0771      *
0772      * \code
0773      *   KResolver* qres = new KResolver(host, service);
0774      *   QObject::connect(qres, SIGNAL(finished(const KNetwork::KResolverResults&)),
0775      *                userObj, userSlot);
0776      *   qres->setFlags(flags);
0777      *   qres->setFamily(families);
0778      *   return qres->start();
0779      * \endcode
0780      *
0781      * You should use it like this in your code:
0782      * \code
0783      *   KResolver::resolveAsync(myObj, SLOT(mySlot(KResolverResults)), host, service);
0784      * \endcode
0785      *
0786      * @param userObj     the object whose slot @p userSlot we will connect
0787      * @param userSlot        the slot to which we'll connect
0788      * @param host        the nodename to resolve
0789      * @param service     the service to resolve
0790      * @param flags       flags to be used
0791      * @param families        families to be searcheed
0792      * @return true if the queuing was successful, false if not
0793      * @see KResolverResults for information on how to obtain the error code
0794      */
0795     static bool resolveAsync(QObject *userObj, const char *userSlot,
0796                              const QString &host, const QString &service,
0797                              int flags = 0, int families = KResolver::InternetFamily);
0798 
0799     /**
0800      * Returns the domain name in an ASCII Compatible Encoding form, suitable
0801      * for DNS lookups. This is the base for International Domain Name support
0802      * over the Internet.
0803      *
0804      * Note this function may fail, in which case it'll return a null
0805      * QByteArray. Reasons for failure include use of unknown code
0806      * points (Unicode characters).
0807      *
0808      * Note that the encoding is illegible and, thus, should not be presented
0809      * to the user, except if requested.
0810      *
0811      * @param unicodeDomain   the domain name to be encoded
0812      * @return the ACE-encoded suitable for DNS queries if successful, a null
0813      *         QByteArray if failure.
0814      */
0815     static QByteArray domainToAscii(const QString &unicodeDomain);
0816 
0817     /**
0818      * Does the inverse of domainToAscii() and return an Unicode domain
0819      * name from the given ACE-encoded domain.
0820      *
0821      * This function may fail if the given domain cannot be successfully
0822      * converted back to Unicode. Reasons for failure include a malformed
0823      * domain name or good ones whose reencoding back to ACE don't match
0824      * the form given here (e.g., ACE-encoding of an already
0825      * ASCII-compatible domain).
0826      *
0827      * It is, however, guaranteed that domains returned
0828      * by domainToAscii() will work.
0829      *
0830      * @param asciiDomain the ACE-encoded domain name to be decoded
0831      * @return the Unicode representation of the given domain name
0832      * if successful, the original string if not
0833      * @note ACE = ASCII-Compatible Encoding, i.e., 7-bit
0834      */
0835     static QString domainToUnicode(const QByteArray &asciiDomain);
0836 
0837     /**
0838      * The same as above, but taking a QString argument.
0839      *
0840      * @param asciiDomain the ACE-encoded domain name to be decoded
0841      * @return the Unicode representation of the given domain name
0842      * if successful, QString() if not.
0843      */
0844     static QString domainToUnicode(const QString &asciiDomain);
0845 
0846     /**
0847      * Normalise a domain name.
0848      *
0849      * In order to prevent simple mistakes in International Domain
0850      * Names (IDN), it has been decided that certain code points
0851      * (characters in Unicode) would be instead converted to others.
0852      * This includes turning them all to lower case, as well certain
0853      * other specific operations, as specified in the documents.
0854      *
0855      * For instance, the German 'ß' will be changed into 'ss', while
0856      * the micro symbol 'µ' will be changed to the Greek mu 'μ'.
0857      *
0858      * Two equivalent domains have the same normalised form. And the
0859      * normalised form of a normalised domain is itself (i.e., if
0860      * d is normalised, the following is true: d == normalizeDomain(d) )
0861      *
0862      * This operation is equivalent to encoding and the decoding a Unicode
0863      * hostname.
0864      *
0865      * @param domain      a domain to be normalised
0866      * @return the normalised domain, or QString() if the domain is
0867      * invalid.
0868      */
0869     static QString normalizeDomain(const QString &domain);
0870 
0871     /**
0872      * Resolves a protocol number to its names
0873      *
0874      * Note: the returned QStrList operates on deep-copies.
0875      *
0876      * @param protonum    the protocol number to be looked for
0877      * @return all the protocol names in a list. The first is the "proper"
0878      *        name.
0879      */
0880     static QList<QByteArray> protocolName(int protonum);
0881 
0882     /**
0883      * Finds all aliases for a given protocol name
0884      *
0885      * @param protoname   the protocol name to be looked for
0886      * @return all the protocol names in a list. The first is the "proper"
0887      *        name.
0888      */
0889     static QList<QByteArray> protocolName(const char *protoname);
0890 
0891     /**
0892      * Resolves a protocol name to its number
0893      *
0894      * @param protoname   the protocol name to be looked for
0895      * @return the protocol number or -1 if we couldn't locate it
0896      */
0897     static int protocolNumber(const char *protoname);
0898 
0899     /**
0900      * Resolves a service name to its port number
0901      *
0902      * @param servname        the service name to be looked for
0903      * @param protoname       the protocol it is associated with
0904      * @return the port number in host byte-order or -1 in case of error
0905      */
0906     static int servicePort(const char *servname, const char *protoname);
0907 
0908     /**
0909      * Finds all the aliases for a given service name
0910      *
0911      * Note: the returned QList<QByteArray> operates on deep-copies.
0912      *
0913      * @param servname        the service alias to be looked for
0914      * @param protoname       the protocol it is associated with
0915      * @return all the service names in a list. The first is the "proper"
0916      *        name.
0917      */
0918     static QList<QByteArray> serviceName(const char *servname, const char *protoname);
0919 
0920     /**
0921      * Resolves a port number to its names
0922      *
0923      * Note: the returned QList<QByteArray> operates on deep copies.
0924      *
0925      * @param port        the port number, in host byte-order
0926      * @param protoname       the protocol it is associated with
0927      * @return all the service names in a list. The first is the "proper"
0928      *        name.
0929      */
0930     static QList<QByteArray> serviceName(int port, const char *protoname);
0931 
0932     /**
0933      * Returns this machine's local hostname.
0934      *
0935      * @return this machine's local hostname
0936      */
0937     static QString localHostName();
0938 
0939 protected:
0940 
0941     /**
0942      * Sets the error codes
0943      */
0944     void setError(int errorcode, int systemerror = 0);
0945 
0946     /** Standard hack to add virtuals later. @internal */
0947     virtual void virtual_hook(int id, void *data);
0948 private:
0949     KResolverPrivate *const d;
0950     friend class KResolverResults;
0951     friend class ::KNetwork::Internal::KResolverManager;
0952 };
0953 
0954 }               // namespace KNetwork
0955 
0956 #endif