File indexing completed on 2024-04-14 14:19:50

0001 /*  -*- C++ -*-
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 KREVERSERESOLVER_H
0026 #define KREVERSERESOLVER_H
0027 
0028 //////////////////
0029 // Needed includes
0030 #include <QObject>
0031 #include <QString>
0032 
0033 #include "k3socketaddress.h"
0034 
0035 namespace KNetwork
0036 {
0037 
0038 class KReverseResolverPrivate;
0039 /** @class KReverseResolver k3reverseresolver.h k3reverseresolver.h
0040  *  @brief Run a reverse-resolution on a socket address.
0041  *
0042  * This class is provided as a counterpart to KResolver in such a way
0043  * as it produces a reverse resolution: it resolves a socket address
0044  * from its binary representations into a textual representation.
0045  *
0046  * Most users will use the static functions resolve(), which work
0047  * both synchronously (blocking) and asynchronously (non-blocking).
0048  *
0049  * @author Thiago Macieira <thiago@kde.org>
0050  * @deprecated Use KSocketFactory or KLocalSocket instead
0051  */
0052 class KDELIBS4SUPPORT_DEPRECATED_EXPORT KReverseResolver: public QObject
0053 {
0054     Q_OBJECT
0055 
0056 public:
0057     /**
0058      * Flags for the reverse resolution.
0059      *
0060      * These flags are used by the reverse resolution functions for
0061      * setting resolution parameters. The possible values are:
0062      * @li NumericHost: don't try to resolve the host address to a text form.
0063      *        Instead, convert the address to its numeric textual representation.
0064      * @li NumericService: the same as NumericHost, but for the service name
0065      * @li NodeNameOnly: returns the node name only (i.e., not the Fully
0066      *        Qualified Domain Name)
0067      * @li Datagram: in case of ambiguity in the service name, prefer the
0068      *        name associated with the datagram protocol
0069      * @li NumericScope: for those addresses which have the concept of scope,
0070      *            resolve using the numeric value instead of the proper scope name.
0071      * @li ResolutionRequired: normally, when resolving, if the name resolution
0072      *            fails, the process normally converts the numeric address into its
0073      *            presentation forms. This flag causes the function to return
0074      *            with error instead.
0075      */
0076     enum Flags {
0077         NumericHost = 0x01,
0078         NumericService = 0x02,
0079         NodeNameOnly = 0x04,
0080         Datagram = 0x08,
0081         NumericScope = 0x10,
0082         ResolutionRequired = 0x20
0083     };
0084 
0085     /**
0086      * Constructs this object to resolve the given socket address.
0087      *
0088      * @param addr        the address to resolve
0089      * @param flags       the flags to use, see Flags
0090      * @param parent      the parent object (see QObject)
0091      */
0092     KDELIBS4SUPPORT_DEPRECATED explicit KReverseResolver(const KSocketAddress &addr, int flags = 0,
0093                               QObject *parent = nullptr);
0094 
0095     /**
0096      * Destructor.
0097      */
0098     ~KReverseResolver() override;
0099 
0100     /**
0101      * This function returns 'true' if the processing is still running.
0102      */
0103     bool isRunning() const;
0104 
0105     /**
0106      * This function returns true if the processing has finished with
0107      * success, false if it's still running or failed.
0108      */
0109     bool success() const;
0110 
0111     /**
0112      * This function returns true if the processing has finished with
0113      * failure, false if it's still running or succeeded.
0114      */
0115     bool failure() const;
0116 
0117     /**
0118      * Returns the resolved node name, if the resolution has finished
0119      * successfully, or QString() otherwise.
0120      */
0121     QString node() const;
0122 
0123     /**
0124      * Returns the resolved service name, if the resolution has finished
0125      * successfully, or QString() otherwise.
0126      */
0127     QString service() const;
0128 
0129     /**
0130      * Returns the socket address which was subject to resolution.
0131      */
0132     const KSocketAddress &address() const;
0133 
0134     /**
0135      * Starts the resolution. This function returns 'true'
0136      * if the resolution has started successfully.
0137      */
0138     bool start();
0139 
0140     /**
0141      * Overrides event handling
0142      */
0143     bool event(QEvent *) override;
0144 
0145 Q_SIGNALS:
0146     /**
0147      * This signal is emitted when the resolution has finished.
0148      *
0149      * @param obj     this class, which contains the results
0150      */
0151     void finished(const KNetwork::KReverseResolver &obj);
0152 
0153 public:
0154     /**
0155      * Resolves a socket address to its textual representation
0156      *
0157      * FIXME!! How can we do this in a non-blocking manner!?
0158      *
0159      * This function is used to resolve a socket address from its
0160      * binary representation to a textual form, even if numeric only.
0161      *
0162      * @param addr    the socket address to be resolved
0163      * @param node    the QString where we will store the resolved node
0164      * @param serv    the QString where we will store the resolved service
0165      * @param flags   flags to be used for this resolution.
0166      * @return true if the resolution succeeded, false if not
0167      * @see ReverseFlags for the possible values for @p flags
0168      */
0169     static bool resolve(const KSocketAddress &addr, QString &node,
0170                         QString &serv, int flags = 0);
0171 
0172     /**
0173      * Resolves a socket address to its textual representation
0174      *
0175      * FIXME!! How can we do this in a non-blocking manner!?
0176      *
0177      * This function behaves just like the above one, except it takes
0178      * a sockaddr structure and its size as parameters.
0179      *
0180      * @param sa  the sockaddr structure containing the address to be resolved
0181      * @param salen   the length of the sockaddr structure
0182      * @param node    the QString where we will store the resolved node
0183      * @param serv    the QString where we will store the resolved service
0184      * @param flags   flags to be used for this resolution.
0185      * @return true if the resolution succeeded, false if not
0186      * @see ReverseFlags for the possible values for @p flags
0187      */
0188     static bool resolve(const struct sockaddr *sa, quint16 salen,
0189                         QString &node, QString &serv, int flags = 0);
0190 
0191 private:
0192     KReverseResolverPrivate *const d;
0193 };
0194 
0195 }               // namespace KNetwork
0196 
0197 #endif